Add hppa*-*-hpux* to targets which do not support split DWARF
[official-gcc.git] / gcc / dwarf2out.cc
blob1c994bb8b9b1e8599c38f4f0c8daba869a5d4957
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2ctf.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tree-pretty-print.h"
87 #include "print-rtl.h"
88 #include "debug.h"
89 #include "common/common-target.h"
90 #include "langhooks.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
96 #include "rtl-iter.h"
97 #include "stringpool.h"
98 #include "attribs.h"
99 #include "file-prefix-map.h" /* remap_debug_filename() */
101 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
102 int, bool);
103 static rtx_insn *last_var_location_insn;
104 static rtx_insn *cached_next_real_insn;
105 static void dwarf2out_decl (tree);
106 static bool is_redundant_typedef (const_tree);
108 #ifndef XCOFF_DEBUGGING_INFO
109 #define XCOFF_DEBUGGING_INFO 0
110 #endif
112 #ifndef HAVE_XCOFF_DWARF_EXTRAS
113 #define HAVE_XCOFF_DWARF_EXTRAS 0
114 #endif
116 #ifdef VMS_DEBUGGING_INFO
117 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119 /* Define this macro to be a nonzero value if the directory specifications
120 which are output in the debug info should end with a separator. */
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
122 /* Define this macro to evaluate to a nonzero value if GCC should refrain
123 from generating indirect strings in DWARF2 debug information, for instance
124 if your target is stuck with an old version of GDB that is unable to
125 process them properly or uses VMS Debug. */
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
127 #else
128 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
130 #endif
132 /* ??? Poison these here until it can be done generically. They've been
133 totally replaced in this file; make sure it stays that way. */
134 #undef DWARF2_UNWIND_INFO
135 #undef DWARF2_FRAME_INFO
136 #if (GCC_VERSION >= 3000)
137 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
138 #endif
140 /* The size of the target's pointer type. */
141 #ifndef PTR_SIZE
142 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
143 #endif
145 /* Array of RTXes referenced by the debugging information, which therefore
146 must be kept around forever. */
147 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149 /* A pointer to the base of a list of incomplete types which might be
150 completed at some later time. incomplete_types_list needs to be a
151 vec<tree, va_gc> *because we want to tell the garbage collector about
152 it. */
153 static GTY(()) vec<tree, va_gc> *incomplete_types;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static unsigned macinfo_label_base = 1;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_ranges_dwo_section;
176 static GTY(()) section *debug_frame_section;
178 /* Maximum size (in bytes) of an artificially generated label. */
179 #define MAX_ARTIFICIAL_LABEL_BYTES 40
181 /* According to the (draft) DWARF 3 specification, the initial length
182 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
183 bytes are 0xffffffff, followed by the length stored in the next 8
184 bytes.
186 However, the SGI/MIPS ABI uses an initial length which is equal to
187 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189 #ifndef DWARF_INITIAL_LENGTH_SIZE
190 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
191 #endif
193 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
194 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
195 #endif
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
225 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227 typedef const char *compare_type;
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238 main object file, rather than the dwo, similar to the force_direct
239 parameter elsewhere but with additional complications:
241 1) The string is needed in both the main object file and the dwo.
242 That is, the comp_dir and dwo_name will appear in both places.
244 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
245 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247 3) GCC chooses the form to use late, depending on the size and
248 reference count.
250 Rather than forcing the all debug string handling functions and
251 callers to deal with these complications, simply use a separate,
252 special-cased string table for any attribute that should go in the
253 main object file. This limits the complexity to just the places
254 that need it. */
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258 static GTY(()) int dw2_string_counter;
260 /* True if the compilation unit places functions in more than one section. */
261 static GTY(()) bool have_multiple_function_sections = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* True if currently in text section. */
267 static GTY(()) bool in_text_section_p = false;
269 /* Last debug-on location in corresponding section. */
270 static GTY(()) const char *last_text_label;
271 static GTY(()) const char *last_cold_label;
273 /* Mark debug-on/off locations per section.
274 NULL means the section is not used at all. */
275 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
276 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278 /* The DIE for C++14 'auto' in a function return type. */
279 static GTY(()) dw_die_ref auto_die;
281 /* The DIE for C++14 'decltype(auto)' in a function return type. */
282 static GTY(()) dw_die_ref decltype_auto_die;
284 /* Forward declarations for functions defined in this file. */
286 static void output_call_frame_info (int);
288 /* Personality decl of current unit. Used only when assembler does not support
289 personality CFI. */
290 static GTY(()) rtx current_unit_personality;
292 /* Whether an eh_frame section is required. */
293 static GTY(()) bool do_eh_frame = false;
295 /* .debug_rnglists next index. */
296 static unsigned int rnglist_idx;
298 /* Data and reference forms for relocatable data. */
299 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
300 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302 #ifndef DEBUG_FRAME_SECTION
303 #define DEBUG_FRAME_SECTION ".debug_frame"
304 #endif
306 #ifndef FUNC_BEGIN_LABEL
307 #define FUNC_BEGIN_LABEL "LFB"
308 #endif
310 #ifndef FUNC_SECOND_SECT_LABEL
311 #define FUNC_SECOND_SECT_LABEL "LFSB"
312 #endif
314 #ifndef FUNC_END_LABEL
315 #define FUNC_END_LABEL "LFE"
316 #endif
318 #ifndef PROLOGUE_END_LABEL
319 #define PROLOGUE_END_LABEL "LPE"
320 #endif
322 #ifndef EPILOGUE_BEGIN_LABEL
323 #define EPILOGUE_BEGIN_LABEL "LEB"
324 #endif
326 #ifndef FRAME_BEGIN_LABEL
327 #define FRAME_BEGIN_LABEL "Lframe"
328 #endif
329 #define CIE_AFTER_SIZE_LABEL "LSCIE"
330 #define CIE_END_LABEL "LECIE"
331 #define FDE_LABEL "LSFDE"
332 #define FDE_AFTER_SIZE_LABEL "LASFDE"
333 #define FDE_END_LABEL "LEFDE"
334 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
335 #define LINE_NUMBER_END_LABEL "LELT"
336 #define LN_PROLOG_AS_LABEL "LASLTP"
337 #define LN_PROLOG_END_LABEL "LELTP"
338 #define DIE_LABEL_PREFIX "DW"
340 /* Match the base name of a file to the base name of a compilation unit. */
342 static bool
343 matches_main_base (const char *path)
345 /* Cache the last query. */
346 static const char *last_path = NULL;
347 static bool last_match = false;
348 if (path != last_path)
350 const char *base;
351 int length = base_of_path (path, &base);
352 last_path = path;
353 last_match = (length == main_input_baselength
354 && memcmp (base, main_input_basename, length) == 0);
356 return last_match;
359 #ifdef DEBUG_DEBUG_STRUCT
361 static bool
362 dump_struct_debug (tree type, enum debug_info_usage usage,
363 enum debug_struct_file criterion, int generic,
364 bool matches, bool result)
366 /* Find the type name. */
367 tree type_decl = TYPE_STUB_DECL (type);
368 tree t = type_decl;
369 const char *name = 0;
370 if (TREE_CODE (t) == TYPE_DECL)
371 t = DECL_NAME (t);
372 if (t)
373 name = IDENTIFIER_POINTER (t);
375 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
376 criterion,
377 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
378 matches ? "bas" : "hdr",
379 generic ? "gen" : "ord",
380 usage == DINFO_USAGE_DFN ? ";" :
381 usage == DINFO_USAGE_DIR_USE ? "." : "*",
382 result,
383 (void*) type_decl, name);
384 return result;
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 dump_struct_debug (type, usage, criterion, generic, matches, result)
389 #else
391 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
392 (result)
394 #endif
396 /* Get the number of HOST_WIDE_INTs needed to represent the precision
397 of the number. */
399 static unsigned int
400 get_full_len (const dw_wide_int &op)
402 return CEIL (op.get_precision (), HOST_BITS_PER_WIDE_INT);
405 static bool
406 should_emit_struct_debug (tree type, enum debug_info_usage usage)
408 if (debug_info_level <= DINFO_LEVEL_TERSE)
409 return false;
411 enum debug_struct_file criterion;
412 tree type_decl;
413 bool generic = lang_hooks.types.generic_p (type);
415 if (generic)
416 criterion = debug_struct_generic[usage];
417 else
418 criterion = debug_struct_ordinary[usage];
420 if (criterion == DINFO_STRUCT_FILE_NONE)
421 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
422 if (criterion == DINFO_STRUCT_FILE_ANY)
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
427 if (type_decl != NULL)
429 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
430 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
432 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
433 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
436 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
439 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
440 switch to the data section instead, and write out a synthetic start label
441 for collect2 the first time around. */
443 static void
444 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
446 if (eh_frame_section == 0)
448 int flags;
450 if (EH_TABLES_CAN_BE_READ_ONLY)
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
471 else
472 flags = SECTION_WRITE;
474 #ifdef EH_FRAME_SECTION_NAME
475 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
476 #else
477 eh_frame_section = ((flags == SECTION_WRITE)
478 ? data_section : readonly_data_section);
479 #endif /* EH_FRAME_SECTION_NAME */
482 switch_to_section (eh_frame_section);
484 #ifdef EH_FRAME_THROUGH_COLLECT2
485 /* We have no special eh_frame section. Emit special labels to guide
486 collect2. */
487 if (!back)
489 tree label = get_file_function_name ("F");
490 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
491 targetm.asm_out.globalize_label (asm_out_file,
492 IDENTIFIER_POINTER (label));
493 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
495 #endif
498 /* Switch [BACK] to the eh or debug frame table section, depending on
499 FOR_EH. */
501 static void
502 switch_to_frame_table_section (int for_eh, bool back)
504 if (for_eh)
505 switch_to_eh_frame_section (back);
506 else
508 if (!debug_frame_section)
509 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
510 SECTION_DEBUG, NULL);
511 switch_to_section (debug_frame_section);
515 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
517 enum dw_cfi_oprnd_type
518 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
520 switch (cfi)
522 case DW_CFA_nop:
523 case DW_CFA_GNU_window_save:
524 case DW_CFA_remember_state:
525 case DW_CFA_restore_state:
526 return dw_cfi_oprnd_unused;
528 case DW_CFA_set_loc:
529 case DW_CFA_advance_loc1:
530 case DW_CFA_advance_loc2:
531 case DW_CFA_advance_loc4:
532 case DW_CFA_MIPS_advance_loc8:
533 return dw_cfi_oprnd_addr;
535 case DW_CFA_offset:
536 case DW_CFA_offset_extended:
537 case DW_CFA_def_cfa:
538 case DW_CFA_offset_extended_sf:
539 case DW_CFA_def_cfa_sf:
540 case DW_CFA_restore:
541 case DW_CFA_restore_extended:
542 case DW_CFA_undefined:
543 case DW_CFA_same_value:
544 case DW_CFA_def_cfa_register:
545 case DW_CFA_register:
546 case DW_CFA_expression:
547 case DW_CFA_val_expression:
548 return dw_cfi_oprnd_reg_num;
550 case DW_CFA_def_cfa_offset:
551 case DW_CFA_GNU_args_size:
552 case DW_CFA_def_cfa_offset_sf:
553 return dw_cfi_oprnd_offset;
555 case DW_CFA_def_cfa_expression:
556 return dw_cfi_oprnd_loc;
558 default:
559 gcc_unreachable ();
563 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
565 enum dw_cfi_oprnd_type
566 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
568 switch (cfi)
570 case DW_CFA_def_cfa:
571 case DW_CFA_def_cfa_sf:
572 case DW_CFA_offset:
573 case DW_CFA_offset_extended_sf:
574 case DW_CFA_offset_extended:
575 return dw_cfi_oprnd_offset;
577 case DW_CFA_register:
578 return dw_cfi_oprnd_reg_num;
580 case DW_CFA_expression:
581 case DW_CFA_val_expression:
582 return dw_cfi_oprnd_loc;
584 case DW_CFA_def_cfa_expression:
585 return dw_cfi_oprnd_cfa_loc;
587 default:
588 return dw_cfi_oprnd_unused;
592 /* Output one FDE. */
594 static void
595 output_fde (dw_fde_ref fde, bool for_eh, bool second,
596 char *section_start_label, int fde_encoding, char *augmentation,
597 bool any_lsda_needed, int lsda_encoding)
599 const char *begin, *end;
600 static unsigned int j;
601 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
603 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
604 /* empty */ 0);
605 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
606 for_eh + j);
607 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
608 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
609 if (!XCOFF_DEBUGGING_INFO || for_eh)
611 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
612 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
613 " indicating 64-bit DWARF extension");
614 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
615 "FDE Length");
617 ASM_OUTPUT_LABEL (asm_out_file, l1);
619 if (for_eh)
620 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
621 else
622 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
623 debug_frame_section, "FDE CIE offset");
625 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
626 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
628 if (for_eh)
630 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
631 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
632 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
633 "FDE initial location");
634 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
635 end, begin, "FDE address range");
637 else
639 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
640 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
643 if (augmentation[0])
645 if (any_lsda_needed)
647 int size = size_of_encoded_value (lsda_encoding);
649 if (lsda_encoding == DW_EH_PE_aligned)
651 int offset = ( 4 /* Length */
652 + 4 /* CIE offset */
653 + 2 * size_of_encoded_value (fde_encoding)
654 + 1 /* Augmentation size */ );
655 int pad = -offset & (PTR_SIZE - 1);
657 size += pad;
658 gcc_assert (size_of_uleb128 (size) == 1);
661 dw2_asm_output_data_uleb128 (size, "Augmentation size");
663 if (fde->uses_eh_lsda)
665 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
666 fde->funcdef_number);
667 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
668 gen_rtx_SYMBOL_REF (Pmode, l1),
669 false,
670 "Language Specific Data Area");
672 else
674 if (lsda_encoding == DW_EH_PE_aligned)
675 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
676 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
677 "Language Specific Data Area (none)");
680 else
681 dw2_asm_output_data_uleb128 (0, "Augmentation size");
684 /* Loop through the Call Frame Instructions associated with this FDE. */
685 fde->dw_fde_current_label = begin;
687 size_t from, until, i;
689 from = 0;
690 until = vec_safe_length (fde->dw_fde_cfi);
692 if (fde->dw_fde_second_begin == NULL)
694 else if (!second)
695 until = fde->dw_fde_switch_cfi_index;
696 else
697 from = fde->dw_fde_switch_cfi_index;
699 for (i = from; i < until; i++)
700 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
703 /* If we are to emit a ref/link from function bodies to their frame tables,
704 do it now. This is typically performed to make sure that tables
705 associated with functions are dragged with them and not discarded in
706 garbage collecting links. We need to do this on a per function basis to
707 cope with -ffunction-sections. */
709 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
710 /* Switch to the function section, emit the ref to the tables, and
711 switch *back* into the table section. */
712 switch_to_section (function_section (fde->decl));
713 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
714 switch_to_frame_table_section (for_eh, true);
715 #endif
717 /* Pad the FDE out to an address sized boundary. */
718 ASM_OUTPUT_ALIGN (asm_out_file,
719 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
720 ASM_OUTPUT_LABEL (asm_out_file, l2);
722 j += 2;
725 /* Return true if frame description entry FDE is needed for EH. */
727 static bool
728 fde_needed_for_eh_p (dw_fde_ref fde)
730 if (flag_asynchronous_unwind_tables)
731 return true;
733 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
734 return true;
736 if (fde->uses_eh_lsda)
737 return true;
739 /* If exceptions are enabled, we have collected nothrow info. */
740 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
741 return false;
743 return true;
746 /* Output the call frame information used to record information
747 that relates to calculating the frame pointer, and records the
748 location of saved registers. */
750 static void
751 output_call_frame_info (int for_eh)
753 unsigned int i;
754 dw_fde_ref fde;
755 dw_cfi_ref cfi;
756 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
757 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
758 bool any_lsda_needed = false;
759 char augmentation[6];
760 int augmentation_size;
761 int fde_encoding = DW_EH_PE_absptr;
762 int per_encoding = DW_EH_PE_absptr;
763 int lsda_encoding = DW_EH_PE_absptr;
764 int return_reg;
765 rtx personality = NULL;
766 int dw_cie_version;
768 /* Don't emit a CIE if there won't be any FDEs. */
769 if (!fde_vec)
770 return;
772 /* Nothing to do if the assembler's doing it all. */
773 if (dwarf2out_do_cfi_asm ())
774 return;
776 /* If we don't have any functions we'll want to unwind out of, don't emit
777 any EH unwind information. If we make FDEs linkonce, we may have to
778 emit an empty label for an FDE that wouldn't otherwise be emitted. We
779 want to avoid having an FDE kept around when the function it refers to
780 is discarded. Example where this matters: a primary function template
781 in C++ requires EH information, an explicit specialization doesn't. */
782 if (for_eh)
784 bool any_eh_needed = false;
786 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
788 if (fde->uses_eh_lsda)
789 any_eh_needed = any_lsda_needed = true;
790 else if (fde_needed_for_eh_p (fde))
791 any_eh_needed = true;
792 else if (TARGET_USES_WEAK_UNWIND_INFO)
793 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
796 if (!any_eh_needed)
797 return;
800 /* We're going to be generating comments, so turn on app. */
801 if (flag_debug_asm)
802 app_enable ();
804 /* Switch to the proper frame section, first time. */
805 switch_to_frame_table_section (for_eh, false);
807 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
808 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
810 /* Output the CIE. */
811 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
812 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
813 if (!XCOFF_DEBUGGING_INFO || for_eh)
815 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
816 dw2_asm_output_data (4, 0xffffffff,
817 "Initial length escape value indicating 64-bit DWARF extension");
818 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
819 "Length of Common Information Entry");
821 ASM_OUTPUT_LABEL (asm_out_file, l1);
823 /* Now that the CIE pointer is PC-relative for EH,
824 use 0 to identify the CIE. */
825 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
826 (for_eh ? 0 : DWARF_CIE_ID),
827 "CIE Identifier Tag");
829 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
830 use CIE version 1, unless that would produce incorrect results
831 due to overflowing the return register column. */
832 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
833 dw_cie_version = 1;
834 if (return_reg >= 256 || dwarf_version > 2)
835 dw_cie_version = 3;
836 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
838 augmentation[0] = 0;
839 augmentation_size = 0;
841 personality = current_unit_personality;
842 if (for_eh)
844 char *p;
846 /* Augmentation:
847 z Indicates that a uleb128 is present to size the
848 augmentation section.
849 L Indicates the encoding (and thus presence) of
850 an LSDA pointer in the FDE augmentation.
851 R Indicates a non-default pointer encoding for
852 FDE code pointers.
853 P Indicates the presence of an encoding + language
854 personality routine in the CIE augmentation. */
856 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
857 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
858 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
860 p = augmentation + 1;
861 if (personality)
863 *p++ = 'P';
864 augmentation_size += 1 + size_of_encoded_value (per_encoding);
865 assemble_external_libcall (personality);
867 if (any_lsda_needed)
869 *p++ = 'L';
870 augmentation_size += 1;
872 if (fde_encoding != DW_EH_PE_absptr)
874 *p++ = 'R';
875 augmentation_size += 1;
877 if (p > augmentation + 1)
879 augmentation[0] = 'z';
880 *p = '\0';
883 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
884 if (personality && per_encoding == DW_EH_PE_aligned)
886 int offset = ( 4 /* Length */
887 + 4 /* CIE Id */
888 + 1 /* CIE version */
889 + strlen (augmentation) + 1 /* Augmentation */
890 + size_of_uleb128 (1) /* Code alignment */
891 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
892 + 1 /* RA column */
893 + 1 /* Augmentation size */
894 + 1 /* Personality encoding */ );
895 int pad = -offset & (PTR_SIZE - 1);
897 augmentation_size += pad;
899 /* Augmentations should be small, so there's scarce need to
900 iterate for a solution. Die if we exceed one uleb128 byte. */
901 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
905 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
906 if (dw_cie_version >= 4)
908 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
909 dw2_asm_output_data (1, 0, "CIE Segment Size");
911 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
912 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
913 "CIE Data Alignment Factor");
915 if (dw_cie_version == 1)
916 dw2_asm_output_data (1, return_reg, "CIE RA Column");
917 else
918 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
920 if (augmentation[0])
922 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
923 if (personality)
925 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
926 eh_data_format_name (per_encoding));
927 dw2_asm_output_encoded_addr_rtx (per_encoding,
928 personality,
929 true, NULL);
932 if (any_lsda_needed)
933 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
934 eh_data_format_name (lsda_encoding));
936 if (fde_encoding != DW_EH_PE_absptr)
937 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
938 eh_data_format_name (fde_encoding));
941 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
942 output_cfi (cfi, NULL, for_eh);
944 /* Pad the CIE out to an address sized boundary. */
945 ASM_OUTPUT_ALIGN (asm_out_file,
946 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
947 ASM_OUTPUT_LABEL (asm_out_file, l2);
949 /* Loop through all of the FDE's. */
950 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
952 unsigned int k;
954 /* Don't emit EH unwind info for leaf functions that don't need it. */
955 if (for_eh && !fde_needed_for_eh_p (fde))
956 continue;
958 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
959 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
960 augmentation, any_lsda_needed, lsda_encoding);
963 if (for_eh && targetm.terminate_dw2_eh_frame_info)
964 dw2_asm_output_data (4, 0, "End of Table");
966 /* Turn off app to make assembly quicker. */
967 if (flag_debug_asm)
968 app_disable ();
971 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
973 static void
974 dwarf2out_do_cfi_startproc (bool second)
976 int enc;
977 rtx ref;
979 fprintf (asm_out_file, "\t.cfi_startproc\n");
981 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
983 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
984 eh unwinders. */
985 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
986 return;
988 rtx personality = get_personality_function (current_function_decl);
990 if (personality)
992 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
993 ref = personality;
995 /* ??? The GAS support isn't entirely consistent. We have to
996 handle indirect support ourselves, but PC-relative is done
997 in the assembler. Further, the assembler can't handle any
998 of the weirder relocation types. */
999 if (enc & DW_EH_PE_indirect)
1001 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1002 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1003 else
1004 ref = dw2_force_const_mem (ref, true);
1007 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1008 output_addr_const (asm_out_file, ref);
1009 fputc ('\n', asm_out_file);
1012 if (crtl->uses_eh_lsda)
1014 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1016 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1017 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1018 current_function_funcdef_no);
1019 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1020 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1022 if (enc & DW_EH_PE_indirect)
1024 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1025 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1026 else
1027 ref = dw2_force_const_mem (ref, true);
1030 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1031 output_addr_const (asm_out_file, ref);
1032 fputc ('\n', asm_out_file);
1036 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1037 this allocation may be done before pass_final. */
1039 dw_fde_ref
1040 dwarf2out_alloc_current_fde (void)
1042 dw_fde_ref fde;
1044 fde = ggc_cleared_alloc<dw_fde_node> ();
1045 fde->decl = current_function_decl;
1046 fde->funcdef_number = current_function_funcdef_no;
1047 fde->fde_index = vec_safe_length (fde_vec);
1048 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1049 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1050 fde->nothrow = crtl->nothrow;
1051 fde->drap_reg = INVALID_REGNUM;
1052 fde->vdrap_reg = INVALID_REGNUM;
1054 /* Record the FDE associated with this function. */
1055 cfun->fde = fde;
1056 vec_safe_push (fde_vec, fde);
1058 return fde;
1061 /* Output a marker (i.e. a label) for the beginning of a function, before
1062 the prologue. */
1064 void
1065 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1066 unsigned int column ATTRIBUTE_UNUSED,
1067 const char *file ATTRIBUTE_UNUSED)
1069 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1070 char * dup_label;
1071 dw_fde_ref fde;
1072 section *fnsec;
1073 bool do_frame;
1075 current_function_func_begin_label = NULL;
1077 do_frame = dwarf2out_do_frame ();
1079 /* ??? current_function_func_begin_label is also used by except.cc for
1080 call-site information. We must emit this label if it might be used. */
1081 if (!do_frame
1082 && (!flag_exceptions
1083 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1084 return;
1086 fnsec = function_section (current_function_decl);
1087 switch_to_section (fnsec);
1088 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1089 current_function_funcdef_no);
1090 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1091 current_function_funcdef_no);
1092 dup_label = xstrdup (label);
1093 current_function_func_begin_label = dup_label;
1095 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1096 if (!do_frame)
1097 return;
1099 /* Unlike the debug version, the EH version of frame unwind info is a per-
1100 function setting so we need to record whether we need it for the unit. */
1101 do_eh_frame |= dwarf2out_do_eh_frame ();
1103 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1104 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1105 would include pass_dwarf2_frame. If we've not created the FDE yet,
1106 do so now. */
1107 fde = cfun->fde;
1108 if (fde == NULL)
1109 fde = dwarf2out_alloc_current_fde ();
1111 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1112 fde->dw_fde_begin = dup_label;
1113 fde->dw_fde_current_label = dup_label;
1114 fde->in_std_section = (fnsec == text_section
1115 || (cold_text_section && fnsec == cold_text_section));
1116 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1117 in_text_section_p = fnsec == text_section;
1119 /* We only want to output line number information for the genuine dwarf2
1120 prologue case, not the eh frame case. */
1121 #ifdef DWARF2_DEBUGGING_INFO
1122 if (file)
1123 dwarf2out_source_line (line, column, file, 0, true);
1124 #endif
1126 if (dwarf2out_do_cfi_asm ())
1127 dwarf2out_do_cfi_startproc (false);
1128 else
1130 rtx personality = get_personality_function (current_function_decl);
1131 if (!current_unit_personality)
1132 current_unit_personality = personality;
1134 /* We cannot keep a current personality per function as without CFI
1135 asm, at the point where we emit the CFI data, there is no current
1136 function anymore. */
1137 if (personality && current_unit_personality != personality)
1138 sorry ("multiple EH personalities are supported only with assemblers "
1139 "supporting %<.cfi_personality%> directive");
1143 /* Output a marker (i.e. a label) for the end of the generated code
1144 for a function prologue. This gets called *after* the prologue code has
1145 been generated. */
1147 void
1148 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1149 const char *file ATTRIBUTE_UNUSED)
1151 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1153 /* Output a label to mark the endpoint of the code generated for this
1154 function. */
1155 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1156 current_function_funcdef_no);
1157 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1158 current_function_funcdef_no);
1159 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1162 /* Output a marker (i.e. a label) for the beginning of the generated code
1163 for a function epilogue. This gets called *before* the prologue code has
1164 been generated. */
1166 void
1167 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1168 const char *file ATTRIBUTE_UNUSED)
1170 dw_fde_ref fde = cfun->fde;
1171 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1173 if (fde->dw_fde_vms_begin_epilogue)
1174 return;
1176 /* Output a label to mark the endpoint of the code generated for this
1177 function. */
1178 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1179 current_function_funcdef_no);
1180 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1181 current_function_funcdef_no);
1182 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1185 /* Mark the ranges of non-debug subsections in the std text sections. */
1187 static void
1188 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1190 bool std_section;
1191 const char *begin_label, *end_label;
1192 const char **last_end_label;
1193 vec<const char *, va_gc> **switch_ranges;
1195 if (second)
1197 std_section = fde->second_in_std_section;
1198 begin_label = fde->dw_fde_second_begin;
1199 end_label = fde->dw_fde_second_end;
1201 else
1203 std_section = fde->in_std_section;
1204 begin_label = fde->dw_fde_begin;
1205 end_label = fde->dw_fde_end;
1208 if (!std_section)
1209 return;
1211 if (in_text_section_p)
1213 last_end_label = &last_text_label;
1214 switch_ranges = &switch_text_ranges;
1216 else
1218 last_end_label = &last_cold_label;
1219 switch_ranges = &switch_cold_ranges;
1222 if (fde->ignored_debug)
1224 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1225 vec_safe_push (*switch_ranges, *last_end_label);
1227 else
1229 *last_end_label = end_label;
1231 if (!*switch_ranges)
1232 vec_alloc (*switch_ranges, 16);
1233 else if (vec_safe_length (*switch_ranges) & 1)
1234 vec_safe_push (*switch_ranges, begin_label);
1238 /* Output a marker (i.e. a label) for the absolute end of the generated code
1239 for a function definition. This gets called *after* the epilogue code has
1240 been generated. */
1242 void
1243 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1244 const char *file ATTRIBUTE_UNUSED)
1246 dw_fde_ref fde;
1247 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1249 last_var_location_insn = NULL;
1250 cached_next_real_insn = NULL;
1252 if (dwarf2out_do_cfi_asm ())
1253 fprintf (asm_out_file, "\t.cfi_endproc\n");
1255 /* Output a label to mark the endpoint of the code generated for this
1256 function. */
1257 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1258 current_function_funcdef_no);
1259 ASM_OUTPUT_LABEL (asm_out_file, label);
1260 fde = cfun->fde;
1261 gcc_assert (fde != NULL);
1262 if (fde->dw_fde_second_begin == NULL)
1263 fde->dw_fde_end = xstrdup (label);
1265 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1268 void
1269 dwarf2out_frame_finish (void)
1271 /* Output call frame information. */
1272 if (targetm.debug_unwind_info () == UI_DWARF2)
1273 output_call_frame_info (0);
1275 /* Output another copy for the unwinder. */
1276 if (do_eh_frame)
1277 output_call_frame_info (1);
1280 static void var_location_switch_text_section (void);
1281 static void set_cur_line_info_table (section *);
1283 void
1284 dwarf2out_switch_text_section (void)
1286 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1287 section *sect;
1288 dw_fde_ref fde = cfun->fde;
1290 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1292 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1293 current_function_funcdef_no);
1295 fde->dw_fde_second_begin = ggc_strdup (label);
1296 if (!in_cold_section_p)
1298 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1299 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1301 else
1303 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1304 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1306 have_multiple_function_sections = true;
1308 if (dwarf2out_do_cfi_asm ())
1309 fprintf (asm_out_file, "\t.cfi_endproc\n");
1311 mark_ignored_debug_section (fde, false);
1313 /* Now do the real section switch. */
1314 sect = current_function_section ();
1315 switch_to_section (sect);
1317 fde->second_in_std_section
1318 = (sect == text_section
1319 || (cold_text_section && sect == cold_text_section));
1320 in_text_section_p = sect == text_section;
1322 if (dwarf2out_do_cfi_asm ())
1323 dwarf2out_do_cfi_startproc (true);
1325 var_location_switch_text_section ();
1327 if (cold_text_section != NULL)
1328 set_cur_line_info_table (sect);
1331 /* And now, the subset of the debugging information support code necessary
1332 for emitting location expressions. */
1334 /* Describe an entry into the .debug_addr section. */
1336 enum ate_kind {
1337 ate_kind_rtx,
1338 ate_kind_rtx_dtprel,
1339 ate_kind_label
1342 struct GTY((for_user)) addr_table_entry {
1343 enum ate_kind kind;
1344 unsigned int refcount;
1345 unsigned int index;
1346 union addr_table_entry_struct_union
1348 rtx GTY ((tag ("0"))) rtl;
1349 char * GTY ((tag ("1"))) label;
1351 GTY ((desc ("%1.kind"))) addr;
1354 typedef unsigned int var_loc_view;
1356 /* Location lists are ranges + location descriptions for that range,
1357 so you can track variables that are in different places over
1358 their entire life. */
1359 typedef struct GTY(()) dw_loc_list_struct {
1360 dw_loc_list_ref dw_loc_next;
1361 const char *begin; /* Label and addr_entry for start of range */
1362 addr_table_entry *begin_entry;
1363 const char *end; /* Label for end of range */
1364 addr_table_entry *end_entry;
1365 char *ll_symbol; /* Label for beginning of location list.
1366 Only on head of list. */
1367 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1368 const char *section; /* Section this loclist is relative to */
1369 dw_loc_descr_ref expr;
1370 var_loc_view vbegin, vend;
1371 hashval_t hash;
1372 /* True if all addresses in this and subsequent lists are known to be
1373 resolved. */
1374 bool resolved_addr;
1375 /* True if this list has been replaced by dw_loc_next. */
1376 bool replaced;
1377 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1378 section. */
1379 unsigned char emitted : 1;
1380 /* True if hash field is index rather than hash value. */
1381 unsigned char num_assigned : 1;
1382 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1383 unsigned char offset_emitted : 1;
1384 /* True if note_variable_value_in_expr has been called on it. */
1385 unsigned char noted_variable_value : 1;
1386 /* True if the range should be emitted even if begin and end
1387 are the same. */
1388 bool force;
1389 } dw_loc_list_node;
1391 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1392 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1394 /* Convert a DWARF stack opcode into its string name. */
1396 static const char *
1397 dwarf_stack_op_name (unsigned int op)
1399 const char *name = get_DW_OP_name (op);
1401 if (name != NULL)
1402 return name;
1404 return "OP_<unknown>";
1407 /* Return TRUE iff we're to output location view lists as a separate
1408 attribute next to the location lists, as an extension compatible
1409 with DWARF 2 and above. */
1411 static inline bool
1412 dwarf2out_locviews_in_attribute ()
1414 return debug_variable_location_views == 1;
1417 /* Return TRUE iff we're to output location view lists as part of the
1418 location lists, as proposed for standardization after DWARF 5. */
1420 static inline bool
1421 dwarf2out_locviews_in_loclist ()
1423 #ifndef DW_LLE_view_pair
1424 return false;
1425 #else
1426 return debug_variable_location_views == -1;
1427 #endif
1430 /* Return a pointer to a newly allocated location description. Location
1431 descriptions are simple expression terms that can be strung
1432 together to form more complicated location (address) descriptions. */
1434 static inline dw_loc_descr_ref
1435 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1436 unsigned HOST_WIDE_INT oprnd2)
1438 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1440 descr->dw_loc_opc = op;
1441 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1442 descr->dw_loc_oprnd1.val_entry = NULL;
1443 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1444 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1445 descr->dw_loc_oprnd2.val_entry = NULL;
1446 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1448 return descr;
1451 /* Add a location description term to a location description expression. */
1453 static inline void
1454 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1456 dw_loc_descr_ref *d;
1458 /* Find the end of the chain. */
1459 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1462 *d = descr;
1465 /* Compare two location operands for exact equality. */
1467 static bool
1468 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1470 if (a->val_class != b->val_class)
1471 return false;
1472 switch (a->val_class)
1474 case dw_val_class_none:
1475 return true;
1476 case dw_val_class_addr:
1477 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1479 case dw_val_class_offset:
1480 case dw_val_class_unsigned_const:
1481 case dw_val_class_const:
1482 case dw_val_class_unsigned_const_implicit:
1483 case dw_val_class_const_implicit:
1484 case dw_val_class_range_list:
1485 /* These are all HOST_WIDE_INT, signed or unsigned. */
1486 return a->v.val_unsigned == b->v.val_unsigned;
1488 case dw_val_class_loc:
1489 return a->v.val_loc == b->v.val_loc;
1490 case dw_val_class_loc_list:
1491 return a->v.val_loc_list == b->v.val_loc_list;
1492 case dw_val_class_view_list:
1493 return a->v.val_view_list == b->v.val_view_list;
1494 case dw_val_class_die_ref:
1495 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1496 case dw_val_class_fde_ref:
1497 return a->v.val_fde_index == b->v.val_fde_index;
1498 case dw_val_class_symview:
1499 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1500 case dw_val_class_lbl_id:
1501 case dw_val_class_lineptr:
1502 case dw_val_class_macptr:
1503 case dw_val_class_loclistsptr:
1504 case dw_val_class_high_pc:
1505 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1506 case dw_val_class_str:
1507 return a->v.val_str == b->v.val_str;
1508 case dw_val_class_flag:
1509 return a->v.val_flag == b->v.val_flag;
1510 case dw_val_class_file:
1511 case dw_val_class_file_implicit:
1512 return a->v.val_file == b->v.val_file;
1513 case dw_val_class_decl_ref:
1514 return a->v.val_decl_ref == b->v.val_decl_ref;
1516 case dw_val_class_const_double:
1517 return (a->v.val_double.high == b->v.val_double.high
1518 && a->v.val_double.low == b->v.val_double.low);
1520 case dw_val_class_wide_int:
1521 return *a->v.val_wide == *b->v.val_wide;
1523 case dw_val_class_vec:
1525 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1526 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1528 return (a_len == b_len
1529 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1532 case dw_val_class_data8:
1533 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1535 case dw_val_class_vms_delta:
1536 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1537 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1539 case dw_val_class_discr_value:
1540 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1541 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1542 case dw_val_class_discr_list:
1543 /* It makes no sense comparing two discriminant value lists. */
1544 return false;
1546 gcc_unreachable ();
1549 /* Compare two location atoms for exact equality. */
1551 static bool
1552 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1554 if (a->dw_loc_opc != b->dw_loc_opc)
1555 return false;
1557 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1558 address size, but since we always allocate cleared storage it
1559 should be zero for other types of locations. */
1560 if (a->dtprel != b->dtprel)
1561 return false;
1563 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1564 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1567 /* Compare two complete location expressions for exact equality. */
1569 bool
1570 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1572 while (1)
1574 if (a == b)
1575 return true;
1576 if (a == NULL || b == NULL)
1577 return false;
1578 if (!loc_descr_equal_p_1 (a, b))
1579 return false;
1581 a = a->dw_loc_next;
1582 b = b->dw_loc_next;
1587 /* Add a constant POLY_OFFSET to a location expression. */
1589 static void
1590 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1592 dw_loc_descr_ref loc;
1593 HOST_WIDE_INT *p;
1595 gcc_assert (*list_head != NULL);
1597 if (known_eq (poly_offset, 0))
1598 return;
1600 /* Find the end of the chain. */
1601 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1604 HOST_WIDE_INT offset;
1605 if (!poly_offset.is_constant (&offset))
1607 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1608 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1609 return;
1612 p = NULL;
1613 if (loc->dw_loc_opc == DW_OP_fbreg
1614 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1615 p = &loc->dw_loc_oprnd1.v.val_int;
1616 else if (loc->dw_loc_opc == DW_OP_bregx)
1617 p = &loc->dw_loc_oprnd2.v.val_int;
1619 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1620 offset. Don't optimize if an signed integer overflow would happen. */
1621 if (p != NULL
1622 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1623 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1624 *p += offset;
1626 else if (offset > 0)
1627 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1629 else
1631 loc->dw_loc_next
1632 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1633 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1637 /* Return a pointer to a newly allocated location description for
1638 REG and OFFSET. */
1640 static inline dw_loc_descr_ref
1641 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1643 HOST_WIDE_INT const_offset;
1644 if (offset.is_constant (&const_offset))
1646 if (reg <= 31)
1647 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1648 const_offset, 0);
1649 else
1650 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1652 else
1654 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1655 loc_descr_plus_const (&ret, offset);
1656 return ret;
1660 /* Add a constant OFFSET to a location list. */
1662 static void
1663 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1665 dw_loc_list_ref d;
1666 for (d = list_head; d != NULL; d = d->dw_loc_next)
1667 loc_descr_plus_const (&d->expr, offset);
1670 #define DWARF_REF_SIZE \
1671 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1673 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1674 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1675 DW_FORM_data16 with 128 bits. */
1676 #define DWARF_LARGEST_DATA_FORM_BITS \
1677 (dwarf_version >= 5 ? 128 : 64)
1679 /* Utility inline function for construction of ops that were GNU extension
1680 before DWARF 5. */
1681 static inline enum dwarf_location_atom
1682 dwarf_OP (enum dwarf_location_atom op)
1684 switch (op)
1686 case DW_OP_implicit_pointer:
1687 if (dwarf_version < 5)
1688 return DW_OP_GNU_implicit_pointer;
1689 break;
1691 case DW_OP_entry_value:
1692 if (dwarf_version < 5)
1693 return DW_OP_GNU_entry_value;
1694 break;
1696 case DW_OP_const_type:
1697 if (dwarf_version < 5)
1698 return DW_OP_GNU_const_type;
1699 break;
1701 case DW_OP_regval_type:
1702 if (dwarf_version < 5)
1703 return DW_OP_GNU_regval_type;
1704 break;
1706 case DW_OP_deref_type:
1707 if (dwarf_version < 5)
1708 return DW_OP_GNU_deref_type;
1709 break;
1711 case DW_OP_convert:
1712 if (dwarf_version < 5)
1713 return DW_OP_GNU_convert;
1714 break;
1716 case DW_OP_reinterpret:
1717 if (dwarf_version < 5)
1718 return DW_OP_GNU_reinterpret;
1719 break;
1721 case DW_OP_addrx:
1722 if (dwarf_version < 5)
1723 return DW_OP_GNU_addr_index;
1724 break;
1726 case DW_OP_constx:
1727 if (dwarf_version < 5)
1728 return DW_OP_GNU_const_index;
1729 break;
1731 default:
1732 break;
1734 return op;
1737 /* Similarly for attributes. */
1738 static inline enum dwarf_attribute
1739 dwarf_AT (enum dwarf_attribute at)
1741 switch (at)
1743 case DW_AT_call_return_pc:
1744 if (dwarf_version < 5)
1745 return DW_AT_low_pc;
1746 break;
1748 case DW_AT_call_tail_call:
1749 if (dwarf_version < 5)
1750 return DW_AT_GNU_tail_call;
1751 break;
1753 case DW_AT_call_origin:
1754 if (dwarf_version < 5)
1755 return DW_AT_abstract_origin;
1756 break;
1758 case DW_AT_call_target:
1759 if (dwarf_version < 5)
1760 return DW_AT_GNU_call_site_target;
1761 break;
1763 case DW_AT_call_target_clobbered:
1764 if (dwarf_version < 5)
1765 return DW_AT_GNU_call_site_target_clobbered;
1766 break;
1768 case DW_AT_call_parameter:
1769 if (dwarf_version < 5)
1770 return DW_AT_abstract_origin;
1771 break;
1773 case DW_AT_call_value:
1774 if (dwarf_version < 5)
1775 return DW_AT_GNU_call_site_value;
1776 break;
1778 case DW_AT_call_data_value:
1779 if (dwarf_version < 5)
1780 return DW_AT_GNU_call_site_data_value;
1781 break;
1783 case DW_AT_call_all_calls:
1784 if (dwarf_version < 5)
1785 return DW_AT_GNU_all_call_sites;
1786 break;
1788 case DW_AT_call_all_tail_calls:
1789 if (dwarf_version < 5)
1790 return DW_AT_GNU_all_tail_call_sites;
1791 break;
1793 case DW_AT_dwo_name:
1794 if (dwarf_version < 5)
1795 return DW_AT_GNU_dwo_name;
1796 break;
1798 case DW_AT_addr_base:
1799 if (dwarf_version < 5)
1800 return DW_AT_GNU_addr_base;
1801 break;
1803 default:
1804 break;
1806 return at;
1809 /* And similarly for tags. */
1810 static inline enum dwarf_tag
1811 dwarf_TAG (enum dwarf_tag tag)
1813 switch (tag)
1815 case DW_TAG_call_site:
1816 if (dwarf_version < 5)
1817 return DW_TAG_GNU_call_site;
1818 break;
1820 case DW_TAG_call_site_parameter:
1821 if (dwarf_version < 5)
1822 return DW_TAG_GNU_call_site_parameter;
1823 break;
1825 default:
1826 break;
1828 return tag;
1831 /* And similarly for forms. */
1832 static inline enum dwarf_form
1833 dwarf_FORM (enum dwarf_form form)
1835 switch (form)
1837 case DW_FORM_addrx:
1838 if (dwarf_version < 5)
1839 return DW_FORM_GNU_addr_index;
1840 break;
1842 case DW_FORM_strx:
1843 if (dwarf_version < 5)
1844 return DW_FORM_GNU_str_index;
1845 break;
1847 default:
1848 break;
1850 return form;
1853 static unsigned long int get_base_type_offset (dw_die_ref);
1855 /* Return the size of a location descriptor. */
1857 static unsigned long
1858 size_of_loc_descr (dw_loc_descr_ref loc)
1860 unsigned long size = 1;
1862 switch (loc->dw_loc_opc)
1864 case DW_OP_addr:
1865 size += DWARF2_ADDR_SIZE;
1866 break;
1867 case DW_OP_GNU_addr_index:
1868 case DW_OP_addrx:
1869 case DW_OP_GNU_const_index:
1870 case DW_OP_constx:
1871 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1872 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1873 break;
1874 case DW_OP_const1u:
1875 case DW_OP_const1s:
1876 size += 1;
1877 break;
1878 case DW_OP_const2u:
1879 case DW_OP_const2s:
1880 size += 2;
1881 break;
1882 case DW_OP_const4u:
1883 case DW_OP_const4s:
1884 size += 4;
1885 break;
1886 case DW_OP_const8u:
1887 case DW_OP_const8s:
1888 size += 8;
1889 break;
1890 case DW_OP_constu:
1891 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1892 break;
1893 case DW_OP_consts:
1894 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1895 break;
1896 case DW_OP_pick:
1897 size += 1;
1898 break;
1899 case DW_OP_plus_uconst:
1900 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1901 break;
1902 case DW_OP_skip:
1903 case DW_OP_bra:
1904 size += 2;
1905 break;
1906 case DW_OP_breg0:
1907 case DW_OP_breg1:
1908 case DW_OP_breg2:
1909 case DW_OP_breg3:
1910 case DW_OP_breg4:
1911 case DW_OP_breg5:
1912 case DW_OP_breg6:
1913 case DW_OP_breg7:
1914 case DW_OP_breg8:
1915 case DW_OP_breg9:
1916 case DW_OP_breg10:
1917 case DW_OP_breg11:
1918 case DW_OP_breg12:
1919 case DW_OP_breg13:
1920 case DW_OP_breg14:
1921 case DW_OP_breg15:
1922 case DW_OP_breg16:
1923 case DW_OP_breg17:
1924 case DW_OP_breg18:
1925 case DW_OP_breg19:
1926 case DW_OP_breg20:
1927 case DW_OP_breg21:
1928 case DW_OP_breg22:
1929 case DW_OP_breg23:
1930 case DW_OP_breg24:
1931 case DW_OP_breg25:
1932 case DW_OP_breg26:
1933 case DW_OP_breg27:
1934 case DW_OP_breg28:
1935 case DW_OP_breg29:
1936 case DW_OP_breg30:
1937 case DW_OP_breg31:
1938 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1939 break;
1940 case DW_OP_regx:
1941 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1942 break;
1943 case DW_OP_fbreg:
1944 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1945 break;
1946 case DW_OP_bregx:
1947 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1948 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1949 break;
1950 case DW_OP_piece:
1951 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1952 break;
1953 case DW_OP_bit_piece:
1954 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1955 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1956 break;
1957 case DW_OP_deref_size:
1958 case DW_OP_xderef_size:
1959 size += 1;
1960 break;
1961 case DW_OP_call2:
1962 size += 2;
1963 break;
1964 case DW_OP_call4:
1965 size += 4;
1966 break;
1967 case DW_OP_call_ref:
1968 case DW_OP_GNU_variable_value:
1969 size += DWARF_REF_SIZE;
1970 break;
1971 case DW_OP_implicit_value:
1972 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1973 + loc->dw_loc_oprnd1.v.val_unsigned;
1974 break;
1975 case DW_OP_implicit_pointer:
1976 case DW_OP_GNU_implicit_pointer:
1977 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1978 break;
1979 case DW_OP_entry_value:
1980 case DW_OP_GNU_entry_value:
1982 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1983 size += size_of_uleb128 (op_size) + op_size;
1984 break;
1986 case DW_OP_const_type:
1987 case DW_OP_GNU_const_type:
1989 unsigned long o
1990 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1991 size += size_of_uleb128 (o) + 1;
1992 switch (loc->dw_loc_oprnd2.val_class)
1994 case dw_val_class_vec:
1995 size += loc->dw_loc_oprnd2.v.val_vec.length
1996 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1997 break;
1998 case dw_val_class_const:
1999 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2000 break;
2001 case dw_val_class_const_double:
2002 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2003 break;
2004 case dw_val_class_wide_int:
2005 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2006 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2007 break;
2008 default:
2009 gcc_unreachable ();
2011 break;
2013 case DW_OP_regval_type:
2014 case DW_OP_GNU_regval_type:
2016 unsigned long o
2017 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2018 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2019 + size_of_uleb128 (o);
2021 break;
2022 case DW_OP_deref_type:
2023 case DW_OP_GNU_deref_type:
2025 unsigned long o
2026 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2027 size += 1 + size_of_uleb128 (o);
2029 break;
2030 case DW_OP_convert:
2031 case DW_OP_reinterpret:
2032 case DW_OP_GNU_convert:
2033 case DW_OP_GNU_reinterpret:
2034 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2035 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2036 else
2038 unsigned long o
2039 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2040 size += size_of_uleb128 (o);
2042 break;
2043 case DW_OP_GNU_parameter_ref:
2044 size += 4;
2045 break;
2046 default:
2047 break;
2050 return size;
2053 /* Return the size of a series of location descriptors. */
2055 unsigned long
2056 size_of_locs (dw_loc_descr_ref loc)
2058 dw_loc_descr_ref l;
2059 unsigned long size;
2061 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2062 field, to avoid writing to a PCH file. */
2063 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2065 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2066 break;
2067 size += size_of_loc_descr (l);
2069 if (! l)
2070 return size;
2072 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2074 l->dw_loc_addr = size;
2075 size += size_of_loc_descr (l);
2078 return size;
2081 /* Return the size of the value in a DW_AT_discr_value attribute. */
2083 static int
2084 size_of_discr_value (dw_discr_value *discr_value)
2086 if (discr_value->pos)
2087 return size_of_uleb128 (discr_value->v.uval);
2088 else
2089 return size_of_sleb128 (discr_value->v.sval);
2092 /* Return the size of the value in a DW_AT_discr_list attribute. */
2094 static int
2095 size_of_discr_list (dw_discr_list_ref discr_list)
2097 int size = 0;
2099 for (dw_discr_list_ref list = discr_list;
2100 list != NULL;
2101 list = list->dw_discr_next)
2103 /* One byte for the discriminant value descriptor, and then one or two
2104 LEB128 numbers, depending on whether it's a single case label or a
2105 range label. */
2106 size += 1;
2107 size += size_of_discr_value (&list->dw_discr_lower_bound);
2108 if (list->dw_discr_range != 0)
2109 size += size_of_discr_value (&list->dw_discr_upper_bound);
2111 return size;
2114 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2115 static void get_ref_die_offset_label (char *, dw_die_ref);
2116 static unsigned long int get_ref_die_offset (dw_die_ref);
2118 /* Output location description stack opcode's operands (if any).
2119 The for_eh_or_skip parameter controls whether register numbers are
2120 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2121 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2122 info). This should be suppressed for the cases that have not been converted
2123 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2125 static void
2126 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2128 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2129 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2131 switch (loc->dw_loc_opc)
2133 #ifdef DWARF2_DEBUGGING_INFO
2134 case DW_OP_const2u:
2135 case DW_OP_const2s:
2136 dw2_asm_output_data (2, val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_const4u:
2139 if (loc->dtprel)
2141 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2142 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2143 val1->v.val_addr);
2144 fputc ('\n', asm_out_file);
2145 break;
2147 /* FALLTHRU */
2148 case DW_OP_const4s:
2149 dw2_asm_output_data (4, val1->v.val_int, NULL);
2150 break;
2151 case DW_OP_const8u:
2152 if (loc->dtprel)
2154 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2155 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2156 val1->v.val_addr);
2157 fputc ('\n', asm_out_file);
2158 break;
2160 /* FALLTHRU */
2161 case DW_OP_const8s:
2162 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2163 dw2_asm_output_data (8, val1->v.val_int, NULL);
2164 break;
2165 case DW_OP_skip:
2166 case DW_OP_bra:
2168 int offset;
2170 gcc_assert (val1->val_class == dw_val_class_loc);
2171 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2173 dw2_asm_output_data (2, offset, NULL);
2175 break;
2176 case DW_OP_implicit_value:
2177 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2178 switch (val2->val_class)
2180 case dw_val_class_const:
2181 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2182 break;
2183 case dw_val_class_vec:
2185 unsigned int elt_size = val2->v.val_vec.elt_size;
2186 unsigned int len = val2->v.val_vec.length;
2187 unsigned int i;
2188 unsigned char *p;
2190 if (elt_size > sizeof (HOST_WIDE_INT))
2192 elt_size /= 2;
2193 len *= 2;
2195 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2196 i < len;
2197 i++, p += elt_size)
2198 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2199 "fp or vector constant word %u", i);
2201 break;
2202 case dw_val_class_const_double:
2204 unsigned HOST_WIDE_INT first, second;
2206 if (WORDS_BIG_ENDIAN)
2208 first = val2->v.val_double.high;
2209 second = val2->v.val_double.low;
2211 else
2213 first = val2->v.val_double.low;
2214 second = val2->v.val_double.high;
2216 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2217 first, NULL);
2218 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2219 second, NULL);
2221 break;
2222 case dw_val_class_wide_int:
2224 int i;
2225 int len = get_full_len (*val2->v.val_wide);
2226 if (WORDS_BIG_ENDIAN)
2227 for (i = len - 1; i >= 0; --i)
2228 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2229 val2->v.val_wide->elt (i), NULL);
2230 else
2231 for (i = 0; i < len; ++i)
2232 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2233 val2->v.val_wide->elt (i), NULL);
2235 break;
2236 case dw_val_class_addr:
2237 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2238 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2239 break;
2240 default:
2241 gcc_unreachable ();
2243 break;
2244 #else
2245 case DW_OP_const2u:
2246 case DW_OP_const2s:
2247 case DW_OP_const4u:
2248 case DW_OP_const4s:
2249 case DW_OP_const8u:
2250 case DW_OP_const8s:
2251 case DW_OP_skip:
2252 case DW_OP_bra:
2253 case DW_OP_implicit_value:
2254 /* We currently don't make any attempt to make sure these are
2255 aligned properly like we do for the main unwind info, so
2256 don't support emitting things larger than a byte if we're
2257 only doing unwinding. */
2258 gcc_unreachable ();
2259 #endif
2260 case DW_OP_const1u:
2261 case DW_OP_const1s:
2262 dw2_asm_output_data (1, val1->v.val_int, NULL);
2263 break;
2264 case DW_OP_constu:
2265 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2266 break;
2267 case DW_OP_consts:
2268 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2269 break;
2270 case DW_OP_pick:
2271 dw2_asm_output_data (1, val1->v.val_int, NULL);
2272 break;
2273 case DW_OP_plus_uconst:
2274 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2275 break;
2276 case DW_OP_breg0:
2277 case DW_OP_breg1:
2278 case DW_OP_breg2:
2279 case DW_OP_breg3:
2280 case DW_OP_breg4:
2281 case DW_OP_breg5:
2282 case DW_OP_breg6:
2283 case DW_OP_breg7:
2284 case DW_OP_breg8:
2285 case DW_OP_breg9:
2286 case DW_OP_breg10:
2287 case DW_OP_breg11:
2288 case DW_OP_breg12:
2289 case DW_OP_breg13:
2290 case DW_OP_breg14:
2291 case DW_OP_breg15:
2292 case DW_OP_breg16:
2293 case DW_OP_breg17:
2294 case DW_OP_breg18:
2295 case DW_OP_breg19:
2296 case DW_OP_breg20:
2297 case DW_OP_breg21:
2298 case DW_OP_breg22:
2299 case DW_OP_breg23:
2300 case DW_OP_breg24:
2301 case DW_OP_breg25:
2302 case DW_OP_breg26:
2303 case DW_OP_breg27:
2304 case DW_OP_breg28:
2305 case DW_OP_breg29:
2306 case DW_OP_breg30:
2307 case DW_OP_breg31:
2308 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2309 break;
2310 case DW_OP_regx:
2312 unsigned r = val1->v.val_unsigned;
2313 if (for_eh_or_skip >= 0)
2314 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2315 gcc_assert (size_of_uleb128 (r)
2316 == size_of_uleb128 (val1->v.val_unsigned));
2317 dw2_asm_output_data_uleb128 (r, NULL);
2319 break;
2320 case DW_OP_fbreg:
2321 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2322 break;
2323 case DW_OP_bregx:
2325 unsigned r = val1->v.val_unsigned;
2326 if (for_eh_or_skip >= 0)
2327 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2328 gcc_assert (size_of_uleb128 (r)
2329 == size_of_uleb128 (val1->v.val_unsigned));
2330 dw2_asm_output_data_uleb128 (r, NULL);
2331 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2333 break;
2334 case DW_OP_piece:
2335 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2336 break;
2337 case DW_OP_bit_piece:
2338 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2339 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2340 break;
2341 case DW_OP_deref_size:
2342 case DW_OP_xderef_size:
2343 dw2_asm_output_data (1, val1->v.val_int, NULL);
2344 break;
2346 case DW_OP_addr:
2347 if (loc->dtprel)
2349 if (targetm.asm_out.output_dwarf_dtprel)
2351 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2352 DWARF2_ADDR_SIZE,
2353 val1->v.val_addr);
2354 fputc ('\n', asm_out_file);
2356 else
2357 gcc_unreachable ();
2359 else
2361 #ifdef DWARF2_DEBUGGING_INFO
2362 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2363 #else
2364 gcc_unreachable ();
2365 #endif
2367 break;
2369 case DW_OP_GNU_addr_index:
2370 case DW_OP_addrx:
2371 case DW_OP_GNU_const_index:
2372 case DW_OP_constx:
2373 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2374 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2375 "(index into .debug_addr)");
2376 break;
2378 case DW_OP_call2:
2379 case DW_OP_call4:
2381 unsigned long die_offset
2382 = get_ref_die_offset (val1->v.val_die_ref.die);
2383 /* Make sure the offset has been computed and that we can encode it as
2384 an operand. */
2385 gcc_assert (die_offset > 0
2386 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2387 ? 0xffff
2388 : 0xffffffff));
2389 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2390 die_offset, NULL);
2392 break;
2394 case DW_OP_call_ref:
2395 case DW_OP_GNU_variable_value:
2397 char label[MAX_ARTIFICIAL_LABEL_BYTES
2398 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2399 gcc_assert (val1->val_class == dw_val_class_die_ref);
2400 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2401 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2403 break;
2405 case DW_OP_implicit_pointer:
2406 case DW_OP_GNU_implicit_pointer:
2408 char label[MAX_ARTIFICIAL_LABEL_BYTES
2409 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2410 gcc_assert (val1->val_class == dw_val_class_die_ref);
2411 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2412 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2413 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2415 break;
2417 case DW_OP_entry_value:
2418 case DW_OP_GNU_entry_value:
2419 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2420 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2421 break;
2423 case DW_OP_const_type:
2424 case DW_OP_GNU_const_type:
2426 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2427 gcc_assert (o);
2428 dw2_asm_output_data_uleb128 (o, NULL);
2429 switch (val2->val_class)
2431 case dw_val_class_const:
2432 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2433 dw2_asm_output_data (1, l, NULL);
2434 dw2_asm_output_data (l, val2->v.val_int, NULL);
2435 break;
2436 case dw_val_class_vec:
2438 unsigned int elt_size = val2->v.val_vec.elt_size;
2439 unsigned int len = val2->v.val_vec.length;
2440 unsigned int i;
2441 unsigned char *p;
2443 l = len * elt_size;
2444 dw2_asm_output_data (1, l, NULL);
2445 if (elt_size > sizeof (HOST_WIDE_INT))
2447 elt_size /= 2;
2448 len *= 2;
2450 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2451 i < len;
2452 i++, p += elt_size)
2453 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2454 "fp or vector constant word %u", i);
2456 break;
2457 case dw_val_class_const_double:
2459 unsigned HOST_WIDE_INT first, second;
2460 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2462 dw2_asm_output_data (1, 2 * l, NULL);
2463 if (WORDS_BIG_ENDIAN)
2465 first = val2->v.val_double.high;
2466 second = val2->v.val_double.low;
2468 else
2470 first = val2->v.val_double.low;
2471 second = val2->v.val_double.high;
2473 dw2_asm_output_data (l, first, NULL);
2474 dw2_asm_output_data (l, second, NULL);
2476 break;
2477 case dw_val_class_wide_int:
2479 int i;
2480 int len = get_full_len (*val2->v.val_wide);
2481 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2483 dw2_asm_output_data (1, len * l, NULL);
2484 if (WORDS_BIG_ENDIAN)
2485 for (i = len - 1; i >= 0; --i)
2486 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2487 else
2488 for (i = 0; i < len; ++i)
2489 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2491 break;
2492 default:
2493 gcc_unreachable ();
2496 break;
2497 case DW_OP_regval_type:
2498 case DW_OP_GNU_regval_type:
2500 unsigned r = val1->v.val_unsigned;
2501 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2502 gcc_assert (o);
2503 if (for_eh_or_skip >= 0)
2505 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2506 gcc_assert (size_of_uleb128 (r)
2507 == size_of_uleb128 (val1->v.val_unsigned));
2509 dw2_asm_output_data_uleb128 (r, NULL);
2510 dw2_asm_output_data_uleb128 (o, NULL);
2512 break;
2513 case DW_OP_deref_type:
2514 case DW_OP_GNU_deref_type:
2516 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2517 gcc_assert (o);
2518 dw2_asm_output_data (1, val1->v.val_int, NULL);
2519 dw2_asm_output_data_uleb128 (o, NULL);
2521 break;
2522 case DW_OP_convert:
2523 case DW_OP_reinterpret:
2524 case DW_OP_GNU_convert:
2525 case DW_OP_GNU_reinterpret:
2526 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2527 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2528 else
2530 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2531 gcc_assert (o);
2532 dw2_asm_output_data_uleb128 (o, NULL);
2534 break;
2536 case DW_OP_GNU_parameter_ref:
2538 unsigned long o;
2539 gcc_assert (val1->val_class == dw_val_class_die_ref);
2540 o = get_ref_die_offset (val1->v.val_die_ref.die);
2541 dw2_asm_output_data (4, o, NULL);
2543 break;
2545 default:
2546 /* Other codes have no operands. */
2547 break;
2551 /* Output a sequence of location operations.
2552 The for_eh_or_skip parameter controls whether register numbers are
2553 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2554 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2555 info). This should be suppressed for the cases that have not been converted
2556 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2558 void
2559 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2561 for (; loc != NULL; loc = loc->dw_loc_next)
2563 enum dwarf_location_atom opc = loc->dw_loc_opc;
2564 /* Output the opcode. */
2565 if (for_eh_or_skip >= 0
2566 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2568 unsigned r = (opc - DW_OP_breg0);
2569 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2570 gcc_assert (r <= 31);
2571 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2573 else if (for_eh_or_skip >= 0
2574 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2576 unsigned r = (opc - DW_OP_reg0);
2577 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2578 gcc_assert (r <= 31);
2579 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2582 dw2_asm_output_data (1, opc,
2583 "%s", dwarf_stack_op_name (opc));
2585 /* Output the operand(s) (if any). */
2586 output_loc_operands (loc, for_eh_or_skip);
2590 /* Output location description stack opcode's operands (if any).
2591 The output is single bytes on a line, suitable for .cfi_escape. */
2593 static void
2594 output_loc_operands_raw (dw_loc_descr_ref loc)
2596 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2597 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2599 switch (loc->dw_loc_opc)
2601 case DW_OP_addr:
2602 case DW_OP_GNU_addr_index:
2603 case DW_OP_addrx:
2604 case DW_OP_GNU_const_index:
2605 case DW_OP_constx:
2606 case DW_OP_implicit_value:
2607 /* We cannot output addresses in .cfi_escape, only bytes. */
2608 gcc_unreachable ();
2610 case DW_OP_const1u:
2611 case DW_OP_const1s:
2612 case DW_OP_pick:
2613 case DW_OP_deref_size:
2614 case DW_OP_xderef_size:
2615 fputc (',', asm_out_file);
2616 dw2_asm_output_data_raw (1, val1->v.val_int);
2617 break;
2619 case DW_OP_const2u:
2620 case DW_OP_const2s:
2621 fputc (',', asm_out_file);
2622 dw2_asm_output_data_raw (2, val1->v.val_int);
2623 break;
2625 case DW_OP_const4u:
2626 case DW_OP_const4s:
2627 fputc (',', asm_out_file);
2628 dw2_asm_output_data_raw (4, val1->v.val_int);
2629 break;
2631 case DW_OP_const8u:
2632 case DW_OP_const8s:
2633 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2634 fputc (',', asm_out_file);
2635 dw2_asm_output_data_raw (8, val1->v.val_int);
2636 break;
2638 case DW_OP_skip:
2639 case DW_OP_bra:
2641 int offset;
2643 gcc_assert (val1->val_class == dw_val_class_loc);
2644 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2646 fputc (',', asm_out_file);
2647 dw2_asm_output_data_raw (2, offset);
2649 break;
2651 case DW_OP_regx:
2653 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2654 gcc_assert (size_of_uleb128 (r)
2655 == size_of_uleb128 (val1->v.val_unsigned));
2656 fputc (',', asm_out_file);
2657 dw2_asm_output_data_uleb128_raw (r);
2659 break;
2661 case DW_OP_constu:
2662 case DW_OP_plus_uconst:
2663 case DW_OP_piece:
2664 fputc (',', asm_out_file);
2665 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2666 break;
2668 case DW_OP_bit_piece:
2669 fputc (',', asm_out_file);
2670 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2671 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2672 break;
2674 case DW_OP_consts:
2675 case DW_OP_breg0:
2676 case DW_OP_breg1:
2677 case DW_OP_breg2:
2678 case DW_OP_breg3:
2679 case DW_OP_breg4:
2680 case DW_OP_breg5:
2681 case DW_OP_breg6:
2682 case DW_OP_breg7:
2683 case DW_OP_breg8:
2684 case DW_OP_breg9:
2685 case DW_OP_breg10:
2686 case DW_OP_breg11:
2687 case DW_OP_breg12:
2688 case DW_OP_breg13:
2689 case DW_OP_breg14:
2690 case DW_OP_breg15:
2691 case DW_OP_breg16:
2692 case DW_OP_breg17:
2693 case DW_OP_breg18:
2694 case DW_OP_breg19:
2695 case DW_OP_breg20:
2696 case DW_OP_breg21:
2697 case DW_OP_breg22:
2698 case DW_OP_breg23:
2699 case DW_OP_breg24:
2700 case DW_OP_breg25:
2701 case DW_OP_breg26:
2702 case DW_OP_breg27:
2703 case DW_OP_breg28:
2704 case DW_OP_breg29:
2705 case DW_OP_breg30:
2706 case DW_OP_breg31:
2707 case DW_OP_fbreg:
2708 fputc (',', asm_out_file);
2709 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2710 break;
2712 case DW_OP_bregx:
2714 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2715 gcc_assert (size_of_uleb128 (r)
2716 == size_of_uleb128 (val1->v.val_unsigned));
2717 fputc (',', asm_out_file);
2718 dw2_asm_output_data_uleb128_raw (r);
2719 fputc (',', asm_out_file);
2720 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2722 break;
2724 case DW_OP_implicit_pointer:
2725 case DW_OP_entry_value:
2726 case DW_OP_const_type:
2727 case DW_OP_regval_type:
2728 case DW_OP_deref_type:
2729 case DW_OP_convert:
2730 case DW_OP_reinterpret:
2731 case DW_OP_GNU_implicit_pointer:
2732 case DW_OP_GNU_entry_value:
2733 case DW_OP_GNU_const_type:
2734 case DW_OP_GNU_regval_type:
2735 case DW_OP_GNU_deref_type:
2736 case DW_OP_GNU_convert:
2737 case DW_OP_GNU_reinterpret:
2738 case DW_OP_GNU_parameter_ref:
2739 gcc_unreachable ();
2740 break;
2742 default:
2743 /* Other codes have no operands. */
2744 break;
2748 void
2749 output_loc_sequence_raw (dw_loc_descr_ref loc)
2751 while (1)
2753 enum dwarf_location_atom opc = loc->dw_loc_opc;
2754 /* Output the opcode. */
2755 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2757 unsigned r = (opc - DW_OP_breg0);
2758 r = DWARF2_FRAME_REG_OUT (r, 1);
2759 gcc_assert (r <= 31);
2760 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2762 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2764 unsigned r = (opc - DW_OP_reg0);
2765 r = DWARF2_FRAME_REG_OUT (r, 1);
2766 gcc_assert (r <= 31);
2767 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2769 /* Output the opcode. */
2770 fprintf (asm_out_file, "%#x", opc);
2771 output_loc_operands_raw (loc);
2773 if (!loc->dw_loc_next)
2774 break;
2775 loc = loc->dw_loc_next;
2777 fputc (',', asm_out_file);
2781 static void
2782 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2784 if (regno <= 31)
2785 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2786 (DW_OP_breg0 + regno), 0, 0));
2787 else
2788 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2791 /* Build a dwarf location for a cfa_reg spanning multiple
2792 consecutive registers. */
2794 struct dw_loc_descr_node *
2795 build_span_loc (struct cfa_reg reg)
2797 struct dw_loc_descr_node *head = NULL;
2799 gcc_assert (reg.span_width > 0);
2800 gcc_assert (reg.span > 1);
2802 /* Start from the highest number register as it goes in the upper bits. */
2803 unsigned int regno = reg.reg + reg.span - 1;
2804 build_breg_loc (&head, regno);
2806 /* Deal with the remaining registers in the span. */
2807 for (int i = reg.span - 2; i >= 0; i--)
2809 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2810 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2811 regno--;
2812 build_breg_loc (&head, regno);
2813 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2815 return head;
2818 /* This function builds a dwarf location descriptor sequence from a
2819 dw_cfa_location, adding the given OFFSET to the result of the
2820 expression. */
2822 struct dw_loc_descr_node *
2823 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2825 struct dw_loc_descr_node *head, *tmp;
2827 offset += cfa->offset;
2829 if (cfa->reg.span > 1)
2831 head = build_span_loc (cfa->reg);
2833 if (maybe_ne (offset, 0))
2834 loc_descr_plus_const (&head, offset);
2836 else if (cfa->indirect)
2838 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2839 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2840 head->dw_loc_oprnd1.val_entry = NULL;
2841 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2842 add_loc_descr (&head, tmp);
2843 loc_descr_plus_const (&head, offset);
2845 else
2846 head = new_reg_loc_descr (cfa->reg.reg, offset);
2848 return head;
2851 /* This function builds a dwarf location descriptor sequence for
2852 the address at OFFSET from the CFA when stack is aligned to
2853 ALIGNMENT byte. */
2855 struct dw_loc_descr_node *
2856 build_cfa_aligned_loc (dw_cfa_location *cfa,
2857 poly_int64 offset, HOST_WIDE_INT alignment)
2859 struct dw_loc_descr_node *head;
2860 unsigned int dwarf_fp
2861 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2863 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2864 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2866 head = new_reg_loc_descr (dwarf_fp, 0);
2867 add_loc_descr (&head, int_loc_descriptor (alignment));
2868 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2869 loc_descr_plus_const (&head, offset);
2871 else
2872 head = new_reg_loc_descr (dwarf_fp, offset);
2873 return head;
2876 /* And now, the support for symbolic debugging information. */
2878 /* .debug_str support. */
2880 static void dwarf2out_init (const char *);
2881 static void dwarf2out_finish (const char *);
2882 static void dwarf2out_early_finish (const char *);
2883 static void dwarf2out_assembly_start (void);
2884 static void dwarf2out_define (unsigned int, const char *);
2885 static void dwarf2out_undef (unsigned int, const char *);
2886 static void dwarf2out_start_source_file (unsigned, const char *);
2887 static void dwarf2out_end_source_file (unsigned);
2888 static void dwarf2out_function_decl (tree);
2889 static void dwarf2out_begin_block (unsigned, unsigned);
2890 static void dwarf2out_end_block (unsigned, unsigned);
2891 static bool dwarf2out_ignore_block (const_tree);
2892 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2893 static void dwarf2out_early_global_decl (tree);
2894 static void dwarf2out_late_global_decl (tree);
2895 static void dwarf2out_type_decl (tree, int);
2896 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2897 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2898 dw_die_ref);
2899 static void dwarf2out_abstract_function (tree);
2900 static void dwarf2out_var_location (rtx_insn *);
2901 static void dwarf2out_inline_entry (tree);
2902 static void dwarf2out_size_function (tree);
2903 static void dwarf2out_begin_function (tree);
2904 static void dwarf2out_end_function (unsigned int);
2905 static void dwarf2out_register_main_translation_unit (tree unit);
2906 static void dwarf2out_set_name (tree, tree);
2907 static void dwarf2out_register_external_die (tree decl, const char *sym,
2908 unsigned HOST_WIDE_INT off);
2909 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2910 unsigned HOST_WIDE_INT *off);
2912 /* The debug hooks structure. */
2914 const struct gcc_debug_hooks dwarf2_debug_hooks =
2916 dwarf2out_init,
2917 dwarf2out_finish,
2918 dwarf2out_early_finish,
2919 dwarf2out_assembly_start,
2920 dwarf2out_define,
2921 dwarf2out_undef,
2922 dwarf2out_start_source_file,
2923 dwarf2out_end_source_file,
2924 dwarf2out_begin_block,
2925 dwarf2out_end_block,
2926 dwarf2out_ignore_block,
2927 dwarf2out_source_line,
2928 dwarf2out_set_ignored_loc,
2929 dwarf2out_begin_prologue,
2930 #if VMS_DEBUGGING_INFO
2931 dwarf2out_vms_end_prologue,
2932 dwarf2out_vms_begin_epilogue,
2933 #else
2934 debug_nothing_int_charstar,
2935 debug_nothing_int_charstar,
2936 #endif
2937 dwarf2out_end_epilogue,
2938 dwarf2out_begin_function,
2939 dwarf2out_end_function, /* end_function */
2940 dwarf2out_register_main_translation_unit,
2941 dwarf2out_function_decl, /* function_decl */
2942 dwarf2out_early_global_decl,
2943 dwarf2out_late_global_decl,
2944 dwarf2out_type_decl, /* type_decl */
2945 dwarf2out_imported_module_or_decl,
2946 dwarf2out_die_ref_for_decl,
2947 dwarf2out_register_external_die,
2948 debug_nothing_tree, /* deferred_inline_function */
2949 /* The DWARF 2 backend tries to reduce debugging bloat by not
2950 emitting the abstract description of inline functions until
2951 something tries to reference them. */
2952 dwarf2out_abstract_function, /* outlining_inline_function */
2953 debug_nothing_rtx_code_label, /* label */
2954 debug_nothing_int, /* handle_pch */
2955 dwarf2out_var_location,
2956 dwarf2out_inline_entry, /* inline_entry */
2957 dwarf2out_size_function, /* size_function */
2958 dwarf2out_switch_text_section,
2959 dwarf2out_set_name,
2960 1, /* start_end_main_source_file */
2961 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2964 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2966 dwarf2out_init,
2967 debug_nothing_charstar,
2968 debug_nothing_charstar,
2969 dwarf2out_assembly_start,
2970 debug_nothing_int_charstar,
2971 debug_nothing_int_charstar,
2972 debug_nothing_int_charstar,
2973 debug_nothing_int,
2974 debug_nothing_int_int, /* begin_block */
2975 debug_nothing_int_int, /* end_block */
2976 debug_true_const_tree, /* ignore_block */
2977 dwarf2out_source_line, /* source_line */
2978 debug_nothing_int_int_charstar, /* set_ignored_loc */
2979 debug_nothing_int_int_charstar, /* begin_prologue */
2980 debug_nothing_int_charstar, /* end_prologue */
2981 debug_nothing_int_charstar, /* begin_epilogue */
2982 debug_nothing_int_charstar, /* end_epilogue */
2983 debug_nothing_tree, /* begin_function */
2984 debug_nothing_int, /* end_function */
2985 debug_nothing_tree, /* register_main_translation_unit */
2986 debug_nothing_tree, /* function_decl */
2987 debug_nothing_tree, /* early_global_decl */
2988 debug_nothing_tree, /* late_global_decl */
2989 debug_nothing_tree_int, /* type_decl */
2990 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2991 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2992 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2993 debug_nothing_tree, /* deferred_inline_function */
2994 debug_nothing_tree, /* outlining_inline_function */
2995 debug_nothing_rtx_code_label, /* label */
2996 debug_nothing_int, /* handle_pch */
2997 debug_nothing_rtx_insn, /* var_location */
2998 debug_nothing_tree, /* inline_entry */
2999 debug_nothing_tree, /* size_function */
3000 debug_nothing_void, /* switch_text_section */
3001 debug_nothing_tree_tree, /* set_name */
3002 0, /* start_end_main_source_file */
3003 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
3006 /* NOTE: In the comments in this file, many references are made to
3007 "Debugging Information Entries". This term is abbreviated as `DIE'
3008 throughout the remainder of this file. */
3010 /* An internal representation of the DWARF output is built, and then
3011 walked to generate the DWARF debugging info. The walk of the internal
3012 representation is done after the entire program has been compiled.
3013 The types below are used to describe the internal representation. */
3015 /* Whether to put type DIEs into their own section .debug_types instead
3016 of making them part of the .debug_info section. Only supported for
3017 Dwarf V4 or higher and the user didn't disable them through
3018 -fno-debug-types-section. It is more efficient to put them in a
3019 separate comdat sections since the linker will then be able to
3020 remove duplicates. But not all tools support .debug_types sections
3021 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3022 it is DW_UT_type unit type in .debug_info section. For late LTO
3023 debug there should be almost no types emitted so avoid enabling
3024 -fdebug-types-section there. */
3026 #define use_debug_types (dwarf_version >= 4 \
3027 && flag_debug_types_section \
3028 && !in_lto_p)
3030 /* Various DIE's use offsets relative to the beginning of the
3031 .debug_info section to refer to each other. */
3033 typedef long int dw_offset;
3035 struct comdat_type_node;
3037 /* The entries in the line_info table more-or-less mirror the opcodes
3038 that are used in the real dwarf line table. Arrays of these entries
3039 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3040 supported. */
3042 enum dw_line_info_opcode {
3043 /* Emit DW_LNE_set_address; the operand is the label index. */
3044 LI_set_address,
3046 /* Emit a row to the matrix with the given line. This may be done
3047 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3048 special opcodes. */
3049 LI_set_line,
3051 /* Emit a DW_LNS_set_file. */
3052 LI_set_file,
3054 /* Emit a DW_LNS_set_column. */
3055 LI_set_column,
3057 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3058 LI_negate_stmt,
3060 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3061 LI_set_prologue_end,
3062 LI_set_epilogue_begin,
3064 /* Emit a DW_LNE_set_discriminator. */
3065 LI_set_discriminator,
3067 /* Output a Fixed Advance PC; the target PC is the label index; the
3068 base PC is the previous LI_adv_address or LI_set_address entry.
3069 We only use this when emitting debug views without assembler
3070 support, at explicit user request. Ideally, we should only use
3071 it when the offset might be zero but we can't tell: it's the only
3072 way to maybe change the PC without resetting the view number. */
3073 LI_adv_address
3076 typedef struct GTY(()) dw_line_info_struct {
3077 enum dw_line_info_opcode opcode;
3078 unsigned int val;
3079 } dw_line_info_entry;
3082 struct GTY(()) dw_line_info_table {
3083 /* The label that marks the end of this section. */
3084 const char *end_label;
3086 /* The values for the last row of the matrix, as collected in the table.
3087 These are used to minimize the changes to the next row. */
3088 unsigned int file_num;
3089 unsigned int line_num;
3090 unsigned int column_num;
3091 int discrim_num;
3092 bool is_stmt;
3093 bool in_use;
3095 /* This denotes the NEXT view number.
3097 If it is 0, it is known that the NEXT view will be the first view
3098 at the given PC.
3100 If it is -1, we're forcing the view number to be reset, e.g. at a
3101 function entry.
3103 The meaning of other nonzero values depends on whether we're
3104 computing views internally or leaving it for the assembler to do
3105 so. If we're emitting them internally, view denotes the view
3106 number since the last known advance of PC. If we're leaving it
3107 for the assembler, it denotes the LVU label number that we're
3108 going to ask the assembler to assign. */
3109 var_loc_view view;
3111 /* This counts the number of symbolic views emitted in this table
3112 since the latest view reset. Its max value, over all tables,
3113 sets symview_upper_bound. */
3114 var_loc_view symviews_since_reset;
3116 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3117 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3118 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3119 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3121 vec<dw_line_info_entry, va_gc> *entries;
3124 /* This is an upper bound for view numbers that the assembler may
3125 assign to symbolic views output in this translation. It is used to
3126 decide how big a field to use to represent view numbers in
3127 symview-classed attributes. */
3129 static var_loc_view symview_upper_bound;
3131 /* If we're keep track of location views and their reset points, and
3132 INSN is a reset point (i.e., it necessarily advances the PC), mark
3133 the next view in TABLE as reset. */
3135 static void
3136 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3138 if (!debug_internal_reset_location_views)
3139 return;
3141 /* Maybe turn (part of?) this test into a default target hook. */
3142 int reset = 0;
3144 if (targetm.reset_location_view)
3145 reset = targetm.reset_location_view (insn);
3147 if (reset)
3149 else if (JUMP_TABLE_DATA_P (insn))
3150 reset = 1;
3151 else if (GET_CODE (insn) == USE
3152 || GET_CODE (insn) == CLOBBER
3153 || GET_CODE (insn) == ASM_INPUT
3154 || asm_noperands (insn) >= 0)
3156 else if (get_attr_min_length (insn) > 0)
3157 reset = 1;
3159 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3160 RESET_NEXT_VIEW (table->view);
3163 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3164 The children of each node form a circular list linked by
3165 die_sib. die_child points to the node *before* the "first" child node. */
3167 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3168 union die_symbol_or_type_node
3170 const char * GTY ((tag ("0"))) die_symbol;
3171 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3173 GTY ((desc ("%0.comdat_type_p"))) die_id;
3174 vec<dw_attr_node, va_gc> *die_attr;
3175 dw_die_ref die_parent;
3176 dw_die_ref die_child;
3177 dw_die_ref die_sib;
3178 dw_die_ref die_definition; /* ref from a specification to its definition */
3179 dw_offset die_offset;
3180 unsigned long die_abbrev;
3181 int die_mark;
3182 unsigned int decl_id;
3183 enum dwarf_tag die_tag;
3184 /* Die is used and must not be pruned as unused. */
3185 BOOL_BITFIELD die_perennial_p : 1;
3186 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3187 /* For an external ref to die_symbol if die_offset contains an extra
3188 offset to that symbol. */
3189 BOOL_BITFIELD with_offset : 1;
3190 /* Whether this DIE was removed from the DIE tree, for example via
3191 prune_unused_types. We don't consider those present from the
3192 DIE lookup routines. */
3193 BOOL_BITFIELD removed : 1;
3194 /* Lots of spare bits. */
3196 die_node;
3198 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3199 static bool early_dwarf;
3200 static bool early_dwarf_finished;
3201 class set_early_dwarf {
3202 public:
3203 bool saved;
3204 set_early_dwarf () : saved(early_dwarf)
3206 gcc_assert (! early_dwarf_finished);
3207 early_dwarf = true;
3209 ~set_early_dwarf () { early_dwarf = saved; }
3212 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3213 #define FOR_EACH_CHILD(die, c, expr) do { \
3214 c = die->die_child; \
3215 if (c) do { \
3216 c = c->die_sib; \
3217 expr; \
3218 } while (c != die->die_child); \
3219 } while (0)
3221 /* The pubname structure */
3223 typedef struct GTY(()) pubname_struct {
3224 dw_die_ref die;
3225 const char *name;
3227 pubname_entry;
3230 struct GTY(()) dw_ranges {
3231 const char *label;
3232 /* If this is positive, it's a block number, otherwise it's a
3233 bitwise-negated index into dw_ranges_by_label. */
3234 int num;
3235 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3236 into .debug_rnglists section rather than .debug_rnglists.dwo
3237 for -gsplit-dwarf and DWARF >= 5. */
3238 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3239 /* Index for the range list for DW_FORM_rnglistx. */
3240 unsigned int idx : 31;
3241 /* True if this range might be possibly in a different section
3242 from previous entry. */
3243 unsigned int maybe_new_sec : 1;
3244 addr_table_entry *begin_entry;
3245 addr_table_entry *end_entry;
3248 /* A structure to hold a macinfo entry. */
3250 typedef struct GTY(()) macinfo_struct {
3251 unsigned char code;
3252 unsigned HOST_WIDE_INT lineno;
3253 const char *info;
3255 macinfo_entry;
3258 struct GTY(()) dw_ranges_by_label {
3259 const char *begin;
3260 const char *end;
3263 /* The comdat type node structure. */
3264 struct GTY(()) comdat_type_node
3266 dw_die_ref root_die;
3267 dw_die_ref type_die;
3268 dw_die_ref skeleton_die;
3269 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3270 comdat_type_node *next;
3273 /* A list of DIEs for which we can't determine ancestry (parent_die
3274 field) just yet. Later in dwarf2out_finish we will fill in the
3275 missing bits. */
3276 typedef struct GTY(()) limbo_die_struct {
3277 dw_die_ref die;
3278 /* The tree for which this DIE was created. We use this to
3279 determine ancestry later. */
3280 tree created_for;
3281 struct limbo_die_struct *next;
3283 limbo_die_node;
3285 typedef struct skeleton_chain_struct
3287 dw_die_ref old_die;
3288 dw_die_ref new_die;
3289 struct skeleton_chain_struct *parent;
3291 skeleton_chain_node;
3293 /* Define a macro which returns nonzero for a TYPE_DECL which was
3294 implicitly generated for a type.
3296 Note that, unlike the C front-end (which generates a NULL named
3297 TYPE_DECL node for each complete tagged type, each array type,
3298 and each function type node created) the C++ front-end generates
3299 a _named_ TYPE_DECL node for each tagged type node created.
3300 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3301 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3302 front-end, but for each type, tagged or not. */
3304 #define TYPE_DECL_IS_STUB(decl) \
3305 (DECL_NAME (decl) == NULL_TREE \
3306 || (DECL_ARTIFICIAL (decl) \
3307 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3308 /* This is necessary for stub decls that \
3309 appear in nested inline functions. */ \
3310 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3311 && (decl_ultimate_origin (decl) \
3312 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3314 /* Information concerning the compilation unit's programming
3315 language, and compiler version. */
3317 /* Fixed size portion of the DWARF compilation unit header. */
3318 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3319 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3320 + (dwarf_version >= 5 ? 4 : 3))
3322 /* Fixed size portion of the DWARF comdat type unit header. */
3323 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3324 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3325 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3327 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3328 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3329 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3331 /* Fixed size portion of public names info. */
3332 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3334 /* Fixed size portion of the address range info. */
3335 #define DWARF_ARANGES_HEADER_SIZE \
3336 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3337 DWARF2_ADDR_SIZE * 2) \
3338 - DWARF_INITIAL_LENGTH_SIZE)
3340 /* Size of padding portion in the address range info. It must be
3341 aligned to twice the pointer size. */
3342 #define DWARF_ARANGES_PAD_SIZE \
3343 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3344 DWARF2_ADDR_SIZE * 2) \
3345 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3347 /* Use assembler line directives if available. */
3348 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3349 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3350 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3351 #else
3352 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3353 #endif
3354 #endif
3356 /* Use assembler views in line directives if available. */
3357 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3358 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3359 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3360 #else
3361 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3362 #endif
3363 #endif
3365 /* Return true if GCC configure detected assembler support for .loc. */
3367 bool
3368 dwarf2out_default_as_loc_support (void)
3370 return DWARF2_ASM_LINE_DEBUG_INFO;
3371 #if (GCC_VERSION >= 3000)
3372 # undef DWARF2_ASM_LINE_DEBUG_INFO
3373 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3374 #endif
3377 /* Return true if GCC configure detected assembler support for views
3378 in .loc directives. */
3380 bool
3381 dwarf2out_default_as_locview_support (void)
3383 return DWARF2_ASM_VIEW_DEBUG_INFO;
3384 #if (GCC_VERSION >= 3000)
3385 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3386 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3387 #endif
3390 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3391 view computation, and it refers to a view identifier for which we
3392 will not emit a label because it is known to map to a view number
3393 zero. We won't allocate the bitmap if we're not using assembler
3394 support for location views, but we have to make the variable
3395 visible for GGC and for code that will be optimized out for lack of
3396 support but that's still parsed and compiled. We could abstract it
3397 out with macros, but it's not worth it. */
3398 static GTY(()) bitmap zero_view_p;
3400 /* Evaluate to TRUE iff N is known to identify the first location view
3401 at its PC. When not using assembler location view computation,
3402 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3403 and views label numbers recorded in it are the ones known to be
3404 zero. */
3405 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3406 || (N) == (var_loc_view)-1 \
3407 || (zero_view_p \
3408 && bitmap_bit_p (zero_view_p, (N))))
3410 /* Return true iff we're to emit .loc directives for the assembler to
3411 generate line number sections.
3413 When we're not emitting views, all we need from the assembler is
3414 support for .loc directives.
3416 If we are emitting views, we can only use the assembler's .loc
3417 support if it also supports views.
3419 When the compiler is emitting the line number programs and
3420 computing view numbers itself, it resets view numbers at known PC
3421 changes and counts from that, and then it emits view numbers as
3422 literal constants in locviewlists. There are cases in which the
3423 compiler is not sure about PC changes, e.g. when extra alignment is
3424 requested for a label. In these cases, the compiler may not reset
3425 the view counter, and the potential PC advance in the line number
3426 program will use an opcode that does not reset the view counter
3427 even if the PC actually changes, so that compiler and debug info
3428 consumer can keep view numbers in sync.
3430 When the compiler defers view computation to the assembler, it
3431 emits symbolic view numbers in locviewlists, with the exception of
3432 views known to be zero (forced resets, or reset after
3433 compiler-visible PC changes): instead of emitting symbols for
3434 these, we emit literal zero and assert the assembler agrees with
3435 the compiler's assessment. We could use symbolic views everywhere,
3436 instead of special-casing zero views, but then we'd be unable to
3437 optimize out locviewlists that contain only zeros. */
3439 static bool
3440 output_asm_line_debug_info (void)
3442 return (dwarf2out_as_loc_support
3443 && (dwarf2out_as_locview_support
3444 || !debug_variable_location_views));
3447 static bool asm_outputs_debug_line_str (void);
3449 /* Minimum line offset in a special line info. opcode.
3450 This value was chosen to give a reasonable range of values. */
3451 #define DWARF_LINE_BASE -10
3453 /* First special line opcode - leave room for the standard opcodes. */
3454 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3456 /* Range of line offsets in a special line info. opcode. */
3457 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3459 /* Flag that indicates the initial value of the is_stmt_start flag.
3460 In the present implementation, we do not mark any lines as
3461 the beginning of a source statement, because that information
3462 is not made available by the GCC front-end. */
3463 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3465 /* Maximum number of operations per instruction bundle. */
3466 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3467 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3468 #endif
3470 /* This location is used by calc_die_sizes() to keep track
3471 the offset of each DIE within the .debug_info section. */
3472 static unsigned long next_die_offset;
3474 /* Record the root of the DIE's built for the current compilation unit. */
3475 static GTY(()) dw_die_ref single_comp_unit_die;
3477 /* A list of type DIEs that have been separated into comdat sections. */
3478 static GTY(()) comdat_type_node *comdat_type_list;
3480 /* A list of CU DIEs that have been separated. */
3481 static GTY(()) limbo_die_node *cu_die_list;
3483 /* A list of DIEs with a NULL parent waiting to be relocated. */
3484 static GTY(()) limbo_die_node *limbo_die_list;
3486 /* A list of DIEs for which we may have to generate
3487 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3488 static GTY(()) limbo_die_node *deferred_asm_name;
3490 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3492 typedef const char *compare_type;
3494 static hashval_t hash (dwarf_file_data *);
3495 static bool equal (dwarf_file_data *, const char *);
3498 /* Filenames referenced by this compilation unit. */
3499 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3501 struct decl_die_hasher : ggc_ptr_hash<die_node>
3503 typedef tree compare_type;
3505 static hashval_t hash (die_node *);
3506 static bool equal (die_node *, tree);
3508 /* A hash table of references to DIE's that describe declarations.
3509 The key is a DECL_UID() which is a unique number identifying each decl. */
3510 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3512 struct GTY ((for_user)) variable_value_struct {
3513 unsigned int decl_id;
3514 vec<dw_die_ref, va_gc> *dies;
3517 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3519 typedef tree compare_type;
3521 static hashval_t hash (variable_value_struct *);
3522 static bool equal (variable_value_struct *, tree);
3524 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3525 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3526 DECL_CONTEXT of the referenced VAR_DECLs. */
3527 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3529 struct block_die_hasher : ggc_ptr_hash<die_struct>
3531 static hashval_t hash (die_struct *);
3532 static bool equal (die_struct *, die_struct *);
3535 /* A hash table of references to DIE's that describe COMMON blocks.
3536 The key is DECL_UID() ^ die_parent. */
3537 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3539 typedef struct GTY(()) die_arg_entry_struct {
3540 dw_die_ref die;
3541 tree arg;
3542 } die_arg_entry;
3545 /* Node of the variable location list. */
3546 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3547 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3548 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3549 in mode of the EXPR_LIST node and first EXPR_LIST operand
3550 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3551 location or NULL for padding. For larger bitsizes,
3552 mode is 0 and first operand is a CONCAT with bitsize
3553 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3554 NULL as second operand. */
3555 rtx GTY (()) loc;
3556 const char * GTY (()) label;
3557 struct var_loc_node * GTY (()) next;
3558 var_loc_view view;
3561 /* Variable location list. */
3562 struct GTY ((for_user)) var_loc_list_def {
3563 struct var_loc_node * GTY (()) first;
3565 /* Pointer to the last but one or last element of the
3566 chained list. If the list is empty, both first and
3567 last are NULL, if the list contains just one node
3568 or the last node certainly is not redundant, it points
3569 to the last node, otherwise points to the last but one.
3570 Do not mark it for GC because it is marked through the chain. */
3571 struct var_loc_node * GTY ((skip ("%h"))) last;
3573 /* Pointer to the last element before section switch,
3574 if NULL, either sections weren't switched or first
3575 is after section switch. */
3576 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3578 /* DECL_UID of the variable decl. */
3579 unsigned int decl_id;
3581 typedef struct var_loc_list_def var_loc_list;
3583 /* Call argument location list. */
3584 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3585 rtx GTY (()) call_arg_loc_note;
3586 const char * GTY (()) label;
3587 tree GTY (()) block;
3588 bool tail_call_p;
3589 rtx GTY (()) symbol_ref;
3590 struct call_arg_loc_node * GTY (()) next;
3594 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3596 typedef const_tree compare_type;
3598 static hashval_t hash (var_loc_list *);
3599 static bool equal (var_loc_list *, const_tree);
3602 /* Table of decl location linked lists. */
3603 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3605 /* Head and tail of call_arg_loc chain. */
3606 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3607 static struct call_arg_loc_node *call_arg_loc_last;
3609 /* Number of call sites in the current function. */
3610 static int call_site_count = -1;
3611 /* Number of tail call sites in the current function. */
3612 static int tail_call_site_count = -1;
3614 /* A cached location list. */
3615 struct GTY ((for_user)) cached_dw_loc_list_def {
3616 /* The DECL_UID of the decl that this entry describes. */
3617 unsigned int decl_id;
3619 /* The cached location list. */
3620 dw_loc_list_ref loc_list;
3622 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3624 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3627 typedef const_tree compare_type;
3629 static hashval_t hash (cached_dw_loc_list *);
3630 static bool equal (cached_dw_loc_list *, const_tree);
3633 /* Table of cached location lists. */
3634 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3636 /* A vector of references to DIE's that are uniquely identified by their tag,
3637 presence/absence of children DIE's, and list of attribute/value pairs. */
3638 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3640 /* A hash map to remember the stack usage for DWARF procedures. The value
3641 stored is the stack size difference between before the DWARF procedure
3642 invokation and after it returned. In other words, for a DWARF procedure
3643 that consumes N stack slots and that pushes M ones, this stores M - N. */
3644 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3646 /* A global counter for generating labels for line number data. */
3647 static unsigned int line_info_label_num;
3649 /* The current table to which we should emit line number information
3650 for the current function. This will be set up at the beginning of
3651 assembly for the function. */
3652 static GTY(()) dw_line_info_table *cur_line_info_table;
3654 /* The two default tables of line number info. */
3655 static GTY(()) dw_line_info_table *text_section_line_info;
3656 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3658 /* The set of all non-default tables of line number info. */
3659 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3661 /* A flag to tell pubnames/types export if there is an info section to
3662 refer to. */
3663 static bool info_section_emitted;
3665 /* A pointer to the base of a table that contains a list of publicly
3666 accessible names. */
3667 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3669 /* A pointer to the base of a table that contains a list of publicly
3670 accessible types. */
3671 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3673 /* A pointer to the base of a table that contains a list of macro
3674 defines/undefines (and file start/end markers). */
3675 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3677 /* True if .debug_macinfo or .debug_macros section is going to be
3678 emitted. */
3679 #define have_macinfo \
3680 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3681 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3682 && !macinfo_table->is_empty ())
3684 /* Vector of dies for which we should generate .debug_ranges info. */
3685 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3687 /* Vector of pairs of labels referenced in ranges_table. */
3688 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3690 /* Whether we have location lists that need outputting */
3691 static GTY(()) bool have_location_lists;
3693 /* Unique label counter. */
3694 static GTY(()) unsigned int loclabel_num;
3696 /* Unique label counter for point-of-call tables. */
3697 static GTY(()) unsigned int poc_label_num;
3699 /* The last file entry emitted by maybe_emit_file(). */
3700 static GTY(()) struct dwarf_file_data * last_emitted_file;
3702 /* Number of internal labels generated by gen_internal_sym(). */
3703 static GTY(()) int label_num;
3705 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3707 /* Instances of generic types for which we need to generate debug
3708 info that describe their generic parameters and arguments. That
3709 generation needs to happen once all types are properly laid out so
3710 we do it at the end of compilation. */
3711 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3713 /* Offset from the "steady-state frame pointer" to the frame base,
3714 within the current function. */
3715 static poly_int64 frame_pointer_fb_offset;
3716 static bool frame_pointer_fb_offset_valid;
3718 static vec<dw_die_ref> base_types;
3720 /* Flags to represent a set of attribute classes for attributes that represent
3721 a scalar value (bounds, pointers, ...). */
3722 enum dw_scalar_form
3724 dw_scalar_form_constant = 0x01,
3725 dw_scalar_form_exprloc = 0x02,
3726 dw_scalar_form_reference = 0x04
3729 /* Forward declarations for functions defined in this file. */
3731 static bool is_pseudo_reg (const_rtx);
3732 static tree type_main_variant (tree);
3733 static bool is_tagged_type (const_tree);
3734 static const char *dwarf_tag_name (unsigned);
3735 static const char *dwarf_attr_name (unsigned);
3736 static const char *dwarf_form_name (unsigned);
3737 static tree decl_ultimate_origin (const_tree);
3738 static tree decl_class_context (tree);
3739 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3740 static inline unsigned int AT_index (dw_attr_node *);
3741 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3742 static inline unsigned AT_flag (dw_attr_node *);
3743 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3744 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3745 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3746 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3747 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3748 unsigned int, unsigned char *);
3749 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3750 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3751 static inline const char *AT_string (dw_attr_node *);
3752 static enum dwarf_form AT_string_form (dw_attr_node *);
3753 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3754 static void add_AT_specification (dw_die_ref, dw_die_ref);
3755 static inline dw_die_ref AT_ref (dw_attr_node *);
3756 static inline int AT_ref_external (dw_attr_node *);
3757 static inline void set_AT_ref_external (dw_attr_node *, int);
3758 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3759 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3760 dw_loc_list_ref);
3761 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3762 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3763 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3764 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3765 static void remove_addr_table_entry (addr_table_entry *);
3766 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3767 static inline rtx AT_addr (dw_attr_node *);
3768 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3769 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3770 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3771 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3772 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3773 unsigned long, bool);
3774 static inline const char *AT_lbl (dw_attr_node *);
3775 static const char *get_AT_low_pc (dw_die_ref);
3776 static bool is_c (void);
3777 static bool is_cxx (void);
3778 static bool is_cxx (const_tree);
3779 static bool is_fortran (void);
3780 static bool is_ada (void);
3781 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3782 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3783 static void add_child_die (dw_die_ref, dw_die_ref);
3784 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3785 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3786 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3787 static void equate_type_number_to_die (tree, dw_die_ref);
3788 static var_loc_list *lookup_decl_loc (const_tree);
3789 static void equate_decl_number_to_die (tree, dw_die_ref);
3790 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3791 static void print_spaces (FILE *);
3792 static void print_die (dw_die_ref, FILE *);
3793 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3794 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3795 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3796 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3797 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3798 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3799 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3800 struct md5_ctx *, int *);
3801 struct checksum_attributes;
3802 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3803 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3804 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3805 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3806 static bool same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3807 static bool same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3808 static bool same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3809 static bool same_die_p (dw_die_ref, dw_die_ref, int *);
3810 static bool is_type_die (dw_die_ref);
3811 static inline bool is_template_instantiation (dw_die_ref);
3812 static bool is_declaration_die (dw_die_ref);
3813 static bool should_move_die_to_comdat (dw_die_ref);
3814 static dw_die_ref clone_as_declaration (dw_die_ref);
3815 static dw_die_ref clone_die (dw_die_ref);
3816 static dw_die_ref clone_tree (dw_die_ref);
3817 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3818 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3819 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3820 static dw_die_ref generate_skeleton (dw_die_ref);
3821 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3822 dw_die_ref,
3823 dw_die_ref);
3824 static void break_out_comdat_types (dw_die_ref);
3825 static void copy_decls_for_unworthy_types (dw_die_ref);
3827 static void add_sibling_attributes (dw_die_ref);
3828 static void output_location_lists (dw_die_ref);
3829 static int constant_size (unsigned HOST_WIDE_INT);
3830 static unsigned long size_of_die (dw_die_ref);
3831 static void calc_die_sizes (dw_die_ref);
3832 static void calc_base_type_die_sizes (void);
3833 static void mark_dies (dw_die_ref);
3834 static void unmark_dies (dw_die_ref);
3835 static void unmark_all_dies (dw_die_ref);
3836 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3837 static unsigned long size_of_aranges (void);
3838 static enum dwarf_form value_format (dw_attr_node *);
3839 static void output_value_format (dw_attr_node *);
3840 static void output_abbrev_section (void);
3841 static void output_die_abbrevs (unsigned long, dw_die_ref);
3842 static void output_die (dw_die_ref);
3843 static void output_compilation_unit_header (enum dwarf_unit_type);
3844 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3845 static void output_comdat_type_unit (comdat_type_node *, bool);
3846 static const char *dwarf2_name (tree, int);
3847 static void add_pubname (tree, dw_die_ref);
3848 static void add_enumerator_pubname (const char *, dw_die_ref);
3849 static void add_pubname_string (const char *, dw_die_ref);
3850 static void add_pubtype (tree, dw_die_ref);
3851 static void output_pubnames (vec<pubname_entry, va_gc> *);
3852 static void output_aranges (void);
3853 static unsigned int add_ranges (const_tree, bool = false);
3854 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3855 bool *, bool);
3856 static void output_ranges (void);
3857 static dw_line_info_table *new_line_info_table (void);
3858 static void output_line_info (bool);
3859 static void output_file_names (void);
3860 static bool is_base_type (tree);
3861 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3862 static int decl_quals (const_tree);
3863 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3864 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3865 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3866 static unsigned int debugger_reg_number (const_rtx);
3867 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3868 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3869 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3870 enum var_init_status);
3871 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3872 enum var_init_status);
3873 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3874 enum var_init_status);
3875 static bool is_based_loc (const_rtx);
3876 static bool resolve_one_addr (rtx *);
3877 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3878 enum var_init_status);
3879 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3880 enum var_init_status);
3881 struct loc_descr_context;
3882 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3883 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3884 static dw_loc_list_ref loc_list_from_tree (tree, int,
3885 struct loc_descr_context *);
3886 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3887 struct loc_descr_context *);
3888 static tree field_type (const_tree);
3889 static unsigned int simple_type_align_in_bits (const_tree);
3890 static unsigned int simple_decl_align_in_bits (const_tree);
3891 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3892 struct vlr_context;
3893 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3894 HOST_WIDE_INT *);
3895 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3896 dw_loc_list_ref);
3897 static void add_data_member_location_attribute (dw_die_ref, tree,
3898 struct vlr_context *);
3899 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3900 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3901 static void insert_wide_int (const wide_int_ref &, unsigned char *, int);
3902 static unsigned insert_float (const_rtx, unsigned char *);
3903 static rtx rtl_for_decl_location (tree);
3904 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3905 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3906 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3907 static void add_desc_attribute (dw_die_ref, tree);
3908 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3909 static void add_comp_dir_attribute (dw_die_ref);
3910 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3911 struct loc_descr_context *);
3912 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3913 struct loc_descr_context *);
3914 static void add_subscript_info (dw_die_ref, tree, bool);
3915 static void add_byte_size_attribute (dw_die_ref, tree);
3916 static void add_alignment_attribute (dw_die_ref, tree);
3917 static void add_bit_offset_attribute (dw_die_ref, tree);
3918 static void add_bit_size_attribute (dw_die_ref, tree);
3919 static void add_prototyped_attribute (dw_die_ref, tree);
3920 static void add_abstract_origin_attribute (dw_die_ref, tree);
3921 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3922 static void add_src_coords_attributes (dw_die_ref, tree);
3923 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3924 static void add_discr_value (dw_die_ref, dw_discr_value *);
3925 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3926 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3927 static dw_die_ref scope_die_for (tree, dw_die_ref);
3928 static inline bool local_scope_p (dw_die_ref);
3929 static inline bool class_scope_p (dw_die_ref);
3930 static inline bool class_or_namespace_scope_p (dw_die_ref);
3931 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3932 static void add_calling_convention_attribute (dw_die_ref, tree);
3933 static const char *type_tag (const_tree);
3934 static tree member_declared_type (const_tree);
3935 #if 0
3936 static const char *decl_start_label (tree);
3937 #endif
3938 static void gen_array_type_die (tree, dw_die_ref);
3939 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3940 #if 0
3941 static void gen_entry_point_die (tree, dw_die_ref);
3942 #endif
3943 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref, bool);
3944 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3945 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3946 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3947 static void gen_formal_types_die (tree, dw_die_ref);
3948 static void gen_subprogram_die (tree, dw_die_ref);
3949 static void gen_variable_die (tree, tree, dw_die_ref);
3950 static void gen_const_die (tree, dw_die_ref);
3951 static void gen_label_die (tree, dw_die_ref);
3952 static void gen_lexical_block_die (tree, dw_die_ref);
3953 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3954 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3955 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3956 static dw_die_ref gen_compile_unit_die (const char *);
3957 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3958 static void gen_member_die (tree, dw_die_ref);
3959 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3960 enum debug_info_usage);
3961 static void gen_subroutine_type_die (tree, dw_die_ref);
3962 static void gen_typedef_die (tree, dw_die_ref);
3963 static void gen_type_die (tree, dw_die_ref, bool = false);
3964 static void gen_block_die (tree, dw_die_ref);
3965 static void decls_for_scope (tree, dw_die_ref, bool = true);
3966 static bool is_naming_typedef_decl (const_tree);
3967 static inline dw_die_ref get_context_die (tree);
3968 static void gen_namespace_die (tree, dw_die_ref);
3969 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3970 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3971 static dw_die_ref force_decl_die (tree);
3972 static dw_die_ref force_type_die (tree);
3973 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3974 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3975 static struct dwarf_file_data * lookup_filename (const char *);
3976 static void retry_incomplete_types (void);
3977 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3978 static void gen_generic_params_dies (tree);
3979 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage,
3980 bool = false);
3981 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage,
3982 bool = false);
3983 static void splice_child_die (dw_die_ref, dw_die_ref);
3984 static int file_info_cmp (const void *, const void *);
3985 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3986 const char *, var_loc_view, const char *);
3987 static void output_loc_list (dw_loc_list_ref);
3988 static char *gen_internal_sym (const char *);
3989 static bool want_pubnames (void);
3991 static void prune_unmark_dies (dw_die_ref);
3992 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3993 static void prune_unused_types_mark (dw_die_ref, int);
3994 static void prune_unused_types_walk (dw_die_ref);
3995 static void prune_unused_types_walk_attribs (dw_die_ref);
3996 static void prune_unused_types_prune (dw_die_ref);
3997 static void prune_unused_types (void);
3998 static int maybe_emit_file (struct dwarf_file_data *fd);
3999 static inline const char *AT_vms_delta1 (dw_attr_node *);
4000 static inline const char *AT_vms_delta2 (dw_attr_node *);
4001 #if VMS_DEBUGGING_INFO
4002 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4003 const char *, const char *);
4004 #endif
4005 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4006 static void gen_remaining_tmpl_value_param_die_attribute (void);
4007 static bool generic_type_p (tree);
4008 static void schedule_generic_params_dies_gen (tree t);
4009 static void gen_scheduled_generic_parms_dies (void);
4010 static void resolve_variable_values (void);
4012 static const char *comp_dir_string (void);
4014 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
4016 /* enum for tracking thread-local variables whose address is really an offset
4017 relative to the TLS pointer, which will need link-time relocation, but will
4018 not need relocation by the DWARF consumer. */
4020 enum dtprel_bool
4022 dtprel_false = 0,
4023 dtprel_true = 1
4026 /* Return the operator to use for an address of a variable. For dtprel_true, we
4027 use DW_OP_const*. For regular variables, which need both link-time
4028 relocation and consumer-level relocation (e.g., to account for shared objects
4029 loaded at a random address), we use DW_OP_addr*. */
4031 static inline enum dwarf_location_atom
4032 dw_addr_op (enum dtprel_bool dtprel)
4034 if (dtprel == dtprel_true)
4035 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4036 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4037 else
4038 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4041 /* Return a pointer to a newly allocated address location description. If
4042 dwarf_split_debug_info is true, then record the address with the appropriate
4043 relocation. */
4044 static inline dw_loc_descr_ref
4045 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4047 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4049 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4050 ref->dw_loc_oprnd1.v.val_addr = addr;
4051 ref->dtprel = dtprel;
4052 if (dwarf_split_debug_info)
4053 ref->dw_loc_oprnd1.val_entry
4054 = add_addr_table_entry (addr,
4055 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4056 else
4057 ref->dw_loc_oprnd1.val_entry = NULL;
4059 return ref;
4062 /* Section names used to hold DWARF debugging information. */
4064 #ifndef DEBUG_INFO_SECTION
4065 #define DEBUG_INFO_SECTION ".debug_info"
4066 #endif
4067 #ifndef DEBUG_DWO_INFO_SECTION
4068 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4069 #endif
4070 #ifndef DEBUG_LTO_INFO_SECTION
4071 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4072 #endif
4073 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4074 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4075 #endif
4076 #ifndef DEBUG_ABBREV_SECTION
4077 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4078 #endif
4079 #ifndef DEBUG_LTO_ABBREV_SECTION
4080 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4081 #endif
4082 #ifndef DEBUG_DWO_ABBREV_SECTION
4083 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4084 #endif
4085 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4086 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4087 #endif
4088 #ifndef DEBUG_ARANGES_SECTION
4089 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4090 #endif
4091 #ifndef DEBUG_ADDR_SECTION
4092 #define DEBUG_ADDR_SECTION ".debug_addr"
4093 #endif
4094 #ifndef DEBUG_MACINFO_SECTION
4095 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4096 #endif
4097 #ifndef DEBUG_LTO_MACINFO_SECTION
4098 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4099 #endif
4100 #ifndef DEBUG_DWO_MACINFO_SECTION
4101 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4102 #endif
4103 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4104 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4105 #endif
4106 #ifndef DEBUG_MACRO_SECTION
4107 #define DEBUG_MACRO_SECTION ".debug_macro"
4108 #endif
4109 #ifndef DEBUG_LTO_MACRO_SECTION
4110 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4111 #endif
4112 #ifndef DEBUG_DWO_MACRO_SECTION
4113 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4114 #endif
4115 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4116 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4117 #endif
4118 #ifndef DEBUG_LINE_SECTION
4119 #define DEBUG_LINE_SECTION ".debug_line"
4120 #endif
4121 #ifndef DEBUG_LTO_LINE_SECTION
4122 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4123 #endif
4124 #ifndef DEBUG_DWO_LINE_SECTION
4125 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4126 #endif
4127 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4128 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4129 #endif
4130 #ifndef DEBUG_LOC_SECTION
4131 #define DEBUG_LOC_SECTION ".debug_loc"
4132 #endif
4133 #ifndef DEBUG_DWO_LOC_SECTION
4134 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4135 #endif
4136 #ifndef DEBUG_LOCLISTS_SECTION
4137 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4138 #endif
4139 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4140 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4141 #endif
4142 #ifndef DEBUG_PUBNAMES_SECTION
4143 #define DEBUG_PUBNAMES_SECTION \
4144 ((debug_generate_pub_sections == 2) \
4145 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4146 #endif
4147 #ifndef DEBUG_PUBTYPES_SECTION
4148 #define DEBUG_PUBTYPES_SECTION \
4149 ((debug_generate_pub_sections == 2) \
4150 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4151 #endif
4152 #ifndef DEBUG_STR_OFFSETS_SECTION
4153 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4154 #endif
4155 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4156 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4157 #endif
4158 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4159 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4160 #endif
4161 #ifndef DEBUG_STR_SECTION
4162 #define DEBUG_STR_SECTION ".debug_str"
4163 #endif
4164 #ifndef DEBUG_LTO_STR_SECTION
4165 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4166 #endif
4167 #ifndef DEBUG_STR_DWO_SECTION
4168 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4169 #endif
4170 #ifndef DEBUG_LTO_STR_DWO_SECTION
4171 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4172 #endif
4173 #ifndef DEBUG_RANGES_SECTION
4174 #define DEBUG_RANGES_SECTION ".debug_ranges"
4175 #endif
4176 #ifndef DEBUG_RNGLISTS_SECTION
4177 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4178 #endif
4179 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4180 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4181 #endif
4182 #ifndef DEBUG_LINE_STR_SECTION
4183 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4184 #endif
4185 #ifndef DEBUG_LTO_LINE_STR_SECTION
4186 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4187 #endif
4189 /* Section flags for .debug_str section. */
4190 #define DEBUG_STR_SECTION_FLAGS \
4191 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4192 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4193 : SECTION_DEBUG)
4195 /* Section flags for .debug_str.dwo section. */
4196 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4198 /* Attribute used to refer to the macro section. */
4199 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4200 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4202 /* Labels we insert at beginning sections we can reference instead of
4203 the section names themselves. */
4205 #ifndef TEXT_SECTION_LABEL
4206 #define TEXT_SECTION_LABEL "Ltext"
4207 #endif
4208 #ifndef COLD_TEXT_SECTION_LABEL
4209 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4210 #endif
4211 #ifndef DEBUG_LINE_SECTION_LABEL
4212 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4213 #endif
4214 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4215 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4216 #endif
4217 #ifndef DEBUG_INFO_SECTION_LABEL
4218 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4219 #endif
4220 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4221 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4222 #endif
4223 #ifndef DEBUG_ABBREV_SECTION_LABEL
4224 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4225 #endif
4226 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4227 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4228 #endif
4229 #ifndef DEBUG_ADDR_SECTION_LABEL
4230 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4231 #endif
4232 #ifndef DEBUG_LOC_SECTION_LABEL
4233 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4234 #endif
4235 #ifndef DEBUG_RANGES_SECTION_LABEL
4236 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4237 #endif
4238 #ifndef DEBUG_MACINFO_SECTION_LABEL
4239 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4240 #endif
4241 #ifndef DEBUG_MACRO_SECTION_LABEL
4242 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4243 #endif
4244 #define SKELETON_COMP_DIE_ABBREV 1
4245 #define SKELETON_TYPE_DIE_ABBREV 2
4247 /* Definitions of defaults for formats and names of various special
4248 (artificial) labels which may be generated within this file (when the -g
4249 options is used and DWARF2_DEBUGGING_INFO is in effect.
4250 If necessary, these may be overridden from within the tm.h file, but
4251 typically, overriding these defaults is unnecessary. */
4253 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4254 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4255 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4256 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4258 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4266 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4269 #ifndef TEXT_END_LABEL
4270 #define TEXT_END_LABEL "Letext"
4271 #endif
4272 #ifndef COLD_END_LABEL
4273 #define COLD_END_LABEL "Letext_cold"
4274 #endif
4275 #ifndef BLOCK_BEGIN_LABEL
4276 #define BLOCK_BEGIN_LABEL "LBB"
4277 #endif
4278 #ifndef BLOCK_INLINE_ENTRY_LABEL
4279 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4280 #endif
4281 #ifndef BLOCK_END_LABEL
4282 #define BLOCK_END_LABEL "LBE"
4283 #endif
4284 #ifndef LINE_CODE_LABEL
4285 #define LINE_CODE_LABEL "LM"
4286 #endif
4289 /* Return the root of the DIE's built for the current compilation unit. */
4290 static dw_die_ref
4291 comp_unit_die (void)
4293 if (!single_comp_unit_die)
4294 single_comp_unit_die = gen_compile_unit_die (NULL);
4295 return single_comp_unit_die;
4298 /* We allow a language front-end to designate a function that is to be
4299 called to "demangle" any name before it is put into a DIE. */
4301 static const char *(*demangle_name_func) (const char *);
4303 void
4304 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4306 demangle_name_func = func;
4309 /* Test if rtl node points to a pseudo register. */
4311 static inline bool
4312 is_pseudo_reg (const_rtx rtl)
4314 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4315 || (GET_CODE (rtl) == SUBREG
4316 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4319 /* Return a reference to a type, with its const and volatile qualifiers
4320 removed. */
4322 static inline tree
4323 type_main_variant (tree type)
4325 type = TYPE_MAIN_VARIANT (type);
4327 /* ??? There really should be only one main variant among any group of
4328 variants of a given type (and all of the MAIN_VARIANT values for all
4329 members of the group should point to that one type) but sometimes the C
4330 front-end messes this up for array types, so we work around that bug
4331 here. */
4332 if (TREE_CODE (type) == ARRAY_TYPE)
4333 while (type != TYPE_MAIN_VARIANT (type))
4334 type = TYPE_MAIN_VARIANT (type);
4336 return type;
4339 /* Return true if the given type node represents a tagged type. */
4341 static inline bool
4342 is_tagged_type (const_tree type)
4344 enum tree_code code = TREE_CODE (type);
4346 return (code == RECORD_TYPE || code == UNION_TYPE
4347 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4350 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4352 static void
4353 get_ref_die_offset_label (char *label, dw_die_ref ref)
4355 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4358 /* Return die_offset of a DIE reference to a base type. */
4360 static unsigned long int
4361 get_base_type_offset (dw_die_ref ref)
4363 if (ref->die_offset)
4364 return ref->die_offset;
4365 if (comp_unit_die ()->die_abbrev)
4367 calc_base_type_die_sizes ();
4368 gcc_assert (ref->die_offset);
4370 return ref->die_offset;
4373 /* Return die_offset of a DIE reference other than base type. */
4375 static unsigned long int
4376 get_ref_die_offset (dw_die_ref ref)
4378 gcc_assert (ref->die_offset);
4379 return ref->die_offset;
4382 /* Convert a DIE tag into its string name. */
4384 static const char *
4385 dwarf_tag_name (unsigned int tag)
4387 const char *name = get_DW_TAG_name (tag);
4389 if (name != NULL)
4390 return name;
4392 return "DW_TAG_<unknown>";
4395 /* Convert a DWARF attribute code into its string name. */
4397 static const char *
4398 dwarf_attr_name (unsigned int attr)
4400 const char *name;
4402 switch (attr)
4404 #if VMS_DEBUGGING_INFO
4405 case DW_AT_HP_prologue:
4406 return "DW_AT_HP_prologue";
4407 #else
4408 case DW_AT_MIPS_loop_unroll_factor:
4409 return "DW_AT_MIPS_loop_unroll_factor";
4410 #endif
4412 #if VMS_DEBUGGING_INFO
4413 case DW_AT_HP_epilogue:
4414 return "DW_AT_HP_epilogue";
4415 #else
4416 case DW_AT_MIPS_stride:
4417 return "DW_AT_MIPS_stride";
4418 #endif
4421 name = get_DW_AT_name (attr);
4423 if (name != NULL)
4424 return name;
4426 return "DW_AT_<unknown>";
4429 /* Convert a DWARF value form code into its string name. */
4431 static const char *
4432 dwarf_form_name (unsigned int form)
4434 const char *name = get_DW_FORM_name (form);
4436 if (name != NULL)
4437 return name;
4439 return "DW_FORM_<unknown>";
4442 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4443 instance of an inlined instance of a decl which is local to an inline
4444 function, so we have to trace all of the way back through the origin chain
4445 to find out what sort of node actually served as the original seed for the
4446 given block. */
4448 static tree
4449 decl_ultimate_origin (const_tree decl)
4451 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4452 return NULL_TREE;
4454 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4455 we're trying to output the abstract instance of this function. */
4456 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4457 return NULL_TREE;
4459 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4460 most distant ancestor, this should never happen. */
4461 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4463 return DECL_ABSTRACT_ORIGIN (decl);
4466 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4467 of a virtual function may refer to a base class, so we check the 'this'
4468 parameter. */
4470 static tree
4471 decl_class_context (tree decl)
4473 tree context = NULL_TREE;
4475 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4476 context = DECL_CONTEXT (decl);
4477 else
4478 context = TYPE_MAIN_VARIANT
4479 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4481 if (context && !TYPE_P (context))
4482 context = NULL_TREE;
4484 return context;
4487 /* Add an attribute/value pair to a DIE. */
4489 static inline void
4490 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4492 /* Maybe this should be an assert? */
4493 if (die == NULL)
4494 return;
4496 if (flag_checking)
4498 /* Check we do not add duplicate attrs. Can't use get_AT here
4499 because that recurses to the specification/abstract origin DIE. */
4500 dw_attr_node *a;
4501 unsigned ix;
4502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4503 gcc_assert (a->dw_attr != attr->dw_attr);
4506 vec_safe_reserve (die->die_attr, 1);
4507 vec_safe_push (die->die_attr, *attr);
4510 enum dw_val_class
4511 AT_class (dw_attr_node *a)
4513 return a->dw_attr_val.val_class;
4516 /* Return the index for any attribute that will be referenced with a
4517 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4518 indices are stored in dw_attr_val.v.val_str for reference counting
4519 pruning. */
4521 static inline unsigned int
4522 AT_index (dw_attr_node *a)
4524 if (AT_class (a) == dw_val_class_str)
4525 return a->dw_attr_val.v.val_str->index;
4526 else if (a->dw_attr_val.val_entry != NULL)
4527 return a->dw_attr_val.val_entry->index;
4528 return NOT_INDEXED;
4531 /* Add a flag value attribute to a DIE. */
4533 static inline void
4534 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_flag;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_flag = flag;
4542 add_dwarf_attr (die, &attr);
4545 static inline unsigned
4546 AT_flag (dw_attr_node *a)
4548 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4549 return a->dw_attr_val.v.val_flag;
4552 /* Add a signed integer attribute value to a DIE. */
4554 static inline void
4555 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4557 dw_attr_node attr;
4559 attr.dw_attr = attr_kind;
4560 attr.dw_attr_val.val_class = dw_val_class_const;
4561 attr.dw_attr_val.val_entry = NULL;
4562 attr.dw_attr_val.v.val_int = int_val;
4563 add_dwarf_attr (die, &attr);
4566 HOST_WIDE_INT
4567 AT_int (dw_attr_node *a)
4569 gcc_assert (a && (AT_class (a) == dw_val_class_const
4570 || AT_class (a) == dw_val_class_const_implicit));
4571 return a->dw_attr_val.v.val_int;
4574 /* Add an unsigned integer attribute value to a DIE. */
4576 static inline void
4577 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4578 unsigned HOST_WIDE_INT unsigned_val)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4586 add_dwarf_attr (die, &attr);
4589 unsigned HOST_WIDE_INT
4590 AT_unsigned (dw_attr_node *a)
4592 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4593 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4594 return a->dw_attr_val.v.val_unsigned;
4597 dw_wide_int *
4598 alloc_dw_wide_int (const wide_int_ref &w)
4600 dw_wide_int *p
4601 = (dw_wide_int *) ggc_internal_alloc (sizeof (dw_wide_int)
4602 + ((w.get_len () - 1)
4603 * sizeof (HOST_WIDE_INT)));
4604 p->precision = w.get_precision ();
4605 p->len = w.get_len ();
4606 memcpy (p->val, w.get_val (), p->len * sizeof (HOST_WIDE_INT));
4607 return p;
4610 /* Add an unsigned wide integer attribute value to a DIE. */
4612 static inline void
4613 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4614 const wide_int_ref &w)
4616 dw_attr_node attr;
4618 attr.dw_attr = attr_kind;
4619 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4620 attr.dw_attr_val.val_entry = NULL;
4621 attr.dw_attr_val.v.val_wide = alloc_dw_wide_int (w);
4622 add_dwarf_attr (die, &attr);
4625 /* Add an unsigned double integer attribute value to a DIE. */
4627 static inline void
4628 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4629 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4631 dw_attr_node attr;
4633 attr.dw_attr = attr_kind;
4634 attr.dw_attr_val.val_class = dw_val_class_const_double;
4635 attr.dw_attr_val.val_entry = NULL;
4636 attr.dw_attr_val.v.val_double.high = high;
4637 attr.dw_attr_val.v.val_double.low = low;
4638 add_dwarf_attr (die, &attr);
4641 /* Add a floating point attribute value to a DIE and return it. */
4643 static inline void
4644 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4645 unsigned int length, unsigned int elt_size, unsigned char *array)
4647 dw_attr_node attr;
4649 attr.dw_attr = attr_kind;
4650 attr.dw_attr_val.val_class = dw_val_class_vec;
4651 attr.dw_attr_val.val_entry = NULL;
4652 attr.dw_attr_val.v.val_vec.length = length;
4653 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4654 attr.dw_attr_val.v.val_vec.array = array;
4655 add_dwarf_attr (die, &attr);
4658 /* Add an 8-byte data attribute value to a DIE. */
4660 static inline void
4661 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4662 unsigned char data8[8])
4664 dw_attr_node attr;
4666 attr.dw_attr = attr_kind;
4667 attr.dw_attr_val.val_class = dw_val_class_data8;
4668 attr.dw_attr_val.val_entry = NULL;
4669 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4670 add_dwarf_attr (die, &attr);
4673 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4674 dwarf_split_debug_info, address attributes in dies destined for the
4675 final executable have force_direct set to avoid using indexed
4676 references. */
4678 static inline void
4679 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4680 bool force_direct)
4682 dw_attr_node attr;
4683 char * lbl_id;
4685 lbl_id = xstrdup (lbl_low);
4686 attr.dw_attr = DW_AT_low_pc;
4687 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4688 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4689 if (dwarf_split_debug_info && !force_direct)
4690 attr.dw_attr_val.val_entry
4691 = add_addr_table_entry (lbl_id, ate_kind_label);
4692 else
4693 attr.dw_attr_val.val_entry = NULL;
4694 add_dwarf_attr (die, &attr);
4696 attr.dw_attr = DW_AT_high_pc;
4697 if (dwarf_version < 4)
4698 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4699 else
4700 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4701 lbl_id = xstrdup (lbl_high);
4702 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4703 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4704 && dwarf_split_debug_info && !force_direct)
4705 attr.dw_attr_val.val_entry
4706 = add_addr_table_entry (lbl_id, ate_kind_label);
4707 else
4708 attr.dw_attr_val.val_entry = NULL;
4709 add_dwarf_attr (die, &attr);
4712 /* Hash and equality functions for debug_str_hash. */
4714 hashval_t
4715 indirect_string_hasher::hash (indirect_string_node *x)
4717 return htab_hash_string (x->str);
4720 bool
4721 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4723 return strcmp (x1->str, x2) == 0;
4726 /* Add STR to the given string hash table. */
4728 static struct indirect_string_node *
4729 find_AT_string_in_table (const char *str,
4730 hash_table<indirect_string_hasher> *table,
4731 enum insert_option insert = INSERT)
4733 struct indirect_string_node *node;
4735 indirect_string_node **slot
4736 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4737 if (*slot == NULL)
4739 node = ggc_cleared_alloc<indirect_string_node> ();
4740 node->str = ggc_strdup (str);
4741 *slot = node;
4743 else
4744 node = *slot;
4746 node->refcount++;
4747 return node;
4750 /* Add STR to the indirect string hash table. */
4752 static struct indirect_string_node *
4753 find_AT_string (const char *str, enum insert_option insert = INSERT)
4755 if (! debug_str_hash)
4756 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4758 return find_AT_string_in_table (str, debug_str_hash, insert);
4761 /* Add a string attribute value to a DIE. */
4763 static inline void
4764 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4766 dw_attr_node attr;
4767 struct indirect_string_node *node;
4769 node = find_AT_string (str);
4771 attr.dw_attr = attr_kind;
4772 attr.dw_attr_val.val_class = dw_val_class_str;
4773 attr.dw_attr_val.val_entry = NULL;
4774 attr.dw_attr_val.v.val_str = node;
4775 add_dwarf_attr (die, &attr);
4778 static inline const char *
4779 AT_string (dw_attr_node *a)
4781 gcc_assert (a && AT_class (a) == dw_val_class_str);
4782 return a->dw_attr_val.v.val_str->str;
4785 /* Call this function directly to bypass AT_string_form's logic to put
4786 the string inline in the die. */
4788 static void
4789 set_indirect_string (struct indirect_string_node *node)
4791 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4792 /* Already indirect is a no op. */
4793 if (node->form == DW_FORM_strp
4794 || node->form == DW_FORM_line_strp
4795 || node->form == dwarf_FORM (DW_FORM_strx))
4797 gcc_assert (node->label);
4798 return;
4800 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4801 ++dw2_string_counter;
4802 node->label = xstrdup (label);
4804 if (!dwarf_split_debug_info)
4806 node->form = DW_FORM_strp;
4807 node->index = NOT_INDEXED;
4809 else
4811 node->form = dwarf_FORM (DW_FORM_strx);
4812 node->index = NO_INDEX_ASSIGNED;
4816 /* A helper function for dwarf2out_finish, called to reset indirect
4817 string decisions done for early LTO dwarf output before fat object
4818 dwarf output. */
4821 reset_indirect_string (indirect_string_node **h, void *)
4823 struct indirect_string_node *node = *h;
4824 if (node->form == DW_FORM_strp
4825 || node->form == DW_FORM_line_strp
4826 || node->form == dwarf_FORM (DW_FORM_strx))
4828 free (node->label);
4829 node->label = NULL;
4830 node->form = (dwarf_form) 0;
4831 node->index = 0;
4833 return 1;
4836 /* Add a string representing a file or filepath attribute value to a DIE. */
4838 static inline void
4839 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4840 const char *str)
4842 if (! asm_outputs_debug_line_str ())
4843 add_AT_string (die, attr_kind, str);
4844 else
4846 dw_attr_node attr;
4847 struct indirect_string_node *node;
4849 if (!debug_line_str_hash)
4850 debug_line_str_hash
4851 = hash_table<indirect_string_hasher>::create_ggc (10);
4853 node = find_AT_string_in_table (str, debug_line_str_hash);
4854 set_indirect_string (node);
4855 node->form = DW_FORM_line_strp;
4857 attr.dw_attr = attr_kind;
4858 attr.dw_attr_val.val_class = dw_val_class_str;
4859 attr.dw_attr_val.val_entry = NULL;
4860 attr.dw_attr_val.v.val_str = node;
4861 add_dwarf_attr (die, &attr);
4865 /* Find out whether a string should be output inline in DIE
4866 or out-of-line in .debug_str section. */
4868 static enum dwarf_form
4869 find_string_form (struct indirect_string_node *node)
4871 unsigned int len;
4873 if (node->form)
4874 return node->form;
4876 len = strlen (node->str) + 1;
4878 /* If the string is shorter or equal to the size of the reference, it is
4879 always better to put it inline. */
4880 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4881 return node->form = DW_FORM_string;
4883 /* If we cannot expect the linker to merge strings in .debug_str
4884 section, only put it into .debug_str if it is worth even in this
4885 single module. */
4886 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4887 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4888 && (len - dwarf_offset_size) * node->refcount <= len))
4889 return node->form = DW_FORM_string;
4891 set_indirect_string (node);
4893 return node->form;
4896 /* Find out whether the string referenced from the attribute should be
4897 output inline in DIE or out-of-line in .debug_str section. */
4899 static enum dwarf_form
4900 AT_string_form (dw_attr_node *a)
4902 gcc_assert (a && AT_class (a) == dw_val_class_str);
4903 return find_string_form (a->dw_attr_val.v.val_str);
4906 /* Add a DIE reference attribute value to a DIE. */
4908 static inline void
4909 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4911 dw_attr_node attr;
4912 gcc_checking_assert (targ_die != NULL);
4913 gcc_assert (targ_die != die
4914 || (attr_kind != DW_AT_abstract_origin
4915 && attr_kind != DW_AT_specification));
4917 /* With LTO we can end up trying to reference something we didn't create
4918 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4919 if (targ_die == NULL)
4920 return;
4922 attr.dw_attr = attr_kind;
4923 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4924 attr.dw_attr_val.val_entry = NULL;
4925 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4926 attr.dw_attr_val.v.val_die_ref.external = 0;
4927 add_dwarf_attr (die, &attr);
4930 /* Change DIE reference REF to point to NEW_DIE instead. */
4932 static inline void
4933 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4935 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4936 ref->dw_attr_val.v.val_die_ref.die = new_die;
4937 ref->dw_attr_val.v.val_die_ref.external = 0;
4940 /* Add an AT_specification attribute to a DIE, and also make the back
4941 pointer from the specification to the definition. */
4943 static inline void
4944 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4946 add_AT_die_ref (die, DW_AT_specification, targ_die);
4947 gcc_assert (!targ_die->die_definition);
4948 targ_die->die_definition = die;
4951 static inline dw_die_ref
4952 AT_ref (dw_attr_node *a)
4954 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4955 return a->dw_attr_val.v.val_die_ref.die;
4958 static inline int
4959 AT_ref_external (dw_attr_node *a)
4961 if (a && AT_class (a) == dw_val_class_die_ref)
4962 return a->dw_attr_val.v.val_die_ref.external;
4964 return 0;
4967 static inline void
4968 set_AT_ref_external (dw_attr_node *a, int i)
4970 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4971 a->dw_attr_val.v.val_die_ref.external = i;
4974 /* Add a location description attribute value to a DIE. */
4976 static inline void
4977 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4979 dw_attr_node attr;
4981 attr.dw_attr = attr_kind;
4982 attr.dw_attr_val.val_class = dw_val_class_loc;
4983 attr.dw_attr_val.val_entry = NULL;
4984 attr.dw_attr_val.v.val_loc = loc;
4985 add_dwarf_attr (die, &attr);
4988 dw_loc_descr_ref
4989 AT_loc (dw_attr_node *a)
4991 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4992 return a->dw_attr_val.v.val_loc;
4995 static inline void
4996 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4998 dw_attr_node attr;
5000 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5001 return;
5003 attr.dw_attr = attr_kind;
5004 attr.dw_attr_val.val_class = dw_val_class_loc_list;
5005 attr.dw_attr_val.val_entry = NULL;
5006 attr.dw_attr_val.v.val_loc_list = loc_list;
5007 add_dwarf_attr (die, &attr);
5008 have_location_lists = true;
5011 static inline dw_loc_list_ref
5012 AT_loc_list (dw_attr_node *a)
5014 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5015 return a->dw_attr_val.v.val_loc_list;
5018 /* Add a view list attribute to DIE. It must have a DW_AT_location
5019 attribute, because the view list complements the location list. */
5021 static inline void
5022 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5024 dw_attr_node attr;
5026 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5027 return;
5029 attr.dw_attr = attr_kind;
5030 attr.dw_attr_val.val_class = dw_val_class_view_list;
5031 attr.dw_attr_val.val_entry = NULL;
5032 attr.dw_attr_val.v.val_view_list = die;
5033 add_dwarf_attr (die, &attr);
5034 gcc_checking_assert (get_AT (die, DW_AT_location));
5035 gcc_assert (have_location_lists);
5038 /* Return a pointer to the location list referenced by the attribute.
5039 If the named attribute is a view list, look up the corresponding
5040 DW_AT_location attribute and return its location list. */
5042 static inline dw_loc_list_ref *
5043 AT_loc_list_ptr (dw_attr_node *a)
5045 gcc_assert (a);
5046 switch (AT_class (a))
5048 case dw_val_class_loc_list:
5049 return &a->dw_attr_val.v.val_loc_list;
5050 case dw_val_class_view_list:
5052 dw_attr_node *l;
5053 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5054 if (!l)
5055 return NULL;
5056 gcc_checking_assert (l + 1 == a);
5057 return AT_loc_list_ptr (l);
5059 default:
5060 gcc_unreachable ();
5064 /* Return the location attribute value associated with a view list
5065 attribute value. */
5067 static inline dw_val_node *
5068 view_list_to_loc_list_val_node (dw_val_node *val)
5070 gcc_assert (val->val_class == dw_val_class_view_list);
5071 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5072 if (!loc)
5073 return NULL;
5074 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5075 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5076 return &loc->dw_attr_val;
5079 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5081 static hashval_t hash (addr_table_entry *);
5082 static bool equal (addr_table_entry *, addr_table_entry *);
5085 /* Table of entries into the .debug_addr section. */
5087 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5089 /* Hash an address_table_entry. */
5091 hashval_t
5092 addr_hasher::hash (addr_table_entry *a)
5094 inchash::hash hstate;
5095 switch (a->kind)
5097 case ate_kind_rtx:
5098 hstate.add_int (0);
5099 break;
5100 case ate_kind_rtx_dtprel:
5101 hstate.add_int (1);
5102 break;
5103 case ate_kind_label:
5104 return htab_hash_string (a->addr.label);
5105 default:
5106 gcc_unreachable ();
5108 inchash::add_rtx (a->addr.rtl, hstate);
5109 return hstate.end ();
5112 /* Determine equality for two address_table_entries. */
5114 bool
5115 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5117 if (a1->kind != a2->kind)
5118 return false;
5119 switch (a1->kind)
5121 case ate_kind_rtx:
5122 case ate_kind_rtx_dtprel:
5123 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5124 case ate_kind_label:
5125 return strcmp (a1->addr.label, a2->addr.label) == 0;
5126 default:
5127 gcc_unreachable ();
5131 /* Initialize an addr_table_entry. */
5133 void
5134 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5136 e->kind = kind;
5137 switch (kind)
5139 case ate_kind_rtx:
5140 case ate_kind_rtx_dtprel:
5141 e->addr.rtl = (rtx) addr;
5142 break;
5143 case ate_kind_label:
5144 e->addr.label = (char *) addr;
5145 break;
5147 e->refcount = 0;
5148 e->index = NO_INDEX_ASSIGNED;
5151 /* Add attr to the address table entry to the table. Defer setting an
5152 index until output time. */
5154 static addr_table_entry *
5155 add_addr_table_entry (void *addr, enum ate_kind kind)
5157 addr_table_entry *node;
5158 addr_table_entry finder;
5160 gcc_assert (dwarf_split_debug_info);
5161 if (! addr_index_table)
5162 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5163 init_addr_table_entry (&finder, kind, addr);
5164 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5166 if (*slot == HTAB_EMPTY_ENTRY)
5168 node = ggc_cleared_alloc<addr_table_entry> ();
5169 init_addr_table_entry (node, kind, addr);
5170 *slot = node;
5172 else
5173 node = *slot;
5175 node->refcount++;
5176 return node;
5179 /* Remove an entry from the addr table by decrementing its refcount.
5180 Strictly, decrementing the refcount would be enough, but the
5181 assertion that the entry is actually in the table has found
5182 bugs. */
5184 static void
5185 remove_addr_table_entry (addr_table_entry *entry)
5187 gcc_assert (dwarf_split_debug_info && addr_index_table);
5188 /* After an index is assigned, the table is frozen. */
5189 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5190 entry->refcount--;
5193 /* Given a location list, remove all addresses it refers to from the
5194 address_table. */
5196 static void
5197 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5199 for (; descr; descr = descr->dw_loc_next)
5200 if (descr->dw_loc_oprnd1.val_entry != NULL)
5202 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5203 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5207 /* A helper function for dwarf2out_finish called through
5208 htab_traverse. Assign an addr_table_entry its index. All entries
5209 must be collected into the table when this function is called,
5210 because the indexing code relies on htab_traverse to traverse nodes
5211 in the same order for each run. */
5214 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5216 addr_table_entry *node = *h;
5218 /* Don't index unreferenced nodes. */
5219 if (node->refcount == 0)
5220 return 1;
5222 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5223 node->index = *index;
5224 *index += 1;
5226 return 1;
5229 /* Return the tag of a given DIE. */
5231 enum dwarf_tag
5232 dw_get_die_tag (dw_die_ref die)
5234 return die->die_tag;
5237 /* Return a reference to the children list of a given DIE. */
5239 dw_die_ref
5240 dw_get_die_child (dw_die_ref die)
5242 return die->die_child;
5245 /* Return a reference to the sibling of a given DIE. */
5247 dw_die_ref
5248 dw_get_die_sib (dw_die_ref die)
5250 return die->die_sib;
5253 /* Add an address constant attribute value to a DIE. When using
5254 dwarf_split_debug_info, address attributes in dies destined for the
5255 final executable should be direct references--setting the parameter
5256 force_direct ensures this behavior. */
5258 static inline void
5259 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5260 bool force_direct)
5262 dw_attr_node attr;
5264 attr.dw_attr = attr_kind;
5265 attr.dw_attr_val.val_class = dw_val_class_addr;
5266 attr.dw_attr_val.v.val_addr = addr;
5267 if (dwarf_split_debug_info && !force_direct)
5268 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5269 else
5270 attr.dw_attr_val.val_entry = NULL;
5271 add_dwarf_attr (die, &attr);
5274 /* Get the RTX from to an address DIE attribute. */
5276 static inline rtx
5277 AT_addr (dw_attr_node *a)
5279 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5280 return a->dw_attr_val.v.val_addr;
5283 /* Add a file attribute value to a DIE. */
5285 static inline void
5286 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5287 struct dwarf_file_data *fd)
5289 dw_attr_node attr;
5291 attr.dw_attr = attr_kind;
5292 attr.dw_attr_val.val_class = dw_val_class_file;
5293 attr.dw_attr_val.val_entry = NULL;
5294 attr.dw_attr_val.v.val_file = fd;
5295 add_dwarf_attr (die, &attr);
5298 /* Get the dwarf_file_data from a file DIE attribute. */
5300 static inline struct dwarf_file_data *
5301 AT_file (dw_attr_node *a)
5303 gcc_assert (a && (AT_class (a) == dw_val_class_file
5304 || AT_class (a) == dw_val_class_file_implicit));
5305 return a->dw_attr_val.v.val_file;
5308 #if VMS_DEBUGGING_INFO
5309 /* Add a vms delta attribute value to a DIE. */
5311 static inline void
5312 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5313 const char *lbl1, const char *lbl2)
5315 dw_attr_node attr;
5317 attr.dw_attr = attr_kind;
5318 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5319 attr.dw_attr_val.val_entry = NULL;
5320 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5321 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5322 add_dwarf_attr (die, &attr);
5324 #endif
5326 /* Add a symbolic view identifier attribute value to a DIE. */
5328 static inline void
5329 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5330 const char *view_label)
5332 dw_attr_node attr;
5334 attr.dw_attr = attr_kind;
5335 attr.dw_attr_val.val_class = dw_val_class_symview;
5336 attr.dw_attr_val.val_entry = NULL;
5337 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5338 add_dwarf_attr (die, &attr);
5341 /* Add a label identifier attribute value to a DIE. */
5343 static inline void
5344 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5345 const char *lbl_id)
5347 dw_attr_node attr;
5349 attr.dw_attr = attr_kind;
5350 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5351 attr.dw_attr_val.val_entry = NULL;
5352 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5353 if (dwarf_split_debug_info)
5354 attr.dw_attr_val.val_entry
5355 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5356 ate_kind_label);
5357 add_dwarf_attr (die, &attr);
5360 /* Add a section offset attribute value to a DIE, an offset into the
5361 debug_line section. */
5363 static inline void
5364 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5365 const char *label)
5367 dw_attr_node attr;
5369 attr.dw_attr = attr_kind;
5370 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5371 attr.dw_attr_val.val_entry = NULL;
5372 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5373 add_dwarf_attr (die, &attr);
5376 /* Add a section offset attribute value to a DIE, an offset into the
5377 debug_macinfo section. */
5379 static inline void
5380 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5381 const char *label)
5383 dw_attr_node attr;
5385 attr.dw_attr = attr_kind;
5386 attr.dw_attr_val.val_class = dw_val_class_macptr;
5387 attr.dw_attr_val.val_entry = NULL;
5388 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5389 add_dwarf_attr (die, &attr);
5392 /* Add a range_list attribute value to a DIE. When using
5393 dwarf_split_debug_info, address attributes in dies destined for the
5394 final executable should be direct references--setting the parameter
5395 force_direct ensures this behavior. */
5397 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5398 #define RELOCATED_OFFSET (NULL)
5400 static void
5401 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5402 long unsigned int offset, bool force_direct)
5404 dw_attr_node attr;
5406 attr.dw_attr = attr_kind;
5407 attr.dw_attr_val.val_class = dw_val_class_range_list;
5408 /* For the range_list attribute, use val_entry to store whether the
5409 offset should follow split-debug-info or normal semantics. This
5410 value is read in output_range_list_offset. */
5411 if (dwarf_split_debug_info && !force_direct)
5412 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5413 else
5414 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5415 attr.dw_attr_val.v.val_offset = offset;
5416 add_dwarf_attr (die, &attr);
5419 /* Return the start label of a delta attribute. */
5421 static inline const char *
5422 AT_vms_delta1 (dw_attr_node *a)
5424 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5425 return a->dw_attr_val.v.val_vms_delta.lbl1;
5428 /* Return the end label of a delta attribute. */
5430 static inline const char *
5431 AT_vms_delta2 (dw_attr_node *a)
5433 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5434 return a->dw_attr_val.v.val_vms_delta.lbl2;
5437 static inline const char *
5438 AT_lbl (dw_attr_node *a)
5440 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5441 || AT_class (a) == dw_val_class_lineptr
5442 || AT_class (a) == dw_val_class_macptr
5443 || AT_class (a) == dw_val_class_loclistsptr
5444 || AT_class (a) == dw_val_class_high_pc));
5445 return a->dw_attr_val.v.val_lbl_id;
5448 /* Get the attribute of type attr_kind. */
5450 dw_attr_node *
5451 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5453 dw_attr_node *a;
5454 unsigned ix;
5455 dw_die_ref spec = NULL;
5457 if (! die)
5458 return NULL;
5460 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5461 if (a->dw_attr == attr_kind)
5462 return a;
5463 else if (a->dw_attr == DW_AT_specification
5464 || a->dw_attr == DW_AT_abstract_origin)
5465 spec = AT_ref (a);
5467 if (spec)
5468 return get_AT (spec, attr_kind);
5470 return NULL;
5473 /* Returns the parent of the declaration of DIE. */
5475 static dw_die_ref
5476 get_die_parent (dw_die_ref die)
5478 dw_die_ref t;
5480 if (!die)
5481 return NULL;
5483 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5484 || (t = get_AT_ref (die, DW_AT_specification)))
5485 die = t;
5487 return die->die_parent;
5490 /* Return the "low pc" attribute value, typically associated with a subprogram
5491 DIE. Return null if the "low pc" attribute is either not present, or if it
5492 cannot be represented as an assembler label identifier. */
5494 static inline const char *
5495 get_AT_low_pc (dw_die_ref die)
5497 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5499 return a ? AT_lbl (a) : NULL;
5502 /* Return the value of the string attribute designated by ATTR_KIND, or
5503 NULL if it is not present. */
5505 const char *
5506 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5508 dw_attr_node *a = get_AT (die, attr_kind);
5510 return a ? AT_string (a) : NULL;
5513 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5514 if it is not present. */
5517 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5519 dw_attr_node *a = get_AT (die, attr_kind);
5521 return a ? AT_flag (a) : 0;
5524 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5525 if it is not present. */
5527 unsigned
5528 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5530 dw_attr_node *a = get_AT (die, attr_kind);
5532 return a ? AT_unsigned (a) : 0;
5535 dw_die_ref
5536 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5538 dw_attr_node *a = get_AT (die, attr_kind);
5540 return a ? AT_ref (a) : NULL;
5543 struct dwarf_file_data *
5544 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5546 dw_attr_node *a = get_AT (die, attr_kind);
5548 return a ? AT_file (a) : NULL;
5551 /* Return TRUE if the language is C. */
5553 static inline bool
5554 is_c (void)
5556 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5558 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5559 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5564 /* Return TRUE if the language is C++. */
5566 static inline bool
5567 is_cxx (void)
5569 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5571 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5572 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5575 /* Return TRUE if DECL was created by the C++ frontend. */
5577 static bool
5578 is_cxx (const_tree decl)
5580 if (in_lto_p)
5582 const_tree context = get_ultimate_context (decl);
5583 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5584 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5586 return is_cxx ();
5589 /* Return TRUE if the language is Fortran. */
5591 static inline bool
5592 is_fortran (void)
5594 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5596 return (lang == DW_LANG_Fortran77
5597 || lang == DW_LANG_Fortran90
5598 || lang == DW_LANG_Fortran95
5599 || lang == DW_LANG_Fortran03
5600 || lang == DW_LANG_Fortran08);
5603 static inline bool
5604 is_fortran (const_tree decl)
5606 if (in_lto_p)
5608 const_tree context = get_ultimate_context (decl);
5609 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5610 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5611 "GNU Fortran", 11) == 0
5612 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5613 "GNU F77") == 0);
5615 return is_fortran ();
5618 /* Return TRUE if the language is Rust.
5619 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5621 static inline bool
5622 is_rust (void)
5624 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5626 return lang == DW_LANG_Rust;
5629 /* Return TRUE if the language is Ada. */
5631 static inline bool
5632 is_ada (void)
5634 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5636 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5639 /* Return TRUE if the language is D. */
5641 static inline bool
5642 is_dlang (void)
5644 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5646 return lang == DW_LANG_D;
5649 /* Remove the specified attribute if present. Return TRUE if removal
5650 was successful. */
5652 static bool
5653 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5655 dw_attr_node *a;
5656 unsigned ix;
5658 if (! die)
5659 return false;
5661 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5662 if (a->dw_attr == attr_kind)
5664 if (AT_class (a) == dw_val_class_str)
5665 if (a->dw_attr_val.v.val_str->refcount)
5666 a->dw_attr_val.v.val_str->refcount--;
5668 /* vec::ordered_remove should help reduce the number of abbrevs
5669 that are needed. */
5670 die->die_attr->ordered_remove (ix);
5671 return true;
5673 return false;
5676 /* Remove CHILD from its parent. PREV must have the property that
5677 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5679 static void
5680 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5682 gcc_assert (child->die_parent == prev->die_parent);
5683 gcc_assert (prev->die_sib == child);
5684 if (prev == child)
5686 gcc_assert (child->die_parent->die_child == child);
5687 prev = NULL;
5689 else
5690 prev->die_sib = child->die_sib;
5691 if (child->die_parent->die_child == child)
5692 child->die_parent->die_child = prev;
5693 child->die_sib = NULL;
5696 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5697 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5699 static void
5700 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5702 dw_die_ref parent = old_child->die_parent;
5704 gcc_assert (parent == prev->die_parent);
5705 gcc_assert (prev->die_sib == old_child);
5707 new_child->die_parent = parent;
5708 if (prev == old_child)
5710 gcc_assert (parent->die_child == old_child);
5711 new_child->die_sib = new_child;
5713 else
5715 prev->die_sib = new_child;
5716 new_child->die_sib = old_child->die_sib;
5718 if (old_child->die_parent->die_child == old_child)
5719 old_child->die_parent->die_child = new_child;
5720 old_child->die_sib = NULL;
5723 /* Move all children from OLD_PARENT to NEW_PARENT. */
5725 static void
5726 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5728 dw_die_ref c;
5729 new_parent->die_child = old_parent->die_child;
5730 old_parent->die_child = NULL;
5731 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5734 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5735 matches TAG. */
5737 static void
5738 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5740 dw_die_ref c;
5742 c = die->die_child;
5743 if (c) do {
5744 dw_die_ref prev = c;
5745 c = c->die_sib;
5746 while (c->die_tag == tag)
5748 remove_child_with_prev (c, prev);
5749 c->die_parent = NULL;
5750 /* Might have removed every child. */
5751 if (die->die_child == NULL)
5752 return;
5753 c = prev->die_sib;
5755 } while (c != die->die_child);
5758 /* Add a CHILD_DIE as the last child of DIE. */
5760 static void
5761 add_child_die (dw_die_ref die, dw_die_ref child_die)
5763 /* FIXME this should probably be an assert. */
5764 if (! die || ! child_die)
5765 return;
5766 gcc_assert (die != child_die);
5768 child_die->die_parent = die;
5769 if (die->die_child)
5771 child_die->die_sib = die->die_child->die_sib;
5772 die->die_child->die_sib = child_die;
5774 else
5775 child_die->die_sib = child_die;
5776 die->die_child = child_die;
5779 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5781 static void
5782 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5783 dw_die_ref after_die)
5785 gcc_assert (die
5786 && child_die
5787 && after_die
5788 && die->die_child
5789 && die != child_die);
5791 child_die->die_parent = die;
5792 child_die->die_sib = after_die->die_sib;
5793 after_die->die_sib = child_die;
5794 if (die->die_child == after_die)
5795 die->die_child = child_die;
5798 /* Unassociate CHILD from its parent, and make its parent be
5799 NEW_PARENT. */
5801 static void
5802 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5804 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5805 if (p->die_sib == child)
5807 remove_child_with_prev (child, p);
5808 break;
5810 add_child_die (new_parent, child);
5813 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5814 is the specification, to the end of PARENT's list of children.
5815 This is done by removing and re-adding it. */
5817 static void
5818 splice_child_die (dw_die_ref parent, dw_die_ref child)
5820 /* We want the declaration DIE from inside the class, not the
5821 specification DIE at toplevel. */
5822 if (child->die_parent != parent)
5824 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5826 if (tmp)
5827 child = tmp;
5830 gcc_assert (child->die_parent == parent
5831 || (child->die_parent
5832 == get_AT_ref (parent, DW_AT_specification)));
5834 reparent_child (child, parent);
5837 /* Create and return a new die with TAG_VALUE as tag. */
5839 dw_die_ref
5840 new_die_raw (enum dwarf_tag tag_value)
5842 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5843 die->die_tag = tag_value;
5844 return die;
5847 /* Create and return a new die with a parent of PARENT_DIE. If
5848 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5849 associated tree T must be supplied to determine parenthood
5850 later. */
5852 static inline dw_die_ref
5853 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5855 dw_die_ref die = new_die_raw (tag_value);
5857 if (parent_die != NULL)
5858 add_child_die (parent_die, die);
5859 else
5861 limbo_die_node *limbo_node;
5863 /* No DIEs created after early dwarf should end up in limbo,
5864 because the limbo list should not persist past LTO
5865 streaming. */
5866 if (tag_value != DW_TAG_compile_unit
5867 /* These are allowed because they're generated while
5868 breaking out COMDAT units late. */
5869 && tag_value != DW_TAG_type_unit
5870 && tag_value != DW_TAG_skeleton_unit
5871 && !early_dwarf
5872 /* Allow nested functions to live in limbo because they will
5873 only temporarily live there, as decls_for_scope will fix
5874 them up. */
5875 && (TREE_CODE (t) != FUNCTION_DECL
5876 || !decl_function_context (t))
5877 /* Same as nested functions above but for types. Types that
5878 are local to a function will be fixed in
5879 decls_for_scope. */
5880 && (!RECORD_OR_UNION_TYPE_P (t)
5881 || !TYPE_CONTEXT (t)
5882 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5883 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5884 especially in the ltrans stage, but once we implement LTO
5885 dwarf streaming, we should remove this exception. */
5886 && !in_lto_p)
5888 fprintf (stderr, "symbol ended up in limbo too late:");
5889 debug_generic_stmt (t);
5890 gcc_unreachable ();
5893 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5894 limbo_node->die = die;
5895 limbo_node->created_for = t;
5896 limbo_node->next = limbo_die_list;
5897 limbo_die_list = limbo_node;
5900 return die;
5903 /* Return the DIE associated with the given type specifier. */
5905 dw_die_ref
5906 lookup_type_die (tree type)
5908 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5909 if (die && die->removed)
5911 TYPE_SYMTAB_DIE (type) = NULL;
5912 TREE_ASM_WRITTEN (type) = 0;
5913 return NULL;
5915 return die;
5918 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5919 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5920 anonymous type instead the one of the naming typedef. */
5922 static inline dw_die_ref
5923 strip_naming_typedef (tree type, dw_die_ref type_die)
5925 if (type
5926 && TREE_CODE (type) == RECORD_TYPE
5927 && type_die
5928 && type_die->die_tag == DW_TAG_typedef
5929 && is_naming_typedef_decl (TYPE_NAME (type)))
5930 type_die = get_AT_ref (type_die, DW_AT_type);
5931 return type_die;
5934 /* Like lookup_type_die, but if type is an anonymous type named by a
5935 typedef[1], return the DIE of the anonymous type instead the one of
5936 the naming typedef. This is because in gen_typedef_die, we did
5937 equate the anonymous struct named by the typedef with the DIE of
5938 the naming typedef. So by default, lookup_type_die on an anonymous
5939 struct yields the DIE of the naming typedef.
5941 [1]: Read the comment of is_naming_typedef_decl to learn about what
5942 a naming typedef is. */
5944 static inline dw_die_ref
5945 lookup_type_die_strip_naming_typedef (tree type)
5947 dw_die_ref die = lookup_type_die (type);
5948 return strip_naming_typedef (type, die);
5951 /* Equate a DIE to a given type specifier. */
5953 static inline void
5954 equate_type_number_to_die (tree type, dw_die_ref type_die)
5956 TYPE_SYMTAB_DIE (type) = type_die;
5959 static dw_die_ref maybe_create_die_with_external_ref (tree);
5960 struct GTY(()) sym_off_pair
5962 const char * GTY((skip)) sym;
5963 unsigned HOST_WIDE_INT off;
5965 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5967 /* Returns a hash value for X (which really is a die_struct). */
5969 inline hashval_t
5970 decl_die_hasher::hash (die_node *x)
5972 return (hashval_t) x->decl_id;
5975 /* Return true if decl_id of die_struct X is the same as UID of decl *Y. */
5977 inline bool
5978 decl_die_hasher::equal (die_node *x, tree y)
5980 return (x->decl_id == DECL_UID (y));
5983 /* Return the DIE associated with a given declaration. */
5985 dw_die_ref
5986 lookup_decl_die (tree decl)
5988 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5989 NO_INSERT);
5990 if (!die)
5992 if (in_lto_p)
5993 return maybe_create_die_with_external_ref (decl);
5994 return NULL;
5996 if ((*die)->removed)
5998 decl_die_table->clear_slot (die);
5999 return NULL;
6001 return *die;
6005 /* Return the DIE associated with BLOCK. */
6007 static inline dw_die_ref
6008 lookup_block_die (tree block)
6010 dw_die_ref die = BLOCK_DIE (block);
6011 if (!die && in_lto_p)
6012 return maybe_create_die_with_external_ref (block);
6013 return die;
6016 /* Associate DIE with BLOCK. */
6018 static inline void
6019 equate_block_to_die (tree block, dw_die_ref die)
6021 BLOCK_DIE (block) = die;
6023 #undef BLOCK_DIE
6026 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6027 style reference. Return true if we found one refering to a DIE for
6028 DECL, otherwise return false. */
6030 static bool
6031 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6032 unsigned HOST_WIDE_INT *off)
6034 dw_die_ref die;
6036 if (in_lto_p)
6038 /* During WPA stage and incremental linking we use a hash-map
6039 to store the decl <-> label + offset map. */
6040 if (!external_die_map)
6041 return false;
6042 sym_off_pair *desc = external_die_map->get (decl);
6043 if (!desc)
6044 return false;
6045 *sym = desc->sym;
6046 *off = desc->off;
6047 return true;
6050 if (TREE_CODE (decl) == BLOCK)
6051 die = lookup_block_die (decl);
6052 else
6053 die = lookup_decl_die (decl);
6054 if (!die)
6055 return false;
6057 /* Similar to get_ref_die_offset_label, but using the "correct"
6058 label. */
6059 *off = die->die_offset;
6060 while (die->die_parent)
6061 die = die->die_parent;
6062 /* For the containing CU DIE we compute a die_symbol in
6063 compute_comp_unit_symbol. */
6064 gcc_assert (die->die_tag == DW_TAG_compile_unit
6065 && die->die_id.die_symbol != NULL);
6066 *sym = die->die_id.die_symbol;
6067 return true;
6070 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6072 static void
6073 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6074 const char *symbol, HOST_WIDE_INT offset)
6076 /* Create a fake DIE that contains the reference. Don't use
6077 new_die because we don't want to end up in the limbo list. */
6078 /* ??? We probably want to share these, thus put a ref to the DIE
6079 we create here to the external_die_map entry. */
6080 dw_die_ref ref = new_die_raw (die->die_tag);
6081 ref->die_id.die_symbol = symbol;
6082 ref->die_offset = offset;
6083 ref->with_offset = 1;
6084 add_AT_die_ref (die, attr_kind, ref);
6087 /* Create a DIE for DECL if required and add a reference to a DIE
6088 at SYMBOL + OFFSET which contains attributes dumped early. */
6090 static void
6091 dwarf2out_register_external_die (tree decl, const char *sym,
6092 unsigned HOST_WIDE_INT off)
6094 if (debug_info_level == DINFO_LEVEL_NONE)
6095 return;
6097 if (!external_die_map)
6098 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6099 gcc_checking_assert (!external_die_map->get (decl));
6100 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6101 external_die_map->put (decl, p);
6104 /* If we have a registered external DIE for DECL return a new DIE for
6105 the concrete instance with an appropriate abstract origin. */
6107 static dw_die_ref
6108 maybe_create_die_with_external_ref (tree decl)
6110 if (!external_die_map)
6111 return NULL;
6112 sym_off_pair *desc = external_die_map->get (decl);
6113 if (!desc)
6114 return NULL;
6116 const char *sym = desc->sym;
6117 unsigned HOST_WIDE_INT off = desc->off;
6118 external_die_map->remove (decl);
6120 in_lto_p = false;
6121 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6122 ? lookup_block_die (decl) : lookup_decl_die (decl));
6123 gcc_assert (!die);
6124 in_lto_p = true;
6126 tree ctx;
6127 dw_die_ref parent = NULL;
6128 /* Need to lookup a DIE for the decls context - the containing
6129 function or translation unit. */
6130 if (TREE_CODE (decl) == BLOCK)
6132 ctx = BLOCK_SUPERCONTEXT (decl);
6133 /* ??? We do not output DIEs for all scopes thus skip as
6134 many DIEs as needed. */
6135 while (TREE_CODE (ctx) == BLOCK
6136 && !lookup_block_die (ctx))
6137 ctx = BLOCK_SUPERCONTEXT (ctx);
6139 else
6140 ctx = DECL_CONTEXT (decl);
6141 /* Peel types in the context stack. */
6142 while (ctx && TYPE_P (ctx))
6143 ctx = TYPE_CONTEXT (ctx);
6144 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6145 if (debug_info_level <= DINFO_LEVEL_TERSE)
6146 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6147 ctx = DECL_CONTEXT (ctx);
6148 if (ctx)
6150 if (TREE_CODE (ctx) == BLOCK)
6151 parent = lookup_block_die (ctx);
6152 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6153 /* Keep the 1:1 association during WPA. */
6154 && !flag_wpa
6155 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6156 /* Otherwise all late annotations go to the main CU which
6157 imports the original CUs. */
6158 parent = comp_unit_die ();
6159 else if (TREE_CODE (ctx) == FUNCTION_DECL
6160 && TREE_CODE (decl) != FUNCTION_DECL
6161 && TREE_CODE (decl) != PARM_DECL
6162 && TREE_CODE (decl) != RESULT_DECL
6163 && TREE_CODE (decl) != BLOCK)
6164 /* Leave function local entities parent determination to when
6165 we process scope vars. */
6167 else
6168 parent = lookup_decl_die (ctx);
6170 else
6171 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6172 Handle this case gracefully by globalizing stuff. */
6173 parent = comp_unit_die ();
6174 /* Create a DIE "stub". */
6175 switch (TREE_CODE (decl))
6177 case TRANSLATION_UNIT_DECL:
6179 die = comp_unit_die ();
6180 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6181 to create a DIE for the original CUs. */
6182 return die;
6184 case NAMESPACE_DECL:
6185 if (is_fortran (decl))
6186 die = new_die (DW_TAG_module, parent, decl);
6187 else
6188 die = new_die (DW_TAG_namespace, parent, decl);
6189 break;
6190 case FUNCTION_DECL:
6191 die = new_die (DW_TAG_subprogram, parent, decl);
6192 break;
6193 case VAR_DECL:
6194 die = new_die (DW_TAG_variable, parent, decl);
6195 break;
6196 case RESULT_DECL:
6197 die = new_die (DW_TAG_variable, parent, decl);
6198 break;
6199 case PARM_DECL:
6200 die = new_die (DW_TAG_formal_parameter, parent, decl);
6201 break;
6202 case CONST_DECL:
6203 die = new_die (DW_TAG_constant, parent, decl);
6204 break;
6205 case LABEL_DECL:
6206 die = new_die (DW_TAG_label, parent, decl);
6207 break;
6208 case BLOCK:
6209 die = new_die (DW_TAG_lexical_block, parent, decl);
6210 break;
6211 default:
6212 gcc_unreachable ();
6214 if (TREE_CODE (decl) == BLOCK)
6215 equate_block_to_die (decl, die);
6216 else
6217 equate_decl_number_to_die (decl, die);
6219 add_desc_attribute (die, decl);
6221 /* Add a reference to the DIE providing early debug at $sym + off. */
6222 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6224 return die;
6227 /* Returns a hash value for X (which really is a var_loc_list). */
6229 inline hashval_t
6230 decl_loc_hasher::hash (var_loc_list *x)
6232 return (hashval_t) x->decl_id;
6235 /* Return true if decl_id of var_loc_list X is the same as
6236 UID of decl *Y. */
6238 inline bool
6239 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6241 return (x->decl_id == DECL_UID (y));
6244 /* Return the var_loc list associated with a given declaration. */
6246 static inline var_loc_list *
6247 lookup_decl_loc (const_tree decl)
6249 if (!decl_loc_table)
6250 return NULL;
6251 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6254 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6256 inline hashval_t
6257 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6259 return (hashval_t) x->decl_id;
6262 /* Return true if decl_id of cached_dw_loc_list X is the same as
6263 UID of decl *Y. */
6265 inline bool
6266 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6268 return (x->decl_id == DECL_UID (y));
6271 /* Equate a DIE to a particular declaration. */
6273 static void
6274 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6276 unsigned int decl_id = DECL_UID (decl);
6278 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6279 decl_die->decl_id = decl_id;
6282 /* Return how many bits covers PIECE EXPR_LIST. */
6284 static HOST_WIDE_INT
6285 decl_piece_bitsize (rtx piece)
6287 int ret = (int) GET_MODE (piece);
6288 if (ret)
6289 return ret;
6290 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6291 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6292 return INTVAL (XEXP (XEXP (piece, 0), 0));
6295 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6297 static rtx *
6298 decl_piece_varloc_ptr (rtx piece)
6300 if ((int) GET_MODE (piece))
6301 return &XEXP (piece, 0);
6302 else
6303 return &XEXP (XEXP (piece, 0), 1);
6306 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6307 Next is the chain of following piece nodes. */
6309 static rtx_expr_list *
6310 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6312 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6313 return alloc_EXPR_LIST (bitsize, loc_note, next);
6314 else
6315 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6316 GEN_INT (bitsize),
6317 loc_note), next);
6320 /* Return rtx that should be stored into loc field for
6321 LOC_NOTE and BITPOS/BITSIZE. */
6323 static rtx
6324 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6325 HOST_WIDE_INT bitsize)
6327 if (bitsize != -1)
6329 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6330 if (bitpos != 0)
6331 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6333 return loc_note;
6336 /* This function either modifies location piece list *DEST in
6337 place (if SRC and INNER is NULL), or copies location piece list
6338 *SRC to *DEST while modifying it. Location BITPOS is modified
6339 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6340 not copied and if needed some padding around it is added.
6341 When modifying in place, DEST should point to EXPR_LIST where
6342 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6343 to the start of the whole list and INNER points to the EXPR_LIST
6344 where earlier pieces cover PIECE_BITPOS bits. */
6346 static void
6347 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6348 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6349 HOST_WIDE_INT bitsize, rtx loc_note)
6351 HOST_WIDE_INT diff;
6352 bool copy = inner != NULL;
6354 if (copy)
6356 /* First copy all nodes preceding the current bitpos. */
6357 while (src != inner)
6359 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6360 decl_piece_bitsize (*src), NULL_RTX);
6361 dest = &XEXP (*dest, 1);
6362 src = &XEXP (*src, 1);
6365 /* Add padding if needed. */
6366 if (bitpos != piece_bitpos)
6368 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6369 copy ? NULL_RTX : *dest);
6370 dest = &XEXP (*dest, 1);
6372 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6374 gcc_assert (!copy);
6375 /* A piece with correct bitpos and bitsize already exist,
6376 just update the location for it and return. */
6377 *decl_piece_varloc_ptr (*dest) = loc_note;
6378 return;
6380 /* Add the piece that changed. */
6381 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6382 dest = &XEXP (*dest, 1);
6383 /* Skip over pieces that overlap it. */
6384 diff = bitpos - piece_bitpos + bitsize;
6385 if (!copy)
6386 src = dest;
6387 while (diff > 0 && *src)
6389 rtx piece = *src;
6390 diff -= decl_piece_bitsize (piece);
6391 if (copy)
6392 src = &XEXP (piece, 1);
6393 else
6395 *src = XEXP (piece, 1);
6396 free_EXPR_LIST_node (piece);
6399 /* Add padding if needed. */
6400 if (diff < 0 && *src)
6402 if (!copy)
6403 dest = src;
6404 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6405 dest = &XEXP (*dest, 1);
6407 if (!copy)
6408 return;
6409 /* Finally copy all nodes following it. */
6410 while (*src)
6412 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6413 decl_piece_bitsize (*src), NULL_RTX);
6414 dest = &XEXP (*dest, 1);
6415 src = &XEXP (*src, 1);
6419 /* Add a variable location node to the linked list for DECL. */
6421 static struct var_loc_node *
6422 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6424 unsigned int decl_id;
6425 var_loc_list *temp;
6426 struct var_loc_node *loc = NULL;
6427 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6429 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6431 tree realdecl = DECL_DEBUG_EXPR (decl);
6432 if (handled_component_p (realdecl)
6433 || (TREE_CODE (realdecl) == MEM_REF
6434 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6436 bool reverse;
6437 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6438 &bitsize, &reverse);
6439 if (!innerdecl
6440 || !DECL_P (innerdecl)
6441 || DECL_IGNORED_P (innerdecl)
6442 || TREE_STATIC (innerdecl)
6443 || bitsize == 0
6444 || bitpos + bitsize > 256)
6445 return NULL;
6446 decl = innerdecl;
6450 decl_id = DECL_UID (decl);
6451 var_loc_list **slot
6452 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6453 if (*slot == NULL)
6455 temp = ggc_cleared_alloc<var_loc_list> ();
6456 temp->decl_id = decl_id;
6457 *slot = temp;
6459 else
6460 temp = *slot;
6462 /* For PARM_DECLs try to keep around the original incoming value,
6463 even if that means we'll emit a zero-range .debug_loc entry. */
6464 if (temp->last
6465 && temp->first == temp->last
6466 && TREE_CODE (decl) == PARM_DECL
6467 && NOTE_P (temp->first->loc)
6468 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6469 && DECL_INCOMING_RTL (decl)
6470 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6471 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6472 == GET_CODE (DECL_INCOMING_RTL (decl))
6473 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6474 && (bitsize != -1
6475 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6476 NOTE_VAR_LOCATION_LOC (loc_note))
6477 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6478 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6480 loc = ggc_cleared_alloc<var_loc_node> ();
6481 temp->first->next = loc;
6482 temp->last = loc;
6483 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6485 else if (temp->last)
6487 struct var_loc_node *last = temp->last, *unused = NULL;
6488 rtx *piece_loc = NULL, last_loc_note;
6489 HOST_WIDE_INT piece_bitpos = 0;
6490 if (last->next)
6492 last = last->next;
6493 gcc_assert (last->next == NULL);
6495 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6497 piece_loc = &last->loc;
6500 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6501 if (piece_bitpos + cur_bitsize > bitpos)
6502 break;
6503 piece_bitpos += cur_bitsize;
6504 piece_loc = &XEXP (*piece_loc, 1);
6506 while (*piece_loc);
6508 /* TEMP->LAST here is either pointer to the last but one or
6509 last element in the chained list, LAST is pointer to the
6510 last element. */
6511 if (label && strcmp (last->label, label) == 0 && last->view == view)
6513 /* For SRA optimized variables if there weren't any real
6514 insns since last note, just modify the last node. */
6515 if (piece_loc != NULL)
6517 adjust_piece_list (piece_loc, NULL, NULL,
6518 bitpos, piece_bitpos, bitsize, loc_note);
6519 return NULL;
6521 /* If the last note doesn't cover any instructions, remove it. */
6522 if (temp->last != last)
6524 temp->last->next = NULL;
6525 unused = last;
6526 last = temp->last;
6527 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6529 else
6531 gcc_assert (temp->first == temp->last
6532 || (temp->first->next == temp->last
6533 && TREE_CODE (decl) == PARM_DECL));
6534 memset (temp->last, '\0', sizeof (*temp->last));
6535 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6536 return temp->last;
6539 if (bitsize == -1 && NOTE_P (last->loc))
6540 last_loc_note = last->loc;
6541 else if (piece_loc != NULL
6542 && *piece_loc != NULL_RTX
6543 && piece_bitpos == bitpos
6544 && decl_piece_bitsize (*piece_loc) == bitsize)
6545 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6546 else
6547 last_loc_note = NULL_RTX;
6548 /* If the current location is the same as the end of the list,
6549 and either both or neither of the locations is uninitialized,
6550 we have nothing to do. */
6551 if (last_loc_note == NULL_RTX
6552 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6553 NOTE_VAR_LOCATION_LOC (loc_note)))
6554 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6555 != NOTE_VAR_LOCATION_STATUS (loc_note))
6556 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6557 == VAR_INIT_STATUS_UNINITIALIZED)
6558 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6559 == VAR_INIT_STATUS_UNINITIALIZED))))
6561 /* Add LOC to the end of list and update LAST. If the last
6562 element of the list has been removed above, reuse its
6563 memory for the new node, otherwise allocate a new one. */
6564 if (unused)
6566 loc = unused;
6567 memset (loc, '\0', sizeof (*loc));
6569 else
6570 loc = ggc_cleared_alloc<var_loc_node> ();
6571 if (bitsize == -1 || piece_loc == NULL)
6572 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6573 else
6574 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6575 bitpos, piece_bitpos, bitsize, loc_note);
6576 last->next = loc;
6577 /* Ensure TEMP->LAST will point either to the new last but one
6578 element of the chain, or to the last element in it. */
6579 if (last != temp->last)
6580 temp->last = last;
6582 else if (unused)
6583 ggc_free (unused);
6585 else
6587 loc = ggc_cleared_alloc<var_loc_node> ();
6588 temp->first = loc;
6589 temp->last = loc;
6590 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6592 return loc;
6595 /* Keep track of the number of spaces used to indent the
6596 output of the debugging routines that print the structure of
6597 the DIE internal representation. */
6598 static int print_indent;
6600 /* Indent the line the number of spaces given by print_indent. */
6602 static inline void
6603 print_spaces (FILE *outfile)
6605 fprintf (outfile, "%*s", print_indent, "");
6608 /* Print a type signature in hex. */
6610 static inline void
6611 print_signature (FILE *outfile, char *sig)
6613 int i;
6615 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6616 fprintf (outfile, "%02x", sig[i] & 0xff);
6619 static inline void
6620 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6622 if (discr_value->pos)
6623 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6624 else
6625 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6628 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6630 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6631 RECURSE, output location descriptor operations. */
6633 static void
6634 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6636 switch (val->val_class)
6638 case dw_val_class_addr:
6639 fprintf (outfile, "address");
6640 break;
6641 case dw_val_class_offset:
6642 fprintf (outfile, "offset");
6643 break;
6644 case dw_val_class_loc:
6645 fprintf (outfile, "location descriptor");
6646 if (val->v.val_loc == NULL)
6647 fprintf (outfile, " -> <null>\n");
6648 else if (recurse)
6650 fprintf (outfile, ":\n");
6651 print_indent += 4;
6652 print_loc_descr (val->v.val_loc, outfile);
6653 print_indent -= 4;
6655 else
6657 if (flag_dump_noaddr || flag_dump_unnumbered)
6658 fprintf (outfile, " #\n");
6659 else
6660 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6662 break;
6663 case dw_val_class_loc_list:
6664 fprintf (outfile, "location list -> label:%s",
6665 val->v.val_loc_list->ll_symbol);
6666 break;
6667 case dw_val_class_view_list:
6668 val = view_list_to_loc_list_val_node (val);
6669 fprintf (outfile, "location list with views -> labels:%s and %s",
6670 val->v.val_loc_list->ll_symbol,
6671 val->v.val_loc_list->vl_symbol);
6672 break;
6673 case dw_val_class_range_list:
6674 fprintf (outfile, "range list");
6675 break;
6676 case dw_val_class_const:
6677 case dw_val_class_const_implicit:
6678 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6679 break;
6680 case dw_val_class_unsigned_const:
6681 case dw_val_class_unsigned_const_implicit:
6682 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6683 break;
6684 case dw_val_class_const_double:
6685 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6686 HOST_WIDE_INT_PRINT_UNSIGNED")",
6687 val->v.val_double.high,
6688 val->v.val_double.low);
6689 break;
6690 case dw_val_class_wide_int:
6692 int i = val->v.val_wide->get_len ();
6693 fprintf (outfile, "constant (");
6694 gcc_assert (i > 0);
6695 if (val->v.val_wide->elt (i - 1) == 0)
6696 fprintf (outfile, "0x");
6697 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6698 val->v.val_wide->elt (--i));
6699 while (--i >= 0)
6700 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6701 val->v.val_wide->elt (i));
6702 fprintf (outfile, ")");
6703 break;
6705 case dw_val_class_vec:
6706 fprintf (outfile, "floating-point or vector constant");
6707 break;
6708 case dw_val_class_flag:
6709 fprintf (outfile, "%u", val->v.val_flag);
6710 break;
6711 case dw_val_class_die_ref:
6712 if (val->v.val_die_ref.die != NULL)
6714 dw_die_ref die = val->v.val_die_ref.die;
6716 if (die->comdat_type_p)
6718 fprintf (outfile, "die -> signature: ");
6719 print_signature (outfile,
6720 die->die_id.die_type_node->signature);
6722 else if (die->die_id.die_symbol)
6724 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6725 if (die->with_offset)
6726 fprintf (outfile, " + %ld", die->die_offset);
6728 else
6729 fprintf (outfile, "die -> %ld", die->die_offset);
6730 if (flag_dump_noaddr || flag_dump_unnumbered)
6731 fprintf (outfile, " #");
6732 else
6733 fprintf (outfile, " (%p)", (void *) die);
6735 else
6736 fprintf (outfile, "die -> <null>");
6737 break;
6738 case dw_val_class_vms_delta:
6739 fprintf (outfile, "delta: @slotcount(%s-%s)",
6740 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6741 break;
6742 case dw_val_class_symview:
6743 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6744 break;
6745 case dw_val_class_lbl_id:
6746 case dw_val_class_lineptr:
6747 case dw_val_class_macptr:
6748 case dw_val_class_loclistsptr:
6749 case dw_val_class_high_pc:
6750 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6751 break;
6752 case dw_val_class_str:
6753 if (val->v.val_str->str != NULL)
6754 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6755 else
6756 fprintf (outfile, "<null>");
6757 break;
6758 case dw_val_class_file:
6759 case dw_val_class_file_implicit:
6760 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6761 val->v.val_file->emitted_number);
6762 break;
6763 case dw_val_class_data8:
6765 int i;
6767 for (i = 0; i < 8; i++)
6768 fprintf (outfile, "%02x", val->v.val_data8[i]);
6769 break;
6771 case dw_val_class_discr_value:
6772 print_discr_value (outfile, &val->v.val_discr_value);
6773 break;
6774 case dw_val_class_discr_list:
6775 for (dw_discr_list_ref node = val->v.val_discr_list;
6776 node != NULL;
6777 node = node->dw_discr_next)
6779 if (node->dw_discr_range)
6781 fprintf (outfile, " .. ");
6782 print_discr_value (outfile, &node->dw_discr_lower_bound);
6783 print_discr_value (outfile, &node->dw_discr_upper_bound);
6785 else
6786 print_discr_value (outfile, &node->dw_discr_lower_bound);
6788 if (node->dw_discr_next != NULL)
6789 fprintf (outfile, " | ");
6791 default:
6792 break;
6796 /* Likewise, for a DIE attribute. */
6798 static void
6799 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6801 print_dw_val (&a->dw_attr_val, recurse, outfile);
6805 /* Print the list of operands in the LOC location description to OUTFILE. This
6806 routine is a debugging aid only. */
6808 static void
6809 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6811 dw_loc_descr_ref l = loc;
6813 if (loc == NULL)
6815 print_spaces (outfile);
6816 fprintf (outfile, "<null>\n");
6817 return;
6820 for (l = loc; l != NULL; l = l->dw_loc_next)
6822 print_spaces (outfile);
6823 if (flag_dump_noaddr || flag_dump_unnumbered)
6824 fprintf (outfile, "#");
6825 else
6826 fprintf (outfile, "(%p)", (void *) l);
6827 fprintf (outfile, " %s",
6828 dwarf_stack_op_name (l->dw_loc_opc));
6829 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6831 fprintf (outfile, " ");
6832 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6834 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6836 fprintf (outfile, ", ");
6837 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6839 fprintf (outfile, "\n");
6843 /* Print the information associated with a given DIE, and its children.
6844 This routine is a debugging aid only. */
6846 static void
6847 print_die (dw_die_ref die, FILE *outfile)
6849 dw_attr_node *a;
6850 dw_die_ref c;
6851 unsigned ix;
6853 print_spaces (outfile);
6854 fprintf (outfile, "DIE %4ld: %s ",
6855 die->die_offset, dwarf_tag_name (die->die_tag));
6856 if (flag_dump_noaddr || flag_dump_unnumbered)
6857 fprintf (outfile, "#\n");
6858 else
6859 fprintf (outfile, "(%p)\n", (void*) die);
6860 print_spaces (outfile);
6861 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6862 fprintf (outfile, " offset: %ld", die->die_offset);
6863 fprintf (outfile, " mark: %d\n", die->die_mark);
6865 if (die->comdat_type_p)
6867 print_spaces (outfile);
6868 fprintf (outfile, " signature: ");
6869 print_signature (outfile, die->die_id.die_type_node->signature);
6870 fprintf (outfile, "\n");
6873 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6875 print_spaces (outfile);
6876 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6878 print_attribute (a, true, outfile);
6879 fprintf (outfile, "\n");
6882 if (die->die_child != NULL)
6884 print_indent += 4;
6885 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6886 print_indent -= 4;
6888 if (print_indent == 0)
6889 fprintf (outfile, "\n");
6892 /* Print the list of operations in the LOC location description. */
6894 DEBUG_FUNCTION void
6895 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6897 print_loc_descr (loc, stderr);
6900 /* Print the information collected for a given DIE. */
6902 DEBUG_FUNCTION void
6903 debug_dwarf_die (dw_die_ref die)
6905 print_die (die, stderr);
6908 DEBUG_FUNCTION void
6909 debug (die_struct &ref)
6911 print_die (&ref, stderr);
6914 DEBUG_FUNCTION void
6915 debug (die_struct *ptr)
6917 if (ptr)
6918 debug (*ptr);
6919 else
6920 fprintf (stderr, "<nil>\n");
6924 /* Print all DWARF information collected for the compilation unit.
6925 This routine is a debugging aid only. */
6927 DEBUG_FUNCTION void
6928 debug_dwarf (void)
6930 print_indent = 0;
6931 print_die (comp_unit_die (), stderr);
6934 /* Verify the DIE tree structure. */
6936 DEBUG_FUNCTION void
6937 verify_die (dw_die_ref die)
6939 gcc_assert (!die->die_mark);
6940 if (die->die_parent == NULL
6941 && die->die_sib == NULL)
6942 return;
6943 /* Verify the die_sib list is cyclic. */
6944 dw_die_ref x = die;
6947 x->die_mark = 1;
6948 x = x->die_sib;
6950 while (x && !x->die_mark);
6951 gcc_assert (x == die);
6952 x = die;
6955 /* Verify all dies have the same parent. */
6956 gcc_assert (x->die_parent == die->die_parent);
6957 if (x->die_child)
6959 /* Verify the child has the proper parent and recurse. */
6960 gcc_assert (x->die_child->die_parent == x);
6961 verify_die (x->die_child);
6963 x->die_mark = 0;
6964 x = x->die_sib;
6966 while (x && x->die_mark);
6969 /* Sanity checks on DIEs. */
6971 static void
6972 check_die (dw_die_ref die)
6974 unsigned ix;
6975 dw_attr_node *a;
6976 bool inline_found = false;
6977 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6978 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6979 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6981 switch (a->dw_attr)
6983 case DW_AT_inline:
6984 if (a->dw_attr_val.v.val_unsigned)
6985 inline_found = true;
6986 break;
6987 case DW_AT_location:
6988 ++n_location;
6989 break;
6990 case DW_AT_low_pc:
6991 ++n_low_pc;
6992 break;
6993 case DW_AT_high_pc:
6994 ++n_high_pc;
6995 break;
6996 case DW_AT_artificial:
6997 ++n_artificial;
6998 break;
6999 case DW_AT_decl_column:
7000 ++n_decl_column;
7001 break;
7002 case DW_AT_decl_line:
7003 ++n_decl_line;
7004 break;
7005 case DW_AT_decl_file:
7006 ++n_decl_file;
7007 break;
7008 default:
7009 break;
7012 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
7013 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
7015 fprintf (stderr, "Duplicate attributes in DIE:\n");
7016 debug_dwarf_die (die);
7017 gcc_unreachable ();
7019 if (inline_found)
7021 /* A debugging information entry that is a member of an abstract
7022 instance tree [that has DW_AT_inline] should not contain any
7023 attributes which describe aspects of the subroutine which vary
7024 between distinct inlined expansions or distinct out-of-line
7025 expansions. */
7026 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7027 gcc_assert (a->dw_attr != DW_AT_low_pc
7028 && a->dw_attr != DW_AT_high_pc
7029 && a->dw_attr != DW_AT_location
7030 && a->dw_attr != DW_AT_frame_base
7031 && a->dw_attr != DW_AT_call_all_calls
7032 && a->dw_attr != DW_AT_GNU_all_call_sites);
7036 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7037 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7038 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7040 /* Calculate the checksum of a location expression. */
7042 static inline void
7043 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7045 int tem;
7046 inchash::hash hstate;
7047 hashval_t hash;
7049 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7050 CHECKSUM (tem);
7051 hash_loc_operands (loc, hstate);
7052 hash = hstate.end();
7053 CHECKSUM (hash);
7056 /* Calculate the checksum of an attribute. */
7058 static void
7059 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7061 dw_loc_descr_ref loc;
7062 rtx r;
7064 CHECKSUM (at->dw_attr);
7066 /* We don't care that this was compiled with a different compiler
7067 snapshot; if the output is the same, that's what matters. */
7068 if (at->dw_attr == DW_AT_producer)
7069 return;
7071 switch (AT_class (at))
7073 case dw_val_class_const:
7074 case dw_val_class_const_implicit:
7075 CHECKSUM (at->dw_attr_val.v.val_int);
7076 break;
7077 case dw_val_class_unsigned_const:
7078 case dw_val_class_unsigned_const_implicit:
7079 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7080 break;
7081 case dw_val_class_const_double:
7082 CHECKSUM (at->dw_attr_val.v.val_double);
7083 break;
7084 case dw_val_class_wide_int:
7085 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7086 get_full_len (*at->dw_attr_val.v.val_wide)
7087 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7088 break;
7089 case dw_val_class_vec:
7090 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7091 (at->dw_attr_val.v.val_vec.length
7092 * at->dw_attr_val.v.val_vec.elt_size));
7093 break;
7094 case dw_val_class_flag:
7095 CHECKSUM (at->dw_attr_val.v.val_flag);
7096 break;
7097 case dw_val_class_str:
7098 CHECKSUM_STRING (AT_string (at));
7099 break;
7101 case dw_val_class_addr:
7102 r = AT_addr (at);
7103 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7104 CHECKSUM_STRING (XSTR (r, 0));
7105 break;
7107 case dw_val_class_offset:
7108 CHECKSUM (at->dw_attr_val.v.val_offset);
7109 break;
7111 case dw_val_class_loc:
7112 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7113 loc_checksum (loc, ctx);
7114 break;
7116 case dw_val_class_die_ref:
7117 die_checksum (AT_ref (at), ctx, mark);
7118 break;
7120 case dw_val_class_fde_ref:
7121 case dw_val_class_vms_delta:
7122 case dw_val_class_symview:
7123 case dw_val_class_lbl_id:
7124 case dw_val_class_lineptr:
7125 case dw_val_class_macptr:
7126 case dw_val_class_loclistsptr:
7127 case dw_val_class_high_pc:
7128 break;
7130 case dw_val_class_file:
7131 case dw_val_class_file_implicit:
7132 CHECKSUM_STRING (AT_file (at)->filename);
7133 break;
7135 case dw_val_class_data8:
7136 CHECKSUM (at->dw_attr_val.v.val_data8);
7137 break;
7139 default:
7140 break;
7144 /* Calculate the checksum of a DIE. */
7146 static void
7147 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7149 dw_die_ref c;
7150 dw_attr_node *a;
7151 unsigned ix;
7153 /* To avoid infinite recursion. */
7154 if (die->die_mark)
7156 CHECKSUM (die->die_mark);
7157 return;
7159 die->die_mark = ++(*mark);
7161 CHECKSUM (die->die_tag);
7163 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7164 attr_checksum (a, ctx, mark);
7166 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7169 #undef CHECKSUM
7170 #undef CHECKSUM_BLOCK
7171 #undef CHECKSUM_STRING
7173 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7174 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7175 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7176 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7177 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7178 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7179 #define CHECKSUM_ATTR(FOO) \
7180 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7182 /* Calculate the checksum of a number in signed LEB128 format. */
7184 static void
7185 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7187 unsigned char byte;
7188 bool more;
7190 while (1)
7192 byte = (value & 0x7f);
7193 value >>= 7;
7194 more = !((value == 0 && (byte & 0x40) == 0)
7195 || (value == -1 && (byte & 0x40) != 0));
7196 if (more)
7197 byte |= 0x80;
7198 CHECKSUM (byte);
7199 if (!more)
7200 break;
7204 /* Calculate the checksum of a number in unsigned LEB128 format. */
7206 static void
7207 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7209 while (1)
7211 unsigned char byte = (value & 0x7f);
7212 value >>= 7;
7213 if (value != 0)
7214 /* More bytes to follow. */
7215 byte |= 0x80;
7216 CHECKSUM (byte);
7217 if (value == 0)
7218 break;
7222 /* Checksum the context of the DIE. This adds the names of any
7223 surrounding namespaces or structures to the checksum. */
7225 static void
7226 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7228 const char *name;
7229 dw_die_ref spec;
7230 int tag = die->die_tag;
7232 if (tag != DW_TAG_namespace
7233 && tag != DW_TAG_structure_type
7234 && tag != DW_TAG_class_type)
7235 return;
7237 name = get_AT_string (die, DW_AT_name);
7239 spec = get_AT_ref (die, DW_AT_specification);
7240 if (spec != NULL)
7241 die = spec;
7243 if (die->die_parent != NULL)
7244 checksum_die_context (die->die_parent, ctx);
7246 CHECKSUM_ULEB128 ('C');
7247 CHECKSUM_ULEB128 (tag);
7248 if (name != NULL)
7249 CHECKSUM_STRING (name);
7252 /* Calculate the checksum of a location expression. */
7254 static inline void
7255 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7257 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7258 were emitted as a DW_FORM_sdata instead of a location expression. */
7259 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7261 CHECKSUM_ULEB128 (DW_FORM_sdata);
7262 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7263 return;
7266 /* Otherwise, just checksum the raw location expression. */
7267 while (loc != NULL)
7269 inchash::hash hstate;
7270 hashval_t hash;
7272 CHECKSUM_ULEB128 (loc->dtprel);
7273 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7274 hash_loc_operands (loc, hstate);
7275 hash = hstate.end ();
7276 CHECKSUM (hash);
7277 loc = loc->dw_loc_next;
7281 /* Calculate the checksum of an attribute. */
7283 static void
7284 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7285 struct md5_ctx *ctx, int *mark)
7287 dw_loc_descr_ref loc;
7288 rtx r;
7290 if (AT_class (at) == dw_val_class_die_ref)
7292 dw_die_ref target_die = AT_ref (at);
7294 /* For pointer and reference types, we checksum only the (qualified)
7295 name of the target type (if there is a name). For friend entries,
7296 we checksum only the (qualified) name of the target type or function.
7297 This allows the checksum to remain the same whether the target type
7298 is complete or not. */
7299 if ((at->dw_attr == DW_AT_type
7300 && (tag == DW_TAG_pointer_type
7301 || tag == DW_TAG_reference_type
7302 || tag == DW_TAG_rvalue_reference_type
7303 || tag == DW_TAG_ptr_to_member_type))
7304 || (at->dw_attr == DW_AT_friend
7305 && tag == DW_TAG_friend))
7307 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7309 if (name_attr != NULL)
7311 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7313 if (decl == NULL)
7314 decl = target_die;
7315 CHECKSUM_ULEB128 ('N');
7316 CHECKSUM_ULEB128 (at->dw_attr);
7317 if (decl->die_parent != NULL)
7318 checksum_die_context (decl->die_parent, ctx);
7319 CHECKSUM_ULEB128 ('E');
7320 CHECKSUM_STRING (AT_string (name_attr));
7321 return;
7325 /* For all other references to another DIE, we check to see if the
7326 target DIE has already been visited. If it has, we emit a
7327 backward reference; if not, we descend recursively. */
7328 if (target_die->die_mark > 0)
7330 CHECKSUM_ULEB128 ('R');
7331 CHECKSUM_ULEB128 (at->dw_attr);
7332 CHECKSUM_ULEB128 (target_die->die_mark);
7334 else
7336 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7338 if (decl == NULL)
7339 decl = target_die;
7340 target_die->die_mark = ++(*mark);
7341 CHECKSUM_ULEB128 ('T');
7342 CHECKSUM_ULEB128 (at->dw_attr);
7343 if (decl->die_parent != NULL)
7344 checksum_die_context (decl->die_parent, ctx);
7345 die_checksum_ordered (target_die, ctx, mark);
7347 return;
7350 CHECKSUM_ULEB128 ('A');
7351 CHECKSUM_ULEB128 (at->dw_attr);
7353 switch (AT_class (at))
7355 case dw_val_class_const:
7356 case dw_val_class_const_implicit:
7357 CHECKSUM_ULEB128 (DW_FORM_sdata);
7358 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7359 break;
7361 case dw_val_class_unsigned_const:
7362 case dw_val_class_unsigned_const_implicit:
7363 CHECKSUM_ULEB128 (DW_FORM_sdata);
7364 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7365 break;
7367 case dw_val_class_const_double:
7368 CHECKSUM_ULEB128 (DW_FORM_block);
7369 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7370 CHECKSUM (at->dw_attr_val.v.val_double);
7371 break;
7373 case dw_val_class_wide_int:
7374 CHECKSUM_ULEB128 (DW_FORM_block);
7375 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7376 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7377 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7378 get_full_len (*at->dw_attr_val.v.val_wide)
7379 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7380 break;
7382 case dw_val_class_vec:
7383 CHECKSUM_ULEB128 (DW_FORM_block);
7384 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7385 * at->dw_attr_val.v.val_vec.elt_size);
7386 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7387 (at->dw_attr_val.v.val_vec.length
7388 * at->dw_attr_val.v.val_vec.elt_size));
7389 break;
7391 case dw_val_class_flag:
7392 CHECKSUM_ULEB128 (DW_FORM_flag);
7393 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7394 break;
7396 case dw_val_class_str:
7397 CHECKSUM_ULEB128 (DW_FORM_string);
7398 CHECKSUM_STRING (AT_string (at));
7399 break;
7401 case dw_val_class_addr:
7402 r = AT_addr (at);
7403 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7404 CHECKSUM_ULEB128 (DW_FORM_string);
7405 CHECKSUM_STRING (XSTR (r, 0));
7406 break;
7408 case dw_val_class_offset:
7409 CHECKSUM_ULEB128 (DW_FORM_sdata);
7410 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7411 break;
7413 case dw_val_class_loc:
7414 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7415 loc_checksum_ordered (loc, ctx);
7416 break;
7418 case dw_val_class_fde_ref:
7419 case dw_val_class_symview:
7420 case dw_val_class_lbl_id:
7421 case dw_val_class_lineptr:
7422 case dw_val_class_macptr:
7423 case dw_val_class_loclistsptr:
7424 case dw_val_class_high_pc:
7425 break;
7427 case dw_val_class_file:
7428 case dw_val_class_file_implicit:
7429 CHECKSUM_ULEB128 (DW_FORM_string);
7430 CHECKSUM_STRING (AT_file (at)->filename);
7431 break;
7433 case dw_val_class_data8:
7434 CHECKSUM (at->dw_attr_val.v.val_data8);
7435 break;
7437 default:
7438 break;
7442 struct checksum_attributes
7444 dw_attr_node *at_name;
7445 dw_attr_node *at_type;
7446 dw_attr_node *at_friend;
7447 dw_attr_node *at_accessibility;
7448 dw_attr_node *at_address_class;
7449 dw_attr_node *at_alignment;
7450 dw_attr_node *at_allocated;
7451 dw_attr_node *at_artificial;
7452 dw_attr_node *at_associated;
7453 dw_attr_node *at_binary_scale;
7454 dw_attr_node *at_bit_offset;
7455 dw_attr_node *at_bit_size;
7456 dw_attr_node *at_bit_stride;
7457 dw_attr_node *at_byte_size;
7458 dw_attr_node *at_byte_stride;
7459 dw_attr_node *at_const_value;
7460 dw_attr_node *at_containing_type;
7461 dw_attr_node *at_count;
7462 dw_attr_node *at_data_location;
7463 dw_attr_node *at_data_member_location;
7464 dw_attr_node *at_decimal_scale;
7465 dw_attr_node *at_decimal_sign;
7466 dw_attr_node *at_default_value;
7467 dw_attr_node *at_digit_count;
7468 dw_attr_node *at_discr;
7469 dw_attr_node *at_discr_list;
7470 dw_attr_node *at_discr_value;
7471 dw_attr_node *at_encoding;
7472 dw_attr_node *at_endianity;
7473 dw_attr_node *at_explicit;
7474 dw_attr_node *at_is_optional;
7475 dw_attr_node *at_location;
7476 dw_attr_node *at_lower_bound;
7477 dw_attr_node *at_mutable;
7478 dw_attr_node *at_ordering;
7479 dw_attr_node *at_picture_string;
7480 dw_attr_node *at_prototyped;
7481 dw_attr_node *at_small;
7482 dw_attr_node *at_segment;
7483 dw_attr_node *at_string_length;
7484 dw_attr_node *at_string_length_bit_size;
7485 dw_attr_node *at_string_length_byte_size;
7486 dw_attr_node *at_threads_scaled;
7487 dw_attr_node *at_upper_bound;
7488 dw_attr_node *at_use_location;
7489 dw_attr_node *at_use_UTF8;
7490 dw_attr_node *at_variable_parameter;
7491 dw_attr_node *at_virtuality;
7492 dw_attr_node *at_visibility;
7493 dw_attr_node *at_vtable_elem_location;
7496 /* Collect the attributes that we will want to use for the checksum. */
7498 static void
7499 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7501 dw_attr_node *a;
7502 unsigned ix;
7504 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7506 switch (a->dw_attr)
7508 case DW_AT_name:
7509 attrs->at_name = a;
7510 break;
7511 case DW_AT_type:
7512 attrs->at_type = a;
7513 break;
7514 case DW_AT_friend:
7515 attrs->at_friend = a;
7516 break;
7517 case DW_AT_accessibility:
7518 attrs->at_accessibility = a;
7519 break;
7520 case DW_AT_address_class:
7521 attrs->at_address_class = a;
7522 break;
7523 case DW_AT_alignment:
7524 attrs->at_alignment = a;
7525 break;
7526 case DW_AT_allocated:
7527 attrs->at_allocated = a;
7528 break;
7529 case DW_AT_artificial:
7530 attrs->at_artificial = a;
7531 break;
7532 case DW_AT_associated:
7533 attrs->at_associated = a;
7534 break;
7535 case DW_AT_binary_scale:
7536 attrs->at_binary_scale = a;
7537 break;
7538 case DW_AT_bit_offset:
7539 attrs->at_bit_offset = a;
7540 break;
7541 case DW_AT_bit_size:
7542 attrs->at_bit_size = a;
7543 break;
7544 case DW_AT_bit_stride:
7545 attrs->at_bit_stride = a;
7546 break;
7547 case DW_AT_byte_size:
7548 attrs->at_byte_size = a;
7549 break;
7550 case DW_AT_byte_stride:
7551 attrs->at_byte_stride = a;
7552 break;
7553 case DW_AT_const_value:
7554 attrs->at_const_value = a;
7555 break;
7556 case DW_AT_containing_type:
7557 attrs->at_containing_type = a;
7558 break;
7559 case DW_AT_count:
7560 attrs->at_count = a;
7561 break;
7562 case DW_AT_data_location:
7563 attrs->at_data_location = a;
7564 break;
7565 case DW_AT_data_member_location:
7566 attrs->at_data_member_location = a;
7567 break;
7568 case DW_AT_decimal_scale:
7569 attrs->at_decimal_scale = a;
7570 break;
7571 case DW_AT_decimal_sign:
7572 attrs->at_decimal_sign = a;
7573 break;
7574 case DW_AT_default_value:
7575 attrs->at_default_value = a;
7576 break;
7577 case DW_AT_digit_count:
7578 attrs->at_digit_count = a;
7579 break;
7580 case DW_AT_discr:
7581 attrs->at_discr = a;
7582 break;
7583 case DW_AT_discr_list:
7584 attrs->at_discr_list = a;
7585 break;
7586 case DW_AT_discr_value:
7587 attrs->at_discr_value = a;
7588 break;
7589 case DW_AT_encoding:
7590 attrs->at_encoding = a;
7591 break;
7592 case DW_AT_endianity:
7593 attrs->at_endianity = a;
7594 break;
7595 case DW_AT_explicit:
7596 attrs->at_explicit = a;
7597 break;
7598 case DW_AT_is_optional:
7599 attrs->at_is_optional = a;
7600 break;
7601 case DW_AT_location:
7602 attrs->at_location = a;
7603 break;
7604 case DW_AT_lower_bound:
7605 attrs->at_lower_bound = a;
7606 break;
7607 case DW_AT_mutable:
7608 attrs->at_mutable = a;
7609 break;
7610 case DW_AT_ordering:
7611 attrs->at_ordering = a;
7612 break;
7613 case DW_AT_picture_string:
7614 attrs->at_picture_string = a;
7615 break;
7616 case DW_AT_prototyped:
7617 attrs->at_prototyped = a;
7618 break;
7619 case DW_AT_small:
7620 attrs->at_small = a;
7621 break;
7622 case DW_AT_segment:
7623 attrs->at_segment = a;
7624 break;
7625 case DW_AT_string_length:
7626 attrs->at_string_length = a;
7627 break;
7628 case DW_AT_string_length_bit_size:
7629 attrs->at_string_length_bit_size = a;
7630 break;
7631 case DW_AT_string_length_byte_size:
7632 attrs->at_string_length_byte_size = a;
7633 break;
7634 case DW_AT_threads_scaled:
7635 attrs->at_threads_scaled = a;
7636 break;
7637 case DW_AT_upper_bound:
7638 attrs->at_upper_bound = a;
7639 break;
7640 case DW_AT_use_location:
7641 attrs->at_use_location = a;
7642 break;
7643 case DW_AT_use_UTF8:
7644 attrs->at_use_UTF8 = a;
7645 break;
7646 case DW_AT_variable_parameter:
7647 attrs->at_variable_parameter = a;
7648 break;
7649 case DW_AT_virtuality:
7650 attrs->at_virtuality = a;
7651 break;
7652 case DW_AT_visibility:
7653 attrs->at_visibility = a;
7654 break;
7655 case DW_AT_vtable_elem_location:
7656 attrs->at_vtable_elem_location = a;
7657 break;
7658 default:
7659 break;
7664 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7666 static void
7667 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7669 dw_die_ref c;
7670 dw_die_ref decl;
7671 struct checksum_attributes attrs;
7673 CHECKSUM_ULEB128 ('D');
7674 CHECKSUM_ULEB128 (die->die_tag);
7676 memset (&attrs, 0, sizeof (attrs));
7678 decl = get_AT_ref (die, DW_AT_specification);
7679 if (decl != NULL)
7680 collect_checksum_attributes (&attrs, decl);
7681 collect_checksum_attributes (&attrs, die);
7683 CHECKSUM_ATTR (attrs.at_name);
7684 CHECKSUM_ATTR (attrs.at_accessibility);
7685 CHECKSUM_ATTR (attrs.at_address_class);
7686 CHECKSUM_ATTR (attrs.at_allocated);
7687 CHECKSUM_ATTR (attrs.at_artificial);
7688 CHECKSUM_ATTR (attrs.at_associated);
7689 CHECKSUM_ATTR (attrs.at_binary_scale);
7690 CHECKSUM_ATTR (attrs.at_bit_offset);
7691 CHECKSUM_ATTR (attrs.at_bit_size);
7692 CHECKSUM_ATTR (attrs.at_bit_stride);
7693 CHECKSUM_ATTR (attrs.at_byte_size);
7694 CHECKSUM_ATTR (attrs.at_byte_stride);
7695 CHECKSUM_ATTR (attrs.at_const_value);
7696 CHECKSUM_ATTR (attrs.at_containing_type);
7697 CHECKSUM_ATTR (attrs.at_count);
7698 CHECKSUM_ATTR (attrs.at_data_location);
7699 CHECKSUM_ATTR (attrs.at_data_member_location);
7700 CHECKSUM_ATTR (attrs.at_decimal_scale);
7701 CHECKSUM_ATTR (attrs.at_decimal_sign);
7702 CHECKSUM_ATTR (attrs.at_default_value);
7703 CHECKSUM_ATTR (attrs.at_digit_count);
7704 CHECKSUM_ATTR (attrs.at_discr);
7705 CHECKSUM_ATTR (attrs.at_discr_list);
7706 CHECKSUM_ATTR (attrs.at_discr_value);
7707 CHECKSUM_ATTR (attrs.at_encoding);
7708 CHECKSUM_ATTR (attrs.at_endianity);
7709 CHECKSUM_ATTR (attrs.at_explicit);
7710 CHECKSUM_ATTR (attrs.at_is_optional);
7711 CHECKSUM_ATTR (attrs.at_location);
7712 CHECKSUM_ATTR (attrs.at_lower_bound);
7713 CHECKSUM_ATTR (attrs.at_mutable);
7714 CHECKSUM_ATTR (attrs.at_ordering);
7715 CHECKSUM_ATTR (attrs.at_picture_string);
7716 CHECKSUM_ATTR (attrs.at_prototyped);
7717 CHECKSUM_ATTR (attrs.at_small);
7718 CHECKSUM_ATTR (attrs.at_segment);
7719 CHECKSUM_ATTR (attrs.at_string_length);
7720 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7721 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7722 CHECKSUM_ATTR (attrs.at_threads_scaled);
7723 CHECKSUM_ATTR (attrs.at_upper_bound);
7724 CHECKSUM_ATTR (attrs.at_use_location);
7725 CHECKSUM_ATTR (attrs.at_use_UTF8);
7726 CHECKSUM_ATTR (attrs.at_variable_parameter);
7727 CHECKSUM_ATTR (attrs.at_virtuality);
7728 CHECKSUM_ATTR (attrs.at_visibility);
7729 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7730 CHECKSUM_ATTR (attrs.at_type);
7731 CHECKSUM_ATTR (attrs.at_friend);
7732 CHECKSUM_ATTR (attrs.at_alignment);
7734 /* Checksum the child DIEs. */
7735 c = die->die_child;
7736 if (c) do {
7737 dw_attr_node *name_attr;
7739 c = c->die_sib;
7740 name_attr = get_AT (c, DW_AT_name);
7741 if (is_template_instantiation (c))
7743 /* Ignore instantiations of member type and function templates. */
7745 else if (name_attr != NULL
7746 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7748 /* Use a shallow checksum for named nested types and member
7749 functions. */
7750 CHECKSUM_ULEB128 ('S');
7751 CHECKSUM_ULEB128 (c->die_tag);
7752 CHECKSUM_STRING (AT_string (name_attr));
7754 else
7756 /* Use a deep checksum for other children. */
7757 /* Mark this DIE so it gets processed when unmarking. */
7758 if (c->die_mark == 0)
7759 c->die_mark = -1;
7760 die_checksum_ordered (c, ctx, mark);
7762 } while (c != die->die_child);
7764 CHECKSUM_ULEB128 (0);
7767 /* Add a type name and tag to a hash. */
7768 static void
7769 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7771 CHECKSUM_ULEB128 (tag);
7772 CHECKSUM_STRING (name);
7775 #undef CHECKSUM
7776 #undef CHECKSUM_STRING
7777 #undef CHECKSUM_ATTR
7778 #undef CHECKSUM_LEB128
7779 #undef CHECKSUM_ULEB128
7781 /* Generate the type signature for DIE. This is computed by generating an
7782 MD5 checksum over the DIE's tag, its relevant attributes, and its
7783 children. Attributes that are references to other DIEs are processed
7784 by recursion, using the MARK field to prevent infinite recursion.
7785 If the DIE is nested inside a namespace or another type, we also
7786 need to include that context in the signature. The lower 64 bits
7787 of the resulting MD5 checksum comprise the signature. */
7789 static void
7790 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7792 int mark;
7793 const char *name;
7794 unsigned char checksum[16];
7795 struct md5_ctx ctx;
7796 dw_die_ref decl;
7797 dw_die_ref parent;
7799 name = get_AT_string (die, DW_AT_name);
7800 decl = get_AT_ref (die, DW_AT_specification);
7801 parent = get_die_parent (die);
7803 /* First, compute a signature for just the type name (and its surrounding
7804 context, if any. This is stored in the type unit DIE for link-time
7805 ODR (one-definition rule) checking. */
7807 if (is_cxx () && name != NULL)
7809 md5_init_ctx (&ctx);
7811 /* Checksum the names of surrounding namespaces and structures. */
7812 if (parent != NULL)
7813 checksum_die_context (parent, &ctx);
7815 /* Checksum the current DIE. */
7816 die_odr_checksum (die->die_tag, name, &ctx);
7817 md5_finish_ctx (&ctx, checksum);
7819 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7822 /* Next, compute the complete type signature. */
7824 md5_init_ctx (&ctx);
7825 mark = 1;
7826 die->die_mark = mark;
7828 /* Checksum the names of surrounding namespaces and structures. */
7829 if (parent != NULL)
7830 checksum_die_context (parent, &ctx);
7832 /* Checksum the DIE and its children. */
7833 die_checksum_ordered (die, &ctx, &mark);
7834 unmark_all_dies (die);
7835 md5_finish_ctx (&ctx, checksum);
7837 /* Store the signature in the type node and link the type DIE and the
7838 type node together. */
7839 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7840 DWARF_TYPE_SIGNATURE_SIZE);
7841 die->comdat_type_p = true;
7842 die->die_id.die_type_node = type_node;
7843 type_node->type_die = die;
7845 /* If the DIE is a specification, link its declaration to the type node
7846 as well. */
7847 if (decl != NULL)
7849 decl->comdat_type_p = true;
7850 decl->die_id.die_type_node = type_node;
7854 /* Do the location expressions look same? */
7855 static inline bool
7856 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7858 return loc1->dw_loc_opc == loc2->dw_loc_opc
7859 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7860 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7863 /* Do the values look the same? */
7864 static bool
7865 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7867 dw_loc_descr_ref loc1, loc2;
7868 rtx r1, r2;
7870 if (v1->val_class != v2->val_class)
7871 return false;
7873 switch (v1->val_class)
7875 case dw_val_class_const:
7876 case dw_val_class_const_implicit:
7877 return v1->v.val_int == v2->v.val_int;
7878 case dw_val_class_unsigned_const:
7879 case dw_val_class_unsigned_const_implicit:
7880 return v1->v.val_unsigned == v2->v.val_unsigned;
7881 case dw_val_class_const_double:
7882 return v1->v.val_double.high == v2->v.val_double.high
7883 && v1->v.val_double.low == v2->v.val_double.low;
7884 case dw_val_class_wide_int:
7885 return *v1->v.val_wide == *v2->v.val_wide;
7886 case dw_val_class_vec:
7887 if (v1->v.val_vec.length != v2->v.val_vec.length
7888 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7889 return false;
7890 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7891 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7892 return false;
7893 return true;
7894 case dw_val_class_flag:
7895 return v1->v.val_flag == v2->v.val_flag;
7896 case dw_val_class_str:
7897 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7899 case dw_val_class_addr:
7900 r1 = v1->v.val_addr;
7901 r2 = v2->v.val_addr;
7902 if (GET_CODE (r1) != GET_CODE (r2))
7903 return false;
7904 return !rtx_equal_p (r1, r2);
7906 case dw_val_class_offset:
7907 return v1->v.val_offset == v2->v.val_offset;
7909 case dw_val_class_loc:
7910 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7911 loc1 && loc2;
7912 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7913 if (!same_loc_p (loc1, loc2, mark))
7914 return false;
7915 return !loc1 && !loc2;
7917 case dw_val_class_die_ref:
7918 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7920 case dw_val_class_symview:
7921 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7923 case dw_val_class_fde_ref:
7924 case dw_val_class_vms_delta:
7925 case dw_val_class_lbl_id:
7926 case dw_val_class_lineptr:
7927 case dw_val_class_macptr:
7928 case dw_val_class_loclistsptr:
7929 case dw_val_class_high_pc:
7930 return true;
7932 case dw_val_class_file:
7933 case dw_val_class_file_implicit:
7934 return v1->v.val_file == v2->v.val_file;
7936 case dw_val_class_data8:
7937 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7939 default:
7940 return true;
7944 /* Do the attributes look the same? */
7946 static bool
7947 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7949 if (at1->dw_attr != at2->dw_attr)
7950 return false;
7952 /* We don't care that this was compiled with a different compiler
7953 snapshot; if the output is the same, that's what matters. */
7954 if (at1->dw_attr == DW_AT_producer)
7955 return true;
7957 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7960 /* Do the dies look the same? */
7962 static bool
7963 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7965 dw_die_ref c1, c2;
7966 dw_attr_node *a1;
7967 unsigned ix;
7969 /* To avoid infinite recursion. */
7970 if (die1->die_mark)
7971 return die1->die_mark == die2->die_mark;
7972 die1->die_mark = die2->die_mark = ++(*mark);
7974 if (die1->die_tag != die2->die_tag)
7975 return false;
7977 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7978 return false;
7980 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7981 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7982 return false;
7984 c1 = die1->die_child;
7985 c2 = die2->die_child;
7986 if (! c1)
7988 if (c2)
7989 return false;
7991 else
7992 for (;;)
7994 if (!same_die_p (c1, c2, mark))
7995 return false;
7996 c1 = c1->die_sib;
7997 c2 = c2->die_sib;
7998 if (c1 == die1->die_child)
8000 if (c2 == die2->die_child)
8001 break;
8002 else
8003 return false;
8007 return true;
8010 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8011 children, and set die_symbol. */
8013 static void
8014 compute_comp_unit_symbol (dw_die_ref unit_die)
8016 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8017 const char *base = die_name ? lbasename (die_name) : "anonymous";
8018 char *name = XALLOCAVEC (char, strlen (base) + 64);
8019 char *p;
8020 int i, mark;
8021 unsigned char checksum[16];
8022 struct md5_ctx ctx;
8024 /* Compute the checksum of the DIE, then append part of it as hex digits to
8025 the name filename of the unit. */
8027 md5_init_ctx (&ctx);
8028 mark = 0;
8029 die_checksum (unit_die, &ctx, &mark);
8030 unmark_all_dies (unit_die);
8031 md5_finish_ctx (&ctx, checksum);
8033 /* When we this for comp_unit_die () we have a DW_AT_name that might
8034 not start with a letter but with anything valid for filenames and
8035 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8036 character is not a letter. */
8037 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8038 clean_symbol_name (name);
8040 p = name + strlen (name);
8041 for (i = 0; i < 4; i++)
8043 sprintf (p, "%.2x", checksum[i]);
8044 p += 2;
8047 unit_die->die_id.die_symbol = xstrdup (name);
8050 /* Returns true if DIE represents a type, in the sense of TYPE_P. */
8052 static bool
8053 is_type_die (dw_die_ref die)
8055 switch (die->die_tag)
8057 case DW_TAG_array_type:
8058 case DW_TAG_class_type:
8059 case DW_TAG_interface_type:
8060 case DW_TAG_enumeration_type:
8061 case DW_TAG_pointer_type:
8062 case DW_TAG_reference_type:
8063 case DW_TAG_rvalue_reference_type:
8064 case DW_TAG_string_type:
8065 case DW_TAG_structure_type:
8066 case DW_TAG_subroutine_type:
8067 case DW_TAG_union_type:
8068 case DW_TAG_ptr_to_member_type:
8069 case DW_TAG_set_type:
8070 case DW_TAG_subrange_type:
8071 case DW_TAG_base_type:
8072 case DW_TAG_const_type:
8073 case DW_TAG_file_type:
8074 case DW_TAG_packed_type:
8075 case DW_TAG_volatile_type:
8076 case DW_TAG_typedef:
8077 return true;
8078 default:
8079 return false;
8083 /* Returns true iff C is a compile-unit DIE. */
8085 static inline bool
8086 is_cu_die (dw_die_ref c)
8088 return c && (c->die_tag == DW_TAG_compile_unit
8089 || c->die_tag == DW_TAG_skeleton_unit);
8092 /* Returns true iff C is a unit DIE of some sort. */
8094 static inline bool
8095 is_unit_die (dw_die_ref c)
8097 return c && (c->die_tag == DW_TAG_compile_unit
8098 || c->die_tag == DW_TAG_partial_unit
8099 || c->die_tag == DW_TAG_type_unit
8100 || c->die_tag == DW_TAG_skeleton_unit);
8103 /* Returns true iff C is a namespace DIE. */
8105 static inline bool
8106 is_namespace_die (dw_die_ref c)
8108 return c && c->die_tag == DW_TAG_namespace;
8111 /* Return true if this DIE is a template parameter. */
8113 static inline bool
8114 is_template_parameter (dw_die_ref die)
8116 switch (die->die_tag)
8118 case DW_TAG_template_type_param:
8119 case DW_TAG_template_value_param:
8120 case DW_TAG_GNU_template_template_param:
8121 case DW_TAG_GNU_template_parameter_pack:
8122 return true;
8123 default:
8124 return false;
8128 /* Return true if this DIE represents a template instantiation. */
8130 static inline bool
8131 is_template_instantiation (dw_die_ref die)
8133 dw_die_ref c;
8135 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8136 return false;
8137 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8138 return false;
8141 static char *
8142 gen_internal_sym (const char *prefix)
8144 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8146 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8147 return xstrdup (buf);
8150 /* Return true if this DIE is a declaration. */
8152 static bool
8153 is_declaration_die (dw_die_ref die)
8155 dw_attr_node *a;
8156 unsigned ix;
8158 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8159 if (a->dw_attr == DW_AT_declaration)
8160 return true;
8162 return false;
8165 /* Return true if this DIE is nested inside a subprogram. */
8167 static bool
8168 is_nested_in_subprogram (dw_die_ref die)
8170 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8172 if (decl == NULL)
8173 decl = die;
8174 return local_scope_p (decl);
8177 /* Return true if this DIE contains a defining declaration of a
8178 subprogram. */
8180 static bool
8181 contains_subprogram_definition (dw_die_ref die)
8183 dw_die_ref c;
8185 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8186 return true;
8187 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8188 return false;
8191 /* Return true if this is a type DIE that should be moved to a
8192 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8193 unit type. */
8195 static bool
8196 should_move_die_to_comdat (dw_die_ref die)
8198 switch (die->die_tag)
8200 case DW_TAG_class_type:
8201 case DW_TAG_structure_type:
8202 case DW_TAG_enumeration_type:
8203 case DW_TAG_union_type:
8204 /* Don't move declarations, inlined instances, types nested in a
8205 subprogram, or types that contain subprogram definitions. */
8206 if (is_declaration_die (die)
8207 || get_AT (die, DW_AT_abstract_origin)
8208 || is_nested_in_subprogram (die)
8209 || contains_subprogram_definition (die))
8210 return false;
8211 return true;
8212 case DW_TAG_array_type:
8213 case DW_TAG_interface_type:
8214 case DW_TAG_pointer_type:
8215 case DW_TAG_reference_type:
8216 case DW_TAG_rvalue_reference_type:
8217 case DW_TAG_string_type:
8218 case DW_TAG_subroutine_type:
8219 case DW_TAG_ptr_to_member_type:
8220 case DW_TAG_set_type:
8221 case DW_TAG_subrange_type:
8222 case DW_TAG_base_type:
8223 case DW_TAG_const_type:
8224 case DW_TAG_file_type:
8225 case DW_TAG_packed_type:
8226 case DW_TAG_volatile_type:
8227 case DW_TAG_typedef:
8228 default:
8229 return false;
8233 /* Make a clone of DIE. */
8235 static dw_die_ref
8236 clone_die (dw_die_ref die)
8238 dw_die_ref clone = new_die_raw (die->die_tag);
8239 dw_attr_node *a;
8240 unsigned ix;
8242 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8243 add_dwarf_attr (clone, a);
8245 return clone;
8248 /* Make a clone of the tree rooted at DIE. */
8250 static dw_die_ref
8251 clone_tree (dw_die_ref die)
8253 dw_die_ref c;
8254 dw_die_ref clone = clone_die (die);
8256 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8258 return clone;
8261 /* Make a clone of DIE as a declaration. */
8263 static dw_die_ref
8264 clone_as_declaration (dw_die_ref die)
8266 dw_die_ref clone;
8267 dw_die_ref decl;
8268 dw_attr_node *a;
8269 unsigned ix;
8271 /* If the DIE is already a declaration, just clone it. */
8272 if (is_declaration_die (die))
8273 return clone_die (die);
8275 /* If the DIE is a specification, just clone its declaration DIE. */
8276 decl = get_AT_ref (die, DW_AT_specification);
8277 if (decl != NULL)
8279 clone = clone_die (decl);
8280 if (die->comdat_type_p)
8281 add_AT_die_ref (clone, DW_AT_signature, die);
8282 return clone;
8285 clone = new_die_raw (die->die_tag);
8287 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8289 /* We don't want to copy over all attributes.
8290 For example we don't want DW_AT_byte_size because otherwise we will no
8291 longer have a declaration and GDB will treat it as a definition. */
8293 switch (a->dw_attr)
8295 case DW_AT_abstract_origin:
8296 case DW_AT_artificial:
8297 case DW_AT_containing_type:
8298 case DW_AT_external:
8299 case DW_AT_name:
8300 case DW_AT_type:
8301 case DW_AT_virtuality:
8302 case DW_AT_linkage_name:
8303 case DW_AT_MIPS_linkage_name:
8304 add_dwarf_attr (clone, a);
8305 break;
8306 case DW_AT_byte_size:
8307 case DW_AT_alignment:
8308 default:
8309 break;
8313 if (die->comdat_type_p)
8314 add_AT_die_ref (clone, DW_AT_signature, die);
8316 add_AT_flag (clone, DW_AT_declaration, 1);
8317 return clone;
8321 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8323 struct decl_table_entry
8325 dw_die_ref orig;
8326 dw_die_ref copy;
8329 /* Helpers to manipulate hash table of copied declarations. */
8331 /* Hashtable helpers. */
8333 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8335 typedef die_struct *compare_type;
8336 static inline hashval_t hash (const decl_table_entry *);
8337 static inline bool equal (const decl_table_entry *, const die_struct *);
8340 inline hashval_t
8341 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8343 return htab_hash_pointer (entry->orig);
8346 inline bool
8347 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8348 const die_struct *entry2)
8350 return entry1->orig == entry2;
8353 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8355 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8356 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8357 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8358 to check if the ancestor has already been copied into UNIT. */
8360 static dw_die_ref
8361 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8362 decl_hash_type *decl_table)
8364 dw_die_ref parent = die->die_parent;
8365 dw_die_ref new_parent = unit;
8366 dw_die_ref copy;
8367 decl_table_entry **slot = NULL;
8368 struct decl_table_entry *entry = NULL;
8370 /* If DIE refers to a stub unfold that so we get the appropriate
8371 DIE registered as orig in decl_table. */
8372 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8373 die = c;
8375 if (decl_table)
8377 /* Check if the entry has already been copied to UNIT. */
8378 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8379 INSERT);
8380 if (*slot != HTAB_EMPTY_ENTRY)
8382 entry = *slot;
8383 return entry->copy;
8386 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8387 entry = XCNEW (struct decl_table_entry);
8388 entry->orig = die;
8389 entry->copy = NULL;
8390 *slot = entry;
8393 if (parent != NULL)
8395 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8396 if (spec != NULL)
8397 parent = spec;
8398 if (!is_unit_die (parent))
8399 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8402 copy = clone_as_declaration (die);
8403 add_child_die (new_parent, copy);
8405 if (decl_table)
8407 /* Record the pointer to the copy. */
8408 entry->copy = copy;
8411 return copy;
8413 /* Copy the declaration context to the new type unit DIE. This includes
8414 any surrounding namespace or type declarations. If the DIE has an
8415 AT_specification attribute, it also includes attributes and children
8416 attached to the specification, and returns a pointer to the original
8417 parent of the declaration DIE. Returns NULL otherwise. */
8419 static dw_die_ref
8420 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8422 dw_die_ref decl;
8423 dw_die_ref new_decl;
8424 dw_die_ref orig_parent = NULL;
8426 decl = get_AT_ref (die, DW_AT_specification);
8427 if (decl == NULL)
8428 decl = die;
8429 else
8431 unsigned ix;
8432 dw_die_ref c;
8433 dw_attr_node *a;
8435 /* The original DIE will be changed to a declaration, and must
8436 be moved to be a child of the original declaration DIE. */
8437 orig_parent = decl->die_parent;
8439 /* Copy the type node pointer from the new DIE to the original
8440 declaration DIE so we can forward references later. */
8441 decl->comdat_type_p = true;
8442 decl->die_id.die_type_node = die->die_id.die_type_node;
8444 remove_AT (die, DW_AT_specification);
8446 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8448 if (a->dw_attr != DW_AT_name
8449 && a->dw_attr != DW_AT_declaration
8450 && a->dw_attr != DW_AT_external)
8451 add_dwarf_attr (die, a);
8454 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8457 if (decl->die_parent != NULL
8458 && !is_unit_die (decl->die_parent))
8460 new_decl = copy_ancestor_tree (unit, decl, NULL);
8461 if (new_decl != NULL)
8463 remove_AT (new_decl, DW_AT_signature);
8464 add_AT_specification (die, new_decl);
8468 return orig_parent;
8471 /* Generate the skeleton ancestor tree for the given NODE, then clone
8472 the DIE and add the clone into the tree. */
8474 static void
8475 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8477 if (node->new_die != NULL)
8478 return;
8480 node->new_die = clone_as_declaration (node->old_die);
8482 if (node->parent != NULL)
8484 generate_skeleton_ancestor_tree (node->parent);
8485 add_child_die (node->parent->new_die, node->new_die);
8489 /* Generate a skeleton tree of DIEs containing any declarations that are
8490 found in the original tree. We traverse the tree looking for declaration
8491 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8493 static void
8494 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8496 skeleton_chain_node node;
8497 dw_die_ref c;
8498 dw_die_ref first;
8499 dw_die_ref prev = NULL;
8500 dw_die_ref next = NULL;
8502 node.parent = parent;
8504 first = c = parent->old_die->die_child;
8505 if (c)
8506 next = c->die_sib;
8507 if (c) do {
8508 if (prev == NULL || prev->die_sib == c)
8509 prev = c;
8510 c = next;
8511 next = (c == first ? NULL : c->die_sib);
8512 node.old_die = c;
8513 node.new_die = NULL;
8514 if (is_declaration_die (c))
8516 if (is_template_instantiation (c))
8518 /* Instantiated templates do not need to be cloned into the
8519 type unit. Just move the DIE and its children back to
8520 the skeleton tree (in the main CU). */
8521 remove_child_with_prev (c, prev);
8522 add_child_die (parent->new_die, c);
8523 c = prev;
8525 else if (c->comdat_type_p)
8527 /* This is the skeleton of earlier break_out_comdat_types
8528 type. Clone the existing DIE, but keep the children
8529 under the original (which is in the main CU). */
8530 dw_die_ref clone = clone_die (c);
8532 replace_child (c, clone, prev);
8533 generate_skeleton_ancestor_tree (parent);
8534 add_child_die (parent->new_die, c);
8535 c = clone;
8536 continue;
8538 else
8540 /* Clone the existing DIE, move the original to the skeleton
8541 tree (which is in the main CU), and put the clone, with
8542 all the original's children, where the original came from
8543 (which is about to be moved to the type unit). */
8544 dw_die_ref clone = clone_die (c);
8545 move_all_children (c, clone);
8547 /* If the original has a DW_AT_object_pointer attribute,
8548 it would now point to a child DIE just moved to the
8549 cloned tree, so we need to remove that attribute from
8550 the original. */
8551 remove_AT (c, DW_AT_object_pointer);
8553 replace_child (c, clone, prev);
8554 generate_skeleton_ancestor_tree (parent);
8555 add_child_die (parent->new_die, c);
8556 node.old_die = clone;
8557 node.new_die = c;
8558 c = clone;
8561 generate_skeleton_bottom_up (&node);
8562 } while (next != NULL);
8565 /* Wrapper function for generate_skeleton_bottom_up. */
8567 static dw_die_ref
8568 generate_skeleton (dw_die_ref die)
8570 skeleton_chain_node node;
8572 node.old_die = die;
8573 node.new_die = NULL;
8574 node.parent = NULL;
8576 /* If this type definition is nested inside another type,
8577 and is not an instantiation of a template, always leave
8578 at least a declaration in its place. */
8579 if (die->die_parent != NULL
8580 && is_type_die (die->die_parent)
8581 && !is_template_instantiation (die))
8582 node.new_die = clone_as_declaration (die);
8584 generate_skeleton_bottom_up (&node);
8585 return node.new_die;
8588 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8589 declaration. The original DIE is moved to a new compile unit so that
8590 existing references to it follow it to the new location. If any of the
8591 original DIE's descendants is a declaration, we need to replace the
8592 original DIE with a skeleton tree and move the declarations back into the
8593 skeleton tree. */
8595 static dw_die_ref
8596 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8597 dw_die_ref prev)
8599 dw_die_ref skeleton, orig_parent;
8601 /* Copy the declaration context to the type unit DIE. If the returned
8602 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8603 that DIE. */
8604 orig_parent = copy_declaration_context (unit, child);
8606 skeleton = generate_skeleton (child);
8607 if (skeleton == NULL)
8608 remove_child_with_prev (child, prev);
8609 else
8611 skeleton->comdat_type_p = true;
8612 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8614 /* If the original DIE was a specification, we need to put
8615 the skeleton under the parent DIE of the declaration.
8616 This leaves the original declaration in the tree, but
8617 it will be pruned later since there are no longer any
8618 references to it. */
8619 if (orig_parent != NULL)
8621 remove_child_with_prev (child, prev);
8622 add_child_die (orig_parent, skeleton);
8624 else
8625 replace_child (child, skeleton, prev);
8628 return skeleton;
8631 static void
8632 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8633 comdat_type_node *type_node,
8634 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8636 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8637 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8638 DWARF procedure references in the DW_AT_location attribute. */
8640 static dw_die_ref
8641 copy_dwarf_procedure (dw_die_ref die,
8642 comdat_type_node *type_node,
8643 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8645 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8647 /* DWARF procedures are not supposed to have children... */
8648 gcc_assert (die->die_child == NULL);
8650 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8651 gcc_assert (vec_safe_length (die->die_attr) == 1
8652 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8654 /* Do not copy more than once DWARF procedures. */
8655 bool existed;
8656 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8657 if (existed)
8658 return die_copy;
8660 die_copy = clone_die (die);
8661 add_child_die (type_node->root_die, die_copy);
8662 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8663 return die_copy;
8666 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8667 procedures in DIE's attributes. */
8669 static void
8670 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8671 comdat_type_node *type_node,
8672 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8674 dw_attr_node *a;
8675 unsigned i;
8677 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8679 dw_loc_descr_ref loc;
8681 if (a->dw_attr_val.val_class != dw_val_class_loc)
8682 continue;
8684 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8686 switch (loc->dw_loc_opc)
8688 case DW_OP_call2:
8689 case DW_OP_call4:
8690 case DW_OP_call_ref:
8691 gcc_assert (loc->dw_loc_oprnd1.val_class
8692 == dw_val_class_die_ref);
8693 loc->dw_loc_oprnd1.v.val_die_ref.die
8694 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8695 type_node,
8696 copied_dwarf_procs);
8698 default:
8699 break;
8705 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8706 rewrite references to point to the copies.
8708 References are looked for in DIE's attributes and recursively in all its
8709 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8710 mapping from old DWARF procedures to their copy. It is used not to copy
8711 twice the same DWARF procedure under TYPE_NODE. */
8713 static void
8714 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8715 comdat_type_node *type_node,
8716 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8718 dw_die_ref c;
8720 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8721 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8722 type_node,
8723 copied_dwarf_procs));
8726 /* Traverse the DIE and set up additional .debug_types or .debug_info
8727 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8728 section. */
8730 static void
8731 break_out_comdat_types (dw_die_ref die)
8733 dw_die_ref c;
8734 dw_die_ref first;
8735 dw_die_ref prev = NULL;
8736 dw_die_ref next = NULL;
8737 dw_die_ref unit = NULL;
8739 first = c = die->die_child;
8740 if (c)
8741 next = c->die_sib;
8742 if (c) do {
8743 if (prev == NULL || prev->die_sib == c)
8744 prev = c;
8745 c = next;
8746 next = (c == first ? NULL : c->die_sib);
8747 if (should_move_die_to_comdat (c))
8749 dw_die_ref replacement;
8750 comdat_type_node *type_node;
8752 /* Break out nested types into their own type units. */
8753 break_out_comdat_types (c);
8755 /* Create a new type unit DIE as the root for the new tree. */
8756 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8757 add_AT_unsigned (unit, DW_AT_language,
8758 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8760 /* Add the new unit's type DIE into the comdat type list. */
8761 type_node = ggc_cleared_alloc<comdat_type_node> ();
8762 type_node->root_die = unit;
8763 type_node->next = comdat_type_list;
8764 comdat_type_list = type_node;
8766 /* Generate the type signature. */
8767 generate_type_signature (c, type_node);
8769 /* Copy the declaration context, attributes, and children of the
8770 declaration into the new type unit DIE, then remove this DIE
8771 from the main CU (or replace it with a skeleton if necessary). */
8772 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8773 type_node->skeleton_die = replacement;
8775 /* Add the DIE to the new compunit. */
8776 add_child_die (unit, c);
8778 /* Types can reference DWARF procedures for type size or data location
8779 expressions. Calls in DWARF expressions cannot target procedures
8780 that are not in the same section. So we must copy DWARF procedures
8781 along with this type and then rewrite references to them. */
8782 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8783 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8785 if (replacement != NULL)
8786 c = replacement;
8788 else if (c->die_tag == DW_TAG_namespace
8789 || c->die_tag == DW_TAG_class_type
8790 || c->die_tag == DW_TAG_structure_type
8791 || c->die_tag == DW_TAG_union_type)
8793 /* Look for nested types that can be broken out. */
8794 break_out_comdat_types (c);
8796 } while (next != NULL);
8799 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8800 Enter all the cloned children into the hash table decl_table. */
8802 static dw_die_ref
8803 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8805 dw_die_ref c;
8806 dw_die_ref clone;
8807 struct decl_table_entry *entry;
8808 decl_table_entry **slot;
8810 if (die->die_tag == DW_TAG_subprogram)
8811 clone = clone_as_declaration (die);
8812 else
8813 clone = clone_die (die);
8815 slot = decl_table->find_slot_with_hash (die,
8816 htab_hash_pointer (die), INSERT);
8818 /* Assert that DIE isn't in the hash table yet. If it would be there
8819 before, the ancestors would be necessarily there as well, therefore
8820 clone_tree_partial wouldn't be called. */
8821 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8823 entry = XCNEW (struct decl_table_entry);
8824 entry->orig = die;
8825 entry->copy = clone;
8826 *slot = entry;
8828 if (die->die_tag != DW_TAG_subprogram)
8829 FOR_EACH_CHILD (die, c,
8830 add_child_die (clone, clone_tree_partial (c, decl_table)));
8832 return clone;
8835 /* Walk the DIE and its children, looking for references to incomplete
8836 or trivial types that are unmarked (i.e., that are not in the current
8837 type_unit). */
8839 static void
8840 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8842 dw_die_ref c;
8843 dw_attr_node *a;
8844 unsigned ix;
8846 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8848 if (AT_class (a) == dw_val_class_die_ref)
8850 dw_die_ref targ = AT_ref (a);
8851 decl_table_entry **slot;
8852 struct decl_table_entry *entry;
8854 if (targ->die_mark != 0 || targ->comdat_type_p)
8855 continue;
8857 slot = decl_table->find_slot_with_hash (targ,
8858 htab_hash_pointer (targ),
8859 INSERT);
8861 if (*slot != HTAB_EMPTY_ENTRY)
8863 /* TARG has already been copied, so we just need to
8864 modify the reference to point to the copy. */
8865 entry = *slot;
8866 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8868 else
8870 dw_die_ref parent = unit;
8871 dw_die_ref copy = clone_die (targ);
8873 /* Record in DECL_TABLE that TARG has been copied.
8874 Need to do this now, before the recursive call,
8875 because DECL_TABLE may be expanded and SLOT
8876 would no longer be a valid pointer. */
8877 entry = XCNEW (struct decl_table_entry);
8878 entry->orig = targ;
8879 entry->copy = copy;
8880 *slot = entry;
8882 /* If TARG is not a declaration DIE, we need to copy its
8883 children. */
8884 if (!is_declaration_die (targ))
8886 FOR_EACH_CHILD (
8887 targ, c,
8888 add_child_die (copy,
8889 clone_tree_partial (c, decl_table)));
8892 /* Make sure the cloned tree is marked as part of the
8893 type unit. */
8894 mark_dies (copy);
8896 /* If TARG has surrounding context, copy its ancestor tree
8897 into the new type unit. */
8898 if (targ->die_parent != NULL
8899 && !is_unit_die (targ->die_parent))
8900 parent = copy_ancestor_tree (unit, targ->die_parent,
8901 decl_table);
8903 add_child_die (parent, copy);
8904 a->dw_attr_val.v.val_die_ref.die = copy;
8906 /* Make sure the newly-copied DIE is walked. If it was
8907 installed in a previously-added context, it won't
8908 get visited otherwise. */
8909 if (parent != unit)
8911 /* Find the highest point of the newly-added tree,
8912 mark each node along the way, and walk from there. */
8913 parent->die_mark = 1;
8914 while (parent->die_parent
8915 && parent->die_parent->die_mark == 0)
8917 parent = parent->die_parent;
8918 parent->die_mark = 1;
8920 copy_decls_walk (unit, parent, decl_table);
8926 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8929 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8930 and record them in DECL_TABLE. */
8932 static void
8933 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8935 dw_die_ref c;
8937 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8939 dw_die_ref targ = AT_ref (a);
8940 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8941 decl_table_entry **slot
8942 = decl_table->find_slot_with_hash (targ,
8943 htab_hash_pointer (targ),
8944 INSERT);
8945 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8946 /* Record in DECL_TABLE that TARG has been already copied
8947 by remove_child_or_replace_with_skeleton. */
8948 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8949 entry->orig = targ;
8950 entry->copy = die;
8951 *slot = entry;
8953 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8956 /* Copy declarations for "unworthy" types into the new comdat section.
8957 Incomplete types, modified types, and certain other types aren't broken
8958 out into comdat sections of their own, so they don't have a signature,
8959 and we need to copy the declaration into the same section so that we
8960 don't have an external reference. */
8962 static void
8963 copy_decls_for_unworthy_types (dw_die_ref unit)
8965 mark_dies (unit);
8966 decl_hash_type decl_table (10);
8967 collect_skeleton_dies (unit, &decl_table);
8968 copy_decls_walk (unit, unit, &decl_table);
8969 unmark_dies (unit);
8972 /* Traverse the DIE and add a sibling attribute if it may have the
8973 effect of speeding up access to siblings. To save some space,
8974 avoid generating sibling attributes for DIE's without children. */
8976 static void
8977 add_sibling_attributes (dw_die_ref die)
8979 dw_die_ref c;
8981 if (! die->die_child)
8982 return;
8984 if (die->die_parent && die != die->die_parent->die_child)
8985 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8987 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8990 /* Output all location lists for the DIE and its children. */
8992 static void
8993 output_location_lists (dw_die_ref die)
8995 dw_die_ref c;
8996 dw_attr_node *a;
8997 unsigned ix;
8999 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9000 if (AT_class (a) == dw_val_class_loc_list)
9001 output_loc_list (AT_loc_list (a));
9003 FOR_EACH_CHILD (die, c, output_location_lists (c));
9006 /* During assign_location_list_indexes and output_loclists_offset the
9007 current index, after it the number of assigned indexes (i.e. how
9008 large the .debug_loclists* offset table should be). */
9009 static unsigned int loc_list_idx;
9011 /* Output all location list offsets for the DIE and its children. */
9013 static void
9014 output_loclists_offsets (dw_die_ref die)
9016 dw_die_ref c;
9017 dw_attr_node *a;
9018 unsigned ix;
9020 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9021 if (AT_class (a) == dw_val_class_loc_list)
9023 dw_loc_list_ref l = AT_loc_list (a);
9024 if (l->offset_emitted)
9025 continue;
9026 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9027 loc_section_label, NULL);
9028 gcc_assert (l->hash == loc_list_idx);
9029 loc_list_idx++;
9030 l->offset_emitted = true;
9033 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9036 /* Recursively set indexes of location lists. */
9038 static void
9039 assign_location_list_indexes (dw_die_ref die)
9041 dw_die_ref c;
9042 dw_attr_node *a;
9043 unsigned ix;
9045 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9046 if (AT_class (a) == dw_val_class_loc_list)
9048 dw_loc_list_ref list = AT_loc_list (a);
9049 if (!list->num_assigned)
9051 list->num_assigned = true;
9052 list->hash = loc_list_idx++;
9056 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9059 /* We want to limit the number of external references, because they are
9060 larger than local references: a relocation takes multiple words, and
9061 even a sig8 reference is always eight bytes, whereas a local reference
9062 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9063 So if we encounter multiple external references to the same type DIE, we
9064 make a local typedef stub for it and redirect all references there.
9066 This is the element of the hash table for keeping track of these
9067 references. */
9069 struct external_ref
9071 dw_die_ref type;
9072 dw_die_ref stub;
9073 unsigned n_refs;
9076 /* Hashtable helpers. */
9078 struct external_ref_hasher : free_ptr_hash <external_ref>
9080 static inline hashval_t hash (const external_ref *);
9081 static inline bool equal (const external_ref *, const external_ref *);
9084 inline hashval_t
9085 external_ref_hasher::hash (const external_ref *r)
9087 dw_die_ref die = r->type;
9088 hashval_t h = 0;
9090 /* We can't use the address of the DIE for hashing, because
9091 that will make the order of the stub DIEs non-deterministic. */
9092 if (! die->comdat_type_p)
9093 /* We have a symbol; use it to compute a hash. */
9094 h = htab_hash_string (die->die_id.die_symbol);
9095 else
9097 /* We have a type signature; use a subset of the bits as the hash.
9098 The 8-byte signature is at least as large as hashval_t. */
9099 comdat_type_node *type_node = die->die_id.die_type_node;
9100 memcpy (&h, type_node->signature, sizeof (h));
9102 return h;
9105 inline bool
9106 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9108 return r1->type == r2->type;
9111 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9113 /* Return a pointer to the external_ref for references to DIE. */
9115 static struct external_ref *
9116 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9118 struct external_ref ref, *ref_p;
9119 external_ref **slot;
9121 ref.type = die;
9122 slot = map->find_slot (&ref, INSERT);
9123 if (*slot != HTAB_EMPTY_ENTRY)
9124 return *slot;
9126 ref_p = XCNEW (struct external_ref);
9127 ref_p->type = die;
9128 *slot = ref_p;
9129 return ref_p;
9132 /* Subroutine of optimize_external_refs, below.
9134 If we see a type skeleton, record it as our stub. If we see external
9135 references, remember how many we've seen. */
9137 static void
9138 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9140 dw_die_ref c;
9141 dw_attr_node *a;
9142 unsigned ix;
9143 struct external_ref *ref_p;
9145 if (is_type_die (die)
9146 && (c = get_AT_ref (die, DW_AT_signature)))
9148 /* This is a local skeleton; use it for local references. */
9149 ref_p = lookup_external_ref (map, c);
9150 ref_p->stub = die;
9153 /* Scan the DIE references, and remember any that refer to DIEs from
9154 other CUs (i.e. those which are not marked). */
9155 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9156 if (AT_class (a) == dw_val_class_die_ref
9157 && (c = AT_ref (a))->die_mark == 0
9158 && is_type_die (c))
9160 ref_p = lookup_external_ref (map, c);
9161 ref_p->n_refs++;
9164 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9167 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9168 points to an external_ref, DATA is the CU we're processing. If we don't
9169 already have a local stub, and we have multiple refs, build a stub. */
9172 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9174 struct external_ref *ref_p = *slot;
9176 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9178 /* We have multiple references to this type, so build a small stub.
9179 Both of these forms are a bit dodgy from the perspective of the
9180 DWARF standard, since technically they should have names. */
9181 dw_die_ref cu = data;
9182 dw_die_ref type = ref_p->type;
9183 dw_die_ref stub = NULL;
9185 if (type->comdat_type_p)
9187 /* If we refer to this type via sig8, use AT_signature. */
9188 stub = new_die (type->die_tag, cu, NULL_TREE);
9189 add_AT_die_ref (stub, DW_AT_signature, type);
9191 else
9193 /* Otherwise, use a typedef with no name. */
9194 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9195 add_AT_die_ref (stub, DW_AT_type, type);
9198 stub->die_mark++;
9199 ref_p->stub = stub;
9201 return 1;
9204 /* DIE is a unit; look through all the DIE references to see if there are
9205 any external references to types, and if so, create local stubs for
9206 them which will be applied in build_abbrev_table. This is useful because
9207 references to local DIEs are smaller. */
9209 static external_ref_hash_type *
9210 optimize_external_refs (dw_die_ref die)
9212 external_ref_hash_type *map = new external_ref_hash_type (10);
9213 optimize_external_refs_1 (die, map);
9214 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9215 return map;
9218 /* The following 3 variables are temporaries that are computed only during the
9219 build_abbrev_table call and used and released during the following
9220 optimize_abbrev_table call. */
9222 /* First abbrev_id that can be optimized based on usage. */
9223 static unsigned int abbrev_opt_start;
9225 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9226 abbrev_id smaller than this, because they must be already sized
9227 during build_abbrev_table). */
9228 static unsigned int abbrev_opt_base_type_end;
9230 /* Vector of usage counts during build_abbrev_table. Indexed by
9231 abbrev_id - abbrev_opt_start. */
9232 static vec<unsigned int> abbrev_usage_count;
9234 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9235 static vec<dw_die_ref> sorted_abbrev_dies;
9237 /* The format of each DIE (and its attribute value pairs) is encoded in an
9238 abbreviation table. This routine builds the abbreviation table and assigns
9239 a unique abbreviation id for each abbreviation entry. The children of each
9240 die are visited recursively. */
9242 static void
9243 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9245 unsigned int abbrev_id = 0;
9246 dw_die_ref c;
9247 dw_attr_node *a;
9248 unsigned ix;
9249 dw_die_ref abbrev;
9251 /* Scan the DIE references, and replace any that refer to
9252 DIEs from other CUs (i.e. those which are not marked) with
9253 the local stubs we built in optimize_external_refs. */
9254 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9255 if (AT_class (a) == dw_val_class_die_ref
9256 && (c = AT_ref (a))->die_mark == 0)
9258 struct external_ref *ref_p;
9259 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9261 if (is_type_die (c)
9262 && (ref_p = lookup_external_ref (extern_map, c))
9263 && ref_p->stub && ref_p->stub != die)
9265 gcc_assert (a->dw_attr != DW_AT_signature);
9266 change_AT_die_ref (a, ref_p->stub);
9268 else
9269 /* We aren't changing this reference, so mark it external. */
9270 set_AT_ref_external (a, 1);
9273 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9275 dw_attr_node *die_a, *abbrev_a;
9276 unsigned ix;
9277 bool ok = true;
9279 if (abbrev_id == 0)
9280 continue;
9281 if (abbrev->die_tag != die->die_tag)
9282 continue;
9283 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9284 continue;
9286 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9287 continue;
9289 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9291 abbrev_a = &(*abbrev->die_attr)[ix];
9292 if ((abbrev_a->dw_attr != die_a->dw_attr)
9293 || (value_format (abbrev_a) != value_format (die_a)))
9295 ok = false;
9296 break;
9299 if (ok)
9300 break;
9303 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9305 vec_safe_push (abbrev_die_table, die);
9306 if (abbrev_opt_start)
9307 abbrev_usage_count.safe_push (0);
9309 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9311 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9312 sorted_abbrev_dies.safe_push (die);
9315 die->die_abbrev = abbrev_id;
9316 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9319 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9320 by die_abbrev's usage count, from the most commonly used
9321 abbreviation to the least. */
9323 static int
9324 die_abbrev_cmp (const void *p1, const void *p2)
9326 dw_die_ref die1 = *(const dw_die_ref *) p1;
9327 dw_die_ref die2 = *(const dw_die_ref *) p2;
9329 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9330 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9332 if (die1->die_abbrev >= abbrev_opt_base_type_end
9333 && die2->die_abbrev >= abbrev_opt_base_type_end)
9335 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9336 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9337 return -1;
9338 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9339 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9340 return 1;
9343 /* Stabilize the sort. */
9344 if (die1->die_abbrev < die2->die_abbrev)
9345 return -1;
9346 if (die1->die_abbrev > die2->die_abbrev)
9347 return 1;
9349 return 0;
9352 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9353 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9354 into dw_val_class_const_implicit or
9355 dw_val_class_unsigned_const_implicit. */
9357 static void
9358 optimize_implicit_const (unsigned int first_id, unsigned int end,
9359 vec<bool> &implicit_consts)
9361 /* It never makes sense if there is just one DIE using the abbreviation. */
9362 if (end < first_id + 2)
9363 return;
9365 dw_attr_node *a;
9366 unsigned ix, i;
9367 dw_die_ref die = sorted_abbrev_dies[first_id];
9368 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9369 if (implicit_consts[ix])
9371 enum dw_val_class new_class = dw_val_class_none;
9372 switch (AT_class (a))
9374 case dw_val_class_unsigned_const:
9375 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9376 continue;
9378 /* The .debug_abbrev section will grow by
9379 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9380 in all the DIEs using that abbreviation. */
9381 if (constant_size (AT_unsigned (a)) * (end - first_id)
9382 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9383 continue;
9385 new_class = dw_val_class_unsigned_const_implicit;
9386 break;
9388 case dw_val_class_const:
9389 new_class = dw_val_class_const_implicit;
9390 break;
9392 case dw_val_class_file:
9393 new_class = dw_val_class_file_implicit;
9394 break;
9396 default:
9397 continue;
9399 for (i = first_id; i < end; i++)
9400 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9401 = new_class;
9405 /* Attempt to optimize abbreviation table from abbrev_opt_start
9406 abbreviation above. */
9408 static void
9409 optimize_abbrev_table (void)
9411 if (abbrev_opt_start
9412 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9413 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9415 auto_vec<bool, 32> implicit_consts;
9416 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9418 unsigned int abbrev_id = abbrev_opt_start - 1;
9419 unsigned int first_id = ~0U;
9420 unsigned int last_abbrev_id = 0;
9421 unsigned int i;
9422 dw_die_ref die;
9423 if (abbrev_opt_base_type_end > abbrev_opt_start)
9424 abbrev_id = abbrev_opt_base_type_end - 1;
9425 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9426 most commonly used abbreviations come first. */
9427 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9429 dw_attr_node *a;
9430 unsigned ix;
9432 /* If calc_base_type_die_sizes has been called, the CU and
9433 base types after it can't be optimized, because we've already
9434 calculated their DIE offsets. We've sorted them first. */
9435 if (die->die_abbrev < abbrev_opt_base_type_end)
9436 continue;
9437 if (die->die_abbrev != last_abbrev_id)
9439 last_abbrev_id = die->die_abbrev;
9440 if (dwarf_version >= 5 && first_id != ~0U)
9441 optimize_implicit_const (first_id, i, implicit_consts);
9442 abbrev_id++;
9443 (*abbrev_die_table)[abbrev_id] = die;
9444 if (dwarf_version >= 5)
9446 first_id = i;
9447 implicit_consts.truncate (0);
9449 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9450 switch (AT_class (a))
9452 case dw_val_class_const:
9453 case dw_val_class_unsigned_const:
9454 case dw_val_class_file:
9455 implicit_consts.safe_push (true);
9456 break;
9457 default:
9458 implicit_consts.safe_push (false);
9459 break;
9463 else if (dwarf_version >= 5)
9465 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9466 if (!implicit_consts[ix])
9467 continue;
9468 else
9470 dw_attr_node *other_a
9471 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9472 if (!dw_val_equal_p (&a->dw_attr_val,
9473 &other_a->dw_attr_val))
9474 implicit_consts[ix] = false;
9477 die->die_abbrev = abbrev_id;
9479 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9480 if (dwarf_version >= 5 && first_id != ~0U)
9481 optimize_implicit_const (first_id, i, implicit_consts);
9484 abbrev_opt_start = 0;
9485 abbrev_opt_base_type_end = 0;
9486 abbrev_usage_count.release ();
9487 sorted_abbrev_dies.release ();
9490 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9492 static int
9493 constant_size (unsigned HOST_WIDE_INT value)
9495 int log;
9497 if (value == 0)
9498 log = 0;
9499 else
9500 log = floor_log2 (value);
9502 log = log / 8;
9503 log = 1 << (floor_log2 (log) + 1);
9505 return log;
9508 /* Return the size of a DIE as it is represented in the
9509 .debug_info section. */
9511 static unsigned long
9512 size_of_die (dw_die_ref die)
9514 unsigned long size = 0;
9515 dw_attr_node *a;
9516 unsigned ix;
9517 enum dwarf_form form;
9519 size += size_of_uleb128 (die->die_abbrev);
9520 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9522 switch (AT_class (a))
9524 case dw_val_class_addr:
9525 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9527 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9528 size += size_of_uleb128 (AT_index (a));
9530 else
9531 size += DWARF2_ADDR_SIZE;
9532 break;
9533 case dw_val_class_offset:
9534 size += dwarf_offset_size;
9535 break;
9536 case dw_val_class_loc:
9538 unsigned long lsize = size_of_locs (AT_loc (a));
9540 /* Block length. */
9541 if (dwarf_version >= 4)
9542 size += size_of_uleb128 (lsize);
9543 else
9544 size += constant_size (lsize);
9545 size += lsize;
9547 break;
9548 case dw_val_class_loc_list:
9549 if (dwarf_split_debug_info && dwarf_version >= 5)
9551 gcc_assert (AT_loc_list (a)->num_assigned);
9552 size += size_of_uleb128 (AT_loc_list (a)->hash);
9554 else
9555 size += dwarf_offset_size;
9556 break;
9557 case dw_val_class_view_list:
9558 size += dwarf_offset_size;
9559 break;
9560 case dw_val_class_range_list:
9561 if (value_format (a) == DW_FORM_rnglistx)
9563 gcc_assert (rnglist_idx);
9564 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9565 size += size_of_uleb128 (r->idx);
9567 else
9568 size += dwarf_offset_size;
9569 break;
9570 case dw_val_class_const:
9571 size += size_of_sleb128 (AT_int (a));
9572 break;
9573 case dw_val_class_unsigned_const:
9575 int csize = constant_size (AT_unsigned (a));
9576 if (dwarf_version == 3
9577 && a->dw_attr == DW_AT_data_member_location
9578 && csize >= 4)
9579 size += size_of_uleb128 (AT_unsigned (a));
9580 else
9581 size += csize;
9583 break;
9584 case dw_val_class_symview:
9585 if (symview_upper_bound <= 0xff)
9586 size += 1;
9587 else if (symview_upper_bound <= 0xffff)
9588 size += 2;
9589 else if (symview_upper_bound <= 0xffffffff)
9590 size += 4;
9591 else
9592 size += 8;
9593 break;
9594 case dw_val_class_const_implicit:
9595 case dw_val_class_unsigned_const_implicit:
9596 case dw_val_class_file_implicit:
9597 /* These occupy no size in the DIE, just an extra sleb128 in
9598 .debug_abbrev. */
9599 break;
9600 case dw_val_class_const_double:
9601 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9602 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9603 size++; /* block */
9604 break;
9605 case dw_val_class_wide_int:
9606 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9607 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9608 if (get_full_len (*a->dw_attr_val.v.val_wide)
9609 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9610 size++; /* block */
9611 break;
9612 case dw_val_class_vec:
9613 size += constant_size (a->dw_attr_val.v.val_vec.length
9614 * a->dw_attr_val.v.val_vec.elt_size)
9615 + a->dw_attr_val.v.val_vec.length
9616 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9617 break;
9618 case dw_val_class_flag:
9619 if (dwarf_version >= 4)
9620 /* Currently all add_AT_flag calls pass in 1 as last argument,
9621 so DW_FORM_flag_present can be used. If that ever changes,
9622 we'll need to use DW_FORM_flag and have some optimization
9623 in build_abbrev_table that will change those to
9624 DW_FORM_flag_present if it is set to 1 in all DIEs using
9625 the same abbrev entry. */
9626 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9627 else
9628 size += 1;
9629 break;
9630 case dw_val_class_die_ref:
9631 if (AT_ref_external (a))
9633 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9634 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9635 is sized by target address length, whereas in DWARF3
9636 it's always sized as an offset. */
9637 if (AT_ref (a)->comdat_type_p)
9638 size += DWARF_TYPE_SIGNATURE_SIZE;
9639 else if (dwarf_version == 2)
9640 size += DWARF2_ADDR_SIZE;
9641 else
9642 size += dwarf_offset_size;
9644 else
9645 size += dwarf_offset_size;
9646 break;
9647 case dw_val_class_fde_ref:
9648 size += dwarf_offset_size;
9649 break;
9650 case dw_val_class_lbl_id:
9651 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9653 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9654 size += size_of_uleb128 (AT_index (a));
9656 else
9657 size += DWARF2_ADDR_SIZE;
9658 break;
9659 case dw_val_class_lineptr:
9660 case dw_val_class_macptr:
9661 case dw_val_class_loclistsptr:
9662 size += dwarf_offset_size;
9663 break;
9664 case dw_val_class_str:
9665 form = AT_string_form (a);
9666 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9667 size += dwarf_offset_size;
9668 else if (form == dwarf_FORM (DW_FORM_strx))
9669 size += size_of_uleb128 (AT_index (a));
9670 else
9671 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9672 break;
9673 case dw_val_class_file:
9674 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9675 break;
9676 case dw_val_class_data8:
9677 size += 8;
9678 break;
9679 case dw_val_class_vms_delta:
9680 size += dwarf_offset_size;
9681 break;
9682 case dw_val_class_high_pc:
9683 size += DWARF2_ADDR_SIZE;
9684 break;
9685 case dw_val_class_discr_value:
9686 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9687 break;
9688 case dw_val_class_discr_list:
9690 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9692 /* This is a block, so we have the block length and then its
9693 data. */
9694 size += constant_size (block_size) + block_size;
9696 break;
9697 default:
9698 gcc_unreachable ();
9702 return size;
9705 /* Size the debugging information associated with a given DIE. Visits the
9706 DIE's children recursively. Updates the global variable next_die_offset, on
9707 each time through. Uses the current value of next_die_offset to update the
9708 die_offset field in each DIE. */
9710 static void
9711 calc_die_sizes (dw_die_ref die)
9713 dw_die_ref c;
9715 gcc_assert (die->die_offset == 0
9716 || (unsigned long int) die->die_offset == next_die_offset);
9717 die->die_offset = next_die_offset;
9718 next_die_offset += size_of_die (die);
9720 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9722 if (die->die_child != NULL)
9723 /* Count the null byte used to terminate sibling lists. */
9724 next_die_offset += 1;
9727 /* Size just the base type children at the start of the CU.
9728 This is needed because build_abbrev needs to size locs
9729 and sizing of type based stack ops needs to know die_offset
9730 values for the base types. */
9732 static void
9733 calc_base_type_die_sizes (void)
9735 unsigned long die_offset = (dwarf_split_debug_info
9736 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9737 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9738 unsigned int i;
9739 dw_die_ref base_type;
9740 #if ENABLE_ASSERT_CHECKING
9741 dw_die_ref prev = comp_unit_die ()->die_child;
9742 #endif
9744 die_offset += size_of_die (comp_unit_die ());
9745 for (i = 0; base_types.iterate (i, &base_type); i++)
9747 #if ENABLE_ASSERT_CHECKING
9748 gcc_assert (base_type->die_offset == 0
9749 && prev->die_sib == base_type
9750 && base_type->die_child == NULL
9751 && base_type->die_abbrev);
9752 prev = base_type;
9753 #endif
9754 if (abbrev_opt_start
9755 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9756 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9757 base_type->die_offset = die_offset;
9758 die_offset += size_of_die (base_type);
9762 /* Set the marks for a die and its children. We do this so
9763 that we know whether or not a reference needs to use FORM_ref_addr; only
9764 DIEs in the same CU will be marked. We used to clear out the offset
9765 and use that as the flag, but ran into ordering problems. */
9767 static void
9768 mark_dies (dw_die_ref die)
9770 dw_die_ref c;
9772 gcc_assert (!die->die_mark);
9774 die->die_mark = 1;
9775 FOR_EACH_CHILD (die, c, mark_dies (c));
9778 /* Clear the marks for a die and its children. */
9780 static void
9781 unmark_dies (dw_die_ref die)
9783 dw_die_ref c;
9785 if (! use_debug_types)
9786 gcc_assert (die->die_mark);
9788 die->die_mark = 0;
9789 FOR_EACH_CHILD (die, c, unmark_dies (c));
9792 /* Clear the marks for a die, its children and referred dies. */
9794 static void
9795 unmark_all_dies (dw_die_ref die)
9797 dw_die_ref c;
9798 dw_attr_node *a;
9799 unsigned ix;
9801 if (!die->die_mark)
9802 return;
9803 die->die_mark = 0;
9805 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9807 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9808 if (AT_class (a) == dw_val_class_die_ref)
9809 unmark_all_dies (AT_ref (a));
9812 /* Calculate if the entry should appear in the final output file. It may be
9813 from a pruned a type. */
9815 static bool
9816 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9818 /* By limiting gnu pubnames to definitions only, gold can generate a
9819 gdb index without entries for declarations, which don't include
9820 enough information to be useful. */
9821 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9822 return false;
9824 if (table == pubname_table)
9826 /* Enumerator names are part of the pubname table, but the
9827 parent DW_TAG_enumeration_type die may have been pruned.
9828 Don't output them if that is the case. */
9829 if (p->die->die_tag == DW_TAG_enumerator &&
9830 (p->die->die_parent == NULL
9831 || !p->die->die_parent->die_perennial_p))
9832 return false;
9834 /* Everything else in the pubname table is included. */
9835 return true;
9838 /* The pubtypes table shouldn't include types that have been
9839 pruned. */
9840 return (p->die->die_offset != 0
9841 || !flag_eliminate_unused_debug_types);
9844 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9845 generated for the compilation unit. */
9847 static unsigned long
9848 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9850 unsigned long size;
9851 unsigned i;
9852 pubname_entry *p;
9853 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9855 size = DWARF_PUBNAMES_HEADER_SIZE;
9856 FOR_EACH_VEC_ELT (*names, i, p)
9857 if (include_pubname_in_output (names, p))
9858 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9860 size += dwarf_offset_size;
9861 return size;
9864 /* Return the size of the information in the .debug_aranges section. */
9866 static unsigned long
9867 size_of_aranges (void)
9869 unsigned long size;
9871 size = DWARF_ARANGES_HEADER_SIZE;
9873 /* Count the address/length pair for this compilation unit. */
9874 if (switch_text_ranges)
9875 size += 2 * DWARF2_ADDR_SIZE
9876 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9877 if (switch_cold_ranges)
9878 size += 2 * DWARF2_ADDR_SIZE
9879 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9880 if (have_multiple_function_sections)
9882 unsigned fde_idx;
9883 dw_fde_ref fde;
9885 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9887 if (fde->ignored_debug)
9888 continue;
9889 if (!fde->in_std_section)
9890 size += 2 * DWARF2_ADDR_SIZE;
9891 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9892 size += 2 * DWARF2_ADDR_SIZE;
9896 /* Count the two zero words used to terminated the address range table. */
9897 size += 2 * DWARF2_ADDR_SIZE;
9898 return size;
9901 /* Select the encoding of an attribute value. */
9903 static enum dwarf_form
9904 value_format (dw_attr_node *a)
9906 switch (AT_class (a))
9908 case dw_val_class_addr:
9909 /* Only very few attributes allow DW_FORM_addr. */
9910 switch (a->dw_attr)
9912 case DW_AT_low_pc:
9913 case DW_AT_high_pc:
9914 case DW_AT_entry_pc:
9915 case DW_AT_trampoline:
9916 return (AT_index (a) == NOT_INDEXED
9917 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9918 default:
9919 break;
9921 switch (DWARF2_ADDR_SIZE)
9923 case 1:
9924 return DW_FORM_data1;
9925 case 2:
9926 return DW_FORM_data2;
9927 case 4:
9928 return DW_FORM_data4;
9929 case 8:
9930 return DW_FORM_data8;
9931 default:
9932 gcc_unreachable ();
9934 case dw_val_class_loc_list:
9935 if (dwarf_split_debug_info
9936 && dwarf_version >= 5
9937 && AT_loc_list (a)->num_assigned)
9938 return DW_FORM_loclistx;
9939 /* FALLTHRU */
9940 case dw_val_class_view_list:
9941 case dw_val_class_range_list:
9942 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9943 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9944 care about sizes of .debug* sections in shared libraries and
9945 executables and don't take into account relocations that affect just
9946 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9947 table in the .debug_rnglists section. */
9948 if (dwarf_split_debug_info
9949 && dwarf_version >= 5
9950 && AT_class (a) == dw_val_class_range_list
9951 && rnglist_idx
9952 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9953 return DW_FORM_rnglistx;
9954 if (dwarf_version >= 4)
9955 return DW_FORM_sec_offset;
9956 /* FALLTHRU */
9957 case dw_val_class_vms_delta:
9958 case dw_val_class_offset:
9959 switch (dwarf_offset_size)
9961 case 4:
9962 return DW_FORM_data4;
9963 case 8:
9964 return DW_FORM_data8;
9965 default:
9966 gcc_unreachable ();
9968 case dw_val_class_loc:
9969 if (dwarf_version >= 4)
9970 return DW_FORM_exprloc;
9971 switch (constant_size (size_of_locs (AT_loc (a))))
9973 case 1:
9974 return DW_FORM_block1;
9975 case 2:
9976 return DW_FORM_block2;
9977 case 4:
9978 return DW_FORM_block4;
9979 default:
9980 gcc_unreachable ();
9982 case dw_val_class_const:
9983 return DW_FORM_sdata;
9984 case dw_val_class_unsigned_const:
9985 switch (constant_size (AT_unsigned (a)))
9987 case 1:
9988 return DW_FORM_data1;
9989 case 2:
9990 return DW_FORM_data2;
9991 case 4:
9992 /* In DWARF3 DW_AT_data_member_location with
9993 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9994 constant, so we need to use DW_FORM_udata if we need
9995 a large constant. */
9996 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9997 return DW_FORM_udata;
9998 return DW_FORM_data4;
9999 case 8:
10000 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
10001 return DW_FORM_udata;
10002 return DW_FORM_data8;
10003 default:
10004 gcc_unreachable ();
10006 case dw_val_class_const_implicit:
10007 case dw_val_class_unsigned_const_implicit:
10008 case dw_val_class_file_implicit:
10009 return DW_FORM_implicit_const;
10010 case dw_val_class_const_double:
10011 switch (HOST_BITS_PER_WIDE_INT)
10013 case 8:
10014 return DW_FORM_data2;
10015 case 16:
10016 return DW_FORM_data4;
10017 case 32:
10018 return DW_FORM_data8;
10019 case 64:
10020 if (dwarf_version >= 5)
10021 return DW_FORM_data16;
10022 /* FALLTHRU */
10023 default:
10024 return DW_FORM_block1;
10026 case dw_val_class_wide_int:
10027 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10029 case 8:
10030 return DW_FORM_data1;
10031 case 16:
10032 return DW_FORM_data2;
10033 case 32:
10034 return DW_FORM_data4;
10035 case 64:
10036 return DW_FORM_data8;
10037 case 128:
10038 if (dwarf_version >= 5)
10039 return DW_FORM_data16;
10040 /* FALLTHRU */
10041 default:
10042 return DW_FORM_block1;
10044 case dw_val_class_symview:
10045 /* ??? We might use uleb128, but then we'd have to compute
10046 .debug_info offsets in the assembler. */
10047 if (symview_upper_bound <= 0xff)
10048 return DW_FORM_data1;
10049 else if (symview_upper_bound <= 0xffff)
10050 return DW_FORM_data2;
10051 else if (symview_upper_bound <= 0xffffffff)
10052 return DW_FORM_data4;
10053 else
10054 return DW_FORM_data8;
10055 case dw_val_class_vec:
10056 switch (constant_size (a->dw_attr_val.v.val_vec.length
10057 * a->dw_attr_val.v.val_vec.elt_size))
10059 case 1:
10060 return DW_FORM_block1;
10061 case 2:
10062 return DW_FORM_block2;
10063 case 4:
10064 return DW_FORM_block4;
10065 default:
10066 gcc_unreachable ();
10068 case dw_val_class_flag:
10069 if (dwarf_version >= 4)
10071 /* Currently all add_AT_flag calls pass in 1 as last argument,
10072 so DW_FORM_flag_present can be used. If that ever changes,
10073 we'll need to use DW_FORM_flag and have some optimization
10074 in build_abbrev_table that will change those to
10075 DW_FORM_flag_present if it is set to 1 in all DIEs using
10076 the same abbrev entry. */
10077 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10078 return DW_FORM_flag_present;
10080 return DW_FORM_flag;
10081 case dw_val_class_die_ref:
10082 if (AT_ref_external (a))
10084 if (AT_ref (a)->comdat_type_p)
10085 return DW_FORM_ref_sig8;
10086 else
10087 return DW_FORM_ref_addr;
10089 else
10090 return DW_FORM_ref;
10091 case dw_val_class_fde_ref:
10092 return DW_FORM_data;
10093 case dw_val_class_lbl_id:
10094 return (AT_index (a) == NOT_INDEXED
10095 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10096 case dw_val_class_lineptr:
10097 case dw_val_class_macptr:
10098 case dw_val_class_loclistsptr:
10099 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10100 case dw_val_class_str:
10101 return AT_string_form (a);
10102 case dw_val_class_file:
10103 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10105 case 1:
10106 return DW_FORM_data1;
10107 case 2:
10108 return DW_FORM_data2;
10109 case 4:
10110 return DW_FORM_data4;
10111 default:
10112 gcc_unreachable ();
10115 case dw_val_class_data8:
10116 return DW_FORM_data8;
10118 case dw_val_class_high_pc:
10119 switch (DWARF2_ADDR_SIZE)
10121 case 1:
10122 return DW_FORM_data1;
10123 case 2:
10124 return DW_FORM_data2;
10125 case 4:
10126 return DW_FORM_data4;
10127 case 8:
10128 return DW_FORM_data8;
10129 default:
10130 gcc_unreachable ();
10133 case dw_val_class_discr_value:
10134 return (a->dw_attr_val.v.val_discr_value.pos
10135 ? DW_FORM_udata
10136 : DW_FORM_sdata);
10137 case dw_val_class_discr_list:
10138 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10140 case 1:
10141 return DW_FORM_block1;
10142 case 2:
10143 return DW_FORM_block2;
10144 case 4:
10145 return DW_FORM_block4;
10146 default:
10147 gcc_unreachable ();
10150 default:
10151 gcc_unreachable ();
10155 /* Output the encoding of an attribute value. */
10157 static void
10158 output_value_format (dw_attr_node *a)
10160 enum dwarf_form form = value_format (a);
10162 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10165 /* Given a die and id, produce the appropriate abbreviations. */
10167 static void
10168 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10170 unsigned ix;
10171 dw_attr_node *a_attr;
10173 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10174 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10175 dwarf_tag_name (abbrev->die_tag));
10177 if (abbrev->die_child != NULL)
10178 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10179 else
10180 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10182 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10184 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10185 dwarf_attr_name (a_attr->dw_attr));
10186 output_value_format (a_attr);
10187 if (value_format (a_attr) == DW_FORM_implicit_const)
10189 if (AT_class (a_attr) == dw_val_class_file_implicit)
10191 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10192 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10193 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10195 else
10196 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10200 dw2_asm_output_data (1, 0, NULL);
10201 dw2_asm_output_data (1, 0, NULL);
10205 /* Output the .debug_abbrev section which defines the DIE abbreviation
10206 table. */
10208 static void
10209 output_abbrev_section (void)
10211 unsigned int abbrev_id;
10212 dw_die_ref abbrev;
10214 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10215 if (abbrev_id != 0)
10216 output_die_abbrevs (abbrev_id, abbrev);
10218 /* Terminate the table. */
10219 dw2_asm_output_data (1, 0, NULL);
10222 /* Return a new location list, given the begin and end range, and the
10223 expression. */
10225 static inline dw_loc_list_ref
10226 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10227 const char *end, var_loc_view vend,
10228 const char *section)
10230 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10232 retlist->begin = begin;
10233 retlist->begin_entry = NULL;
10234 retlist->end = end;
10235 retlist->end_entry = NULL;
10236 retlist->expr = expr;
10237 retlist->section = section;
10238 retlist->vbegin = vbegin;
10239 retlist->vend = vend;
10241 return retlist;
10244 /* Return true iff there's any nonzero view number in the loc list.
10246 ??? When views are not enabled, we'll often extend a single range
10247 to the entire function, so that we emit a single location
10248 expression rather than a location list. With views, even with a
10249 single range, we'll output a list if start or end have a nonzero
10250 view. If we change this, we may want to stop splitting a single
10251 range in dw_loc_list just because of a nonzero view, even if it
10252 straddles across hot/cold partitions. */
10254 static bool
10255 loc_list_has_views (dw_loc_list_ref list)
10257 if (!debug_variable_location_views)
10258 return false;
10260 for (dw_loc_list_ref loc = list;
10261 loc != NULL; loc = loc->dw_loc_next)
10262 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10263 return true;
10265 return false;
10268 /* Generate a new internal symbol for this location list node, if it
10269 hasn't got one yet. */
10271 static inline void
10272 gen_llsym (dw_loc_list_ref list)
10274 gcc_assert (!list->ll_symbol);
10275 list->ll_symbol = gen_internal_sym ("LLST");
10277 if (!loc_list_has_views (list))
10278 return;
10280 if (dwarf2out_locviews_in_attribute ())
10282 /* Use the same label_num for the view list. */
10283 label_num--;
10284 list->vl_symbol = gen_internal_sym ("LVUS");
10286 else
10287 list->vl_symbol = list->ll_symbol;
10290 /* Generate a symbol for the list, but only if we really want to emit
10291 it as a list. */
10293 static inline void
10294 maybe_gen_llsym (dw_loc_list_ref list)
10296 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10297 return;
10299 gen_llsym (list);
10302 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10303 NULL, don't consider size of the location expression. If we're not
10304 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10305 representation in *SIZEP. */
10307 static bool
10308 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10310 /* Don't output an entry that starts and ends at the same address. */
10311 if (strcmp (curr->begin, curr->end) == 0
10312 && curr->vbegin == curr->vend && !curr->force)
10313 return true;
10315 if (!sizep)
10316 return false;
10318 unsigned long size = size_of_locs (curr->expr);
10320 /* If the expression is too large, drop it on the floor. We could
10321 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10322 in the expression, but >= 64KB expressions for a single value
10323 in a single range are unlikely very useful. */
10324 if (dwarf_version < 5 && size > 0xffff)
10325 return true;
10327 *sizep = size;
10329 return false;
10332 /* Output a view pair loclist entry for CURR, if it requires one. */
10334 static void
10335 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10337 if (!dwarf2out_locviews_in_loclist ())
10338 return;
10340 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10341 return;
10343 #ifdef DW_LLE_view_pair
10344 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10346 if (dwarf2out_as_locview_support)
10348 if (ZERO_VIEW_P (curr->vbegin))
10349 dw2_asm_output_data_uleb128 (0, "Location view begin");
10350 else
10352 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10353 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10354 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10357 if (ZERO_VIEW_P (curr->vend))
10358 dw2_asm_output_data_uleb128 (0, "Location view end");
10359 else
10361 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10362 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10363 dw2_asm_output_symname_uleb128 (label, "Location view end");
10366 else
10368 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10369 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10371 #endif /* DW_LLE_view_pair */
10373 return;
10376 /* Output the location list given to us. */
10378 static void
10379 output_loc_list (dw_loc_list_ref list_head)
10381 int vcount = 0, lcount = 0;
10383 if (list_head->emitted)
10384 return;
10385 list_head->emitted = true;
10387 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10389 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10391 for (dw_loc_list_ref curr = list_head; curr != NULL;
10392 curr = curr->dw_loc_next)
10394 unsigned long size;
10396 if (skip_loc_list_entry (curr, &size))
10397 continue;
10399 vcount++;
10401 /* ?? dwarf_split_debug_info? */
10402 if (dwarf2out_as_locview_support)
10404 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10406 if (!ZERO_VIEW_P (curr->vbegin))
10408 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10409 dw2_asm_output_symname_uleb128 (label,
10410 "View list begin (%s)",
10411 list_head->vl_symbol);
10413 else
10414 dw2_asm_output_data_uleb128 (0,
10415 "View list begin (%s)",
10416 list_head->vl_symbol);
10418 if (!ZERO_VIEW_P (curr->vend))
10420 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10421 dw2_asm_output_symname_uleb128 (label,
10422 "View list end (%s)",
10423 list_head->vl_symbol);
10425 else
10426 dw2_asm_output_data_uleb128 (0,
10427 "View list end (%s)",
10428 list_head->vl_symbol);
10430 else
10432 dw2_asm_output_data_uleb128 (curr->vbegin,
10433 "View list begin (%s)",
10434 list_head->vl_symbol);
10435 dw2_asm_output_data_uleb128 (curr->vend,
10436 "View list end (%s)",
10437 list_head->vl_symbol);
10442 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10444 const char *last_section = NULL;
10445 const char *base_label = NULL;
10447 /* Walk the location list, and output each range + expression. */
10448 for (dw_loc_list_ref curr = list_head; curr != NULL;
10449 curr = curr->dw_loc_next)
10451 unsigned long size;
10453 /* Skip this entry? If we skip it here, we must skip it in the
10454 view list above as well. */
10455 if (skip_loc_list_entry (curr, &size))
10456 continue;
10458 lcount++;
10460 if (dwarf_version >= 5)
10462 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10464 dwarf2out_maybe_output_loclist_view_pair (curr);
10465 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10466 uleb128 index into .debug_addr and uleb128 length. */
10467 dw2_asm_output_data (1, DW_LLE_startx_length,
10468 "DW_LLE_startx_length (%s)",
10469 list_head->ll_symbol);
10470 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10471 "Location list range start index "
10472 "(%s)", curr->begin);
10473 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10474 "Location list length (%s)",
10475 list_head->ll_symbol);
10477 else if (dwarf_split_debug_info)
10479 dwarf2out_maybe_output_loclist_view_pair (curr);
10480 /* For -gsplit-dwarf without usable .uleb128 support, emit
10481 DW_LLE_startx_endx, which has two uleb128 indexes into
10482 .debug_addr. */
10483 dw2_asm_output_data (1, DW_LLE_startx_endx,
10484 "DW_LLE_startx_endx (%s)",
10485 list_head->ll_symbol);
10486 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10487 "Location list range start index "
10488 "(%s)", curr->begin);
10489 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10490 "Location list range end index "
10491 "(%s)", curr->end);
10493 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10495 dwarf2out_maybe_output_loclist_view_pair (curr);
10496 /* If all code is in .text section, the base address is
10497 already provided by the CU attributes. Use
10498 DW_LLE_offset_pair where both addresses are uleb128 encoded
10499 offsets against that base. */
10500 dw2_asm_output_data (1, DW_LLE_offset_pair,
10501 "DW_LLE_offset_pair (%s)",
10502 list_head->ll_symbol);
10503 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10504 "Location list begin address (%s)",
10505 list_head->ll_symbol);
10506 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10507 "Location list end address (%s)",
10508 list_head->ll_symbol);
10510 else if (HAVE_AS_LEB128)
10512 /* Otherwise, find out how many consecutive entries could share
10513 the same base entry. If just one, emit DW_LLE_start_length,
10514 otherwise emit DW_LLE_base_address for the base address
10515 followed by a series of DW_LLE_offset_pair. */
10516 if (last_section == NULL || curr->section != last_section)
10518 dw_loc_list_ref curr2;
10519 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10520 curr2 = curr2->dw_loc_next)
10522 if (strcmp (curr2->begin, curr2->end) == 0
10523 && !curr2->force)
10524 continue;
10525 break;
10527 if (curr2 == NULL || curr->section != curr2->section)
10528 last_section = NULL;
10529 else
10531 last_section = curr->section;
10532 base_label = curr->begin;
10533 dw2_asm_output_data (1, DW_LLE_base_address,
10534 "DW_LLE_base_address (%s)",
10535 list_head->ll_symbol);
10536 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10537 "Base address (%s)",
10538 list_head->ll_symbol);
10541 /* Only one entry with the same base address. Use
10542 DW_LLE_start_length with absolute address and uleb128
10543 length. */
10544 if (last_section == NULL)
10546 dwarf2out_maybe_output_loclist_view_pair (curr);
10547 dw2_asm_output_data (1, DW_LLE_start_length,
10548 "DW_LLE_start_length (%s)",
10549 list_head->ll_symbol);
10550 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10551 "Location list begin address (%s)",
10552 list_head->ll_symbol);
10553 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10554 "Location list length "
10555 "(%s)", list_head->ll_symbol);
10557 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10558 DW_LLE_base_address. */
10559 else
10561 dwarf2out_maybe_output_loclist_view_pair (curr);
10562 dw2_asm_output_data (1, DW_LLE_offset_pair,
10563 "DW_LLE_offset_pair (%s)",
10564 list_head->ll_symbol);
10565 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10566 "Location list begin address "
10567 "(%s)", list_head->ll_symbol);
10568 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10569 "Location list end address "
10570 "(%s)", list_head->ll_symbol);
10573 /* The assembler does not support .uleb128 directive. Emit
10574 DW_LLE_start_end with a pair of absolute addresses. */
10575 else
10577 dwarf2out_maybe_output_loclist_view_pair (curr);
10578 dw2_asm_output_data (1, DW_LLE_start_end,
10579 "DW_LLE_start_end (%s)",
10580 list_head->ll_symbol);
10581 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10582 "Location list begin address (%s)",
10583 list_head->ll_symbol);
10584 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10585 "Location list end address (%s)",
10586 list_head->ll_symbol);
10589 else if (dwarf_split_debug_info)
10591 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10592 and 4 byte length. */
10593 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10594 "Location list start/length entry (%s)",
10595 list_head->ll_symbol);
10596 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10597 "Location list range start index (%s)",
10598 curr->begin);
10599 /* The length field is 4 bytes. If we ever need to support
10600 an 8-byte length, we can add a new DW_LLE code or fall back
10601 to DW_LLE_GNU_start_end_entry. */
10602 dw2_asm_output_delta (4, curr->end, curr->begin,
10603 "Location list range length (%s)",
10604 list_head->ll_symbol);
10606 else if (!have_multiple_function_sections)
10608 /* Pair of relative addresses against start of text section. */
10609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10610 "Location list begin address (%s)",
10611 list_head->ll_symbol);
10612 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10613 "Location list end address (%s)",
10614 list_head->ll_symbol);
10616 else
10618 /* Pair of absolute addresses. */
10619 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10620 "Location list begin address (%s)",
10621 list_head->ll_symbol);
10622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10623 "Location list end address (%s)",
10624 list_head->ll_symbol);
10627 /* Output the block length for this list of location operations. */
10628 if (dwarf_version >= 5)
10629 dw2_asm_output_data_uleb128 (size, "Location expression size");
10630 else
10632 gcc_assert (size <= 0xffff);
10633 dw2_asm_output_data (2, size, "Location expression size");
10636 output_loc_sequence (curr->expr, -1);
10639 /* And finally list termination. */
10640 if (dwarf_version >= 5)
10641 dw2_asm_output_data (1, DW_LLE_end_of_list,
10642 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10643 else if (dwarf_split_debug_info)
10644 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10645 "Location list terminator (%s)",
10646 list_head->ll_symbol);
10647 else
10649 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10650 "Location list terminator begin (%s)",
10651 list_head->ll_symbol);
10652 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10653 "Location list terminator end (%s)",
10654 list_head->ll_symbol);
10657 gcc_assert (!list_head->vl_symbol
10658 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10661 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10662 section. Emit a relocated reference if val_entry is NULL, otherwise,
10663 emit an indirect reference. */
10665 static void
10666 output_range_list_offset (dw_attr_node *a)
10668 const char *name = dwarf_attr_name (a->dw_attr);
10670 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10672 if (dwarf_version >= 5)
10674 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10675 dw2_asm_output_offset (dwarf_offset_size, r->label,
10676 debug_ranges_section, "%s", name);
10678 else
10680 char *p = strchr (ranges_section_label, '\0');
10681 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10682 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10683 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10684 debug_ranges_section, "%s", name);
10685 *p = '\0';
10688 else if (dwarf_version >= 5)
10690 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10691 gcc_assert (rnglist_idx);
10692 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10694 else
10695 dw2_asm_output_data (dwarf_offset_size,
10696 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10697 "%s (offset from %s)", name, ranges_section_label);
10700 /* Output the offset into the debug_loc section. */
10702 static void
10703 output_loc_list_offset (dw_attr_node *a)
10705 char *sym = AT_loc_list (a)->ll_symbol;
10707 gcc_assert (sym);
10708 if (!dwarf_split_debug_info)
10709 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10710 "%s", dwarf_attr_name (a->dw_attr));
10711 else if (dwarf_version >= 5)
10713 gcc_assert (AT_loc_list (a)->num_assigned);
10714 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10715 dwarf_attr_name (a->dw_attr),
10716 sym);
10718 else
10719 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10720 "%s", dwarf_attr_name (a->dw_attr));
10723 /* Output the offset into the debug_loc section. */
10725 static void
10726 output_view_list_offset (dw_attr_node *a)
10728 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10730 gcc_assert (sym);
10731 if (dwarf_split_debug_info)
10732 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10733 "%s", dwarf_attr_name (a->dw_attr));
10734 else
10735 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10736 "%s", dwarf_attr_name (a->dw_attr));
10739 /* Output an attribute's index or value appropriately. */
10741 static void
10742 output_attr_index_or_value (dw_attr_node *a)
10744 const char *name = dwarf_attr_name (a->dw_attr);
10746 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10748 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10749 return;
10751 switch (AT_class (a))
10753 case dw_val_class_addr:
10754 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10755 break;
10756 case dw_val_class_high_pc:
10757 case dw_val_class_lbl_id:
10758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10759 break;
10760 default:
10761 gcc_unreachable ();
10765 /* Output a type signature. */
10767 static inline void
10768 output_signature (const char *sig, const char *name)
10770 int i;
10772 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10773 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10776 /* Output a discriminant value. */
10778 static inline void
10779 output_discr_value (dw_discr_value *discr_value, const char *name)
10781 if (discr_value->pos)
10782 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10783 else
10784 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10787 /* Output the DIE and its attributes. Called recursively to generate
10788 the definitions of each child DIE. */
10790 static void
10791 output_die (dw_die_ref die)
10793 dw_attr_node *a;
10794 dw_die_ref c;
10795 unsigned long size;
10796 unsigned ix;
10798 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10799 (unsigned long)die->die_offset,
10800 dwarf_tag_name (die->die_tag));
10802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10804 const char *name = dwarf_attr_name (a->dw_attr);
10806 switch (AT_class (a))
10808 case dw_val_class_addr:
10809 output_attr_index_or_value (a);
10810 break;
10812 case dw_val_class_offset:
10813 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10814 "%s", name);
10815 break;
10817 case dw_val_class_range_list:
10818 output_range_list_offset (a);
10819 break;
10821 case dw_val_class_loc:
10822 size = size_of_locs (AT_loc (a));
10824 /* Output the block length for this list of location operations. */
10825 if (dwarf_version >= 4)
10826 dw2_asm_output_data_uleb128 (size, "%s", name);
10827 else
10828 dw2_asm_output_data (constant_size (size), size, "%s", name);
10830 output_loc_sequence (AT_loc (a), -1);
10831 break;
10833 case dw_val_class_const:
10834 /* ??? It would be slightly more efficient to use a scheme like is
10835 used for unsigned constants below, but gdb 4.x does not sign
10836 extend. Gdb 5.x does sign extend. */
10837 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10838 break;
10840 case dw_val_class_unsigned_const:
10842 int csize = constant_size (AT_unsigned (a));
10843 if (dwarf_version == 3
10844 && a->dw_attr == DW_AT_data_member_location
10845 && csize >= 4)
10846 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10847 else
10848 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10850 break;
10852 case dw_val_class_symview:
10854 int vsize;
10855 if (symview_upper_bound <= 0xff)
10856 vsize = 1;
10857 else if (symview_upper_bound <= 0xffff)
10858 vsize = 2;
10859 else if (symview_upper_bound <= 0xffffffff)
10860 vsize = 4;
10861 else
10862 vsize = 8;
10863 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10864 "%s", name);
10866 break;
10868 case dw_val_class_const_implicit:
10869 if (flag_debug_asm)
10870 fprintf (asm_out_file, "\t\t\t%s %s ("
10871 HOST_WIDE_INT_PRINT_DEC ")\n",
10872 ASM_COMMENT_START, name, AT_int (a));
10873 break;
10875 case dw_val_class_unsigned_const_implicit:
10876 if (flag_debug_asm)
10877 fprintf (asm_out_file, "\t\t\t%s %s ("
10878 HOST_WIDE_INT_PRINT_HEX ")\n",
10879 ASM_COMMENT_START, name, AT_unsigned (a));
10880 break;
10882 case dw_val_class_const_double:
10884 unsigned HOST_WIDE_INT first, second;
10886 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10887 dw2_asm_output_data (1,
10888 HOST_BITS_PER_DOUBLE_INT
10889 / HOST_BITS_PER_CHAR,
10890 NULL);
10892 if (WORDS_BIG_ENDIAN)
10894 first = a->dw_attr_val.v.val_double.high;
10895 second = a->dw_attr_val.v.val_double.low;
10897 else
10899 first = a->dw_attr_val.v.val_double.low;
10900 second = a->dw_attr_val.v.val_double.high;
10903 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10904 first, "%s", name);
10905 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10906 second, NULL);
10908 break;
10910 case dw_val_class_wide_int:
10912 int i;
10913 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10914 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10915 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10916 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10917 * l, NULL);
10919 if (WORDS_BIG_ENDIAN)
10920 for (i = len - 1; i >= 0; --i)
10922 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10923 "%s", name);
10924 name = "";
10926 else
10927 for (i = 0; i < len; ++i)
10929 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10930 "%s", name);
10931 name = "";
10934 break;
10936 case dw_val_class_vec:
10938 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10939 unsigned int len = a->dw_attr_val.v.val_vec.length;
10940 unsigned int i;
10941 unsigned char *p;
10943 dw2_asm_output_data (constant_size (len * elt_size),
10944 len * elt_size, "%s", name);
10945 if (elt_size > sizeof (HOST_WIDE_INT))
10947 elt_size /= 2;
10948 len *= 2;
10950 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10951 i < len;
10952 i++, p += elt_size)
10953 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10954 "fp or vector constant word %u", i);
10955 break;
10958 case dw_val_class_flag:
10959 if (dwarf_version >= 4)
10961 /* Currently all add_AT_flag calls pass in 1 as last argument,
10962 so DW_FORM_flag_present can be used. If that ever changes,
10963 we'll need to use DW_FORM_flag and have some optimization
10964 in build_abbrev_table that will change those to
10965 DW_FORM_flag_present if it is set to 1 in all DIEs using
10966 the same abbrev entry. */
10967 gcc_assert (AT_flag (a) == 1);
10968 if (flag_debug_asm)
10969 fprintf (asm_out_file, "\t\t\t%s %s\n",
10970 ASM_COMMENT_START, name);
10971 break;
10973 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10974 break;
10976 case dw_val_class_loc_list:
10977 output_loc_list_offset (a);
10978 break;
10980 case dw_val_class_view_list:
10981 output_view_list_offset (a);
10982 break;
10984 case dw_val_class_die_ref:
10985 if (AT_ref_external (a))
10987 if (AT_ref (a)->comdat_type_p)
10989 comdat_type_node *type_node
10990 = AT_ref (a)->die_id.die_type_node;
10992 gcc_assert (type_node);
10993 output_signature (type_node->signature, name);
10995 else
10997 const char *sym = AT_ref (a)->die_id.die_symbol;
10998 int size;
11000 gcc_assert (sym);
11001 /* In DWARF2, DW_FORM_ref_addr is sized by target address
11002 length, whereas in DWARF3 it's always sized as an
11003 offset. */
11004 if (dwarf_version == 2)
11005 size = DWARF2_ADDR_SIZE;
11006 else
11007 size = dwarf_offset_size;
11008 /* ??? We cannot unconditionally output die_offset if
11009 non-zero - others might create references to those
11010 DIEs via symbols.
11011 And we do not clear its DIE offset after outputting it
11012 (and the label refers to the actual DIEs, not the
11013 DWARF CU unit header which is when using label + offset
11014 would be the correct thing to do).
11015 ??? This is the reason for the with_offset flag. */
11016 if (AT_ref (a)->with_offset)
11017 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11018 debug_info_section, "%s", name);
11019 else
11020 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11021 name);
11024 else
11026 gcc_assert (AT_ref (a)->die_offset);
11027 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11028 "%s", name);
11030 break;
11032 case dw_val_class_fde_ref:
11034 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11036 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11037 a->dw_attr_val.v.val_fde_index * 2);
11038 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11039 "%s", name);
11041 break;
11043 case dw_val_class_vms_delta:
11044 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11045 dw2_asm_output_vms_delta (dwarf_offset_size,
11046 AT_vms_delta2 (a), AT_vms_delta1 (a),
11047 "%s", name);
11048 #else
11049 dw2_asm_output_delta (dwarf_offset_size,
11050 AT_vms_delta2 (a), AT_vms_delta1 (a),
11051 "%s", name);
11052 #endif
11053 break;
11055 case dw_val_class_lbl_id:
11056 output_attr_index_or_value (a);
11057 break;
11059 case dw_val_class_lineptr:
11060 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11061 debug_line_section, "%s", name);
11062 break;
11064 case dw_val_class_macptr:
11065 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11066 debug_macinfo_section, "%s", name);
11067 break;
11069 case dw_val_class_loclistsptr:
11070 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11071 debug_loc_section, "%s", name);
11072 break;
11074 case dw_val_class_str:
11075 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11076 dw2_asm_output_offset (dwarf_offset_size,
11077 a->dw_attr_val.v.val_str->label,
11078 debug_str_section,
11079 "%s: \"%s\"", name, AT_string (a));
11080 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11081 dw2_asm_output_offset (dwarf_offset_size,
11082 a->dw_attr_val.v.val_str->label,
11083 debug_line_str_section,
11084 "%s: \"%s\"", name, AT_string (a));
11085 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11086 dw2_asm_output_data_uleb128 (AT_index (a),
11087 "%s: \"%s\"", name, AT_string (a));
11088 else
11089 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11090 break;
11092 case dw_val_class_file:
11094 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11096 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11097 a->dw_attr_val.v.val_file->filename);
11098 break;
11101 case dw_val_class_file_implicit:
11102 if (flag_debug_asm)
11103 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11104 ASM_COMMENT_START, name,
11105 maybe_emit_file (a->dw_attr_val.v.val_file),
11106 a->dw_attr_val.v.val_file->filename);
11107 break;
11109 case dw_val_class_data8:
11111 int i;
11113 for (i = 0; i < 8; i++)
11114 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11115 i == 0 ? "%s" : NULL, name);
11116 break;
11119 case dw_val_class_high_pc:
11120 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11121 get_AT_low_pc (die), "DW_AT_high_pc");
11122 break;
11124 case dw_val_class_discr_value:
11125 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11126 break;
11128 case dw_val_class_discr_list:
11130 dw_discr_list_ref list = AT_discr_list (a);
11131 const int size = size_of_discr_list (list);
11133 /* This is a block, so output its length first. */
11134 dw2_asm_output_data (constant_size (size), size,
11135 "%s: block size", name);
11137 for (; list != NULL; list = list->dw_discr_next)
11139 /* One byte for the discriminant value descriptor, and then as
11140 many LEB128 numbers as required. */
11141 if (list->dw_discr_range)
11142 dw2_asm_output_data (1, DW_DSC_range,
11143 "%s: DW_DSC_range", name);
11144 else
11145 dw2_asm_output_data (1, DW_DSC_label,
11146 "%s: DW_DSC_label", name);
11148 output_discr_value (&list->dw_discr_lower_bound, name);
11149 if (list->dw_discr_range)
11150 output_discr_value (&list->dw_discr_upper_bound, name);
11152 break;
11155 default:
11156 gcc_unreachable ();
11160 FOR_EACH_CHILD (die, c, output_die (c));
11162 /* Add null byte to terminate sibling list. */
11163 if (die->die_child != NULL)
11164 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11165 (unsigned long) die->die_offset);
11168 /* Output the dwarf version number. */
11170 static void
11171 output_dwarf_version ()
11173 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11174 views in loclist. That will change eventually. */
11175 if (dwarf_version == 6)
11177 static bool once;
11178 if (!once)
11180 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11181 "incompatibilities");
11182 once = true;
11184 dw2_asm_output_data (2, 5, "DWARF version number");
11186 else
11187 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11190 /* Output the compilation unit that appears at the beginning of the
11191 .debug_info section, and precedes the DIE descriptions. */
11193 static void
11194 output_compilation_unit_header (enum dwarf_unit_type ut)
11196 if (!XCOFF_DEBUGGING_INFO)
11198 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11199 dw2_asm_output_data (4, 0xffffffff,
11200 "Initial length escape value indicating 64-bit DWARF extension");
11201 dw2_asm_output_data (dwarf_offset_size,
11202 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11203 "Length of Compilation Unit Info");
11206 output_dwarf_version ();
11207 if (dwarf_version >= 5)
11209 const char *name;
11210 switch (ut)
11212 case DW_UT_compile: name = "DW_UT_compile"; break;
11213 case DW_UT_type: name = "DW_UT_type"; break;
11214 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11215 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11216 default: gcc_unreachable ();
11218 dw2_asm_output_data (1, ut, "%s", name);
11219 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11221 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11222 debug_abbrev_section,
11223 "Offset Into Abbrev. Section");
11224 if (dwarf_version < 5)
11225 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11228 /* Output the compilation unit DIE and its children. */
11230 static void
11231 output_comp_unit (dw_die_ref die, int output_if_empty,
11232 const unsigned char *dwo_id)
11234 const char *secname, *oldsym;
11235 char *tmp;
11237 /* Unless we are outputting main CU, we may throw away empty ones. */
11238 if (!output_if_empty && die->die_child == NULL)
11239 return;
11241 /* Even if there are no children of this DIE, we must output the information
11242 about the compilation unit. Otherwise, on an empty translation unit, we
11243 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11244 will then complain when examining the file. First mark all the DIEs in
11245 this CU so we know which get local refs. */
11246 mark_dies (die);
11248 external_ref_hash_type *extern_map = optimize_external_refs (die);
11250 /* For now, optimize only the main CU, in order to optimize the rest
11251 we'd need to see all of them earlier. Leave the rest for post-linking
11252 tools like DWZ. */
11253 if (die == comp_unit_die ())
11254 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11256 build_abbrev_table (die, extern_map);
11258 optimize_abbrev_table ();
11260 delete extern_map;
11262 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11263 next_die_offset = (dwo_id
11264 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11265 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11266 calc_die_sizes (die);
11268 oldsym = die->die_id.die_symbol;
11269 if (oldsym && die->comdat_type_p)
11271 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11273 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11274 secname = tmp;
11275 die->die_id.die_symbol = NULL;
11276 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11278 else
11280 switch_to_section (debug_info_section);
11281 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11282 info_section_emitted = true;
11285 /* For LTO cross unit DIE refs we want a symbol on the start of the
11286 debuginfo section, not on the CU DIE. */
11287 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11289 /* ??? No way to get visibility assembled without a decl. */
11290 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11291 get_identifier (oldsym), char_type_node);
11292 TREE_PUBLIC (decl) = true;
11293 TREE_STATIC (decl) = true;
11294 DECL_ARTIFICIAL (decl) = true;
11295 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11296 DECL_VISIBILITY_SPECIFIED (decl) = true;
11297 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11298 #ifdef ASM_WEAKEN_LABEL
11299 /* We prefer a .weak because that handles duplicates from duplicate
11300 archive members in a graceful way. */
11301 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11302 #else
11303 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11304 #endif
11305 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11308 /* Output debugging information. */
11309 output_compilation_unit_header (dwo_id
11310 ? DW_UT_split_compile : DW_UT_compile);
11311 if (dwarf_version >= 5)
11313 if (dwo_id != NULL)
11314 for (int i = 0; i < 8; i++)
11315 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11317 output_die (die);
11319 /* Leave the marks on the main CU, so we can check them in
11320 output_pubnames. */
11321 if (oldsym)
11323 unmark_dies (die);
11324 die->die_id.die_symbol = oldsym;
11328 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11329 and .debug_pubtypes. This is configured per-target, but can be
11330 overridden by the -gpubnames or -gno-pubnames options. */
11332 static inline bool
11333 want_pubnames (void)
11335 if (debug_info_level <= DINFO_LEVEL_TERSE
11336 /* Names and types go to the early debug part only. */
11337 || in_lto_p)
11338 return false;
11339 if (debug_generate_pub_sections != -1)
11340 return debug_generate_pub_sections;
11341 return targetm.want_debug_pub_sections;
11344 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11346 static void
11347 add_AT_pubnames (dw_die_ref die)
11349 if (want_pubnames ())
11350 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11353 /* Add a string attribute value to a skeleton DIE. */
11355 static inline void
11356 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11357 const char *str)
11359 dw_attr_node attr;
11360 struct indirect_string_node *node;
11362 if (! skeleton_debug_str_hash)
11363 skeleton_debug_str_hash
11364 = hash_table<indirect_string_hasher>::create_ggc (10);
11366 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11367 find_string_form (node);
11368 if (node->form == dwarf_FORM (DW_FORM_strx))
11369 node->form = DW_FORM_strp;
11371 attr.dw_attr = attr_kind;
11372 attr.dw_attr_val.val_class = dw_val_class_str;
11373 attr.dw_attr_val.val_entry = NULL;
11374 attr.dw_attr_val.v.val_str = node;
11375 add_dwarf_attr (die, &attr);
11378 /* Helper function to generate top-level dies for skeleton debug_info and
11379 debug_types. */
11381 static void
11382 add_top_level_skeleton_die_attrs (dw_die_ref die)
11384 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11385 const char *comp_dir = comp_dir_string ();
11387 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11388 if (comp_dir != NULL)
11389 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11390 add_AT_pubnames (die);
11391 if (addr_index_table != NULL && addr_index_table->size () > 0)
11392 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11395 /* Output skeleton debug sections that point to the dwo file. */
11397 static void
11398 output_skeleton_debug_sections (dw_die_ref comp_unit,
11399 const unsigned char *dwo_id)
11401 /* These attributes will be found in the full debug_info section. */
11402 remove_AT (comp_unit, DW_AT_producer);
11403 remove_AT (comp_unit, DW_AT_language);
11405 switch_to_section (debug_skeleton_info_section);
11406 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11408 /* Produce the skeleton compilation-unit header. This one differs enough from
11409 a normal CU header that it's better not to call output_compilation_unit
11410 header. */
11411 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11412 dw2_asm_output_data (4, 0xffffffff,
11413 "Initial length escape value indicating 64-bit "
11414 "DWARF extension");
11416 dw2_asm_output_data (dwarf_offset_size,
11417 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11418 - DWARF_INITIAL_LENGTH_SIZE
11419 + size_of_die (comp_unit),
11420 "Length of Compilation Unit Info");
11421 output_dwarf_version ();
11422 if (dwarf_version >= 5)
11424 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11425 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11427 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11428 debug_skeleton_abbrev_section,
11429 "Offset Into Abbrev. Section");
11430 if (dwarf_version < 5)
11431 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11432 else
11433 for (int i = 0; i < 8; i++)
11434 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11436 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11437 output_die (comp_unit);
11439 /* Build the skeleton debug_abbrev section. */
11440 switch_to_section (debug_skeleton_abbrev_section);
11441 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11443 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11445 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11448 /* Output a comdat type unit DIE and its children. */
11450 static void
11451 output_comdat_type_unit (comdat_type_node *node,
11452 bool early_lto_debug ATTRIBUTE_UNUSED)
11454 const char *secname;
11455 char *tmp;
11456 int i;
11457 #if defined (OBJECT_FORMAT_ELF)
11458 tree comdat_key;
11459 #endif
11461 /* First mark all the DIEs in this CU so we know which get local refs. */
11462 mark_dies (node->root_die);
11464 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11466 build_abbrev_table (node->root_die, extern_map);
11468 delete extern_map;
11469 extern_map = NULL;
11471 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11472 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11473 calc_die_sizes (node->root_die);
11475 #if defined (OBJECT_FORMAT_ELF)
11476 if (dwarf_version >= 5)
11478 if (!dwarf_split_debug_info)
11479 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11480 else
11481 secname = (early_lto_debug
11482 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11484 else if (!dwarf_split_debug_info)
11485 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11486 else
11487 secname = (early_lto_debug
11488 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11490 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11491 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11492 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11493 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11494 comdat_key = get_identifier (tmp);
11495 targetm.asm_out.named_section (secname,
11496 SECTION_DEBUG | SECTION_LINKONCE,
11497 comdat_key);
11498 #else
11499 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11500 sprintf (tmp, (dwarf_version >= 5
11501 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11502 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11503 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11504 secname = tmp;
11505 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11506 #endif
11508 /* Output debugging information. */
11509 output_compilation_unit_header (dwarf_split_debug_info
11510 ? DW_UT_split_type : DW_UT_type);
11511 output_signature (node->signature, "Type Signature");
11512 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11513 "Offset to Type DIE");
11514 output_die (node->root_die);
11516 unmark_dies (node->root_die);
11519 /* Return the DWARF2/3 pubname associated with a decl. */
11521 static const char *
11522 dwarf2_name (tree decl, int scope)
11524 if (DECL_NAMELESS (decl))
11525 return NULL;
11526 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11529 /* Add a new entry to .debug_pubnames if appropriate. */
11531 static void
11532 add_pubname_string (const char *str, dw_die_ref die)
11534 pubname_entry e;
11536 e.die = die;
11537 e.name = xstrdup (str);
11538 vec_safe_push (pubname_table, e);
11541 static void
11542 add_pubname (tree decl, dw_die_ref die)
11544 if (!want_pubnames ())
11545 return;
11547 /* Don't add items to the table when we expect that the consumer will have
11548 just read the enclosing die. For example, if the consumer is looking at a
11549 class_member, it will either be inside the class already, or will have just
11550 looked up the class to find the member. Either way, searching the class is
11551 faster than searching the index. */
11552 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11553 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11555 const char *name = dwarf2_name (decl, 1);
11557 if (name)
11558 add_pubname_string (name, die);
11562 /* Add an enumerator to the pubnames section. */
11564 static void
11565 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11567 pubname_entry e;
11569 gcc_assert (scope_name);
11570 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11571 e.die = die;
11572 vec_safe_push (pubname_table, e);
11575 /* Add a new entry to .debug_pubtypes if appropriate. */
11577 static void
11578 add_pubtype (tree decl, dw_die_ref die)
11580 pubname_entry e;
11582 if (!want_pubnames ())
11583 return;
11585 if ((TREE_PUBLIC (decl)
11586 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11587 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11589 tree scope = NULL;
11590 const char *scope_name = "";
11591 const char *sep = is_cxx () ? "::" : ".";
11592 const char *name;
11594 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11595 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11597 scope_name = lang_hooks.dwarf_name (scope, 1);
11598 if (scope_name != NULL && scope_name[0] != '\0')
11599 scope_name = concat (scope_name, sep, NULL);
11600 else
11601 scope_name = "";
11604 if (TYPE_P (decl))
11605 name = type_tag (decl);
11606 else
11607 name = lang_hooks.dwarf_name (decl, 1);
11609 /* If we don't have a name for the type, there's no point in adding
11610 it to the table. */
11611 if (name != NULL && name[0] != '\0')
11613 e.die = die;
11614 e.name = concat (scope_name, name, NULL);
11615 vec_safe_push (pubtype_table, e);
11618 /* Although it might be more consistent to add the pubinfo for the
11619 enumerators as their dies are created, they should only be added if the
11620 enum type meets the criteria above. So rather than re-check the parent
11621 enum type whenever an enumerator die is created, just output them all
11622 here. This isn't protected by the name conditional because anonymous
11623 enums don't have names. */
11624 if (die->die_tag == DW_TAG_enumeration_type)
11626 dw_die_ref c;
11628 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11633 /* Output a single entry in the pubnames table. */
11635 static void
11636 output_pubname (dw_offset die_offset, pubname_entry *entry)
11638 dw_die_ref die = entry->die;
11639 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11641 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11643 if (debug_generate_pub_sections == 2)
11645 /* This logic follows gdb's method for determining the value of the flag
11646 byte. */
11647 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11648 switch (die->die_tag)
11650 case DW_TAG_typedef:
11651 case DW_TAG_base_type:
11652 case DW_TAG_subrange_type:
11653 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11654 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11655 break;
11656 case DW_TAG_enumerator:
11657 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11658 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11659 if (!is_cxx ())
11660 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11661 break;
11662 case DW_TAG_subprogram:
11663 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11664 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11665 if (!is_ada ())
11666 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11667 break;
11668 case DW_TAG_constant:
11669 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11670 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11671 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11672 break;
11673 case DW_TAG_variable:
11674 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11675 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11676 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11677 break;
11678 case DW_TAG_namespace:
11679 case DW_TAG_imported_declaration:
11680 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11681 break;
11682 case DW_TAG_class_type:
11683 case DW_TAG_interface_type:
11684 case DW_TAG_structure_type:
11685 case DW_TAG_union_type:
11686 case DW_TAG_enumeration_type:
11687 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11688 if (!is_cxx ())
11689 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11690 break;
11691 default:
11692 /* An unusual tag. Leave the flag-byte empty. */
11693 break;
11695 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11696 "GDB-index flags");
11699 dw2_asm_output_nstring (entry->name, -1, "external name");
11703 /* Output the public names table used to speed up access to externally
11704 visible names; or the public types table used to find type definitions. */
11706 static void
11707 output_pubnames (vec<pubname_entry, va_gc> *names)
11709 unsigned i;
11710 unsigned long pubnames_length = size_of_pubnames (names);
11711 pubname_entry *pub;
11713 if (!XCOFF_DEBUGGING_INFO)
11715 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11716 dw2_asm_output_data (4, 0xffffffff,
11717 "Initial length escape value indicating 64-bit DWARF extension");
11718 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11719 "Pub Info Length");
11722 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11723 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11725 if (dwarf_split_debug_info)
11726 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11727 debug_skeleton_info_section,
11728 "Offset of Compilation Unit Info");
11729 else
11730 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11731 debug_info_section,
11732 "Offset of Compilation Unit Info");
11733 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11734 "Compilation Unit Length");
11736 FOR_EACH_VEC_ELT (*names, i, pub)
11738 if (include_pubname_in_output (names, pub))
11740 dw_offset die_offset = pub->die->die_offset;
11742 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11743 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11744 gcc_assert (pub->die->die_mark);
11746 /* If we're putting types in their own .debug_types sections,
11747 the .debug_pubtypes table will still point to the compile
11748 unit (not the type unit), so we want to use the offset of
11749 the skeleton DIE (if there is one). */
11750 if (pub->die->comdat_type_p && names == pubtype_table)
11752 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11754 if (type_node != NULL)
11755 die_offset = (type_node->skeleton_die != NULL
11756 ? type_node->skeleton_die->die_offset
11757 : comp_unit_die ()->die_offset);
11760 output_pubname (die_offset, pub);
11764 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11767 /* Output public names and types tables if necessary. */
11769 static void
11770 output_pubtables (void)
11772 if (!want_pubnames () || !info_section_emitted)
11773 return;
11775 switch_to_section (debug_pubnames_section);
11776 output_pubnames (pubname_table);
11777 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11778 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11779 simply won't look for the section. */
11780 switch_to_section (debug_pubtypes_section);
11781 output_pubnames (pubtype_table);
11785 /* Output the information that goes into the .debug_aranges table.
11786 Namely, define the beginning and ending address range of the
11787 text section generated for this compilation unit. */
11789 static void
11790 output_aranges (void)
11792 unsigned i;
11793 unsigned long aranges_length = size_of_aranges ();
11795 if (!XCOFF_DEBUGGING_INFO)
11797 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11798 dw2_asm_output_data (4, 0xffffffff,
11799 "Initial length escape value indicating 64-bit DWARF extension");
11800 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11801 "Length of Address Ranges Info");
11804 /* Version number for aranges is still 2, even up to DWARF5. */
11805 dw2_asm_output_data (2, 2, "DWARF aranges version");
11806 if (dwarf_split_debug_info)
11807 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11808 debug_skeleton_info_section,
11809 "Offset of Compilation Unit Info");
11810 else
11811 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11812 debug_info_section,
11813 "Offset of Compilation Unit Info");
11814 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11815 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11817 /* We need to align to twice the pointer size here. */
11818 if (DWARF_ARANGES_PAD_SIZE)
11820 /* Pad using a 2 byte words so that padding is correct for any
11821 pointer size. */
11822 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11823 2 * DWARF2_ADDR_SIZE);
11824 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11825 dw2_asm_output_data (2, 0, NULL);
11828 /* It is necessary not to output these entries if the sections were
11829 not used; if the sections were not used, the length will be 0 and
11830 the address may end up as 0 if the section is discarded by ld
11831 --gc-sections, leaving an invalid (0, 0) entry that can be
11832 confused with the terminator. */
11833 if (switch_text_ranges)
11835 const char *prev_loc = text_section_label;
11836 const char *loc;
11837 unsigned idx;
11839 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11840 if (prev_loc)
11842 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11843 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11844 prev_loc = NULL;
11846 else
11847 prev_loc = loc;
11849 if (prev_loc)
11851 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11852 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11853 prev_loc, "Length");
11857 if (switch_cold_ranges)
11859 const char *prev_loc = cold_text_section_label;
11860 const char *loc;
11861 unsigned idx;
11863 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11864 if (prev_loc)
11866 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11867 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11868 prev_loc = NULL;
11870 else
11871 prev_loc = loc;
11873 if (prev_loc)
11875 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11876 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11877 prev_loc, "Length");
11881 if (have_multiple_function_sections)
11883 unsigned fde_idx;
11884 dw_fde_ref fde;
11886 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11888 if (fde->ignored_debug)
11889 continue;
11890 if (!fde->in_std_section)
11892 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11893 "Address");
11894 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11895 fde->dw_fde_begin, "Length");
11897 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11899 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11900 "Address");
11901 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11902 fde->dw_fde_second_begin, "Length");
11907 /* Output the terminator words. */
11908 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11909 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11912 /* Add a new entry to .debug_ranges. Return its index into
11913 ranges_table vector. */
11915 static unsigned int
11916 add_ranges_num (int num, bool maybe_new_sec)
11918 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11919 vec_safe_push (ranges_table, r);
11920 return vec_safe_length (ranges_table) - 1;
11923 /* Add a new entry to .debug_ranges corresponding to a block, or a
11924 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11925 this entry might be in a different section from previous range. */
11927 static unsigned int
11928 add_ranges (const_tree block, bool maybe_new_sec)
11930 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11933 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11934 chain, or middle entry of a chain that will be directly referred to. */
11936 static void
11937 note_rnglist_head (unsigned int offset)
11939 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11940 return;
11941 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11944 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11945 When using dwarf_split_debug_info, address attributes in dies destined
11946 for the final executable should be direct references--setting the
11947 parameter force_direct ensures this behavior. */
11949 static void
11950 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11951 bool *added, bool force_direct)
11953 unsigned int in_use = vec_safe_length (ranges_by_label);
11954 unsigned int offset;
11955 dw_ranges_by_label rbl = { begin, end };
11956 vec_safe_push (ranges_by_label, rbl);
11957 offset = add_ranges_num (-(int)in_use - 1, true);
11958 if (!*added)
11960 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11961 *added = true;
11962 note_rnglist_head (offset);
11963 if (dwarf_split_debug_info && force_direct)
11964 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11968 /* Emit .debug_ranges section. */
11970 static void
11971 output_ranges (void)
11973 unsigned i;
11974 static const char *const start_fmt = "Offset %#x";
11975 const char *fmt = start_fmt;
11976 dw_ranges *r;
11978 switch_to_section (debug_ranges_section);
11979 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11980 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11982 int block_num = r->num;
11984 if (block_num > 0)
11986 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11987 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11989 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11990 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11992 /* If all code is in the text section, then the compilation
11993 unit base address defaults to DW_AT_low_pc, which is the
11994 base of the text section. */
11995 if (!have_multiple_function_sections)
11997 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11998 text_section_label,
11999 fmt, i * 2 * DWARF2_ADDR_SIZE);
12000 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12001 text_section_label, NULL);
12004 /* Otherwise, the compilation unit base address is zero,
12005 which allows us to use absolute addresses, and not worry
12006 about whether the target supports cross-section
12007 arithmetic. */
12008 else
12010 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12011 fmt, i * 2 * DWARF2_ADDR_SIZE);
12012 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12015 fmt = NULL;
12018 /* Negative block_num stands for an index into ranges_by_label. */
12019 else if (block_num < 0)
12021 int lab_idx = - block_num - 1;
12023 if (!have_multiple_function_sections)
12025 gcc_unreachable ();
12026 #if 0
12027 /* If we ever use add_ranges_by_labels () for a single
12028 function section, all we have to do is to take out
12029 the #if 0 above. */
12030 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12031 (*ranges_by_label)[lab_idx].begin,
12032 text_section_label,
12033 fmt, i * 2 * DWARF2_ADDR_SIZE);
12034 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12035 (*ranges_by_label)[lab_idx].end,
12036 text_section_label, NULL);
12037 #endif
12039 else
12041 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12042 (*ranges_by_label)[lab_idx].begin,
12043 fmt, i * 2 * DWARF2_ADDR_SIZE);
12044 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12045 (*ranges_by_label)[lab_idx].end,
12046 NULL);
12049 else
12051 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12052 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12053 fmt = start_fmt;
12058 /* Non-zero if .debug_line_str should be used for .debug_line section
12059 strings or strings that are likely shareable with those. */
12060 #define DWARF5_USE_DEBUG_LINE_STR \
12061 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12062 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12063 /* FIXME: there is no .debug_line_str.dwo section, \
12064 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12065 && !dwarf_split_debug_info)
12068 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12069 DWARF5 .debug_line tables using .debug_line_str or we generate
12070 it ourselves, except for split-dwarf which doesn't have a
12071 .debug_line_str. */
12072 static bool
12073 asm_outputs_debug_line_str (void)
12075 if (dwarf_version >= 5
12076 && ! output_asm_line_debug_info ()
12077 && DWARF5_USE_DEBUG_LINE_STR)
12078 return true;
12079 else
12081 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12082 return !dwarf_split_debug_info && dwarf_version >= 5;
12083 #else
12084 return false;
12085 #endif
12089 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12090 I is index of the following range. */
12092 static bool
12093 use_distinct_base_address_for_range (unsigned int i)
12095 if (i >= vec_safe_length (ranges_table))
12096 return false;
12098 dw_ranges *r2 = &(*ranges_table)[i];
12099 /* Use DW_RLE_base_address{,x} if there is a next range in the
12100 range list and is guaranteed to be in the same section. */
12101 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12104 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12105 section when needed. */
12107 static void
12108 index_rnglists (void)
12110 unsigned i;
12111 dw_ranges *r;
12112 bool base = false;
12114 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12116 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12117 r->idx = rnglist_idx++;
12119 int block_num = r->num;
12120 if ((HAVE_AS_LEB128 || block_num < 0)
12121 && !have_multiple_function_sections)
12122 continue;
12123 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12124 base = false;
12125 if (block_num > 0)
12127 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12128 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12130 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12131 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12133 if (HAVE_AS_LEB128)
12135 if (!base && use_distinct_base_address_for_range (i + 1))
12137 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12138 ate_kind_label);
12139 base = true;
12141 if (base)
12142 /* If we have a base, no need for further
12143 begin_entry/end_entry, as DW_RLE_offset_pair will be
12144 used. */
12145 continue;
12146 r->begin_entry
12147 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12148 /* No need for end_entry, DW_RLE_start{,x}_length will use
12149 length as opposed to a pair of addresses. */
12151 else
12153 r->begin_entry
12154 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12155 r->end_entry
12156 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12160 /* Negative block_num stands for an index into ranges_by_label. */
12161 else if (block_num < 0)
12163 int lab_idx = - block_num - 1;
12164 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12165 const char *elabel = (*ranges_by_label)[lab_idx].end;
12167 r->begin_entry
12168 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12169 if (!HAVE_AS_LEB128)
12170 r->end_entry
12171 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12176 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12178 static bool
12179 output_rnglists (unsigned generation, bool dwo)
12181 unsigned i;
12182 dw_ranges *r;
12183 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12184 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12185 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12187 if (dwo)
12188 switch_to_section (debug_ranges_dwo_section);
12189 else
12191 switch_to_section (debug_ranges_section);
12192 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12194 /* There are up to 4 unique ranges labels per generation.
12195 See also init_sections_and_labels. */
12196 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12197 2 + 2 * dwo + generation * 6);
12198 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12199 3 + 2 * dwo + generation * 6);
12200 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12201 dw2_asm_output_data (4, 0xffffffff,
12202 "Initial length escape value indicating "
12203 "64-bit DWARF extension");
12204 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12205 "Length of Range Lists");
12206 ASM_OUTPUT_LABEL (asm_out_file, l1);
12207 output_dwarf_version ();
12208 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12209 dw2_asm_output_data (1, 0, "Segment Size");
12210 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12211 about relocation sizes and primarily care about the size of .debug*
12212 sections in linked shared libraries and executables, then
12213 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12214 into it are usually larger than just DW_FORM_sec_offset offsets
12215 into the .debug_rnglists section. */
12216 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12217 "Offset Entry Count");
12218 if (dwo)
12220 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12221 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12222 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12223 dw2_asm_output_delta (dwarf_offset_size, r->label,
12224 ranges_base_label, NULL);
12227 const char *lab = "";
12228 const char *base = NULL;
12229 bool skipping = false;
12230 bool ret = false;
12231 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12233 int block_num = r->num;
12235 if (r->label)
12237 if (dwarf_split_debug_info
12238 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12240 ret = true;
12241 skipping = true;
12242 continue;
12244 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12245 lab = r->label;
12247 if (skipping)
12249 if (block_num == 0)
12250 skipping = false;
12251 continue;
12253 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12254 base = NULL;
12255 if (block_num > 0)
12257 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12258 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12260 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12261 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12263 if (HAVE_AS_LEB128)
12265 /* If all code is in the text section, then the compilation
12266 unit base address defaults to DW_AT_low_pc, which is the
12267 base of the text section. */
12268 if (!have_multiple_function_sections)
12270 dw2_asm_output_data (1, DW_RLE_offset_pair,
12271 "DW_RLE_offset_pair (%s)", lab);
12272 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12273 "Range begin address (%s)", lab);
12274 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12275 "Range end address (%s)", lab);
12276 continue;
12278 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12280 if (dwarf_split_debug_info)
12282 dw2_asm_output_data (1, DW_RLE_base_addressx,
12283 "DW_RLE_base_addressx (%s)", lab);
12284 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12285 "Base address index (%s)",
12286 blabel);
12288 else
12290 dw2_asm_output_data (1, DW_RLE_base_address,
12291 "DW_RLE_base_address (%s)", lab);
12292 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12293 "Base address (%s)", lab);
12295 strcpy (basebuf, blabel);
12296 base = basebuf;
12298 if (base)
12300 dw2_asm_output_data (1, DW_RLE_offset_pair,
12301 "DW_RLE_offset_pair (%s)", lab);
12302 dw2_asm_output_delta_uleb128 (blabel, base,
12303 "Range begin address (%s)", lab);
12304 dw2_asm_output_delta_uleb128 (elabel, base,
12305 "Range end address (%s)", lab);
12306 continue;
12308 if (dwarf_split_debug_info)
12310 dw2_asm_output_data (1, DW_RLE_startx_length,
12311 "DW_RLE_startx_length (%s)", lab);
12312 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12313 "Range begin address index "
12314 "(%s)", blabel);
12316 else
12318 dw2_asm_output_data (1, DW_RLE_start_length,
12319 "DW_RLE_start_length (%s)", lab);
12320 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12321 "Range begin address (%s)", lab);
12323 dw2_asm_output_delta_uleb128 (elabel, blabel,
12324 "Range length (%s)", lab);
12326 else if (dwarf_split_debug_info)
12328 dw2_asm_output_data (1, DW_RLE_startx_endx,
12329 "DW_RLE_startx_endx (%s)", lab);
12330 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12331 "Range begin address index "
12332 "(%s)", blabel);
12333 dw2_asm_output_data_uleb128 (r->end_entry->index,
12334 "Range end address index "
12335 "(%s)", elabel);
12337 else
12339 dw2_asm_output_data (1, DW_RLE_start_end,
12340 "DW_RLE_start_end (%s)", lab);
12341 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12342 "Range begin address (%s)", lab);
12343 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12344 "Range end address (%s)", lab);
12348 /* Negative block_num stands for an index into ranges_by_label. */
12349 else if (block_num < 0)
12351 int lab_idx = - block_num - 1;
12352 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12353 const char *elabel = (*ranges_by_label)[lab_idx].end;
12355 if (!have_multiple_function_sections)
12356 gcc_unreachable ();
12357 if (HAVE_AS_LEB128)
12359 if (dwarf_split_debug_info)
12361 dw2_asm_output_data (1, DW_RLE_startx_length,
12362 "DW_RLE_startx_length (%s)", lab);
12363 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12364 "Range begin address index "
12365 "(%s)", blabel);
12367 else
12369 dw2_asm_output_data (1, DW_RLE_start_length,
12370 "DW_RLE_start_length (%s)", lab);
12371 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12372 "Range begin address (%s)", lab);
12374 dw2_asm_output_delta_uleb128 (elabel, blabel,
12375 "Range length (%s)", lab);
12377 else if (dwarf_split_debug_info)
12379 dw2_asm_output_data (1, DW_RLE_startx_endx,
12380 "DW_RLE_startx_endx (%s)", lab);
12381 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12382 "Range begin address index "
12383 "(%s)", blabel);
12384 dw2_asm_output_data_uleb128 (r->end_entry->index,
12385 "Range end address index "
12386 "(%s)", elabel);
12388 else
12390 dw2_asm_output_data (1, DW_RLE_start_end,
12391 "DW_RLE_start_end (%s)", lab);
12392 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12393 "Range begin address (%s)", lab);
12394 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12395 "Range end address (%s)", lab);
12398 else
12399 dw2_asm_output_data (1, DW_RLE_end_of_list,
12400 "DW_RLE_end_of_list (%s)", lab);
12402 ASM_OUTPUT_LABEL (asm_out_file, l2);
12403 return ret;
12406 /* Data structure containing information about input files. */
12407 struct file_info
12409 const char *path; /* Complete file name. */
12410 const char *fname; /* File name part. */
12411 int length; /* Length of entire string. */
12412 struct dwarf_file_data * file_idx; /* Index in input file table. */
12413 int dir_idx; /* Index in directory table. */
12416 /* Data structure containing information about directories with source
12417 files. */
12418 struct dir_info
12420 const char *path; /* Path including directory name. */
12421 int length; /* Path length. */
12422 int prefix; /* Index of directory entry which is a prefix. */
12423 int count; /* Number of files in this directory. */
12424 int dir_idx; /* Index of directory used as base. */
12427 /* Callback function for file_info comparison. We sort by looking at
12428 the directories in the path. */
12430 static int
12431 file_info_cmp (const void *p1, const void *p2)
12433 const struct file_info *const s1 = (const struct file_info *) p1;
12434 const struct file_info *const s2 = (const struct file_info *) p2;
12435 const unsigned char *cp1;
12436 const unsigned char *cp2;
12438 /* Take care of file names without directories. We need to make sure that
12439 we return consistent values to qsort since some will get confused if
12440 we return the same value when identical operands are passed in opposite
12441 orders. So if neither has a directory, return 0 and otherwise return
12442 1 or -1 depending on which one has the directory. We want the one with
12443 the directory to sort after the one without, so all no directory files
12444 are at the start (normally only the compilation unit file). */
12445 if ((s1->path == s1->fname || s2->path == s2->fname))
12446 return (s2->path == s2->fname) - (s1->path == s1->fname);
12448 cp1 = (const unsigned char *) s1->path;
12449 cp2 = (const unsigned char *) s2->path;
12451 while (1)
12453 ++cp1;
12454 ++cp2;
12455 /* Reached the end of the first path? If so, handle like above,
12456 but now we want longer directory prefixes before shorter ones. */
12457 if ((cp1 == (const unsigned char *) s1->fname)
12458 || (cp2 == (const unsigned char *) s2->fname))
12459 return ((cp1 == (const unsigned char *) s1->fname)
12460 - (cp2 == (const unsigned char *) s2->fname));
12462 /* Character of current path component the same? */
12463 else if (*cp1 != *cp2)
12464 return *cp1 - *cp2;
12468 struct file_name_acquire_data
12470 struct file_info *files;
12471 int used_files;
12472 int max_files;
12475 /* Traversal function for the hash table. */
12478 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12480 struct dwarf_file_data *d = *slot;
12481 struct file_info *fi;
12482 const char *f;
12484 gcc_assert (fnad->max_files >= d->emitted_number);
12486 if (! d->emitted_number)
12487 return 1;
12489 gcc_assert (fnad->max_files != fnad->used_files);
12491 fi = fnad->files + fnad->used_files++;
12493 f = d->filename;
12495 /* Skip all leading "./". */
12496 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12497 f += 2;
12499 /* Create a new array entry. */
12500 fi->path = f;
12501 fi->length = strlen (f);
12502 fi->file_idx = d;
12504 /* Search for the file name part. */
12505 f = strrchr (f, DIR_SEPARATOR);
12506 #if defined (DIR_SEPARATOR_2)
12508 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12510 if (g != NULL)
12512 if (f == NULL || f < g)
12513 f = g;
12516 #endif
12518 fi->fname = f == NULL ? fi->path : f + 1;
12519 return 1;
12522 /* Helper function for output_file_names. Emit a FORM encoded
12523 string STR, with assembly comment start ENTRY_KIND and
12524 index IDX */
12526 static void
12527 output_line_string (enum dwarf_form form, const char *str,
12528 const char *entry_kind, unsigned int idx)
12530 switch (form)
12532 case DW_FORM_string:
12533 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12534 break;
12535 case DW_FORM_line_strp:
12536 if (!debug_line_str_hash)
12537 debug_line_str_hash
12538 = hash_table<indirect_string_hasher>::create_ggc (10);
12540 struct indirect_string_node *node;
12541 node = find_AT_string_in_table (str, debug_line_str_hash);
12542 set_indirect_string (node);
12543 node->form = form;
12544 dw2_asm_output_offset (dwarf_offset_size, node->label,
12545 debug_line_str_section, "%s: %#x: \"%s\"",
12546 entry_kind, 0, node->str);
12547 break;
12548 default:
12549 gcc_unreachable ();
12553 /* Output the directory table and the file name table. We try to minimize
12554 the total amount of memory needed. A heuristic is used to avoid large
12555 slowdowns with many input files. */
12557 static void
12558 output_file_names (void)
12560 struct file_name_acquire_data fnad;
12561 int numfiles;
12562 struct file_info *files;
12563 struct dir_info *dirs;
12564 int *saved;
12565 int *savehere;
12566 int *backmap;
12567 int ndirs;
12568 int idx_offset;
12569 int i;
12571 if (!last_emitted_file)
12573 if (dwarf_version >= 5)
12575 const char *comp_dir = comp_dir_string ();
12576 if (comp_dir == NULL)
12577 comp_dir = "";
12578 dw2_asm_output_data (1, 1, "Directory entry format count");
12579 enum dwarf_form str_form = DW_FORM_string;
12580 if (DWARF5_USE_DEBUG_LINE_STR)
12581 str_form = DW_FORM_line_strp;
12582 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12583 dw2_asm_output_data_uleb128 (str_form, "%s",
12584 get_DW_FORM_name (str_form));
12585 dw2_asm_output_data_uleb128 (1, "Directories count");
12586 if (str_form == DW_FORM_string)
12587 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12588 else
12589 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12590 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12591 if (filename0 == NULL)
12592 filename0 = "";
12593 #ifdef VMS_DEBUGGING_INFO
12594 dw2_asm_output_data (1, 4, "File name entry format count");
12595 #else
12596 dw2_asm_output_data (1, 2, "File name entry format count");
12597 #endif
12598 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12599 dw2_asm_output_data_uleb128 (str_form, "%s",
12600 get_DW_FORM_name (str_form));
12601 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12602 "DW_LNCT_directory_index");
12603 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12604 get_DW_FORM_name (DW_FORM_data1));
12605 #ifdef VMS_DEBUGGING_INFO
12606 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12607 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12608 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12609 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12610 #endif
12611 dw2_asm_output_data_uleb128 (1, "File names count");
12613 output_line_string (str_form, filename0, "File Entry", 0);
12614 dw2_asm_output_data (1, 0, NULL);
12615 #ifdef VMS_DEBUGGING_INFO
12616 dw2_asm_output_data_uleb128 (0, NULL);
12617 dw2_asm_output_data_uleb128 (0, NULL);
12618 #endif
12620 else
12622 dw2_asm_output_data (1, 0, "End directory table");
12623 dw2_asm_output_data (1, 0, "End file name table");
12625 return;
12628 numfiles = last_emitted_file->emitted_number;
12630 /* Allocate the various arrays we need. */
12631 files = XALLOCAVEC (struct file_info, numfiles);
12632 dirs = XALLOCAVEC (struct dir_info, numfiles);
12634 fnad.files = files;
12635 fnad.used_files = 0;
12636 fnad.max_files = numfiles;
12637 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12638 gcc_assert (fnad.used_files == fnad.max_files);
12640 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12642 /* Find all the different directories used. */
12643 dirs[0].path = files[0].path;
12644 dirs[0].length = files[0].fname - files[0].path;
12645 dirs[0].prefix = -1;
12646 dirs[0].count = 1;
12647 dirs[0].dir_idx = 0;
12648 files[0].dir_idx = 0;
12649 ndirs = 1;
12651 for (i = 1; i < numfiles; i++)
12652 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12653 && memcmp (dirs[ndirs - 1].path, files[i].path,
12654 dirs[ndirs - 1].length) == 0)
12656 /* Same directory as last entry. */
12657 files[i].dir_idx = ndirs - 1;
12658 ++dirs[ndirs - 1].count;
12660 else
12662 int j;
12664 /* This is a new directory. */
12665 dirs[ndirs].path = files[i].path;
12666 dirs[ndirs].length = files[i].fname - files[i].path;
12667 dirs[ndirs].count = 1;
12668 dirs[ndirs].dir_idx = ndirs;
12669 files[i].dir_idx = ndirs;
12671 /* Search for a prefix. */
12672 dirs[ndirs].prefix = -1;
12673 for (j = 0; j < ndirs; j++)
12674 if (dirs[j].length < dirs[ndirs].length
12675 && dirs[j].length > 1
12676 && (dirs[ndirs].prefix == -1
12677 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12678 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12679 dirs[ndirs].prefix = j;
12681 ++ndirs;
12684 /* Now to the actual work. We have to find a subset of the directories which
12685 allow expressing the file name using references to the directory table
12686 with the least amount of characters. We do not do an exhaustive search
12687 where we would have to check out every combination of every single
12688 possible prefix. Instead we use a heuristic which provides nearly optimal
12689 results in most cases and never is much off. */
12690 saved = XALLOCAVEC (int, ndirs);
12691 savehere = XALLOCAVEC (int, ndirs);
12693 memset (saved, '\0', ndirs * sizeof (saved[0]));
12694 for (i = 0; i < ndirs; i++)
12696 int j;
12697 int total;
12699 /* We can always save some space for the current directory. But this
12700 does not mean it will be enough to justify adding the directory. */
12701 savehere[i] = dirs[i].length;
12702 total = (savehere[i] - saved[i]) * dirs[i].count;
12704 for (j = i + 1; j < ndirs; j++)
12706 savehere[j] = 0;
12707 if (saved[j] < dirs[i].length)
12709 /* Determine whether the dirs[i] path is a prefix of the
12710 dirs[j] path. */
12711 int k;
12713 k = dirs[j].prefix;
12714 while (k != -1 && k != (int) i)
12715 k = dirs[k].prefix;
12717 if (k == (int) i)
12719 /* Yes it is. We can possibly save some memory by
12720 writing the filenames in dirs[j] relative to
12721 dirs[i]. */
12722 savehere[j] = dirs[i].length;
12723 total += (savehere[j] - saved[j]) * dirs[j].count;
12728 /* Check whether we can save enough to justify adding the dirs[i]
12729 directory. */
12730 if (total > dirs[i].length + 1)
12732 /* It's worthwhile adding. */
12733 for (j = i; j < ndirs; j++)
12734 if (savehere[j] > 0)
12736 /* Remember how much we saved for this directory so far. */
12737 saved[j] = savehere[j];
12739 /* Remember the prefix directory. */
12740 dirs[j].dir_idx = i;
12745 /* Emit the directory name table. */
12746 idx_offset = dirs[0].length > 0 ? 1 : 0;
12747 enum dwarf_form str_form = DW_FORM_string;
12748 enum dwarf_form idx_form = DW_FORM_udata;
12749 if (dwarf_version >= 5)
12751 const char *comp_dir = comp_dir_string ();
12752 if (comp_dir == NULL)
12753 comp_dir = "";
12754 dw2_asm_output_data (1, 1, "Directory entry format count");
12755 if (DWARF5_USE_DEBUG_LINE_STR)
12756 str_form = DW_FORM_line_strp;
12757 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12758 dw2_asm_output_data_uleb128 (str_form, "%s",
12759 get_DW_FORM_name (str_form));
12760 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12761 if (str_form == DW_FORM_string)
12763 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12764 for (i = 1 - idx_offset; i < ndirs; i++)
12765 dw2_asm_output_nstring (dirs[i].path,
12766 dirs[i].length
12767 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12768 "Directory Entry: %#x", i + idx_offset);
12770 else
12772 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12773 for (i = 1 - idx_offset; i < ndirs; i++)
12775 const char *str
12776 = ggc_alloc_string (dirs[i].path,
12777 dirs[i].length
12778 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12779 output_line_string (str_form, str, "Directory Entry",
12780 (unsigned) i + idx_offset);
12784 else
12786 for (i = 1 - idx_offset; i < ndirs; i++)
12787 dw2_asm_output_nstring (dirs[i].path,
12788 dirs[i].length
12789 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12790 "Directory Entry: %#x", i + idx_offset);
12792 dw2_asm_output_data (1, 0, "End directory table");
12795 /* We have to emit them in the order of emitted_number since that's
12796 used in the debug info generation. To do this efficiently we
12797 generate a back-mapping of the indices first. */
12798 backmap = XALLOCAVEC (int, numfiles);
12799 for (i = 0; i < numfiles; i++)
12800 backmap[files[i].file_idx->emitted_number - 1] = i;
12802 if (dwarf_version >= 5)
12804 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12805 if (filename0 == NULL)
12806 filename0 = "";
12807 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12808 DW_FORM_data2. Choose one based on the number of directories
12809 and how much space would they occupy in each encoding.
12810 If we have at most 256 directories, all indexes fit into
12811 a single byte, so DW_FORM_data1 is most compact (if there
12812 are at most 128 directories, DW_FORM_udata would be as
12813 compact as that, but not shorter and slower to decode). */
12814 if (ndirs + idx_offset <= 256)
12815 idx_form = DW_FORM_data1;
12816 /* If there are more than 65536 directories, we have to use
12817 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12818 Otherwise, compute what space would occupy if all the indexes
12819 used DW_FORM_udata - sum - and compare that to how large would
12820 be DW_FORM_data2 encoding, and pick the more efficient one. */
12821 else if (ndirs + idx_offset <= 65536)
12823 unsigned HOST_WIDE_INT sum = 1;
12824 for (i = 0; i < numfiles; i++)
12826 int file_idx = backmap[i];
12827 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12828 sum += size_of_uleb128 (dir_idx);
12830 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12831 idx_form = DW_FORM_data2;
12833 #ifdef VMS_DEBUGGING_INFO
12834 dw2_asm_output_data (1, 4, "File name entry format count");
12835 #else
12836 dw2_asm_output_data (1, 2, "File name entry format count");
12837 #endif
12838 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12839 dw2_asm_output_data_uleb128 (str_form, "%s",
12840 get_DW_FORM_name (str_form));
12841 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12842 "DW_LNCT_directory_index");
12843 dw2_asm_output_data_uleb128 (idx_form, "%s",
12844 get_DW_FORM_name (idx_form));
12845 #ifdef VMS_DEBUGGING_INFO
12846 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12847 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12848 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12849 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12850 #endif
12851 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12853 output_line_string (str_form, filename0, "File Entry", 0);
12855 /* Include directory index. */
12856 if (idx_form != DW_FORM_udata)
12857 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12858 0, NULL);
12859 else
12860 dw2_asm_output_data_uleb128 (0, NULL);
12862 #ifdef VMS_DEBUGGING_INFO
12863 dw2_asm_output_data_uleb128 (0, NULL);
12864 dw2_asm_output_data_uleb128 (0, NULL);
12865 #endif
12868 /* Now write all the file names. */
12869 for (i = 0; i < numfiles; i++)
12871 int file_idx = backmap[i];
12872 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12874 #ifdef VMS_DEBUGGING_INFO
12875 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12877 /* Setting these fields can lead to debugger miscomparisons,
12878 but VMS Debug requires them to be set correctly. */
12880 int ver;
12881 long long cdt;
12882 long siz;
12883 int maxfilelen = (strlen (files[file_idx].path)
12884 + dirs[dir_idx].length
12885 + MAX_VMS_VERSION_LEN + 1);
12886 char *filebuf = XALLOCAVEC (char, maxfilelen);
12888 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12889 snprintf (filebuf, maxfilelen, "%s;%d",
12890 files[file_idx].path + dirs[dir_idx].length, ver);
12892 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12894 /* Include directory index. */
12895 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12896 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12897 dir_idx + idx_offset, NULL);
12898 else
12899 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12901 /* Modification time. */
12902 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12903 &cdt, 0, 0, 0) == 0)
12904 ? cdt : 0, NULL);
12906 /* File length in bytes. */
12907 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12908 0, &siz, 0, 0) == 0)
12909 ? siz : 0, NULL);
12910 #else
12911 output_line_string (str_form,
12912 files[file_idx].path + dirs[dir_idx].length,
12913 "File Entry", (unsigned) i + 1);
12915 /* Include directory index. */
12916 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12917 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12918 dir_idx + idx_offset, NULL);
12919 else
12920 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12922 if (dwarf_version >= 5)
12923 continue;
12925 /* Modification time. */
12926 dw2_asm_output_data_uleb128 (0, NULL);
12928 /* File length in bytes. */
12929 dw2_asm_output_data_uleb128 (0, NULL);
12930 #endif /* VMS_DEBUGGING_INFO */
12933 if (dwarf_version < 5)
12934 dw2_asm_output_data (1, 0, "End file name table");
12938 /* Output one line number table into the .debug_line section. */
12940 static void
12941 output_one_line_info_table (dw_line_info_table *table)
12943 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12944 unsigned int current_line = 1;
12945 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12946 dw_line_info_entry *ent, *prev_addr = NULL;
12947 size_t i;
12948 unsigned int view;
12950 view = 0;
12952 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12954 switch (ent->opcode)
12956 case LI_set_address:
12957 /* ??? Unfortunately, we have little choice here currently, and
12958 must always use the most general form. GCC does not know the
12959 address delta itself, so we can't use DW_LNS_advance_pc. Many
12960 ports do have length attributes which will give an upper bound
12961 on the address range. We could perhaps use length attributes
12962 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12963 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12965 view = 0;
12967 /* This can handle any delta. This takes
12968 4+DWARF2_ADDR_SIZE bytes. */
12969 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12970 debug_variable_location_views
12971 ? ", reset view to 0" : "");
12972 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12973 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12974 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12976 prev_addr = ent;
12977 break;
12979 case LI_adv_address:
12981 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12982 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12983 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12985 view++;
12987 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12988 dw2_asm_output_delta (2, line_label, prev_label,
12989 "from %s to %s", prev_label, line_label);
12991 prev_addr = ent;
12992 break;
12995 case LI_set_line:
12996 if (ent->val == current_line)
12998 /* We still need to start a new row, so output a copy insn. */
12999 dw2_asm_output_data (1, DW_LNS_copy,
13000 "copy line %u", current_line);
13002 else
13004 int line_offset = ent->val - current_line;
13005 int line_delta = line_offset - DWARF_LINE_BASE;
13007 current_line = ent->val;
13008 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
13010 /* This can handle deltas from -10 to 234, using the current
13011 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
13012 This takes 1 byte. */
13013 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
13014 "line %u", current_line);
13016 else
13018 /* This can handle any delta. This takes at least 4 bytes,
13019 depending on the value being encoded. */
13020 dw2_asm_output_data (1, DW_LNS_advance_line,
13021 "advance to line %u", current_line);
13022 dw2_asm_output_data_sleb128 (line_offset, NULL);
13023 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13026 break;
13028 case LI_set_file:
13029 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13030 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13031 break;
13033 case LI_set_column:
13034 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13035 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13036 break;
13038 case LI_negate_stmt:
13039 current_is_stmt = !current_is_stmt;
13040 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13041 "is_stmt %d", current_is_stmt);
13042 break;
13044 case LI_set_prologue_end:
13045 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13046 "set prologue end");
13047 break;
13049 case LI_set_epilogue_begin:
13050 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13051 "set epilogue begin");
13052 break;
13054 case LI_set_discriminator:
13055 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13056 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13057 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13058 dw2_asm_output_data_uleb128 (ent->val, NULL);
13059 break;
13063 /* Emit debug info for the address of the end of the table. */
13064 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13065 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13066 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13067 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13069 dw2_asm_output_data (1, 0, "end sequence");
13070 dw2_asm_output_data_uleb128 (1, NULL);
13071 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13074 static unsigned int output_line_info_generation;
13076 /* Output the source line number correspondence information. This
13077 information goes into the .debug_line section. */
13079 static void
13080 output_line_info (bool prologue_only)
13082 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13083 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13084 bool saw_one = false;
13085 int opc;
13087 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13088 output_line_info_generation);
13089 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13090 output_line_info_generation);
13091 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13092 output_line_info_generation);
13093 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13094 output_line_info_generation++);
13096 if (!XCOFF_DEBUGGING_INFO)
13098 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13099 dw2_asm_output_data (4, 0xffffffff,
13100 "Initial length escape value indicating 64-bit DWARF extension");
13101 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13102 "Length of Source Line Info");
13105 ASM_OUTPUT_LABEL (asm_out_file, l1);
13107 output_dwarf_version ();
13108 if (dwarf_version >= 5)
13110 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13111 dw2_asm_output_data (1, 0, "Segment Size");
13113 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13114 ASM_OUTPUT_LABEL (asm_out_file, p1);
13116 /* Define the architecture-dependent minimum instruction length (in bytes).
13117 In this implementation of DWARF, this field is used for information
13118 purposes only. Since GCC generates assembly language, we have no
13119 a priori knowledge of how many instruction bytes are generated for each
13120 source line, and therefore can use only the DW_LNE_set_address and
13121 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13122 this as '1', which is "correct enough" for all architectures,
13123 and don't let the target override. */
13124 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13126 if (dwarf_version >= 4)
13127 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13128 "Maximum Operations Per Instruction");
13129 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13130 "Default is_stmt_start flag");
13131 dw2_asm_output_data (1, DWARF_LINE_BASE,
13132 "Line Base Value (Special Opcodes)");
13133 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13134 "Line Range Value (Special Opcodes)");
13135 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13136 "Special Opcode Base");
13138 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13140 int n_op_args;
13141 switch (opc)
13143 case DW_LNS_advance_pc:
13144 case DW_LNS_advance_line:
13145 case DW_LNS_set_file:
13146 case DW_LNS_set_column:
13147 case DW_LNS_fixed_advance_pc:
13148 case DW_LNS_set_isa:
13149 n_op_args = 1;
13150 break;
13151 default:
13152 n_op_args = 0;
13153 break;
13156 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13157 opc, n_op_args);
13160 /* Write out the information about the files we use. */
13161 output_file_names ();
13162 ASM_OUTPUT_LABEL (asm_out_file, p2);
13163 if (prologue_only)
13165 /* Output the marker for the end of the line number info. */
13166 ASM_OUTPUT_LABEL (asm_out_file, l2);
13167 return;
13170 if (separate_line_info)
13172 dw_line_info_table *table;
13173 size_t i;
13175 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13176 if (table->in_use)
13178 output_one_line_info_table (table);
13179 saw_one = true;
13182 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13184 output_one_line_info_table (cold_text_section_line_info);
13185 saw_one = true;
13188 /* ??? Some Darwin linkers crash on a .debug_line section with no
13189 sequences. Further, merely a DW_LNE_end_sequence entry is not
13190 sufficient -- the address column must also be initialized.
13191 Make sure to output at least one set_address/end_sequence pair,
13192 choosing .text since that section is always present. */
13193 if (text_section_line_info->in_use || !saw_one)
13194 output_one_line_info_table (text_section_line_info);
13196 /* Output the marker for the end of the line number info. */
13197 ASM_OUTPUT_LABEL (asm_out_file, l2);
13200 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13202 static inline bool
13203 need_endianity_attribute_p (bool reverse)
13205 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13208 /* Given a pointer to a tree node for some base type, return a pointer to
13209 a DIE that describes the given type. REVERSE is true if the type is
13210 to be interpreted in the reverse storage order wrt the target order.
13212 This routine must only be called for GCC type nodes that correspond to
13213 Dwarf base (fundamental) types. */
13215 dw_die_ref
13216 base_type_die (tree type, bool reverse)
13218 dw_die_ref base_type_result;
13219 enum dwarf_type encoding;
13220 bool fpt_used = false;
13221 struct fixed_point_type_info fpt_info;
13222 tree type_bias = NULL_TREE;
13224 /* If this is a subtype that should not be emitted as a subrange type,
13225 use the base type. See subrange_type_for_debug_p. */
13226 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13227 type = TREE_TYPE (type);
13229 switch (TREE_CODE (type))
13231 case INTEGER_TYPE:
13232 if ((dwarf_version >= 4 || !dwarf_strict)
13233 && TYPE_NAME (type)
13234 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13235 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13236 && DECL_NAME (TYPE_NAME (type)))
13238 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13239 if (strcmp (name, "char16_t") == 0
13240 || strcmp (name, "char8_t") == 0
13241 || strcmp (name, "char32_t") == 0)
13243 encoding = DW_ATE_UTF;
13244 break;
13247 if ((dwarf_version >= 3 || !dwarf_strict)
13248 && lang_hooks.types.get_fixed_point_type_info)
13250 memset (&fpt_info, 0, sizeof (fpt_info));
13251 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13253 fpt_used = true;
13254 encoding = ((TYPE_UNSIGNED (type))
13255 ? DW_ATE_unsigned_fixed
13256 : DW_ATE_signed_fixed);
13257 break;
13260 if (TYPE_STRING_FLAG (type))
13262 if ((dwarf_version >= 4 || !dwarf_strict)
13263 && is_rust ()
13264 && int_size_in_bytes (type) == 4)
13265 encoding = DW_ATE_UTF;
13266 else if (TYPE_UNSIGNED (type))
13267 encoding = DW_ATE_unsigned_char;
13268 else
13269 encoding = DW_ATE_signed_char;
13271 else if (TYPE_UNSIGNED (type))
13272 encoding = DW_ATE_unsigned;
13273 else
13274 encoding = DW_ATE_signed;
13276 if (!dwarf_strict
13277 && lang_hooks.types.get_type_bias)
13278 type_bias = lang_hooks.types.get_type_bias (type);
13279 break;
13281 case REAL_TYPE:
13282 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13284 if (dwarf_version >= 3 || !dwarf_strict)
13285 encoding = DW_ATE_decimal_float;
13286 else
13287 encoding = DW_ATE_lo_user;
13289 else
13290 encoding = DW_ATE_float;
13291 break;
13293 case FIXED_POINT_TYPE:
13294 if (!(dwarf_version >= 3 || !dwarf_strict))
13295 encoding = DW_ATE_lo_user;
13296 else if (TYPE_UNSIGNED (type))
13297 encoding = DW_ATE_unsigned_fixed;
13298 else
13299 encoding = DW_ATE_signed_fixed;
13300 break;
13302 /* Dwarf2 doesn't know anything about complex ints, so use
13303 a user defined type for it. */
13304 case COMPLEX_TYPE:
13305 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
13306 encoding = DW_ATE_complex_float;
13307 else
13308 encoding = DW_ATE_lo_user;
13309 break;
13311 case BOOLEAN_TYPE:
13312 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13313 encoding = DW_ATE_boolean;
13314 break;
13316 case BITINT_TYPE:
13317 /* C23 _BitInt(N). */
13318 if (TYPE_UNSIGNED (type))
13319 encoding = DW_ATE_unsigned;
13320 else
13321 encoding = DW_ATE_signed;
13322 break;
13324 default:
13325 /* No other TREE_CODEs are Dwarf fundamental types. */
13326 gcc_unreachable ();
13329 base_type_result = new_die_raw (DW_TAG_base_type);
13331 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13332 int_size_in_bytes (type));
13333 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13334 if (TREE_CODE (type) == BITINT_TYPE)
13335 add_AT_unsigned (base_type_result, DW_AT_bit_size, TYPE_PRECISION (type));
13337 if (need_endianity_attribute_p (reverse))
13338 add_AT_unsigned (base_type_result, DW_AT_endianity,
13339 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13341 add_alignment_attribute (base_type_result, type);
13343 if (fpt_used)
13345 switch (fpt_info.scale_factor_kind)
13347 case fixed_point_scale_factor_binary:
13348 add_AT_int (base_type_result, DW_AT_binary_scale,
13349 fpt_info.scale_factor.binary);
13350 break;
13352 case fixed_point_scale_factor_decimal:
13353 add_AT_int (base_type_result, DW_AT_decimal_scale,
13354 fpt_info.scale_factor.decimal);
13355 break;
13357 case fixed_point_scale_factor_arbitrary:
13358 /* Arbitrary scale factors cannot be described in standard DWARF. */
13359 if (!dwarf_strict)
13361 /* Describe the scale factor as a rational constant. */
13362 const dw_die_ref scale_factor
13363 = new_die (DW_TAG_constant, comp_unit_die (), type);
13365 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13366 fpt_info.scale_factor.arbitrary.numerator,
13367 dw_scalar_form_constant, NULL);
13368 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13369 fpt_info.scale_factor.arbitrary.denominator,
13370 dw_scalar_form_constant, NULL);
13372 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13374 break;
13376 default:
13377 gcc_unreachable ();
13381 if (type_bias)
13382 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13383 dw_scalar_form_constant
13384 | dw_scalar_form_exprloc
13385 | dw_scalar_form_reference,
13386 NULL);
13388 return base_type_result;
13391 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13392 named 'auto' in its type: return true for it, false otherwise. */
13394 static inline bool
13395 is_cxx_auto (tree type)
13397 if (is_cxx ())
13399 tree name = TYPE_IDENTIFIER (type);
13400 if (name == get_identifier ("auto")
13401 || name == get_identifier ("decltype(auto)"))
13402 return true;
13404 return false;
13407 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if the
13408 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13410 static inline bool
13411 is_base_type (tree type)
13413 switch (TREE_CODE (type))
13415 case INTEGER_TYPE:
13416 case REAL_TYPE:
13417 case FIXED_POINT_TYPE:
13418 case COMPLEX_TYPE:
13419 case BOOLEAN_TYPE:
13420 case BITINT_TYPE:
13421 return true;
13423 case VOID_TYPE:
13424 case OPAQUE_TYPE:
13425 case ARRAY_TYPE:
13426 case RECORD_TYPE:
13427 case UNION_TYPE:
13428 case QUAL_UNION_TYPE:
13429 case ENUMERAL_TYPE:
13430 case FUNCTION_TYPE:
13431 case METHOD_TYPE:
13432 case POINTER_TYPE:
13433 case REFERENCE_TYPE:
13434 case NULLPTR_TYPE:
13435 case OFFSET_TYPE:
13436 case LANG_TYPE:
13437 case VECTOR_TYPE:
13438 return false;
13440 default:
13441 if (is_cxx_auto (type))
13442 return false;
13443 gcc_unreachable ();
13447 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13448 node, return the size in bits for the type if it is a constant, or else
13449 return the alignment for the type if the type's size is not constant, or
13450 else return BITS_PER_WORD if the type actually turns out to be an
13451 ERROR_MARK node. */
13453 static inline unsigned HOST_WIDE_INT
13454 simple_type_size_in_bits (const_tree type)
13456 if (TREE_CODE (type) == ERROR_MARK)
13457 return BITS_PER_WORD;
13458 else if (TYPE_SIZE (type) == NULL_TREE)
13459 return 0;
13460 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13461 return tree_to_uhwi (TYPE_SIZE (type));
13462 else
13463 return TYPE_ALIGN (type);
13466 /* Similarly, but return an offset_int instead of UHWI. */
13468 static inline offset_int
13469 offset_int_type_size_in_bits (const_tree type)
13471 if (TREE_CODE (type) == ERROR_MARK)
13472 return BITS_PER_WORD;
13473 else if (TYPE_SIZE (type) == NULL_TREE)
13474 return 0;
13475 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13476 return wi::to_offset (TYPE_SIZE (type));
13477 else
13478 return TYPE_ALIGN (type);
13481 /* Given a pointer to a tree node for a subrange type, return a pointer
13482 to a DIE that describes the given type. */
13484 static dw_die_ref
13485 subrange_type_die (tree type, tree low, tree high, tree bias,
13486 dw_die_ref context_die)
13488 dw_die_ref subrange_die;
13489 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13491 if (context_die == NULL)
13492 context_die = comp_unit_die ();
13494 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13496 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13498 /* The size of the subrange type and its base type do not match,
13499 so we need to generate a size attribute for the subrange type. */
13500 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13503 add_alignment_attribute (subrange_die, type);
13505 if (low)
13506 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13507 if (high)
13508 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13509 if (bias && !dwarf_strict)
13510 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13511 dw_scalar_form_constant
13512 | dw_scalar_form_exprloc
13513 | dw_scalar_form_reference,
13514 NULL);
13516 return subrange_die;
13519 /* Returns the (const and/or volatile) cv_qualifiers associated with
13520 the decl node. This will normally be augmented with the
13521 cv_qualifiers of the underlying type in add_type_attribute. */
13523 static int
13524 decl_quals (const_tree decl)
13526 return ((TREE_READONLY (decl)
13527 /* The C++ front-end correctly marks reference-typed
13528 variables as readonly, but from a language (and debug
13529 info) standpoint they are not const-qualified. */
13530 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13531 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13532 | (TREE_THIS_VOLATILE (decl)
13533 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13536 /* Determine the TYPE whose qualifiers match the largest strict subset
13537 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13538 qualifiers outside QUAL_MASK. */
13540 static int
13541 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13543 tree t;
13544 int best_rank = 0, best_qual = 0, max_rank;
13546 type_quals &= qual_mask;
13547 max_rank = popcount_hwi (type_quals) - 1;
13549 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13550 t = TYPE_NEXT_VARIANT (t))
13552 int q = TYPE_QUALS (t) & qual_mask;
13554 if ((q & type_quals) == q && q != type_quals
13555 && check_base_type (t, type))
13557 int rank = popcount_hwi (q);
13559 if (rank > best_rank)
13561 best_rank = rank;
13562 best_qual = q;
13567 return best_qual;
13570 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13571 static const dwarf_qual_info_t dwarf_qual_info[] =
13573 { TYPE_QUAL_CONST, DW_TAG_const_type },
13574 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13575 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13576 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13578 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13580 /* If DIE is a qualified DIE of some base DIE with the same parent,
13581 return the base DIE, otherwise return NULL. Set MASK to the
13582 qualifiers added compared to the returned DIE. */
13584 static dw_die_ref
13585 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13587 unsigned int i;
13588 for (i = 0; i < dwarf_qual_info_size; i++)
13589 if (die->die_tag == dwarf_qual_info[i].t)
13590 break;
13591 if (i == dwarf_qual_info_size)
13592 return NULL;
13593 if (vec_safe_length (die->die_attr) != 1)
13594 return NULL;
13595 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13596 if (type == NULL || type->die_parent != die->die_parent)
13597 return NULL;
13598 *mask |= dwarf_qual_info[i].q;
13599 if (depth)
13601 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13602 if (ret)
13603 return ret;
13605 return type;
13608 /* If TYPE is long double or complex long double that
13609 should be emitted as artificial typedef to _Float128 or
13610 complex _Float128, return the type it should be emitted as.
13611 This is done in case the target already supports 16-byte
13612 composite floating point type (ibm_extended_format). */
13614 static tree
13615 long_double_as_float128 (tree type)
13617 if (type != long_double_type_node
13618 && type != complex_long_double_type_node)
13619 return NULL_TREE;
13621 machine_mode mode, fmode;
13622 if (TREE_CODE (type) == COMPLEX_TYPE)
13623 mode = TYPE_MODE (TREE_TYPE (type));
13624 else
13625 mode = TYPE_MODE (type);
13626 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13627 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13628 if (known_eq (GET_MODE_SIZE (fmode), 16)
13629 && MODE_COMPOSITE_P (fmode))
13631 if (type == long_double_type_node)
13633 if (float128_type_node
13634 && (TYPE_MODE (float128_type_node)
13635 == TYPE_MODE (type)))
13636 return float128_type_node;
13637 return NULL_TREE;
13639 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13640 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13641 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13642 == TYPE_MODE (type)))
13643 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13646 return NULL_TREE;
13649 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13650 entry that chains the modifiers specified by CV_QUALS in front of the
13651 given type. REVERSE is true if the type is to be interpreted in the
13652 reverse storage order wrt the target order. */
13654 static dw_die_ref
13655 modified_type_die (tree type, int cv_quals, bool reverse,
13656 dw_die_ref context_die)
13658 enum tree_code code = TREE_CODE (type);
13659 dw_die_ref mod_type_die;
13660 dw_die_ref sub_die = NULL;
13661 tree item_type = NULL;
13662 tree qualified_type;
13663 tree name, low, high;
13664 dw_die_ref mod_scope;
13665 struct array_descr_info info;
13666 /* Only these cv-qualifiers are currently handled. */
13667 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13668 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13669 ENCODE_QUAL_ADDR_SPACE(~0U));
13670 /* DW_AT_endianity is specified only for base types in the standard. */
13671 const bool reverse_type
13672 = need_endianity_attribute_p (reverse)
13673 && (is_base_type (type)
13674 || (TREE_CODE (type) == ENUMERAL_TYPE && !dwarf_strict));
13676 if (code == ERROR_MARK)
13677 return NULL;
13679 if (lang_hooks.types.get_debug_type)
13681 tree debug_type = lang_hooks.types.get_debug_type (type);
13683 if (debug_type != NULL_TREE && debug_type != type)
13684 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13687 cv_quals &= cv_qual_mask;
13689 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13690 tag modifier (and not an attribute) old consumers won't be able
13691 to handle it. */
13692 if (dwarf_version < 3)
13693 cv_quals &= ~TYPE_QUAL_RESTRICT;
13695 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13696 if (dwarf_version < 5)
13697 cv_quals &= ~TYPE_QUAL_ATOMIC;
13699 /* See if we already have the appropriately qualified variant of
13700 this type. */
13701 qualified_type = get_qualified_type (type, cv_quals);
13703 if (qualified_type == sizetype)
13705 /* Try not to expose the internal sizetype type's name. */
13706 if (TYPE_NAME (qualified_type)
13707 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13709 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13711 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13712 && (TYPE_PRECISION (t)
13713 == TYPE_PRECISION (qualified_type))
13714 && (TYPE_UNSIGNED (t)
13715 == TYPE_UNSIGNED (qualified_type)));
13716 qualified_type = t;
13718 else if (qualified_type == sizetype
13719 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13720 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13721 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13722 qualified_type = size_type_node;
13723 if (type == sizetype)
13724 type = qualified_type;
13727 /* If we do, then we can just use its DIE, if it exists. */
13728 if (qualified_type)
13730 mod_type_die = lookup_type_die (qualified_type);
13732 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13733 dealt with specially: the DIE with the attribute, if it exists, is
13734 placed immediately after the regular DIE for the same type. */
13735 if (mod_type_die
13736 && (!reverse_type
13737 || ((mod_type_die = mod_type_die->die_sib) != NULL
13738 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13739 return mod_type_die;
13742 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13744 /* Handle C typedef types. */
13745 if (name
13746 && TREE_CODE (name) == TYPE_DECL
13747 && DECL_ORIGINAL_TYPE (name)
13748 && !DECL_ARTIFICIAL (name))
13750 tree dtype = TREE_TYPE (name);
13752 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13753 if (qualified_type == dtype && !reverse_type)
13755 tree origin = decl_ultimate_origin (name);
13757 /* Typedef variants that have an abstract origin don't get their own
13758 type DIE (see gen_typedef_die), so fall back on the ultimate
13759 abstract origin instead. */
13760 if (origin != NULL && origin != name)
13761 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13762 context_die);
13764 /* For a named type, use the typedef. */
13765 gen_type_die (qualified_type, context_die);
13766 return lookup_type_die (qualified_type);
13768 else
13770 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13771 dquals &= cv_qual_mask;
13772 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13773 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13774 /* cv-unqualified version of named type. Just use
13775 the unnamed type to which it refers. */
13776 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13777 reverse, context_die);
13778 /* Else cv-qualified version of named type; fall through. */
13782 mod_scope = scope_die_for (type, context_die);
13784 if (cv_quals)
13786 int sub_quals = 0, first_quals = 0;
13787 unsigned i;
13788 dw_die_ref first = NULL, last = NULL;
13790 /* Determine a lesser qualified type that most closely matches
13791 this one. Then generate DW_TAG_* entries for the remaining
13792 qualifiers. */
13793 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13794 cv_qual_mask);
13795 if (sub_quals && use_debug_types)
13797 bool needed = false;
13798 /* If emitting type units, make sure the order of qualifiers
13799 is canonical. Thus, start from unqualified type if
13800 an earlier qualifier is missing in sub_quals, but some later
13801 one is present there. */
13802 for (i = 0; i < dwarf_qual_info_size; i++)
13803 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13804 needed = true;
13805 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13807 sub_quals = 0;
13808 break;
13811 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13812 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13814 /* As not all intermediate qualified DIEs have corresponding
13815 tree types, ensure that qualified DIEs in the same scope
13816 as their DW_AT_type are emitted after their DW_AT_type,
13817 only with other qualified DIEs for the same type possibly
13818 in between them. Determine the range of such qualified
13819 DIEs now (first being the base type, last being corresponding
13820 last qualified DIE for it). */
13821 unsigned int count = 0;
13822 first = qualified_die_p (mod_type_die, &first_quals,
13823 dwarf_qual_info_size);
13824 if (first == NULL)
13825 first = mod_type_die;
13826 gcc_assert ((first_quals & ~sub_quals) == 0);
13827 for (count = 0, last = first;
13828 count < (1U << dwarf_qual_info_size);
13829 count++, last = last->die_sib)
13831 int quals = 0;
13832 if (last == mod_scope->die_child)
13833 break;
13834 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13835 != first)
13836 break;
13840 for (i = 0; i < dwarf_qual_info_size; i++)
13841 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13843 dw_die_ref d;
13844 if (first && first != last)
13846 for (d = first->die_sib; ; d = d->die_sib)
13848 int quals = 0;
13849 qualified_die_p (d, &quals, dwarf_qual_info_size);
13850 if (quals == (first_quals | dwarf_qual_info[i].q))
13851 break;
13852 if (d == last)
13854 d = NULL;
13855 break;
13858 if (d)
13860 mod_type_die = d;
13861 continue;
13864 if (first)
13866 d = new_die_raw (dwarf_qual_info[i].t);
13867 add_child_die_after (mod_scope, d, last);
13868 last = d;
13870 else
13871 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13872 if (mod_type_die)
13873 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13874 mod_type_die = d;
13875 first_quals |= dwarf_qual_info[i].q;
13878 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13880 dwarf_tag tag = DW_TAG_pointer_type;
13881 if (code == REFERENCE_TYPE)
13883 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13884 tag = DW_TAG_rvalue_reference_type;
13885 else
13886 tag = DW_TAG_reference_type;
13888 mod_type_die = new_die (tag, mod_scope, type);
13890 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13891 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13892 add_alignment_attribute (mod_type_die, type);
13893 item_type = TREE_TYPE (type);
13895 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13896 if (!ADDR_SPACE_GENERIC_P (as))
13898 int action = targetm.addr_space.debug (as);
13899 if (action >= 0)
13901 /* Positive values indicate an address_class. */
13902 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13904 else
13906 /* Negative values indicate an (inverted) segment base reg. */
13907 dw_loc_descr_ref d
13908 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13909 add_AT_loc (mod_type_die, DW_AT_segment, d);
13913 else if (code == ARRAY_TYPE
13914 || (lang_hooks.types.get_array_descr_info
13915 && lang_hooks.types.get_array_descr_info (type, &info)))
13917 gen_type_die (type, context_die);
13918 return lookup_type_die (type);
13920 else if (code == INTEGER_TYPE
13921 && TREE_TYPE (type) != NULL_TREE
13922 && subrange_type_for_debug_p (type, &low, &high))
13924 tree bias = NULL_TREE;
13925 if (lang_hooks.types.get_type_bias)
13926 bias = lang_hooks.types.get_type_bias (type);
13927 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13928 item_type = TREE_TYPE (type);
13930 else if (is_base_type (type))
13932 /* If a target supports long double as different floating point
13933 modes with the same 16-byte size, use normal DW_TAG_base_type
13934 only for the composite (ibm_extended_real_format) type and
13935 for the other for the time being emit instead a "_Float128"
13936 or "complex _Float128" DW_TAG_base_type and a "long double"
13937 or "complex long double" typedef to it. */
13938 if (tree other_type = long_double_as_float128 (type))
13940 dw_die_ref other_die;
13941 if (TYPE_NAME (other_type))
13942 other_die
13943 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13944 context_die);
13945 else
13947 other_die = base_type_die (type, reverse);
13948 add_child_die (comp_unit_die (), other_die);
13949 add_name_attribute (other_die,
13950 TREE_CODE (type) == COMPLEX_TYPE
13951 ? "complex _Float128" : "_Float128");
13953 mod_type_die = new_die_raw (DW_TAG_typedef);
13954 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13956 else
13957 mod_type_die = base_type_die (type, reverse);
13959 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13960 if (reverse_type)
13962 dw_die_ref after_die
13963 = modified_type_die (type, cv_quals, false, context_die);
13964 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13966 else
13967 add_child_die (comp_unit_die (), mod_type_die);
13969 add_pubtype (type, mod_type_die);
13971 else
13973 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13974 if (reverse_type)
13976 dw_die_ref after_die
13977 = modified_type_die (type, cv_quals, false, context_die);
13978 gen_type_die (type, context_die, true);
13979 gcc_assert (after_die->die_sib
13980 && get_AT_unsigned (after_die->die_sib, DW_AT_endianity));
13981 return after_die->die_sib;
13984 gen_type_die (type, context_die);
13986 /* We have to get the type_main_variant here (and pass that to the
13987 `lookup_type_die' routine) because the ..._TYPE node we have
13988 might simply be a *copy* of some original type node (where the
13989 copy was created to help us keep track of typedef names) and
13990 that copy might have a different TYPE_UID from the original
13991 ..._TYPE node. */
13992 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13994 /* For function/method types, can't just use type_main_variant here,
13995 because that can have different ref-qualifiers for C++,
13996 but try to canonicalize. */
13997 tree main = TYPE_MAIN_VARIANT (type);
13998 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13999 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
14000 && check_base_type (t, main)
14001 && check_lang_type (t, type))
14002 return lookup_type_die (t);
14003 return lookup_type_die (type);
14005 /* Vectors have the debugging information in the type,
14006 not the main variant. */
14007 else if (code == VECTOR_TYPE)
14008 return lookup_type_die (type);
14009 else
14010 return lookup_type_die (type_main_variant (type));
14013 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
14014 don't output a DW_TAG_typedef, since there isn't one in the
14015 user's program; just attach a DW_AT_name to the type.
14016 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
14017 if the base type already has the same name. */
14018 if (name
14019 && ((TREE_CODE (name) != TYPE_DECL
14020 && (qualified_type == TYPE_MAIN_VARIANT (type)
14021 || (cv_quals == TYPE_UNQUALIFIED)))
14022 || (TREE_CODE (name) == TYPE_DECL
14023 && TREE_TYPE (name) == qualified_type
14024 && DECL_NAME (name))))
14026 if (TREE_CODE (name) == TYPE_DECL)
14027 /* Could just call add_name_and_src_coords_attributes here,
14028 but since this is a builtin type it doesn't have any
14029 useful source coordinates anyway. */
14030 name = DECL_NAME (name);
14031 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
14033 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
14035 if (TREE_CODE (type) == BITINT_TYPE)
14037 char name_buf[sizeof ("unsigned _BitInt(2147483647)")];
14038 snprintf (name_buf, sizeof (name_buf),
14039 "%s_BitInt(%d)", TYPE_UNSIGNED (type) ? "unsigned " : "",
14040 TYPE_PRECISION (type));
14041 add_name_attribute (mod_type_die, name_buf);
14043 else
14045 /* This probably indicates a bug. */
14046 name = TYPE_IDENTIFIER (type);
14047 add_name_attribute (mod_type_die,
14048 name
14049 ? IDENTIFIER_POINTER (name) : "__unknown__");
14053 if (qualified_type && !reverse_type)
14054 equate_type_number_to_die (qualified_type, mod_type_die);
14056 if (item_type)
14057 /* We must do this after the equate_type_number_to_die call, in case
14058 this is a recursive type. This ensures that the modified_type_die
14059 recursion will terminate even if the type is recursive. Recursive
14060 types are possible in Ada. */
14061 sub_die = modified_type_die (item_type,
14062 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14063 reverse,
14064 context_die);
14066 if (sub_die != NULL)
14067 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14069 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14070 if (TYPE_ARTIFICIAL (type))
14071 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14073 return mod_type_die;
14076 /* Generate DIEs for the generic parameters of T.
14077 T must be either a generic type or a generic function.
14078 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14080 static void
14081 gen_generic_params_dies (tree t)
14083 tree parms, args;
14084 int parms_num, i;
14085 dw_die_ref die = NULL;
14086 int non_default;
14088 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14089 return;
14091 if (TYPE_P (t))
14092 die = lookup_type_die (t);
14093 else if (DECL_P (t))
14094 die = lookup_decl_die (t);
14096 gcc_assert (die);
14098 parms = lang_hooks.get_innermost_generic_parms (t);
14099 if (!parms)
14100 /* T has no generic parameter. It means T is neither a generic type
14101 or function. End of story. */
14102 return;
14104 parms_num = TREE_VEC_LENGTH (parms);
14105 args = lang_hooks.get_innermost_generic_args (t);
14106 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14107 non_default = int_cst_value (TREE_CHAIN (args));
14108 else
14109 non_default = TREE_VEC_LENGTH (args);
14110 for (i = 0; i < parms_num; i++)
14112 tree parm, arg, arg_pack_elems;
14113 dw_die_ref parm_die;
14115 parm = TREE_VEC_ELT (parms, i);
14116 arg = TREE_VEC_ELT (args, i);
14117 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14118 gcc_assert (parm && TREE_VALUE (parm) && arg);
14120 if (parm && TREE_VALUE (parm) && arg)
14122 /* If PARM represents a template parameter pack,
14123 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14124 by DW_TAG_template_*_parameter DIEs for the argument
14125 pack elements of ARG. Note that ARG would then be
14126 an argument pack. */
14127 if (arg_pack_elems)
14128 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14129 arg_pack_elems,
14130 die);
14131 else
14132 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14133 true /* emit name */, die);
14134 if (i >= non_default)
14135 add_AT_flag (parm_die, DW_AT_default_value, 1);
14140 /* Create and return a DIE for PARM which should be
14141 the representation of a generic type parameter.
14142 For instance, in the C++ front end, PARM would be a template parameter.
14143 ARG is the argument to PARM.
14144 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14145 name of the PARM.
14146 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14147 as a child node. */
14149 static dw_die_ref
14150 generic_parameter_die (tree parm, tree arg,
14151 bool emit_name_p,
14152 dw_die_ref parent_die)
14154 dw_die_ref tmpl_die = NULL;
14155 const char *name = NULL;
14157 /* C++20 accepts class literals as template parameters, and var
14158 decls with initializers represent them. The VAR_DECLs would be
14159 rejected, but we can take the DECL_INITIAL constructor and
14160 attempt to expand it. */
14161 if (arg && VAR_P (arg))
14162 arg = DECL_INITIAL (arg);
14164 if (!parm || !DECL_NAME (parm) || !arg)
14165 return NULL;
14167 /* We support non-type generic parameters and arguments,
14168 type generic parameters and arguments, as well as
14169 generic generic parameters (a.k.a. template template parameters in C++)
14170 and arguments. */
14171 if (TREE_CODE (parm) == PARM_DECL)
14172 /* PARM is a nontype generic parameter */
14173 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14174 else if (TREE_CODE (parm) == TYPE_DECL)
14175 /* PARM is a type generic parameter. */
14176 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14177 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14178 /* PARM is a generic generic parameter.
14179 Its DIE is a GNU extension. It shall have a
14180 DW_AT_name attribute to represent the name of the template template
14181 parameter, and a DW_AT_GNU_template_name attribute to represent the
14182 name of the template template argument. */
14183 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14184 parent_die, parm);
14185 else
14186 gcc_unreachable ();
14188 if (tmpl_die)
14190 tree tmpl_type;
14192 /* If PARM is a generic parameter pack, it means we are
14193 emitting debug info for a template argument pack element.
14194 In other terms, ARG is a template argument pack element.
14195 In that case, we don't emit any DW_AT_name attribute for
14196 the die. */
14197 if (emit_name_p)
14199 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14200 gcc_assert (name);
14201 add_AT_string (tmpl_die, DW_AT_name, name);
14204 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14206 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14207 TMPL_DIE should have a child DW_AT_type attribute that is set
14208 to the type of the argument to PARM, which is ARG.
14209 If PARM is a type generic parameter, TMPL_DIE should have a
14210 child DW_AT_type that is set to ARG. */
14211 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14212 add_type_attribute (tmpl_die, tmpl_type,
14213 (TREE_THIS_VOLATILE (tmpl_type)
14214 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14215 false, parent_die);
14217 else
14219 /* So TMPL_DIE is a DIE representing a
14220 a generic generic template parameter, a.k.a template template
14221 parameter in C++ and arg is a template. */
14223 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14224 to the name of the argument. */
14225 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14226 if (name)
14227 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14230 if (TREE_CODE (parm) == PARM_DECL)
14231 /* So PARM is a non-type generic parameter.
14232 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14233 attribute of TMPL_DIE which value represents the value
14234 of ARG.
14235 We must be careful here:
14236 The value of ARG might reference some function decls.
14237 We might currently be emitting debug info for a generic
14238 type and types are emitted before function decls, we don't
14239 know if the function decls referenced by ARG will actually be
14240 emitted after cgraph computations.
14241 So must defer the generation of the DW_AT_const_value to
14242 after cgraph is ready. */
14243 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14246 return tmpl_die;
14249 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14250 PARM_PACK must be a template parameter pack. The returned DIE
14251 will be child DIE of PARENT_DIE. */
14253 static dw_die_ref
14254 template_parameter_pack_die (tree parm_pack,
14255 tree parm_pack_args,
14256 dw_die_ref parent_die)
14258 dw_die_ref die;
14259 int j;
14261 gcc_assert (parent_die && parm_pack);
14263 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14264 add_name_and_src_coords_attributes (die, parm_pack);
14265 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14266 generic_parameter_die (parm_pack,
14267 TREE_VEC_ELT (parm_pack_args, j),
14268 false /* Don't emit DW_AT_name */,
14269 die);
14270 return die;
14273 /* Return the debugger register number described by a given RTL node. */
14275 static unsigned int
14276 debugger_reg_number (const_rtx rtl)
14278 unsigned regno = REGNO (rtl);
14280 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14282 #ifdef LEAF_REG_REMAP
14283 if (crtl->uses_only_leaf_regs)
14285 int leaf_reg = LEAF_REG_REMAP (regno);
14286 if (leaf_reg != -1)
14287 regno = (unsigned) leaf_reg;
14289 #endif
14291 regno = DEBUGGER_REGNO (regno);
14292 gcc_assert (regno != INVALID_REGNUM);
14293 return regno;
14296 /* Optionally add a DW_OP_piece term to a location description expression.
14297 DW_OP_piece is only added if the location description expression already
14298 doesn't end with DW_OP_piece. */
14300 static void
14301 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14303 dw_loc_descr_ref loc;
14305 if (*list_head != NULL)
14307 /* Find the end of the chain. */
14308 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14311 if (loc->dw_loc_opc != DW_OP_piece)
14312 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14316 /* Return a location descriptor that designates a machine register or
14317 zero if there is none. */
14319 static dw_loc_descr_ref
14320 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14322 rtx regs;
14324 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14325 return 0;
14327 /* We only use "frame base" when we're sure we're talking about the
14328 post-prologue local stack frame. We do this by *not* running
14329 register elimination until this point, and recognizing the special
14330 argument pointer and soft frame pointer rtx's.
14331 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14332 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14333 && (ira_use_lra_p
14334 ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX)
14335 : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl)
14337 dw_loc_descr_ref result = NULL;
14339 if (dwarf_version >= 4 || !dwarf_strict)
14341 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14342 initialized);
14343 if (result)
14344 add_loc_descr (&result,
14345 new_loc_descr (DW_OP_stack_value, 0, 0));
14347 return result;
14350 regs = targetm.dwarf_register_span (rtl);
14352 if (REG_NREGS (rtl) > 1 || regs)
14353 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14354 else
14356 unsigned int debugger_regnum = debugger_reg_number (rtl);
14357 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14358 return 0;
14359 return one_reg_loc_descriptor (debugger_regnum, initialized);
14363 /* Return a location descriptor that designates a machine register for
14364 a given hard register number. */
14366 static dw_loc_descr_ref
14367 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14369 dw_loc_descr_ref reg_loc_descr;
14371 if (regno <= 31)
14372 reg_loc_descr
14373 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14374 else
14375 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14377 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14378 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14380 return reg_loc_descr;
14383 /* Given an RTL of a register, return a location descriptor that
14384 designates a value that spans more than one register. */
14386 static dw_loc_descr_ref
14387 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14388 enum var_init_status initialized)
14390 int size, i;
14391 dw_loc_descr_ref loc_result = NULL;
14393 /* Simple, contiguous registers. */
14394 if (regs == NULL_RTX)
14396 unsigned reg = REGNO (rtl);
14397 int nregs;
14399 #ifdef LEAF_REG_REMAP
14400 if (crtl->uses_only_leaf_regs)
14402 int leaf_reg = LEAF_REG_REMAP (reg);
14403 if (leaf_reg != -1)
14404 reg = (unsigned) leaf_reg;
14406 #endif
14408 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14409 nregs = REG_NREGS (rtl);
14411 /* At present we only track constant-sized pieces. */
14412 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14413 return NULL;
14414 size /= nregs;
14416 loc_result = NULL;
14417 while (nregs--)
14419 dw_loc_descr_ref t;
14421 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14422 VAR_INIT_STATUS_INITIALIZED);
14423 add_loc_descr (&loc_result, t);
14424 add_loc_descr_op_piece (&loc_result, size);
14425 ++reg;
14427 return loc_result;
14430 /* Now onto stupid register sets in non contiguous locations. */
14432 gcc_assert (GET_CODE (regs) == PARALLEL);
14434 /* At present we only track constant-sized pieces. */
14435 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14436 return NULL;
14437 loc_result = NULL;
14439 for (i = 0; i < XVECLEN (regs, 0); ++i)
14441 dw_loc_descr_ref t;
14443 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14444 VAR_INIT_STATUS_INITIALIZED);
14445 add_loc_descr (&loc_result, t);
14446 add_loc_descr_op_piece (&loc_result, size);
14449 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14450 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14451 return loc_result;
14454 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14456 /* Return a location descriptor that designates a constant i,
14457 as a compound operation from constant (i >> shift), constant shift
14458 and DW_OP_shl. */
14460 static dw_loc_descr_ref
14461 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14463 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14464 add_loc_descr (&ret, int_loc_descriptor (shift));
14465 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14466 return ret;
14469 /* Return a location descriptor that designates constant POLY_I. */
14471 static dw_loc_descr_ref
14472 int_loc_descriptor (poly_int64 poly_i)
14474 enum dwarf_location_atom op;
14476 HOST_WIDE_INT i;
14477 if (!poly_i.is_constant (&i))
14479 /* Create location descriptions for the non-constant part and
14480 add any constant offset at the end. */
14481 dw_loc_descr_ref ret = NULL;
14482 HOST_WIDE_INT constant = poly_i.coeffs[0];
14483 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14485 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14486 if (coeff != 0)
14488 dw_loc_descr_ref start = ret;
14489 unsigned int factor;
14490 int bias;
14491 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14492 (j, &factor, &bias);
14494 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14495 add COEFF * (REGNO / FACTOR) now and subtract
14496 COEFF * BIAS from the final constant part. */
14497 constant -= coeff * bias;
14498 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14499 if (coeff % factor == 0)
14500 coeff /= factor;
14501 else
14503 int amount = exact_log2 (factor);
14504 gcc_assert (amount >= 0);
14505 add_loc_descr (&ret, int_loc_descriptor (amount));
14506 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14508 if (coeff != 1)
14510 add_loc_descr (&ret, int_loc_descriptor (coeff));
14511 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14513 if (start)
14514 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14517 loc_descr_plus_const (&ret, constant);
14518 return ret;
14521 /* Pick the smallest representation of a constant, rather than just
14522 defaulting to the LEB encoding. */
14523 if (i >= 0)
14525 int clz = clz_hwi (i);
14526 int ctz = ctz_hwi (i);
14527 if (i <= 31)
14528 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14529 else if (i <= 0xff)
14530 op = DW_OP_const1u;
14531 else if (i <= 0xffff)
14532 op = DW_OP_const2u;
14533 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14534 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14535 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14536 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14537 while DW_OP_const4u is 5 bytes. */
14538 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14539 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14540 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14541 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14542 while DW_OP_const4u is 5 bytes. */
14543 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14545 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14546 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14547 <= 4)
14549 /* As i >= 2**31, the double cast above will yield a negative number.
14550 Since wrapping is defined in DWARF expressions we can output big
14551 positive integers as small negative ones, regardless of the size
14552 of host wide ints.
14554 Here, since the evaluator will handle 32-bit values and since i >=
14555 2**31, we know it's going to be interpreted as a negative literal:
14556 store it this way if we can do better than 5 bytes this way. */
14557 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14559 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14560 op = DW_OP_const4u;
14562 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14563 least 6 bytes: see if we can do better before falling back to it. */
14564 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14565 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14566 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14567 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14568 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14569 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14570 >= HOST_BITS_PER_WIDE_INT)
14571 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14572 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14573 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14574 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14575 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14576 && size_of_uleb128 (i) > 6)
14577 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14578 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14579 else
14580 op = DW_OP_constu;
14582 else
14584 if (i >= -0x80)
14585 op = DW_OP_const1s;
14586 else if (i >= -0x8000)
14587 op = DW_OP_const2s;
14588 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14590 if (size_of_int_loc_descriptor (i) < 5)
14592 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14593 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14594 return ret;
14596 op = DW_OP_const4s;
14598 else
14600 if (size_of_int_loc_descriptor (i)
14601 < (unsigned long) 1 + size_of_sleb128 (i))
14603 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14604 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14605 return ret;
14607 op = DW_OP_consts;
14611 return new_loc_descr (op, i, 0);
14614 /* Likewise, for unsigned constants. */
14616 static dw_loc_descr_ref
14617 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14619 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14620 const unsigned HOST_WIDE_INT max_uint
14621 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14623 /* If possible, use the clever signed constants handling. */
14624 if (i <= max_int)
14625 return int_loc_descriptor ((HOST_WIDE_INT) i);
14627 /* Here, we are left with positive numbers that cannot be represented as
14628 HOST_WIDE_INT, i.e.:
14629 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14631 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14632 whereas may be better to output a negative integer: thanks to integer
14633 wrapping, we know that:
14634 x = x - 2 ** DWARF2_ADDR_SIZE
14635 = x - 2 * (max (HOST_WIDE_INT) + 1)
14636 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14637 small negative integers. Let's try that in cases it will clearly improve
14638 the encoding: there is no gain turning DW_OP_const4u into
14639 DW_OP_const4s. */
14640 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14641 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14642 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14644 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14646 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14647 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14648 const HOST_WIDE_INT second_shift
14649 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14651 /* So we finally have:
14652 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14653 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14654 return int_loc_descriptor (second_shift);
14657 /* Last chance: fallback to a simple constant operation. */
14658 return new_loc_descr
14659 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14660 ? DW_OP_const4u
14661 : DW_OP_const8u,
14662 i, 0);
14665 /* Generate and return a location description that computes the unsigned
14666 comparison of the two stack top entries (a OP b where b is the top-most
14667 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14668 LE_EXPR, GT_EXPR or GE_EXPR. */
14670 static dw_loc_descr_ref
14671 uint_comparison_loc_list (enum tree_code kind)
14673 enum dwarf_location_atom op, flip_op;
14674 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14676 switch (kind)
14678 case LT_EXPR:
14679 op = DW_OP_lt;
14680 break;
14681 case LE_EXPR:
14682 op = DW_OP_le;
14683 break;
14684 case GT_EXPR:
14685 op = DW_OP_gt;
14686 break;
14687 case GE_EXPR:
14688 op = DW_OP_ge;
14689 break;
14690 default:
14691 gcc_unreachable ();
14694 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14695 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14697 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14698 possible to perform unsigned comparisons: we just have to distinguish
14699 three cases:
14701 1. when a and b have the same sign (as signed integers); then we should
14702 return: a OP(signed) b;
14704 2. when a is a negative signed integer while b is a positive one, then a
14705 is a greater unsigned integer than b; likewise when a and b's roles
14706 are flipped.
14708 So first, compare the sign of the two operands. */
14709 ret = new_loc_descr (DW_OP_over, 0, 0);
14710 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14711 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14712 /* If they have different signs (i.e. they have different sign bits), then
14713 the stack top value has now the sign bit set and thus it's smaller than
14714 zero. */
14715 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14716 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14717 add_loc_descr (&ret, bra_node);
14719 /* We are in case 1. At this point, we know both operands have the same
14720 sign, to it's safe to use the built-in signed comparison. */
14721 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14722 add_loc_descr (&ret, jmp_node);
14724 /* We are in case 2. Here, we know both operands do not have the same sign,
14725 so we have to flip the signed comparison. */
14726 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14727 tmp = new_loc_descr (flip_op, 0, 0);
14728 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14729 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14730 add_loc_descr (&ret, tmp);
14732 /* This dummy operation is necessary to make the two branches join. */
14733 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14734 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14735 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14736 add_loc_descr (&ret, tmp);
14738 return ret;
14741 /* Likewise, but takes the location description lists (might be destructive on
14742 them). Return NULL if either is NULL or if concatenation fails. */
14744 static dw_loc_list_ref
14745 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14746 enum tree_code kind)
14748 if (left == NULL || right == NULL)
14749 return NULL;
14751 add_loc_list (&left, right);
14752 if (left == NULL)
14753 return NULL;
14755 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14756 return left;
14759 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14760 without actually allocating it. */
14762 static unsigned long
14763 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14765 return size_of_int_loc_descriptor (i >> shift)
14766 + size_of_int_loc_descriptor (shift)
14767 + 1;
14770 /* Return size_of_locs (int_loc_descriptor (i)) without
14771 actually allocating it. */
14773 static unsigned long
14774 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14776 unsigned long s;
14778 if (i >= 0)
14780 int clz, ctz;
14781 if (i <= 31)
14782 return 1;
14783 else if (i <= 0xff)
14784 return 2;
14785 else if (i <= 0xffff)
14786 return 3;
14787 clz = clz_hwi (i);
14788 ctz = ctz_hwi (i);
14789 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14790 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14791 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14792 - clz - 5);
14793 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14794 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14795 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14796 - clz - 8);
14797 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14798 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14799 <= 4)
14800 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14801 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14802 return 5;
14803 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14804 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14805 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14806 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14807 - clz - 8);
14808 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14809 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14810 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14811 - clz - 16);
14812 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14813 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14814 && s > 6)
14815 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14816 - clz - 32);
14817 else
14818 return 1 + s;
14820 else
14822 if (i >= -0x80)
14823 return 2;
14824 else if (i >= -0x8000)
14825 return 3;
14826 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14828 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14830 s = size_of_int_loc_descriptor (-i) + 1;
14831 if (s < 5)
14832 return s;
14834 return 5;
14836 else
14838 unsigned long r = 1 + size_of_sleb128 (i);
14839 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14841 s = size_of_int_loc_descriptor (-i) + 1;
14842 if (s < r)
14843 return s;
14845 return r;
14850 /* Return loc description representing "address" of integer value.
14851 This can appear only as toplevel expression. */
14853 static dw_loc_descr_ref
14854 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14856 int litsize;
14857 dw_loc_descr_ref loc_result = NULL;
14859 if (!(dwarf_version >= 4 || !dwarf_strict))
14860 return NULL;
14862 litsize = size_of_int_loc_descriptor (i);
14863 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14864 is more compact. For DW_OP_stack_value we need:
14865 litsize + 1 (DW_OP_stack_value)
14866 and for DW_OP_implicit_value:
14867 1 (DW_OP_implicit_value) + 1 (length) + size. */
14868 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14870 loc_result = int_loc_descriptor (i);
14871 add_loc_descr (&loc_result,
14872 new_loc_descr (DW_OP_stack_value, 0, 0));
14873 return loc_result;
14876 loc_result = new_loc_descr (DW_OP_implicit_value,
14877 size, 0);
14878 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14879 loc_result->dw_loc_oprnd2.v.val_int = i;
14880 return loc_result;
14883 /* Return a location descriptor that designates a base+offset location. */
14885 static dw_loc_descr_ref
14886 based_loc_descr (rtx reg, poly_int64 offset,
14887 enum var_init_status initialized)
14889 unsigned int regno;
14890 dw_loc_descr_ref result;
14891 dw_fde_ref fde = cfun->fde;
14893 /* We only use "frame base" when we're sure we're talking about the
14894 post-prologue local stack frame. We do this by *not* running
14895 register elimination until this point, and recognizing the special
14896 argument pointer and soft frame pointer rtx's. */
14897 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14899 rtx elim = (ira_use_lra_p
14900 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14901 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14903 if (elim != reg)
14905 /* Allow hard frame pointer here even if frame pointer
14906 isn't used since hard frame pointer is encoded with
14907 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14908 not hard frame pointer directly. */
14909 elim = strip_offset_and_add (elim, &offset);
14910 gcc_assert (elim == hard_frame_pointer_rtx
14911 || elim == stack_pointer_rtx);
14913 /* If drap register is used to align stack, use frame
14914 pointer + offset to access stack variables. If stack
14915 is aligned without drap, use stack pointer + offset to
14916 access stack variables. */
14917 if (crtl->stack_realign_tried
14918 && reg == frame_pointer_rtx)
14920 int base_reg
14921 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14922 ? HARD_FRAME_POINTER_REGNUM
14923 : REGNO (elim));
14924 return new_reg_loc_descr (base_reg, offset);
14927 gcc_assert (frame_pointer_fb_offset_valid);
14928 offset += frame_pointer_fb_offset;
14929 HOST_WIDE_INT const_offset;
14930 if (offset.is_constant (&const_offset))
14931 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14932 else
14934 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14935 loc_descr_plus_const (&ret, offset);
14936 return ret;
14941 regno = REGNO (reg);
14942 #ifdef LEAF_REG_REMAP
14943 if (crtl->uses_only_leaf_regs)
14945 int leaf_reg = LEAF_REG_REMAP (regno);
14946 if (leaf_reg != -1)
14947 regno = (unsigned) leaf_reg;
14949 #endif
14950 regno = DWARF_FRAME_REGNUM (regno);
14952 HOST_WIDE_INT const_offset;
14953 if (!optimize && fde
14954 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14955 && offset.is_constant (&const_offset))
14957 /* Use cfa+offset to represent the location of arguments passed
14958 on the stack when drap is used to align stack.
14959 Only do this when not optimizing, for optimized code var-tracking
14960 is supposed to track where the arguments live and the register
14961 used as vdrap or drap in some spot might be used for something
14962 else in other part of the routine. */
14963 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14966 result = new_reg_loc_descr (regno, offset);
14968 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14969 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14971 return result;
14974 /* Return true if this RTL expression describes a base+offset calculation. */
14976 static inline bool
14977 is_based_loc (const_rtx rtl)
14979 return (GET_CODE (rtl) == PLUS
14980 && ((REG_P (XEXP (rtl, 0))
14981 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14982 && CONST_INT_P (XEXP (rtl, 1)))));
14985 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14986 failed. */
14988 static dw_loc_descr_ref
14989 tls_mem_loc_descriptor (rtx mem)
14991 tree base;
14992 dw_loc_descr_ref loc_result;
14994 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14995 return NULL;
14997 base = get_base_address (MEM_EXPR (mem));
14998 if (base == NULL
14999 || !VAR_P (base)
15000 || !DECL_THREAD_LOCAL_P (base))
15001 return NULL;
15003 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
15004 if (loc_result == NULL)
15005 return NULL;
15007 if (maybe_ne (MEM_OFFSET (mem), 0))
15008 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
15010 return loc_result;
15013 /* Output debug info about reason why we failed to expand expression as dwarf
15014 expression. */
15016 static void
15017 expansion_failed (tree expr, rtx rtl, char const *reason)
15019 if (dump_file && (dump_flags & TDF_DETAILS))
15021 fprintf (dump_file, "Failed to expand as dwarf: ");
15022 if (expr)
15023 print_generic_expr (dump_file, expr, dump_flags);
15024 if (rtl)
15026 fprintf (dump_file, "\n");
15027 print_rtl (dump_file, rtl);
15029 fprintf (dump_file, "\nReason: %s\n", reason);
15033 /* Helper function for const_ok_for_output. */
15035 static bool
15036 const_ok_for_output_1 (rtx rtl)
15038 if (targetm.const_not_ok_for_debug_p (rtl))
15040 if (GET_CODE (rtl) != UNSPEC)
15042 expansion_failed (NULL_TREE, rtl,
15043 "Expression rejected for debug by the backend.\n");
15044 return false;
15047 /* If delegitimize_address couldn't do anything with the UNSPEC, and
15048 the target hook doesn't explicitly allow it in debug info, assume
15049 we can't express it in the debug info. */
15050 /* Don't complain about TLS UNSPECs, those are just too hard to
15051 delegitimize. Note this could be a non-decl SYMBOL_REF such as
15052 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
15053 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
15054 if (flag_checking
15055 && (XVECLEN (rtl, 0) == 0
15056 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15057 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15058 inform (current_function_decl
15059 ? DECL_SOURCE_LOCATION (current_function_decl)
15060 : UNKNOWN_LOCATION,
15061 #if NUM_UNSPEC_VALUES > 0
15062 "non-delegitimized UNSPEC %s (%d) found in variable location",
15063 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15064 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15065 #else
15066 "non-delegitimized UNSPEC %d found in variable location",
15067 #endif
15068 XINT (rtl, 1));
15069 expansion_failed (NULL_TREE, rtl,
15070 "UNSPEC hasn't been delegitimized.\n");
15071 return false;
15074 if (CONST_POLY_INT_P (rtl))
15075 return false;
15077 /* FIXME: Refer to PR60655. It is possible for simplification
15078 of rtl expressions in var tracking to produce such expressions.
15079 We should really identify / validate expressions
15080 enclosed in CONST that can be handled by assemblers on various
15081 targets and only handle legitimate cases here. */
15082 switch (GET_CODE (rtl))
15084 case SYMBOL_REF:
15085 break;
15086 case NOT:
15087 case NEG:
15088 return false;
15089 case PLUS:
15091 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15092 operands. */
15093 subrtx_var_iterator::array_type array;
15094 bool first = false;
15095 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15096 if (SYMBOL_REF_P (*iter)
15097 || LABEL_P (*iter)
15098 || GET_CODE (*iter) == UNSPEC)
15100 first = true;
15101 break;
15103 if (!first)
15104 return true;
15105 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15106 if (SYMBOL_REF_P (*iter)
15107 || LABEL_P (*iter)
15108 || GET_CODE (*iter) == UNSPEC)
15109 return false;
15110 return true;
15112 case MINUS:
15114 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15115 appear in the second operand of MINUS. */
15116 subrtx_var_iterator::array_type array;
15117 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15118 if (SYMBOL_REF_P (*iter)
15119 || LABEL_P (*iter)
15120 || GET_CODE (*iter) == UNSPEC)
15121 return false;
15122 return true;
15124 default:
15125 return true;
15128 if (CONSTANT_POOL_ADDRESS_P (rtl))
15130 bool marked;
15131 get_pool_constant_mark (rtl, &marked);
15132 /* If all references to this pool constant were optimized away,
15133 it was not output and thus we can't represent it. */
15134 if (!marked)
15136 expansion_failed (NULL_TREE, rtl,
15137 "Constant was removed from constant pool.\n");
15138 return false;
15142 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15143 return false;
15145 /* Avoid references to external symbols in debug info, on several targets
15146 the linker might even refuse to link when linking a shared library,
15147 and in many other cases the relocations for .debug_info/.debug_loc are
15148 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15149 to be defined within the same shared library or executable are fine. */
15150 if (SYMBOL_REF_EXTERNAL_P (rtl))
15152 tree decl = SYMBOL_REF_DECL (rtl);
15154 if (decl == NULL || !targetm.binds_local_p (decl))
15156 expansion_failed (NULL_TREE, rtl,
15157 "Symbol not defined in current TU.\n");
15158 return false;
15162 return true;
15165 /* Return true if constant RTL can be emitted in DW_OP_addr or
15166 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15167 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15169 static bool
15170 const_ok_for_output (rtx rtl)
15172 if (GET_CODE (rtl) == SYMBOL_REF)
15173 return const_ok_for_output_1 (rtl);
15175 if (GET_CODE (rtl) == CONST)
15177 subrtx_var_iterator::array_type array;
15178 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15179 if (!const_ok_for_output_1 (*iter))
15180 return false;
15181 return true;
15184 return true;
15187 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15188 if possible, NULL otherwise. */
15190 static dw_die_ref
15191 base_type_for_mode (machine_mode mode, bool unsignedp)
15193 dw_die_ref type_die;
15194 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15196 if (type == NULL)
15197 return NULL;
15198 switch (TREE_CODE (type))
15200 case INTEGER_TYPE:
15201 case REAL_TYPE:
15202 break;
15203 default:
15204 return NULL;
15206 type_die = lookup_type_die (type);
15207 if (!type_die)
15208 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15209 comp_unit_die ());
15210 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15211 return NULL;
15212 return type_die;
15215 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15216 type matching MODE, or, if MODE is narrower than or as wide as
15217 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15218 possible. */
15220 static dw_loc_descr_ref
15221 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15223 machine_mode outer_mode = mode;
15224 dw_die_ref type_die;
15225 dw_loc_descr_ref cvt;
15227 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15229 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15230 return op;
15232 type_die = base_type_for_mode (outer_mode, 1);
15233 if (type_die == NULL)
15234 return NULL;
15235 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15236 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15237 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15238 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15239 add_loc_descr (&op, cvt);
15240 return op;
15243 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15245 static dw_loc_descr_ref
15246 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15247 dw_loc_descr_ref op1)
15249 dw_loc_descr_ref ret = op0;
15250 add_loc_descr (&ret, op1);
15251 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15252 if (STORE_FLAG_VALUE != 1)
15254 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15255 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15257 return ret;
15260 /* Subroutine of scompare_loc_descriptor for the case in which we're
15261 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15262 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15264 static dw_loc_descr_ref
15265 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15266 scalar_int_mode op_mode,
15267 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15269 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15270 dw_loc_descr_ref cvt;
15272 if (type_die == NULL)
15273 return NULL;
15274 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15275 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15276 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15277 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15278 add_loc_descr (&op0, cvt);
15279 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15280 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15281 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15282 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15283 add_loc_descr (&op1, cvt);
15284 return compare_loc_descriptor (op, op0, op1);
15287 /* Subroutine of scompare_loc_descriptor for the case in which we're
15288 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15289 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15291 static dw_loc_descr_ref
15292 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15293 scalar_int_mode op_mode,
15294 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15296 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15297 /* For eq/ne, if the operands are known to be zero-extended,
15298 there is no need to do the fancy shifting up. */
15299 if (op == DW_OP_eq || op == DW_OP_ne)
15301 dw_loc_descr_ref last0, last1;
15302 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15304 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15306 /* deref_size zero extends, and for constants we can check
15307 whether they are zero extended or not. */
15308 if (((last0->dw_loc_opc == DW_OP_deref_size
15309 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15310 || (CONST_INT_P (XEXP (rtl, 0))
15311 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15312 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15313 && ((last1->dw_loc_opc == DW_OP_deref_size
15314 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15315 || (CONST_INT_P (XEXP (rtl, 1))
15316 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15317 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15318 return compare_loc_descriptor (op, op0, op1);
15320 /* EQ/NE comparison against constant in narrower type than
15321 DWARF2_ADDR_SIZE can be performed either as
15322 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15323 DW_OP_{eq,ne}
15325 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15326 DW_OP_{eq,ne}. Pick whatever is shorter. */
15327 if (CONST_INT_P (XEXP (rtl, 1))
15328 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15329 && (size_of_int_loc_descriptor (shift) + 1
15330 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15331 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15332 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15333 & GET_MODE_MASK (op_mode))))
15335 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15336 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15337 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15338 & GET_MODE_MASK (op_mode));
15339 return compare_loc_descriptor (op, op0, op1);
15342 add_loc_descr (&op0, int_loc_descriptor (shift));
15343 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15344 if (CONST_INT_P (XEXP (rtl, 1)))
15345 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15346 else
15348 add_loc_descr (&op1, int_loc_descriptor (shift));
15349 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15351 return compare_loc_descriptor (op, op0, op1);
15354 /* Return location descriptor for signed comparison OP RTL. */
15356 static dw_loc_descr_ref
15357 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15358 machine_mode mem_mode)
15360 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15361 dw_loc_descr_ref op0, op1;
15363 if (op_mode == VOIDmode)
15364 op_mode = GET_MODE (XEXP (rtl, 1));
15365 if (op_mode == VOIDmode)
15366 return NULL;
15368 scalar_int_mode int_op_mode;
15369 if (dwarf_strict
15370 && dwarf_version < 5
15371 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15372 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15373 return NULL;
15375 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15376 VAR_INIT_STATUS_INITIALIZED);
15377 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15378 VAR_INIT_STATUS_INITIALIZED);
15380 if (op0 == NULL || op1 == NULL)
15381 return NULL;
15383 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15385 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15386 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15388 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15389 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15391 return compare_loc_descriptor (op, op0, op1);
15394 /* Return location descriptor for unsigned comparison OP RTL. */
15396 static dw_loc_descr_ref
15397 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15398 machine_mode mem_mode)
15400 dw_loc_descr_ref op0, op1;
15402 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15403 if (test_op_mode == VOIDmode)
15404 test_op_mode = GET_MODE (XEXP (rtl, 1));
15406 scalar_int_mode op_mode;
15407 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15408 return NULL;
15410 if (dwarf_strict
15411 && dwarf_version < 5
15412 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15413 return NULL;
15415 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15416 VAR_INIT_STATUS_INITIALIZED);
15417 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15418 VAR_INIT_STATUS_INITIALIZED);
15420 if (op0 == NULL || op1 == NULL)
15421 return NULL;
15423 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15425 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15426 dw_loc_descr_ref last0, last1;
15427 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15429 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15431 if (CONST_INT_P (XEXP (rtl, 0)))
15432 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15433 /* deref_size zero extends, so no need to mask it again. */
15434 else if (last0->dw_loc_opc != DW_OP_deref_size
15435 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15437 add_loc_descr (&op0, int_loc_descriptor (mask));
15438 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15440 if (CONST_INT_P (XEXP (rtl, 1)))
15441 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15442 /* deref_size zero extends, so no need to mask it again. */
15443 else if (last1->dw_loc_opc != DW_OP_deref_size
15444 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15446 add_loc_descr (&op1, int_loc_descriptor (mask));
15447 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15450 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15452 HOST_WIDE_INT bias = 1;
15453 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15454 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15455 if (CONST_INT_P (XEXP (rtl, 1)))
15456 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15457 + INTVAL (XEXP (rtl, 1)));
15458 else
15459 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15460 bias, 0));
15462 return compare_loc_descriptor (op, op0, op1);
15465 /* Return location descriptor for {U,S}{MIN,MAX}. */
15467 static dw_loc_descr_ref
15468 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15469 machine_mode mem_mode)
15471 enum dwarf_location_atom op;
15472 dw_loc_descr_ref op0, op1, ret;
15473 dw_loc_descr_ref bra_node, drop_node;
15475 scalar_int_mode int_mode;
15476 if (dwarf_strict
15477 && dwarf_version < 5
15478 && (!is_a <scalar_int_mode> (mode, &int_mode)
15479 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15480 return NULL;
15482 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15483 VAR_INIT_STATUS_INITIALIZED);
15484 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15485 VAR_INIT_STATUS_INITIALIZED);
15487 if (op0 == NULL || op1 == NULL)
15488 return NULL;
15490 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15491 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15492 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15493 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15495 /* Checked by the caller. */
15496 int_mode = as_a <scalar_int_mode> (mode);
15497 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15499 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15500 add_loc_descr (&op0, int_loc_descriptor (mask));
15501 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15502 add_loc_descr (&op1, int_loc_descriptor (mask));
15503 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15505 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15507 HOST_WIDE_INT bias = 1;
15508 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15509 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15510 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15513 else if (is_a <scalar_int_mode> (mode, &int_mode)
15514 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15516 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15517 add_loc_descr (&op0, int_loc_descriptor (shift));
15518 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15519 add_loc_descr (&op1, int_loc_descriptor (shift));
15520 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15522 else if (is_a <scalar_int_mode> (mode, &int_mode)
15523 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15525 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15526 dw_loc_descr_ref cvt;
15527 if (type_die == NULL)
15528 return NULL;
15529 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15530 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15531 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15532 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15533 add_loc_descr (&op0, cvt);
15534 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15535 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15536 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15537 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15538 add_loc_descr (&op1, cvt);
15541 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15542 op = DW_OP_lt;
15543 else
15544 op = DW_OP_gt;
15545 ret = op0;
15546 add_loc_descr (&ret, op1);
15547 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15548 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15549 add_loc_descr (&ret, bra_node);
15550 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15551 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15552 add_loc_descr (&ret, drop_node);
15553 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15554 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15555 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15556 && is_a <scalar_int_mode> (mode, &int_mode)
15557 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15558 ret = convert_descriptor_to_mode (int_mode, ret);
15559 return ret;
15562 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15563 but after converting arguments to type_die, afterwards
15564 convert back to unsigned. */
15566 static dw_loc_descr_ref
15567 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15568 scalar_int_mode mode, machine_mode mem_mode)
15570 dw_loc_descr_ref cvt, op0, op1;
15572 if (type_die == NULL)
15573 return NULL;
15574 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15575 VAR_INIT_STATUS_INITIALIZED);
15576 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15577 VAR_INIT_STATUS_INITIALIZED);
15578 if (op0 == NULL || op1 == NULL)
15579 return NULL;
15580 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15581 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15582 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15583 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15584 add_loc_descr (&op0, cvt);
15585 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15586 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15587 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15588 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15589 add_loc_descr (&op1, cvt);
15590 add_loc_descr (&op0, op1);
15591 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15592 return convert_descriptor_to_mode (mode, op0);
15595 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15596 const0 is DW_OP_lit0 or corresponding typed constant,
15597 const1 is DW_OP_lit1 or corresponding typed constant
15598 and constMSB is constant with just the MSB bit set
15599 for the mode):
15600 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15601 L1: const0 DW_OP_swap
15602 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15603 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15604 L3: DW_OP_drop
15605 L4: DW_OP_nop
15607 CTZ is similar:
15608 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15609 L1: const0 DW_OP_swap
15610 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15611 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15612 L3: DW_OP_drop
15613 L4: DW_OP_nop
15615 FFS is similar:
15616 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15617 L1: const1 DW_OP_swap
15618 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15619 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15620 L3: DW_OP_drop
15621 L4: DW_OP_nop */
15623 static dw_loc_descr_ref
15624 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15625 machine_mode mem_mode)
15627 dw_loc_descr_ref op0, ret, tmp;
15628 HOST_WIDE_INT valv;
15629 dw_loc_descr_ref l1jump, l1label;
15630 dw_loc_descr_ref l2jump, l2label;
15631 dw_loc_descr_ref l3jump, l3label;
15632 dw_loc_descr_ref l4jump, l4label;
15633 rtx msb;
15635 if (GET_MODE (XEXP (rtl, 0)) != mode)
15636 return NULL;
15638 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15639 VAR_INIT_STATUS_INITIALIZED);
15640 if (op0 == NULL)
15641 return NULL;
15642 ret = op0;
15643 if (GET_CODE (rtl) == CLZ)
15645 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15646 valv = GET_MODE_BITSIZE (mode);
15648 else if (GET_CODE (rtl) == FFS)
15649 valv = 0;
15650 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15651 valv = GET_MODE_BITSIZE (mode);
15652 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15653 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15654 add_loc_descr (&ret, l1jump);
15655 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15656 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15657 VAR_INIT_STATUS_INITIALIZED);
15658 if (tmp == NULL)
15659 return NULL;
15660 add_loc_descr (&ret, tmp);
15661 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15662 add_loc_descr (&ret, l4jump);
15663 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15664 ? const1_rtx : const0_rtx,
15665 mode, mem_mode,
15666 VAR_INIT_STATUS_INITIALIZED);
15667 if (l1label == NULL)
15668 return NULL;
15669 add_loc_descr (&ret, l1label);
15670 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15671 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15672 add_loc_descr (&ret, l2label);
15673 if (GET_CODE (rtl) != CLZ)
15674 msb = const1_rtx;
15675 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15676 msb = GEN_INT (HOST_WIDE_INT_1U
15677 << (GET_MODE_BITSIZE (mode) - 1));
15678 else
15679 msb = immed_wide_int_const
15680 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15681 GET_MODE_PRECISION (mode)), mode);
15682 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15683 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15684 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15685 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15686 else
15687 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15688 VAR_INIT_STATUS_INITIALIZED);
15689 if (tmp == NULL)
15690 return NULL;
15691 add_loc_descr (&ret, tmp);
15692 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15693 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15694 add_loc_descr (&ret, l3jump);
15695 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15696 VAR_INIT_STATUS_INITIALIZED);
15697 if (tmp == NULL)
15698 return NULL;
15699 add_loc_descr (&ret, tmp);
15700 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15701 ? DW_OP_shl : DW_OP_shr, 0, 0));
15702 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15703 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15704 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15705 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15706 add_loc_descr (&ret, l2jump);
15707 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15708 add_loc_descr (&ret, l3label);
15709 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15710 add_loc_descr (&ret, l4label);
15711 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15712 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15713 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15714 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15715 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15716 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15717 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15718 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15719 return ret;
15722 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15723 const1 is DW_OP_lit1 or corresponding typed constant):
15724 const0 DW_OP_swap
15725 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15726 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15727 L2: DW_OP_drop
15729 PARITY is similar:
15730 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15731 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15732 L2: DW_OP_drop */
15734 static dw_loc_descr_ref
15735 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15736 machine_mode mem_mode)
15738 dw_loc_descr_ref op0, ret, tmp;
15739 dw_loc_descr_ref l1jump, l1label;
15740 dw_loc_descr_ref l2jump, l2label;
15742 if (GET_MODE (XEXP (rtl, 0)) != mode)
15743 return NULL;
15745 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15746 VAR_INIT_STATUS_INITIALIZED);
15747 if (op0 == NULL)
15748 return NULL;
15749 ret = op0;
15750 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15751 VAR_INIT_STATUS_INITIALIZED);
15752 if (tmp == NULL)
15753 return NULL;
15754 add_loc_descr (&ret, tmp);
15755 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15756 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15757 add_loc_descr (&ret, l1label);
15758 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15759 add_loc_descr (&ret, l2jump);
15760 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15761 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15762 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15763 VAR_INIT_STATUS_INITIALIZED);
15764 if (tmp == NULL)
15765 return NULL;
15766 add_loc_descr (&ret, tmp);
15767 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15768 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15769 ? DW_OP_plus : DW_OP_xor, 0, 0));
15770 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15771 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15772 VAR_INIT_STATUS_INITIALIZED);
15773 add_loc_descr (&ret, tmp);
15774 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15775 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15776 add_loc_descr (&ret, l1jump);
15777 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15778 add_loc_descr (&ret, l2label);
15779 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15780 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15781 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15782 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15783 return ret;
15786 /* BSWAP (constS is initial shift count, either 56 or 24):
15787 constS const0
15788 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15789 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15790 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15791 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15792 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15794 static dw_loc_descr_ref
15795 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15796 machine_mode mem_mode)
15798 dw_loc_descr_ref op0, ret, tmp;
15799 dw_loc_descr_ref l1jump, l1label;
15800 dw_loc_descr_ref l2jump, l2label;
15802 if (BITS_PER_UNIT != 8
15803 || (GET_MODE_BITSIZE (mode) != 32
15804 && GET_MODE_BITSIZE (mode) != 64))
15805 return NULL;
15807 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15808 VAR_INIT_STATUS_INITIALIZED);
15809 if (op0 == NULL)
15810 return NULL;
15812 ret = op0;
15813 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15814 mode, mem_mode,
15815 VAR_INIT_STATUS_INITIALIZED);
15816 if (tmp == NULL)
15817 return NULL;
15818 add_loc_descr (&ret, tmp);
15819 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15820 VAR_INIT_STATUS_INITIALIZED);
15821 if (tmp == NULL)
15822 return NULL;
15823 add_loc_descr (&ret, tmp);
15824 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15825 add_loc_descr (&ret, l1label);
15826 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15827 mode, mem_mode,
15828 VAR_INIT_STATUS_INITIALIZED);
15829 add_loc_descr (&ret, tmp);
15830 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15831 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15832 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15833 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15834 VAR_INIT_STATUS_INITIALIZED);
15835 if (tmp == NULL)
15836 return NULL;
15837 add_loc_descr (&ret, tmp);
15838 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15839 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15840 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15841 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15842 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15843 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15844 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15845 VAR_INIT_STATUS_INITIALIZED);
15846 add_loc_descr (&ret, tmp);
15847 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15848 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15849 add_loc_descr (&ret, l2jump);
15850 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15851 VAR_INIT_STATUS_INITIALIZED);
15852 add_loc_descr (&ret, tmp);
15853 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15854 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15855 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15856 add_loc_descr (&ret, l1jump);
15857 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15858 add_loc_descr (&ret, l2label);
15859 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15860 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15861 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15862 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15863 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15864 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15865 return ret;
15868 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15869 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15870 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15871 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15873 ROTATERT is similar:
15874 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15875 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15876 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15878 static dw_loc_descr_ref
15879 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15880 machine_mode mem_mode)
15882 rtx rtlop1 = XEXP (rtl, 1);
15883 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15884 int i;
15886 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15887 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15888 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15889 VAR_INIT_STATUS_INITIALIZED);
15890 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15891 VAR_INIT_STATUS_INITIALIZED);
15892 if (op0 == NULL || op1 == NULL)
15893 return NULL;
15894 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15895 for (i = 0; i < 2; i++)
15897 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15898 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15899 mode, mem_mode,
15900 VAR_INIT_STATUS_INITIALIZED);
15901 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15902 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15903 ? DW_OP_const4u
15904 : HOST_BITS_PER_WIDE_INT == 64
15905 ? DW_OP_const8u : DW_OP_constu,
15906 GET_MODE_MASK (mode), 0);
15907 else
15908 mask[i] = NULL;
15909 if (mask[i] == NULL)
15910 return NULL;
15911 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15913 ret = op0;
15914 add_loc_descr (&ret, op1);
15915 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15916 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15917 if (GET_CODE (rtl) == ROTATERT)
15919 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15920 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15921 GET_MODE_BITSIZE (mode), 0));
15923 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15924 if (mask[0] != NULL)
15925 add_loc_descr (&ret, mask[0]);
15926 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15927 if (mask[1] != NULL)
15929 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15930 add_loc_descr (&ret, mask[1]);
15931 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15933 if (GET_CODE (rtl) == ROTATE)
15935 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15936 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15937 GET_MODE_BITSIZE (mode), 0));
15939 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15940 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15941 return ret;
15944 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15945 for DEBUG_PARAMETER_REF RTL. */
15947 static dw_loc_descr_ref
15948 parameter_ref_descriptor (rtx rtl)
15950 dw_loc_descr_ref ret;
15951 dw_die_ref ref;
15953 if (dwarf_strict)
15954 return NULL;
15955 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15956 /* With LTO during LTRANS we get the late DIE that refers to the early
15957 DIE, thus we add another indirection here. This seems to confuse
15958 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15959 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15960 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15961 if (ref)
15963 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15964 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15965 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15967 else
15969 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15970 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15972 return ret;
15975 /* The following routine converts the RTL for a variable or parameter
15976 (resident in memory) into an equivalent Dwarf representation of a
15977 mechanism for getting the address of that same variable onto the top of a
15978 hypothetical "address evaluation" stack.
15980 When creating memory location descriptors, we are effectively transforming
15981 the RTL for a memory-resident object into its Dwarf postfix expression
15982 equivalent. This routine recursively descends an RTL tree, turning
15983 it into Dwarf postfix code as it goes.
15985 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15987 MEM_MODE is the mode of the memory reference, needed to handle some
15988 autoincrement addressing modes.
15990 Return 0 if we can't represent the location. */
15992 dw_loc_descr_ref
15993 mem_loc_descriptor (rtx rtl, machine_mode mode,
15994 machine_mode mem_mode,
15995 enum var_init_status initialized)
15997 dw_loc_descr_ref mem_loc_result = NULL;
15998 enum dwarf_location_atom op;
15999 dw_loc_descr_ref op0, op1;
16000 rtx inner = NULL_RTX;
16002 if (mode == VOIDmode)
16003 mode = GET_MODE (rtl);
16005 /* Note that for a dynamically sized array, the location we will generate a
16006 description of here will be the lowest numbered location which is
16007 actually within the array. That's *not* necessarily the same as the
16008 zeroth element of the array. */
16010 rtl = targetm.delegitimize_address (rtl);
16012 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
16013 return NULL;
16015 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
16016 switch (GET_CODE (rtl))
16018 case POST_INC:
16019 case POST_DEC:
16020 case POST_MODIFY:
16021 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
16023 case SUBREG:
16024 /* The case of a subreg may arise when we have a local (register)
16025 variable or a formal (register) parameter which doesn't quite fill
16026 up an entire register. For now, just assume that it is
16027 legitimate to make the Dwarf info refer to the whole register which
16028 contains the given subreg. */
16029 if (!subreg_lowpart_p (rtl))
16030 break;
16031 inner = SUBREG_REG (rtl);
16032 /* FALLTHRU */
16033 case TRUNCATE:
16034 if (inner == NULL_RTX)
16035 inner = XEXP (rtl, 0);
16036 if (is_a <scalar_int_mode> (mode, &int_mode)
16037 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16038 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16039 #ifdef POINTERS_EXTEND_UNSIGNED
16040 || (int_mode == Pmode && mem_mode != VOIDmode)
16041 #endif
16043 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
16045 mem_loc_result = mem_loc_descriptor (inner,
16046 inner_mode,
16047 mem_mode, initialized);
16048 break;
16050 if (dwarf_strict && dwarf_version < 5)
16051 break;
16052 if (is_a <scalar_int_mode> (mode, &int_mode)
16053 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16054 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16055 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16057 dw_die_ref type_die;
16058 dw_loc_descr_ref cvt;
16060 mem_loc_result = mem_loc_descriptor (inner,
16061 GET_MODE (inner),
16062 mem_mode, initialized);
16063 if (mem_loc_result == NULL)
16064 break;
16065 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16066 if (type_die == NULL)
16068 mem_loc_result = NULL;
16069 break;
16071 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16072 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16073 else
16074 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16075 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16076 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16077 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16078 add_loc_descr (&mem_loc_result, cvt);
16079 if (is_a <scalar_int_mode> (mode, &int_mode)
16080 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16082 /* Convert it to untyped afterwards. */
16083 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16084 add_loc_descr (&mem_loc_result, cvt);
16087 break;
16089 case REG:
16090 if (!is_a <scalar_int_mode> (mode, &int_mode)
16091 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16092 && rtl != arg_pointer_rtx
16093 && rtl != frame_pointer_rtx
16094 #ifdef POINTERS_EXTEND_UNSIGNED
16095 && (int_mode != Pmode || mem_mode == VOIDmode)
16096 #endif
16099 dw_die_ref type_die;
16100 unsigned int debugger_regnum;
16102 if (dwarf_strict && dwarf_version < 5)
16103 break;
16104 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16105 break;
16106 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16107 if (type_die == NULL)
16108 break;
16110 debugger_regnum = debugger_reg_number (rtl);
16111 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16112 break;
16113 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16114 debugger_regnum, 0);
16115 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16116 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16117 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16118 break;
16120 /* Whenever a register number forms a part of the description of the
16121 method for calculating the (dynamic) address of a memory resident
16122 object, DWARF rules require the register number be referred to as
16123 a "base register". This distinction is not based in any way upon
16124 what category of register the hardware believes the given register
16125 belongs to. This is strictly DWARF terminology we're dealing with
16126 here. Note that in cases where the location of a memory-resident
16127 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16128 OP_CONST (0)) the actual DWARF location descriptor that we generate
16129 may just be OP_BASEREG (basereg). This may look deceptively like
16130 the object in question was allocated to a register (rather than in
16131 memory) so DWARF consumers need to be aware of the subtle
16132 distinction between OP_REG and OP_BASEREG. */
16133 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16134 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16135 else if (stack_realign_drap
16136 && crtl->drap_reg
16137 && crtl->args.internal_arg_pointer == rtl
16138 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16140 /* If RTL is internal_arg_pointer, which has been optimized
16141 out, use DRAP instead. */
16142 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16143 VAR_INIT_STATUS_INITIALIZED);
16145 break;
16147 case SIGN_EXTEND:
16148 case ZERO_EXTEND:
16149 if (!is_a <scalar_int_mode> (mode, &int_mode)
16150 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16151 break;
16152 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16153 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16154 if (op0 == 0)
16155 break;
16156 else if (GET_CODE (rtl) == ZERO_EXTEND
16157 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16158 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16159 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16160 to expand zero extend as two shifts instead of
16161 masking. */
16162 && GET_MODE_SIZE (inner_mode) <= 4)
16164 mem_loc_result = op0;
16165 add_loc_descr (&mem_loc_result,
16166 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16167 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16169 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16171 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16172 shift *= BITS_PER_UNIT;
16173 if (GET_CODE (rtl) == SIGN_EXTEND)
16174 op = DW_OP_shra;
16175 else
16176 op = DW_OP_shr;
16177 mem_loc_result = op0;
16178 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16179 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16180 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16181 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16183 else if (!dwarf_strict || dwarf_version >= 5)
16185 dw_die_ref type_die1, type_die2;
16186 dw_loc_descr_ref cvt;
16188 type_die1 = base_type_for_mode (inner_mode,
16189 GET_CODE (rtl) == ZERO_EXTEND);
16190 if (type_die1 == NULL)
16191 break;
16192 type_die2 = base_type_for_mode (int_mode, 1);
16193 if (type_die2 == NULL)
16194 break;
16195 mem_loc_result = op0;
16196 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16197 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16198 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16199 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16200 add_loc_descr (&mem_loc_result, cvt);
16201 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16202 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16203 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16204 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16205 add_loc_descr (&mem_loc_result, cvt);
16207 break;
16209 case MEM:
16211 rtx new_rtl = avoid_constant_pool_reference (rtl);
16212 if (new_rtl != rtl)
16214 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16215 initialized);
16216 if (mem_loc_result != NULL)
16217 return mem_loc_result;
16220 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16221 get_address_mode (rtl), mode,
16222 VAR_INIT_STATUS_INITIALIZED);
16223 if (mem_loc_result == NULL)
16224 mem_loc_result = tls_mem_loc_descriptor (rtl);
16225 if (mem_loc_result != NULL)
16227 if (!is_a <scalar_int_mode> (mode, &int_mode)
16228 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16230 dw_die_ref type_die;
16231 dw_loc_descr_ref deref;
16232 HOST_WIDE_INT size;
16234 if (dwarf_strict && dwarf_version < 5)
16235 return NULL;
16236 if (!GET_MODE_SIZE (mode).is_constant (&size))
16237 return NULL;
16238 type_die
16239 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16240 if (type_die == NULL)
16241 return NULL;
16242 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16243 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16244 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16245 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16246 add_loc_descr (&mem_loc_result, deref);
16248 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16249 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16250 else
16251 add_loc_descr (&mem_loc_result,
16252 new_loc_descr (DW_OP_deref_size,
16253 GET_MODE_SIZE (int_mode), 0));
16255 break;
16257 case LO_SUM:
16258 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16260 case LABEL_REF:
16261 /* Some ports can transform a symbol ref into a label ref, because
16262 the symbol ref is too far away and has to be dumped into a constant
16263 pool. */
16264 case CONST:
16265 case SYMBOL_REF:
16266 case UNSPEC:
16267 if (!is_a <scalar_int_mode> (mode, &int_mode)
16268 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16269 #ifdef POINTERS_EXTEND_UNSIGNED
16270 && (int_mode != Pmode || mem_mode == VOIDmode)
16271 #endif
16273 break;
16275 if (GET_CODE (rtl) == UNSPEC)
16277 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16278 can't express it in the debug info. This can happen e.g. with some
16279 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16280 approves. */
16281 bool not_ok = false;
16282 subrtx_var_iterator::array_type array;
16283 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16284 if (*iter != rtl && !CONSTANT_P (*iter))
16286 not_ok = true;
16287 break;
16290 if (not_ok)
16291 break;
16293 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16294 if (!const_ok_for_output_1 (*iter))
16296 not_ok = true;
16297 break;
16300 if (not_ok)
16301 break;
16303 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16304 goto symref;
16307 if (GET_CODE (rtl) == SYMBOL_REF
16308 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16310 dw_loc_descr_ref temp;
16312 /* If this is not defined, we have no way to emit the data. */
16313 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16314 break;
16316 temp = new_addr_loc_descr (rtl, dtprel_true);
16318 /* We check for DWARF 5 here because gdb did not implement
16319 DW_OP_form_tls_address until after 7.12. */
16320 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16321 ? DW_OP_form_tls_address
16322 : DW_OP_GNU_push_tls_address),
16323 0, 0);
16324 add_loc_descr (&mem_loc_result, temp);
16326 break;
16329 if (!const_ok_for_output (rtl))
16331 if (GET_CODE (rtl) == CONST)
16332 switch (GET_CODE (XEXP (rtl, 0)))
16334 case NOT:
16335 op = DW_OP_not;
16336 goto try_const_unop;
16337 case NEG:
16338 op = DW_OP_neg;
16339 goto try_const_unop;
16340 try_const_unop:
16341 rtx arg;
16342 arg = XEXP (XEXP (rtl, 0), 0);
16343 if (!CONSTANT_P (arg))
16344 arg = gen_rtx_CONST (int_mode, arg);
16345 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16346 initialized);
16347 if (op0)
16349 mem_loc_result = op0;
16350 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16352 break;
16353 default:
16354 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16355 mem_mode, initialized);
16356 break;
16358 break;
16361 symref:
16362 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16363 vec_safe_push (used_rtx_array, rtl);
16364 break;
16366 case CONCAT:
16367 case CONCATN:
16368 case VAR_LOCATION:
16369 case DEBUG_IMPLICIT_PTR:
16370 expansion_failed (NULL_TREE, rtl,
16371 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16372 return 0;
16374 case ENTRY_VALUE:
16375 if (dwarf_strict && dwarf_version < 5)
16376 return NULL;
16377 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16379 if (!is_a <scalar_int_mode> (mode, &int_mode)
16380 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16381 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16382 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16383 else
16385 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16386 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16387 return NULL;
16388 op0 = one_reg_loc_descriptor (debugger_regnum,
16389 VAR_INIT_STATUS_INITIALIZED);
16392 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16393 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16395 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16396 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16397 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16398 return NULL;
16400 else
16401 gcc_unreachable ();
16402 if (op0 == NULL)
16403 return NULL;
16404 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16405 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16406 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16407 break;
16409 case DEBUG_PARAMETER_REF:
16410 mem_loc_result = parameter_ref_descriptor (rtl);
16411 break;
16413 case PRE_MODIFY:
16414 /* Extract the PLUS expression nested inside and fall into
16415 PLUS code below. */
16416 rtl = XEXP (rtl, 1);
16417 goto plus;
16419 case PRE_INC:
16420 case PRE_DEC:
16421 /* Turn these into a PLUS expression and fall into the PLUS code
16422 below. */
16423 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16424 gen_int_mode (GET_CODE (rtl) == PRE_INC
16425 ? GET_MODE_UNIT_SIZE (mem_mode)
16426 : -GET_MODE_UNIT_SIZE (mem_mode),
16427 mode));
16429 /* fall through */
16431 case PLUS:
16432 plus:
16433 if (is_based_loc (rtl)
16434 && is_a <scalar_int_mode> (mode, &int_mode)
16435 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16436 || XEXP (rtl, 0) == arg_pointer_rtx
16437 || XEXP (rtl, 0) == frame_pointer_rtx))
16438 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16439 INTVAL (XEXP (rtl, 1)),
16440 VAR_INIT_STATUS_INITIALIZED);
16441 else
16443 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16444 VAR_INIT_STATUS_INITIALIZED);
16445 if (mem_loc_result == 0)
16446 break;
16448 if (CONST_INT_P (XEXP (rtl, 1))
16449 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16450 <= DWARF2_ADDR_SIZE))
16451 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16452 else
16454 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16455 VAR_INIT_STATUS_INITIALIZED);
16456 if (op1 == 0)
16457 return NULL;
16458 add_loc_descr (&mem_loc_result, op1);
16459 add_loc_descr (&mem_loc_result,
16460 new_loc_descr (DW_OP_plus, 0, 0));
16463 break;
16465 /* If a pseudo-reg is optimized away, it is possible for it to
16466 be replaced with a MEM containing a multiply or shift. */
16467 case MINUS:
16468 op = DW_OP_minus;
16469 goto do_binop;
16471 case MULT:
16472 op = DW_OP_mul;
16473 goto do_binop;
16475 case DIV:
16476 if ((!dwarf_strict || dwarf_version >= 5)
16477 && is_a <scalar_int_mode> (mode, &int_mode)
16478 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16480 mem_loc_result = typed_binop (DW_OP_div, rtl,
16481 base_type_for_mode (mode, 0),
16482 int_mode, mem_mode);
16483 break;
16485 op = DW_OP_div;
16486 goto do_binop;
16488 case UMOD:
16489 op = DW_OP_mod;
16490 goto do_binop;
16492 case ASHIFT:
16493 op = DW_OP_shl;
16494 goto do_shift;
16496 case ASHIFTRT:
16497 op = DW_OP_shra;
16498 goto do_shift;
16500 case LSHIFTRT:
16501 op = DW_OP_shr;
16502 goto do_shift;
16504 do_shift:
16505 if (!is_a <scalar_int_mode> (mode, &int_mode))
16506 break;
16507 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16508 VAR_INIT_STATUS_INITIALIZED);
16510 rtx rtlop1 = XEXP (rtl, 1);
16511 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16512 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16513 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16514 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16515 VAR_INIT_STATUS_INITIALIZED);
16518 if (op0 == 0 || op1 == 0)
16519 break;
16521 mem_loc_result = op0;
16522 add_loc_descr (&mem_loc_result, op1);
16523 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16524 break;
16526 case AND:
16527 op = DW_OP_and;
16528 goto do_binop;
16530 case IOR:
16531 op = DW_OP_or;
16532 goto do_binop;
16534 case XOR:
16535 op = DW_OP_xor;
16536 goto do_binop;
16538 do_binop:
16539 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16540 VAR_INIT_STATUS_INITIALIZED);
16541 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16543 if (op0 == 0)
16544 break;
16545 mem_loc_result = op0;
16546 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16547 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16548 break;
16550 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16551 VAR_INIT_STATUS_INITIALIZED);
16553 if (op0 == 0 || op1 == 0)
16554 break;
16556 mem_loc_result = op0;
16557 add_loc_descr (&mem_loc_result, op1);
16558 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16559 break;
16561 case MOD:
16562 if ((!dwarf_strict || dwarf_version >= 5)
16563 && is_a <scalar_int_mode> (mode, &int_mode)
16564 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16566 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16567 base_type_for_mode (mode, 0),
16568 int_mode, mem_mode);
16569 break;
16572 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16573 VAR_INIT_STATUS_INITIALIZED);
16574 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16575 VAR_INIT_STATUS_INITIALIZED);
16577 if (op0 == 0 || op1 == 0)
16578 break;
16580 mem_loc_result = op0;
16581 add_loc_descr (&mem_loc_result, op1);
16582 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16583 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16584 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16585 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16586 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16587 break;
16589 case UDIV:
16590 if ((!dwarf_strict || dwarf_version >= 5)
16591 && is_a <scalar_int_mode> (mode, &int_mode))
16593 /* We can use a signed divide if the sign bit is not set. */
16594 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16596 op = DW_OP_div;
16597 goto do_binop;
16600 mem_loc_result = typed_binop (DW_OP_div, rtl,
16601 base_type_for_mode (int_mode, 1),
16602 int_mode, mem_mode);
16604 break;
16606 case NOT:
16607 op = DW_OP_not;
16608 goto do_unop;
16610 case ABS:
16611 op = DW_OP_abs;
16612 goto do_unop;
16614 case NEG:
16615 op = DW_OP_neg;
16616 goto do_unop;
16618 do_unop:
16619 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16620 VAR_INIT_STATUS_INITIALIZED);
16622 if (op0 == 0)
16623 break;
16625 mem_loc_result = op0;
16626 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16627 break;
16629 case CONST_INT:
16630 if (!is_a <scalar_int_mode> (mode, &int_mode)
16631 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16632 #ifdef POINTERS_EXTEND_UNSIGNED
16633 || (int_mode == Pmode
16634 && mem_mode != VOIDmode
16635 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16636 #endif
16639 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16640 break;
16642 if ((!dwarf_strict || dwarf_version >= 5)
16643 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16644 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16646 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16647 scalar_int_mode amode;
16648 if (type_die == NULL)
16649 return NULL;
16650 if (INTVAL (rtl) >= 0
16651 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16652 .exists (&amode))
16653 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16654 /* const DW_OP_convert <XXX> vs.
16655 DW_OP_const_type <XXX, 1, const>. */
16656 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16657 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16659 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16660 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16661 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16662 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16663 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16664 add_loc_descr (&mem_loc_result, op0);
16665 return mem_loc_result;
16667 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16668 INTVAL (rtl));
16669 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16670 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16671 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16672 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16673 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16674 else
16676 mem_loc_result->dw_loc_oprnd2.val_class
16677 = dw_val_class_const_double;
16678 mem_loc_result->dw_loc_oprnd2.v.val_double
16679 = double_int::from_shwi (INTVAL (rtl));
16682 break;
16684 case CONST_DOUBLE:
16685 if (!dwarf_strict || dwarf_version >= 5)
16687 dw_die_ref type_die;
16689 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16690 CONST_DOUBLE rtx could represent either a large integer
16691 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16692 the value is always a floating point constant.
16694 When it is an integer, a CONST_DOUBLE is used whenever
16695 the constant requires 2 HWIs to be adequately represented.
16696 We output CONST_DOUBLEs as blocks. */
16697 if (mode == VOIDmode
16698 || (GET_MODE (rtl) == VOIDmode
16699 && maybe_ne (GET_MODE_BITSIZE (mode),
16700 HOST_BITS_PER_DOUBLE_INT)))
16701 break;
16702 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16703 if (type_die == NULL)
16704 return NULL;
16705 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16706 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16707 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16708 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16709 #if TARGET_SUPPORTS_WIDE_INT == 0
16710 if (!SCALAR_FLOAT_MODE_P (mode))
16712 mem_loc_result->dw_loc_oprnd2.val_class
16713 = dw_val_class_const_double;
16714 mem_loc_result->dw_loc_oprnd2.v.val_double
16715 = rtx_to_double_int (rtl);
16717 else
16718 #endif
16720 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16721 unsigned int length = GET_MODE_SIZE (float_mode);
16722 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16723 unsigned int elt_size = insert_float (rtl, array);
16725 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16726 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16727 = length / elt_size;
16728 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16729 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16732 break;
16734 case CONST_WIDE_INT:
16735 if (!dwarf_strict || dwarf_version >= 5)
16737 dw_die_ref type_die;
16739 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16740 if (type_die == NULL)
16741 return NULL;
16742 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16743 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16744 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16745 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16746 mem_loc_result->dw_loc_oprnd2.val_class
16747 = dw_val_class_wide_int;
16748 mem_loc_result->dw_loc_oprnd2.v.val_wide
16749 = alloc_dw_wide_int (rtx_mode_t (rtl, mode));
16751 break;
16753 case CONST_POLY_INT:
16754 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16755 break;
16757 case EQ:
16758 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16759 break;
16761 case GE:
16762 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16763 break;
16765 case GT:
16766 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16767 break;
16769 case LE:
16770 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16771 break;
16773 case LT:
16774 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16775 break;
16777 case NE:
16778 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16779 break;
16781 case GEU:
16782 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16783 break;
16785 case GTU:
16786 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16787 break;
16789 case LEU:
16790 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16791 break;
16793 case LTU:
16794 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16795 break;
16797 case UMIN:
16798 case UMAX:
16799 if (!SCALAR_INT_MODE_P (mode))
16800 break;
16801 /* FALLTHRU */
16802 case SMIN:
16803 case SMAX:
16804 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16805 break;
16807 case ZERO_EXTRACT:
16808 case SIGN_EXTRACT:
16809 if (CONST_INT_P (XEXP (rtl, 1))
16810 && CONST_INT_P (XEXP (rtl, 2))
16811 && is_a <scalar_int_mode> (mode, &int_mode)
16812 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16813 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16814 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16815 && ((unsigned) INTVAL (XEXP (rtl, 1))
16816 + (unsigned) INTVAL (XEXP (rtl, 2))
16817 <= GET_MODE_BITSIZE (int_mode)))
16819 int shift, size;
16820 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16821 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16822 if (op0 == 0)
16823 break;
16824 if (GET_CODE (rtl) == SIGN_EXTRACT)
16825 op = DW_OP_shra;
16826 else
16827 op = DW_OP_shr;
16828 mem_loc_result = op0;
16829 size = INTVAL (XEXP (rtl, 1));
16830 shift = INTVAL (XEXP (rtl, 2));
16831 if (BITS_BIG_ENDIAN)
16832 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16833 if (shift + size != (int) DWARF2_ADDR_SIZE)
16835 add_loc_descr (&mem_loc_result,
16836 int_loc_descriptor (DWARF2_ADDR_SIZE
16837 - shift - size));
16838 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16840 if (size != (int) DWARF2_ADDR_SIZE)
16842 add_loc_descr (&mem_loc_result,
16843 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16844 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16847 break;
16849 case IF_THEN_ELSE:
16851 dw_loc_descr_ref op2, bra_node, drop_node;
16852 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16853 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16854 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16855 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16856 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16857 VAR_INIT_STATUS_INITIALIZED);
16858 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16859 VAR_INIT_STATUS_INITIALIZED);
16860 if (op0 == NULL || op1 == NULL || op2 == NULL)
16861 break;
16863 mem_loc_result = op1;
16864 add_loc_descr (&mem_loc_result, op2);
16865 add_loc_descr (&mem_loc_result, op0);
16866 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16867 add_loc_descr (&mem_loc_result, bra_node);
16868 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16869 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16870 add_loc_descr (&mem_loc_result, drop_node);
16871 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16872 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16874 break;
16876 case FLOAT_EXTEND:
16877 case FLOAT_TRUNCATE:
16878 case FLOAT:
16879 case UNSIGNED_FLOAT:
16880 case FIX:
16881 case UNSIGNED_FIX:
16882 if (!dwarf_strict || dwarf_version >= 5)
16884 dw_die_ref type_die;
16885 dw_loc_descr_ref cvt;
16887 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16888 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16889 if (op0 == NULL)
16890 break;
16891 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16892 && (GET_CODE (rtl) == FLOAT
16893 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16895 type_die = base_type_for_mode (int_mode,
16896 GET_CODE (rtl) == UNSIGNED_FLOAT);
16897 if (type_die == NULL)
16898 break;
16899 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16900 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16901 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16902 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16903 add_loc_descr (&op0, cvt);
16905 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16906 if (type_die == NULL)
16907 break;
16908 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16909 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16910 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16911 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16912 add_loc_descr (&op0, cvt);
16913 if (is_a <scalar_int_mode> (mode, &int_mode)
16914 && (GET_CODE (rtl) == FIX
16915 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16917 op0 = convert_descriptor_to_mode (int_mode, op0);
16918 if (op0 == NULL)
16919 break;
16921 mem_loc_result = op0;
16923 break;
16925 case CLZ:
16926 case CTZ:
16927 case FFS:
16928 if (is_a <scalar_int_mode> (mode, &int_mode))
16929 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16930 break;
16932 case POPCOUNT:
16933 case PARITY:
16934 if (is_a <scalar_int_mode> (mode, &int_mode))
16935 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16936 break;
16938 case BSWAP:
16939 if (is_a <scalar_int_mode> (mode, &int_mode))
16940 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16941 break;
16943 case ROTATE:
16944 case ROTATERT:
16945 if (is_a <scalar_int_mode> (mode, &int_mode))
16946 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16947 break;
16949 case COMPARE:
16950 /* In theory, we could implement the above. */
16951 /* DWARF cannot represent the unsigned compare operations
16952 natively. */
16953 case SS_MULT:
16954 case US_MULT:
16955 case SS_DIV:
16956 case US_DIV:
16957 case SS_PLUS:
16958 case US_PLUS:
16959 case SS_MINUS:
16960 case US_MINUS:
16961 case SS_NEG:
16962 case US_NEG:
16963 case SS_ABS:
16964 case SS_ASHIFT:
16965 case US_ASHIFT:
16966 case SS_TRUNCATE:
16967 case US_TRUNCATE:
16968 case UNORDERED:
16969 case ORDERED:
16970 case UNEQ:
16971 case UNGE:
16972 case UNGT:
16973 case UNLE:
16974 case UNLT:
16975 case LTGT:
16976 case FRACT_CONVERT:
16977 case UNSIGNED_FRACT_CONVERT:
16978 case SAT_FRACT:
16979 case UNSIGNED_SAT_FRACT:
16980 case SQRT:
16981 case ASM_OPERANDS:
16982 case VEC_MERGE:
16983 case VEC_SELECT:
16984 case VEC_CONCAT:
16985 case VEC_DUPLICATE:
16986 case VEC_SERIES:
16987 case HIGH:
16988 case FMA:
16989 case STRICT_LOW_PART:
16990 case CONST_VECTOR:
16991 case CONST_FIXED:
16992 case CLRSB:
16993 case CLOBBER:
16994 case SMUL_HIGHPART:
16995 case UMUL_HIGHPART:
16996 case BITREVERSE:
16997 case COPYSIGN:
16998 break;
17000 case CONST_STRING:
17001 resolve_one_addr (&rtl);
17002 goto symref;
17004 /* RTL sequences inside PARALLEL record a series of DWARF operations for
17005 the expression. An UNSPEC rtx represents a raw DWARF operation,
17006 new_loc_descr is called for it to build the operation directly.
17007 Otherwise mem_loc_descriptor is called recursively. */
17008 case PARALLEL:
17010 int index = 0;
17011 dw_loc_descr_ref exp_result = NULL;
17013 for (; index < XVECLEN (rtl, 0); index++)
17015 rtx elem = XVECEXP (rtl, 0, index);
17016 if (GET_CODE (elem) == UNSPEC)
17018 /* Each DWARF operation UNSPEC contain two operands, if
17019 one operand is not used for the operation, const0_rtx is
17020 passed. */
17021 gcc_assert (XVECLEN (elem, 0) == 2);
17023 HOST_WIDE_INT dw_op = XINT (elem, 1);
17024 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
17025 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
17026 exp_result
17027 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
17028 oprnd2);
17030 else
17031 exp_result
17032 = mem_loc_descriptor (elem, mode, mem_mode,
17033 VAR_INIT_STATUS_INITIALIZED);
17035 if (!mem_loc_result)
17036 mem_loc_result = exp_result;
17037 else
17038 add_loc_descr (&mem_loc_result, exp_result);
17041 break;
17044 default:
17045 if (flag_checking)
17047 print_rtl (stderr, rtl);
17048 gcc_unreachable ();
17050 break;
17053 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17054 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17056 return mem_loc_result;
17059 /* Return a descriptor that describes the concatenation of two locations.
17060 This is typically a complex variable. */
17062 static dw_loc_descr_ref
17063 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17065 /* At present we only track constant-sized pieces. */
17066 unsigned int size0, size1;
17067 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17068 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17069 return 0;
17071 dw_loc_descr_ref cc_loc_result = NULL;
17072 dw_loc_descr_ref x0_ref
17073 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17074 dw_loc_descr_ref x1_ref
17075 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17077 if (x0_ref == 0 || x1_ref == 0)
17078 return 0;
17080 cc_loc_result = x0_ref;
17081 add_loc_descr_op_piece (&cc_loc_result, size0);
17083 add_loc_descr (&cc_loc_result, x1_ref);
17084 add_loc_descr_op_piece (&cc_loc_result, size1);
17086 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17087 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17089 return cc_loc_result;
17092 /* Return a descriptor that describes the concatenation of N
17093 locations. */
17095 static dw_loc_descr_ref
17096 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17098 unsigned int i;
17099 dw_loc_descr_ref cc_loc_result = NULL;
17100 unsigned int n = XVECLEN (concatn, 0);
17101 unsigned int size;
17103 for (i = 0; i < n; ++i)
17105 dw_loc_descr_ref ref;
17106 rtx x = XVECEXP (concatn, 0, i);
17108 /* At present we only track constant-sized pieces. */
17109 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17110 return NULL;
17112 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17113 if (ref == NULL)
17114 return NULL;
17116 add_loc_descr (&cc_loc_result, ref);
17117 add_loc_descr_op_piece (&cc_loc_result, size);
17120 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17121 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17123 return cc_loc_result;
17126 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17127 for DEBUG_IMPLICIT_PTR RTL. */
17129 static dw_loc_descr_ref
17130 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17132 dw_loc_descr_ref ret;
17133 dw_die_ref ref;
17135 if (dwarf_strict && dwarf_version < 5)
17136 return NULL;
17137 gcc_assert (VAR_P (DEBUG_IMPLICIT_PTR_DECL (rtl))
17138 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17139 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17140 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17141 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17142 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17143 if (ref)
17145 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17146 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17147 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17149 else
17151 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17152 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17154 return ret;
17157 /* Output a proper Dwarf location descriptor for a variable or parameter
17158 which is either allocated in a register or in a memory location. For a
17159 register, we just generate an OP_REG and the register number. For a
17160 memory location we provide a Dwarf postfix expression describing how to
17161 generate the (dynamic) address of the object onto the address stack.
17163 MODE is mode of the decl if this loc_descriptor is going to be used in
17164 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17165 allowed, VOIDmode otherwise.
17167 If we don't know how to describe it, return 0. */
17169 static dw_loc_descr_ref
17170 loc_descriptor (rtx rtl, machine_mode mode,
17171 enum var_init_status initialized)
17173 dw_loc_descr_ref loc_result = NULL;
17174 scalar_int_mode int_mode;
17176 switch (GET_CODE (rtl))
17178 case SUBREG:
17179 /* The case of a subreg may arise when we have a local (register)
17180 variable or a formal (register) parameter which doesn't quite fill
17181 up an entire register. For now, just assume that it is
17182 legitimate to make the Dwarf info refer to the whole register which
17183 contains the given subreg. */
17184 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17185 loc_result = loc_descriptor (SUBREG_REG (rtl),
17186 GET_MODE (SUBREG_REG (rtl)), initialized);
17187 else
17188 goto do_default;
17189 break;
17191 case REG:
17192 loc_result = reg_loc_descriptor (rtl, initialized);
17193 break;
17195 case MEM:
17196 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17197 GET_MODE (rtl), initialized);
17198 if (loc_result == NULL)
17199 loc_result = tls_mem_loc_descriptor (rtl);
17200 if (loc_result == NULL)
17202 rtx new_rtl = avoid_constant_pool_reference (rtl);
17203 if (new_rtl != rtl)
17204 loc_result = loc_descriptor (new_rtl, mode, initialized);
17206 break;
17208 case CONCAT:
17209 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17210 initialized);
17211 break;
17213 case CONCATN:
17214 loc_result = concatn_loc_descriptor (rtl, initialized);
17215 break;
17217 case VAR_LOCATION:
17218 /* Single part. */
17219 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17221 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17222 if (GET_CODE (loc) == EXPR_LIST)
17223 loc = XEXP (loc, 0);
17224 loc_result = loc_descriptor (loc, mode, initialized);
17225 break;
17228 rtl = XEXP (rtl, 1);
17229 /* FALLTHRU */
17231 case PARALLEL:
17233 rtvec par_elems = XVEC (rtl, 0);
17234 int num_elem = GET_NUM_ELEM (par_elems);
17235 machine_mode mode;
17236 int i, size;
17238 /* Create the first one, so we have something to add to. */
17239 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17240 VOIDmode, initialized);
17241 if (loc_result == NULL)
17242 return NULL;
17243 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17244 /* At present we only track constant-sized pieces. */
17245 if (!GET_MODE_SIZE (mode).is_constant (&size))
17246 return NULL;
17247 add_loc_descr_op_piece (&loc_result, size);
17248 for (i = 1; i < num_elem; i++)
17250 dw_loc_descr_ref temp;
17252 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17253 VOIDmode, initialized);
17254 if (temp == NULL)
17255 return NULL;
17256 add_loc_descr (&loc_result, temp);
17257 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17258 /* At present we only track constant-sized pieces. */
17259 if (!GET_MODE_SIZE (mode).is_constant (&size))
17260 return NULL;
17261 add_loc_descr_op_piece (&loc_result, size);
17264 break;
17266 case CONST_INT:
17267 if (mode != VOIDmode && mode != BLKmode)
17269 int_mode = as_a <scalar_int_mode> (mode);
17270 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17271 INTVAL (rtl));
17273 break;
17275 case CONST_DOUBLE:
17276 if (mode == VOIDmode)
17277 mode = GET_MODE (rtl);
17279 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17281 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17283 /* Note that a CONST_DOUBLE rtx could represent either an integer
17284 or a floating-point constant. A CONST_DOUBLE is used whenever
17285 the constant requires more than one word in order to be
17286 adequately represented. We output CONST_DOUBLEs as blocks. */
17287 scalar_mode smode = as_a <scalar_mode> (mode);
17288 loc_result = new_loc_descr (DW_OP_implicit_value,
17289 GET_MODE_SIZE (smode), 0);
17290 #if TARGET_SUPPORTS_WIDE_INT == 0
17291 if (!SCALAR_FLOAT_MODE_P (smode))
17293 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17294 loc_result->dw_loc_oprnd2.v.val_double
17295 = rtx_to_double_int (rtl);
17297 else
17298 #endif
17300 unsigned int length = GET_MODE_SIZE (smode);
17301 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17302 unsigned int elt_size = insert_float (rtl, array);
17304 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17305 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17306 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17307 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17310 break;
17312 case CONST_WIDE_INT:
17313 if (mode == VOIDmode)
17314 mode = GET_MODE (rtl);
17316 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17318 int_mode = as_a <scalar_int_mode> (mode);
17319 loc_result = new_loc_descr (DW_OP_implicit_value,
17320 GET_MODE_SIZE (int_mode), 0);
17321 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17322 loc_result->dw_loc_oprnd2.v.val_wide
17323 = alloc_dw_wide_int (rtx_mode_t (rtl, int_mode));
17325 break;
17327 case CONST_VECTOR:
17328 if (mode == VOIDmode)
17329 mode = GET_MODE (rtl);
17331 if (mode != VOIDmode
17332 /* The combination of a length and byte elt_size doesn't extend
17333 naturally to boolean vectors, where several elements are packed
17334 into the same byte. */
17335 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17336 && (dwarf_version >= 4 || !dwarf_strict))
17338 unsigned int length;
17339 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17340 return NULL;
17342 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17343 unsigned char *array
17344 = ggc_vec_alloc<unsigned char> (length * elt_size);
17345 unsigned int i;
17346 unsigned char *p;
17347 machine_mode imode = GET_MODE_INNER (mode);
17349 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17350 switch (GET_MODE_CLASS (mode))
17352 case MODE_VECTOR_INT:
17353 for (i = 0, p = array; i < length; i++, p += elt_size)
17355 rtx elt = CONST_VECTOR_ELT (rtl, i);
17356 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17358 break;
17360 case MODE_VECTOR_FLOAT:
17361 for (i = 0, p = array; i < length; i++, p += elt_size)
17363 rtx elt = CONST_VECTOR_ELT (rtl, i);
17364 insert_float (elt, p);
17366 break;
17368 default:
17369 gcc_unreachable ();
17372 loc_result = new_loc_descr (DW_OP_implicit_value,
17373 length * elt_size, 0);
17374 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17375 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17376 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17377 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17379 break;
17381 case CONST:
17382 if (mode == VOIDmode
17383 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17384 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17385 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17387 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17388 break;
17390 /* FALLTHROUGH */
17391 case SYMBOL_REF:
17392 if (!const_ok_for_output (rtl))
17393 break;
17394 /* FALLTHROUGH */
17395 case LABEL_REF:
17396 if (is_a <scalar_int_mode> (mode, &int_mode)
17397 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17398 && (dwarf_version >= 4 || !dwarf_strict))
17400 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17401 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17402 vec_safe_push (used_rtx_array, rtl);
17404 break;
17406 case DEBUG_IMPLICIT_PTR:
17407 loc_result = implicit_ptr_descriptor (rtl, 0);
17408 break;
17410 case PLUS:
17411 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17412 && CONST_INT_P (XEXP (rtl, 1)))
17414 loc_result
17415 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17416 break;
17418 /* FALLTHRU */
17419 do_default:
17420 default:
17421 if ((is_a <scalar_int_mode> (mode, &int_mode)
17422 && GET_MODE (rtl) == int_mode
17423 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17424 && dwarf_version >= 4)
17425 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17427 /* Value expression. */
17428 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17429 if (loc_result)
17430 add_loc_descr (&loc_result,
17431 new_loc_descr (DW_OP_stack_value, 0, 0));
17433 break;
17436 return loc_result;
17439 /* We need to figure out what section we should use as the base for the
17440 address ranges where a given location is valid.
17441 1. If this particular DECL has a section associated with it, use that.
17442 2. If this function has a section associated with it, use that.
17443 3. Otherwise, use the text section.
17444 XXX: If you split a variable across multiple sections, we won't notice. */
17446 static const char *
17447 secname_for_decl (const_tree decl)
17449 const char *secname;
17451 if (VAR_OR_FUNCTION_DECL_P (decl)
17452 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17453 && DECL_SECTION_NAME (decl))
17454 secname = DECL_SECTION_NAME (decl);
17455 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17457 if (in_cold_section_p)
17459 section *sec = current_function_section ();
17460 if (sec->common.flags & SECTION_NAMED)
17461 return sec->named.name;
17463 secname = DECL_SECTION_NAME (current_function_decl);
17465 else if (cfun && in_cold_section_p)
17466 secname = crtl->subsections.cold_section_label;
17467 else
17468 secname = text_section_label;
17470 return secname;
17473 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17475 static bool
17476 decl_by_reference_p (tree decl)
17478 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17479 || VAR_P (decl))
17480 && DECL_BY_REFERENCE (decl));
17483 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17484 for VARLOC. */
17486 static dw_loc_descr_ref
17487 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17488 enum var_init_status initialized)
17490 int have_address = 0;
17491 dw_loc_descr_ref descr;
17492 machine_mode mode;
17494 if (want_address != 2)
17496 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17497 /* Single part. */
17498 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17500 varloc = PAT_VAR_LOCATION_LOC (varloc);
17501 if (GET_CODE (varloc) == EXPR_LIST)
17502 varloc = XEXP (varloc, 0);
17503 mode = GET_MODE (varloc);
17504 if (MEM_P (varloc))
17506 rtx addr = XEXP (varloc, 0);
17507 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17508 mode, initialized);
17509 if (descr)
17510 have_address = 1;
17511 else
17513 rtx x = avoid_constant_pool_reference (varloc);
17514 if (x != varloc)
17515 descr = mem_loc_descriptor (x, mode, VOIDmode,
17516 initialized);
17519 else
17520 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17522 else
17523 return 0;
17525 else
17527 if (GET_CODE (varloc) == VAR_LOCATION)
17528 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17529 else
17530 mode = DECL_MODE (loc);
17531 descr = loc_descriptor (varloc, mode, initialized);
17532 have_address = 1;
17535 if (!descr)
17536 return 0;
17538 if (want_address == 2 && !have_address
17539 && (dwarf_version >= 4 || !dwarf_strict))
17541 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17543 expansion_failed (loc, NULL_RTX,
17544 "DWARF address size mismatch");
17545 return 0;
17547 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17548 have_address = 1;
17550 /* Show if we can't fill the request for an address. */
17551 if (want_address && !have_address)
17553 expansion_failed (loc, NULL_RTX,
17554 "Want address and only have value");
17555 return 0;
17558 /* If we've got an address and don't want one, dereference. */
17559 if (!want_address && have_address)
17561 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17562 enum dwarf_location_atom op;
17564 if (size > DWARF2_ADDR_SIZE || size == -1)
17566 expansion_failed (loc, NULL_RTX,
17567 "DWARF address size mismatch");
17568 return 0;
17570 else if (size == DWARF2_ADDR_SIZE)
17571 op = DW_OP_deref;
17572 else
17573 op = DW_OP_deref_size;
17575 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17578 return descr;
17581 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17582 if it is not possible. */
17584 static dw_loc_descr_ref
17585 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17587 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17588 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17589 else if (dwarf_version >= 3 || !dwarf_strict)
17590 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17591 else
17592 return NULL;
17595 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17596 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17598 static dw_loc_descr_ref
17599 dw_sra_loc_expr (tree decl, rtx loc)
17601 rtx p;
17602 unsigned HOST_WIDE_INT padsize = 0;
17603 dw_loc_descr_ref descr, *descr_tail;
17604 unsigned HOST_WIDE_INT decl_size;
17605 rtx varloc;
17606 enum var_init_status initialized;
17608 if (DECL_SIZE (decl) == NULL
17609 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17610 return NULL;
17612 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17613 descr = NULL;
17614 descr_tail = &descr;
17616 for (p = loc; p; p = XEXP (p, 1))
17618 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17619 rtx loc_note = *decl_piece_varloc_ptr (p);
17620 dw_loc_descr_ref cur_descr;
17621 dw_loc_descr_ref *tail, last = NULL;
17622 unsigned HOST_WIDE_INT opsize = 0;
17624 if (loc_note == NULL_RTX
17625 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17627 padsize += bitsize;
17628 continue;
17630 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17631 varloc = NOTE_VAR_LOCATION (loc_note);
17632 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17633 if (cur_descr == NULL)
17635 padsize += bitsize;
17636 continue;
17639 /* Check that cur_descr either doesn't use
17640 DW_OP_*piece operations, or their sum is equal
17641 to bitsize. Otherwise we can't embed it. */
17642 for (tail = &cur_descr; *tail != NULL;
17643 tail = &(*tail)->dw_loc_next)
17644 if ((*tail)->dw_loc_opc == DW_OP_piece)
17646 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17647 * BITS_PER_UNIT;
17648 last = *tail;
17650 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17652 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17653 last = *tail;
17656 if (last != NULL && opsize != bitsize)
17658 padsize += bitsize;
17659 /* Discard the current piece of the descriptor and release any
17660 addr_table entries it uses. */
17661 remove_loc_list_addr_table_entries (cur_descr);
17662 continue;
17665 /* If there is a hole, add DW_OP_*piece after empty DWARF
17666 expression, which means that those bits are optimized out. */
17667 if (padsize)
17669 if (padsize > decl_size)
17671 remove_loc_list_addr_table_entries (cur_descr);
17672 goto discard_descr;
17674 decl_size -= padsize;
17675 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17676 if (*descr_tail == NULL)
17678 remove_loc_list_addr_table_entries (cur_descr);
17679 goto discard_descr;
17681 descr_tail = &(*descr_tail)->dw_loc_next;
17682 padsize = 0;
17684 *descr_tail = cur_descr;
17685 descr_tail = tail;
17686 if (bitsize > decl_size)
17687 goto discard_descr;
17688 decl_size -= bitsize;
17689 if (last == NULL)
17691 HOST_WIDE_INT offset = 0;
17692 if (GET_CODE (varloc) == VAR_LOCATION
17693 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17695 varloc = PAT_VAR_LOCATION_LOC (varloc);
17696 if (GET_CODE (varloc) == EXPR_LIST)
17697 varloc = XEXP (varloc, 0);
17701 if (GET_CODE (varloc) == CONST
17702 || GET_CODE (varloc) == SIGN_EXTEND
17703 || GET_CODE (varloc) == ZERO_EXTEND)
17704 varloc = XEXP (varloc, 0);
17705 else if (GET_CODE (varloc) == SUBREG)
17706 varloc = SUBREG_REG (varloc);
17707 else
17708 break;
17710 while (1);
17711 /* DW_OP_bit_size offset should be zero for register
17712 or implicit location descriptions and empty location
17713 descriptions, but for memory addresses needs big endian
17714 adjustment. */
17715 if (MEM_P (varloc))
17717 unsigned HOST_WIDE_INT memsize;
17718 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17719 goto discard_descr;
17720 memsize *= BITS_PER_UNIT;
17721 if (memsize != bitsize)
17723 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17724 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17725 goto discard_descr;
17726 if (memsize < bitsize)
17727 goto discard_descr;
17728 if (BITS_BIG_ENDIAN)
17729 offset = memsize - bitsize;
17733 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17734 if (*descr_tail == NULL)
17735 goto discard_descr;
17736 descr_tail = &(*descr_tail)->dw_loc_next;
17740 /* If there were any non-empty expressions, add padding till the end of
17741 the decl. */
17742 if (descr != NULL && decl_size != 0)
17744 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17745 if (*descr_tail == NULL)
17746 goto discard_descr;
17748 return descr;
17750 discard_descr:
17751 /* Discard the descriptor and release any addr_table entries it uses. */
17752 remove_loc_list_addr_table_entries (descr);
17753 return NULL;
17756 /* Return the dwarf representation of the location list LOC_LIST of
17757 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17758 function. */
17760 static dw_loc_list_ref
17761 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17763 const char *endname, *secname;
17764 var_loc_view endview;
17765 rtx varloc;
17766 enum var_init_status initialized;
17767 struct var_loc_node *node;
17768 dw_loc_descr_ref descr;
17769 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17770 dw_loc_list_ref list = NULL;
17771 dw_loc_list_ref *listp = &list;
17773 /* Now that we know what section we are using for a base,
17774 actually construct the list of locations.
17775 The first location information is what is passed to the
17776 function that creates the location list, and the remaining
17777 locations just get added on to that list.
17778 Note that we only know the start address for a location
17779 (IE location changes), so to build the range, we use
17780 the range [current location start, next location start].
17781 This means we have to special case the last node, and generate
17782 a range of [last location start, end of function label]. */
17784 if (cfun && crtl->has_bb_partition)
17786 bool save_in_cold_section_p = in_cold_section_p;
17787 in_cold_section_p = first_function_block_is_cold;
17788 if (loc_list->last_before_switch == NULL)
17789 in_cold_section_p = !in_cold_section_p;
17790 secname = secname_for_decl (decl);
17791 in_cold_section_p = save_in_cold_section_p;
17793 else
17794 secname = secname_for_decl (decl);
17796 for (node = loc_list->first; node; node = node->next)
17798 bool range_across_switch = false;
17799 if (GET_CODE (node->loc) == EXPR_LIST
17800 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17802 if (GET_CODE (node->loc) == EXPR_LIST)
17804 descr = NULL;
17805 /* This requires DW_OP_{,bit_}piece, which is not usable
17806 inside DWARF expressions. */
17807 if (want_address == 2)
17808 descr = dw_sra_loc_expr (decl, node->loc);
17810 else
17812 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17813 varloc = NOTE_VAR_LOCATION (node->loc);
17814 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17816 if (descr)
17818 /* If section switch happens in between node->label
17819 and node->next->label (or end of function) and
17820 we can't emit it as a single entry list,
17821 emit two ranges, first one ending at the end
17822 of first partition and second one starting at the
17823 beginning of second partition. */
17824 if (node == loc_list->last_before_switch
17825 && (node != loc_list->first || loc_list->first->next
17826 /* If we are to emit a view number, we will emit
17827 a loclist rather than a single location
17828 expression for the entire function (see
17829 loc_list_has_views), so we have to split the
17830 range that straddles across partitions. */
17831 || !ZERO_VIEW_P (node->view))
17832 && current_function_decl)
17834 endname = cfun->fde->dw_fde_end;
17835 endview = 0;
17836 range_across_switch = true;
17838 /* The variable has a location between NODE->LABEL and
17839 NODE->NEXT->LABEL. */
17840 else if (node->next)
17841 endname = node->next->label, endview = node->next->view;
17842 /* If the variable has a location at the last label
17843 it keeps its location until the end of function. */
17844 else if (!current_function_decl)
17845 endname = text_end_label, endview = 0;
17846 else
17848 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17849 current_function_funcdef_no);
17850 endname = ggc_strdup (label_id);
17851 endview = 0;
17854 *listp = new_loc_list (descr, node->label, node->view,
17855 endname, endview, secname);
17856 if (TREE_CODE (decl) == PARM_DECL
17857 && node == loc_list->first
17858 && NOTE_P (node->loc)
17859 && strcmp (node->label, endname) == 0)
17860 (*listp)->force = true;
17861 listp = &(*listp)->dw_loc_next;
17865 if (cfun
17866 && crtl->has_bb_partition
17867 && node == loc_list->last_before_switch)
17869 bool save_in_cold_section_p = in_cold_section_p;
17870 in_cold_section_p = !first_function_block_is_cold;
17871 secname = secname_for_decl (decl);
17872 in_cold_section_p = save_in_cold_section_p;
17875 if (range_across_switch)
17877 if (GET_CODE (node->loc) == EXPR_LIST)
17878 descr = dw_sra_loc_expr (decl, node->loc);
17879 else
17881 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17882 varloc = NOTE_VAR_LOCATION (node->loc);
17883 descr = dw_loc_list_1 (decl, varloc, want_address,
17884 initialized);
17886 gcc_assert (descr);
17887 /* The variable has a location between NODE->LABEL and
17888 NODE->NEXT->LABEL. */
17889 if (node->next)
17890 endname = node->next->label, endview = node->next->view;
17891 else
17892 endname = cfun->fde->dw_fde_second_end, endview = 0;
17893 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17894 endname, endview, secname);
17895 listp = &(*listp)->dw_loc_next;
17899 /* Try to avoid the overhead of a location list emitting a location
17900 expression instead, but only if we didn't have more than one
17901 location entry in the first place. If some entries were not
17902 representable, we don't want to pretend a single entry that was
17903 applies to the entire scope in which the variable is
17904 available. */
17905 if (list && loc_list->first->next)
17906 gen_llsym (list);
17907 else
17908 maybe_gen_llsym (list);
17910 return list;
17913 /* Return true if the loc_list has only single element and thus
17914 can be represented as location description. */
17916 static bool
17917 single_element_loc_list_p (dw_loc_list_ref list)
17919 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17920 return !list->ll_symbol;
17923 /* Duplicate a single element of location list. */
17925 static inline dw_loc_descr_ref
17926 copy_loc_descr (dw_loc_descr_ref ref)
17928 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17929 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17930 return copy;
17933 /* To each location in list LIST append loc descr REF. */
17935 static void
17936 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17938 dw_loc_descr_ref copy;
17939 add_loc_descr (&list->expr, ref);
17940 list = list->dw_loc_next;
17941 while (list)
17943 copy = copy_loc_descr (ref);
17944 add_loc_descr (&list->expr, copy);
17945 while (copy->dw_loc_next)
17946 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17947 list = list->dw_loc_next;
17951 /* To each location in list LIST prepend loc descr REF. */
17953 static void
17954 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17956 dw_loc_descr_ref copy;
17957 dw_loc_descr_ref ref_end = list->expr;
17958 add_loc_descr (&ref, list->expr);
17959 list->expr = ref;
17960 list = list->dw_loc_next;
17961 while (list)
17963 dw_loc_descr_ref end = list->expr;
17964 list->expr = copy = copy_loc_descr (ref);
17965 while (copy->dw_loc_next != ref_end)
17966 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17967 copy->dw_loc_next = end;
17968 list = list->dw_loc_next;
17972 /* Given two lists RET and LIST
17973 produce location list that is result of adding expression in LIST
17974 to expression in RET on each position in program.
17975 Might be destructive on both RET and LIST.
17977 TODO: We handle only simple cases of RET or LIST having at most one
17978 element. General case would involve sorting the lists in program order
17979 and merging them that will need some additional work.
17980 Adding that will improve quality of debug info especially for SRA-ed
17981 structures. */
17983 static void
17984 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17986 if (!list)
17987 return;
17988 if (!*ret)
17990 *ret = list;
17991 return;
17993 if (!list->dw_loc_next)
17995 add_loc_descr_to_each (*ret, list->expr);
17996 return;
17998 if (!(*ret)->dw_loc_next)
18000 prepend_loc_descr_to_each (list, (*ret)->expr);
18001 *ret = list;
18002 return;
18004 expansion_failed (NULL_TREE, NULL_RTX,
18005 "Don't know how to merge two non-trivial"
18006 " location lists.\n");
18007 *ret = NULL;
18008 return;
18011 /* LOC is constant expression. Try a luck, look it up in constant
18012 pool and return its loc_descr of its address. */
18014 static dw_loc_descr_ref
18015 cst_pool_loc_descr (tree loc)
18017 /* Get an RTL for this, if something has been emitted. */
18018 rtx rtl = lookup_constant_def (loc);
18020 if (!rtl || !MEM_P (rtl))
18022 gcc_assert (!rtl);
18023 return 0;
18025 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
18027 /* TODO: We might get more coverage if we was actually delaying expansion
18028 of all expressions till end of compilation when constant pools are fully
18029 populated. */
18030 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
18032 expansion_failed (loc, NULL_RTX,
18033 "CST value in contant pool but not marked.");
18034 return 0;
18036 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
18037 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
18040 /* Return dw_loc_list representing address of addr_expr LOC
18041 by looking for inner INDIRECT_REF expression and turning
18042 it into simple arithmetics.
18044 See loc_list_from_tree for the meaning of CONTEXT. */
18046 static dw_loc_list_ref
18047 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
18048 loc_descr_context *context)
18050 tree obj, offset;
18051 poly_int64 bitsize, bitpos, bytepos;
18052 machine_mode mode;
18053 int unsignedp, reversep, volatilep = 0;
18054 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18056 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18057 &bitsize, &bitpos, &offset, &mode,
18058 &unsignedp, &reversep, &volatilep);
18059 STRIP_NOPS (obj);
18060 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18062 expansion_failed (loc, NULL_RTX, "bitfield access");
18063 return 0;
18065 if (!INDIRECT_REF_P (obj))
18067 expansion_failed (obj,
18068 NULL_RTX, "no indirect ref in inner refrence");
18069 return 0;
18071 if (!offset && known_eq (bitpos, 0))
18072 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18073 context);
18074 else if (toplev
18075 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18076 && (dwarf_version >= 4 || !dwarf_strict))
18078 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18079 if (!list_ret)
18080 return 0;
18081 if (offset)
18083 /* Variable offset. */
18084 list_ret1 = loc_list_from_tree (offset, 0, context);
18085 if (list_ret1 == 0)
18086 return 0;
18087 add_loc_list (&list_ret, list_ret1);
18088 if (!list_ret)
18089 return 0;
18090 add_loc_descr_to_each (list_ret,
18091 new_loc_descr (DW_OP_plus, 0, 0));
18093 HOST_WIDE_INT value;
18094 if (bytepos.is_constant (&value) && value > 0)
18095 add_loc_descr_to_each (list_ret,
18096 new_loc_descr (DW_OP_plus_uconst, value, 0));
18097 else if (maybe_ne (bytepos, 0))
18098 loc_list_plus_const (list_ret, bytepos);
18099 add_loc_descr_to_each (list_ret,
18100 new_loc_descr (DW_OP_stack_value, 0, 0));
18102 return list_ret;
18105 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18106 all operations from LOC are nops, move to the last one. Insert in NOPS all
18107 operations that are skipped. */
18109 static void
18110 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18111 hash_set<dw_loc_descr_ref> &nops)
18113 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18115 nops.add (loc);
18116 loc = loc->dw_loc_next;
18120 /* Helper for loc_descr_without_nops: free the location description operation
18121 P. */
18123 bool
18124 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18126 ggc_free (loc);
18127 return true;
18130 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18131 finishes LOC. */
18133 static void
18134 loc_descr_without_nops (dw_loc_descr_ref &loc)
18136 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18137 return;
18139 /* Set of all DW_OP_nop operations we remove. */
18140 hash_set<dw_loc_descr_ref> nops;
18142 /* First, strip all prefix NOP operations in order to keep the head of the
18143 operations list. */
18144 loc_descr_to_next_no_nop (loc, nops);
18146 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18148 /* For control flow operations: strip "prefix" nops in destination
18149 labels. */
18150 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18151 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18152 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18153 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18155 /* Do the same for the operations that follow, then move to the next
18156 iteration. */
18157 if (cur->dw_loc_next != NULL)
18158 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18159 cur = cur->dw_loc_next;
18162 nops.traverse<void *, free_loc_descr> (NULL);
18166 struct dwarf_procedure_info;
18168 /* Helper structure for location descriptions generation. */
18169 struct loc_descr_context
18171 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18172 NULL_TREE if DW_OP_push_object_address in invalid for this location
18173 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18174 tree context_type;
18175 /* The ..._DECL node that should be translated as a
18176 DW_OP_push_object_address operation. */
18177 tree base_decl;
18178 /* Information about the DWARF procedure we are currently generating. NULL if
18179 we are not generating a DWARF procedure. */
18180 struct dwarf_procedure_info *dpi;
18181 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18182 by consumer. Used for DW_TAG_generic_subrange attributes. */
18183 bool placeholder_arg;
18184 /* True if PLACEHOLDER_EXPR has been seen. */
18185 bool placeholder_seen;
18186 /* True if strict preservation of signedness has been requested. */
18187 bool strict_signedness;
18190 /* DWARF procedures generation
18192 DWARF expressions (aka. location descriptions) are used to encode variable
18193 things such as sizes or offsets. Such computations can have redundant parts
18194 that can be factorized in order to reduce the size of the output debug
18195 information. This is the whole point of DWARF procedures.
18197 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18198 already factorized into functions ("size functions") in order to handle very
18199 big and complex types. Such functions are quite simple: they have integral
18200 arguments, they return an integral result and their body contains only a
18201 return statement with arithmetic expressions. This is the only kind of
18202 function we are interested in translating into DWARF procedures, here.
18204 DWARF expressions and DWARF procedure are executed using a stack, so we have
18205 to define some calling convention for them to interact. Let's say that:
18207 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18208 all arguments in reverse order (right-to-left) so that when the DWARF
18209 procedure execution starts, the first argument is the top of the stack.
18211 - Then, when returning, the DWARF procedure must have consumed all arguments
18212 on the stack, must have pushed the result and touched nothing else.
18214 - Each integral argument and the result are integral types can be hold in a
18215 single stack slot.
18217 - We call "frame offset" the number of stack slots that are "under DWARF
18218 procedure control": it includes the arguments slots, the temporaries and
18219 the result slot. Thus, it is equal to the number of arguments when the
18220 procedure execution starts and must be equal to one (the result) when it
18221 returns. */
18223 /* Helper structure used when generating operations for a DWARF procedure. */
18224 struct dwarf_procedure_info
18226 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18227 currently translated. */
18228 tree fndecl;
18229 /* The number of arguments FNDECL takes. */
18230 unsigned args_count;
18233 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18234 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18235 equate it to this DIE. */
18237 static dw_die_ref
18238 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18239 dw_die_ref parent_die)
18241 dw_die_ref dwarf_proc_die;
18243 if ((dwarf_version < 3 && dwarf_strict)
18244 || location == NULL)
18245 return NULL;
18247 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18248 if (fndecl)
18249 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18250 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18251 return dwarf_proc_die;
18254 /* Return whether TYPE is a supported type as a DWARF procedure argument
18255 type or return type (we handle only scalar types and pointer types that
18256 aren't wider than the DWARF expression evaluation stack). */
18258 static bool
18259 is_handled_procedure_type (tree type)
18261 return ((INTEGRAL_TYPE_P (type)
18262 || TREE_CODE (type) == OFFSET_TYPE
18263 || TREE_CODE (type) == POINTER_TYPE)
18264 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18267 /* Helper for resolve_args_picking: do the same but stop when coming across
18268 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18269 offset *before* evaluating the corresponding operation. */
18271 static bool
18272 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18273 struct dwarf_procedure_info *dpi,
18274 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18276 /* The "frame_offset" identifier is already used to name a macro... */
18277 unsigned frame_offset_ = initial_frame_offset;
18278 dw_loc_descr_ref l;
18280 for (l = loc; l != NULL;)
18282 bool existed;
18283 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18285 /* If we already met this node, there is nothing to compute anymore. */
18286 if (existed)
18288 /* Make sure that the stack size is consistent wherever the execution
18289 flow comes from. */
18290 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18291 break;
18293 l_frame_offset = frame_offset_;
18295 /* If needed, relocate the picking offset with respect to the frame
18296 offset. */
18297 if (l->frame_offset_rel)
18299 unsigned HOST_WIDE_INT off;
18300 switch (l->dw_loc_opc)
18302 case DW_OP_pick:
18303 off = l->dw_loc_oprnd1.v.val_unsigned;
18304 break;
18305 case DW_OP_dup:
18306 off = 0;
18307 break;
18308 case DW_OP_over:
18309 off = 1;
18310 break;
18311 default:
18312 gcc_unreachable ();
18314 /* frame_offset_ is the size of the current stack frame, including
18315 incoming arguments. Besides, the arguments are pushed
18316 right-to-left. Thus, in order to access the Nth argument from
18317 this operation node, the picking has to skip temporaries *plus*
18318 one stack slot per argument (0 for the first one, 1 for the second
18319 one, etc.).
18321 The targetted argument number (N) is already set as the operand,
18322 and the number of temporaries can be computed with:
18323 frame_offsets_ - dpi->args_count */
18324 off += frame_offset_ - dpi->args_count;
18326 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18327 if (off > 255)
18328 return false;
18330 if (off == 0)
18332 l->dw_loc_opc = DW_OP_dup;
18333 l->dw_loc_oprnd1.v.val_unsigned = 0;
18335 else if (off == 1)
18337 l->dw_loc_opc = DW_OP_over;
18338 l->dw_loc_oprnd1.v.val_unsigned = 0;
18340 else
18342 l->dw_loc_opc = DW_OP_pick;
18343 l->dw_loc_oprnd1.v.val_unsigned = off;
18347 /* Update frame_offset according to the effect the current operation has
18348 on the stack. */
18349 switch (l->dw_loc_opc)
18351 case DW_OP_deref:
18352 case DW_OP_swap:
18353 case DW_OP_rot:
18354 case DW_OP_abs:
18355 case DW_OP_neg:
18356 case DW_OP_not:
18357 case DW_OP_plus_uconst:
18358 case DW_OP_skip:
18359 case DW_OP_reg0:
18360 case DW_OP_reg1:
18361 case DW_OP_reg2:
18362 case DW_OP_reg3:
18363 case DW_OP_reg4:
18364 case DW_OP_reg5:
18365 case DW_OP_reg6:
18366 case DW_OP_reg7:
18367 case DW_OP_reg8:
18368 case DW_OP_reg9:
18369 case DW_OP_reg10:
18370 case DW_OP_reg11:
18371 case DW_OP_reg12:
18372 case DW_OP_reg13:
18373 case DW_OP_reg14:
18374 case DW_OP_reg15:
18375 case DW_OP_reg16:
18376 case DW_OP_reg17:
18377 case DW_OP_reg18:
18378 case DW_OP_reg19:
18379 case DW_OP_reg20:
18380 case DW_OP_reg21:
18381 case DW_OP_reg22:
18382 case DW_OP_reg23:
18383 case DW_OP_reg24:
18384 case DW_OP_reg25:
18385 case DW_OP_reg26:
18386 case DW_OP_reg27:
18387 case DW_OP_reg28:
18388 case DW_OP_reg29:
18389 case DW_OP_reg30:
18390 case DW_OP_reg31:
18391 case DW_OP_bregx:
18392 case DW_OP_piece:
18393 case DW_OP_deref_size:
18394 case DW_OP_nop:
18395 case DW_OP_bit_piece:
18396 case DW_OP_implicit_value:
18397 case DW_OP_stack_value:
18398 case DW_OP_deref_type:
18399 case DW_OP_convert:
18400 case DW_OP_reinterpret:
18401 case DW_OP_GNU_deref_type:
18402 case DW_OP_GNU_convert:
18403 case DW_OP_GNU_reinterpret:
18404 break;
18406 case DW_OP_addr:
18407 case DW_OP_const1u:
18408 case DW_OP_const1s:
18409 case DW_OP_const2u:
18410 case DW_OP_const2s:
18411 case DW_OP_const4u:
18412 case DW_OP_const4s:
18413 case DW_OP_const8u:
18414 case DW_OP_const8s:
18415 case DW_OP_constu:
18416 case DW_OP_consts:
18417 case DW_OP_dup:
18418 case DW_OP_over:
18419 case DW_OP_pick:
18420 case DW_OP_lit0:
18421 case DW_OP_lit1:
18422 case DW_OP_lit2:
18423 case DW_OP_lit3:
18424 case DW_OP_lit4:
18425 case DW_OP_lit5:
18426 case DW_OP_lit6:
18427 case DW_OP_lit7:
18428 case DW_OP_lit8:
18429 case DW_OP_lit9:
18430 case DW_OP_lit10:
18431 case DW_OP_lit11:
18432 case DW_OP_lit12:
18433 case DW_OP_lit13:
18434 case DW_OP_lit14:
18435 case DW_OP_lit15:
18436 case DW_OP_lit16:
18437 case DW_OP_lit17:
18438 case DW_OP_lit18:
18439 case DW_OP_lit19:
18440 case DW_OP_lit20:
18441 case DW_OP_lit21:
18442 case DW_OP_lit22:
18443 case DW_OP_lit23:
18444 case DW_OP_lit24:
18445 case DW_OP_lit25:
18446 case DW_OP_lit26:
18447 case DW_OP_lit27:
18448 case DW_OP_lit28:
18449 case DW_OP_lit29:
18450 case DW_OP_lit30:
18451 case DW_OP_lit31:
18452 case DW_OP_breg0:
18453 case DW_OP_breg1:
18454 case DW_OP_breg2:
18455 case DW_OP_breg3:
18456 case DW_OP_breg4:
18457 case DW_OP_breg5:
18458 case DW_OP_breg6:
18459 case DW_OP_breg7:
18460 case DW_OP_breg8:
18461 case DW_OP_breg9:
18462 case DW_OP_breg10:
18463 case DW_OP_breg11:
18464 case DW_OP_breg12:
18465 case DW_OP_breg13:
18466 case DW_OP_breg14:
18467 case DW_OP_breg15:
18468 case DW_OP_breg16:
18469 case DW_OP_breg17:
18470 case DW_OP_breg18:
18471 case DW_OP_breg19:
18472 case DW_OP_breg20:
18473 case DW_OP_breg21:
18474 case DW_OP_breg22:
18475 case DW_OP_breg23:
18476 case DW_OP_breg24:
18477 case DW_OP_breg25:
18478 case DW_OP_breg26:
18479 case DW_OP_breg27:
18480 case DW_OP_breg28:
18481 case DW_OP_breg29:
18482 case DW_OP_breg30:
18483 case DW_OP_breg31:
18484 case DW_OP_fbreg:
18485 case DW_OP_push_object_address:
18486 case DW_OP_call_frame_cfa:
18487 case DW_OP_GNU_variable_value:
18488 case DW_OP_GNU_addr_index:
18489 case DW_OP_GNU_const_index:
18490 ++frame_offset_;
18491 break;
18493 case DW_OP_drop:
18494 case DW_OP_xderef:
18495 case DW_OP_and:
18496 case DW_OP_div:
18497 case DW_OP_minus:
18498 case DW_OP_mod:
18499 case DW_OP_mul:
18500 case DW_OP_or:
18501 case DW_OP_plus:
18502 case DW_OP_shl:
18503 case DW_OP_shr:
18504 case DW_OP_shra:
18505 case DW_OP_xor:
18506 case DW_OP_bra:
18507 case DW_OP_eq:
18508 case DW_OP_ge:
18509 case DW_OP_gt:
18510 case DW_OP_le:
18511 case DW_OP_lt:
18512 case DW_OP_ne:
18513 case DW_OP_regx:
18514 case DW_OP_xderef_size:
18515 --frame_offset_;
18516 break;
18518 case DW_OP_call2:
18519 case DW_OP_call4:
18520 case DW_OP_call_ref:
18522 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18523 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18525 if (stack_usage == NULL)
18526 return false;
18527 frame_offset_ += *stack_usage;
18528 break;
18531 case DW_OP_implicit_pointer:
18532 case DW_OP_entry_value:
18533 case DW_OP_const_type:
18534 case DW_OP_regval_type:
18535 case DW_OP_form_tls_address:
18536 case DW_OP_GNU_push_tls_address:
18537 case DW_OP_GNU_uninit:
18538 case DW_OP_GNU_encoded_addr:
18539 case DW_OP_GNU_implicit_pointer:
18540 case DW_OP_GNU_entry_value:
18541 case DW_OP_GNU_const_type:
18542 case DW_OP_GNU_regval_type:
18543 case DW_OP_GNU_parameter_ref:
18544 /* loc_list_from_tree will probably not output these operations for
18545 size functions, so assume they will not appear here. */
18546 /* Fall through... */
18548 default:
18549 gcc_unreachable ();
18552 /* Now, follow the control flow (except subroutine calls). */
18553 switch (l->dw_loc_opc)
18555 case DW_OP_bra:
18556 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18557 frame_offsets))
18558 return false;
18559 /* Fall through. */
18561 case DW_OP_skip:
18562 l = l->dw_loc_oprnd1.v.val_loc;
18563 break;
18565 case DW_OP_stack_value:
18566 return true;
18568 default:
18569 l = l->dw_loc_next;
18570 break;
18574 return true;
18577 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18578 operations) in order to resolve the operand of DW_OP_pick operations that
18579 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18580 offset *before* LOC is executed. Return if all relocations were
18581 successful. */
18583 static bool
18584 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18585 struct dwarf_procedure_info *dpi)
18587 /* Associate to all visited operations the frame offset *before* evaluating
18588 this operation. */
18589 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18591 return
18592 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18595 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18596 Return NULL if it is not possible. */
18598 static dw_die_ref
18599 function_to_dwarf_procedure (tree fndecl)
18601 struct dwarf_procedure_info dpi;
18602 struct loc_descr_context ctx = {
18603 NULL_TREE, /* context_type */
18604 NULL_TREE, /* base_decl */
18605 &dpi, /* dpi */
18606 false, /* placeholder_arg */
18607 false, /* placeholder_seen */
18608 true /* strict_signedness */
18610 dw_die_ref dwarf_proc_die;
18611 tree tree_body = DECL_SAVED_TREE (fndecl);
18612 dw_loc_descr_ref loc_body, epilogue;
18614 tree cursor;
18615 unsigned i;
18617 /* Do not generate multiple DWARF procedures for the same function
18618 declaration. */
18619 dwarf_proc_die = lookup_decl_die (fndecl);
18620 if (dwarf_proc_die != NULL)
18621 return dwarf_proc_die;
18623 /* DWARF procedures are available starting with the DWARFv3 standard. */
18624 if (dwarf_version < 3 && dwarf_strict)
18625 return NULL;
18627 /* We handle only functions for which we still have a body, that return a
18628 supported type and that takes arguments with supported types. Note that
18629 there is no point translating functions that return nothing. */
18630 if (tree_body == NULL_TREE
18631 || DECL_RESULT (fndecl) == NULL_TREE
18632 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18633 return NULL;
18635 for (cursor = DECL_ARGUMENTS (fndecl);
18636 cursor != NULL_TREE;
18637 cursor = TREE_CHAIN (cursor))
18638 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18639 return NULL;
18641 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18642 if (TREE_CODE (tree_body) != RETURN_EXPR)
18643 return NULL;
18644 tree_body = TREE_OPERAND (tree_body, 0);
18645 if (TREE_CODE (tree_body) != MODIFY_EXPR
18646 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18647 return NULL;
18648 tree_body = TREE_OPERAND (tree_body, 1);
18650 /* Try to translate the body expression itself. Note that this will probably
18651 cause an infinite recursion if its call graph has a cycle. This is very
18652 unlikely for size functions, however, so don't bother with such things at
18653 the moment. */
18654 dpi.fndecl = fndecl;
18655 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18656 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18657 if (!loc_body)
18658 return NULL;
18660 /* After evaluating all operands in "loc_body", we should still have on the
18661 stack all arguments plus the desired function result (top of the stack).
18662 Generate code in order to keep only the result in our stack frame. */
18663 epilogue = NULL;
18664 for (i = 0; i < dpi.args_count; ++i)
18666 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18667 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18668 op_couple->dw_loc_next->dw_loc_next = epilogue;
18669 epilogue = op_couple;
18671 add_loc_descr (&loc_body, epilogue);
18672 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18673 return NULL;
18675 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18676 because they are considered useful. Now there is an epilogue, they are
18677 not anymore, so give it another try. */
18678 loc_descr_without_nops (loc_body);
18680 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18681 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18682 though, given that size functions do not come from source, so they should
18683 not have a dedicated DW_TAG_subprogram DIE. */
18684 dwarf_proc_die
18685 = new_dwarf_proc_die (loc_body, fndecl,
18686 get_context_die (DECL_CONTEXT (fndecl)));
18688 /* The called DWARF procedure consumes one stack slot per argument and
18689 returns one stack slot. */
18690 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18692 return dwarf_proc_die;
18695 /* Helper function for loc_list_from_tree. Perform OP binary op,
18696 but after converting arguments to type_die, afterwards convert
18697 back to unsigned. */
18699 static dw_loc_list_ref
18700 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18701 dw_die_ref type_die, scalar_int_mode mode,
18702 struct loc_descr_context *context)
18704 dw_loc_list_ref op0, op1;
18705 dw_loc_descr_ref cvt, binop;
18707 if (type_die == NULL)
18708 return NULL;
18710 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18711 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18712 if (op0 == NULL || op1 == NULL)
18713 return NULL;
18715 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18716 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18717 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18718 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18719 add_loc_descr_to_each (op0, cvt);
18721 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18722 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18723 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18724 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18725 add_loc_descr_to_each (op1, cvt);
18727 add_loc_list (&op0, op1);
18728 if (op0 == NULL)
18729 return NULL;
18731 binop = new_loc_descr (op, 0, 0);
18732 convert_descriptor_to_mode (mode, binop);
18733 add_loc_descr_to_each (op0, binop);
18735 return op0;
18738 /* Generate Dwarf location list representing LOC.
18739 If WANT_ADDRESS is false, expression computing LOC will be computed
18740 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18741 if WANT_ADDRESS is 2, expression computing address useable in location
18742 will be returned (i.e. DW_OP_reg can be used
18743 to refer to register values).
18745 CONTEXT provides information to customize the location descriptions
18746 generation. Its context_type field specifies what type is implicitly
18747 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18748 will not be generated.
18750 Its DPI field determines whether we are generating a DWARF expression for a
18751 DWARF procedure, so PARM_DECL references are processed specifically.
18753 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18754 and dpi fields were null. */
18756 static dw_loc_list_ref
18757 loc_list_from_tree_1 (tree loc, int want_address,
18758 struct loc_descr_context *context)
18760 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18761 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18762 int have_address = 0;
18763 enum dwarf_location_atom op;
18765 /* ??? Most of the time we do not take proper care for sign/zero
18766 extending the values properly. Hopefully this won't be a real
18767 problem... */
18769 if (context != NULL
18770 && context->base_decl == loc
18771 && want_address == 0)
18773 if (dwarf_version >= 3 || !dwarf_strict)
18774 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18775 NULL, 0, NULL, 0, NULL);
18776 else
18777 return NULL;
18780 switch (TREE_CODE (loc))
18782 case ERROR_MARK:
18783 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18784 return 0;
18786 case PLACEHOLDER_EXPR:
18787 /* This case involves extracting fields from an object to determine the
18788 position of other fields. It is supposed to appear only as the first
18789 operand of COMPONENT_REF nodes and to reference precisely the type
18790 that the context allows or its enclosing type. */
18791 if (context != NULL
18792 && (TREE_TYPE (loc) == context->context_type
18793 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18794 && want_address >= 1)
18796 if (dwarf_version >= 3 || !dwarf_strict)
18798 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18799 have_address = 1;
18800 break;
18802 else
18803 return NULL;
18805 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18806 the single argument passed by consumer. */
18807 else if (context != NULL
18808 && context->placeholder_arg
18809 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18810 && want_address == 0)
18812 ret = new_loc_descr (DW_OP_pick, 0, 0);
18813 ret->frame_offset_rel = 1;
18814 context->placeholder_seen = true;
18815 break;
18817 else
18818 expansion_failed (loc, NULL_RTX,
18819 "PLACEHOLDER_EXPR for an unexpected type");
18820 break;
18822 case CALL_EXPR:
18824 tree callee = get_callee_fndecl (loc);
18825 dw_die_ref dwarf_proc;
18827 if (callee
18828 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18829 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18831 /* DWARF procedures are used for size functions, which are built
18832 when size expressions contain conditional constructs, so we
18833 request strict preservation of signedness for comparisons. */
18834 bool old_strict_signedness;
18835 if (context)
18837 old_strict_signedness = context->strict_signedness;
18838 context->strict_signedness = true;
18841 /* Evaluate arguments right-to-left so that the first argument
18842 will be the top-most one on the stack. */
18843 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18845 tree arg = CALL_EXPR_ARG (loc, i);
18846 ret1 = loc_descriptor_from_tree (arg, 0, context);
18847 if (!ret1)
18849 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18850 return NULL;
18852 add_loc_descr (&ret, ret1);
18855 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18856 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18857 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18858 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18859 add_loc_descr (&ret, ret1);
18860 if (context)
18861 context->strict_signedness = old_strict_signedness;
18863 else
18864 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18865 break;
18868 case PREINCREMENT_EXPR:
18869 case PREDECREMENT_EXPR:
18870 case POSTINCREMENT_EXPR:
18871 case POSTDECREMENT_EXPR:
18872 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18873 /* There are no opcodes for these operations. */
18874 return 0;
18876 case ADDR_EXPR:
18877 /* If we already want an address, see if there is INDIRECT_REF inside
18878 e.g. for &this->field. */
18879 if (want_address)
18881 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18882 (loc, want_address == 2, context);
18883 if (list_ret)
18884 have_address = 1;
18885 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18886 && (ret = cst_pool_loc_descr (loc)))
18887 have_address = 1;
18889 /* Otherwise, process the argument and look for the address. */
18890 if (!list_ret && !ret)
18891 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18892 else
18894 if (want_address)
18895 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18896 return NULL;
18898 break;
18900 case VAR_DECL:
18901 if (DECL_THREAD_LOCAL_P (loc))
18903 rtx rtl;
18904 enum dwarf_location_atom tls_op;
18905 enum dtprel_bool dtprel = dtprel_false;
18907 if (targetm.have_tls)
18909 /* If this is not defined, we have no way to emit the
18910 data. */
18911 if (!targetm.asm_out.output_dwarf_dtprel)
18912 return 0;
18914 /* The way DW_OP_GNU_push_tls_address is specified, we
18915 can only look up addresses of objects in the current
18916 module. We used DW_OP_addr as first op, but that's
18917 wrong, because DW_OP_addr is relocated by the debug
18918 info consumer, while DW_OP_GNU_push_tls_address
18919 operand shouldn't be. */
18920 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18921 return 0;
18922 dtprel = dtprel_true;
18923 /* We check for DWARF 5 here because gdb did not implement
18924 DW_OP_form_tls_address until after 7.12. */
18925 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18926 : DW_OP_GNU_push_tls_address);
18928 else
18930 if (!targetm.emutls.debug_form_tls_address
18931 || !(dwarf_version >= 3 || !dwarf_strict))
18932 return 0;
18933 /* We stuffed the control variable into the DECL_VALUE_EXPR
18934 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18935 no longer appear in gimple code. We used the control
18936 variable in specific so that we could pick it up here. */
18937 loc = DECL_VALUE_EXPR (loc);
18938 tls_op = DW_OP_form_tls_address;
18941 rtl = rtl_for_decl_location (loc);
18942 if (rtl == NULL_RTX)
18943 return 0;
18945 if (!MEM_P (rtl))
18946 return 0;
18947 rtl = XEXP (rtl, 0);
18948 if (! CONSTANT_P (rtl))
18949 return 0;
18951 ret = new_addr_loc_descr (rtl, dtprel);
18952 ret1 = new_loc_descr (tls_op, 0, 0);
18953 add_loc_descr (&ret, ret1);
18955 have_address = 1;
18956 break;
18958 /* FALLTHRU */
18960 case PARM_DECL:
18961 if (context != NULL && context->dpi != NULL
18962 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18964 /* We are generating code for a DWARF procedure and we want to access
18965 one of its arguments: find the appropriate argument offset and let
18966 the resolve_args_picking pass compute the offset that complies
18967 with the stack frame size. */
18968 unsigned i = 0;
18969 tree cursor;
18971 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18972 cursor != NULL_TREE && cursor != loc;
18973 cursor = TREE_CHAIN (cursor), ++i)
18975 /* If we are translating a DWARF procedure, all referenced parameters
18976 must belong to the current function. */
18977 gcc_assert (cursor != NULL_TREE);
18979 ret = new_loc_descr (DW_OP_pick, i, 0);
18980 ret->frame_offset_rel = 1;
18981 break;
18983 /* FALLTHRU */
18985 case RESULT_DECL:
18986 if (DECL_HAS_VALUE_EXPR_P (loc))
18988 tree value_expr = DECL_VALUE_EXPR (loc);
18990 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18991 to wait until they get an RTX in order to reference them. */
18992 if (early_dwarf
18993 && TREE_CODE (value_expr) == COMPONENT_REF
18994 && VAR_P (TREE_OPERAND (value_expr, 0))
18995 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18997 else
18998 return loc_list_from_tree_1 (value_expr, want_address, context);
19001 /* FALLTHRU */
19003 case FUNCTION_DECL:
19005 rtx rtl;
19006 var_loc_list *loc_list = lookup_decl_loc (loc);
19008 if (loc_list && loc_list->first)
19010 list_ret = dw_loc_list (loc_list, loc, want_address);
19011 have_address = want_address != 0;
19012 break;
19014 rtl = rtl_for_decl_location (loc);
19015 if (rtl == NULL_RTX)
19017 if (TREE_CODE (loc) != FUNCTION_DECL
19018 && early_dwarf
19019 && want_address != 1
19020 && ! DECL_IGNORED_P (loc)
19021 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
19022 || POINTER_TYPE_P (TREE_TYPE (loc)))
19023 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
19024 <= DWARF2_ADDR_SIZE))
19026 dw_die_ref ref = lookup_decl_die (loc);
19027 if (ref)
19029 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19030 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19031 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
19032 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
19034 else if (current_function_decl
19035 && DECL_CONTEXT (loc) == current_function_decl)
19037 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
19038 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19039 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
19041 break;
19043 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
19044 return 0;
19046 else if (CONST_INT_P (rtl))
19048 HOST_WIDE_INT val = INTVAL (rtl);
19049 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19050 val &= GET_MODE_MASK (DECL_MODE (loc));
19051 ret = int_loc_descriptor (val);
19053 else if (GET_CODE (rtl) == CONST_STRING)
19055 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19056 return 0;
19058 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19059 ret = new_addr_loc_descr (rtl, dtprel_false);
19060 else
19062 machine_mode mode, mem_mode;
19064 /* Certain constructs can only be represented at top-level. */
19065 if (want_address == 2)
19067 ret = loc_descriptor (rtl, VOIDmode,
19068 VAR_INIT_STATUS_INITIALIZED);
19069 have_address = 1;
19071 else
19073 mode = GET_MODE (rtl);
19074 mem_mode = VOIDmode;
19075 if (MEM_P (rtl))
19077 mem_mode = mode;
19078 mode = get_address_mode (rtl);
19079 rtl = XEXP (rtl, 0);
19080 have_address = 1;
19082 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19083 VAR_INIT_STATUS_INITIALIZED);
19085 if (!ret)
19086 expansion_failed (loc, rtl,
19087 "failed to produce loc descriptor for rtl");
19090 break;
19092 case MEM_REF:
19093 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19095 have_address = 1;
19096 goto do_plus;
19098 /* Fallthru. */
19099 case INDIRECT_REF:
19100 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19101 have_address = 1;
19102 break;
19104 case TARGET_MEM_REF:
19105 case SSA_NAME:
19106 case DEBUG_EXPR_DECL:
19107 return NULL;
19109 case COMPOUND_EXPR:
19110 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19111 context);
19113 CASE_CONVERT:
19114 case VIEW_CONVERT_EXPR:
19115 case SAVE_EXPR:
19116 case MODIFY_EXPR:
19117 case NON_LVALUE_EXPR:
19118 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19119 context);
19121 case COMPONENT_REF:
19122 case BIT_FIELD_REF:
19123 case ARRAY_REF:
19124 case ARRAY_RANGE_REF:
19125 case REALPART_EXPR:
19126 case IMAGPART_EXPR:
19128 tree obj, offset;
19129 poly_int64 bitsize, bitpos, bytepos;
19130 machine_mode mode;
19131 int unsignedp, reversep, volatilep = 0;
19133 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19134 &unsignedp, &reversep, &volatilep);
19136 gcc_assert (obj != loc);
19138 list_ret = loc_list_from_tree_1 (obj,
19139 want_address == 2
19140 && known_eq (bitpos, 0)
19141 && !offset ? 2 : 1,
19142 context);
19143 /* TODO: We can extract value of the small expression via shifting even
19144 for nonzero bitpos. */
19145 if (list_ret == 0)
19146 return 0;
19147 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19148 || !multiple_p (bitsize, BITS_PER_UNIT))
19150 expansion_failed (loc, NULL_RTX,
19151 "bitfield access");
19152 return 0;
19155 if (offset != NULL_TREE)
19157 /* Variable offset. */
19158 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19159 if (list_ret1 == 0)
19160 return 0;
19161 add_loc_list (&list_ret, list_ret1);
19162 if (!list_ret)
19163 return 0;
19164 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19167 HOST_WIDE_INT value;
19168 if (bytepos.is_constant (&value) && value > 0)
19169 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19170 value, 0));
19171 else if (maybe_ne (bytepos, 0))
19172 loc_list_plus_const (list_ret, bytepos);
19174 have_address = 1;
19175 break;
19178 case INTEGER_CST:
19179 if ((want_address || !tree_fits_shwi_p (loc))
19180 && (ret = cst_pool_loc_descr (loc)))
19181 have_address = 1;
19182 else if (want_address == 2
19183 && tree_fits_shwi_p (loc)
19184 && (ret = address_of_int_loc_descriptor
19185 (int_size_in_bytes (TREE_TYPE (loc)),
19186 tree_to_shwi (loc))))
19187 have_address = 1;
19188 else if (tree_fits_shwi_p (loc))
19189 ret = int_loc_descriptor (tree_to_shwi (loc));
19190 else if (tree_fits_uhwi_p (loc))
19191 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19192 else
19194 expansion_failed (loc, NULL_RTX,
19195 "Integer operand is not host integer");
19196 return 0;
19198 break;
19200 case POLY_INT_CST:
19202 if (want_address)
19204 expansion_failed (loc, NULL_RTX,
19205 "constant address with a runtime component");
19206 return 0;
19208 poly_int64 value;
19209 if (!poly_int_tree_p (loc, &value))
19211 expansion_failed (loc, NULL_RTX, "constant too big");
19212 return 0;
19214 ret = int_loc_descriptor (value);
19216 break;
19218 case CONSTRUCTOR:
19219 case REAL_CST:
19220 case STRING_CST:
19221 case COMPLEX_CST:
19222 if ((ret = cst_pool_loc_descr (loc)))
19223 have_address = 1;
19224 else if (TREE_CODE (loc) == CONSTRUCTOR)
19226 tree type = TREE_TYPE (loc);
19227 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19228 unsigned HOST_WIDE_INT offset = 0;
19229 unsigned HOST_WIDE_INT cnt;
19230 constructor_elt *ce;
19232 if (TREE_CODE (type) == RECORD_TYPE)
19234 /* This is very limited, but it's enough to output
19235 pointers to member functions, as long as the
19236 referenced function is defined in the current
19237 translation unit. */
19238 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19240 tree val = ce->value;
19242 tree field = ce->index;
19244 if (val)
19245 STRIP_NOPS (val);
19247 if (!field || DECL_BIT_FIELD (field))
19249 expansion_failed (loc, NULL_RTX,
19250 "bitfield in record type constructor");
19251 size = offset = (unsigned HOST_WIDE_INT)-1;
19252 ret = NULL;
19253 break;
19256 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19257 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19258 gcc_assert (pos + fieldsize <= size);
19259 if (pos < offset)
19261 expansion_failed (loc, NULL_RTX,
19262 "out-of-order fields in record constructor");
19263 size = offset = (unsigned HOST_WIDE_INT)-1;
19264 ret = NULL;
19265 break;
19267 if (pos > offset)
19269 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19270 add_loc_descr (&ret, ret1);
19271 offset = pos;
19273 if (val && fieldsize != 0)
19275 ret1 = loc_descriptor_from_tree (val, want_address, context);
19276 if (!ret1)
19278 expansion_failed (loc, NULL_RTX,
19279 "unsupported expression in field");
19280 size = offset = (unsigned HOST_WIDE_INT)-1;
19281 ret = NULL;
19282 break;
19284 add_loc_descr (&ret, ret1);
19286 if (fieldsize)
19288 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19289 add_loc_descr (&ret, ret1);
19290 offset = pos + fieldsize;
19294 if (offset != size)
19296 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19297 add_loc_descr (&ret, ret1);
19298 offset = size;
19301 have_address = !!want_address;
19303 else
19304 expansion_failed (loc, NULL_RTX,
19305 "constructor of non-record type");
19307 else
19308 /* We can construct small constants here using int_loc_descriptor. */
19309 expansion_failed (loc, NULL_RTX,
19310 "constructor or constant not in constant pool");
19311 break;
19313 case TRUTH_AND_EXPR:
19314 case TRUTH_ANDIF_EXPR:
19315 case BIT_AND_EXPR:
19316 op = DW_OP_and;
19317 goto do_binop;
19319 case TRUTH_XOR_EXPR:
19320 case BIT_XOR_EXPR:
19321 op = DW_OP_xor;
19322 goto do_binop;
19324 case TRUTH_OR_EXPR:
19325 case TRUTH_ORIF_EXPR:
19326 case BIT_IOR_EXPR:
19327 op = DW_OP_or;
19328 goto do_binop;
19330 case EXACT_DIV_EXPR:
19331 case FLOOR_DIV_EXPR:
19332 case TRUNC_DIV_EXPR:
19333 /* Turn a divide by a power of 2 into a shift when possible. */
19334 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19335 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19337 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19338 if (log2 > 0)
19340 list_ret
19341 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19342 if (list_ret == 0)
19343 return 0;
19345 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19346 add_loc_descr_to_each (list_ret,
19347 new_loc_descr (DW_OP_shr, 0, 0));
19348 break;
19352 /* fall through */
19354 case CEIL_DIV_EXPR:
19355 case ROUND_DIV_EXPR:
19356 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19358 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19359 scalar_int_mode int_mode;
19361 if ((dwarf_strict && dwarf_version < 5)
19362 || !is_a <scalar_int_mode> (mode, &int_mode))
19363 return 0;
19365 /* We can use a signed divide if the sign bit is not set. */
19366 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19368 op = DW_OP_div;
19369 goto do_binop;
19372 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19373 base_type_for_mode (int_mode, 1),
19374 int_mode, context);
19375 break;
19377 op = DW_OP_div;
19378 goto do_binop;
19380 case MINUS_EXPR:
19381 op = DW_OP_minus;
19382 goto do_binop;
19384 case FLOOR_MOD_EXPR:
19385 case CEIL_MOD_EXPR:
19386 case ROUND_MOD_EXPR:
19387 case TRUNC_MOD_EXPR:
19388 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19390 op = DW_OP_mod;
19391 goto do_binop;
19393 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19394 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19395 if (list_ret == 0 || list_ret1 == 0)
19396 return 0;
19398 add_loc_list (&list_ret, list_ret1);
19399 if (list_ret == 0)
19400 return 0;
19401 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19402 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19403 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19404 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19405 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19406 break;
19408 case MULT_EXPR:
19409 op = DW_OP_mul;
19410 goto do_binop;
19412 case LSHIFT_EXPR:
19413 op = DW_OP_shl;
19414 goto do_binop;
19416 case RSHIFT_EXPR:
19417 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19418 goto do_binop;
19420 case POINTER_PLUS_EXPR:
19421 case PLUS_EXPR:
19422 do_plus:
19423 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19425 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19426 smarter to encode their opposite. The DW_OP_plus_uconst operation
19427 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19428 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19429 bytes, Y being the size of the operation that pushes the opposite
19430 of the addend. So let's choose the smallest representation. */
19431 const tree tree_addend = TREE_OPERAND (loc, 1);
19432 offset_int wi_addend;
19433 HOST_WIDE_INT shwi_addend;
19434 dw_loc_descr_ref loc_naddend;
19436 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19437 if (list_ret == 0)
19438 return 0;
19440 /* Try to get the literal to push. It is the opposite of the addend,
19441 so as we rely on wrapping during DWARF evaluation, first decode
19442 the literal as a "DWARF-sized" signed number. */
19443 wi_addend = wi::to_offset (tree_addend);
19444 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19445 shwi_addend = wi_addend.to_shwi ();
19446 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19447 ? int_loc_descriptor (-shwi_addend)
19448 : NULL;
19450 if (loc_naddend != NULL
19451 && ((unsigned) size_of_uleb128 (shwi_addend)
19452 > size_of_loc_descr (loc_naddend)))
19454 add_loc_descr_to_each (list_ret, loc_naddend);
19455 add_loc_descr_to_each (list_ret,
19456 new_loc_descr (DW_OP_minus, 0, 0));
19458 else
19460 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19462 loc_naddend = loc_cur;
19463 loc_cur = loc_cur->dw_loc_next;
19464 ggc_free (loc_naddend);
19466 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19468 break;
19471 op = DW_OP_plus;
19472 goto do_binop;
19474 case LE_EXPR:
19475 op = DW_OP_le;
19476 goto do_comp_binop;
19478 case GE_EXPR:
19479 op = DW_OP_ge;
19480 goto do_comp_binop;
19482 case LT_EXPR:
19483 op = DW_OP_lt;
19484 goto do_comp_binop;
19486 case GT_EXPR:
19487 op = DW_OP_gt;
19488 goto do_comp_binop;
19490 do_comp_binop:
19491 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19493 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19494 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19495 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19496 TREE_CODE (loc));
19497 break;
19499 else
19500 goto do_binop;
19502 case EQ_EXPR:
19503 op = DW_OP_eq;
19504 goto do_binop;
19506 case NE_EXPR:
19507 op = DW_OP_ne;
19508 goto do_binop;
19510 do_binop:
19511 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19512 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19513 if (list_ret == 0 || list_ret1 == 0)
19514 return 0;
19516 add_loc_list (&list_ret, list_ret1);
19517 if (list_ret == 0)
19518 return 0;
19519 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19520 break;
19522 case TRUTH_NOT_EXPR:
19523 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19524 if (list_ret == 0)
19525 return 0;
19527 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19528 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19529 break;
19531 case BIT_NOT_EXPR:
19532 op = DW_OP_not;
19533 goto do_unop;
19535 case ABS_EXPR:
19536 op = DW_OP_abs;
19537 goto do_unop;
19539 case NEGATE_EXPR:
19540 op = DW_OP_neg;
19541 goto do_unop;
19543 do_unop:
19544 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19545 if (list_ret == 0)
19546 return 0;
19548 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19549 break;
19551 case MIN_EXPR:
19552 case MAX_EXPR:
19554 const enum tree_code code =
19555 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19557 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19558 build2 (code, integer_type_node,
19559 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19560 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19563 /* fall through */
19565 case COND_EXPR:
19567 dw_loc_descr_ref lhs
19568 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19569 dw_loc_list_ref rhs
19570 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19571 dw_loc_descr_ref bra_node, jump_node, tmp;
19573 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19574 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19575 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19576 list_ret
19577 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19578 0, context);
19579 /* Likewise, swap the operands for a logically negated condition. */
19580 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19582 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19583 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19584 list_ret
19585 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19586 0, context);
19588 else
19589 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19590 if (list_ret == 0 || lhs == 0 || rhs == 0)
19591 return 0;
19593 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19594 add_loc_descr_to_each (list_ret, bra_node);
19596 add_loc_list (&list_ret, rhs);
19597 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19598 add_loc_descr_to_each (list_ret, jump_node);
19600 add_loc_descr_to_each (list_ret, lhs);
19601 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19602 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19604 /* ??? Need a node to point the skip at. Use a nop. */
19605 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19606 add_loc_descr_to_each (list_ret, tmp);
19607 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19608 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19610 break;
19612 case FIX_TRUNC_EXPR:
19613 return 0;
19615 case COMPOUND_LITERAL_EXPR:
19616 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19617 0, context);
19619 default:
19620 /* Leave front-end specific codes as simply unknown. This comes
19621 up, for instance, with the C STMT_EXPR. */
19622 if ((unsigned int) TREE_CODE (loc)
19623 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19625 expansion_failed (loc, NULL_RTX,
19626 "language specific tree node");
19627 return 0;
19630 /* Otherwise this is a generic code; we should just lists all of
19631 these explicitly. We forgot one. */
19632 if (flag_checking)
19633 gcc_unreachable ();
19635 /* In a release build, we want to degrade gracefully: better to
19636 generate incomplete debugging information than to crash. */
19637 return NULL;
19640 if (!ret && !list_ret)
19641 return 0;
19643 if (want_address == 2 && !have_address
19644 && (dwarf_version >= 4 || !dwarf_strict))
19646 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19648 expansion_failed (loc, NULL_RTX,
19649 "DWARF address size mismatch");
19650 return 0;
19652 if (ret)
19653 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19654 else
19655 add_loc_descr_to_each (list_ret,
19656 new_loc_descr (DW_OP_stack_value, 0, 0));
19657 have_address = 1;
19659 /* Show if we can't fill the request for an address. */
19660 if (want_address && !have_address)
19662 expansion_failed (loc, NULL_RTX,
19663 "Want address and only have value");
19664 return 0;
19667 gcc_assert (!ret || !list_ret);
19669 /* If we've got an address and don't want one, dereference. */
19670 if (!want_address && have_address)
19672 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19673 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19674 scalar_int_mode int_mode;
19675 dw_die_ref type_die;
19676 dw_loc_descr_ref deref;
19678 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19679 if (size > DWARF2_ADDR_SIZE || size == -1)
19681 expansion_failed (loc, NULL_RTX,
19682 "DWARF address size mismatch");
19683 return 0;
19686 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19687 else if (size == DWARF2_ADDR_SIZE)
19688 deref = new_loc_descr (DW_OP_deref, size, 0);
19690 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19691 extend the value, which is really OK for unsigned types only. */
19692 else if (!(context && context->strict_signedness)
19693 || TYPE_UNSIGNED (TREE_TYPE (loc))
19694 || (dwarf_strict && dwarf_version < 5)
19695 || !is_a <scalar_int_mode> (mode, &int_mode)
19696 || !(type_die = base_type_for_mode (mode, false)))
19697 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19699 /* Use DW_OP_deref_type for signed integral types if possible, but
19700 convert back to the generic type to avoid type mismatches later. */
19701 else
19703 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19704 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19705 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19706 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19707 add_loc_descr (&deref,
19708 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19711 if (ret)
19712 add_loc_descr (&ret, deref);
19713 else
19714 add_loc_descr_to_each (list_ret, deref);
19717 if (ret)
19718 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19720 return list_ret;
19723 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19724 expressions. */
19726 static dw_loc_list_ref
19727 loc_list_from_tree (tree loc, int want_address,
19728 struct loc_descr_context *context)
19730 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19732 for (dw_loc_list_ref loc_cur = result;
19733 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19734 loc_descr_without_nops (loc_cur->expr);
19735 return result;
19738 /* Same as above but return only single location expression. */
19739 static dw_loc_descr_ref
19740 loc_descriptor_from_tree (tree loc, int want_address,
19741 struct loc_descr_context *context)
19743 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19744 if (!ret)
19745 return NULL;
19746 if (ret->dw_loc_next)
19748 expansion_failed (loc, NULL_RTX,
19749 "Location list where only loc descriptor needed");
19750 return NULL;
19752 return ret->expr;
19755 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19756 pointer to the declared type for the relevant field variable, or return
19757 `integer_type_node' if the given node turns out to be an
19758 ERROR_MARK node. */
19760 static inline tree
19761 field_type (const_tree decl)
19763 tree type;
19765 if (TREE_CODE (decl) == ERROR_MARK)
19766 return integer_type_node;
19768 type = DECL_BIT_FIELD_TYPE (decl);
19769 if (type == NULL_TREE)
19770 type = TREE_TYPE (decl);
19772 return type;
19775 /* Given a pointer to a tree node, return the alignment in bits for
19776 it, or else return BITS_PER_WORD if the node actually turns out to
19777 be an ERROR_MARK node. */
19779 static inline unsigned
19780 simple_type_align_in_bits (const_tree type)
19782 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19785 static inline unsigned
19786 simple_decl_align_in_bits (const_tree decl)
19788 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19791 /* Return the result of rounding T up to ALIGN. */
19793 static inline offset_int
19794 round_up_to_align (const offset_int &t, unsigned int align)
19796 return wi::udiv_trunc (t + align - 1, align) * align;
19799 /* Helper structure for RECORD_TYPE processing. */
19800 struct vlr_context
19802 /* Root RECORD_TYPE. It is needed to generate data member location
19803 descriptions in variable-length records (VLR), but also to cope with
19804 variants, which are composed of nested structures multiplexed with
19805 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19806 function processing a FIELD_DECL, it is required to be non null. */
19807 tree struct_type;
19809 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19810 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19811 this variant part as part of the root record (in storage units). For
19812 regular records, it must be NULL_TREE. */
19813 tree variant_part_offset;
19816 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19817 addressed byte of the "containing object" for the given FIELD_DECL. If
19818 possible, return a native constant through CST_OFFSET (in which case NULL is
19819 returned); otherwise return a DWARF expression that computes the offset.
19821 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19822 that offset is, either because the argument turns out to be a pointer to an
19823 ERROR_MARK node, or because the offset expression is too complex for us.
19825 CTX is required: see the comment for VLR_CONTEXT. */
19827 static dw_loc_descr_ref
19828 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19829 HOST_WIDE_INT *cst_offset)
19831 tree tree_result;
19832 dw_loc_list_ref loc_result;
19834 *cst_offset = 0;
19836 if (TREE_CODE (decl) == ERROR_MARK)
19837 return NULL;
19838 else
19839 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19841 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19842 case. */
19843 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19844 return NULL;
19846 /* We used to handle only constant offsets in all cases. Now, we handle
19847 properly dynamic byte offsets only when PCC bitfield type doesn't
19848 matter. */
19849 if (PCC_BITFIELD_TYPE_MATTERS
19850 && DECL_BIT_FIELD_TYPE (decl)
19851 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19853 offset_int object_offset_in_bits;
19854 offset_int object_offset_in_bytes;
19855 offset_int bitpos_int;
19856 tree type;
19857 tree field_size_tree;
19858 offset_int deepest_bitpos;
19859 offset_int field_size_in_bits;
19860 unsigned int type_align_in_bits;
19861 unsigned int decl_align_in_bits;
19862 offset_int type_size_in_bits;
19864 bitpos_int = wi::to_offset (bit_position (decl));
19865 type = field_type (decl);
19866 type_size_in_bits = offset_int_type_size_in_bits (type);
19867 type_align_in_bits = simple_type_align_in_bits (type);
19869 field_size_tree = DECL_SIZE (decl);
19871 /* The size could be unspecified if there was an error, or for
19872 a flexible array member. */
19873 if (!field_size_tree)
19874 field_size_tree = bitsize_zero_node;
19876 /* If the size of the field is not constant, use the type size. */
19877 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19878 field_size_in_bits = wi::to_offset (field_size_tree);
19879 else
19880 field_size_in_bits = type_size_in_bits;
19882 decl_align_in_bits = simple_decl_align_in_bits (decl);
19884 /* The GCC front-end doesn't make any attempt to keep track of the
19885 starting bit offset (relative to the start of the containing
19886 structure type) of the hypothetical "containing object" for a
19887 bit-field. Thus, when computing the byte offset value for the
19888 start of the "containing object" of a bit-field, we must deduce
19889 this information on our own. This can be rather tricky to do in
19890 some cases. For example, handling the following structure type
19891 definition when compiling for an i386/i486 target (which only
19892 aligns long long's to 32-bit boundaries) can be very tricky:
19894 struct S { int field1; long long field2:31; };
19896 Fortunately, there is a simple rule-of-thumb which can be used
19897 in such cases. When compiling for an i386/i486, GCC will
19898 allocate 8 bytes for the structure shown above. It decides to
19899 do this based upon one simple rule for bit-field allocation.
19900 GCC allocates each "containing object" for each bit-field at
19901 the first (i.e. lowest addressed) legitimate alignment boundary
19902 (based upon the required minimum alignment for the declared
19903 type of the field) which it can possibly use, subject to the
19904 condition that there is still enough available space remaining
19905 in the containing object (when allocated at the selected point)
19906 to fully accommodate all of the bits of the bit-field itself.
19908 This simple rule makes it obvious why GCC allocates 8 bytes for
19909 each object of the structure type shown above. When looking
19910 for a place to allocate the "containing object" for `field2',
19911 the compiler simply tries to allocate a 64-bit "containing
19912 object" at each successive 32-bit boundary (starting at zero)
19913 until it finds a place to allocate that 64- bit field such that
19914 at least 31 contiguous (and previously unallocated) bits remain
19915 within that selected 64 bit field. (As it turns out, for the
19916 example above, the compiler finds it is OK to allocate the
19917 "containing object" 64-bit field at bit-offset zero within the
19918 structure type.)
19920 Here we attempt to work backwards from the limited set of facts
19921 we're given, and we try to deduce from those facts, where GCC
19922 must have believed that the containing object started (within
19923 the structure type). The value we deduce is then used (by the
19924 callers of this routine) to generate DW_AT_location and
19925 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19926 the case of DW_AT_location, regular fields as well). */
19928 /* Figure out the bit-distance from the start of the structure to
19929 the "deepest" bit of the bit-field. */
19930 deepest_bitpos = bitpos_int + field_size_in_bits;
19932 /* This is the tricky part. Use some fancy footwork to deduce
19933 where the lowest addressed bit of the containing object must
19934 be. */
19935 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19937 /* Round up to type_align by default. This works best for
19938 bitfields. */
19939 object_offset_in_bits
19940 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19942 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19944 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19946 /* Round up to decl_align instead. */
19947 object_offset_in_bits
19948 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19951 object_offset_in_bytes
19952 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19953 if (ctx->variant_part_offset == NULL_TREE)
19955 *cst_offset = object_offset_in_bytes.to_shwi ();
19956 return NULL;
19958 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19960 else
19961 tree_result = byte_position (decl);
19963 if (ctx->variant_part_offset != NULL_TREE)
19964 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19965 ctx->variant_part_offset, tree_result);
19967 /* If the byte offset is a constant, it's simplier to handle a native
19968 constant rather than a DWARF expression. */
19969 if (TREE_CODE (tree_result) == INTEGER_CST)
19971 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19972 return NULL;
19975 struct loc_descr_context loc_ctx = {
19976 ctx->struct_type, /* context_type */
19977 NULL_TREE, /* base_decl */
19978 NULL, /* dpi */
19979 false, /* placeholder_arg */
19980 false, /* placeholder_seen */
19981 false /* strict_signedness */
19983 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19985 /* We want a DWARF expression: abort if we only have a location list with
19986 multiple elements. */
19987 if (!loc_result || !single_element_loc_list_p (loc_result))
19988 return NULL;
19989 else
19990 return loc_result->expr;
19993 /* The following routines define various Dwarf attributes and any data
19994 associated with them. */
19996 /* Add a location description attribute value to a DIE.
19998 This emits location attributes suitable for whole variables and
19999 whole parameters. Note that the location attributes for struct fields are
20000 generated by the routine `data_member_location_attribute' below. */
20002 static inline void
20003 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
20004 dw_loc_list_ref descr)
20006 bool check_no_locviews = true;
20007 if (descr == 0)
20008 return;
20009 if (single_element_loc_list_p (descr))
20010 add_AT_loc (die, attr_kind, descr->expr);
20011 else
20013 add_AT_loc_list (die, attr_kind, descr);
20014 gcc_assert (descr->ll_symbol);
20015 if (attr_kind == DW_AT_location && descr->vl_symbol
20016 && dwarf2out_locviews_in_attribute ())
20018 add_AT_view_list (die, DW_AT_GNU_locviews);
20019 check_no_locviews = false;
20023 if (check_no_locviews)
20024 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
20027 /* Add DW_AT_accessibility attribute to DIE if needed. */
20029 static void
20030 add_accessibility_attribute (dw_die_ref die, tree decl)
20032 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20033 children, otherwise the default is DW_ACCESS_public. In DWARF2
20034 the default has always been DW_ACCESS_public. */
20035 if (TREE_PROTECTED (decl))
20036 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20037 else if (TREE_PRIVATE (decl))
20039 if (dwarf_version == 2
20040 || die->die_parent == NULL
20041 || die->die_parent->die_tag != DW_TAG_class_type)
20042 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20044 else if (dwarf_version > 2
20045 && die->die_parent
20046 && die->die_parent->die_tag == DW_TAG_class_type)
20047 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20050 /* Attach the specialized form of location attribute used for data members of
20051 struct and union types. In the special case of a FIELD_DECL node which
20052 represents a bit-field, the "offset" part of this special location
20053 descriptor must indicate the distance in bytes from the lowest-addressed
20054 byte of the containing struct or union type to the lowest-addressed byte of
20055 the "containing object" for the bit-field. (See the `field_byte_offset'
20056 function above).
20058 For any given bit-field, the "containing object" is a hypothetical object
20059 (of some integral or enum type) within which the given bit-field lives. The
20060 type of this hypothetical "containing object" is always the same as the
20061 declared type of the individual bit-field itself (for GCC anyway... the
20062 DWARF spec doesn't actually mandate this). Note that it is the size (in
20063 bytes) of the hypothetical "containing object" which will be given in the
20064 DW_AT_byte_size attribute for this bit-field. (See the
20065 `byte_size_attribute' function below.) It is also used when calculating the
20066 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20067 function below.)
20069 CTX is required: see the comment for VLR_CONTEXT. */
20071 static void
20072 add_data_member_location_attribute (dw_die_ref die,
20073 tree decl,
20074 struct vlr_context *ctx)
20076 HOST_WIDE_INT offset;
20077 dw_loc_descr_ref loc_descr = 0;
20079 if (TREE_CODE (decl) == TREE_BINFO)
20081 /* We're working on the TAG_inheritance for a base class. */
20082 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20084 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20085 aren't at a fixed offset from all (sub)objects of the same
20086 type. We need to extract the appropriate offset from our
20087 vtable. The following dwarf expression means
20089 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20091 This is specific to the V3 ABI, of course. */
20093 dw_loc_descr_ref tmp;
20095 /* Make a copy of the object address. */
20096 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20097 add_loc_descr (&loc_descr, tmp);
20099 /* Extract the vtable address. */
20100 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20101 add_loc_descr (&loc_descr, tmp);
20103 /* Calculate the address of the offset. */
20104 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20105 gcc_assert (offset < 0);
20107 tmp = int_loc_descriptor (-offset);
20108 add_loc_descr (&loc_descr, tmp);
20109 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20110 add_loc_descr (&loc_descr, tmp);
20112 /* Extract the offset. */
20113 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20114 add_loc_descr (&loc_descr, tmp);
20116 /* Add it to the object address. */
20117 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20118 add_loc_descr (&loc_descr, tmp);
20120 else
20121 offset = tree_to_shwi (BINFO_OFFSET (decl));
20123 else
20125 loc_descr = field_byte_offset (decl, ctx, &offset);
20127 if (!loc_descr)
20130 /* If loc_descr is available, then we know the offset is dynamic. */
20131 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20133 loc_descr = NULL;
20134 offset = 0;
20137 /* Data member location evaluation starts with the base address on the
20138 stack. Compute the field offset and add it to this base address. */
20139 else
20140 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20143 if (!loc_descr)
20145 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20146 e.g. GDB only added support to it in November 2016. For DWARF5
20147 we need newer debug info consumers anyway. We might change this
20148 to dwarf_version >= 4 once most consumers catched up. */
20149 if (dwarf_version >= 5
20150 && TREE_CODE (decl) == FIELD_DECL
20151 && DECL_BIT_FIELD_TYPE (decl)
20152 && (ctx->variant_part_offset == NULL_TREE
20153 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20155 tree off = bit_position (decl);
20156 if (ctx->variant_part_offset)
20157 off = bit_from_pos (ctx->variant_part_offset, off);
20158 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20160 remove_AT (die, DW_AT_byte_size);
20161 remove_AT (die, DW_AT_bit_offset);
20162 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20163 return;
20166 if (dwarf_version > 2)
20168 /* Don't need to output a location expression, just the constant. */
20169 if (offset < 0)
20170 add_AT_int (die, DW_AT_data_member_location, offset);
20171 else
20172 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20173 return;
20175 else
20177 enum dwarf_location_atom op;
20179 /* The DWARF2 standard says that we should assume that the structure
20180 address is already on the stack, so we can specify a structure
20181 field address by using DW_OP_plus_uconst. */
20182 op = DW_OP_plus_uconst;
20183 loc_descr = new_loc_descr (op, offset, 0);
20187 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20190 /* Writes integer values to dw_vec_const array. */
20192 static void
20193 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20195 while (size != 0)
20197 *dest++ = val & 0xff;
20198 val >>= 8;
20199 --size;
20203 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20205 static HOST_WIDE_INT
20206 extract_int (const unsigned char *src, unsigned int size)
20208 HOST_WIDE_INT val = 0;
20210 src += size;
20211 while (size != 0)
20213 val <<= 8;
20214 val |= *--src & 0xff;
20215 --size;
20217 return val;
20220 /* Writes wide_int values to dw_vec_const array. */
20222 static void
20223 insert_wide_int (const wide_int_ref &val, unsigned char *dest, int elt_size)
20225 int i;
20227 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20229 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20230 return;
20233 /* We'd have to extend this code to support odd sizes. */
20234 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20236 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20238 if (WORDS_BIG_ENDIAN)
20239 for (i = n - 1; i >= 0; i--)
20241 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20242 dest += sizeof (HOST_WIDE_INT);
20244 else
20245 for (i = 0; i < n; i++)
20247 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20248 dest += sizeof (HOST_WIDE_INT);
20252 /* Writes floating point values to dw_vec_const array. */
20254 static unsigned
20255 insert_float (const_rtx rtl, unsigned char *array)
20257 long val[4];
20258 int i;
20259 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20261 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20263 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20264 if (GET_MODE_SIZE (mode) < 4)
20266 gcc_assert (GET_MODE_SIZE (mode) == 2);
20267 insert_int (val[0], 2, array);
20268 return 2;
20271 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20273 insert_int (val[i], 4, array);
20274 array += 4;
20276 return 4;
20279 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20280 does not have a "location" either in memory or in a register. These
20281 things can arise in GNU C when a constant is passed as an actual parameter
20282 to an inlined function. They can also arise in C++ where declared
20283 constants do not necessarily get memory "homes". */
20285 static bool
20286 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20288 scalar_mode int_mode;
20290 switch (GET_CODE (rtl))
20292 case CONST_INT:
20294 HOST_WIDE_INT val = INTVAL (rtl);
20296 if (val < 0)
20297 add_AT_int (die, DW_AT_const_value, val);
20298 else
20299 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20301 return true;
20303 case CONST_WIDE_INT:
20304 if (is_int_mode (mode, &int_mode)
20305 && (GET_MODE_PRECISION (int_mode)
20306 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20308 add_AT_wide (die, DW_AT_const_value, rtx_mode_t (rtl, int_mode));
20309 return true;
20311 return false;
20313 case CONST_DOUBLE:
20314 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20315 floating-point constant. A CONST_DOUBLE is used whenever the
20316 constant requires more than one word in order to be adequately
20317 represented. */
20318 if (TARGET_SUPPORTS_WIDE_INT == 0
20319 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20320 add_AT_double (die, DW_AT_const_value,
20321 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20322 else
20324 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20325 unsigned int length = GET_MODE_SIZE (mode);
20326 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20327 unsigned int elt_size = insert_float (rtl, array);
20329 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20330 array);
20332 return true;
20334 case CONST_VECTOR:
20336 unsigned int length;
20337 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20338 return false;
20340 machine_mode mode = GET_MODE (rtl);
20341 /* The combination of a length and byte elt_size doesn't extend
20342 naturally to boolean vectors, where several elements are packed
20343 into the same byte. */
20344 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20345 return false;
20347 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20348 unsigned char *array
20349 = ggc_vec_alloc<unsigned char> (length * elt_size);
20350 unsigned int i;
20351 unsigned char *p;
20352 machine_mode imode = GET_MODE_INNER (mode);
20354 switch (GET_MODE_CLASS (mode))
20356 case MODE_VECTOR_INT:
20357 for (i = 0, p = array; i < length; i++, p += elt_size)
20359 rtx elt = CONST_VECTOR_ELT (rtl, i);
20360 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20362 break;
20364 case MODE_VECTOR_FLOAT:
20365 for (i = 0, p = array; i < length; i++, p += elt_size)
20367 rtx elt = CONST_VECTOR_ELT (rtl, i);
20368 insert_float (elt, p);
20370 break;
20372 default:
20373 gcc_unreachable ();
20376 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20378 return true;
20380 case CONST_STRING:
20381 if (dwarf_version >= 4 || !dwarf_strict)
20383 dw_loc_descr_ref loc_result;
20384 resolve_one_addr (&rtl);
20385 rtl_addr:
20386 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20387 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20388 add_AT_loc (die, DW_AT_location, loc_result);
20389 vec_safe_push (used_rtx_array, rtl);
20390 return true;
20392 return false;
20394 case CONST:
20395 if (CONSTANT_P (XEXP (rtl, 0)))
20396 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20397 /* FALLTHROUGH */
20398 case SYMBOL_REF:
20399 if (!const_ok_for_output (rtl))
20400 return false;
20401 /* FALLTHROUGH */
20402 case LABEL_REF:
20403 if (dwarf_version >= 4 || !dwarf_strict)
20404 goto rtl_addr;
20405 return false;
20407 case PLUS:
20408 /* In cases where an inlined instance of an inline function is passed
20409 the address of an `auto' variable (which is local to the caller) we
20410 can get a situation where the DECL_RTL of the artificial local
20411 variable (for the inlining) which acts as a stand-in for the
20412 corresponding formal parameter (of the inline function) will look
20413 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20414 exactly a compile-time constant expression, but it isn't the address
20415 of the (artificial) local variable either. Rather, it represents the
20416 *value* which the artificial local variable always has during its
20417 lifetime. We currently have no way to represent such quasi-constant
20418 values in Dwarf, so for now we just punt and generate nothing. */
20419 return false;
20421 case HIGH:
20422 case CONST_FIXED:
20423 case MINUS:
20424 case SIGN_EXTEND:
20425 case ZERO_EXTEND:
20426 case CONST_POLY_INT:
20427 return false;
20429 case MEM:
20430 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20431 && MEM_READONLY_P (rtl)
20432 && GET_MODE (rtl) == BLKmode)
20434 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20435 return true;
20437 return false;
20439 default:
20440 /* No other kinds of rtx should be possible here. */
20441 gcc_unreachable ();
20445 /* Determine whether the evaluation of EXPR references any variables
20446 or functions which aren't otherwise used (and therefore may not be
20447 output). */
20448 static tree
20449 reference_to_unused (tree * tp, int * walk_subtrees,
20450 void * data ATTRIBUTE_UNUSED)
20452 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20453 *walk_subtrees = 0;
20455 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20456 && ! TREE_ASM_WRITTEN (*tp))
20457 return *tp;
20458 /* ??? The C++ FE emits debug information for using decls, so
20459 putting gcc_unreachable here falls over. See PR31899. For now
20460 be conservative. */
20461 else if (!symtab->global_info_ready && VAR_P (*tp))
20462 return *tp;
20463 else if (VAR_P (*tp))
20465 varpool_node *node = varpool_node::get (*tp);
20466 if (!node || !node->definition)
20467 return *tp;
20469 else if (TREE_CODE (*tp) == FUNCTION_DECL
20470 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20472 /* The call graph machinery must have finished analyzing,
20473 optimizing and gimplifying the CU by now.
20474 So if *TP has no call graph node associated
20475 to it, it means *TP will not be emitted. */
20476 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20477 return *tp;
20479 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20480 return *tp;
20482 return NULL_TREE;
20485 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20486 for use in a later add_const_value_attribute call. */
20488 static rtx
20489 rtl_for_decl_init (tree init, tree type)
20491 rtx rtl = NULL_RTX;
20493 STRIP_NOPS (init);
20495 /* If a variable is initialized with a string constant without embedded
20496 zeros, build CONST_STRING. */
20497 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20499 tree enttype = TREE_TYPE (type);
20500 tree domain = TYPE_DOMAIN (type);
20501 scalar_int_mode mode;
20503 if (is_int_mode (TYPE_MODE (enttype), &mode)
20504 && GET_MODE_SIZE (mode) == 1
20505 && domain
20506 && TYPE_MAX_VALUE (domain)
20507 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20508 && integer_zerop (TYPE_MIN_VALUE (domain))
20509 && compare_tree_int (TYPE_MAX_VALUE (domain),
20510 TREE_STRING_LENGTH (init) - 1) == 0
20511 && ((size_t) TREE_STRING_LENGTH (init)
20512 == strlen (TREE_STRING_POINTER (init)) + 1))
20514 rtl = gen_rtx_CONST_STRING (VOIDmode,
20515 ggc_strdup (TREE_STRING_POINTER (init)));
20516 rtl = gen_rtx_MEM (BLKmode, rtl);
20517 MEM_READONLY_P (rtl) = 1;
20520 /* Other aggregates, and complex values, could be represented using
20521 CONCAT: FIXME!
20522 If this changes, please adjust tree_add_const_value_attribute
20523 so that for early_dwarf it will for such initializers mangle referenced
20524 decls. */
20525 else if (AGGREGATE_TYPE_P (type)
20526 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20527 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20528 || TREE_CODE (type) == COMPLEX_TYPE)
20530 /* Vectors only work if their mode is supported by the target.
20531 FIXME: generic vectors ought to work too. */
20532 else if (TREE_CODE (type) == VECTOR_TYPE
20533 && !VECTOR_MODE_P (TYPE_MODE (type)))
20535 /* If the initializer is something that we know will expand into an
20536 immediate RTL constant, expand it now. We must be careful not to
20537 reference variables which won't be output. */
20538 else if (initializer_constant_valid_p (init, type)
20539 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20541 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20542 possible. */
20543 if (TREE_CODE (type) == VECTOR_TYPE)
20544 switch (TREE_CODE (init))
20546 case VECTOR_CST:
20547 break;
20548 case CONSTRUCTOR:
20549 if (TREE_CONSTANT (init))
20551 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20552 bool constant_p = true;
20553 tree value;
20554 unsigned HOST_WIDE_INT ix;
20556 /* Even when ctor is constant, it might contain non-*_CST
20557 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20558 belong into VECTOR_CST nodes. */
20559 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20560 if (!CONSTANT_CLASS_P (value))
20562 constant_p = false;
20563 break;
20566 if (constant_p)
20568 init = build_vector_from_ctor (type, elts);
20569 break;
20572 /* FALLTHRU */
20574 default:
20575 return NULL;
20578 /* Large _BitInt BLKmode INTEGER_CSTs would yield a MEM. */
20579 if (TREE_CODE (init) == INTEGER_CST
20580 && TREE_CODE (TREE_TYPE (init)) == BITINT_TYPE
20581 && TYPE_MODE (TREE_TYPE (init)) == BLKmode)
20583 if (tree_fits_shwi_p (init))
20584 return GEN_INT (tree_to_shwi (init));
20585 else
20586 return NULL;
20589 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20591 /* If expand_expr returns a MEM, it wasn't immediate. */
20592 gcc_assert (!rtl || !MEM_P (rtl));
20595 return rtl;
20598 /* Generate RTL for the variable DECL to represent its location. */
20600 static rtx
20601 rtl_for_decl_location (tree decl)
20603 rtx rtl;
20605 /* Here we have to decide where we are going to say the parameter "lives"
20606 (as far as the debugger is concerned). We only have a couple of
20607 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20609 DECL_RTL normally indicates where the parameter lives during most of the
20610 activation of the function. If optimization is enabled however, this
20611 could be either NULL or else a pseudo-reg. Both of those cases indicate
20612 that the parameter doesn't really live anywhere (as far as the code
20613 generation parts of GCC are concerned) during most of the function's
20614 activation. That will happen (for example) if the parameter is never
20615 referenced within the function.
20617 We could just generate a location descriptor here for all non-NULL
20618 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20619 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20620 where DECL_RTL is NULL or is a pseudo-reg.
20622 Note however that we can only get away with using DECL_INCOMING_RTL as
20623 a backup substitute for DECL_RTL in certain limited cases. In cases
20624 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20625 we can be sure that the parameter was passed using the same type as it is
20626 declared to have within the function, and that its DECL_INCOMING_RTL
20627 points us to a place where a value of that type is passed.
20629 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20630 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20631 because in these cases DECL_INCOMING_RTL points us to a value of some
20632 type which is *different* from the type of the parameter itself. Thus,
20633 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20634 such cases, the debugger would end up (for example) trying to fetch a
20635 `float' from a place which actually contains the first part of a
20636 `double'. That would lead to really incorrect and confusing
20637 output at debug-time.
20639 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20640 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20641 are a couple of exceptions however. On little-endian machines we can
20642 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20643 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20644 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20645 when (on a little-endian machine) a non-prototyped function has a
20646 parameter declared to be of type `short' or `char'. In such cases,
20647 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20648 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20649 passed `int' value. If the debugger then uses that address to fetch
20650 a `short' or a `char' (on a little-endian machine) the result will be
20651 the correct data, so we allow for such exceptional cases below.
20653 Note that our goal here is to describe the place where the given formal
20654 parameter lives during most of the function's activation (i.e. between the
20655 end of the prologue and the start of the epilogue). We'll do that as best
20656 as we can. Note however that if the given formal parameter is modified
20657 sometime during the execution of the function, then a stack backtrace (at
20658 debug-time) will show the function as having been called with the *new*
20659 value rather than the value which was originally passed in. This happens
20660 rarely enough that it is not a major problem, but it *is* a problem, and
20661 I'd like to fix it.
20663 A future version of dwarf2out.cc may generate two additional attributes for
20664 any given DW_TAG_formal_parameter DIE which will describe the "passed
20665 type" and the "passed location" for the given formal parameter in addition
20666 to the attributes we now generate to indicate the "declared type" and the
20667 "active location" for each parameter. This additional set of attributes
20668 could be used by debuggers for stack backtraces. Separately, note that
20669 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20670 This happens (for example) for inlined-instances of inline function formal
20671 parameters which are never referenced. This really shouldn't be
20672 happening. All PARM_DECL nodes should get valid non-NULL
20673 DECL_INCOMING_RTL values. FIXME. */
20675 /* Use DECL_RTL as the "location" unless we find something better. */
20676 rtl = DECL_RTL_IF_SET (decl);
20678 /* When generating abstract instances, ignore everything except
20679 constants, symbols living in memory, and symbols living in
20680 fixed registers. */
20681 if (! reload_completed)
20683 if (rtl
20684 && (CONSTANT_P (rtl)
20685 || (MEM_P (rtl)
20686 && CONSTANT_P (XEXP (rtl, 0)))
20687 || (REG_P (rtl)
20688 && VAR_P (decl)
20689 && TREE_STATIC (decl))))
20691 rtl = targetm.delegitimize_address (rtl);
20692 return rtl;
20694 rtl = NULL_RTX;
20696 else if (TREE_CODE (decl) == PARM_DECL)
20698 if (rtl == NULL_RTX
20699 || is_pseudo_reg (rtl)
20700 || (MEM_P (rtl)
20701 && is_pseudo_reg (XEXP (rtl, 0))
20702 && DECL_INCOMING_RTL (decl)
20703 && MEM_P (DECL_INCOMING_RTL (decl))
20704 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20706 tree declared_type = TREE_TYPE (decl);
20707 tree passed_type = DECL_ARG_TYPE (decl);
20708 machine_mode dmode = TYPE_MODE (declared_type);
20709 machine_mode pmode = TYPE_MODE (passed_type);
20711 /* This decl represents a formal parameter which was optimized out.
20712 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20713 all cases where (rtl == NULL_RTX) just below. */
20714 if (dmode == pmode)
20715 rtl = DECL_INCOMING_RTL (decl);
20716 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20717 && SCALAR_INT_MODE_P (dmode)
20718 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20719 && DECL_INCOMING_RTL (decl))
20721 rtx inc = DECL_INCOMING_RTL (decl);
20722 if (REG_P (inc))
20723 rtl = inc;
20724 else if (MEM_P (inc))
20726 if (BYTES_BIG_ENDIAN)
20727 rtl = adjust_address_nv (inc, dmode,
20728 GET_MODE_SIZE (pmode)
20729 - GET_MODE_SIZE (dmode));
20730 else
20731 rtl = inc;
20736 /* If the parm was passed in registers, but lives on the stack, then
20737 make a big endian correction if the mode of the type of the
20738 parameter is not the same as the mode of the rtl. */
20739 /* ??? This is the same series of checks that are made in dbxout.cc before
20740 we reach the big endian correction code there. It isn't clear if all
20741 of these checks are necessary here, but keeping them all is the safe
20742 thing to do. */
20743 else if (MEM_P (rtl)
20744 && XEXP (rtl, 0) != const0_rtx
20745 && ! CONSTANT_P (XEXP (rtl, 0))
20746 /* Not passed in memory. */
20747 && !MEM_P (DECL_INCOMING_RTL (decl))
20748 /* Not passed by invisible reference. */
20749 && (!REG_P (XEXP (rtl, 0))
20750 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20751 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20752 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20753 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20754 #endif
20756 /* Big endian correction check. */
20757 && BYTES_BIG_ENDIAN
20758 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20759 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20760 UNITS_PER_WORD))
20762 machine_mode addr_mode = get_address_mode (rtl);
20763 poly_int64 offset = (UNITS_PER_WORD
20764 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20766 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20767 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20770 else if (VAR_P (decl)
20771 && rtl
20772 && MEM_P (rtl)
20773 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20775 machine_mode addr_mode = get_address_mode (rtl);
20776 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20777 GET_MODE (rtl));
20779 /* If a variable is declared "register" yet is smaller than
20780 a register, then if we store the variable to memory, it
20781 looks like we're storing a register-sized value, when in
20782 fact we are not. We need to adjust the offset of the
20783 storage location to reflect the actual value's bytes,
20784 else gdb will not be able to display it. */
20785 if (maybe_ne (offset, 0))
20786 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20787 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20790 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20791 and will have been substituted directly into all expressions that use it.
20792 C does not have such a concept, but C++ and other languages do. */
20793 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20794 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20796 if (rtl)
20797 rtl = targetm.delegitimize_address (rtl);
20799 /* If we don't look past the constant pool, we risk emitting a
20800 reference to a constant pool entry that isn't referenced from
20801 code, and thus is not emitted. */
20802 if (rtl)
20803 rtl = avoid_constant_pool_reference (rtl);
20805 /* Try harder to get a rtl. If this symbol ends up not being emitted
20806 in the current CU, resolve_addr will remove the expression referencing
20807 it. */
20808 if (rtl == NULL_RTX
20809 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20810 && VAR_P (decl)
20811 && !DECL_EXTERNAL (decl)
20812 && TREE_STATIC (decl)
20813 && DECL_NAME (decl)
20814 && !DECL_HARD_REGISTER (decl)
20815 && DECL_MODE (decl) != VOIDmode)
20817 rtl = make_decl_rtl_for_debug (decl);
20818 if (!MEM_P (rtl)
20819 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20820 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20821 rtl = NULL_RTX;
20824 return rtl;
20827 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20828 returned. If so, the decl for the COMMON block is returned, and the
20829 value is the offset into the common block for the symbol. */
20831 static tree
20832 fortran_common (tree decl, HOST_WIDE_INT *value)
20834 tree val_expr, cvar;
20835 machine_mode mode;
20836 poly_int64 bitsize, bitpos;
20837 tree offset;
20838 HOST_WIDE_INT cbitpos;
20839 int unsignedp, reversep, volatilep = 0;
20841 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20842 it does not have a value (the offset into the common area), or if it
20843 is thread local (as opposed to global) then it isn't common, and shouldn't
20844 be handled as such. */
20845 if (!VAR_P (decl)
20846 || !TREE_STATIC (decl)
20847 || !DECL_HAS_VALUE_EXPR_P (decl)
20848 || !is_fortran ())
20849 return NULL_TREE;
20851 val_expr = DECL_VALUE_EXPR (decl);
20852 if (TREE_CODE (val_expr) != COMPONENT_REF)
20853 return NULL_TREE;
20855 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20856 &unsignedp, &reversep, &volatilep);
20858 if (cvar == NULL_TREE
20859 || !VAR_P (cvar)
20860 || DECL_ARTIFICIAL (cvar)
20861 || !TREE_PUBLIC (cvar)
20862 /* We don't expect to have to cope with variable offsets,
20863 since at present all static data must have a constant size. */
20864 || !bitpos.is_constant (&cbitpos))
20865 return NULL_TREE;
20867 *value = 0;
20868 if (offset != NULL)
20870 if (!tree_fits_shwi_p (offset))
20871 return NULL_TREE;
20872 *value = tree_to_shwi (offset);
20874 if (cbitpos != 0)
20875 *value += cbitpos / BITS_PER_UNIT;
20877 return cvar;
20880 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20881 data attribute for a variable or a parameter. We generate the
20882 DW_AT_const_value attribute only in those cases where the given variable
20883 or parameter does not have a true "location" either in memory or in a
20884 register. This can happen (for example) when a constant is passed as an
20885 actual argument in a call to an inline function. (It's possible that
20886 these things can crop up in other ways also.) Note that one type of
20887 constant value which can be passed into an inlined function is a constant
20888 pointer. This can happen for example if an actual argument in an inlined
20889 function call evaluates to a compile-time constant address.
20891 CACHE_P is true if it is worth caching the location list for DECL,
20892 so that future calls can reuse it rather than regenerate it from scratch.
20893 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20894 since we will need to refer to them each time the function is inlined. */
20896 static bool
20897 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20899 rtx rtl;
20900 dw_loc_list_ref list;
20901 var_loc_list *loc_list;
20902 cached_dw_loc_list *cache;
20904 if (early_dwarf)
20905 return false;
20907 if (TREE_CODE (decl) == ERROR_MARK)
20908 return false;
20910 if (get_AT (die, DW_AT_location)
20911 || get_AT (die, DW_AT_const_value))
20912 return true;
20914 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20915 || TREE_CODE (decl) == RESULT_DECL);
20917 /* Try to get some constant RTL for this decl, and use that as the value of
20918 the location. */
20920 rtl = rtl_for_decl_location (decl);
20921 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20922 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20923 return true;
20925 /* See if we have single element location list that is equivalent to
20926 a constant value. That way we are better to use add_const_value_attribute
20927 rather than expanding constant value equivalent. */
20928 loc_list = lookup_decl_loc (decl);
20929 if (loc_list
20930 && loc_list->first
20931 && loc_list->first->next == NULL
20932 && NOTE_P (loc_list->first->loc)
20933 && NOTE_VAR_LOCATION (loc_list->first->loc)
20934 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20936 struct var_loc_node *node;
20938 node = loc_list->first;
20939 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20940 if (GET_CODE (rtl) == EXPR_LIST)
20941 rtl = XEXP (rtl, 0);
20942 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20943 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20944 return true;
20946 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20947 list several times. See if we've already cached the contents. */
20948 list = NULL;
20949 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20950 cache_p = false;
20951 if (cache_p)
20953 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20954 if (cache)
20955 list = cache->loc_list;
20957 if (list == NULL)
20959 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20960 NULL);
20961 /* It is usually worth caching this result if the decl is from
20962 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20963 if (cache_p && list && list->dw_loc_next)
20965 cached_dw_loc_list **slot
20966 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20967 DECL_UID (decl),
20968 INSERT);
20969 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20970 cache->decl_id = DECL_UID (decl);
20971 cache->loc_list = list;
20972 *slot = cache;
20975 if (list)
20977 add_AT_location_description (die, DW_AT_location, list);
20978 return true;
20980 /* None of that worked, so it must not really have a location;
20981 try adding a constant value attribute from the DECL_INITIAL. */
20982 return tree_add_const_value_attribute_for_decl (die, decl);
20985 /* Mangle referenced decls. */
20986 static tree
20987 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
20989 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20990 *walk_subtrees = 0;
20992 if (VAR_OR_FUNCTION_DECL_P (*tp))
20993 assign_assembler_name_if_needed (*tp);
20995 return NULL_TREE;
20998 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20999 attribute is the const value T. */
21001 static bool
21002 tree_add_const_value_attribute (dw_die_ref die, tree t)
21004 tree init;
21005 tree type = TREE_TYPE (t);
21007 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
21008 return false;
21010 init = t;
21011 gcc_assert (!DECL_P (init));
21013 if (TREE_CODE (init) == INTEGER_CST)
21015 if (tree_fits_uhwi_p (init))
21017 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
21018 return true;
21020 if (tree_fits_shwi_p (init))
21022 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
21023 return true;
21026 if (!early_dwarf)
21028 rtx rtl = rtl_for_decl_init (init, type);
21029 if (rtl)
21030 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
21032 else
21034 /* For early_dwarf force mangling of all referenced symbols. */
21035 tree initializer = init;
21036 STRIP_NOPS (initializer);
21037 /* rtl_for_decl_init punts on other aggregates, and complex values. */
21038 if (AGGREGATE_TYPE_P (type)
21039 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
21040 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
21041 || TREE_CODE (type) == COMPLEX_TYPE)
21043 else if (initializer_constant_valid_p (initializer, type))
21044 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
21046 /* If the host and target are sane, try harder. */
21047 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
21048 && initializer_constant_valid_p (init, type))
21050 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
21051 if (size > 0 && (int) size == size)
21053 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
21055 if (native_encode_initializer (init, array, size) == size)
21057 add_AT_vec (die, DW_AT_const_value, size, 1, array);
21058 return true;
21060 ggc_free (array);
21063 return false;
21066 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21067 attribute is the const value of T, where T is an integral constant
21068 variable with static storage duration
21069 (so it can't be a PARM_DECL or a RESULT_DECL). */
21071 static bool
21072 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21075 if (!decl
21076 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21077 || (VAR_P (decl) && !TREE_STATIC (decl)))
21078 return false;
21080 if (TREE_READONLY (decl)
21081 && ! TREE_THIS_VOLATILE (decl)
21082 && DECL_INITIAL (decl))
21083 /* OK */;
21084 else
21085 return false;
21087 /* Don't add DW_AT_const_value if abstract origin already has one. */
21088 if (get_AT (var_die, DW_AT_const_value))
21089 return false;
21091 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21094 /* Convert the CFI instructions for the current function into a
21095 location list. This is used for DW_AT_frame_base when we targeting
21096 a dwarf2 consumer that does not support the dwarf3
21097 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21098 expressions. */
21100 static dw_loc_list_ref
21101 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21103 int ix;
21104 dw_fde_ref fde;
21105 dw_loc_list_ref list, *list_tail;
21106 dw_cfi_ref cfi;
21107 dw_cfa_location last_cfa, next_cfa;
21108 const char *start_label, *last_label, *section;
21109 dw_cfa_location remember;
21111 fde = cfun->fde;
21112 gcc_assert (fde != NULL);
21114 section = secname_for_decl (current_function_decl);
21115 list_tail = &list;
21116 list = NULL;
21118 memset (&next_cfa, 0, sizeof (next_cfa));
21119 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21120 remember = next_cfa;
21122 start_label = fde->dw_fde_begin;
21124 /* ??? Bald assumption that the CIE opcode list does not contain
21125 advance opcodes. */
21126 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21127 lookup_cfa_1 (cfi, &next_cfa, &remember);
21129 last_cfa = next_cfa;
21130 last_label = start_label;
21132 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21134 /* If the first partition contained no CFI adjustments, the
21135 CIE opcodes apply to the whole first partition. */
21136 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21137 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21138 list_tail =&(*list_tail)->dw_loc_next;
21139 start_label = last_label = fde->dw_fde_second_begin;
21142 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21144 switch (cfi->dw_cfi_opc)
21146 case DW_CFA_set_loc:
21147 case DW_CFA_advance_loc1:
21148 case DW_CFA_advance_loc2:
21149 case DW_CFA_advance_loc4:
21150 if (!cfa_equal_p (&last_cfa, &next_cfa))
21152 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21153 start_label, 0, last_label, 0, section);
21155 list_tail = &(*list_tail)->dw_loc_next;
21156 last_cfa = next_cfa;
21157 start_label = last_label;
21159 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21160 break;
21162 case DW_CFA_advance_loc:
21163 /* The encoding is complex enough that we should never emit this. */
21164 gcc_unreachable ();
21166 default:
21167 lookup_cfa_1 (cfi, &next_cfa, &remember);
21168 break;
21170 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21172 if (!cfa_equal_p (&last_cfa, &next_cfa))
21174 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21175 start_label, 0, last_label, 0, section);
21177 list_tail = &(*list_tail)->dw_loc_next;
21178 last_cfa = next_cfa;
21179 start_label = last_label;
21181 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21182 start_label, 0, fde->dw_fde_end, 0, section);
21183 list_tail = &(*list_tail)->dw_loc_next;
21184 start_label = last_label = fde->dw_fde_second_begin;
21188 if (!cfa_equal_p (&last_cfa, &next_cfa))
21190 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21191 start_label, 0, last_label, 0, section);
21192 list_tail = &(*list_tail)->dw_loc_next;
21193 start_label = last_label;
21196 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21197 start_label, 0,
21198 fde->dw_fde_second_begin
21199 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21200 section);
21202 maybe_gen_llsym (list);
21204 return list;
21207 /* Compute a displacement from the "steady-state frame pointer" to the
21208 frame base (often the same as the CFA), and store it in
21209 frame_pointer_fb_offset. OFFSET is added to the displacement
21210 before the latter is negated. */
21212 static void
21213 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21215 rtx reg, elim;
21217 #ifdef FRAME_POINTER_CFA_OFFSET
21218 reg = frame_pointer_rtx;
21219 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21220 #else
21221 reg = arg_pointer_rtx;
21222 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21223 #endif
21225 elim = (ira_use_lra_p
21226 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21227 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21228 elim = strip_offset_and_add (elim, &offset);
21230 frame_pointer_fb_offset = -offset;
21232 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21233 in which to eliminate. This is because it's stack pointer isn't
21234 directly accessible as a register within the ISA. To work around
21235 this, assume that while we cannot provide a proper value for
21236 frame_pointer_fb_offset, we won't need one either. We can use
21237 hard frame pointer in debug info even if frame pointer isn't used
21238 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21239 which uses the DW_AT_frame_base attribute, not hard frame pointer
21240 directly. */
21241 frame_pointer_fb_offset_valid
21242 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21245 /* Generate a DW_AT_name attribute given some string value to be included as
21246 the value of the attribute. */
21248 void
21249 add_name_attribute (dw_die_ref die, const char *name_string)
21251 if (name_string != NULL && *name_string != 0)
21253 if (demangle_name_func)
21254 name_string = (*demangle_name_func) (name_string);
21256 add_AT_string (die, DW_AT_name, name_string);
21260 /* Generate a DW_AT_name attribute given some string value representing a
21261 file or filepath to be included as value of the attribute. */
21262 static void
21263 add_filename_attribute (dw_die_ref die, const char *name_string)
21265 if (name_string != NULL && *name_string != 0)
21266 add_filepath_AT_string (die, DW_AT_name, name_string);
21269 /* Generate a DW_AT_description attribute given some string value to be included
21270 as the value of the attribute. */
21272 static void
21273 add_desc_attribute (dw_die_ref die, const char *name_string)
21275 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21276 return;
21278 if (name_string == NULL || *name_string == 0)
21279 return;
21281 if (demangle_name_func)
21282 name_string = (*demangle_name_func) (name_string);
21284 add_AT_string (die, DW_AT_description, name_string);
21287 /* Generate a DW_AT_description attribute given some decl to be included
21288 as the value of the attribute. */
21290 static void
21291 add_desc_attribute (dw_die_ref die, tree decl)
21293 tree decl_name;
21295 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21296 return;
21298 if (decl == NULL_TREE || !DECL_P (decl))
21299 return;
21300 decl_name = DECL_NAME (decl);
21302 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21304 const char *name = dwarf2_name (decl, 0);
21305 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21307 else
21309 char *desc = print_generic_expr_to_str (decl);
21310 add_desc_attribute (die, desc);
21311 free (desc);
21315 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21316 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21317 of TYPE accordingly.
21319 ??? This is a temporary measure until after we're able to generate
21320 regular DWARF for the complex Ada type system. */
21322 static void
21323 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21324 dw_die_ref context_die)
21326 tree dtype;
21327 dw_die_ref dtype_die;
21329 if (!lang_hooks.types.descriptive_type)
21330 return;
21332 dtype = lang_hooks.types.descriptive_type (type);
21333 if (!dtype)
21334 return;
21336 dtype_die = lookup_type_die (dtype);
21337 if (!dtype_die)
21339 gen_type_die (dtype, context_die);
21340 dtype_die = lookup_type_die (dtype);
21341 gcc_assert (dtype_die);
21344 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21347 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21349 static const char *
21350 comp_dir_string (void)
21352 const char *wd;
21353 char *wd_plus_sep = NULL;
21354 static const char *cached_wd = NULL;
21356 if (cached_wd != NULL)
21357 return cached_wd;
21359 wd = get_src_pwd ();
21360 if (wd == NULL)
21361 return NULL;
21363 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21365 size_t wdlen = strlen (wd);
21366 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21367 strcpy (wd_plus_sep, wd);
21368 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21369 wd_plus_sep [wdlen + 1] = 0;
21370 wd = wd_plus_sep;
21373 cached_wd = remap_debug_filename (wd);
21375 /* remap_debug_filename can just pass through wd or return a new gc string.
21376 These two types can't be both stored in a GTY(())-tagged string, but since
21377 the cached value lives forever just copy it if needed. */
21378 if (cached_wd != wd)
21380 cached_wd = xstrdup (cached_wd);
21381 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21382 free (wd_plus_sep);
21385 return cached_wd;
21388 /* Generate a DW_AT_comp_dir attribute for DIE. */
21390 static void
21391 add_comp_dir_attribute (dw_die_ref die)
21393 const char * wd = comp_dir_string ();
21394 if (wd != NULL)
21395 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21398 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21399 pointer computation, ...), output a representation for that bound according
21400 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21401 loc_list_from_tree for the meaning of CONTEXT. */
21403 static void
21404 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21405 int forms, struct loc_descr_context *context)
21407 dw_die_ref context_die, decl_die = NULL;
21408 dw_loc_list_ref list;
21409 bool strip_conversions = true;
21410 bool placeholder_seen = false;
21412 while (strip_conversions)
21413 switch (TREE_CODE (value))
21415 case ERROR_MARK:
21416 case SAVE_EXPR:
21417 return;
21419 CASE_CONVERT:
21420 case VIEW_CONVERT_EXPR:
21421 value = TREE_OPERAND (value, 0);
21422 break;
21424 default:
21425 strip_conversions = false;
21426 break;
21429 /* If possible and permitted, output the attribute as a constant. */
21430 if ((forms & dw_scalar_form_constant) != 0
21431 && TREE_CODE (value) == INTEGER_CST)
21433 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21435 /* If HOST_WIDE_INT is big enough then represent the bound as
21436 a constant value. We need to choose a form based on
21437 whether the type is signed or unsigned. We cannot just
21438 call add_AT_unsigned if the value itself is positive
21439 (add_AT_unsigned might add the unsigned value encoded as
21440 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21441 bounds type and then sign extend any unsigned values found
21442 for signed types. This is needed only for
21443 DW_AT_{lower,upper}_bound, since for most other attributes,
21444 consumers will treat DW_FORM_data[1248] as unsigned values,
21445 regardless of the underlying type. */
21446 if (prec <= HOST_BITS_PER_WIDE_INT
21447 || tree_fits_uhwi_p (value))
21449 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21450 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21451 else
21452 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21454 else if (dwarf_version >= 5
21455 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21456 /* Otherwise represent the bound as an unsigned value with
21457 the precision of its type. The precision and signedness
21458 of the type will be necessary to re-interpret it
21459 unambiguously. */
21460 add_AT_wide (die, attr, wi::to_wide (value));
21461 else
21463 rtx v = immed_wide_int_const (wi::to_wide (value),
21464 TYPE_MODE (TREE_TYPE (value)));
21465 dw_loc_descr_ref loc
21466 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21467 VAR_INIT_STATUS_INITIALIZED);
21468 if (loc)
21469 add_AT_loc (die, attr, loc);
21471 return;
21474 /* Otherwise, if it's possible and permitted too, output a reference to
21475 another DIE. */
21476 if ((forms & dw_scalar_form_reference) != 0)
21478 tree decl = NULL_TREE;
21480 /* Some type attributes reference an outer type. For instance, the upper
21481 bound of an array may reference an embedding record (this happens in
21482 Ada). */
21483 if (TREE_CODE (value) == COMPONENT_REF
21484 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21485 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21486 decl = TREE_OPERAND (value, 1);
21488 else if (VAR_P (value)
21489 || TREE_CODE (value) == PARM_DECL
21490 || TREE_CODE (value) == RESULT_DECL)
21491 decl = value;
21493 if (decl != NULL_TREE)
21495 decl_die = lookup_decl_die (decl);
21497 /* ??? Can this happen, or should the variable have been bound
21498 first? Probably it can, since I imagine that we try to create
21499 the types of parameters in the order in which they exist in
21500 the list, and won't have created a forward reference to a
21501 later parameter. */
21502 if (decl_die != NULL)
21504 if (get_AT (decl_die, DW_AT_location)
21505 || get_AT (decl_die, DW_AT_data_member_location)
21506 || get_AT (decl_die, DW_AT_data_bit_offset)
21507 || get_AT (decl_die, DW_AT_const_value))
21509 add_AT_die_ref (die, attr, decl_die);
21510 return;
21516 /* Last chance: try to create a stack operation procedure to evaluate the
21517 value. Do nothing if even that is not possible or permitted. */
21518 if ((forms & dw_scalar_form_exprloc) == 0)
21519 return;
21521 list = loc_list_from_tree (value, 2, context);
21522 if (context && context->placeholder_arg)
21524 placeholder_seen = context->placeholder_seen;
21525 context->placeholder_seen = false;
21527 if (list == NULL || single_element_loc_list_p (list))
21529 /* If this attribute is not a reference nor constant, it is
21530 a DWARF expression rather than location description. For that
21531 loc_list_from_tree (value, 0, &context) is needed. */
21532 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21533 if (list2 && single_element_loc_list_p (list2))
21535 if (placeholder_seen)
21537 struct dwarf_procedure_info dpi;
21538 dpi.fndecl = NULL_TREE;
21539 dpi.args_count = 1;
21540 if (!resolve_args_picking (list2->expr, 1, &dpi))
21541 return;
21543 add_AT_loc (die, attr, list2->expr);
21544 return;
21548 /* If that failed to give a single element location list, fall back to
21549 outputting this as a reference... still if permitted. */
21550 if (list == NULL
21551 || (forms & dw_scalar_form_reference) == 0
21552 || placeholder_seen)
21553 return;
21555 if (!decl_die)
21557 if (current_function_decl == 0)
21558 context_die = comp_unit_die ();
21559 else
21560 context_die = lookup_decl_die (current_function_decl);
21562 decl_die = new_die (DW_TAG_variable, context_die, value);
21563 add_AT_flag (decl_die, DW_AT_artificial, 1);
21564 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21565 context_die);
21568 add_AT_location_description (decl_die, DW_AT_location, list);
21569 add_AT_die_ref (die, attr, decl_die);
21572 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21573 default. */
21575 static int
21576 lower_bound_default (void)
21578 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21580 case DW_LANG_C:
21581 case DW_LANG_C89:
21582 case DW_LANG_C99:
21583 case DW_LANG_C11:
21584 case DW_LANG_C_plus_plus:
21585 case DW_LANG_C_plus_plus_11:
21586 case DW_LANG_C_plus_plus_14:
21587 case DW_LANG_ObjC:
21588 case DW_LANG_ObjC_plus_plus:
21589 return 0;
21590 case DW_LANG_Fortran77:
21591 case DW_LANG_Fortran90:
21592 case DW_LANG_Fortran95:
21593 case DW_LANG_Fortran03:
21594 case DW_LANG_Fortran08:
21595 return 1;
21596 case DW_LANG_UPC:
21597 case DW_LANG_D:
21598 case DW_LANG_Python:
21599 return dwarf_version >= 4 ? 0 : -1;
21600 case DW_LANG_Ada95:
21601 case DW_LANG_Ada83:
21602 case DW_LANG_Cobol74:
21603 case DW_LANG_Cobol85:
21604 case DW_LANG_Modula2:
21605 case DW_LANG_PLI:
21606 return dwarf_version >= 4 ? 1 : -1;
21607 default:
21608 return -1;
21612 /* Given a tree node describing an array bound (either lower or upper) output
21613 a representation for that bound. */
21615 static void
21616 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21617 tree bound, struct loc_descr_context *context)
21619 int dflt;
21621 while (1)
21622 switch (TREE_CODE (bound))
21624 /* Strip all conversions. */
21625 CASE_CONVERT:
21626 case VIEW_CONVERT_EXPR:
21627 bound = TREE_OPERAND (bound, 0);
21628 break;
21630 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21631 are even omitted when they are the default. */
21632 case INTEGER_CST:
21633 /* If the value for this bound is the default one, we can even omit the
21634 attribute. */
21635 if (bound_attr == DW_AT_lower_bound
21636 && tree_fits_shwi_p (bound)
21637 && (dflt = lower_bound_default ()) != -1
21638 && tree_to_shwi (bound) == dflt)
21639 return;
21641 /* FALLTHRU */
21643 default:
21644 /* Let GNAT encodings do the magic for self-referential bounds. */
21645 if (is_ada ()
21646 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21647 && contains_placeholder_p (bound))
21648 return;
21650 add_scalar_info (subrange_die, bound_attr, bound,
21651 dw_scalar_form_constant
21652 | dw_scalar_form_exprloc
21653 | dw_scalar_form_reference,
21654 context);
21655 return;
21659 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21660 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21662 This function reuses previously set type and bound information if
21663 available. */
21665 static void
21666 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21668 dw_die_ref child = type_die->die_child;
21669 struct array_descr_info info;
21670 int dimension_number;
21672 if (lang_hooks.types.get_array_descr_info)
21674 memset (&info, 0, sizeof (info));
21675 if (lang_hooks.types.get_array_descr_info (type, &info))
21676 /* Fortran sometimes emits array types with no dimension. */
21677 gcc_assert (info.ndimensions >= 0
21678 && info.ndimensions
21679 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21681 else
21682 info.ndimensions = 0;
21684 for (dimension_number = 0;
21685 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21686 type = TREE_TYPE (type), dimension_number++)
21688 tree domain = TYPE_DOMAIN (type);
21690 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21691 break;
21693 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21694 and (in GNU C only) variable bounds. Handle all three forms
21695 here. */
21697 /* Find and reuse a previously generated DW_TAG_subrange_type if
21698 available.
21700 For multi-dimensional arrays, as we iterate through the
21701 various dimensions in the enclosing for loop above, we also
21702 iterate through the DIE children and pick at each
21703 DW_TAG_subrange_type previously generated (if available).
21704 Each child DW_TAG_subrange_type DIE describes the range of
21705 the current dimension. At this point we should have as many
21706 DW_TAG_subrange_type's as we have dimensions in the
21707 array. */
21708 dw_die_ref subrange_die = NULL;
21709 if (child)
21710 while (1)
21712 child = child->die_sib;
21713 if (child->die_tag == DW_TAG_subrange_type)
21714 subrange_die = child;
21715 if (child == type_die->die_child)
21717 /* If we wrapped around, stop looking next time. */
21718 child = NULL;
21719 break;
21721 if (child->die_tag == DW_TAG_subrange_type)
21722 break;
21724 if (!subrange_die)
21725 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21727 if (domain)
21729 /* We have an array type with specified bounds. */
21730 tree lower = TYPE_MIN_VALUE (domain);
21731 tree upper = TYPE_MAX_VALUE (domain);
21732 tree index_type = TREE_TYPE (domain);
21734 if (dimension_number <= info.ndimensions - 1)
21736 lower = info.dimen[dimension_number].lower_bound;
21737 upper = info.dimen[dimension_number].upper_bound;
21738 index_type = info.dimen[dimension_number].bounds_type;
21741 /* Define the index type. */
21742 if (index_type && !get_AT (subrange_die, DW_AT_type))
21743 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21744 false, type_die);
21746 /* ??? If upper is NULL, the array has unspecified length,
21747 but it does have a lower bound. This happens with Fortran
21748 dimension arr(N:*)
21749 Since the debugger is definitely going to need to know N
21750 to produce useful results, go ahead and output the lower
21751 bound solo, and hope the debugger can cope. */
21753 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21754 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21756 if (!get_AT (subrange_die, DW_AT_upper_bound)
21757 && !get_AT (subrange_die, DW_AT_count))
21759 if (upper)
21760 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21761 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21762 /* Zero-length array. */
21763 add_bound_info (subrange_die, DW_AT_count,
21764 build_int_cst (TREE_TYPE (lower), 0), NULL);
21768 /* Otherwise we have an array type with an unspecified length. The
21769 DWARF-2 spec does not say how to handle this; let's just leave out the
21770 bounds. */
21774 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21776 static void
21777 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21779 dw_die_ref decl_die;
21780 HOST_WIDE_INT size;
21782 switch (TREE_CODE (tree_node))
21784 case ERROR_MARK:
21785 size = 0;
21786 break;
21787 case ENUMERAL_TYPE:
21788 case RECORD_TYPE:
21789 case UNION_TYPE:
21790 case QUAL_UNION_TYPE:
21791 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21792 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21794 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21795 return;
21797 size = int_size_in_bytes (tree_node);
21798 break;
21799 case FIELD_DECL:
21800 /* For a data member of a struct or union, the DW_AT_byte_size is
21801 generally given as the number of bytes normally allocated for an
21802 object of the *declared* type of the member itself. This is true
21803 even for bit-fields. */
21804 size = int_size_in_bytes (field_type (tree_node));
21805 break;
21806 default:
21807 gcc_unreachable ();
21810 /* Note that `size' might be -1 when we get to this point. If it is, that
21811 indicates that the byte size of the entity in question is variable. */
21812 if (size >= 0)
21813 add_AT_unsigned (die, DW_AT_byte_size, size);
21815 /* Support for dynamically-sized objects was introduced in DWARF3. */
21816 else if (TYPE_P (tree_node)
21817 && (dwarf_version >= 3 || !dwarf_strict)
21818 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21820 struct loc_descr_context ctx = {
21821 const_cast<tree> (tree_node), /* context_type */
21822 NULL_TREE, /* base_decl */
21823 NULL, /* dpi */
21824 false, /* placeholder_arg */
21825 false, /* placeholder_seen */
21826 false /* strict_signedness */
21829 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21830 add_scalar_info (die, DW_AT_byte_size, tree_size,
21831 dw_scalar_form_constant
21832 | dw_scalar_form_exprloc
21833 | dw_scalar_form_reference,
21834 &ctx);
21838 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21839 alignment. */
21841 static void
21842 add_alignment_attribute (dw_die_ref die, tree tree_node)
21844 if (dwarf_version < 5 && dwarf_strict)
21845 return;
21847 unsigned align;
21849 if (DECL_P (tree_node))
21851 if (!DECL_USER_ALIGN (tree_node))
21852 return;
21854 align = DECL_ALIGN_UNIT (tree_node);
21856 else if (TYPE_P (tree_node))
21858 if (!TYPE_USER_ALIGN (tree_node))
21859 return;
21861 align = TYPE_ALIGN_UNIT (tree_node);
21863 else
21864 gcc_unreachable ();
21866 add_AT_unsigned (die, DW_AT_alignment, align);
21869 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21870 which specifies the distance in bits from the highest order bit of the
21871 "containing object" for the bit-field to the highest order bit of the
21872 bit-field itself.
21874 For any given bit-field, the "containing object" is a hypothetical object
21875 (of some integral or enum type) within which the given bit-field lives. The
21876 type of this hypothetical "containing object" is always the same as the
21877 declared type of the individual bit-field itself. The determination of the
21878 exact location of the "containing object" for a bit-field is rather
21879 complicated. It's handled by the `field_byte_offset' function (above).
21881 Note that it is the size (in bytes) of the hypothetical "containing object"
21882 which will be given in the DW_AT_byte_size attribute for this bit-field.
21883 (See `byte_size_attribute' above). */
21885 static inline void
21886 add_bit_offset_attribute (dw_die_ref die, tree decl)
21888 HOST_WIDE_INT object_offset_in_bytes;
21889 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21890 HOST_WIDE_INT bitpos_int;
21891 HOST_WIDE_INT highest_order_object_bit_offset;
21892 HOST_WIDE_INT highest_order_field_bit_offset;
21893 HOST_WIDE_INT bit_offset;
21895 /* The containing object is within the DECL_CONTEXT. */
21896 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21898 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21900 /* Must be a field and a bit field. */
21901 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21903 /* We can't yet handle bit-fields whose offsets are variable, so if we
21904 encounter such things, just return without generating any attribute
21905 whatsoever. Likewise for variable or too large size. */
21906 if (! tree_fits_shwi_p (bit_position (decl))
21907 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21908 return;
21910 bitpos_int = int_bit_position (decl);
21912 /* Note that the bit offset is always the distance (in bits) from the
21913 highest-order bit of the "containing object" to the highest-order bit of
21914 the bit-field itself. Since the "high-order end" of any object or field
21915 is different on big-endian and little-endian machines, the computation
21916 below must take account of these differences. */
21917 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21918 highest_order_field_bit_offset = bitpos_int;
21920 if (! BYTES_BIG_ENDIAN)
21922 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21923 highest_order_object_bit_offset +=
21924 simple_type_size_in_bits (original_type);
21927 bit_offset
21928 = (! BYTES_BIG_ENDIAN
21929 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21930 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21932 if (bit_offset < 0)
21933 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21934 else
21935 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21938 /* For a FIELD_DECL node which represents a bit field, output an attribute
21939 which specifies the length in bits of the given field. */
21941 static inline void
21942 add_bit_size_attribute (dw_die_ref die, tree decl)
21944 /* Must be a field and a bit field. */
21945 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21946 && DECL_BIT_FIELD_TYPE (decl));
21948 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21949 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21952 /* If the compiled language is ANSI C, then add a 'prototyped'
21953 attribute, if arg types are given for the parameters of a function. */
21955 static inline void
21956 add_prototyped_attribute (dw_die_ref die, tree func_type)
21958 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21960 case DW_LANG_C:
21961 case DW_LANG_C89:
21962 case DW_LANG_C99:
21963 case DW_LANG_C11:
21964 case DW_LANG_ObjC:
21965 if (prototype_p (func_type))
21966 add_AT_flag (die, DW_AT_prototyped, 1);
21967 break;
21968 default:
21969 break;
21973 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21974 by looking in the type declaration, the object declaration equate table or
21975 the block mapping. */
21977 static inline void
21978 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21980 dw_die_ref origin_die = NULL;
21982 /* For late LTO debug output we want to refer directly to the abstract
21983 DIE in the early debug rather to the possibly existing concrete
21984 instance and avoid creating that just for this purpose. */
21985 sym_off_pair *desc;
21986 if (in_lto_p
21987 && external_die_map
21988 && (desc = external_die_map->get (origin)))
21990 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21991 desc->sym, desc->off);
21992 return;
21995 if (DECL_P (origin))
21996 origin_die = lookup_decl_die (origin);
21997 else if (TYPE_P (origin))
21998 origin_die = lookup_type_die (origin);
21999 else if (TREE_CODE (origin) == BLOCK)
22000 origin_die = lookup_block_die (origin);
22002 /* XXX: Functions that are never lowered don't always have correct block
22003 trees (in the case of java, they simply have no block tree, in some other
22004 languages). For these functions, there is nothing we can really do to
22005 output correct debug info for inlined functions in all cases. Rather
22006 than die, we'll just produce deficient debug info now, in that we will
22007 have variables without a proper abstract origin. In the future, when all
22008 functions are lowered, we should re-add a gcc_assert (origin_die)
22009 here. */
22011 if (origin_die)
22013 dw_attr_node *a;
22014 /* Like above, if we already created a concrete instance DIE
22015 do not use that for the abstract origin but the early DIE
22016 if present. */
22017 if (in_lto_p
22018 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
22019 origin_die = AT_ref (a);
22020 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
22024 /* We do not currently support the pure_virtual attribute. */
22026 static inline void
22027 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
22029 if (DECL_VINDEX (func_decl))
22031 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22033 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
22034 add_AT_loc (die, DW_AT_vtable_elem_location,
22035 new_loc_descr (DW_OP_constu,
22036 tree_to_shwi (DECL_VINDEX (func_decl)),
22037 0));
22039 /* GNU extension: Record what type this method came from originally. */
22040 if (debug_info_level > DINFO_LEVEL_TERSE
22041 && DECL_CONTEXT (func_decl))
22042 add_AT_die_ref (die, DW_AT_containing_type,
22043 lookup_type_die (DECL_CONTEXT (func_decl)));
22047 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
22048 given decl. This used to be a vendor extension until after DWARF 4
22049 standardized it. */
22051 static void
22052 add_linkage_attr (dw_die_ref die, tree decl)
22054 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
22056 /* Mimic what assemble_name_raw does with a leading '*'. */
22057 if (name[0] == '*')
22058 name = &name[1];
22060 if (dwarf_version >= 4)
22061 add_AT_string (die, DW_AT_linkage_name, name);
22062 else
22063 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22066 /* Add source coordinate attributes for the given decl. */
22068 static void
22069 add_src_coords_attributes (dw_die_ref die, tree decl)
22071 expanded_location s;
22073 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22074 return;
22075 s = expand_location (DECL_SOURCE_LOCATION (decl));
22076 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22077 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22078 if (debug_column_info && s.column)
22079 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22082 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22084 static void
22085 add_linkage_name_raw (dw_die_ref die, tree decl)
22087 /* Defer until we have an assembler name set. */
22088 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22090 limbo_die_node *asm_name;
22092 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22093 asm_name->die = die;
22094 asm_name->created_for = decl;
22095 asm_name->next = deferred_asm_name;
22096 deferred_asm_name = asm_name;
22098 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22099 add_linkage_attr (die, decl);
22102 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22104 static void
22105 add_linkage_name (dw_die_ref die, tree decl)
22107 if (debug_info_level > DINFO_LEVEL_NONE
22108 && VAR_OR_FUNCTION_DECL_P (decl)
22109 && TREE_PUBLIC (decl)
22110 && !(VAR_P (decl) && DECL_REGISTER (decl))
22111 && die->die_tag != DW_TAG_member)
22112 add_linkage_name_raw (die, decl);
22115 /* Add a DW_AT_name attribute and source coordinate attribute for the
22116 given decl, but only if it actually has a name. */
22118 static void
22119 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22120 bool no_linkage_name)
22122 tree decl_name;
22124 decl_name = DECL_NAME (decl);
22125 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22127 const char *name = dwarf2_name (decl, 0);
22128 if (name)
22129 add_name_attribute (die, name);
22130 else
22131 add_desc_attribute (die, decl);
22133 if (! DECL_ARTIFICIAL (decl))
22134 add_src_coords_attributes (die, decl);
22136 if (!no_linkage_name)
22137 add_linkage_name (die, decl);
22139 else
22140 add_desc_attribute (die, decl);
22142 #ifdef VMS_DEBUGGING_INFO
22143 /* Get the function's name, as described by its RTL. This may be different
22144 from the DECL_NAME name used in the source file. */
22145 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22147 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22148 XEXP (DECL_RTL (decl), 0), false);
22149 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22151 #endif /* VMS_DEBUGGING_INFO */
22154 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22156 static void
22157 add_discr_value (dw_die_ref die, dw_discr_value *value)
22159 dw_attr_node attr;
22161 attr.dw_attr = DW_AT_discr_value;
22162 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22163 attr.dw_attr_val.val_entry = NULL;
22164 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22165 if (value->pos)
22166 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22167 else
22168 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22169 add_dwarf_attr (die, &attr);
22172 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22174 static void
22175 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22177 dw_attr_node attr;
22179 attr.dw_attr = DW_AT_discr_list;
22180 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22181 attr.dw_attr_val.val_entry = NULL;
22182 attr.dw_attr_val.v.val_discr_list = discr_list;
22183 add_dwarf_attr (die, &attr);
22186 static inline dw_discr_list_ref
22187 AT_discr_list (dw_attr_node *attr)
22189 return attr->dw_attr_val.v.val_discr_list;
22192 #ifdef VMS_DEBUGGING_INFO
22193 /* Output the debug main pointer die for VMS */
22195 void
22196 dwarf2out_vms_debug_main_pointer (void)
22198 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22199 dw_die_ref die;
22201 /* Allocate the VMS debug main subprogram die. */
22202 die = new_die_raw (DW_TAG_subprogram);
22203 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22204 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22205 current_function_funcdef_no);
22206 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22208 /* Make it the first child of comp_unit_die (). */
22209 die->die_parent = comp_unit_die ();
22210 if (comp_unit_die ()->die_child)
22212 die->die_sib = comp_unit_die ()->die_child->die_sib;
22213 comp_unit_die ()->die_child->die_sib = die;
22215 else
22217 die->die_sib = die;
22218 comp_unit_die ()->die_child = die;
22221 #endif /* VMS_DEBUGGING_INFO */
22223 /* walk_tree helper function for uses_local_type, below. */
22225 static tree
22226 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22228 if (!TYPE_P (*tp))
22229 *walk_subtrees = 0;
22230 else
22232 tree name = TYPE_NAME (*tp);
22233 if (name && DECL_P (name) && decl_function_context (name))
22234 return *tp;
22236 return NULL_TREE;
22239 /* If TYPE involves a function-local type (including a local typedef to a
22240 non-local type), returns that type; otherwise returns NULL_TREE. */
22242 static tree
22243 uses_local_type (tree type)
22245 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22246 return used;
22249 /* Return the DIE for the scope that immediately contains this type.
22250 Non-named types that do not involve a function-local type get global
22251 scope. Named types nested in namespaces or other types get their
22252 containing scope. All other types (i.e. function-local named types) get
22253 the current active scope. */
22255 static dw_die_ref
22256 scope_die_for (tree t, dw_die_ref context_die)
22258 dw_die_ref scope_die = NULL;
22259 tree containing_scope;
22261 /* Non-types always go in the current scope. */
22262 gcc_assert (TYPE_P (t));
22264 /* Use the scope of the typedef, rather than the scope of the type
22265 it refers to. */
22266 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22267 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22268 else
22269 containing_scope = TYPE_CONTEXT (t);
22271 /* Use the containing namespace if there is one. */
22272 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22274 if (context_die == lookup_decl_die (containing_scope))
22275 /* OK */;
22276 else if (debug_info_level > DINFO_LEVEL_TERSE)
22277 context_die = get_context_die (containing_scope);
22278 else
22279 containing_scope = NULL_TREE;
22282 /* Ignore function type "scopes" from the C frontend. They mean that
22283 a tagged type is local to a parmlist of a function declarator, but
22284 that isn't useful to DWARF. */
22285 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22286 containing_scope = NULL_TREE;
22288 if (SCOPE_FILE_SCOPE_P (containing_scope))
22290 /* If T uses a local type keep it local as well, to avoid references
22291 to function-local DIEs from outside the function. */
22292 if (current_function_decl && uses_local_type (t))
22293 scope_die = context_die;
22294 else
22295 scope_die = comp_unit_die ();
22297 else if (TYPE_P (containing_scope))
22299 /* For types, we can just look up the appropriate DIE. */
22300 if (debug_info_level > DINFO_LEVEL_TERSE)
22301 scope_die = get_context_die (containing_scope);
22302 else
22304 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22305 if (scope_die == NULL)
22306 scope_die = comp_unit_die ();
22309 else
22310 scope_die = context_die;
22312 return scope_die;
22315 /* Returns true if CONTEXT_DIE is internal to a function. */
22317 static inline bool
22318 local_scope_p (dw_die_ref context_die)
22320 for (; context_die; context_die = context_die->die_parent)
22321 if (context_die->die_tag == DW_TAG_inlined_subroutine
22322 || context_die->die_tag == DW_TAG_subprogram)
22323 return true;
22325 return false;
22328 /* Returns true if CONTEXT_DIE is a class. */
22330 static inline bool
22331 class_scope_p (dw_die_ref context_die)
22333 return (context_die
22334 && (context_die->die_tag == DW_TAG_structure_type
22335 || context_die->die_tag == DW_TAG_class_type
22336 || context_die->die_tag == DW_TAG_interface_type
22337 || context_die->die_tag == DW_TAG_union_type));
22340 /* Returns true if CONTEXT_DIE is a class or namespace, for deciding
22341 whether or not to treat a DIE in this context as a declaration. */
22343 static inline bool
22344 class_or_namespace_scope_p (dw_die_ref context_die)
22346 return (class_scope_p (context_die)
22347 || (context_die && context_die->die_tag == DW_TAG_namespace));
22350 /* Many forms of DIEs require a "type description" attribute. This
22351 routine locates the proper "type descriptor" die for the type given
22352 by 'type' plus any additional qualifiers given by 'cv_quals', and
22353 adds a DW_AT_type attribute below the given die. */
22355 static void
22356 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22357 bool reverse, dw_die_ref context_die)
22359 enum tree_code code = TREE_CODE (type);
22360 dw_die_ref type_die = NULL;
22362 if (debug_info_level <= DINFO_LEVEL_TERSE)
22363 return;
22365 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22366 or fixed-point type, use the inner type. This is because we have no
22367 support for unnamed types in base_type_die. This can happen if this is
22368 an Ada subrange type. Correct solution is emit a subrange type die. */
22369 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22370 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22371 type = TREE_TYPE (type), code = TREE_CODE (type);
22373 if (code == ERROR_MARK
22374 /* Handle a special case. For functions whose return type is void, we
22375 generate *no* type attribute. (Note that no object may have type
22376 `void', so this only applies to function return types). */
22377 || code == VOID_TYPE)
22378 return;
22380 type_die = modified_type_die (type,
22381 cv_quals | TYPE_QUALS (type),
22382 reverse,
22383 context_die);
22385 if (type_die != NULL)
22386 add_AT_die_ref (object_die, DW_AT_type, type_die);
22389 /* Given an object die, add the calling convention attribute for the
22390 function call type. */
22391 static void
22392 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22394 enum dwarf_calling_convention value = DW_CC_normal;
22396 value = ((enum dwarf_calling_convention)
22397 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22399 if (is_fortran ()
22400 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22402 /* DWARF 2 doesn't provide a way to identify a program's source-level
22403 entry point. DW_AT_calling_convention attributes are only meant
22404 to describe functions' calling conventions. However, lacking a
22405 better way to signal the Fortran main program, we used this for
22406 a long time, following existing custom. Now, DWARF 4 has
22407 DW_AT_main_subprogram, which we add below, but some tools still
22408 rely on the old way, which we thus keep. */
22409 value = DW_CC_program;
22411 if (dwarf_version >= 4 || !dwarf_strict)
22412 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22415 /* Only add the attribute if the backend requests it, and
22416 is not DW_CC_normal. */
22417 if (value && (value != DW_CC_normal))
22418 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22421 /* Given a tree pointer to a struct, class, union, or enum type node, return
22422 a pointer to the (string) tag name for the given type, or zero if the type
22423 was declared without a tag. */
22425 static const char *
22426 type_tag (const_tree type)
22428 const char *name = 0;
22430 if (TYPE_NAME (type) != 0)
22432 tree t = 0;
22434 /* Find the IDENTIFIER_NODE for the type name. */
22435 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22436 && !TYPE_NAMELESS (type))
22437 t = TYPE_NAME (type);
22439 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22440 a TYPE_DECL node, regardless of whether or not a `typedef' was
22441 involved. */
22442 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22443 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22445 /* We want to be extra verbose. Don't call dwarf_name if
22446 DECL_NAME isn't set. The default hook for decl_printable_name
22447 doesn't like that, and in this context it's correct to return
22448 0, instead of "<anonymous>" or the like. */
22449 if (DECL_NAME (TYPE_NAME (type))
22450 && !DECL_NAMELESS (TYPE_NAME (type)))
22451 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22454 /* Now get the name as a string, or invent one. */
22455 if (!name && t != 0)
22456 name = IDENTIFIER_POINTER (t);
22459 return (name == 0 || *name == '\0') ? 0 : name;
22462 /* Return the type associated with a data member, make a special check
22463 for bit field types. */
22465 static inline tree
22466 member_declared_type (const_tree member)
22468 return (DECL_BIT_FIELD_TYPE (member)
22469 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22472 /* Get the decl's label, as described by its RTL. This may be different
22473 from the DECL_NAME name used in the source file. */
22475 #if 0
22476 static const char *
22477 decl_start_label (tree decl)
22479 rtx x;
22480 const char *fnname;
22482 x = DECL_RTL (decl);
22483 gcc_assert (MEM_P (x));
22485 x = XEXP (x, 0);
22486 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22488 fnname = XSTR (x, 0);
22489 return fnname;
22491 #endif
22493 /* For variable-length arrays that have been previously generated, but
22494 may be incomplete due to missing subscript info, fill the subscript
22495 info. Return TRUE if this is one of those cases. */
22497 static bool
22498 fill_variable_array_bounds (tree type)
22500 if (TREE_ASM_WRITTEN (type)
22501 && TREE_CODE (type) == ARRAY_TYPE
22502 && variably_modified_type_p (type, NULL))
22504 dw_die_ref array_die = lookup_type_die (type);
22505 if (!array_die)
22506 return false;
22507 add_subscript_info (array_die, type, !is_ada ());
22508 return true;
22510 return false;
22513 /* These routines generate the internal representation of the DIE's for
22514 the compilation unit. Debugging information is collected by walking
22515 the declaration trees passed in from dwarf2out_decl(). */
22517 static void
22518 gen_array_type_die (tree type, dw_die_ref context_die)
22520 dw_die_ref array_die;
22522 /* GNU compilers represent multidimensional array types as sequences of one
22523 dimensional array types whose element types are themselves array types.
22524 We sometimes squish that down to a single array_type DIE with multiple
22525 subscripts in the Dwarf debugging info. The draft Dwarf specification
22526 say that we are allowed to do this kind of compression in C, because
22527 there is no difference between an array of arrays and a multidimensional
22528 array. We don't do this for Ada to remain as close as possible to the
22529 actual representation, which is especially important against the language
22530 flexibilty wrt arrays of variable size. */
22532 bool collapse_nested_arrays = !is_ada ();
22534 if (fill_variable_array_bounds (type))
22535 return;
22537 dw_die_ref scope_die = scope_die_for (type, context_die);
22538 tree element_type;
22540 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22541 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22542 if (TREE_CODE (type) == ARRAY_TYPE
22543 && TYPE_STRING_FLAG (type)
22544 && is_fortran ()
22545 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22547 HOST_WIDE_INT size;
22549 array_die = new_die (DW_TAG_string_type, scope_die, type);
22550 add_name_attribute (array_die, type_tag (type));
22551 equate_type_number_to_die (type, array_die);
22552 size = int_size_in_bytes (type);
22553 if (size >= 0)
22554 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22555 /* ??? We can't annotate types late, but for LTO we may not
22556 generate a location early either (gfortran.dg/save_6.f90). */
22557 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22558 && TYPE_DOMAIN (type) != NULL_TREE
22559 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22561 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22562 tree rszdecl = szdecl;
22564 size = int_size_in_bytes (TREE_TYPE (szdecl));
22565 if (!DECL_P (szdecl))
22567 if (INDIRECT_REF_P (szdecl)
22568 && DECL_P (TREE_OPERAND (szdecl, 0)))
22570 rszdecl = TREE_OPERAND (szdecl, 0);
22571 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22572 != DWARF2_ADDR_SIZE)
22573 size = 0;
22575 else
22576 size = 0;
22578 if (size > 0)
22580 dw_loc_list_ref loc
22581 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22582 NULL);
22583 if (loc)
22585 add_AT_location_description (array_die, DW_AT_string_length,
22586 loc);
22587 if (size != DWARF2_ADDR_SIZE)
22588 add_AT_unsigned (array_die, dwarf_version >= 5
22589 ? DW_AT_string_length_byte_size
22590 : DW_AT_byte_size, size);
22594 return;
22597 array_die = new_die (DW_TAG_array_type, scope_die, type);
22598 add_name_attribute (array_die, type_tag (type));
22599 equate_type_number_to_die (type, array_die);
22601 if (VECTOR_TYPE_P (type))
22602 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22604 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22605 if (is_fortran ()
22606 && TREE_CODE (type) == ARRAY_TYPE
22607 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22608 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22609 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22611 #if 0
22612 /* We default the array ordering. Debuggers will probably do the right
22613 things even if DW_AT_ordering is not present. It's not even an issue
22614 until we start to get into multidimensional arrays anyway. If a debugger
22615 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22616 then we'll have to put the DW_AT_ordering attribute back in. (But if
22617 and when we find out that we need to put these in, we will only do so
22618 for multidimensional arrays. */
22619 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22620 #endif
22622 if (VECTOR_TYPE_P (type))
22624 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22625 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22626 int lb = lower_bound_default ();
22627 if (lb == -1)
22628 lb = 0;
22629 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22630 add_bound_info (subrange_die, DW_AT_upper_bound,
22631 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22633 else
22634 add_subscript_info (array_die, type, collapse_nested_arrays);
22636 /* Add representation of the type of the elements of this array type and
22637 emit the corresponding DIE if we haven't done it already. */
22638 element_type = TREE_TYPE (type);
22639 if (collapse_nested_arrays)
22640 while (TREE_CODE (element_type) == ARRAY_TYPE)
22642 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22643 break;
22644 element_type = TREE_TYPE (element_type);
22647 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22648 TREE_CODE (type) == ARRAY_TYPE
22649 && TYPE_REVERSE_STORAGE_ORDER (type),
22650 context_die);
22652 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22653 if (TYPE_ARTIFICIAL (type))
22654 add_AT_flag (array_die, DW_AT_artificial, 1);
22656 if (get_AT (array_die, DW_AT_name))
22657 add_pubtype (type, array_die);
22659 add_alignment_attribute (array_die, type);
22662 /* This routine generates DIE for array with hidden descriptor, details
22663 are filled into *info by a langhook. */
22665 static void
22666 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22667 dw_die_ref context_die)
22669 const dw_die_ref scope_die = scope_die_for (type, context_die);
22670 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22671 struct loc_descr_context context = {
22672 type, /* context_type */
22673 info->base_decl, /* base_decl */
22674 NULL, /* dpi */
22675 false, /* placeholder_arg */
22676 false, /* placeholder_seen */
22677 false /* strict_signedness */
22679 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22680 int dim;
22682 add_name_attribute (array_die, type_tag (type));
22683 equate_type_number_to_die (type, array_die);
22685 if (info->ndimensions > 1)
22686 switch (info->ordering)
22688 case array_descr_ordering_row_major:
22689 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22690 break;
22691 case array_descr_ordering_column_major:
22692 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22693 break;
22694 default:
22695 break;
22698 if (dwarf_version >= 3 || !dwarf_strict)
22700 if (info->data_location)
22701 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22702 dw_scalar_form_exprloc, &context);
22703 if (info->associated)
22704 add_scalar_info (array_die, DW_AT_associated, info->associated,
22705 dw_scalar_form_constant
22706 | dw_scalar_form_exprloc
22707 | dw_scalar_form_reference, &context);
22708 if (info->allocated)
22709 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22710 dw_scalar_form_constant
22711 | dw_scalar_form_exprloc
22712 | dw_scalar_form_reference, &context);
22713 if (info->stride)
22715 const enum dwarf_attribute attr
22716 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22717 const int forms
22718 = (info->stride_in_bits)
22719 ? dw_scalar_form_constant
22720 : (dw_scalar_form_constant
22721 | dw_scalar_form_exprloc
22722 | dw_scalar_form_reference);
22724 add_scalar_info (array_die, attr, info->stride, forms, &context);
22727 if (dwarf_version >= 5)
22729 if (info->rank)
22731 add_scalar_info (array_die, DW_AT_rank, info->rank,
22732 dw_scalar_form_constant
22733 | dw_scalar_form_exprloc, &context);
22734 subrange_tag = DW_TAG_generic_subrange;
22735 context.placeholder_arg = true;
22739 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22741 for (dim = 0; dim < info->ndimensions; dim++)
22743 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22745 if (info->dimen[dim].bounds_type)
22746 add_type_attribute (subrange_die,
22747 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22748 false, context_die);
22749 if (info->dimen[dim].lower_bound)
22750 add_bound_info (subrange_die, DW_AT_lower_bound,
22751 info->dimen[dim].lower_bound, &context);
22752 if (info->dimen[dim].upper_bound)
22753 add_bound_info (subrange_die, DW_AT_upper_bound,
22754 info->dimen[dim].upper_bound, &context);
22755 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22756 add_scalar_info (subrange_die, DW_AT_byte_stride,
22757 info->dimen[dim].stride,
22758 dw_scalar_form_constant
22759 | dw_scalar_form_exprloc
22760 | dw_scalar_form_reference,
22761 &context);
22764 gen_type_die (info->element_type, context_die);
22765 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22766 TREE_CODE (type) == ARRAY_TYPE
22767 && TYPE_REVERSE_STORAGE_ORDER (type),
22768 context_die);
22770 if (get_AT (array_die, DW_AT_name))
22771 add_pubtype (type, array_die);
22773 add_alignment_attribute (array_die, type);
22776 #if 0
22777 static void
22778 gen_entry_point_die (tree decl, dw_die_ref context_die)
22780 tree origin = decl_ultimate_origin (decl);
22781 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22783 if (origin != NULL)
22784 add_abstract_origin_attribute (decl_die, origin);
22785 else
22787 add_name_and_src_coords_attributes (decl_die, decl);
22788 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22789 TYPE_UNQUALIFIED, false, context_die);
22792 if (DECL_ABSTRACT_P (decl))
22793 equate_decl_number_to_die (decl, decl_die);
22794 else
22795 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22797 #endif
22799 /* Walk through the list of incomplete types again, trying once more to
22800 emit full debugging info for them. */
22802 static void
22803 retry_incomplete_types (void)
22805 set_early_dwarf s;
22806 int i;
22808 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22809 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22810 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22811 vec_safe_truncate (incomplete_types, 0);
22814 /* Determine what tag to use for a record type. */
22816 static enum dwarf_tag
22817 record_type_tag (tree type)
22819 if (! lang_hooks.types.classify_record)
22820 return DW_TAG_structure_type;
22822 switch (lang_hooks.types.classify_record (type))
22824 case RECORD_IS_STRUCT:
22825 return DW_TAG_structure_type;
22827 case RECORD_IS_CLASS:
22828 return DW_TAG_class_type;
22830 case RECORD_IS_INTERFACE:
22831 if (dwarf_version >= 3 || !dwarf_strict)
22832 return DW_TAG_interface_type;
22833 return DW_TAG_structure_type;
22835 default:
22836 gcc_unreachable ();
22840 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22841 include all of the information about the enumeration values also. Each
22842 enumerated type name/value is listed as a child of the enumerated type
22843 DIE. REVERSE is true if the type is to be interpreted in the reverse
22844 storage order wrt the target order. */
22846 static dw_die_ref
22847 gen_enumeration_type_die (tree type, dw_die_ref context_die, bool reverse)
22849 dw_die_ref type_die = lookup_type_die (type);
22850 dw_die_ref orig_type_die = type_die;
22852 if (type_die == NULL || reverse)
22854 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
22855 if (reverse)
22857 gcc_assert (type_die);
22858 dw_die_ref after_die = type_die;
22859 type_die = new_die_raw (DW_TAG_enumeration_type);
22860 add_child_die_after (context_die, type_die, after_die);
22862 else
22864 type_die = new_die (DW_TAG_enumeration_type,
22865 scope_die_for (type, context_die), type);
22866 equate_type_number_to_die (type, type_die);
22868 add_name_attribute (type_die, type_tag (type));
22869 if ((dwarf_version >= 4 || !dwarf_strict)
22870 && ENUM_IS_SCOPED (type))
22871 add_AT_flag (type_die, DW_AT_enum_class, 1);
22872 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22873 add_AT_flag (type_die, DW_AT_declaration, 1);
22874 if (!dwarf_strict)
22875 add_AT_unsigned (type_die, DW_AT_encoding,
22876 TYPE_UNSIGNED (type)
22877 ? DW_ATE_unsigned
22878 : DW_ATE_signed);
22879 if (reverse)
22880 add_AT_unsigned (type_die, DW_AT_endianity,
22881 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
22883 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22884 return type_die;
22885 else
22886 remove_AT (type_die, DW_AT_declaration);
22888 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22889 given enum type is incomplete, do not generate the DW_AT_byte_size
22890 attribute or the DW_AT_element_list attribute. */
22891 if (TYPE_SIZE (type))
22893 tree link;
22895 if (!ENUM_IS_OPAQUE (type))
22896 TREE_ASM_WRITTEN (type) = 1;
22897 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22898 add_byte_size_attribute (type_die, type);
22899 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22900 add_alignment_attribute (type_die, type);
22901 if ((dwarf_version >= 3 || !dwarf_strict)
22902 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22904 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22905 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22906 context_die);
22908 if (TYPE_STUB_DECL (type) != NULL_TREE)
22910 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22911 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22912 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22913 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22916 /* If the first reference to this type was as the return type of an
22917 inline function, then it may not have a parent. Fix this now. */
22918 if (type_die->die_parent == NULL)
22919 add_child_die (scope_die_for (type, context_die), type_die);
22921 for (link = TYPE_VALUES (type);
22922 link != NULL; link = TREE_CHAIN (link))
22924 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22925 tree value = TREE_VALUE (link);
22927 if (DECL_P (value))
22928 equate_decl_number_to_die (value, enum_die);
22930 gcc_assert (!ENUM_IS_OPAQUE (type));
22931 add_name_attribute (enum_die,
22932 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22934 if (TREE_CODE (value) == CONST_DECL)
22935 value = DECL_INITIAL (value);
22937 if (simple_type_size_in_bits (TREE_TYPE (value))
22938 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22940 /* For constant forms created by add_AT_unsigned DWARF
22941 consumers (GDB, elfutils, etc.) always zero extend
22942 the value. Only when the actual value is negative
22943 do we need to use add_AT_int to generate a constant
22944 form that can represent negative values. */
22945 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22946 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22947 add_AT_unsigned (enum_die, DW_AT_const_value,
22948 (unsigned HOST_WIDE_INT) val);
22949 else
22950 add_AT_int (enum_die, DW_AT_const_value, val);
22952 else
22953 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22954 that here. TODO: This should be re-worked to use correct
22955 signed/unsigned double tags for all cases. */
22956 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22959 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22960 if (TYPE_ARTIFICIAL (type)
22961 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22962 add_AT_flag (type_die, DW_AT_artificial, 1);
22964 else
22965 add_AT_flag (type_die, DW_AT_declaration, 1);
22967 add_pubtype (type, type_die);
22969 return type_die;
22972 /* Generate a DIE to represent either a real live formal parameter decl or to
22973 represent just the type of some formal parameter position in some function
22974 type.
22976 Note that this routine is a bit unusual because its argument may be a
22977 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22978 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22979 node. If it's the former then this function is being called to output a
22980 DIE to represent a formal parameter object (or some inlining thereof). If
22981 it's the latter, then this function is only being called to output a
22982 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22983 argument type of some subprogram type.
22984 If EMIT_NAME_P is true, name and source coordinate attributes
22985 are emitted. */
22987 static dw_die_ref
22988 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22989 dw_die_ref context_die)
22991 tree node_or_origin = node ? node : origin;
22992 tree ultimate_origin;
22993 dw_die_ref parm_die = NULL;
22995 if (DECL_P (node_or_origin))
22997 parm_die = lookup_decl_die (node);
22999 /* If the contexts differ, we may not be talking about the same
23000 thing.
23001 ??? When in LTO the DIE parent is the "abstract" copy and the
23002 context_die is the specification "copy". */
23003 if (parm_die
23004 && parm_die->die_parent != context_die
23005 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
23006 || parm_die->die_parent->die_parent != context_die)
23007 && !in_lto_p)
23009 gcc_assert (!DECL_ABSTRACT_P (node));
23010 /* This can happen when creating a concrete instance, in
23011 which case we need to create a new DIE that will get
23012 annotated with DW_AT_abstract_origin. */
23013 parm_die = NULL;
23016 if (parm_die && parm_die->die_parent == NULL)
23018 /* Check that parm_die already has the right attributes that
23019 we would have added below. If any attributes are
23020 missing, fall through to add them. */
23021 if (! DECL_ABSTRACT_P (node_or_origin)
23022 && !get_AT (parm_die, DW_AT_location)
23023 && !get_AT (parm_die, DW_AT_const_value))
23024 /* We are missing location info, and are about to add it. */
23026 else
23028 add_child_die (context_die, parm_die);
23029 return parm_die;
23034 /* If we have a previously generated DIE, use it, unless this is an
23035 concrete instance (origin != NULL), in which case we need a new
23036 DIE with a corresponding DW_AT_abstract_origin. */
23037 bool reusing_die;
23038 if (parm_die && origin == NULL)
23039 reusing_die = true;
23040 else
23042 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
23043 reusing_die = false;
23046 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
23048 case tcc_declaration:
23049 ultimate_origin = decl_ultimate_origin (node_or_origin);
23050 if (node || ultimate_origin)
23051 origin = ultimate_origin;
23053 if (reusing_die)
23054 goto add_location;
23056 if (origin != NULL)
23057 add_abstract_origin_attribute (parm_die, origin);
23058 else if (emit_name_p)
23059 add_name_and_src_coords_attributes (parm_die, node);
23060 if (origin == NULL
23061 || (! DECL_ABSTRACT_P (node_or_origin)
23062 && variably_modified_type_p (TREE_TYPE (node_or_origin),
23063 decl_function_context
23064 (node_or_origin))))
23066 tree type = TREE_TYPE (node_or_origin);
23067 if (decl_by_reference_p (node_or_origin))
23068 add_type_attribute (parm_die, TREE_TYPE (type),
23069 TYPE_UNQUALIFIED,
23070 false, context_die);
23071 else
23072 add_type_attribute (parm_die, type,
23073 decl_quals (node_or_origin),
23074 false, context_die);
23076 if (origin == NULL && DECL_ARTIFICIAL (node))
23077 add_AT_flag (parm_die, DW_AT_artificial, 1);
23078 add_location:
23079 if (node && node != origin)
23080 equate_decl_number_to_die (node, parm_die);
23081 if (! DECL_ABSTRACT_P (node_or_origin))
23082 add_location_or_const_value_attribute (parm_die, node_or_origin,
23083 node == NULL);
23085 break;
23087 case tcc_type:
23088 /* We were called with some kind of a ..._TYPE node. */
23089 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23090 context_die);
23091 break;
23093 default:
23094 gcc_unreachable ();
23097 return parm_die;
23100 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23101 children DW_TAG_formal_parameter DIEs representing the arguments of the
23102 parameter pack.
23104 PARM_PACK must be a function parameter pack.
23105 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23106 must point to the subsequent arguments of the function PACK_ARG belongs to.
23107 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23108 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23109 following the last one for which a DIE was generated. */
23111 static dw_die_ref
23112 gen_formal_parameter_pack_die (tree parm_pack,
23113 tree pack_arg,
23114 dw_die_ref subr_die,
23115 tree *next_arg)
23117 tree arg;
23118 dw_die_ref parm_pack_die;
23120 gcc_assert (parm_pack
23121 && lang_hooks.function_parameter_pack_p (parm_pack)
23122 && subr_die);
23124 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23125 add_src_coords_attributes (parm_pack_die, parm_pack);
23127 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23129 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23130 parm_pack))
23131 break;
23132 gen_formal_parameter_die (arg, NULL,
23133 false /* Don't emit name attribute. */,
23134 parm_pack_die);
23136 if (next_arg)
23137 *next_arg = arg;
23138 return parm_pack_die;
23141 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23142 at the end of an (ANSI prototyped) formal parameters list. */
23144 static void
23145 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23147 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23150 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23151 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23152 parameters as specified in some function type specification (except for
23153 those which appear as part of a function *definition*). */
23155 static void
23156 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23158 tree link;
23159 tree formal_type = NULL;
23160 tree first_parm_type;
23161 tree arg;
23163 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23165 arg = DECL_ARGUMENTS (function_or_method_type);
23166 function_or_method_type = TREE_TYPE (function_or_method_type);
23168 else
23169 arg = NULL_TREE;
23171 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23173 /* Make our first pass over the list of formal parameter types and output a
23174 DW_TAG_formal_parameter DIE for each one. */
23175 for (link = first_parm_type; link; )
23177 dw_die_ref parm_die;
23179 formal_type = TREE_VALUE (link);
23180 if (formal_type == void_type_node)
23181 break;
23183 /* Output a (nameless) DIE to represent the formal parameter itself. */
23184 parm_die = gen_formal_parameter_die (formal_type, NULL,
23185 true /* Emit name attribute. */,
23186 context_die);
23187 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23188 && link == first_parm_type)
23190 add_AT_flag (parm_die, DW_AT_artificial, 1);
23191 if (dwarf_version >= 3 || !dwarf_strict)
23192 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23194 else if (arg && DECL_ARTIFICIAL (arg))
23195 add_AT_flag (parm_die, DW_AT_artificial, 1);
23197 link = TREE_CHAIN (link);
23198 if (arg)
23199 arg = DECL_CHAIN (arg);
23202 /* If this function type has an ellipsis, add a
23203 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23204 if (formal_type != void_type_node)
23205 gen_unspecified_parameters_die (function_or_method_type, context_die);
23207 /* Make our second (and final) pass over the list of formal parameter types
23208 and output DIEs to represent those types (as necessary). */
23209 for (link = TYPE_ARG_TYPES (function_or_method_type);
23210 link && TREE_VALUE (link);
23211 link = TREE_CHAIN (link))
23212 gen_type_die (TREE_VALUE (link), context_die);
23215 /* We want to generate the DIE for TYPE so that we can generate the
23216 die for MEMBER, which has been defined; we will need to refer back
23217 to the member declaration nested within TYPE. If we're trying to
23218 generate minimal debug info for TYPE, processing TYPE won't do the
23219 trick; we need to attach the member declaration by hand. */
23221 static void
23222 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23224 gen_type_die (type, context_die);
23226 /* If we're trying to avoid duplicate debug info, we may not have
23227 emitted the member decl for this function. Emit it now. */
23228 if (TYPE_STUB_DECL (type)
23229 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23230 && ! lookup_decl_die (member))
23232 dw_die_ref type_die;
23233 gcc_assert (!decl_ultimate_origin (member));
23235 type_die = lookup_type_die_strip_naming_typedef (type);
23236 if (TREE_CODE (member) == FUNCTION_DECL)
23237 gen_subprogram_die (member, type_die);
23238 else if (TREE_CODE (member) == FIELD_DECL)
23240 /* Ignore the nameless fields that are used to skip bits but handle
23241 C++ anonymous unions and structs. */
23242 if (DECL_NAME (member) != NULL_TREE
23243 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23244 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23246 struct vlr_context vlr_ctx = {
23247 DECL_CONTEXT (member), /* struct_type */
23248 NULL_TREE /* variant_part_offset */
23250 gen_type_die (member_declared_type (member), type_die);
23251 gen_field_die (member, &vlr_ctx, type_die);
23254 else
23255 gen_variable_die (member, NULL_TREE, type_die);
23259 /* Forward declare these functions, because they are mutually recursive
23260 with their set_block_* pairing functions. */
23261 static void set_decl_origin_self (tree);
23263 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23264 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23265 that it points to the node itself, thus indicating that the node is its
23266 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23267 the given node is NULL, recursively descend the decl/block tree which
23268 it is the root of, and for each other ..._DECL or BLOCK node contained
23269 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23270 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23271 values to point to themselves. */
23273 static void
23274 set_block_origin_self (tree stmt)
23276 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23278 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23281 tree local_decl;
23283 for (local_decl = BLOCK_VARS (stmt);
23284 local_decl != NULL_TREE;
23285 local_decl = DECL_CHAIN (local_decl))
23286 /* Do not recurse on nested functions since the inlining status
23287 of parent and child can be different as per the DWARF spec. */
23288 if (TREE_CODE (local_decl) != FUNCTION_DECL
23289 && !DECL_EXTERNAL (local_decl))
23290 set_decl_origin_self (local_decl);
23294 tree subblock;
23296 for (subblock = BLOCK_SUBBLOCKS (stmt);
23297 subblock != NULL_TREE;
23298 subblock = BLOCK_CHAIN (subblock))
23299 set_block_origin_self (subblock); /* Recurse. */
23304 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23305 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23306 node to so that it points to the node itself, thus indicating that the
23307 node represents its own (abstract) origin. Additionally, if the
23308 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23309 the decl/block tree of which the given node is the root of, and for
23310 each other ..._DECL or BLOCK node contained therein whose
23311 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23312 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23313 point to themselves. */
23315 static void
23316 set_decl_origin_self (tree decl)
23318 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23320 DECL_ABSTRACT_ORIGIN (decl) = decl;
23321 if (TREE_CODE (decl) == FUNCTION_DECL)
23323 tree arg;
23325 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23326 DECL_ABSTRACT_ORIGIN (arg) = arg;
23327 if (DECL_INITIAL (decl) != NULL_TREE
23328 && DECL_INITIAL (decl) != error_mark_node)
23329 set_block_origin_self (DECL_INITIAL (decl));
23334 /* Mark the early DIE for DECL as the abstract instance. */
23336 static void
23337 dwarf2out_abstract_function (tree decl)
23339 dw_die_ref old_die;
23341 /* Make sure we have the actual abstract inline, not a clone. */
23342 decl = DECL_ORIGIN (decl);
23344 if (DECL_IGNORED_P (decl))
23345 return;
23347 /* In LTO we're all set. We already created abstract instances
23348 early and we want to avoid creating a concrete instance of that
23349 if we don't output it. */
23350 if (in_lto_p)
23351 return;
23353 old_die = lookup_decl_die (decl);
23354 gcc_assert (old_die != NULL);
23355 if (get_AT (old_die, DW_AT_inline))
23356 /* We've already generated the abstract instance. */
23357 return;
23359 /* Go ahead and put DW_AT_inline on the DIE. */
23360 if (DECL_DECLARED_INLINE_P (decl))
23362 if (cgraph_function_possibly_inlined_p (decl))
23363 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23364 else
23365 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23367 else
23369 if (cgraph_function_possibly_inlined_p (decl))
23370 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23371 else
23372 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23375 if (DECL_DECLARED_INLINE_P (decl)
23376 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23377 add_AT_flag (old_die, DW_AT_artificial, 1);
23379 set_decl_origin_self (decl);
23382 /* Helper function of premark_used_types() which gets called through
23383 htab_traverse.
23385 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23386 marked as unused by prune_unused_types. */
23388 bool
23389 premark_used_types_helper (tree const &type, void *)
23391 dw_die_ref die;
23393 die = lookup_type_die (type);
23394 if (die != NULL)
23395 die->die_perennial_p = 1;
23396 return true;
23399 /* Helper function of premark_types_used_by_global_vars which gets called
23400 through htab_traverse.
23402 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23403 marked as unused by prune_unused_types. The DIE of the type is marked
23404 only if the global variable using the type will actually be emitted. */
23407 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23408 void *)
23410 struct types_used_by_vars_entry *entry;
23411 dw_die_ref die;
23413 entry = (struct types_used_by_vars_entry *) *slot;
23414 gcc_assert (entry->type != NULL
23415 && entry->var_decl != NULL);
23416 die = lookup_type_die (entry->type);
23417 if (die)
23419 /* Ask cgraph if the global variable really is to be emitted.
23420 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23421 varpool_node *node = varpool_node::get (entry->var_decl);
23422 if (node && node->definition)
23424 die->die_perennial_p = 1;
23425 /* Keep the parent DIEs as well. */
23426 while ((die = die->die_parent) && die->die_perennial_p == 0)
23427 die->die_perennial_p = 1;
23430 return 1;
23433 /* Mark all members of used_types_hash as perennial. */
23435 static void
23436 premark_used_types (struct function *fun)
23438 if (fun && fun->used_types_hash)
23439 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23442 /* Mark all members of types_used_by_vars_entry as perennial. */
23444 static void
23445 premark_types_used_by_global_vars (void)
23447 if (types_used_by_vars_hash)
23448 types_used_by_vars_hash
23449 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23452 /* Mark all variables used by the symtab as perennial. */
23454 static void
23455 premark_used_variables (void)
23457 /* Mark DIEs in the symtab as used. */
23458 varpool_node *var;
23459 FOR_EACH_VARIABLE (var)
23461 dw_die_ref die = lookup_decl_die (var->decl);
23462 if (die)
23463 die->die_perennial_p = 1;
23467 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23468 for CA_LOC call arg loc node. */
23470 static dw_die_ref
23471 gen_call_site_die (tree decl, dw_die_ref subr_die,
23472 struct call_arg_loc_node *ca_loc)
23474 dw_die_ref stmt_die = NULL, die;
23475 tree block = ca_loc->block;
23477 while (block
23478 && block != DECL_INITIAL (decl)
23479 && TREE_CODE (block) == BLOCK)
23481 stmt_die = lookup_block_die (block);
23482 if (stmt_die)
23483 break;
23484 block = BLOCK_SUPERCONTEXT (block);
23486 if (stmt_die == NULL)
23487 stmt_die = subr_die;
23488 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23489 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23490 if (ca_loc->tail_call_p)
23491 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23492 if (ca_loc->symbol_ref)
23494 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23495 if (tdie)
23496 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23497 else
23498 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23499 false);
23501 return die;
23504 /* Generate a DIE to represent a declared function (either file-scope or
23505 block-local). */
23507 static void
23508 gen_subprogram_die (tree decl, dw_die_ref context_die)
23510 tree origin = decl_ultimate_origin (decl);
23511 dw_die_ref subr_die;
23512 dw_die_ref old_die = lookup_decl_die (decl);
23513 bool old_die_had_no_children = false;
23515 /* This function gets called multiple times for different stages of
23516 the debug process. For example, for func() in this code:
23518 namespace S
23520 void func() { ... }
23523 ...we get called 4 times. Twice in early debug and twice in
23524 late debug:
23526 Early debug
23527 -----------
23529 1. Once while generating func() within the namespace. This is
23530 the declaration. The declaration bit below is set, as the
23531 context is the namespace.
23533 A new DIE will be generated with DW_AT_declaration set.
23535 2. Once for func() itself. This is the specification. The
23536 declaration bit below is clear as the context is the CU.
23538 We will use the cached DIE from (1) to create a new DIE with
23539 DW_AT_specification pointing to the declaration in (1).
23541 Late debug via rest_of_handle_final()
23542 -------------------------------------
23544 3. Once generating func() within the namespace. This is also the
23545 declaration, as in (1), but this time we will early exit below
23546 as we have a cached DIE and a declaration needs no additional
23547 annotations (no locations), as the source declaration line
23548 info is enough.
23550 4. Once for func() itself. As in (2), this is the specification,
23551 but this time we will re-use the cached DIE, and just annotate
23552 it with the location information that should now be available.
23554 For something without namespaces, but with abstract instances, we
23555 are also called a multiple times:
23557 class Base
23559 public:
23560 Base (); // constructor declaration (1)
23563 Base::Base () { } // constructor specification (2)
23565 Early debug
23566 -----------
23568 1. Once for the Base() constructor by virtue of it being a
23569 member of the Base class. This is done via
23570 rest_of_type_compilation.
23572 This is a declaration, so a new DIE will be created with
23573 DW_AT_declaration.
23575 2. Once for the Base() constructor definition, but this time
23576 while generating the abstract instance of the base
23577 constructor (__base_ctor) which is being generated via early
23578 debug of reachable functions.
23580 Even though we have a cached version of the declaration (1),
23581 we will create a DW_AT_specification of the declaration DIE
23582 in (1).
23584 3. Once for the __base_ctor itself, but this time, we generate
23585 an DW_AT_abstract_origin version of the DW_AT_specification in
23586 (2).
23588 Late debug via rest_of_handle_final
23589 -----------------------------------
23591 4. One final time for the __base_ctor (which will have a cached
23592 DIE with DW_AT_abstract_origin created in (3). This time,
23593 we will just annotate the location information now
23594 available.
23596 int declaration = (current_function_decl != decl
23597 || (!DECL_INITIAL (decl) && !origin)
23598 || class_or_namespace_scope_p (context_die));
23600 /* A declaration that has been previously dumped needs no
23601 additional information. */
23602 if (old_die && declaration)
23603 return;
23605 if (in_lto_p && old_die && old_die->die_child == NULL)
23606 old_die_had_no_children = true;
23608 /* Now that the C++ front end lazily declares artificial member fns, we
23609 might need to retrofit the declaration into its class. */
23610 if (!declaration && !origin && !old_die
23611 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23612 && !class_or_namespace_scope_p (context_die)
23613 && debug_info_level > DINFO_LEVEL_TERSE)
23614 old_die = force_decl_die (decl);
23616 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23617 if (origin != NULL)
23619 gcc_assert (!declaration || local_scope_p (context_die));
23621 /* Fixup die_parent for the abstract instance of a nested
23622 inline function. */
23623 if (old_die && old_die->die_parent == NULL)
23624 add_child_die (context_die, old_die);
23626 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23628 /* If we have a DW_AT_abstract_origin we have a working
23629 cached version. */
23630 subr_die = old_die;
23632 else
23634 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23635 add_abstract_origin_attribute (subr_die, origin);
23636 /* This is where the actual code for a cloned function is.
23637 Let's emit linkage name attribute for it. This helps
23638 debuggers to e.g, set breakpoints into
23639 constructors/destructors when the user asks "break
23640 K::K". */
23641 add_linkage_name (subr_die, decl);
23644 /* A cached copy, possibly from early dwarf generation. Reuse as
23645 much as possible. */
23646 else if (old_die)
23648 if (!get_AT_flag (old_die, DW_AT_declaration)
23649 /* We can have a normal definition following an inline one in the
23650 case of redefinition of GNU C extern inlines.
23651 It seems reasonable to use AT_specification in this case. */
23652 && !get_AT (old_die, DW_AT_inline))
23654 /* Detect and ignore this case, where we are trying to output
23655 something we have already output. */
23656 if (get_AT (old_die, DW_AT_low_pc)
23657 || get_AT (old_die, DW_AT_ranges))
23658 return;
23660 /* If we have no location information, this must be a
23661 partially generated DIE from early dwarf generation.
23662 Fall through and generate it. */
23665 /* If the definition comes from the same place as the declaration,
23666 maybe use the old DIE. We always want the DIE for this function
23667 that has the *_pc attributes to be under comp_unit_die so the
23668 debugger can find it. We also need to do this for abstract
23669 instances of inlines, since the spec requires the out-of-line copy
23670 to have the same parent. For local class methods, this doesn't
23671 apply; we just use the old DIE. */
23672 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23673 struct dwarf_file_data * file_index = lookup_filename (s.file);
23674 if (((is_unit_die (old_die->die_parent)
23675 /* This condition fixes the inconsistency/ICE with the
23676 following Fortran test (or some derivative thereof) while
23677 building libgfortran:
23679 module some_m
23680 contains
23681 logical function funky (FLAG)
23682 funky = .true.
23683 end function
23684 end module
23686 || (old_die->die_parent
23687 && old_die->die_parent->die_tag == DW_TAG_module)
23688 || local_scope_p (old_die->die_parent)
23689 || context_die == NULL)
23690 && (DECL_ARTIFICIAL (decl)
23691 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23692 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23693 == (unsigned) s.line)
23694 && (!debug_column_info
23695 || s.column == 0
23696 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23697 == (unsigned) s.column)))))
23698 /* With LTO if there's an abstract instance for
23699 the old DIE, this is a concrete instance and
23700 thus re-use the DIE. */
23701 || get_AT (old_die, DW_AT_abstract_origin))
23703 subr_die = old_die;
23705 /* Clear out the declaration attribute, but leave the
23706 parameters so they can be augmented with location
23707 information later. Unless this was a declaration, in
23708 which case, wipe out the nameless parameters and recreate
23709 them further down. */
23710 if (remove_AT (subr_die, DW_AT_declaration))
23713 remove_AT (subr_die, DW_AT_object_pointer);
23714 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23717 /* Make a specification pointing to the previously built
23718 declaration. */
23719 else
23721 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23722 add_AT_specification (subr_die, old_die);
23723 add_pubname (decl, subr_die);
23724 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23725 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23726 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23727 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23728 if (debug_column_info
23729 && s.column
23730 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23731 != (unsigned) s.column))
23732 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23734 /* If the prototype had an 'auto' or 'decltype(auto)' in
23735 the return type, emit the real type on the definition die. */
23736 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23738 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23739 while (die
23740 && (die->die_tag == DW_TAG_reference_type
23741 || die->die_tag == DW_TAG_rvalue_reference_type
23742 || die->die_tag == DW_TAG_pointer_type
23743 || die->die_tag == DW_TAG_const_type
23744 || die->die_tag == DW_TAG_volatile_type
23745 || die->die_tag == DW_TAG_restrict_type
23746 || die->die_tag == DW_TAG_array_type
23747 || die->die_tag == DW_TAG_ptr_to_member_type
23748 || die->die_tag == DW_TAG_subroutine_type))
23749 die = get_AT_ref (die, DW_AT_type);
23750 if (die == auto_die || die == decltype_auto_die)
23751 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23752 TYPE_UNQUALIFIED, false, context_die);
23755 /* When we process the method declaration, we haven't seen
23756 the out-of-class defaulted definition yet, so we have to
23757 recheck now. */
23758 if ((dwarf_version >= 5 || ! dwarf_strict)
23759 && !get_AT (subr_die, DW_AT_defaulted))
23761 int defaulted
23762 = lang_hooks.decls.decl_dwarf_attribute (decl,
23763 DW_AT_defaulted);
23764 if (defaulted != -1)
23766 /* Other values must have been handled before. */
23767 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23768 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23773 /* Create a fresh DIE for anything else. */
23774 else
23776 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23778 if (TREE_PUBLIC (decl))
23779 add_AT_flag (subr_die, DW_AT_external, 1);
23781 add_name_and_src_coords_attributes (subr_die, decl);
23782 add_pubname (decl, subr_die);
23783 if (debug_info_level > DINFO_LEVEL_TERSE)
23785 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23786 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23787 TYPE_UNQUALIFIED, false, context_die);
23790 add_pure_or_virtual_attribute (subr_die, decl);
23791 if (DECL_ARTIFICIAL (decl))
23792 add_AT_flag (subr_die, DW_AT_artificial, 1);
23794 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23795 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23797 add_alignment_attribute (subr_die, decl);
23799 add_accessibility_attribute (subr_die, decl);
23802 /* Unless we have an existing non-declaration DIE, equate the new
23803 DIE. */
23804 if (!old_die || is_declaration_die (old_die))
23805 equate_decl_number_to_die (decl, subr_die);
23807 if (declaration)
23809 if (!old_die || !get_AT (old_die, DW_AT_inline))
23811 add_AT_flag (subr_die, DW_AT_declaration, 1);
23813 /* If this is an explicit function declaration then generate
23814 a DW_AT_explicit attribute. */
23815 if ((dwarf_version >= 3 || !dwarf_strict)
23816 && lang_hooks.decls.decl_dwarf_attribute (decl,
23817 DW_AT_explicit) == 1)
23818 add_AT_flag (subr_die, DW_AT_explicit, 1);
23820 /* If this is a C++11 deleted special function member then generate
23821 a DW_AT_deleted attribute. */
23822 if ((dwarf_version >= 5 || !dwarf_strict)
23823 && lang_hooks.decls.decl_dwarf_attribute (decl,
23824 DW_AT_deleted) == 1)
23825 add_AT_flag (subr_die, DW_AT_deleted, 1);
23827 /* If this is a C++11 defaulted special function member then
23828 generate a DW_AT_defaulted attribute. */
23829 if (dwarf_version >= 5 || !dwarf_strict)
23831 int defaulted
23832 = lang_hooks.decls.decl_dwarf_attribute (decl,
23833 DW_AT_defaulted);
23834 if (defaulted != -1)
23835 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23838 /* If this is a C++11 non-static member function with & ref-qualifier
23839 then generate a DW_AT_reference attribute. */
23840 if ((dwarf_version >= 5 || !dwarf_strict)
23841 && lang_hooks.decls.decl_dwarf_attribute (decl,
23842 DW_AT_reference) == 1)
23843 add_AT_flag (subr_die, DW_AT_reference, 1);
23845 /* If this is a C++11 non-static member function with &&
23846 ref-qualifier then generate a DW_AT_reference attribute. */
23847 if ((dwarf_version >= 5 || !dwarf_strict)
23848 && lang_hooks.decls.decl_dwarf_attribute (decl,
23849 DW_AT_rvalue_reference)
23850 == 1)
23851 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23854 /* For non DECL_EXTERNALs, if range information is available, fill
23855 the DIE with it. */
23856 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23858 HOST_WIDE_INT cfa_fb_offset;
23860 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23862 if (!crtl->has_bb_partition)
23864 dw_fde_ref fde = fun->fde;
23865 if (fde->dw_fde_begin)
23867 /* We have already generated the labels. */
23868 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23869 fde->dw_fde_end, false);
23871 else
23873 /* Create start/end labels and add the range. */
23874 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23875 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23876 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23877 current_function_funcdef_no);
23878 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23879 current_function_funcdef_no);
23880 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23881 false);
23884 #if VMS_DEBUGGING_INFO
23885 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23886 Section 2.3 Prologue and Epilogue Attributes:
23887 When a breakpoint is set on entry to a function, it is generally
23888 desirable for execution to be suspended, not on the very first
23889 instruction of the function, but rather at a point after the
23890 function's frame has been set up, after any language defined local
23891 declaration processing has been completed, and before execution of
23892 the first statement of the function begins. Debuggers generally
23893 cannot properly determine where this point is. Similarly for a
23894 breakpoint set on exit from a function. The prologue and epilogue
23895 attributes allow a compiler to communicate the location(s) to use. */
23898 if (fde->dw_fde_vms_end_prologue)
23899 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23900 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23902 if (fde->dw_fde_vms_begin_epilogue)
23903 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23904 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23906 #endif
23909 else
23911 /* Generate pubnames entries for the split function code ranges. */
23912 dw_fde_ref fde = fun->fde;
23914 if (fde->dw_fde_second_begin)
23916 if (dwarf_version >= 3 || !dwarf_strict)
23918 /* We should use ranges for non-contiguous code section
23919 addresses. Use the actual code range for the initial
23920 section, since the HOT/COLD labels might precede an
23921 alignment offset. */
23922 bool range_list_added = false;
23923 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23924 fde->dw_fde_end, &range_list_added,
23925 false);
23926 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23927 fde->dw_fde_second_end,
23928 &range_list_added, false);
23929 if (range_list_added)
23930 add_ranges (NULL);
23932 else
23934 /* There is no real support in DW2 for this .. so we make
23935 a work-around. First, emit the pub name for the segment
23936 containing the function label. Then make and emit a
23937 simplified subprogram DIE for the second segment with the
23938 name pre-fixed by __hot/cold_sect_of_. We use the same
23939 linkage name for the second die so that gdb will find both
23940 sections when given "b foo". */
23941 const char *name = NULL;
23942 tree decl_name = DECL_NAME (decl);
23943 dw_die_ref seg_die;
23945 /* Do the 'primary' section. */
23946 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23947 fde->dw_fde_end, false);
23949 /* Build a minimal DIE for the secondary section. */
23950 seg_die = new_die (DW_TAG_subprogram,
23951 subr_die->die_parent, decl);
23953 if (TREE_PUBLIC (decl))
23954 add_AT_flag (seg_die, DW_AT_external, 1);
23956 if (decl_name != NULL
23957 && IDENTIFIER_POINTER (decl_name) != NULL)
23959 name = dwarf2_name (decl, 1);
23960 if (! DECL_ARTIFICIAL (decl))
23961 add_src_coords_attributes (seg_die, decl);
23963 add_linkage_name (seg_die, decl);
23965 gcc_assert (name != NULL);
23966 add_pure_or_virtual_attribute (seg_die, decl);
23967 if (DECL_ARTIFICIAL (decl))
23968 add_AT_flag (seg_die, DW_AT_artificial, 1);
23970 name = concat ("__second_sect_of_", name, NULL);
23971 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23972 fde->dw_fde_second_end, false);
23973 add_name_attribute (seg_die, name);
23974 if (want_pubnames ())
23975 add_pubname_string (name, seg_die);
23978 else
23979 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23980 false);
23983 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23985 /* We define the "frame base" as the function's CFA. This is more
23986 convenient for several reasons: (1) It's stable across the prologue
23987 and epilogue, which makes it better than just a frame pointer,
23988 (2) With dwarf3, there exists a one-byte encoding that allows us
23989 to reference the .debug_frame data by proxy, but failing that,
23990 (3) We can at least reuse the code inspection and interpretation
23991 code that determines the CFA position at various points in the
23992 function. */
23993 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23995 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23996 add_AT_loc (subr_die, DW_AT_frame_base, op);
23998 else
24000 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
24001 if (list->dw_loc_next)
24002 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
24003 else
24004 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
24007 /* Compute a displacement from the "steady-state frame pointer" to
24008 the CFA. The former is what all stack slots and argument slots
24009 will reference in the rtl; the latter is what we've told the
24010 debugger about. We'll need to adjust all frame_base references
24011 by this displacement. */
24012 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
24014 if (fun->static_chain_decl)
24016 /* DWARF requires here a location expression that computes the
24017 address of the enclosing subprogram's frame base. The machinery
24018 in tree-nested.cc is supposed to store this specific address in the
24019 last field of the FRAME record. */
24020 const tree frame_type
24021 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
24022 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
24024 tree fb_expr
24025 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
24026 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
24027 fb_expr, fb_decl, NULL_TREE);
24029 add_AT_location_description (subr_die, DW_AT_static_link,
24030 loc_list_from_tree (fb_expr, 0, NULL));
24033 resolve_variable_values ();
24036 /* Generate child dies for template parameters. */
24037 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
24038 gen_generic_params_dies (decl);
24040 /* Now output descriptions of the arguments for this function. This gets
24041 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
24042 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
24043 `...' at the end of the formal parameter list. In order to find out if
24044 there was a trailing ellipsis or not, we must instead look at the type
24045 associated with the FUNCTION_DECL. This will be a node of type
24046 FUNCTION_TYPE. If the chain of type nodes hanging off of this
24047 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
24048 an ellipsis at the end. */
24050 /* In the case where we are describing a mere function declaration, all we
24051 need to do here (and all we *can* do here) is to describe the *types* of
24052 its formal parameters. */
24053 if (debug_info_level <= DINFO_LEVEL_TERSE)
24055 else if (declaration)
24056 gen_formal_types_die (decl, subr_die);
24057 else
24059 /* Generate DIEs to represent all known formal parameters. */
24060 tree parm = DECL_ARGUMENTS (decl);
24061 tree generic_decl = early_dwarf
24062 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
24063 tree generic_decl_parm = generic_decl
24064 ? DECL_ARGUMENTS (generic_decl)
24065 : NULL;
24067 /* Now we want to walk the list of parameters of the function and
24068 emit their relevant DIEs.
24070 We consider the case of DECL being an instance of a generic function
24071 as well as it being a normal function.
24073 If DECL is an instance of a generic function we walk the
24074 parameters of the generic function declaration _and_ the parameters of
24075 DECL itself. This is useful because we want to emit specific DIEs for
24076 function parameter packs and those are declared as part of the
24077 generic function declaration. In that particular case,
24078 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
24079 That DIE has children DIEs representing the set of arguments
24080 of the pack. Note that the set of pack arguments can be empty.
24081 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24082 children DIE.
24084 Otherwise, we just consider the parameters of DECL. */
24085 while (generic_decl_parm || parm)
24087 if (generic_decl_parm
24088 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24089 gen_formal_parameter_pack_die (generic_decl_parm,
24090 parm, subr_die,
24091 &parm);
24092 else if (parm)
24094 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24096 if (early_dwarf
24097 && parm == DECL_ARGUMENTS (decl)
24098 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24099 && parm_die
24100 && (dwarf_version >= 3 || !dwarf_strict))
24101 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24103 parm = DECL_CHAIN (parm);
24106 if (generic_decl_parm)
24107 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24110 /* Decide whether we need an unspecified_parameters DIE at the end.
24111 There are 2 more cases to do this for: 1) the ansi ... declaration -
24112 this is detectable when the end of the arg list is not a
24113 void_type_node 2) an unprototyped function declaration (not a
24114 definition). This just means that we have no info about the
24115 parameters at all. */
24116 if (early_dwarf)
24118 if (prototype_p (TREE_TYPE (decl)))
24120 /* This is the prototyped case, check for.... */
24121 if (stdarg_p (TREE_TYPE (decl)))
24122 gen_unspecified_parameters_die (decl, subr_die);
24124 else if (DECL_INITIAL (decl) == NULL_TREE)
24125 gen_unspecified_parameters_die (decl, subr_die);
24127 else if ((subr_die != old_die || old_die_had_no_children)
24128 && prototype_p (TREE_TYPE (decl))
24129 && stdarg_p (TREE_TYPE (decl)))
24130 gen_unspecified_parameters_die (decl, subr_die);
24133 if (subr_die != old_die)
24134 /* Add the calling convention attribute if requested. */
24135 add_calling_convention_attribute (subr_die, decl);
24137 /* Output Dwarf info for all of the stuff within the body of the function
24138 (if it has one - it may be just a declaration).
24140 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24141 a function. This BLOCK actually represents the outermost binding contour
24142 for the function, i.e. the contour in which the function's formal
24143 parameters and labels get declared. Curiously, it appears that the front
24144 end doesn't actually put the PARM_DECL nodes for the current function onto
24145 the BLOCK_VARS list for this outer scope, but are strung off of the
24146 DECL_ARGUMENTS list for the function instead.
24148 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24149 the LABEL_DECL nodes for the function however, and we output DWARF info
24150 for those in decls_for_scope. Just within the `outer_scope' there will be
24151 a BLOCK node representing the function's outermost pair of curly braces,
24152 and any blocks used for the base and member initializers of a C++
24153 constructor function. */
24154 tree outer_scope = DECL_INITIAL (decl);
24155 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24157 int call_site_note_count = 0;
24158 int tail_call_site_note_count = 0;
24160 /* Emit a DW_TAG_variable DIE for a named return value. */
24161 if (DECL_NAME (DECL_RESULT (decl)))
24162 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24164 /* The first time through decls_for_scope we will generate the
24165 DIEs for the locals. The second time, we fill in the
24166 location info. */
24167 decls_for_scope (outer_scope, subr_die);
24169 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24171 struct call_arg_loc_node *ca_loc;
24172 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24174 dw_die_ref die = NULL;
24175 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24176 rtx arg, next_arg;
24177 tree arg_decl = NULL_TREE;
24179 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
24180 ? XEXP (ca_loc->call_arg_loc_note, 0)
24181 : NULL_RTX);
24182 arg; arg = next_arg)
24184 dw_loc_descr_ref reg, val;
24185 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24186 dw_die_ref cdie, tdie = NULL;
24188 next_arg = XEXP (arg, 1);
24189 if (REG_P (XEXP (XEXP (arg, 0), 0))
24190 && next_arg
24191 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24192 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24193 && REGNO (XEXP (XEXP (arg, 0), 0))
24194 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24195 next_arg = XEXP (next_arg, 1);
24196 if (mode == VOIDmode)
24198 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24199 if (mode == VOIDmode)
24200 mode = GET_MODE (XEXP (arg, 0));
24202 if (mode == VOIDmode || mode == BLKmode)
24203 continue;
24204 /* Get dynamic information about call target only if we
24205 have no static information: we cannot generate both
24206 DW_AT_call_origin and DW_AT_call_target
24207 attributes. */
24208 if (ca_loc->symbol_ref == NULL_RTX)
24210 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24212 tloc = XEXP (XEXP (arg, 0), 1);
24213 continue;
24215 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24216 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24218 tlocc = XEXP (XEXP (arg, 0), 1);
24219 continue;
24222 reg = NULL;
24223 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24224 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24225 VAR_INIT_STATUS_INITIALIZED);
24226 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24228 rtx mem = XEXP (XEXP (arg, 0), 0);
24229 reg = mem_loc_descriptor (XEXP (mem, 0),
24230 get_address_mode (mem),
24231 GET_MODE (mem),
24232 VAR_INIT_STATUS_INITIALIZED);
24234 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24235 == DEBUG_PARAMETER_REF)
24237 tree tdecl
24238 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24239 tdie = lookup_decl_die (tdecl);
24240 if (tdie == NULL)
24241 continue;
24242 arg_decl = tdecl;
24244 else
24245 continue;
24246 if (reg == NULL
24247 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24248 != DEBUG_PARAMETER_REF)
24249 continue;
24250 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24251 VOIDmode,
24252 VAR_INIT_STATUS_INITIALIZED);
24253 if (val == NULL)
24254 continue;
24255 if (die == NULL)
24256 die = gen_call_site_die (decl, subr_die, ca_loc);
24257 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24258 NULL_TREE);
24259 add_desc_attribute (cdie, arg_decl);
24260 if (reg != NULL)
24261 add_AT_loc (cdie, DW_AT_location, reg);
24262 else if (tdie != NULL)
24263 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24264 tdie);
24265 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24266 if (next_arg != XEXP (arg, 1))
24268 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24269 if (mode == VOIDmode)
24270 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24271 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24272 0), 1),
24273 mode, VOIDmode,
24274 VAR_INIT_STATUS_INITIALIZED);
24275 if (val != NULL)
24276 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24277 val);
24280 if (die == NULL
24281 && (ca_loc->symbol_ref || tloc))
24282 die = gen_call_site_die (decl, subr_die, ca_loc);
24283 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24285 dw_loc_descr_ref tval = NULL;
24287 if (tloc != NULL_RTX)
24288 tval = mem_loc_descriptor (tloc,
24289 GET_MODE (tloc) == VOIDmode
24290 ? Pmode : GET_MODE (tloc),
24291 VOIDmode,
24292 VAR_INIT_STATUS_INITIALIZED);
24293 if (tval)
24294 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24295 else if (tlocc != NULL_RTX)
24297 tval = mem_loc_descriptor (tlocc,
24298 GET_MODE (tlocc) == VOIDmode
24299 ? Pmode : GET_MODE (tlocc),
24300 VOIDmode,
24301 VAR_INIT_STATUS_INITIALIZED);
24302 if (tval)
24303 add_AT_loc (die,
24304 dwarf_AT (DW_AT_call_target_clobbered),
24305 tval);
24308 if (die != NULL)
24310 call_site_note_count++;
24311 if (ca_loc->tail_call_p)
24312 tail_call_site_note_count++;
24316 call_arg_locations = NULL;
24317 call_arg_loc_last = NULL;
24318 if (tail_call_site_count >= 0
24319 && tail_call_site_count == tail_call_site_note_count
24320 && (!dwarf_strict || dwarf_version >= 5))
24322 if (call_site_count >= 0
24323 && call_site_count == call_site_note_count)
24324 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24325 else
24326 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24328 call_site_count = -1;
24329 tail_call_site_count = -1;
24332 /* Mark used types after we have created DIEs for the functions scopes. */
24333 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24336 /* Returns a hash value for X (which really is a die_struct). */
24338 hashval_t
24339 block_die_hasher::hash (die_struct *d)
24341 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24344 /* Return true if decl_id and die_parent of die_struct X is the same
24345 as decl_id and die_parent of die_struct Y. */
24347 bool
24348 block_die_hasher::equal (die_struct *x, die_struct *y)
24350 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24353 /* Hold information about markers for inlined entry points. */
24354 struct GTY ((for_user)) inline_entry_data
24356 /* The block that's the inlined_function_outer_scope for an inlined
24357 function. */
24358 tree block;
24360 /* The label at the inlined entry point. */
24361 const char *label_pfx;
24362 unsigned int label_num;
24364 /* The view number to be used as the inlined entry point. */
24365 var_loc_view view;
24368 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24370 typedef tree compare_type;
24371 static inline hashval_t hash (const inline_entry_data *);
24372 static inline bool equal (const inline_entry_data *, const_tree);
24375 /* Hash table routines for inline_entry_data. */
24377 inline hashval_t
24378 inline_entry_data_hasher::hash (const inline_entry_data *data)
24380 return htab_hash_pointer (data->block);
24383 inline bool
24384 inline_entry_data_hasher::equal (const inline_entry_data *data,
24385 const_tree block)
24387 return data->block == block;
24390 /* Inlined entry points pending DIE creation in this compilation unit. */
24392 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24395 /* Return TRUE if DECL, which may have been previously generated as
24396 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24397 true if decl (or its origin) is either an extern declaration or a
24398 class/namespace scoped declaration.
24400 The declare_in_namespace support causes us to get two DIEs for one
24401 variable, both of which are declarations. We want to avoid
24402 considering one to be a specification, so we must test for
24403 DECLARATION and DW_AT_declaration. */
24404 static inline bool
24405 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24407 return (old_die && TREE_STATIC (decl) && !declaration
24408 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24411 /* Return true if DECL is a local static. */
24413 static inline bool
24414 local_function_static (tree decl)
24416 gcc_assert (VAR_P (decl));
24417 return TREE_STATIC (decl)
24418 && DECL_CONTEXT (decl)
24419 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24422 /* Return true iff DECL overrides (presumably completes) the type of
24423 OLD_DIE within CONTEXT_DIE. */
24425 static bool
24426 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24427 dw_die_ref context_die)
24429 tree type = TREE_TYPE (decl);
24430 int cv_quals;
24432 if (decl_by_reference_p (decl))
24434 type = TREE_TYPE (type);
24435 cv_quals = TYPE_UNQUALIFIED;
24437 else
24438 cv_quals = decl_quals (decl);
24440 dw_die_ref type_die = modified_type_die (type,
24441 cv_quals | TYPE_QUALS (type),
24442 false,
24443 context_die);
24445 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24447 return type_die != old_type_die;
24450 /* Generate a DIE to represent a declared data object.
24451 Either DECL or ORIGIN must be non-null. */
24453 static void
24454 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24456 HOST_WIDE_INT off = 0;
24457 tree com_decl;
24458 tree decl_or_origin = decl ? decl : origin;
24459 tree ultimate_origin;
24460 dw_die_ref var_die;
24461 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24462 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24463 || class_or_namespace_scope_p (context_die));
24464 bool specialization_p = false;
24465 bool no_linkage_name = false;
24467 /* While C++ inline static data members have definitions inside of the
24468 class, force the first DIE to be a declaration, then let gen_member_die
24469 reparent it to the class context and call gen_variable_die again
24470 to create the outside of the class DIE for the definition. */
24471 if (!declaration
24472 && old_die == NULL
24473 && decl
24474 && DECL_CONTEXT (decl)
24475 && TYPE_P (DECL_CONTEXT (decl))
24476 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24478 declaration = true;
24479 if (dwarf_version < 5)
24480 no_linkage_name = true;
24483 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24484 if (decl || ultimate_origin)
24485 origin = ultimate_origin;
24486 com_decl = fortran_common (decl_or_origin, &off);
24488 /* Symbol in common gets emitted as a child of the common block, in the form
24489 of a data member. */
24490 if (com_decl)
24492 dw_die_ref com_die;
24493 dw_loc_list_ref loc = NULL;
24494 die_node com_die_arg;
24496 var_die = lookup_decl_die (decl_or_origin);
24497 if (var_die)
24499 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24501 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24502 if (loc)
24504 if (off)
24506 /* Optimize the common case. */
24507 if (single_element_loc_list_p (loc)
24508 && loc->expr->dw_loc_opc == DW_OP_addr
24509 && loc->expr->dw_loc_next == NULL
24510 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24511 == SYMBOL_REF)
24513 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24514 loc->expr->dw_loc_oprnd1.v.val_addr
24515 = plus_constant (GET_MODE (x), x , off);
24517 else
24518 loc_list_plus_const (loc, off);
24520 add_AT_location_description (var_die, DW_AT_location, loc);
24521 remove_AT (var_die, DW_AT_declaration);
24524 return;
24527 if (common_block_die_table == NULL)
24528 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24530 com_die_arg.decl_id = DECL_UID (com_decl);
24531 com_die_arg.die_parent = context_die;
24532 com_die = common_block_die_table->find (&com_die_arg);
24533 if (! early_dwarf)
24534 loc = loc_list_from_tree (com_decl, 2, NULL);
24535 if (com_die == NULL)
24537 const char *cnam
24538 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24539 die_node **slot;
24541 com_die = new_die (DW_TAG_common_block, context_die, decl);
24542 add_name_and_src_coords_attributes (com_die, com_decl);
24543 if (loc)
24545 add_AT_location_description (com_die, DW_AT_location, loc);
24546 /* Avoid sharing the same loc descriptor between
24547 DW_TAG_common_block and DW_TAG_variable. */
24548 loc = loc_list_from_tree (com_decl, 2, NULL);
24550 else if (DECL_EXTERNAL (decl_or_origin))
24551 add_AT_flag (com_die, DW_AT_declaration, 1);
24552 if (want_pubnames ())
24553 add_pubname_string (cnam, com_die); /* ??? needed? */
24554 com_die->decl_id = DECL_UID (com_decl);
24555 slot = common_block_die_table->find_slot (com_die, INSERT);
24556 *slot = com_die;
24558 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24560 add_AT_location_description (com_die, DW_AT_location, loc);
24561 loc = loc_list_from_tree (com_decl, 2, NULL);
24562 remove_AT (com_die, DW_AT_declaration);
24564 var_die = new_die (DW_TAG_variable, com_die, decl);
24565 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24566 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24567 decl_quals (decl_or_origin), false,
24568 context_die);
24569 add_alignment_attribute (var_die, decl);
24570 add_AT_flag (var_die, DW_AT_external, 1);
24571 if (loc)
24573 if (off)
24575 /* Optimize the common case. */
24576 if (single_element_loc_list_p (loc)
24577 && loc->expr->dw_loc_opc == DW_OP_addr
24578 && loc->expr->dw_loc_next == NULL
24579 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24581 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24582 loc->expr->dw_loc_oprnd1.v.val_addr
24583 = plus_constant (GET_MODE (x), x, off);
24585 else
24586 loc_list_plus_const (loc, off);
24588 add_AT_location_description (var_die, DW_AT_location, loc);
24590 else if (DECL_EXTERNAL (decl_or_origin))
24591 add_AT_flag (var_die, DW_AT_declaration, 1);
24592 if (decl)
24593 equate_decl_number_to_die (decl, var_die);
24594 return;
24597 if (old_die)
24599 if (declaration)
24601 /* A declaration that has been previously dumped, needs no
24602 further annotations, since it doesn't need location on
24603 the second pass. */
24604 return;
24606 else if (decl_will_get_specification_p (old_die, decl, declaration)
24607 && !get_AT (old_die, DW_AT_specification))
24609 /* Fall-thru so we can make a new variable die along with a
24610 DW_AT_specification. */
24612 else if (origin && old_die->die_parent != context_die)
24614 /* If we will be creating an inlined instance, we need a
24615 new DIE that will get annotated with
24616 DW_AT_abstract_origin. */
24617 gcc_assert (!DECL_ABSTRACT_P (decl));
24619 else
24621 /* If a DIE was dumped early, it still needs location info.
24622 Skip to where we fill the location bits. */
24623 var_die = old_die;
24625 /* ??? In LTRANS we cannot annotate early created variably
24626 modified type DIEs without copying them and adjusting all
24627 references to them. Thus we dumped them again. Also add a
24628 reference to them but beware of -g0 compile and -g link
24629 in which case the reference will be already present. */
24630 tree type = TREE_TYPE (decl_or_origin);
24631 if (in_lto_p
24632 && ! get_AT (var_die, DW_AT_type)
24633 && variably_modified_type_p
24634 (type, decl_function_context (decl_or_origin)))
24636 if (decl_by_reference_p (decl_or_origin))
24637 add_type_attribute (var_die, TREE_TYPE (type),
24638 TYPE_UNQUALIFIED, false, context_die);
24639 else
24640 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24641 false, context_die);
24644 goto gen_variable_die_location;
24648 /* For static data members, the declaration in the class is supposed
24649 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24650 also in DWARF2; the specification should still be DW_TAG_variable
24651 referencing the DW_TAG_member DIE. */
24652 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24653 var_die = new_die (DW_TAG_member, context_die, decl);
24654 else
24655 var_die = new_die (DW_TAG_variable, context_die, decl);
24657 if (origin != NULL)
24658 add_abstract_origin_attribute (var_die, origin);
24660 /* Loop unrolling can create multiple blocks that refer to the same
24661 static variable, so we must test for the DW_AT_declaration flag.
24663 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24664 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24665 sharing them.
24667 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24668 else if (decl_will_get_specification_p (old_die, decl, declaration))
24670 /* This is a definition of a C++ class level static. */
24671 add_AT_specification (var_die, old_die);
24672 specialization_p = true;
24673 if (DECL_NAME (decl))
24675 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24676 struct dwarf_file_data * file_index = lookup_filename (s.file);
24678 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24679 add_AT_file (var_die, DW_AT_decl_file, file_index);
24681 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24682 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24684 if (debug_column_info
24685 && s.column
24686 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24687 != (unsigned) s.column))
24688 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24690 if (old_die->die_tag == DW_TAG_member)
24691 add_linkage_name (var_die, decl);
24694 else
24695 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24697 if ((origin == NULL && !specialization_p)
24698 || (origin != NULL
24699 && !DECL_ABSTRACT_P (decl_or_origin)
24700 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24701 decl_function_context
24702 (decl_or_origin)))
24703 || (old_die && specialization_p
24704 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24706 tree type = TREE_TYPE (decl_or_origin);
24708 if (decl_by_reference_p (decl_or_origin))
24709 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24710 context_die);
24711 else
24712 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24713 context_die);
24716 if (origin == NULL && !specialization_p)
24718 if (TREE_PUBLIC (decl))
24719 add_AT_flag (var_die, DW_AT_external, 1);
24721 if (DECL_ARTIFICIAL (decl))
24722 add_AT_flag (var_die, DW_AT_artificial, 1);
24724 add_alignment_attribute (var_die, decl);
24726 add_accessibility_attribute (var_die, decl);
24729 if (declaration)
24730 add_AT_flag (var_die, DW_AT_declaration, 1);
24732 if (decl && (DECL_ABSTRACT_P (decl)
24733 || !old_die || is_declaration_die (old_die)))
24734 equate_decl_number_to_die (decl, var_die);
24736 gen_variable_die_location:
24737 if (! declaration
24738 && (! DECL_ABSTRACT_P (decl_or_origin)
24739 /* Local static vars are shared between all clones/inlines,
24740 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24741 already set. */
24742 || (VAR_P (decl_or_origin)
24743 && TREE_STATIC (decl_or_origin)
24744 && DECL_RTL_SET_P (decl_or_origin))))
24746 if (early_dwarf)
24748 add_pubname (decl_or_origin, var_die);
24749 /* For global register variables, emit DW_AT_location if possible
24750 already during early_dwarf, as late_global_decl won't be usually
24751 called. */
24752 if (DECL_HARD_REGISTER (decl_or_origin)
24753 && TREE_STATIC (decl_or_origin)
24754 && !decl_by_reference_p (decl_or_origin)
24755 && !get_AT (var_die, DW_AT_location)
24756 && !get_AT (var_die, DW_AT_const_value)
24757 && DECL_RTL_SET_P (decl_or_origin)
24758 && REG_P (DECL_RTL (decl_or_origin)))
24760 dw_loc_descr_ref descr
24761 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24762 VAR_INIT_STATUS_INITIALIZED);
24763 if (descr)
24764 add_AT_loc (var_die, DW_AT_location, descr);
24767 else
24768 add_location_or_const_value_attribute (var_die, decl_or_origin,
24769 decl == NULL);
24771 else
24772 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24774 if ((dwarf_version >= 4 || !dwarf_strict)
24775 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24776 DW_AT_const_expr) == 1
24777 && !get_AT (var_die, DW_AT_const_expr)
24778 && !specialization_p)
24779 add_AT_flag (var_die, DW_AT_const_expr, 1);
24781 if (!dwarf_strict)
24783 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24784 DW_AT_inline);
24785 if (inl != -1
24786 && !get_AT (var_die, DW_AT_inline)
24787 && !specialization_p)
24788 add_AT_unsigned (var_die, DW_AT_inline, inl);
24792 /* Generate a DIE to represent a named constant. */
24794 static void
24795 gen_const_die (tree decl, dw_die_ref context_die)
24797 dw_die_ref const_die;
24798 tree type = TREE_TYPE (decl);
24800 const_die = lookup_decl_die (decl);
24801 if (const_die)
24802 return;
24804 const_die = new_die (DW_TAG_constant, context_die, decl);
24805 equate_decl_number_to_die (decl, const_die);
24806 add_name_and_src_coords_attributes (const_die, decl);
24807 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24808 if (TREE_PUBLIC (decl))
24809 add_AT_flag (const_die, DW_AT_external, 1);
24810 if (DECL_ARTIFICIAL (decl))
24811 add_AT_flag (const_die, DW_AT_artificial, 1);
24812 tree_add_const_value_attribute_for_decl (const_die, decl);
24815 /* Generate a DIE to represent a label identifier. */
24817 static void
24818 gen_label_die (tree decl, dw_die_ref context_die)
24820 tree origin = decl_ultimate_origin (decl);
24821 dw_die_ref lbl_die = lookup_decl_die (decl);
24822 rtx insn;
24823 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24825 if (!lbl_die)
24827 lbl_die = new_die (DW_TAG_label, context_die, decl);
24828 equate_decl_number_to_die (decl, lbl_die);
24830 if (origin != NULL)
24831 add_abstract_origin_attribute (lbl_die, origin);
24832 else
24833 add_name_and_src_coords_attributes (lbl_die, decl);
24836 if (DECL_ABSTRACT_P (decl))
24837 equate_decl_number_to_die (decl, lbl_die);
24838 else if (! early_dwarf)
24840 insn = DECL_RTL_IF_SET (decl);
24842 /* Deleted labels are programmer specified labels which have been
24843 eliminated because of various optimizations. We still emit them
24844 here so that it is possible to put breakpoints on them. */
24845 if (insn
24846 && (LABEL_P (insn)
24847 || ((NOTE_P (insn)
24848 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24850 /* When optimization is enabled (via -O) some parts of the compiler
24851 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24852 represent source-level labels which were explicitly declared by
24853 the user. This really shouldn't be happening though, so catch
24854 it if it ever does happen. */
24855 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24857 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24858 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24860 else if (insn
24861 && NOTE_P (insn)
24862 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24863 && CODE_LABEL_NUMBER (insn) != -1)
24865 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24866 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24871 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24872 attributes to the DIE for a block STMT, to describe where the inlined
24873 function was called from. This is similar to add_src_coords_attributes. */
24875 static inline void
24876 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24878 /* We can end up with BUILTINS_LOCATION here. */
24879 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24880 return;
24882 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24883 expanded_location s = expand_location (locus);
24885 if (dwarf_version >= 3 || !dwarf_strict)
24887 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24888 add_AT_unsigned (die, DW_AT_call_line, s.line);
24889 if (debug_column_info && s.column)
24890 add_AT_unsigned (die, DW_AT_call_column, s.column);
24891 unsigned discr = get_discriminator_from_loc (locus);
24892 if (discr != 0)
24893 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24898 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24899 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24901 static inline void
24902 add_high_low_attributes (tree stmt, dw_die_ref die)
24904 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24906 if (inline_entry_data **iedp
24907 = !inline_entry_data_table ? NULL
24908 : inline_entry_data_table->find_slot_with_hash (stmt,
24909 htab_hash_pointer (stmt),
24910 NO_INSERT))
24912 inline_entry_data *ied = *iedp;
24913 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24914 gcc_assert (debug_inline_points);
24915 gcc_assert (inlined_function_outer_scope_p (stmt));
24917 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24918 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24920 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24921 && !dwarf_strict)
24923 if (!output_asm_line_debug_info ())
24924 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24925 else
24927 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24928 /* FIXME: this will resolve to a small number. Could we
24929 possibly emit smaller data? Ideally we'd emit a
24930 uleb128, but that would make the size of DIEs
24931 impossible for the compiler to compute, since it's
24932 the assembler that computes the value of the view
24933 label in this case. Ideally, we'd have a single form
24934 encompassing both the address and the view, and
24935 indirecting them through a table might make things
24936 easier, but even that would be more wasteful,
24937 space-wise, than what we have now. */
24938 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24942 inline_entry_data_table->clear_slot (iedp);
24945 if (BLOCK_FRAGMENT_CHAIN (stmt)
24946 && (dwarf_version >= 3 || !dwarf_strict))
24948 tree chain, superblock = NULL_TREE;
24949 dw_die_ref pdie;
24950 dw_attr_node *attr = NULL;
24952 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24954 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24955 BLOCK_NUMBER (stmt));
24956 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24959 /* Optimize duplicate .debug_ranges lists or even tails of
24960 lists. If this BLOCK has same ranges as its supercontext,
24961 lookup DW_AT_ranges attribute in the supercontext (and
24962 recursively so), verify that the ranges_table contains the
24963 right values and use it instead of adding a new .debug_range. */
24964 for (chain = stmt, pdie = die;
24965 BLOCK_SAME_RANGE (chain);
24966 chain = BLOCK_SUPERCONTEXT (chain))
24968 dw_attr_node *new_attr;
24970 pdie = pdie->die_parent;
24971 if (pdie == NULL)
24972 break;
24973 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24974 break;
24975 new_attr = get_AT (pdie, DW_AT_ranges);
24976 if (new_attr == NULL
24977 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24978 break;
24979 attr = new_attr;
24980 superblock = BLOCK_SUPERCONTEXT (chain);
24982 if (attr != NULL
24983 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24984 == (int)BLOCK_NUMBER (superblock))
24985 && BLOCK_FRAGMENT_CHAIN (superblock))
24987 unsigned long off = attr->dw_attr_val.v.val_offset;
24988 unsigned long supercnt = 0, thiscnt = 0;
24989 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24990 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24992 ++supercnt;
24993 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24994 == (int)BLOCK_NUMBER (chain));
24996 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24997 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24998 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24999 ++thiscnt;
25000 gcc_assert (supercnt >= thiscnt);
25001 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
25002 false);
25003 note_rnglist_head (off + supercnt - thiscnt);
25004 return;
25007 unsigned int offset = add_ranges (stmt, true);
25008 add_AT_range_list (die, DW_AT_ranges, offset, false);
25009 note_rnglist_head (offset);
25011 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
25012 chain = BLOCK_FRAGMENT_CHAIN (stmt);
25015 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
25016 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
25017 chain = BLOCK_FRAGMENT_CHAIN (chain);
25019 while (chain);
25020 add_ranges (NULL);
25022 else
25024 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
25025 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
25026 BLOCK_NUMBER (stmt));
25027 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
25028 BLOCK_NUMBER (stmt));
25029 add_AT_low_high_pc (die, label, label_high, false);
25033 /* Generate a DIE for a lexical block. */
25035 static void
25036 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
25038 dw_die_ref old_die = lookup_block_die (stmt);
25039 dw_die_ref stmt_die = NULL;
25040 if (!old_die)
25042 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25043 equate_block_to_die (stmt, stmt_die);
25046 if (BLOCK_ABSTRACT_ORIGIN (stmt))
25048 /* If this is an inlined or conrecte instance, create a new lexical
25049 die for anything below to attach DW_AT_abstract_origin to. */
25050 if (old_die)
25051 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
25053 tree origin = block_ultimate_origin (stmt);
25054 if (origin != NULL_TREE && (origin != stmt || old_die))
25055 add_abstract_origin_attribute (stmt_die, origin);
25057 old_die = NULL;
25060 if (old_die)
25061 stmt_die = old_die;
25063 /* A non abstract block whose blocks have already been reordered
25064 should have the instruction range for this block. If so, set the
25065 high/low attributes. */
25066 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
25068 gcc_assert (stmt_die);
25069 add_high_low_attributes (stmt, stmt_die);
25072 decls_for_scope (stmt, stmt_die);
25075 /* Generate a DIE for an inlined subprogram. */
25077 static void
25078 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
25080 tree decl = block_ultimate_origin (stmt);
25082 /* Make sure any inlined functions are known to be inlineable. */
25083 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25084 || cgraph_function_possibly_inlined_p (decl));
25086 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25088 if (call_arg_locations || debug_inline_points)
25089 equate_block_to_die (stmt, subr_die);
25090 add_abstract_origin_attribute (subr_die, decl);
25091 if (TREE_ASM_WRITTEN (stmt))
25092 add_high_low_attributes (stmt, subr_die);
25093 add_call_src_coords_attributes (stmt, subr_die);
25095 /* The inliner creates an extra BLOCK for the parameter setup,
25096 we want to merge that with the actual outermost BLOCK of the
25097 inlined function to avoid duplicate locals in consumers.
25098 Do that by doing the recursion to subblocks on the single subblock
25099 of STMT. */
25100 bool unwrap_one = false;
25101 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
25103 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
25104 if (origin
25105 && TREE_CODE (origin) == BLOCK
25106 && BLOCK_SUPERCONTEXT (origin) == decl)
25107 unwrap_one = true;
25109 decls_for_scope (stmt, subr_die, !unwrap_one);
25110 if (unwrap_one)
25111 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
25114 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25115 the comment for VLR_CONTEXT. */
25117 static void
25118 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25120 dw_die_ref decl_die;
25122 if (TREE_TYPE (decl) == error_mark_node)
25123 return;
25125 decl_die = new_die (DW_TAG_member, context_die, decl);
25126 add_name_and_src_coords_attributes (decl_die, decl);
25127 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25128 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25129 context_die);
25131 if (DECL_BIT_FIELD_TYPE (decl))
25133 add_byte_size_attribute (decl_die, decl);
25134 add_bit_size_attribute (decl_die, decl);
25135 add_bit_offset_attribute (decl_die, decl);
25138 add_alignment_attribute (decl_die, decl);
25140 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25141 add_data_member_location_attribute (decl_die, decl, ctx);
25143 if (DECL_ARTIFICIAL (decl))
25144 add_AT_flag (decl_die, DW_AT_artificial, 1);
25146 add_accessibility_attribute (decl_die, decl);
25148 /* Equate decl number to die, so that we can look up this decl later on. */
25149 equate_decl_number_to_die (decl, decl_die);
25152 /* Generate a DIE for a pointer to a member type. TYPE can be an
25153 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25154 pointer to member function. */
25156 static void
25157 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25159 if (lookup_type_die (type))
25160 return;
25162 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25163 scope_die_for (type, context_die), type);
25165 equate_type_number_to_die (type, ptr_die);
25166 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25167 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25168 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25169 context_die);
25170 add_alignment_attribute (ptr_die, type);
25172 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25173 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25175 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25176 add_AT_loc (ptr_die, DW_AT_use_location, op);
25180 static char *producer_string;
25182 /* Given a C and/or C++ language/version string return the "highest".
25183 C++ is assumed to be "higher" than C in this case. Used for merging
25184 LTO translation unit languages. */
25185 static const char *
25186 highest_c_language (const char *lang1, const char *lang2)
25188 if (strcmp ("GNU C++26", lang1) == 0 || strcmp ("GNU C++26", lang2) == 0)
25189 return "GNU C++26";
25190 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25191 return "GNU C++23";
25192 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25193 return "GNU C++20";
25194 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25195 return "GNU C++17";
25196 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25197 return "GNU C++14";
25198 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25199 return "GNU C++11";
25200 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25201 return "GNU C++98";
25203 if (strcmp ("GNU C23", lang1) == 0 || strcmp ("GNU C23", lang2) == 0)
25204 return "GNU C23";
25205 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25206 return "GNU C17";
25207 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25208 return "GNU C11";
25209 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25210 return "GNU C99";
25211 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25212 return "GNU C89";
25214 gcc_unreachable ();
25218 /* Generate the DIE for the compilation unit. */
25220 static dw_die_ref
25221 gen_compile_unit_die (const char *filename)
25223 dw_die_ref die;
25224 const char *language_string = lang_hooks.name;
25225 int language;
25227 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25229 if (filename)
25231 add_filename_attribute (die, filename);
25232 /* Don't add cwd for <built-in>. */
25233 if (filename[0] != '<')
25234 add_comp_dir_attribute (die);
25237 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25239 /* If our producer is LTO try to figure out a common language to use
25240 from the global list of translation units. */
25241 if (strcmp (language_string, "GNU GIMPLE") == 0)
25243 unsigned i;
25244 tree t;
25245 const char *common_lang = NULL;
25247 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25249 if (!TRANSLATION_UNIT_LANGUAGE (t))
25250 continue;
25251 if (!common_lang)
25252 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25253 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25255 else if (startswith (common_lang, "GNU C")
25256 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25257 /* Mixing C and C++ is ok, use C++ in that case. */
25258 common_lang = highest_c_language (common_lang,
25259 TRANSLATION_UNIT_LANGUAGE (t));
25260 else
25262 /* Fall back to C. */
25263 common_lang = NULL;
25264 break;
25268 if (common_lang)
25269 language_string = common_lang;
25272 language = DW_LANG_C;
25273 if (startswith (language_string, "GNU C")
25274 && ISDIGIT (language_string[5]))
25276 language = DW_LANG_C89;
25277 if (dwarf_version >= 3 || !dwarf_strict)
25279 if (strcmp (language_string, "GNU C89") != 0)
25280 language = DW_LANG_C99;
25282 if (dwarf_version >= 5 /* || !dwarf_strict */)
25283 if (strcmp (language_string, "GNU C11") == 0
25284 || strcmp (language_string, "GNU C17") == 0
25285 || strcmp (language_string, "GNU C23") == 0)
25286 language = DW_LANG_C11;
25289 else if (startswith (language_string, "GNU C++"))
25291 language = DW_LANG_C_plus_plus;
25292 if (dwarf_version >= 5 /* || !dwarf_strict */)
25294 if (strcmp (language_string, "GNU C++11") == 0)
25295 language = DW_LANG_C_plus_plus_11;
25296 else if (strcmp (language_string, "GNU C++14") == 0)
25297 language = DW_LANG_C_plus_plus_14;
25298 else if (strcmp (language_string, "GNU C++17") == 0
25299 || strcmp (language_string, "GNU C++20") == 0
25300 || strcmp (language_string, "GNU C++23") == 0
25301 || strcmp (language_string, "GNU C++26") == 0)
25302 /* For now. */
25303 language = DW_LANG_C_plus_plus_14;
25306 else if (strcmp (language_string, "GNU F77") == 0)
25307 language = DW_LANG_Fortran77;
25308 else if (strcmp (language_string, "GNU Modula-2") == 0)
25309 language = DW_LANG_Modula2;
25310 else if (dwarf_version >= 3 || !dwarf_strict)
25312 if (strcmp (language_string, "GNU Ada") == 0)
25313 language = DW_LANG_Ada95;
25314 else if (startswith (language_string, "GNU Fortran"))
25316 language = DW_LANG_Fortran95;
25317 if (dwarf_version >= 5 /* || !dwarf_strict */)
25319 if (strcmp (language_string, "GNU Fortran2003") == 0)
25320 language = DW_LANG_Fortran03;
25321 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25322 language = DW_LANG_Fortran08;
25325 else if (strcmp (language_string, "GNU Objective-C") == 0)
25326 language = DW_LANG_ObjC;
25327 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25328 language = DW_LANG_ObjC_plus_plus;
25329 else if (strcmp (language_string, "GNU D") == 0)
25330 language = DW_LANG_D;
25331 else if (dwarf_version >= 5 || !dwarf_strict)
25333 if (strcmp (language_string, "GNU Go") == 0)
25334 language = DW_LANG_Go;
25335 else if (strcmp (language_string, "GNU Rust") == 0)
25336 language = DW_LANG_Rust;
25339 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25340 else if (startswith (language_string, "GNU Fortran"))
25341 language = DW_LANG_Fortran90;
25342 /* Likewise for Ada. */
25343 else if (strcmp (language_string, "GNU Ada") == 0)
25344 language = DW_LANG_Ada83;
25346 add_AT_unsigned (die, DW_AT_language, language);
25348 switch (language)
25350 case DW_LANG_Fortran77:
25351 case DW_LANG_Fortran90:
25352 case DW_LANG_Fortran95:
25353 case DW_LANG_Fortran03:
25354 case DW_LANG_Fortran08:
25355 /* Fortran has case insensitive identifiers and the front-end
25356 lowercases everything. */
25357 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25358 break;
25359 default:
25360 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25361 break;
25363 return die;
25366 /* Generate the DIE for a base class. */
25368 static void
25369 gen_inheritance_die (tree binfo, tree access, tree type,
25370 dw_die_ref context_die)
25372 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25373 struct vlr_context ctx = { type, NULL };
25375 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25376 context_die);
25377 add_data_member_location_attribute (die, binfo, &ctx);
25379 if (BINFO_VIRTUAL_P (binfo))
25380 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25382 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25383 children, otherwise the default is DW_ACCESS_public. In DWARF2
25384 the default has always been DW_ACCESS_private. */
25385 if (access == access_public_node)
25387 if (dwarf_version == 2
25388 || context_die->die_tag == DW_TAG_class_type)
25389 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25391 else if (access == access_protected_node)
25392 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25393 else if (dwarf_version > 2
25394 && context_die->die_tag != DW_TAG_class_type)
25395 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25398 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25399 structure. */
25401 static bool
25402 is_variant_part (tree decl)
25404 return (TREE_CODE (decl) == FIELD_DECL
25405 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25408 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25409 return the FIELD_DECL. Return NULL_TREE otherwise. */
25411 static tree
25412 analyze_discr_in_predicate (tree operand, tree struct_type)
25414 while (CONVERT_EXPR_P (operand))
25415 operand = TREE_OPERAND (operand, 0);
25417 /* Match field access to members of struct_type only. */
25418 if (TREE_CODE (operand) == COMPONENT_REF
25419 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25420 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25421 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25422 return TREE_OPERAND (operand, 1);
25423 else
25424 return NULL_TREE;
25427 /* Check that SRC is a constant integer that can be represented as a native
25428 integer constant (either signed or unsigned). If so, store it into DEST and
25429 return true. Return false otherwise. */
25431 static bool
25432 get_discr_value (tree src, dw_discr_value *dest)
25434 tree discr_type = TREE_TYPE (src);
25436 if (lang_hooks.types.get_debug_type)
25438 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25439 if (debug_type != NULL)
25440 discr_type = debug_type;
25443 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25444 return false;
25446 /* Signedness can vary between the original type and the debug type. This
25447 can happen for character types in Ada for instance: the character type
25448 used for code generation can be signed, to be compatible with the C one,
25449 but from a debugger point of view, it must be unsigned. */
25450 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25451 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25453 if (is_orig_unsigned != is_debug_unsigned)
25454 src = fold_convert (discr_type, src);
25456 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25457 return false;
25459 dest->pos = is_debug_unsigned;
25460 if (is_debug_unsigned)
25461 dest->v.uval = tree_to_uhwi (src);
25462 else
25463 dest->v.sval = tree_to_shwi (src);
25465 return true;
25468 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25469 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25470 store NULL_TREE in DISCR_DECL. Otherwise:
25472 - store the discriminant field in STRUCT_TYPE that controls the variant
25473 part to *DISCR_DECL
25475 - put in *DISCR_LISTS_P an array where for each variant, the item
25476 represents the corresponding matching list of discriminant values.
25478 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25479 the above array.
25481 Note that when the array is allocated (i.e. when the analysis is
25482 successful), it is up to the caller to free the array. */
25484 static void
25485 analyze_variants_discr (tree variant_part_decl,
25486 tree struct_type,
25487 tree *discr_decl,
25488 dw_discr_list_ref **discr_lists_p,
25489 unsigned *discr_lists_length)
25491 tree variant_part_type = TREE_TYPE (variant_part_decl);
25492 tree variant;
25493 dw_discr_list_ref *discr_lists;
25494 unsigned i;
25496 /* Compute how many variants there are in this variant part. */
25497 *discr_lists_length = 0;
25498 for (variant = TYPE_FIELDS (variant_part_type);
25499 variant != NULL_TREE;
25500 variant = DECL_CHAIN (variant))
25501 ++*discr_lists_length;
25503 *discr_decl = NULL_TREE;
25504 *discr_lists_p
25505 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25506 sizeof (**discr_lists_p));
25507 discr_lists = *discr_lists_p;
25509 /* And then analyze all variants to extract discriminant information for all
25510 of them. This analysis is conservative: as soon as we detect something we
25511 do not support, abort everything and pretend we found nothing. */
25512 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25513 variant != NULL_TREE;
25514 variant = DECL_CHAIN (variant), ++i)
25516 tree match_expr = DECL_QUALIFIER (variant);
25518 /* Now, try to analyze the predicate and deduce a discriminant for
25519 it. */
25520 if (match_expr == boolean_true_node)
25521 /* Typically happens for the default variant: it matches all cases that
25522 previous variants rejected. Don't output any matching value for
25523 this one. */
25524 continue;
25526 /* The following loop tries to iterate over each discriminant
25527 possibility: single values or ranges. */
25528 while (match_expr != NULL_TREE)
25530 tree next_round_match_expr;
25531 tree candidate_discr = NULL_TREE;
25532 dw_discr_list_ref new_node = NULL;
25534 /* Possibilities are matched one after the other by nested
25535 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25536 continue with the rest at next iteration. */
25537 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25539 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25540 match_expr = TREE_OPERAND (match_expr, 1);
25542 else
25543 next_round_match_expr = NULL_TREE;
25545 if (match_expr == boolean_false_node)
25546 /* This sub-expression matches nothing: just wait for the next
25547 one. */
25550 else if (TREE_CODE (match_expr) == EQ_EXPR)
25552 /* We are matching: <discr_field> == <integer_cst>
25553 This sub-expression matches a single value. */
25554 tree integer_cst = TREE_OPERAND (match_expr, 1);
25556 candidate_discr
25557 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25558 struct_type);
25560 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25561 if (!get_discr_value (integer_cst,
25562 &new_node->dw_discr_lower_bound))
25563 goto abort;
25564 new_node->dw_discr_range = false;
25567 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25569 /* We are matching:
25570 <discr_field> > <integer_cst>
25571 && <discr_field> < <integer_cst>.
25572 This sub-expression matches the range of values between the
25573 two matched integer constants. Note that comparisons can be
25574 inclusive or exclusive. */
25575 tree candidate_discr_1, candidate_discr_2;
25576 tree lower_cst, upper_cst;
25577 bool lower_cst_included, upper_cst_included;
25578 tree lower_op = TREE_OPERAND (match_expr, 0);
25579 tree upper_op = TREE_OPERAND (match_expr, 1);
25581 /* When the comparison is exclusive, the integer constant is not
25582 the discriminant range bound we are looking for: we will have
25583 to increment or decrement it. */
25584 if (TREE_CODE (lower_op) == GE_EXPR)
25585 lower_cst_included = true;
25586 else if (TREE_CODE (lower_op) == GT_EXPR)
25587 lower_cst_included = false;
25588 else
25589 goto abort;
25591 if (TREE_CODE (upper_op) == LE_EXPR)
25592 upper_cst_included = true;
25593 else if (TREE_CODE (upper_op) == LT_EXPR)
25594 upper_cst_included = false;
25595 else
25596 goto abort;
25598 /* Extract the discriminant from the first operand and check it
25599 is consistant with the same analysis in the second
25600 operand. */
25601 candidate_discr_1
25602 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25603 struct_type);
25604 candidate_discr_2
25605 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25606 struct_type);
25607 if (candidate_discr_1 == candidate_discr_2)
25608 candidate_discr = candidate_discr_1;
25609 else
25610 goto abort;
25612 /* Extract bounds from both. */
25613 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25614 lower_cst = TREE_OPERAND (lower_op, 1);
25615 upper_cst = TREE_OPERAND (upper_op, 1);
25617 if (!lower_cst_included)
25618 lower_cst
25619 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25620 build_int_cst (TREE_TYPE (lower_cst), 1));
25621 if (!upper_cst_included)
25622 upper_cst
25623 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25624 build_int_cst (TREE_TYPE (upper_cst), 1));
25626 if (!get_discr_value (lower_cst,
25627 &new_node->dw_discr_lower_bound)
25628 || !get_discr_value (upper_cst,
25629 &new_node->dw_discr_upper_bound))
25630 goto abort;
25632 new_node->dw_discr_range = true;
25635 else if ((candidate_discr
25636 = analyze_discr_in_predicate (match_expr, struct_type))
25637 && (TREE_TYPE (candidate_discr) == boolean_type_node
25638 || TREE_TYPE (TREE_TYPE (candidate_discr))
25639 == boolean_type_node))
25641 /* We are matching: <discr_field> for a boolean discriminant.
25642 This sub-expression matches boolean_true_node. */
25643 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25644 if (!get_discr_value (boolean_true_node,
25645 &new_node->dw_discr_lower_bound))
25646 goto abort;
25647 new_node->dw_discr_range = false;
25650 else
25651 /* Unsupported sub-expression: we cannot determine the set of
25652 matching discriminant values. Abort everything. */
25653 goto abort;
25655 /* If the discriminant info is not consistant with what we saw so
25656 far, consider the analysis failed and abort everything. */
25657 if (candidate_discr == NULL_TREE
25658 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25659 goto abort;
25660 else
25661 *discr_decl = candidate_discr;
25663 if (new_node != NULL)
25665 new_node->dw_discr_next = discr_lists[i];
25666 discr_lists[i] = new_node;
25668 match_expr = next_round_match_expr;
25672 /* If we reach this point, we could match everything we were interested
25673 in. */
25674 return;
25676 abort:
25677 /* Clean all data structure and return no result. */
25678 free (*discr_lists_p);
25679 *discr_lists_p = NULL;
25680 *discr_decl = NULL_TREE;
25683 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25684 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25685 under CONTEXT_DIE.
25687 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25688 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25689 this type, which are record types, represent the available variants and each
25690 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25691 values are inferred from these attributes.
25693 In trees, the offsets for the fields inside these sub-records are relative
25694 to the variant part itself, whereas the corresponding DIEs should have
25695 offset attributes that are relative to the embedding record base address.
25696 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25697 must be an expression that computes the offset of the variant part to
25698 describe in DWARF. */
25700 static void
25701 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25702 dw_die_ref context_die)
25704 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25705 tree variant_part_offset = vlr_ctx->variant_part_offset;
25707 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25708 NULL_TREE if there is no such field. */
25709 tree discr_decl = NULL_TREE;
25710 dw_discr_list_ref *discr_lists;
25711 unsigned discr_lists_length = 0;
25712 unsigned i;
25714 dw_die_ref dwarf_proc_die = NULL;
25715 dw_die_ref variant_part_die
25716 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25718 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25720 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25721 &discr_decl, &discr_lists, &discr_lists_length);
25723 if (discr_decl != NULL_TREE)
25725 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25727 if (discr_die)
25728 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25729 else
25730 /* We have no DIE for the discriminant, so just discard all
25731 discrimimant information in the output. */
25732 discr_decl = NULL_TREE;
25735 /* If the offset for this variant part is more complex than a constant,
25736 create a DWARF procedure for it so that we will not have to generate
25737 DWARF expressions for it for each member. */
25738 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25739 && (dwarf_version >= 3 || !dwarf_strict))
25741 struct loc_descr_context ctx = {
25742 vlr_ctx->struct_type, /* context_type */
25743 NULL_TREE, /* base_decl */
25744 NULL, /* dpi */
25745 false, /* placeholder_arg */
25746 false, /* placeholder_seen */
25747 false /* strict_signedness */
25749 const tree dwarf_proc_fndecl
25750 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25751 build_function_type (TREE_TYPE (variant_part_offset),
25752 NULL_TREE));
25753 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25754 const dw_loc_descr_ref dwarf_proc_body
25755 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25757 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25758 dwarf_proc_fndecl, context_die);
25759 if (dwarf_proc_die != NULL)
25760 variant_part_offset = dwarf_proc_call;
25763 /* Output DIEs for all variants. */
25764 i = 0;
25765 for (tree variant = TYPE_FIELDS (variant_part_type);
25766 variant != NULL_TREE;
25767 variant = DECL_CHAIN (variant), ++i)
25769 tree variant_type = TREE_TYPE (variant);
25770 dw_die_ref variant_die;
25772 /* All variants (i.e. members of a variant part) are supposed to be
25773 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25774 under these records. */
25775 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25777 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25778 equate_decl_number_to_die (variant, variant_die);
25780 /* Output discriminant values this variant matches, if any. */
25781 if (discr_decl == NULL || discr_lists[i] == NULL)
25782 /* In the case we have discriminant information at all, this is
25783 probably the default variant: as the standard says, don't
25784 output any discriminant value/list attribute. */
25786 else if (discr_lists[i]->dw_discr_next == NULL
25787 && !discr_lists[i]->dw_discr_range)
25788 /* If there is only one accepted value, don't bother outputting a
25789 list. */
25790 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25791 else
25792 add_discr_list (variant_die, discr_lists[i]);
25794 for (tree member = TYPE_FIELDS (variant_type);
25795 member != NULL_TREE;
25796 member = DECL_CHAIN (member))
25798 struct vlr_context vlr_sub_ctx = {
25799 vlr_ctx->struct_type, /* struct_type */
25800 NULL /* variant_part_offset */
25802 if (is_variant_part (member))
25804 /* All offsets for fields inside variant parts are relative to
25805 the top-level embedding RECORD_TYPE's base address. On the
25806 other hand, offsets in GCC's types are relative to the
25807 nested-most variant part. So we have to sum offsets each time
25808 we recurse. */
25810 vlr_sub_ctx.variant_part_offset
25811 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25812 variant_part_offset, byte_position (member));
25813 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25815 else
25817 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25818 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25823 free (discr_lists);
25826 /* Generate a DIE for a class member. */
25828 static void
25829 gen_member_die (tree type, dw_die_ref context_die)
25831 tree member;
25832 tree binfo = TYPE_BINFO (type);
25834 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25836 /* If this is not an incomplete type, output descriptions of each of its
25837 members. Note that as we output the DIEs necessary to represent the
25838 members of this record or union type, we will also be trying to output
25839 DIEs to represent the *types* of those members. However the `type'
25840 function (above) will specifically avoid generating type DIEs for member
25841 types *within* the list of member DIEs for this (containing) type except
25842 for those types (of members) which are explicitly marked as also being
25843 members of this (containing) type themselves. The g++ front- end can
25844 force any given type to be treated as a member of some other (containing)
25845 type by setting the TYPE_CONTEXT of the given (member) type to point to
25846 the TREE node representing the appropriate (containing) type. */
25848 /* First output info about the base classes. */
25849 if (binfo && early_dwarf)
25851 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25852 int i;
25853 tree base;
25855 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25856 gen_inheritance_die (base,
25857 (accesses ? (*accesses)[i] : access_public_node),
25858 type,
25859 context_die);
25862 /* Now output info about the members. */
25863 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25865 /* Ignore clones. */
25866 if (DECL_ABSTRACT_ORIGIN (member))
25867 continue;
25869 struct vlr_context vlr_ctx = { type, NULL_TREE };
25870 bool static_inline_p
25871 = (VAR_P (member)
25872 && TREE_STATIC (member)
25873 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25874 != -1));
25876 /* If we thought we were generating minimal debug info for TYPE
25877 and then changed our minds, some of the member declarations
25878 may have already been defined. Don't define them again, but
25879 do put them in the right order. */
25881 if (dw_die_ref child = lookup_decl_die (member))
25883 /* Handle inline static data members, which only have in-class
25884 declarations. */
25885 bool splice = true;
25887 dw_die_ref ref = NULL;
25888 if (child->die_tag == DW_TAG_variable
25889 && child->die_parent == comp_unit_die ())
25891 ref = get_AT_ref (child, DW_AT_specification);
25893 /* For C++17 inline static data members followed by redundant
25894 out of class redeclaration, we might get here with
25895 child being the DIE created for the out of class
25896 redeclaration and with its DW_AT_specification being
25897 the DIE created for in-class definition. We want to
25898 reparent the latter, and don't want to create another
25899 DIE with DW_AT_specification in that case, because
25900 we already have one. */
25901 if (ref
25902 && static_inline_p
25903 && ref->die_tag == DW_TAG_variable
25904 && ref->die_parent == comp_unit_die ()
25905 && get_AT (ref, DW_AT_specification) == NULL)
25907 child = ref;
25908 ref = NULL;
25909 static_inline_p = false;
25912 if (!ref)
25914 reparent_child (child, context_die);
25915 if (dwarf_version < 5)
25916 child->die_tag = DW_TAG_member;
25917 splice = false;
25920 else if (child->die_tag == DW_TAG_enumerator)
25921 /* Enumerators remain under their enumeration even if
25922 their names are introduced in the enclosing scope. */
25923 splice = false;
25925 if (splice)
25926 splice_child_die (context_die, child);
25929 /* Do not generate DWARF for variant parts if we are generating the
25930 corresponding GNAT encodings: DIEs generated for the two schemes
25931 would conflict in our mappings. */
25932 else if (is_variant_part (member)
25933 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25935 vlr_ctx.variant_part_offset = byte_position (member);
25936 gen_variant_part (member, &vlr_ctx, context_die);
25938 else
25940 vlr_ctx.variant_part_offset = NULL_TREE;
25941 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25944 /* For C++ inline static data members emit immediately a DW_TAG_variable
25945 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25946 DW_AT_specification. */
25947 if (static_inline_p)
25949 int old_extern = DECL_EXTERNAL (member);
25950 DECL_EXTERNAL (member) = 0;
25951 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25952 DECL_EXTERNAL (member) = old_extern;
25957 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25958 is set, we pretend that the type was never defined, so we only get the
25959 member DIEs needed by later specification DIEs. */
25961 static void
25962 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25963 enum debug_info_usage usage)
25965 if (TREE_ASM_WRITTEN (type))
25967 /* Fill in the bound of variable-length fields in late dwarf if
25968 still incomplete. */
25969 if (!early_dwarf && variably_modified_type_p (type, NULL))
25970 for (tree member = TYPE_FIELDS (type);
25971 member;
25972 member = DECL_CHAIN (member))
25973 fill_variable_array_bounds (TREE_TYPE (member));
25974 return;
25977 dw_die_ref type_die = lookup_type_die (type);
25978 dw_die_ref scope_die = 0;
25979 bool nested = false;
25980 bool complete = (TYPE_SIZE (type)
25981 && (! TYPE_STUB_DECL (type)
25982 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25983 bool ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25984 complete = complete && should_emit_struct_debug (type, usage);
25986 if (type_die && ! complete)
25987 return;
25989 if (TYPE_CONTEXT (type) != NULL_TREE
25990 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25991 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25992 nested = true;
25994 scope_die = scope_die_for (type, context_die);
25996 /* Generate child dies for template parameters. */
25997 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25998 schedule_generic_params_dies_gen (type);
26000 if (! type_die || (nested && is_cu_die (scope_die)))
26001 /* First occurrence of type or toplevel definition of nested class. */
26003 dw_die_ref old_die = type_die;
26005 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
26006 ? record_type_tag (type) : DW_TAG_union_type,
26007 scope_die, type);
26008 equate_type_number_to_die (type, type_die);
26009 if (old_die)
26010 add_AT_specification (type_die, old_die);
26011 else
26012 add_name_attribute (type_die, type_tag (type));
26014 else
26015 remove_AT (type_die, DW_AT_declaration);
26017 /* If this type has been completed, then give it a byte_size attribute and
26018 then give a list of members. */
26019 if (complete && !ns_decl)
26021 /* Prevent infinite recursion in cases where the type of some member of
26022 this type is expressed in terms of this type itself. */
26023 TREE_ASM_WRITTEN (type) = 1;
26024 add_byte_size_attribute (type_die, type);
26025 add_alignment_attribute (type_die, type);
26026 if (TYPE_STUB_DECL (type) != NULL_TREE)
26028 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
26029 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
26032 /* If the first reference to this type was as the return type of an
26033 inline function, then it may not have a parent. Fix this now. */
26034 if (type_die->die_parent == NULL)
26035 add_child_die (scope_die, type_die);
26037 gen_member_die (type, type_die);
26039 add_gnat_descriptive_type_attribute (type_die, type, context_die);
26040 if (TYPE_ARTIFICIAL (type))
26041 add_AT_flag (type_die, DW_AT_artificial, 1);
26043 /* GNU extension: Record what type our vtable lives in. */
26044 if (TYPE_VFIELD (type))
26046 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
26048 gen_type_die (vtype, context_die);
26049 add_AT_die_ref (type_die, DW_AT_containing_type,
26050 lookup_type_die (vtype));
26053 else
26055 add_AT_flag (type_die, DW_AT_declaration, 1);
26057 /* We don't need to do this for function-local types. */
26058 if (TYPE_STUB_DECL (type)
26059 && ! decl_function_context (TYPE_STUB_DECL (type)))
26060 vec_safe_push (incomplete_types, type);
26063 if (get_AT (type_die, DW_AT_name))
26064 add_pubtype (type, type_die);
26067 /* Generate a DIE for a subroutine _type_. */
26069 static void
26070 gen_subroutine_type_die (tree type, dw_die_ref context_die)
26072 tree return_type = TREE_TYPE (type);
26073 dw_die_ref subr_die
26074 = new_die (DW_TAG_subroutine_type,
26075 scope_die_for (type, context_die), type);
26077 equate_type_number_to_die (type, subr_die);
26078 add_prototyped_attribute (subr_die, type);
26079 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
26080 context_die);
26081 add_alignment_attribute (subr_die, type);
26082 gen_formal_types_die (type, subr_die);
26084 if (get_AT (subr_die, DW_AT_name))
26085 add_pubtype (type, subr_die);
26086 if ((dwarf_version >= 5 || !dwarf_strict)
26087 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26088 add_AT_flag (subr_die, DW_AT_reference, 1);
26089 if ((dwarf_version >= 5 || !dwarf_strict)
26090 && lang_hooks.types.type_dwarf_attribute (type,
26091 DW_AT_rvalue_reference) != -1)
26092 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26095 /* Generate a DIE for a type definition. */
26097 static void
26098 gen_typedef_die (tree decl, dw_die_ref context_die)
26100 dw_die_ref type_die;
26101 tree type;
26103 if (TREE_ASM_WRITTEN (decl))
26105 if (DECL_ORIGINAL_TYPE (decl))
26106 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26107 return;
26110 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26111 checks in process_scope_var and modified_type_die), this should be called
26112 only for original types. */
26113 gcc_assert (decl_ultimate_origin (decl) == NULL
26114 || decl_ultimate_origin (decl) == decl);
26116 TREE_ASM_WRITTEN (decl) = 1;
26117 type_die = new_die (DW_TAG_typedef, context_die, decl);
26119 add_name_and_src_coords_attributes (type_die, decl);
26120 if (DECL_ORIGINAL_TYPE (decl))
26122 type = DECL_ORIGINAL_TYPE (decl);
26123 if (type == error_mark_node)
26124 return;
26126 gcc_assert (type != TREE_TYPE (decl));
26127 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26129 else
26131 type = TREE_TYPE (decl);
26132 if (type == error_mark_node)
26133 return;
26135 if (is_naming_typedef_decl (TYPE_NAME (type)))
26137 /* Here, we are in the case of decl being a typedef naming
26138 an anonymous type, e.g:
26139 typedef struct {...} foo;
26140 In that case TREE_TYPE (decl) is not a typedef variant
26141 type and TYPE_NAME of the anonymous type is set to the
26142 TYPE_DECL of the typedef. This construct is emitted by
26143 the C++ FE.
26145 TYPE is the anonymous struct named by the typedef
26146 DECL. As we need the DW_AT_type attribute of the
26147 DW_TAG_typedef to point to the DIE of TYPE, let's
26148 generate that DIE right away. add_type_attribute
26149 called below will then pick (via lookup_type_die) that
26150 anonymous struct DIE. */
26151 if (!TREE_ASM_WRITTEN (type))
26152 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26154 /* This is a GNU Extension. We are adding a
26155 DW_AT_linkage_name attribute to the DIE of the
26156 anonymous struct TYPE. The value of that attribute
26157 is the name of the typedef decl naming the anonymous
26158 struct. This greatly eases the work of consumers of
26159 this debug info. */
26160 add_linkage_name_raw (lookup_type_die (type), decl);
26164 add_type_attribute (type_die, type, decl_quals (decl), false,
26165 context_die);
26167 if (is_naming_typedef_decl (decl))
26168 /* We want that all subsequent calls to lookup_type_die with
26169 TYPE in argument yield the DW_TAG_typedef we have just
26170 created. */
26171 equate_type_number_to_die (type, type_die);
26173 add_alignment_attribute (type_die, TREE_TYPE (decl));
26175 add_accessibility_attribute (type_die, decl);
26177 if (DECL_ABSTRACT_P (decl))
26178 equate_decl_number_to_die (decl, type_die);
26180 if (get_AT (type_die, DW_AT_name))
26181 add_pubtype (decl, type_die);
26184 /* Generate a DIE for a struct, class, enum or union type. */
26186 static void
26187 gen_tagged_type_die (tree type,
26188 dw_die_ref context_die,
26189 enum debug_info_usage usage,
26190 bool reverse)
26192 if (type == NULL_TREE
26193 || !is_tagged_type (type))
26194 return;
26196 if (TREE_ASM_WRITTEN (type))
26198 /* If this is a nested type whose containing class hasn't been written
26199 out yet, writing it out will cover this one, too. This does not apply
26200 to instantiations of member class templates; they need to be added to
26201 the containing class as they are generated. FIXME: This hurts the
26202 idea of combining type decls from multiple TUs, since we can't predict
26203 what set of template instantiations we'll get. */
26204 else if (TYPE_CONTEXT (type)
26205 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26206 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26208 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26210 if (TREE_ASM_WRITTEN (type))
26211 return;
26213 /* If that failed, attach ourselves to the stub. */
26214 context_die = lookup_type_die (TYPE_CONTEXT (type));
26216 else if (TYPE_CONTEXT (type) != NULL_TREE
26217 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26219 /* If this type is local to a function that hasn't been written
26220 out yet, use a NULL context for now; it will be fixed up in
26221 decls_for_scope. */
26222 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26223 /* A declaration DIE doesn't count; nested types need to go in the
26224 specification. */
26225 if (context_die && is_declaration_die (context_die))
26226 context_die = NULL;
26228 else
26229 context_die = declare_in_namespace (type, context_die);
26231 if (TREE_CODE (type) == ENUMERAL_TYPE)
26233 /* This might have been written out by the call to
26234 declare_in_namespace. */
26235 if (!TREE_ASM_WRITTEN (type) || reverse)
26236 gen_enumeration_type_die (type, context_die, reverse);
26238 else
26239 gen_struct_or_union_type_die (type, context_die, usage);
26241 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26242 it up if it is ever completed. gen_*_type_die will set it for us
26243 when appropriate. */
26246 /* Generate a type description DIE. */
26248 static void
26249 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26250 enum debug_info_usage usage, bool reverse)
26252 struct array_descr_info info;
26254 if (type == NULL_TREE || type == error_mark_node)
26255 return;
26257 if (flag_checking && type)
26258 verify_type (type);
26260 if (TYPE_NAME (type) != NULL_TREE
26261 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26262 && is_redundant_typedef (TYPE_NAME (type))
26263 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26264 /* The DECL of this type is a typedef we don't want to emit debug
26265 info for but we want debug info for its underlying typedef.
26266 This can happen for e.g, the injected-class-name of a C++
26267 type. */
26268 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26270 /* If TYPE is a typedef type variant, let's generate debug info
26271 for the parent typedef which TYPE is a type of. */
26272 if (typedef_variant_p (type))
26274 if (TREE_ASM_WRITTEN (type))
26275 return;
26277 tree name = TYPE_NAME (type);
26278 tree origin = decl_ultimate_origin (name);
26279 if (origin != NULL && origin != name)
26281 gen_decl_die (origin, NULL, NULL, context_die);
26282 return;
26285 /* Prevent broken recursion; we can't hand off to the same type. */
26286 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26288 /* Give typedefs the right scope. */
26289 context_die = scope_die_for (type, context_die);
26291 TREE_ASM_WRITTEN (type) = 1;
26293 gen_decl_die (name, NULL, NULL, context_die);
26294 return;
26297 /* If type is an anonymous tagged type named by a typedef, let's
26298 generate debug info for the typedef. */
26299 if (is_naming_typedef_decl (TYPE_NAME (type)))
26301 /* Give typedefs the right scope. */
26302 context_die = scope_die_for (type, context_die);
26304 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26305 return;
26308 if (lang_hooks.types.get_debug_type)
26310 tree debug_type = lang_hooks.types.get_debug_type (type);
26312 if (debug_type != NULL_TREE && debug_type != type)
26314 gen_type_die_with_usage (debug_type, context_die, usage, reverse);
26315 return;
26319 /* We are going to output a DIE to represent the unqualified version
26320 of this type (i.e. without any const or volatile qualifiers) so
26321 get the main variant (i.e. the unqualified version) of this type
26322 now. (Vectors and arrays are special because the debugging info is in the
26323 cloned type itself. Similarly function/method types can contain extra
26324 ref-qualification). */
26325 if (FUNC_OR_METHOD_TYPE_P (type))
26327 /* For function/method types, can't use type_main_variant here,
26328 because that can have different ref-qualifiers for C++,
26329 but try to canonicalize. */
26330 tree main = TYPE_MAIN_VARIANT (type);
26331 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26332 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26333 && check_base_type (t, main)
26334 && check_lang_type (t, type))
26336 type = t;
26337 break;
26340 else if (TREE_CODE (type) != VECTOR_TYPE
26341 && TREE_CODE (type) != ARRAY_TYPE)
26342 type = type_main_variant (type);
26344 /* If this is an array type with hidden descriptor, handle it first. */
26345 if (!TREE_ASM_WRITTEN (type)
26346 && lang_hooks.types.get_array_descr_info)
26348 memset (&info, 0, sizeof (info));
26349 if (lang_hooks.types.get_array_descr_info (type, &info))
26351 /* Fortran sometimes emits array types with no dimension. */
26352 gcc_assert (info.ndimensions >= 0
26353 && (info.ndimensions
26354 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26355 gen_descr_array_type_die (type, &info, context_die);
26356 TREE_ASM_WRITTEN (type) = 1;
26357 return;
26361 if (TREE_ASM_WRITTEN (type) && !reverse)
26363 /* Variable-length types may be incomplete even if
26364 TREE_ASM_WRITTEN. For such types, fall through to
26365 gen_array_type_die() and possibly fill in
26366 DW_AT_{upper,lower}_bound attributes. */
26367 if ((TREE_CODE (type) != ARRAY_TYPE
26368 && TREE_CODE (type) != RECORD_TYPE
26369 && TREE_CODE (type) != UNION_TYPE
26370 && TREE_CODE (type) != QUAL_UNION_TYPE)
26371 || !variably_modified_type_p (type, NULL))
26372 return;
26375 switch (TREE_CODE (type))
26377 case ERROR_MARK:
26378 break;
26380 case POINTER_TYPE:
26381 case REFERENCE_TYPE:
26382 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26383 ensures that the gen_type_die recursion will terminate even if the
26384 type is recursive. Recursive types are possible in Ada. */
26385 /* ??? We could perhaps do this for all types before the switch
26386 statement. */
26387 TREE_ASM_WRITTEN (type) = 1;
26389 /* For these types, all that is required is that we output a DIE (or a
26390 set of DIEs) to represent the "basis" type. */
26391 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26392 DINFO_USAGE_IND_USE);
26393 break;
26395 case OFFSET_TYPE:
26396 /* This code is used for C++ pointer-to-data-member types.
26397 Output a description of the relevant class type. */
26398 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26399 DINFO_USAGE_IND_USE);
26401 /* Output a description of the type of the object pointed to. */
26402 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26403 DINFO_USAGE_IND_USE);
26405 /* Now output a DIE to represent this pointer-to-data-member type
26406 itself. */
26407 gen_ptr_to_mbr_type_die (type, context_die);
26408 break;
26410 case FUNCTION_TYPE:
26411 /* Force out return type (in case it wasn't forced out already). */
26412 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26413 DINFO_USAGE_DIR_USE);
26414 gen_subroutine_type_die (type, context_die);
26415 break;
26417 case METHOD_TYPE:
26418 /* Force out return type (in case it wasn't forced out already). */
26419 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26420 DINFO_USAGE_DIR_USE);
26421 gen_subroutine_type_die (type, context_die);
26422 break;
26424 case ARRAY_TYPE:
26425 case VECTOR_TYPE:
26426 gen_array_type_die (type, context_die);
26427 break;
26429 case ENUMERAL_TYPE:
26430 case RECORD_TYPE:
26431 case UNION_TYPE:
26432 case QUAL_UNION_TYPE:
26433 gen_tagged_type_die (type, context_die, usage, reverse);
26434 return;
26436 case VOID_TYPE:
26437 case OPAQUE_TYPE:
26438 case INTEGER_TYPE:
26439 case REAL_TYPE:
26440 case FIXED_POINT_TYPE:
26441 case COMPLEX_TYPE:
26442 case BOOLEAN_TYPE:
26443 case BITINT_TYPE:
26444 /* No DIEs needed for fundamental types. */
26445 break;
26447 case NULLPTR_TYPE:
26448 case LANG_TYPE:
26449 /* Just use DW_TAG_unspecified_type. */
26451 dw_die_ref type_die = lookup_type_die (type);
26452 if (type_die == NULL)
26454 tree name = TYPE_IDENTIFIER (type);
26455 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26456 type);
26457 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26458 equate_type_number_to_die (type, type_die);
26461 break;
26463 default:
26464 if (is_cxx_auto (type))
26466 tree name = TYPE_IDENTIFIER (type);
26467 dw_die_ref *die = (name == get_identifier ("auto")
26468 ? &auto_die : &decltype_auto_die);
26469 if (!*die)
26471 *die = new_die (DW_TAG_unspecified_type,
26472 comp_unit_die (), NULL_TREE);
26473 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26475 equate_type_number_to_die (type, *die);
26476 break;
26478 gcc_unreachable ();
26481 TREE_ASM_WRITTEN (type) = 1;
26484 static void
26485 gen_type_die (tree type, dw_die_ref context_die, bool reverse)
26487 if (type != error_mark_node)
26489 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE, reverse);
26490 if (flag_checking)
26492 dw_die_ref die = lookup_type_die (type);
26493 if (die)
26494 check_die (die);
26499 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26500 things which are local to the given block. */
26502 static void
26503 gen_block_die (tree stmt, dw_die_ref context_die)
26505 int must_output_die = 0;
26506 bool inlined_func;
26508 /* Ignore blocks that are NULL. */
26509 if (stmt == NULL_TREE)
26510 return;
26512 inlined_func = inlined_function_outer_scope_p (stmt);
26514 /* If the block is one fragment of a non-contiguous block, do not
26515 process the variables, since they will have been done by the
26516 origin block. Do process subblocks. */
26517 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26519 tree sub;
26521 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26522 gen_block_die (sub, context_die);
26524 return;
26527 /* Determine if we need to output any Dwarf DIEs at all to represent this
26528 block. */
26529 if (inlined_func)
26530 /* The outer scopes for inlinings *must* always be represented. We
26531 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26532 must_output_die = 1;
26533 else if (lookup_block_die (stmt))
26534 /* If we already have a DIE then it was filled early. Meanwhile
26535 we might have pruned all BLOCK_VARS as optimized out but we
26536 still want to generate high/low PC attributes so output it. */
26537 must_output_die = 1;
26538 else if (TREE_USED (stmt)
26539 || TREE_ASM_WRITTEN (stmt))
26541 /* Determine if this block directly contains any "significant"
26542 local declarations which we will need to output DIEs for. */
26543 if (debug_info_level > DINFO_LEVEL_TERSE)
26545 /* We are not in terse mode so any local declaration that
26546 is not ignored for debug purposes counts as being a
26547 "significant" one. */
26548 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26549 must_output_die = 1;
26550 else
26551 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26552 if (!DECL_IGNORED_P (var))
26554 must_output_die = 1;
26555 break;
26558 else if (!dwarf2out_ignore_block (stmt))
26559 must_output_die = 1;
26562 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26563 DIE for any block which contains no significant local declarations at
26564 all. Rather, in such cases we just call `decls_for_scope' so that any
26565 needed Dwarf info for any sub-blocks will get properly generated. Note
26566 that in terse mode, our definition of what constitutes a "significant"
26567 local declaration gets restricted to include only inlined function
26568 instances and local (nested) function definitions. */
26569 if (must_output_die)
26571 if (inlined_func)
26572 gen_inlined_subroutine_die (stmt, context_die);
26573 else
26574 gen_lexical_block_die (stmt, context_die);
26576 else
26577 decls_for_scope (stmt, context_die);
26580 /* Process variable DECL (or variable with origin ORIGIN) within
26581 block STMT and add it to CONTEXT_DIE. */
26582 static void
26583 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26585 dw_die_ref die;
26586 tree decl_or_origin = decl ? decl : origin;
26588 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26589 die = lookup_decl_die (decl_or_origin);
26590 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26592 if (TYPE_DECL_IS_STUB (decl_or_origin))
26593 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26594 else
26595 die = lookup_decl_die (decl_or_origin);
26596 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26597 if (! die && ! early_dwarf)
26598 return;
26600 else
26601 die = NULL;
26603 /* Avoid creating DIEs for local typedefs and concrete static variables that
26604 will only be pruned later. */
26605 if ((origin || decl_ultimate_origin (decl))
26606 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26607 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26609 origin = decl_ultimate_origin (decl_or_origin);
26610 if (decl && VAR_P (decl) && die != NULL)
26612 die = lookup_decl_die (origin);
26613 if (die != NULL)
26614 equate_decl_number_to_die (decl, die);
26616 return;
26619 if (die != NULL && die->die_parent == NULL)
26620 add_child_die (context_die, die);
26622 if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26624 if (early_dwarf)
26625 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26626 stmt, context_die);
26628 else
26630 if (decl && DECL_P (decl))
26632 die = lookup_decl_die (decl);
26634 /* Early created DIEs do not have a parent as the decls refer
26635 to the function as DECL_CONTEXT rather than the BLOCK. */
26636 if (die && die->die_parent == NULL)
26638 gcc_assert (in_lto_p);
26639 add_child_die (context_die, die);
26643 gen_decl_die (decl, origin, NULL, context_die);
26647 /* Generate all of the decls declared within a given scope and (recursively)
26648 all of its sub-blocks. */
26650 static void
26651 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26653 tree decl;
26654 unsigned int i;
26655 tree subblocks;
26657 /* Ignore NULL blocks. */
26658 if (stmt == NULL_TREE)
26659 return;
26661 /* Output the DIEs to represent all of the data objects and typedefs
26662 declared directly within this block but not within any nested
26663 sub-blocks. Also, nested function and tag DIEs have been
26664 generated with a parent of NULL; fix that up now. We don't
26665 have to do this if we're at -g1. */
26666 if (debug_info_level > DINFO_LEVEL_TERSE)
26668 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26669 process_scope_var (stmt, decl, NULL_TREE, context_die);
26670 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26671 origin - avoid doing this twice as we have no good way to see
26672 if we've done it once already. */
26673 if (! early_dwarf)
26674 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26676 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26677 if (decl == current_function_decl)
26678 /* Ignore declarations of the current function, while they
26679 are declarations, gen_subprogram_die would treat them
26680 as definitions again, because they are equal to
26681 current_function_decl and endlessly recurse. */;
26682 else if (TREE_CODE (decl) == FUNCTION_DECL)
26683 process_scope_var (stmt, decl, NULL_TREE, context_die);
26684 else
26685 process_scope_var (stmt, NULL_TREE, decl, context_die);
26689 /* Even if we're at -g1, we need to process the subblocks in order to get
26690 inlined call information. */
26692 /* Output the DIEs to represent all sub-blocks (and the items declared
26693 therein) of this block. */
26694 if (recurse)
26695 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26696 subblocks != NULL;
26697 subblocks = BLOCK_CHAIN (subblocks))
26698 gen_block_die (subblocks, context_die);
26701 /* Is this a typedef we can avoid emitting? */
26703 static bool
26704 is_redundant_typedef (const_tree decl)
26706 if (TYPE_DECL_IS_STUB (decl))
26707 return true;
26709 if (DECL_ARTIFICIAL (decl)
26710 && DECL_CONTEXT (decl)
26711 && is_tagged_type (DECL_CONTEXT (decl))
26712 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26713 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26714 /* Also ignore the artificial member typedef for the class name. */
26715 return true;
26717 return false;
26720 /* Return TRUE if TYPE is a typedef that names a type for linkage
26721 purposes. This kind of typedefs is produced by the C++ FE for
26722 constructs like:
26724 typedef struct {...} foo;
26726 In that case, there is no typedef variant type produced for foo.
26727 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26728 struct type. */
26730 static bool
26731 is_naming_typedef_decl (const_tree decl)
26733 if (decl == NULL_TREE
26734 || TREE_CODE (decl) != TYPE_DECL
26735 || DECL_NAMELESS (decl)
26736 || !is_tagged_type (TREE_TYPE (decl))
26737 || DECL_IS_UNDECLARED_BUILTIN (decl)
26738 || is_redundant_typedef (decl)
26739 /* It looks like Ada produces TYPE_DECLs that are very similar
26740 to C++ naming typedefs but that have different
26741 semantics. Let's be specific to c++ for now. */
26742 || !is_cxx (decl))
26743 return false;
26745 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26746 && TYPE_NAME (TREE_TYPE (decl)) == decl
26747 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26748 != TYPE_NAME (TREE_TYPE (decl))));
26751 /* Looks up the DIE for a context. */
26753 static inline dw_die_ref
26754 lookup_context_die (tree context)
26756 if (context)
26758 /* Find die that represents this context. */
26759 if (TYPE_P (context))
26761 context = TYPE_MAIN_VARIANT (context);
26762 dw_die_ref ctx = lookup_type_die (context);
26763 if (!ctx)
26764 return NULL;
26765 return strip_naming_typedef (context, ctx);
26767 else
26768 return lookup_decl_die (context);
26770 return comp_unit_die ();
26773 /* Returns the DIE for a context. */
26775 static inline dw_die_ref
26776 get_context_die (tree context)
26778 if (context)
26780 /* Find die that represents this context. */
26781 if (TYPE_P (context))
26783 context = TYPE_MAIN_VARIANT (context);
26784 return strip_naming_typedef (context, force_type_die (context));
26786 else
26787 return force_decl_die (context);
26789 return comp_unit_die ();
26792 /* Returns the DIE for decl. A DIE will always be returned. */
26794 static dw_die_ref
26795 force_decl_die (tree decl)
26797 dw_die_ref decl_die;
26798 unsigned saved_external_flag;
26799 tree save_fn = NULL_TREE;
26800 decl_die = lookup_decl_die (decl);
26801 if (!decl_die)
26803 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26805 decl_die = lookup_decl_die (decl);
26806 if (decl_die)
26807 return decl_die;
26809 switch (TREE_CODE (decl))
26811 case FUNCTION_DECL:
26812 /* Clear current_function_decl, so that gen_subprogram_die thinks
26813 that this is a declaration. At this point, we just want to force
26814 declaration die. */
26815 save_fn = current_function_decl;
26816 current_function_decl = NULL_TREE;
26817 gen_subprogram_die (decl, context_die);
26818 current_function_decl = save_fn;
26819 break;
26821 case VAR_DECL:
26822 /* Set external flag to force declaration die. Restore it after
26823 gen_decl_die() call. */
26824 saved_external_flag = DECL_EXTERNAL (decl);
26825 DECL_EXTERNAL (decl) = 1;
26826 gen_decl_die (decl, NULL, NULL, context_die);
26827 DECL_EXTERNAL (decl) = saved_external_flag;
26828 break;
26830 case NAMESPACE_DECL:
26831 if (dwarf_version >= 3 || !dwarf_strict)
26832 dwarf2out_decl (decl);
26833 else
26834 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26835 decl_die = comp_unit_die ();
26836 break;
26838 case CONST_DECL:
26839 /* Enumerators shouldn't need force_decl_die. */
26840 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26841 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26842 gen_decl_die (decl, NULL, NULL, context_die);
26843 break;
26845 case TRANSLATION_UNIT_DECL:
26846 decl_die = comp_unit_die ();
26847 break;
26849 default:
26850 gcc_unreachable ();
26853 /* We should be able to find the DIE now. */
26854 if (!decl_die)
26855 decl_die = lookup_decl_die (decl);
26856 gcc_assert (decl_die);
26859 return decl_die;
26862 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26863 always returned. */
26865 static dw_die_ref
26866 force_type_die (tree type)
26868 dw_die_ref type_die;
26870 type_die = lookup_type_die (type);
26871 if (!type_die)
26873 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26875 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26876 false, context_die);
26877 gcc_assert (type_die);
26879 return type_die;
26882 /* Force out any required namespaces to be able to output DECL,
26883 and return the new context_die for it, if it's changed. */
26885 static dw_die_ref
26886 setup_namespace_context (tree thing, dw_die_ref context_die)
26888 tree context = (DECL_P (thing)
26889 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26890 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26891 /* Force out the namespace. */
26892 context_die = force_decl_die (context);
26894 return context_die;
26897 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26898 type) within its namespace, if appropriate.
26900 For compatibility with older debuggers, namespace DIEs only contain
26901 declarations; all definitions are emitted at CU scope, with
26902 DW_AT_specification pointing to the declaration (like with class
26903 members). */
26905 static dw_die_ref
26906 declare_in_namespace (tree thing, dw_die_ref context_die)
26908 dw_die_ref ns_context;
26910 if (debug_info_level <= DINFO_LEVEL_TERSE)
26911 return context_die;
26913 /* External declarations in the local scope only need to be emitted
26914 once, not once in the namespace and once in the scope.
26916 This avoids declaring the `extern' below in the
26917 namespace DIE as well as in the innermost scope:
26919 namespace S
26921 int i=5;
26922 int foo()
26924 int i=8;
26925 extern int i;
26926 return i;
26930 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26931 return context_die;
26933 /* If this decl is from an inlined function, then don't try to emit it in its
26934 namespace, as we will get confused. It would have already been emitted
26935 when the abstract instance of the inline function was emitted anyways. */
26936 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26937 return context_die;
26939 ns_context = setup_namespace_context (thing, context_die);
26941 if (ns_context != context_die)
26943 if (is_fortran () || is_dlang ())
26944 return ns_context;
26945 if (DECL_P (thing))
26946 gen_decl_die (thing, NULL, NULL, ns_context);
26947 else
26948 gen_type_die (thing, ns_context);
26950 return context_die;
26953 /* Generate a DIE for a namespace or namespace alias. */
26955 static void
26956 gen_namespace_die (tree decl, dw_die_ref context_die)
26958 dw_die_ref namespace_die;
26960 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26961 they are an alias of. */
26962 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26964 /* Output a real namespace or module. */
26965 context_die = setup_namespace_context (decl, comp_unit_die ());
26966 namespace_die = new_die (is_fortran () || is_dlang ()
26967 ? DW_TAG_module : DW_TAG_namespace,
26968 context_die, decl);
26969 /* For Fortran modules defined in different CU don't add src coords. */
26970 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26972 const char *name = dwarf2_name (decl, 0);
26973 if (name)
26974 add_name_attribute (namespace_die, name);
26976 else
26977 add_name_and_src_coords_attributes (namespace_die, decl);
26978 if (DECL_EXTERNAL (decl))
26979 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26980 equate_decl_number_to_die (decl, namespace_die);
26982 else
26984 /* Output a namespace alias. */
26986 /* Force out the namespace we are an alias of, if necessary. */
26987 dw_die_ref origin_die
26988 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26990 if (DECL_FILE_SCOPE_P (decl)
26991 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26992 context_die = setup_namespace_context (decl, comp_unit_die ());
26993 /* Now create the namespace alias DIE. */
26994 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26995 add_name_and_src_coords_attributes (namespace_die, decl);
26996 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26997 equate_decl_number_to_die (decl, namespace_die);
26999 if ((dwarf_version >= 5 || !dwarf_strict)
27000 && lang_hooks.decls.decl_dwarf_attribute (decl,
27001 DW_AT_export_symbols) == 1)
27002 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
27004 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
27005 if (want_pubnames ())
27006 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
27009 /* Generate Dwarf debug information for a decl described by DECL.
27010 The return value is currently only meaningful for PARM_DECLs,
27011 for all other decls it returns NULL.
27013 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
27014 It can be NULL otherwise. */
27016 static dw_die_ref
27017 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
27018 dw_die_ref context_die)
27020 tree decl_or_origin = decl ? decl : origin;
27021 tree class_origin = NULL, ultimate_origin;
27023 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
27024 return NULL;
27026 switch (TREE_CODE (decl_or_origin))
27028 case ERROR_MARK:
27029 break;
27031 case CONST_DECL:
27032 if (!is_fortran () && !is_ada () && !is_dlang ())
27034 /* The individual enumerators of an enum type get output when we output
27035 the Dwarf representation of the relevant enum type itself. */
27036 break;
27039 /* Emit its type. */
27040 gen_type_die (TREE_TYPE (decl), context_die);
27042 /* And its containing namespace. */
27043 context_die = declare_in_namespace (decl, context_die);
27045 gen_const_die (decl, context_die);
27046 break;
27048 case FUNCTION_DECL:
27049 #if 0
27050 /* FIXME */
27051 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
27052 on local redeclarations of global functions. That seems broken. */
27053 if (current_function_decl != decl)
27054 /* This is only a declaration. */;
27055 #endif
27057 /* We should have abstract copies already and should not generate
27058 stray type DIEs in late LTO dumping. */
27059 if (! early_dwarf)
27062 /* If we're emitting a clone, emit info for the abstract instance. */
27063 else if (origin || DECL_ORIGIN (decl) != decl)
27064 dwarf2out_abstract_function (origin
27065 ? DECL_ORIGIN (origin)
27066 : DECL_ABSTRACT_ORIGIN (decl));
27068 /* If we're emitting a possibly inlined function emit it as
27069 abstract instance. */
27070 else if (cgraph_function_possibly_inlined_p (decl)
27071 && ! DECL_ABSTRACT_P (decl)
27072 && ! class_or_namespace_scope_p (context_die)
27073 /* dwarf2out_abstract_function won't emit a die if this is just
27074 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
27075 that case, because that works only if we have a die. */
27076 && DECL_INITIAL (decl) != NULL_TREE)
27077 dwarf2out_abstract_function (decl);
27079 /* Otherwise we're emitting the primary DIE for this decl. */
27080 else if (debug_info_level > DINFO_LEVEL_TERSE)
27082 /* Before we describe the FUNCTION_DECL itself, make sure that we
27083 have its containing type. */
27084 if (!origin)
27085 origin = decl_class_context (decl);
27086 if (origin != NULL_TREE)
27087 gen_type_die (origin, context_die);
27089 /* And its return type. */
27090 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27092 /* And its virtual context. */
27093 if (DECL_VINDEX (decl) != NULL_TREE)
27094 gen_type_die (DECL_CONTEXT (decl), context_die);
27096 /* Make sure we have a member DIE for decl. */
27097 if (origin != NULL_TREE)
27098 gen_type_die_for_member (origin, decl, context_die);
27100 /* And its containing namespace. */
27101 context_die = declare_in_namespace (decl, context_die);
27104 /* Now output a DIE to represent the function itself. */
27105 if (decl)
27106 gen_subprogram_die (decl, context_die);
27107 break;
27109 case TYPE_DECL:
27110 /* If we are in terse mode, don't generate any DIEs to represent any
27111 actual typedefs. */
27112 if (debug_info_level <= DINFO_LEVEL_TERSE)
27113 break;
27115 /* In the special case of a TYPE_DECL node representing the declaration
27116 of some type tag, if the given TYPE_DECL is marked as having been
27117 instantiated from some other (original) TYPE_DECL node (e.g. one which
27118 was generated within the original definition of an inline function) we
27119 used to generate a special (abbreviated) DW_TAG_structure_type,
27120 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27121 should be actually referencing those DIEs, as variable DIEs with that
27122 type would be emitted already in the abstract origin, so it was always
27123 removed during unused type prunning. Don't add anything in this
27124 case. */
27125 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27126 break;
27128 if (is_redundant_typedef (decl))
27129 gen_type_die (TREE_TYPE (decl), context_die);
27130 else
27131 /* Output a DIE to represent the typedef itself. */
27132 gen_typedef_die (decl, context_die);
27133 break;
27135 case LABEL_DECL:
27136 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27137 gen_label_die (decl, context_die);
27138 break;
27140 case VAR_DECL:
27141 case RESULT_DECL:
27142 /* If we are in terse mode, don't generate any DIEs to represent any
27143 variable declarations or definitions unless it is external. */
27144 if (debug_info_level < DINFO_LEVEL_TERSE
27145 || (debug_info_level == DINFO_LEVEL_TERSE
27146 && !TREE_PUBLIC (decl_or_origin)))
27147 break;
27149 if (debug_info_level > DINFO_LEVEL_TERSE)
27151 /* Avoid generating stray type DIEs during late dwarf dumping.
27152 All types have been dumped early. */
27153 if (early_dwarf
27154 /* ??? But in LTRANS we cannot annotate early created variably
27155 modified type DIEs without copying them and adjusting all
27156 references to them. Dump them again as happens for inlining
27157 which copies both the decl and the types. */
27158 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27159 in VLA bound information for example. */
27160 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27161 current_function_decl)))
27163 /* Output any DIEs that are needed to specify the type of this data
27164 object. */
27165 if (decl_by_reference_p (decl_or_origin))
27166 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27167 else
27168 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27171 if (early_dwarf)
27173 /* And its containing type. */
27174 class_origin = decl_class_context (decl_or_origin);
27175 if (class_origin != NULL_TREE)
27176 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27178 /* And its containing namespace. */
27179 context_die = declare_in_namespace (decl_or_origin, context_die);
27183 /* Now output the DIE to represent the data object itself. This gets
27184 complicated because of the possibility that the VAR_DECL really
27185 represents an inlined instance of a formal parameter for an inline
27186 function. */
27187 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27188 if (ultimate_origin != NULL_TREE
27189 && TREE_CODE (ultimate_origin) == PARM_DECL)
27190 gen_formal_parameter_die (decl, origin,
27191 true /* Emit name attribute. */,
27192 context_die);
27193 else
27194 gen_variable_die (decl, origin, context_die);
27195 break;
27197 case FIELD_DECL:
27198 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27199 /* Ignore the nameless fields that are used to skip bits but handle C++
27200 anonymous unions and structs. */
27201 if (DECL_NAME (decl) != NULL_TREE
27202 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27203 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27205 gen_type_die (member_declared_type (decl), context_die);
27206 gen_field_die (decl, ctx, context_die);
27208 break;
27210 case PARM_DECL:
27211 /* Avoid generating stray type DIEs during late dwarf dumping.
27212 All types have been dumped early. */
27213 if (early_dwarf
27214 /* ??? But in LTRANS we cannot annotate early created variably
27215 modified type DIEs without copying them and adjusting all
27216 references to them. Dump them again as happens for inlining
27217 which copies both the decl and the types. */
27218 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27219 in VLA bound information for example. */
27220 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27221 current_function_decl)))
27223 if (DECL_BY_REFERENCE (decl_or_origin))
27224 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27225 else
27226 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27228 return gen_formal_parameter_die (decl, origin,
27229 true /* Emit name attribute. */,
27230 context_die);
27232 case NAMESPACE_DECL:
27233 if (dwarf_version >= 3 || !dwarf_strict)
27234 gen_namespace_die (decl, context_die);
27235 break;
27237 case IMPORTED_DECL:
27238 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27239 DECL_CONTEXT (decl), context_die);
27240 break;
27242 case NAMELIST_DECL:
27243 gen_namelist_decl (DECL_NAME (decl), context_die,
27244 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27245 break;
27247 default:
27248 /* Probably some frontend-internal decl. Assume we don't care. */
27249 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27250 break;
27253 return NULL;
27256 /* Output initial debug information for global DECL. Called at the
27257 end of the parsing process.
27259 This is the initial debug generation process. As such, the DIEs
27260 generated may be incomplete. A later debug generation pass
27261 (dwarf2out_late_global_decl) will augment the information generated
27262 in this pass (e.g., with complete location info). */
27264 static void
27265 dwarf2out_early_global_decl (tree decl)
27267 set_early_dwarf s;
27269 /* gen_decl_die() will set DECL_ABSTRACT because
27270 cgraph_function_possibly_inlined_p() returns true. This is in
27271 turn will cause DW_AT_inline attributes to be set.
27273 This happens because at early dwarf generation, there is no
27274 cgraph information, causing cgraph_function_possibly_inlined_p()
27275 to return true. Trick cgraph_function_possibly_inlined_p()
27276 while we generate dwarf early. */
27277 bool save = symtab->global_info_ready;
27278 symtab->global_info_ready = true;
27280 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27281 other DECLs and they can point to template types or other things
27282 that dwarf2out can't handle when done via dwarf2out_decl. */
27283 if (TREE_CODE (decl) != TYPE_DECL
27284 && TREE_CODE (decl) != PARM_DECL)
27286 if (TREE_CODE (decl) == FUNCTION_DECL)
27288 tree save_fndecl = current_function_decl;
27290 /* For nested functions, make sure we have DIEs for the parents first
27291 so that all nested DIEs are generated at the proper scope in the
27292 first shot. */
27293 tree context = decl_function_context (decl);
27294 if (context != NULL)
27296 dw_die_ref context_die = lookup_decl_die (context);
27297 current_function_decl = context;
27299 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27300 enough so that it lands in its own context. This avoids type
27301 pruning issues later on. */
27302 if (context_die == NULL || is_declaration_die (context_die))
27303 dwarf2out_early_global_decl (context);
27306 /* Emit an abstract origin of a function first. This happens
27307 with C++ constructor clones for example and makes
27308 dwarf2out_abstract_function happy which requires the early
27309 DIE of the abstract instance to be present. */
27310 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27311 dw_die_ref origin_die;
27312 if (origin != NULL
27313 /* Do not emit the DIE multiple times but make sure to
27314 process it fully here in case we just saw a declaration. */
27315 && ((origin_die = lookup_decl_die (origin)) == NULL
27316 || is_declaration_die (origin_die)))
27318 current_function_decl = origin;
27319 dwarf2out_decl (origin);
27322 /* Emit the DIE for decl but avoid doing that multiple times. */
27323 dw_die_ref old_die;
27324 if ((old_die = lookup_decl_die (decl)) == NULL
27325 || is_declaration_die (old_die))
27327 current_function_decl = decl;
27328 dwarf2out_decl (decl);
27331 current_function_decl = save_fndecl;
27333 else
27334 dwarf2out_decl (decl);
27336 symtab->global_info_ready = save;
27339 /* Return whether EXPR is an expression with the following pattern:
27340 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27342 static bool
27343 is_trivial_indirect_ref (tree expr)
27345 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27346 return false;
27348 tree nop = TREE_OPERAND (expr, 0);
27349 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27350 return false;
27352 tree int_cst = TREE_OPERAND (nop, 0);
27353 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27356 /* Output debug information for global decl DECL. Called from
27357 toplev.cc after compilation proper has finished. */
27359 static void
27360 dwarf2out_late_global_decl (tree decl)
27362 /* Fill-in any location information we were unable to determine
27363 on the first pass. */
27364 if (VAR_P (decl))
27366 dw_die_ref die = lookup_decl_die (decl);
27368 /* We may have to generate full debug late for LTO in case debug
27369 was not enabled at compile-time or the target doesn't support
27370 the LTO early debug scheme. */
27371 if (! die && in_lto_p
27372 /* Function scope variables are emitted when emitting the
27373 DIE for the function. */
27374 && ! local_function_static (decl))
27375 dwarf2out_decl (decl);
27376 else if (die)
27378 /* We get called via the symtab code invoking late_global_decl
27379 for symbols that are optimized out.
27381 Do not add locations for those, except if they have a
27382 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27383 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27384 INDIRECT_REF expression, as this could generate relocations to
27385 text symbols in LTO object files, which is invalid. */
27386 varpool_node *node = varpool_node::get (decl);
27387 if ((! node || ! node->definition)
27388 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27389 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27390 tree_add_const_value_attribute_for_decl (die, decl);
27391 else
27392 add_location_or_const_value_attribute (die, decl, false);
27397 /* Output debug information for type decl DECL. Called from toplev.cc
27398 and from language front ends (to record built-in types). */
27399 static void
27400 dwarf2out_type_decl (tree decl, int local)
27402 if (!local)
27404 set_early_dwarf s;
27405 dwarf2out_decl (decl);
27409 /* Output debug information for imported module or decl DECL.
27410 NAME is non-NULL name in the lexical block if the decl has been renamed.
27411 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27412 that DECL belongs to.
27413 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27414 static void
27415 dwarf2out_imported_module_or_decl_1 (tree decl,
27416 tree name,
27417 tree lexical_block,
27418 dw_die_ref lexical_block_die)
27420 expanded_location xloc;
27421 dw_die_ref imported_die = NULL;
27422 dw_die_ref at_import_die;
27424 if (TREE_CODE (decl) == IMPORTED_DECL)
27426 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27427 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27428 gcc_assert (decl);
27430 else
27431 xloc = expand_location (input_location);
27433 if (TREE_CODE (decl) == TYPE_DECL)
27435 at_import_die = force_type_die (TREE_TYPE (decl));
27436 /* For namespace N { typedef void T; } using N::T; base_type_die
27437 returns NULL, but DW_TAG_imported_declaration requires
27438 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27439 if (!at_import_die)
27441 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27442 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27443 at_import_die = lookup_type_die (TREE_TYPE (decl));
27444 gcc_assert (at_import_die);
27447 else
27449 at_import_die = lookup_decl_die (decl);
27450 if (!at_import_die)
27452 /* If we're trying to avoid duplicate debug info, we may not have
27453 emitted the member decl for this field. Emit it now. */
27454 if (TREE_CODE (decl) == FIELD_DECL)
27456 tree type = DECL_CONTEXT (decl);
27458 if (TYPE_CONTEXT (type)
27459 && TYPE_P (TYPE_CONTEXT (type))
27460 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27461 DINFO_USAGE_DIR_USE))
27462 return;
27463 gen_type_die_for_member (type, decl,
27464 get_context_die (TYPE_CONTEXT (type)));
27466 if (TREE_CODE (decl) == CONST_DECL)
27468 /* Individual enumerators of an enum type do not get output here
27469 (see gen_decl_die), so we cannot call force_decl_die. */
27470 if (!is_fortran () && !is_ada () && !is_dlang ())
27471 return;
27473 if (TREE_CODE (decl) == NAMELIST_DECL)
27474 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27475 get_context_die (DECL_CONTEXT (decl)),
27476 NULL_TREE);
27477 else
27478 at_import_die = force_decl_die (decl);
27482 if (TREE_CODE (decl) == NAMESPACE_DECL)
27484 if (dwarf_version >= 3 || !dwarf_strict)
27485 imported_die = new_die (DW_TAG_imported_module,
27486 lexical_block_die,
27487 lexical_block);
27488 else
27489 return;
27491 else
27492 imported_die = new_die (DW_TAG_imported_declaration,
27493 lexical_block_die,
27494 lexical_block);
27496 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27497 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27498 if (debug_column_info && xloc.column)
27499 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27500 if (name)
27501 add_AT_string (imported_die, DW_AT_name,
27502 IDENTIFIER_POINTER (name));
27503 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27506 /* Output debug information for imported module or decl DECL.
27507 NAME is non-NULL name in context if the decl has been renamed.
27508 CHILD is true if decl is one of the renamed decls as part of
27509 importing whole module.
27510 IMPLICIT is set if this hook is called for an implicit import
27511 such as inline namespace. */
27513 static void
27514 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27515 bool child, bool implicit)
27517 /* dw_die_ref at_import_die; */
27518 dw_die_ref scope_die;
27520 if (debug_info_level <= DINFO_LEVEL_TERSE)
27521 return;
27523 gcc_assert (decl);
27525 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27526 should be enough, for DWARF4 and older even if we emit as extension
27527 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27528 for the benefit of consumers unaware of DW_AT_export_symbols. */
27529 if (implicit
27530 && dwarf_version >= 5
27531 && lang_hooks.decls.decl_dwarf_attribute (decl,
27532 DW_AT_export_symbols) == 1)
27533 return;
27535 set_early_dwarf s;
27537 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27538 We need decl DIE for reference and scope die. First, get DIE for the decl
27539 itself. */
27541 /* Get the scope die for decl context. Use comp_unit_die for global module
27542 or decl. If die is not found for non globals, force new die. */
27543 if (context
27544 && TYPE_P (context)
27545 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27546 return;
27548 scope_die = get_context_die (context);
27550 if (child)
27552 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27553 there is nothing we can do, here. */
27554 if (dwarf_version < 3 && dwarf_strict)
27555 return;
27557 gcc_assert (scope_die->die_child);
27558 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27559 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27560 scope_die = scope_die->die_child;
27563 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27564 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27567 /* Output debug information for namelists. */
27569 static dw_die_ref
27570 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27572 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27573 tree value;
27574 unsigned i;
27576 if (debug_info_level <= DINFO_LEVEL_TERSE)
27577 return NULL;
27579 gcc_assert (scope_die != NULL);
27580 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27581 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27583 /* If there are no item_decls, we have a nondefining namelist, e.g.
27584 with USE association; hence, set DW_AT_declaration. */
27585 if (item_decls == NULL_TREE)
27587 add_AT_flag (nml_die, DW_AT_declaration, 1);
27588 return nml_die;
27591 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27593 nml_item_ref_die = lookup_decl_die (value);
27594 if (!nml_item_ref_die)
27595 nml_item_ref_die = force_decl_die (value);
27597 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27598 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27600 return nml_die;
27604 /* Write the debugging output for DECL and return the DIE. */
27606 static void
27607 dwarf2out_decl (tree decl)
27609 dw_die_ref context_die = comp_unit_die ();
27611 switch (TREE_CODE (decl))
27613 case ERROR_MARK:
27614 return;
27616 case FUNCTION_DECL:
27617 /* If we're a nested function, initially use a parent of NULL; if we're
27618 a plain function, this will be fixed up in decls_for_scope. If
27619 we're a method, it will be ignored, since we already have a DIE.
27620 Avoid doing this late though since clones of class methods may
27621 otherwise end up in limbo and create type DIEs late. */
27622 if (early_dwarf
27623 && decl_function_context (decl)
27624 /* But if we're in terse mode, we don't care about scope. */
27625 && debug_info_level > DINFO_LEVEL_TERSE)
27626 context_die = NULL;
27627 break;
27629 case VAR_DECL:
27630 /* For local statics lookup proper context die. */
27631 if (local_function_static (decl))
27632 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27634 /* If we are in terse mode, don't generate any DIEs to represent any
27635 variable declarations or definitions unless it is external. */
27636 if (debug_info_level < DINFO_LEVEL_TERSE
27637 || (debug_info_level == DINFO_LEVEL_TERSE
27638 && !TREE_PUBLIC (decl)))
27639 return;
27640 break;
27642 case CONST_DECL:
27643 if (debug_info_level <= DINFO_LEVEL_TERSE)
27644 return;
27645 if (!is_fortran () && !is_ada () && !is_dlang ())
27646 return;
27647 if (TREE_STATIC (decl) && decl_function_context (decl))
27648 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27649 break;
27651 case NAMESPACE_DECL:
27652 case IMPORTED_DECL:
27653 if (debug_info_level <= DINFO_LEVEL_TERSE)
27654 return;
27655 if (lookup_decl_die (decl) != NULL)
27656 return;
27657 break;
27659 case TYPE_DECL:
27660 /* Don't emit stubs for types unless they are needed by other DIEs. */
27661 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27662 return;
27664 /* Don't bother trying to generate any DIEs to represent any of the
27665 normal built-in types for the language we are compiling. */
27666 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27667 return;
27669 /* If we are in terse mode, don't generate any DIEs for types. */
27670 if (debug_info_level <= DINFO_LEVEL_TERSE)
27671 return;
27673 /* If we're a function-scope tag, initially use a parent of NULL;
27674 this will be fixed up in decls_for_scope. */
27675 if (decl_function_context (decl))
27676 context_die = NULL;
27678 break;
27680 case NAMELIST_DECL:
27681 break;
27683 default:
27684 return;
27687 gen_decl_die (decl, NULL, NULL, context_die);
27689 if (flag_checking)
27691 dw_die_ref die = lookup_decl_die (decl);
27692 if (die)
27693 check_die (die);
27697 /* Write the debugging output for DECL. */
27699 static void
27700 dwarf2out_function_decl (tree decl)
27702 dwarf2out_decl (decl);
27703 call_arg_locations = NULL;
27704 call_arg_loc_last = NULL;
27705 call_site_count = -1;
27706 tail_call_site_count = -1;
27707 decl_loc_table->empty ();
27708 cached_dw_loc_list_table->empty ();
27711 /* Output a marker (i.e. a label) for the beginning of the generated code for
27712 a lexical block. */
27714 static void
27715 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27716 unsigned int blocknum)
27718 switch_to_section (current_function_section ());
27719 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27722 /* Output a marker (i.e. a label) for the end of the generated code for a
27723 lexical block. */
27725 static void
27726 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27728 switch_to_section (current_function_section ());
27729 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27732 /* Returns true if it is appropriate not to emit any debugging
27733 information for BLOCK, because it doesn't contain any instructions.
27735 Don't allow this for blocks with nested functions or local classes
27736 as we would end up with orphans, and in the presence of scheduling
27737 we may end up calling them anyway. */
27739 static bool
27740 dwarf2out_ignore_block (const_tree block)
27742 tree decl;
27743 unsigned int i;
27745 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27746 if (TREE_CODE (decl) == FUNCTION_DECL
27747 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27748 return false;
27749 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27751 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27752 if (TREE_CODE (decl) == FUNCTION_DECL
27753 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27754 return false;
27757 return true;
27760 /* Hash table routines for file_hash. */
27762 bool
27763 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27765 return filename_cmp (p1->key, p2) == 0;
27768 hashval_t
27769 dwarf_file_hasher::hash (dwarf_file_data *p)
27771 return htab_hash_string (p->key);
27774 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27775 dwarf2out.cc) and return its "index". The index of each (known) filename is
27776 just a unique number which is associated with only that one filename. We
27777 need such numbers for the sake of generating labels (in the .debug_sfnames
27778 section) and references to those files numbers (in the .debug_srcinfo
27779 and .debug_macinfo sections). If the filename given as an argument is not
27780 found in our current list, add it to the list and assign it the next
27781 available unique index number. */
27783 static struct dwarf_file_data *
27784 lookup_filename (const char *file_name)
27786 struct dwarf_file_data * created;
27788 if (!file_name)
27789 return NULL;
27791 if (!file_name[0])
27792 file_name = "<stdin>";
27794 dwarf_file_data **slot
27795 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27796 INSERT);
27797 if (*slot)
27798 return *slot;
27800 created = ggc_alloc<dwarf_file_data> ();
27801 created->key = file_name;
27802 created->filename = remap_debug_filename (file_name);
27803 created->emitted_number = 0;
27804 *slot = created;
27805 return created;
27808 /* If the assembler will construct the file table, then translate the compiler
27809 internal file table number into the assembler file table number, and emit
27810 a .file directive if we haven't already emitted one yet. The file table
27811 numbers are different because we prune debug info for unused variables and
27812 types, which may include filenames. */
27814 static int
27815 maybe_emit_file (struct dwarf_file_data * fd)
27817 if (! fd->emitted_number)
27819 if (last_emitted_file)
27820 fd->emitted_number = last_emitted_file->emitted_number + 1;
27821 else
27822 fd->emitted_number = 1;
27823 last_emitted_file = fd;
27825 if (output_asm_line_debug_info ())
27827 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27828 output_quoted_string (asm_out_file, fd->filename);
27829 fputc ('\n', asm_out_file);
27833 return fd->emitted_number;
27836 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27837 That generation should happen after function debug info has been
27838 generated. The value of the attribute is the constant value of ARG. */
27840 static void
27841 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27843 die_arg_entry entry;
27845 if (!die || !arg)
27846 return;
27848 gcc_assert (early_dwarf);
27850 if (!tmpl_value_parm_die_table)
27851 vec_alloc (tmpl_value_parm_die_table, 32);
27853 entry.die = die;
27854 entry.arg = arg;
27855 vec_safe_push (tmpl_value_parm_die_table, entry);
27858 /* Return TRUE if T is an instance of generic type, FALSE
27859 otherwise. */
27861 static bool
27862 generic_type_p (tree t)
27864 if (t == NULL_TREE || !TYPE_P (t))
27865 return false;
27866 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27869 /* Schedule the generation of the generic parameter dies for the
27870 instance of generic type T. The proper generation itself is later
27871 done by gen_scheduled_generic_parms_dies. */
27873 static void
27874 schedule_generic_params_dies_gen (tree t)
27876 if (!generic_type_p (t))
27877 return;
27879 gcc_assert (early_dwarf);
27881 if (!generic_type_instances)
27882 vec_alloc (generic_type_instances, 256);
27884 vec_safe_push (generic_type_instances, t);
27887 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27888 by append_entry_to_tmpl_value_parm_die_table. This function must
27889 be called after function DIEs have been generated. */
27891 static void
27892 gen_remaining_tmpl_value_param_die_attribute (void)
27894 if (tmpl_value_parm_die_table)
27896 unsigned i, j;
27897 die_arg_entry *e;
27899 /* We do this in two phases - first get the cases we can
27900 handle during early-finish, preserving those we cannot
27901 (containing symbolic constants where we don't yet know
27902 whether we are going to output the referenced symbols).
27903 For those we try again at late-finish. */
27904 j = 0;
27905 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27907 if (!e->die->removed
27908 && !tree_add_const_value_attribute (e->die, e->arg))
27910 dw_loc_descr_ref loc = NULL;
27911 if (! early_dwarf
27912 && (dwarf_version >= 5 || !dwarf_strict))
27913 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27914 if (loc)
27915 add_AT_loc (e->die, DW_AT_location, loc);
27916 else
27917 (*tmpl_value_parm_die_table)[j++] = *e;
27920 tmpl_value_parm_die_table->truncate (j);
27924 /* Generate generic parameters DIEs for instances of generic types
27925 that have been previously scheduled by
27926 schedule_generic_params_dies_gen. This function must be called
27927 after all the types of the CU have been laid out. */
27929 static void
27930 gen_scheduled_generic_parms_dies (void)
27932 unsigned i;
27933 tree t;
27935 if (!generic_type_instances)
27936 return;
27938 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27939 if (COMPLETE_TYPE_P (t))
27940 gen_generic_params_dies (t);
27942 generic_type_instances = NULL;
27946 /* Replace DW_AT_name for the decl with name. */
27948 static void
27949 dwarf2out_set_name (tree decl, tree name)
27951 dw_die_ref die;
27952 dw_attr_node *attr;
27953 const char *dname;
27955 die = TYPE_SYMTAB_DIE (decl);
27956 if (!die)
27957 return;
27959 dname = dwarf2_name (name, 0);
27960 if (!dname)
27961 return;
27963 attr = get_AT (die, DW_AT_name);
27964 if (attr)
27966 struct indirect_string_node *node;
27968 node = find_AT_string (dname);
27969 /* replace the string. */
27970 attr->dw_attr_val.v.val_str = node;
27973 else
27974 add_name_attribute (die, dname);
27977 /* True if before or during processing of the first function being emitted. */
27978 static bool in_first_function_p = true;
27979 /* True if loc_note during dwarf2out_var_location call might still be
27980 before first real instruction at address equal to .Ltext0. */
27981 static bool maybe_at_text_label_p = true;
27982 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27983 static unsigned int first_loclabel_num_not_at_text_label;
27985 /* Look ahead for a real insn. */
27987 static rtx_insn *
27988 dwarf2out_next_real_insn (rtx_insn *loc_note)
27990 rtx_insn *next_real = NEXT_INSN (loc_note);
27992 while (next_real)
27993 if (INSN_P (next_real))
27994 break;
27995 else
27996 next_real = NEXT_INSN (next_real);
27998 return next_real;
28001 /* Called by the final INSN scan whenever we see a var location. We
28002 use it to drop labels in the right places, and throw the location in
28003 our lookup table. */
28005 static void
28006 dwarf2out_var_location (rtx_insn *loc_note)
28008 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
28009 struct var_loc_node *newloc;
28010 rtx_insn *next_real;
28011 rtx_insn *call_insn = NULL;
28012 static const char *last_label;
28013 static const char *last_postcall_label;
28014 static bool last_in_cold_section_p;
28015 static rtx_insn *expected_next_loc_note;
28016 tree decl;
28017 bool var_loc_p;
28018 var_loc_view view = 0;
28020 if (!NOTE_P (loc_note))
28022 if (CALL_P (loc_note))
28024 maybe_reset_location_view (loc_note, cur_line_info_table);
28025 call_site_count++;
28026 if (SIBLING_CALL_P (loc_note))
28027 tail_call_site_count++;
28028 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
28030 call_insn = loc_note;
28031 loc_note = NULL;
28032 var_loc_p = false;
28034 next_real = dwarf2out_next_real_insn (call_insn);
28035 cached_next_real_insn = NULL;
28036 goto create_label;
28038 if (optimize == 0 && !flag_var_tracking)
28040 /* When the var-tracking pass is not running, there is no note
28041 for indirect calls whose target is compile-time known. In this
28042 case, process such calls specifically so that we generate call
28043 sites for them anyway. */
28044 rtx x = PATTERN (loc_note);
28045 if (GET_CODE (x) == PARALLEL)
28046 x = XVECEXP (x, 0, 0);
28047 if (GET_CODE (x) == SET)
28048 x = SET_SRC (x);
28049 if (GET_CODE (x) == CALL)
28050 x = XEXP (x, 0);
28051 if (!MEM_P (x)
28052 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
28053 || !SYMBOL_REF_DECL (XEXP (x, 0))
28054 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
28055 != FUNCTION_DECL))
28057 call_insn = loc_note;
28058 loc_note = NULL;
28059 var_loc_p = false;
28061 next_real = dwarf2out_next_real_insn (call_insn);
28062 cached_next_real_insn = NULL;
28063 goto create_label;
28067 else if (!debug_variable_location_views)
28068 gcc_unreachable ();
28069 else
28070 maybe_reset_location_view (loc_note, cur_line_info_table);
28072 return;
28075 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
28076 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
28077 return;
28079 /* Optimize processing a large consecutive sequence of location
28080 notes so we don't spend too much time in next_real_insn. If the
28081 next insn is another location note, remember the next_real_insn
28082 calculation for next time. */
28083 next_real = cached_next_real_insn;
28084 if (next_real)
28086 if (expected_next_loc_note != loc_note)
28087 next_real = NULL;
28090 if (! next_real)
28091 next_real = dwarf2out_next_real_insn (loc_note);
28093 if (next_real)
28095 rtx_insn *next_note = NEXT_INSN (loc_note);
28096 while (next_note != next_real)
28098 if (! next_note->deleted ()
28099 && NOTE_P (next_note)
28100 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28101 break;
28102 next_note = NEXT_INSN (next_note);
28105 if (next_note == next_real)
28106 cached_next_real_insn = NULL;
28107 else
28109 expected_next_loc_note = next_note;
28110 cached_next_real_insn = next_real;
28113 else
28114 cached_next_real_insn = NULL;
28116 /* If there are no instructions which would be affected by this note,
28117 don't do anything. */
28118 if (var_loc_p
28119 && next_real == NULL_RTX
28120 && !NOTE_DURING_CALL_P (loc_note))
28121 return;
28123 create_label:
28125 if (next_real == NULL_RTX)
28126 next_real = get_last_insn ();
28128 /* If there were any real insns between note we processed last time
28129 and this note (or if it is the first note), clear
28130 last_{,postcall_}label so that they are not reused this time. */
28131 if (last_var_location_insn == NULL_RTX
28132 || last_var_location_insn != next_real
28133 || last_in_cold_section_p != in_cold_section_p)
28135 last_label = NULL;
28136 last_postcall_label = NULL;
28139 if (var_loc_p)
28141 const char *label
28142 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28143 view = cur_line_info_table->view;
28144 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28145 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28146 if (newloc == NULL)
28147 return;
28149 else
28151 decl = NULL_TREE;
28152 newloc = NULL;
28155 /* If there were no real insns between note we processed last time
28156 and this note, use the label we emitted last time. Otherwise
28157 create a new label and emit it. */
28158 if (last_label == NULL)
28160 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28161 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28162 loclabel_num++;
28163 last_label = ggc_strdup (loclabel);
28164 /* See if loclabel might be equal to .Ltext0. If yes,
28165 bump first_loclabel_num_not_at_text_label. */
28166 if (!have_multiple_function_sections
28167 && in_first_function_p
28168 && maybe_at_text_label_p)
28170 static rtx_insn *last_start;
28171 rtx_insn *insn;
28172 for (insn = loc_note; insn; insn = previous_insn (insn))
28173 if (insn == last_start)
28174 break;
28175 else if (!NONDEBUG_INSN_P (insn))
28176 continue;
28177 else
28179 rtx body = PATTERN (insn);
28180 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28181 continue;
28182 /* Inline asm could occupy zero bytes. */
28183 else if (GET_CODE (body) == ASM_INPUT
28184 || asm_noperands (body) >= 0)
28185 continue;
28186 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28187 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28188 continue;
28189 #endif
28190 else
28192 /* Assume insn has non-zero length. */
28193 maybe_at_text_label_p = false;
28194 break;
28197 if (maybe_at_text_label_p)
28199 last_start = loc_note;
28200 first_loclabel_num_not_at_text_label = loclabel_num;
28205 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28206 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28208 if (!var_loc_p)
28210 struct call_arg_loc_node *ca_loc
28211 = ggc_cleared_alloc<call_arg_loc_node> ();
28212 rtx_insn *prev = call_insn;
28214 ca_loc->call_arg_loc_note
28215 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
28216 ca_loc->next = NULL;
28217 ca_loc->label = last_label;
28218 gcc_assert (prev
28219 && (CALL_P (prev)
28220 || (NONJUMP_INSN_P (prev)
28221 && GET_CODE (PATTERN (prev)) == SEQUENCE
28222 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28223 if (!CALL_P (prev))
28224 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28225 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28227 /* Look for a SYMBOL_REF in the "prev" instruction. */
28228 rtx x = get_call_rtx_from (prev);
28229 if (x)
28231 /* Try to get the call symbol, if any. */
28232 if (MEM_P (XEXP (x, 0)))
28233 x = XEXP (x, 0);
28234 /* First, look for a memory access to a symbol_ref. */
28235 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28236 && SYMBOL_REF_DECL (XEXP (x, 0))
28237 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28238 ca_loc->symbol_ref = XEXP (x, 0);
28239 /* Otherwise, look at a compile-time known user-level function
28240 declaration. */
28241 else if (MEM_P (x)
28242 && MEM_EXPR (x)
28243 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28244 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28247 ca_loc->block = insn_scope (prev);
28248 if (call_arg_locations)
28249 call_arg_loc_last->next = ca_loc;
28250 else
28251 call_arg_locations = ca_loc;
28252 call_arg_loc_last = ca_loc;
28254 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28256 newloc->label = last_label;
28257 newloc->view = view;
28259 else
28261 if (!last_postcall_label)
28263 sprintf (loclabel, "%s-1", last_label);
28264 last_postcall_label = ggc_strdup (loclabel);
28266 newloc->label = last_postcall_label;
28267 /* ??? This view is at last_label, not last_label-1, but we
28268 could only assume view at last_label-1 is zero if we could
28269 assume calls always have length greater than one. This is
28270 probably true in general, though there might be a rare
28271 exception to this rule, e.g. if a call insn is optimized out
28272 by target magic. Then, even the -1 in the label will be
28273 wrong, which might invalidate the range. Anyway, using view,
28274 though technically possibly incorrect, will work as far as
28275 ranges go: since L-1 is in the middle of the call insn,
28276 (L-1).0 and (L-1).V shouldn't make any difference, and having
28277 the loclist entry refer to the .loc entry might be useful, so
28278 leave it like this. */
28279 newloc->view = view;
28282 if (var_loc_p && flag_debug_asm)
28284 const char *name, *sep, *patstr;
28285 if (decl && DECL_NAME (decl))
28286 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28287 else
28288 name = "";
28289 if (NOTE_VAR_LOCATION_LOC (loc_note))
28291 sep = " => ";
28292 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28294 else
28296 sep = " ";
28297 patstr = "RESET";
28299 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28300 name, sep, patstr);
28303 last_var_location_insn = next_real;
28304 last_in_cold_section_p = in_cold_section_p;
28307 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28308 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28309 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28310 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28311 BLOCK_FRAGMENT_ORIGIN links. */
28312 static bool
28313 block_within_block_p (tree block, tree outer, bool bothways)
28315 if (block == outer)
28316 return true;
28318 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28319 for (tree context = BLOCK_SUPERCONTEXT (block);
28320 context != outer;
28321 context = BLOCK_SUPERCONTEXT (context))
28322 if (!context || TREE_CODE (context) != BLOCK)
28323 return false;
28325 if (!bothways)
28326 return true;
28328 /* Now check that each block is actually referenced by its
28329 parent. */
28330 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28331 context = BLOCK_SUPERCONTEXT (context))
28333 if (BLOCK_FRAGMENT_ORIGIN (context))
28335 gcc_assert (!BLOCK_SUBBLOCKS (context));
28336 context = BLOCK_FRAGMENT_ORIGIN (context);
28338 for (tree sub = BLOCK_SUBBLOCKS (context);
28339 sub != block;
28340 sub = BLOCK_CHAIN (sub))
28341 if (!sub)
28342 return false;
28343 if (context == outer)
28344 return true;
28345 else
28346 block = context;
28350 /* Called during final while assembling the marker of the entry point
28351 for an inlined function. */
28353 static void
28354 dwarf2out_inline_entry (tree block)
28356 gcc_assert (debug_inline_points);
28358 /* If we can't represent it, don't bother. */
28359 if (!(dwarf_version >= 3 || !dwarf_strict))
28360 return;
28362 gcc_assert (DECL_P (block_ultimate_origin (block)));
28364 /* Sanity check the block tree. This would catch a case in which
28365 BLOCK got removed from the tree reachable from the outermost
28366 lexical block, but got retained in markers. It would still link
28367 back to its parents, but some ancestor would be missing a link
28368 down the path to the sub BLOCK. If the block got removed, its
28369 BLOCK_NUMBER will not be a usable value. */
28370 if (flag_checking)
28371 gcc_assert (block_within_block_p (block,
28372 DECL_INITIAL (current_function_decl),
28373 true));
28375 gcc_assert (inlined_function_outer_scope_p (block));
28376 gcc_assert (!lookup_block_die (block));
28378 if (BLOCK_FRAGMENT_ORIGIN (block))
28379 block = BLOCK_FRAGMENT_ORIGIN (block);
28380 /* Can the entry point ever not be at the beginning of an
28381 unfragmented lexical block? */
28382 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28383 || (cur_line_info_table
28384 && !ZERO_VIEW_P (cur_line_info_table->view))))
28385 return;
28387 if (!inline_entry_data_table)
28388 inline_entry_data_table
28389 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28392 inline_entry_data **iedp
28393 = inline_entry_data_table->find_slot_with_hash (block,
28394 htab_hash_pointer (block),
28395 INSERT);
28396 if (*iedp)
28397 /* ??? Ideally, we'd record all entry points for the same inlined
28398 function (some may have been duplicated by e.g. unrolling), but
28399 we have no way to represent that ATM. */
28400 return;
28402 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28403 ied->block = block;
28404 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28405 ied->label_num = BLOCK_NUMBER (block);
28406 if (cur_line_info_table)
28407 ied->view = cur_line_info_table->view;
28409 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28410 BLOCK_NUMBER (block));
28413 /* Called from finalize_size_functions for size functions so that their body
28414 can be encoded in the debug info to describe the layout of variable-length
28415 structures. */
28417 static void
28418 dwarf2out_size_function (tree decl)
28420 set_early_dwarf s;
28421 function_to_dwarf_procedure (decl);
28424 /* Note in one location list that text section has changed. */
28427 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28429 var_loc_list *list = *slot;
28430 if (list->first)
28431 list->last_before_switch
28432 = list->last->next ? list->last->next : list->last;
28433 return 1;
28436 /* Note in all location lists that text section has changed. */
28438 static void
28439 var_location_switch_text_section (void)
28441 if (decl_loc_table == NULL)
28442 return;
28444 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28447 /* Create a new line number table. */
28449 static dw_line_info_table *
28450 new_line_info_table (void)
28452 dw_line_info_table *table;
28454 table = ggc_cleared_alloc<dw_line_info_table> ();
28455 table->file_num = 1;
28456 table->line_num = 1;
28457 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28458 FORCE_RESET_NEXT_VIEW (table->view);
28459 table->symviews_since_reset = 0;
28461 return table;
28464 /* Lookup the "current" table into which we emit line info, so
28465 that we don't have to do it for every source line. */
28467 static void
28468 set_cur_line_info_table (section *sec)
28470 dw_line_info_table *table;
28472 if (sec == text_section)
28473 table = text_section_line_info;
28474 else if (sec == cold_text_section)
28476 table = cold_text_section_line_info;
28477 if (!table)
28479 cold_text_section_line_info = table = new_line_info_table ();
28480 table->end_label = cold_end_label;
28483 else
28485 const char *end_label;
28487 if (crtl->has_bb_partition)
28489 if (in_cold_section_p)
28490 end_label = crtl->subsections.cold_section_end_label;
28491 else
28492 end_label = crtl->subsections.hot_section_end_label;
28494 else
28496 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28497 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28498 current_function_funcdef_no);
28499 end_label = ggc_strdup (label);
28502 table = new_line_info_table ();
28503 table->end_label = end_label;
28505 vec_safe_push (separate_line_info, table);
28508 if (output_asm_line_debug_info ())
28509 table->is_stmt = (cur_line_info_table
28510 ? cur_line_info_table->is_stmt
28511 : DWARF_LINE_DEFAULT_IS_STMT_START);
28512 cur_line_info_table = table;
28516 /* We need to reset the locations at the beginning of each
28517 function. We can't do this in the end_function hook, because the
28518 declarations that use the locations won't have been output when
28519 that hook is called. Also compute have_multiple_function_sections here. */
28521 static void
28522 dwarf2out_begin_function (tree fun)
28524 section *sec = function_section (fun);
28526 if (sec != text_section)
28527 have_multiple_function_sections = true;
28529 if (crtl->has_bb_partition && !cold_text_section)
28531 gcc_assert (current_function_decl == fun);
28532 cold_text_section = unlikely_text_section ();
28533 switch_to_section (cold_text_section);
28534 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28535 switch_to_section (sec);
28538 call_site_count = 0;
28539 tail_call_site_count = 0;
28541 set_cur_line_info_table (sec);
28542 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28545 /* Helper function of dwarf2out_end_function, called only after emitting
28546 the very first function into assembly. Check if some .debug_loc range
28547 might end with a .LVL* label that could be equal to .Ltext0.
28548 In that case we must force using absolute addresses in .debug_loc ranges,
28549 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28550 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28551 list terminator.
28552 Set have_multiple_function_sections to true in that case and
28553 terminate htab traversal. */
28556 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28558 var_loc_list *entry = *slot;
28559 struct var_loc_node *node;
28561 node = entry->first;
28562 if (node && node->next && node->next->label)
28564 unsigned int i;
28565 const char *label = node->next->label;
28566 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28568 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28570 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28571 if (strcmp (label, loclabel) == 0)
28573 have_multiple_function_sections = true;
28574 return 0;
28578 return 1;
28581 /* Hook called after emitting a function into assembly.
28582 This does something only for the very first function emitted. */
28584 static void
28585 dwarf2out_end_function (unsigned int)
28587 if (in_first_function_p
28588 && !have_multiple_function_sections
28589 && first_loclabel_num_not_at_text_label
28590 && decl_loc_table)
28591 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28592 in_first_function_p = false;
28593 maybe_at_text_label_p = false;
28596 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28597 front-ends register a translation unit even before dwarf2out_init is
28598 called. */
28599 static tree main_translation_unit = NULL_TREE;
28601 /* Hook called by front-ends after they built their main translation unit.
28602 Associate comp_unit_die to UNIT. */
28604 static void
28605 dwarf2out_register_main_translation_unit (tree unit)
28607 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28608 && main_translation_unit == NULL_TREE);
28609 main_translation_unit = unit;
28610 /* If dwarf2out_init has not been called yet, it will perform the association
28611 itself looking at main_translation_unit. */
28612 if (decl_die_table != NULL)
28613 equate_decl_number_to_die (unit, comp_unit_die ());
28616 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28618 static void
28619 push_dw_line_info_entry (dw_line_info_table *table,
28620 enum dw_line_info_opcode opcode, unsigned int val)
28622 dw_line_info_entry e;
28623 e.opcode = opcode;
28624 e.val = val;
28625 vec_safe_push (table->entries, e);
28628 /* Output a label to mark the beginning of a source code line entry
28629 and record information relating to this source line, in
28630 'line_info_table' for later output of the .debug_line section. */
28631 /* ??? The discriminator parameter ought to be unsigned. */
28633 static void
28634 dwarf2out_source_line (unsigned int line, unsigned int column,
28635 const char *filename,
28636 int discriminator, bool is_stmt)
28638 unsigned int file_num;
28639 dw_line_info_table *table;
28640 static var_loc_view lvugid;
28642 /* 'line_info_table' information gathering is not needed when the debug
28643 info level is set to the lowest value. Also, the current DWARF-based
28644 debug formats do not use this info. */
28645 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28646 return;
28648 table = cur_line_info_table;
28650 if (line == 0)
28652 if (debug_variable_location_views
28653 && output_asm_line_debug_info ()
28654 && table && !RESETTING_VIEW_P (table->view))
28656 /* If we're using the assembler to compute view numbers, we
28657 can't issue a .loc directive for line zero, so we can't
28658 get a view number at this point. We might attempt to
28659 compute it from the previous view, or equate it to a
28660 subsequent view (though it might not be there!), but
28661 since we're omitting the line number entry, we might as
28662 well omit the view number as well. That means pretending
28663 it's a view number zero, which might very well turn out
28664 to be correct. ??? Extend the assembler so that the
28665 compiler could emit e.g. ".locview .LVU#", to output a
28666 view without changing line number information. We'd then
28667 have to count it in symviews_since_reset; when it's omitted,
28668 it doesn't count. */
28669 if (!zero_view_p)
28670 zero_view_p = BITMAP_GGC_ALLOC ();
28671 bitmap_set_bit (zero_view_p, table->view);
28672 if (flag_debug_asm)
28674 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28675 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28676 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28677 ASM_COMMENT_START);
28678 assemble_name (asm_out_file, label);
28679 putc ('\n', asm_out_file);
28681 table->view = ++lvugid;
28683 return;
28686 /* The discriminator column was added in dwarf4. Simplify the below
28687 by simply removing it if we're not supposed to output it. */
28688 if (dwarf_version < 4 && dwarf_strict)
28689 discriminator = 0;
28691 if (!debug_column_info)
28692 column = 0;
28694 file_num = maybe_emit_file (lookup_filename (filename));
28696 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28697 the debugger has used the second (possibly duplicate) line number
28698 at the beginning of the function to mark the end of the prologue.
28699 We could eliminate any other duplicates within the function. For
28700 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28701 that second line number entry. */
28702 /* Recall that this end-of-prologue indication is *not* the same thing
28703 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28704 to which the hook corresponds, follows the last insn that was
28705 emitted by gen_prologue. What we need is to precede the first insn
28706 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28707 insn that corresponds to something the user wrote. These may be
28708 very different locations once scheduling is enabled. */
28710 if (0 && file_num == table->file_num
28711 && line == table->line_num
28712 && column == table->column_num
28713 && discriminator == table->discrim_num
28714 && is_stmt == table->is_stmt)
28715 return;
28717 switch_to_section (current_function_section ());
28719 /* If requested, emit something human-readable. */
28720 if (flag_debug_asm)
28722 if (debug_column_info)
28723 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28724 filename, line, column);
28725 else
28726 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28727 filename, line);
28730 if (output_asm_line_debug_info ())
28732 /* Emit the .loc directive understood by GNU as. */
28733 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28734 file_num, line, is_stmt, discriminator */
28735 fputs ("\t.loc ", asm_out_file);
28736 fprint_ul (asm_out_file, file_num);
28737 putc (' ', asm_out_file);
28738 fprint_ul (asm_out_file, line);
28739 putc (' ', asm_out_file);
28740 fprint_ul (asm_out_file, column);
28742 if (is_stmt != table->is_stmt)
28744 #if HAVE_GAS_LOC_STMT
28745 fputs (" is_stmt ", asm_out_file);
28746 putc (is_stmt ? '1' : '0', asm_out_file);
28747 #endif
28749 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28751 gcc_assert (discriminator > 0);
28752 fputs (" discriminator ", asm_out_file);
28753 fprint_ul (asm_out_file, (unsigned long) discriminator);
28755 if (debug_variable_location_views)
28757 if (!RESETTING_VIEW_P (table->view))
28759 table->symviews_since_reset++;
28760 if (table->symviews_since_reset > symview_upper_bound)
28761 symview_upper_bound = table->symviews_since_reset;
28762 /* When we're using the assembler to compute view
28763 numbers, we output symbolic labels after "view" in
28764 .loc directives, and the assembler will set them for
28765 us, so that we can refer to the view numbers in
28766 location lists. The only exceptions are when we know
28767 a view will be zero: "-0" is a forced reset, used
28768 e.g. in the beginning of functions, whereas "0" tells
28769 the assembler to check that there was a PC change
28770 since the previous view, in a way that implicitly
28771 resets the next view. */
28772 fputs (" view ", asm_out_file);
28773 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28774 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28775 assemble_name (asm_out_file, label);
28776 table->view = ++lvugid;
28778 else
28780 table->symviews_since_reset = 0;
28781 if (FORCE_RESETTING_VIEW_P (table->view))
28782 fputs (" view -0", asm_out_file);
28783 else
28784 fputs (" view 0", asm_out_file);
28785 /* Mark the present view as a zero view. Earlier debug
28786 binds may have already added its id to loclists to be
28787 emitted later, so we can't reuse the id for something
28788 else. However, it's good to know whether a view is
28789 known to be zero, because then we may be able to
28790 optimize out locviews that are all zeros, so take
28791 note of it in zero_view_p. */
28792 if (!zero_view_p)
28793 zero_view_p = BITMAP_GGC_ALLOC ();
28794 bitmap_set_bit (zero_view_p, lvugid);
28795 table->view = ++lvugid;
28798 putc ('\n', asm_out_file);
28800 else
28802 unsigned int label_num = ++line_info_label_num;
28804 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28806 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28807 push_dw_line_info_entry (table, LI_adv_address, label_num);
28808 else
28809 push_dw_line_info_entry (table, LI_set_address, label_num);
28810 if (debug_variable_location_views)
28812 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28813 if (resetting)
28814 table->view = 0;
28816 if (flag_debug_asm)
28817 fprintf (asm_out_file, "\t%s view %s%d\n",
28818 ASM_COMMENT_START,
28819 resetting ? "-" : "",
28820 table->view);
28822 table->view++;
28824 if (file_num != table->file_num)
28825 push_dw_line_info_entry (table, LI_set_file, file_num);
28826 if (discriminator != table->discrim_num)
28827 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28828 if (is_stmt != table->is_stmt)
28829 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28830 push_dw_line_info_entry (table, LI_set_line, line);
28831 if (debug_column_info)
28832 push_dw_line_info_entry (table, LI_set_column, column);
28835 table->file_num = file_num;
28836 table->line_num = line;
28837 table->column_num = column;
28838 table->discrim_num = discriminator;
28839 table->is_stmt = is_stmt;
28840 table->in_use = true;
28843 /* Record a source file location for a DECL_IGNORED_P function. */
28845 static void
28846 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28847 const char *filename)
28849 dw_fde_ref fde = cfun->fde;
28851 fde->ignored_debug = false;
28852 set_cur_line_info_table (function_section (fde->decl));
28854 dwarf2out_source_line (line, column, filename, 0, true);
28857 /* Record the beginning of a new source file. */
28859 static void
28860 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28862 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28864 macinfo_entry e;
28865 e.code = DW_MACINFO_start_file;
28866 e.lineno = lineno;
28867 e.info = ggc_strdup (filename);
28868 vec_safe_push (macinfo_table, e);
28872 /* Record the end of a source file. */
28874 static void
28875 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28877 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28879 macinfo_entry e;
28880 e.code = DW_MACINFO_end_file;
28881 e.lineno = lineno;
28882 e.info = NULL;
28883 vec_safe_push (macinfo_table, e);
28887 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
28888 the tail part of the directive line, i.e. the part which is past the
28889 initial whitespace, #, whitespace, directive-name, whitespace part. */
28891 static void
28892 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28893 const char *buffer ATTRIBUTE_UNUSED)
28895 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28897 macinfo_entry e;
28898 /* Insert a dummy first entry to be able to optimize the whole
28899 predefined macro block using DW_MACRO_import. */
28900 if (macinfo_table->is_empty () && lineno <= 1)
28902 e.code = 0;
28903 e.lineno = 0;
28904 e.info = NULL;
28905 vec_safe_push (macinfo_table, e);
28907 e.code = DW_MACINFO_define;
28908 e.lineno = lineno;
28909 e.info = ggc_strdup (buffer);
28910 vec_safe_push (macinfo_table, e);
28914 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
28915 the tail part of the directive line, i.e. the part which is past the
28916 initial whitespace, #, whitespace, directive-name, whitespace part. */
28918 static void
28919 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28920 const char *buffer ATTRIBUTE_UNUSED)
28922 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28924 macinfo_entry e;
28925 /* Insert a dummy first entry to be able to optimize the whole
28926 predefined macro block using DW_MACRO_import. */
28927 if (macinfo_table->is_empty () && lineno <= 1)
28929 e.code = 0;
28930 e.lineno = 0;
28931 e.info = NULL;
28932 vec_safe_push (macinfo_table, e);
28934 e.code = DW_MACINFO_undef;
28935 e.lineno = lineno;
28936 e.info = ggc_strdup (buffer);
28937 vec_safe_push (macinfo_table, e);
28941 /* Helpers to manipulate hash table of CUs. */
28943 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28945 static inline hashval_t hash (const macinfo_entry *);
28946 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28949 inline hashval_t
28950 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28952 return htab_hash_string (entry->info);
28955 inline bool
28956 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28957 const macinfo_entry *entry2)
28959 return !strcmp (entry1->info, entry2->info);
28962 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28964 /* Output a single .debug_macinfo entry. */
28966 static void
28967 output_macinfo_op (macinfo_entry *ref)
28969 int file_num;
28970 size_t len;
28971 struct indirect_string_node *node;
28972 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28973 struct dwarf_file_data *fd;
28975 switch (ref->code)
28977 case DW_MACINFO_start_file:
28978 fd = lookup_filename (ref->info);
28979 file_num = maybe_emit_file (fd);
28980 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28981 dw2_asm_output_data_uleb128 (ref->lineno,
28982 "Included from line number %lu",
28983 (unsigned long) ref->lineno);
28984 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28985 break;
28986 case DW_MACINFO_end_file:
28987 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28988 break;
28989 case DW_MACINFO_define:
28990 case DW_MACINFO_undef:
28991 len = strlen (ref->info) + 1;
28992 if ((!dwarf_strict || dwarf_version >= 5)
28993 && len > (size_t) dwarf_offset_size
28994 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28995 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28997 if (dwarf_split_debug_info && dwarf_version >= 5)
28998 ref->code = ref->code == DW_MACINFO_define
28999 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
29000 else
29001 ref->code = ref->code == DW_MACINFO_define
29002 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
29003 output_macinfo_op (ref);
29004 return;
29006 dw2_asm_output_data (1, ref->code,
29007 ref->code == DW_MACINFO_define
29008 ? "Define macro" : "Undefine macro");
29009 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
29010 (unsigned long) ref->lineno);
29011 dw2_asm_output_nstring (ref->info, -1, "The macro");
29012 break;
29013 case DW_MACRO_define_strp:
29014 dw2_asm_output_data (1, ref->code, "Define macro strp");
29015 goto do_DW_MACRO_define_strpx;
29016 case DW_MACRO_undef_strp:
29017 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
29018 goto do_DW_MACRO_define_strpx;
29019 case DW_MACRO_define_strx:
29020 dw2_asm_output_data (1, ref->code, "Define macro strx");
29021 goto do_DW_MACRO_define_strpx;
29022 case DW_MACRO_undef_strx:
29023 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
29024 /* FALLTHRU */
29025 do_DW_MACRO_define_strpx:
29026 /* NB: dwarf2out_finish performs:
29027 1. save_macinfo_strings
29028 2. hash table traverse of index_string
29029 3. output_macinfo -> output_macinfo_op
29030 4. output_indirect_strings
29031 -> hash table traverse of output_index_string
29033 When output_macinfo_op is called, all index strings have been
29034 added to hash table by save_macinfo_strings and we can't pass
29035 INSERT to find_slot_with_hash which may expand hash table, even
29036 if no insertion is needed, and change hash table traverse order
29037 between index_string and output_index_string. */
29038 node = find_AT_string (ref->info, NO_INSERT);
29039 gcc_assert (node
29040 && (node->form == DW_FORM_strp
29041 || node->form == dwarf_FORM (DW_FORM_strx)));
29042 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
29043 (unsigned long) ref->lineno);
29044 if (node->form == DW_FORM_strp)
29045 dw2_asm_output_offset (dwarf_offset_size, node->label,
29046 debug_str_section, "The macro: \"%s\"",
29047 ref->info);
29048 else
29049 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
29050 ref->info);
29051 break;
29052 case DW_MACRO_import:
29053 dw2_asm_output_data (1, ref->code, "Import");
29054 ASM_GENERATE_INTERNAL_LABEL (label,
29055 DEBUG_MACRO_SECTION_LABEL,
29056 ref->lineno + macinfo_label_base);
29057 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
29058 break;
29059 default:
29060 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
29061 ASM_COMMENT_START, (unsigned long) ref->code);
29062 break;
29066 /* Attempt to make a sequence of define/undef macinfo ops shareable with
29067 other compilation unit .debug_macinfo sections. IDX is the first
29068 index of a define/undef, return the number of ops that should be
29069 emitted in a comdat .debug_macinfo section and emit
29070 a DW_MACRO_import entry referencing it.
29071 If the define/undef entry should be emitted normally, return 0. */
29073 static unsigned
29074 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
29075 macinfo_hash_type **macinfo_htab)
29077 macinfo_entry *first, *second, *cur, *inc;
29078 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
29079 unsigned char checksum[16];
29080 struct md5_ctx ctx;
29081 char *grp_name, *tail;
29082 const char *base;
29083 unsigned int i, count, encoded_filename_len, linebuf_len;
29084 macinfo_entry **slot;
29086 first = &(*macinfo_table)[idx];
29087 second = &(*macinfo_table)[idx + 1];
29089 /* Optimize only if there are at least two consecutive define/undef ops,
29090 and either all of them are before first DW_MACINFO_start_file
29091 with lineno {0,1} (i.e. predefined macro block), or all of them are
29092 in some included header file. */
29093 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29094 return 0;
29095 if (vec_safe_is_empty (files))
29097 if (first->lineno > 1 || second->lineno > 1)
29098 return 0;
29100 else if (first->lineno == 0)
29101 return 0;
29103 /* Find the last define/undef entry that can be grouped together
29104 with first and at the same time compute md5 checksum of their
29105 codes, linenumbers and strings. */
29106 md5_init_ctx (&ctx);
29107 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29108 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29109 break;
29110 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29111 break;
29112 else
29114 unsigned char code = cur->code;
29115 md5_process_bytes (&code, 1, &ctx);
29116 checksum_uleb128 (cur->lineno, &ctx);
29117 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29119 md5_finish_ctx (&ctx, checksum);
29120 count = i - idx;
29122 /* From the containing include filename (if any) pick up just
29123 usable characters from its basename. */
29124 if (vec_safe_is_empty (files))
29125 base = "";
29126 else
29127 base = lbasename (files->last ().info);
29128 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29129 if (ISIDNUM (base[i]) || base[i] == '.')
29130 encoded_filename_len++;
29131 /* Count . at the end. */
29132 if (encoded_filename_len)
29133 encoded_filename_len++;
29135 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29136 linebuf_len = strlen (linebuf);
29138 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29139 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29140 + 16 * 2 + 1);
29141 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29142 tail = grp_name + 4;
29143 if (encoded_filename_len)
29145 for (i = 0; base[i]; i++)
29146 if (ISIDNUM (base[i]) || base[i] == '.')
29147 *tail++ = base[i];
29148 *tail++ = '.';
29150 memcpy (tail, linebuf, linebuf_len);
29151 tail += linebuf_len;
29152 *tail++ = '.';
29153 for (i = 0; i < 16; i++)
29154 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29156 /* Construct a macinfo_entry for DW_MACRO_import
29157 in the empty vector entry before the first define/undef. */
29158 inc = &(*macinfo_table)[idx - 1];
29159 inc->code = DW_MACRO_import;
29160 inc->lineno = 0;
29161 inc->info = ggc_strdup (grp_name);
29162 if (!*macinfo_htab)
29163 *macinfo_htab = new macinfo_hash_type (10);
29164 /* Avoid emitting duplicates. */
29165 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29166 if (*slot != NULL)
29168 inc->code = 0;
29169 inc->info = NULL;
29170 /* If such an entry has been used before, just emit
29171 a DW_MACRO_import op. */
29172 inc = *slot;
29173 output_macinfo_op (inc);
29174 /* And clear all macinfo_entry in the range to avoid emitting them
29175 in the second pass. */
29176 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29178 cur->code = 0;
29179 cur->info = NULL;
29182 else
29184 *slot = inc;
29185 inc->lineno = (*macinfo_htab)->elements ();
29186 output_macinfo_op (inc);
29188 return count;
29191 /* Save any strings needed by the macinfo table in the debug str
29192 table. All strings must be collected into the table by the time
29193 index_string is called. */
29195 static void
29196 save_macinfo_strings (void)
29198 unsigned len;
29199 unsigned i;
29200 macinfo_entry *ref;
29202 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29204 switch (ref->code)
29206 /* Match the logic in output_macinfo_op to decide on
29207 indirect strings. */
29208 case DW_MACINFO_define:
29209 case DW_MACINFO_undef:
29210 len = strlen (ref->info) + 1;
29211 if ((!dwarf_strict || dwarf_version >= 5)
29212 && len > (unsigned) dwarf_offset_size
29213 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29214 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29215 set_indirect_string (find_AT_string (ref->info));
29216 break;
29217 case DW_MACINFO_start_file:
29218 /* -gsplit-dwarf -g3 will also output filename as indirect
29219 string. */
29220 if (!dwarf_split_debug_info)
29221 break;
29222 /* Fall through. */
29223 case DW_MACRO_define_strp:
29224 case DW_MACRO_undef_strp:
29225 case DW_MACRO_define_strx:
29226 case DW_MACRO_undef_strx:
29227 set_indirect_string (find_AT_string (ref->info));
29228 break;
29229 default:
29230 break;
29235 /* Output macinfo section(s). */
29237 static void
29238 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29240 unsigned i;
29241 unsigned long length = vec_safe_length (macinfo_table);
29242 macinfo_entry *ref;
29243 vec<macinfo_entry, va_gc> *files = NULL;
29244 macinfo_hash_type *macinfo_htab = NULL;
29245 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29247 if (! length)
29248 return;
29250 /* output_macinfo* uses these interchangeably. */
29251 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29252 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29253 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29254 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29256 /* AIX Assembler inserts the length, so adjust the reference to match the
29257 offset expected by debuggers. */
29258 strcpy (dl_section_ref, debug_line_label);
29259 if (XCOFF_DEBUGGING_INFO)
29260 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29262 /* For .debug_macro emit the section header. */
29263 if (!dwarf_strict || dwarf_version >= 5)
29265 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29266 "DWARF macro version number");
29267 if (dwarf_offset_size == 8)
29268 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29269 else
29270 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29271 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29272 debug_line_section, NULL);
29275 /* In the first loop, it emits the primary .debug_macinfo section
29276 and after each emitted op the macinfo_entry is cleared.
29277 If a longer range of define/undef ops can be optimized using
29278 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29279 the vector before the first define/undef in the range and the
29280 whole range of define/undef ops is not emitted and kept. */
29281 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29283 switch (ref->code)
29285 case DW_MACINFO_start_file:
29286 vec_safe_push (files, *ref);
29287 break;
29288 case DW_MACINFO_end_file:
29289 if (!vec_safe_is_empty (files))
29290 files->pop ();
29291 break;
29292 case DW_MACINFO_define:
29293 case DW_MACINFO_undef:
29294 if ((!dwarf_strict || dwarf_version >= 5)
29295 && !dwarf_split_debug_info
29296 && HAVE_COMDAT_GROUP
29297 && vec_safe_length (files) != 1
29298 && i > 0
29299 && i + 1 < length
29300 && (*macinfo_table)[i - 1].code == 0)
29302 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29303 if (count)
29305 i += count - 1;
29306 continue;
29309 break;
29310 case 0:
29311 /* A dummy entry may be inserted at the beginning to be able
29312 to optimize the whole block of predefined macros. */
29313 if (i == 0)
29314 continue;
29315 default:
29316 break;
29318 output_macinfo_op (ref);
29319 ref->info = NULL;
29320 ref->code = 0;
29323 if (!macinfo_htab)
29324 return;
29326 /* Save the number of transparent includes so we can adjust the
29327 label number for the fat LTO object DWARF. */
29328 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29330 delete macinfo_htab;
29331 macinfo_htab = NULL;
29333 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29334 terminate the current chain and switch to a new comdat .debug_macinfo
29335 section and emit the define/undef entries within it. */
29336 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29337 switch (ref->code)
29339 case 0:
29340 continue;
29341 case DW_MACRO_import:
29343 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29344 tree comdat_key = get_identifier (ref->info);
29345 /* Terminate the previous .debug_macinfo section. */
29346 dw2_asm_output_data (1, 0, "End compilation unit");
29347 targetm.asm_out.named_section (debug_macinfo_section_name,
29348 SECTION_DEBUG
29349 | SECTION_LINKONCE
29350 | (early_lto_debug
29351 ? SECTION_EXCLUDE : 0),
29352 comdat_key);
29353 ASM_GENERATE_INTERNAL_LABEL (label,
29354 DEBUG_MACRO_SECTION_LABEL,
29355 ref->lineno + macinfo_label_base);
29356 ASM_OUTPUT_LABEL (asm_out_file, label);
29357 ref->code = 0;
29358 ref->info = NULL;
29359 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29360 "DWARF macro version number");
29361 if (dwarf_offset_size == 8)
29362 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29363 else
29364 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29366 break;
29367 case DW_MACINFO_define:
29368 case DW_MACINFO_undef:
29369 output_macinfo_op (ref);
29370 ref->code = 0;
29371 ref->info = NULL;
29372 break;
29373 default:
29374 gcc_unreachable ();
29377 macinfo_label_base += macinfo_label_base_adj;
29380 /* As init_sections_and_labels may get called multiple times, have a
29381 generation count for labels. */
29382 static unsigned init_sections_and_labels_generation;
29384 /* Initialize the various sections and labels for dwarf output and prefix
29385 them with PREFIX if non-NULL. Returns the generation (zero based
29386 number of times function was called). */
29388 static unsigned
29389 init_sections_and_labels (bool early_lto_debug)
29391 if (early_lto_debug)
29393 if (!dwarf_split_debug_info)
29395 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29396 SECTION_DEBUG | SECTION_EXCLUDE,
29397 NULL);
29398 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29399 SECTION_DEBUG | SECTION_EXCLUDE,
29400 NULL);
29401 debug_macinfo_section_name
29402 = ((dwarf_strict && dwarf_version < 5)
29403 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29404 debug_macinfo_section = get_section (debug_macinfo_section_name,
29405 SECTION_DEBUG
29406 | SECTION_EXCLUDE, NULL);
29408 else
29410 /* ??? Which of the following do we need early? */
29411 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29412 SECTION_DEBUG | SECTION_EXCLUDE,
29413 NULL);
29414 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29415 SECTION_DEBUG | SECTION_EXCLUDE,
29416 NULL);
29417 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29418 SECTION_DEBUG
29419 | SECTION_EXCLUDE, NULL);
29420 debug_skeleton_abbrev_section
29421 = get_section (DEBUG_LTO_ABBREV_SECTION,
29422 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29423 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29424 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29425 init_sections_and_labels_generation);
29427 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29428 stay in the main .o, but the skeleton_line goes into the split
29429 off dwo. */
29430 debug_skeleton_line_section
29431 = get_section (DEBUG_LTO_LINE_SECTION,
29432 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29433 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29434 DEBUG_SKELETON_LINE_SECTION_LABEL,
29435 init_sections_and_labels_generation);
29436 debug_str_offsets_section
29437 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29438 SECTION_DEBUG | SECTION_EXCLUDE,
29439 NULL);
29440 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29441 DEBUG_SKELETON_INFO_SECTION_LABEL,
29442 init_sections_and_labels_generation);
29443 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29444 DEBUG_STR_DWO_SECTION_FLAGS,
29445 NULL);
29446 debug_macinfo_section_name
29447 = ((dwarf_strict && dwarf_version < 5)
29448 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29449 debug_macinfo_section = get_section (debug_macinfo_section_name,
29450 SECTION_DEBUG | SECTION_EXCLUDE,
29451 NULL);
29453 /* For macro info and the file table we have to refer to a
29454 debug_line section. */
29455 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29456 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29457 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29458 DEBUG_LINE_SECTION_LABEL,
29459 init_sections_and_labels_generation);
29461 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29462 DEBUG_STR_SECTION_FLAGS
29463 | SECTION_EXCLUDE, NULL);
29464 if (!dwarf_split_debug_info)
29465 debug_line_str_section
29466 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29467 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29469 else
29471 if (!dwarf_split_debug_info)
29473 debug_info_section = get_section (DEBUG_INFO_SECTION,
29474 SECTION_DEBUG, NULL);
29475 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29476 SECTION_DEBUG, NULL);
29477 debug_loc_section = get_section (dwarf_version >= 5
29478 ? DEBUG_LOCLISTS_SECTION
29479 : DEBUG_LOC_SECTION,
29480 SECTION_DEBUG, NULL);
29481 debug_macinfo_section_name
29482 = ((dwarf_strict && dwarf_version < 5)
29483 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29484 debug_macinfo_section = get_section (debug_macinfo_section_name,
29485 SECTION_DEBUG, NULL);
29487 else
29489 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29490 SECTION_DEBUG | SECTION_EXCLUDE,
29491 NULL);
29492 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29493 SECTION_DEBUG | SECTION_EXCLUDE,
29494 NULL);
29495 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29496 SECTION_DEBUG, NULL);
29497 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29498 SECTION_DEBUG, NULL);
29499 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29500 SECTION_DEBUG, NULL);
29501 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29502 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29503 init_sections_and_labels_generation);
29505 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29506 stay in the main .o, but the skeleton_line goes into the
29507 split off dwo. */
29508 debug_skeleton_line_section
29509 = get_section (DEBUG_DWO_LINE_SECTION,
29510 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29511 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29512 DEBUG_SKELETON_LINE_SECTION_LABEL,
29513 init_sections_and_labels_generation);
29514 debug_str_offsets_section
29515 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29516 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29517 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29518 DEBUG_SKELETON_INFO_SECTION_LABEL,
29519 init_sections_and_labels_generation);
29520 debug_loc_section = get_section (dwarf_version >= 5
29521 ? DEBUG_DWO_LOCLISTS_SECTION
29522 : DEBUG_DWO_LOC_SECTION,
29523 SECTION_DEBUG | SECTION_EXCLUDE,
29524 NULL);
29525 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29526 DEBUG_STR_DWO_SECTION_FLAGS,
29527 NULL);
29528 debug_macinfo_section_name
29529 = ((dwarf_strict && dwarf_version < 5)
29530 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29531 debug_macinfo_section = get_section (debug_macinfo_section_name,
29532 SECTION_DEBUG | SECTION_EXCLUDE,
29533 NULL);
29534 if (dwarf_version >= 5)
29535 debug_ranges_dwo_section
29536 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29537 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29539 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29540 SECTION_DEBUG, NULL);
29541 debug_line_section = get_section (DEBUG_LINE_SECTION,
29542 SECTION_DEBUG, NULL);
29543 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29544 SECTION_DEBUG, NULL);
29545 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29546 SECTION_DEBUG, NULL);
29547 debug_str_section = get_section (DEBUG_STR_SECTION,
29548 DEBUG_STR_SECTION_FLAGS, NULL);
29549 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29550 || asm_outputs_debug_line_str ())
29551 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29552 DEBUG_STR_SECTION_FLAGS, NULL);
29554 debug_ranges_section = get_section (dwarf_version >= 5
29555 ? DEBUG_RNGLISTS_SECTION
29556 : DEBUG_RANGES_SECTION,
29557 SECTION_DEBUG, NULL);
29558 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29559 SECTION_DEBUG, NULL);
29562 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29563 DEBUG_ABBREV_SECTION_LABEL,
29564 init_sections_and_labels_generation);
29565 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29566 DEBUG_INFO_SECTION_LABEL,
29567 init_sections_and_labels_generation);
29568 info_section_emitted = false;
29569 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29570 DEBUG_LINE_SECTION_LABEL,
29571 init_sections_and_labels_generation);
29572 /* There are up to 6 unique ranges labels per generation.
29573 See also output_rnglists. */
29574 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29575 DEBUG_RANGES_SECTION_LABEL,
29576 init_sections_and_labels_generation * 6);
29577 if (dwarf_version >= 5 && dwarf_split_debug_info)
29578 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29579 DEBUG_RANGES_SECTION_LABEL,
29580 1 + init_sections_and_labels_generation * 6);
29581 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29582 DEBUG_ADDR_SECTION_LABEL,
29583 init_sections_and_labels_generation);
29584 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29585 (dwarf_strict && dwarf_version < 5)
29586 ? DEBUG_MACINFO_SECTION_LABEL
29587 : DEBUG_MACRO_SECTION_LABEL,
29588 init_sections_and_labels_generation);
29589 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29590 init_sections_and_labels_generation);
29592 ++init_sections_and_labels_generation;
29593 return init_sections_and_labels_generation - 1;
29596 /* Set up for Dwarf output at the start of compilation. */
29598 static void
29599 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29601 /* Allocate the file_table. */
29602 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29604 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29605 /* Allocate the decl_die_table. */
29606 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29608 /* Allocate the decl_loc_table. */
29609 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29611 /* Allocate the cached_dw_loc_list_table. */
29612 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29614 /* Allocate the initial hunk of the abbrev_die_table. */
29615 vec_alloc (abbrev_die_table, 256);
29616 /* Zero-th entry is allocated, but unused. */
29617 abbrev_die_table->quick_push (NULL);
29619 /* Allocate the dwarf_proc_stack_usage_map. */
29620 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29622 /* Allocate the pubtypes and pubnames vectors. */
29623 vec_alloc (pubname_table, 32);
29624 vec_alloc (pubtype_table, 32);
29626 vec_alloc (incomplete_types, 64);
29628 vec_alloc (used_rtx_array, 32);
29630 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29631 vec_alloc (macinfo_table, 64);
29632 #endif
29634 /* If front-ends already registered a main translation unit but we were not
29635 ready to perform the association, do this now. */
29636 if (main_translation_unit != NULL_TREE)
29637 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29640 /* Called before compile () starts outputtting functions, variables
29641 and toplevel asms into assembly. */
29643 static void
29644 dwarf2out_assembly_start (void)
29646 if (text_section_line_info)
29647 return;
29649 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29650 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29651 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29652 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29653 COLD_TEXT_SECTION_LABEL, 0);
29654 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29656 switch_to_section (text_section);
29657 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29658 #endif
29660 /* Make sure the line number table for .text always exists. */
29661 text_section_line_info = new_line_info_table ();
29662 text_section_line_info->end_label = text_end_label;
29664 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29665 cur_line_info_table = text_section_line_info;
29666 #endif
29668 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29669 && dwarf2out_do_cfi_asm ()
29670 && !dwarf2out_do_eh_frame ())
29671 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29673 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29674 if (output_asm_line_debug_info () && dwarf_version >= 5)
29676 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29677 tell it the comp_dir and main file name for the zero entry
29678 line table. */
29679 const char *comp_dir, *filename0;
29681 comp_dir = comp_dir_string ();
29682 if (comp_dir == NULL)
29683 comp_dir = "";
29685 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29686 if (filename0 == NULL)
29687 filename0 = "";
29689 fprintf (asm_out_file, "\t.file 0 ");
29690 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29691 fputc (' ', asm_out_file);
29692 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29693 fputc ('\n', asm_out_file);
29695 else
29696 #endif
29697 /* Work around for PR101575: output a dummy .file directive. */
29698 if (!last_emitted_file && dwarf_debuginfo_p ()
29699 && debug_info_level >= DINFO_LEVEL_TERSE)
29701 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29703 if (filename0 == NULL)
29704 filename0 = "<dummy>";
29705 maybe_emit_file (lookup_filename (filename0));
29709 /* A helper function for dwarf2out_finish called through
29710 htab_traverse. Assign a string its index. All strings must be
29711 collected into the table by the time index_string is called,
29712 because the indexing code relies on htab_traverse to traverse nodes
29713 in the same order for each run. */
29716 index_string (indirect_string_node **h, unsigned int *index)
29718 indirect_string_node *node = *h;
29720 find_string_form (node);
29721 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29723 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29724 node->index = *index;
29725 *index += 1;
29727 return 1;
29730 /* A helper function for output_indirect_strings called through
29731 htab_traverse. Output the offset to a string and update the
29732 current offset. */
29735 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29737 indirect_string_node *node = *h;
29739 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29741 /* Assert that this node has been assigned an index. */
29742 gcc_assert (node->index != NO_INDEX_ASSIGNED
29743 && node->index != NOT_INDEXED);
29744 dw2_asm_output_data (dwarf_offset_size, *offset,
29745 "indexed string 0x%x: %s", node->index, node->str);
29746 *offset += strlen (node->str) + 1;
29748 return 1;
29751 /* A helper function for dwarf2out_finish called through
29752 htab_traverse. Output the indexed string. */
29755 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29757 struct indirect_string_node *node = *h;
29759 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29761 /* Assert that the strings are output in the same order as their
29762 indexes were assigned. */
29763 gcc_assert (*cur_idx == node->index);
29764 assemble_string (node->str, strlen (node->str) + 1);
29765 *cur_idx += 1;
29767 return 1;
29770 /* A helper function for output_indirect_strings. Counts the number
29771 of index strings offsets. Must match the logic of the functions
29772 output_index_string[_offsets] above. */
29774 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29776 struct indirect_string_node *node = *h;
29778 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29779 *last_idx += 1;
29780 return 1;
29783 /* A helper function for dwarf2out_finish called through
29784 htab_traverse. Emit one queued .debug_str string. */
29787 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29789 struct indirect_string_node *node = *h;
29791 node->form = find_string_form (node);
29792 if (node->form == form && node->refcount > 0)
29794 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29795 assemble_string (node->str, strlen (node->str) + 1);
29798 return 1;
29801 /* Output the indexed string table. */
29803 static void
29804 output_indirect_strings (void)
29806 switch_to_section (debug_str_section);
29807 if (!dwarf_split_debug_info)
29808 debug_str_hash->traverse<enum dwarf_form,
29809 output_indirect_string> (DW_FORM_strp);
29810 else
29812 unsigned int offset = 0;
29813 unsigned int cur_idx = 0;
29815 if (skeleton_debug_str_hash)
29816 skeleton_debug_str_hash->traverse<enum dwarf_form,
29817 output_indirect_string> (DW_FORM_strp);
29819 switch_to_section (debug_str_offsets_section);
29820 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29821 header. Note that we don't need to generate a label to the
29822 actual index table following the header here, because this is
29823 for the split dwarf case only. In an .dwo file there is only
29824 one string offsets table (and one debug info section). But
29825 if we would start using string offset tables for the main (or
29826 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29827 pointing to the actual index after the header. Split dwarf
29828 units will never have a string offsets base attribute. When
29829 a split unit is moved into a .dwp file the string offsets can
29830 be found through the .debug_cu_index section table. */
29831 if (dwarf_version >= 5)
29833 unsigned int last_idx = 0;
29834 unsigned long str_offsets_length;
29836 debug_str_hash->traverse_noresize
29837 <unsigned int *, count_index_strings> (&last_idx);
29838 str_offsets_length = last_idx * dwarf_offset_size + 4;
29839 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29840 dw2_asm_output_data (4, 0xffffffff,
29841 "Escape value for 64-bit DWARF extension");
29842 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29843 "Length of string offsets unit");
29844 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29845 dw2_asm_output_data (2, 0, "Header zero padding");
29847 debug_str_hash->traverse_noresize
29848 <unsigned int *, output_index_string_offset> (&offset);
29849 switch_to_section (debug_str_dwo_section);
29850 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29851 (&cur_idx);
29855 /* Callback for htab_traverse to assign an index to an entry in the
29856 table, and to write that entry to the .debug_addr section. */
29859 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29861 addr_table_entry *entry = *slot;
29863 if (entry->refcount == 0)
29865 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29866 || entry->index == NOT_INDEXED);
29867 return 1;
29870 gcc_assert (entry->index == *cur_index);
29871 (*cur_index)++;
29873 switch (entry->kind)
29875 case ate_kind_rtx:
29876 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29877 "0x%x", entry->index);
29878 break;
29879 case ate_kind_rtx_dtprel:
29880 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29881 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29882 DWARF2_ADDR_SIZE,
29883 entry->addr.rtl);
29884 fputc ('\n', asm_out_file);
29885 break;
29886 case ate_kind_label:
29887 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29888 "0x%x", entry->index);
29889 break;
29890 default:
29891 gcc_unreachable ();
29893 return 1;
29896 /* A helper function for dwarf2out_finish. Counts the number
29897 of indexed addresses. Must match the logic of the functions
29898 output_addr_table_entry above. */
29900 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29902 addr_table_entry *entry = *slot;
29904 if (entry->refcount > 0)
29905 *last_idx += 1;
29906 return 1;
29909 /* Produce the .debug_addr section. */
29911 static void
29912 output_addr_table (void)
29914 unsigned int index = 0;
29915 if (addr_index_table == NULL || addr_index_table->size () == 0)
29916 return;
29918 switch_to_section (debug_addr_section);
29919 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29920 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29921 before DWARF5, didn't have a header for .debug_addr units.
29922 DWARF5 specifies a small header when address tables are used. */
29923 if (dwarf_version >= 5)
29925 unsigned int last_idx = 0;
29926 unsigned long addrs_length;
29928 addr_index_table->traverse_noresize
29929 <unsigned int *, count_index_addrs> (&last_idx);
29930 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29932 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29933 dw2_asm_output_data (4, 0xffffffff,
29934 "Escape value for 64-bit DWARF extension");
29935 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29936 "Length of Address Unit");
29937 dw2_asm_output_data (2, 5, "DWARF addr version");
29938 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29939 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29941 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29943 addr_index_table
29944 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29947 #if ENABLE_ASSERT_CHECKING
29948 /* Verify that all marks are clear. */
29950 static void
29951 verify_marks_clear (dw_die_ref die)
29953 dw_die_ref c;
29955 gcc_assert (! die->die_mark);
29956 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29958 #endif /* ENABLE_ASSERT_CHECKING */
29960 /* Clear the marks for a die and its children.
29961 Be cool if the mark isn't set. */
29963 static void
29964 prune_unmark_dies (dw_die_ref die)
29966 dw_die_ref c;
29968 if (die->die_mark)
29969 die->die_mark = 0;
29970 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29973 /* Given LOC that is referenced by a DIE we're marking as used, find all
29974 referenced DWARF procedures it references and mark them as used. */
29976 static void
29977 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29979 for (; loc != NULL; loc = loc->dw_loc_next)
29980 switch (loc->dw_loc_opc)
29982 case DW_OP_implicit_pointer:
29983 case DW_OP_convert:
29984 case DW_OP_reinterpret:
29985 case DW_OP_GNU_implicit_pointer:
29986 case DW_OP_GNU_convert:
29987 case DW_OP_GNU_reinterpret:
29988 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29989 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29990 break;
29991 case DW_OP_GNU_variable_value:
29992 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29994 dw_die_ref ref
29995 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29996 if (ref == NULL)
29997 break;
29998 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29999 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30000 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30002 /* FALLTHRU */
30003 case DW_OP_call2:
30004 case DW_OP_call4:
30005 case DW_OP_call_ref:
30006 case DW_OP_const_type:
30007 case DW_OP_GNU_const_type:
30008 case DW_OP_GNU_parameter_ref:
30009 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
30010 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
30011 break;
30012 case DW_OP_regval_type:
30013 case DW_OP_deref_type:
30014 case DW_OP_GNU_regval_type:
30015 case DW_OP_GNU_deref_type:
30016 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
30017 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
30018 break;
30019 case DW_OP_entry_value:
30020 case DW_OP_GNU_entry_value:
30021 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
30022 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
30023 break;
30024 default:
30025 break;
30029 /* Given DIE that we're marking as used, find any other dies
30030 it references as attributes and mark them as used. */
30032 static void
30033 prune_unused_types_walk_attribs (dw_die_ref die)
30035 dw_attr_node *a;
30036 unsigned ix;
30038 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30040 switch (AT_class (a))
30042 /* Make sure DWARF procedures referenced by location descriptions will
30043 get emitted. */
30044 case dw_val_class_loc:
30045 prune_unused_types_walk_loc_descr (AT_loc (a));
30046 break;
30047 case dw_val_class_loc_list:
30048 for (dw_loc_list_ref list = AT_loc_list (a);
30049 list != NULL;
30050 list = list->dw_loc_next)
30051 prune_unused_types_walk_loc_descr (list->expr);
30052 break;
30054 case dw_val_class_view_list:
30055 /* This points to a loc_list in another attribute, so it's
30056 already covered. */
30057 break;
30059 case dw_val_class_die_ref:
30060 /* A reference to another DIE.
30061 Make sure that it will get emitted.
30062 If it was broken out into a comdat group, don't follow it. */
30063 if (! AT_ref (a)->comdat_type_p
30064 || a->dw_attr == DW_AT_specification)
30065 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
30066 break;
30068 case dw_val_class_str:
30069 /* Set the string's refcount to 0 so that prune_unused_types_mark
30070 accounts properly for it. */
30071 a->dw_attr_val.v.val_str->refcount = 0;
30072 break;
30074 default:
30075 break;
30080 /* Mark the generic parameters and arguments children DIEs of DIE. */
30082 static void
30083 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
30085 dw_die_ref c;
30087 if (die == NULL || die->die_child == NULL)
30088 return;
30089 c = die->die_child;
30092 if (is_template_parameter (c))
30093 prune_unused_types_mark (c, 1);
30094 c = c->die_sib;
30095 } while (c && c != die->die_child);
30098 /* Mark DIE as being used. If DOKIDS is true, then walk down
30099 to DIE's children. */
30101 static void
30102 prune_unused_types_mark (dw_die_ref die, int dokids)
30104 dw_die_ref c;
30106 if (die->die_mark == 0)
30108 /* We haven't done this node yet. Mark it as used. */
30109 die->die_mark = 1;
30110 /* If this is the DIE of a generic type instantiation,
30111 mark the children DIEs that describe its generic parms and
30112 args. */
30113 prune_unused_types_mark_generic_parms_dies (die);
30115 /* We also have to mark its parents as used.
30116 (But we don't want to mark our parent's kids due to this,
30117 unless it is a class.) */
30118 if (die->die_parent)
30119 prune_unused_types_mark (die->die_parent,
30120 class_scope_p (die->die_parent));
30122 /* Mark any referenced nodes. */
30123 prune_unused_types_walk_attribs (die);
30125 /* If this node is a specification,
30126 also mark the definition, if it exists. */
30127 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30128 prune_unused_types_mark (die->die_definition, 1);
30131 if (dokids && die->die_mark != 2)
30133 /* We need to walk the children, but haven't done so yet.
30134 Remember that we've walked the kids. */
30135 die->die_mark = 2;
30137 /* If this is an array type, we need to make sure our
30138 kids get marked, even if they're types. If we're
30139 breaking out types into comdat sections, do this
30140 for all type definitions. */
30141 if (die->die_tag == DW_TAG_array_type
30142 || (use_debug_types
30143 && is_type_die (die) && ! is_declaration_die (die)))
30144 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30145 else
30146 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30150 /* For local classes, look if any static member functions were emitted
30151 and if so, mark them. */
30153 static void
30154 prune_unused_types_walk_local_classes (dw_die_ref die)
30156 dw_die_ref c;
30158 if (die->die_mark == 2)
30159 return;
30161 switch (die->die_tag)
30163 case DW_TAG_structure_type:
30164 case DW_TAG_union_type:
30165 case DW_TAG_class_type:
30166 case DW_TAG_interface_type:
30167 break;
30169 case DW_TAG_subprogram:
30170 if (!get_AT_flag (die, DW_AT_declaration)
30171 || die->die_definition != NULL)
30172 prune_unused_types_mark (die, 1);
30173 return;
30175 default:
30176 return;
30179 /* Mark children. */
30180 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30183 /* Walk the tree DIE and mark types that we actually use. */
30185 static void
30186 prune_unused_types_walk (dw_die_ref die)
30188 dw_die_ref c;
30190 /* Don't do anything if this node is already marked and
30191 children have been marked as well. */
30192 if (die->die_mark == 2)
30193 return;
30195 switch (die->die_tag)
30197 case DW_TAG_structure_type:
30198 case DW_TAG_union_type:
30199 case DW_TAG_class_type:
30200 case DW_TAG_interface_type:
30201 if (die->die_perennial_p)
30202 break;
30204 for (c = die->die_parent; c; c = c->die_parent)
30205 if (c->die_tag == DW_TAG_subprogram)
30206 break;
30208 /* Finding used static member functions inside of classes
30209 is needed just for local classes, because for other classes
30210 static member function DIEs with DW_AT_specification
30211 are emitted outside of the DW_TAG_*_type. If we ever change
30212 it, we'd need to call this even for non-local classes. */
30213 if (c)
30214 prune_unused_types_walk_local_classes (die);
30216 /* It's a type node --- don't mark it. */
30217 return;
30219 case DW_TAG_const_type:
30220 case DW_TAG_packed_type:
30221 case DW_TAG_pointer_type:
30222 case DW_TAG_reference_type:
30223 case DW_TAG_rvalue_reference_type:
30224 case DW_TAG_volatile_type:
30225 case DW_TAG_restrict_type:
30226 case DW_TAG_shared_type:
30227 case DW_TAG_atomic_type:
30228 case DW_TAG_immutable_type:
30229 case DW_TAG_typedef:
30230 case DW_TAG_array_type:
30231 case DW_TAG_coarray_type:
30232 case DW_TAG_friend:
30233 case DW_TAG_enumeration_type:
30234 case DW_TAG_subroutine_type:
30235 case DW_TAG_string_type:
30236 case DW_TAG_set_type:
30237 case DW_TAG_subrange_type:
30238 case DW_TAG_ptr_to_member_type:
30239 case DW_TAG_file_type:
30240 case DW_TAG_unspecified_type:
30241 case DW_TAG_dynamic_type:
30242 /* Type nodes are useful only when other DIEs reference them --- don't
30243 mark them. */
30244 /* FALLTHROUGH */
30246 case DW_TAG_dwarf_procedure:
30247 /* Likewise for DWARF procedures. */
30249 if (die->die_perennial_p)
30250 break;
30252 return;
30254 case DW_TAG_variable:
30255 if (flag_debug_only_used_symbols)
30257 if (die->die_perennial_p)
30258 break;
30260 /* For static data members, the declaration in the class is supposed
30261 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30262 DWARF5. DW_TAG_member will be marked, so mark even such
30263 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30264 attribute. */
30265 if (dwarf_version >= 5
30266 && class_scope_p (die->die_parent)
30267 && get_AT (die, DW_AT_const_value))
30268 break;
30270 /* premark_used_variables marks external variables --- don't mark
30271 them here. But function-local externals are always considered
30272 used. */
30273 if (get_AT (die, DW_AT_external))
30275 for (c = die->die_parent; c; c = c->die_parent)
30276 if (c->die_tag == DW_TAG_subprogram)
30277 break;
30278 if (!c)
30279 return;
30282 /* FALLTHROUGH */
30284 default:
30285 /* Mark everything else. */
30286 break;
30289 if (die->die_mark == 0)
30291 die->die_mark = 1;
30293 /* Now, mark any dies referenced from here. */
30294 prune_unused_types_walk_attribs (die);
30297 die->die_mark = 2;
30299 /* Mark children. */
30300 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30303 /* Increment the string counts on strings referred to from DIE's
30304 attributes. */
30306 static void
30307 prune_unused_types_update_strings (dw_die_ref die)
30309 dw_attr_node *a;
30310 unsigned ix;
30312 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30313 if (AT_class (a) == dw_val_class_str)
30315 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30316 s->refcount++;
30317 /* Avoid unnecessarily putting strings that are used less than
30318 twice in the hash table. */
30319 if (s->form != DW_FORM_line_strp
30320 && (s->refcount
30321 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30323 indirect_string_node **slot
30324 = debug_str_hash->find_slot_with_hash (s->str,
30325 htab_hash_string (s->str),
30326 INSERT);
30327 gcc_assert (*slot == NULL);
30328 *slot = s;
30333 /* Mark DIE and its children as removed. */
30335 static void
30336 mark_removed (dw_die_ref die)
30338 dw_die_ref c;
30339 die->removed = true;
30340 FOR_EACH_CHILD (die, c, mark_removed (c));
30343 /* Remove from the tree DIE any dies that aren't marked. */
30345 static void
30346 prune_unused_types_prune (dw_die_ref die)
30348 dw_die_ref c;
30350 gcc_assert (die->die_mark);
30351 prune_unused_types_update_strings (die);
30353 if (! die->die_child)
30354 return;
30356 c = die->die_child;
30357 do {
30358 dw_die_ref prev = c, next;
30359 for (c = c->die_sib; ! c->die_mark; c = next)
30360 if (c == die->die_child)
30362 /* No marked children between 'prev' and the end of the list. */
30363 if (prev == c)
30364 /* No marked children at all. */
30365 die->die_child = NULL;
30366 else
30368 prev->die_sib = c->die_sib;
30369 die->die_child = prev;
30371 c->die_sib = NULL;
30372 mark_removed (c);
30373 return;
30375 else
30377 next = c->die_sib;
30378 c->die_sib = NULL;
30379 mark_removed (c);
30382 if (c != prev->die_sib)
30383 prev->die_sib = c;
30384 prune_unused_types_prune (c);
30385 } while (c != die->die_child);
30388 /* Remove dies representing declarations that we never use. */
30390 static void
30391 prune_unused_types (void)
30393 unsigned int i;
30394 limbo_die_node *node;
30395 comdat_type_node *ctnode;
30396 pubname_entry *pub;
30397 dw_die_ref base_type;
30399 #if ENABLE_ASSERT_CHECKING
30400 /* All the marks should already be clear. */
30401 verify_marks_clear (comp_unit_die ());
30402 for (node = limbo_die_list; node; node = node->next)
30403 verify_marks_clear (node->die);
30404 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30405 verify_marks_clear (ctnode->root_die);
30406 #endif /* ENABLE_ASSERT_CHECKING */
30408 /* Mark types that are used in global variables. */
30409 premark_types_used_by_global_vars ();
30411 /* Mark variables used in the symtab. */
30412 if (flag_debug_only_used_symbols)
30413 premark_used_variables ();
30415 /* Set the mark on nodes that are actually used. */
30416 prune_unused_types_walk (comp_unit_die ());
30417 for (node = limbo_die_list; node; node = node->next)
30418 prune_unused_types_walk (node->die);
30419 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30421 prune_unused_types_walk (ctnode->root_die);
30422 prune_unused_types_mark (ctnode->type_die, 1);
30425 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30426 are unusual in that they are pubnames that are the children of pubtypes.
30427 They should only be marked via their parent DW_TAG_enumeration_type die,
30428 not as roots in themselves. */
30429 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30430 if (pub->die->die_tag != DW_TAG_enumerator)
30431 prune_unused_types_mark (pub->die, 1);
30432 for (i = 0; base_types.iterate (i, &base_type); i++)
30433 prune_unused_types_mark (base_type, 1);
30435 /* Also set the mark on nodes that could be referenced by
30436 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30437 by DW_TAG_inlined_subroutine origins. */
30438 cgraph_node *cnode;
30439 FOR_EACH_FUNCTION (cnode)
30440 if (cnode->referred_to_p (false))
30442 dw_die_ref die = lookup_decl_die (cnode->decl);
30443 if (die == NULL || die->die_mark)
30444 continue;
30445 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30446 if (e->caller != cnode)
30448 prune_unused_types_mark (die, 1);
30449 break;
30453 if (debug_str_hash)
30454 debug_str_hash->empty ();
30455 if (skeleton_debug_str_hash)
30456 skeleton_debug_str_hash->empty ();
30457 prune_unused_types_prune (comp_unit_die ());
30458 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30460 node = *pnode;
30461 if (!node->die->die_mark)
30462 *pnode = node->next;
30463 else
30465 prune_unused_types_prune (node->die);
30466 pnode = &node->next;
30469 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30470 prune_unused_types_prune (ctnode->root_die);
30472 /* Leave the marks clear. */
30473 prune_unmark_dies (comp_unit_die ());
30474 for (node = limbo_die_list; node; node = node->next)
30475 prune_unmark_dies (node->die);
30476 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30477 prune_unmark_dies (ctnode->root_die);
30480 /* Helpers to manipulate hash table of comdat type units. */
30482 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30484 static inline hashval_t hash (const comdat_type_node *);
30485 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30488 inline hashval_t
30489 comdat_type_hasher::hash (const comdat_type_node *type_node)
30491 hashval_t h;
30492 memcpy (&h, type_node->signature, sizeof (h));
30493 return h;
30496 inline bool
30497 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30498 const comdat_type_node *type_node_2)
30500 return (! memcmp (type_node_1->signature, type_node_2->signature,
30501 DWARF_TYPE_SIGNATURE_SIZE));
30504 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30505 to the location it would have been added, should we know its
30506 DECL_ASSEMBLER_NAME when we added other attributes. This will
30507 probably improve compactness of debug info, removing equivalent
30508 abbrevs, and hide any differences caused by deferring the
30509 computation of the assembler name, triggered by e.g. PCH. */
30511 static inline void
30512 move_linkage_attr (dw_die_ref die)
30514 unsigned ix = vec_safe_length (die->die_attr);
30515 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30517 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30518 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30520 while (--ix > 0)
30522 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30524 if (prev->dw_attr == DW_AT_decl_line
30525 || prev->dw_attr == DW_AT_decl_column
30526 || prev->dw_attr == DW_AT_name)
30527 break;
30530 if (ix != vec_safe_length (die->die_attr) - 1)
30532 die->die_attr->pop ();
30533 die->die_attr->quick_insert (ix, linkage);
30537 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30538 referenced from typed stack ops and count how often they are used. */
30540 static void
30541 mark_base_types (dw_loc_descr_ref loc)
30543 dw_die_ref base_type = NULL;
30545 for (; loc; loc = loc->dw_loc_next)
30547 switch (loc->dw_loc_opc)
30549 case DW_OP_regval_type:
30550 case DW_OP_deref_type:
30551 case DW_OP_GNU_regval_type:
30552 case DW_OP_GNU_deref_type:
30553 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30554 break;
30555 case DW_OP_convert:
30556 case DW_OP_reinterpret:
30557 case DW_OP_GNU_convert:
30558 case DW_OP_GNU_reinterpret:
30559 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30560 continue;
30561 /* FALLTHRU */
30562 case DW_OP_const_type:
30563 case DW_OP_GNU_const_type:
30564 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30565 break;
30566 case DW_OP_entry_value:
30567 case DW_OP_GNU_entry_value:
30568 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30569 continue;
30570 default:
30571 continue;
30573 gcc_assert (base_type->die_parent == comp_unit_die ());
30574 if (base_type->die_mark)
30575 base_type->die_mark++;
30576 else
30578 base_types.safe_push (base_type);
30579 base_type->die_mark = 1;
30584 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30585 referenced from typed stack ops and count how often they are used. */
30587 static void
30588 mark_base_types (dw_die_ref die)
30590 dw_die_ref c;
30591 dw_attr_node *a;
30592 dw_loc_list_ref *curr;
30593 unsigned ix;
30595 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30596 switch (AT_class (a))
30598 case dw_val_class_loc_list:
30599 curr = AT_loc_list_ptr (a);
30600 while (*curr)
30602 mark_base_types ((*curr)->expr);
30603 curr = &(*curr)->dw_loc_next;
30605 break;
30607 case dw_val_class_loc:
30608 mark_base_types (AT_loc (a));
30609 break;
30611 default:
30612 break;
30615 FOR_EACH_CHILD (die, c, mark_base_types (c));
30618 /* Comparison function for sorting marked base types. */
30620 static int
30621 base_type_cmp (const void *x, const void *y)
30623 dw_die_ref dx = *(const dw_die_ref *) x;
30624 dw_die_ref dy = *(const dw_die_ref *) y;
30625 unsigned int byte_size1, byte_size2;
30626 unsigned int encoding1, encoding2;
30627 unsigned int align1, align2;
30628 if (dx->die_mark > dy->die_mark)
30629 return -1;
30630 if (dx->die_mark < dy->die_mark)
30631 return 1;
30632 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30633 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30634 if (byte_size1 < byte_size2)
30635 return 1;
30636 if (byte_size1 > byte_size2)
30637 return -1;
30638 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30639 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30640 if (encoding1 < encoding2)
30641 return 1;
30642 if (encoding1 > encoding2)
30643 return -1;
30644 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30645 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30646 if (align1 < align2)
30647 return 1;
30648 if (align1 > align2)
30649 return -1;
30650 return 0;
30653 /* Move base types marked by mark_base_types as early as possible
30654 in the CU, sorted by decreasing usage count both to make the
30655 uleb128 references as small as possible and to make sure they
30656 will have die_offset already computed by calc_die_sizes when
30657 sizes of typed stack loc ops is computed. */
30659 static void
30660 move_marked_base_types (void)
30662 unsigned int i;
30663 dw_die_ref base_type, die, c;
30665 if (base_types.is_empty ())
30666 return;
30668 /* Sort by decreasing usage count, they will be added again in that
30669 order later on. */
30670 base_types.qsort (base_type_cmp);
30671 die = comp_unit_die ();
30672 c = die->die_child;
30675 dw_die_ref prev = c;
30676 c = c->die_sib;
30677 while (c->die_mark)
30679 remove_child_with_prev (c, prev);
30680 /* As base types got marked, there must be at least
30681 one node other than DW_TAG_base_type. */
30682 gcc_assert (die->die_child != NULL);
30683 c = prev->die_sib;
30686 while (c != die->die_child);
30687 gcc_assert (die->die_child);
30688 c = die->die_child;
30689 for (i = 0; base_types.iterate (i, &base_type); i++)
30691 base_type->die_mark = 0;
30692 base_type->die_sib = c->die_sib;
30693 c->die_sib = base_type;
30694 c = base_type;
30698 /* Helper function for resolve_addr, attempt to resolve
30699 one CONST_STRING, return true if successful. Similarly verify that
30700 SYMBOL_REFs refer to variables emitted in the current CU. */
30702 static bool
30703 resolve_one_addr (rtx *addr)
30705 rtx rtl = *addr;
30707 if (GET_CODE (rtl) == CONST_STRING)
30709 size_t len = strlen (XSTR (rtl, 0)) + 1;
30710 tree t = build_string (len, XSTR (rtl, 0));
30711 tree tlen = size_int (len - 1);
30712 TREE_TYPE (t)
30713 = build_array_type (char_type_node, build_index_type (tlen));
30714 rtl = lookup_constant_def (t);
30715 if (!rtl || !MEM_P (rtl))
30716 return false;
30717 rtl = XEXP (rtl, 0);
30718 if (GET_CODE (rtl) == SYMBOL_REF
30719 && SYMBOL_REF_DECL (rtl)
30720 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30721 return false;
30722 vec_safe_push (used_rtx_array, rtl);
30723 *addr = rtl;
30724 return true;
30727 if (GET_CODE (rtl) == SYMBOL_REF
30728 && SYMBOL_REF_DECL (rtl))
30730 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30732 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30733 return false;
30735 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30736 return false;
30739 if (GET_CODE (rtl) == CONST)
30741 subrtx_ptr_iterator::array_type array;
30742 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30743 if (!resolve_one_addr (*iter))
30744 return false;
30747 return true;
30750 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30751 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30752 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30754 static rtx
30755 string_cst_pool_decl (tree t)
30757 rtx rtl = output_constant_def (t, 1);
30758 unsigned char *array;
30759 dw_loc_descr_ref l;
30760 tree decl;
30761 size_t len;
30762 dw_die_ref ref;
30764 if (!rtl || !MEM_P (rtl))
30765 return NULL_RTX;
30766 rtl = XEXP (rtl, 0);
30767 if (GET_CODE (rtl) != SYMBOL_REF
30768 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30769 return NULL_RTX;
30771 decl = SYMBOL_REF_DECL (rtl);
30772 if (!lookup_decl_die (decl))
30774 len = TREE_STRING_LENGTH (t);
30775 vec_safe_push (used_rtx_array, rtl);
30776 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30777 array = ggc_vec_alloc<unsigned char> (len);
30778 memcpy (array, TREE_STRING_POINTER (t), len);
30779 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30780 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30781 l->dw_loc_oprnd2.v.val_vec.length = len;
30782 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30783 l->dw_loc_oprnd2.v.val_vec.array = array;
30784 add_AT_loc (ref, DW_AT_location, l);
30785 equate_decl_number_to_die (decl, ref);
30787 return rtl;
30790 /* Helper function of resolve_addr_in_expr. LOC is
30791 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30792 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30793 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30794 with DW_OP_implicit_pointer if possible
30795 and return true, if unsuccessful, return false. */
30797 static bool
30798 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30800 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30801 HOST_WIDE_INT offset = 0;
30802 dw_die_ref ref = NULL;
30803 tree decl;
30805 if (GET_CODE (rtl) == CONST
30806 && GET_CODE (XEXP (rtl, 0)) == PLUS
30807 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30809 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30810 rtl = XEXP (XEXP (rtl, 0), 0);
30812 if (GET_CODE (rtl) == CONST_STRING)
30814 size_t len = strlen (XSTR (rtl, 0)) + 1;
30815 tree t = build_string (len, XSTR (rtl, 0));
30816 tree tlen = size_int (len - 1);
30818 TREE_TYPE (t)
30819 = build_array_type (char_type_node, build_index_type (tlen));
30820 rtl = string_cst_pool_decl (t);
30821 if (!rtl)
30822 return false;
30824 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30826 decl = SYMBOL_REF_DECL (rtl);
30827 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30829 ref = lookup_decl_die (decl);
30830 if (ref && (get_AT (ref, DW_AT_location)
30831 || get_AT (ref, DW_AT_const_value)))
30833 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30834 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30835 loc->dw_loc_oprnd1.val_entry = NULL;
30836 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30837 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30838 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30839 loc->dw_loc_oprnd2.v.val_int = offset;
30840 return true;
30844 return false;
30847 /* Helper function for resolve_addr, handle one location
30848 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30849 the location list couldn't be resolved. */
30851 static bool
30852 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30854 dw_loc_descr_ref keep = NULL;
30855 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30856 switch (loc->dw_loc_opc)
30858 case DW_OP_addr:
30859 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30861 if ((prev == NULL
30862 || prev->dw_loc_opc == DW_OP_piece
30863 || prev->dw_loc_opc == DW_OP_bit_piece)
30864 && loc->dw_loc_next
30865 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30866 && (!dwarf_strict || dwarf_version >= 5)
30867 && optimize_one_addr_into_implicit_ptr (loc))
30868 break;
30869 return false;
30871 break;
30872 case DW_OP_GNU_addr_index:
30873 case DW_OP_addrx:
30874 case DW_OP_GNU_const_index:
30875 case DW_OP_constx:
30876 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30877 || loc->dw_loc_opc == DW_OP_addrx)
30878 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30879 || loc->dw_loc_opc == DW_OP_constx)
30880 && loc->dtprel))
30882 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30883 if (!resolve_one_addr (&rtl))
30884 return false;
30885 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30886 loc->dw_loc_oprnd1.val_entry
30887 = add_addr_table_entry (rtl, ate_kind_rtx);
30889 break;
30890 case DW_OP_const4u:
30891 case DW_OP_const8u:
30892 if (loc->dtprel
30893 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30894 return false;
30895 break;
30896 case DW_OP_plus_uconst:
30897 if (size_of_loc_descr (loc)
30898 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30900 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30902 dw_loc_descr_ref repl
30903 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30904 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30905 add_loc_descr (&repl, loc->dw_loc_next);
30906 *loc = *repl;
30908 break;
30909 case DW_OP_implicit_value:
30910 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30911 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30912 return false;
30913 break;
30914 case DW_OP_implicit_pointer:
30915 case DW_OP_GNU_implicit_pointer:
30916 case DW_OP_GNU_parameter_ref:
30917 case DW_OP_GNU_variable_value:
30918 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30920 dw_die_ref ref
30921 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30922 if (ref == NULL)
30923 return false;
30924 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30925 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30926 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30928 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30930 if (prev == NULL
30931 && loc->dw_loc_next == NULL
30932 && AT_class (a) == dw_val_class_loc)
30933 switch (a->dw_attr)
30935 /* Following attributes allow both exprloc and reference,
30936 so if the whole expression is DW_OP_GNU_variable_value
30937 alone we could transform it into reference. */
30938 case DW_AT_byte_size:
30939 case DW_AT_bit_size:
30940 case DW_AT_lower_bound:
30941 case DW_AT_upper_bound:
30942 case DW_AT_bit_stride:
30943 case DW_AT_count:
30944 case DW_AT_allocated:
30945 case DW_AT_associated:
30946 case DW_AT_byte_stride:
30947 a->dw_attr_val.val_class = dw_val_class_die_ref;
30948 a->dw_attr_val.val_entry = NULL;
30949 a->dw_attr_val.v.val_die_ref.die
30950 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30951 a->dw_attr_val.v.val_die_ref.external = 0;
30952 return true;
30953 default:
30954 break;
30956 if (dwarf_strict)
30957 return false;
30959 break;
30960 case DW_OP_const_type:
30961 case DW_OP_regval_type:
30962 case DW_OP_deref_type:
30963 case DW_OP_convert:
30964 case DW_OP_reinterpret:
30965 case DW_OP_GNU_const_type:
30966 case DW_OP_GNU_regval_type:
30967 case DW_OP_GNU_deref_type:
30968 case DW_OP_GNU_convert:
30969 case DW_OP_GNU_reinterpret:
30970 while (loc->dw_loc_next
30971 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30972 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30974 dw_die_ref base1, base2;
30975 unsigned enc1, enc2, size1, size2;
30976 if (loc->dw_loc_opc == DW_OP_regval_type
30977 || loc->dw_loc_opc == DW_OP_deref_type
30978 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30979 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30980 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30981 else if (loc->dw_loc_oprnd1.val_class
30982 == dw_val_class_unsigned_const)
30983 break;
30984 else
30985 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30986 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30987 == dw_val_class_unsigned_const)
30988 break;
30989 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30990 gcc_assert (base1->die_tag == DW_TAG_base_type
30991 && base2->die_tag == DW_TAG_base_type);
30992 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30993 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30994 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30995 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30996 if (size1 == size2
30997 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30998 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30999 && loc != keep)
31000 || enc1 == enc2))
31002 /* Optimize away next DW_OP_convert after
31003 adjusting LOC's base type die reference. */
31004 if (loc->dw_loc_opc == DW_OP_regval_type
31005 || loc->dw_loc_opc == DW_OP_deref_type
31006 || loc->dw_loc_opc == DW_OP_GNU_regval_type
31007 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
31008 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
31009 else
31010 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
31011 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
31012 continue;
31014 /* Don't change integer DW_OP_convert after e.g. floating
31015 point typed stack entry. */
31016 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
31017 keep = loc->dw_loc_next;
31018 break;
31020 break;
31021 default:
31022 break;
31024 return true;
31027 /* Helper function of resolve_addr. DIE had DW_AT_location of
31028 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
31029 and DW_OP_addr couldn't be resolved. resolve_addr has already
31030 removed the DW_AT_location attribute. This function attempts to
31031 add a new DW_AT_location attribute with DW_OP_implicit_pointer
31032 to it or DW_AT_const_value attribute, if possible. */
31034 static void
31035 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
31037 if (!VAR_P (decl)
31038 || lookup_decl_die (decl) != die
31039 || DECL_EXTERNAL (decl)
31040 || !TREE_STATIC (decl)
31041 || DECL_INITIAL (decl) == NULL_TREE
31042 || DECL_P (DECL_INITIAL (decl))
31043 || get_AT (die, DW_AT_const_value))
31044 return;
31046 tree init = DECL_INITIAL (decl);
31047 HOST_WIDE_INT offset = 0;
31048 /* For variables that have been optimized away and thus
31049 don't have a memory location, see if we can emit
31050 DW_AT_const_value instead. */
31051 if (tree_add_const_value_attribute (die, init))
31052 return;
31053 if (dwarf_strict && dwarf_version < 5)
31054 return;
31055 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
31056 and ADDR_EXPR refers to a decl that has DW_AT_location or
31057 DW_AT_const_value (but isn't addressable, otherwise
31058 resolving the original DW_OP_addr wouldn't fail), see if
31059 we can add DW_OP_implicit_pointer. */
31060 STRIP_NOPS (init);
31061 if (TREE_CODE (init) == POINTER_PLUS_EXPR
31062 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
31064 offset = tree_to_shwi (TREE_OPERAND (init, 1));
31065 init = TREE_OPERAND (init, 0);
31066 STRIP_NOPS (init);
31068 if (TREE_CODE (init) != ADDR_EXPR)
31069 return;
31070 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
31071 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
31072 || (VAR_P (TREE_OPERAND (init, 0))
31073 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
31074 && TREE_OPERAND (init, 0) != decl))
31076 dw_die_ref ref;
31077 dw_loc_descr_ref l;
31079 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
31081 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
31082 if (!rtl)
31083 return;
31084 decl = SYMBOL_REF_DECL (rtl);
31086 else
31087 decl = TREE_OPERAND (init, 0);
31088 ref = lookup_decl_die (decl);
31089 if (ref == NULL
31090 || (!get_AT (ref, DW_AT_location)
31091 && !get_AT (ref, DW_AT_const_value)))
31092 return;
31093 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
31094 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31095 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
31096 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31097 add_AT_loc (die, DW_AT_location, l);
31101 /* Return NULL if l is a DWARF expression, or first op that is not
31102 valid DWARF expression. */
31104 static dw_loc_descr_ref
31105 non_dwarf_expression (dw_loc_descr_ref l)
31107 while (l)
31109 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31110 return l;
31111 switch (l->dw_loc_opc)
31113 case DW_OP_regx:
31114 case DW_OP_implicit_value:
31115 case DW_OP_stack_value:
31116 case DW_OP_implicit_pointer:
31117 case DW_OP_GNU_implicit_pointer:
31118 case DW_OP_GNU_parameter_ref:
31119 case DW_OP_piece:
31120 case DW_OP_bit_piece:
31121 return l;
31122 default:
31123 break;
31125 l = l->dw_loc_next;
31127 return NULL;
31130 /* Return adjusted copy of EXPR:
31131 If it is empty DWARF expression, return it.
31132 If it is valid non-empty DWARF expression,
31133 return copy of EXPR with DW_OP_deref appended to it.
31134 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31135 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31136 If it is DWARF expression followed by DW_OP_stack_value, return
31137 copy of the DWARF expression without anything appended.
31138 Otherwise, return NULL. */
31140 static dw_loc_descr_ref
31141 copy_deref_exprloc (dw_loc_descr_ref expr)
31143 dw_loc_descr_ref tail = NULL;
31145 if (expr == NULL)
31146 return NULL;
31148 dw_loc_descr_ref l = non_dwarf_expression (expr);
31149 if (l && l->dw_loc_next)
31150 return NULL;
31152 if (l)
31154 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31155 tail = new_loc_descr ((enum dwarf_location_atom)
31156 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31157 0, 0);
31158 else
31159 switch (l->dw_loc_opc)
31161 case DW_OP_regx:
31162 tail = new_loc_descr (DW_OP_bregx,
31163 l->dw_loc_oprnd1.v.val_unsigned, 0);
31164 break;
31165 case DW_OP_stack_value:
31166 break;
31167 default:
31168 return NULL;
31171 else
31172 tail = new_loc_descr (DW_OP_deref, 0, 0);
31174 dw_loc_descr_ref ret = NULL, *p = &ret;
31175 while (expr != l)
31177 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31178 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31179 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31180 p = &(*p)->dw_loc_next;
31181 expr = expr->dw_loc_next;
31183 *p = tail;
31184 return ret;
31187 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31188 reference to a variable or argument, adjust it if needed and return:
31189 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31190 attribute if present should be removed
31191 0 keep the attribute perhaps with minor modifications, no need to rescan
31192 1 if the attribute has been successfully adjusted. */
31194 static int
31195 optimize_string_length (dw_attr_node *a)
31197 dw_loc_descr_ref l = AT_loc (a), lv;
31198 dw_die_ref die;
31199 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31201 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31202 die = lookup_decl_die (decl);
31203 if (die)
31205 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31206 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31207 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31209 else
31210 return -1;
31212 else
31213 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31215 /* DWARF5 allows reference class, so we can then reference the DIE.
31216 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31217 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31219 a->dw_attr_val.val_class = dw_val_class_die_ref;
31220 a->dw_attr_val.val_entry = NULL;
31221 a->dw_attr_val.v.val_die_ref.die = die;
31222 a->dw_attr_val.v.val_die_ref.external = 0;
31223 return 0;
31226 dw_attr_node *av = get_AT (die, DW_AT_location);
31227 dw_loc_list_ref d;
31228 bool non_dwarf_expr = false;
31230 if (av == NULL)
31231 return dwarf_strict ? -1 : 0;
31232 switch (AT_class (av))
31234 case dw_val_class_loc_list:
31235 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31236 if (d->expr && non_dwarf_expression (d->expr))
31237 non_dwarf_expr = true;
31238 break;
31239 case dw_val_class_view_list:
31240 gcc_unreachable ();
31241 case dw_val_class_loc:
31242 lv = AT_loc (av);
31243 if (lv == NULL)
31244 return dwarf_strict ? -1 : 0;
31245 if (non_dwarf_expression (lv))
31246 non_dwarf_expr = true;
31247 break;
31248 default:
31249 return dwarf_strict ? -1 : 0;
31252 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31253 into DW_OP_call4 or DW_OP_GNU_variable_value into
31254 DW_OP_call4 DW_OP_deref, do so. */
31255 if (!non_dwarf_expr
31256 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31258 l->dw_loc_opc = DW_OP_call4;
31259 if (l->dw_loc_next)
31260 l->dw_loc_next = NULL;
31261 else
31262 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31263 return 0;
31266 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31267 copy over the DW_AT_location attribute from die to a. */
31268 if (l->dw_loc_next != NULL)
31270 a->dw_attr_val = av->dw_attr_val;
31271 return 1;
31274 dw_loc_list_ref list, *p;
31275 switch (AT_class (av))
31277 case dw_val_class_loc_list:
31278 p = &list;
31279 list = NULL;
31280 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31282 lv = copy_deref_exprloc (d->expr);
31283 if (lv)
31285 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31286 p = &(*p)->dw_loc_next;
31288 else if (!dwarf_strict && d->expr)
31289 return 0;
31291 if (list == NULL)
31292 return dwarf_strict ? -1 : 0;
31293 a->dw_attr_val.val_class = dw_val_class_loc_list;
31294 gen_llsym (list);
31295 *AT_loc_list_ptr (a) = list;
31296 return 1;
31297 case dw_val_class_loc:
31298 lv = copy_deref_exprloc (AT_loc (av));
31299 if (lv == NULL)
31300 return dwarf_strict ? -1 : 0;
31301 a->dw_attr_val.v.val_loc = lv;
31302 return 1;
31303 default:
31304 gcc_unreachable ();
31308 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31309 an address in .rodata section if the string literal is emitted there,
31310 or remove the containing location list or replace DW_AT_const_value
31311 with DW_AT_location and empty location expression, if it isn't found
31312 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31313 to something that has been emitted in the current CU. */
31315 static void
31316 resolve_addr (dw_die_ref die)
31318 dw_die_ref c;
31319 dw_attr_node *a;
31320 dw_loc_list_ref *curr, *start, loc;
31321 unsigned ix;
31322 bool remove_AT_byte_size = false;
31324 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31325 switch (AT_class (a))
31327 case dw_val_class_loc_list:
31328 start = curr = AT_loc_list_ptr (a);
31329 loc = *curr;
31330 gcc_assert (loc);
31331 /* The same list can be referenced more than once. See if we have
31332 already recorded the result from a previous pass. */
31333 if (loc->replaced)
31334 *curr = loc->dw_loc_next;
31335 else if (!loc->resolved_addr)
31337 /* As things stand, we do not expect or allow one die to
31338 reference a suffix of another die's location list chain.
31339 References must be identical or completely separate.
31340 There is therefore no need to cache the result of this
31341 pass on any list other than the first; doing so
31342 would lead to unnecessary writes. */
31343 while (*curr)
31345 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31346 if (!resolve_addr_in_expr (a, (*curr)->expr))
31348 dw_loc_list_ref next = (*curr)->dw_loc_next;
31349 dw_loc_descr_ref l = (*curr)->expr;
31351 if (next && (*curr)->ll_symbol)
31353 gcc_assert (!next->ll_symbol);
31354 next->ll_symbol = (*curr)->ll_symbol;
31355 next->vl_symbol = (*curr)->vl_symbol;
31357 if (dwarf_split_debug_info)
31358 remove_loc_list_addr_table_entries (l);
31359 *curr = next;
31361 else
31363 mark_base_types ((*curr)->expr);
31364 curr = &(*curr)->dw_loc_next;
31367 if (loc == *start)
31368 loc->resolved_addr = 1;
31369 else
31371 loc->replaced = 1;
31372 loc->dw_loc_next = *start;
31375 if (!*start)
31377 remove_AT (die, a->dw_attr);
31378 ix--;
31380 break;
31381 case dw_val_class_view_list:
31383 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31384 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31385 dw_val_node *llnode
31386 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31387 /* If we no longer have a loclist, or it no longer needs
31388 views, drop this attribute. */
31389 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31391 remove_AT (die, a->dw_attr);
31392 ix--;
31394 break;
31396 case dw_val_class_loc:
31398 dw_loc_descr_ref l = AT_loc (a);
31399 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31400 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31401 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31402 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31403 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31404 with DW_FORM_ref referencing the same DIE as
31405 DW_OP_GNU_variable_value used to reference. */
31406 if (a->dw_attr == DW_AT_string_length
31407 && l
31408 && l->dw_loc_opc == DW_OP_GNU_variable_value
31409 && (l->dw_loc_next == NULL
31410 || (l->dw_loc_next->dw_loc_next == NULL
31411 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31413 switch (optimize_string_length (a))
31415 case -1:
31416 remove_AT (die, a->dw_attr);
31417 ix--;
31418 /* If we drop DW_AT_string_length, we need to drop also
31419 DW_AT_{string_length_,}byte_size. */
31420 remove_AT_byte_size = true;
31421 continue;
31422 default:
31423 break;
31424 case 1:
31425 /* Even if we keep the optimized DW_AT_string_length,
31426 it might have changed AT_class, so process it again. */
31427 ix--;
31428 continue;
31431 /* For -gdwarf-2 don't attempt to optimize
31432 DW_AT_data_member_location containing
31433 DW_OP_plus_uconst - older consumers might
31434 rely on it being that op instead of a more complex,
31435 but shorter, location description. */
31436 if ((dwarf_version > 2
31437 || a->dw_attr != DW_AT_data_member_location
31438 || l == NULL
31439 || l->dw_loc_opc != DW_OP_plus_uconst
31440 || l->dw_loc_next != NULL)
31441 && !resolve_addr_in_expr (a, l))
31443 if (dwarf_split_debug_info)
31444 remove_loc_list_addr_table_entries (l);
31445 if (l != NULL
31446 && l->dw_loc_next == NULL
31447 && l->dw_loc_opc == DW_OP_addr
31448 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31449 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31450 && a->dw_attr == DW_AT_location)
31452 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31453 remove_AT (die, a->dw_attr);
31454 ix--;
31455 optimize_location_into_implicit_ptr (die, decl);
31456 break;
31458 if (a->dw_attr == DW_AT_string_length)
31459 /* If we drop DW_AT_string_length, we need to drop also
31460 DW_AT_{string_length_,}byte_size. */
31461 remove_AT_byte_size = true;
31462 remove_AT (die, a->dw_attr);
31463 ix--;
31465 else
31466 mark_base_types (l);
31468 break;
31469 case dw_val_class_addr:
31470 if (a->dw_attr == DW_AT_const_value
31471 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31473 if (AT_index (a) != NOT_INDEXED)
31474 remove_addr_table_entry (a->dw_attr_val.val_entry);
31475 remove_AT (die, a->dw_attr);
31476 ix--;
31478 if ((die->die_tag == DW_TAG_call_site
31479 && a->dw_attr == DW_AT_call_origin)
31480 || (die->die_tag == DW_TAG_GNU_call_site
31481 && a->dw_attr == DW_AT_abstract_origin))
31483 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31484 dw_die_ref tdie = lookup_decl_die (tdecl);
31485 dw_die_ref cdie;
31486 if (tdie == NULL
31487 && DECL_EXTERNAL (tdecl)
31488 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31489 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31491 dw_die_ref pdie = cdie;
31492 /* Make sure we don't add these DIEs into type units.
31493 We could emit skeleton DIEs for context (namespaces,
31494 outer structs/classes) and a skeleton DIE for the
31495 innermost context with DW_AT_signature pointing to the
31496 type unit. See PR78835. */
31497 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31498 pdie = pdie->die_parent;
31499 if (pdie == NULL)
31501 /* Creating a full DIE for tdecl is overly expensive and
31502 at this point even wrong when in the LTO phase
31503 as it can end up generating new type DIEs we didn't
31504 output and thus optimize_external_refs will crash. */
31505 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31506 add_AT_flag (tdie, DW_AT_external, 1);
31507 add_AT_flag (tdie, DW_AT_declaration, 1);
31508 add_linkage_attr (tdie, tdecl);
31509 add_name_and_src_coords_attributes (tdie, tdecl, true);
31510 equate_decl_number_to_die (tdecl, tdie);
31513 if (tdie)
31515 a->dw_attr_val.val_class = dw_val_class_die_ref;
31516 a->dw_attr_val.v.val_die_ref.die = tdie;
31517 a->dw_attr_val.v.val_die_ref.external = 0;
31519 else
31521 if (AT_index (a) != NOT_INDEXED)
31522 remove_addr_table_entry (a->dw_attr_val.val_entry);
31523 remove_AT (die, a->dw_attr);
31524 ix--;
31527 break;
31528 default:
31529 break;
31532 if (remove_AT_byte_size)
31533 remove_AT (die, dwarf_version >= 5
31534 ? DW_AT_string_length_byte_size
31535 : DW_AT_byte_size);
31537 FOR_EACH_CHILD (die, c, resolve_addr (c));
31540 /* Helper routines for optimize_location_lists.
31541 This pass tries to share identical local lists in .debug_loc
31542 section. */
31544 /* Iteratively hash operands of LOC opcode into HSTATE. */
31546 static void
31547 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31549 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31550 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31552 switch (loc->dw_loc_opc)
31554 case DW_OP_const4u:
31555 case DW_OP_const8u:
31556 if (loc->dtprel)
31557 goto hash_addr;
31558 /* FALLTHRU */
31559 case DW_OP_const1u:
31560 case DW_OP_const1s:
31561 case DW_OP_const2u:
31562 case DW_OP_const2s:
31563 case DW_OP_const4s:
31564 case DW_OP_const8s:
31565 case DW_OP_constu:
31566 case DW_OP_consts:
31567 case DW_OP_pick:
31568 case DW_OP_plus_uconst:
31569 case DW_OP_breg0:
31570 case DW_OP_breg1:
31571 case DW_OP_breg2:
31572 case DW_OP_breg3:
31573 case DW_OP_breg4:
31574 case DW_OP_breg5:
31575 case DW_OP_breg6:
31576 case DW_OP_breg7:
31577 case DW_OP_breg8:
31578 case DW_OP_breg9:
31579 case DW_OP_breg10:
31580 case DW_OP_breg11:
31581 case DW_OP_breg12:
31582 case DW_OP_breg13:
31583 case DW_OP_breg14:
31584 case DW_OP_breg15:
31585 case DW_OP_breg16:
31586 case DW_OP_breg17:
31587 case DW_OP_breg18:
31588 case DW_OP_breg19:
31589 case DW_OP_breg20:
31590 case DW_OP_breg21:
31591 case DW_OP_breg22:
31592 case DW_OP_breg23:
31593 case DW_OP_breg24:
31594 case DW_OP_breg25:
31595 case DW_OP_breg26:
31596 case DW_OP_breg27:
31597 case DW_OP_breg28:
31598 case DW_OP_breg29:
31599 case DW_OP_breg30:
31600 case DW_OP_breg31:
31601 case DW_OP_regx:
31602 case DW_OP_fbreg:
31603 case DW_OP_piece:
31604 case DW_OP_deref_size:
31605 case DW_OP_xderef_size:
31606 hstate.add_object (val1->v.val_int);
31607 break;
31608 case DW_OP_skip:
31609 case DW_OP_bra:
31611 int offset;
31613 gcc_assert (val1->val_class == dw_val_class_loc);
31614 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31615 hstate.add_object (offset);
31617 break;
31618 case DW_OP_implicit_value:
31619 hstate.add_object (val1->v.val_unsigned);
31620 switch (val2->val_class)
31622 case dw_val_class_const:
31623 hstate.add_object (val2->v.val_int);
31624 break;
31625 case dw_val_class_vec:
31627 unsigned int elt_size = val2->v.val_vec.elt_size;
31628 unsigned int len = val2->v.val_vec.length;
31630 hstate.add_int (elt_size);
31631 hstate.add_int (len);
31632 hstate.add (val2->v.val_vec.array, len * elt_size);
31634 break;
31635 case dw_val_class_const_double:
31636 hstate.add_object (val2->v.val_double.low);
31637 hstate.add_object (val2->v.val_double.high);
31638 break;
31639 case dw_val_class_wide_int:
31640 hstate.add (val2->v.val_wide->get_val (),
31641 get_full_len (*val2->v.val_wide)
31642 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31643 break;
31644 case dw_val_class_addr:
31645 inchash::add_rtx (val2->v.val_addr, hstate);
31646 break;
31647 default:
31648 gcc_unreachable ();
31650 break;
31651 case DW_OP_bregx:
31652 case DW_OP_bit_piece:
31653 hstate.add_object (val1->v.val_int);
31654 hstate.add_object (val2->v.val_int);
31655 break;
31656 case DW_OP_addr:
31657 hash_addr:
31658 if (loc->dtprel)
31660 unsigned char dtprel = 0xd1;
31661 hstate.add_object (dtprel);
31663 inchash::add_rtx (val1->v.val_addr, hstate);
31664 break;
31665 case DW_OP_GNU_addr_index:
31666 case DW_OP_addrx:
31667 case DW_OP_GNU_const_index:
31668 case DW_OP_constx:
31670 if (loc->dtprel)
31672 unsigned char dtprel = 0xd1;
31673 hstate.add_object (dtprel);
31675 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31677 break;
31678 case DW_OP_implicit_pointer:
31679 case DW_OP_GNU_implicit_pointer:
31680 hstate.add_int (val2->v.val_int);
31681 break;
31682 case DW_OP_entry_value:
31683 case DW_OP_GNU_entry_value:
31684 hstate.add_object (val1->v.val_loc);
31685 break;
31686 case DW_OP_regval_type:
31687 case DW_OP_deref_type:
31688 case DW_OP_GNU_regval_type:
31689 case DW_OP_GNU_deref_type:
31691 unsigned int byte_size
31692 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31693 unsigned int encoding
31694 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31695 hstate.add_object (val1->v.val_int);
31696 hstate.add_object (byte_size);
31697 hstate.add_object (encoding);
31699 break;
31700 case DW_OP_convert:
31701 case DW_OP_reinterpret:
31702 case DW_OP_GNU_convert:
31703 case DW_OP_GNU_reinterpret:
31704 if (val1->val_class == dw_val_class_unsigned_const)
31706 hstate.add_object (val1->v.val_unsigned);
31707 break;
31709 /* FALLTHRU */
31710 case DW_OP_const_type:
31711 case DW_OP_GNU_const_type:
31713 unsigned int byte_size
31714 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31715 unsigned int encoding
31716 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31717 hstate.add_object (byte_size);
31718 hstate.add_object (encoding);
31719 if (loc->dw_loc_opc != DW_OP_const_type
31720 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31721 break;
31722 hstate.add_object (val2->val_class);
31723 switch (val2->val_class)
31725 case dw_val_class_const:
31726 hstate.add_object (val2->v.val_int);
31727 break;
31728 case dw_val_class_vec:
31730 unsigned int elt_size = val2->v.val_vec.elt_size;
31731 unsigned int len = val2->v.val_vec.length;
31733 hstate.add_object (elt_size);
31734 hstate.add_object (len);
31735 hstate.add (val2->v.val_vec.array, len * elt_size);
31737 break;
31738 case dw_val_class_const_double:
31739 hstate.add_object (val2->v.val_double.low);
31740 hstate.add_object (val2->v.val_double.high);
31741 break;
31742 case dw_val_class_wide_int:
31743 hstate.add (val2->v.val_wide->get_val (),
31744 get_full_len (*val2->v.val_wide)
31745 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31746 break;
31747 default:
31748 gcc_unreachable ();
31751 break;
31753 default:
31754 /* Other codes have no operands. */
31755 break;
31759 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31761 static inline void
31762 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31764 dw_loc_descr_ref l;
31765 bool sizes_computed = false;
31766 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31767 size_of_locs (loc);
31769 for (l = loc; l != NULL; l = l->dw_loc_next)
31771 enum dwarf_location_atom opc = l->dw_loc_opc;
31772 hstate.add_object (opc);
31773 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31775 size_of_locs (loc);
31776 sizes_computed = true;
31778 hash_loc_operands (l, hstate);
31782 /* Compute hash of the whole location list LIST_HEAD. */
31784 static inline void
31785 hash_loc_list (dw_loc_list_ref list_head)
31787 dw_loc_list_ref curr = list_head;
31788 inchash::hash hstate;
31790 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31792 hstate.add (curr->begin, strlen (curr->begin) + 1);
31793 hstate.add (curr->end, strlen (curr->end) + 1);
31794 hstate.add_object (curr->vbegin);
31795 hstate.add_object (curr->vend);
31796 if (curr->section)
31797 hstate.add (curr->section, strlen (curr->section) + 1);
31798 hash_locs (curr->expr, hstate);
31800 list_head->hash = hstate.end ();
31803 /* Return true if X and Y opcodes have the same operands. */
31805 static inline bool
31806 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31808 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31809 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31810 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31811 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31813 switch (x->dw_loc_opc)
31815 case DW_OP_const4u:
31816 case DW_OP_const8u:
31817 if (x->dtprel)
31818 goto hash_addr;
31819 /* FALLTHRU */
31820 case DW_OP_const1u:
31821 case DW_OP_const1s:
31822 case DW_OP_const2u:
31823 case DW_OP_const2s:
31824 case DW_OP_const4s:
31825 case DW_OP_const8s:
31826 case DW_OP_constu:
31827 case DW_OP_consts:
31828 case DW_OP_pick:
31829 case DW_OP_plus_uconst:
31830 case DW_OP_breg0:
31831 case DW_OP_breg1:
31832 case DW_OP_breg2:
31833 case DW_OP_breg3:
31834 case DW_OP_breg4:
31835 case DW_OP_breg5:
31836 case DW_OP_breg6:
31837 case DW_OP_breg7:
31838 case DW_OP_breg8:
31839 case DW_OP_breg9:
31840 case DW_OP_breg10:
31841 case DW_OP_breg11:
31842 case DW_OP_breg12:
31843 case DW_OP_breg13:
31844 case DW_OP_breg14:
31845 case DW_OP_breg15:
31846 case DW_OP_breg16:
31847 case DW_OP_breg17:
31848 case DW_OP_breg18:
31849 case DW_OP_breg19:
31850 case DW_OP_breg20:
31851 case DW_OP_breg21:
31852 case DW_OP_breg22:
31853 case DW_OP_breg23:
31854 case DW_OP_breg24:
31855 case DW_OP_breg25:
31856 case DW_OP_breg26:
31857 case DW_OP_breg27:
31858 case DW_OP_breg28:
31859 case DW_OP_breg29:
31860 case DW_OP_breg30:
31861 case DW_OP_breg31:
31862 case DW_OP_regx:
31863 case DW_OP_fbreg:
31864 case DW_OP_piece:
31865 case DW_OP_deref_size:
31866 case DW_OP_xderef_size:
31867 return valx1->v.val_int == valy1->v.val_int;
31868 case DW_OP_skip:
31869 case DW_OP_bra:
31870 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31871 can cause irrelevant differences in dw_loc_addr. */
31872 gcc_assert (valx1->val_class == dw_val_class_loc
31873 && valy1->val_class == dw_val_class_loc
31874 && (dwarf_split_debug_info
31875 || x->dw_loc_addr == y->dw_loc_addr));
31876 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31877 case DW_OP_implicit_value:
31878 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31879 || valx2->val_class != valy2->val_class)
31880 return false;
31881 switch (valx2->val_class)
31883 case dw_val_class_const:
31884 return valx2->v.val_int == valy2->v.val_int;
31885 case dw_val_class_vec:
31886 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31887 && valx2->v.val_vec.length == valy2->v.val_vec.length
31888 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31889 valx2->v.val_vec.elt_size
31890 * valx2->v.val_vec.length) == 0;
31891 case dw_val_class_const_double:
31892 return valx2->v.val_double.low == valy2->v.val_double.low
31893 && valx2->v.val_double.high == valy2->v.val_double.high;
31894 case dw_val_class_wide_int:
31895 return *valx2->v.val_wide == *valy2->v.val_wide;
31896 case dw_val_class_addr:
31897 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31898 default:
31899 gcc_unreachable ();
31901 case DW_OP_bregx:
31902 case DW_OP_bit_piece:
31903 return valx1->v.val_int == valy1->v.val_int
31904 && valx2->v.val_int == valy2->v.val_int;
31905 case DW_OP_addr:
31906 hash_addr:
31907 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31908 case DW_OP_GNU_addr_index:
31909 case DW_OP_addrx:
31910 case DW_OP_GNU_const_index:
31911 case DW_OP_constx:
31913 rtx ax1 = valx1->val_entry->addr.rtl;
31914 rtx ay1 = valy1->val_entry->addr.rtl;
31915 return rtx_equal_p (ax1, ay1);
31917 case DW_OP_implicit_pointer:
31918 case DW_OP_GNU_implicit_pointer:
31919 return valx1->val_class == dw_val_class_die_ref
31920 && valx1->val_class == valy1->val_class
31921 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31922 && valx2->v.val_int == valy2->v.val_int;
31923 case DW_OP_entry_value:
31924 case DW_OP_GNU_entry_value:
31925 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31926 case DW_OP_const_type:
31927 case DW_OP_GNU_const_type:
31928 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31929 || valx2->val_class != valy2->val_class)
31930 return false;
31931 switch (valx2->val_class)
31933 case dw_val_class_const:
31934 return valx2->v.val_int == valy2->v.val_int;
31935 case dw_val_class_vec:
31936 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31937 && valx2->v.val_vec.length == valy2->v.val_vec.length
31938 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31939 valx2->v.val_vec.elt_size
31940 * valx2->v.val_vec.length) == 0;
31941 case dw_val_class_const_double:
31942 return valx2->v.val_double.low == valy2->v.val_double.low
31943 && valx2->v.val_double.high == valy2->v.val_double.high;
31944 case dw_val_class_wide_int:
31945 return *valx2->v.val_wide == *valy2->v.val_wide;
31946 default:
31947 gcc_unreachable ();
31949 case DW_OP_regval_type:
31950 case DW_OP_deref_type:
31951 case DW_OP_GNU_regval_type:
31952 case DW_OP_GNU_deref_type:
31953 return valx1->v.val_int == valy1->v.val_int
31954 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31955 case DW_OP_convert:
31956 case DW_OP_reinterpret:
31957 case DW_OP_GNU_convert:
31958 case DW_OP_GNU_reinterpret:
31959 if (valx1->val_class != valy1->val_class)
31960 return false;
31961 if (valx1->val_class == dw_val_class_unsigned_const)
31962 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31963 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31964 case DW_OP_GNU_parameter_ref:
31965 return valx1->val_class == dw_val_class_die_ref
31966 && valx1->val_class == valy1->val_class
31967 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31968 default:
31969 /* Other codes have no operands. */
31970 return true;
31974 /* Return true if DWARF location expressions X and Y are the same. */
31976 static inline bool
31977 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31979 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31980 if (x->dw_loc_opc != y->dw_loc_opc
31981 || x->dtprel != y->dtprel
31982 || !compare_loc_operands (x, y))
31983 break;
31984 return x == NULL && y == NULL;
31987 /* Hashtable helpers. */
31989 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31991 static inline hashval_t hash (const dw_loc_list_struct *);
31992 static inline bool equal (const dw_loc_list_struct *,
31993 const dw_loc_list_struct *);
31996 /* Return precomputed hash of location list X. */
31998 inline hashval_t
31999 loc_list_hasher::hash (const dw_loc_list_struct *x)
32001 return x->hash;
32004 /* Return true if location lists A and B are the same. */
32006 inline bool
32007 loc_list_hasher::equal (const dw_loc_list_struct *a,
32008 const dw_loc_list_struct *b)
32010 if (a == b)
32011 return true;
32012 if (a->hash != b->hash)
32013 return false;
32014 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
32015 if (strcmp (a->begin, b->begin) != 0
32016 || strcmp (a->end, b->end) != 0
32017 || (a->section == NULL) != (b->section == NULL)
32018 || (a->section && strcmp (a->section, b->section) != 0)
32019 || a->vbegin != b->vbegin || a->vend != b->vend
32020 || !compare_locs (a->expr, b->expr))
32021 break;
32022 return a == NULL && b == NULL;
32025 typedef hash_table<loc_list_hasher> loc_list_hash_type;
32028 /* Recursively optimize location lists referenced from DIE
32029 children and share them whenever possible. */
32031 static void
32032 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
32034 dw_die_ref c;
32035 dw_attr_node *a;
32036 unsigned ix;
32037 dw_loc_list_struct **slot;
32038 bool drop_locviews = false;
32039 bool has_locviews = false;
32041 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32042 if (AT_class (a) == dw_val_class_loc_list)
32044 dw_loc_list_ref list = AT_loc_list (a);
32045 /* TODO: perform some optimizations here, before hashing
32046 it and storing into the hash table. */
32047 hash_loc_list (list);
32048 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
32049 if (*slot == NULL)
32051 *slot = list;
32052 if (loc_list_has_views (list))
32053 gcc_assert (list->vl_symbol);
32054 else if (list->vl_symbol)
32056 drop_locviews = true;
32057 list->vl_symbol = NULL;
32060 else
32062 if (list->vl_symbol && !(*slot)->vl_symbol)
32063 drop_locviews = true;
32064 a->dw_attr_val.v.val_loc_list = *slot;
32067 else if (AT_class (a) == dw_val_class_view_list)
32069 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
32070 has_locviews = true;
32074 if (drop_locviews && has_locviews)
32075 remove_AT (die, DW_AT_GNU_locviews);
32077 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
32081 /* Recursively assign each location list a unique index into the debug_addr
32082 section. */
32084 static void
32085 index_location_lists (dw_die_ref die)
32087 dw_die_ref c;
32088 dw_attr_node *a;
32089 unsigned ix;
32091 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32092 if (AT_class (a) == dw_val_class_loc_list)
32094 dw_loc_list_ref list = AT_loc_list (a);
32095 dw_loc_list_ref curr;
32096 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32098 /* Don't index an entry that has already been indexed
32099 or won't be output. Make sure skip_loc_list_entry doesn't
32100 call size_of_locs, because that might cause circular dependency,
32101 index_location_lists requiring address table indexes to be
32102 computed, but adding new indexes through add_addr_table_entry
32103 and address table index computation requiring no new additions
32104 to the hash table. In the rare case of DWARF[234] >= 64KB
32105 location expression, we'll just waste unused address table entry
32106 for it. */
32107 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32108 continue;
32110 curr->begin_entry
32111 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32112 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32113 curr->end_entry
32114 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32118 FOR_EACH_CHILD (die, c, index_location_lists (c));
32121 /* Optimize location lists referenced from DIE
32122 children and share them whenever possible. */
32124 static void
32125 optimize_location_lists (dw_die_ref die)
32127 loc_list_hash_type htab (500);
32128 optimize_location_lists_1 (die, &htab);
32131 /* Traverse the limbo die list, and add parent/child links. The only
32132 dies without parents that should be here are concrete instances of
32133 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32134 For concrete instances, we can get the parent die from the abstract
32135 instance. */
32137 static void
32138 flush_limbo_die_list (void)
32140 limbo_die_node *node;
32142 /* get_context_die calls force_decl_die, which can put new DIEs on the
32143 limbo list in LTO mode when nested functions are put in a different
32144 partition than that of their parent function. */
32145 while ((node = limbo_die_list))
32147 dw_die_ref die = node->die;
32148 limbo_die_list = node->next;
32150 if (die->die_parent == NULL)
32152 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32154 if (origin && origin->die_parent)
32155 add_child_die (origin->die_parent, die);
32156 else if (is_cu_die (die))
32158 else if (seen_error ())
32159 /* It's OK to be confused by errors in the input. */
32160 add_child_die (comp_unit_die (), die);
32161 else
32163 /* In certain situations, the lexical block containing a
32164 nested function can be optimized away, which results
32165 in the nested function die being orphaned. Likewise
32166 with the return type of that nested function. Force
32167 this to be a child of the containing function.
32169 It may happen that even the containing function got fully
32170 inlined and optimized out. In that case we are lost and
32171 assign the empty child. This should not be big issue as
32172 the function is likely unreachable too. */
32173 gcc_assert (node->created_for);
32175 if (DECL_P (node->created_for))
32176 origin = get_context_die (DECL_CONTEXT (node->created_for));
32177 else if (TYPE_P (node->created_for))
32178 origin = scope_die_for (node->created_for, comp_unit_die ());
32179 else
32180 origin = comp_unit_die ();
32182 add_child_die (origin, die);
32188 /* Reset DIEs so we can output them again. */
32190 static void
32191 reset_dies (dw_die_ref die)
32193 dw_die_ref c;
32195 /* Remove stuff we re-generate. */
32196 die->die_mark = 0;
32197 die->die_offset = 0;
32198 die->die_abbrev = 0;
32199 remove_AT (die, DW_AT_sibling);
32201 FOR_EACH_CHILD (die, c, reset_dies (c));
32204 /* reset_indirect_string removed the references coming from DW_AT_name
32205 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32206 .debug_line_str strings again. */
32208 static void
32209 adjust_name_comp_dir (dw_die_ref die)
32211 for (int i = 0; i < 2; i++)
32213 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32214 dw_attr_node *a = get_AT (die, attr_kind);
32215 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32216 continue;
32218 if (!debug_line_str_hash)
32219 debug_line_str_hash
32220 = hash_table<indirect_string_hasher>::create_ggc (10);
32222 struct indirect_string_node *node
32223 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32224 debug_line_str_hash);
32225 set_indirect_string (node);
32226 node->form = DW_FORM_line_strp;
32227 a->dw_attr_val.v.val_str = node;
32231 /* Output stuff that dwarf requires at the end of every file,
32232 and generate the DWARF-2 debugging info. */
32234 static void
32235 dwarf2out_finish (const char *filename)
32237 comdat_type_node *ctnode;
32238 dw_die_ref main_comp_unit_die;
32239 unsigned char checksum[16];
32240 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32242 /* Generate CTF/BTF debug info. */
32243 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32244 || btf_debuginfo_p ()) && lang_GNU_C ())
32245 ctf_debug_finish (filename);
32247 /* Skip emitting DWARF if not required. */
32248 if (!dwarf_debuginfo_p ())
32249 return;
32251 /* Flush out any latecomers to the limbo party. */
32252 flush_limbo_die_list ();
32254 if (inline_entry_data_table)
32255 gcc_assert (inline_entry_data_table->is_empty ());
32257 if (flag_checking)
32259 verify_die (comp_unit_die ());
32260 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32261 verify_die (node->die);
32264 /* We shouldn't have any symbols with delayed asm names for
32265 DIEs generated after early finish. */
32266 gcc_assert (deferred_asm_name == NULL);
32268 gen_remaining_tmpl_value_param_die_attribute ();
32270 if (flag_generate_lto || flag_generate_offload)
32272 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32274 /* Prune stuff so that dwarf2out_finish runs successfully
32275 for the fat part of the object. */
32276 reset_dies (comp_unit_die ());
32277 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32278 reset_dies (node->die);
32280 hash_table<comdat_type_hasher> comdat_type_table (100);
32281 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32283 comdat_type_node **slot
32284 = comdat_type_table.find_slot (ctnode, INSERT);
32286 /* Don't reset types twice. */
32287 if (*slot != HTAB_EMPTY_ENTRY)
32288 continue;
32290 /* Remove the pointer to the line table. */
32291 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32293 if (debug_info_level >= DINFO_LEVEL_TERSE)
32294 reset_dies (ctnode->root_die);
32296 *slot = ctnode;
32299 /* Reset die CU symbol so we don't output it twice. */
32300 comp_unit_die ()->die_id.die_symbol = NULL;
32302 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32303 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32304 if (have_macinfo)
32305 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32307 /* Remove indirect string decisions. */
32308 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32309 if (debug_line_str_hash)
32311 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32312 debug_line_str_hash = NULL;
32313 if (asm_outputs_debug_line_str ())
32315 adjust_name_comp_dir (comp_unit_die ());
32316 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32317 adjust_name_comp_dir (node->die);
32322 #if ENABLE_ASSERT_CHECKING
32324 dw_die_ref die = comp_unit_die (), c;
32325 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32327 #endif
32328 base_types.truncate (0);
32329 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32330 resolve_addr (ctnode->root_die);
32331 resolve_addr (comp_unit_die ());
32332 move_marked_base_types ();
32334 if (dump_file)
32336 fprintf (dump_file, "DWARF for %s\n", filename);
32337 print_die (comp_unit_die (), dump_file);
32340 /* Initialize sections and labels used for actual assembler output. */
32341 unsigned generation = init_sections_and_labels (false);
32343 /* Traverse the DIE's and add sibling attributes to those DIE's that
32344 have children. */
32345 add_sibling_attributes (comp_unit_die ());
32346 limbo_die_node *node;
32347 for (node = cu_die_list; node; node = node->next)
32348 add_sibling_attributes (node->die);
32349 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32350 add_sibling_attributes (ctnode->root_die);
32352 /* When splitting DWARF info, we put some attributes in the
32353 skeleton compile_unit DIE that remains in the .o, while
32354 most attributes go in the DWO compile_unit_die. */
32355 if (dwarf_split_debug_info)
32357 limbo_die_node *cu;
32358 main_comp_unit_die = gen_compile_unit_die (NULL);
32359 if (dwarf_version >= 5)
32360 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32361 cu = limbo_die_list;
32362 gcc_assert (cu->die == main_comp_unit_die);
32363 limbo_die_list = limbo_die_list->next;
32364 cu->next = cu_die_list;
32365 cu_die_list = cu;
32367 else
32368 main_comp_unit_die = comp_unit_die ();
32370 /* Output a terminator label for the .text section. */
32371 switch_to_section (text_section);
32372 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32373 if (cold_text_section)
32375 switch_to_section (cold_text_section);
32376 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32379 /* We can only use the low/high_pc attributes if all of the code was
32380 in .text. */
32381 if ((!have_multiple_function_sections
32382 && vec_safe_length (switch_text_ranges) < 2)
32383 || (dwarf_version < 3 && dwarf_strict))
32385 const char *end_label = text_end_label;
32386 if (vec_safe_length (switch_text_ranges) == 1)
32387 end_label = (*switch_text_ranges)[0];
32388 /* Don't add if the CU has no associated code. */
32389 if (switch_text_ranges)
32390 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32391 end_label, true);
32393 else
32395 unsigned fde_idx;
32396 dw_fde_ref fde;
32397 bool range_list_added = false;
32398 if (switch_text_ranges)
32400 const char *prev_loc = text_section_label;
32401 const char *loc;
32402 unsigned idx;
32404 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32405 if (prev_loc)
32407 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32408 loc, &range_list_added, true);
32409 prev_loc = NULL;
32411 else
32412 prev_loc = loc;
32414 if (prev_loc)
32415 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32416 text_end_label, &range_list_added, true);
32419 if (switch_cold_ranges)
32421 const char *prev_loc = cold_text_section_label;
32422 const char *loc;
32423 unsigned idx;
32425 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32426 if (prev_loc)
32428 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32429 loc, &range_list_added, true);
32430 prev_loc = NULL;
32432 else
32433 prev_loc = loc;
32435 if (prev_loc)
32436 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32437 cold_end_label, &range_list_added, true);
32440 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32442 if (fde->ignored_debug)
32443 continue;
32444 if (!fde->in_std_section)
32445 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32446 fde->dw_fde_end, &range_list_added,
32447 true);
32448 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32449 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32450 fde->dw_fde_second_end, &range_list_added,
32451 true);
32454 if (range_list_added)
32456 /* We need to give .debug_loc and .debug_ranges an appropriate
32457 "base address". Use zero so that these addresses become
32458 absolute. Historically, we've emitted the unexpected
32459 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32460 Emit both to give time for other tools to adapt. */
32461 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32462 if (! dwarf_strict && dwarf_version < 4)
32463 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32465 add_ranges (NULL);
32466 have_multiple_function_sections = true;
32470 /* AIX Assembler inserts the length, so adjust the reference to match the
32471 offset expected by debuggers. */
32472 strcpy (dl_section_ref, debug_line_section_label);
32473 if (XCOFF_DEBUGGING_INFO)
32474 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32476 if (debug_info_level >= DINFO_LEVEL_TERSE)
32477 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32478 dl_section_ref);
32480 if (have_macinfo)
32481 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32482 macinfo_section_label);
32484 if (dwarf_split_debug_info)
32486 if (have_location_lists)
32488 /* Since we generate the loclists in the split DWARF .dwo
32489 file itself, we don't need to generate a loclists_base
32490 attribute for the split compile unit DIE. That attribute
32491 (and using relocatable sec_offset FORMs) isn't allowed
32492 for a split compile unit. Only if the .debug_loclists
32493 section was in the main file, would we need to generate a
32494 loclists_base attribute here (for the full or skeleton
32495 unit DIE). */
32497 /* optimize_location_lists calculates the size of the lists,
32498 so index them first, and assign indices to the entries.
32499 Although optimize_location_lists will remove entries from
32500 the table, it only does so for duplicates, and therefore
32501 only reduces ref_counts to 1. */
32502 index_location_lists (comp_unit_die ());
32505 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32506 index_rnglists ();
32508 if (addr_index_table != NULL)
32510 unsigned int index = 0;
32511 addr_index_table
32512 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32513 (&index);
32517 loc_list_idx = 0;
32518 if (have_location_lists)
32520 optimize_location_lists (comp_unit_die ());
32521 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32522 if (dwarf_version >= 5 && dwarf_split_debug_info)
32523 assign_location_list_indexes (comp_unit_die ());
32526 save_macinfo_strings ();
32528 if (dwarf_split_debug_info)
32530 unsigned int index = 0;
32532 /* Add attributes common to skeleton compile_units and
32533 type_units. Because these attributes include strings, it
32534 must be done before freezing the string table. Top-level
32535 skeleton die attrs are added when the skeleton type unit is
32536 created, so ensure it is created by this point. */
32537 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32538 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32541 /* Output all of the compilation units. We put the main one last so that
32542 the offsets are available to output_pubnames. */
32543 for (node = cu_die_list; node; node = node->next)
32544 output_comp_unit (node->die, 0, NULL);
32546 hash_table<comdat_type_hasher> comdat_type_table (100);
32547 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32549 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32551 /* Don't output duplicate types. */
32552 if (*slot != HTAB_EMPTY_ENTRY)
32553 continue;
32555 /* Add a pointer to the line table for the main compilation unit
32556 so that the debugger can make sense of DW_AT_decl_file
32557 attributes. */
32558 if (debug_info_level >= DINFO_LEVEL_TERSE)
32559 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32560 (!dwarf_split_debug_info
32561 ? dl_section_ref
32562 : debug_skeleton_line_section_label));
32564 output_comdat_type_unit (ctnode, false);
32565 *slot = ctnode;
32568 if (dwarf_split_debug_info)
32570 int mark;
32571 struct md5_ctx ctx;
32573 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32574 md5_init_ctx (&ctx);
32575 mark = 0;
32576 die_checksum (comp_unit_die (), &ctx, &mark);
32577 unmark_all_dies (comp_unit_die ());
32578 md5_finish_ctx (&ctx, checksum);
32580 if (dwarf_version < 5)
32582 /* Use the first 8 bytes of the checksum as the dwo_id,
32583 and add it to both comp-unit DIEs. */
32584 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32585 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32588 /* Add the base offset of the ranges table to the skeleton
32589 comp-unit DIE. */
32590 if (!vec_safe_is_empty (ranges_table))
32592 if (dwarf_version < 5)
32593 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32594 ranges_section_label);
32597 output_addr_table ();
32600 /* Output the main compilation unit if non-empty or if .debug_macinfo
32601 or .debug_macro will be emitted. */
32602 output_comp_unit (comp_unit_die (), have_macinfo,
32603 dwarf_split_debug_info ? checksum : NULL);
32605 if (dwarf_split_debug_info && info_section_emitted)
32606 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32608 /* Output the abbreviation table. */
32609 if (vec_safe_length (abbrev_die_table) != 1)
32611 switch_to_section (debug_abbrev_section);
32612 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32613 output_abbrev_section ();
32616 /* Output location list section if necessary. */
32617 if (have_location_lists)
32619 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32620 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32621 /* Output the location lists info. */
32622 switch_to_section (debug_loc_section);
32623 if (dwarf_version >= 5)
32625 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32626 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32627 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32628 dw2_asm_output_data (4, 0xffffffff,
32629 "Initial length escape value indicating "
32630 "64-bit DWARF extension");
32631 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32632 "Length of Location Lists");
32633 ASM_OUTPUT_LABEL (asm_out_file, l1);
32634 output_dwarf_version ();
32635 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32636 dw2_asm_output_data (1, 0, "Segment Size");
32637 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32638 "Offset Entry Count");
32640 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32641 if (dwarf_version >= 5 && dwarf_split_debug_info)
32643 unsigned int save_loc_list_idx = loc_list_idx;
32644 loc_list_idx = 0;
32645 output_loclists_offsets (comp_unit_die ());
32646 gcc_assert (save_loc_list_idx == loc_list_idx);
32648 output_location_lists (comp_unit_die ());
32649 if (dwarf_version >= 5)
32650 ASM_OUTPUT_LABEL (asm_out_file, l2);
32653 output_pubtables ();
32655 /* Output the address range information if a CU (.debug_info section)
32656 was emitted. We output an empty table even if we had no functions
32657 to put in it. This because the consumer has no way to tell the
32658 difference between an empty table that we omitted and failure to
32659 generate a table that would have contained data. */
32660 if (info_section_emitted)
32662 switch_to_section (debug_aranges_section);
32663 output_aranges ();
32666 /* Output ranges section if necessary. */
32667 if (!vec_safe_is_empty (ranges_table))
32669 if (dwarf_version >= 5)
32671 if (dwarf_split_debug_info)
32673 /* We don't know right now whether there are any
32674 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32675 Depending on into which of those two belongs the first
32676 ranges_table entry, emit that section first and that
32677 output_rnglists call will return true if the other kind of
32678 ranges needs to be emitted as well. */
32679 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32680 if (output_rnglists (generation, dwo))
32681 output_rnglists (generation, !dwo);
32683 else
32684 output_rnglists (generation, false);
32686 else
32687 output_ranges ();
32690 /* Have to end the macro section. */
32691 if (have_macinfo)
32693 switch_to_section (debug_macinfo_section);
32694 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32695 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32696 : debug_skeleton_line_section_label, false);
32697 dw2_asm_output_data (1, 0, "End compilation unit");
32700 /* Output the source line correspondence table. We must do this
32701 even if there is no line information. Otherwise, on an empty
32702 translation unit, we will generate a present, but empty,
32703 .debug_info section. IRIX 6.5 `nm' will then complain when
32704 examining the file. This is done late so that any filenames
32705 used by the debug_info section are marked as 'used'. */
32706 switch_to_section (debug_line_section);
32707 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32708 if (! output_asm_line_debug_info ())
32709 output_line_info (false);
32711 if (dwarf_split_debug_info && info_section_emitted)
32713 switch_to_section (debug_skeleton_line_section);
32714 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32715 output_line_info (true);
32718 /* If we emitted any indirect strings, output the string table too. */
32719 if (debug_str_hash || skeleton_debug_str_hash)
32720 output_indirect_strings ();
32721 if (debug_line_str_hash)
32723 switch_to_section (debug_line_str_section);
32724 const enum dwarf_form form = DW_FORM_line_strp;
32725 debug_line_str_hash->traverse<enum dwarf_form,
32726 output_indirect_string> (form);
32729 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32730 symview_upper_bound = 0;
32731 if (zero_view_p)
32732 bitmap_clear (zero_view_p);
32735 /* Returns a hash value for X (which really is a variable_value_struct). */
32737 inline hashval_t
32738 variable_value_hasher::hash (variable_value_struct *x)
32740 return (hashval_t) x->decl_id;
32743 /* Return true if decl_id of variable_value_struct X is the same as
32744 UID of decl Y. */
32746 inline bool
32747 variable_value_hasher::equal (variable_value_struct *x, tree y)
32749 return x->decl_id == DECL_UID (y);
32752 /* Helper function for resolve_variable_value, handle
32753 DW_OP_GNU_variable_value in one location expression.
32754 Return true if exprloc has been changed into loclist. */
32756 static bool
32757 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32759 dw_loc_descr_ref next;
32760 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32762 next = loc->dw_loc_next;
32763 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32764 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32765 continue;
32767 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32768 if (DECL_CONTEXT (decl) != current_function_decl)
32769 continue;
32771 dw_die_ref ref = lookup_decl_die (decl);
32772 if (ref)
32774 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32775 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32776 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32777 continue;
32779 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32780 if (l == NULL)
32781 continue;
32782 if (l->dw_loc_next)
32784 if (AT_class (a) != dw_val_class_loc)
32785 continue;
32786 switch (a->dw_attr)
32788 /* Following attributes allow both exprloc and loclist
32789 classes, so we can change them into a loclist. */
32790 case DW_AT_location:
32791 case DW_AT_string_length:
32792 case DW_AT_return_addr:
32793 case DW_AT_data_member_location:
32794 case DW_AT_frame_base:
32795 case DW_AT_segment:
32796 case DW_AT_static_link:
32797 case DW_AT_use_location:
32798 case DW_AT_vtable_elem_location:
32799 if (prev)
32801 prev->dw_loc_next = NULL;
32802 prepend_loc_descr_to_each (l, AT_loc (a));
32804 if (next)
32805 add_loc_descr_to_each (l, next);
32806 a->dw_attr_val.val_class = dw_val_class_loc_list;
32807 a->dw_attr_val.val_entry = NULL;
32808 a->dw_attr_val.v.val_loc_list = l;
32809 have_location_lists = true;
32810 return true;
32811 /* Following attributes allow both exprloc and reference,
32812 so if the whole expression is DW_OP_GNU_variable_value alone
32813 we could transform it into reference. */
32814 case DW_AT_byte_size:
32815 case DW_AT_bit_size:
32816 case DW_AT_lower_bound:
32817 case DW_AT_upper_bound:
32818 case DW_AT_bit_stride:
32819 case DW_AT_count:
32820 case DW_AT_allocated:
32821 case DW_AT_associated:
32822 case DW_AT_byte_stride:
32823 if (prev == NULL && next == NULL)
32824 break;
32825 /* FALLTHRU */
32826 default:
32827 if (dwarf_strict)
32828 continue;
32829 break;
32831 /* Create DW_TAG_variable that we can refer to. */
32832 gen_decl_die (decl, NULL_TREE, NULL,
32833 lookup_decl_die (current_function_decl));
32834 ref = lookup_decl_die (decl);
32835 if (ref)
32837 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32838 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32839 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32841 continue;
32843 if (prev)
32845 prev->dw_loc_next = l->expr;
32846 add_loc_descr (&prev->dw_loc_next, next);
32847 free_loc_descr (loc, NULL);
32848 next = prev->dw_loc_next;
32850 else
32852 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32853 add_loc_descr (&loc, next);
32854 next = loc;
32856 loc = prev;
32858 return false;
32861 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32863 static void
32864 resolve_variable_value (dw_die_ref die)
32866 dw_attr_node *a;
32867 dw_loc_list_ref loc;
32868 unsigned ix;
32870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32871 switch (AT_class (a))
32873 case dw_val_class_loc:
32874 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32875 break;
32876 /* FALLTHRU */
32877 case dw_val_class_loc_list:
32878 loc = AT_loc_list (a);
32879 gcc_assert (loc);
32880 for (; loc; loc = loc->dw_loc_next)
32881 resolve_variable_value_in_expr (a, loc->expr);
32882 break;
32883 default:
32884 break;
32888 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32889 temporaries in the current function. */
32891 static void
32892 resolve_variable_values (void)
32894 if (!variable_value_hash || !current_function_decl)
32895 return;
32897 struct variable_value_struct *node
32898 = variable_value_hash->find_with_hash (current_function_decl,
32899 DECL_UID (current_function_decl));
32901 if (node == NULL)
32902 return;
32904 unsigned int i;
32905 dw_die_ref die;
32906 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32907 resolve_variable_value (die);
32910 /* Helper function for note_variable_value, handle one location
32911 expression. */
32913 static void
32914 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32916 for (; loc; loc = loc->dw_loc_next)
32917 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32918 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32920 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32921 dw_die_ref ref = lookup_decl_die (decl);
32922 if (! ref && (flag_generate_lto || flag_generate_offload))
32924 /* ??? This is somewhat a hack because we do not create DIEs
32925 for variables not in BLOCK trees early but when generating
32926 early LTO output we need the dw_val_class_decl_ref to be
32927 fully resolved. For fat LTO objects we'd also like to
32928 undo this after LTO dwarf output. */
32929 gcc_assert (DECL_CONTEXT (decl));
32930 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32931 gcc_assert (ctx != NULL);
32932 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32933 ref = lookup_decl_die (decl);
32934 gcc_assert (ref != NULL);
32936 if (ref)
32938 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32939 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32940 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32941 continue;
32943 if (VAR_P (decl)
32944 && DECL_CONTEXT (decl)
32945 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32946 && lookup_decl_die (DECL_CONTEXT (decl)))
32948 if (!variable_value_hash)
32949 variable_value_hash
32950 = hash_table<variable_value_hasher>::create_ggc (10);
32952 tree fndecl = DECL_CONTEXT (decl);
32953 struct variable_value_struct *node;
32954 struct variable_value_struct **slot
32955 = variable_value_hash->find_slot_with_hash (fndecl,
32956 DECL_UID (fndecl),
32957 INSERT);
32958 if (*slot == NULL)
32960 node = ggc_cleared_alloc<variable_value_struct> ();
32961 node->decl_id = DECL_UID (fndecl);
32962 *slot = node;
32964 else
32965 node = *slot;
32967 vec_safe_push (node->dies, die);
32972 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32973 with dw_val_class_decl_ref operand. */
32975 static void
32976 note_variable_value (dw_die_ref die)
32978 dw_die_ref c;
32979 dw_attr_node *a;
32980 dw_loc_list_ref loc;
32981 unsigned ix;
32983 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32984 switch (AT_class (a))
32986 case dw_val_class_loc_list:
32987 loc = AT_loc_list (a);
32988 gcc_assert (loc);
32989 if (!loc->noted_variable_value)
32991 loc->noted_variable_value = 1;
32992 for (; loc; loc = loc->dw_loc_next)
32993 note_variable_value_in_expr (die, loc->expr);
32995 break;
32996 case dw_val_class_loc:
32997 note_variable_value_in_expr (die, AT_loc (a));
32998 break;
32999 default:
33000 break;
33003 /* Mark children. */
33004 FOR_EACH_CHILD (die, c, note_variable_value (c));
33007 /* Process DWARF dies for CTF generation. */
33009 static void
33010 ctf_debug_do_cu (dw_die_ref die)
33012 dw_die_ref c;
33014 if (!ctf_do_die (die))
33015 return;
33017 FOR_EACH_CHILD (die, c, ctf_do_die (c));
33020 /* Perform any cleanups needed after the early debug generation pass
33021 has run. */
33023 static void
33024 dwarf2out_early_finish (const char *filename)
33026 comdat_type_node *ctnode;
33027 set_early_dwarf s;
33028 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
33030 /* PCH might result in DW_AT_producer string being restored from the
33031 header compilation, so always fill it with empty string initially
33032 and overwrite only here. */
33033 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
33035 if (dwarf_record_gcc_switches)
33036 producer_string = gen_producer_string (lang_hooks.name,
33037 save_decoded_options,
33038 save_decoded_options_count);
33039 else
33040 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
33042 producer->dw_attr_val.v.val_str->refcount--;
33043 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
33045 /* Add the name for the main input file now. We delayed this from
33046 dwarf2out_init to avoid complications with PCH. */
33047 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
33048 add_comp_dir_attribute (comp_unit_die ());
33050 /* With LTO early dwarf was really finished at compile-time, so make
33051 sure to adjust the phase after annotating the LTRANS CU DIE. */
33052 if (in_lto_p)
33054 early_dwarf_finished = true;
33055 if (dump_file)
33057 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
33058 print_die (comp_unit_die (), dump_file);
33060 return;
33063 /* Walk through the list of incomplete types again, trying once more to
33064 emit full debugging info for them. */
33065 retry_incomplete_types ();
33067 gen_scheduled_generic_parms_dies ();
33068 gen_remaining_tmpl_value_param_die_attribute ();
33070 /* The point here is to flush out the limbo list so that it is empty
33071 and we don't need to stream it for LTO. */
33072 flush_limbo_die_list ();
33074 /* Add DW_AT_linkage_name for all deferred DIEs. */
33075 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
33077 tree decl = node->created_for;
33078 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
33079 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
33080 ended up in deferred_asm_name before we knew it was
33081 constant and never written to disk. */
33082 && DECL_ASSEMBLER_NAME (decl))
33084 add_linkage_attr (node->die, decl);
33085 move_linkage_attr (node->die);
33088 deferred_asm_name = NULL;
33090 if (flag_eliminate_unused_debug_types)
33091 prune_unused_types ();
33093 /* Generate separate COMDAT sections for type DIEs. */
33094 if (use_debug_types)
33096 break_out_comdat_types (comp_unit_die ());
33098 /* Each new type_unit DIE was added to the limbo die list when created.
33099 Since these have all been added to comdat_type_list, clear the
33100 limbo die list. */
33101 limbo_die_list = NULL;
33103 /* For each new comdat type unit, copy declarations for incomplete
33104 types to make the new unit self-contained (i.e., no direct
33105 references to the main compile unit). */
33106 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33107 copy_decls_for_unworthy_types (ctnode->root_die);
33108 copy_decls_for_unworthy_types (comp_unit_die ());
33110 /* In the process of copying declarations from one unit to another,
33111 we may have left some declarations behind that are no longer
33112 referenced. Prune them. */
33113 prune_unused_types ();
33116 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33117 with dw_val_class_decl_ref operand. */
33118 note_variable_value (comp_unit_die ());
33119 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33120 note_variable_value (node->die);
33121 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33122 note_variable_value (ctnode->root_die);
33123 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33124 note_variable_value (node->die);
33126 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33127 both the main_cu and all skeleton TUs. Making this call unconditional
33128 would end up either adding a second copy of the AT_pubnames attribute, or
33129 requiring a special case in add_top_level_skeleton_die_attrs. */
33130 if (!dwarf_split_debug_info)
33131 add_AT_pubnames (comp_unit_die ());
33133 /* The early debug phase is now finished. */
33134 early_dwarf_finished = true;
33135 if (dump_file)
33137 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33138 print_die (comp_unit_die (), dump_file);
33141 /* Generate CTF/BTF debug info. */
33142 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33143 || btf_debuginfo_p ()) && lang_GNU_C ())
33145 ctf_debug_init ();
33146 ctf_debug_do_cu (comp_unit_die ());
33147 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33148 ctf_debug_do_cu (node->die);
33149 /* Post process the debug data in the CTF container if necessary. */
33150 ctf_debug_init_postprocess (btf_debuginfo_p ());
33152 ctf_debug_early_finish (filename);
33155 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33156 if ((!flag_generate_lto && !flag_generate_offload)
33157 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33158 copy_lto_debug_sections operation of the simple object support in
33159 libiberty is not implemented for them yet. */
33160 || TARGET_PECOFF || TARGET_COFF)
33161 return;
33163 /* Now as we are going to output for LTO initialize sections and labels
33164 to the LTO variants. We don't need a random-seed postfix as other
33165 LTO sections as linking the LTO debug sections into one in a partial
33166 link is fine. */
33167 init_sections_and_labels (true);
33169 /* The output below is modeled after dwarf2out_finish with all
33170 location related output removed and some LTO specific changes.
33171 Some refactoring might make both smaller and easier to match up. */
33173 base_types.truncate (0);
33174 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33175 mark_base_types (ctnode->root_die);
33176 mark_base_types (comp_unit_die ());
33177 move_marked_base_types ();
33179 /* Traverse the DIE's and add sibling attributes to those DIE's
33180 that have children. */
33181 add_sibling_attributes (comp_unit_die ());
33182 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33183 add_sibling_attributes (node->die);
33184 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33185 add_sibling_attributes (ctnode->root_die);
33187 /* AIX Assembler inserts the length, so adjust the reference to match the
33188 offset expected by debuggers. */
33189 strcpy (dl_section_ref, debug_line_section_label);
33190 if (XCOFF_DEBUGGING_INFO)
33191 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33193 if (debug_info_level >= DINFO_LEVEL_TERSE)
33194 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33196 if (have_macinfo)
33197 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33198 macinfo_section_label);
33200 save_macinfo_strings ();
33202 if (dwarf_split_debug_info)
33204 unsigned int index = 0;
33205 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33208 /* Output all of the compilation units. We put the main one last so that
33209 the offsets are available to output_pubnames. */
33210 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33211 output_comp_unit (node->die, 0, NULL);
33213 hash_table<comdat_type_hasher> comdat_type_table (100);
33214 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33216 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33218 /* Don't output duplicate types. */
33219 if (*slot != HTAB_EMPTY_ENTRY)
33220 continue;
33222 /* Add a pointer to the line table for the main compilation unit
33223 so that the debugger can make sense of DW_AT_decl_file
33224 attributes. */
33225 if (debug_info_level >= DINFO_LEVEL_TERSE)
33226 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33227 (!dwarf_split_debug_info
33228 ? debug_line_section_label
33229 : debug_skeleton_line_section_label));
33231 output_comdat_type_unit (ctnode, true);
33232 *slot = ctnode;
33235 /* Stick a unique symbol to the main debuginfo section. */
33236 compute_comp_unit_symbol (comp_unit_die ());
33238 /* Output the main compilation unit. We always need it if only for
33239 the CU symbol. */
33240 output_comp_unit (comp_unit_die (), true, NULL);
33242 /* Output the abbreviation table. */
33243 if (vec_safe_length (abbrev_die_table) != 1)
33245 switch_to_section (debug_abbrev_section);
33246 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33247 output_abbrev_section ();
33250 /* Have to end the macro section. */
33251 if (have_macinfo)
33253 /* We have to save macinfo state if we need to output it again
33254 for the FAT part of the object. */
33255 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33256 if (flag_fat_lto_objects)
33257 macinfo_table = macinfo_table->copy ();
33259 switch_to_section (debug_macinfo_section);
33260 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33261 output_macinfo (debug_line_section_label, true);
33262 dw2_asm_output_data (1, 0, "End compilation unit");
33264 if (flag_fat_lto_objects)
33266 vec_free (macinfo_table);
33267 macinfo_table = saved_macinfo_table;
33271 /* Emit a skeleton debug_line section. */
33272 switch_to_section (debug_line_section);
33273 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33274 output_line_info (true);
33276 /* If we emitted any indirect strings, output the string table too. */
33277 if (debug_str_hash || skeleton_debug_str_hash)
33278 output_indirect_strings ();
33279 if (debug_line_str_hash)
33281 switch_to_section (debug_line_str_section);
33282 const enum dwarf_form form = DW_FORM_line_strp;
33283 debug_line_str_hash->traverse<enum dwarf_form,
33284 output_indirect_string> (form);
33287 /* Switch back to the text section. */
33288 switch_to_section (text_section);
33291 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33292 within the same process. For use by toplev::finalize. */
33294 void
33295 dwarf2out_cc_finalize (void)
33297 last_var_location_insn = NULL;
33298 cached_next_real_insn = NULL;
33299 used_rtx_array = NULL;
33300 incomplete_types = NULL;
33301 debug_info_section = NULL;
33302 debug_skeleton_info_section = NULL;
33303 debug_abbrev_section = NULL;
33304 debug_skeleton_abbrev_section = NULL;
33305 debug_aranges_section = NULL;
33306 debug_addr_section = NULL;
33307 debug_macinfo_section = NULL;
33308 debug_line_section = NULL;
33309 debug_skeleton_line_section = NULL;
33310 debug_loc_section = NULL;
33311 debug_pubnames_section = NULL;
33312 debug_pubtypes_section = NULL;
33313 debug_str_section = NULL;
33314 debug_line_str_section = NULL;
33315 debug_str_dwo_section = NULL;
33316 debug_str_offsets_section = NULL;
33317 debug_ranges_section = NULL;
33318 debug_ranges_dwo_section = NULL;
33319 debug_frame_section = NULL;
33320 fde_vec = NULL;
33321 debug_str_hash = NULL;
33322 debug_line_str_hash = NULL;
33323 skeleton_debug_str_hash = NULL;
33324 dw2_string_counter = 0;
33325 have_multiple_function_sections = false;
33326 in_text_section_p = false;
33327 cold_text_section = NULL;
33328 last_text_label = NULL;
33329 last_cold_label = NULL;
33330 switch_text_ranges = NULL;
33331 switch_cold_ranges = NULL;
33332 current_unit_personality = NULL;
33334 early_dwarf = false;
33335 early_dwarf_finished = false;
33337 next_die_offset = 0;
33338 single_comp_unit_die = NULL;
33339 comdat_type_list = NULL;
33340 limbo_die_list = NULL;
33341 file_table = NULL;
33342 decl_die_table = NULL;
33343 common_block_die_table = NULL;
33344 decl_loc_table = NULL;
33345 call_arg_locations = NULL;
33346 call_arg_loc_last = NULL;
33347 call_site_count = -1;
33348 tail_call_site_count = -1;
33349 cached_dw_loc_list_table = NULL;
33350 abbrev_die_table = NULL;
33351 delete dwarf_proc_stack_usage_map;
33352 dwarf_proc_stack_usage_map = NULL;
33353 line_info_label_num = 0;
33354 cur_line_info_table = NULL;
33355 text_section_line_info = NULL;
33356 cold_text_section_line_info = NULL;
33357 separate_line_info = NULL;
33358 info_section_emitted = false;
33359 pubname_table = NULL;
33360 pubtype_table = NULL;
33361 macinfo_table = NULL;
33362 ranges_table = NULL;
33363 ranges_by_label = NULL;
33364 rnglist_idx = 0;
33365 have_location_lists = false;
33366 loclabel_num = 0;
33367 poc_label_num = 0;
33368 last_emitted_file = NULL;
33369 label_num = 0;
33370 tmpl_value_parm_die_table = NULL;
33371 generic_type_instances = NULL;
33372 frame_pointer_fb_offset = 0;
33373 frame_pointer_fb_offset_valid = false;
33374 base_types.release ();
33375 XDELETEVEC (producer_string);
33376 producer_string = NULL;
33377 output_line_info_generation = 0;
33378 init_sections_and_labels_generation = 0;
33381 #include "gt-dwarf2out.h"