d: Add test for PR d/108167 to the testsuite [PR108167]
[official-gcc.git] / gcc / dwarf2out.cc
blob1f39df3b1e250c34c017b2d7811a3d5b3466a3a3
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2023 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2ctf.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tree-pretty-print.h"
87 #include "print-rtl.h"
88 #include "debug.h"
89 #include "common/common-target.h"
90 #include "langhooks.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
96 #include "rtl-iter.h"
97 #include "stringpool.h"
98 #include "attribs.h"
99 #include "file-prefix-map.h" /* remap_debug_filename() */
101 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
102 int, bool);
103 static rtx_insn *last_var_location_insn;
104 static rtx_insn *cached_next_real_insn;
105 static void dwarf2out_decl (tree);
106 static bool is_redundant_typedef (const_tree);
108 #ifndef XCOFF_DEBUGGING_INFO
109 #define XCOFF_DEBUGGING_INFO 0
110 #endif
112 #ifndef HAVE_XCOFF_DWARF_EXTRAS
113 #define HAVE_XCOFF_DWARF_EXTRAS 0
114 #endif
116 #ifdef VMS_DEBUGGING_INFO
117 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119 /* Define this macro to be a nonzero value if the directory specifications
120 which are output in the debug info should end with a separator. */
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
122 /* Define this macro to evaluate to a nonzero value if GCC should refrain
123 from generating indirect strings in DWARF2 debug information, for instance
124 if your target is stuck with an old version of GDB that is unable to
125 process them properly or uses VMS Debug. */
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
127 #else
128 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
130 #endif
132 /* ??? Poison these here until it can be done generically. They've been
133 totally replaced in this file; make sure it stays that way. */
134 #undef DWARF2_UNWIND_INFO
135 #undef DWARF2_FRAME_INFO
136 #if (GCC_VERSION >= 3000)
137 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
138 #endif
140 /* The size of the target's pointer type. */
141 #ifndef PTR_SIZE
142 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
143 #endif
145 /* Array of RTXes referenced by the debugging information, which therefore
146 must be kept around forever. */
147 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149 /* A pointer to the base of a list of incomplete types which might be
150 completed at some later time. incomplete_types_list needs to be a
151 vec<tree, va_gc> *because we want to tell the garbage collector about
152 it. */
153 static GTY(()) vec<tree, va_gc> *incomplete_types;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static unsigned macinfo_label_base = 1;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_ranges_dwo_section;
176 static GTY(()) section *debug_frame_section;
178 /* Maximum size (in bytes) of an artificially generated label. */
179 #define MAX_ARTIFICIAL_LABEL_BYTES 40
181 /* According to the (draft) DWARF 3 specification, the initial length
182 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
183 bytes are 0xffffffff, followed by the length stored in the next 8
184 bytes.
186 However, the SGI/MIPS ABI uses an initial length which is equal to
187 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189 #ifndef DWARF_INITIAL_LENGTH_SIZE
190 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
191 #endif
193 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
194 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
195 #endif
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
225 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227 typedef const char *compare_type;
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238 main object file, rather than the dwo, similar to the force_direct
239 parameter elsewhere but with additional complications:
241 1) The string is needed in both the main object file and the dwo.
242 That is, the comp_dir and dwo_name will appear in both places.
244 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
245 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247 3) GCC chooses the form to use late, depending on the size and
248 reference count.
250 Rather than forcing the all debug string handling functions and
251 callers to deal with these complications, simply use a separate,
252 special-cased string table for any attribute that should go in the
253 main object file. This limits the complexity to just the places
254 that need it. */
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258 static GTY(()) int dw2_string_counter;
260 /* True if the compilation unit places functions in more than one section. */
261 static GTY(()) bool have_multiple_function_sections = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* True if currently in text section. */
267 static GTY(()) bool in_text_section_p = false;
269 /* Last debug-on location in corresponding section. */
270 static GTY(()) const char *last_text_label;
271 static GTY(()) const char *last_cold_label;
273 /* Mark debug-on/off locations per section.
274 NULL means the section is not used at all. */
275 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
276 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278 /* The DIE for C++14 'auto' in a function return type. */
279 static GTY(()) dw_die_ref auto_die;
281 /* The DIE for C++14 'decltype(auto)' in a function return type. */
282 static GTY(()) dw_die_ref decltype_auto_die;
284 /* Forward declarations for functions defined in this file. */
286 static void output_call_frame_info (int);
288 /* Personality decl of current unit. Used only when assembler does not support
289 personality CFI. */
290 static GTY(()) rtx current_unit_personality;
292 /* Whether an eh_frame section is required. */
293 static GTY(()) bool do_eh_frame = false;
295 /* .debug_rnglists next index. */
296 static unsigned int rnglist_idx;
298 /* Data and reference forms for relocatable data. */
299 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
300 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302 #ifndef DEBUG_FRAME_SECTION
303 #define DEBUG_FRAME_SECTION ".debug_frame"
304 #endif
306 #ifndef FUNC_BEGIN_LABEL
307 #define FUNC_BEGIN_LABEL "LFB"
308 #endif
310 #ifndef FUNC_SECOND_SECT_LABEL
311 #define FUNC_SECOND_SECT_LABEL "LFSB"
312 #endif
314 #ifndef FUNC_END_LABEL
315 #define FUNC_END_LABEL "LFE"
316 #endif
318 #ifndef PROLOGUE_END_LABEL
319 #define PROLOGUE_END_LABEL "LPE"
320 #endif
322 #ifndef EPILOGUE_BEGIN_LABEL
323 #define EPILOGUE_BEGIN_LABEL "LEB"
324 #endif
326 #ifndef FRAME_BEGIN_LABEL
327 #define FRAME_BEGIN_LABEL "Lframe"
328 #endif
329 #define CIE_AFTER_SIZE_LABEL "LSCIE"
330 #define CIE_END_LABEL "LECIE"
331 #define FDE_LABEL "LSFDE"
332 #define FDE_AFTER_SIZE_LABEL "LASFDE"
333 #define FDE_END_LABEL "LEFDE"
334 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
335 #define LINE_NUMBER_END_LABEL "LELT"
336 #define LN_PROLOG_AS_LABEL "LASLTP"
337 #define LN_PROLOG_END_LABEL "LELTP"
338 #define DIE_LABEL_PREFIX "DW"
340 /* Match the base name of a file to the base name of a compilation unit. */
342 static int
343 matches_main_base (const char *path)
345 /* Cache the last query. */
346 static const char *last_path = NULL;
347 static int last_match = 0;
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 int
362 dump_struct_debug (tree type, enum debug_info_usage usage,
363 enum debug_struct_file criterion, int generic,
364 int matches, int result)
366 /* Find the type name. */
367 tree type_decl = TYPE_STUB_DECL (type);
368 tree t = type_decl;
369 const char *name = 0;
370 if (TREE_CODE (t) == TYPE_DECL)
371 t = DECL_NAME (t);
372 if (t)
373 name = IDENTIFIER_POINTER (t);
375 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
376 criterion,
377 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
378 matches ? "bas" : "hdr",
379 generic ? "gen" : "ord",
380 usage == DINFO_USAGE_DFN ? ";" :
381 usage == DINFO_USAGE_DIR_USE ? "." : "*",
382 result,
383 (void*) type_decl, name);
384 return result;
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 dump_struct_debug (type, usage, criterion, generic, matches, result)
389 #else
391 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
392 (result)
394 #endif
396 /* Get the number of HOST_WIDE_INTs needed to represent the precision
397 of the number. */
399 static unsigned int
400 get_full_len (const wide_int &op)
402 int prec = wi::get_precision (op);
403 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
404 / HOST_BITS_PER_WIDE_INT);
407 static bool
408 should_emit_struct_debug (tree type, enum debug_info_usage usage)
410 if (debug_info_level <= DINFO_LEVEL_TERSE)
411 return false;
413 enum debug_struct_file criterion;
414 tree type_decl;
415 bool generic = lang_hooks.types.generic_p (type);
417 if (generic)
418 criterion = debug_struct_generic[usage];
419 else
420 criterion = debug_struct_ordinary[usage];
422 if (criterion == DINFO_STRUCT_FILE_NONE)
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
424 if (criterion == DINFO_STRUCT_FILE_ANY)
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
427 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
429 if (type_decl != NULL)
431 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
432 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
434 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
435 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
438 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
441 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
442 switch to the data section instead, and write out a synthetic start label
443 for collect2 the first time around. */
445 static void
446 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
448 if (eh_frame_section == 0)
450 int flags;
452 if (EH_TABLES_CAN_BE_READ_ONLY)
454 int fde_encoding;
455 int per_encoding;
456 int lsda_encoding;
458 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
459 /*global=*/0);
460 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
461 /*global=*/1);
462 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
463 /*global=*/0);
464 flags = ((! flag_pic
465 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
466 && (fde_encoding & 0x70) != DW_EH_PE_aligned
467 && (per_encoding & 0x70) != DW_EH_PE_absptr
468 && (per_encoding & 0x70) != DW_EH_PE_aligned
469 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
470 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
471 ? 0 : SECTION_WRITE);
473 else
474 flags = SECTION_WRITE;
476 #ifdef EH_FRAME_SECTION_NAME
477 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
478 #else
479 eh_frame_section = ((flags == SECTION_WRITE)
480 ? data_section : readonly_data_section);
481 #endif /* EH_FRAME_SECTION_NAME */
484 switch_to_section (eh_frame_section);
486 #ifdef EH_FRAME_THROUGH_COLLECT2
487 /* We have no special eh_frame section. Emit special labels to guide
488 collect2. */
489 if (!back)
491 tree label = get_file_function_name ("F");
492 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
493 targetm.asm_out.globalize_label (asm_out_file,
494 IDENTIFIER_POINTER (label));
495 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
497 #endif
500 /* Switch [BACK] to the eh or debug frame table section, depending on
501 FOR_EH. */
503 static void
504 switch_to_frame_table_section (int for_eh, bool back)
506 if (for_eh)
507 switch_to_eh_frame_section (back);
508 else
510 if (!debug_frame_section)
511 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
512 SECTION_DEBUG, NULL);
513 switch_to_section (debug_frame_section);
517 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
519 enum dw_cfi_oprnd_type
520 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
522 switch (cfi)
524 case DW_CFA_nop:
525 case DW_CFA_GNU_window_save:
526 case DW_CFA_remember_state:
527 case DW_CFA_restore_state:
528 return dw_cfi_oprnd_unused;
530 case DW_CFA_set_loc:
531 case DW_CFA_advance_loc1:
532 case DW_CFA_advance_loc2:
533 case DW_CFA_advance_loc4:
534 case DW_CFA_MIPS_advance_loc8:
535 return dw_cfi_oprnd_addr;
537 case DW_CFA_offset:
538 case DW_CFA_offset_extended:
539 case DW_CFA_def_cfa:
540 case DW_CFA_offset_extended_sf:
541 case DW_CFA_def_cfa_sf:
542 case DW_CFA_restore:
543 case DW_CFA_restore_extended:
544 case DW_CFA_undefined:
545 case DW_CFA_same_value:
546 case DW_CFA_def_cfa_register:
547 case DW_CFA_register:
548 case DW_CFA_expression:
549 case DW_CFA_val_expression:
550 return dw_cfi_oprnd_reg_num;
552 case DW_CFA_def_cfa_offset:
553 case DW_CFA_GNU_args_size:
554 case DW_CFA_def_cfa_offset_sf:
555 return dw_cfi_oprnd_offset;
557 case DW_CFA_def_cfa_expression:
558 return dw_cfi_oprnd_loc;
560 default:
561 gcc_unreachable ();
565 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
567 enum dw_cfi_oprnd_type
568 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
570 switch (cfi)
572 case DW_CFA_def_cfa:
573 case DW_CFA_def_cfa_sf:
574 case DW_CFA_offset:
575 case DW_CFA_offset_extended_sf:
576 case DW_CFA_offset_extended:
577 return dw_cfi_oprnd_offset;
579 case DW_CFA_register:
580 return dw_cfi_oprnd_reg_num;
582 case DW_CFA_expression:
583 case DW_CFA_val_expression:
584 return dw_cfi_oprnd_loc;
586 case DW_CFA_def_cfa_expression:
587 return dw_cfi_oprnd_cfa_loc;
589 default:
590 return dw_cfi_oprnd_unused;
594 /* Output one FDE. */
596 static void
597 output_fde (dw_fde_ref fde, bool for_eh, bool second,
598 char *section_start_label, int fde_encoding, char *augmentation,
599 bool any_lsda_needed, int lsda_encoding)
601 const char *begin, *end;
602 static unsigned int j;
603 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
605 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
606 /* empty */ 0);
607 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
608 for_eh + j);
609 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
610 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
611 if (!XCOFF_DEBUGGING_INFO || for_eh)
613 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
614 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
615 " indicating 64-bit DWARF extension");
616 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
617 "FDE Length");
619 ASM_OUTPUT_LABEL (asm_out_file, l1);
621 if (for_eh)
622 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
623 else
624 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
625 debug_frame_section, "FDE CIE offset");
627 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
628 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
630 if (for_eh)
632 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
633 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
634 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
635 "FDE initial location");
636 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
637 end, begin, "FDE address range");
639 else
641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645 if (augmentation[0])
647 if (any_lsda_needed)
649 int size = size_of_encoded_value (lsda_encoding);
651 if (lsda_encoding == DW_EH_PE_aligned)
653 int offset = ( 4 /* Length */
654 + 4 /* CIE offset */
655 + 2 * size_of_encoded_value (fde_encoding)
656 + 1 /* Augmentation size */ );
657 int pad = -offset & (PTR_SIZE - 1);
659 size += pad;
660 gcc_assert (size_of_uleb128 (size) == 1);
663 dw2_asm_output_data_uleb128 (size, "Augmentation size");
665 if (fde->uses_eh_lsda)
667 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
668 fde->funcdef_number);
669 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
670 gen_rtx_SYMBOL_REF (Pmode, l1),
671 false,
672 "Language Specific Data Area");
674 else
676 if (lsda_encoding == DW_EH_PE_aligned)
677 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
678 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
679 "Language Specific Data Area (none)");
682 else
683 dw2_asm_output_data_uleb128 (0, "Augmentation size");
686 /* Loop through the Call Frame Instructions associated with this FDE. */
687 fde->dw_fde_current_label = begin;
689 size_t from, until, i;
691 from = 0;
692 until = vec_safe_length (fde->dw_fde_cfi);
694 if (fde->dw_fde_second_begin == NULL)
696 else if (!second)
697 until = fde->dw_fde_switch_cfi_index;
698 else
699 from = fde->dw_fde_switch_cfi_index;
701 for (i = from; i < until; i++)
702 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705 /* If we are to emit a ref/link from function bodies to their frame tables,
706 do it now. This is typically performed to make sure that tables
707 associated with functions are dragged with them and not discarded in
708 garbage collecting links. We need to do this on a per function basis to
709 cope with -ffunction-sections. */
711 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
712 /* Switch to the function section, emit the ref to the tables, and
713 switch *back* into the table section. */
714 switch_to_section (function_section (fde->decl));
715 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
716 switch_to_frame_table_section (for_eh, true);
717 #endif
719 /* Pad the FDE out to an address sized boundary. */
720 ASM_OUTPUT_ALIGN (asm_out_file,
721 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
722 ASM_OUTPUT_LABEL (asm_out_file, l2);
724 j += 2;
727 /* Return true if frame description entry FDE is needed for EH. */
729 static bool
730 fde_needed_for_eh_p (dw_fde_ref fde)
732 if (flag_asynchronous_unwind_tables)
733 return true;
735 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
736 return true;
738 if (fde->uses_eh_lsda)
739 return true;
741 /* If exceptions are enabled, we have collected nothrow info. */
742 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
743 return false;
745 return true;
748 /* Output the call frame information used to record information
749 that relates to calculating the frame pointer, and records the
750 location of saved registers. */
752 static void
753 output_call_frame_info (int for_eh)
755 unsigned int i;
756 dw_fde_ref fde;
757 dw_cfi_ref cfi;
758 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
759 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
760 bool any_lsda_needed = false;
761 char augmentation[6];
762 int augmentation_size;
763 int fde_encoding = DW_EH_PE_absptr;
764 int per_encoding = DW_EH_PE_absptr;
765 int lsda_encoding = DW_EH_PE_absptr;
766 int return_reg;
767 rtx personality = NULL;
768 int dw_cie_version;
770 /* Don't emit a CIE if there won't be any FDEs. */
771 if (!fde_vec)
772 return;
774 /* Nothing to do if the assembler's doing it all. */
775 if (dwarf2out_do_cfi_asm ())
776 return;
778 /* If we don't have any functions we'll want to unwind out of, don't emit
779 any EH unwind information. If we make FDEs linkonce, we may have to
780 emit an empty label for an FDE that wouldn't otherwise be emitted. We
781 want to avoid having an FDE kept around when the function it refers to
782 is discarded. Example where this matters: a primary function template
783 in C++ requires EH information, an explicit specialization doesn't. */
784 if (for_eh)
786 bool any_eh_needed = false;
788 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790 if (fde->uses_eh_lsda)
791 any_eh_needed = any_lsda_needed = true;
792 else if (fde_needed_for_eh_p (fde))
793 any_eh_needed = true;
794 else if (TARGET_USES_WEAK_UNWIND_INFO)
795 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
798 if (!any_eh_needed)
799 return;
802 /* We're going to be generating comments, so turn on app. */
803 if (flag_debug_asm)
804 app_enable ();
806 /* Switch to the proper frame section, first time. */
807 switch_to_frame_table_section (for_eh, false);
809 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
810 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812 /* Output the CIE. */
813 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
814 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
815 if (!XCOFF_DEBUGGING_INFO || for_eh)
817 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
818 dw2_asm_output_data (4, 0xffffffff,
819 "Initial length escape value indicating 64-bit DWARF extension");
820 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
821 "Length of Common Information Entry");
823 ASM_OUTPUT_LABEL (asm_out_file, l1);
825 /* Now that the CIE pointer is PC-relative for EH,
826 use 0 to identify the CIE. */
827 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
828 (for_eh ? 0 : DWARF_CIE_ID),
829 "CIE Identifier Tag");
831 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
832 use CIE version 1, unless that would produce incorrect results
833 due to overflowing the return register column. */
834 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
835 dw_cie_version = 1;
836 if (return_reg >= 256 || dwarf_version > 2)
837 dw_cie_version = 3;
838 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
840 augmentation[0] = 0;
841 augmentation_size = 0;
843 personality = current_unit_personality;
844 if (for_eh)
846 char *p;
848 /* Augmentation:
849 z Indicates that a uleb128 is present to size the
850 augmentation section.
851 L Indicates the encoding (and thus presence) of
852 an LSDA pointer in the FDE augmentation.
853 R Indicates a non-default pointer encoding for
854 FDE code pointers.
855 P Indicates the presence of an encoding + language
856 personality routine in the CIE augmentation. */
858 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
859 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
860 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
862 p = augmentation + 1;
863 if (personality)
865 *p++ = 'P';
866 augmentation_size += 1 + size_of_encoded_value (per_encoding);
867 assemble_external_libcall (personality);
869 if (any_lsda_needed)
871 *p++ = 'L';
872 augmentation_size += 1;
874 if (fde_encoding != DW_EH_PE_absptr)
876 *p++ = 'R';
877 augmentation_size += 1;
879 if (p > augmentation + 1)
881 augmentation[0] = 'z';
882 *p = '\0';
885 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
886 if (personality && per_encoding == DW_EH_PE_aligned)
888 int offset = ( 4 /* Length */
889 + 4 /* CIE Id */
890 + 1 /* CIE version */
891 + strlen (augmentation) + 1 /* Augmentation */
892 + size_of_uleb128 (1) /* Code alignment */
893 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
894 + 1 /* RA column */
895 + 1 /* Augmentation size */
896 + 1 /* Personality encoding */ );
897 int pad = -offset & (PTR_SIZE - 1);
899 augmentation_size += pad;
901 /* Augmentations should be small, so there's scarce need to
902 iterate for a solution. Die if we exceed one uleb128 byte. */
903 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
907 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
908 if (dw_cie_version >= 4)
910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
911 dw2_asm_output_data (1, 0, "CIE Segment Size");
913 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
914 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
915 "CIE Data Alignment Factor");
917 if (dw_cie_version == 1)
918 dw2_asm_output_data (1, return_reg, "CIE RA Column");
919 else
920 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
922 if (augmentation[0])
924 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
925 if (personality)
927 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
928 eh_data_format_name (per_encoding));
929 dw2_asm_output_encoded_addr_rtx (per_encoding,
930 personality,
931 true, NULL);
934 if (any_lsda_needed)
935 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
936 eh_data_format_name (lsda_encoding));
938 if (fde_encoding != DW_EH_PE_absptr)
939 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
940 eh_data_format_name (fde_encoding));
943 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
944 output_cfi (cfi, NULL, for_eh);
946 /* Pad the CIE out to an address sized boundary. */
947 ASM_OUTPUT_ALIGN (asm_out_file,
948 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
949 ASM_OUTPUT_LABEL (asm_out_file, l2);
951 /* Loop through all of the FDE's. */
952 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
954 unsigned int k;
956 /* Don't emit EH unwind info for leaf functions that don't need it. */
957 if (for_eh && !fde_needed_for_eh_p (fde))
958 continue;
960 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
961 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
962 augmentation, any_lsda_needed, lsda_encoding);
965 if (for_eh && targetm.terminate_dw2_eh_frame_info)
966 dw2_asm_output_data (4, 0, "End of Table");
968 /* Turn off app to make assembly quicker. */
969 if (flag_debug_asm)
970 app_disable ();
973 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
975 static void
976 dwarf2out_do_cfi_startproc (bool second)
978 int enc;
979 rtx ref;
981 fprintf (asm_out_file, "\t.cfi_startproc\n");
983 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
985 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
986 eh unwinders. */
987 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
988 return;
990 rtx personality = get_personality_function (current_function_decl);
992 if (personality)
994 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
995 ref = personality;
997 /* ??? The GAS support isn't entirely consistent. We have to
998 handle indirect support ourselves, but PC-relative is done
999 in the assembler. Further, the assembler can't handle any
1000 of the weirder relocation types. */
1001 if (enc & DW_EH_PE_indirect)
1003 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1004 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1005 else
1006 ref = dw2_force_const_mem (ref, true);
1009 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1010 output_addr_const (asm_out_file, ref);
1011 fputc ('\n', asm_out_file);
1014 if (crtl->uses_eh_lsda)
1016 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1018 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1019 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1020 current_function_funcdef_no);
1021 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1022 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1024 if (enc & DW_EH_PE_indirect)
1026 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1027 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1028 else
1029 ref = dw2_force_const_mem (ref, true);
1032 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1033 output_addr_const (asm_out_file, ref);
1034 fputc ('\n', asm_out_file);
1038 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1039 this allocation may be done before pass_final. */
1041 dw_fde_ref
1042 dwarf2out_alloc_current_fde (void)
1044 dw_fde_ref fde;
1046 fde = ggc_cleared_alloc<dw_fde_node> ();
1047 fde->decl = current_function_decl;
1048 fde->funcdef_number = current_function_funcdef_no;
1049 fde->fde_index = vec_safe_length (fde_vec);
1050 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1051 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1052 fde->nothrow = crtl->nothrow;
1053 fde->drap_reg = INVALID_REGNUM;
1054 fde->vdrap_reg = INVALID_REGNUM;
1056 /* Record the FDE associated with this function. */
1057 cfun->fde = fde;
1058 vec_safe_push (fde_vec, fde);
1060 return fde;
1063 /* Output a marker (i.e. a label) for the beginning of a function, before
1064 the prologue. */
1066 void
1067 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 unsigned int column ATTRIBUTE_UNUSED,
1069 const char *file ATTRIBUTE_UNUSED)
1071 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 char * dup_label;
1073 dw_fde_ref fde;
1074 section *fnsec;
1075 bool do_frame;
1077 current_function_func_begin_label = NULL;
1079 do_frame = dwarf2out_do_frame ();
1081 /* ??? current_function_func_begin_label is also used by except.cc for
1082 call-site information. We must emit this label if it might be used. */
1083 if (!do_frame
1084 && (!flag_exceptions
1085 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1086 return;
1088 fnsec = function_section (current_function_decl);
1089 switch_to_section (fnsec);
1090 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1091 current_function_funcdef_no);
1092 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1093 current_function_funcdef_no);
1094 dup_label = xstrdup (label);
1095 current_function_func_begin_label = dup_label;
1097 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1098 if (!do_frame)
1099 return;
1101 /* Unlike the debug version, the EH version of frame unwind info is a per-
1102 function setting so we need to record whether we need it for the unit. */
1103 do_eh_frame |= dwarf2out_do_eh_frame ();
1105 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1106 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1107 would include pass_dwarf2_frame. If we've not created the FDE yet,
1108 do so now. */
1109 fde = cfun->fde;
1110 if (fde == NULL)
1111 fde = dwarf2out_alloc_current_fde ();
1113 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1114 fde->dw_fde_begin = dup_label;
1115 fde->dw_fde_current_label = dup_label;
1116 fde->in_std_section = (fnsec == text_section
1117 || (cold_text_section && fnsec == cold_text_section));
1118 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1119 in_text_section_p = fnsec == text_section;
1121 /* We only want to output line number information for the genuine dwarf2
1122 prologue case, not the eh frame case. */
1123 #ifdef DWARF2_DEBUGGING_INFO
1124 if (file)
1125 dwarf2out_source_line (line, column, file, 0, true);
1126 #endif
1128 if (dwarf2out_do_cfi_asm ())
1129 dwarf2out_do_cfi_startproc (false);
1130 else
1132 rtx personality = get_personality_function (current_function_decl);
1133 if (!current_unit_personality)
1134 current_unit_personality = personality;
1136 /* We cannot keep a current personality per function as without CFI
1137 asm, at the point where we emit the CFI data, there is no current
1138 function anymore. */
1139 if (personality && current_unit_personality != personality)
1140 sorry ("multiple EH personalities are supported only with assemblers "
1141 "supporting %<.cfi_personality%> directive");
1145 /* Output a marker (i.e. a label) for the end of the generated code
1146 for a function prologue. This gets called *after* the prologue code has
1147 been generated. */
1149 void
1150 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1151 const char *file ATTRIBUTE_UNUSED)
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1160 current_function_funcdef_no);
1161 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the beginning of the generated code
1165 for a function epilogue. This gets called *before* the prologue code has
1166 been generated. */
1168 void
1169 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde = cfun->fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 if (fde->dw_fde_vms_begin_epilogue)
1176 return;
1178 /* Output a label to mark the endpoint of the code generated for this
1179 function. */
1180 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1181 current_function_funcdef_no);
1182 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1183 current_function_funcdef_no);
1184 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1187 /* Mark the ranges of non-debug subsections in the std text sections. */
1189 static void
1190 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1192 bool std_section;
1193 const char *begin_label, *end_label;
1194 const char **last_end_label;
1195 vec<const char *, va_gc> **switch_ranges;
1197 if (second)
1199 std_section = fde->second_in_std_section;
1200 begin_label = fde->dw_fde_second_begin;
1201 end_label = fde->dw_fde_second_end;
1203 else
1205 std_section = fde->in_std_section;
1206 begin_label = fde->dw_fde_begin;
1207 end_label = fde->dw_fde_end;
1210 if (!std_section)
1211 return;
1213 if (in_text_section_p)
1215 last_end_label = &last_text_label;
1216 switch_ranges = &switch_text_ranges;
1218 else
1220 last_end_label = &last_cold_label;
1221 switch_ranges = &switch_cold_ranges;
1224 if (fde->ignored_debug)
1226 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1227 vec_safe_push (*switch_ranges, *last_end_label);
1229 else
1231 *last_end_label = end_label;
1233 if (!*switch_ranges)
1234 vec_alloc (*switch_ranges, 16);
1235 else if (vec_safe_length (*switch_ranges) & 1)
1236 vec_safe_push (*switch_ranges, begin_label);
1240 /* Output a marker (i.e. a label) for the absolute end of the generated code
1241 for a function definition. This gets called *after* the epilogue code has
1242 been generated. */
1244 void
1245 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1246 const char *file ATTRIBUTE_UNUSED)
1248 dw_fde_ref fde;
1249 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1251 last_var_location_insn = NULL;
1252 cached_next_real_insn = NULL;
1254 if (dwarf2out_do_cfi_asm ())
1255 fprintf (asm_out_file, "\t.cfi_endproc\n");
1257 /* Output a label to mark the endpoint of the code generated for this
1258 function. */
1259 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1260 current_function_funcdef_no);
1261 ASM_OUTPUT_LABEL (asm_out_file, label);
1262 fde = cfun->fde;
1263 gcc_assert (fde != NULL);
1264 if (fde->dw_fde_second_begin == NULL)
1265 fde->dw_fde_end = xstrdup (label);
1267 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1270 void
1271 dwarf2out_frame_finish (void)
1273 /* Output call frame information. */
1274 if (targetm.debug_unwind_info () == UI_DWARF2)
1275 output_call_frame_info (0);
1277 /* Output another copy for the unwinder. */
1278 if (do_eh_frame)
1279 output_call_frame_info (1);
1282 static void var_location_switch_text_section (void);
1283 static void set_cur_line_info_table (section *);
1285 void
1286 dwarf2out_switch_text_section (void)
1288 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1289 section *sect;
1290 dw_fde_ref fde = cfun->fde;
1292 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1294 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1295 current_function_funcdef_no);
1297 fde->dw_fde_second_begin = ggc_strdup (label);
1298 if (!in_cold_section_p)
1300 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1301 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1303 else
1305 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1306 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1308 have_multiple_function_sections = true;
1310 if (dwarf2out_do_cfi_asm ())
1311 fprintf (asm_out_file, "\t.cfi_endproc\n");
1313 mark_ignored_debug_section (fde, false);
1315 /* Now do the real section switch. */
1316 sect = current_function_section ();
1317 switch_to_section (sect);
1319 fde->second_in_std_section
1320 = (sect == text_section
1321 || (cold_text_section && sect == cold_text_section));
1322 in_text_section_p = sect == text_section;
1324 if (dwarf2out_do_cfi_asm ())
1325 dwarf2out_do_cfi_startproc (true);
1327 var_location_switch_text_section ();
1329 if (cold_text_section != NULL)
1330 set_cur_line_info_table (sect);
1333 /* And now, the subset of the debugging information support code necessary
1334 for emitting location expressions. */
1336 /* Describe an entry into the .debug_addr section. */
1338 enum ate_kind {
1339 ate_kind_rtx,
1340 ate_kind_rtx_dtprel,
1341 ate_kind_label
1344 struct GTY((for_user)) addr_table_entry {
1345 enum ate_kind kind;
1346 unsigned int refcount;
1347 unsigned int index;
1348 union addr_table_entry_struct_union
1350 rtx GTY ((tag ("0"))) rtl;
1351 char * GTY ((tag ("1"))) label;
1353 GTY ((desc ("%1.kind"))) addr;
1356 typedef unsigned int var_loc_view;
1358 /* Location lists are ranges + location descriptions for that range,
1359 so you can track variables that are in different places over
1360 their entire life. */
1361 typedef struct GTY(()) dw_loc_list_struct {
1362 dw_loc_list_ref dw_loc_next;
1363 const char *begin; /* Label and addr_entry for start of range */
1364 addr_table_entry *begin_entry;
1365 const char *end; /* Label for end of range */
1366 addr_table_entry *end_entry;
1367 char *ll_symbol; /* Label for beginning of location list.
1368 Only on head of list. */
1369 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1370 const char *section; /* Section this loclist is relative to */
1371 dw_loc_descr_ref expr;
1372 var_loc_view vbegin, vend;
1373 hashval_t hash;
1374 /* True if all addresses in this and subsequent lists are known to be
1375 resolved. */
1376 bool resolved_addr;
1377 /* True if this list has been replaced by dw_loc_next. */
1378 bool replaced;
1379 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1380 section. */
1381 unsigned char emitted : 1;
1382 /* True if hash field is index rather than hash value. */
1383 unsigned char num_assigned : 1;
1384 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1385 unsigned char offset_emitted : 1;
1386 /* True if note_variable_value_in_expr has been called on it. */
1387 unsigned char noted_variable_value : 1;
1388 /* True if the range should be emitted even if begin and end
1389 are the same. */
1390 bool force;
1391 } dw_loc_list_node;
1393 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1394 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1396 /* Convert a DWARF stack opcode into its string name. */
1398 static const char *
1399 dwarf_stack_op_name (unsigned int op)
1401 const char *name = get_DW_OP_name (op);
1403 if (name != NULL)
1404 return name;
1406 return "OP_<unknown>";
1409 /* Return TRUE iff we're to output location view lists as a separate
1410 attribute next to the location lists, as an extension compatible
1411 with DWARF 2 and above. */
1413 static inline bool
1414 dwarf2out_locviews_in_attribute ()
1416 return debug_variable_location_views == 1;
1419 /* Return TRUE iff we're to output location view lists as part of the
1420 location lists, as proposed for standardization after DWARF 5. */
1422 static inline bool
1423 dwarf2out_locviews_in_loclist ()
1425 #ifndef DW_LLE_view_pair
1426 return false;
1427 #else
1428 return debug_variable_location_views == -1;
1429 #endif
1432 /* Return a pointer to a newly allocated location description. Location
1433 descriptions are simple expression terms that can be strung
1434 together to form more complicated location (address) descriptions. */
1436 static inline dw_loc_descr_ref
1437 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1438 unsigned HOST_WIDE_INT oprnd2)
1440 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1442 descr->dw_loc_opc = op;
1443 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1444 descr->dw_loc_oprnd1.val_entry = NULL;
1445 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1446 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1447 descr->dw_loc_oprnd2.val_entry = NULL;
1448 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1450 return descr;
1453 /* Add a location description term to a location description expression. */
1455 static inline void
1456 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1458 dw_loc_descr_ref *d;
1460 /* Find the end of the chain. */
1461 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1464 *d = descr;
1467 /* Compare two location operands for exact equality. */
1469 static bool
1470 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1472 if (a->val_class != b->val_class)
1473 return false;
1474 switch (a->val_class)
1476 case dw_val_class_none:
1477 return true;
1478 case dw_val_class_addr:
1479 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1481 case dw_val_class_offset:
1482 case dw_val_class_unsigned_const:
1483 case dw_val_class_const:
1484 case dw_val_class_unsigned_const_implicit:
1485 case dw_val_class_const_implicit:
1486 case dw_val_class_range_list:
1487 /* These are all HOST_WIDE_INT, signed or unsigned. */
1488 return a->v.val_unsigned == b->v.val_unsigned;
1490 case dw_val_class_loc:
1491 return a->v.val_loc == b->v.val_loc;
1492 case dw_val_class_loc_list:
1493 return a->v.val_loc_list == b->v.val_loc_list;
1494 case dw_val_class_view_list:
1495 return a->v.val_view_list == b->v.val_view_list;
1496 case dw_val_class_die_ref:
1497 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1498 case dw_val_class_fde_ref:
1499 return a->v.val_fde_index == b->v.val_fde_index;
1500 case dw_val_class_symview:
1501 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1502 case dw_val_class_lbl_id:
1503 case dw_val_class_lineptr:
1504 case dw_val_class_macptr:
1505 case dw_val_class_loclistsptr:
1506 case dw_val_class_high_pc:
1507 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1508 case dw_val_class_str:
1509 return a->v.val_str == b->v.val_str;
1510 case dw_val_class_flag:
1511 return a->v.val_flag == b->v.val_flag;
1512 case dw_val_class_file:
1513 case dw_val_class_file_implicit:
1514 return a->v.val_file == b->v.val_file;
1515 case dw_val_class_decl_ref:
1516 return a->v.val_decl_ref == b->v.val_decl_ref;
1518 case dw_val_class_const_double:
1519 return (a->v.val_double.high == b->v.val_double.high
1520 && a->v.val_double.low == b->v.val_double.low);
1522 case dw_val_class_wide_int:
1523 return *a->v.val_wide == *b->v.val_wide;
1525 case dw_val_class_vec:
1527 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1528 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1530 return (a_len == b_len
1531 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1534 case dw_val_class_data8:
1535 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1537 case dw_val_class_vms_delta:
1538 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1539 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1541 case dw_val_class_discr_value:
1542 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1543 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1544 case dw_val_class_discr_list:
1545 /* It makes no sense comparing two discriminant value lists. */
1546 return false;
1548 gcc_unreachable ();
1551 /* Compare two location atoms for exact equality. */
1553 static bool
1554 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1556 if (a->dw_loc_opc != b->dw_loc_opc)
1557 return false;
1559 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1560 address size, but since we always allocate cleared storage it
1561 should be zero for other types of locations. */
1562 if (a->dtprel != b->dtprel)
1563 return false;
1565 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1566 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1569 /* Compare two complete location expressions for exact equality. */
1571 bool
1572 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1574 while (1)
1576 if (a == b)
1577 return true;
1578 if (a == NULL || b == NULL)
1579 return false;
1580 if (!loc_descr_equal_p_1 (a, b))
1581 return false;
1583 a = a->dw_loc_next;
1584 b = b->dw_loc_next;
1589 /* Add a constant POLY_OFFSET to a location expression. */
1591 static void
1592 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1594 dw_loc_descr_ref loc;
1595 HOST_WIDE_INT *p;
1597 gcc_assert (*list_head != NULL);
1599 if (known_eq (poly_offset, 0))
1600 return;
1602 /* Find the end of the chain. */
1603 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1606 HOST_WIDE_INT offset;
1607 if (!poly_offset.is_constant (&offset))
1609 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1610 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1611 return;
1614 p = NULL;
1615 if (loc->dw_loc_opc == DW_OP_fbreg
1616 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1617 p = &loc->dw_loc_oprnd1.v.val_int;
1618 else if (loc->dw_loc_opc == DW_OP_bregx)
1619 p = &loc->dw_loc_oprnd2.v.val_int;
1621 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1622 offset. Don't optimize if an signed integer overflow would happen. */
1623 if (p != NULL
1624 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1625 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1626 *p += offset;
1628 else if (offset > 0)
1629 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1631 else
1633 loc->dw_loc_next
1634 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1635 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1639 /* Return a pointer to a newly allocated location description for
1640 REG and OFFSET. */
1642 static inline dw_loc_descr_ref
1643 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1645 HOST_WIDE_INT const_offset;
1646 if (offset.is_constant (&const_offset))
1648 if (reg <= 31)
1649 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1650 const_offset, 0);
1651 else
1652 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1654 else
1656 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1657 loc_descr_plus_const (&ret, offset);
1658 return ret;
1662 /* Add a constant OFFSET to a location list. */
1664 static void
1665 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1667 dw_loc_list_ref d;
1668 for (d = list_head; d != NULL; d = d->dw_loc_next)
1669 loc_descr_plus_const (&d->expr, offset);
1672 #define DWARF_REF_SIZE \
1673 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1675 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1676 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1677 DW_FORM_data16 with 128 bits. */
1678 #define DWARF_LARGEST_DATA_FORM_BITS \
1679 (dwarf_version >= 5 ? 128 : 64)
1681 /* Utility inline function for construction of ops that were GNU extension
1682 before DWARF 5. */
1683 static inline enum dwarf_location_atom
1684 dwarf_OP (enum dwarf_location_atom op)
1686 switch (op)
1688 case DW_OP_implicit_pointer:
1689 if (dwarf_version < 5)
1690 return DW_OP_GNU_implicit_pointer;
1691 break;
1693 case DW_OP_entry_value:
1694 if (dwarf_version < 5)
1695 return DW_OP_GNU_entry_value;
1696 break;
1698 case DW_OP_const_type:
1699 if (dwarf_version < 5)
1700 return DW_OP_GNU_const_type;
1701 break;
1703 case DW_OP_regval_type:
1704 if (dwarf_version < 5)
1705 return DW_OP_GNU_regval_type;
1706 break;
1708 case DW_OP_deref_type:
1709 if (dwarf_version < 5)
1710 return DW_OP_GNU_deref_type;
1711 break;
1713 case DW_OP_convert:
1714 if (dwarf_version < 5)
1715 return DW_OP_GNU_convert;
1716 break;
1718 case DW_OP_reinterpret:
1719 if (dwarf_version < 5)
1720 return DW_OP_GNU_reinterpret;
1721 break;
1723 case DW_OP_addrx:
1724 if (dwarf_version < 5)
1725 return DW_OP_GNU_addr_index;
1726 break;
1728 case DW_OP_constx:
1729 if (dwarf_version < 5)
1730 return DW_OP_GNU_const_index;
1731 break;
1733 default:
1734 break;
1736 return op;
1739 /* Similarly for attributes. */
1740 static inline enum dwarf_attribute
1741 dwarf_AT (enum dwarf_attribute at)
1743 switch (at)
1745 case DW_AT_call_return_pc:
1746 if (dwarf_version < 5)
1747 return DW_AT_low_pc;
1748 break;
1750 case DW_AT_call_tail_call:
1751 if (dwarf_version < 5)
1752 return DW_AT_GNU_tail_call;
1753 break;
1755 case DW_AT_call_origin:
1756 if (dwarf_version < 5)
1757 return DW_AT_abstract_origin;
1758 break;
1760 case DW_AT_call_target:
1761 if (dwarf_version < 5)
1762 return DW_AT_GNU_call_site_target;
1763 break;
1765 case DW_AT_call_target_clobbered:
1766 if (dwarf_version < 5)
1767 return DW_AT_GNU_call_site_target_clobbered;
1768 break;
1770 case DW_AT_call_parameter:
1771 if (dwarf_version < 5)
1772 return DW_AT_abstract_origin;
1773 break;
1775 case DW_AT_call_value:
1776 if (dwarf_version < 5)
1777 return DW_AT_GNU_call_site_value;
1778 break;
1780 case DW_AT_call_data_value:
1781 if (dwarf_version < 5)
1782 return DW_AT_GNU_call_site_data_value;
1783 break;
1785 case DW_AT_call_all_calls:
1786 if (dwarf_version < 5)
1787 return DW_AT_GNU_all_call_sites;
1788 break;
1790 case DW_AT_call_all_tail_calls:
1791 if (dwarf_version < 5)
1792 return DW_AT_GNU_all_tail_call_sites;
1793 break;
1795 case DW_AT_dwo_name:
1796 if (dwarf_version < 5)
1797 return DW_AT_GNU_dwo_name;
1798 break;
1800 case DW_AT_addr_base:
1801 if (dwarf_version < 5)
1802 return DW_AT_GNU_addr_base;
1803 break;
1805 default:
1806 break;
1808 return at;
1811 /* And similarly for tags. */
1812 static inline enum dwarf_tag
1813 dwarf_TAG (enum dwarf_tag tag)
1815 switch (tag)
1817 case DW_TAG_call_site:
1818 if (dwarf_version < 5)
1819 return DW_TAG_GNU_call_site;
1820 break;
1822 case DW_TAG_call_site_parameter:
1823 if (dwarf_version < 5)
1824 return DW_TAG_GNU_call_site_parameter;
1825 break;
1827 default:
1828 break;
1830 return tag;
1833 /* And similarly for forms. */
1834 static inline enum dwarf_form
1835 dwarf_FORM (enum dwarf_form form)
1837 switch (form)
1839 case DW_FORM_addrx:
1840 if (dwarf_version < 5)
1841 return DW_FORM_GNU_addr_index;
1842 break;
1844 case DW_FORM_strx:
1845 if (dwarf_version < 5)
1846 return DW_FORM_GNU_str_index;
1847 break;
1849 default:
1850 break;
1852 return form;
1855 static unsigned long int get_base_type_offset (dw_die_ref);
1857 /* Return the size of a location descriptor. */
1859 static unsigned long
1860 size_of_loc_descr (dw_loc_descr_ref loc)
1862 unsigned long size = 1;
1864 switch (loc->dw_loc_opc)
1866 case DW_OP_addr:
1867 size += DWARF2_ADDR_SIZE;
1868 break;
1869 case DW_OP_GNU_addr_index:
1870 case DW_OP_addrx:
1871 case DW_OP_GNU_const_index:
1872 case DW_OP_constx:
1873 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1874 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1875 break;
1876 case DW_OP_const1u:
1877 case DW_OP_const1s:
1878 size += 1;
1879 break;
1880 case DW_OP_const2u:
1881 case DW_OP_const2s:
1882 size += 2;
1883 break;
1884 case DW_OP_const4u:
1885 case DW_OP_const4s:
1886 size += 4;
1887 break;
1888 case DW_OP_const8u:
1889 case DW_OP_const8s:
1890 size += 8;
1891 break;
1892 case DW_OP_constu:
1893 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1894 break;
1895 case DW_OP_consts:
1896 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1897 break;
1898 case DW_OP_pick:
1899 size += 1;
1900 break;
1901 case DW_OP_plus_uconst:
1902 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1903 break;
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 size += 2;
1907 break;
1908 case DW_OP_breg0:
1909 case DW_OP_breg1:
1910 case DW_OP_breg2:
1911 case DW_OP_breg3:
1912 case DW_OP_breg4:
1913 case DW_OP_breg5:
1914 case DW_OP_breg6:
1915 case DW_OP_breg7:
1916 case DW_OP_breg8:
1917 case DW_OP_breg9:
1918 case DW_OP_breg10:
1919 case DW_OP_breg11:
1920 case DW_OP_breg12:
1921 case DW_OP_breg13:
1922 case DW_OP_breg14:
1923 case DW_OP_breg15:
1924 case DW_OP_breg16:
1925 case DW_OP_breg17:
1926 case DW_OP_breg18:
1927 case DW_OP_breg19:
1928 case DW_OP_breg20:
1929 case DW_OP_breg21:
1930 case DW_OP_breg22:
1931 case DW_OP_breg23:
1932 case DW_OP_breg24:
1933 case DW_OP_breg25:
1934 case DW_OP_breg26:
1935 case DW_OP_breg27:
1936 case DW_OP_breg28:
1937 case DW_OP_breg29:
1938 case DW_OP_breg30:
1939 case DW_OP_breg31:
1940 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1941 break;
1942 case DW_OP_regx:
1943 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1944 break;
1945 case DW_OP_fbreg:
1946 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1947 break;
1948 case DW_OP_bregx:
1949 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1950 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1951 break;
1952 case DW_OP_piece:
1953 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1954 break;
1955 case DW_OP_bit_piece:
1956 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1957 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1958 break;
1959 case DW_OP_deref_size:
1960 case DW_OP_xderef_size:
1961 size += 1;
1962 break;
1963 case DW_OP_call2:
1964 size += 2;
1965 break;
1966 case DW_OP_call4:
1967 size += 4;
1968 break;
1969 case DW_OP_call_ref:
1970 case DW_OP_GNU_variable_value:
1971 size += DWARF_REF_SIZE;
1972 break;
1973 case DW_OP_implicit_value:
1974 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1975 + loc->dw_loc_oprnd1.v.val_unsigned;
1976 break;
1977 case DW_OP_implicit_pointer:
1978 case DW_OP_GNU_implicit_pointer:
1979 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1980 break;
1981 case DW_OP_entry_value:
1982 case DW_OP_GNU_entry_value:
1984 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1985 size += size_of_uleb128 (op_size) + op_size;
1986 break;
1988 case DW_OP_const_type:
1989 case DW_OP_GNU_const_type:
1991 unsigned long o
1992 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1993 size += size_of_uleb128 (o) + 1;
1994 switch (loc->dw_loc_oprnd2.val_class)
1996 case dw_val_class_vec:
1997 size += loc->dw_loc_oprnd2.v.val_vec.length
1998 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1999 break;
2000 case dw_val_class_const:
2001 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2002 break;
2003 case dw_val_class_const_double:
2004 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2005 break;
2006 case dw_val_class_wide_int:
2007 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2008 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2009 break;
2010 default:
2011 gcc_unreachable ();
2013 break;
2015 case DW_OP_regval_type:
2016 case DW_OP_GNU_regval_type:
2018 unsigned long o
2019 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2020 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2021 + size_of_uleb128 (o);
2023 break;
2024 case DW_OP_deref_type:
2025 case DW_OP_GNU_deref_type:
2027 unsigned long o
2028 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2029 size += 1 + size_of_uleb128 (o);
2031 break;
2032 case DW_OP_convert:
2033 case DW_OP_reinterpret:
2034 case DW_OP_GNU_convert:
2035 case DW_OP_GNU_reinterpret:
2036 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2037 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2038 else
2040 unsigned long o
2041 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2042 size += size_of_uleb128 (o);
2044 break;
2045 case DW_OP_GNU_parameter_ref:
2046 size += 4;
2047 break;
2048 default:
2049 break;
2052 return size;
2055 /* Return the size of a series of location descriptors. */
2057 unsigned long
2058 size_of_locs (dw_loc_descr_ref loc)
2060 dw_loc_descr_ref l;
2061 unsigned long size;
2063 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2064 field, to avoid writing to a PCH file. */
2065 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2067 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2068 break;
2069 size += size_of_loc_descr (l);
2071 if (! l)
2072 return size;
2074 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2076 l->dw_loc_addr = size;
2077 size += size_of_loc_descr (l);
2080 return size;
2083 /* Return the size of the value in a DW_AT_discr_value attribute. */
2085 static int
2086 size_of_discr_value (dw_discr_value *discr_value)
2088 if (discr_value->pos)
2089 return size_of_uleb128 (discr_value->v.uval);
2090 else
2091 return size_of_sleb128 (discr_value->v.sval);
2094 /* Return the size of the value in a DW_AT_discr_list attribute. */
2096 static int
2097 size_of_discr_list (dw_discr_list_ref discr_list)
2099 int size = 0;
2101 for (dw_discr_list_ref list = discr_list;
2102 list != NULL;
2103 list = list->dw_discr_next)
2105 /* One byte for the discriminant value descriptor, and then one or two
2106 LEB128 numbers, depending on whether it's a single case label or a
2107 range label. */
2108 size += 1;
2109 size += size_of_discr_value (&list->dw_discr_lower_bound);
2110 if (list->dw_discr_range != 0)
2111 size += size_of_discr_value (&list->dw_discr_upper_bound);
2113 return size;
2116 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2117 static void get_ref_die_offset_label (char *, dw_die_ref);
2118 static unsigned long int get_ref_die_offset (dw_die_ref);
2120 /* Output location description stack opcode's operands (if any).
2121 The for_eh_or_skip parameter controls whether register numbers are
2122 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2123 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2124 info). This should be suppressed for the cases that have not been converted
2125 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2127 static void
2128 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2130 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2131 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2133 switch (loc->dw_loc_opc)
2135 #ifdef DWARF2_DEBUGGING_INFO
2136 case DW_OP_const2u:
2137 case DW_OP_const2s:
2138 dw2_asm_output_data (2, val1->v.val_int, NULL);
2139 break;
2140 case DW_OP_const4u:
2141 if (loc->dtprel)
2143 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2144 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2145 val1->v.val_addr);
2146 fputc ('\n', asm_out_file);
2147 break;
2149 /* FALLTHRU */
2150 case DW_OP_const4s:
2151 dw2_asm_output_data (4, val1->v.val_int, NULL);
2152 break;
2153 case DW_OP_const8u:
2154 if (loc->dtprel)
2156 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2157 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2158 val1->v.val_addr);
2159 fputc ('\n', asm_out_file);
2160 break;
2162 /* FALLTHRU */
2163 case DW_OP_const8s:
2164 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2165 dw2_asm_output_data (8, val1->v.val_int, NULL);
2166 break;
2167 case DW_OP_skip:
2168 case DW_OP_bra:
2170 int offset;
2172 gcc_assert (val1->val_class == dw_val_class_loc);
2173 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2175 dw2_asm_output_data (2, offset, NULL);
2177 break;
2178 case DW_OP_implicit_value:
2179 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2180 switch (val2->val_class)
2182 case dw_val_class_const:
2183 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2184 break;
2185 case dw_val_class_vec:
2187 unsigned int elt_size = val2->v.val_vec.elt_size;
2188 unsigned int len = val2->v.val_vec.length;
2189 unsigned int i;
2190 unsigned char *p;
2192 if (elt_size > sizeof (HOST_WIDE_INT))
2194 elt_size /= 2;
2195 len *= 2;
2197 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2198 i < len;
2199 i++, p += elt_size)
2200 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2201 "fp or vector constant word %u", i);
2203 break;
2204 case dw_val_class_const_double:
2206 unsigned HOST_WIDE_INT first, second;
2208 if (WORDS_BIG_ENDIAN)
2210 first = val2->v.val_double.high;
2211 second = val2->v.val_double.low;
2213 else
2215 first = val2->v.val_double.low;
2216 second = val2->v.val_double.high;
2218 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2219 first, NULL);
2220 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2221 second, NULL);
2223 break;
2224 case dw_val_class_wide_int:
2226 int i;
2227 int len = get_full_len (*val2->v.val_wide);
2228 if (WORDS_BIG_ENDIAN)
2229 for (i = len - 1; i >= 0; --i)
2230 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2231 val2->v.val_wide->elt (i), NULL);
2232 else
2233 for (i = 0; i < len; ++i)
2234 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2235 val2->v.val_wide->elt (i), NULL);
2237 break;
2238 case dw_val_class_addr:
2239 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2240 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2241 break;
2242 default:
2243 gcc_unreachable ();
2245 break;
2246 #else
2247 case DW_OP_const2u:
2248 case DW_OP_const2s:
2249 case DW_OP_const4u:
2250 case DW_OP_const4s:
2251 case DW_OP_const8u:
2252 case DW_OP_const8s:
2253 case DW_OP_skip:
2254 case DW_OP_bra:
2255 case DW_OP_implicit_value:
2256 /* We currently don't make any attempt to make sure these are
2257 aligned properly like we do for the main unwind info, so
2258 don't support emitting things larger than a byte if we're
2259 only doing unwinding. */
2260 gcc_unreachable ();
2261 #endif
2262 case DW_OP_const1u:
2263 case DW_OP_const1s:
2264 dw2_asm_output_data (1, val1->v.val_int, NULL);
2265 break;
2266 case DW_OP_constu:
2267 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2268 break;
2269 case DW_OP_consts:
2270 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2271 break;
2272 case DW_OP_pick:
2273 dw2_asm_output_data (1, val1->v.val_int, NULL);
2274 break;
2275 case DW_OP_plus_uconst:
2276 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2277 break;
2278 case DW_OP_breg0:
2279 case DW_OP_breg1:
2280 case DW_OP_breg2:
2281 case DW_OP_breg3:
2282 case DW_OP_breg4:
2283 case DW_OP_breg5:
2284 case DW_OP_breg6:
2285 case DW_OP_breg7:
2286 case DW_OP_breg8:
2287 case DW_OP_breg9:
2288 case DW_OP_breg10:
2289 case DW_OP_breg11:
2290 case DW_OP_breg12:
2291 case DW_OP_breg13:
2292 case DW_OP_breg14:
2293 case DW_OP_breg15:
2294 case DW_OP_breg16:
2295 case DW_OP_breg17:
2296 case DW_OP_breg18:
2297 case DW_OP_breg19:
2298 case DW_OP_breg20:
2299 case DW_OP_breg21:
2300 case DW_OP_breg22:
2301 case DW_OP_breg23:
2302 case DW_OP_breg24:
2303 case DW_OP_breg25:
2304 case DW_OP_breg26:
2305 case DW_OP_breg27:
2306 case DW_OP_breg28:
2307 case DW_OP_breg29:
2308 case DW_OP_breg30:
2309 case DW_OP_breg31:
2310 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2311 break;
2312 case DW_OP_regx:
2314 unsigned r = val1->v.val_unsigned;
2315 if (for_eh_or_skip >= 0)
2316 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 dw2_asm_output_data_uleb128 (r, NULL);
2321 break;
2322 case DW_OP_fbreg:
2323 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2324 break;
2325 case DW_OP_bregx:
2327 unsigned r = val1->v.val_unsigned;
2328 if (for_eh_or_skip >= 0)
2329 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2330 gcc_assert (size_of_uleb128 (r)
2331 == size_of_uleb128 (val1->v.val_unsigned));
2332 dw2_asm_output_data_uleb128 (r, NULL);
2333 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2335 break;
2336 case DW_OP_piece:
2337 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2338 break;
2339 case DW_OP_bit_piece:
2340 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2341 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2342 break;
2343 case DW_OP_deref_size:
2344 case DW_OP_xderef_size:
2345 dw2_asm_output_data (1, val1->v.val_int, NULL);
2346 break;
2348 case DW_OP_addr:
2349 if (loc->dtprel)
2351 if (targetm.asm_out.output_dwarf_dtprel)
2353 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2354 DWARF2_ADDR_SIZE,
2355 val1->v.val_addr);
2356 fputc ('\n', asm_out_file);
2358 else
2359 gcc_unreachable ();
2361 else
2363 #ifdef DWARF2_DEBUGGING_INFO
2364 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2365 #else
2366 gcc_unreachable ();
2367 #endif
2369 break;
2371 case DW_OP_GNU_addr_index:
2372 case DW_OP_addrx:
2373 case DW_OP_GNU_const_index:
2374 case DW_OP_constx:
2375 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2376 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2377 "(index into .debug_addr)");
2378 break;
2380 case DW_OP_call2:
2381 case DW_OP_call4:
2383 unsigned long die_offset
2384 = get_ref_die_offset (val1->v.val_die_ref.die);
2385 /* Make sure the offset has been computed and that we can encode it as
2386 an operand. */
2387 gcc_assert (die_offset > 0
2388 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2389 ? 0xffff
2390 : 0xffffffff));
2391 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2392 die_offset, NULL);
2394 break;
2396 case DW_OP_call_ref:
2397 case DW_OP_GNU_variable_value:
2399 char label[MAX_ARTIFICIAL_LABEL_BYTES
2400 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2401 gcc_assert (val1->val_class == dw_val_class_die_ref);
2402 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2403 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2405 break;
2407 case DW_OP_implicit_pointer:
2408 case DW_OP_GNU_implicit_pointer:
2410 char label[MAX_ARTIFICIAL_LABEL_BYTES
2411 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2412 gcc_assert (val1->val_class == dw_val_class_die_ref);
2413 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2414 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2415 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2417 break;
2419 case DW_OP_entry_value:
2420 case DW_OP_GNU_entry_value:
2421 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2422 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2423 break;
2425 case DW_OP_const_type:
2426 case DW_OP_GNU_const_type:
2428 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2429 gcc_assert (o);
2430 dw2_asm_output_data_uleb128 (o, NULL);
2431 switch (val2->val_class)
2433 case dw_val_class_const:
2434 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2435 dw2_asm_output_data (1, l, NULL);
2436 dw2_asm_output_data (l, val2->v.val_int, NULL);
2437 break;
2438 case dw_val_class_vec:
2440 unsigned int elt_size = val2->v.val_vec.elt_size;
2441 unsigned int len = val2->v.val_vec.length;
2442 unsigned int i;
2443 unsigned char *p;
2445 l = len * elt_size;
2446 dw2_asm_output_data (1, l, NULL);
2447 if (elt_size > sizeof (HOST_WIDE_INT))
2449 elt_size /= 2;
2450 len *= 2;
2452 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2453 i < len;
2454 i++, p += elt_size)
2455 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2456 "fp or vector constant word %u", i);
2458 break;
2459 case dw_val_class_const_double:
2461 unsigned HOST_WIDE_INT first, second;
2462 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2464 dw2_asm_output_data (1, 2 * l, NULL);
2465 if (WORDS_BIG_ENDIAN)
2467 first = val2->v.val_double.high;
2468 second = val2->v.val_double.low;
2470 else
2472 first = val2->v.val_double.low;
2473 second = val2->v.val_double.high;
2475 dw2_asm_output_data (l, first, NULL);
2476 dw2_asm_output_data (l, second, NULL);
2478 break;
2479 case dw_val_class_wide_int:
2481 int i;
2482 int len = get_full_len (*val2->v.val_wide);
2483 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2485 dw2_asm_output_data (1, len * l, NULL);
2486 if (WORDS_BIG_ENDIAN)
2487 for (i = len - 1; i >= 0; --i)
2488 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2489 else
2490 for (i = 0; i < len; ++i)
2491 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2493 break;
2494 default:
2495 gcc_unreachable ();
2498 break;
2499 case DW_OP_regval_type:
2500 case DW_OP_GNU_regval_type:
2502 unsigned r = val1->v.val_unsigned;
2503 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2504 gcc_assert (o);
2505 if (for_eh_or_skip >= 0)
2507 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2508 gcc_assert (size_of_uleb128 (r)
2509 == size_of_uleb128 (val1->v.val_unsigned));
2511 dw2_asm_output_data_uleb128 (r, NULL);
2512 dw2_asm_output_data_uleb128 (o, NULL);
2514 break;
2515 case DW_OP_deref_type:
2516 case DW_OP_GNU_deref_type:
2518 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2519 gcc_assert (o);
2520 dw2_asm_output_data (1, val1->v.val_int, NULL);
2521 dw2_asm_output_data_uleb128 (o, NULL);
2523 break;
2524 case DW_OP_convert:
2525 case DW_OP_reinterpret:
2526 case DW_OP_GNU_convert:
2527 case DW_OP_GNU_reinterpret:
2528 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2529 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2530 else
2532 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2533 gcc_assert (o);
2534 dw2_asm_output_data_uleb128 (o, NULL);
2536 break;
2538 case DW_OP_GNU_parameter_ref:
2540 unsigned long o;
2541 gcc_assert (val1->val_class == dw_val_class_die_ref);
2542 o = get_ref_die_offset (val1->v.val_die_ref.die);
2543 dw2_asm_output_data (4, o, NULL);
2545 break;
2547 default:
2548 /* Other codes have no operands. */
2549 break;
2553 /* Output a sequence of location operations.
2554 The for_eh_or_skip parameter controls whether register numbers are
2555 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2556 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2557 info). This should be suppressed for the cases that have not been converted
2558 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2560 void
2561 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2563 for (; loc != NULL; loc = loc->dw_loc_next)
2565 enum dwarf_location_atom opc = loc->dw_loc_opc;
2566 /* Output the opcode. */
2567 if (for_eh_or_skip >= 0
2568 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2570 unsigned r = (opc - DW_OP_breg0);
2571 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2572 gcc_assert (r <= 31);
2573 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2575 else if (for_eh_or_skip >= 0
2576 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2578 unsigned r = (opc - DW_OP_reg0);
2579 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2580 gcc_assert (r <= 31);
2581 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2584 dw2_asm_output_data (1, opc,
2585 "%s", dwarf_stack_op_name (opc));
2587 /* Output the operand(s) (if any). */
2588 output_loc_operands (loc, for_eh_or_skip);
2592 /* Output location description stack opcode's operands (if any).
2593 The output is single bytes on a line, suitable for .cfi_escape. */
2595 static void
2596 output_loc_operands_raw (dw_loc_descr_ref loc)
2598 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2599 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2601 switch (loc->dw_loc_opc)
2603 case DW_OP_addr:
2604 case DW_OP_GNU_addr_index:
2605 case DW_OP_addrx:
2606 case DW_OP_GNU_const_index:
2607 case DW_OP_constx:
2608 case DW_OP_implicit_value:
2609 /* We cannot output addresses in .cfi_escape, only bytes. */
2610 gcc_unreachable ();
2612 case DW_OP_const1u:
2613 case DW_OP_const1s:
2614 case DW_OP_pick:
2615 case DW_OP_deref_size:
2616 case DW_OP_xderef_size:
2617 fputc (',', asm_out_file);
2618 dw2_asm_output_data_raw (1, val1->v.val_int);
2619 break;
2621 case DW_OP_const2u:
2622 case DW_OP_const2s:
2623 fputc (',', asm_out_file);
2624 dw2_asm_output_data_raw (2, val1->v.val_int);
2625 break;
2627 case DW_OP_const4u:
2628 case DW_OP_const4s:
2629 fputc (',', asm_out_file);
2630 dw2_asm_output_data_raw (4, val1->v.val_int);
2631 break;
2633 case DW_OP_const8u:
2634 case DW_OP_const8s:
2635 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2636 fputc (',', asm_out_file);
2637 dw2_asm_output_data_raw (8, val1->v.val_int);
2638 break;
2640 case DW_OP_skip:
2641 case DW_OP_bra:
2643 int offset;
2645 gcc_assert (val1->val_class == dw_val_class_loc);
2646 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_raw (2, offset);
2651 break;
2653 case DW_OP_regx:
2655 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2656 gcc_assert (size_of_uleb128 (r)
2657 == size_of_uleb128 (val1->v.val_unsigned));
2658 fputc (',', asm_out_file);
2659 dw2_asm_output_data_uleb128_raw (r);
2661 break;
2663 case DW_OP_constu:
2664 case DW_OP_plus_uconst:
2665 case DW_OP_piece:
2666 fputc (',', asm_out_file);
2667 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2668 break;
2670 case DW_OP_bit_piece:
2671 fputc (',', asm_out_file);
2672 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2673 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2674 break;
2676 case DW_OP_consts:
2677 case DW_OP_breg0:
2678 case DW_OP_breg1:
2679 case DW_OP_breg2:
2680 case DW_OP_breg3:
2681 case DW_OP_breg4:
2682 case DW_OP_breg5:
2683 case DW_OP_breg6:
2684 case DW_OP_breg7:
2685 case DW_OP_breg8:
2686 case DW_OP_breg9:
2687 case DW_OP_breg10:
2688 case DW_OP_breg11:
2689 case DW_OP_breg12:
2690 case DW_OP_breg13:
2691 case DW_OP_breg14:
2692 case DW_OP_breg15:
2693 case DW_OP_breg16:
2694 case DW_OP_breg17:
2695 case DW_OP_breg18:
2696 case DW_OP_breg19:
2697 case DW_OP_breg20:
2698 case DW_OP_breg21:
2699 case DW_OP_breg22:
2700 case DW_OP_breg23:
2701 case DW_OP_breg24:
2702 case DW_OP_breg25:
2703 case DW_OP_breg26:
2704 case DW_OP_breg27:
2705 case DW_OP_breg28:
2706 case DW_OP_breg29:
2707 case DW_OP_breg30:
2708 case DW_OP_breg31:
2709 case DW_OP_fbreg:
2710 fputc (',', asm_out_file);
2711 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2712 break;
2714 case DW_OP_bregx:
2716 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2717 gcc_assert (size_of_uleb128 (r)
2718 == size_of_uleb128 (val1->v.val_unsigned));
2719 fputc (',', asm_out_file);
2720 dw2_asm_output_data_uleb128_raw (r);
2721 fputc (',', asm_out_file);
2722 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2724 break;
2726 case DW_OP_implicit_pointer:
2727 case DW_OP_entry_value:
2728 case DW_OP_const_type:
2729 case DW_OP_regval_type:
2730 case DW_OP_deref_type:
2731 case DW_OP_convert:
2732 case DW_OP_reinterpret:
2733 case DW_OP_GNU_implicit_pointer:
2734 case DW_OP_GNU_entry_value:
2735 case DW_OP_GNU_const_type:
2736 case DW_OP_GNU_regval_type:
2737 case DW_OP_GNU_deref_type:
2738 case DW_OP_GNU_convert:
2739 case DW_OP_GNU_reinterpret:
2740 case DW_OP_GNU_parameter_ref:
2741 gcc_unreachable ();
2742 break;
2744 default:
2745 /* Other codes have no operands. */
2746 break;
2750 void
2751 output_loc_sequence_raw (dw_loc_descr_ref loc)
2753 while (1)
2755 enum dwarf_location_atom opc = loc->dw_loc_opc;
2756 /* Output the opcode. */
2757 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2759 unsigned r = (opc - DW_OP_breg0);
2760 r = DWARF2_FRAME_REG_OUT (r, 1);
2761 gcc_assert (r <= 31);
2762 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2764 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2766 unsigned r = (opc - DW_OP_reg0);
2767 r = DWARF2_FRAME_REG_OUT (r, 1);
2768 gcc_assert (r <= 31);
2769 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2771 /* Output the opcode. */
2772 fprintf (asm_out_file, "%#x", opc);
2773 output_loc_operands_raw (loc);
2775 if (!loc->dw_loc_next)
2776 break;
2777 loc = loc->dw_loc_next;
2779 fputc (',', asm_out_file);
2783 static void
2784 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2786 if (regno <= 31)
2787 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2788 (DW_OP_breg0 + regno), 0, 0));
2789 else
2790 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2793 /* Build a dwarf location for a cfa_reg spanning multiple
2794 consecutive registers. */
2796 struct dw_loc_descr_node *
2797 build_span_loc (struct cfa_reg reg)
2799 struct dw_loc_descr_node *head = NULL;
2801 gcc_assert (reg.span_width > 0);
2802 gcc_assert (reg.span > 1);
2804 /* Start from the highest number register as it goes in the upper bits. */
2805 unsigned int regno = reg.reg + reg.span - 1;
2806 build_breg_loc (&head, regno);
2808 /* Deal with the remaining registers in the span. */
2809 for (int i = reg.span - 2; i >= 0; i--)
2811 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2812 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2813 regno--;
2814 build_breg_loc (&head, regno);
2815 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2817 return head;
2820 /* This function builds a dwarf location descriptor sequence from a
2821 dw_cfa_location, adding the given OFFSET to the result of the
2822 expression. */
2824 struct dw_loc_descr_node *
2825 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2827 struct dw_loc_descr_node *head, *tmp;
2829 offset += cfa->offset;
2831 if (cfa->reg.span > 1)
2833 head = build_span_loc (cfa->reg);
2835 if (maybe_ne (offset, 0))
2836 loc_descr_plus_const (&head, offset);
2838 else if (cfa->indirect)
2840 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2841 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2842 head->dw_loc_oprnd1.val_entry = NULL;
2843 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2844 add_loc_descr (&head, tmp);
2845 loc_descr_plus_const (&head, offset);
2847 else
2848 head = new_reg_loc_descr (cfa->reg.reg, offset);
2850 return head;
2853 /* This function builds a dwarf location descriptor sequence for
2854 the address at OFFSET from the CFA when stack is aligned to
2855 ALIGNMENT byte. */
2857 struct dw_loc_descr_node *
2858 build_cfa_aligned_loc (dw_cfa_location *cfa,
2859 poly_int64 offset, HOST_WIDE_INT alignment)
2861 struct dw_loc_descr_node *head;
2862 unsigned int dwarf_fp
2863 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2865 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2866 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2868 head = new_reg_loc_descr (dwarf_fp, 0);
2869 add_loc_descr (&head, int_loc_descriptor (alignment));
2870 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2871 loc_descr_plus_const (&head, offset);
2873 else
2874 head = new_reg_loc_descr (dwarf_fp, offset);
2875 return head;
2878 /* And now, the support for symbolic debugging information. */
2880 /* .debug_str support. */
2882 static void dwarf2out_init (const char *);
2883 static void dwarf2out_finish (const char *);
2884 static void dwarf2out_early_finish (const char *);
2885 static void dwarf2out_assembly_start (void);
2886 static void dwarf2out_define (unsigned int, const char *);
2887 static void dwarf2out_undef (unsigned int, const char *);
2888 static void dwarf2out_start_source_file (unsigned, const char *);
2889 static void dwarf2out_end_source_file (unsigned);
2890 static void dwarf2out_function_decl (tree);
2891 static void dwarf2out_begin_block (unsigned, unsigned);
2892 static void dwarf2out_end_block (unsigned, unsigned);
2893 static bool dwarf2out_ignore_block (const_tree);
2894 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2895 static void dwarf2out_early_global_decl (tree);
2896 static void dwarf2out_late_global_decl (tree);
2897 static void dwarf2out_type_decl (tree, int);
2898 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2899 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2900 dw_die_ref);
2901 static void dwarf2out_abstract_function (tree);
2902 static void dwarf2out_var_location (rtx_insn *);
2903 static void dwarf2out_inline_entry (tree);
2904 static void dwarf2out_size_function (tree);
2905 static void dwarf2out_begin_function (tree);
2906 static void dwarf2out_end_function (unsigned int);
2907 static void dwarf2out_register_main_translation_unit (tree unit);
2908 static void dwarf2out_set_name (tree, tree);
2909 static void dwarf2out_register_external_die (tree decl, const char *sym,
2910 unsigned HOST_WIDE_INT off);
2911 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2912 unsigned HOST_WIDE_INT *off);
2914 /* The debug hooks structure. */
2916 const struct gcc_debug_hooks dwarf2_debug_hooks =
2918 dwarf2out_init,
2919 dwarf2out_finish,
2920 dwarf2out_early_finish,
2921 dwarf2out_assembly_start,
2922 dwarf2out_define,
2923 dwarf2out_undef,
2924 dwarf2out_start_source_file,
2925 dwarf2out_end_source_file,
2926 dwarf2out_begin_block,
2927 dwarf2out_end_block,
2928 dwarf2out_ignore_block,
2929 dwarf2out_source_line,
2930 dwarf2out_set_ignored_loc,
2931 dwarf2out_begin_prologue,
2932 #if VMS_DEBUGGING_INFO
2933 dwarf2out_vms_end_prologue,
2934 dwarf2out_vms_begin_epilogue,
2935 #else
2936 debug_nothing_int_charstar,
2937 debug_nothing_int_charstar,
2938 #endif
2939 dwarf2out_end_epilogue,
2940 dwarf2out_begin_function,
2941 dwarf2out_end_function, /* end_function */
2942 dwarf2out_register_main_translation_unit,
2943 dwarf2out_function_decl, /* function_decl */
2944 dwarf2out_early_global_decl,
2945 dwarf2out_late_global_decl,
2946 dwarf2out_type_decl, /* type_decl */
2947 dwarf2out_imported_module_or_decl,
2948 dwarf2out_die_ref_for_decl,
2949 dwarf2out_register_external_die,
2950 debug_nothing_tree, /* deferred_inline_function */
2951 /* The DWARF 2 backend tries to reduce debugging bloat by not
2952 emitting the abstract description of inline functions until
2953 something tries to reference them. */
2954 dwarf2out_abstract_function, /* outlining_inline_function */
2955 debug_nothing_rtx_code_label, /* label */
2956 debug_nothing_int, /* handle_pch */
2957 dwarf2out_var_location,
2958 dwarf2out_inline_entry, /* inline_entry */
2959 dwarf2out_size_function, /* size_function */
2960 dwarf2out_switch_text_section,
2961 dwarf2out_set_name,
2962 1, /* start_end_main_source_file */
2963 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2966 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2968 dwarf2out_init,
2969 debug_nothing_charstar,
2970 debug_nothing_charstar,
2971 dwarf2out_assembly_start,
2972 debug_nothing_int_charstar,
2973 debug_nothing_int_charstar,
2974 debug_nothing_int_charstar,
2975 debug_nothing_int,
2976 debug_nothing_int_int, /* begin_block */
2977 debug_nothing_int_int, /* end_block */
2978 debug_true_const_tree, /* ignore_block */
2979 dwarf2out_source_line, /* source_line */
2980 debug_nothing_int_int_charstar, /* set_ignored_loc */
2981 debug_nothing_int_int_charstar, /* begin_prologue */
2982 debug_nothing_int_charstar, /* end_prologue */
2983 debug_nothing_int_charstar, /* begin_epilogue */
2984 debug_nothing_int_charstar, /* end_epilogue */
2985 debug_nothing_tree, /* begin_function */
2986 debug_nothing_int, /* end_function */
2987 debug_nothing_tree, /* register_main_translation_unit */
2988 debug_nothing_tree, /* function_decl */
2989 debug_nothing_tree, /* early_global_decl */
2990 debug_nothing_tree, /* late_global_decl */
2991 debug_nothing_tree_int, /* type_decl */
2992 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2993 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2994 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2995 debug_nothing_tree, /* deferred_inline_function */
2996 debug_nothing_tree, /* outlining_inline_function */
2997 debug_nothing_rtx_code_label, /* label */
2998 debug_nothing_int, /* handle_pch */
2999 debug_nothing_rtx_insn, /* var_location */
3000 debug_nothing_tree, /* inline_entry */
3001 debug_nothing_tree, /* size_function */
3002 debug_nothing_void, /* switch_text_section */
3003 debug_nothing_tree_tree, /* set_name */
3004 0, /* start_end_main_source_file */
3005 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
3008 /* NOTE: In the comments in this file, many references are made to
3009 "Debugging Information Entries". This term is abbreviated as `DIE'
3010 throughout the remainder of this file. */
3012 /* An internal representation of the DWARF output is built, and then
3013 walked to generate the DWARF debugging info. The walk of the internal
3014 representation is done after the entire program has been compiled.
3015 The types below are used to describe the internal representation. */
3017 /* Whether to put type DIEs into their own section .debug_types instead
3018 of making them part of the .debug_info section. Only supported for
3019 Dwarf V4 or higher and the user didn't disable them through
3020 -fno-debug-types-section. It is more efficient to put them in a
3021 separate comdat sections since the linker will then be able to
3022 remove duplicates. But not all tools support .debug_types sections
3023 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3024 it is DW_UT_type unit type in .debug_info section. For late LTO
3025 debug there should be almost no types emitted so avoid enabling
3026 -fdebug-types-section there. */
3028 #define use_debug_types (dwarf_version >= 4 \
3029 && flag_debug_types_section \
3030 && !in_lto_p)
3032 /* Various DIE's use offsets relative to the beginning of the
3033 .debug_info section to refer to each other. */
3035 typedef long int dw_offset;
3037 struct comdat_type_node;
3039 /* The entries in the line_info table more-or-less mirror the opcodes
3040 that are used in the real dwarf line table. Arrays of these entries
3041 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3042 supported. */
3044 enum dw_line_info_opcode {
3045 /* Emit DW_LNE_set_address; the operand is the label index. */
3046 LI_set_address,
3048 /* Emit a row to the matrix with the given line. This may be done
3049 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3050 special opcodes. */
3051 LI_set_line,
3053 /* Emit a DW_LNS_set_file. */
3054 LI_set_file,
3056 /* Emit a DW_LNS_set_column. */
3057 LI_set_column,
3059 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3060 LI_negate_stmt,
3062 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3063 LI_set_prologue_end,
3064 LI_set_epilogue_begin,
3066 /* Emit a DW_LNE_set_discriminator. */
3067 LI_set_discriminator,
3069 /* Output a Fixed Advance PC; the target PC is the label index; the
3070 base PC is the previous LI_adv_address or LI_set_address entry.
3071 We only use this when emitting debug views without assembler
3072 support, at explicit user request. Ideally, we should only use
3073 it when the offset might be zero but we can't tell: it's the only
3074 way to maybe change the PC without resetting the view number. */
3075 LI_adv_address
3078 typedef struct GTY(()) dw_line_info_struct {
3079 enum dw_line_info_opcode opcode;
3080 unsigned int val;
3081 } dw_line_info_entry;
3084 struct GTY(()) dw_line_info_table {
3085 /* The label that marks the end of this section. */
3086 const char *end_label;
3088 /* The values for the last row of the matrix, as collected in the table.
3089 These are used to minimize the changes to the next row. */
3090 unsigned int file_num;
3091 unsigned int line_num;
3092 unsigned int column_num;
3093 int discrim_num;
3094 bool is_stmt;
3095 bool in_use;
3097 /* This denotes the NEXT view number.
3099 If it is 0, it is known that the NEXT view will be the first view
3100 at the given PC.
3102 If it is -1, we're forcing the view number to be reset, e.g. at a
3103 function entry.
3105 The meaning of other nonzero values depends on whether we're
3106 computing views internally or leaving it for the assembler to do
3107 so. If we're emitting them internally, view denotes the view
3108 number since the last known advance of PC. If we're leaving it
3109 for the assembler, it denotes the LVU label number that we're
3110 going to ask the assembler to assign. */
3111 var_loc_view view;
3113 /* This counts the number of symbolic views emitted in this table
3114 since the latest view reset. Its max value, over all tables,
3115 sets symview_upper_bound. */
3116 var_loc_view symviews_since_reset;
3118 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3119 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3120 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3121 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3123 vec<dw_line_info_entry, va_gc> *entries;
3126 /* This is an upper bound for view numbers that the assembler may
3127 assign to symbolic views output in this translation. It is used to
3128 decide how big a field to use to represent view numbers in
3129 symview-classed attributes. */
3131 static var_loc_view symview_upper_bound;
3133 /* If we're keep track of location views and their reset points, and
3134 INSN is a reset point (i.e., it necessarily advances the PC), mark
3135 the next view in TABLE as reset. */
3137 static void
3138 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3140 if (!debug_internal_reset_location_views)
3141 return;
3143 /* Maybe turn (part of?) this test into a default target hook. */
3144 int reset = 0;
3146 if (targetm.reset_location_view)
3147 reset = targetm.reset_location_view (insn);
3149 if (reset)
3151 else if (JUMP_TABLE_DATA_P (insn))
3152 reset = 1;
3153 else if (GET_CODE (insn) == USE
3154 || GET_CODE (insn) == CLOBBER
3155 || GET_CODE (insn) == ASM_INPUT
3156 || asm_noperands (insn) >= 0)
3158 else if (get_attr_min_length (insn) > 0)
3159 reset = 1;
3161 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3162 RESET_NEXT_VIEW (table->view);
3165 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3166 The children of each node form a circular list linked by
3167 die_sib. die_child points to the node *before* the "first" child node. */
3169 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3170 union die_symbol_or_type_node
3172 const char * GTY ((tag ("0"))) die_symbol;
3173 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3175 GTY ((desc ("%0.comdat_type_p"))) die_id;
3176 vec<dw_attr_node, va_gc> *die_attr;
3177 dw_die_ref die_parent;
3178 dw_die_ref die_child;
3179 dw_die_ref die_sib;
3180 dw_die_ref die_definition; /* ref from a specification to its definition */
3181 dw_offset die_offset;
3182 unsigned long die_abbrev;
3183 int die_mark;
3184 unsigned int decl_id;
3185 enum dwarf_tag die_tag;
3186 /* Die is used and must not be pruned as unused. */
3187 BOOL_BITFIELD die_perennial_p : 1;
3188 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3189 /* For an external ref to die_symbol if die_offset contains an extra
3190 offset to that symbol. */
3191 BOOL_BITFIELD with_offset : 1;
3192 /* Whether this DIE was removed from the DIE tree, for example via
3193 prune_unused_types. We don't consider those present from the
3194 DIE lookup routines. */
3195 BOOL_BITFIELD removed : 1;
3196 /* Lots of spare bits. */
3198 die_node;
3200 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3201 static bool early_dwarf;
3202 static bool early_dwarf_finished;
3203 class set_early_dwarf {
3204 public:
3205 bool saved;
3206 set_early_dwarf () : saved(early_dwarf)
3208 gcc_assert (! early_dwarf_finished);
3209 early_dwarf = true;
3211 ~set_early_dwarf () { early_dwarf = saved; }
3214 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3215 #define FOR_EACH_CHILD(die, c, expr) do { \
3216 c = die->die_child; \
3217 if (c) do { \
3218 c = c->die_sib; \
3219 expr; \
3220 } while (c != die->die_child); \
3221 } while (0)
3223 /* The pubname structure */
3225 typedef struct GTY(()) pubname_struct {
3226 dw_die_ref die;
3227 const char *name;
3229 pubname_entry;
3232 struct GTY(()) dw_ranges {
3233 const char *label;
3234 /* If this is positive, it's a block number, otherwise it's a
3235 bitwise-negated index into dw_ranges_by_label. */
3236 int num;
3237 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3238 into .debug_rnglists section rather than .debug_rnglists.dwo
3239 for -gsplit-dwarf and DWARF >= 5. */
3240 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3241 /* Index for the range list for DW_FORM_rnglistx. */
3242 unsigned int idx : 31;
3243 /* True if this range might be possibly in a different section
3244 from previous entry. */
3245 unsigned int maybe_new_sec : 1;
3246 addr_table_entry *begin_entry;
3247 addr_table_entry *end_entry;
3250 /* A structure to hold a macinfo entry. */
3252 typedef struct GTY(()) macinfo_struct {
3253 unsigned char code;
3254 unsigned HOST_WIDE_INT lineno;
3255 const char *info;
3257 macinfo_entry;
3260 struct GTY(()) dw_ranges_by_label {
3261 const char *begin;
3262 const char *end;
3265 /* The comdat type node structure. */
3266 struct GTY(()) comdat_type_node
3268 dw_die_ref root_die;
3269 dw_die_ref type_die;
3270 dw_die_ref skeleton_die;
3271 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3272 comdat_type_node *next;
3275 /* A list of DIEs for which we can't determine ancestry (parent_die
3276 field) just yet. Later in dwarf2out_finish we will fill in the
3277 missing bits. */
3278 typedef struct GTY(()) limbo_die_struct {
3279 dw_die_ref die;
3280 /* The tree for which this DIE was created. We use this to
3281 determine ancestry later. */
3282 tree created_for;
3283 struct limbo_die_struct *next;
3285 limbo_die_node;
3287 typedef struct skeleton_chain_struct
3289 dw_die_ref old_die;
3290 dw_die_ref new_die;
3291 struct skeleton_chain_struct *parent;
3293 skeleton_chain_node;
3295 /* Define a macro which returns nonzero for a TYPE_DECL which was
3296 implicitly generated for a type.
3298 Note that, unlike the C front-end (which generates a NULL named
3299 TYPE_DECL node for each complete tagged type, each array type,
3300 and each function type node created) the C++ front-end generates
3301 a _named_ TYPE_DECL node for each tagged type node created.
3302 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3303 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3304 front-end, but for each type, tagged or not. */
3306 #define TYPE_DECL_IS_STUB(decl) \
3307 (DECL_NAME (decl) == NULL_TREE \
3308 || (DECL_ARTIFICIAL (decl) \
3309 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3310 /* This is necessary for stub decls that \
3311 appear in nested inline functions. */ \
3312 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3313 && (decl_ultimate_origin (decl) \
3314 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3316 /* Information concerning the compilation unit's programming
3317 language, and compiler version. */
3319 /* Fixed size portion of the DWARF compilation unit header. */
3320 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3321 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3322 + (dwarf_version >= 5 ? 4 : 3))
3324 /* Fixed size portion of the DWARF comdat type unit header. */
3325 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3326 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3327 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3329 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3330 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3331 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3333 /* Fixed size portion of public names info. */
3334 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3336 /* Fixed size portion of the address range info. */
3337 #define DWARF_ARANGES_HEADER_SIZE \
3338 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3339 DWARF2_ADDR_SIZE * 2) \
3340 - DWARF_INITIAL_LENGTH_SIZE)
3342 /* Size of padding portion in the address range info. It must be
3343 aligned to twice the pointer size. */
3344 #define DWARF_ARANGES_PAD_SIZE \
3345 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3346 DWARF2_ADDR_SIZE * 2) \
3347 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3349 /* Use assembler line directives if available. */
3350 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3351 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3352 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3353 #else
3354 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3355 #endif
3356 #endif
3358 /* Use assembler views in line directives if available. */
3359 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3360 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3361 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3362 #else
3363 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3364 #endif
3365 #endif
3367 /* Return true if GCC configure detected assembler support for .loc. */
3369 bool
3370 dwarf2out_default_as_loc_support (void)
3372 return DWARF2_ASM_LINE_DEBUG_INFO;
3373 #if (GCC_VERSION >= 3000)
3374 # undef DWARF2_ASM_LINE_DEBUG_INFO
3375 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3376 #endif
3379 /* Return true if GCC configure detected assembler support for views
3380 in .loc directives. */
3382 bool
3383 dwarf2out_default_as_locview_support (void)
3385 return DWARF2_ASM_VIEW_DEBUG_INFO;
3386 #if (GCC_VERSION >= 3000)
3387 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3388 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3389 #endif
3392 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3393 view computation, and it refers to a view identifier for which we
3394 will not emit a label because it is known to map to a view number
3395 zero. We won't allocate the bitmap if we're not using assembler
3396 support for location views, but we have to make the variable
3397 visible for GGC and for code that will be optimized out for lack of
3398 support but that's still parsed and compiled. We could abstract it
3399 out with macros, but it's not worth it. */
3400 static GTY(()) bitmap zero_view_p;
3402 /* Evaluate to TRUE iff N is known to identify the first location view
3403 at its PC. When not using assembler location view computation,
3404 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3405 and views label numbers recorded in it are the ones known to be
3406 zero. */
3407 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3408 || (N) == (var_loc_view)-1 \
3409 || (zero_view_p \
3410 && bitmap_bit_p (zero_view_p, (N))))
3412 /* Return true iff we're to emit .loc directives for the assembler to
3413 generate line number sections.
3415 When we're not emitting views, all we need from the assembler is
3416 support for .loc directives.
3418 If we are emitting views, we can only use the assembler's .loc
3419 support if it also supports views.
3421 When the compiler is emitting the line number programs and
3422 computing view numbers itself, it resets view numbers at known PC
3423 changes and counts from that, and then it emits view numbers as
3424 literal constants in locviewlists. There are cases in which the
3425 compiler is not sure about PC changes, e.g. when extra alignment is
3426 requested for a label. In these cases, the compiler may not reset
3427 the view counter, and the potential PC advance in the line number
3428 program will use an opcode that does not reset the view counter
3429 even if the PC actually changes, so that compiler and debug info
3430 consumer can keep view numbers in sync.
3432 When the compiler defers view computation to the assembler, it
3433 emits symbolic view numbers in locviewlists, with the exception of
3434 views known to be zero (forced resets, or reset after
3435 compiler-visible PC changes): instead of emitting symbols for
3436 these, we emit literal zero and assert the assembler agrees with
3437 the compiler's assessment. We could use symbolic views everywhere,
3438 instead of special-casing zero views, but then we'd be unable to
3439 optimize out locviewlists that contain only zeros. */
3441 static bool
3442 output_asm_line_debug_info (void)
3444 return (dwarf2out_as_loc_support
3445 && (dwarf2out_as_locview_support
3446 || !debug_variable_location_views));
3449 static bool asm_outputs_debug_line_str (void);
3451 /* Minimum line offset in a special line info. opcode.
3452 This value was chosen to give a reasonable range of values. */
3453 #define DWARF_LINE_BASE -10
3455 /* First special line opcode - leave room for the standard opcodes. */
3456 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3458 /* Range of line offsets in a special line info. opcode. */
3459 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3461 /* Flag that indicates the initial value of the is_stmt_start flag.
3462 In the present implementation, we do not mark any lines as
3463 the beginning of a source statement, because that information
3464 is not made available by the GCC front-end. */
3465 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3467 /* Maximum number of operations per instruction bundle. */
3468 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3469 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3470 #endif
3472 /* This location is used by calc_die_sizes() to keep track
3473 the offset of each DIE within the .debug_info section. */
3474 static unsigned long next_die_offset;
3476 /* Record the root of the DIE's built for the current compilation unit. */
3477 static GTY(()) dw_die_ref single_comp_unit_die;
3479 /* A list of type DIEs that have been separated into comdat sections. */
3480 static GTY(()) comdat_type_node *comdat_type_list;
3482 /* A list of CU DIEs that have been separated. */
3483 static GTY(()) limbo_die_node *cu_die_list;
3485 /* A list of DIEs with a NULL parent waiting to be relocated. */
3486 static GTY(()) limbo_die_node *limbo_die_list;
3488 /* A list of DIEs for which we may have to generate
3489 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3490 static GTY(()) limbo_die_node *deferred_asm_name;
3492 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3494 typedef const char *compare_type;
3496 static hashval_t hash (dwarf_file_data *);
3497 static bool equal (dwarf_file_data *, const char *);
3500 /* Filenames referenced by this compilation unit. */
3501 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3503 struct decl_die_hasher : ggc_ptr_hash<die_node>
3505 typedef tree compare_type;
3507 static hashval_t hash (die_node *);
3508 static bool equal (die_node *, tree);
3510 /* A hash table of references to DIE's that describe declarations.
3511 The key is a DECL_UID() which is a unique number identifying each decl. */
3512 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3514 struct GTY ((for_user)) variable_value_struct {
3515 unsigned int decl_id;
3516 vec<dw_die_ref, va_gc> *dies;
3519 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3521 typedef tree compare_type;
3523 static hashval_t hash (variable_value_struct *);
3524 static bool equal (variable_value_struct *, tree);
3526 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3527 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3528 DECL_CONTEXT of the referenced VAR_DECLs. */
3529 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3531 struct block_die_hasher : ggc_ptr_hash<die_struct>
3533 static hashval_t hash (die_struct *);
3534 static bool equal (die_struct *, die_struct *);
3537 /* A hash table of references to DIE's that describe COMMON blocks.
3538 The key is DECL_UID() ^ die_parent. */
3539 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3541 typedef struct GTY(()) die_arg_entry_struct {
3542 dw_die_ref die;
3543 tree arg;
3544 } die_arg_entry;
3547 /* Node of the variable location list. */
3548 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3549 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3550 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3551 in mode of the EXPR_LIST node and first EXPR_LIST operand
3552 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3553 location or NULL for padding. For larger bitsizes,
3554 mode is 0 and first operand is a CONCAT with bitsize
3555 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3556 NULL as second operand. */
3557 rtx GTY (()) loc;
3558 const char * GTY (()) label;
3559 struct var_loc_node * GTY (()) next;
3560 var_loc_view view;
3563 /* Variable location list. */
3564 struct GTY ((for_user)) var_loc_list_def {
3565 struct var_loc_node * GTY (()) first;
3567 /* Pointer to the last but one or last element of the
3568 chained list. If the list is empty, both first and
3569 last are NULL, if the list contains just one node
3570 or the last node certainly is not redundant, it points
3571 to the last node, otherwise points to the last but one.
3572 Do not mark it for GC because it is marked through the chain. */
3573 struct var_loc_node * GTY ((skip ("%h"))) last;
3575 /* Pointer to the last element before section switch,
3576 if NULL, either sections weren't switched or first
3577 is after section switch. */
3578 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3580 /* DECL_UID of the variable decl. */
3581 unsigned int decl_id;
3583 typedef struct var_loc_list_def var_loc_list;
3585 /* Call argument location list. */
3586 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3587 rtx GTY (()) call_arg_loc_note;
3588 const char * GTY (()) label;
3589 tree GTY (()) block;
3590 bool tail_call_p;
3591 rtx GTY (()) symbol_ref;
3592 struct call_arg_loc_node * GTY (()) next;
3596 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3598 typedef const_tree compare_type;
3600 static hashval_t hash (var_loc_list *);
3601 static bool equal (var_loc_list *, const_tree);
3604 /* Table of decl location linked lists. */
3605 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3607 /* Head and tail of call_arg_loc chain. */
3608 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3609 static struct call_arg_loc_node *call_arg_loc_last;
3611 /* Number of call sites in the current function. */
3612 static int call_site_count = -1;
3613 /* Number of tail call sites in the current function. */
3614 static int tail_call_site_count = -1;
3616 /* A cached location list. */
3617 struct GTY ((for_user)) cached_dw_loc_list_def {
3618 /* The DECL_UID of the decl that this entry describes. */
3619 unsigned int decl_id;
3621 /* The cached location list. */
3622 dw_loc_list_ref loc_list;
3624 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3626 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3629 typedef const_tree compare_type;
3631 static hashval_t hash (cached_dw_loc_list *);
3632 static bool equal (cached_dw_loc_list *, const_tree);
3635 /* Table of cached location lists. */
3636 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3638 /* A vector of references to DIE's that are uniquely identified by their tag,
3639 presence/absence of children DIE's, and list of attribute/value pairs. */
3640 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3642 /* A hash map to remember the stack usage for DWARF procedures. The value
3643 stored is the stack size difference between before the DWARF procedure
3644 invokation and after it returned. In other words, for a DWARF procedure
3645 that consumes N stack slots and that pushes M ones, this stores M - N. */
3646 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3648 /* A global counter for generating labels for line number data. */
3649 static unsigned int line_info_label_num;
3651 /* The current table to which we should emit line number information
3652 for the current function. This will be set up at the beginning of
3653 assembly for the function. */
3654 static GTY(()) dw_line_info_table *cur_line_info_table;
3656 /* The two default tables of line number info. */
3657 static GTY(()) dw_line_info_table *text_section_line_info;
3658 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3660 /* The set of all non-default tables of line number info. */
3661 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3663 /* A flag to tell pubnames/types export if there is an info section to
3664 refer to. */
3665 static bool info_section_emitted;
3667 /* A pointer to the base of a table that contains a list of publicly
3668 accessible names. */
3669 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3671 /* A pointer to the base of a table that contains a list of publicly
3672 accessible types. */
3673 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3675 /* A pointer to the base of a table that contains a list of macro
3676 defines/undefines (and file start/end markers). */
3677 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3679 /* True if .debug_macinfo or .debug_macros section is going to be
3680 emitted. */
3681 #define have_macinfo \
3682 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3683 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3684 && !macinfo_table->is_empty ())
3686 /* Vector of dies for which we should generate .debug_ranges info. */
3687 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3689 /* Vector of pairs of labels referenced in ranges_table. */
3690 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3692 /* Whether we have location lists that need outputting */
3693 static GTY(()) bool have_location_lists;
3695 /* Unique label counter. */
3696 static GTY(()) unsigned int loclabel_num;
3698 /* Unique label counter for point-of-call tables. */
3699 static GTY(()) unsigned int poc_label_num;
3701 /* The last file entry emitted by maybe_emit_file(). */
3702 static GTY(()) struct dwarf_file_data * last_emitted_file;
3704 /* Number of internal labels generated by gen_internal_sym(). */
3705 static GTY(()) int label_num;
3707 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3709 /* Instances of generic types for which we need to generate debug
3710 info that describe their generic parameters and arguments. That
3711 generation needs to happen once all types are properly laid out so
3712 we do it at the end of compilation. */
3713 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3715 /* Offset from the "steady-state frame pointer" to the frame base,
3716 within the current function. */
3717 static poly_int64 frame_pointer_fb_offset;
3718 static bool frame_pointer_fb_offset_valid;
3720 static vec<dw_die_ref> base_types;
3722 /* Flags to represent a set of attribute classes for attributes that represent
3723 a scalar value (bounds, pointers, ...). */
3724 enum dw_scalar_form
3726 dw_scalar_form_constant = 0x01,
3727 dw_scalar_form_exprloc = 0x02,
3728 dw_scalar_form_reference = 0x04
3731 /* Forward declarations for functions defined in this file. */
3733 static int is_pseudo_reg (const_rtx);
3734 static tree type_main_variant (tree);
3735 static int is_tagged_type (const_tree);
3736 static const char *dwarf_tag_name (unsigned);
3737 static const char *dwarf_attr_name (unsigned);
3738 static const char *dwarf_form_name (unsigned);
3739 static tree decl_ultimate_origin (const_tree);
3740 static tree decl_class_context (tree);
3741 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3742 static inline unsigned int AT_index (dw_attr_node *);
3743 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3744 static inline unsigned AT_flag (dw_attr_node *);
3745 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3746 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3747 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3748 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3749 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3750 unsigned int, unsigned char *);
3751 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3752 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3753 static inline const char *AT_string (dw_attr_node *);
3754 static enum dwarf_form AT_string_form (dw_attr_node *);
3755 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3756 static void add_AT_specification (dw_die_ref, dw_die_ref);
3757 static inline dw_die_ref AT_ref (dw_attr_node *);
3758 static inline int AT_ref_external (dw_attr_node *);
3759 static inline void set_AT_ref_external (dw_attr_node *, int);
3760 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3761 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3762 dw_loc_list_ref);
3763 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3764 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3765 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3766 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3767 static void remove_addr_table_entry (addr_table_entry *);
3768 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3769 static inline rtx AT_addr (dw_attr_node *);
3770 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3771 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3772 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3773 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3774 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3775 unsigned long, bool);
3776 static inline const char *AT_lbl (dw_attr_node *);
3777 static const char *get_AT_low_pc (dw_die_ref);
3778 static bool is_c (void);
3779 static bool is_cxx (void);
3780 static bool is_cxx (const_tree);
3781 static bool is_fortran (void);
3782 static bool is_ada (void);
3783 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3784 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3785 static void add_child_die (dw_die_ref, dw_die_ref);
3786 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3787 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3788 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3789 static void equate_type_number_to_die (tree, dw_die_ref);
3790 static var_loc_list *lookup_decl_loc (const_tree);
3791 static void equate_decl_number_to_die (tree, dw_die_ref);
3792 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3793 static void print_spaces (FILE *);
3794 static void print_die (dw_die_ref, FILE *);
3795 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3796 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3797 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3798 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3799 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3800 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3801 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3802 struct md5_ctx *, int *);
3803 struct checksum_attributes;
3804 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3805 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3806 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3807 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3808 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3809 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3810 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3811 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3812 static int is_type_die (dw_die_ref);
3813 static inline bool is_template_instantiation (dw_die_ref);
3814 static int is_declaration_die (dw_die_ref);
3815 static int should_move_die_to_comdat (dw_die_ref);
3816 static dw_die_ref clone_as_declaration (dw_die_ref);
3817 static dw_die_ref clone_die (dw_die_ref);
3818 static dw_die_ref clone_tree (dw_die_ref);
3819 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3820 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3821 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3822 static dw_die_ref generate_skeleton (dw_die_ref);
3823 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3824 dw_die_ref,
3825 dw_die_ref);
3826 static void break_out_comdat_types (dw_die_ref);
3827 static void copy_decls_for_unworthy_types (dw_die_ref);
3829 static void add_sibling_attributes (dw_die_ref);
3830 static void output_location_lists (dw_die_ref);
3831 static int constant_size (unsigned HOST_WIDE_INT);
3832 static unsigned long size_of_die (dw_die_ref);
3833 static void calc_die_sizes (dw_die_ref);
3834 static void calc_base_type_die_sizes (void);
3835 static void mark_dies (dw_die_ref);
3836 static void unmark_dies (dw_die_ref);
3837 static void unmark_all_dies (dw_die_ref);
3838 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3839 static unsigned long size_of_aranges (void);
3840 static enum dwarf_form value_format (dw_attr_node *);
3841 static void output_value_format (dw_attr_node *);
3842 static void output_abbrev_section (void);
3843 static void output_die_abbrevs (unsigned long, dw_die_ref);
3844 static void output_die (dw_die_ref);
3845 static void output_compilation_unit_header (enum dwarf_unit_type);
3846 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3847 static void output_comdat_type_unit (comdat_type_node *, bool);
3848 static const char *dwarf2_name (tree, int);
3849 static void add_pubname (tree, dw_die_ref);
3850 static void add_enumerator_pubname (const char *, dw_die_ref);
3851 static void add_pubname_string (const char *, dw_die_ref);
3852 static void add_pubtype (tree, dw_die_ref);
3853 static void output_pubnames (vec<pubname_entry, va_gc> *);
3854 static void output_aranges (void);
3855 static unsigned int add_ranges (const_tree, bool = false);
3856 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3857 bool *, bool);
3858 static void output_ranges (void);
3859 static dw_line_info_table *new_line_info_table (void);
3860 static void output_line_info (bool);
3861 static void output_file_names (void);
3862 static int is_base_type (tree);
3863 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3864 static int decl_quals (const_tree);
3865 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3866 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3867 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3868 static unsigned int debugger_reg_number (const_rtx);
3869 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3870 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3871 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3872 enum var_init_status);
3873 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3874 enum var_init_status);
3875 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3876 enum var_init_status);
3877 static int is_based_loc (const_rtx);
3878 static bool resolve_one_addr (rtx *);
3879 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3880 enum var_init_status);
3881 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3882 enum var_init_status);
3883 struct loc_descr_context;
3884 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3885 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3886 static dw_loc_list_ref loc_list_from_tree (tree, int,
3887 struct loc_descr_context *);
3888 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3889 struct loc_descr_context *);
3890 static tree field_type (const_tree);
3891 static unsigned int simple_type_align_in_bits (const_tree);
3892 static unsigned int simple_decl_align_in_bits (const_tree);
3893 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3894 struct vlr_context;
3895 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3896 HOST_WIDE_INT *);
3897 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3898 dw_loc_list_ref);
3899 static void add_data_member_location_attribute (dw_die_ref, tree,
3900 struct vlr_context *);
3901 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3902 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3903 static void insert_wide_int (const wide_int &, unsigned char *, int);
3904 static unsigned insert_float (const_rtx, unsigned char *);
3905 static rtx rtl_for_decl_location (tree);
3906 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3907 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3908 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3909 static void add_desc_attribute (dw_die_ref, tree);
3910 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3911 static void add_comp_dir_attribute (dw_die_ref);
3912 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3913 struct loc_descr_context *);
3914 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3915 struct loc_descr_context *);
3916 static void add_subscript_info (dw_die_ref, tree, bool);
3917 static void add_byte_size_attribute (dw_die_ref, tree);
3918 static void add_alignment_attribute (dw_die_ref, tree);
3919 static void add_bit_offset_attribute (dw_die_ref, tree);
3920 static void add_bit_size_attribute (dw_die_ref, tree);
3921 static void add_prototyped_attribute (dw_die_ref, tree);
3922 static void add_abstract_origin_attribute (dw_die_ref, tree);
3923 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3924 static void add_src_coords_attributes (dw_die_ref, tree);
3925 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3926 static void add_discr_value (dw_die_ref, dw_discr_value *);
3927 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3928 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3929 static dw_die_ref scope_die_for (tree, dw_die_ref);
3930 static inline int local_scope_p (dw_die_ref);
3931 static inline int class_scope_p (dw_die_ref);
3932 static inline int class_or_namespace_scope_p (dw_die_ref);
3933 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3934 static void add_calling_convention_attribute (dw_die_ref, tree);
3935 static const char *type_tag (const_tree);
3936 static tree member_declared_type (const_tree);
3937 #if 0
3938 static const char *decl_start_label (tree);
3939 #endif
3940 static void gen_array_type_die (tree, dw_die_ref);
3941 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3942 #if 0
3943 static void gen_entry_point_die (tree, dw_die_ref);
3944 #endif
3945 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3946 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3947 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3948 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3949 static void gen_formal_types_die (tree, dw_die_ref);
3950 static void gen_subprogram_die (tree, dw_die_ref);
3951 static void gen_variable_die (tree, tree, dw_die_ref);
3952 static void gen_const_die (tree, dw_die_ref);
3953 static void gen_label_die (tree, dw_die_ref);
3954 static void gen_lexical_block_die (tree, dw_die_ref);
3955 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3956 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3957 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3958 static dw_die_ref gen_compile_unit_die (const char *);
3959 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3960 static void gen_member_die (tree, dw_die_ref);
3961 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3962 enum debug_info_usage);
3963 static void gen_subroutine_type_die (tree, dw_die_ref);
3964 static void gen_typedef_die (tree, dw_die_ref);
3965 static void gen_type_die (tree, dw_die_ref);
3966 static void gen_block_die (tree, dw_die_ref);
3967 static void decls_for_scope (tree, dw_die_ref, bool = true);
3968 static bool is_naming_typedef_decl (const_tree);
3969 static inline dw_die_ref get_context_die (tree);
3970 static void gen_namespace_die (tree, dw_die_ref);
3971 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3972 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3973 static dw_die_ref force_decl_die (tree);
3974 static dw_die_ref force_type_die (tree);
3975 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3976 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3977 static struct dwarf_file_data * lookup_filename (const char *);
3978 static void retry_incomplete_types (void);
3979 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3980 static void gen_generic_params_dies (tree);
3981 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3982 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3983 static void splice_child_die (dw_die_ref, dw_die_ref);
3984 static int file_info_cmp (const void *, const void *);
3985 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3986 const char *, var_loc_view, const char *);
3987 static void output_loc_list (dw_loc_list_ref);
3988 static char *gen_internal_sym (const char *);
3989 static bool want_pubnames (void);
3991 static void prune_unmark_dies (dw_die_ref);
3992 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3993 static void prune_unused_types_mark (dw_die_ref, int);
3994 static void prune_unused_types_walk (dw_die_ref);
3995 static void prune_unused_types_walk_attribs (dw_die_ref);
3996 static void prune_unused_types_prune (dw_die_ref);
3997 static void prune_unused_types (void);
3998 static int maybe_emit_file (struct dwarf_file_data *fd);
3999 static inline const char *AT_vms_delta1 (dw_attr_node *);
4000 static inline const char *AT_vms_delta2 (dw_attr_node *);
4001 #if VMS_DEBUGGING_INFO
4002 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4003 const char *, const char *);
4004 #endif
4005 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4006 static void gen_remaining_tmpl_value_param_die_attribute (void);
4007 static bool generic_type_p (tree);
4008 static void schedule_generic_params_dies_gen (tree t);
4009 static void gen_scheduled_generic_parms_dies (void);
4010 static void resolve_variable_values (void);
4012 static const char *comp_dir_string (void);
4014 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
4016 /* enum for tracking thread-local variables whose address is really an offset
4017 relative to the TLS pointer, which will need link-time relocation, but will
4018 not need relocation by the DWARF consumer. */
4020 enum dtprel_bool
4022 dtprel_false = 0,
4023 dtprel_true = 1
4026 /* Return the operator to use for an address of a variable. For dtprel_true, we
4027 use DW_OP_const*. For regular variables, which need both link-time
4028 relocation and consumer-level relocation (e.g., to account for shared objects
4029 loaded at a random address), we use DW_OP_addr*. */
4031 static inline enum dwarf_location_atom
4032 dw_addr_op (enum dtprel_bool dtprel)
4034 if (dtprel == dtprel_true)
4035 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4036 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4037 else
4038 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4041 /* Return a pointer to a newly allocated address location description. If
4042 dwarf_split_debug_info is true, then record the address with the appropriate
4043 relocation. */
4044 static inline dw_loc_descr_ref
4045 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4047 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4049 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4050 ref->dw_loc_oprnd1.v.val_addr = addr;
4051 ref->dtprel = dtprel;
4052 if (dwarf_split_debug_info)
4053 ref->dw_loc_oprnd1.val_entry
4054 = add_addr_table_entry (addr,
4055 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4056 else
4057 ref->dw_loc_oprnd1.val_entry = NULL;
4059 return ref;
4062 /* Section names used to hold DWARF debugging information. */
4064 #ifndef DEBUG_INFO_SECTION
4065 #define DEBUG_INFO_SECTION ".debug_info"
4066 #endif
4067 #ifndef DEBUG_DWO_INFO_SECTION
4068 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4069 #endif
4070 #ifndef DEBUG_LTO_INFO_SECTION
4071 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4072 #endif
4073 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4074 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4075 #endif
4076 #ifndef DEBUG_ABBREV_SECTION
4077 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4078 #endif
4079 #ifndef DEBUG_LTO_ABBREV_SECTION
4080 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4081 #endif
4082 #ifndef DEBUG_DWO_ABBREV_SECTION
4083 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4084 #endif
4085 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4086 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4087 #endif
4088 #ifndef DEBUG_ARANGES_SECTION
4089 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4090 #endif
4091 #ifndef DEBUG_ADDR_SECTION
4092 #define DEBUG_ADDR_SECTION ".debug_addr"
4093 #endif
4094 #ifndef DEBUG_MACINFO_SECTION
4095 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4096 #endif
4097 #ifndef DEBUG_LTO_MACINFO_SECTION
4098 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4099 #endif
4100 #ifndef DEBUG_DWO_MACINFO_SECTION
4101 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4102 #endif
4103 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4104 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4105 #endif
4106 #ifndef DEBUG_MACRO_SECTION
4107 #define DEBUG_MACRO_SECTION ".debug_macro"
4108 #endif
4109 #ifndef DEBUG_LTO_MACRO_SECTION
4110 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4111 #endif
4112 #ifndef DEBUG_DWO_MACRO_SECTION
4113 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4114 #endif
4115 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4116 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4117 #endif
4118 #ifndef DEBUG_LINE_SECTION
4119 #define DEBUG_LINE_SECTION ".debug_line"
4120 #endif
4121 #ifndef DEBUG_LTO_LINE_SECTION
4122 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4123 #endif
4124 #ifndef DEBUG_DWO_LINE_SECTION
4125 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4126 #endif
4127 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4128 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4129 #endif
4130 #ifndef DEBUG_LOC_SECTION
4131 #define DEBUG_LOC_SECTION ".debug_loc"
4132 #endif
4133 #ifndef DEBUG_DWO_LOC_SECTION
4134 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4135 #endif
4136 #ifndef DEBUG_LOCLISTS_SECTION
4137 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4138 #endif
4139 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4140 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4141 #endif
4142 #ifndef DEBUG_PUBNAMES_SECTION
4143 #define DEBUG_PUBNAMES_SECTION \
4144 ((debug_generate_pub_sections == 2) \
4145 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4146 #endif
4147 #ifndef DEBUG_PUBTYPES_SECTION
4148 #define DEBUG_PUBTYPES_SECTION \
4149 ((debug_generate_pub_sections == 2) \
4150 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4151 #endif
4152 #ifndef DEBUG_STR_OFFSETS_SECTION
4153 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4154 #endif
4155 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4156 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4157 #endif
4158 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4159 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4160 #endif
4161 #ifndef DEBUG_STR_SECTION
4162 #define DEBUG_STR_SECTION ".debug_str"
4163 #endif
4164 #ifndef DEBUG_LTO_STR_SECTION
4165 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4166 #endif
4167 #ifndef DEBUG_STR_DWO_SECTION
4168 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4169 #endif
4170 #ifndef DEBUG_LTO_STR_DWO_SECTION
4171 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4172 #endif
4173 #ifndef DEBUG_RANGES_SECTION
4174 #define DEBUG_RANGES_SECTION ".debug_ranges"
4175 #endif
4176 #ifndef DEBUG_RNGLISTS_SECTION
4177 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4178 #endif
4179 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4180 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4181 #endif
4182 #ifndef DEBUG_LINE_STR_SECTION
4183 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4184 #endif
4185 #ifndef DEBUG_LTO_LINE_STR_SECTION
4186 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4187 #endif
4189 /* Section flags for .debug_str section. */
4190 #define DEBUG_STR_SECTION_FLAGS \
4191 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4192 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4193 : SECTION_DEBUG)
4195 /* Section flags for .debug_str.dwo section. */
4196 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4198 /* Attribute used to refer to the macro section. */
4199 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4200 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4202 /* Labels we insert at beginning sections we can reference instead of
4203 the section names themselves. */
4205 #ifndef TEXT_SECTION_LABEL
4206 #define TEXT_SECTION_LABEL "Ltext"
4207 #endif
4208 #ifndef COLD_TEXT_SECTION_LABEL
4209 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4210 #endif
4211 #ifndef DEBUG_LINE_SECTION_LABEL
4212 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4213 #endif
4214 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4215 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4216 #endif
4217 #ifndef DEBUG_INFO_SECTION_LABEL
4218 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4219 #endif
4220 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4221 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4222 #endif
4223 #ifndef DEBUG_ABBREV_SECTION_LABEL
4224 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4225 #endif
4226 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4227 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4228 #endif
4229 #ifndef DEBUG_ADDR_SECTION_LABEL
4230 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4231 #endif
4232 #ifndef DEBUG_LOC_SECTION_LABEL
4233 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4234 #endif
4235 #ifndef DEBUG_RANGES_SECTION_LABEL
4236 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4237 #endif
4238 #ifndef DEBUG_MACINFO_SECTION_LABEL
4239 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4240 #endif
4241 #ifndef DEBUG_MACRO_SECTION_LABEL
4242 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4243 #endif
4244 #define SKELETON_COMP_DIE_ABBREV 1
4245 #define SKELETON_TYPE_DIE_ABBREV 2
4247 /* Definitions of defaults for formats and names of various special
4248 (artificial) labels which may be generated within this file (when the -g
4249 options is used and DWARF2_DEBUGGING_INFO is in effect.
4250 If necessary, these may be overridden from within the tm.h file, but
4251 typically, overriding these defaults is unnecessary. */
4253 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4254 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4255 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4256 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4258 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4266 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4269 #ifndef TEXT_END_LABEL
4270 #define TEXT_END_LABEL "Letext"
4271 #endif
4272 #ifndef COLD_END_LABEL
4273 #define COLD_END_LABEL "Letext_cold"
4274 #endif
4275 #ifndef BLOCK_BEGIN_LABEL
4276 #define BLOCK_BEGIN_LABEL "LBB"
4277 #endif
4278 #ifndef BLOCK_INLINE_ENTRY_LABEL
4279 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4280 #endif
4281 #ifndef BLOCK_END_LABEL
4282 #define BLOCK_END_LABEL "LBE"
4283 #endif
4284 #ifndef LINE_CODE_LABEL
4285 #define LINE_CODE_LABEL "LM"
4286 #endif
4289 /* Return the root of the DIE's built for the current compilation unit. */
4290 static dw_die_ref
4291 comp_unit_die (void)
4293 if (!single_comp_unit_die)
4294 single_comp_unit_die = gen_compile_unit_die (NULL);
4295 return single_comp_unit_die;
4298 /* We allow a language front-end to designate a function that is to be
4299 called to "demangle" any name before it is put into a DIE. */
4301 static const char *(*demangle_name_func) (const char *);
4303 void
4304 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4306 demangle_name_func = func;
4309 /* Test if rtl node points to a pseudo register. */
4311 static inline int
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 nonzero if the given type node represents a tagged type. */
4341 static inline int
4342 is_tagged_type (const_tree type)
4344 enum tree_code code = TREE_CODE (type);
4346 return (code == RECORD_TYPE || code == UNION_TYPE
4347 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4350 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4352 static void
4353 get_ref_die_offset_label (char *label, dw_die_ref ref)
4355 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4358 /* Return die_offset of a DIE reference to a base type. */
4360 static unsigned long int
4361 get_base_type_offset (dw_die_ref ref)
4363 if (ref->die_offset)
4364 return ref->die_offset;
4365 if (comp_unit_die ()->die_abbrev)
4367 calc_base_type_die_sizes ();
4368 gcc_assert (ref->die_offset);
4370 return ref->die_offset;
4373 /* Return die_offset of a DIE reference other than base type. */
4375 static unsigned long int
4376 get_ref_die_offset (dw_die_ref ref)
4378 gcc_assert (ref->die_offset);
4379 return ref->die_offset;
4382 /* Convert a DIE tag into its string name. */
4384 static const char *
4385 dwarf_tag_name (unsigned int tag)
4387 const char *name = get_DW_TAG_name (tag);
4389 if (name != NULL)
4390 return name;
4392 return "DW_TAG_<unknown>";
4395 /* Convert a DWARF attribute code into its string name. */
4397 static const char *
4398 dwarf_attr_name (unsigned int attr)
4400 const char *name;
4402 switch (attr)
4404 #if VMS_DEBUGGING_INFO
4405 case DW_AT_HP_prologue:
4406 return "DW_AT_HP_prologue";
4407 #else
4408 case DW_AT_MIPS_loop_unroll_factor:
4409 return "DW_AT_MIPS_loop_unroll_factor";
4410 #endif
4412 #if VMS_DEBUGGING_INFO
4413 case DW_AT_HP_epilogue:
4414 return "DW_AT_HP_epilogue";
4415 #else
4416 case DW_AT_MIPS_stride:
4417 return "DW_AT_MIPS_stride";
4418 #endif
4421 name = get_DW_AT_name (attr);
4423 if (name != NULL)
4424 return name;
4426 return "DW_AT_<unknown>";
4429 /* Convert a DWARF value form code into its string name. */
4431 static const char *
4432 dwarf_form_name (unsigned int form)
4434 const char *name = get_DW_FORM_name (form);
4436 if (name != NULL)
4437 return name;
4439 return "DW_FORM_<unknown>";
4442 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4443 instance of an inlined instance of a decl which is local to an inline
4444 function, so we have to trace all of the way back through the origin chain
4445 to find out what sort of node actually served as the original seed for the
4446 given block. */
4448 static tree
4449 decl_ultimate_origin (const_tree decl)
4451 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4452 return NULL_TREE;
4454 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4455 we're trying to output the abstract instance of this function. */
4456 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4457 return NULL_TREE;
4459 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4460 most distant ancestor, this should never happen. */
4461 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4463 return DECL_ABSTRACT_ORIGIN (decl);
4466 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4467 of a virtual function may refer to a base class, so we check the 'this'
4468 parameter. */
4470 static tree
4471 decl_class_context (tree decl)
4473 tree context = NULL_TREE;
4475 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4476 context = DECL_CONTEXT (decl);
4477 else
4478 context = TYPE_MAIN_VARIANT
4479 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4481 if (context && !TYPE_P (context))
4482 context = NULL_TREE;
4484 return context;
4487 /* Add an attribute/value pair to a DIE. */
4489 static inline void
4490 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4492 /* Maybe this should be an assert? */
4493 if (die == NULL)
4494 return;
4496 if (flag_checking)
4498 /* Check we do not add duplicate attrs. Can't use get_AT here
4499 because that recurses to the specification/abstract origin DIE. */
4500 dw_attr_node *a;
4501 unsigned ix;
4502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4503 gcc_assert (a->dw_attr != attr->dw_attr);
4506 vec_safe_reserve (die->die_attr, 1);
4507 vec_safe_push (die->die_attr, *attr);
4510 enum dw_val_class
4511 AT_class (dw_attr_node *a)
4513 return a->dw_attr_val.val_class;
4516 /* Return the index for any attribute that will be referenced with a
4517 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4518 indices are stored in dw_attr_val.v.val_str for reference counting
4519 pruning. */
4521 static inline unsigned int
4522 AT_index (dw_attr_node *a)
4524 if (AT_class (a) == dw_val_class_str)
4525 return a->dw_attr_val.v.val_str->index;
4526 else if (a->dw_attr_val.val_entry != NULL)
4527 return a->dw_attr_val.val_entry->index;
4528 return NOT_INDEXED;
4531 /* Add a flag value attribute to a DIE. */
4533 static inline void
4534 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_flag;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_flag = flag;
4542 add_dwarf_attr (die, &attr);
4545 static inline unsigned
4546 AT_flag (dw_attr_node *a)
4548 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4549 return a->dw_attr_val.v.val_flag;
4552 /* Add a signed integer attribute value to a DIE. */
4554 static inline void
4555 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4557 dw_attr_node attr;
4559 attr.dw_attr = attr_kind;
4560 attr.dw_attr_val.val_class = dw_val_class_const;
4561 attr.dw_attr_val.val_entry = NULL;
4562 attr.dw_attr_val.v.val_int = int_val;
4563 add_dwarf_attr (die, &attr);
4566 HOST_WIDE_INT
4567 AT_int (dw_attr_node *a)
4569 gcc_assert (a && (AT_class (a) == dw_val_class_const
4570 || AT_class (a) == dw_val_class_const_implicit));
4571 return a->dw_attr_val.v.val_int;
4574 /* Add an unsigned integer attribute value to a DIE. */
4576 static inline void
4577 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4578 unsigned HOST_WIDE_INT unsigned_val)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4586 add_dwarf_attr (die, &attr);
4589 unsigned HOST_WIDE_INT
4590 AT_unsigned (dw_attr_node *a)
4592 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4593 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4594 return a->dw_attr_val.v.val_unsigned;
4597 /* Add an unsigned wide integer attribute value to a DIE. */
4599 static inline void
4600 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4601 const wide_int& w)
4603 dw_attr_node attr;
4605 attr.dw_attr = attr_kind;
4606 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4607 attr.dw_attr_val.val_entry = NULL;
4608 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4609 *attr.dw_attr_val.v.val_wide = w;
4610 add_dwarf_attr (die, &attr);
4613 /* Add an unsigned double integer attribute value to a DIE. */
4615 static inline void
4616 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4617 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4619 dw_attr_node attr;
4621 attr.dw_attr = attr_kind;
4622 attr.dw_attr_val.val_class = dw_val_class_const_double;
4623 attr.dw_attr_val.val_entry = NULL;
4624 attr.dw_attr_val.v.val_double.high = high;
4625 attr.dw_attr_val.v.val_double.low = low;
4626 add_dwarf_attr (die, &attr);
4629 /* Add a floating point attribute value to a DIE and return it. */
4631 static inline void
4632 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4633 unsigned int length, unsigned int elt_size, unsigned char *array)
4635 dw_attr_node attr;
4637 attr.dw_attr = attr_kind;
4638 attr.dw_attr_val.val_class = dw_val_class_vec;
4639 attr.dw_attr_val.val_entry = NULL;
4640 attr.dw_attr_val.v.val_vec.length = length;
4641 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4642 attr.dw_attr_val.v.val_vec.array = array;
4643 add_dwarf_attr (die, &attr);
4646 /* Add an 8-byte data attribute value to a DIE. */
4648 static inline void
4649 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4650 unsigned char data8[8])
4652 dw_attr_node attr;
4654 attr.dw_attr = attr_kind;
4655 attr.dw_attr_val.val_class = dw_val_class_data8;
4656 attr.dw_attr_val.val_entry = NULL;
4657 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4658 add_dwarf_attr (die, &attr);
4661 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4662 dwarf_split_debug_info, address attributes in dies destined for the
4663 final executable have force_direct set to avoid using indexed
4664 references. */
4666 static inline void
4667 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4668 bool force_direct)
4670 dw_attr_node attr;
4671 char * lbl_id;
4673 lbl_id = xstrdup (lbl_low);
4674 attr.dw_attr = DW_AT_low_pc;
4675 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4676 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4677 if (dwarf_split_debug_info && !force_direct)
4678 attr.dw_attr_val.val_entry
4679 = add_addr_table_entry (lbl_id, ate_kind_label);
4680 else
4681 attr.dw_attr_val.val_entry = NULL;
4682 add_dwarf_attr (die, &attr);
4684 attr.dw_attr = DW_AT_high_pc;
4685 if (dwarf_version < 4)
4686 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4687 else
4688 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4689 lbl_id = xstrdup (lbl_high);
4690 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4691 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4692 && dwarf_split_debug_info && !force_direct)
4693 attr.dw_attr_val.val_entry
4694 = add_addr_table_entry (lbl_id, ate_kind_label);
4695 else
4696 attr.dw_attr_val.val_entry = NULL;
4697 add_dwarf_attr (die, &attr);
4700 /* Hash and equality functions for debug_str_hash. */
4702 hashval_t
4703 indirect_string_hasher::hash (indirect_string_node *x)
4705 return htab_hash_string (x->str);
4708 bool
4709 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4711 return strcmp (x1->str, x2) == 0;
4714 /* Add STR to the given string hash table. */
4716 static struct indirect_string_node *
4717 find_AT_string_in_table (const char *str,
4718 hash_table<indirect_string_hasher> *table,
4719 enum insert_option insert = INSERT)
4721 struct indirect_string_node *node;
4723 indirect_string_node **slot
4724 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4725 if (*slot == NULL)
4727 node = ggc_cleared_alloc<indirect_string_node> ();
4728 node->str = ggc_strdup (str);
4729 *slot = node;
4731 else
4732 node = *slot;
4734 node->refcount++;
4735 return node;
4738 /* Add STR to the indirect string hash table. */
4740 static struct indirect_string_node *
4741 find_AT_string (const char *str, enum insert_option insert = INSERT)
4743 if (! debug_str_hash)
4744 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4746 return find_AT_string_in_table (str, debug_str_hash, insert);
4749 /* Add a string attribute value to a DIE. */
4751 static inline void
4752 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4754 dw_attr_node attr;
4755 struct indirect_string_node *node;
4757 node = find_AT_string (str);
4759 attr.dw_attr = attr_kind;
4760 attr.dw_attr_val.val_class = dw_val_class_str;
4761 attr.dw_attr_val.val_entry = NULL;
4762 attr.dw_attr_val.v.val_str = node;
4763 add_dwarf_attr (die, &attr);
4766 static inline const char *
4767 AT_string (dw_attr_node *a)
4769 gcc_assert (a && AT_class (a) == dw_val_class_str);
4770 return a->dw_attr_val.v.val_str->str;
4773 /* Call this function directly to bypass AT_string_form's logic to put
4774 the string inline in the die. */
4776 static void
4777 set_indirect_string (struct indirect_string_node *node)
4779 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4780 /* Already indirect is a no op. */
4781 if (node->form == DW_FORM_strp
4782 || node->form == DW_FORM_line_strp
4783 || node->form == dwarf_FORM (DW_FORM_strx))
4785 gcc_assert (node->label);
4786 return;
4788 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4789 ++dw2_string_counter;
4790 node->label = xstrdup (label);
4792 if (!dwarf_split_debug_info)
4794 node->form = DW_FORM_strp;
4795 node->index = NOT_INDEXED;
4797 else
4799 node->form = dwarf_FORM (DW_FORM_strx);
4800 node->index = NO_INDEX_ASSIGNED;
4804 /* A helper function for dwarf2out_finish, called to reset indirect
4805 string decisions done for early LTO dwarf output before fat object
4806 dwarf output. */
4809 reset_indirect_string (indirect_string_node **h, void *)
4811 struct indirect_string_node *node = *h;
4812 if (node->form == DW_FORM_strp
4813 || node->form == DW_FORM_line_strp
4814 || node->form == dwarf_FORM (DW_FORM_strx))
4816 free (node->label);
4817 node->label = NULL;
4818 node->form = (dwarf_form) 0;
4819 node->index = 0;
4821 return 1;
4824 /* Add a string representing a file or filepath attribute value to a DIE. */
4826 static inline void
4827 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4828 const char *str)
4830 if (! asm_outputs_debug_line_str ())
4831 add_AT_string (die, attr_kind, str);
4832 else
4834 dw_attr_node attr;
4835 struct indirect_string_node *node;
4837 if (!debug_line_str_hash)
4838 debug_line_str_hash
4839 = hash_table<indirect_string_hasher>::create_ggc (10);
4841 node = find_AT_string_in_table (str, debug_line_str_hash);
4842 set_indirect_string (node);
4843 node->form = DW_FORM_line_strp;
4845 attr.dw_attr = attr_kind;
4846 attr.dw_attr_val.val_class = dw_val_class_str;
4847 attr.dw_attr_val.val_entry = NULL;
4848 attr.dw_attr_val.v.val_str = node;
4849 add_dwarf_attr (die, &attr);
4853 /* Find out whether a string should be output inline in DIE
4854 or out-of-line in .debug_str section. */
4856 static enum dwarf_form
4857 find_string_form (struct indirect_string_node *node)
4859 unsigned int len;
4861 if (node->form)
4862 return node->form;
4864 len = strlen (node->str) + 1;
4866 /* If the string is shorter or equal to the size of the reference, it is
4867 always better to put it inline. */
4868 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4869 return node->form = DW_FORM_string;
4871 /* If we cannot expect the linker to merge strings in .debug_str
4872 section, only put it into .debug_str if it is worth even in this
4873 single module. */
4874 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4875 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4876 && (len - dwarf_offset_size) * node->refcount <= len))
4877 return node->form = DW_FORM_string;
4879 set_indirect_string (node);
4881 return node->form;
4884 /* Find out whether the string referenced from the attribute should be
4885 output inline in DIE or out-of-line in .debug_str section. */
4887 static enum dwarf_form
4888 AT_string_form (dw_attr_node *a)
4890 gcc_assert (a && AT_class (a) == dw_val_class_str);
4891 return find_string_form (a->dw_attr_val.v.val_str);
4894 /* Add a DIE reference attribute value to a DIE. */
4896 static inline void
4897 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4899 dw_attr_node attr;
4900 gcc_checking_assert (targ_die != NULL);
4902 /* With LTO we can end up trying to reference something we didn't create
4903 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4904 if (targ_die == NULL)
4905 return;
4907 attr.dw_attr = attr_kind;
4908 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4909 attr.dw_attr_val.val_entry = NULL;
4910 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4911 attr.dw_attr_val.v.val_die_ref.external = 0;
4912 add_dwarf_attr (die, &attr);
4915 /* Change DIE reference REF to point to NEW_DIE instead. */
4917 static inline void
4918 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4920 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4921 ref->dw_attr_val.v.val_die_ref.die = new_die;
4922 ref->dw_attr_val.v.val_die_ref.external = 0;
4925 /* Add an AT_specification attribute to a DIE, and also make the back
4926 pointer from the specification to the definition. */
4928 static inline void
4929 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4931 add_AT_die_ref (die, DW_AT_specification, targ_die);
4932 gcc_assert (!targ_die->die_definition);
4933 targ_die->die_definition = die;
4936 static inline dw_die_ref
4937 AT_ref (dw_attr_node *a)
4939 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4940 return a->dw_attr_val.v.val_die_ref.die;
4943 static inline int
4944 AT_ref_external (dw_attr_node *a)
4946 if (a && AT_class (a) == dw_val_class_die_ref)
4947 return a->dw_attr_val.v.val_die_ref.external;
4949 return 0;
4952 static inline void
4953 set_AT_ref_external (dw_attr_node *a, int i)
4955 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4956 a->dw_attr_val.v.val_die_ref.external = i;
4959 /* Add a location description attribute value to a DIE. */
4961 static inline void
4962 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4964 dw_attr_node attr;
4966 attr.dw_attr = attr_kind;
4967 attr.dw_attr_val.val_class = dw_val_class_loc;
4968 attr.dw_attr_val.val_entry = NULL;
4969 attr.dw_attr_val.v.val_loc = loc;
4970 add_dwarf_attr (die, &attr);
4973 dw_loc_descr_ref
4974 AT_loc (dw_attr_node *a)
4976 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4977 return a->dw_attr_val.v.val_loc;
4980 static inline void
4981 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4983 dw_attr_node attr;
4985 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4986 return;
4988 attr.dw_attr = attr_kind;
4989 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4990 attr.dw_attr_val.val_entry = NULL;
4991 attr.dw_attr_val.v.val_loc_list = loc_list;
4992 add_dwarf_attr (die, &attr);
4993 have_location_lists = true;
4996 static inline dw_loc_list_ref
4997 AT_loc_list (dw_attr_node *a)
4999 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5000 return a->dw_attr_val.v.val_loc_list;
5003 /* Add a view list attribute to DIE. It must have a DW_AT_location
5004 attribute, because the view list complements the location list. */
5006 static inline void
5007 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5009 dw_attr_node attr;
5011 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5012 return;
5014 attr.dw_attr = attr_kind;
5015 attr.dw_attr_val.val_class = dw_val_class_view_list;
5016 attr.dw_attr_val.val_entry = NULL;
5017 attr.dw_attr_val.v.val_view_list = die;
5018 add_dwarf_attr (die, &attr);
5019 gcc_checking_assert (get_AT (die, DW_AT_location));
5020 gcc_assert (have_location_lists);
5023 /* Return a pointer to the location list referenced by the attribute.
5024 If the named attribute is a view list, look up the corresponding
5025 DW_AT_location attribute and return its location list. */
5027 static inline dw_loc_list_ref *
5028 AT_loc_list_ptr (dw_attr_node *a)
5030 gcc_assert (a);
5031 switch (AT_class (a))
5033 case dw_val_class_loc_list:
5034 return &a->dw_attr_val.v.val_loc_list;
5035 case dw_val_class_view_list:
5037 dw_attr_node *l;
5038 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5039 if (!l)
5040 return NULL;
5041 gcc_checking_assert (l + 1 == a);
5042 return AT_loc_list_ptr (l);
5044 default:
5045 gcc_unreachable ();
5049 /* Return the location attribute value associated with a view list
5050 attribute value. */
5052 static inline dw_val_node *
5053 view_list_to_loc_list_val_node (dw_val_node *val)
5055 gcc_assert (val->val_class == dw_val_class_view_list);
5056 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5057 if (!loc)
5058 return NULL;
5059 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5060 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5061 return &loc->dw_attr_val;
5064 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5066 static hashval_t hash (addr_table_entry *);
5067 static bool equal (addr_table_entry *, addr_table_entry *);
5070 /* Table of entries into the .debug_addr section. */
5072 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5074 /* Hash an address_table_entry. */
5076 hashval_t
5077 addr_hasher::hash (addr_table_entry *a)
5079 inchash::hash hstate;
5080 switch (a->kind)
5082 case ate_kind_rtx:
5083 hstate.add_int (0);
5084 break;
5085 case ate_kind_rtx_dtprel:
5086 hstate.add_int (1);
5087 break;
5088 case ate_kind_label:
5089 return htab_hash_string (a->addr.label);
5090 default:
5091 gcc_unreachable ();
5093 inchash::add_rtx (a->addr.rtl, hstate);
5094 return hstate.end ();
5097 /* Determine equality for two address_table_entries. */
5099 bool
5100 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5102 if (a1->kind != a2->kind)
5103 return 0;
5104 switch (a1->kind)
5106 case ate_kind_rtx:
5107 case ate_kind_rtx_dtprel:
5108 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5109 case ate_kind_label:
5110 return strcmp (a1->addr.label, a2->addr.label) == 0;
5111 default:
5112 gcc_unreachable ();
5116 /* Initialize an addr_table_entry. */
5118 void
5119 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5121 e->kind = kind;
5122 switch (kind)
5124 case ate_kind_rtx:
5125 case ate_kind_rtx_dtprel:
5126 e->addr.rtl = (rtx) addr;
5127 break;
5128 case ate_kind_label:
5129 e->addr.label = (char *) addr;
5130 break;
5132 e->refcount = 0;
5133 e->index = NO_INDEX_ASSIGNED;
5136 /* Add attr to the address table entry to the table. Defer setting an
5137 index until output time. */
5139 static addr_table_entry *
5140 add_addr_table_entry (void *addr, enum ate_kind kind)
5142 addr_table_entry *node;
5143 addr_table_entry finder;
5145 gcc_assert (dwarf_split_debug_info);
5146 if (! addr_index_table)
5147 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5148 init_addr_table_entry (&finder, kind, addr);
5149 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5151 if (*slot == HTAB_EMPTY_ENTRY)
5153 node = ggc_cleared_alloc<addr_table_entry> ();
5154 init_addr_table_entry (node, kind, addr);
5155 *slot = node;
5157 else
5158 node = *slot;
5160 node->refcount++;
5161 return node;
5164 /* Remove an entry from the addr table by decrementing its refcount.
5165 Strictly, decrementing the refcount would be enough, but the
5166 assertion that the entry is actually in the table has found
5167 bugs. */
5169 static void
5170 remove_addr_table_entry (addr_table_entry *entry)
5172 gcc_assert (dwarf_split_debug_info && addr_index_table);
5173 /* After an index is assigned, the table is frozen. */
5174 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5175 entry->refcount--;
5178 /* Given a location list, remove all addresses it refers to from the
5179 address_table. */
5181 static void
5182 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5184 for (; descr; descr = descr->dw_loc_next)
5185 if (descr->dw_loc_oprnd1.val_entry != NULL)
5187 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5188 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5192 /* A helper function for dwarf2out_finish called through
5193 htab_traverse. Assign an addr_table_entry its index. All entries
5194 must be collected into the table when this function is called,
5195 because the indexing code relies on htab_traverse to traverse nodes
5196 in the same order for each run. */
5199 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5201 addr_table_entry *node = *h;
5203 /* Don't index unreferenced nodes. */
5204 if (node->refcount == 0)
5205 return 1;
5207 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5208 node->index = *index;
5209 *index += 1;
5211 return 1;
5214 /* Return the tag of a given DIE. */
5216 enum dwarf_tag
5217 dw_get_die_tag (dw_die_ref die)
5219 return die->die_tag;
5222 /* Return a reference to the children list of a given DIE. */
5224 dw_die_ref
5225 dw_get_die_child (dw_die_ref die)
5227 return die->die_child;
5230 /* Return a reference to the sibling of a given DIE. */
5232 dw_die_ref
5233 dw_get_die_sib (dw_die_ref die)
5235 return die->die_sib;
5238 /* Add an address constant attribute value to a DIE. When using
5239 dwarf_split_debug_info, address attributes in dies destined for the
5240 final executable should be direct references--setting the parameter
5241 force_direct ensures this behavior. */
5243 static inline void
5244 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5245 bool force_direct)
5247 dw_attr_node attr;
5249 attr.dw_attr = attr_kind;
5250 attr.dw_attr_val.val_class = dw_val_class_addr;
5251 attr.dw_attr_val.v.val_addr = addr;
5252 if (dwarf_split_debug_info && !force_direct)
5253 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5254 else
5255 attr.dw_attr_val.val_entry = NULL;
5256 add_dwarf_attr (die, &attr);
5259 /* Get the RTX from to an address DIE attribute. */
5261 static inline rtx
5262 AT_addr (dw_attr_node *a)
5264 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5265 return a->dw_attr_val.v.val_addr;
5268 /* Add a file attribute value to a DIE. */
5270 static inline void
5271 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5272 struct dwarf_file_data *fd)
5274 dw_attr_node attr;
5276 attr.dw_attr = attr_kind;
5277 attr.dw_attr_val.val_class = dw_val_class_file;
5278 attr.dw_attr_val.val_entry = NULL;
5279 attr.dw_attr_val.v.val_file = fd;
5280 add_dwarf_attr (die, &attr);
5283 /* Get the dwarf_file_data from a file DIE attribute. */
5285 static inline struct dwarf_file_data *
5286 AT_file (dw_attr_node *a)
5288 gcc_assert (a && (AT_class (a) == dw_val_class_file
5289 || AT_class (a) == dw_val_class_file_implicit));
5290 return a->dw_attr_val.v.val_file;
5293 #if VMS_DEBUGGING_INFO
5294 /* Add a vms delta attribute value to a DIE. */
5296 static inline void
5297 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5298 const char *lbl1, const char *lbl2)
5300 dw_attr_node attr;
5302 attr.dw_attr = attr_kind;
5303 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5304 attr.dw_attr_val.val_entry = NULL;
5305 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5306 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5307 add_dwarf_attr (die, &attr);
5309 #endif
5311 /* Add a symbolic view identifier attribute value to a DIE. */
5313 static inline void
5314 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5315 const char *view_label)
5317 dw_attr_node attr;
5319 attr.dw_attr = attr_kind;
5320 attr.dw_attr_val.val_class = dw_val_class_symview;
5321 attr.dw_attr_val.val_entry = NULL;
5322 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5323 add_dwarf_attr (die, &attr);
5326 /* Add a label identifier attribute value to a DIE. */
5328 static inline void
5329 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5330 const char *lbl_id)
5332 dw_attr_node attr;
5334 attr.dw_attr = attr_kind;
5335 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5336 attr.dw_attr_val.val_entry = NULL;
5337 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5338 if (dwarf_split_debug_info)
5339 attr.dw_attr_val.val_entry
5340 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5341 ate_kind_label);
5342 add_dwarf_attr (die, &attr);
5345 /* Add a section offset attribute value to a DIE, an offset into the
5346 debug_line section. */
5348 static inline void
5349 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5350 const char *label)
5352 dw_attr_node attr;
5354 attr.dw_attr = attr_kind;
5355 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5356 attr.dw_attr_val.val_entry = NULL;
5357 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5358 add_dwarf_attr (die, &attr);
5361 /* Add a section offset attribute value to a DIE, an offset into the
5362 debug_macinfo section. */
5364 static inline void
5365 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5366 const char *label)
5368 dw_attr_node attr;
5370 attr.dw_attr = attr_kind;
5371 attr.dw_attr_val.val_class = dw_val_class_macptr;
5372 attr.dw_attr_val.val_entry = NULL;
5373 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5374 add_dwarf_attr (die, &attr);
5377 /* Add a range_list attribute value to a DIE. When using
5378 dwarf_split_debug_info, address attributes in dies destined for the
5379 final executable should be direct references--setting the parameter
5380 force_direct ensures this behavior. */
5382 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5383 #define RELOCATED_OFFSET (NULL)
5385 static void
5386 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5387 long unsigned int offset, bool force_direct)
5389 dw_attr_node attr;
5391 attr.dw_attr = attr_kind;
5392 attr.dw_attr_val.val_class = dw_val_class_range_list;
5393 /* For the range_list attribute, use val_entry to store whether the
5394 offset should follow split-debug-info or normal semantics. This
5395 value is read in output_range_list_offset. */
5396 if (dwarf_split_debug_info && !force_direct)
5397 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5398 else
5399 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5400 attr.dw_attr_val.v.val_offset = offset;
5401 add_dwarf_attr (die, &attr);
5404 /* Return the start label of a delta attribute. */
5406 static inline const char *
5407 AT_vms_delta1 (dw_attr_node *a)
5409 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5410 return a->dw_attr_val.v.val_vms_delta.lbl1;
5413 /* Return the end label of a delta attribute. */
5415 static inline const char *
5416 AT_vms_delta2 (dw_attr_node *a)
5418 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5419 return a->dw_attr_val.v.val_vms_delta.lbl2;
5422 static inline const char *
5423 AT_lbl (dw_attr_node *a)
5425 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5426 || AT_class (a) == dw_val_class_lineptr
5427 || AT_class (a) == dw_val_class_macptr
5428 || AT_class (a) == dw_val_class_loclistsptr
5429 || AT_class (a) == dw_val_class_high_pc));
5430 return a->dw_attr_val.v.val_lbl_id;
5433 /* Get the attribute of type attr_kind. */
5435 dw_attr_node *
5436 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5438 dw_attr_node *a;
5439 unsigned ix;
5440 dw_die_ref spec = NULL;
5442 if (! die)
5443 return NULL;
5445 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5446 if (a->dw_attr == attr_kind)
5447 return a;
5448 else if (a->dw_attr == DW_AT_specification
5449 || a->dw_attr == DW_AT_abstract_origin)
5450 spec = AT_ref (a);
5452 if (spec)
5453 return get_AT (spec, attr_kind);
5455 return NULL;
5458 /* Returns the parent of the declaration of DIE. */
5460 static dw_die_ref
5461 get_die_parent (dw_die_ref die)
5463 dw_die_ref t;
5465 if (!die)
5466 return NULL;
5468 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5469 || (t = get_AT_ref (die, DW_AT_specification)))
5470 die = t;
5472 return die->die_parent;
5475 /* Return the "low pc" attribute value, typically associated with a subprogram
5476 DIE. Return null if the "low pc" attribute is either not present, or if it
5477 cannot be represented as an assembler label identifier. */
5479 static inline const char *
5480 get_AT_low_pc (dw_die_ref die)
5482 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5484 return a ? AT_lbl (a) : NULL;
5487 /* Return the value of the string attribute designated by ATTR_KIND, or
5488 NULL if it is not present. */
5490 const char *
5491 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5493 dw_attr_node *a = get_AT (die, attr_kind);
5495 return a ? AT_string (a) : NULL;
5498 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5499 if it is not present. */
5502 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5504 dw_attr_node *a = get_AT (die, attr_kind);
5506 return a ? AT_flag (a) : 0;
5509 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5510 if it is not present. */
5512 unsigned
5513 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5515 dw_attr_node *a = get_AT (die, attr_kind);
5517 return a ? AT_unsigned (a) : 0;
5520 dw_die_ref
5521 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5523 dw_attr_node *a = get_AT (die, attr_kind);
5525 return a ? AT_ref (a) : NULL;
5528 struct dwarf_file_data *
5529 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5531 dw_attr_node *a = get_AT (die, attr_kind);
5533 return a ? AT_file (a) : NULL;
5536 /* Return TRUE if the language is C. */
5538 static inline bool
5539 is_c (void)
5541 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5543 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5544 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5549 /* Return TRUE if the language is C++. */
5551 static inline bool
5552 is_cxx (void)
5554 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5556 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5557 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5560 /* Return TRUE if DECL was created by the C++ frontend. */
5562 static bool
5563 is_cxx (const_tree decl)
5565 if (in_lto_p)
5567 const_tree context = get_ultimate_context (decl);
5568 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5569 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5571 return is_cxx ();
5574 /* Return TRUE if the language is Fortran. */
5576 static inline bool
5577 is_fortran (void)
5579 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5581 return (lang == DW_LANG_Fortran77
5582 || lang == DW_LANG_Fortran90
5583 || lang == DW_LANG_Fortran95
5584 || lang == DW_LANG_Fortran03
5585 || lang == DW_LANG_Fortran08);
5588 static inline bool
5589 is_fortran (const_tree decl)
5591 if (in_lto_p)
5593 const_tree context = get_ultimate_context (decl);
5594 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5595 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5596 "GNU Fortran", 11) == 0
5597 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5598 "GNU F77") == 0);
5600 return is_fortran ();
5603 /* Return TRUE if the language is Rust.
5604 Note, returns FALSE for dwarf_version < 5 && dwarf_strict. */
5606 static inline bool
5607 is_rust ()
5609 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5611 return lang == DW_LANG_Rust;
5614 /* Return TRUE if the language is Ada. */
5616 static inline bool
5617 is_ada (void)
5619 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5621 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5624 /* Return TRUE if the language is D. */
5626 static inline bool
5627 is_dlang (void)
5629 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5631 return lang == DW_LANG_D;
5634 /* Remove the specified attribute if present. Return TRUE if removal
5635 was successful. */
5637 static bool
5638 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5640 dw_attr_node *a;
5641 unsigned ix;
5643 if (! die)
5644 return false;
5646 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5647 if (a->dw_attr == attr_kind)
5649 if (AT_class (a) == dw_val_class_str)
5650 if (a->dw_attr_val.v.val_str->refcount)
5651 a->dw_attr_val.v.val_str->refcount--;
5653 /* vec::ordered_remove should help reduce the number of abbrevs
5654 that are needed. */
5655 die->die_attr->ordered_remove (ix);
5656 return true;
5658 return false;
5661 /* Remove CHILD from its parent. PREV must have the property that
5662 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5664 static void
5665 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5667 gcc_assert (child->die_parent == prev->die_parent);
5668 gcc_assert (prev->die_sib == child);
5669 if (prev == child)
5671 gcc_assert (child->die_parent->die_child == child);
5672 prev = NULL;
5674 else
5675 prev->die_sib = child->die_sib;
5676 if (child->die_parent->die_child == child)
5677 child->die_parent->die_child = prev;
5678 child->die_sib = NULL;
5681 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5682 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5684 static void
5685 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5687 dw_die_ref parent = old_child->die_parent;
5689 gcc_assert (parent == prev->die_parent);
5690 gcc_assert (prev->die_sib == old_child);
5692 new_child->die_parent = parent;
5693 if (prev == old_child)
5695 gcc_assert (parent->die_child == old_child);
5696 new_child->die_sib = new_child;
5698 else
5700 prev->die_sib = new_child;
5701 new_child->die_sib = old_child->die_sib;
5703 if (old_child->die_parent->die_child == old_child)
5704 old_child->die_parent->die_child = new_child;
5705 old_child->die_sib = NULL;
5708 /* Move all children from OLD_PARENT to NEW_PARENT. */
5710 static void
5711 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5713 dw_die_ref c;
5714 new_parent->die_child = old_parent->die_child;
5715 old_parent->die_child = NULL;
5716 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5719 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5720 matches TAG. */
5722 static void
5723 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5725 dw_die_ref c;
5727 c = die->die_child;
5728 if (c) do {
5729 dw_die_ref prev = c;
5730 c = c->die_sib;
5731 while (c->die_tag == tag)
5733 remove_child_with_prev (c, prev);
5734 c->die_parent = NULL;
5735 /* Might have removed every child. */
5736 if (die->die_child == NULL)
5737 return;
5738 c = prev->die_sib;
5740 } while (c != die->die_child);
5743 /* Add a CHILD_DIE as the last child of DIE. */
5745 static void
5746 add_child_die (dw_die_ref die, dw_die_ref child_die)
5748 /* FIXME this should probably be an assert. */
5749 if (! die || ! child_die)
5750 return;
5751 gcc_assert (die != child_die);
5753 child_die->die_parent = die;
5754 if (die->die_child)
5756 child_die->die_sib = die->die_child->die_sib;
5757 die->die_child->die_sib = child_die;
5759 else
5760 child_die->die_sib = child_die;
5761 die->die_child = child_die;
5764 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5766 static void
5767 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5768 dw_die_ref after_die)
5770 gcc_assert (die
5771 && child_die
5772 && after_die
5773 && die->die_child
5774 && die != child_die);
5776 child_die->die_parent = die;
5777 child_die->die_sib = after_die->die_sib;
5778 after_die->die_sib = child_die;
5779 if (die->die_child == after_die)
5780 die->die_child = child_die;
5783 /* Unassociate CHILD from its parent, and make its parent be
5784 NEW_PARENT. */
5786 static void
5787 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5789 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5790 if (p->die_sib == child)
5792 remove_child_with_prev (child, p);
5793 break;
5795 add_child_die (new_parent, child);
5798 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5799 is the specification, to the end of PARENT's list of children.
5800 This is done by removing and re-adding it. */
5802 static void
5803 splice_child_die (dw_die_ref parent, dw_die_ref child)
5805 /* We want the declaration DIE from inside the class, not the
5806 specification DIE at toplevel. */
5807 if (child->die_parent != parent)
5809 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5811 if (tmp)
5812 child = tmp;
5815 gcc_assert (child->die_parent == parent
5816 || (child->die_parent
5817 == get_AT_ref (parent, DW_AT_specification)));
5819 reparent_child (child, parent);
5822 /* Create and return a new die with TAG_VALUE as tag. */
5824 dw_die_ref
5825 new_die_raw (enum dwarf_tag tag_value)
5827 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5828 die->die_tag = tag_value;
5829 return die;
5832 /* Create and return a new die with a parent of PARENT_DIE. If
5833 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5834 associated tree T must be supplied to determine parenthood
5835 later. */
5837 static inline dw_die_ref
5838 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5840 dw_die_ref die = new_die_raw (tag_value);
5842 if (parent_die != NULL)
5843 add_child_die (parent_die, die);
5844 else
5846 limbo_die_node *limbo_node;
5848 /* No DIEs created after early dwarf should end up in limbo,
5849 because the limbo list should not persist past LTO
5850 streaming. */
5851 if (tag_value != DW_TAG_compile_unit
5852 /* These are allowed because they're generated while
5853 breaking out COMDAT units late. */
5854 && tag_value != DW_TAG_type_unit
5855 && tag_value != DW_TAG_skeleton_unit
5856 && !early_dwarf
5857 /* Allow nested functions to live in limbo because they will
5858 only temporarily live there, as decls_for_scope will fix
5859 them up. */
5860 && (TREE_CODE (t) != FUNCTION_DECL
5861 || !decl_function_context (t))
5862 /* Same as nested functions above but for types. Types that
5863 are local to a function will be fixed in
5864 decls_for_scope. */
5865 && (!RECORD_OR_UNION_TYPE_P (t)
5866 || !TYPE_CONTEXT (t)
5867 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5868 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5869 especially in the ltrans stage, but once we implement LTO
5870 dwarf streaming, we should remove this exception. */
5871 && !in_lto_p)
5873 fprintf (stderr, "symbol ended up in limbo too late:");
5874 debug_generic_stmt (t);
5875 gcc_unreachable ();
5878 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5879 limbo_node->die = die;
5880 limbo_node->created_for = t;
5881 limbo_node->next = limbo_die_list;
5882 limbo_die_list = limbo_node;
5885 return die;
5888 /* Return the DIE associated with the given type specifier. */
5890 dw_die_ref
5891 lookup_type_die (tree type)
5893 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5894 if (die && die->removed)
5896 TYPE_SYMTAB_DIE (type) = NULL;
5897 return NULL;
5899 return die;
5902 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5903 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5904 anonymous type instead the one of the naming typedef. */
5906 static inline dw_die_ref
5907 strip_naming_typedef (tree type, dw_die_ref type_die)
5909 if (type
5910 && TREE_CODE (type) == RECORD_TYPE
5911 && type_die
5912 && type_die->die_tag == DW_TAG_typedef
5913 && is_naming_typedef_decl (TYPE_NAME (type)))
5914 type_die = get_AT_ref (type_die, DW_AT_type);
5915 return type_die;
5918 /* Like lookup_type_die, but if type is an anonymous type named by a
5919 typedef[1], return the DIE of the anonymous type instead the one of
5920 the naming typedef. This is because in gen_typedef_die, we did
5921 equate the anonymous struct named by the typedef with the DIE of
5922 the naming typedef. So by default, lookup_type_die on an anonymous
5923 struct yields the DIE of the naming typedef.
5925 [1]: Read the comment of is_naming_typedef_decl to learn about what
5926 a naming typedef is. */
5928 static inline dw_die_ref
5929 lookup_type_die_strip_naming_typedef (tree type)
5931 dw_die_ref die = lookup_type_die (type);
5932 return strip_naming_typedef (type, die);
5935 /* Equate a DIE to a given type specifier. */
5937 static inline void
5938 equate_type_number_to_die (tree type, dw_die_ref type_die)
5940 TYPE_SYMTAB_DIE (type) = type_die;
5943 static dw_die_ref maybe_create_die_with_external_ref (tree);
5944 struct GTY(()) sym_off_pair
5946 const char * GTY((skip)) sym;
5947 unsigned HOST_WIDE_INT off;
5949 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5951 /* Returns a hash value for X (which really is a die_struct). */
5953 inline hashval_t
5954 decl_die_hasher::hash (die_node *x)
5956 return (hashval_t) x->decl_id;
5959 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5961 inline bool
5962 decl_die_hasher::equal (die_node *x, tree y)
5964 return (x->decl_id == DECL_UID (y));
5967 /* Return the DIE associated with a given declaration. */
5969 dw_die_ref
5970 lookup_decl_die (tree decl)
5972 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5973 NO_INSERT);
5974 if (!die)
5976 if (in_lto_p)
5977 return maybe_create_die_with_external_ref (decl);
5978 return NULL;
5980 if ((*die)->removed)
5982 decl_die_table->clear_slot (die);
5983 return NULL;
5985 return *die;
5989 /* Return the DIE associated with BLOCK. */
5991 static inline dw_die_ref
5992 lookup_block_die (tree block)
5994 dw_die_ref die = BLOCK_DIE (block);
5995 if (!die && in_lto_p)
5996 return maybe_create_die_with_external_ref (block);
5997 return die;
6000 /* Associate DIE with BLOCK. */
6002 static inline void
6003 equate_block_to_die (tree block, dw_die_ref die)
6005 BLOCK_DIE (block) = die;
6007 #undef BLOCK_DIE
6010 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6011 style reference. Return true if we found one refering to a DIE for
6012 DECL, otherwise return false. */
6014 static bool
6015 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6016 unsigned HOST_WIDE_INT *off)
6018 dw_die_ref die;
6020 if (in_lto_p)
6022 /* During WPA stage and incremental linking we use a hash-map
6023 to store the decl <-> label + offset map. */
6024 if (!external_die_map)
6025 return false;
6026 sym_off_pair *desc = external_die_map->get (decl);
6027 if (!desc)
6028 return false;
6029 *sym = desc->sym;
6030 *off = desc->off;
6031 return true;
6034 if (TREE_CODE (decl) == BLOCK)
6035 die = lookup_block_die (decl);
6036 else
6037 die = lookup_decl_die (decl);
6038 if (!die)
6039 return false;
6041 /* Similar to get_ref_die_offset_label, but using the "correct"
6042 label. */
6043 *off = die->die_offset;
6044 while (die->die_parent)
6045 die = die->die_parent;
6046 /* For the containing CU DIE we compute a die_symbol in
6047 compute_comp_unit_symbol. */
6048 gcc_assert (die->die_tag == DW_TAG_compile_unit
6049 && die->die_id.die_symbol != NULL);
6050 *sym = die->die_id.die_symbol;
6051 return true;
6054 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6056 static void
6057 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6058 const char *symbol, HOST_WIDE_INT offset)
6060 /* Create a fake DIE that contains the reference. Don't use
6061 new_die because we don't want to end up in the limbo list. */
6062 /* ??? We probably want to share these, thus put a ref to the DIE
6063 we create here to the external_die_map entry. */
6064 dw_die_ref ref = new_die_raw (die->die_tag);
6065 ref->die_id.die_symbol = symbol;
6066 ref->die_offset = offset;
6067 ref->with_offset = 1;
6068 add_AT_die_ref (die, attr_kind, ref);
6071 /* Create a DIE for DECL if required and add a reference to a DIE
6072 at SYMBOL + OFFSET which contains attributes dumped early. */
6074 static void
6075 dwarf2out_register_external_die (tree decl, const char *sym,
6076 unsigned HOST_WIDE_INT off)
6078 if (debug_info_level == DINFO_LEVEL_NONE)
6079 return;
6081 if (!external_die_map)
6082 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6083 gcc_checking_assert (!external_die_map->get (decl));
6084 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6085 external_die_map->put (decl, p);
6088 /* If we have a registered external DIE for DECL return a new DIE for
6089 the concrete instance with an appropriate abstract origin. */
6091 static dw_die_ref
6092 maybe_create_die_with_external_ref (tree decl)
6094 if (!external_die_map)
6095 return NULL;
6096 sym_off_pair *desc = external_die_map->get (decl);
6097 if (!desc)
6098 return NULL;
6100 const char *sym = desc->sym;
6101 unsigned HOST_WIDE_INT off = desc->off;
6102 external_die_map->remove (decl);
6104 in_lto_p = false;
6105 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6106 ? lookup_block_die (decl) : lookup_decl_die (decl));
6107 gcc_assert (!die);
6108 in_lto_p = true;
6110 tree ctx;
6111 dw_die_ref parent = NULL;
6112 /* Need to lookup a DIE for the decls context - the containing
6113 function or translation unit. */
6114 if (TREE_CODE (decl) == BLOCK)
6116 ctx = BLOCK_SUPERCONTEXT (decl);
6117 /* ??? We do not output DIEs for all scopes thus skip as
6118 many DIEs as needed. */
6119 while (TREE_CODE (ctx) == BLOCK
6120 && !lookup_block_die (ctx))
6121 ctx = BLOCK_SUPERCONTEXT (ctx);
6123 else
6124 ctx = DECL_CONTEXT (decl);
6125 /* Peel types in the context stack. */
6126 while (ctx && TYPE_P (ctx))
6127 ctx = TYPE_CONTEXT (ctx);
6128 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6129 if (debug_info_level <= DINFO_LEVEL_TERSE)
6130 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6131 ctx = DECL_CONTEXT (ctx);
6132 if (ctx)
6134 if (TREE_CODE (ctx) == BLOCK)
6135 parent = lookup_block_die (ctx);
6136 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6137 /* Keep the 1:1 association during WPA. */
6138 && !flag_wpa
6139 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6140 /* Otherwise all late annotations go to the main CU which
6141 imports the original CUs. */
6142 parent = comp_unit_die ();
6143 else if (TREE_CODE (ctx) == FUNCTION_DECL
6144 && TREE_CODE (decl) != FUNCTION_DECL
6145 && TREE_CODE (decl) != PARM_DECL
6146 && TREE_CODE (decl) != RESULT_DECL
6147 && TREE_CODE (decl) != BLOCK)
6148 /* Leave function local entities parent determination to when
6149 we process scope vars. */
6151 else
6152 parent = lookup_decl_die (ctx);
6154 else
6155 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6156 Handle this case gracefully by globalizing stuff. */
6157 parent = comp_unit_die ();
6158 /* Create a DIE "stub". */
6159 switch (TREE_CODE (decl))
6161 case TRANSLATION_UNIT_DECL:
6163 die = comp_unit_die ();
6164 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6165 to create a DIE for the original CUs. */
6166 return die;
6168 case NAMESPACE_DECL:
6169 if (is_fortran (decl))
6170 die = new_die (DW_TAG_module, parent, decl);
6171 else
6172 die = new_die (DW_TAG_namespace, parent, decl);
6173 break;
6174 case FUNCTION_DECL:
6175 die = new_die (DW_TAG_subprogram, parent, decl);
6176 break;
6177 case VAR_DECL:
6178 die = new_die (DW_TAG_variable, parent, decl);
6179 break;
6180 case RESULT_DECL:
6181 die = new_die (DW_TAG_variable, parent, decl);
6182 break;
6183 case PARM_DECL:
6184 die = new_die (DW_TAG_formal_parameter, parent, decl);
6185 break;
6186 case CONST_DECL:
6187 die = new_die (DW_TAG_constant, parent, decl);
6188 break;
6189 case LABEL_DECL:
6190 die = new_die (DW_TAG_label, parent, decl);
6191 break;
6192 case BLOCK:
6193 die = new_die (DW_TAG_lexical_block, parent, decl);
6194 break;
6195 default:
6196 gcc_unreachable ();
6198 if (TREE_CODE (decl) == BLOCK)
6199 equate_block_to_die (decl, die);
6200 else
6201 equate_decl_number_to_die (decl, die);
6203 add_desc_attribute (die, decl);
6205 /* Add a reference to the DIE providing early debug at $sym + off. */
6206 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6208 return die;
6211 /* Returns a hash value for X (which really is a var_loc_list). */
6213 inline hashval_t
6214 decl_loc_hasher::hash (var_loc_list *x)
6216 return (hashval_t) x->decl_id;
6219 /* Return nonzero if decl_id of var_loc_list X is the same as
6220 UID of decl *Y. */
6222 inline bool
6223 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6225 return (x->decl_id == DECL_UID (y));
6228 /* Return the var_loc list associated with a given declaration. */
6230 static inline var_loc_list *
6231 lookup_decl_loc (const_tree decl)
6233 if (!decl_loc_table)
6234 return NULL;
6235 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6238 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6240 inline hashval_t
6241 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6243 return (hashval_t) x->decl_id;
6246 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6247 UID of decl *Y. */
6249 inline bool
6250 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6252 return (x->decl_id == DECL_UID (y));
6255 /* Equate a DIE to a particular declaration. */
6257 static void
6258 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6260 unsigned int decl_id = DECL_UID (decl);
6262 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6263 decl_die->decl_id = decl_id;
6266 /* Return how many bits covers PIECE EXPR_LIST. */
6268 static HOST_WIDE_INT
6269 decl_piece_bitsize (rtx piece)
6271 int ret = (int) GET_MODE (piece);
6272 if (ret)
6273 return ret;
6274 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6275 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6276 return INTVAL (XEXP (XEXP (piece, 0), 0));
6279 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6281 static rtx *
6282 decl_piece_varloc_ptr (rtx piece)
6284 if ((int) GET_MODE (piece))
6285 return &XEXP (piece, 0);
6286 else
6287 return &XEXP (XEXP (piece, 0), 1);
6290 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6291 Next is the chain of following piece nodes. */
6293 static rtx_expr_list *
6294 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6296 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6297 return alloc_EXPR_LIST (bitsize, loc_note, next);
6298 else
6299 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6300 GEN_INT (bitsize),
6301 loc_note), next);
6304 /* Return rtx that should be stored into loc field for
6305 LOC_NOTE and BITPOS/BITSIZE. */
6307 static rtx
6308 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6309 HOST_WIDE_INT bitsize)
6311 if (bitsize != -1)
6313 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6314 if (bitpos != 0)
6315 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6317 return loc_note;
6320 /* This function either modifies location piece list *DEST in
6321 place (if SRC and INNER is NULL), or copies location piece list
6322 *SRC to *DEST while modifying it. Location BITPOS is modified
6323 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6324 not copied and if needed some padding around it is added.
6325 When modifying in place, DEST should point to EXPR_LIST where
6326 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6327 to the start of the whole list and INNER points to the EXPR_LIST
6328 where earlier pieces cover PIECE_BITPOS bits. */
6330 static void
6331 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6332 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6333 HOST_WIDE_INT bitsize, rtx loc_note)
6335 HOST_WIDE_INT diff;
6336 bool copy = inner != NULL;
6338 if (copy)
6340 /* First copy all nodes preceding the current bitpos. */
6341 while (src != inner)
6343 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6344 decl_piece_bitsize (*src), NULL_RTX);
6345 dest = &XEXP (*dest, 1);
6346 src = &XEXP (*src, 1);
6349 /* Add padding if needed. */
6350 if (bitpos != piece_bitpos)
6352 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6353 copy ? NULL_RTX : *dest);
6354 dest = &XEXP (*dest, 1);
6356 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6358 gcc_assert (!copy);
6359 /* A piece with correct bitpos and bitsize already exist,
6360 just update the location for it and return. */
6361 *decl_piece_varloc_ptr (*dest) = loc_note;
6362 return;
6364 /* Add the piece that changed. */
6365 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6366 dest = &XEXP (*dest, 1);
6367 /* Skip over pieces that overlap it. */
6368 diff = bitpos - piece_bitpos + bitsize;
6369 if (!copy)
6370 src = dest;
6371 while (diff > 0 && *src)
6373 rtx piece = *src;
6374 diff -= decl_piece_bitsize (piece);
6375 if (copy)
6376 src = &XEXP (piece, 1);
6377 else
6379 *src = XEXP (piece, 1);
6380 free_EXPR_LIST_node (piece);
6383 /* Add padding if needed. */
6384 if (diff < 0 && *src)
6386 if (!copy)
6387 dest = src;
6388 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6389 dest = &XEXP (*dest, 1);
6391 if (!copy)
6392 return;
6393 /* Finally copy all nodes following it. */
6394 while (*src)
6396 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6397 decl_piece_bitsize (*src), NULL_RTX);
6398 dest = &XEXP (*dest, 1);
6399 src = &XEXP (*src, 1);
6403 /* Add a variable location node to the linked list for DECL. */
6405 static struct var_loc_node *
6406 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6408 unsigned int decl_id;
6409 var_loc_list *temp;
6410 struct var_loc_node *loc = NULL;
6411 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6413 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6415 tree realdecl = DECL_DEBUG_EXPR (decl);
6416 if (handled_component_p (realdecl)
6417 || (TREE_CODE (realdecl) == MEM_REF
6418 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6420 bool reverse;
6421 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6422 &bitsize, &reverse);
6423 if (!innerdecl
6424 || !DECL_P (innerdecl)
6425 || DECL_IGNORED_P (innerdecl)
6426 || TREE_STATIC (innerdecl)
6427 || bitsize == 0
6428 || bitpos + bitsize > 256)
6429 return NULL;
6430 decl = innerdecl;
6434 decl_id = DECL_UID (decl);
6435 var_loc_list **slot
6436 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6437 if (*slot == NULL)
6439 temp = ggc_cleared_alloc<var_loc_list> ();
6440 temp->decl_id = decl_id;
6441 *slot = temp;
6443 else
6444 temp = *slot;
6446 /* For PARM_DECLs try to keep around the original incoming value,
6447 even if that means we'll emit a zero-range .debug_loc entry. */
6448 if (temp->last
6449 && temp->first == temp->last
6450 && TREE_CODE (decl) == PARM_DECL
6451 && NOTE_P (temp->first->loc)
6452 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6453 && DECL_INCOMING_RTL (decl)
6454 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6455 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6456 == GET_CODE (DECL_INCOMING_RTL (decl))
6457 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6458 && (bitsize != -1
6459 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6460 NOTE_VAR_LOCATION_LOC (loc_note))
6461 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6462 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6464 loc = ggc_cleared_alloc<var_loc_node> ();
6465 temp->first->next = loc;
6466 temp->last = loc;
6467 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6469 else if (temp->last)
6471 struct var_loc_node *last = temp->last, *unused = NULL;
6472 rtx *piece_loc = NULL, last_loc_note;
6473 HOST_WIDE_INT piece_bitpos = 0;
6474 if (last->next)
6476 last = last->next;
6477 gcc_assert (last->next == NULL);
6479 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6481 piece_loc = &last->loc;
6484 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6485 if (piece_bitpos + cur_bitsize > bitpos)
6486 break;
6487 piece_bitpos += cur_bitsize;
6488 piece_loc = &XEXP (*piece_loc, 1);
6490 while (*piece_loc);
6492 /* TEMP->LAST here is either pointer to the last but one or
6493 last element in the chained list, LAST is pointer to the
6494 last element. */
6495 if (label && strcmp (last->label, label) == 0 && last->view == view)
6497 /* For SRA optimized variables if there weren't any real
6498 insns since last note, just modify the last node. */
6499 if (piece_loc != NULL)
6501 adjust_piece_list (piece_loc, NULL, NULL,
6502 bitpos, piece_bitpos, bitsize, loc_note);
6503 return NULL;
6505 /* If the last note doesn't cover any instructions, remove it. */
6506 if (temp->last != last)
6508 temp->last->next = NULL;
6509 unused = last;
6510 last = temp->last;
6511 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6513 else
6515 gcc_assert (temp->first == temp->last
6516 || (temp->first->next == temp->last
6517 && TREE_CODE (decl) == PARM_DECL));
6518 memset (temp->last, '\0', sizeof (*temp->last));
6519 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6520 return temp->last;
6523 if (bitsize == -1 && NOTE_P (last->loc))
6524 last_loc_note = last->loc;
6525 else if (piece_loc != NULL
6526 && *piece_loc != NULL_RTX
6527 && piece_bitpos == bitpos
6528 && decl_piece_bitsize (*piece_loc) == bitsize)
6529 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6530 else
6531 last_loc_note = NULL_RTX;
6532 /* If the current location is the same as the end of the list,
6533 and either both or neither of the locations is uninitialized,
6534 we have nothing to do. */
6535 if (last_loc_note == NULL_RTX
6536 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6537 NOTE_VAR_LOCATION_LOC (loc_note)))
6538 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6539 != NOTE_VAR_LOCATION_STATUS (loc_note))
6540 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6541 == VAR_INIT_STATUS_UNINITIALIZED)
6542 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6543 == VAR_INIT_STATUS_UNINITIALIZED))))
6545 /* Add LOC to the end of list and update LAST. If the last
6546 element of the list has been removed above, reuse its
6547 memory for the new node, otherwise allocate a new one. */
6548 if (unused)
6550 loc = unused;
6551 memset (loc, '\0', sizeof (*loc));
6553 else
6554 loc = ggc_cleared_alloc<var_loc_node> ();
6555 if (bitsize == -1 || piece_loc == NULL)
6556 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6557 else
6558 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6559 bitpos, piece_bitpos, bitsize, loc_note);
6560 last->next = loc;
6561 /* Ensure TEMP->LAST will point either to the new last but one
6562 element of the chain, or to the last element in it. */
6563 if (last != temp->last)
6564 temp->last = last;
6566 else if (unused)
6567 ggc_free (unused);
6569 else
6571 loc = ggc_cleared_alloc<var_loc_node> ();
6572 temp->first = loc;
6573 temp->last = loc;
6574 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6576 return loc;
6579 /* Keep track of the number of spaces used to indent the
6580 output of the debugging routines that print the structure of
6581 the DIE internal representation. */
6582 static int print_indent;
6584 /* Indent the line the number of spaces given by print_indent. */
6586 static inline void
6587 print_spaces (FILE *outfile)
6589 fprintf (outfile, "%*s", print_indent, "");
6592 /* Print a type signature in hex. */
6594 static inline void
6595 print_signature (FILE *outfile, char *sig)
6597 int i;
6599 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6600 fprintf (outfile, "%02x", sig[i] & 0xff);
6603 static inline void
6604 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6606 if (discr_value->pos)
6607 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6608 else
6609 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6612 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6614 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6615 RECURSE, output location descriptor operations. */
6617 static void
6618 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6620 switch (val->val_class)
6622 case dw_val_class_addr:
6623 fprintf (outfile, "address");
6624 break;
6625 case dw_val_class_offset:
6626 fprintf (outfile, "offset");
6627 break;
6628 case dw_val_class_loc:
6629 fprintf (outfile, "location descriptor");
6630 if (val->v.val_loc == NULL)
6631 fprintf (outfile, " -> <null>\n");
6632 else if (recurse)
6634 fprintf (outfile, ":\n");
6635 print_indent += 4;
6636 print_loc_descr (val->v.val_loc, outfile);
6637 print_indent -= 4;
6639 else
6641 if (flag_dump_noaddr || flag_dump_unnumbered)
6642 fprintf (outfile, " #\n");
6643 else
6644 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6646 break;
6647 case dw_val_class_loc_list:
6648 fprintf (outfile, "location list -> label:%s",
6649 val->v.val_loc_list->ll_symbol);
6650 break;
6651 case dw_val_class_view_list:
6652 val = view_list_to_loc_list_val_node (val);
6653 fprintf (outfile, "location list with views -> labels:%s and %s",
6654 val->v.val_loc_list->ll_symbol,
6655 val->v.val_loc_list->vl_symbol);
6656 break;
6657 case dw_val_class_range_list:
6658 fprintf (outfile, "range list");
6659 break;
6660 case dw_val_class_const:
6661 case dw_val_class_const_implicit:
6662 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6663 break;
6664 case dw_val_class_unsigned_const:
6665 case dw_val_class_unsigned_const_implicit:
6666 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6667 break;
6668 case dw_val_class_const_double:
6669 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6670 HOST_WIDE_INT_PRINT_UNSIGNED")",
6671 val->v.val_double.high,
6672 val->v.val_double.low);
6673 break;
6674 case dw_val_class_wide_int:
6676 int i = val->v.val_wide->get_len ();
6677 fprintf (outfile, "constant (");
6678 gcc_assert (i > 0);
6679 if (val->v.val_wide->elt (i - 1) == 0)
6680 fprintf (outfile, "0x");
6681 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6682 val->v.val_wide->elt (--i));
6683 while (--i >= 0)
6684 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6685 val->v.val_wide->elt (i));
6686 fprintf (outfile, ")");
6687 break;
6689 case dw_val_class_vec:
6690 fprintf (outfile, "floating-point or vector constant");
6691 break;
6692 case dw_val_class_flag:
6693 fprintf (outfile, "%u", val->v.val_flag);
6694 break;
6695 case dw_val_class_die_ref:
6696 if (val->v.val_die_ref.die != NULL)
6698 dw_die_ref die = val->v.val_die_ref.die;
6700 if (die->comdat_type_p)
6702 fprintf (outfile, "die -> signature: ");
6703 print_signature (outfile,
6704 die->die_id.die_type_node->signature);
6706 else if (die->die_id.die_symbol)
6708 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6709 if (die->with_offset)
6710 fprintf (outfile, " + %ld", die->die_offset);
6712 else
6713 fprintf (outfile, "die -> %ld", die->die_offset);
6714 if (flag_dump_noaddr || flag_dump_unnumbered)
6715 fprintf (outfile, " #");
6716 else
6717 fprintf (outfile, " (%p)", (void *) die);
6719 else
6720 fprintf (outfile, "die -> <null>");
6721 break;
6722 case dw_val_class_vms_delta:
6723 fprintf (outfile, "delta: @slotcount(%s-%s)",
6724 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6725 break;
6726 case dw_val_class_symview:
6727 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6728 break;
6729 case dw_val_class_lbl_id:
6730 case dw_val_class_lineptr:
6731 case dw_val_class_macptr:
6732 case dw_val_class_loclistsptr:
6733 case dw_val_class_high_pc:
6734 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6735 break;
6736 case dw_val_class_str:
6737 if (val->v.val_str->str != NULL)
6738 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6739 else
6740 fprintf (outfile, "<null>");
6741 break;
6742 case dw_val_class_file:
6743 case dw_val_class_file_implicit:
6744 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6745 val->v.val_file->emitted_number);
6746 break;
6747 case dw_val_class_data8:
6749 int i;
6751 for (i = 0; i < 8; i++)
6752 fprintf (outfile, "%02x", val->v.val_data8[i]);
6753 break;
6755 case dw_val_class_discr_value:
6756 print_discr_value (outfile, &val->v.val_discr_value);
6757 break;
6758 case dw_val_class_discr_list:
6759 for (dw_discr_list_ref node = val->v.val_discr_list;
6760 node != NULL;
6761 node = node->dw_discr_next)
6763 if (node->dw_discr_range)
6765 fprintf (outfile, " .. ");
6766 print_discr_value (outfile, &node->dw_discr_lower_bound);
6767 print_discr_value (outfile, &node->dw_discr_upper_bound);
6769 else
6770 print_discr_value (outfile, &node->dw_discr_lower_bound);
6772 if (node->dw_discr_next != NULL)
6773 fprintf (outfile, " | ");
6775 default:
6776 break;
6780 /* Likewise, for a DIE attribute. */
6782 static void
6783 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6785 print_dw_val (&a->dw_attr_val, recurse, outfile);
6789 /* Print the list of operands in the LOC location description to OUTFILE. This
6790 routine is a debugging aid only. */
6792 static void
6793 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6795 dw_loc_descr_ref l = loc;
6797 if (loc == NULL)
6799 print_spaces (outfile);
6800 fprintf (outfile, "<null>\n");
6801 return;
6804 for (l = loc; l != NULL; l = l->dw_loc_next)
6806 print_spaces (outfile);
6807 if (flag_dump_noaddr || flag_dump_unnumbered)
6808 fprintf (outfile, "#");
6809 else
6810 fprintf (outfile, "(%p)", (void *) l);
6811 fprintf (outfile, " %s",
6812 dwarf_stack_op_name (l->dw_loc_opc));
6813 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6815 fprintf (outfile, " ");
6816 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6818 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6820 fprintf (outfile, ", ");
6821 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6823 fprintf (outfile, "\n");
6827 /* Print the information associated with a given DIE, and its children.
6828 This routine is a debugging aid only. */
6830 static void
6831 print_die (dw_die_ref die, FILE *outfile)
6833 dw_attr_node *a;
6834 dw_die_ref c;
6835 unsigned ix;
6837 print_spaces (outfile);
6838 fprintf (outfile, "DIE %4ld: %s ",
6839 die->die_offset, dwarf_tag_name (die->die_tag));
6840 if (flag_dump_noaddr || flag_dump_unnumbered)
6841 fprintf (outfile, "#\n");
6842 else
6843 fprintf (outfile, "(%p)\n", (void*) die);
6844 print_spaces (outfile);
6845 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6846 fprintf (outfile, " offset: %ld", die->die_offset);
6847 fprintf (outfile, " mark: %d\n", die->die_mark);
6849 if (die->comdat_type_p)
6851 print_spaces (outfile);
6852 fprintf (outfile, " signature: ");
6853 print_signature (outfile, die->die_id.die_type_node->signature);
6854 fprintf (outfile, "\n");
6857 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6859 print_spaces (outfile);
6860 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6862 print_attribute (a, true, outfile);
6863 fprintf (outfile, "\n");
6866 if (die->die_child != NULL)
6868 print_indent += 4;
6869 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6870 print_indent -= 4;
6872 if (print_indent == 0)
6873 fprintf (outfile, "\n");
6876 /* Print the list of operations in the LOC location description. */
6878 DEBUG_FUNCTION void
6879 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6881 print_loc_descr (loc, stderr);
6884 /* Print the information collected for a given DIE. */
6886 DEBUG_FUNCTION void
6887 debug_dwarf_die (dw_die_ref die)
6889 print_die (die, stderr);
6892 DEBUG_FUNCTION void
6893 debug (die_struct &ref)
6895 print_die (&ref, stderr);
6898 DEBUG_FUNCTION void
6899 debug (die_struct *ptr)
6901 if (ptr)
6902 debug (*ptr);
6903 else
6904 fprintf (stderr, "<nil>\n");
6908 /* Print all DWARF information collected for the compilation unit.
6909 This routine is a debugging aid only. */
6911 DEBUG_FUNCTION void
6912 debug_dwarf (void)
6914 print_indent = 0;
6915 print_die (comp_unit_die (), stderr);
6918 /* Verify the DIE tree structure. */
6920 DEBUG_FUNCTION void
6921 verify_die (dw_die_ref die)
6923 gcc_assert (!die->die_mark);
6924 if (die->die_parent == NULL
6925 && die->die_sib == NULL)
6926 return;
6927 /* Verify the die_sib list is cyclic. */
6928 dw_die_ref x = die;
6931 x->die_mark = 1;
6932 x = x->die_sib;
6934 while (x && !x->die_mark);
6935 gcc_assert (x == die);
6936 x = die;
6939 /* Verify all dies have the same parent. */
6940 gcc_assert (x->die_parent == die->die_parent);
6941 if (x->die_child)
6943 /* Verify the child has the proper parent and recurse. */
6944 gcc_assert (x->die_child->die_parent == x);
6945 verify_die (x->die_child);
6947 x->die_mark = 0;
6948 x = x->die_sib;
6950 while (x && x->die_mark);
6953 /* Sanity checks on DIEs. */
6955 static void
6956 check_die (dw_die_ref die)
6958 unsigned ix;
6959 dw_attr_node *a;
6960 bool inline_found = false;
6961 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6962 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6963 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6965 switch (a->dw_attr)
6967 case DW_AT_inline:
6968 if (a->dw_attr_val.v.val_unsigned)
6969 inline_found = true;
6970 break;
6971 case DW_AT_location:
6972 ++n_location;
6973 break;
6974 case DW_AT_low_pc:
6975 ++n_low_pc;
6976 break;
6977 case DW_AT_high_pc:
6978 ++n_high_pc;
6979 break;
6980 case DW_AT_artificial:
6981 ++n_artificial;
6982 break;
6983 case DW_AT_decl_column:
6984 ++n_decl_column;
6985 break;
6986 case DW_AT_decl_line:
6987 ++n_decl_line;
6988 break;
6989 case DW_AT_decl_file:
6990 ++n_decl_file;
6991 break;
6992 default:
6993 break;
6996 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6997 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6999 fprintf (stderr, "Duplicate attributes in DIE:\n");
7000 debug_dwarf_die (die);
7001 gcc_unreachable ();
7003 if (inline_found)
7005 /* A debugging information entry that is a member of an abstract
7006 instance tree [that has DW_AT_inline] should not contain any
7007 attributes which describe aspects of the subroutine which vary
7008 between distinct inlined expansions or distinct out-of-line
7009 expansions. */
7010 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7011 gcc_assert (a->dw_attr != DW_AT_low_pc
7012 && a->dw_attr != DW_AT_high_pc
7013 && a->dw_attr != DW_AT_location
7014 && a->dw_attr != DW_AT_frame_base
7015 && a->dw_attr != DW_AT_call_all_calls
7016 && a->dw_attr != DW_AT_GNU_all_call_sites);
7020 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7021 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7022 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7024 /* Calculate the checksum of a location expression. */
7026 static inline void
7027 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7029 int tem;
7030 inchash::hash hstate;
7031 hashval_t hash;
7033 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7034 CHECKSUM (tem);
7035 hash_loc_operands (loc, hstate);
7036 hash = hstate.end();
7037 CHECKSUM (hash);
7040 /* Calculate the checksum of an attribute. */
7042 static void
7043 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7045 dw_loc_descr_ref loc;
7046 rtx r;
7048 CHECKSUM (at->dw_attr);
7050 /* We don't care that this was compiled with a different compiler
7051 snapshot; if the output is the same, that's what matters. */
7052 if (at->dw_attr == DW_AT_producer)
7053 return;
7055 switch (AT_class (at))
7057 case dw_val_class_const:
7058 case dw_val_class_const_implicit:
7059 CHECKSUM (at->dw_attr_val.v.val_int);
7060 break;
7061 case dw_val_class_unsigned_const:
7062 case dw_val_class_unsigned_const_implicit:
7063 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7064 break;
7065 case dw_val_class_const_double:
7066 CHECKSUM (at->dw_attr_val.v.val_double);
7067 break;
7068 case dw_val_class_wide_int:
7069 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7070 get_full_len (*at->dw_attr_val.v.val_wide)
7071 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7072 break;
7073 case dw_val_class_vec:
7074 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7075 (at->dw_attr_val.v.val_vec.length
7076 * at->dw_attr_val.v.val_vec.elt_size));
7077 break;
7078 case dw_val_class_flag:
7079 CHECKSUM (at->dw_attr_val.v.val_flag);
7080 break;
7081 case dw_val_class_str:
7082 CHECKSUM_STRING (AT_string (at));
7083 break;
7085 case dw_val_class_addr:
7086 r = AT_addr (at);
7087 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7088 CHECKSUM_STRING (XSTR (r, 0));
7089 break;
7091 case dw_val_class_offset:
7092 CHECKSUM (at->dw_attr_val.v.val_offset);
7093 break;
7095 case dw_val_class_loc:
7096 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7097 loc_checksum (loc, ctx);
7098 break;
7100 case dw_val_class_die_ref:
7101 die_checksum (AT_ref (at), ctx, mark);
7102 break;
7104 case dw_val_class_fde_ref:
7105 case dw_val_class_vms_delta:
7106 case dw_val_class_symview:
7107 case dw_val_class_lbl_id:
7108 case dw_val_class_lineptr:
7109 case dw_val_class_macptr:
7110 case dw_val_class_loclistsptr:
7111 case dw_val_class_high_pc:
7112 break;
7114 case dw_val_class_file:
7115 case dw_val_class_file_implicit:
7116 CHECKSUM_STRING (AT_file (at)->filename);
7117 break;
7119 case dw_val_class_data8:
7120 CHECKSUM (at->dw_attr_val.v.val_data8);
7121 break;
7123 default:
7124 break;
7128 /* Calculate the checksum of a DIE. */
7130 static void
7131 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7133 dw_die_ref c;
7134 dw_attr_node *a;
7135 unsigned ix;
7137 /* To avoid infinite recursion. */
7138 if (die->die_mark)
7140 CHECKSUM (die->die_mark);
7141 return;
7143 die->die_mark = ++(*mark);
7145 CHECKSUM (die->die_tag);
7147 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7148 attr_checksum (a, ctx, mark);
7150 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7153 #undef CHECKSUM
7154 #undef CHECKSUM_BLOCK
7155 #undef CHECKSUM_STRING
7157 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7158 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7159 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7160 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7161 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7162 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7163 #define CHECKSUM_ATTR(FOO) \
7164 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7166 /* Calculate the checksum of a number in signed LEB128 format. */
7168 static void
7169 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7171 unsigned char byte;
7172 bool more;
7174 while (1)
7176 byte = (value & 0x7f);
7177 value >>= 7;
7178 more = !((value == 0 && (byte & 0x40) == 0)
7179 || (value == -1 && (byte & 0x40) != 0));
7180 if (more)
7181 byte |= 0x80;
7182 CHECKSUM (byte);
7183 if (!more)
7184 break;
7188 /* Calculate the checksum of a number in unsigned LEB128 format. */
7190 static void
7191 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7193 while (1)
7195 unsigned char byte = (value & 0x7f);
7196 value >>= 7;
7197 if (value != 0)
7198 /* More bytes to follow. */
7199 byte |= 0x80;
7200 CHECKSUM (byte);
7201 if (value == 0)
7202 break;
7206 /* Checksum the context of the DIE. This adds the names of any
7207 surrounding namespaces or structures to the checksum. */
7209 static void
7210 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7212 const char *name;
7213 dw_die_ref spec;
7214 int tag = die->die_tag;
7216 if (tag != DW_TAG_namespace
7217 && tag != DW_TAG_structure_type
7218 && tag != DW_TAG_class_type)
7219 return;
7221 name = get_AT_string (die, DW_AT_name);
7223 spec = get_AT_ref (die, DW_AT_specification);
7224 if (spec != NULL)
7225 die = spec;
7227 if (die->die_parent != NULL)
7228 checksum_die_context (die->die_parent, ctx);
7230 CHECKSUM_ULEB128 ('C');
7231 CHECKSUM_ULEB128 (tag);
7232 if (name != NULL)
7233 CHECKSUM_STRING (name);
7236 /* Calculate the checksum of a location expression. */
7238 static inline void
7239 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7241 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7242 were emitted as a DW_FORM_sdata instead of a location expression. */
7243 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7245 CHECKSUM_ULEB128 (DW_FORM_sdata);
7246 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7247 return;
7250 /* Otherwise, just checksum the raw location expression. */
7251 while (loc != NULL)
7253 inchash::hash hstate;
7254 hashval_t hash;
7256 CHECKSUM_ULEB128 (loc->dtprel);
7257 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7258 hash_loc_operands (loc, hstate);
7259 hash = hstate.end ();
7260 CHECKSUM (hash);
7261 loc = loc->dw_loc_next;
7265 /* Calculate the checksum of an attribute. */
7267 static void
7268 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7269 struct md5_ctx *ctx, int *mark)
7271 dw_loc_descr_ref loc;
7272 rtx r;
7274 if (AT_class (at) == dw_val_class_die_ref)
7276 dw_die_ref target_die = AT_ref (at);
7278 /* For pointer and reference types, we checksum only the (qualified)
7279 name of the target type (if there is a name). For friend entries,
7280 we checksum only the (qualified) name of the target type or function.
7281 This allows the checksum to remain the same whether the target type
7282 is complete or not. */
7283 if ((at->dw_attr == DW_AT_type
7284 && (tag == DW_TAG_pointer_type
7285 || tag == DW_TAG_reference_type
7286 || tag == DW_TAG_rvalue_reference_type
7287 || tag == DW_TAG_ptr_to_member_type))
7288 || (at->dw_attr == DW_AT_friend
7289 && tag == DW_TAG_friend))
7291 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7293 if (name_attr != NULL)
7295 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7297 if (decl == NULL)
7298 decl = target_die;
7299 CHECKSUM_ULEB128 ('N');
7300 CHECKSUM_ULEB128 (at->dw_attr);
7301 if (decl->die_parent != NULL)
7302 checksum_die_context (decl->die_parent, ctx);
7303 CHECKSUM_ULEB128 ('E');
7304 CHECKSUM_STRING (AT_string (name_attr));
7305 return;
7309 /* For all other references to another DIE, we check to see if the
7310 target DIE has already been visited. If it has, we emit a
7311 backward reference; if not, we descend recursively. */
7312 if (target_die->die_mark > 0)
7314 CHECKSUM_ULEB128 ('R');
7315 CHECKSUM_ULEB128 (at->dw_attr);
7316 CHECKSUM_ULEB128 (target_die->die_mark);
7318 else
7320 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7322 if (decl == NULL)
7323 decl = target_die;
7324 target_die->die_mark = ++(*mark);
7325 CHECKSUM_ULEB128 ('T');
7326 CHECKSUM_ULEB128 (at->dw_attr);
7327 if (decl->die_parent != NULL)
7328 checksum_die_context (decl->die_parent, ctx);
7329 die_checksum_ordered (target_die, ctx, mark);
7331 return;
7334 CHECKSUM_ULEB128 ('A');
7335 CHECKSUM_ULEB128 (at->dw_attr);
7337 switch (AT_class (at))
7339 case dw_val_class_const:
7340 case dw_val_class_const_implicit:
7341 CHECKSUM_ULEB128 (DW_FORM_sdata);
7342 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7343 break;
7345 case dw_val_class_unsigned_const:
7346 case dw_val_class_unsigned_const_implicit:
7347 CHECKSUM_ULEB128 (DW_FORM_sdata);
7348 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7349 break;
7351 case dw_val_class_const_double:
7352 CHECKSUM_ULEB128 (DW_FORM_block);
7353 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7354 CHECKSUM (at->dw_attr_val.v.val_double);
7355 break;
7357 case dw_val_class_wide_int:
7358 CHECKSUM_ULEB128 (DW_FORM_block);
7359 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7360 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7361 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7362 get_full_len (*at->dw_attr_val.v.val_wide)
7363 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7364 break;
7366 case dw_val_class_vec:
7367 CHECKSUM_ULEB128 (DW_FORM_block);
7368 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7369 * at->dw_attr_val.v.val_vec.elt_size);
7370 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7371 (at->dw_attr_val.v.val_vec.length
7372 * at->dw_attr_val.v.val_vec.elt_size));
7373 break;
7375 case dw_val_class_flag:
7376 CHECKSUM_ULEB128 (DW_FORM_flag);
7377 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7378 break;
7380 case dw_val_class_str:
7381 CHECKSUM_ULEB128 (DW_FORM_string);
7382 CHECKSUM_STRING (AT_string (at));
7383 break;
7385 case dw_val_class_addr:
7386 r = AT_addr (at);
7387 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7388 CHECKSUM_ULEB128 (DW_FORM_string);
7389 CHECKSUM_STRING (XSTR (r, 0));
7390 break;
7392 case dw_val_class_offset:
7393 CHECKSUM_ULEB128 (DW_FORM_sdata);
7394 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7395 break;
7397 case dw_val_class_loc:
7398 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7399 loc_checksum_ordered (loc, ctx);
7400 break;
7402 case dw_val_class_fde_ref:
7403 case dw_val_class_symview:
7404 case dw_val_class_lbl_id:
7405 case dw_val_class_lineptr:
7406 case dw_val_class_macptr:
7407 case dw_val_class_loclistsptr:
7408 case dw_val_class_high_pc:
7409 break;
7411 case dw_val_class_file:
7412 case dw_val_class_file_implicit:
7413 CHECKSUM_ULEB128 (DW_FORM_string);
7414 CHECKSUM_STRING (AT_file (at)->filename);
7415 break;
7417 case dw_val_class_data8:
7418 CHECKSUM (at->dw_attr_val.v.val_data8);
7419 break;
7421 default:
7422 break;
7426 struct checksum_attributes
7428 dw_attr_node *at_name;
7429 dw_attr_node *at_type;
7430 dw_attr_node *at_friend;
7431 dw_attr_node *at_accessibility;
7432 dw_attr_node *at_address_class;
7433 dw_attr_node *at_alignment;
7434 dw_attr_node *at_allocated;
7435 dw_attr_node *at_artificial;
7436 dw_attr_node *at_associated;
7437 dw_attr_node *at_binary_scale;
7438 dw_attr_node *at_bit_offset;
7439 dw_attr_node *at_bit_size;
7440 dw_attr_node *at_bit_stride;
7441 dw_attr_node *at_byte_size;
7442 dw_attr_node *at_byte_stride;
7443 dw_attr_node *at_const_value;
7444 dw_attr_node *at_containing_type;
7445 dw_attr_node *at_count;
7446 dw_attr_node *at_data_location;
7447 dw_attr_node *at_data_member_location;
7448 dw_attr_node *at_decimal_scale;
7449 dw_attr_node *at_decimal_sign;
7450 dw_attr_node *at_default_value;
7451 dw_attr_node *at_digit_count;
7452 dw_attr_node *at_discr;
7453 dw_attr_node *at_discr_list;
7454 dw_attr_node *at_discr_value;
7455 dw_attr_node *at_encoding;
7456 dw_attr_node *at_endianity;
7457 dw_attr_node *at_explicit;
7458 dw_attr_node *at_is_optional;
7459 dw_attr_node *at_location;
7460 dw_attr_node *at_lower_bound;
7461 dw_attr_node *at_mutable;
7462 dw_attr_node *at_ordering;
7463 dw_attr_node *at_picture_string;
7464 dw_attr_node *at_prototyped;
7465 dw_attr_node *at_small;
7466 dw_attr_node *at_segment;
7467 dw_attr_node *at_string_length;
7468 dw_attr_node *at_string_length_bit_size;
7469 dw_attr_node *at_string_length_byte_size;
7470 dw_attr_node *at_threads_scaled;
7471 dw_attr_node *at_upper_bound;
7472 dw_attr_node *at_use_location;
7473 dw_attr_node *at_use_UTF8;
7474 dw_attr_node *at_variable_parameter;
7475 dw_attr_node *at_virtuality;
7476 dw_attr_node *at_visibility;
7477 dw_attr_node *at_vtable_elem_location;
7480 /* Collect the attributes that we will want to use for the checksum. */
7482 static void
7483 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7485 dw_attr_node *a;
7486 unsigned ix;
7488 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7490 switch (a->dw_attr)
7492 case DW_AT_name:
7493 attrs->at_name = a;
7494 break;
7495 case DW_AT_type:
7496 attrs->at_type = a;
7497 break;
7498 case DW_AT_friend:
7499 attrs->at_friend = a;
7500 break;
7501 case DW_AT_accessibility:
7502 attrs->at_accessibility = a;
7503 break;
7504 case DW_AT_address_class:
7505 attrs->at_address_class = a;
7506 break;
7507 case DW_AT_alignment:
7508 attrs->at_alignment = a;
7509 break;
7510 case DW_AT_allocated:
7511 attrs->at_allocated = a;
7512 break;
7513 case DW_AT_artificial:
7514 attrs->at_artificial = a;
7515 break;
7516 case DW_AT_associated:
7517 attrs->at_associated = a;
7518 break;
7519 case DW_AT_binary_scale:
7520 attrs->at_binary_scale = a;
7521 break;
7522 case DW_AT_bit_offset:
7523 attrs->at_bit_offset = a;
7524 break;
7525 case DW_AT_bit_size:
7526 attrs->at_bit_size = a;
7527 break;
7528 case DW_AT_bit_stride:
7529 attrs->at_bit_stride = a;
7530 break;
7531 case DW_AT_byte_size:
7532 attrs->at_byte_size = a;
7533 break;
7534 case DW_AT_byte_stride:
7535 attrs->at_byte_stride = a;
7536 break;
7537 case DW_AT_const_value:
7538 attrs->at_const_value = a;
7539 break;
7540 case DW_AT_containing_type:
7541 attrs->at_containing_type = a;
7542 break;
7543 case DW_AT_count:
7544 attrs->at_count = a;
7545 break;
7546 case DW_AT_data_location:
7547 attrs->at_data_location = a;
7548 break;
7549 case DW_AT_data_member_location:
7550 attrs->at_data_member_location = a;
7551 break;
7552 case DW_AT_decimal_scale:
7553 attrs->at_decimal_scale = a;
7554 break;
7555 case DW_AT_decimal_sign:
7556 attrs->at_decimal_sign = a;
7557 break;
7558 case DW_AT_default_value:
7559 attrs->at_default_value = a;
7560 break;
7561 case DW_AT_digit_count:
7562 attrs->at_digit_count = a;
7563 break;
7564 case DW_AT_discr:
7565 attrs->at_discr = a;
7566 break;
7567 case DW_AT_discr_list:
7568 attrs->at_discr_list = a;
7569 break;
7570 case DW_AT_discr_value:
7571 attrs->at_discr_value = a;
7572 break;
7573 case DW_AT_encoding:
7574 attrs->at_encoding = a;
7575 break;
7576 case DW_AT_endianity:
7577 attrs->at_endianity = a;
7578 break;
7579 case DW_AT_explicit:
7580 attrs->at_explicit = a;
7581 break;
7582 case DW_AT_is_optional:
7583 attrs->at_is_optional = a;
7584 break;
7585 case DW_AT_location:
7586 attrs->at_location = a;
7587 break;
7588 case DW_AT_lower_bound:
7589 attrs->at_lower_bound = a;
7590 break;
7591 case DW_AT_mutable:
7592 attrs->at_mutable = a;
7593 break;
7594 case DW_AT_ordering:
7595 attrs->at_ordering = a;
7596 break;
7597 case DW_AT_picture_string:
7598 attrs->at_picture_string = a;
7599 break;
7600 case DW_AT_prototyped:
7601 attrs->at_prototyped = a;
7602 break;
7603 case DW_AT_small:
7604 attrs->at_small = a;
7605 break;
7606 case DW_AT_segment:
7607 attrs->at_segment = a;
7608 break;
7609 case DW_AT_string_length:
7610 attrs->at_string_length = a;
7611 break;
7612 case DW_AT_string_length_bit_size:
7613 attrs->at_string_length_bit_size = a;
7614 break;
7615 case DW_AT_string_length_byte_size:
7616 attrs->at_string_length_byte_size = a;
7617 break;
7618 case DW_AT_threads_scaled:
7619 attrs->at_threads_scaled = a;
7620 break;
7621 case DW_AT_upper_bound:
7622 attrs->at_upper_bound = a;
7623 break;
7624 case DW_AT_use_location:
7625 attrs->at_use_location = a;
7626 break;
7627 case DW_AT_use_UTF8:
7628 attrs->at_use_UTF8 = a;
7629 break;
7630 case DW_AT_variable_parameter:
7631 attrs->at_variable_parameter = a;
7632 break;
7633 case DW_AT_virtuality:
7634 attrs->at_virtuality = a;
7635 break;
7636 case DW_AT_visibility:
7637 attrs->at_visibility = a;
7638 break;
7639 case DW_AT_vtable_elem_location:
7640 attrs->at_vtable_elem_location = a;
7641 break;
7642 default:
7643 break;
7648 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7650 static void
7651 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7653 dw_die_ref c;
7654 dw_die_ref decl;
7655 struct checksum_attributes attrs;
7657 CHECKSUM_ULEB128 ('D');
7658 CHECKSUM_ULEB128 (die->die_tag);
7660 memset (&attrs, 0, sizeof (attrs));
7662 decl = get_AT_ref (die, DW_AT_specification);
7663 if (decl != NULL)
7664 collect_checksum_attributes (&attrs, decl);
7665 collect_checksum_attributes (&attrs, die);
7667 CHECKSUM_ATTR (attrs.at_name);
7668 CHECKSUM_ATTR (attrs.at_accessibility);
7669 CHECKSUM_ATTR (attrs.at_address_class);
7670 CHECKSUM_ATTR (attrs.at_allocated);
7671 CHECKSUM_ATTR (attrs.at_artificial);
7672 CHECKSUM_ATTR (attrs.at_associated);
7673 CHECKSUM_ATTR (attrs.at_binary_scale);
7674 CHECKSUM_ATTR (attrs.at_bit_offset);
7675 CHECKSUM_ATTR (attrs.at_bit_size);
7676 CHECKSUM_ATTR (attrs.at_bit_stride);
7677 CHECKSUM_ATTR (attrs.at_byte_size);
7678 CHECKSUM_ATTR (attrs.at_byte_stride);
7679 CHECKSUM_ATTR (attrs.at_const_value);
7680 CHECKSUM_ATTR (attrs.at_containing_type);
7681 CHECKSUM_ATTR (attrs.at_count);
7682 CHECKSUM_ATTR (attrs.at_data_location);
7683 CHECKSUM_ATTR (attrs.at_data_member_location);
7684 CHECKSUM_ATTR (attrs.at_decimal_scale);
7685 CHECKSUM_ATTR (attrs.at_decimal_sign);
7686 CHECKSUM_ATTR (attrs.at_default_value);
7687 CHECKSUM_ATTR (attrs.at_digit_count);
7688 CHECKSUM_ATTR (attrs.at_discr);
7689 CHECKSUM_ATTR (attrs.at_discr_list);
7690 CHECKSUM_ATTR (attrs.at_discr_value);
7691 CHECKSUM_ATTR (attrs.at_encoding);
7692 CHECKSUM_ATTR (attrs.at_endianity);
7693 CHECKSUM_ATTR (attrs.at_explicit);
7694 CHECKSUM_ATTR (attrs.at_is_optional);
7695 CHECKSUM_ATTR (attrs.at_location);
7696 CHECKSUM_ATTR (attrs.at_lower_bound);
7697 CHECKSUM_ATTR (attrs.at_mutable);
7698 CHECKSUM_ATTR (attrs.at_ordering);
7699 CHECKSUM_ATTR (attrs.at_picture_string);
7700 CHECKSUM_ATTR (attrs.at_prototyped);
7701 CHECKSUM_ATTR (attrs.at_small);
7702 CHECKSUM_ATTR (attrs.at_segment);
7703 CHECKSUM_ATTR (attrs.at_string_length);
7704 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7705 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7706 CHECKSUM_ATTR (attrs.at_threads_scaled);
7707 CHECKSUM_ATTR (attrs.at_upper_bound);
7708 CHECKSUM_ATTR (attrs.at_use_location);
7709 CHECKSUM_ATTR (attrs.at_use_UTF8);
7710 CHECKSUM_ATTR (attrs.at_variable_parameter);
7711 CHECKSUM_ATTR (attrs.at_virtuality);
7712 CHECKSUM_ATTR (attrs.at_visibility);
7713 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7714 CHECKSUM_ATTR (attrs.at_type);
7715 CHECKSUM_ATTR (attrs.at_friend);
7716 CHECKSUM_ATTR (attrs.at_alignment);
7718 /* Checksum the child DIEs. */
7719 c = die->die_child;
7720 if (c) do {
7721 dw_attr_node *name_attr;
7723 c = c->die_sib;
7724 name_attr = get_AT (c, DW_AT_name);
7725 if (is_template_instantiation (c))
7727 /* Ignore instantiations of member type and function templates. */
7729 else if (name_attr != NULL
7730 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7732 /* Use a shallow checksum for named nested types and member
7733 functions. */
7734 CHECKSUM_ULEB128 ('S');
7735 CHECKSUM_ULEB128 (c->die_tag);
7736 CHECKSUM_STRING (AT_string (name_attr));
7738 else
7740 /* Use a deep checksum for other children. */
7741 /* Mark this DIE so it gets processed when unmarking. */
7742 if (c->die_mark == 0)
7743 c->die_mark = -1;
7744 die_checksum_ordered (c, ctx, mark);
7746 } while (c != die->die_child);
7748 CHECKSUM_ULEB128 (0);
7751 /* Add a type name and tag to a hash. */
7752 static void
7753 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7755 CHECKSUM_ULEB128 (tag);
7756 CHECKSUM_STRING (name);
7759 #undef CHECKSUM
7760 #undef CHECKSUM_STRING
7761 #undef CHECKSUM_ATTR
7762 #undef CHECKSUM_LEB128
7763 #undef CHECKSUM_ULEB128
7765 /* Generate the type signature for DIE. This is computed by generating an
7766 MD5 checksum over the DIE's tag, its relevant attributes, and its
7767 children. Attributes that are references to other DIEs are processed
7768 by recursion, using the MARK field to prevent infinite recursion.
7769 If the DIE is nested inside a namespace or another type, we also
7770 need to include that context in the signature. The lower 64 bits
7771 of the resulting MD5 checksum comprise the signature. */
7773 static void
7774 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7776 int mark;
7777 const char *name;
7778 unsigned char checksum[16];
7779 struct md5_ctx ctx;
7780 dw_die_ref decl;
7781 dw_die_ref parent;
7783 name = get_AT_string (die, DW_AT_name);
7784 decl = get_AT_ref (die, DW_AT_specification);
7785 parent = get_die_parent (die);
7787 /* First, compute a signature for just the type name (and its surrounding
7788 context, if any. This is stored in the type unit DIE for link-time
7789 ODR (one-definition rule) checking. */
7791 if (is_cxx () && name != NULL)
7793 md5_init_ctx (&ctx);
7795 /* Checksum the names of surrounding namespaces and structures. */
7796 if (parent != NULL)
7797 checksum_die_context (parent, &ctx);
7799 /* Checksum the current DIE. */
7800 die_odr_checksum (die->die_tag, name, &ctx);
7801 md5_finish_ctx (&ctx, checksum);
7803 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7806 /* Next, compute the complete type signature. */
7808 md5_init_ctx (&ctx);
7809 mark = 1;
7810 die->die_mark = mark;
7812 /* Checksum the names of surrounding namespaces and structures. */
7813 if (parent != NULL)
7814 checksum_die_context (parent, &ctx);
7816 /* Checksum the DIE and its children. */
7817 die_checksum_ordered (die, &ctx, &mark);
7818 unmark_all_dies (die);
7819 md5_finish_ctx (&ctx, checksum);
7821 /* Store the signature in the type node and link the type DIE and the
7822 type node together. */
7823 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7824 DWARF_TYPE_SIGNATURE_SIZE);
7825 die->comdat_type_p = true;
7826 die->die_id.die_type_node = type_node;
7827 type_node->type_die = die;
7829 /* If the DIE is a specification, link its declaration to the type node
7830 as well. */
7831 if (decl != NULL)
7833 decl->comdat_type_p = true;
7834 decl->die_id.die_type_node = type_node;
7838 /* Do the location expressions look same? */
7839 static inline int
7840 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7842 return loc1->dw_loc_opc == loc2->dw_loc_opc
7843 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7844 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7847 /* Do the values look the same? */
7848 static int
7849 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7851 dw_loc_descr_ref loc1, loc2;
7852 rtx r1, r2;
7854 if (v1->val_class != v2->val_class)
7855 return 0;
7857 switch (v1->val_class)
7859 case dw_val_class_const:
7860 case dw_val_class_const_implicit:
7861 return v1->v.val_int == v2->v.val_int;
7862 case dw_val_class_unsigned_const:
7863 case dw_val_class_unsigned_const_implicit:
7864 return v1->v.val_unsigned == v2->v.val_unsigned;
7865 case dw_val_class_const_double:
7866 return v1->v.val_double.high == v2->v.val_double.high
7867 && v1->v.val_double.low == v2->v.val_double.low;
7868 case dw_val_class_wide_int:
7869 return *v1->v.val_wide == *v2->v.val_wide;
7870 case dw_val_class_vec:
7871 if (v1->v.val_vec.length != v2->v.val_vec.length
7872 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7873 return 0;
7874 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7875 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7876 return 0;
7877 return 1;
7878 case dw_val_class_flag:
7879 return v1->v.val_flag == v2->v.val_flag;
7880 case dw_val_class_str:
7881 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7883 case dw_val_class_addr:
7884 r1 = v1->v.val_addr;
7885 r2 = v2->v.val_addr;
7886 if (GET_CODE (r1) != GET_CODE (r2))
7887 return 0;
7888 return !rtx_equal_p (r1, r2);
7890 case dw_val_class_offset:
7891 return v1->v.val_offset == v2->v.val_offset;
7893 case dw_val_class_loc:
7894 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7895 loc1 && loc2;
7896 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7897 if (!same_loc_p (loc1, loc2, mark))
7898 return 0;
7899 return !loc1 && !loc2;
7901 case dw_val_class_die_ref:
7902 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7904 case dw_val_class_symview:
7905 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7907 case dw_val_class_fde_ref:
7908 case dw_val_class_vms_delta:
7909 case dw_val_class_lbl_id:
7910 case dw_val_class_lineptr:
7911 case dw_val_class_macptr:
7912 case dw_val_class_loclistsptr:
7913 case dw_val_class_high_pc:
7914 return 1;
7916 case dw_val_class_file:
7917 case dw_val_class_file_implicit:
7918 return v1->v.val_file == v2->v.val_file;
7920 case dw_val_class_data8:
7921 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7923 default:
7924 return 1;
7928 /* Do the attributes look the same? */
7930 static int
7931 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7933 if (at1->dw_attr != at2->dw_attr)
7934 return 0;
7936 /* We don't care that this was compiled with a different compiler
7937 snapshot; if the output is the same, that's what matters. */
7938 if (at1->dw_attr == DW_AT_producer)
7939 return 1;
7941 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7944 /* Do the dies look the same? */
7946 static int
7947 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7949 dw_die_ref c1, c2;
7950 dw_attr_node *a1;
7951 unsigned ix;
7953 /* To avoid infinite recursion. */
7954 if (die1->die_mark)
7955 return die1->die_mark == die2->die_mark;
7956 die1->die_mark = die2->die_mark = ++(*mark);
7958 if (die1->die_tag != die2->die_tag)
7959 return 0;
7961 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7962 return 0;
7964 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7965 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7966 return 0;
7968 c1 = die1->die_child;
7969 c2 = die2->die_child;
7970 if (! c1)
7972 if (c2)
7973 return 0;
7975 else
7976 for (;;)
7978 if (!same_die_p (c1, c2, mark))
7979 return 0;
7980 c1 = c1->die_sib;
7981 c2 = c2->die_sib;
7982 if (c1 == die1->die_child)
7984 if (c2 == die2->die_child)
7985 break;
7986 else
7987 return 0;
7991 return 1;
7994 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7995 children, and set die_symbol. */
7997 static void
7998 compute_comp_unit_symbol (dw_die_ref unit_die)
8000 const char *die_name = get_AT_string (unit_die, DW_AT_name);
8001 const char *base = die_name ? lbasename (die_name) : "anonymous";
8002 char *name = XALLOCAVEC (char, strlen (base) + 64);
8003 char *p;
8004 int i, mark;
8005 unsigned char checksum[16];
8006 struct md5_ctx ctx;
8008 /* Compute the checksum of the DIE, then append part of it as hex digits to
8009 the name filename of the unit. */
8011 md5_init_ctx (&ctx);
8012 mark = 0;
8013 die_checksum (unit_die, &ctx, &mark);
8014 unmark_all_dies (unit_die);
8015 md5_finish_ctx (&ctx, checksum);
8017 /* When we this for comp_unit_die () we have a DW_AT_name that might
8018 not start with a letter but with anything valid for filenames and
8019 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8020 character is not a letter. */
8021 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8022 clean_symbol_name (name);
8024 p = name + strlen (name);
8025 for (i = 0; i < 4; i++)
8027 sprintf (p, "%.2x", checksum[i]);
8028 p += 2;
8031 unit_die->die_id.die_symbol = xstrdup (name);
8034 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
8036 static int
8037 is_type_die (dw_die_ref die)
8039 switch (die->die_tag)
8041 case DW_TAG_array_type:
8042 case DW_TAG_class_type:
8043 case DW_TAG_interface_type:
8044 case DW_TAG_enumeration_type:
8045 case DW_TAG_pointer_type:
8046 case DW_TAG_reference_type:
8047 case DW_TAG_rvalue_reference_type:
8048 case DW_TAG_string_type:
8049 case DW_TAG_structure_type:
8050 case DW_TAG_subroutine_type:
8051 case DW_TAG_union_type:
8052 case DW_TAG_ptr_to_member_type:
8053 case DW_TAG_set_type:
8054 case DW_TAG_subrange_type:
8055 case DW_TAG_base_type:
8056 case DW_TAG_const_type:
8057 case DW_TAG_file_type:
8058 case DW_TAG_packed_type:
8059 case DW_TAG_volatile_type:
8060 case DW_TAG_typedef:
8061 return 1;
8062 default:
8063 return 0;
8067 /* Returns true iff C is a compile-unit DIE. */
8069 static inline bool
8070 is_cu_die (dw_die_ref c)
8072 return c && (c->die_tag == DW_TAG_compile_unit
8073 || c->die_tag == DW_TAG_skeleton_unit);
8076 /* Returns true iff C is a unit DIE of some sort. */
8078 static inline bool
8079 is_unit_die (dw_die_ref c)
8081 return c && (c->die_tag == DW_TAG_compile_unit
8082 || c->die_tag == DW_TAG_partial_unit
8083 || c->die_tag == DW_TAG_type_unit
8084 || c->die_tag == DW_TAG_skeleton_unit);
8087 /* Returns true iff C is a namespace DIE. */
8089 static inline bool
8090 is_namespace_die (dw_die_ref c)
8092 return c && c->die_tag == DW_TAG_namespace;
8095 /* Return non-zero if this DIE is a template parameter. */
8097 static inline bool
8098 is_template_parameter (dw_die_ref die)
8100 switch (die->die_tag)
8102 case DW_TAG_template_type_param:
8103 case DW_TAG_template_value_param:
8104 case DW_TAG_GNU_template_template_param:
8105 case DW_TAG_GNU_template_parameter_pack:
8106 return true;
8107 default:
8108 return false;
8112 /* Return non-zero if this DIE represents a template instantiation. */
8114 static inline bool
8115 is_template_instantiation (dw_die_ref die)
8117 dw_die_ref c;
8119 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8120 return false;
8121 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8122 return false;
8125 static char *
8126 gen_internal_sym (const char *prefix)
8128 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8130 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8131 return xstrdup (buf);
8134 /* Return non-zero if this DIE is a declaration. */
8136 static int
8137 is_declaration_die (dw_die_ref die)
8139 dw_attr_node *a;
8140 unsigned ix;
8142 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8143 if (a->dw_attr == DW_AT_declaration)
8144 return 1;
8146 return 0;
8149 /* Return non-zero if this DIE is nested inside a subprogram. */
8151 static int
8152 is_nested_in_subprogram (dw_die_ref die)
8154 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8156 if (decl == NULL)
8157 decl = die;
8158 return local_scope_p (decl);
8161 /* Return non-zero if this DIE contains a defining declaration of a
8162 subprogram. */
8164 static int
8165 contains_subprogram_definition (dw_die_ref die)
8167 dw_die_ref c;
8169 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8170 return 1;
8171 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8172 return 0;
8175 /* Return non-zero if this is a type DIE that should be moved to a
8176 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8177 unit type. */
8179 static int
8180 should_move_die_to_comdat (dw_die_ref die)
8182 switch (die->die_tag)
8184 case DW_TAG_class_type:
8185 case DW_TAG_structure_type:
8186 case DW_TAG_enumeration_type:
8187 case DW_TAG_union_type:
8188 /* Don't move declarations, inlined instances, types nested in a
8189 subprogram, or types that contain subprogram definitions. */
8190 if (is_declaration_die (die)
8191 || get_AT (die, DW_AT_abstract_origin)
8192 || is_nested_in_subprogram (die)
8193 || contains_subprogram_definition (die))
8194 return 0;
8195 return 1;
8196 case DW_TAG_array_type:
8197 case DW_TAG_interface_type:
8198 case DW_TAG_pointer_type:
8199 case DW_TAG_reference_type:
8200 case DW_TAG_rvalue_reference_type:
8201 case DW_TAG_string_type:
8202 case DW_TAG_subroutine_type:
8203 case DW_TAG_ptr_to_member_type:
8204 case DW_TAG_set_type:
8205 case DW_TAG_subrange_type:
8206 case DW_TAG_base_type:
8207 case DW_TAG_const_type:
8208 case DW_TAG_file_type:
8209 case DW_TAG_packed_type:
8210 case DW_TAG_volatile_type:
8211 case DW_TAG_typedef:
8212 default:
8213 return 0;
8217 /* Make a clone of DIE. */
8219 static dw_die_ref
8220 clone_die (dw_die_ref die)
8222 dw_die_ref clone = new_die_raw (die->die_tag);
8223 dw_attr_node *a;
8224 unsigned ix;
8226 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8227 add_dwarf_attr (clone, a);
8229 return clone;
8232 /* Make a clone of the tree rooted at DIE. */
8234 static dw_die_ref
8235 clone_tree (dw_die_ref die)
8237 dw_die_ref c;
8238 dw_die_ref clone = clone_die (die);
8240 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8242 return clone;
8245 /* Make a clone of DIE as a declaration. */
8247 static dw_die_ref
8248 clone_as_declaration (dw_die_ref die)
8250 dw_die_ref clone;
8251 dw_die_ref decl;
8252 dw_attr_node *a;
8253 unsigned ix;
8255 /* If the DIE is already a declaration, just clone it. */
8256 if (is_declaration_die (die))
8257 return clone_die (die);
8259 /* If the DIE is a specification, just clone its declaration DIE. */
8260 decl = get_AT_ref (die, DW_AT_specification);
8261 if (decl != NULL)
8263 clone = clone_die (decl);
8264 if (die->comdat_type_p)
8265 add_AT_die_ref (clone, DW_AT_signature, die);
8266 return clone;
8269 clone = new_die_raw (die->die_tag);
8271 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8273 /* We don't want to copy over all attributes.
8274 For example we don't want DW_AT_byte_size because otherwise we will no
8275 longer have a declaration and GDB will treat it as a definition. */
8277 switch (a->dw_attr)
8279 case DW_AT_abstract_origin:
8280 case DW_AT_artificial:
8281 case DW_AT_containing_type:
8282 case DW_AT_external:
8283 case DW_AT_name:
8284 case DW_AT_type:
8285 case DW_AT_virtuality:
8286 case DW_AT_linkage_name:
8287 case DW_AT_MIPS_linkage_name:
8288 add_dwarf_attr (clone, a);
8289 break;
8290 case DW_AT_byte_size:
8291 case DW_AT_alignment:
8292 default:
8293 break;
8297 if (die->comdat_type_p)
8298 add_AT_die_ref (clone, DW_AT_signature, die);
8300 add_AT_flag (clone, DW_AT_declaration, 1);
8301 return clone;
8305 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8307 struct decl_table_entry
8309 dw_die_ref orig;
8310 dw_die_ref copy;
8313 /* Helpers to manipulate hash table of copied declarations. */
8315 /* Hashtable helpers. */
8317 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8319 typedef die_struct *compare_type;
8320 static inline hashval_t hash (const decl_table_entry *);
8321 static inline bool equal (const decl_table_entry *, const die_struct *);
8324 inline hashval_t
8325 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8327 return htab_hash_pointer (entry->orig);
8330 inline bool
8331 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8332 const die_struct *entry2)
8334 return entry1->orig == entry2;
8337 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8339 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8340 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8341 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8342 to check if the ancestor has already been copied into UNIT. */
8344 static dw_die_ref
8345 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8346 decl_hash_type *decl_table)
8348 dw_die_ref parent = die->die_parent;
8349 dw_die_ref new_parent = unit;
8350 dw_die_ref copy;
8351 decl_table_entry **slot = NULL;
8352 struct decl_table_entry *entry = NULL;
8354 /* If DIE refers to a stub unfold that so we get the appropriate
8355 DIE registered as orig in decl_table. */
8356 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8357 die = c;
8359 if (decl_table)
8361 /* Check if the entry has already been copied to UNIT. */
8362 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8363 INSERT);
8364 if (*slot != HTAB_EMPTY_ENTRY)
8366 entry = *slot;
8367 return entry->copy;
8370 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8371 entry = XCNEW (struct decl_table_entry);
8372 entry->orig = die;
8373 entry->copy = NULL;
8374 *slot = entry;
8377 if (parent != NULL)
8379 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8380 if (spec != NULL)
8381 parent = spec;
8382 if (!is_unit_die (parent))
8383 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8386 copy = clone_as_declaration (die);
8387 add_child_die (new_parent, copy);
8389 if (decl_table)
8391 /* Record the pointer to the copy. */
8392 entry->copy = copy;
8395 return copy;
8397 /* Copy the declaration context to the new type unit DIE. This includes
8398 any surrounding namespace or type declarations. If the DIE has an
8399 AT_specification attribute, it also includes attributes and children
8400 attached to the specification, and returns a pointer to the original
8401 parent of the declaration DIE. Returns NULL otherwise. */
8403 static dw_die_ref
8404 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8406 dw_die_ref decl;
8407 dw_die_ref new_decl;
8408 dw_die_ref orig_parent = NULL;
8410 decl = get_AT_ref (die, DW_AT_specification);
8411 if (decl == NULL)
8412 decl = die;
8413 else
8415 unsigned ix;
8416 dw_die_ref c;
8417 dw_attr_node *a;
8419 /* The original DIE will be changed to a declaration, and must
8420 be moved to be a child of the original declaration DIE. */
8421 orig_parent = decl->die_parent;
8423 /* Copy the type node pointer from the new DIE to the original
8424 declaration DIE so we can forward references later. */
8425 decl->comdat_type_p = true;
8426 decl->die_id.die_type_node = die->die_id.die_type_node;
8428 remove_AT (die, DW_AT_specification);
8430 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8432 if (a->dw_attr != DW_AT_name
8433 && a->dw_attr != DW_AT_declaration
8434 && a->dw_attr != DW_AT_external)
8435 add_dwarf_attr (die, a);
8438 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8441 if (decl->die_parent != NULL
8442 && !is_unit_die (decl->die_parent))
8444 new_decl = copy_ancestor_tree (unit, decl, NULL);
8445 if (new_decl != NULL)
8447 remove_AT (new_decl, DW_AT_signature);
8448 add_AT_specification (die, new_decl);
8452 return orig_parent;
8455 /* Generate the skeleton ancestor tree for the given NODE, then clone
8456 the DIE and add the clone into the tree. */
8458 static void
8459 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8461 if (node->new_die != NULL)
8462 return;
8464 node->new_die = clone_as_declaration (node->old_die);
8466 if (node->parent != NULL)
8468 generate_skeleton_ancestor_tree (node->parent);
8469 add_child_die (node->parent->new_die, node->new_die);
8473 /* Generate a skeleton tree of DIEs containing any declarations that are
8474 found in the original tree. We traverse the tree looking for declaration
8475 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8477 static void
8478 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8480 skeleton_chain_node node;
8481 dw_die_ref c;
8482 dw_die_ref first;
8483 dw_die_ref prev = NULL;
8484 dw_die_ref next = NULL;
8486 node.parent = parent;
8488 first = c = parent->old_die->die_child;
8489 if (c)
8490 next = c->die_sib;
8491 if (c) do {
8492 if (prev == NULL || prev->die_sib == c)
8493 prev = c;
8494 c = next;
8495 next = (c == first ? NULL : c->die_sib);
8496 node.old_die = c;
8497 node.new_die = NULL;
8498 if (is_declaration_die (c))
8500 if (is_template_instantiation (c))
8502 /* Instantiated templates do not need to be cloned into the
8503 type unit. Just move the DIE and its children back to
8504 the skeleton tree (in the main CU). */
8505 remove_child_with_prev (c, prev);
8506 add_child_die (parent->new_die, c);
8507 c = prev;
8509 else if (c->comdat_type_p)
8511 /* This is the skeleton of earlier break_out_comdat_types
8512 type. Clone the existing DIE, but keep the children
8513 under the original (which is in the main CU). */
8514 dw_die_ref clone = clone_die (c);
8516 replace_child (c, clone, prev);
8517 generate_skeleton_ancestor_tree (parent);
8518 add_child_die (parent->new_die, c);
8519 c = clone;
8520 continue;
8522 else
8524 /* Clone the existing DIE, move the original to the skeleton
8525 tree (which is in the main CU), and put the clone, with
8526 all the original's children, where the original came from
8527 (which is about to be moved to the type unit). */
8528 dw_die_ref clone = clone_die (c);
8529 move_all_children (c, clone);
8531 /* If the original has a DW_AT_object_pointer attribute,
8532 it would now point to a child DIE just moved to the
8533 cloned tree, so we need to remove that attribute from
8534 the original. */
8535 remove_AT (c, DW_AT_object_pointer);
8537 replace_child (c, clone, prev);
8538 generate_skeleton_ancestor_tree (parent);
8539 add_child_die (parent->new_die, c);
8540 node.old_die = clone;
8541 node.new_die = c;
8542 c = clone;
8545 generate_skeleton_bottom_up (&node);
8546 } while (next != NULL);
8549 /* Wrapper function for generate_skeleton_bottom_up. */
8551 static dw_die_ref
8552 generate_skeleton (dw_die_ref die)
8554 skeleton_chain_node node;
8556 node.old_die = die;
8557 node.new_die = NULL;
8558 node.parent = NULL;
8560 /* If this type definition is nested inside another type,
8561 and is not an instantiation of a template, always leave
8562 at least a declaration in its place. */
8563 if (die->die_parent != NULL
8564 && is_type_die (die->die_parent)
8565 && !is_template_instantiation (die))
8566 node.new_die = clone_as_declaration (die);
8568 generate_skeleton_bottom_up (&node);
8569 return node.new_die;
8572 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8573 declaration. The original DIE is moved to a new compile unit so that
8574 existing references to it follow it to the new location. If any of the
8575 original DIE's descendants is a declaration, we need to replace the
8576 original DIE with a skeleton tree and move the declarations back into the
8577 skeleton tree. */
8579 static dw_die_ref
8580 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8581 dw_die_ref prev)
8583 dw_die_ref skeleton, orig_parent;
8585 /* Copy the declaration context to the type unit DIE. If the returned
8586 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8587 that DIE. */
8588 orig_parent = copy_declaration_context (unit, child);
8590 skeleton = generate_skeleton (child);
8591 if (skeleton == NULL)
8592 remove_child_with_prev (child, prev);
8593 else
8595 skeleton->comdat_type_p = true;
8596 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8598 /* If the original DIE was a specification, we need to put
8599 the skeleton under the parent DIE of the declaration.
8600 This leaves the original declaration in the tree, but
8601 it will be pruned later since there are no longer any
8602 references to it. */
8603 if (orig_parent != NULL)
8605 remove_child_with_prev (child, prev);
8606 add_child_die (orig_parent, skeleton);
8608 else
8609 replace_child (child, skeleton, prev);
8612 return skeleton;
8615 static void
8616 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8617 comdat_type_node *type_node,
8618 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8620 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8621 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8622 DWARF procedure references in the DW_AT_location attribute. */
8624 static dw_die_ref
8625 copy_dwarf_procedure (dw_die_ref die,
8626 comdat_type_node *type_node,
8627 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8629 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8631 /* DWARF procedures are not supposed to have children... */
8632 gcc_assert (die->die_child == NULL);
8634 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8635 gcc_assert (vec_safe_length (die->die_attr) == 1
8636 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8638 /* Do not copy more than once DWARF procedures. */
8639 bool existed;
8640 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8641 if (existed)
8642 return die_copy;
8644 die_copy = clone_die (die);
8645 add_child_die (type_node->root_die, die_copy);
8646 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8647 return die_copy;
8650 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8651 procedures in DIE's attributes. */
8653 static void
8654 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8655 comdat_type_node *type_node,
8656 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8658 dw_attr_node *a;
8659 unsigned i;
8661 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8663 dw_loc_descr_ref loc;
8665 if (a->dw_attr_val.val_class != dw_val_class_loc)
8666 continue;
8668 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8670 switch (loc->dw_loc_opc)
8672 case DW_OP_call2:
8673 case DW_OP_call4:
8674 case DW_OP_call_ref:
8675 gcc_assert (loc->dw_loc_oprnd1.val_class
8676 == dw_val_class_die_ref);
8677 loc->dw_loc_oprnd1.v.val_die_ref.die
8678 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8679 type_node,
8680 copied_dwarf_procs);
8682 default:
8683 break;
8689 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8690 rewrite references to point to the copies.
8692 References are looked for in DIE's attributes and recursively in all its
8693 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8694 mapping from old DWARF procedures to their copy. It is used not to copy
8695 twice the same DWARF procedure under TYPE_NODE. */
8697 static void
8698 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8699 comdat_type_node *type_node,
8700 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8702 dw_die_ref c;
8704 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8705 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8706 type_node,
8707 copied_dwarf_procs));
8710 /* Traverse the DIE and set up additional .debug_types or .debug_info
8711 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8712 section. */
8714 static void
8715 break_out_comdat_types (dw_die_ref die)
8717 dw_die_ref c;
8718 dw_die_ref first;
8719 dw_die_ref prev = NULL;
8720 dw_die_ref next = NULL;
8721 dw_die_ref unit = NULL;
8723 first = c = die->die_child;
8724 if (c)
8725 next = c->die_sib;
8726 if (c) do {
8727 if (prev == NULL || prev->die_sib == c)
8728 prev = c;
8729 c = next;
8730 next = (c == first ? NULL : c->die_sib);
8731 if (should_move_die_to_comdat (c))
8733 dw_die_ref replacement;
8734 comdat_type_node *type_node;
8736 /* Break out nested types into their own type units. */
8737 break_out_comdat_types (c);
8739 /* Create a new type unit DIE as the root for the new tree. */
8740 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8741 add_AT_unsigned (unit, DW_AT_language,
8742 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8744 /* Add the new unit's type DIE into the comdat type list. */
8745 type_node = ggc_cleared_alloc<comdat_type_node> ();
8746 type_node->root_die = unit;
8747 type_node->next = comdat_type_list;
8748 comdat_type_list = type_node;
8750 /* Generate the type signature. */
8751 generate_type_signature (c, type_node);
8753 /* Copy the declaration context, attributes, and children of the
8754 declaration into the new type unit DIE, then remove this DIE
8755 from the main CU (or replace it with a skeleton if necessary). */
8756 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8757 type_node->skeleton_die = replacement;
8759 /* Add the DIE to the new compunit. */
8760 add_child_die (unit, c);
8762 /* Types can reference DWARF procedures for type size or data location
8763 expressions. Calls in DWARF expressions cannot target procedures
8764 that are not in the same section. So we must copy DWARF procedures
8765 along with this type and then rewrite references to them. */
8766 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8767 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8769 if (replacement != NULL)
8770 c = replacement;
8772 else if (c->die_tag == DW_TAG_namespace
8773 || c->die_tag == DW_TAG_class_type
8774 || c->die_tag == DW_TAG_structure_type
8775 || c->die_tag == DW_TAG_union_type)
8777 /* Look for nested types that can be broken out. */
8778 break_out_comdat_types (c);
8780 } while (next != NULL);
8783 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8784 Enter all the cloned children into the hash table decl_table. */
8786 static dw_die_ref
8787 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8789 dw_die_ref c;
8790 dw_die_ref clone;
8791 struct decl_table_entry *entry;
8792 decl_table_entry **slot;
8794 if (die->die_tag == DW_TAG_subprogram)
8795 clone = clone_as_declaration (die);
8796 else
8797 clone = clone_die (die);
8799 slot = decl_table->find_slot_with_hash (die,
8800 htab_hash_pointer (die), INSERT);
8802 /* Assert that DIE isn't in the hash table yet. If it would be there
8803 before, the ancestors would be necessarily there as well, therefore
8804 clone_tree_partial wouldn't be called. */
8805 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8807 entry = XCNEW (struct decl_table_entry);
8808 entry->orig = die;
8809 entry->copy = clone;
8810 *slot = entry;
8812 if (die->die_tag != DW_TAG_subprogram)
8813 FOR_EACH_CHILD (die, c,
8814 add_child_die (clone, clone_tree_partial (c, decl_table)));
8816 return clone;
8819 /* Walk the DIE and its children, looking for references to incomplete
8820 or trivial types that are unmarked (i.e., that are not in the current
8821 type_unit). */
8823 static void
8824 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8826 dw_die_ref c;
8827 dw_attr_node *a;
8828 unsigned ix;
8830 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8832 if (AT_class (a) == dw_val_class_die_ref)
8834 dw_die_ref targ = AT_ref (a);
8835 decl_table_entry **slot;
8836 struct decl_table_entry *entry;
8838 if (targ->die_mark != 0 || targ->comdat_type_p)
8839 continue;
8841 slot = decl_table->find_slot_with_hash (targ,
8842 htab_hash_pointer (targ),
8843 INSERT);
8845 if (*slot != HTAB_EMPTY_ENTRY)
8847 /* TARG has already been copied, so we just need to
8848 modify the reference to point to the copy. */
8849 entry = *slot;
8850 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8852 else
8854 dw_die_ref parent = unit;
8855 dw_die_ref copy = clone_die (targ);
8857 /* Record in DECL_TABLE that TARG has been copied.
8858 Need to do this now, before the recursive call,
8859 because DECL_TABLE may be expanded and SLOT
8860 would no longer be a valid pointer. */
8861 entry = XCNEW (struct decl_table_entry);
8862 entry->orig = targ;
8863 entry->copy = copy;
8864 *slot = entry;
8866 /* If TARG is not a declaration DIE, we need to copy its
8867 children. */
8868 if (!is_declaration_die (targ))
8870 FOR_EACH_CHILD (
8871 targ, c,
8872 add_child_die (copy,
8873 clone_tree_partial (c, decl_table)));
8876 /* Make sure the cloned tree is marked as part of the
8877 type unit. */
8878 mark_dies (copy);
8880 /* If TARG has surrounding context, copy its ancestor tree
8881 into the new type unit. */
8882 if (targ->die_parent != NULL
8883 && !is_unit_die (targ->die_parent))
8884 parent = copy_ancestor_tree (unit, targ->die_parent,
8885 decl_table);
8887 add_child_die (parent, copy);
8888 a->dw_attr_val.v.val_die_ref.die = copy;
8890 /* Make sure the newly-copied DIE is walked. If it was
8891 installed in a previously-added context, it won't
8892 get visited otherwise. */
8893 if (parent != unit)
8895 /* Find the highest point of the newly-added tree,
8896 mark each node along the way, and walk from there. */
8897 parent->die_mark = 1;
8898 while (parent->die_parent
8899 && parent->die_parent->die_mark == 0)
8901 parent = parent->die_parent;
8902 parent->die_mark = 1;
8904 copy_decls_walk (unit, parent, decl_table);
8910 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8913 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8914 and record them in DECL_TABLE. */
8916 static void
8917 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8919 dw_die_ref c;
8921 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8923 dw_die_ref targ = AT_ref (a);
8924 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8925 decl_table_entry **slot
8926 = decl_table->find_slot_with_hash (targ,
8927 htab_hash_pointer (targ),
8928 INSERT);
8929 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8930 /* Record in DECL_TABLE that TARG has been already copied
8931 by remove_child_or_replace_with_skeleton. */
8932 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8933 entry->orig = targ;
8934 entry->copy = die;
8935 *slot = entry;
8937 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8940 /* Copy declarations for "unworthy" types into the new comdat section.
8941 Incomplete types, modified types, and certain other types aren't broken
8942 out into comdat sections of their own, so they don't have a signature,
8943 and we need to copy the declaration into the same section so that we
8944 don't have an external reference. */
8946 static void
8947 copy_decls_for_unworthy_types (dw_die_ref unit)
8949 mark_dies (unit);
8950 decl_hash_type decl_table (10);
8951 collect_skeleton_dies (unit, &decl_table);
8952 copy_decls_walk (unit, unit, &decl_table);
8953 unmark_dies (unit);
8956 /* Traverse the DIE and add a sibling attribute if it may have the
8957 effect of speeding up access to siblings. To save some space,
8958 avoid generating sibling attributes for DIE's without children. */
8960 static void
8961 add_sibling_attributes (dw_die_ref die)
8963 dw_die_ref c;
8965 if (! die->die_child)
8966 return;
8968 if (die->die_parent && die != die->die_parent->die_child)
8969 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8971 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8974 /* Output all location lists for the DIE and its children. */
8976 static void
8977 output_location_lists (dw_die_ref die)
8979 dw_die_ref c;
8980 dw_attr_node *a;
8981 unsigned ix;
8983 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8984 if (AT_class (a) == dw_val_class_loc_list)
8985 output_loc_list (AT_loc_list (a));
8987 FOR_EACH_CHILD (die, c, output_location_lists (c));
8990 /* During assign_location_list_indexes and output_loclists_offset the
8991 current index, after it the number of assigned indexes (i.e. how
8992 large the .debug_loclists* offset table should be). */
8993 static unsigned int loc_list_idx;
8995 /* Output all location list offsets for the DIE and its children. */
8997 static void
8998 output_loclists_offsets (dw_die_ref die)
9000 dw_die_ref c;
9001 dw_attr_node *a;
9002 unsigned ix;
9004 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9005 if (AT_class (a) == dw_val_class_loc_list)
9007 dw_loc_list_ref l = AT_loc_list (a);
9008 if (l->offset_emitted)
9009 continue;
9010 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9011 loc_section_label, NULL);
9012 gcc_assert (l->hash == loc_list_idx);
9013 loc_list_idx++;
9014 l->offset_emitted = true;
9017 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9020 /* Recursively set indexes of location lists. */
9022 static void
9023 assign_location_list_indexes (dw_die_ref die)
9025 dw_die_ref c;
9026 dw_attr_node *a;
9027 unsigned ix;
9029 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9030 if (AT_class (a) == dw_val_class_loc_list)
9032 dw_loc_list_ref list = AT_loc_list (a);
9033 if (!list->num_assigned)
9035 list->num_assigned = true;
9036 list->hash = loc_list_idx++;
9040 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9043 /* We want to limit the number of external references, because they are
9044 larger than local references: a relocation takes multiple words, and
9045 even a sig8 reference is always eight bytes, whereas a local reference
9046 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9047 So if we encounter multiple external references to the same type DIE, we
9048 make a local typedef stub for it and redirect all references there.
9050 This is the element of the hash table for keeping track of these
9051 references. */
9053 struct external_ref
9055 dw_die_ref type;
9056 dw_die_ref stub;
9057 unsigned n_refs;
9060 /* Hashtable helpers. */
9062 struct external_ref_hasher : free_ptr_hash <external_ref>
9064 static inline hashval_t hash (const external_ref *);
9065 static inline bool equal (const external_ref *, const external_ref *);
9068 inline hashval_t
9069 external_ref_hasher::hash (const external_ref *r)
9071 dw_die_ref die = r->type;
9072 hashval_t h = 0;
9074 /* We can't use the address of the DIE for hashing, because
9075 that will make the order of the stub DIEs non-deterministic. */
9076 if (! die->comdat_type_p)
9077 /* We have a symbol; use it to compute a hash. */
9078 h = htab_hash_string (die->die_id.die_symbol);
9079 else
9081 /* We have a type signature; use a subset of the bits as the hash.
9082 The 8-byte signature is at least as large as hashval_t. */
9083 comdat_type_node *type_node = die->die_id.die_type_node;
9084 memcpy (&h, type_node->signature, sizeof (h));
9086 return h;
9089 inline bool
9090 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9092 return r1->type == r2->type;
9095 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9097 /* Return a pointer to the external_ref for references to DIE. */
9099 static struct external_ref *
9100 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9102 struct external_ref ref, *ref_p;
9103 external_ref **slot;
9105 ref.type = die;
9106 slot = map->find_slot (&ref, INSERT);
9107 if (*slot != HTAB_EMPTY_ENTRY)
9108 return *slot;
9110 ref_p = XCNEW (struct external_ref);
9111 ref_p->type = die;
9112 *slot = ref_p;
9113 return ref_p;
9116 /* Subroutine of optimize_external_refs, below.
9118 If we see a type skeleton, record it as our stub. If we see external
9119 references, remember how many we've seen. */
9121 static void
9122 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9124 dw_die_ref c;
9125 dw_attr_node *a;
9126 unsigned ix;
9127 struct external_ref *ref_p;
9129 if (is_type_die (die)
9130 && (c = get_AT_ref (die, DW_AT_signature)))
9132 /* This is a local skeleton; use it for local references. */
9133 ref_p = lookup_external_ref (map, c);
9134 ref_p->stub = die;
9137 /* Scan the DIE references, and remember any that refer to DIEs from
9138 other CUs (i.e. those which are not marked). */
9139 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9140 if (AT_class (a) == dw_val_class_die_ref
9141 && (c = AT_ref (a))->die_mark == 0
9142 && is_type_die (c))
9144 ref_p = lookup_external_ref (map, c);
9145 ref_p->n_refs++;
9148 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9151 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9152 points to an external_ref, DATA is the CU we're processing. If we don't
9153 already have a local stub, and we have multiple refs, build a stub. */
9156 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9158 struct external_ref *ref_p = *slot;
9160 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9162 /* We have multiple references to this type, so build a small stub.
9163 Both of these forms are a bit dodgy from the perspective of the
9164 DWARF standard, since technically they should have names. */
9165 dw_die_ref cu = data;
9166 dw_die_ref type = ref_p->type;
9167 dw_die_ref stub = NULL;
9169 if (type->comdat_type_p)
9171 /* If we refer to this type via sig8, use AT_signature. */
9172 stub = new_die (type->die_tag, cu, NULL_TREE);
9173 add_AT_die_ref (stub, DW_AT_signature, type);
9175 else
9177 /* Otherwise, use a typedef with no name. */
9178 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9179 add_AT_die_ref (stub, DW_AT_type, type);
9182 stub->die_mark++;
9183 ref_p->stub = stub;
9185 return 1;
9188 /* DIE is a unit; look through all the DIE references to see if there are
9189 any external references to types, and if so, create local stubs for
9190 them which will be applied in build_abbrev_table. This is useful because
9191 references to local DIEs are smaller. */
9193 static external_ref_hash_type *
9194 optimize_external_refs (dw_die_ref die)
9196 external_ref_hash_type *map = new external_ref_hash_type (10);
9197 optimize_external_refs_1 (die, map);
9198 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9199 return map;
9202 /* The following 3 variables are temporaries that are computed only during the
9203 build_abbrev_table call and used and released during the following
9204 optimize_abbrev_table call. */
9206 /* First abbrev_id that can be optimized based on usage. */
9207 static unsigned int abbrev_opt_start;
9209 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9210 abbrev_id smaller than this, because they must be already sized
9211 during build_abbrev_table). */
9212 static unsigned int abbrev_opt_base_type_end;
9214 /* Vector of usage counts during build_abbrev_table. Indexed by
9215 abbrev_id - abbrev_opt_start. */
9216 static vec<unsigned int> abbrev_usage_count;
9218 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9219 static vec<dw_die_ref> sorted_abbrev_dies;
9221 /* The format of each DIE (and its attribute value pairs) is encoded in an
9222 abbreviation table. This routine builds the abbreviation table and assigns
9223 a unique abbreviation id for each abbreviation entry. The children of each
9224 die are visited recursively. */
9226 static void
9227 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9229 unsigned int abbrev_id = 0;
9230 dw_die_ref c;
9231 dw_attr_node *a;
9232 unsigned ix;
9233 dw_die_ref abbrev;
9235 /* Scan the DIE references, and replace any that refer to
9236 DIEs from other CUs (i.e. those which are not marked) with
9237 the local stubs we built in optimize_external_refs. */
9238 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9239 if (AT_class (a) == dw_val_class_die_ref
9240 && (c = AT_ref (a))->die_mark == 0)
9242 struct external_ref *ref_p;
9243 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9245 if (is_type_die (c)
9246 && (ref_p = lookup_external_ref (extern_map, c))
9247 && ref_p->stub && ref_p->stub != die)
9249 gcc_assert (a->dw_attr != DW_AT_signature);
9250 change_AT_die_ref (a, ref_p->stub);
9252 else
9253 /* We aren't changing this reference, so mark it external. */
9254 set_AT_ref_external (a, 1);
9257 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9259 dw_attr_node *die_a, *abbrev_a;
9260 unsigned ix;
9261 bool ok = true;
9263 if (abbrev_id == 0)
9264 continue;
9265 if (abbrev->die_tag != die->die_tag)
9266 continue;
9267 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9268 continue;
9270 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9271 continue;
9273 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9275 abbrev_a = &(*abbrev->die_attr)[ix];
9276 if ((abbrev_a->dw_attr != die_a->dw_attr)
9277 || (value_format (abbrev_a) != value_format (die_a)))
9279 ok = false;
9280 break;
9283 if (ok)
9284 break;
9287 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9289 vec_safe_push (abbrev_die_table, die);
9290 if (abbrev_opt_start)
9291 abbrev_usage_count.safe_push (0);
9293 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9295 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9296 sorted_abbrev_dies.safe_push (die);
9299 die->die_abbrev = abbrev_id;
9300 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9303 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9304 by die_abbrev's usage count, from the most commonly used
9305 abbreviation to the least. */
9307 static int
9308 die_abbrev_cmp (const void *p1, const void *p2)
9310 dw_die_ref die1 = *(const dw_die_ref *) p1;
9311 dw_die_ref die2 = *(const dw_die_ref *) p2;
9313 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9314 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9316 if (die1->die_abbrev >= abbrev_opt_base_type_end
9317 && die2->die_abbrev >= abbrev_opt_base_type_end)
9319 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9320 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9321 return -1;
9322 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9323 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9324 return 1;
9327 /* Stabilize the sort. */
9328 if (die1->die_abbrev < die2->die_abbrev)
9329 return -1;
9330 if (die1->die_abbrev > die2->die_abbrev)
9331 return 1;
9333 return 0;
9336 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9337 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9338 into dw_val_class_const_implicit or
9339 dw_val_class_unsigned_const_implicit. */
9341 static void
9342 optimize_implicit_const (unsigned int first_id, unsigned int end,
9343 vec<bool> &implicit_consts)
9345 /* It never makes sense if there is just one DIE using the abbreviation. */
9346 if (end < first_id + 2)
9347 return;
9349 dw_attr_node *a;
9350 unsigned ix, i;
9351 dw_die_ref die = sorted_abbrev_dies[first_id];
9352 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9353 if (implicit_consts[ix])
9355 enum dw_val_class new_class = dw_val_class_none;
9356 switch (AT_class (a))
9358 case dw_val_class_unsigned_const:
9359 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9360 continue;
9362 /* The .debug_abbrev section will grow by
9363 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9364 in all the DIEs using that abbreviation. */
9365 if (constant_size (AT_unsigned (a)) * (end - first_id)
9366 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9367 continue;
9369 new_class = dw_val_class_unsigned_const_implicit;
9370 break;
9372 case dw_val_class_const:
9373 new_class = dw_val_class_const_implicit;
9374 break;
9376 case dw_val_class_file:
9377 new_class = dw_val_class_file_implicit;
9378 break;
9380 default:
9381 continue;
9383 for (i = first_id; i < end; i++)
9384 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9385 = new_class;
9389 /* Attempt to optimize abbreviation table from abbrev_opt_start
9390 abbreviation above. */
9392 static void
9393 optimize_abbrev_table (void)
9395 if (abbrev_opt_start
9396 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9397 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9399 auto_vec<bool, 32> implicit_consts;
9400 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9402 unsigned int abbrev_id = abbrev_opt_start - 1;
9403 unsigned int first_id = ~0U;
9404 unsigned int last_abbrev_id = 0;
9405 unsigned int i;
9406 dw_die_ref die;
9407 if (abbrev_opt_base_type_end > abbrev_opt_start)
9408 abbrev_id = abbrev_opt_base_type_end - 1;
9409 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9410 most commonly used abbreviations come first. */
9411 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9413 dw_attr_node *a;
9414 unsigned ix;
9416 /* If calc_base_type_die_sizes has been called, the CU and
9417 base types after it can't be optimized, because we've already
9418 calculated their DIE offsets. We've sorted them first. */
9419 if (die->die_abbrev < abbrev_opt_base_type_end)
9420 continue;
9421 if (die->die_abbrev != last_abbrev_id)
9423 last_abbrev_id = die->die_abbrev;
9424 if (dwarf_version >= 5 && first_id != ~0U)
9425 optimize_implicit_const (first_id, i, implicit_consts);
9426 abbrev_id++;
9427 (*abbrev_die_table)[abbrev_id] = die;
9428 if (dwarf_version >= 5)
9430 first_id = i;
9431 implicit_consts.truncate (0);
9433 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9434 switch (AT_class (a))
9436 case dw_val_class_const:
9437 case dw_val_class_unsigned_const:
9438 case dw_val_class_file:
9439 implicit_consts.safe_push (true);
9440 break;
9441 default:
9442 implicit_consts.safe_push (false);
9443 break;
9447 else if (dwarf_version >= 5)
9449 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9450 if (!implicit_consts[ix])
9451 continue;
9452 else
9454 dw_attr_node *other_a
9455 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9456 if (!dw_val_equal_p (&a->dw_attr_val,
9457 &other_a->dw_attr_val))
9458 implicit_consts[ix] = false;
9461 die->die_abbrev = abbrev_id;
9463 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9464 if (dwarf_version >= 5 && first_id != ~0U)
9465 optimize_implicit_const (first_id, i, implicit_consts);
9468 abbrev_opt_start = 0;
9469 abbrev_opt_base_type_end = 0;
9470 abbrev_usage_count.release ();
9471 sorted_abbrev_dies.release ();
9474 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9476 static int
9477 constant_size (unsigned HOST_WIDE_INT value)
9479 int log;
9481 if (value == 0)
9482 log = 0;
9483 else
9484 log = floor_log2 (value);
9486 log = log / 8;
9487 log = 1 << (floor_log2 (log) + 1);
9489 return log;
9492 /* Return the size of a DIE as it is represented in the
9493 .debug_info section. */
9495 static unsigned long
9496 size_of_die (dw_die_ref die)
9498 unsigned long size = 0;
9499 dw_attr_node *a;
9500 unsigned ix;
9501 enum dwarf_form form;
9503 size += size_of_uleb128 (die->die_abbrev);
9504 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9506 switch (AT_class (a))
9508 case dw_val_class_addr:
9509 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9511 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9512 size += size_of_uleb128 (AT_index (a));
9514 else
9515 size += DWARF2_ADDR_SIZE;
9516 break;
9517 case dw_val_class_offset:
9518 size += dwarf_offset_size;
9519 break;
9520 case dw_val_class_loc:
9522 unsigned long lsize = size_of_locs (AT_loc (a));
9524 /* Block length. */
9525 if (dwarf_version >= 4)
9526 size += size_of_uleb128 (lsize);
9527 else
9528 size += constant_size (lsize);
9529 size += lsize;
9531 break;
9532 case dw_val_class_loc_list:
9533 if (dwarf_split_debug_info && dwarf_version >= 5)
9535 gcc_assert (AT_loc_list (a)->num_assigned);
9536 size += size_of_uleb128 (AT_loc_list (a)->hash);
9538 else
9539 size += dwarf_offset_size;
9540 break;
9541 case dw_val_class_view_list:
9542 size += dwarf_offset_size;
9543 break;
9544 case dw_val_class_range_list:
9545 if (value_format (a) == DW_FORM_rnglistx)
9547 gcc_assert (rnglist_idx);
9548 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9549 size += size_of_uleb128 (r->idx);
9551 else
9552 size += dwarf_offset_size;
9553 break;
9554 case dw_val_class_const:
9555 size += size_of_sleb128 (AT_int (a));
9556 break;
9557 case dw_val_class_unsigned_const:
9559 int csize = constant_size (AT_unsigned (a));
9560 if (dwarf_version == 3
9561 && a->dw_attr == DW_AT_data_member_location
9562 && csize >= 4)
9563 size += size_of_uleb128 (AT_unsigned (a));
9564 else
9565 size += csize;
9567 break;
9568 case dw_val_class_symview:
9569 if (symview_upper_bound <= 0xff)
9570 size += 1;
9571 else if (symview_upper_bound <= 0xffff)
9572 size += 2;
9573 else if (symview_upper_bound <= 0xffffffff)
9574 size += 4;
9575 else
9576 size += 8;
9577 break;
9578 case dw_val_class_const_implicit:
9579 case dw_val_class_unsigned_const_implicit:
9580 case dw_val_class_file_implicit:
9581 /* These occupy no size in the DIE, just an extra sleb128 in
9582 .debug_abbrev. */
9583 break;
9584 case dw_val_class_const_double:
9585 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9586 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9587 size++; /* block */
9588 break;
9589 case dw_val_class_wide_int:
9590 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9591 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9592 if (get_full_len (*a->dw_attr_val.v.val_wide)
9593 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9594 size++; /* block */
9595 break;
9596 case dw_val_class_vec:
9597 size += constant_size (a->dw_attr_val.v.val_vec.length
9598 * a->dw_attr_val.v.val_vec.elt_size)
9599 + a->dw_attr_val.v.val_vec.length
9600 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9601 break;
9602 case dw_val_class_flag:
9603 if (dwarf_version >= 4)
9604 /* Currently all add_AT_flag calls pass in 1 as last argument,
9605 so DW_FORM_flag_present can be used. If that ever changes,
9606 we'll need to use DW_FORM_flag and have some optimization
9607 in build_abbrev_table that will change those to
9608 DW_FORM_flag_present if it is set to 1 in all DIEs using
9609 the same abbrev entry. */
9610 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9611 else
9612 size += 1;
9613 break;
9614 case dw_val_class_die_ref:
9615 if (AT_ref_external (a))
9617 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9618 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9619 is sized by target address length, whereas in DWARF3
9620 it's always sized as an offset. */
9621 if (AT_ref (a)->comdat_type_p)
9622 size += DWARF_TYPE_SIGNATURE_SIZE;
9623 else if (dwarf_version == 2)
9624 size += DWARF2_ADDR_SIZE;
9625 else
9626 size += dwarf_offset_size;
9628 else
9629 size += dwarf_offset_size;
9630 break;
9631 case dw_val_class_fde_ref:
9632 size += dwarf_offset_size;
9633 break;
9634 case dw_val_class_lbl_id:
9635 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9637 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9638 size += size_of_uleb128 (AT_index (a));
9640 else
9641 size += DWARF2_ADDR_SIZE;
9642 break;
9643 case dw_val_class_lineptr:
9644 case dw_val_class_macptr:
9645 case dw_val_class_loclistsptr:
9646 size += dwarf_offset_size;
9647 break;
9648 case dw_val_class_str:
9649 form = AT_string_form (a);
9650 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9651 size += dwarf_offset_size;
9652 else if (form == dwarf_FORM (DW_FORM_strx))
9653 size += size_of_uleb128 (AT_index (a));
9654 else
9655 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9656 break;
9657 case dw_val_class_file:
9658 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9659 break;
9660 case dw_val_class_data8:
9661 size += 8;
9662 break;
9663 case dw_val_class_vms_delta:
9664 size += dwarf_offset_size;
9665 break;
9666 case dw_val_class_high_pc:
9667 size += DWARF2_ADDR_SIZE;
9668 break;
9669 case dw_val_class_discr_value:
9670 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9671 break;
9672 case dw_val_class_discr_list:
9674 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9676 /* This is a block, so we have the block length and then its
9677 data. */
9678 size += constant_size (block_size) + block_size;
9680 break;
9681 default:
9682 gcc_unreachable ();
9686 return size;
9689 /* Size the debugging information associated with a given DIE. Visits the
9690 DIE's children recursively. Updates the global variable next_die_offset, on
9691 each time through. Uses the current value of next_die_offset to update the
9692 die_offset field in each DIE. */
9694 static void
9695 calc_die_sizes (dw_die_ref die)
9697 dw_die_ref c;
9699 gcc_assert (die->die_offset == 0
9700 || (unsigned long int) die->die_offset == next_die_offset);
9701 die->die_offset = next_die_offset;
9702 next_die_offset += size_of_die (die);
9704 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9706 if (die->die_child != NULL)
9707 /* Count the null byte used to terminate sibling lists. */
9708 next_die_offset += 1;
9711 /* Size just the base type children at the start of the CU.
9712 This is needed because build_abbrev needs to size locs
9713 and sizing of type based stack ops needs to know die_offset
9714 values for the base types. */
9716 static void
9717 calc_base_type_die_sizes (void)
9719 unsigned long die_offset = (dwarf_split_debug_info
9720 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9721 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9722 unsigned int i;
9723 dw_die_ref base_type;
9724 #if ENABLE_ASSERT_CHECKING
9725 dw_die_ref prev = comp_unit_die ()->die_child;
9726 #endif
9728 die_offset += size_of_die (comp_unit_die ());
9729 for (i = 0; base_types.iterate (i, &base_type); i++)
9731 #if ENABLE_ASSERT_CHECKING
9732 gcc_assert (base_type->die_offset == 0
9733 && prev->die_sib == base_type
9734 && base_type->die_child == NULL
9735 && base_type->die_abbrev);
9736 prev = base_type;
9737 #endif
9738 if (abbrev_opt_start
9739 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9740 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9741 base_type->die_offset = die_offset;
9742 die_offset += size_of_die (base_type);
9746 /* Set the marks for a die and its children. We do this so
9747 that we know whether or not a reference needs to use FORM_ref_addr; only
9748 DIEs in the same CU will be marked. We used to clear out the offset
9749 and use that as the flag, but ran into ordering problems. */
9751 static void
9752 mark_dies (dw_die_ref die)
9754 dw_die_ref c;
9756 gcc_assert (!die->die_mark);
9758 die->die_mark = 1;
9759 FOR_EACH_CHILD (die, c, mark_dies (c));
9762 /* Clear the marks for a die and its children. */
9764 static void
9765 unmark_dies (dw_die_ref die)
9767 dw_die_ref c;
9769 if (! use_debug_types)
9770 gcc_assert (die->die_mark);
9772 die->die_mark = 0;
9773 FOR_EACH_CHILD (die, c, unmark_dies (c));
9776 /* Clear the marks for a die, its children and referred dies. */
9778 static void
9779 unmark_all_dies (dw_die_ref die)
9781 dw_die_ref c;
9782 dw_attr_node *a;
9783 unsigned ix;
9785 if (!die->die_mark)
9786 return;
9787 die->die_mark = 0;
9789 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9791 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9792 if (AT_class (a) == dw_val_class_die_ref)
9793 unmark_all_dies (AT_ref (a));
9796 /* Calculate if the entry should appear in the final output file. It may be
9797 from a pruned a type. */
9799 static bool
9800 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9802 /* By limiting gnu pubnames to definitions only, gold can generate a
9803 gdb index without entries for declarations, which don't include
9804 enough information to be useful. */
9805 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9806 return false;
9808 if (table == pubname_table)
9810 /* Enumerator names are part of the pubname table, but the
9811 parent DW_TAG_enumeration_type die may have been pruned.
9812 Don't output them if that is the case. */
9813 if (p->die->die_tag == DW_TAG_enumerator &&
9814 (p->die->die_parent == NULL
9815 || !p->die->die_parent->die_perennial_p))
9816 return false;
9818 /* Everything else in the pubname table is included. */
9819 return true;
9822 /* The pubtypes table shouldn't include types that have been
9823 pruned. */
9824 return (p->die->die_offset != 0
9825 || !flag_eliminate_unused_debug_types);
9828 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9829 generated for the compilation unit. */
9831 static unsigned long
9832 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9834 unsigned long size;
9835 unsigned i;
9836 pubname_entry *p;
9837 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9839 size = DWARF_PUBNAMES_HEADER_SIZE;
9840 FOR_EACH_VEC_ELT (*names, i, p)
9841 if (include_pubname_in_output (names, p))
9842 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9844 size += dwarf_offset_size;
9845 return size;
9848 /* Return the size of the information in the .debug_aranges section. */
9850 static unsigned long
9851 size_of_aranges (void)
9853 unsigned long size;
9855 size = DWARF_ARANGES_HEADER_SIZE;
9857 /* Count the address/length pair for this compilation unit. */
9858 if (switch_text_ranges)
9859 size += 2 * DWARF2_ADDR_SIZE
9860 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9861 if (switch_cold_ranges)
9862 size += 2 * DWARF2_ADDR_SIZE
9863 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9864 if (have_multiple_function_sections)
9866 unsigned fde_idx;
9867 dw_fde_ref fde;
9869 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9871 if (fde->ignored_debug)
9872 continue;
9873 if (!fde->in_std_section)
9874 size += 2 * DWARF2_ADDR_SIZE;
9875 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9876 size += 2 * DWARF2_ADDR_SIZE;
9880 /* Count the two zero words used to terminated the address range table. */
9881 size += 2 * DWARF2_ADDR_SIZE;
9882 return size;
9885 /* Select the encoding of an attribute value. */
9887 static enum dwarf_form
9888 value_format (dw_attr_node *a)
9890 switch (AT_class (a))
9892 case dw_val_class_addr:
9893 /* Only very few attributes allow DW_FORM_addr. */
9894 switch (a->dw_attr)
9896 case DW_AT_low_pc:
9897 case DW_AT_high_pc:
9898 case DW_AT_entry_pc:
9899 case DW_AT_trampoline:
9900 return (AT_index (a) == NOT_INDEXED
9901 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9902 default:
9903 break;
9905 switch (DWARF2_ADDR_SIZE)
9907 case 1:
9908 return DW_FORM_data1;
9909 case 2:
9910 return DW_FORM_data2;
9911 case 4:
9912 return DW_FORM_data4;
9913 case 8:
9914 return DW_FORM_data8;
9915 default:
9916 gcc_unreachable ();
9918 case dw_val_class_loc_list:
9919 if (dwarf_split_debug_info
9920 && dwarf_version >= 5
9921 && AT_loc_list (a)->num_assigned)
9922 return DW_FORM_loclistx;
9923 /* FALLTHRU */
9924 case dw_val_class_view_list:
9925 case dw_val_class_range_list:
9926 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9927 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9928 care about sizes of .debug* sections in shared libraries and
9929 executables and don't take into account relocations that affect just
9930 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9931 table in the .debug_rnglists section. */
9932 if (dwarf_split_debug_info
9933 && dwarf_version >= 5
9934 && AT_class (a) == dw_val_class_range_list
9935 && rnglist_idx
9936 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9937 return DW_FORM_rnglistx;
9938 if (dwarf_version >= 4)
9939 return DW_FORM_sec_offset;
9940 /* FALLTHRU */
9941 case dw_val_class_vms_delta:
9942 case dw_val_class_offset:
9943 switch (dwarf_offset_size)
9945 case 4:
9946 return DW_FORM_data4;
9947 case 8:
9948 return DW_FORM_data8;
9949 default:
9950 gcc_unreachable ();
9952 case dw_val_class_loc:
9953 if (dwarf_version >= 4)
9954 return DW_FORM_exprloc;
9955 switch (constant_size (size_of_locs (AT_loc (a))))
9957 case 1:
9958 return DW_FORM_block1;
9959 case 2:
9960 return DW_FORM_block2;
9961 case 4:
9962 return DW_FORM_block4;
9963 default:
9964 gcc_unreachable ();
9966 case dw_val_class_const:
9967 return DW_FORM_sdata;
9968 case dw_val_class_unsigned_const:
9969 switch (constant_size (AT_unsigned (a)))
9971 case 1:
9972 return DW_FORM_data1;
9973 case 2:
9974 return DW_FORM_data2;
9975 case 4:
9976 /* In DWARF3 DW_AT_data_member_location with
9977 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9978 constant, so we need to use DW_FORM_udata if we need
9979 a large constant. */
9980 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9981 return DW_FORM_udata;
9982 return DW_FORM_data4;
9983 case 8:
9984 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9985 return DW_FORM_udata;
9986 return DW_FORM_data8;
9987 default:
9988 gcc_unreachable ();
9990 case dw_val_class_const_implicit:
9991 case dw_val_class_unsigned_const_implicit:
9992 case dw_val_class_file_implicit:
9993 return DW_FORM_implicit_const;
9994 case dw_val_class_const_double:
9995 switch (HOST_BITS_PER_WIDE_INT)
9997 case 8:
9998 return DW_FORM_data2;
9999 case 16:
10000 return DW_FORM_data4;
10001 case 32:
10002 return DW_FORM_data8;
10003 case 64:
10004 if (dwarf_version >= 5)
10005 return DW_FORM_data16;
10006 /* FALLTHRU */
10007 default:
10008 return DW_FORM_block1;
10010 case dw_val_class_wide_int:
10011 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10013 case 8:
10014 return DW_FORM_data1;
10015 case 16:
10016 return DW_FORM_data2;
10017 case 32:
10018 return DW_FORM_data4;
10019 case 64:
10020 return DW_FORM_data8;
10021 case 128:
10022 if (dwarf_version >= 5)
10023 return DW_FORM_data16;
10024 /* FALLTHRU */
10025 default:
10026 return DW_FORM_block1;
10028 case dw_val_class_symview:
10029 /* ??? We might use uleb128, but then we'd have to compute
10030 .debug_info offsets in the assembler. */
10031 if (symview_upper_bound <= 0xff)
10032 return DW_FORM_data1;
10033 else if (symview_upper_bound <= 0xffff)
10034 return DW_FORM_data2;
10035 else if (symview_upper_bound <= 0xffffffff)
10036 return DW_FORM_data4;
10037 else
10038 return DW_FORM_data8;
10039 case dw_val_class_vec:
10040 switch (constant_size (a->dw_attr_val.v.val_vec.length
10041 * a->dw_attr_val.v.val_vec.elt_size))
10043 case 1:
10044 return DW_FORM_block1;
10045 case 2:
10046 return DW_FORM_block2;
10047 case 4:
10048 return DW_FORM_block4;
10049 default:
10050 gcc_unreachable ();
10052 case dw_val_class_flag:
10053 if (dwarf_version >= 4)
10055 /* Currently all add_AT_flag calls pass in 1 as last argument,
10056 so DW_FORM_flag_present can be used. If that ever changes,
10057 we'll need to use DW_FORM_flag and have some optimization
10058 in build_abbrev_table that will change those to
10059 DW_FORM_flag_present if it is set to 1 in all DIEs using
10060 the same abbrev entry. */
10061 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10062 return DW_FORM_flag_present;
10064 return DW_FORM_flag;
10065 case dw_val_class_die_ref:
10066 if (AT_ref_external (a))
10068 if (AT_ref (a)->comdat_type_p)
10069 return DW_FORM_ref_sig8;
10070 else
10071 return DW_FORM_ref_addr;
10073 else
10074 return DW_FORM_ref;
10075 case dw_val_class_fde_ref:
10076 return DW_FORM_data;
10077 case dw_val_class_lbl_id:
10078 return (AT_index (a) == NOT_INDEXED
10079 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10080 case dw_val_class_lineptr:
10081 case dw_val_class_macptr:
10082 case dw_val_class_loclistsptr:
10083 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10084 case dw_val_class_str:
10085 return AT_string_form (a);
10086 case dw_val_class_file:
10087 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10089 case 1:
10090 return DW_FORM_data1;
10091 case 2:
10092 return DW_FORM_data2;
10093 case 4:
10094 return DW_FORM_data4;
10095 default:
10096 gcc_unreachable ();
10099 case dw_val_class_data8:
10100 return DW_FORM_data8;
10102 case dw_val_class_high_pc:
10103 switch (DWARF2_ADDR_SIZE)
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 case 8:
10112 return DW_FORM_data8;
10113 default:
10114 gcc_unreachable ();
10117 case dw_val_class_discr_value:
10118 return (a->dw_attr_val.v.val_discr_value.pos
10119 ? DW_FORM_udata
10120 : DW_FORM_sdata);
10121 case dw_val_class_discr_list:
10122 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10124 case 1:
10125 return DW_FORM_block1;
10126 case 2:
10127 return DW_FORM_block2;
10128 case 4:
10129 return DW_FORM_block4;
10130 default:
10131 gcc_unreachable ();
10134 default:
10135 gcc_unreachable ();
10139 /* Output the encoding of an attribute value. */
10141 static void
10142 output_value_format (dw_attr_node *a)
10144 enum dwarf_form form = value_format (a);
10146 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10149 /* Given a die and id, produce the appropriate abbreviations. */
10151 static void
10152 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10154 unsigned ix;
10155 dw_attr_node *a_attr;
10157 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10158 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10159 dwarf_tag_name (abbrev->die_tag));
10161 if (abbrev->die_child != NULL)
10162 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10163 else
10164 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10166 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10168 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10169 dwarf_attr_name (a_attr->dw_attr));
10170 output_value_format (a_attr);
10171 if (value_format (a_attr) == DW_FORM_implicit_const)
10173 if (AT_class (a_attr) == dw_val_class_file_implicit)
10175 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10176 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10177 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10179 else
10180 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10184 dw2_asm_output_data (1, 0, NULL);
10185 dw2_asm_output_data (1, 0, NULL);
10189 /* Output the .debug_abbrev section which defines the DIE abbreviation
10190 table. */
10192 static void
10193 output_abbrev_section (void)
10195 unsigned int abbrev_id;
10196 dw_die_ref abbrev;
10198 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10199 if (abbrev_id != 0)
10200 output_die_abbrevs (abbrev_id, abbrev);
10202 /* Terminate the table. */
10203 dw2_asm_output_data (1, 0, NULL);
10206 /* Return a new location list, given the begin and end range, and the
10207 expression. */
10209 static inline dw_loc_list_ref
10210 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10211 const char *end, var_loc_view vend,
10212 const char *section)
10214 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10216 retlist->begin = begin;
10217 retlist->begin_entry = NULL;
10218 retlist->end = end;
10219 retlist->end_entry = NULL;
10220 retlist->expr = expr;
10221 retlist->section = section;
10222 retlist->vbegin = vbegin;
10223 retlist->vend = vend;
10225 return retlist;
10228 /* Return true iff there's any nonzero view number in the loc list.
10230 ??? When views are not enabled, we'll often extend a single range
10231 to the entire function, so that we emit a single location
10232 expression rather than a location list. With views, even with a
10233 single range, we'll output a list if start or end have a nonzero
10234 view. If we change this, we may want to stop splitting a single
10235 range in dw_loc_list just because of a nonzero view, even if it
10236 straddles across hot/cold partitions. */
10238 static bool
10239 loc_list_has_views (dw_loc_list_ref list)
10241 if (!debug_variable_location_views)
10242 return false;
10244 for (dw_loc_list_ref loc = list;
10245 loc != NULL; loc = loc->dw_loc_next)
10246 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10247 return true;
10249 return false;
10252 /* Generate a new internal symbol for this location list node, if it
10253 hasn't got one yet. */
10255 static inline void
10256 gen_llsym (dw_loc_list_ref list)
10258 gcc_assert (!list->ll_symbol);
10259 list->ll_symbol = gen_internal_sym ("LLST");
10261 if (!loc_list_has_views (list))
10262 return;
10264 if (dwarf2out_locviews_in_attribute ())
10266 /* Use the same label_num for the view list. */
10267 label_num--;
10268 list->vl_symbol = gen_internal_sym ("LVUS");
10270 else
10271 list->vl_symbol = list->ll_symbol;
10274 /* Generate a symbol for the list, but only if we really want to emit
10275 it as a list. */
10277 static inline void
10278 maybe_gen_llsym (dw_loc_list_ref list)
10280 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10281 return;
10283 gen_llsym (list);
10286 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10287 NULL, don't consider size of the location expression. If we're not
10288 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10289 representation in *SIZEP. */
10291 static bool
10292 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10294 /* Don't output an entry that starts and ends at the same address. */
10295 if (strcmp (curr->begin, curr->end) == 0
10296 && curr->vbegin == curr->vend && !curr->force)
10297 return true;
10299 if (!sizep)
10300 return false;
10302 unsigned long size = size_of_locs (curr->expr);
10304 /* If the expression is too large, drop it on the floor. We could
10305 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10306 in the expression, but >= 64KB expressions for a single value
10307 in a single range are unlikely very useful. */
10308 if (dwarf_version < 5 && size > 0xffff)
10309 return true;
10311 *sizep = size;
10313 return false;
10316 /* Output a view pair loclist entry for CURR, if it requires one. */
10318 static void
10319 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10321 if (!dwarf2out_locviews_in_loclist ())
10322 return;
10324 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10325 return;
10327 #ifdef DW_LLE_view_pair
10328 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10330 if (dwarf2out_as_locview_support)
10332 if (ZERO_VIEW_P (curr->vbegin))
10333 dw2_asm_output_data_uleb128 (0, "Location view begin");
10334 else
10336 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10337 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10338 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10341 if (ZERO_VIEW_P (curr->vend))
10342 dw2_asm_output_data_uleb128 (0, "Location view end");
10343 else
10345 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10346 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10347 dw2_asm_output_symname_uleb128 (label, "Location view end");
10350 else
10352 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10353 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10355 #endif /* DW_LLE_view_pair */
10357 return;
10360 /* Output the location list given to us. */
10362 static void
10363 output_loc_list (dw_loc_list_ref list_head)
10365 int vcount = 0, lcount = 0;
10367 if (list_head->emitted)
10368 return;
10369 list_head->emitted = true;
10371 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10373 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10375 for (dw_loc_list_ref curr = list_head; curr != NULL;
10376 curr = curr->dw_loc_next)
10378 unsigned long size;
10380 if (skip_loc_list_entry (curr, &size))
10381 continue;
10383 vcount++;
10385 /* ?? dwarf_split_debug_info? */
10386 if (dwarf2out_as_locview_support)
10388 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10390 if (!ZERO_VIEW_P (curr->vbegin))
10392 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10393 dw2_asm_output_symname_uleb128 (label,
10394 "View list begin (%s)",
10395 list_head->vl_symbol);
10397 else
10398 dw2_asm_output_data_uleb128 (0,
10399 "View list begin (%s)",
10400 list_head->vl_symbol);
10402 if (!ZERO_VIEW_P (curr->vend))
10404 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10405 dw2_asm_output_symname_uleb128 (label,
10406 "View list end (%s)",
10407 list_head->vl_symbol);
10409 else
10410 dw2_asm_output_data_uleb128 (0,
10411 "View list end (%s)",
10412 list_head->vl_symbol);
10414 else
10416 dw2_asm_output_data_uleb128 (curr->vbegin,
10417 "View list begin (%s)",
10418 list_head->vl_symbol);
10419 dw2_asm_output_data_uleb128 (curr->vend,
10420 "View list end (%s)",
10421 list_head->vl_symbol);
10426 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10428 const char *last_section = NULL;
10429 const char *base_label = NULL;
10431 /* Walk the location list, and output each range + expression. */
10432 for (dw_loc_list_ref curr = list_head; curr != NULL;
10433 curr = curr->dw_loc_next)
10435 unsigned long size;
10437 /* Skip this entry? If we skip it here, we must skip it in the
10438 view list above as well. */
10439 if (skip_loc_list_entry (curr, &size))
10440 continue;
10442 lcount++;
10444 if (dwarf_version >= 5)
10446 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10448 dwarf2out_maybe_output_loclist_view_pair (curr);
10449 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10450 uleb128 index into .debug_addr and uleb128 length. */
10451 dw2_asm_output_data (1, DW_LLE_startx_length,
10452 "DW_LLE_startx_length (%s)",
10453 list_head->ll_symbol);
10454 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10455 "Location list range start index "
10456 "(%s)", curr->begin);
10457 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10458 "Location list length (%s)",
10459 list_head->ll_symbol);
10461 else if (dwarf_split_debug_info)
10463 dwarf2out_maybe_output_loclist_view_pair (curr);
10464 /* For -gsplit-dwarf without usable .uleb128 support, emit
10465 DW_LLE_startx_endx, which has two uleb128 indexes into
10466 .debug_addr. */
10467 dw2_asm_output_data (1, DW_LLE_startx_endx,
10468 "DW_LLE_startx_endx (%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_data_uleb128 (curr->end_entry->index,
10474 "Location list range end index "
10475 "(%s)", curr->end);
10477 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10479 dwarf2out_maybe_output_loclist_view_pair (curr);
10480 /* If all code is in .text section, the base address is
10481 already provided by the CU attributes. Use
10482 DW_LLE_offset_pair where both addresses are uleb128 encoded
10483 offsets against that base. */
10484 dw2_asm_output_data (1, DW_LLE_offset_pair,
10485 "DW_LLE_offset_pair (%s)",
10486 list_head->ll_symbol);
10487 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10488 "Location list begin address (%s)",
10489 list_head->ll_symbol);
10490 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10491 "Location list end address (%s)",
10492 list_head->ll_symbol);
10494 else if (HAVE_AS_LEB128)
10496 /* Otherwise, find out how many consecutive entries could share
10497 the same base entry. If just one, emit DW_LLE_start_length,
10498 otherwise emit DW_LLE_base_address for the base address
10499 followed by a series of DW_LLE_offset_pair. */
10500 if (last_section == NULL || curr->section != last_section)
10502 dw_loc_list_ref curr2;
10503 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10504 curr2 = curr2->dw_loc_next)
10506 if (strcmp (curr2->begin, curr2->end) == 0
10507 && !curr2->force)
10508 continue;
10509 break;
10511 if (curr2 == NULL || curr->section != curr2->section)
10512 last_section = NULL;
10513 else
10515 last_section = curr->section;
10516 base_label = curr->begin;
10517 dw2_asm_output_data (1, DW_LLE_base_address,
10518 "DW_LLE_base_address (%s)",
10519 list_head->ll_symbol);
10520 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10521 "Base address (%s)",
10522 list_head->ll_symbol);
10525 /* Only one entry with the same base address. Use
10526 DW_LLE_start_length with absolute address and uleb128
10527 length. */
10528 if (last_section == NULL)
10530 dwarf2out_maybe_output_loclist_view_pair (curr);
10531 dw2_asm_output_data (1, DW_LLE_start_length,
10532 "DW_LLE_start_length (%s)",
10533 list_head->ll_symbol);
10534 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10535 "Location list begin address (%s)",
10536 list_head->ll_symbol);
10537 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10538 "Location list length "
10539 "(%s)", list_head->ll_symbol);
10541 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10542 DW_LLE_base_address. */
10543 else
10545 dwarf2out_maybe_output_loclist_view_pair (curr);
10546 dw2_asm_output_data (1, DW_LLE_offset_pair,
10547 "DW_LLE_offset_pair (%s)",
10548 list_head->ll_symbol);
10549 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10550 "Location list begin address "
10551 "(%s)", list_head->ll_symbol);
10552 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10553 "Location list end address "
10554 "(%s)", list_head->ll_symbol);
10557 /* The assembler does not support .uleb128 directive. Emit
10558 DW_LLE_start_end with a pair of absolute addresses. */
10559 else
10561 dwarf2out_maybe_output_loclist_view_pair (curr);
10562 dw2_asm_output_data (1, DW_LLE_start_end,
10563 "DW_LLE_start_end (%s)",
10564 list_head->ll_symbol);
10565 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10566 "Location list begin address (%s)",
10567 list_head->ll_symbol);
10568 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10569 "Location list end address (%s)",
10570 list_head->ll_symbol);
10573 else if (dwarf_split_debug_info)
10575 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10576 and 4 byte length. */
10577 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10578 "Location list start/length entry (%s)",
10579 list_head->ll_symbol);
10580 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10581 "Location list range start index (%s)",
10582 curr->begin);
10583 /* The length field is 4 bytes. If we ever need to support
10584 an 8-byte length, we can add a new DW_LLE code or fall back
10585 to DW_LLE_GNU_start_end_entry. */
10586 dw2_asm_output_delta (4, curr->end, curr->begin,
10587 "Location list range length (%s)",
10588 list_head->ll_symbol);
10590 else if (!have_multiple_function_sections)
10592 /* Pair of relative addresses against start of text section. */
10593 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10594 "Location list begin address (%s)",
10595 list_head->ll_symbol);
10596 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10597 "Location list end address (%s)",
10598 list_head->ll_symbol);
10600 else
10602 /* Pair of absolute addresses. */
10603 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10604 "Location list begin address (%s)",
10605 list_head->ll_symbol);
10606 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10607 "Location list end address (%s)",
10608 list_head->ll_symbol);
10611 /* Output the block length for this list of location operations. */
10612 if (dwarf_version >= 5)
10613 dw2_asm_output_data_uleb128 (size, "Location expression size");
10614 else
10616 gcc_assert (size <= 0xffff);
10617 dw2_asm_output_data (2, size, "Location expression size");
10620 output_loc_sequence (curr->expr, -1);
10623 /* And finally list termination. */
10624 if (dwarf_version >= 5)
10625 dw2_asm_output_data (1, DW_LLE_end_of_list,
10626 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10627 else if (dwarf_split_debug_info)
10628 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10629 "Location list terminator (%s)",
10630 list_head->ll_symbol);
10631 else
10633 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10634 "Location list terminator begin (%s)",
10635 list_head->ll_symbol);
10636 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10637 "Location list terminator end (%s)",
10638 list_head->ll_symbol);
10641 gcc_assert (!list_head->vl_symbol
10642 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10645 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10646 section. Emit a relocated reference if val_entry is NULL, otherwise,
10647 emit an indirect reference. */
10649 static void
10650 output_range_list_offset (dw_attr_node *a)
10652 const char *name = dwarf_attr_name (a->dw_attr);
10654 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10656 if (dwarf_version >= 5)
10658 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10659 dw2_asm_output_offset (dwarf_offset_size, r->label,
10660 debug_ranges_section, "%s", name);
10662 else
10664 char *p = strchr (ranges_section_label, '\0');
10665 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10666 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10667 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10668 debug_ranges_section, "%s", name);
10669 *p = '\0';
10672 else if (dwarf_version >= 5)
10674 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10675 gcc_assert (rnglist_idx);
10676 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10678 else
10679 dw2_asm_output_data (dwarf_offset_size,
10680 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10681 "%s (offset from %s)", name, ranges_section_label);
10684 /* Output the offset into the debug_loc section. */
10686 static void
10687 output_loc_list_offset (dw_attr_node *a)
10689 char *sym = AT_loc_list (a)->ll_symbol;
10691 gcc_assert (sym);
10692 if (!dwarf_split_debug_info)
10693 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10694 "%s", dwarf_attr_name (a->dw_attr));
10695 else if (dwarf_version >= 5)
10697 gcc_assert (AT_loc_list (a)->num_assigned);
10698 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10699 dwarf_attr_name (a->dw_attr),
10700 sym);
10702 else
10703 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10704 "%s", dwarf_attr_name (a->dw_attr));
10707 /* Output the offset into the debug_loc section. */
10709 static void
10710 output_view_list_offset (dw_attr_node *a)
10712 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10714 gcc_assert (sym);
10715 if (dwarf_split_debug_info)
10716 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10717 "%s", dwarf_attr_name (a->dw_attr));
10718 else
10719 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10720 "%s", dwarf_attr_name (a->dw_attr));
10723 /* Output an attribute's index or value appropriately. */
10725 static void
10726 output_attr_index_or_value (dw_attr_node *a)
10728 const char *name = dwarf_attr_name (a->dw_attr);
10730 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10732 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10733 return;
10735 switch (AT_class (a))
10737 case dw_val_class_addr:
10738 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10739 break;
10740 case dw_val_class_high_pc:
10741 case dw_val_class_lbl_id:
10742 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10743 break;
10744 default:
10745 gcc_unreachable ();
10749 /* Output a type signature. */
10751 static inline void
10752 output_signature (const char *sig, const char *name)
10754 int i;
10756 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10757 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10760 /* Output a discriminant value. */
10762 static inline void
10763 output_discr_value (dw_discr_value *discr_value, const char *name)
10765 if (discr_value->pos)
10766 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10767 else
10768 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10771 /* Output the DIE and its attributes. Called recursively to generate
10772 the definitions of each child DIE. */
10774 static void
10775 output_die (dw_die_ref die)
10777 dw_attr_node *a;
10778 dw_die_ref c;
10779 unsigned long size;
10780 unsigned ix;
10782 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10783 (unsigned long)die->die_offset,
10784 dwarf_tag_name (die->die_tag));
10786 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10788 const char *name = dwarf_attr_name (a->dw_attr);
10790 switch (AT_class (a))
10792 case dw_val_class_addr:
10793 output_attr_index_or_value (a);
10794 break;
10796 case dw_val_class_offset:
10797 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10798 "%s", name);
10799 break;
10801 case dw_val_class_range_list:
10802 output_range_list_offset (a);
10803 break;
10805 case dw_val_class_loc:
10806 size = size_of_locs (AT_loc (a));
10808 /* Output the block length for this list of location operations. */
10809 if (dwarf_version >= 4)
10810 dw2_asm_output_data_uleb128 (size, "%s", name);
10811 else
10812 dw2_asm_output_data (constant_size (size), size, "%s", name);
10814 output_loc_sequence (AT_loc (a), -1);
10815 break;
10817 case dw_val_class_const:
10818 /* ??? It would be slightly more efficient to use a scheme like is
10819 used for unsigned constants below, but gdb 4.x does not sign
10820 extend. Gdb 5.x does sign extend. */
10821 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10822 break;
10824 case dw_val_class_unsigned_const:
10826 int csize = constant_size (AT_unsigned (a));
10827 if (dwarf_version == 3
10828 && a->dw_attr == DW_AT_data_member_location
10829 && csize >= 4)
10830 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10831 else
10832 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10834 break;
10836 case dw_val_class_symview:
10838 int vsize;
10839 if (symview_upper_bound <= 0xff)
10840 vsize = 1;
10841 else if (symview_upper_bound <= 0xffff)
10842 vsize = 2;
10843 else if (symview_upper_bound <= 0xffffffff)
10844 vsize = 4;
10845 else
10846 vsize = 8;
10847 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10848 "%s", name);
10850 break;
10852 case dw_val_class_const_implicit:
10853 if (flag_debug_asm)
10854 fprintf (asm_out_file, "\t\t\t%s %s ("
10855 HOST_WIDE_INT_PRINT_DEC ")\n",
10856 ASM_COMMENT_START, name, AT_int (a));
10857 break;
10859 case dw_val_class_unsigned_const_implicit:
10860 if (flag_debug_asm)
10861 fprintf (asm_out_file, "\t\t\t%s %s ("
10862 HOST_WIDE_INT_PRINT_HEX ")\n",
10863 ASM_COMMENT_START, name, AT_unsigned (a));
10864 break;
10866 case dw_val_class_const_double:
10868 unsigned HOST_WIDE_INT first, second;
10870 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10871 dw2_asm_output_data (1,
10872 HOST_BITS_PER_DOUBLE_INT
10873 / HOST_BITS_PER_CHAR,
10874 NULL);
10876 if (WORDS_BIG_ENDIAN)
10878 first = a->dw_attr_val.v.val_double.high;
10879 second = a->dw_attr_val.v.val_double.low;
10881 else
10883 first = a->dw_attr_val.v.val_double.low;
10884 second = a->dw_attr_val.v.val_double.high;
10887 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10888 first, "%s", name);
10889 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10890 second, NULL);
10892 break;
10894 case dw_val_class_wide_int:
10896 int i;
10897 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10898 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10899 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10900 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10901 * l, NULL);
10903 if (WORDS_BIG_ENDIAN)
10904 for (i = len - 1; i >= 0; --i)
10906 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10907 "%s", name);
10908 name = "";
10910 else
10911 for (i = 0; i < len; ++i)
10913 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10914 "%s", name);
10915 name = "";
10918 break;
10920 case dw_val_class_vec:
10922 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10923 unsigned int len = a->dw_attr_val.v.val_vec.length;
10924 unsigned int i;
10925 unsigned char *p;
10927 dw2_asm_output_data (constant_size (len * elt_size),
10928 len * elt_size, "%s", name);
10929 if (elt_size > sizeof (HOST_WIDE_INT))
10931 elt_size /= 2;
10932 len *= 2;
10934 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10935 i < len;
10936 i++, p += elt_size)
10937 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10938 "fp or vector constant word %u", i);
10939 break;
10942 case dw_val_class_flag:
10943 if (dwarf_version >= 4)
10945 /* Currently all add_AT_flag calls pass in 1 as last argument,
10946 so DW_FORM_flag_present can be used. If that ever changes,
10947 we'll need to use DW_FORM_flag and have some optimization
10948 in build_abbrev_table that will change those to
10949 DW_FORM_flag_present if it is set to 1 in all DIEs using
10950 the same abbrev entry. */
10951 gcc_assert (AT_flag (a) == 1);
10952 if (flag_debug_asm)
10953 fprintf (asm_out_file, "\t\t\t%s %s\n",
10954 ASM_COMMENT_START, name);
10955 break;
10957 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10958 break;
10960 case dw_val_class_loc_list:
10961 output_loc_list_offset (a);
10962 break;
10964 case dw_val_class_view_list:
10965 output_view_list_offset (a);
10966 break;
10968 case dw_val_class_die_ref:
10969 if (AT_ref_external (a))
10971 if (AT_ref (a)->comdat_type_p)
10973 comdat_type_node *type_node
10974 = AT_ref (a)->die_id.die_type_node;
10976 gcc_assert (type_node);
10977 output_signature (type_node->signature, name);
10979 else
10981 const char *sym = AT_ref (a)->die_id.die_symbol;
10982 int size;
10984 gcc_assert (sym);
10985 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10986 length, whereas in DWARF3 it's always sized as an
10987 offset. */
10988 if (dwarf_version == 2)
10989 size = DWARF2_ADDR_SIZE;
10990 else
10991 size = dwarf_offset_size;
10992 /* ??? We cannot unconditionally output die_offset if
10993 non-zero - others might create references to those
10994 DIEs via symbols.
10995 And we do not clear its DIE offset after outputting it
10996 (and the label refers to the actual DIEs, not the
10997 DWARF CU unit header which is when using label + offset
10998 would be the correct thing to do).
10999 ??? This is the reason for the with_offset flag. */
11000 if (AT_ref (a)->with_offset)
11001 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
11002 debug_info_section, "%s", name);
11003 else
11004 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11005 name);
11008 else
11010 gcc_assert (AT_ref (a)->die_offset);
11011 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11012 "%s", name);
11014 break;
11016 case dw_val_class_fde_ref:
11018 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11020 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11021 a->dw_attr_val.v.val_fde_index * 2);
11022 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11023 "%s", name);
11025 break;
11027 case dw_val_class_vms_delta:
11028 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11029 dw2_asm_output_vms_delta (dwarf_offset_size,
11030 AT_vms_delta2 (a), AT_vms_delta1 (a),
11031 "%s", name);
11032 #else
11033 dw2_asm_output_delta (dwarf_offset_size,
11034 AT_vms_delta2 (a), AT_vms_delta1 (a),
11035 "%s", name);
11036 #endif
11037 break;
11039 case dw_val_class_lbl_id:
11040 output_attr_index_or_value (a);
11041 break;
11043 case dw_val_class_lineptr:
11044 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11045 debug_line_section, "%s", name);
11046 break;
11048 case dw_val_class_macptr:
11049 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11050 debug_macinfo_section, "%s", name);
11051 break;
11053 case dw_val_class_loclistsptr:
11054 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11055 debug_loc_section, "%s", name);
11056 break;
11058 case dw_val_class_str:
11059 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11060 dw2_asm_output_offset (dwarf_offset_size,
11061 a->dw_attr_val.v.val_str->label,
11062 debug_str_section,
11063 "%s: \"%s\"", name, AT_string (a));
11064 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11065 dw2_asm_output_offset (dwarf_offset_size,
11066 a->dw_attr_val.v.val_str->label,
11067 debug_line_str_section,
11068 "%s: \"%s\"", name, AT_string (a));
11069 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11070 dw2_asm_output_data_uleb128 (AT_index (a),
11071 "%s: \"%s\"", name, AT_string (a));
11072 else
11073 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11074 break;
11076 case dw_val_class_file:
11078 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11080 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11081 a->dw_attr_val.v.val_file->filename);
11082 break;
11085 case dw_val_class_file_implicit:
11086 if (flag_debug_asm)
11087 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11088 ASM_COMMENT_START, name,
11089 maybe_emit_file (a->dw_attr_val.v.val_file),
11090 a->dw_attr_val.v.val_file->filename);
11091 break;
11093 case dw_val_class_data8:
11095 int i;
11097 for (i = 0; i < 8; i++)
11098 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11099 i == 0 ? "%s" : NULL, name);
11100 break;
11103 case dw_val_class_high_pc:
11104 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11105 get_AT_low_pc (die), "DW_AT_high_pc");
11106 break;
11108 case dw_val_class_discr_value:
11109 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11110 break;
11112 case dw_val_class_discr_list:
11114 dw_discr_list_ref list = AT_discr_list (a);
11115 const int size = size_of_discr_list (list);
11117 /* This is a block, so output its length first. */
11118 dw2_asm_output_data (constant_size (size), size,
11119 "%s: block size", name);
11121 for (; list != NULL; list = list->dw_discr_next)
11123 /* One byte for the discriminant value descriptor, and then as
11124 many LEB128 numbers as required. */
11125 if (list->dw_discr_range)
11126 dw2_asm_output_data (1, DW_DSC_range,
11127 "%s: DW_DSC_range", name);
11128 else
11129 dw2_asm_output_data (1, DW_DSC_label,
11130 "%s: DW_DSC_label", name);
11132 output_discr_value (&list->dw_discr_lower_bound, name);
11133 if (list->dw_discr_range)
11134 output_discr_value (&list->dw_discr_upper_bound, name);
11136 break;
11139 default:
11140 gcc_unreachable ();
11144 FOR_EACH_CHILD (die, c, output_die (c));
11146 /* Add null byte to terminate sibling list. */
11147 if (die->die_child != NULL)
11148 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11149 (unsigned long) die->die_offset);
11152 /* Output the dwarf version number. */
11154 static void
11155 output_dwarf_version ()
11157 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11158 views in loclist. That will change eventually. */
11159 if (dwarf_version == 6)
11161 static bool once;
11162 if (!once)
11164 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11165 "incompatibilities");
11166 once = true;
11168 dw2_asm_output_data (2, 5, "DWARF version number");
11170 else
11171 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11174 /* Output the compilation unit that appears at the beginning of the
11175 .debug_info section, and precedes the DIE descriptions. */
11177 static void
11178 output_compilation_unit_header (enum dwarf_unit_type ut)
11180 if (!XCOFF_DEBUGGING_INFO)
11182 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11183 dw2_asm_output_data (4, 0xffffffff,
11184 "Initial length escape value indicating 64-bit DWARF extension");
11185 dw2_asm_output_data (dwarf_offset_size,
11186 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11187 "Length of Compilation Unit Info");
11190 output_dwarf_version ();
11191 if (dwarf_version >= 5)
11193 const char *name;
11194 switch (ut)
11196 case DW_UT_compile: name = "DW_UT_compile"; break;
11197 case DW_UT_type: name = "DW_UT_type"; break;
11198 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11199 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11200 default: gcc_unreachable ();
11202 dw2_asm_output_data (1, ut, "%s", name);
11203 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11205 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11206 debug_abbrev_section,
11207 "Offset Into Abbrev. Section");
11208 if (dwarf_version < 5)
11209 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11212 /* Output the compilation unit DIE and its children. */
11214 static void
11215 output_comp_unit (dw_die_ref die, int output_if_empty,
11216 const unsigned char *dwo_id)
11218 const char *secname, *oldsym;
11219 char *tmp;
11221 /* Unless we are outputting main CU, we may throw away empty ones. */
11222 if (!output_if_empty && die->die_child == NULL)
11223 return;
11225 /* Even if there are no children of this DIE, we must output the information
11226 about the compilation unit. Otherwise, on an empty translation unit, we
11227 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11228 will then complain when examining the file. First mark all the DIEs in
11229 this CU so we know which get local refs. */
11230 mark_dies (die);
11232 external_ref_hash_type *extern_map = optimize_external_refs (die);
11234 /* For now, optimize only the main CU, in order to optimize the rest
11235 we'd need to see all of them earlier. Leave the rest for post-linking
11236 tools like DWZ. */
11237 if (die == comp_unit_die ())
11238 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11240 build_abbrev_table (die, extern_map);
11242 optimize_abbrev_table ();
11244 delete extern_map;
11246 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11247 next_die_offset = (dwo_id
11248 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11249 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11250 calc_die_sizes (die);
11252 oldsym = die->die_id.die_symbol;
11253 if (oldsym && die->comdat_type_p)
11255 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11257 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11258 secname = tmp;
11259 die->die_id.die_symbol = NULL;
11260 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11262 else
11264 switch_to_section (debug_info_section);
11265 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11266 info_section_emitted = true;
11269 /* For LTO cross unit DIE refs we want a symbol on the start of the
11270 debuginfo section, not on the CU DIE. */
11271 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11273 /* ??? No way to get visibility assembled without a decl. */
11274 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11275 get_identifier (oldsym), char_type_node);
11276 TREE_PUBLIC (decl) = true;
11277 TREE_STATIC (decl) = true;
11278 DECL_ARTIFICIAL (decl) = true;
11279 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11280 DECL_VISIBILITY_SPECIFIED (decl) = true;
11281 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11282 #ifdef ASM_WEAKEN_LABEL
11283 /* We prefer a .weak because that handles duplicates from duplicate
11284 archive members in a graceful way. */
11285 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11286 #else
11287 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11288 #endif
11289 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11292 /* Output debugging information. */
11293 output_compilation_unit_header (dwo_id
11294 ? DW_UT_split_compile : DW_UT_compile);
11295 if (dwarf_version >= 5)
11297 if (dwo_id != NULL)
11298 for (int i = 0; i < 8; i++)
11299 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11301 output_die (die);
11303 /* Leave the marks on the main CU, so we can check them in
11304 output_pubnames. */
11305 if (oldsym)
11307 unmark_dies (die);
11308 die->die_id.die_symbol = oldsym;
11312 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11313 and .debug_pubtypes. This is configured per-target, but can be
11314 overridden by the -gpubnames or -gno-pubnames options. */
11316 static inline bool
11317 want_pubnames (void)
11319 if (debug_info_level <= DINFO_LEVEL_TERSE
11320 /* Names and types go to the early debug part only. */
11321 || in_lto_p)
11322 return false;
11323 if (debug_generate_pub_sections != -1)
11324 return debug_generate_pub_sections;
11325 return targetm.want_debug_pub_sections;
11328 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11330 static void
11331 add_AT_pubnames (dw_die_ref die)
11333 if (want_pubnames ())
11334 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11337 /* Add a string attribute value to a skeleton DIE. */
11339 static inline void
11340 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11341 const char *str)
11343 dw_attr_node attr;
11344 struct indirect_string_node *node;
11346 if (! skeleton_debug_str_hash)
11347 skeleton_debug_str_hash
11348 = hash_table<indirect_string_hasher>::create_ggc (10);
11350 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11351 find_string_form (node);
11352 if (node->form == dwarf_FORM (DW_FORM_strx))
11353 node->form = DW_FORM_strp;
11355 attr.dw_attr = attr_kind;
11356 attr.dw_attr_val.val_class = dw_val_class_str;
11357 attr.dw_attr_val.val_entry = NULL;
11358 attr.dw_attr_val.v.val_str = node;
11359 add_dwarf_attr (die, &attr);
11362 /* Helper function to generate top-level dies for skeleton debug_info and
11363 debug_types. */
11365 static void
11366 add_top_level_skeleton_die_attrs (dw_die_ref die)
11368 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11369 const char *comp_dir = comp_dir_string ();
11371 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11372 if (comp_dir != NULL)
11373 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11374 add_AT_pubnames (die);
11375 if (addr_index_table != NULL && addr_index_table->size () > 0)
11376 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11379 /* Output skeleton debug sections that point to the dwo file. */
11381 static void
11382 output_skeleton_debug_sections (dw_die_ref comp_unit,
11383 const unsigned char *dwo_id)
11385 /* These attributes will be found in the full debug_info section. */
11386 remove_AT (comp_unit, DW_AT_producer);
11387 remove_AT (comp_unit, DW_AT_language);
11389 switch_to_section (debug_skeleton_info_section);
11390 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11392 /* Produce the skeleton compilation-unit header. This one differs enough from
11393 a normal CU header that it's better not to call output_compilation_unit
11394 header. */
11395 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11396 dw2_asm_output_data (4, 0xffffffff,
11397 "Initial length escape value indicating 64-bit "
11398 "DWARF extension");
11400 dw2_asm_output_data (dwarf_offset_size,
11401 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11402 - DWARF_INITIAL_LENGTH_SIZE
11403 + size_of_die (comp_unit),
11404 "Length of Compilation Unit Info");
11405 output_dwarf_version ();
11406 if (dwarf_version >= 5)
11408 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11409 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11411 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11412 debug_skeleton_abbrev_section,
11413 "Offset Into Abbrev. Section");
11414 if (dwarf_version < 5)
11415 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11416 else
11417 for (int i = 0; i < 8; i++)
11418 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11420 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11421 output_die (comp_unit);
11423 /* Build the skeleton debug_abbrev section. */
11424 switch_to_section (debug_skeleton_abbrev_section);
11425 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11427 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11429 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11432 /* Output a comdat type unit DIE and its children. */
11434 static void
11435 output_comdat_type_unit (comdat_type_node *node,
11436 bool early_lto_debug ATTRIBUTE_UNUSED)
11438 const char *secname;
11439 char *tmp;
11440 int i;
11441 #if defined (OBJECT_FORMAT_ELF)
11442 tree comdat_key;
11443 #endif
11445 /* First mark all the DIEs in this CU so we know which get local refs. */
11446 mark_dies (node->root_die);
11448 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11450 build_abbrev_table (node->root_die, extern_map);
11452 delete extern_map;
11453 extern_map = NULL;
11455 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11456 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11457 calc_die_sizes (node->root_die);
11459 #if defined (OBJECT_FORMAT_ELF)
11460 if (dwarf_version >= 5)
11462 if (!dwarf_split_debug_info)
11463 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11464 else
11465 secname = (early_lto_debug
11466 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11468 else if (!dwarf_split_debug_info)
11469 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11470 else
11471 secname = (early_lto_debug
11472 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11474 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11475 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11476 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11477 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11478 comdat_key = get_identifier (tmp);
11479 targetm.asm_out.named_section (secname,
11480 SECTION_DEBUG | SECTION_LINKONCE,
11481 comdat_key);
11482 #else
11483 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11484 sprintf (tmp, (dwarf_version >= 5
11485 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11486 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11487 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11488 secname = tmp;
11489 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11490 #endif
11492 /* Output debugging information. */
11493 output_compilation_unit_header (dwarf_split_debug_info
11494 ? DW_UT_split_type : DW_UT_type);
11495 output_signature (node->signature, "Type Signature");
11496 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11497 "Offset to Type DIE");
11498 output_die (node->root_die);
11500 unmark_dies (node->root_die);
11503 /* Return the DWARF2/3 pubname associated with a decl. */
11505 static const char *
11506 dwarf2_name (tree decl, int scope)
11508 if (DECL_NAMELESS (decl))
11509 return NULL;
11510 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11513 /* Add a new entry to .debug_pubnames if appropriate. */
11515 static void
11516 add_pubname_string (const char *str, dw_die_ref die)
11518 pubname_entry e;
11520 e.die = die;
11521 e.name = xstrdup (str);
11522 vec_safe_push (pubname_table, e);
11525 static void
11526 add_pubname (tree decl, dw_die_ref die)
11528 if (!want_pubnames ())
11529 return;
11531 /* Don't add items to the table when we expect that the consumer will have
11532 just read the enclosing die. For example, if the consumer is looking at a
11533 class_member, it will either be inside the class already, or will have just
11534 looked up the class to find the member. Either way, searching the class is
11535 faster than searching the index. */
11536 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11537 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11539 const char *name = dwarf2_name (decl, 1);
11541 if (name)
11542 add_pubname_string (name, die);
11546 /* Add an enumerator to the pubnames section. */
11548 static void
11549 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11551 pubname_entry e;
11553 gcc_assert (scope_name);
11554 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11555 e.die = die;
11556 vec_safe_push (pubname_table, e);
11559 /* Add a new entry to .debug_pubtypes if appropriate. */
11561 static void
11562 add_pubtype (tree decl, dw_die_ref die)
11564 pubname_entry e;
11566 if (!want_pubnames ())
11567 return;
11569 if ((TREE_PUBLIC (decl)
11570 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11571 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11573 tree scope = NULL;
11574 const char *scope_name = "";
11575 const char *sep = is_cxx () ? "::" : ".";
11576 const char *name;
11578 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11579 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11581 scope_name = lang_hooks.dwarf_name (scope, 1);
11582 if (scope_name != NULL && scope_name[0] != '\0')
11583 scope_name = concat (scope_name, sep, NULL);
11584 else
11585 scope_name = "";
11588 if (TYPE_P (decl))
11589 name = type_tag (decl);
11590 else
11591 name = lang_hooks.dwarf_name (decl, 1);
11593 /* If we don't have a name for the type, there's no point in adding
11594 it to the table. */
11595 if (name != NULL && name[0] != '\0')
11597 e.die = die;
11598 e.name = concat (scope_name, name, NULL);
11599 vec_safe_push (pubtype_table, e);
11602 /* Although it might be more consistent to add the pubinfo for the
11603 enumerators as their dies are created, they should only be added if the
11604 enum type meets the criteria above. So rather than re-check the parent
11605 enum type whenever an enumerator die is created, just output them all
11606 here. This isn't protected by the name conditional because anonymous
11607 enums don't have names. */
11608 if (die->die_tag == DW_TAG_enumeration_type)
11610 dw_die_ref c;
11612 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11617 /* Output a single entry in the pubnames table. */
11619 static void
11620 output_pubname (dw_offset die_offset, pubname_entry *entry)
11622 dw_die_ref die = entry->die;
11623 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11625 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11627 if (debug_generate_pub_sections == 2)
11629 /* This logic follows gdb's method for determining the value of the flag
11630 byte. */
11631 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11632 switch (die->die_tag)
11634 case DW_TAG_typedef:
11635 case DW_TAG_base_type:
11636 case DW_TAG_subrange_type:
11637 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11638 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11639 break;
11640 case DW_TAG_enumerator:
11641 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11642 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11643 if (!is_cxx ())
11644 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11645 break;
11646 case DW_TAG_subprogram:
11647 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11648 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11649 if (!is_ada ())
11650 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11651 break;
11652 case DW_TAG_constant:
11653 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11654 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11655 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11656 break;
11657 case DW_TAG_variable:
11658 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11659 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11660 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11661 break;
11662 case DW_TAG_namespace:
11663 case DW_TAG_imported_declaration:
11664 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11665 break;
11666 case DW_TAG_class_type:
11667 case DW_TAG_interface_type:
11668 case DW_TAG_structure_type:
11669 case DW_TAG_union_type:
11670 case DW_TAG_enumeration_type:
11671 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11672 if (!is_cxx ())
11673 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11674 break;
11675 default:
11676 /* An unusual tag. Leave the flag-byte empty. */
11677 break;
11679 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11680 "GDB-index flags");
11683 dw2_asm_output_nstring (entry->name, -1, "external name");
11687 /* Output the public names table used to speed up access to externally
11688 visible names; or the public types table used to find type definitions. */
11690 static void
11691 output_pubnames (vec<pubname_entry, va_gc> *names)
11693 unsigned i;
11694 unsigned long pubnames_length = size_of_pubnames (names);
11695 pubname_entry *pub;
11697 if (!XCOFF_DEBUGGING_INFO)
11699 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11700 dw2_asm_output_data (4, 0xffffffff,
11701 "Initial length escape value indicating 64-bit DWARF extension");
11702 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11703 "Pub Info Length");
11706 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11707 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11709 if (dwarf_split_debug_info)
11710 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11711 debug_skeleton_info_section,
11712 "Offset of Compilation Unit Info");
11713 else
11714 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11715 debug_info_section,
11716 "Offset of Compilation Unit Info");
11717 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11718 "Compilation Unit Length");
11720 FOR_EACH_VEC_ELT (*names, i, pub)
11722 if (include_pubname_in_output (names, pub))
11724 dw_offset die_offset = pub->die->die_offset;
11726 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11727 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11728 gcc_assert (pub->die->die_mark);
11730 /* If we're putting types in their own .debug_types sections,
11731 the .debug_pubtypes table will still point to the compile
11732 unit (not the type unit), so we want to use the offset of
11733 the skeleton DIE (if there is one). */
11734 if (pub->die->comdat_type_p && names == pubtype_table)
11736 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11738 if (type_node != NULL)
11739 die_offset = (type_node->skeleton_die != NULL
11740 ? type_node->skeleton_die->die_offset
11741 : comp_unit_die ()->die_offset);
11744 output_pubname (die_offset, pub);
11748 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11751 /* Output public names and types tables if necessary. */
11753 static void
11754 output_pubtables (void)
11756 if (!want_pubnames () || !info_section_emitted)
11757 return;
11759 switch_to_section (debug_pubnames_section);
11760 output_pubnames (pubname_table);
11761 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11762 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11763 simply won't look for the section. */
11764 switch_to_section (debug_pubtypes_section);
11765 output_pubnames (pubtype_table);
11769 /* Output the information that goes into the .debug_aranges table.
11770 Namely, define the beginning and ending address range of the
11771 text section generated for this compilation unit. */
11773 static void
11774 output_aranges (void)
11776 unsigned i;
11777 unsigned long aranges_length = size_of_aranges ();
11779 if (!XCOFF_DEBUGGING_INFO)
11781 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11782 dw2_asm_output_data (4, 0xffffffff,
11783 "Initial length escape value indicating 64-bit DWARF extension");
11784 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11785 "Length of Address Ranges Info");
11788 /* Version number for aranges is still 2, even up to DWARF5. */
11789 dw2_asm_output_data (2, 2, "DWARF aranges version");
11790 if (dwarf_split_debug_info)
11791 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11792 debug_skeleton_info_section,
11793 "Offset of Compilation Unit Info");
11794 else
11795 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11796 debug_info_section,
11797 "Offset of Compilation Unit Info");
11798 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11799 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11801 /* We need to align to twice the pointer size here. */
11802 if (DWARF_ARANGES_PAD_SIZE)
11804 /* Pad using a 2 byte words so that padding is correct for any
11805 pointer size. */
11806 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11807 2 * DWARF2_ADDR_SIZE);
11808 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11809 dw2_asm_output_data (2, 0, NULL);
11812 /* It is necessary not to output these entries if the sections were
11813 not used; if the sections were not used, the length will be 0 and
11814 the address may end up as 0 if the section is discarded by ld
11815 --gc-sections, leaving an invalid (0, 0) entry that can be
11816 confused with the terminator. */
11817 if (switch_text_ranges)
11819 const char *prev_loc = text_section_label;
11820 const char *loc;
11821 unsigned idx;
11823 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11824 if (prev_loc)
11826 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11827 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11828 prev_loc = NULL;
11830 else
11831 prev_loc = loc;
11833 if (prev_loc)
11835 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11836 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11837 prev_loc, "Length");
11841 if (switch_cold_ranges)
11843 const char *prev_loc = cold_text_section_label;
11844 const char *loc;
11845 unsigned idx;
11847 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11848 if (prev_loc)
11850 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11851 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11852 prev_loc = NULL;
11854 else
11855 prev_loc = loc;
11857 if (prev_loc)
11859 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11860 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11861 prev_loc, "Length");
11865 if (have_multiple_function_sections)
11867 unsigned fde_idx;
11868 dw_fde_ref fde;
11870 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11872 if (fde->ignored_debug)
11873 continue;
11874 if (!fde->in_std_section)
11876 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11877 "Address");
11878 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11879 fde->dw_fde_begin, "Length");
11881 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11883 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11884 "Address");
11885 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11886 fde->dw_fde_second_begin, "Length");
11891 /* Output the terminator words. */
11892 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11893 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11896 /* Add a new entry to .debug_ranges. Return its index into
11897 ranges_table vector. */
11899 static unsigned int
11900 add_ranges_num (int num, bool maybe_new_sec)
11902 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11903 vec_safe_push (ranges_table, r);
11904 return vec_safe_length (ranges_table) - 1;
11907 /* Add a new entry to .debug_ranges corresponding to a block, or a
11908 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11909 this entry might be in a different section from previous range. */
11911 static unsigned int
11912 add_ranges (const_tree block, bool maybe_new_sec)
11914 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11917 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11918 chain, or middle entry of a chain that will be directly referred to. */
11920 static void
11921 note_rnglist_head (unsigned int offset)
11923 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11924 return;
11925 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11928 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11929 When using dwarf_split_debug_info, address attributes in dies destined
11930 for the final executable should be direct references--setting the
11931 parameter force_direct ensures this behavior. */
11933 static void
11934 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11935 bool *added, bool force_direct)
11937 unsigned int in_use = vec_safe_length (ranges_by_label);
11938 unsigned int offset;
11939 dw_ranges_by_label rbl = { begin, end };
11940 vec_safe_push (ranges_by_label, rbl);
11941 offset = add_ranges_num (-(int)in_use - 1, true);
11942 if (!*added)
11944 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11945 *added = true;
11946 note_rnglist_head (offset);
11947 if (dwarf_split_debug_info && force_direct)
11948 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11952 /* Emit .debug_ranges section. */
11954 static void
11955 output_ranges (void)
11957 unsigned i;
11958 static const char *const start_fmt = "Offset %#x";
11959 const char *fmt = start_fmt;
11960 dw_ranges *r;
11962 switch_to_section (debug_ranges_section);
11963 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11964 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11966 int block_num = r->num;
11968 if (block_num > 0)
11970 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11971 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11973 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11974 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11976 /* If all code is in the text section, then the compilation
11977 unit base address defaults to DW_AT_low_pc, which is the
11978 base of the text section. */
11979 if (!have_multiple_function_sections)
11981 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11982 text_section_label,
11983 fmt, i * 2 * DWARF2_ADDR_SIZE);
11984 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11985 text_section_label, NULL);
11988 /* Otherwise, the compilation unit base address is zero,
11989 which allows us to use absolute addresses, and not worry
11990 about whether the target supports cross-section
11991 arithmetic. */
11992 else
11994 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11995 fmt, i * 2 * DWARF2_ADDR_SIZE);
11996 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11999 fmt = NULL;
12002 /* Negative block_num stands for an index into ranges_by_label. */
12003 else if (block_num < 0)
12005 int lab_idx = - block_num - 1;
12007 if (!have_multiple_function_sections)
12009 gcc_unreachable ();
12010 #if 0
12011 /* If we ever use add_ranges_by_labels () for a single
12012 function section, all we have to do is to take out
12013 the #if 0 above. */
12014 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12015 (*ranges_by_label)[lab_idx].begin,
12016 text_section_label,
12017 fmt, i * 2 * DWARF2_ADDR_SIZE);
12018 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12019 (*ranges_by_label)[lab_idx].end,
12020 text_section_label, NULL);
12021 #endif
12023 else
12025 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12026 (*ranges_by_label)[lab_idx].begin,
12027 fmt, i * 2 * DWARF2_ADDR_SIZE);
12028 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12029 (*ranges_by_label)[lab_idx].end,
12030 NULL);
12033 else
12035 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12036 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12037 fmt = start_fmt;
12042 /* Non-zero if .debug_line_str should be used for .debug_line section
12043 strings or strings that are likely shareable with those. */
12044 #define DWARF5_USE_DEBUG_LINE_STR \
12045 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12046 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12047 /* FIXME: there is no .debug_line_str.dwo section, \
12048 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12049 && !dwarf_split_debug_info)
12052 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12053 DWARF5 .debug_line tables using .debug_line_str or we generate
12054 it ourselves, except for split-dwarf which doesn't have a
12055 .debug_line_str. */
12056 static bool
12057 asm_outputs_debug_line_str (void)
12059 if (dwarf_version >= 5
12060 && ! output_asm_line_debug_info ()
12061 && DWARF5_USE_DEBUG_LINE_STR)
12062 return true;
12063 else
12065 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12066 return !dwarf_split_debug_info && dwarf_version >= 5;
12067 #else
12068 return false;
12069 #endif
12073 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12074 I is index of the following range. */
12076 static bool
12077 use_distinct_base_address_for_range (unsigned int i)
12079 if (i >= vec_safe_length (ranges_table))
12080 return false;
12082 dw_ranges *r2 = &(*ranges_table)[i];
12083 /* Use DW_RLE_base_address{,x} if there is a next range in the
12084 range list and is guaranteed to be in the same section. */
12085 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12088 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12089 section when needed. */
12091 static void
12092 index_rnglists (void)
12094 unsigned i;
12095 dw_ranges *r;
12096 bool base = false;
12098 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12100 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12101 r->idx = rnglist_idx++;
12103 int block_num = r->num;
12104 if ((HAVE_AS_LEB128 || block_num < 0)
12105 && !have_multiple_function_sections)
12106 continue;
12107 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12108 base = false;
12109 if (block_num > 0)
12111 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12112 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12114 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12115 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12117 if (HAVE_AS_LEB128)
12119 if (!base && use_distinct_base_address_for_range (i + 1))
12121 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12122 ate_kind_label);
12123 base = true;
12125 if (base)
12126 /* If we have a base, no need for further
12127 begin_entry/end_entry, as DW_RLE_offset_pair will be
12128 used. */
12129 continue;
12130 r->begin_entry
12131 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12132 /* No need for end_entry, DW_RLE_start{,x}_length will use
12133 length as opposed to a pair of addresses. */
12135 else
12137 r->begin_entry
12138 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12139 r->end_entry
12140 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12144 /* Negative block_num stands for an index into ranges_by_label. */
12145 else if (block_num < 0)
12147 int lab_idx = - block_num - 1;
12148 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12149 const char *elabel = (*ranges_by_label)[lab_idx].end;
12151 r->begin_entry
12152 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12153 if (!HAVE_AS_LEB128)
12154 r->end_entry
12155 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12160 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12162 static bool
12163 output_rnglists (unsigned generation, bool dwo)
12165 unsigned i;
12166 dw_ranges *r;
12167 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12168 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12169 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12171 if (dwo)
12172 switch_to_section (debug_ranges_dwo_section);
12173 else
12175 switch_to_section (debug_ranges_section);
12176 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12178 /* There are up to 4 unique ranges labels per generation.
12179 See also init_sections_and_labels. */
12180 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12181 2 + 2 * dwo + generation * 6);
12182 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12183 3 + 2 * dwo + generation * 6);
12184 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12185 dw2_asm_output_data (4, 0xffffffff,
12186 "Initial length escape value indicating "
12187 "64-bit DWARF extension");
12188 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12189 "Length of Range Lists");
12190 ASM_OUTPUT_LABEL (asm_out_file, l1);
12191 output_dwarf_version ();
12192 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12193 dw2_asm_output_data (1, 0, "Segment Size");
12194 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12195 about relocation sizes and primarily care about the size of .debug*
12196 sections in linked shared libraries and executables, then
12197 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12198 into it are usually larger than just DW_FORM_sec_offset offsets
12199 into the .debug_rnglists section. */
12200 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12201 "Offset Entry Count");
12202 if (dwo)
12204 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12205 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12206 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12207 dw2_asm_output_delta (dwarf_offset_size, r->label,
12208 ranges_base_label, NULL);
12211 const char *lab = "";
12212 const char *base = NULL;
12213 bool skipping = false;
12214 bool ret = false;
12215 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12217 int block_num = r->num;
12219 if (r->label)
12221 if (dwarf_split_debug_info
12222 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12224 ret = true;
12225 skipping = true;
12226 continue;
12228 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12229 lab = r->label;
12231 if (skipping)
12233 if (block_num == 0)
12234 skipping = false;
12235 continue;
12237 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12238 base = NULL;
12239 if (block_num > 0)
12241 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12242 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12244 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12245 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12247 if (HAVE_AS_LEB128)
12249 /* If all code is in the text section, then the compilation
12250 unit base address defaults to DW_AT_low_pc, which is the
12251 base of the text section. */
12252 if (!have_multiple_function_sections)
12254 dw2_asm_output_data (1, DW_RLE_offset_pair,
12255 "DW_RLE_offset_pair (%s)", lab);
12256 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12257 "Range begin address (%s)", lab);
12258 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12259 "Range end address (%s)", lab);
12260 continue;
12262 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12264 if (dwarf_split_debug_info)
12266 dw2_asm_output_data (1, DW_RLE_base_addressx,
12267 "DW_RLE_base_addressx (%s)", lab);
12268 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12269 "Base address index (%s)",
12270 blabel);
12272 else
12274 dw2_asm_output_data (1, DW_RLE_base_address,
12275 "DW_RLE_base_address (%s)", lab);
12276 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12277 "Base address (%s)", lab);
12279 strcpy (basebuf, blabel);
12280 base = basebuf;
12282 if (base)
12284 dw2_asm_output_data (1, DW_RLE_offset_pair,
12285 "DW_RLE_offset_pair (%s)", lab);
12286 dw2_asm_output_delta_uleb128 (blabel, base,
12287 "Range begin address (%s)", lab);
12288 dw2_asm_output_delta_uleb128 (elabel, base,
12289 "Range end address (%s)", lab);
12290 continue;
12292 if (dwarf_split_debug_info)
12294 dw2_asm_output_data (1, DW_RLE_startx_length,
12295 "DW_RLE_startx_length (%s)", lab);
12296 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12297 "Range begin address index "
12298 "(%s)", blabel);
12300 else
12302 dw2_asm_output_data (1, DW_RLE_start_length,
12303 "DW_RLE_start_length (%s)", lab);
12304 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12305 "Range begin address (%s)", lab);
12307 dw2_asm_output_delta_uleb128 (elabel, blabel,
12308 "Range length (%s)", lab);
12310 else if (dwarf_split_debug_info)
12312 dw2_asm_output_data (1, DW_RLE_startx_endx,
12313 "DW_RLE_startx_endx (%s)", lab);
12314 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12315 "Range begin address index "
12316 "(%s)", blabel);
12317 dw2_asm_output_data_uleb128 (r->end_entry->index,
12318 "Range end address index "
12319 "(%s)", elabel);
12321 else
12323 dw2_asm_output_data (1, DW_RLE_start_end,
12324 "DW_RLE_start_end (%s)", lab);
12325 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12326 "Range begin address (%s)", lab);
12327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12328 "Range end address (%s)", lab);
12332 /* Negative block_num stands for an index into ranges_by_label. */
12333 else if (block_num < 0)
12335 int lab_idx = - block_num - 1;
12336 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12337 const char *elabel = (*ranges_by_label)[lab_idx].end;
12339 if (!have_multiple_function_sections)
12340 gcc_unreachable ();
12341 if (HAVE_AS_LEB128)
12343 if (dwarf_split_debug_info)
12345 dw2_asm_output_data (1, DW_RLE_startx_length,
12346 "DW_RLE_startx_length (%s)", lab);
12347 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12348 "Range begin address index "
12349 "(%s)", blabel);
12351 else
12353 dw2_asm_output_data (1, DW_RLE_start_length,
12354 "DW_RLE_start_length (%s)", lab);
12355 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12356 "Range begin address (%s)", lab);
12358 dw2_asm_output_delta_uleb128 (elabel, blabel,
12359 "Range length (%s)", lab);
12361 else if (dwarf_split_debug_info)
12363 dw2_asm_output_data (1, DW_RLE_startx_endx,
12364 "DW_RLE_startx_endx (%s)", lab);
12365 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12366 "Range begin address index "
12367 "(%s)", blabel);
12368 dw2_asm_output_data_uleb128 (r->end_entry->index,
12369 "Range end address index "
12370 "(%s)", elabel);
12372 else
12374 dw2_asm_output_data (1, DW_RLE_start_end,
12375 "DW_RLE_start_end (%s)", lab);
12376 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12377 "Range begin address (%s)", lab);
12378 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12379 "Range end address (%s)", lab);
12382 else
12383 dw2_asm_output_data (1, DW_RLE_end_of_list,
12384 "DW_RLE_end_of_list (%s)", lab);
12386 ASM_OUTPUT_LABEL (asm_out_file, l2);
12387 return ret;
12390 /* Data structure containing information about input files. */
12391 struct file_info
12393 const char *path; /* Complete file name. */
12394 const char *fname; /* File name part. */
12395 int length; /* Length of entire string. */
12396 struct dwarf_file_data * file_idx; /* Index in input file table. */
12397 int dir_idx; /* Index in directory table. */
12400 /* Data structure containing information about directories with source
12401 files. */
12402 struct dir_info
12404 const char *path; /* Path including directory name. */
12405 int length; /* Path length. */
12406 int prefix; /* Index of directory entry which is a prefix. */
12407 int count; /* Number of files in this directory. */
12408 int dir_idx; /* Index of directory used as base. */
12411 /* Callback function for file_info comparison. We sort by looking at
12412 the directories in the path. */
12414 static int
12415 file_info_cmp (const void *p1, const void *p2)
12417 const struct file_info *const s1 = (const struct file_info *) p1;
12418 const struct file_info *const s2 = (const struct file_info *) p2;
12419 const unsigned char *cp1;
12420 const unsigned char *cp2;
12422 /* Take care of file names without directories. We need to make sure that
12423 we return consistent values to qsort since some will get confused if
12424 we return the same value when identical operands are passed in opposite
12425 orders. So if neither has a directory, return 0 and otherwise return
12426 1 or -1 depending on which one has the directory. We want the one with
12427 the directory to sort after the one without, so all no directory files
12428 are at the start (normally only the compilation unit file). */
12429 if ((s1->path == s1->fname || s2->path == s2->fname))
12430 return (s2->path == s2->fname) - (s1->path == s1->fname);
12432 cp1 = (const unsigned char *) s1->path;
12433 cp2 = (const unsigned char *) s2->path;
12435 while (1)
12437 ++cp1;
12438 ++cp2;
12439 /* Reached the end of the first path? If so, handle like above,
12440 but now we want longer directory prefixes before shorter ones. */
12441 if ((cp1 == (const unsigned char *) s1->fname)
12442 || (cp2 == (const unsigned char *) s2->fname))
12443 return ((cp1 == (const unsigned char *) s1->fname)
12444 - (cp2 == (const unsigned char *) s2->fname));
12446 /* Character of current path component the same? */
12447 else if (*cp1 != *cp2)
12448 return *cp1 - *cp2;
12452 struct file_name_acquire_data
12454 struct file_info *files;
12455 int used_files;
12456 int max_files;
12459 /* Traversal function for the hash table. */
12462 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12464 struct dwarf_file_data *d = *slot;
12465 struct file_info *fi;
12466 const char *f;
12468 gcc_assert (fnad->max_files >= d->emitted_number);
12470 if (! d->emitted_number)
12471 return 1;
12473 gcc_assert (fnad->max_files != fnad->used_files);
12475 fi = fnad->files + fnad->used_files++;
12477 f = d->filename;
12479 /* Skip all leading "./". */
12480 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12481 f += 2;
12483 /* Create a new array entry. */
12484 fi->path = f;
12485 fi->length = strlen (f);
12486 fi->file_idx = d;
12488 /* Search for the file name part. */
12489 f = strrchr (f, DIR_SEPARATOR);
12490 #if defined (DIR_SEPARATOR_2)
12492 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12494 if (g != NULL)
12496 if (f == NULL || f < g)
12497 f = g;
12500 #endif
12502 fi->fname = f == NULL ? fi->path : f + 1;
12503 return 1;
12506 /* Helper function for output_file_names. Emit a FORM encoded
12507 string STR, with assembly comment start ENTRY_KIND and
12508 index IDX */
12510 static void
12511 output_line_string (enum dwarf_form form, const char *str,
12512 const char *entry_kind, unsigned int idx)
12514 switch (form)
12516 case DW_FORM_string:
12517 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12518 break;
12519 case DW_FORM_line_strp:
12520 if (!debug_line_str_hash)
12521 debug_line_str_hash
12522 = hash_table<indirect_string_hasher>::create_ggc (10);
12524 struct indirect_string_node *node;
12525 node = find_AT_string_in_table (str, debug_line_str_hash);
12526 set_indirect_string (node);
12527 node->form = form;
12528 dw2_asm_output_offset (dwarf_offset_size, node->label,
12529 debug_line_str_section, "%s: %#x: \"%s\"",
12530 entry_kind, 0, node->str);
12531 break;
12532 default:
12533 gcc_unreachable ();
12537 /* Output the directory table and the file name table. We try to minimize
12538 the total amount of memory needed. A heuristic is used to avoid large
12539 slowdowns with many input files. */
12541 static void
12542 output_file_names (void)
12544 struct file_name_acquire_data fnad;
12545 int numfiles;
12546 struct file_info *files;
12547 struct dir_info *dirs;
12548 int *saved;
12549 int *savehere;
12550 int *backmap;
12551 int ndirs;
12552 int idx_offset;
12553 int i;
12555 if (!last_emitted_file)
12557 if (dwarf_version >= 5)
12559 const char *comp_dir = comp_dir_string ();
12560 if (comp_dir == NULL)
12561 comp_dir = "";
12562 dw2_asm_output_data (1, 1, "Directory entry format count");
12563 enum dwarf_form str_form = DW_FORM_string;
12564 if (DWARF5_USE_DEBUG_LINE_STR)
12565 str_form = DW_FORM_line_strp;
12566 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12567 dw2_asm_output_data_uleb128 (str_form, "%s",
12568 get_DW_FORM_name (str_form));
12569 dw2_asm_output_data_uleb128 (1, "Directories count");
12570 if (str_form == DW_FORM_string)
12571 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12572 else
12573 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12574 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12575 if (filename0 == NULL)
12576 filename0 = "";
12577 #ifdef VMS_DEBUGGING_INFO
12578 dw2_asm_output_data (1, 4, "File name entry format count");
12579 #else
12580 dw2_asm_output_data (1, 2, "File name entry format count");
12581 #endif
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 (DW_LNCT_directory_index,
12586 "DW_LNCT_directory_index");
12587 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12588 get_DW_FORM_name (DW_FORM_data1));
12589 #ifdef VMS_DEBUGGING_INFO
12590 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12591 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12592 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12593 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12594 #endif
12595 dw2_asm_output_data_uleb128 (1, "File names count");
12597 output_line_string (str_form, filename0, "File Entry", 0);
12598 dw2_asm_output_data (1, 0, NULL);
12599 #ifdef VMS_DEBUGGING_INFO
12600 dw2_asm_output_data_uleb128 (0, NULL);
12601 dw2_asm_output_data_uleb128 (0, NULL);
12602 #endif
12604 else
12606 dw2_asm_output_data (1, 0, "End directory table");
12607 dw2_asm_output_data (1, 0, "End file name table");
12609 return;
12612 numfiles = last_emitted_file->emitted_number;
12614 /* Allocate the various arrays we need. */
12615 files = XALLOCAVEC (struct file_info, numfiles);
12616 dirs = XALLOCAVEC (struct dir_info, numfiles);
12618 fnad.files = files;
12619 fnad.used_files = 0;
12620 fnad.max_files = numfiles;
12621 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12622 gcc_assert (fnad.used_files == fnad.max_files);
12624 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12626 /* Find all the different directories used. */
12627 dirs[0].path = files[0].path;
12628 dirs[0].length = files[0].fname - files[0].path;
12629 dirs[0].prefix = -1;
12630 dirs[0].count = 1;
12631 dirs[0].dir_idx = 0;
12632 files[0].dir_idx = 0;
12633 ndirs = 1;
12635 for (i = 1; i < numfiles; i++)
12636 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12637 && memcmp (dirs[ndirs - 1].path, files[i].path,
12638 dirs[ndirs - 1].length) == 0)
12640 /* Same directory as last entry. */
12641 files[i].dir_idx = ndirs - 1;
12642 ++dirs[ndirs - 1].count;
12644 else
12646 int j;
12648 /* This is a new directory. */
12649 dirs[ndirs].path = files[i].path;
12650 dirs[ndirs].length = files[i].fname - files[i].path;
12651 dirs[ndirs].count = 1;
12652 dirs[ndirs].dir_idx = ndirs;
12653 files[i].dir_idx = ndirs;
12655 /* Search for a prefix. */
12656 dirs[ndirs].prefix = -1;
12657 for (j = 0; j < ndirs; j++)
12658 if (dirs[j].length < dirs[ndirs].length
12659 && dirs[j].length > 1
12660 && (dirs[ndirs].prefix == -1
12661 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12662 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12663 dirs[ndirs].prefix = j;
12665 ++ndirs;
12668 /* Now to the actual work. We have to find a subset of the directories which
12669 allow expressing the file name using references to the directory table
12670 with the least amount of characters. We do not do an exhaustive search
12671 where we would have to check out every combination of every single
12672 possible prefix. Instead we use a heuristic which provides nearly optimal
12673 results in most cases and never is much off. */
12674 saved = XALLOCAVEC (int, ndirs);
12675 savehere = XALLOCAVEC (int, ndirs);
12677 memset (saved, '\0', ndirs * sizeof (saved[0]));
12678 for (i = 0; i < ndirs; i++)
12680 int j;
12681 int total;
12683 /* We can always save some space for the current directory. But this
12684 does not mean it will be enough to justify adding the directory. */
12685 savehere[i] = dirs[i].length;
12686 total = (savehere[i] - saved[i]) * dirs[i].count;
12688 for (j = i + 1; j < ndirs; j++)
12690 savehere[j] = 0;
12691 if (saved[j] < dirs[i].length)
12693 /* Determine whether the dirs[i] path is a prefix of the
12694 dirs[j] path. */
12695 int k;
12697 k = dirs[j].prefix;
12698 while (k != -1 && k != (int) i)
12699 k = dirs[k].prefix;
12701 if (k == (int) i)
12703 /* Yes it is. We can possibly save some memory by
12704 writing the filenames in dirs[j] relative to
12705 dirs[i]. */
12706 savehere[j] = dirs[i].length;
12707 total += (savehere[j] - saved[j]) * dirs[j].count;
12712 /* Check whether we can save enough to justify adding the dirs[i]
12713 directory. */
12714 if (total > dirs[i].length + 1)
12716 /* It's worthwhile adding. */
12717 for (j = i; j < ndirs; j++)
12718 if (savehere[j] > 0)
12720 /* Remember how much we saved for this directory so far. */
12721 saved[j] = savehere[j];
12723 /* Remember the prefix directory. */
12724 dirs[j].dir_idx = i;
12729 /* Emit the directory name table. */
12730 idx_offset = dirs[0].length > 0 ? 1 : 0;
12731 enum dwarf_form str_form = DW_FORM_string;
12732 enum dwarf_form idx_form = DW_FORM_udata;
12733 if (dwarf_version >= 5)
12735 const char *comp_dir = comp_dir_string ();
12736 if (comp_dir == NULL)
12737 comp_dir = "";
12738 dw2_asm_output_data (1, 1, "Directory entry format count");
12739 if (DWARF5_USE_DEBUG_LINE_STR)
12740 str_form = DW_FORM_line_strp;
12741 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12742 dw2_asm_output_data_uleb128 (str_form, "%s",
12743 get_DW_FORM_name (str_form));
12744 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12745 if (str_form == DW_FORM_string)
12747 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12748 for (i = 1 - idx_offset; i < ndirs; i++)
12749 dw2_asm_output_nstring (dirs[i].path,
12750 dirs[i].length
12751 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12752 "Directory Entry: %#x", i + idx_offset);
12754 else
12756 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12757 for (i = 1 - idx_offset; i < ndirs; i++)
12759 const char *str
12760 = ggc_alloc_string (dirs[i].path,
12761 dirs[i].length
12762 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12763 output_line_string (str_form, str, "Directory Entry",
12764 (unsigned) i + idx_offset);
12768 else
12770 for (i = 1 - idx_offset; i < ndirs; i++)
12771 dw2_asm_output_nstring (dirs[i].path,
12772 dirs[i].length
12773 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12774 "Directory Entry: %#x", i + idx_offset);
12776 dw2_asm_output_data (1, 0, "End directory table");
12779 /* We have to emit them in the order of emitted_number since that's
12780 used in the debug info generation. To do this efficiently we
12781 generate a back-mapping of the indices first. */
12782 backmap = XALLOCAVEC (int, numfiles);
12783 for (i = 0; i < numfiles; i++)
12784 backmap[files[i].file_idx->emitted_number - 1] = i;
12786 if (dwarf_version >= 5)
12788 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12789 if (filename0 == NULL)
12790 filename0 = "";
12791 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12792 DW_FORM_data2. Choose one based on the number of directories
12793 and how much space would they occupy in each encoding.
12794 If we have at most 256 directories, all indexes fit into
12795 a single byte, so DW_FORM_data1 is most compact (if there
12796 are at most 128 directories, DW_FORM_udata would be as
12797 compact as that, but not shorter and slower to decode). */
12798 if (ndirs + idx_offset <= 256)
12799 idx_form = DW_FORM_data1;
12800 /* If there are more than 65536 directories, we have to use
12801 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12802 Otherwise, compute what space would occupy if all the indexes
12803 used DW_FORM_udata - sum - and compare that to how large would
12804 be DW_FORM_data2 encoding, and pick the more efficient one. */
12805 else if (ndirs + idx_offset <= 65536)
12807 unsigned HOST_WIDE_INT sum = 1;
12808 for (i = 0; i < numfiles; i++)
12810 int file_idx = backmap[i];
12811 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12812 sum += size_of_uleb128 (dir_idx);
12814 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12815 idx_form = DW_FORM_data2;
12817 #ifdef VMS_DEBUGGING_INFO
12818 dw2_asm_output_data (1, 4, "File name entry format count");
12819 #else
12820 dw2_asm_output_data (1, 2, "File name entry format count");
12821 #endif
12822 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12823 dw2_asm_output_data_uleb128 (str_form, "%s",
12824 get_DW_FORM_name (str_form));
12825 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12826 "DW_LNCT_directory_index");
12827 dw2_asm_output_data_uleb128 (idx_form, "%s",
12828 get_DW_FORM_name (idx_form));
12829 #ifdef VMS_DEBUGGING_INFO
12830 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12831 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12832 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12833 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12834 #endif
12835 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12837 output_line_string (str_form, filename0, "File Entry", 0);
12839 /* Include directory index. */
12840 if (idx_form != DW_FORM_udata)
12841 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12842 0, NULL);
12843 else
12844 dw2_asm_output_data_uleb128 (0, NULL);
12846 #ifdef VMS_DEBUGGING_INFO
12847 dw2_asm_output_data_uleb128 (0, NULL);
12848 dw2_asm_output_data_uleb128 (0, NULL);
12849 #endif
12852 /* Now write all the file names. */
12853 for (i = 0; i < numfiles; i++)
12855 int file_idx = backmap[i];
12856 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12858 #ifdef VMS_DEBUGGING_INFO
12859 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12861 /* Setting these fields can lead to debugger miscomparisons,
12862 but VMS Debug requires them to be set correctly. */
12864 int ver;
12865 long long cdt;
12866 long siz;
12867 int maxfilelen = (strlen (files[file_idx].path)
12868 + dirs[dir_idx].length
12869 + MAX_VMS_VERSION_LEN + 1);
12870 char *filebuf = XALLOCAVEC (char, maxfilelen);
12872 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12873 snprintf (filebuf, maxfilelen, "%s;%d",
12874 files[file_idx].path + dirs[dir_idx].length, ver);
12876 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12878 /* Include directory index. */
12879 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12880 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12881 dir_idx + idx_offset, NULL);
12882 else
12883 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12885 /* Modification time. */
12886 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12887 &cdt, 0, 0, 0) == 0)
12888 ? cdt : 0, NULL);
12890 /* File length in bytes. */
12891 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12892 0, &siz, 0, 0) == 0)
12893 ? siz : 0, NULL);
12894 #else
12895 output_line_string (str_form,
12896 files[file_idx].path + dirs[dir_idx].length,
12897 "File Entry", (unsigned) i + 1);
12899 /* Include directory index. */
12900 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12901 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12902 dir_idx + idx_offset, NULL);
12903 else
12904 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12906 if (dwarf_version >= 5)
12907 continue;
12909 /* Modification time. */
12910 dw2_asm_output_data_uleb128 (0, NULL);
12912 /* File length in bytes. */
12913 dw2_asm_output_data_uleb128 (0, NULL);
12914 #endif /* VMS_DEBUGGING_INFO */
12917 if (dwarf_version < 5)
12918 dw2_asm_output_data (1, 0, "End file name table");
12922 /* Output one line number table into the .debug_line section. */
12924 static void
12925 output_one_line_info_table (dw_line_info_table *table)
12927 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12928 unsigned int current_line = 1;
12929 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12930 dw_line_info_entry *ent, *prev_addr = NULL;
12931 size_t i;
12932 unsigned int view;
12934 view = 0;
12936 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12938 switch (ent->opcode)
12940 case LI_set_address:
12941 /* ??? Unfortunately, we have little choice here currently, and
12942 must always use the most general form. GCC does not know the
12943 address delta itself, so we can't use DW_LNS_advance_pc. Many
12944 ports do have length attributes which will give an upper bound
12945 on the address range. We could perhaps use length attributes
12946 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12947 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12949 view = 0;
12951 /* This can handle any delta. This takes
12952 4+DWARF2_ADDR_SIZE bytes. */
12953 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12954 debug_variable_location_views
12955 ? ", reset view to 0" : "");
12956 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12957 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12958 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12960 prev_addr = ent;
12961 break;
12963 case LI_adv_address:
12965 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12966 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12967 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12969 view++;
12971 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12972 dw2_asm_output_delta (2, line_label, prev_label,
12973 "from %s to %s", prev_label, line_label);
12975 prev_addr = ent;
12976 break;
12979 case LI_set_line:
12980 if (ent->val == current_line)
12982 /* We still need to start a new row, so output a copy insn. */
12983 dw2_asm_output_data (1, DW_LNS_copy,
12984 "copy line %u", current_line);
12986 else
12988 int line_offset = ent->val - current_line;
12989 int line_delta = line_offset - DWARF_LINE_BASE;
12991 current_line = ent->val;
12992 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12994 /* This can handle deltas from -10 to 234, using the current
12995 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12996 This takes 1 byte. */
12997 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12998 "line %u", current_line);
13000 else
13002 /* This can handle any delta. This takes at least 4 bytes,
13003 depending on the value being encoded. */
13004 dw2_asm_output_data (1, DW_LNS_advance_line,
13005 "advance to line %u", current_line);
13006 dw2_asm_output_data_sleb128 (line_offset, NULL);
13007 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13010 break;
13012 case LI_set_file:
13013 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13014 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13015 break;
13017 case LI_set_column:
13018 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13019 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13020 break;
13022 case LI_negate_stmt:
13023 current_is_stmt = !current_is_stmt;
13024 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13025 "is_stmt %d", current_is_stmt);
13026 break;
13028 case LI_set_prologue_end:
13029 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13030 "set prologue end");
13031 break;
13033 case LI_set_epilogue_begin:
13034 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13035 "set epilogue begin");
13036 break;
13038 case LI_set_discriminator:
13039 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13040 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13041 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13042 dw2_asm_output_data_uleb128 (ent->val, NULL);
13043 break;
13047 /* Emit debug info for the address of the end of the table. */
13048 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13049 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13050 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13051 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13053 dw2_asm_output_data (1, 0, "end sequence");
13054 dw2_asm_output_data_uleb128 (1, NULL);
13055 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13058 static unsigned int output_line_info_generation;
13060 /* Output the source line number correspondence information. This
13061 information goes into the .debug_line section. */
13063 static void
13064 output_line_info (bool prologue_only)
13066 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13067 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13068 bool saw_one = false;
13069 int opc;
13071 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13072 output_line_info_generation);
13073 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13074 output_line_info_generation);
13075 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13076 output_line_info_generation);
13077 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13078 output_line_info_generation++);
13080 if (!XCOFF_DEBUGGING_INFO)
13082 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13083 dw2_asm_output_data (4, 0xffffffff,
13084 "Initial length escape value indicating 64-bit DWARF extension");
13085 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13086 "Length of Source Line Info");
13089 ASM_OUTPUT_LABEL (asm_out_file, l1);
13091 output_dwarf_version ();
13092 if (dwarf_version >= 5)
13094 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13095 dw2_asm_output_data (1, 0, "Segment Size");
13097 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13098 ASM_OUTPUT_LABEL (asm_out_file, p1);
13100 /* Define the architecture-dependent minimum instruction length (in bytes).
13101 In this implementation of DWARF, this field is used for information
13102 purposes only. Since GCC generates assembly language, we have no
13103 a priori knowledge of how many instruction bytes are generated for each
13104 source line, and therefore can use only the DW_LNE_set_address and
13105 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13106 this as '1', which is "correct enough" for all architectures,
13107 and don't let the target override. */
13108 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13110 if (dwarf_version >= 4)
13111 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13112 "Maximum Operations Per Instruction");
13113 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13114 "Default is_stmt_start flag");
13115 dw2_asm_output_data (1, DWARF_LINE_BASE,
13116 "Line Base Value (Special Opcodes)");
13117 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13118 "Line Range Value (Special Opcodes)");
13119 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13120 "Special Opcode Base");
13122 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13124 int n_op_args;
13125 switch (opc)
13127 case DW_LNS_advance_pc:
13128 case DW_LNS_advance_line:
13129 case DW_LNS_set_file:
13130 case DW_LNS_set_column:
13131 case DW_LNS_fixed_advance_pc:
13132 case DW_LNS_set_isa:
13133 n_op_args = 1;
13134 break;
13135 default:
13136 n_op_args = 0;
13137 break;
13140 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13141 opc, n_op_args);
13144 /* Write out the information about the files we use. */
13145 output_file_names ();
13146 ASM_OUTPUT_LABEL (asm_out_file, p2);
13147 if (prologue_only)
13149 /* Output the marker for the end of the line number info. */
13150 ASM_OUTPUT_LABEL (asm_out_file, l2);
13151 return;
13154 if (separate_line_info)
13156 dw_line_info_table *table;
13157 size_t i;
13159 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13160 if (table->in_use)
13162 output_one_line_info_table (table);
13163 saw_one = true;
13166 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13168 output_one_line_info_table (cold_text_section_line_info);
13169 saw_one = true;
13172 /* ??? Some Darwin linkers crash on a .debug_line section with no
13173 sequences. Further, merely a DW_LNE_end_sequence entry is not
13174 sufficient -- the address column must also be initialized.
13175 Make sure to output at least one set_address/end_sequence pair,
13176 choosing .text since that section is always present. */
13177 if (text_section_line_info->in_use || !saw_one)
13178 output_one_line_info_table (text_section_line_info);
13180 /* Output the marker for the end of the line number info. */
13181 ASM_OUTPUT_LABEL (asm_out_file, l2);
13184 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13186 static inline bool
13187 need_endianity_attribute_p (bool reverse)
13189 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13192 /* Given a pointer to a tree node for some base type, return a pointer to
13193 a DIE that describes the given type. REVERSE is true if the type is
13194 to be interpreted in the reverse storage order wrt the target order.
13196 This routine must only be called for GCC type nodes that correspond to
13197 Dwarf base (fundamental) types. */
13199 dw_die_ref
13200 base_type_die (tree type, bool reverse)
13202 dw_die_ref base_type_result;
13203 enum dwarf_type encoding;
13204 bool fpt_used = false;
13205 struct fixed_point_type_info fpt_info;
13206 tree type_bias = NULL_TREE;
13208 /* If this is a subtype that should not be emitted as a subrange type,
13209 use the base type. See subrange_type_for_debug_p. */
13210 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13211 type = TREE_TYPE (type);
13213 switch (TREE_CODE (type))
13215 case INTEGER_TYPE:
13216 if ((dwarf_version >= 4 || !dwarf_strict)
13217 && TYPE_NAME (type)
13218 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13219 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13220 && DECL_NAME (TYPE_NAME (type)))
13222 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13223 if (strcmp (name, "char16_t") == 0
13224 || strcmp (name, "char8_t") == 0
13225 || strcmp (name, "char32_t") == 0)
13227 encoding = DW_ATE_UTF;
13228 break;
13231 if ((dwarf_version >= 3 || !dwarf_strict)
13232 && lang_hooks.types.get_fixed_point_type_info)
13234 memset (&fpt_info, 0, sizeof (fpt_info));
13235 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13237 fpt_used = true;
13238 encoding = ((TYPE_UNSIGNED (type))
13239 ? DW_ATE_unsigned_fixed
13240 : DW_ATE_signed_fixed);
13241 break;
13244 if (TYPE_STRING_FLAG (type))
13246 if ((dwarf_version >= 4 || !dwarf_strict)
13247 && is_rust ()
13248 && int_size_in_bytes (type) == 4)
13249 encoding = DW_ATE_UTF;
13250 else if (TYPE_UNSIGNED (type))
13251 encoding = DW_ATE_unsigned_char;
13252 else
13253 encoding = DW_ATE_signed_char;
13255 else if (TYPE_UNSIGNED (type))
13256 encoding = DW_ATE_unsigned;
13257 else
13258 encoding = DW_ATE_signed;
13260 if (!dwarf_strict
13261 && lang_hooks.types.get_type_bias)
13262 type_bias = lang_hooks.types.get_type_bias (type);
13263 break;
13265 case REAL_TYPE:
13266 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13268 if (dwarf_version >= 3 || !dwarf_strict)
13269 encoding = DW_ATE_decimal_float;
13270 else
13271 encoding = DW_ATE_lo_user;
13273 else
13274 encoding = DW_ATE_float;
13275 break;
13277 case FIXED_POINT_TYPE:
13278 if (!(dwarf_version >= 3 || !dwarf_strict))
13279 encoding = DW_ATE_lo_user;
13280 else if (TYPE_UNSIGNED (type))
13281 encoding = DW_ATE_unsigned_fixed;
13282 else
13283 encoding = DW_ATE_signed_fixed;
13284 break;
13286 /* Dwarf2 doesn't know anything about complex ints, so use
13287 a user defined type for it. */
13288 case COMPLEX_TYPE:
13289 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13290 encoding = DW_ATE_complex_float;
13291 else
13292 encoding = DW_ATE_lo_user;
13293 break;
13295 case BOOLEAN_TYPE:
13296 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13297 encoding = DW_ATE_boolean;
13298 break;
13300 default:
13301 /* No other TREE_CODEs are Dwarf fundamental types. */
13302 gcc_unreachable ();
13305 base_type_result = new_die_raw (DW_TAG_base_type);
13307 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13308 int_size_in_bytes (type));
13309 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13311 if (need_endianity_attribute_p (reverse))
13312 add_AT_unsigned (base_type_result, DW_AT_endianity,
13313 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13315 add_alignment_attribute (base_type_result, type);
13317 if (fpt_used)
13319 switch (fpt_info.scale_factor_kind)
13321 case fixed_point_scale_factor_binary:
13322 add_AT_int (base_type_result, DW_AT_binary_scale,
13323 fpt_info.scale_factor.binary);
13324 break;
13326 case fixed_point_scale_factor_decimal:
13327 add_AT_int (base_type_result, DW_AT_decimal_scale,
13328 fpt_info.scale_factor.decimal);
13329 break;
13331 case fixed_point_scale_factor_arbitrary:
13332 /* Arbitrary scale factors cannot be described in standard DWARF. */
13333 if (!dwarf_strict)
13335 /* Describe the scale factor as a rational constant. */
13336 const dw_die_ref scale_factor
13337 = new_die (DW_TAG_constant, comp_unit_die (), type);
13339 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13340 fpt_info.scale_factor.arbitrary.numerator,
13341 dw_scalar_form_constant, NULL);
13342 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13343 fpt_info.scale_factor.arbitrary.denominator,
13344 dw_scalar_form_constant, NULL);
13346 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13348 break;
13350 default:
13351 gcc_unreachable ();
13355 if (type_bias)
13356 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13357 dw_scalar_form_constant
13358 | dw_scalar_form_exprloc
13359 | dw_scalar_form_reference,
13360 NULL);
13362 return base_type_result;
13365 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13366 named 'auto' in its type: return true for it, false otherwise. */
13368 static inline bool
13369 is_cxx_auto (tree type)
13371 if (is_cxx ())
13373 tree name = TYPE_IDENTIFIER (type);
13374 if (name == get_identifier ("auto")
13375 || name == get_identifier ("decltype(auto)"))
13376 return true;
13378 return false;
13381 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13382 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13384 static inline int
13385 is_base_type (tree type)
13387 switch (TREE_CODE (type))
13389 case INTEGER_TYPE:
13390 case REAL_TYPE:
13391 case FIXED_POINT_TYPE:
13392 case COMPLEX_TYPE:
13393 case BOOLEAN_TYPE:
13394 return 1;
13396 case VOID_TYPE:
13397 case OPAQUE_TYPE:
13398 case ARRAY_TYPE:
13399 case RECORD_TYPE:
13400 case UNION_TYPE:
13401 case QUAL_UNION_TYPE:
13402 case ENUMERAL_TYPE:
13403 case FUNCTION_TYPE:
13404 case METHOD_TYPE:
13405 case POINTER_TYPE:
13406 case REFERENCE_TYPE:
13407 case NULLPTR_TYPE:
13408 case OFFSET_TYPE:
13409 case LANG_TYPE:
13410 case VECTOR_TYPE:
13411 return 0;
13413 default:
13414 if (is_cxx_auto (type))
13415 return 0;
13416 gcc_unreachable ();
13420 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13421 node, return the size in bits for the type if it is a constant, or else
13422 return the alignment for the type if the type's size is not constant, or
13423 else return BITS_PER_WORD if the type actually turns out to be an
13424 ERROR_MARK node. */
13426 static inline unsigned HOST_WIDE_INT
13427 simple_type_size_in_bits (const_tree type)
13429 if (TREE_CODE (type) == ERROR_MARK)
13430 return BITS_PER_WORD;
13431 else if (TYPE_SIZE (type) == NULL_TREE)
13432 return 0;
13433 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13434 return tree_to_uhwi (TYPE_SIZE (type));
13435 else
13436 return TYPE_ALIGN (type);
13439 /* Similarly, but return an offset_int instead of UHWI. */
13441 static inline offset_int
13442 offset_int_type_size_in_bits (const_tree type)
13444 if (TREE_CODE (type) == ERROR_MARK)
13445 return BITS_PER_WORD;
13446 else if (TYPE_SIZE (type) == NULL_TREE)
13447 return 0;
13448 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13449 return wi::to_offset (TYPE_SIZE (type));
13450 else
13451 return TYPE_ALIGN (type);
13454 /* Given a pointer to a tree node for a subrange type, return a pointer
13455 to a DIE that describes the given type. */
13457 static dw_die_ref
13458 subrange_type_die (tree type, tree low, tree high, tree bias,
13459 dw_die_ref context_die)
13461 dw_die_ref subrange_die;
13462 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13464 if (context_die == NULL)
13465 context_die = comp_unit_die ();
13467 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13469 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13471 /* The size of the subrange type and its base type do not match,
13472 so we need to generate a size attribute for the subrange type. */
13473 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13476 add_alignment_attribute (subrange_die, type);
13478 if (low)
13479 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13480 if (high)
13481 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13482 if (bias && !dwarf_strict)
13483 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13484 dw_scalar_form_constant
13485 | dw_scalar_form_exprloc
13486 | dw_scalar_form_reference,
13487 NULL);
13489 return subrange_die;
13492 /* Returns the (const and/or volatile) cv_qualifiers associated with
13493 the decl node. This will normally be augmented with the
13494 cv_qualifiers of the underlying type in add_type_attribute. */
13496 static int
13497 decl_quals (const_tree decl)
13499 return ((TREE_READONLY (decl)
13500 /* The C++ front-end correctly marks reference-typed
13501 variables as readonly, but from a language (and debug
13502 info) standpoint they are not const-qualified. */
13503 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13504 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13505 | (TREE_THIS_VOLATILE (decl)
13506 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13509 /* Determine the TYPE whose qualifiers match the largest strict subset
13510 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13511 qualifiers outside QUAL_MASK. */
13513 static int
13514 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13516 tree t;
13517 int best_rank = 0, best_qual = 0, max_rank;
13519 type_quals &= qual_mask;
13520 max_rank = popcount_hwi (type_quals) - 1;
13522 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13523 t = TYPE_NEXT_VARIANT (t))
13525 int q = TYPE_QUALS (t) & qual_mask;
13527 if ((q & type_quals) == q && q != type_quals
13528 && check_base_type (t, type))
13530 int rank = popcount_hwi (q);
13532 if (rank > best_rank)
13534 best_rank = rank;
13535 best_qual = q;
13540 return best_qual;
13543 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13544 static const dwarf_qual_info_t dwarf_qual_info[] =
13546 { TYPE_QUAL_CONST, DW_TAG_const_type },
13547 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13548 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13549 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13551 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13553 /* If DIE is a qualified DIE of some base DIE with the same parent,
13554 return the base DIE, otherwise return NULL. Set MASK to the
13555 qualifiers added compared to the returned DIE. */
13557 static dw_die_ref
13558 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13560 unsigned int i;
13561 for (i = 0; i < dwarf_qual_info_size; i++)
13562 if (die->die_tag == dwarf_qual_info[i].t)
13563 break;
13564 if (i == dwarf_qual_info_size)
13565 return NULL;
13566 if (vec_safe_length (die->die_attr) != 1)
13567 return NULL;
13568 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13569 if (type == NULL || type->die_parent != die->die_parent)
13570 return NULL;
13571 *mask |= dwarf_qual_info[i].q;
13572 if (depth)
13574 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13575 if (ret)
13576 return ret;
13578 return type;
13581 /* If TYPE is long double or complex long double that
13582 should be emitted as artificial typedef to _Float128 or
13583 complex _Float128, return the type it should be emitted as.
13584 This is done in case the target already supports 16-byte
13585 composite floating point type (ibm_extended_format). */
13587 static tree
13588 long_double_as_float128 (tree type)
13590 if (type != long_double_type_node
13591 && type != complex_long_double_type_node)
13592 return NULL_TREE;
13594 machine_mode mode, fmode;
13595 if (TREE_CODE (type) == COMPLEX_TYPE)
13596 mode = TYPE_MODE (TREE_TYPE (type));
13597 else
13598 mode = TYPE_MODE (type);
13599 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13600 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13601 if (known_eq (GET_MODE_SIZE (fmode), 16)
13602 && MODE_COMPOSITE_P (fmode))
13604 if (type == long_double_type_node)
13606 if (float128_type_node
13607 && (TYPE_MODE (float128_type_node)
13608 == TYPE_MODE (type)))
13609 return float128_type_node;
13610 return NULL_TREE;
13612 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13613 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13614 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13615 == TYPE_MODE (type)))
13616 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13619 return NULL_TREE;
13622 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13623 entry that chains the modifiers specified by CV_QUALS in front of the
13624 given type. REVERSE is true if the type is to be interpreted in the
13625 reverse storage order wrt the target order. */
13627 static dw_die_ref
13628 modified_type_die (tree type, int cv_quals, bool reverse,
13629 dw_die_ref context_die)
13631 enum tree_code code = TREE_CODE (type);
13632 dw_die_ref mod_type_die;
13633 dw_die_ref sub_die = NULL;
13634 tree item_type = NULL;
13635 tree qualified_type;
13636 tree name, low, high;
13637 dw_die_ref mod_scope;
13638 struct array_descr_info info;
13639 /* Only these cv-qualifiers are currently handled. */
13640 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13641 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13642 ENCODE_QUAL_ADDR_SPACE(~0U));
13643 const bool reverse_base_type
13644 = need_endianity_attribute_p (reverse) && is_base_type (type);
13646 if (code == ERROR_MARK)
13647 return NULL;
13649 if (lang_hooks.types.get_debug_type)
13651 tree debug_type = lang_hooks.types.get_debug_type (type);
13653 if (debug_type != NULL_TREE && debug_type != type)
13654 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13657 cv_quals &= cv_qual_mask;
13659 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13660 tag modifier (and not an attribute) old consumers won't be able
13661 to handle it. */
13662 if (dwarf_version < 3)
13663 cv_quals &= ~TYPE_QUAL_RESTRICT;
13665 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13666 if (dwarf_version < 5)
13667 cv_quals &= ~TYPE_QUAL_ATOMIC;
13669 /* See if we already have the appropriately qualified variant of
13670 this type. */
13671 qualified_type = get_qualified_type (type, cv_quals);
13673 if (qualified_type == sizetype)
13675 /* Try not to expose the internal sizetype type's name. */
13676 if (TYPE_NAME (qualified_type)
13677 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13679 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13681 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13682 && (TYPE_PRECISION (t)
13683 == TYPE_PRECISION (qualified_type))
13684 && (TYPE_UNSIGNED (t)
13685 == TYPE_UNSIGNED (qualified_type)));
13686 qualified_type = t;
13688 else if (qualified_type == sizetype
13689 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13690 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13691 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13692 qualified_type = size_type_node;
13693 if (type == sizetype)
13694 type = qualified_type;
13697 /* If we do, then we can just use its DIE, if it exists. */
13698 if (qualified_type)
13700 mod_type_die = lookup_type_die (qualified_type);
13702 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13703 dealt with specially: the DIE with the attribute, if it exists, is
13704 placed immediately after the regular DIE for the same base type. */
13705 if (mod_type_die
13706 && (!reverse_base_type
13707 || ((mod_type_die = mod_type_die->die_sib) != NULL
13708 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13709 return mod_type_die;
13712 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13714 /* Handle C typedef types. */
13715 if (name
13716 && TREE_CODE (name) == TYPE_DECL
13717 && DECL_ORIGINAL_TYPE (name)
13718 && !DECL_ARTIFICIAL (name))
13720 tree dtype = TREE_TYPE (name);
13722 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13723 if (qualified_type == dtype && !reverse_base_type)
13725 tree origin = decl_ultimate_origin (name);
13727 /* Typedef variants that have an abstract origin don't get their own
13728 type DIE (see gen_typedef_die), so fall back on the ultimate
13729 abstract origin instead. */
13730 if (origin != NULL && origin != name)
13731 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13732 context_die);
13734 /* For a named type, use the typedef. */
13735 gen_type_die (qualified_type, context_die);
13736 return lookup_type_die (qualified_type);
13738 else
13740 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13741 dquals &= cv_qual_mask;
13742 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13743 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13744 /* cv-unqualified version of named type. Just use
13745 the unnamed type to which it refers. */
13746 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13747 reverse, context_die);
13748 /* Else cv-qualified version of named type; fall through. */
13752 mod_scope = scope_die_for (type, context_die);
13754 if (cv_quals)
13756 int sub_quals = 0, first_quals = 0;
13757 unsigned i;
13758 dw_die_ref first = NULL, last = NULL;
13760 /* Determine a lesser qualified type that most closely matches
13761 this one. Then generate DW_TAG_* entries for the remaining
13762 qualifiers. */
13763 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13764 cv_qual_mask);
13765 if (sub_quals && use_debug_types)
13767 bool needed = false;
13768 /* If emitting type units, make sure the order of qualifiers
13769 is canonical. Thus, start from unqualified type if
13770 an earlier qualifier is missing in sub_quals, but some later
13771 one is present there. */
13772 for (i = 0; i < dwarf_qual_info_size; i++)
13773 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13774 needed = true;
13775 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13777 sub_quals = 0;
13778 break;
13781 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13782 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13784 /* As not all intermediate qualified DIEs have corresponding
13785 tree types, ensure that qualified DIEs in the same scope
13786 as their DW_AT_type are emitted after their DW_AT_type,
13787 only with other qualified DIEs for the same type possibly
13788 in between them. Determine the range of such qualified
13789 DIEs now (first being the base type, last being corresponding
13790 last qualified DIE for it). */
13791 unsigned int count = 0;
13792 first = qualified_die_p (mod_type_die, &first_quals,
13793 dwarf_qual_info_size);
13794 if (first == NULL)
13795 first = mod_type_die;
13796 gcc_assert ((first_quals & ~sub_quals) == 0);
13797 for (count = 0, last = first;
13798 count < (1U << dwarf_qual_info_size);
13799 count++, last = last->die_sib)
13801 int quals = 0;
13802 if (last == mod_scope->die_child)
13803 break;
13804 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13805 != first)
13806 break;
13810 for (i = 0; i < dwarf_qual_info_size; i++)
13811 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13813 dw_die_ref d;
13814 if (first && first != last)
13816 for (d = first->die_sib; ; d = d->die_sib)
13818 int quals = 0;
13819 qualified_die_p (d, &quals, dwarf_qual_info_size);
13820 if (quals == (first_quals | dwarf_qual_info[i].q))
13821 break;
13822 if (d == last)
13824 d = NULL;
13825 break;
13828 if (d)
13830 mod_type_die = d;
13831 continue;
13834 if (first)
13836 d = new_die_raw (dwarf_qual_info[i].t);
13837 add_child_die_after (mod_scope, d, last);
13838 last = d;
13840 else
13841 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13842 if (mod_type_die)
13843 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13844 mod_type_die = d;
13845 first_quals |= dwarf_qual_info[i].q;
13848 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13850 dwarf_tag tag = DW_TAG_pointer_type;
13851 if (code == REFERENCE_TYPE)
13853 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13854 tag = DW_TAG_rvalue_reference_type;
13855 else
13856 tag = DW_TAG_reference_type;
13858 mod_type_die = new_die (tag, mod_scope, type);
13860 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13861 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13862 add_alignment_attribute (mod_type_die, type);
13863 item_type = TREE_TYPE (type);
13865 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13866 if (!ADDR_SPACE_GENERIC_P (as))
13868 int action = targetm.addr_space.debug (as);
13869 if (action >= 0)
13871 /* Positive values indicate an address_class. */
13872 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13874 else
13876 /* Negative values indicate an (inverted) segment base reg. */
13877 dw_loc_descr_ref d
13878 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13879 add_AT_loc (mod_type_die, DW_AT_segment, d);
13883 else if (code == ARRAY_TYPE
13884 || (lang_hooks.types.get_array_descr_info
13885 && lang_hooks.types.get_array_descr_info (type, &info)))
13887 gen_type_die (type, context_die);
13888 return lookup_type_die (type);
13890 else if (code == INTEGER_TYPE
13891 && TREE_TYPE (type) != NULL_TREE
13892 && subrange_type_for_debug_p (type, &low, &high))
13894 tree bias = NULL_TREE;
13895 if (lang_hooks.types.get_type_bias)
13896 bias = lang_hooks.types.get_type_bias (type);
13897 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13898 item_type = TREE_TYPE (type);
13900 else if (is_base_type (type))
13902 /* If a target supports long double as different floating point
13903 modes with the same 16-byte size, use normal DW_TAG_base_type
13904 only for the composite (ibm_extended_real_format) type and
13905 for the other for the time being emit instead a "_Float128"
13906 or "complex _Float128" DW_TAG_base_type and a "long double"
13907 or "complex long double" typedef to it. */
13908 if (tree other_type = long_double_as_float128 (type))
13910 dw_die_ref other_die;
13911 if (TYPE_NAME (other_type))
13912 other_die
13913 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13914 context_die);
13915 else
13917 other_die = base_type_die (type, reverse);
13918 add_child_die (comp_unit_die (), other_die);
13919 add_name_attribute (other_die,
13920 TREE_CODE (type) == COMPLEX_TYPE
13921 ? "complex _Float128" : "_Float128");
13923 mod_type_die = new_die_raw (DW_TAG_typedef);
13924 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13926 else
13927 mod_type_die = base_type_die (type, reverse);
13929 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13930 if (reverse_base_type)
13932 dw_die_ref after_die
13933 = modified_type_die (type, cv_quals, false, context_die);
13934 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13936 else
13937 add_child_die (comp_unit_die (), mod_type_die);
13939 add_pubtype (type, mod_type_die);
13941 else
13943 gen_type_die (type, context_die);
13945 /* We have to get the type_main_variant here (and pass that to the
13946 `lookup_type_die' routine) because the ..._TYPE node we have
13947 might simply be a *copy* of some original type node (where the
13948 copy was created to help us keep track of typedef names) and
13949 that copy might have a different TYPE_UID from the original
13950 ..._TYPE node. */
13951 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13953 /* For function/method types, can't just use type_main_variant here,
13954 because that can have different ref-qualifiers for C++,
13955 but try to canonicalize. */
13956 tree main = TYPE_MAIN_VARIANT (type);
13957 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13958 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13959 && check_base_type (t, main)
13960 && check_lang_type (t, type))
13961 return lookup_type_die (t);
13962 return lookup_type_die (type);
13964 /* Vectors have the debugging information in the type,
13965 not the main variant. */
13966 else if (code == VECTOR_TYPE)
13967 return lookup_type_die (type);
13968 else
13969 return lookup_type_die (type_main_variant (type));
13972 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13973 don't output a DW_TAG_typedef, since there isn't one in the
13974 user's program; just attach a DW_AT_name to the type.
13975 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13976 if the base type already has the same name. */
13977 if (name
13978 && ((TREE_CODE (name) != TYPE_DECL
13979 && (qualified_type == TYPE_MAIN_VARIANT (type)
13980 || (cv_quals == TYPE_UNQUALIFIED)))
13981 || (TREE_CODE (name) == TYPE_DECL
13982 && TREE_TYPE (name) == qualified_type
13983 && DECL_NAME (name))))
13985 if (TREE_CODE (name) == TYPE_DECL)
13986 /* Could just call add_name_and_src_coords_attributes here,
13987 but since this is a builtin type it doesn't have any
13988 useful source coordinates anyway. */
13989 name = DECL_NAME (name);
13990 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13992 /* This probably indicates a bug. */
13993 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13995 name = TYPE_IDENTIFIER (type);
13996 add_name_attribute (mod_type_die,
13997 name ? IDENTIFIER_POINTER (name) : "__unknown__");
14000 if (qualified_type && !reverse_base_type)
14001 equate_type_number_to_die (qualified_type, mod_type_die);
14003 if (item_type)
14004 /* We must do this after the equate_type_number_to_die call, in case
14005 this is a recursive type. This ensures that the modified_type_die
14006 recursion will terminate even if the type is recursive. Recursive
14007 types are possible in Ada. */
14008 sub_die = modified_type_die (item_type,
14009 TYPE_QUALS_NO_ADDR_SPACE (item_type),
14010 reverse,
14011 context_die);
14013 if (sub_die != NULL)
14014 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14016 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14017 if (TYPE_ARTIFICIAL (type))
14018 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14020 return mod_type_die;
14023 /* Generate DIEs for the generic parameters of T.
14024 T must be either a generic type or a generic function.
14025 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14027 static void
14028 gen_generic_params_dies (tree t)
14030 tree parms, args;
14031 int parms_num, i;
14032 dw_die_ref die = NULL;
14033 int non_default;
14035 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14036 return;
14038 if (TYPE_P (t))
14039 die = lookup_type_die (t);
14040 else if (DECL_P (t))
14041 die = lookup_decl_die (t);
14043 gcc_assert (die);
14045 parms = lang_hooks.get_innermost_generic_parms (t);
14046 if (!parms)
14047 /* T has no generic parameter. It means T is neither a generic type
14048 or function. End of story. */
14049 return;
14051 parms_num = TREE_VEC_LENGTH (parms);
14052 args = lang_hooks.get_innermost_generic_args (t);
14053 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14054 non_default = int_cst_value (TREE_CHAIN (args));
14055 else
14056 non_default = TREE_VEC_LENGTH (args);
14057 for (i = 0; i < parms_num; i++)
14059 tree parm, arg, arg_pack_elems;
14060 dw_die_ref parm_die;
14062 parm = TREE_VEC_ELT (parms, i);
14063 arg = TREE_VEC_ELT (args, i);
14064 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14065 gcc_assert (parm && TREE_VALUE (parm) && arg);
14067 if (parm && TREE_VALUE (parm) && arg)
14069 /* If PARM represents a template parameter pack,
14070 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14071 by DW_TAG_template_*_parameter DIEs for the argument
14072 pack elements of ARG. Note that ARG would then be
14073 an argument pack. */
14074 if (arg_pack_elems)
14075 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14076 arg_pack_elems,
14077 die);
14078 else
14079 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14080 true /* emit name */, die);
14081 if (i >= non_default)
14082 add_AT_flag (parm_die, DW_AT_default_value, 1);
14087 /* Create and return a DIE for PARM which should be
14088 the representation of a generic type parameter.
14089 For instance, in the C++ front end, PARM would be a template parameter.
14090 ARG is the argument to PARM.
14091 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14092 name of the PARM.
14093 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14094 as a child node. */
14096 static dw_die_ref
14097 generic_parameter_die (tree parm, tree arg,
14098 bool emit_name_p,
14099 dw_die_ref parent_die)
14101 dw_die_ref tmpl_die = NULL;
14102 const char *name = NULL;
14104 /* C++20 accepts class literals as template parameters, and var
14105 decls with initializers represent them. The VAR_DECLs would be
14106 rejected, but we can take the DECL_INITIAL constructor and
14107 attempt to expand it. */
14108 if (arg && VAR_P (arg))
14109 arg = DECL_INITIAL (arg);
14111 if (!parm || !DECL_NAME (parm) || !arg)
14112 return NULL;
14114 /* We support non-type generic parameters and arguments,
14115 type generic parameters and arguments, as well as
14116 generic generic parameters (a.k.a. template template parameters in C++)
14117 and arguments. */
14118 if (TREE_CODE (parm) == PARM_DECL)
14119 /* PARM is a nontype generic parameter */
14120 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14121 else if (TREE_CODE (parm) == TYPE_DECL)
14122 /* PARM is a type generic parameter. */
14123 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14124 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14125 /* PARM is a generic generic parameter.
14126 Its DIE is a GNU extension. It shall have a
14127 DW_AT_name attribute to represent the name of the template template
14128 parameter, and a DW_AT_GNU_template_name attribute to represent the
14129 name of the template template argument. */
14130 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14131 parent_die, parm);
14132 else
14133 gcc_unreachable ();
14135 if (tmpl_die)
14137 tree tmpl_type;
14139 /* If PARM is a generic parameter pack, it means we are
14140 emitting debug info for a template argument pack element.
14141 In other terms, ARG is a template argument pack element.
14142 In that case, we don't emit any DW_AT_name attribute for
14143 the die. */
14144 if (emit_name_p)
14146 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14147 gcc_assert (name);
14148 add_AT_string (tmpl_die, DW_AT_name, name);
14151 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14153 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14154 TMPL_DIE should have a child DW_AT_type attribute that is set
14155 to the type of the argument to PARM, which is ARG.
14156 If PARM is a type generic parameter, TMPL_DIE should have a
14157 child DW_AT_type that is set to ARG. */
14158 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14159 add_type_attribute (tmpl_die, tmpl_type,
14160 (TREE_THIS_VOLATILE (tmpl_type)
14161 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14162 false, parent_die);
14164 else
14166 /* So TMPL_DIE is a DIE representing a
14167 a generic generic template parameter, a.k.a template template
14168 parameter in C++ and arg is a template. */
14170 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14171 to the name of the argument. */
14172 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14173 if (name)
14174 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14177 if (TREE_CODE (parm) == PARM_DECL)
14178 /* So PARM is a non-type generic parameter.
14179 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14180 attribute of TMPL_DIE which value represents the value
14181 of ARG.
14182 We must be careful here:
14183 The value of ARG might reference some function decls.
14184 We might currently be emitting debug info for a generic
14185 type and types are emitted before function decls, we don't
14186 know if the function decls referenced by ARG will actually be
14187 emitted after cgraph computations.
14188 So must defer the generation of the DW_AT_const_value to
14189 after cgraph is ready. */
14190 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14193 return tmpl_die;
14196 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14197 PARM_PACK must be a template parameter pack. The returned DIE
14198 will be child DIE of PARENT_DIE. */
14200 static dw_die_ref
14201 template_parameter_pack_die (tree parm_pack,
14202 tree parm_pack_args,
14203 dw_die_ref parent_die)
14205 dw_die_ref die;
14206 int j;
14208 gcc_assert (parent_die && parm_pack);
14210 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14211 add_name_and_src_coords_attributes (die, parm_pack);
14212 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14213 generic_parameter_die (parm_pack,
14214 TREE_VEC_ELT (parm_pack_args, j),
14215 false /* Don't emit DW_AT_name */,
14216 die);
14217 return die;
14220 /* Return the debugger register number described by a given RTL node. */
14222 static unsigned int
14223 debugger_reg_number (const_rtx rtl)
14225 unsigned regno = REGNO (rtl);
14227 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14229 #ifdef LEAF_REG_REMAP
14230 if (crtl->uses_only_leaf_regs)
14232 int leaf_reg = LEAF_REG_REMAP (regno);
14233 if (leaf_reg != -1)
14234 regno = (unsigned) leaf_reg;
14236 #endif
14238 regno = DEBUGGER_REGNO (regno);
14239 gcc_assert (regno != INVALID_REGNUM);
14240 return regno;
14243 /* Optionally add a DW_OP_piece term to a location description expression.
14244 DW_OP_piece is only added if the location description expression already
14245 doesn't end with DW_OP_piece. */
14247 static void
14248 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14250 dw_loc_descr_ref loc;
14252 if (*list_head != NULL)
14254 /* Find the end of the chain. */
14255 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14258 if (loc->dw_loc_opc != DW_OP_piece)
14259 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14263 /* Return a location descriptor that designates a machine register or
14264 zero if there is none. */
14266 static dw_loc_descr_ref
14267 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14269 rtx regs;
14271 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14272 return 0;
14274 /* We only use "frame base" when we're sure we're talking about the
14275 post-prologue local stack frame. We do this by *not* running
14276 register elimination until this point, and recognizing the special
14277 argument pointer and soft frame pointer rtx's.
14278 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14279 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14280 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14282 dw_loc_descr_ref result = NULL;
14284 if (dwarf_version >= 4 || !dwarf_strict)
14286 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14287 initialized);
14288 if (result)
14289 add_loc_descr (&result,
14290 new_loc_descr (DW_OP_stack_value, 0, 0));
14292 return result;
14295 regs = targetm.dwarf_register_span (rtl);
14297 if (REG_NREGS (rtl) > 1 || regs)
14298 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14299 else
14301 unsigned int debugger_regnum = debugger_reg_number (rtl);
14302 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14303 return 0;
14304 return one_reg_loc_descriptor (debugger_regnum, initialized);
14308 /* Return a location descriptor that designates a machine register for
14309 a given hard register number. */
14311 static dw_loc_descr_ref
14312 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14314 dw_loc_descr_ref reg_loc_descr;
14316 if (regno <= 31)
14317 reg_loc_descr
14318 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14319 else
14320 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14322 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14323 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14325 return reg_loc_descr;
14328 /* Given an RTL of a register, return a location descriptor that
14329 designates a value that spans more than one register. */
14331 static dw_loc_descr_ref
14332 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14333 enum var_init_status initialized)
14335 int size, i;
14336 dw_loc_descr_ref loc_result = NULL;
14338 /* Simple, contiguous registers. */
14339 if (regs == NULL_RTX)
14341 unsigned reg = REGNO (rtl);
14342 int nregs;
14344 #ifdef LEAF_REG_REMAP
14345 if (crtl->uses_only_leaf_regs)
14347 int leaf_reg = LEAF_REG_REMAP (reg);
14348 if (leaf_reg != -1)
14349 reg = (unsigned) leaf_reg;
14351 #endif
14353 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14354 nregs = REG_NREGS (rtl);
14356 /* At present we only track constant-sized pieces. */
14357 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14358 return NULL;
14359 size /= nregs;
14361 loc_result = NULL;
14362 while (nregs--)
14364 dw_loc_descr_ref t;
14366 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14367 VAR_INIT_STATUS_INITIALIZED);
14368 add_loc_descr (&loc_result, t);
14369 add_loc_descr_op_piece (&loc_result, size);
14370 ++reg;
14372 return loc_result;
14375 /* Now onto stupid register sets in non contiguous locations. */
14377 gcc_assert (GET_CODE (regs) == PARALLEL);
14379 /* At present we only track constant-sized pieces. */
14380 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14381 return NULL;
14382 loc_result = NULL;
14384 for (i = 0; i < XVECLEN (regs, 0); ++i)
14386 dw_loc_descr_ref t;
14388 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14389 VAR_INIT_STATUS_INITIALIZED);
14390 add_loc_descr (&loc_result, t);
14391 add_loc_descr_op_piece (&loc_result, size);
14394 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14395 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14396 return loc_result;
14399 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14401 /* Return a location descriptor that designates a constant i,
14402 as a compound operation from constant (i >> shift), constant shift
14403 and DW_OP_shl. */
14405 static dw_loc_descr_ref
14406 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14408 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14409 add_loc_descr (&ret, int_loc_descriptor (shift));
14410 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14411 return ret;
14414 /* Return a location descriptor that designates constant POLY_I. */
14416 static dw_loc_descr_ref
14417 int_loc_descriptor (poly_int64 poly_i)
14419 enum dwarf_location_atom op;
14421 HOST_WIDE_INT i;
14422 if (!poly_i.is_constant (&i))
14424 /* Create location descriptions for the non-constant part and
14425 add any constant offset at the end. */
14426 dw_loc_descr_ref ret = NULL;
14427 HOST_WIDE_INT constant = poly_i.coeffs[0];
14428 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14430 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14431 if (coeff != 0)
14433 dw_loc_descr_ref start = ret;
14434 unsigned int factor;
14435 int bias;
14436 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14437 (j, &factor, &bias);
14439 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14440 add COEFF * (REGNO / FACTOR) now and subtract
14441 COEFF * BIAS from the final constant part. */
14442 constant -= coeff * bias;
14443 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14444 if (coeff % factor == 0)
14445 coeff /= factor;
14446 else
14448 int amount = exact_log2 (factor);
14449 gcc_assert (amount >= 0);
14450 add_loc_descr (&ret, int_loc_descriptor (amount));
14451 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14453 if (coeff != 1)
14455 add_loc_descr (&ret, int_loc_descriptor (coeff));
14456 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14458 if (start)
14459 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14462 loc_descr_plus_const (&ret, constant);
14463 return ret;
14466 /* Pick the smallest representation of a constant, rather than just
14467 defaulting to the LEB encoding. */
14468 if (i >= 0)
14470 int clz = clz_hwi (i);
14471 int ctz = ctz_hwi (i);
14472 if (i <= 31)
14473 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14474 else if (i <= 0xff)
14475 op = DW_OP_const1u;
14476 else if (i <= 0xffff)
14477 op = DW_OP_const2u;
14478 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14479 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14480 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14481 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14482 while DW_OP_const4u is 5 bytes. */
14483 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14484 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14485 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14486 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14487 while DW_OP_const4u is 5 bytes. */
14488 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14490 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14491 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14492 <= 4)
14494 /* As i >= 2**31, the double cast above will yield a negative number.
14495 Since wrapping is defined in DWARF expressions we can output big
14496 positive integers as small negative ones, regardless of the size
14497 of host wide ints.
14499 Here, since the evaluator will handle 32-bit values and since i >=
14500 2**31, we know it's going to be interpreted as a negative literal:
14501 store it this way if we can do better than 5 bytes this way. */
14502 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14504 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14505 op = DW_OP_const4u;
14507 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14508 least 6 bytes: see if we can do better before falling back to it. */
14509 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14510 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14511 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14512 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14513 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14514 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14515 >= HOST_BITS_PER_WIDE_INT)
14516 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14517 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14518 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14519 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14520 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14521 && size_of_uleb128 (i) > 6)
14522 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14523 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14524 else
14525 op = DW_OP_constu;
14527 else
14529 if (i >= -0x80)
14530 op = DW_OP_const1s;
14531 else if (i >= -0x8000)
14532 op = DW_OP_const2s;
14533 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14535 if (size_of_int_loc_descriptor (i) < 5)
14537 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14538 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14539 return ret;
14541 op = DW_OP_const4s;
14543 else
14545 if (size_of_int_loc_descriptor (i)
14546 < (unsigned long) 1 + size_of_sleb128 (i))
14548 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14549 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14550 return ret;
14552 op = DW_OP_consts;
14556 return new_loc_descr (op, i, 0);
14559 /* Likewise, for unsigned constants. */
14561 static dw_loc_descr_ref
14562 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14564 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14565 const unsigned HOST_WIDE_INT max_uint
14566 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14568 /* If possible, use the clever signed constants handling. */
14569 if (i <= max_int)
14570 return int_loc_descriptor ((HOST_WIDE_INT) i);
14572 /* Here, we are left with positive numbers that cannot be represented as
14573 HOST_WIDE_INT, i.e.:
14574 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14576 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14577 whereas may be better to output a negative integer: thanks to integer
14578 wrapping, we know that:
14579 x = x - 2 ** DWARF2_ADDR_SIZE
14580 = x - 2 * (max (HOST_WIDE_INT) + 1)
14581 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14582 small negative integers. Let's try that in cases it will clearly improve
14583 the encoding: there is no gain turning DW_OP_const4u into
14584 DW_OP_const4s. */
14585 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14586 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14587 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14589 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14591 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14592 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14593 const HOST_WIDE_INT second_shift
14594 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14596 /* So we finally have:
14597 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14598 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14599 return int_loc_descriptor (second_shift);
14602 /* Last chance: fallback to a simple constant operation. */
14603 return new_loc_descr
14604 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14605 ? DW_OP_const4u
14606 : DW_OP_const8u,
14607 i, 0);
14610 /* Generate and return a location description that computes the unsigned
14611 comparison of the two stack top entries (a OP b where b is the top-most
14612 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14613 LE_EXPR, GT_EXPR or GE_EXPR. */
14615 static dw_loc_descr_ref
14616 uint_comparison_loc_list (enum tree_code kind)
14618 enum dwarf_location_atom op, flip_op;
14619 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14621 switch (kind)
14623 case LT_EXPR:
14624 op = DW_OP_lt;
14625 break;
14626 case LE_EXPR:
14627 op = DW_OP_le;
14628 break;
14629 case GT_EXPR:
14630 op = DW_OP_gt;
14631 break;
14632 case GE_EXPR:
14633 op = DW_OP_ge;
14634 break;
14635 default:
14636 gcc_unreachable ();
14639 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14640 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14642 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14643 possible to perform unsigned comparisons: we just have to distinguish
14644 three cases:
14646 1. when a and b have the same sign (as signed integers); then we should
14647 return: a OP(signed) b;
14649 2. when a is a negative signed integer while b is a positive one, then a
14650 is a greater unsigned integer than b; likewise when a and b's roles
14651 are flipped.
14653 So first, compare the sign of the two operands. */
14654 ret = new_loc_descr (DW_OP_over, 0, 0);
14655 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14656 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14657 /* If they have different signs (i.e. they have different sign bits), then
14658 the stack top value has now the sign bit set and thus it's smaller than
14659 zero. */
14660 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14661 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14662 add_loc_descr (&ret, bra_node);
14664 /* We are in case 1. At this point, we know both operands have the same
14665 sign, to it's safe to use the built-in signed comparison. */
14666 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14667 add_loc_descr (&ret, jmp_node);
14669 /* We are in case 2. Here, we know both operands do not have the same sign,
14670 so we have to flip the signed comparison. */
14671 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14672 tmp = new_loc_descr (flip_op, 0, 0);
14673 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14674 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14675 add_loc_descr (&ret, tmp);
14677 /* This dummy operation is necessary to make the two branches join. */
14678 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14679 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14680 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14681 add_loc_descr (&ret, tmp);
14683 return ret;
14686 /* Likewise, but takes the location description lists (might be destructive on
14687 them). Return NULL if either is NULL or if concatenation fails. */
14689 static dw_loc_list_ref
14690 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14691 enum tree_code kind)
14693 if (left == NULL || right == NULL)
14694 return NULL;
14696 add_loc_list (&left, right);
14697 if (left == NULL)
14698 return NULL;
14700 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14701 return left;
14704 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14705 without actually allocating it. */
14707 static unsigned long
14708 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14710 return size_of_int_loc_descriptor (i >> shift)
14711 + size_of_int_loc_descriptor (shift)
14712 + 1;
14715 /* Return size_of_locs (int_loc_descriptor (i)) without
14716 actually allocating it. */
14718 static unsigned long
14719 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14721 unsigned long s;
14723 if (i >= 0)
14725 int clz, ctz;
14726 if (i <= 31)
14727 return 1;
14728 else if (i <= 0xff)
14729 return 2;
14730 else if (i <= 0xffff)
14731 return 3;
14732 clz = clz_hwi (i);
14733 ctz = ctz_hwi (i);
14734 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14735 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14736 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14737 - clz - 5);
14738 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14739 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14740 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14741 - clz - 8);
14742 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14743 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14744 <= 4)
14745 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14746 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14747 return 5;
14748 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14749 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14750 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14751 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14752 - clz - 8);
14753 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14754 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14755 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14756 - clz - 16);
14757 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14758 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14759 && s > 6)
14760 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14761 - clz - 32);
14762 else
14763 return 1 + s;
14765 else
14767 if (i >= -0x80)
14768 return 2;
14769 else if (i >= -0x8000)
14770 return 3;
14771 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14773 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14775 s = size_of_int_loc_descriptor (-i) + 1;
14776 if (s < 5)
14777 return s;
14779 return 5;
14781 else
14783 unsigned long r = 1 + size_of_sleb128 (i);
14784 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14786 s = size_of_int_loc_descriptor (-i) + 1;
14787 if (s < r)
14788 return s;
14790 return r;
14795 /* Return loc description representing "address" of integer value.
14796 This can appear only as toplevel expression. */
14798 static dw_loc_descr_ref
14799 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14801 int litsize;
14802 dw_loc_descr_ref loc_result = NULL;
14804 if (!(dwarf_version >= 4 || !dwarf_strict))
14805 return NULL;
14807 litsize = size_of_int_loc_descriptor (i);
14808 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14809 is more compact. For DW_OP_stack_value we need:
14810 litsize + 1 (DW_OP_stack_value)
14811 and for DW_OP_implicit_value:
14812 1 (DW_OP_implicit_value) + 1 (length) + size. */
14813 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14815 loc_result = int_loc_descriptor (i);
14816 add_loc_descr (&loc_result,
14817 new_loc_descr (DW_OP_stack_value, 0, 0));
14818 return loc_result;
14821 loc_result = new_loc_descr (DW_OP_implicit_value,
14822 size, 0);
14823 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14824 loc_result->dw_loc_oprnd2.v.val_int = i;
14825 return loc_result;
14828 /* Return a location descriptor that designates a base+offset location. */
14830 static dw_loc_descr_ref
14831 based_loc_descr (rtx reg, poly_int64 offset,
14832 enum var_init_status initialized)
14834 unsigned int regno;
14835 dw_loc_descr_ref result;
14836 dw_fde_ref fde = cfun->fde;
14838 /* We only use "frame base" when we're sure we're talking about the
14839 post-prologue local stack frame. We do this by *not* running
14840 register elimination until this point, and recognizing the special
14841 argument pointer and soft frame pointer rtx's. */
14842 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14844 rtx elim = (ira_use_lra_p
14845 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14846 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14848 if (elim != reg)
14850 /* Allow hard frame pointer here even if frame pointer
14851 isn't used since hard frame pointer is encoded with
14852 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14853 not hard frame pointer directly. */
14854 elim = strip_offset_and_add (elim, &offset);
14855 gcc_assert (elim == hard_frame_pointer_rtx
14856 || elim == stack_pointer_rtx);
14858 /* If drap register is used to align stack, use frame
14859 pointer + offset to access stack variables. If stack
14860 is aligned without drap, use stack pointer + offset to
14861 access stack variables. */
14862 if (crtl->stack_realign_tried
14863 && reg == frame_pointer_rtx)
14865 int base_reg
14866 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14867 ? HARD_FRAME_POINTER_REGNUM
14868 : REGNO (elim));
14869 return new_reg_loc_descr (base_reg, offset);
14872 gcc_assert (frame_pointer_fb_offset_valid);
14873 offset += frame_pointer_fb_offset;
14874 HOST_WIDE_INT const_offset;
14875 if (offset.is_constant (&const_offset))
14876 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14877 else
14879 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14880 loc_descr_plus_const (&ret, offset);
14881 return ret;
14886 regno = REGNO (reg);
14887 #ifdef LEAF_REG_REMAP
14888 if (crtl->uses_only_leaf_regs)
14890 int leaf_reg = LEAF_REG_REMAP (regno);
14891 if (leaf_reg != -1)
14892 regno = (unsigned) leaf_reg;
14894 #endif
14895 regno = DWARF_FRAME_REGNUM (regno);
14897 HOST_WIDE_INT const_offset;
14898 if (!optimize && fde
14899 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14900 && offset.is_constant (&const_offset))
14902 /* Use cfa+offset to represent the location of arguments passed
14903 on the stack when drap is used to align stack.
14904 Only do this when not optimizing, for optimized code var-tracking
14905 is supposed to track where the arguments live and the register
14906 used as vdrap or drap in some spot might be used for something
14907 else in other part of the routine. */
14908 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14911 result = new_reg_loc_descr (regno, offset);
14913 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14914 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14916 return result;
14919 /* Return true if this RTL expression describes a base+offset calculation. */
14921 static inline int
14922 is_based_loc (const_rtx rtl)
14924 return (GET_CODE (rtl) == PLUS
14925 && ((REG_P (XEXP (rtl, 0))
14926 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14927 && CONST_INT_P (XEXP (rtl, 1)))));
14930 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14931 failed. */
14933 static dw_loc_descr_ref
14934 tls_mem_loc_descriptor (rtx mem)
14936 tree base;
14937 dw_loc_descr_ref loc_result;
14939 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14940 return NULL;
14942 base = get_base_address (MEM_EXPR (mem));
14943 if (base == NULL
14944 || !VAR_P (base)
14945 || !DECL_THREAD_LOCAL_P (base))
14946 return NULL;
14948 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14949 if (loc_result == NULL)
14950 return NULL;
14952 if (maybe_ne (MEM_OFFSET (mem), 0))
14953 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14955 return loc_result;
14958 /* Output debug info about reason why we failed to expand expression as dwarf
14959 expression. */
14961 static void
14962 expansion_failed (tree expr, rtx rtl, char const *reason)
14964 if (dump_file && (dump_flags & TDF_DETAILS))
14966 fprintf (dump_file, "Failed to expand as dwarf: ");
14967 if (expr)
14968 print_generic_expr (dump_file, expr, dump_flags);
14969 if (rtl)
14971 fprintf (dump_file, "\n");
14972 print_rtl (dump_file, rtl);
14974 fprintf (dump_file, "\nReason: %s\n", reason);
14978 /* Helper function for const_ok_for_output. */
14980 static bool
14981 const_ok_for_output_1 (rtx rtl)
14983 if (targetm.const_not_ok_for_debug_p (rtl))
14985 if (GET_CODE (rtl) != UNSPEC)
14987 expansion_failed (NULL_TREE, rtl,
14988 "Expression rejected for debug by the backend.\n");
14989 return false;
14992 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14993 the target hook doesn't explicitly allow it in debug info, assume
14994 we can't express it in the debug info. */
14995 /* Don't complain about TLS UNSPECs, those are just too hard to
14996 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14997 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14998 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14999 if (flag_checking
15000 && (XVECLEN (rtl, 0) == 0
15001 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
15002 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
15003 inform (current_function_decl
15004 ? DECL_SOURCE_LOCATION (current_function_decl)
15005 : UNKNOWN_LOCATION,
15006 #if NUM_UNSPEC_VALUES > 0
15007 "non-delegitimized UNSPEC %s (%d) found in variable location",
15008 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
15009 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
15010 #else
15011 "non-delegitimized UNSPEC %d found in variable location",
15012 #endif
15013 XINT (rtl, 1));
15014 expansion_failed (NULL_TREE, rtl,
15015 "UNSPEC hasn't been delegitimized.\n");
15016 return false;
15019 if (CONST_POLY_INT_P (rtl))
15020 return false;
15022 /* FIXME: Refer to PR60655. It is possible for simplification
15023 of rtl expressions in var tracking to produce such expressions.
15024 We should really identify / validate expressions
15025 enclosed in CONST that can be handled by assemblers on various
15026 targets and only handle legitimate cases here. */
15027 switch (GET_CODE (rtl))
15029 case SYMBOL_REF:
15030 break;
15031 case NOT:
15032 case NEG:
15033 return false;
15034 case PLUS:
15036 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15037 operands. */
15038 subrtx_var_iterator::array_type array;
15039 bool first = false;
15040 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15041 if (SYMBOL_REF_P (*iter)
15042 || LABEL_P (*iter)
15043 || GET_CODE (*iter) == UNSPEC)
15045 first = true;
15046 break;
15048 if (!first)
15049 return true;
15050 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15051 if (SYMBOL_REF_P (*iter)
15052 || LABEL_P (*iter)
15053 || GET_CODE (*iter) == UNSPEC)
15054 return false;
15055 return true;
15057 case MINUS:
15059 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15060 appear in the second operand of MINUS. */
15061 subrtx_var_iterator::array_type array;
15062 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15063 if (SYMBOL_REF_P (*iter)
15064 || LABEL_P (*iter)
15065 || GET_CODE (*iter) == UNSPEC)
15066 return false;
15067 return true;
15069 default:
15070 return true;
15073 if (CONSTANT_POOL_ADDRESS_P (rtl))
15075 bool marked;
15076 get_pool_constant_mark (rtl, &marked);
15077 /* If all references to this pool constant were optimized away,
15078 it was not output and thus we can't represent it. */
15079 if (!marked)
15081 expansion_failed (NULL_TREE, rtl,
15082 "Constant was removed from constant pool.\n");
15083 return false;
15087 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15088 return false;
15090 /* Avoid references to external symbols in debug info, on several targets
15091 the linker might even refuse to link when linking a shared library,
15092 and in many other cases the relocations for .debug_info/.debug_loc are
15093 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15094 to be defined within the same shared library or executable are fine. */
15095 if (SYMBOL_REF_EXTERNAL_P (rtl))
15097 tree decl = SYMBOL_REF_DECL (rtl);
15099 if (decl == NULL || !targetm.binds_local_p (decl))
15101 expansion_failed (NULL_TREE, rtl,
15102 "Symbol not defined in current TU.\n");
15103 return false;
15107 return true;
15110 /* Return true if constant RTL can be emitted in DW_OP_addr or
15111 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15112 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15114 static bool
15115 const_ok_for_output (rtx rtl)
15117 if (GET_CODE (rtl) == SYMBOL_REF)
15118 return const_ok_for_output_1 (rtl);
15120 if (GET_CODE (rtl) == CONST)
15122 subrtx_var_iterator::array_type array;
15123 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15124 if (!const_ok_for_output_1 (*iter))
15125 return false;
15126 return true;
15129 return true;
15132 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15133 if possible, NULL otherwise. */
15135 static dw_die_ref
15136 base_type_for_mode (machine_mode mode, bool unsignedp)
15138 dw_die_ref type_die;
15139 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15141 if (type == NULL)
15142 return NULL;
15143 switch (TREE_CODE (type))
15145 case INTEGER_TYPE:
15146 case REAL_TYPE:
15147 break;
15148 default:
15149 return NULL;
15151 type_die = lookup_type_die (type);
15152 if (!type_die)
15153 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15154 comp_unit_die ());
15155 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15156 return NULL;
15157 return type_die;
15160 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15161 type matching MODE, or, if MODE is narrower than or as wide as
15162 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15163 possible. */
15165 static dw_loc_descr_ref
15166 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15168 machine_mode outer_mode = mode;
15169 dw_die_ref type_die;
15170 dw_loc_descr_ref cvt;
15172 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15174 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15175 return op;
15177 type_die = base_type_for_mode (outer_mode, 1);
15178 if (type_die == NULL)
15179 return NULL;
15180 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15181 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15182 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15183 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15184 add_loc_descr (&op, cvt);
15185 return op;
15188 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15190 static dw_loc_descr_ref
15191 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15192 dw_loc_descr_ref op1)
15194 dw_loc_descr_ref ret = op0;
15195 add_loc_descr (&ret, op1);
15196 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15197 if (STORE_FLAG_VALUE != 1)
15199 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15200 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15202 return ret;
15205 /* Subroutine of scompare_loc_descriptor for the case in which we're
15206 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15207 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15209 static dw_loc_descr_ref
15210 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15211 scalar_int_mode op_mode,
15212 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15214 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15215 dw_loc_descr_ref cvt;
15217 if (type_die == NULL)
15218 return NULL;
15219 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15220 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15221 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15222 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15223 add_loc_descr (&op0, cvt);
15224 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15225 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15226 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15227 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15228 add_loc_descr (&op1, cvt);
15229 return compare_loc_descriptor (op, op0, op1);
15232 /* Subroutine of scompare_loc_descriptor for the case in which we're
15233 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15234 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15236 static dw_loc_descr_ref
15237 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15238 scalar_int_mode op_mode,
15239 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15241 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15242 /* For eq/ne, if the operands are known to be zero-extended,
15243 there is no need to do the fancy shifting up. */
15244 if (op == DW_OP_eq || op == DW_OP_ne)
15246 dw_loc_descr_ref last0, last1;
15247 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15249 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15251 /* deref_size zero extends, and for constants we can check
15252 whether they are zero extended or not. */
15253 if (((last0->dw_loc_opc == DW_OP_deref_size
15254 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15255 || (CONST_INT_P (XEXP (rtl, 0))
15256 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15257 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15258 && ((last1->dw_loc_opc == DW_OP_deref_size
15259 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15260 || (CONST_INT_P (XEXP (rtl, 1))
15261 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15262 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15263 return compare_loc_descriptor (op, op0, op1);
15265 /* EQ/NE comparison against constant in narrower type than
15266 DWARF2_ADDR_SIZE can be performed either as
15267 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15268 DW_OP_{eq,ne}
15270 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15271 DW_OP_{eq,ne}. Pick whatever is shorter. */
15272 if (CONST_INT_P (XEXP (rtl, 1))
15273 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15274 && (size_of_int_loc_descriptor (shift) + 1
15275 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15276 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15277 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15278 & GET_MODE_MASK (op_mode))))
15280 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15281 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15282 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15283 & GET_MODE_MASK (op_mode));
15284 return compare_loc_descriptor (op, op0, op1);
15287 add_loc_descr (&op0, int_loc_descriptor (shift));
15288 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15289 if (CONST_INT_P (XEXP (rtl, 1)))
15290 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15291 else
15293 add_loc_descr (&op1, int_loc_descriptor (shift));
15294 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15296 return compare_loc_descriptor (op, op0, op1);
15299 /* Return location descriptor for signed comparison OP RTL. */
15301 static dw_loc_descr_ref
15302 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15303 machine_mode mem_mode)
15305 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15306 dw_loc_descr_ref op0, op1;
15308 if (op_mode == VOIDmode)
15309 op_mode = GET_MODE (XEXP (rtl, 1));
15310 if (op_mode == VOIDmode)
15311 return NULL;
15313 scalar_int_mode int_op_mode;
15314 if (dwarf_strict
15315 && dwarf_version < 5
15316 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15317 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15318 return NULL;
15320 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15321 VAR_INIT_STATUS_INITIALIZED);
15322 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15323 VAR_INIT_STATUS_INITIALIZED);
15325 if (op0 == NULL || op1 == NULL)
15326 return NULL;
15328 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15330 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15331 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15333 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15334 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15336 return compare_loc_descriptor (op, op0, op1);
15339 /* Return location descriptor for unsigned comparison OP RTL. */
15341 static dw_loc_descr_ref
15342 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15343 machine_mode mem_mode)
15345 dw_loc_descr_ref op0, op1;
15347 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15348 if (test_op_mode == VOIDmode)
15349 test_op_mode = GET_MODE (XEXP (rtl, 1));
15351 scalar_int_mode op_mode;
15352 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15353 return NULL;
15355 if (dwarf_strict
15356 && dwarf_version < 5
15357 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15358 return NULL;
15360 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15361 VAR_INIT_STATUS_INITIALIZED);
15362 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15363 VAR_INIT_STATUS_INITIALIZED);
15365 if (op0 == NULL || op1 == NULL)
15366 return NULL;
15368 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15370 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15371 dw_loc_descr_ref last0, last1;
15372 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15374 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15376 if (CONST_INT_P (XEXP (rtl, 0)))
15377 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15378 /* deref_size zero extends, so no need to mask it again. */
15379 else if (last0->dw_loc_opc != DW_OP_deref_size
15380 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15382 add_loc_descr (&op0, int_loc_descriptor (mask));
15383 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15385 if (CONST_INT_P (XEXP (rtl, 1)))
15386 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15387 /* deref_size zero extends, so no need to mask it again. */
15388 else if (last1->dw_loc_opc != DW_OP_deref_size
15389 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15391 add_loc_descr (&op1, int_loc_descriptor (mask));
15392 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15395 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15397 HOST_WIDE_INT bias = 1;
15398 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15399 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15400 if (CONST_INT_P (XEXP (rtl, 1)))
15401 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15402 + INTVAL (XEXP (rtl, 1)));
15403 else
15404 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15405 bias, 0));
15407 return compare_loc_descriptor (op, op0, op1);
15410 /* Return location descriptor for {U,S}{MIN,MAX}. */
15412 static dw_loc_descr_ref
15413 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15414 machine_mode mem_mode)
15416 enum dwarf_location_atom op;
15417 dw_loc_descr_ref op0, op1, ret;
15418 dw_loc_descr_ref bra_node, drop_node;
15420 scalar_int_mode int_mode;
15421 if (dwarf_strict
15422 && dwarf_version < 5
15423 && (!is_a <scalar_int_mode> (mode, &int_mode)
15424 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15425 return NULL;
15427 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15428 VAR_INIT_STATUS_INITIALIZED);
15429 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15430 VAR_INIT_STATUS_INITIALIZED);
15432 if (op0 == NULL || op1 == NULL)
15433 return NULL;
15435 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15436 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15437 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15438 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15440 /* Checked by the caller. */
15441 int_mode = as_a <scalar_int_mode> (mode);
15442 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15444 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15445 add_loc_descr (&op0, int_loc_descriptor (mask));
15446 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15447 add_loc_descr (&op1, int_loc_descriptor (mask));
15448 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15450 else if (GET_MODE_SIZE (int_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 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15458 else if (is_a <scalar_int_mode> (mode, &int_mode)
15459 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15461 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15462 add_loc_descr (&op0, int_loc_descriptor (shift));
15463 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15464 add_loc_descr (&op1, int_loc_descriptor (shift));
15465 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15467 else if (is_a <scalar_int_mode> (mode, &int_mode)
15468 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15470 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15471 dw_loc_descr_ref cvt;
15472 if (type_die == NULL)
15473 return NULL;
15474 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15475 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15476 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15477 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15478 add_loc_descr (&op0, cvt);
15479 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15480 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15481 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15482 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15483 add_loc_descr (&op1, cvt);
15486 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15487 op = DW_OP_lt;
15488 else
15489 op = DW_OP_gt;
15490 ret = op0;
15491 add_loc_descr (&ret, op1);
15492 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15493 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15494 add_loc_descr (&ret, bra_node);
15495 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15496 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15497 add_loc_descr (&ret, drop_node);
15498 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15499 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15500 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15501 && is_a <scalar_int_mode> (mode, &int_mode)
15502 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15503 ret = convert_descriptor_to_mode (int_mode, ret);
15504 return ret;
15507 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15508 but after converting arguments to type_die, afterwards
15509 convert back to unsigned. */
15511 static dw_loc_descr_ref
15512 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15513 scalar_int_mode mode, machine_mode mem_mode)
15515 dw_loc_descr_ref cvt, op0, op1;
15517 if (type_die == NULL)
15518 return NULL;
15519 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15520 VAR_INIT_STATUS_INITIALIZED);
15521 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15522 VAR_INIT_STATUS_INITIALIZED);
15523 if (op0 == NULL || op1 == NULL)
15524 return NULL;
15525 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15526 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15527 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15528 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15529 add_loc_descr (&op0, cvt);
15530 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15531 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15532 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15533 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15534 add_loc_descr (&op1, cvt);
15535 add_loc_descr (&op0, op1);
15536 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15537 return convert_descriptor_to_mode (mode, op0);
15540 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15541 const0 is DW_OP_lit0 or corresponding typed constant,
15542 const1 is DW_OP_lit1 or corresponding typed constant
15543 and constMSB is constant with just the MSB bit set
15544 for the mode):
15545 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15546 L1: const0 DW_OP_swap
15547 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15548 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15549 L3: DW_OP_drop
15550 L4: DW_OP_nop
15552 CTZ is similar:
15553 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15554 L1: const0 DW_OP_swap
15555 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15556 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15557 L3: DW_OP_drop
15558 L4: DW_OP_nop
15560 FFS is similar:
15561 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15562 L1: const1 DW_OP_swap
15563 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15564 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15565 L3: DW_OP_drop
15566 L4: DW_OP_nop */
15568 static dw_loc_descr_ref
15569 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15570 machine_mode mem_mode)
15572 dw_loc_descr_ref op0, ret, tmp;
15573 HOST_WIDE_INT valv;
15574 dw_loc_descr_ref l1jump, l1label;
15575 dw_loc_descr_ref l2jump, l2label;
15576 dw_loc_descr_ref l3jump, l3label;
15577 dw_loc_descr_ref l4jump, l4label;
15578 rtx msb;
15580 if (GET_MODE (XEXP (rtl, 0)) != mode)
15581 return NULL;
15583 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15584 VAR_INIT_STATUS_INITIALIZED);
15585 if (op0 == NULL)
15586 return NULL;
15587 ret = op0;
15588 if (GET_CODE (rtl) == CLZ)
15590 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15591 valv = GET_MODE_BITSIZE (mode);
15593 else if (GET_CODE (rtl) == FFS)
15594 valv = 0;
15595 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15596 valv = GET_MODE_BITSIZE (mode);
15597 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15598 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15599 add_loc_descr (&ret, l1jump);
15600 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15601 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15602 VAR_INIT_STATUS_INITIALIZED);
15603 if (tmp == NULL)
15604 return NULL;
15605 add_loc_descr (&ret, tmp);
15606 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15607 add_loc_descr (&ret, l4jump);
15608 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15609 ? const1_rtx : const0_rtx,
15610 mode, mem_mode,
15611 VAR_INIT_STATUS_INITIALIZED);
15612 if (l1label == NULL)
15613 return NULL;
15614 add_loc_descr (&ret, l1label);
15615 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15616 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15617 add_loc_descr (&ret, l2label);
15618 if (GET_CODE (rtl) != CLZ)
15619 msb = const1_rtx;
15620 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15621 msb = GEN_INT (HOST_WIDE_INT_1U
15622 << (GET_MODE_BITSIZE (mode) - 1));
15623 else
15624 msb = immed_wide_int_const
15625 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15626 GET_MODE_PRECISION (mode)), mode);
15627 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15628 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15629 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15630 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15631 else
15632 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15633 VAR_INIT_STATUS_INITIALIZED);
15634 if (tmp == NULL)
15635 return NULL;
15636 add_loc_descr (&ret, tmp);
15637 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15638 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15639 add_loc_descr (&ret, l3jump);
15640 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15641 VAR_INIT_STATUS_INITIALIZED);
15642 if (tmp == NULL)
15643 return NULL;
15644 add_loc_descr (&ret, tmp);
15645 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15646 ? DW_OP_shl : DW_OP_shr, 0, 0));
15647 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15648 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15649 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15650 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15651 add_loc_descr (&ret, l2jump);
15652 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15653 add_loc_descr (&ret, l3label);
15654 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15655 add_loc_descr (&ret, l4label);
15656 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15657 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15658 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15659 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15660 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15661 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15662 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15663 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15664 return ret;
15667 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15668 const1 is DW_OP_lit1 or corresponding typed constant):
15669 const0 DW_OP_swap
15670 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15671 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15672 L2: DW_OP_drop
15674 PARITY is similar:
15675 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15676 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15677 L2: DW_OP_drop */
15679 static dw_loc_descr_ref
15680 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15681 machine_mode mem_mode)
15683 dw_loc_descr_ref op0, ret, tmp;
15684 dw_loc_descr_ref l1jump, l1label;
15685 dw_loc_descr_ref l2jump, l2label;
15687 if (GET_MODE (XEXP (rtl, 0)) != mode)
15688 return NULL;
15690 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15691 VAR_INIT_STATUS_INITIALIZED);
15692 if (op0 == NULL)
15693 return NULL;
15694 ret = op0;
15695 tmp = mem_loc_descriptor (const0_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 (DW_OP_swap, 0, 0));
15701 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15702 add_loc_descr (&ret, l1label);
15703 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15704 add_loc_descr (&ret, l2jump);
15705 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15706 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15707 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15708 VAR_INIT_STATUS_INITIALIZED);
15709 if (tmp == NULL)
15710 return NULL;
15711 add_loc_descr (&ret, tmp);
15712 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15713 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15714 ? DW_OP_plus : DW_OP_xor, 0, 0));
15715 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15716 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15717 VAR_INIT_STATUS_INITIALIZED);
15718 add_loc_descr (&ret, tmp);
15719 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15720 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15721 add_loc_descr (&ret, l1jump);
15722 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15723 add_loc_descr (&ret, l2label);
15724 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15725 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15726 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15727 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15728 return ret;
15731 /* BSWAP (constS is initial shift count, either 56 or 24):
15732 constS const0
15733 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15734 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15735 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15736 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15737 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15739 static dw_loc_descr_ref
15740 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15741 machine_mode mem_mode)
15743 dw_loc_descr_ref op0, ret, tmp;
15744 dw_loc_descr_ref l1jump, l1label;
15745 dw_loc_descr_ref l2jump, l2label;
15747 if (BITS_PER_UNIT != 8
15748 || (GET_MODE_BITSIZE (mode) != 32
15749 && GET_MODE_BITSIZE (mode) != 64))
15750 return NULL;
15752 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15753 VAR_INIT_STATUS_INITIALIZED);
15754 if (op0 == NULL)
15755 return NULL;
15757 ret = op0;
15758 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15759 mode, mem_mode,
15760 VAR_INIT_STATUS_INITIALIZED);
15761 if (tmp == NULL)
15762 return NULL;
15763 add_loc_descr (&ret, tmp);
15764 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15765 VAR_INIT_STATUS_INITIALIZED);
15766 if (tmp == NULL)
15767 return NULL;
15768 add_loc_descr (&ret, tmp);
15769 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15770 add_loc_descr (&ret, l1label);
15771 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15772 mode, mem_mode,
15773 VAR_INIT_STATUS_INITIALIZED);
15774 add_loc_descr (&ret, tmp);
15775 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15776 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15778 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15779 VAR_INIT_STATUS_INITIALIZED);
15780 if (tmp == NULL)
15781 return NULL;
15782 add_loc_descr (&ret, tmp);
15783 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15784 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15785 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15786 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15787 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15788 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15789 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15790 VAR_INIT_STATUS_INITIALIZED);
15791 add_loc_descr (&ret, tmp);
15792 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15793 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15794 add_loc_descr (&ret, l2jump);
15795 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15796 VAR_INIT_STATUS_INITIALIZED);
15797 add_loc_descr (&ret, tmp);
15798 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15799 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15800 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15801 add_loc_descr (&ret, l1jump);
15802 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15803 add_loc_descr (&ret, l2label);
15804 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15805 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15806 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15807 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15808 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15809 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15810 return ret;
15813 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15814 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15815 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15816 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15818 ROTATERT is similar:
15819 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15820 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15821 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15823 static dw_loc_descr_ref
15824 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15825 machine_mode mem_mode)
15827 rtx rtlop1 = XEXP (rtl, 1);
15828 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15829 int i;
15831 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15832 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15833 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15834 VAR_INIT_STATUS_INITIALIZED);
15835 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15836 VAR_INIT_STATUS_INITIALIZED);
15837 if (op0 == NULL || op1 == NULL)
15838 return NULL;
15839 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15840 for (i = 0; i < 2; i++)
15842 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15843 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15844 mode, mem_mode,
15845 VAR_INIT_STATUS_INITIALIZED);
15846 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15847 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15848 ? DW_OP_const4u
15849 : HOST_BITS_PER_WIDE_INT == 64
15850 ? DW_OP_const8u : DW_OP_constu,
15851 GET_MODE_MASK (mode), 0);
15852 else
15853 mask[i] = NULL;
15854 if (mask[i] == NULL)
15855 return NULL;
15856 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15858 ret = op0;
15859 add_loc_descr (&ret, op1);
15860 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15861 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15862 if (GET_CODE (rtl) == ROTATERT)
15864 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15865 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15866 GET_MODE_BITSIZE (mode), 0));
15868 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15869 if (mask[0] != NULL)
15870 add_loc_descr (&ret, mask[0]);
15871 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15872 if (mask[1] != NULL)
15874 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15875 add_loc_descr (&ret, mask[1]);
15876 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15878 if (GET_CODE (rtl) == ROTATE)
15880 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15881 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15882 GET_MODE_BITSIZE (mode), 0));
15884 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15885 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15886 return ret;
15889 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15890 for DEBUG_PARAMETER_REF RTL. */
15892 static dw_loc_descr_ref
15893 parameter_ref_descriptor (rtx rtl)
15895 dw_loc_descr_ref ret;
15896 dw_die_ref ref;
15898 if (dwarf_strict)
15899 return NULL;
15900 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15901 /* With LTO during LTRANS we get the late DIE that refers to the early
15902 DIE, thus we add another indirection here. This seems to confuse
15903 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15904 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15905 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15906 if (ref)
15908 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15909 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15910 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15912 else
15914 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15915 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15917 return ret;
15920 /* The following routine converts the RTL for a variable or parameter
15921 (resident in memory) into an equivalent Dwarf representation of a
15922 mechanism for getting the address of that same variable onto the top of a
15923 hypothetical "address evaluation" stack.
15925 When creating memory location descriptors, we are effectively transforming
15926 the RTL for a memory-resident object into its Dwarf postfix expression
15927 equivalent. This routine recursively descends an RTL tree, turning
15928 it into Dwarf postfix code as it goes.
15930 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15932 MEM_MODE is the mode of the memory reference, needed to handle some
15933 autoincrement addressing modes.
15935 Return 0 if we can't represent the location. */
15937 dw_loc_descr_ref
15938 mem_loc_descriptor (rtx rtl, machine_mode mode,
15939 machine_mode mem_mode,
15940 enum var_init_status initialized)
15942 dw_loc_descr_ref mem_loc_result = NULL;
15943 enum dwarf_location_atom op;
15944 dw_loc_descr_ref op0, op1;
15945 rtx inner = NULL_RTX;
15946 poly_int64 offset;
15948 if (mode == VOIDmode)
15949 mode = GET_MODE (rtl);
15951 /* Note that for a dynamically sized array, the location we will generate a
15952 description of here will be the lowest numbered location which is
15953 actually within the array. That's *not* necessarily the same as the
15954 zeroth element of the array. */
15956 rtl = targetm.delegitimize_address (rtl);
15958 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15959 return NULL;
15961 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15962 switch (GET_CODE (rtl))
15964 case POST_INC:
15965 case POST_DEC:
15966 case POST_MODIFY:
15967 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15969 case SUBREG:
15970 /* The case of a subreg may arise when we have a local (register)
15971 variable or a formal (register) parameter which doesn't quite fill
15972 up an entire register. For now, just assume that it is
15973 legitimate to make the Dwarf info refer to the whole register which
15974 contains the given subreg. */
15975 if (!subreg_lowpart_p (rtl))
15976 break;
15977 inner = SUBREG_REG (rtl);
15978 /* FALLTHRU */
15979 case TRUNCATE:
15980 if (inner == NULL_RTX)
15981 inner = XEXP (rtl, 0);
15982 if (is_a <scalar_int_mode> (mode, &int_mode)
15983 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15984 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15985 #ifdef POINTERS_EXTEND_UNSIGNED
15986 || (int_mode == Pmode && mem_mode != VOIDmode)
15987 #endif
15989 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15991 mem_loc_result = mem_loc_descriptor (inner,
15992 inner_mode,
15993 mem_mode, initialized);
15994 break;
15996 if (dwarf_strict && dwarf_version < 5)
15997 break;
15998 if (is_a <scalar_int_mode> (mode, &int_mode)
15999 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
16000 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
16001 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16003 dw_die_ref type_die;
16004 dw_loc_descr_ref cvt;
16006 mem_loc_result = mem_loc_descriptor (inner,
16007 GET_MODE (inner),
16008 mem_mode, initialized);
16009 if (mem_loc_result == NULL)
16010 break;
16011 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16012 if (type_die == NULL)
16014 mem_loc_result = NULL;
16015 break;
16017 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16018 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16019 else
16020 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16021 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16022 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16023 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16024 add_loc_descr (&mem_loc_result, cvt);
16025 if (is_a <scalar_int_mode> (mode, &int_mode)
16026 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16028 /* Convert it to untyped afterwards. */
16029 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16030 add_loc_descr (&mem_loc_result, cvt);
16033 break;
16035 case REG:
16036 if (!is_a <scalar_int_mode> (mode, &int_mode)
16037 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16038 && rtl != arg_pointer_rtx
16039 && rtl != frame_pointer_rtx
16040 #ifdef POINTERS_EXTEND_UNSIGNED
16041 && (int_mode != Pmode || mem_mode == VOIDmode)
16042 #endif
16045 dw_die_ref type_die;
16046 unsigned int debugger_regnum;
16048 if (dwarf_strict && dwarf_version < 5)
16049 break;
16050 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16051 break;
16052 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16053 if (type_die == NULL)
16054 break;
16056 debugger_regnum = debugger_reg_number (rtl);
16057 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16058 break;
16059 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16060 debugger_regnum, 0);
16061 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16062 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16063 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16064 break;
16066 /* Whenever a register number forms a part of the description of the
16067 method for calculating the (dynamic) address of a memory resident
16068 object, DWARF rules require the register number be referred to as
16069 a "base register". This distinction is not based in any way upon
16070 what category of register the hardware believes the given register
16071 belongs to. This is strictly DWARF terminology we're dealing with
16072 here. Note that in cases where the location of a memory-resident
16073 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16074 OP_CONST (0)) the actual DWARF location descriptor that we generate
16075 may just be OP_BASEREG (basereg). This may look deceptively like
16076 the object in question was allocated to a register (rather than in
16077 memory) so DWARF consumers need to be aware of the subtle
16078 distinction between OP_REG and OP_BASEREG. */
16079 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16080 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16081 else if (stack_realign_drap
16082 && crtl->drap_reg
16083 && crtl->args.internal_arg_pointer == rtl
16084 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16086 /* If RTL is internal_arg_pointer, which has been optimized
16087 out, use DRAP instead. */
16088 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16089 VAR_INIT_STATUS_INITIALIZED);
16091 break;
16093 case SIGN_EXTEND:
16094 case ZERO_EXTEND:
16095 if (!is_a <scalar_int_mode> (mode, &int_mode)
16096 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16097 break;
16098 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16099 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16100 if (op0 == 0)
16101 break;
16102 else if (GET_CODE (rtl) == ZERO_EXTEND
16103 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16104 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16105 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16106 to expand zero extend as two shifts instead of
16107 masking. */
16108 && GET_MODE_SIZE (inner_mode) <= 4)
16110 mem_loc_result = op0;
16111 add_loc_descr (&mem_loc_result,
16112 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16113 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16115 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16117 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16118 shift *= BITS_PER_UNIT;
16119 if (GET_CODE (rtl) == SIGN_EXTEND)
16120 op = DW_OP_shra;
16121 else
16122 op = DW_OP_shr;
16123 mem_loc_result = op0;
16124 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16125 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16126 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16127 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16129 else if (!dwarf_strict || dwarf_version >= 5)
16131 dw_die_ref type_die1, type_die2;
16132 dw_loc_descr_ref cvt;
16134 type_die1 = base_type_for_mode (inner_mode,
16135 GET_CODE (rtl) == ZERO_EXTEND);
16136 if (type_die1 == NULL)
16137 break;
16138 type_die2 = base_type_for_mode (int_mode, 1);
16139 if (type_die2 == NULL)
16140 break;
16141 mem_loc_result = op0;
16142 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16143 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16144 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16145 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16146 add_loc_descr (&mem_loc_result, cvt);
16147 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16148 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16149 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16150 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16151 add_loc_descr (&mem_loc_result, cvt);
16153 break;
16155 case MEM:
16157 rtx new_rtl = avoid_constant_pool_reference (rtl);
16158 if (new_rtl != rtl)
16160 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16161 initialized);
16162 if (mem_loc_result != NULL)
16163 return mem_loc_result;
16166 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16167 get_address_mode (rtl), mode,
16168 VAR_INIT_STATUS_INITIALIZED);
16169 if (mem_loc_result == NULL)
16170 mem_loc_result = tls_mem_loc_descriptor (rtl);
16171 if (mem_loc_result != NULL)
16173 if (!is_a <scalar_int_mode> (mode, &int_mode)
16174 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16176 dw_die_ref type_die;
16177 dw_loc_descr_ref deref;
16178 HOST_WIDE_INT size;
16180 if (dwarf_strict && dwarf_version < 5)
16181 return NULL;
16182 if (!GET_MODE_SIZE (mode).is_constant (&size))
16183 return NULL;
16184 type_die
16185 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16186 if (type_die == NULL)
16187 return NULL;
16188 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16189 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16190 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16191 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16192 add_loc_descr (&mem_loc_result, deref);
16194 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16195 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16196 else
16197 add_loc_descr (&mem_loc_result,
16198 new_loc_descr (DW_OP_deref_size,
16199 GET_MODE_SIZE (int_mode), 0));
16201 break;
16203 case LO_SUM:
16204 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16206 case LABEL_REF:
16207 /* Some ports can transform a symbol ref into a label ref, because
16208 the symbol ref is too far away and has to be dumped into a constant
16209 pool. */
16210 case CONST:
16211 case SYMBOL_REF:
16212 case UNSPEC:
16213 if (!is_a <scalar_int_mode> (mode, &int_mode)
16214 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16215 #ifdef POINTERS_EXTEND_UNSIGNED
16216 && (int_mode != Pmode || mem_mode == VOIDmode)
16217 #endif
16219 break;
16221 if (GET_CODE (rtl) == UNSPEC)
16223 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16224 can't express it in the debug info. This can happen e.g. with some
16225 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16226 approves. */
16227 bool not_ok = false;
16228 subrtx_var_iterator::array_type array;
16229 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16230 if (*iter != rtl && !CONSTANT_P (*iter))
16232 not_ok = true;
16233 break;
16236 if (not_ok)
16237 break;
16239 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16240 if (!const_ok_for_output_1 (*iter))
16242 not_ok = true;
16243 break;
16246 if (not_ok)
16247 break;
16249 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16250 goto symref;
16253 if (GET_CODE (rtl) == SYMBOL_REF
16254 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16256 dw_loc_descr_ref temp;
16258 /* If this is not defined, we have no way to emit the data. */
16259 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16260 break;
16262 temp = new_addr_loc_descr (rtl, dtprel_true);
16264 /* We check for DWARF 5 here because gdb did not implement
16265 DW_OP_form_tls_address until after 7.12. */
16266 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16267 ? DW_OP_form_tls_address
16268 : DW_OP_GNU_push_tls_address),
16269 0, 0);
16270 add_loc_descr (&mem_loc_result, temp);
16272 break;
16275 if (!const_ok_for_output (rtl))
16277 if (GET_CODE (rtl) == CONST)
16278 switch (GET_CODE (XEXP (rtl, 0)))
16280 case NOT:
16281 op = DW_OP_not;
16282 goto try_const_unop;
16283 case NEG:
16284 op = DW_OP_neg;
16285 goto try_const_unop;
16286 try_const_unop:
16287 rtx arg;
16288 arg = XEXP (XEXP (rtl, 0), 0);
16289 if (!CONSTANT_P (arg))
16290 arg = gen_rtx_CONST (int_mode, arg);
16291 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16292 initialized);
16293 if (op0)
16295 mem_loc_result = op0;
16296 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16298 break;
16299 default:
16300 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16301 mem_mode, initialized);
16302 break;
16304 break;
16307 symref:
16308 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16309 vec_safe_push (used_rtx_array, rtl);
16310 break;
16312 case CONCAT:
16313 case CONCATN:
16314 case VAR_LOCATION:
16315 case DEBUG_IMPLICIT_PTR:
16316 expansion_failed (NULL_TREE, rtl,
16317 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16318 return 0;
16320 case ENTRY_VALUE:
16321 if (dwarf_strict && dwarf_version < 5)
16322 return NULL;
16323 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16325 if (!is_a <scalar_int_mode> (mode, &int_mode)
16326 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16327 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16328 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16329 else
16331 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16332 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16333 return NULL;
16334 op0 = one_reg_loc_descriptor (debugger_regnum,
16335 VAR_INIT_STATUS_INITIALIZED);
16338 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16339 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16341 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16342 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16343 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16344 return NULL;
16346 else
16347 gcc_unreachable ();
16348 if (op0 == NULL)
16349 return NULL;
16350 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16351 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16352 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16353 break;
16355 case DEBUG_PARAMETER_REF:
16356 mem_loc_result = parameter_ref_descriptor (rtl);
16357 break;
16359 case PRE_MODIFY:
16360 /* Extract the PLUS expression nested inside and fall into
16361 PLUS code below. */
16362 rtl = XEXP (rtl, 1);
16363 goto plus;
16365 case PRE_INC:
16366 case PRE_DEC:
16367 /* Turn these into a PLUS expression and fall into the PLUS code
16368 below. */
16369 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16370 gen_int_mode (GET_CODE (rtl) == PRE_INC
16371 ? GET_MODE_UNIT_SIZE (mem_mode)
16372 : -GET_MODE_UNIT_SIZE (mem_mode),
16373 mode));
16375 /* fall through */
16377 case PLUS:
16378 plus:
16379 if (is_based_loc (rtl)
16380 && is_a <scalar_int_mode> (mode, &int_mode)
16381 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16382 || XEXP (rtl, 0) == arg_pointer_rtx
16383 || XEXP (rtl, 0) == frame_pointer_rtx))
16384 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16385 INTVAL (XEXP (rtl, 1)),
16386 VAR_INIT_STATUS_INITIALIZED);
16387 else
16389 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16390 VAR_INIT_STATUS_INITIALIZED);
16391 if (mem_loc_result == 0)
16392 break;
16394 if (CONST_INT_P (XEXP (rtl, 1))
16395 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16396 <= DWARF2_ADDR_SIZE))
16397 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16398 else
16400 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16401 VAR_INIT_STATUS_INITIALIZED);
16402 if (op1 == 0)
16403 return NULL;
16404 add_loc_descr (&mem_loc_result, op1);
16405 add_loc_descr (&mem_loc_result,
16406 new_loc_descr (DW_OP_plus, 0, 0));
16409 break;
16411 /* If a pseudo-reg is optimized away, it is possible for it to
16412 be replaced with a MEM containing a multiply or shift. */
16413 case MINUS:
16414 op = DW_OP_minus;
16415 goto do_binop;
16417 case MULT:
16418 op = DW_OP_mul;
16419 goto do_binop;
16421 case DIV:
16422 if ((!dwarf_strict || dwarf_version >= 5)
16423 && is_a <scalar_int_mode> (mode, &int_mode)
16424 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16426 mem_loc_result = typed_binop (DW_OP_div, rtl,
16427 base_type_for_mode (mode, 0),
16428 int_mode, mem_mode);
16429 break;
16431 op = DW_OP_div;
16432 goto do_binop;
16434 case UMOD:
16435 op = DW_OP_mod;
16436 goto do_binop;
16438 case ASHIFT:
16439 op = DW_OP_shl;
16440 goto do_shift;
16442 case ASHIFTRT:
16443 op = DW_OP_shra;
16444 goto do_shift;
16446 case LSHIFTRT:
16447 op = DW_OP_shr;
16448 goto do_shift;
16450 do_shift:
16451 if (!is_a <scalar_int_mode> (mode, &int_mode))
16452 break;
16453 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16454 VAR_INIT_STATUS_INITIALIZED);
16456 rtx rtlop1 = XEXP (rtl, 1);
16457 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16458 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16459 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16460 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16461 VAR_INIT_STATUS_INITIALIZED);
16464 if (op0 == 0 || op1 == 0)
16465 break;
16467 mem_loc_result = op0;
16468 add_loc_descr (&mem_loc_result, op1);
16469 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16470 break;
16472 case AND:
16473 op = DW_OP_and;
16474 goto do_binop;
16476 case IOR:
16477 op = DW_OP_or;
16478 goto do_binop;
16480 case XOR:
16481 op = DW_OP_xor;
16482 goto do_binop;
16484 do_binop:
16485 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16486 VAR_INIT_STATUS_INITIALIZED);
16487 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16489 if (op0 == 0)
16490 break;
16491 mem_loc_result = op0;
16492 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16493 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16494 break;
16496 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16497 VAR_INIT_STATUS_INITIALIZED);
16499 if (op0 == 0 || op1 == 0)
16500 break;
16502 mem_loc_result = op0;
16503 add_loc_descr (&mem_loc_result, op1);
16504 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16505 break;
16507 case MOD:
16508 if ((!dwarf_strict || dwarf_version >= 5)
16509 && is_a <scalar_int_mode> (mode, &int_mode)
16510 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16512 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16513 base_type_for_mode (mode, 0),
16514 int_mode, mem_mode);
16515 break;
16518 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16519 VAR_INIT_STATUS_INITIALIZED);
16520 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16521 VAR_INIT_STATUS_INITIALIZED);
16523 if (op0 == 0 || op1 == 0)
16524 break;
16526 mem_loc_result = op0;
16527 add_loc_descr (&mem_loc_result, op1);
16528 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16529 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16530 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16531 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16532 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16533 break;
16535 case UDIV:
16536 if ((!dwarf_strict || dwarf_version >= 5)
16537 && is_a <scalar_int_mode> (mode, &int_mode))
16539 /* We can use a signed divide if the sign bit is not set. */
16540 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16542 op = DW_OP_div;
16543 goto do_binop;
16546 mem_loc_result = typed_binop (DW_OP_div, rtl,
16547 base_type_for_mode (int_mode, 1),
16548 int_mode, mem_mode);
16550 break;
16552 case NOT:
16553 op = DW_OP_not;
16554 goto do_unop;
16556 case ABS:
16557 op = DW_OP_abs;
16558 goto do_unop;
16560 case NEG:
16561 op = DW_OP_neg;
16562 goto do_unop;
16564 do_unop:
16565 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16566 VAR_INIT_STATUS_INITIALIZED);
16568 if (op0 == 0)
16569 break;
16571 mem_loc_result = op0;
16572 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16573 break;
16575 case CONST_INT:
16576 if (!is_a <scalar_int_mode> (mode, &int_mode)
16577 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16578 #ifdef POINTERS_EXTEND_UNSIGNED
16579 || (int_mode == Pmode
16580 && mem_mode != VOIDmode
16581 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16582 #endif
16585 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16586 break;
16588 if ((!dwarf_strict || dwarf_version >= 5)
16589 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16590 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16592 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16593 scalar_int_mode amode;
16594 if (type_die == NULL)
16595 return NULL;
16596 if (INTVAL (rtl) >= 0
16597 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16598 .exists (&amode))
16599 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16600 /* const DW_OP_convert <XXX> vs.
16601 DW_OP_const_type <XXX, 1, const>. */
16602 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16603 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16605 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16606 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16607 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16608 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16609 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16610 add_loc_descr (&mem_loc_result, op0);
16611 return mem_loc_result;
16613 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16614 INTVAL (rtl));
16615 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16616 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16617 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16618 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16619 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16620 else
16622 mem_loc_result->dw_loc_oprnd2.val_class
16623 = dw_val_class_const_double;
16624 mem_loc_result->dw_loc_oprnd2.v.val_double
16625 = double_int::from_shwi (INTVAL (rtl));
16628 break;
16630 case CONST_DOUBLE:
16631 if (!dwarf_strict || dwarf_version >= 5)
16633 dw_die_ref type_die;
16635 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16636 CONST_DOUBLE rtx could represent either a large integer
16637 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16638 the value is always a floating point constant.
16640 When it is an integer, a CONST_DOUBLE is used whenever
16641 the constant requires 2 HWIs to be adequately represented.
16642 We output CONST_DOUBLEs as blocks. */
16643 if (mode == VOIDmode
16644 || (GET_MODE (rtl) == VOIDmode
16645 && maybe_ne (GET_MODE_BITSIZE (mode),
16646 HOST_BITS_PER_DOUBLE_INT)))
16647 break;
16648 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16649 if (type_die == NULL)
16650 return NULL;
16651 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16652 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16653 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16654 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16655 #if TARGET_SUPPORTS_WIDE_INT == 0
16656 if (!SCALAR_FLOAT_MODE_P (mode))
16658 mem_loc_result->dw_loc_oprnd2.val_class
16659 = dw_val_class_const_double;
16660 mem_loc_result->dw_loc_oprnd2.v.val_double
16661 = rtx_to_double_int (rtl);
16663 else
16664 #endif
16666 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16667 unsigned int length = GET_MODE_SIZE (float_mode);
16668 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16669 unsigned int elt_size = insert_float (rtl, array);
16671 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16672 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16673 = length / elt_size;
16674 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16675 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16678 break;
16680 case CONST_WIDE_INT:
16681 if (!dwarf_strict || dwarf_version >= 5)
16683 dw_die_ref type_die;
16685 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16686 if (type_die == NULL)
16687 return NULL;
16688 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16689 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16690 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16691 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16692 mem_loc_result->dw_loc_oprnd2.val_class
16693 = dw_val_class_wide_int;
16694 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16695 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16697 break;
16699 case CONST_POLY_INT:
16700 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16701 break;
16703 case EQ:
16704 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16705 break;
16707 case GE:
16708 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16709 break;
16711 case GT:
16712 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16713 break;
16715 case LE:
16716 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16717 break;
16719 case LT:
16720 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16721 break;
16723 case NE:
16724 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16725 break;
16727 case GEU:
16728 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16729 break;
16731 case GTU:
16732 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16733 break;
16735 case LEU:
16736 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16737 break;
16739 case LTU:
16740 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16741 break;
16743 case UMIN:
16744 case UMAX:
16745 if (!SCALAR_INT_MODE_P (mode))
16746 break;
16747 /* FALLTHRU */
16748 case SMIN:
16749 case SMAX:
16750 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16751 break;
16753 case ZERO_EXTRACT:
16754 case SIGN_EXTRACT:
16755 if (CONST_INT_P (XEXP (rtl, 1))
16756 && CONST_INT_P (XEXP (rtl, 2))
16757 && is_a <scalar_int_mode> (mode, &int_mode)
16758 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16759 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16760 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16761 && ((unsigned) INTVAL (XEXP (rtl, 1))
16762 + (unsigned) INTVAL (XEXP (rtl, 2))
16763 <= GET_MODE_BITSIZE (int_mode)))
16765 int shift, size;
16766 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16767 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16768 if (op0 == 0)
16769 break;
16770 if (GET_CODE (rtl) == SIGN_EXTRACT)
16771 op = DW_OP_shra;
16772 else
16773 op = DW_OP_shr;
16774 mem_loc_result = op0;
16775 size = INTVAL (XEXP (rtl, 1));
16776 shift = INTVAL (XEXP (rtl, 2));
16777 if (BITS_BIG_ENDIAN)
16778 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16779 if (shift + size != (int) DWARF2_ADDR_SIZE)
16781 add_loc_descr (&mem_loc_result,
16782 int_loc_descriptor (DWARF2_ADDR_SIZE
16783 - shift - size));
16784 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16786 if (size != (int) DWARF2_ADDR_SIZE)
16788 add_loc_descr (&mem_loc_result,
16789 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16790 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16793 break;
16795 case IF_THEN_ELSE:
16797 dw_loc_descr_ref op2, bra_node, drop_node;
16798 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16799 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16800 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16801 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16802 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16803 VAR_INIT_STATUS_INITIALIZED);
16804 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16805 VAR_INIT_STATUS_INITIALIZED);
16806 if (op0 == NULL || op1 == NULL || op2 == NULL)
16807 break;
16809 mem_loc_result = op1;
16810 add_loc_descr (&mem_loc_result, op2);
16811 add_loc_descr (&mem_loc_result, op0);
16812 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16813 add_loc_descr (&mem_loc_result, bra_node);
16814 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16815 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16816 add_loc_descr (&mem_loc_result, drop_node);
16817 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16818 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16820 break;
16822 case FLOAT_EXTEND:
16823 case FLOAT_TRUNCATE:
16824 case FLOAT:
16825 case UNSIGNED_FLOAT:
16826 case FIX:
16827 case UNSIGNED_FIX:
16828 if (!dwarf_strict || dwarf_version >= 5)
16830 dw_die_ref type_die;
16831 dw_loc_descr_ref cvt;
16833 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16834 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16835 if (op0 == NULL)
16836 break;
16837 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16838 && (GET_CODE (rtl) == FLOAT
16839 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16841 type_die = base_type_for_mode (int_mode,
16842 GET_CODE (rtl) == UNSIGNED_FLOAT);
16843 if (type_die == NULL)
16844 break;
16845 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16846 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16847 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16848 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16849 add_loc_descr (&op0, cvt);
16851 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16852 if (type_die == NULL)
16853 break;
16854 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16855 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16856 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16857 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16858 add_loc_descr (&op0, cvt);
16859 if (is_a <scalar_int_mode> (mode, &int_mode)
16860 && (GET_CODE (rtl) == FIX
16861 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16863 op0 = convert_descriptor_to_mode (int_mode, op0);
16864 if (op0 == NULL)
16865 break;
16867 mem_loc_result = op0;
16869 break;
16871 case CLZ:
16872 case CTZ:
16873 case FFS:
16874 if (is_a <scalar_int_mode> (mode, &int_mode))
16875 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16876 break;
16878 case POPCOUNT:
16879 case PARITY:
16880 if (is_a <scalar_int_mode> (mode, &int_mode))
16881 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16882 break;
16884 case BSWAP:
16885 if (is_a <scalar_int_mode> (mode, &int_mode))
16886 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16887 break;
16889 case ROTATE:
16890 case ROTATERT:
16891 if (is_a <scalar_int_mode> (mode, &int_mode))
16892 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16893 break;
16895 case COMPARE:
16896 /* In theory, we could implement the above. */
16897 /* DWARF cannot represent the unsigned compare operations
16898 natively. */
16899 case SS_MULT:
16900 case US_MULT:
16901 case SS_DIV:
16902 case US_DIV:
16903 case SS_PLUS:
16904 case US_PLUS:
16905 case SS_MINUS:
16906 case US_MINUS:
16907 case SS_NEG:
16908 case US_NEG:
16909 case SS_ABS:
16910 case SS_ASHIFT:
16911 case US_ASHIFT:
16912 case SS_TRUNCATE:
16913 case US_TRUNCATE:
16914 case UNORDERED:
16915 case ORDERED:
16916 case UNEQ:
16917 case UNGE:
16918 case UNGT:
16919 case UNLE:
16920 case UNLT:
16921 case LTGT:
16922 case FRACT_CONVERT:
16923 case UNSIGNED_FRACT_CONVERT:
16924 case SAT_FRACT:
16925 case UNSIGNED_SAT_FRACT:
16926 case SQRT:
16927 case ASM_OPERANDS:
16928 case VEC_MERGE:
16929 case VEC_SELECT:
16930 case VEC_CONCAT:
16931 case VEC_DUPLICATE:
16932 case VEC_SERIES:
16933 case HIGH:
16934 case FMA:
16935 case STRICT_LOW_PART:
16936 case CONST_VECTOR:
16937 case CONST_FIXED:
16938 case CLRSB:
16939 case CLOBBER:
16940 case SMUL_HIGHPART:
16941 case UMUL_HIGHPART:
16942 break;
16944 case CONST_STRING:
16945 resolve_one_addr (&rtl);
16946 goto symref;
16948 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16949 the expression. An UNSPEC rtx represents a raw DWARF operation,
16950 new_loc_descr is called for it to build the operation directly.
16951 Otherwise mem_loc_descriptor is called recursively. */
16952 case PARALLEL:
16954 int index = 0;
16955 dw_loc_descr_ref exp_result = NULL;
16957 for (; index < XVECLEN (rtl, 0); index++)
16959 rtx elem = XVECEXP (rtl, 0, index);
16960 if (GET_CODE (elem) == UNSPEC)
16962 /* Each DWARF operation UNSPEC contain two operands, if
16963 one operand is not used for the operation, const0_rtx is
16964 passed. */
16965 gcc_assert (XVECLEN (elem, 0) == 2);
16967 HOST_WIDE_INT dw_op = XINT (elem, 1);
16968 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16969 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16970 exp_result
16971 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16972 oprnd2);
16974 else
16975 exp_result
16976 = mem_loc_descriptor (elem, mode, mem_mode,
16977 VAR_INIT_STATUS_INITIALIZED);
16979 if (!mem_loc_result)
16980 mem_loc_result = exp_result;
16981 else
16982 add_loc_descr (&mem_loc_result, exp_result);
16985 break;
16988 default:
16989 if (flag_checking)
16991 print_rtl (stderr, rtl);
16992 gcc_unreachable ();
16994 break;
16997 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16998 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17000 return mem_loc_result;
17003 /* Return a descriptor that describes the concatenation of two locations.
17004 This is typically a complex variable. */
17006 static dw_loc_descr_ref
17007 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
17009 /* At present we only track constant-sized pieces. */
17010 unsigned int size0, size1;
17011 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
17012 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
17013 return 0;
17015 dw_loc_descr_ref cc_loc_result = NULL;
17016 dw_loc_descr_ref x0_ref
17017 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17018 dw_loc_descr_ref x1_ref
17019 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17021 if (x0_ref == 0 || x1_ref == 0)
17022 return 0;
17024 cc_loc_result = x0_ref;
17025 add_loc_descr_op_piece (&cc_loc_result, size0);
17027 add_loc_descr (&cc_loc_result, x1_ref);
17028 add_loc_descr_op_piece (&cc_loc_result, size1);
17030 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17031 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17033 return cc_loc_result;
17036 /* Return a descriptor that describes the concatenation of N
17037 locations. */
17039 static dw_loc_descr_ref
17040 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17042 unsigned int i;
17043 dw_loc_descr_ref cc_loc_result = NULL;
17044 unsigned int n = XVECLEN (concatn, 0);
17045 unsigned int size;
17047 for (i = 0; i < n; ++i)
17049 dw_loc_descr_ref ref;
17050 rtx x = XVECEXP (concatn, 0, i);
17052 /* At present we only track constant-sized pieces. */
17053 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17054 return NULL;
17056 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17057 if (ref == NULL)
17058 return NULL;
17060 add_loc_descr (&cc_loc_result, ref);
17061 add_loc_descr_op_piece (&cc_loc_result, size);
17064 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17065 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17067 return cc_loc_result;
17070 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17071 for DEBUG_IMPLICIT_PTR RTL. */
17073 static dw_loc_descr_ref
17074 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17076 dw_loc_descr_ref ret;
17077 dw_die_ref ref;
17079 if (dwarf_strict && dwarf_version < 5)
17080 return NULL;
17081 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
17082 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17083 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17084 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17085 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17086 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17087 if (ref)
17089 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17090 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17091 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17093 else
17095 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17096 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17098 return ret;
17101 /* Output a proper Dwarf location descriptor for a variable or parameter
17102 which is either allocated in a register or in a memory location. For a
17103 register, we just generate an OP_REG and the register number. For a
17104 memory location we provide a Dwarf postfix expression describing how to
17105 generate the (dynamic) address of the object onto the address stack.
17107 MODE is mode of the decl if this loc_descriptor is going to be used in
17108 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17109 allowed, VOIDmode otherwise.
17111 If we don't know how to describe it, return 0. */
17113 static dw_loc_descr_ref
17114 loc_descriptor (rtx rtl, machine_mode mode,
17115 enum var_init_status initialized)
17117 dw_loc_descr_ref loc_result = NULL;
17118 scalar_int_mode int_mode;
17120 switch (GET_CODE (rtl))
17122 case SUBREG:
17123 /* The case of a subreg may arise when we have a local (register)
17124 variable or a formal (register) parameter which doesn't quite fill
17125 up an entire register. For now, just assume that it is
17126 legitimate to make the Dwarf info refer to the whole register which
17127 contains the given subreg. */
17128 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17129 loc_result = loc_descriptor (SUBREG_REG (rtl),
17130 GET_MODE (SUBREG_REG (rtl)), initialized);
17131 else
17132 goto do_default;
17133 break;
17135 case REG:
17136 loc_result = reg_loc_descriptor (rtl, initialized);
17137 break;
17139 case MEM:
17140 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17141 GET_MODE (rtl), initialized);
17142 if (loc_result == NULL)
17143 loc_result = tls_mem_loc_descriptor (rtl);
17144 if (loc_result == NULL)
17146 rtx new_rtl = avoid_constant_pool_reference (rtl);
17147 if (new_rtl != rtl)
17148 loc_result = loc_descriptor (new_rtl, mode, initialized);
17150 break;
17152 case CONCAT:
17153 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17154 initialized);
17155 break;
17157 case CONCATN:
17158 loc_result = concatn_loc_descriptor (rtl, initialized);
17159 break;
17161 case VAR_LOCATION:
17162 /* Single part. */
17163 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17165 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17166 if (GET_CODE (loc) == EXPR_LIST)
17167 loc = XEXP (loc, 0);
17168 loc_result = loc_descriptor (loc, mode, initialized);
17169 break;
17172 rtl = XEXP (rtl, 1);
17173 /* FALLTHRU */
17175 case PARALLEL:
17177 rtvec par_elems = XVEC (rtl, 0);
17178 int num_elem = GET_NUM_ELEM (par_elems);
17179 machine_mode mode;
17180 int i, size;
17182 /* Create the first one, so we have something to add to. */
17183 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17184 VOIDmode, initialized);
17185 if (loc_result == NULL)
17186 return NULL;
17187 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17188 /* At present we only track constant-sized pieces. */
17189 if (!GET_MODE_SIZE (mode).is_constant (&size))
17190 return NULL;
17191 add_loc_descr_op_piece (&loc_result, size);
17192 for (i = 1; i < num_elem; i++)
17194 dw_loc_descr_ref temp;
17196 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17197 VOIDmode, initialized);
17198 if (temp == NULL)
17199 return NULL;
17200 add_loc_descr (&loc_result, temp);
17201 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17202 /* At present we only track constant-sized pieces. */
17203 if (!GET_MODE_SIZE (mode).is_constant (&size))
17204 return NULL;
17205 add_loc_descr_op_piece (&loc_result, size);
17208 break;
17210 case CONST_INT:
17211 if (mode != VOIDmode && mode != BLKmode)
17213 int_mode = as_a <scalar_int_mode> (mode);
17214 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17215 INTVAL (rtl));
17217 break;
17219 case CONST_DOUBLE:
17220 if (mode == VOIDmode)
17221 mode = GET_MODE (rtl);
17223 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17225 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17227 /* Note that a CONST_DOUBLE rtx could represent either an integer
17228 or a floating-point constant. A CONST_DOUBLE is used whenever
17229 the constant requires more than one word in order to be
17230 adequately represented. We output CONST_DOUBLEs as blocks. */
17231 scalar_mode smode = as_a <scalar_mode> (mode);
17232 loc_result = new_loc_descr (DW_OP_implicit_value,
17233 GET_MODE_SIZE (smode), 0);
17234 #if TARGET_SUPPORTS_WIDE_INT == 0
17235 if (!SCALAR_FLOAT_MODE_P (smode))
17237 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17238 loc_result->dw_loc_oprnd2.v.val_double
17239 = rtx_to_double_int (rtl);
17241 else
17242 #endif
17244 unsigned int length = GET_MODE_SIZE (smode);
17245 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17246 unsigned int elt_size = insert_float (rtl, array);
17248 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17249 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17250 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17251 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17254 break;
17256 case CONST_WIDE_INT:
17257 if (mode == VOIDmode)
17258 mode = GET_MODE (rtl);
17260 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17262 int_mode = as_a <scalar_int_mode> (mode);
17263 loc_result = new_loc_descr (DW_OP_implicit_value,
17264 GET_MODE_SIZE (int_mode), 0);
17265 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17266 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17267 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17269 break;
17271 case CONST_VECTOR:
17272 if (mode == VOIDmode)
17273 mode = GET_MODE (rtl);
17275 if (mode != VOIDmode
17276 /* The combination of a length and byte elt_size doesn't extend
17277 naturally to boolean vectors, where several elements are packed
17278 into the same byte. */
17279 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17280 && (dwarf_version >= 4 || !dwarf_strict))
17282 unsigned int length;
17283 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17284 return NULL;
17286 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17287 unsigned char *array
17288 = ggc_vec_alloc<unsigned char> (length * elt_size);
17289 unsigned int i;
17290 unsigned char *p;
17291 machine_mode imode = GET_MODE_INNER (mode);
17293 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17294 switch (GET_MODE_CLASS (mode))
17296 case MODE_VECTOR_INT:
17297 for (i = 0, p = array; i < length; i++, p += elt_size)
17299 rtx elt = CONST_VECTOR_ELT (rtl, i);
17300 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17302 break;
17304 case MODE_VECTOR_FLOAT:
17305 for (i = 0, p = array; i < length; i++, p += elt_size)
17307 rtx elt = CONST_VECTOR_ELT (rtl, i);
17308 insert_float (elt, p);
17310 break;
17312 default:
17313 gcc_unreachable ();
17316 loc_result = new_loc_descr (DW_OP_implicit_value,
17317 length * elt_size, 0);
17318 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17319 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17320 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17321 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17323 break;
17325 case CONST:
17326 if (mode == VOIDmode
17327 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17328 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17329 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17331 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17332 break;
17334 /* FALLTHROUGH */
17335 case SYMBOL_REF:
17336 if (!const_ok_for_output (rtl))
17337 break;
17338 /* FALLTHROUGH */
17339 case LABEL_REF:
17340 if (is_a <scalar_int_mode> (mode, &int_mode)
17341 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17342 && (dwarf_version >= 4 || !dwarf_strict))
17344 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17345 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17346 vec_safe_push (used_rtx_array, rtl);
17348 break;
17350 case DEBUG_IMPLICIT_PTR:
17351 loc_result = implicit_ptr_descriptor (rtl, 0);
17352 break;
17354 case PLUS:
17355 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17356 && CONST_INT_P (XEXP (rtl, 1)))
17358 loc_result
17359 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17360 break;
17362 /* FALLTHRU */
17363 do_default:
17364 default:
17365 if ((is_a <scalar_int_mode> (mode, &int_mode)
17366 && GET_MODE (rtl) == int_mode
17367 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17368 && dwarf_version >= 4)
17369 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17371 /* Value expression. */
17372 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17373 if (loc_result)
17374 add_loc_descr (&loc_result,
17375 new_loc_descr (DW_OP_stack_value, 0, 0));
17377 break;
17380 return loc_result;
17383 /* We need to figure out what section we should use as the base for the
17384 address ranges where a given location is valid.
17385 1. If this particular DECL has a section associated with it, use that.
17386 2. If this function has a section associated with it, use that.
17387 3. Otherwise, use the text section.
17388 XXX: If you split a variable across multiple sections, we won't notice. */
17390 static const char *
17391 secname_for_decl (const_tree decl)
17393 const char *secname;
17395 if (VAR_OR_FUNCTION_DECL_P (decl)
17396 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17397 && DECL_SECTION_NAME (decl))
17398 secname = DECL_SECTION_NAME (decl);
17399 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17401 if (in_cold_section_p)
17403 section *sec = current_function_section ();
17404 if (sec->common.flags & SECTION_NAMED)
17405 return sec->named.name;
17407 secname = DECL_SECTION_NAME (current_function_decl);
17409 else if (cfun && in_cold_section_p)
17410 secname = crtl->subsections.cold_section_label;
17411 else
17412 secname = text_section_label;
17414 return secname;
17417 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17419 static bool
17420 decl_by_reference_p (tree decl)
17422 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17423 || VAR_P (decl))
17424 && DECL_BY_REFERENCE (decl));
17427 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17428 for VARLOC. */
17430 static dw_loc_descr_ref
17431 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17432 enum var_init_status initialized)
17434 int have_address = 0;
17435 dw_loc_descr_ref descr;
17436 machine_mode mode;
17438 if (want_address != 2)
17440 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17441 /* Single part. */
17442 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17444 varloc = PAT_VAR_LOCATION_LOC (varloc);
17445 if (GET_CODE (varloc) == EXPR_LIST)
17446 varloc = XEXP (varloc, 0);
17447 mode = GET_MODE (varloc);
17448 if (MEM_P (varloc))
17450 rtx addr = XEXP (varloc, 0);
17451 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17452 mode, initialized);
17453 if (descr)
17454 have_address = 1;
17455 else
17457 rtx x = avoid_constant_pool_reference (varloc);
17458 if (x != varloc)
17459 descr = mem_loc_descriptor (x, mode, VOIDmode,
17460 initialized);
17463 else
17464 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17466 else
17467 return 0;
17469 else
17471 if (GET_CODE (varloc) == VAR_LOCATION)
17472 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17473 else
17474 mode = DECL_MODE (loc);
17475 descr = loc_descriptor (varloc, mode, initialized);
17476 have_address = 1;
17479 if (!descr)
17480 return 0;
17482 if (want_address == 2 && !have_address
17483 && (dwarf_version >= 4 || !dwarf_strict))
17485 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17487 expansion_failed (loc, NULL_RTX,
17488 "DWARF address size mismatch");
17489 return 0;
17491 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17492 have_address = 1;
17494 /* Show if we can't fill the request for an address. */
17495 if (want_address && !have_address)
17497 expansion_failed (loc, NULL_RTX,
17498 "Want address and only have value");
17499 return 0;
17502 /* If we've got an address and don't want one, dereference. */
17503 if (!want_address && have_address)
17505 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17506 enum dwarf_location_atom op;
17508 if (size > DWARF2_ADDR_SIZE || size == -1)
17510 expansion_failed (loc, NULL_RTX,
17511 "DWARF address size mismatch");
17512 return 0;
17514 else if (size == DWARF2_ADDR_SIZE)
17515 op = DW_OP_deref;
17516 else
17517 op = DW_OP_deref_size;
17519 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17522 return descr;
17525 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17526 if it is not possible. */
17528 static dw_loc_descr_ref
17529 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17531 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17532 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17533 else if (dwarf_version >= 3 || !dwarf_strict)
17534 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17535 else
17536 return NULL;
17539 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17540 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17542 static dw_loc_descr_ref
17543 dw_sra_loc_expr (tree decl, rtx loc)
17545 rtx p;
17546 unsigned HOST_WIDE_INT padsize = 0;
17547 dw_loc_descr_ref descr, *descr_tail;
17548 unsigned HOST_WIDE_INT decl_size;
17549 rtx varloc;
17550 enum var_init_status initialized;
17552 if (DECL_SIZE (decl) == NULL
17553 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17554 return NULL;
17556 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17557 descr = NULL;
17558 descr_tail = &descr;
17560 for (p = loc; p; p = XEXP (p, 1))
17562 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17563 rtx loc_note = *decl_piece_varloc_ptr (p);
17564 dw_loc_descr_ref cur_descr;
17565 dw_loc_descr_ref *tail, last = NULL;
17566 unsigned HOST_WIDE_INT opsize = 0;
17568 if (loc_note == NULL_RTX
17569 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17571 padsize += bitsize;
17572 continue;
17574 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17575 varloc = NOTE_VAR_LOCATION (loc_note);
17576 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17577 if (cur_descr == NULL)
17579 padsize += bitsize;
17580 continue;
17583 /* Check that cur_descr either doesn't use
17584 DW_OP_*piece operations, or their sum is equal
17585 to bitsize. Otherwise we can't embed it. */
17586 for (tail = &cur_descr; *tail != NULL;
17587 tail = &(*tail)->dw_loc_next)
17588 if ((*tail)->dw_loc_opc == DW_OP_piece)
17590 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17591 * BITS_PER_UNIT;
17592 last = *tail;
17594 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17596 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17597 last = *tail;
17600 if (last != NULL && opsize != bitsize)
17602 padsize += bitsize;
17603 /* Discard the current piece of the descriptor and release any
17604 addr_table entries it uses. */
17605 remove_loc_list_addr_table_entries (cur_descr);
17606 continue;
17609 /* If there is a hole, add DW_OP_*piece after empty DWARF
17610 expression, which means that those bits are optimized out. */
17611 if (padsize)
17613 if (padsize > decl_size)
17615 remove_loc_list_addr_table_entries (cur_descr);
17616 goto discard_descr;
17618 decl_size -= padsize;
17619 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17620 if (*descr_tail == NULL)
17622 remove_loc_list_addr_table_entries (cur_descr);
17623 goto discard_descr;
17625 descr_tail = &(*descr_tail)->dw_loc_next;
17626 padsize = 0;
17628 *descr_tail = cur_descr;
17629 descr_tail = tail;
17630 if (bitsize > decl_size)
17631 goto discard_descr;
17632 decl_size -= bitsize;
17633 if (last == NULL)
17635 HOST_WIDE_INT offset = 0;
17636 if (GET_CODE (varloc) == VAR_LOCATION
17637 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17639 varloc = PAT_VAR_LOCATION_LOC (varloc);
17640 if (GET_CODE (varloc) == EXPR_LIST)
17641 varloc = XEXP (varloc, 0);
17645 if (GET_CODE (varloc) == CONST
17646 || GET_CODE (varloc) == SIGN_EXTEND
17647 || GET_CODE (varloc) == ZERO_EXTEND)
17648 varloc = XEXP (varloc, 0);
17649 else if (GET_CODE (varloc) == SUBREG)
17650 varloc = SUBREG_REG (varloc);
17651 else
17652 break;
17654 while (1);
17655 /* DW_OP_bit_size offset should be zero for register
17656 or implicit location descriptions and empty location
17657 descriptions, but for memory addresses needs big endian
17658 adjustment. */
17659 if (MEM_P (varloc))
17661 unsigned HOST_WIDE_INT memsize;
17662 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17663 goto discard_descr;
17664 memsize *= BITS_PER_UNIT;
17665 if (memsize != bitsize)
17667 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17668 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17669 goto discard_descr;
17670 if (memsize < bitsize)
17671 goto discard_descr;
17672 if (BITS_BIG_ENDIAN)
17673 offset = memsize - bitsize;
17677 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17678 if (*descr_tail == NULL)
17679 goto discard_descr;
17680 descr_tail = &(*descr_tail)->dw_loc_next;
17684 /* If there were any non-empty expressions, add padding till the end of
17685 the decl. */
17686 if (descr != NULL && decl_size != 0)
17688 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17689 if (*descr_tail == NULL)
17690 goto discard_descr;
17692 return descr;
17694 discard_descr:
17695 /* Discard the descriptor and release any addr_table entries it uses. */
17696 remove_loc_list_addr_table_entries (descr);
17697 return NULL;
17700 /* Return the dwarf representation of the location list LOC_LIST of
17701 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17702 function. */
17704 static dw_loc_list_ref
17705 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17707 const char *endname, *secname;
17708 var_loc_view endview;
17709 rtx varloc;
17710 enum var_init_status initialized;
17711 struct var_loc_node *node;
17712 dw_loc_descr_ref descr;
17713 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17714 dw_loc_list_ref list = NULL;
17715 dw_loc_list_ref *listp = &list;
17717 /* Now that we know what section we are using for a base,
17718 actually construct the list of locations.
17719 The first location information is what is passed to the
17720 function that creates the location list, and the remaining
17721 locations just get added on to that list.
17722 Note that we only know the start address for a location
17723 (IE location changes), so to build the range, we use
17724 the range [current location start, next location start].
17725 This means we have to special case the last node, and generate
17726 a range of [last location start, end of function label]. */
17728 if (cfun && crtl->has_bb_partition)
17730 bool save_in_cold_section_p = in_cold_section_p;
17731 in_cold_section_p = first_function_block_is_cold;
17732 if (loc_list->last_before_switch == NULL)
17733 in_cold_section_p = !in_cold_section_p;
17734 secname = secname_for_decl (decl);
17735 in_cold_section_p = save_in_cold_section_p;
17737 else
17738 secname = secname_for_decl (decl);
17740 for (node = loc_list->first; node; node = node->next)
17742 bool range_across_switch = false;
17743 if (GET_CODE (node->loc) == EXPR_LIST
17744 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17746 if (GET_CODE (node->loc) == EXPR_LIST)
17748 descr = NULL;
17749 /* This requires DW_OP_{,bit_}piece, which is not usable
17750 inside DWARF expressions. */
17751 if (want_address == 2)
17752 descr = dw_sra_loc_expr (decl, node->loc);
17754 else
17756 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17757 varloc = NOTE_VAR_LOCATION (node->loc);
17758 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17760 if (descr)
17762 /* If section switch happens in between node->label
17763 and node->next->label (or end of function) and
17764 we can't emit it as a single entry list,
17765 emit two ranges, first one ending at the end
17766 of first partition and second one starting at the
17767 beginning of second partition. */
17768 if (node == loc_list->last_before_switch
17769 && (node != loc_list->first || loc_list->first->next
17770 /* If we are to emit a view number, we will emit
17771 a loclist rather than a single location
17772 expression for the entire function (see
17773 loc_list_has_views), so we have to split the
17774 range that straddles across partitions. */
17775 || !ZERO_VIEW_P (node->view))
17776 && current_function_decl)
17778 endname = cfun->fde->dw_fde_end;
17779 endview = 0;
17780 range_across_switch = true;
17782 /* The variable has a location between NODE->LABEL and
17783 NODE->NEXT->LABEL. */
17784 else if (node->next)
17785 endname = node->next->label, endview = node->next->view;
17786 /* If the variable has a location at the last label
17787 it keeps its location until the end of function. */
17788 else if (!current_function_decl)
17789 endname = text_end_label, endview = 0;
17790 else
17792 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17793 current_function_funcdef_no);
17794 endname = ggc_strdup (label_id);
17795 endview = 0;
17798 *listp = new_loc_list (descr, node->label, node->view,
17799 endname, endview, secname);
17800 if (TREE_CODE (decl) == PARM_DECL
17801 && node == loc_list->first
17802 && NOTE_P (node->loc)
17803 && strcmp (node->label, endname) == 0)
17804 (*listp)->force = true;
17805 listp = &(*listp)->dw_loc_next;
17809 if (cfun
17810 && crtl->has_bb_partition
17811 && node == loc_list->last_before_switch)
17813 bool save_in_cold_section_p = in_cold_section_p;
17814 in_cold_section_p = !first_function_block_is_cold;
17815 secname = secname_for_decl (decl);
17816 in_cold_section_p = save_in_cold_section_p;
17819 if (range_across_switch)
17821 if (GET_CODE (node->loc) == EXPR_LIST)
17822 descr = dw_sra_loc_expr (decl, node->loc);
17823 else
17825 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17826 varloc = NOTE_VAR_LOCATION (node->loc);
17827 descr = dw_loc_list_1 (decl, varloc, want_address,
17828 initialized);
17830 gcc_assert (descr);
17831 /* The variable has a location between NODE->LABEL and
17832 NODE->NEXT->LABEL. */
17833 if (node->next)
17834 endname = node->next->label, endview = node->next->view;
17835 else
17836 endname = cfun->fde->dw_fde_second_end, endview = 0;
17837 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17838 endname, endview, secname);
17839 listp = &(*listp)->dw_loc_next;
17843 /* Try to avoid the overhead of a location list emitting a location
17844 expression instead, but only if we didn't have more than one
17845 location entry in the first place. If some entries were not
17846 representable, we don't want to pretend a single entry that was
17847 applies to the entire scope in which the variable is
17848 available. */
17849 if (list && loc_list->first->next)
17850 gen_llsym (list);
17851 else
17852 maybe_gen_llsym (list);
17854 return list;
17857 /* Return if the loc_list has only single element and thus can be represented
17858 as location description. */
17860 static bool
17861 single_element_loc_list_p (dw_loc_list_ref list)
17863 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17864 return !list->ll_symbol;
17867 /* Duplicate a single element of location list. */
17869 static inline dw_loc_descr_ref
17870 copy_loc_descr (dw_loc_descr_ref ref)
17872 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17873 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17874 return copy;
17877 /* To each location in list LIST append loc descr REF. */
17879 static void
17880 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17882 dw_loc_descr_ref copy;
17883 add_loc_descr (&list->expr, ref);
17884 list = list->dw_loc_next;
17885 while (list)
17887 copy = copy_loc_descr (ref);
17888 add_loc_descr (&list->expr, copy);
17889 while (copy->dw_loc_next)
17890 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17891 list = list->dw_loc_next;
17895 /* To each location in list LIST prepend loc descr REF. */
17897 static void
17898 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17900 dw_loc_descr_ref copy;
17901 dw_loc_descr_ref ref_end = list->expr;
17902 add_loc_descr (&ref, list->expr);
17903 list->expr = ref;
17904 list = list->dw_loc_next;
17905 while (list)
17907 dw_loc_descr_ref end = list->expr;
17908 list->expr = copy = copy_loc_descr (ref);
17909 while (copy->dw_loc_next != ref_end)
17910 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17911 copy->dw_loc_next = end;
17912 list = list->dw_loc_next;
17916 /* Given two lists RET and LIST
17917 produce location list that is result of adding expression in LIST
17918 to expression in RET on each position in program.
17919 Might be destructive on both RET and LIST.
17921 TODO: We handle only simple cases of RET or LIST having at most one
17922 element. General case would involve sorting the lists in program order
17923 and merging them that will need some additional work.
17924 Adding that will improve quality of debug info especially for SRA-ed
17925 structures. */
17927 static void
17928 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17930 if (!list)
17931 return;
17932 if (!*ret)
17934 *ret = list;
17935 return;
17937 if (!list->dw_loc_next)
17939 add_loc_descr_to_each (*ret, list->expr);
17940 return;
17942 if (!(*ret)->dw_loc_next)
17944 prepend_loc_descr_to_each (list, (*ret)->expr);
17945 *ret = list;
17946 return;
17948 expansion_failed (NULL_TREE, NULL_RTX,
17949 "Don't know how to merge two non-trivial"
17950 " location lists.\n");
17951 *ret = NULL;
17952 return;
17955 /* LOC is constant expression. Try a luck, look it up in constant
17956 pool and return its loc_descr of its address. */
17958 static dw_loc_descr_ref
17959 cst_pool_loc_descr (tree loc)
17961 /* Get an RTL for this, if something has been emitted. */
17962 rtx rtl = lookup_constant_def (loc);
17964 if (!rtl || !MEM_P (rtl))
17966 gcc_assert (!rtl);
17967 return 0;
17969 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17971 /* TODO: We might get more coverage if we was actually delaying expansion
17972 of all expressions till end of compilation when constant pools are fully
17973 populated. */
17974 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17976 expansion_failed (loc, NULL_RTX,
17977 "CST value in contant pool but not marked.");
17978 return 0;
17980 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17981 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17984 /* Return dw_loc_list representing address of addr_expr LOC
17985 by looking for inner INDIRECT_REF expression and turning
17986 it into simple arithmetics.
17988 See loc_list_from_tree for the meaning of CONTEXT. */
17990 static dw_loc_list_ref
17991 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17992 loc_descr_context *context)
17994 tree obj, offset;
17995 poly_int64 bitsize, bitpos, bytepos;
17996 machine_mode mode;
17997 int unsignedp, reversep, volatilep = 0;
17998 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18000 obj = get_inner_reference (TREE_OPERAND (loc, 0),
18001 &bitsize, &bitpos, &offset, &mode,
18002 &unsignedp, &reversep, &volatilep);
18003 STRIP_NOPS (obj);
18004 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
18006 expansion_failed (loc, NULL_RTX, "bitfield access");
18007 return 0;
18009 if (!INDIRECT_REF_P (obj))
18011 expansion_failed (obj,
18012 NULL_RTX, "no indirect ref in inner refrence");
18013 return 0;
18015 if (!offset && known_eq (bitpos, 0))
18016 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18017 context);
18018 else if (toplev
18019 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18020 && (dwarf_version >= 4 || !dwarf_strict))
18022 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18023 if (!list_ret)
18024 return 0;
18025 if (offset)
18027 /* Variable offset. */
18028 list_ret1 = loc_list_from_tree (offset, 0, context);
18029 if (list_ret1 == 0)
18030 return 0;
18031 add_loc_list (&list_ret, list_ret1);
18032 if (!list_ret)
18033 return 0;
18034 add_loc_descr_to_each (list_ret,
18035 new_loc_descr (DW_OP_plus, 0, 0));
18037 HOST_WIDE_INT value;
18038 if (bytepos.is_constant (&value) && value > 0)
18039 add_loc_descr_to_each (list_ret,
18040 new_loc_descr (DW_OP_plus_uconst, value, 0));
18041 else if (maybe_ne (bytepos, 0))
18042 loc_list_plus_const (list_ret, bytepos);
18043 add_loc_descr_to_each (list_ret,
18044 new_loc_descr (DW_OP_stack_value, 0, 0));
18046 return list_ret;
18049 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18050 all operations from LOC are nops, move to the last one. Insert in NOPS all
18051 operations that are skipped. */
18053 static void
18054 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18055 hash_set<dw_loc_descr_ref> &nops)
18057 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18059 nops.add (loc);
18060 loc = loc->dw_loc_next;
18064 /* Helper for loc_descr_without_nops: free the location description operation
18065 P. */
18067 bool
18068 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18070 ggc_free (loc);
18071 return true;
18074 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18075 finishes LOC. */
18077 static void
18078 loc_descr_without_nops (dw_loc_descr_ref &loc)
18080 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18081 return;
18083 /* Set of all DW_OP_nop operations we remove. */
18084 hash_set<dw_loc_descr_ref> nops;
18086 /* First, strip all prefix NOP operations in order to keep the head of the
18087 operations list. */
18088 loc_descr_to_next_no_nop (loc, nops);
18090 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18092 /* For control flow operations: strip "prefix" nops in destination
18093 labels. */
18094 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18095 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18096 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18097 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18099 /* Do the same for the operations that follow, then move to the next
18100 iteration. */
18101 if (cur->dw_loc_next != NULL)
18102 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18103 cur = cur->dw_loc_next;
18106 nops.traverse<void *, free_loc_descr> (NULL);
18110 struct dwarf_procedure_info;
18112 /* Helper structure for location descriptions generation. */
18113 struct loc_descr_context
18115 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18116 NULL_TREE if DW_OP_push_object_address in invalid for this location
18117 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18118 tree context_type;
18119 /* The ..._DECL node that should be translated as a
18120 DW_OP_push_object_address operation. */
18121 tree base_decl;
18122 /* Information about the DWARF procedure we are currently generating. NULL if
18123 we are not generating a DWARF procedure. */
18124 struct dwarf_procedure_info *dpi;
18125 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18126 by consumer. Used for DW_TAG_generic_subrange attributes. */
18127 bool placeholder_arg;
18128 /* True if PLACEHOLDER_EXPR has been seen. */
18129 bool placeholder_seen;
18130 /* True if strict preservation of signedness has been requested. */
18131 bool strict_signedness;
18134 /* DWARF procedures generation
18136 DWARF expressions (aka. location descriptions) are used to encode variable
18137 things such as sizes or offsets. Such computations can have redundant parts
18138 that can be factorized in order to reduce the size of the output debug
18139 information. This is the whole point of DWARF procedures.
18141 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18142 already factorized into functions ("size functions") in order to handle very
18143 big and complex types. Such functions are quite simple: they have integral
18144 arguments, they return an integral result and their body contains only a
18145 return statement with arithmetic expressions. This is the only kind of
18146 function we are interested in translating into DWARF procedures, here.
18148 DWARF expressions and DWARF procedure are executed using a stack, so we have
18149 to define some calling convention for them to interact. Let's say that:
18151 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18152 all arguments in reverse order (right-to-left) so that when the DWARF
18153 procedure execution starts, the first argument is the top of the stack.
18155 - Then, when returning, the DWARF procedure must have consumed all arguments
18156 on the stack, must have pushed the result and touched nothing else.
18158 - Each integral argument and the result are integral types can be hold in a
18159 single stack slot.
18161 - We call "frame offset" the number of stack slots that are "under DWARF
18162 procedure control": it includes the arguments slots, the temporaries and
18163 the result slot. Thus, it is equal to the number of arguments when the
18164 procedure execution starts and must be equal to one (the result) when it
18165 returns. */
18167 /* Helper structure used when generating operations for a DWARF procedure. */
18168 struct dwarf_procedure_info
18170 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18171 currently translated. */
18172 tree fndecl;
18173 /* The number of arguments FNDECL takes. */
18174 unsigned args_count;
18177 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18178 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18179 equate it to this DIE. */
18181 static dw_die_ref
18182 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18183 dw_die_ref parent_die)
18185 dw_die_ref dwarf_proc_die;
18187 if ((dwarf_version < 3 && dwarf_strict)
18188 || location == NULL)
18189 return NULL;
18191 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18192 if (fndecl)
18193 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18194 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18195 return dwarf_proc_die;
18198 /* Return whether TYPE is a supported type as a DWARF procedure argument
18199 type or return type (we handle only scalar types and pointer types that
18200 aren't wider than the DWARF expression evaluation stack). */
18202 static bool
18203 is_handled_procedure_type (tree type)
18205 return ((INTEGRAL_TYPE_P (type)
18206 || TREE_CODE (type) == OFFSET_TYPE
18207 || TREE_CODE (type) == POINTER_TYPE)
18208 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18211 /* Helper for resolve_args_picking: do the same but stop when coming across
18212 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18213 offset *before* evaluating the corresponding operation. */
18215 static bool
18216 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18217 struct dwarf_procedure_info *dpi,
18218 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18220 /* The "frame_offset" identifier is already used to name a macro... */
18221 unsigned frame_offset_ = initial_frame_offset;
18222 dw_loc_descr_ref l;
18224 for (l = loc; l != NULL;)
18226 bool existed;
18227 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18229 /* If we already met this node, there is nothing to compute anymore. */
18230 if (existed)
18232 /* Make sure that the stack size is consistent wherever the execution
18233 flow comes from. */
18234 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18235 break;
18237 l_frame_offset = frame_offset_;
18239 /* If needed, relocate the picking offset with respect to the frame
18240 offset. */
18241 if (l->frame_offset_rel)
18243 unsigned HOST_WIDE_INT off;
18244 switch (l->dw_loc_opc)
18246 case DW_OP_pick:
18247 off = l->dw_loc_oprnd1.v.val_unsigned;
18248 break;
18249 case DW_OP_dup:
18250 off = 0;
18251 break;
18252 case DW_OP_over:
18253 off = 1;
18254 break;
18255 default:
18256 gcc_unreachable ();
18258 /* frame_offset_ is the size of the current stack frame, including
18259 incoming arguments. Besides, the arguments are pushed
18260 right-to-left. Thus, in order to access the Nth argument from
18261 this operation node, the picking has to skip temporaries *plus*
18262 one stack slot per argument (0 for the first one, 1 for the second
18263 one, etc.).
18265 The targetted argument number (N) is already set as the operand,
18266 and the number of temporaries can be computed with:
18267 frame_offsets_ - dpi->args_count */
18268 off += frame_offset_ - dpi->args_count;
18270 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18271 if (off > 255)
18272 return false;
18274 if (off == 0)
18276 l->dw_loc_opc = DW_OP_dup;
18277 l->dw_loc_oprnd1.v.val_unsigned = 0;
18279 else if (off == 1)
18281 l->dw_loc_opc = DW_OP_over;
18282 l->dw_loc_oprnd1.v.val_unsigned = 0;
18284 else
18286 l->dw_loc_opc = DW_OP_pick;
18287 l->dw_loc_oprnd1.v.val_unsigned = off;
18291 /* Update frame_offset according to the effect the current operation has
18292 on the stack. */
18293 switch (l->dw_loc_opc)
18295 case DW_OP_deref:
18296 case DW_OP_swap:
18297 case DW_OP_rot:
18298 case DW_OP_abs:
18299 case DW_OP_neg:
18300 case DW_OP_not:
18301 case DW_OP_plus_uconst:
18302 case DW_OP_skip:
18303 case DW_OP_reg0:
18304 case DW_OP_reg1:
18305 case DW_OP_reg2:
18306 case DW_OP_reg3:
18307 case DW_OP_reg4:
18308 case DW_OP_reg5:
18309 case DW_OP_reg6:
18310 case DW_OP_reg7:
18311 case DW_OP_reg8:
18312 case DW_OP_reg9:
18313 case DW_OP_reg10:
18314 case DW_OP_reg11:
18315 case DW_OP_reg12:
18316 case DW_OP_reg13:
18317 case DW_OP_reg14:
18318 case DW_OP_reg15:
18319 case DW_OP_reg16:
18320 case DW_OP_reg17:
18321 case DW_OP_reg18:
18322 case DW_OP_reg19:
18323 case DW_OP_reg20:
18324 case DW_OP_reg21:
18325 case DW_OP_reg22:
18326 case DW_OP_reg23:
18327 case DW_OP_reg24:
18328 case DW_OP_reg25:
18329 case DW_OP_reg26:
18330 case DW_OP_reg27:
18331 case DW_OP_reg28:
18332 case DW_OP_reg29:
18333 case DW_OP_reg30:
18334 case DW_OP_reg31:
18335 case DW_OP_bregx:
18336 case DW_OP_piece:
18337 case DW_OP_deref_size:
18338 case DW_OP_nop:
18339 case DW_OP_bit_piece:
18340 case DW_OP_implicit_value:
18341 case DW_OP_stack_value:
18342 case DW_OP_deref_type:
18343 case DW_OP_convert:
18344 case DW_OP_reinterpret:
18345 case DW_OP_GNU_deref_type:
18346 case DW_OP_GNU_convert:
18347 case DW_OP_GNU_reinterpret:
18348 break;
18350 case DW_OP_addr:
18351 case DW_OP_const1u:
18352 case DW_OP_const1s:
18353 case DW_OP_const2u:
18354 case DW_OP_const2s:
18355 case DW_OP_const4u:
18356 case DW_OP_const4s:
18357 case DW_OP_const8u:
18358 case DW_OP_const8s:
18359 case DW_OP_constu:
18360 case DW_OP_consts:
18361 case DW_OP_dup:
18362 case DW_OP_over:
18363 case DW_OP_pick:
18364 case DW_OP_lit0:
18365 case DW_OP_lit1:
18366 case DW_OP_lit2:
18367 case DW_OP_lit3:
18368 case DW_OP_lit4:
18369 case DW_OP_lit5:
18370 case DW_OP_lit6:
18371 case DW_OP_lit7:
18372 case DW_OP_lit8:
18373 case DW_OP_lit9:
18374 case DW_OP_lit10:
18375 case DW_OP_lit11:
18376 case DW_OP_lit12:
18377 case DW_OP_lit13:
18378 case DW_OP_lit14:
18379 case DW_OP_lit15:
18380 case DW_OP_lit16:
18381 case DW_OP_lit17:
18382 case DW_OP_lit18:
18383 case DW_OP_lit19:
18384 case DW_OP_lit20:
18385 case DW_OP_lit21:
18386 case DW_OP_lit22:
18387 case DW_OP_lit23:
18388 case DW_OP_lit24:
18389 case DW_OP_lit25:
18390 case DW_OP_lit26:
18391 case DW_OP_lit27:
18392 case DW_OP_lit28:
18393 case DW_OP_lit29:
18394 case DW_OP_lit30:
18395 case DW_OP_lit31:
18396 case DW_OP_breg0:
18397 case DW_OP_breg1:
18398 case DW_OP_breg2:
18399 case DW_OP_breg3:
18400 case DW_OP_breg4:
18401 case DW_OP_breg5:
18402 case DW_OP_breg6:
18403 case DW_OP_breg7:
18404 case DW_OP_breg8:
18405 case DW_OP_breg9:
18406 case DW_OP_breg10:
18407 case DW_OP_breg11:
18408 case DW_OP_breg12:
18409 case DW_OP_breg13:
18410 case DW_OP_breg14:
18411 case DW_OP_breg15:
18412 case DW_OP_breg16:
18413 case DW_OP_breg17:
18414 case DW_OP_breg18:
18415 case DW_OP_breg19:
18416 case DW_OP_breg20:
18417 case DW_OP_breg21:
18418 case DW_OP_breg22:
18419 case DW_OP_breg23:
18420 case DW_OP_breg24:
18421 case DW_OP_breg25:
18422 case DW_OP_breg26:
18423 case DW_OP_breg27:
18424 case DW_OP_breg28:
18425 case DW_OP_breg29:
18426 case DW_OP_breg30:
18427 case DW_OP_breg31:
18428 case DW_OP_fbreg:
18429 case DW_OP_push_object_address:
18430 case DW_OP_call_frame_cfa:
18431 case DW_OP_GNU_variable_value:
18432 case DW_OP_GNU_addr_index:
18433 case DW_OP_GNU_const_index:
18434 ++frame_offset_;
18435 break;
18437 case DW_OP_drop:
18438 case DW_OP_xderef:
18439 case DW_OP_and:
18440 case DW_OP_div:
18441 case DW_OP_minus:
18442 case DW_OP_mod:
18443 case DW_OP_mul:
18444 case DW_OP_or:
18445 case DW_OP_plus:
18446 case DW_OP_shl:
18447 case DW_OP_shr:
18448 case DW_OP_shra:
18449 case DW_OP_xor:
18450 case DW_OP_bra:
18451 case DW_OP_eq:
18452 case DW_OP_ge:
18453 case DW_OP_gt:
18454 case DW_OP_le:
18455 case DW_OP_lt:
18456 case DW_OP_ne:
18457 case DW_OP_regx:
18458 case DW_OP_xderef_size:
18459 --frame_offset_;
18460 break;
18462 case DW_OP_call2:
18463 case DW_OP_call4:
18464 case DW_OP_call_ref:
18466 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18467 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18469 if (stack_usage == NULL)
18470 return false;
18471 frame_offset_ += *stack_usage;
18472 break;
18475 case DW_OP_implicit_pointer:
18476 case DW_OP_entry_value:
18477 case DW_OP_const_type:
18478 case DW_OP_regval_type:
18479 case DW_OP_form_tls_address:
18480 case DW_OP_GNU_push_tls_address:
18481 case DW_OP_GNU_uninit:
18482 case DW_OP_GNU_encoded_addr:
18483 case DW_OP_GNU_implicit_pointer:
18484 case DW_OP_GNU_entry_value:
18485 case DW_OP_GNU_const_type:
18486 case DW_OP_GNU_regval_type:
18487 case DW_OP_GNU_parameter_ref:
18488 /* loc_list_from_tree will probably not output these operations for
18489 size functions, so assume they will not appear here. */
18490 /* Fall through... */
18492 default:
18493 gcc_unreachable ();
18496 /* Now, follow the control flow (except subroutine calls). */
18497 switch (l->dw_loc_opc)
18499 case DW_OP_bra:
18500 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18501 frame_offsets))
18502 return false;
18503 /* Fall through. */
18505 case DW_OP_skip:
18506 l = l->dw_loc_oprnd1.v.val_loc;
18507 break;
18509 case DW_OP_stack_value:
18510 return true;
18512 default:
18513 l = l->dw_loc_next;
18514 break;
18518 return true;
18521 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18522 operations) in order to resolve the operand of DW_OP_pick operations that
18523 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18524 offset *before* LOC is executed. Return if all relocations were
18525 successful. */
18527 static bool
18528 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18529 struct dwarf_procedure_info *dpi)
18531 /* Associate to all visited operations the frame offset *before* evaluating
18532 this operation. */
18533 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18535 return
18536 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18539 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18540 Return NULL if it is not possible. */
18542 static dw_die_ref
18543 function_to_dwarf_procedure (tree fndecl)
18545 struct dwarf_procedure_info dpi;
18546 struct loc_descr_context ctx = {
18547 NULL_TREE, /* context_type */
18548 NULL_TREE, /* base_decl */
18549 &dpi, /* dpi */
18550 false, /* placeholder_arg */
18551 false, /* placeholder_seen */
18552 true /* strict_signedness */
18554 dw_die_ref dwarf_proc_die;
18555 tree tree_body = DECL_SAVED_TREE (fndecl);
18556 dw_loc_descr_ref loc_body, epilogue;
18558 tree cursor;
18559 unsigned i;
18561 /* Do not generate multiple DWARF procedures for the same function
18562 declaration. */
18563 dwarf_proc_die = lookup_decl_die (fndecl);
18564 if (dwarf_proc_die != NULL)
18565 return dwarf_proc_die;
18567 /* DWARF procedures are available starting with the DWARFv3 standard. */
18568 if (dwarf_version < 3 && dwarf_strict)
18569 return NULL;
18571 /* We handle only functions for which we still have a body, that return a
18572 supported type and that takes arguments with supported types. Note that
18573 there is no point translating functions that return nothing. */
18574 if (tree_body == NULL_TREE
18575 || DECL_RESULT (fndecl) == NULL_TREE
18576 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18577 return NULL;
18579 for (cursor = DECL_ARGUMENTS (fndecl);
18580 cursor != NULL_TREE;
18581 cursor = TREE_CHAIN (cursor))
18582 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18583 return NULL;
18585 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18586 if (TREE_CODE (tree_body) != RETURN_EXPR)
18587 return NULL;
18588 tree_body = TREE_OPERAND (tree_body, 0);
18589 if (TREE_CODE (tree_body) != MODIFY_EXPR
18590 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18591 return NULL;
18592 tree_body = TREE_OPERAND (tree_body, 1);
18594 /* Try to translate the body expression itself. Note that this will probably
18595 cause an infinite recursion if its call graph has a cycle. This is very
18596 unlikely for size functions, however, so don't bother with such things at
18597 the moment. */
18598 dpi.fndecl = fndecl;
18599 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18600 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18601 if (!loc_body)
18602 return NULL;
18604 /* After evaluating all operands in "loc_body", we should still have on the
18605 stack all arguments plus the desired function result (top of the stack).
18606 Generate code in order to keep only the result in our stack frame. */
18607 epilogue = NULL;
18608 for (i = 0; i < dpi.args_count; ++i)
18610 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18611 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18612 op_couple->dw_loc_next->dw_loc_next = epilogue;
18613 epilogue = op_couple;
18615 add_loc_descr (&loc_body, epilogue);
18616 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18617 return NULL;
18619 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18620 because they are considered useful. Now there is an epilogue, they are
18621 not anymore, so give it another try. */
18622 loc_descr_without_nops (loc_body);
18624 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18625 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18626 though, given that size functions do not come from source, so they should
18627 not have a dedicated DW_TAG_subprogram DIE. */
18628 dwarf_proc_die
18629 = new_dwarf_proc_die (loc_body, fndecl,
18630 get_context_die (DECL_CONTEXT (fndecl)));
18632 /* The called DWARF procedure consumes one stack slot per argument and
18633 returns one stack slot. */
18634 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18636 return dwarf_proc_die;
18639 /* Helper function for loc_list_from_tree. Perform OP binary op,
18640 but after converting arguments to type_die, afterwards convert
18641 back to unsigned. */
18643 static dw_loc_list_ref
18644 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18645 dw_die_ref type_die, scalar_int_mode mode,
18646 struct loc_descr_context *context)
18648 dw_loc_list_ref op0, op1;
18649 dw_loc_descr_ref cvt, binop;
18651 if (type_die == NULL)
18652 return NULL;
18654 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18655 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18656 if (op0 == NULL || op1 == NULL)
18657 return NULL;
18659 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18660 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18661 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18662 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18663 add_loc_descr_to_each (op0, cvt);
18665 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18666 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18667 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18668 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18669 add_loc_descr_to_each (op1, cvt);
18671 add_loc_list (&op0, op1);
18672 if (op0 == NULL)
18673 return NULL;
18675 binop = new_loc_descr (op, 0, 0);
18676 convert_descriptor_to_mode (mode, binop);
18677 add_loc_descr_to_each (op0, binop);
18679 return op0;
18682 /* Generate Dwarf location list representing LOC.
18683 If WANT_ADDRESS is false, expression computing LOC will be computed
18684 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18685 if WANT_ADDRESS is 2, expression computing address useable in location
18686 will be returned (i.e. DW_OP_reg can be used
18687 to refer to register values).
18689 CONTEXT provides information to customize the location descriptions
18690 generation. Its context_type field specifies what type is implicitly
18691 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18692 will not be generated.
18694 Its DPI field determines whether we are generating a DWARF expression for a
18695 DWARF procedure, so PARM_DECL references are processed specifically.
18697 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18698 and dpi fields were null. */
18700 static dw_loc_list_ref
18701 loc_list_from_tree_1 (tree loc, int want_address,
18702 struct loc_descr_context *context)
18704 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18705 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18706 int have_address = 0;
18707 enum dwarf_location_atom op;
18709 /* ??? Most of the time we do not take proper care for sign/zero
18710 extending the values properly. Hopefully this won't be a real
18711 problem... */
18713 if (context != NULL
18714 && context->base_decl == loc
18715 && want_address == 0)
18717 if (dwarf_version >= 3 || !dwarf_strict)
18718 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18719 NULL, 0, NULL, 0, NULL);
18720 else
18721 return NULL;
18724 switch (TREE_CODE (loc))
18726 case ERROR_MARK:
18727 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18728 return 0;
18730 case PLACEHOLDER_EXPR:
18731 /* This case involves extracting fields from an object to determine the
18732 position of other fields. It is supposed to appear only as the first
18733 operand of COMPONENT_REF nodes and to reference precisely the type
18734 that the context allows or its enclosing type. */
18735 if (context != NULL
18736 && (TREE_TYPE (loc) == context->context_type
18737 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18738 && want_address >= 1)
18740 if (dwarf_version >= 3 || !dwarf_strict)
18742 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18743 have_address = 1;
18744 break;
18746 else
18747 return NULL;
18749 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18750 the single argument passed by consumer. */
18751 else if (context != NULL
18752 && context->placeholder_arg
18753 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18754 && want_address == 0)
18756 ret = new_loc_descr (DW_OP_pick, 0, 0);
18757 ret->frame_offset_rel = 1;
18758 context->placeholder_seen = true;
18759 break;
18761 else
18762 expansion_failed (loc, NULL_RTX,
18763 "PLACEHOLDER_EXPR for an unexpected type");
18764 break;
18766 case CALL_EXPR:
18768 tree callee = get_callee_fndecl (loc);
18769 dw_die_ref dwarf_proc;
18771 if (callee
18772 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18773 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18775 /* DWARF procedures are used for size functions, which are built
18776 when size expressions contain conditional constructs, so we
18777 request strict preservation of signedness for comparisons. */
18778 bool old_strict_signedness;
18779 if (context)
18781 old_strict_signedness = context->strict_signedness;
18782 context->strict_signedness = true;
18785 /* Evaluate arguments right-to-left so that the first argument
18786 will be the top-most one on the stack. */
18787 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18789 tree arg = CALL_EXPR_ARG (loc, i);
18790 ret1 = loc_descriptor_from_tree (arg, 0, context);
18791 if (!ret1)
18793 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18794 return NULL;
18796 add_loc_descr (&ret, ret1);
18799 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18800 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18801 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18802 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18803 add_loc_descr (&ret, ret1);
18804 if (context)
18805 context->strict_signedness = old_strict_signedness;
18807 else
18808 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18809 break;
18812 case PREINCREMENT_EXPR:
18813 case PREDECREMENT_EXPR:
18814 case POSTINCREMENT_EXPR:
18815 case POSTDECREMENT_EXPR:
18816 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18817 /* There are no opcodes for these operations. */
18818 return 0;
18820 case ADDR_EXPR:
18821 /* If we already want an address, see if there is INDIRECT_REF inside
18822 e.g. for &this->field. */
18823 if (want_address)
18825 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18826 (loc, want_address == 2, context);
18827 if (list_ret)
18828 have_address = 1;
18829 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18830 && (ret = cst_pool_loc_descr (loc)))
18831 have_address = 1;
18833 /* Otherwise, process the argument and look for the address. */
18834 if (!list_ret && !ret)
18835 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18836 else
18838 if (want_address)
18839 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18840 return NULL;
18842 break;
18844 case VAR_DECL:
18845 if (DECL_THREAD_LOCAL_P (loc))
18847 rtx rtl;
18848 enum dwarf_location_atom tls_op;
18849 enum dtprel_bool dtprel = dtprel_false;
18851 if (targetm.have_tls)
18853 /* If this is not defined, we have no way to emit the
18854 data. */
18855 if (!targetm.asm_out.output_dwarf_dtprel)
18856 return 0;
18858 /* The way DW_OP_GNU_push_tls_address is specified, we
18859 can only look up addresses of objects in the current
18860 module. We used DW_OP_addr as first op, but that's
18861 wrong, because DW_OP_addr is relocated by the debug
18862 info consumer, while DW_OP_GNU_push_tls_address
18863 operand shouldn't be. */
18864 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18865 return 0;
18866 dtprel = dtprel_true;
18867 /* We check for DWARF 5 here because gdb did not implement
18868 DW_OP_form_tls_address until after 7.12. */
18869 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18870 : DW_OP_GNU_push_tls_address);
18872 else
18874 if (!targetm.emutls.debug_form_tls_address
18875 || !(dwarf_version >= 3 || !dwarf_strict))
18876 return 0;
18877 /* We stuffed the control variable into the DECL_VALUE_EXPR
18878 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18879 no longer appear in gimple code. We used the control
18880 variable in specific so that we could pick it up here. */
18881 loc = DECL_VALUE_EXPR (loc);
18882 tls_op = DW_OP_form_tls_address;
18885 rtl = rtl_for_decl_location (loc);
18886 if (rtl == NULL_RTX)
18887 return 0;
18889 if (!MEM_P (rtl))
18890 return 0;
18891 rtl = XEXP (rtl, 0);
18892 if (! CONSTANT_P (rtl))
18893 return 0;
18895 ret = new_addr_loc_descr (rtl, dtprel);
18896 ret1 = new_loc_descr (tls_op, 0, 0);
18897 add_loc_descr (&ret, ret1);
18899 have_address = 1;
18900 break;
18902 /* FALLTHRU */
18904 case PARM_DECL:
18905 if (context != NULL && context->dpi != NULL
18906 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18908 /* We are generating code for a DWARF procedure and we want to access
18909 one of its arguments: find the appropriate argument offset and let
18910 the resolve_args_picking pass compute the offset that complies
18911 with the stack frame size. */
18912 unsigned i = 0;
18913 tree cursor;
18915 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18916 cursor != NULL_TREE && cursor != loc;
18917 cursor = TREE_CHAIN (cursor), ++i)
18919 /* If we are translating a DWARF procedure, all referenced parameters
18920 must belong to the current function. */
18921 gcc_assert (cursor != NULL_TREE);
18923 ret = new_loc_descr (DW_OP_pick, i, 0);
18924 ret->frame_offset_rel = 1;
18925 break;
18927 /* FALLTHRU */
18929 case RESULT_DECL:
18930 if (DECL_HAS_VALUE_EXPR_P (loc))
18932 tree value_expr = DECL_VALUE_EXPR (loc);
18934 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18935 to wait until they get an RTX in order to reference them. */
18936 if (early_dwarf
18937 && TREE_CODE (value_expr) == COMPONENT_REF
18938 && VAR_P (TREE_OPERAND (value_expr, 0))
18939 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18941 else
18942 return loc_list_from_tree_1 (value_expr, want_address, context);
18945 /* FALLTHRU */
18947 case FUNCTION_DECL:
18949 rtx rtl;
18950 var_loc_list *loc_list = lookup_decl_loc (loc);
18952 if (loc_list && loc_list->first)
18954 list_ret = dw_loc_list (loc_list, loc, want_address);
18955 have_address = want_address != 0;
18956 break;
18958 rtl = rtl_for_decl_location (loc);
18959 if (rtl == NULL_RTX)
18961 if (TREE_CODE (loc) != FUNCTION_DECL
18962 && early_dwarf
18963 && want_address != 1
18964 && ! DECL_IGNORED_P (loc)
18965 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18966 || POINTER_TYPE_P (TREE_TYPE (loc)))
18967 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18968 <= DWARF2_ADDR_SIZE))
18970 dw_die_ref ref = lookup_decl_die (loc);
18971 if (ref)
18973 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18974 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18975 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18976 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18978 else if (current_function_decl
18979 && DECL_CONTEXT (loc) == current_function_decl)
18981 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18982 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18983 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18985 break;
18987 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18988 return 0;
18990 else if (CONST_INT_P (rtl))
18992 HOST_WIDE_INT val = INTVAL (rtl);
18993 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18994 val &= GET_MODE_MASK (DECL_MODE (loc));
18995 ret = int_loc_descriptor (val);
18997 else if (GET_CODE (rtl) == CONST_STRING)
18999 expansion_failed (loc, NULL_RTX, "CONST_STRING");
19000 return 0;
19002 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
19003 ret = new_addr_loc_descr (rtl, dtprel_false);
19004 else
19006 machine_mode mode, mem_mode;
19008 /* Certain constructs can only be represented at top-level. */
19009 if (want_address == 2)
19011 ret = loc_descriptor (rtl, VOIDmode,
19012 VAR_INIT_STATUS_INITIALIZED);
19013 have_address = 1;
19015 else
19017 mode = GET_MODE (rtl);
19018 mem_mode = VOIDmode;
19019 if (MEM_P (rtl))
19021 mem_mode = mode;
19022 mode = get_address_mode (rtl);
19023 rtl = XEXP (rtl, 0);
19024 have_address = 1;
19026 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19027 VAR_INIT_STATUS_INITIALIZED);
19029 if (!ret)
19030 expansion_failed (loc, rtl,
19031 "failed to produce loc descriptor for rtl");
19034 break;
19036 case MEM_REF:
19037 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19039 have_address = 1;
19040 goto do_plus;
19042 /* Fallthru. */
19043 case INDIRECT_REF:
19044 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19045 have_address = 1;
19046 break;
19048 case TARGET_MEM_REF:
19049 case SSA_NAME:
19050 case DEBUG_EXPR_DECL:
19051 return NULL;
19053 case COMPOUND_EXPR:
19054 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19055 context);
19057 CASE_CONVERT:
19058 case VIEW_CONVERT_EXPR:
19059 case SAVE_EXPR:
19060 case MODIFY_EXPR:
19061 case NON_LVALUE_EXPR:
19062 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19063 context);
19065 case COMPONENT_REF:
19066 case BIT_FIELD_REF:
19067 case ARRAY_REF:
19068 case ARRAY_RANGE_REF:
19069 case REALPART_EXPR:
19070 case IMAGPART_EXPR:
19072 tree obj, offset;
19073 poly_int64 bitsize, bitpos, bytepos;
19074 machine_mode mode;
19075 int unsignedp, reversep, volatilep = 0;
19077 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19078 &unsignedp, &reversep, &volatilep);
19080 gcc_assert (obj != loc);
19082 list_ret = loc_list_from_tree_1 (obj,
19083 want_address == 2
19084 && known_eq (bitpos, 0)
19085 && !offset ? 2 : 1,
19086 context);
19087 /* TODO: We can extract value of the small expression via shifting even
19088 for nonzero bitpos. */
19089 if (list_ret == 0)
19090 return 0;
19091 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19092 || !multiple_p (bitsize, BITS_PER_UNIT))
19094 expansion_failed (loc, NULL_RTX,
19095 "bitfield access");
19096 return 0;
19099 if (offset != NULL_TREE)
19101 /* Variable offset. */
19102 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19103 if (list_ret1 == 0)
19104 return 0;
19105 add_loc_list (&list_ret, list_ret1);
19106 if (!list_ret)
19107 return 0;
19108 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19111 HOST_WIDE_INT value;
19112 if (bytepos.is_constant (&value) && value > 0)
19113 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19114 value, 0));
19115 else if (maybe_ne (bytepos, 0))
19116 loc_list_plus_const (list_ret, bytepos);
19118 have_address = 1;
19119 break;
19122 case INTEGER_CST:
19123 if ((want_address || !tree_fits_shwi_p (loc))
19124 && (ret = cst_pool_loc_descr (loc)))
19125 have_address = 1;
19126 else if (want_address == 2
19127 && tree_fits_shwi_p (loc)
19128 && (ret = address_of_int_loc_descriptor
19129 (int_size_in_bytes (TREE_TYPE (loc)),
19130 tree_to_shwi (loc))))
19131 have_address = 1;
19132 else if (tree_fits_shwi_p (loc))
19133 ret = int_loc_descriptor (tree_to_shwi (loc));
19134 else if (tree_fits_uhwi_p (loc))
19135 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19136 else
19138 expansion_failed (loc, NULL_RTX,
19139 "Integer operand is not host integer");
19140 return 0;
19142 break;
19144 case POLY_INT_CST:
19146 if (want_address)
19148 expansion_failed (loc, NULL_RTX,
19149 "constant address with a runtime component");
19150 return 0;
19152 poly_int64 value;
19153 if (!poly_int_tree_p (loc, &value))
19155 expansion_failed (loc, NULL_RTX, "constant too big");
19156 return 0;
19158 ret = int_loc_descriptor (value);
19160 break;
19162 case CONSTRUCTOR:
19163 case REAL_CST:
19164 case STRING_CST:
19165 case COMPLEX_CST:
19166 if ((ret = cst_pool_loc_descr (loc)))
19167 have_address = 1;
19168 else if (TREE_CODE (loc) == CONSTRUCTOR)
19170 tree type = TREE_TYPE (loc);
19171 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19172 unsigned HOST_WIDE_INT offset = 0;
19173 unsigned HOST_WIDE_INT cnt;
19174 constructor_elt *ce;
19176 if (TREE_CODE (type) == RECORD_TYPE)
19178 /* This is very limited, but it's enough to output
19179 pointers to member functions, as long as the
19180 referenced function is defined in the current
19181 translation unit. */
19182 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19184 tree val = ce->value;
19186 tree field = ce->index;
19188 if (val)
19189 STRIP_NOPS (val);
19191 if (!field || DECL_BIT_FIELD (field))
19193 expansion_failed (loc, NULL_RTX,
19194 "bitfield in record type constructor");
19195 size = offset = (unsigned HOST_WIDE_INT)-1;
19196 ret = NULL;
19197 break;
19200 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19201 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19202 gcc_assert (pos + fieldsize <= size);
19203 if (pos < offset)
19205 expansion_failed (loc, NULL_RTX,
19206 "out-of-order fields in record constructor");
19207 size = offset = (unsigned HOST_WIDE_INT)-1;
19208 ret = NULL;
19209 break;
19211 if (pos > offset)
19213 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19214 add_loc_descr (&ret, ret1);
19215 offset = pos;
19217 if (val && fieldsize != 0)
19219 ret1 = loc_descriptor_from_tree (val, want_address, context);
19220 if (!ret1)
19222 expansion_failed (loc, NULL_RTX,
19223 "unsupported expression in field");
19224 size = offset = (unsigned HOST_WIDE_INT)-1;
19225 ret = NULL;
19226 break;
19228 add_loc_descr (&ret, ret1);
19230 if (fieldsize)
19232 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19233 add_loc_descr (&ret, ret1);
19234 offset = pos + fieldsize;
19238 if (offset != size)
19240 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19241 add_loc_descr (&ret, ret1);
19242 offset = size;
19245 have_address = !!want_address;
19247 else
19248 expansion_failed (loc, NULL_RTX,
19249 "constructor of non-record type");
19251 else
19252 /* We can construct small constants here using int_loc_descriptor. */
19253 expansion_failed (loc, NULL_RTX,
19254 "constructor or constant not in constant pool");
19255 break;
19257 case TRUTH_AND_EXPR:
19258 case TRUTH_ANDIF_EXPR:
19259 case BIT_AND_EXPR:
19260 op = DW_OP_and;
19261 goto do_binop;
19263 case TRUTH_XOR_EXPR:
19264 case BIT_XOR_EXPR:
19265 op = DW_OP_xor;
19266 goto do_binop;
19268 case TRUTH_OR_EXPR:
19269 case TRUTH_ORIF_EXPR:
19270 case BIT_IOR_EXPR:
19271 op = DW_OP_or;
19272 goto do_binop;
19274 case EXACT_DIV_EXPR:
19275 case FLOOR_DIV_EXPR:
19276 case TRUNC_DIV_EXPR:
19277 /* Turn a divide by a power of 2 into a shift when possible. */
19278 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19279 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19281 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19282 if (log2 > 0)
19284 list_ret
19285 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19286 if (list_ret == 0)
19287 return 0;
19289 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19290 add_loc_descr_to_each (list_ret,
19291 new_loc_descr (DW_OP_shr, 0, 0));
19292 break;
19296 /* fall through */
19298 case CEIL_DIV_EXPR:
19299 case ROUND_DIV_EXPR:
19300 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19302 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19303 scalar_int_mode int_mode;
19305 if ((dwarf_strict && dwarf_version < 5)
19306 || !is_a <scalar_int_mode> (mode, &int_mode))
19307 return 0;
19309 /* We can use a signed divide if the sign bit is not set. */
19310 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19312 op = DW_OP_div;
19313 goto do_binop;
19316 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19317 base_type_for_mode (int_mode, 1),
19318 int_mode, context);
19319 break;
19321 op = DW_OP_div;
19322 goto do_binop;
19324 case MINUS_EXPR:
19325 op = DW_OP_minus;
19326 goto do_binop;
19328 case FLOOR_MOD_EXPR:
19329 case CEIL_MOD_EXPR:
19330 case ROUND_MOD_EXPR:
19331 case TRUNC_MOD_EXPR:
19332 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19334 op = DW_OP_mod;
19335 goto do_binop;
19337 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19338 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19339 if (list_ret == 0 || list_ret1 == 0)
19340 return 0;
19342 add_loc_list (&list_ret, list_ret1);
19343 if (list_ret == 0)
19344 return 0;
19345 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19346 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19347 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19348 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19349 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19350 break;
19352 case MULT_EXPR:
19353 op = DW_OP_mul;
19354 goto do_binop;
19356 case LSHIFT_EXPR:
19357 op = DW_OP_shl;
19358 goto do_binop;
19360 case RSHIFT_EXPR:
19361 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19362 goto do_binop;
19364 case POINTER_PLUS_EXPR:
19365 case PLUS_EXPR:
19366 do_plus:
19367 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19369 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19370 smarter to encode their opposite. The DW_OP_plus_uconst operation
19371 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19372 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19373 bytes, Y being the size of the operation that pushes the opposite
19374 of the addend. So let's choose the smallest representation. */
19375 const tree tree_addend = TREE_OPERAND (loc, 1);
19376 offset_int wi_addend;
19377 HOST_WIDE_INT shwi_addend;
19378 dw_loc_descr_ref loc_naddend;
19380 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19381 if (list_ret == 0)
19382 return 0;
19384 /* Try to get the literal to push. It is the opposite of the addend,
19385 so as we rely on wrapping during DWARF evaluation, first decode
19386 the literal as a "DWARF-sized" signed number. */
19387 wi_addend = wi::to_offset (tree_addend);
19388 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19389 shwi_addend = wi_addend.to_shwi ();
19390 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19391 ? int_loc_descriptor (-shwi_addend)
19392 : NULL;
19394 if (loc_naddend != NULL
19395 && ((unsigned) size_of_uleb128 (shwi_addend)
19396 > size_of_loc_descr (loc_naddend)))
19398 add_loc_descr_to_each (list_ret, loc_naddend);
19399 add_loc_descr_to_each (list_ret,
19400 new_loc_descr (DW_OP_minus, 0, 0));
19402 else
19404 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19406 loc_naddend = loc_cur;
19407 loc_cur = loc_cur->dw_loc_next;
19408 ggc_free (loc_naddend);
19410 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19412 break;
19415 op = DW_OP_plus;
19416 goto do_binop;
19418 case LE_EXPR:
19419 op = DW_OP_le;
19420 goto do_comp_binop;
19422 case GE_EXPR:
19423 op = DW_OP_ge;
19424 goto do_comp_binop;
19426 case LT_EXPR:
19427 op = DW_OP_lt;
19428 goto do_comp_binop;
19430 case GT_EXPR:
19431 op = DW_OP_gt;
19432 goto do_comp_binop;
19434 do_comp_binop:
19435 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19437 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19438 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19439 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19440 TREE_CODE (loc));
19441 break;
19443 else
19444 goto do_binop;
19446 case EQ_EXPR:
19447 op = DW_OP_eq;
19448 goto do_binop;
19450 case NE_EXPR:
19451 op = DW_OP_ne;
19452 goto do_binop;
19454 do_binop:
19455 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19456 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19457 if (list_ret == 0 || list_ret1 == 0)
19458 return 0;
19460 add_loc_list (&list_ret, list_ret1);
19461 if (list_ret == 0)
19462 return 0;
19463 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19464 break;
19466 case TRUTH_NOT_EXPR:
19467 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19468 if (list_ret == 0)
19469 return 0;
19471 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19472 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19473 break;
19475 case BIT_NOT_EXPR:
19476 op = DW_OP_not;
19477 goto do_unop;
19479 case ABS_EXPR:
19480 op = DW_OP_abs;
19481 goto do_unop;
19483 case NEGATE_EXPR:
19484 op = DW_OP_neg;
19485 goto do_unop;
19487 do_unop:
19488 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19489 if (list_ret == 0)
19490 return 0;
19492 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19493 break;
19495 case MIN_EXPR:
19496 case MAX_EXPR:
19498 const enum tree_code code =
19499 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19501 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19502 build2 (code, integer_type_node,
19503 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19504 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19507 /* fall through */
19509 case COND_EXPR:
19511 dw_loc_descr_ref lhs
19512 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19513 dw_loc_list_ref rhs
19514 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19515 dw_loc_descr_ref bra_node, jump_node, tmp;
19517 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19518 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19519 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19520 list_ret
19521 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19522 0, context);
19523 /* Likewise, swap the operands for a logically negated condition. */
19524 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19526 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19527 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19528 list_ret
19529 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19530 0, context);
19532 else
19533 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19534 if (list_ret == 0 || lhs == 0 || rhs == 0)
19535 return 0;
19537 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19538 add_loc_descr_to_each (list_ret, bra_node);
19540 add_loc_list (&list_ret, rhs);
19541 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19542 add_loc_descr_to_each (list_ret, jump_node);
19544 add_loc_descr_to_each (list_ret, lhs);
19545 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19546 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19548 /* ??? Need a node to point the skip at. Use a nop. */
19549 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19550 add_loc_descr_to_each (list_ret, tmp);
19551 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19552 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19554 break;
19556 case FIX_TRUNC_EXPR:
19557 return 0;
19559 case COMPOUND_LITERAL_EXPR:
19560 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19561 0, context);
19563 default:
19564 /* Leave front-end specific codes as simply unknown. This comes
19565 up, for instance, with the C STMT_EXPR. */
19566 if ((unsigned int) TREE_CODE (loc)
19567 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19569 expansion_failed (loc, NULL_RTX,
19570 "language specific tree node");
19571 return 0;
19574 /* Otherwise this is a generic code; we should just lists all of
19575 these explicitly. We forgot one. */
19576 if (flag_checking)
19577 gcc_unreachable ();
19579 /* In a release build, we want to degrade gracefully: better to
19580 generate incomplete debugging information than to crash. */
19581 return NULL;
19584 if (!ret && !list_ret)
19585 return 0;
19587 if (want_address == 2 && !have_address
19588 && (dwarf_version >= 4 || !dwarf_strict))
19590 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19592 expansion_failed (loc, NULL_RTX,
19593 "DWARF address size mismatch");
19594 return 0;
19596 if (ret)
19597 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19598 else
19599 add_loc_descr_to_each (list_ret,
19600 new_loc_descr (DW_OP_stack_value, 0, 0));
19601 have_address = 1;
19603 /* Show if we can't fill the request for an address. */
19604 if (want_address && !have_address)
19606 expansion_failed (loc, NULL_RTX,
19607 "Want address and only have value");
19608 return 0;
19611 gcc_assert (!ret || !list_ret);
19613 /* If we've got an address and don't want one, dereference. */
19614 if (!want_address && have_address)
19616 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19617 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19618 scalar_int_mode int_mode;
19619 dw_die_ref type_die;
19620 dw_loc_descr_ref deref;
19622 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19623 if (size > DWARF2_ADDR_SIZE || size == -1)
19625 expansion_failed (loc, NULL_RTX,
19626 "DWARF address size mismatch");
19627 return 0;
19630 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19631 else if (size == DWARF2_ADDR_SIZE)
19632 deref = new_loc_descr (DW_OP_deref, size, 0);
19634 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19635 extend the value, which is really OK for unsigned types only. */
19636 else if (!(context && context->strict_signedness)
19637 || TYPE_UNSIGNED (TREE_TYPE (loc))
19638 || (dwarf_strict && dwarf_version < 5)
19639 || !is_a <scalar_int_mode> (mode, &int_mode)
19640 || !(type_die = base_type_for_mode (mode, false)))
19641 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19643 /* Use DW_OP_deref_type for signed integral types if possible, but
19644 convert back to the generic type to avoid type mismatches later. */
19645 else
19647 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19648 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19649 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19650 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19651 add_loc_descr (&deref,
19652 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19655 if (ret)
19656 add_loc_descr (&ret, deref);
19657 else
19658 add_loc_descr_to_each (list_ret, deref);
19661 if (ret)
19662 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19664 return list_ret;
19667 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19668 expressions. */
19670 static dw_loc_list_ref
19671 loc_list_from_tree (tree loc, int want_address,
19672 struct loc_descr_context *context)
19674 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19676 for (dw_loc_list_ref loc_cur = result;
19677 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19678 loc_descr_without_nops (loc_cur->expr);
19679 return result;
19682 /* Same as above but return only single location expression. */
19683 static dw_loc_descr_ref
19684 loc_descriptor_from_tree (tree loc, int want_address,
19685 struct loc_descr_context *context)
19687 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19688 if (!ret)
19689 return NULL;
19690 if (ret->dw_loc_next)
19692 expansion_failed (loc, NULL_RTX,
19693 "Location list where only loc descriptor needed");
19694 return NULL;
19696 return ret->expr;
19699 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19700 pointer to the declared type for the relevant field variable, or return
19701 `integer_type_node' if the given node turns out to be an
19702 ERROR_MARK node. */
19704 static inline tree
19705 field_type (const_tree decl)
19707 tree type;
19709 if (TREE_CODE (decl) == ERROR_MARK)
19710 return integer_type_node;
19712 type = DECL_BIT_FIELD_TYPE (decl);
19713 if (type == NULL_TREE)
19714 type = TREE_TYPE (decl);
19716 return type;
19719 /* Given a pointer to a tree node, return the alignment in bits for
19720 it, or else return BITS_PER_WORD if the node actually turns out to
19721 be an ERROR_MARK node. */
19723 static inline unsigned
19724 simple_type_align_in_bits (const_tree type)
19726 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19729 static inline unsigned
19730 simple_decl_align_in_bits (const_tree decl)
19732 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19735 /* Return the result of rounding T up to ALIGN. */
19737 static inline offset_int
19738 round_up_to_align (const offset_int &t, unsigned int align)
19740 return wi::udiv_trunc (t + align - 1, align) * align;
19743 /* Helper structure for RECORD_TYPE processing. */
19744 struct vlr_context
19746 /* Root RECORD_TYPE. It is needed to generate data member location
19747 descriptions in variable-length records (VLR), but also to cope with
19748 variants, which are composed of nested structures multiplexed with
19749 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19750 function processing a FIELD_DECL, it is required to be non null. */
19751 tree struct_type;
19753 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19754 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19755 this variant part as part of the root record (in storage units). For
19756 regular records, it must be NULL_TREE. */
19757 tree variant_part_offset;
19760 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19761 addressed byte of the "containing object" for the given FIELD_DECL. If
19762 possible, return a native constant through CST_OFFSET (in which case NULL is
19763 returned); otherwise return a DWARF expression that computes the offset.
19765 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19766 that offset is, either because the argument turns out to be a pointer to an
19767 ERROR_MARK node, or because the offset expression is too complex for us.
19769 CTX is required: see the comment for VLR_CONTEXT. */
19771 static dw_loc_descr_ref
19772 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19773 HOST_WIDE_INT *cst_offset)
19775 tree tree_result;
19776 dw_loc_list_ref loc_result;
19778 *cst_offset = 0;
19780 if (TREE_CODE (decl) == ERROR_MARK)
19781 return NULL;
19782 else
19783 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19785 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19786 case. */
19787 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19788 return NULL;
19790 /* We used to handle only constant offsets in all cases. Now, we handle
19791 properly dynamic byte offsets only when PCC bitfield type doesn't
19792 matter. */
19793 if (PCC_BITFIELD_TYPE_MATTERS
19794 && DECL_BIT_FIELD_TYPE (decl)
19795 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19797 offset_int object_offset_in_bits;
19798 offset_int object_offset_in_bytes;
19799 offset_int bitpos_int;
19800 tree type;
19801 tree field_size_tree;
19802 offset_int deepest_bitpos;
19803 offset_int field_size_in_bits;
19804 unsigned int type_align_in_bits;
19805 unsigned int decl_align_in_bits;
19806 offset_int type_size_in_bits;
19808 bitpos_int = wi::to_offset (bit_position (decl));
19809 type = field_type (decl);
19810 type_size_in_bits = offset_int_type_size_in_bits (type);
19811 type_align_in_bits = simple_type_align_in_bits (type);
19813 field_size_tree = DECL_SIZE (decl);
19815 /* The size could be unspecified if there was an error, or for
19816 a flexible array member. */
19817 if (!field_size_tree)
19818 field_size_tree = bitsize_zero_node;
19820 /* If the size of the field is not constant, use the type size. */
19821 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19822 field_size_in_bits = wi::to_offset (field_size_tree);
19823 else
19824 field_size_in_bits = type_size_in_bits;
19826 decl_align_in_bits = simple_decl_align_in_bits (decl);
19828 /* The GCC front-end doesn't make any attempt to keep track of the
19829 starting bit offset (relative to the start of the containing
19830 structure type) of the hypothetical "containing object" for a
19831 bit-field. Thus, when computing the byte offset value for the
19832 start of the "containing object" of a bit-field, we must deduce
19833 this information on our own. This can be rather tricky to do in
19834 some cases. For example, handling the following structure type
19835 definition when compiling for an i386/i486 target (which only
19836 aligns long long's to 32-bit boundaries) can be very tricky:
19838 struct S { int field1; long long field2:31; };
19840 Fortunately, there is a simple rule-of-thumb which can be used
19841 in such cases. When compiling for an i386/i486, GCC will
19842 allocate 8 bytes for the structure shown above. It decides to
19843 do this based upon one simple rule for bit-field allocation.
19844 GCC allocates each "containing object" for each bit-field at
19845 the first (i.e. lowest addressed) legitimate alignment boundary
19846 (based upon the required minimum alignment for the declared
19847 type of the field) which it can possibly use, subject to the
19848 condition that there is still enough available space remaining
19849 in the containing object (when allocated at the selected point)
19850 to fully accommodate all of the bits of the bit-field itself.
19852 This simple rule makes it obvious why GCC allocates 8 bytes for
19853 each object of the structure type shown above. When looking
19854 for a place to allocate the "containing object" for `field2',
19855 the compiler simply tries to allocate a 64-bit "containing
19856 object" at each successive 32-bit boundary (starting at zero)
19857 until it finds a place to allocate that 64- bit field such that
19858 at least 31 contiguous (and previously unallocated) bits remain
19859 within that selected 64 bit field. (As it turns out, for the
19860 example above, the compiler finds it is OK to allocate the
19861 "containing object" 64-bit field at bit-offset zero within the
19862 structure type.)
19864 Here we attempt to work backwards from the limited set of facts
19865 we're given, and we try to deduce from those facts, where GCC
19866 must have believed that the containing object started (within
19867 the structure type). The value we deduce is then used (by the
19868 callers of this routine) to generate DW_AT_location and
19869 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19870 the case of DW_AT_location, regular fields as well). */
19872 /* Figure out the bit-distance from the start of the structure to
19873 the "deepest" bit of the bit-field. */
19874 deepest_bitpos = bitpos_int + field_size_in_bits;
19876 /* This is the tricky part. Use some fancy footwork to deduce
19877 where the lowest addressed bit of the containing object must
19878 be. */
19879 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19881 /* Round up to type_align by default. This works best for
19882 bitfields. */
19883 object_offset_in_bits
19884 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19886 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19888 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19890 /* Round up to decl_align instead. */
19891 object_offset_in_bits
19892 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19895 object_offset_in_bytes
19896 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19897 if (ctx->variant_part_offset == NULL_TREE)
19899 *cst_offset = object_offset_in_bytes.to_shwi ();
19900 return NULL;
19902 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19904 else
19905 tree_result = byte_position (decl);
19907 if (ctx->variant_part_offset != NULL_TREE)
19908 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19909 ctx->variant_part_offset, tree_result);
19911 /* If the byte offset is a constant, it's simplier to handle a native
19912 constant rather than a DWARF expression. */
19913 if (TREE_CODE (tree_result) == INTEGER_CST)
19915 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19916 return NULL;
19919 struct loc_descr_context loc_ctx = {
19920 ctx->struct_type, /* context_type */
19921 NULL_TREE, /* base_decl */
19922 NULL, /* dpi */
19923 false, /* placeholder_arg */
19924 false, /* placeholder_seen */
19925 false /* strict_signedness */
19927 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19929 /* We want a DWARF expression: abort if we only have a location list with
19930 multiple elements. */
19931 if (!loc_result || !single_element_loc_list_p (loc_result))
19932 return NULL;
19933 else
19934 return loc_result->expr;
19937 /* The following routines define various Dwarf attributes and any data
19938 associated with them. */
19940 /* Add a location description attribute value to a DIE.
19942 This emits location attributes suitable for whole variables and
19943 whole parameters. Note that the location attributes for struct fields are
19944 generated by the routine `data_member_location_attribute' below. */
19946 static inline void
19947 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19948 dw_loc_list_ref descr)
19950 bool check_no_locviews = true;
19951 if (descr == 0)
19952 return;
19953 if (single_element_loc_list_p (descr))
19954 add_AT_loc (die, attr_kind, descr->expr);
19955 else
19957 add_AT_loc_list (die, attr_kind, descr);
19958 gcc_assert (descr->ll_symbol);
19959 if (attr_kind == DW_AT_location && descr->vl_symbol
19960 && dwarf2out_locviews_in_attribute ())
19962 add_AT_view_list (die, DW_AT_GNU_locviews);
19963 check_no_locviews = false;
19967 if (check_no_locviews)
19968 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19971 /* Add DW_AT_accessibility attribute to DIE if needed. */
19973 static void
19974 add_accessibility_attribute (dw_die_ref die, tree decl)
19976 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19977 children, otherwise the default is DW_ACCESS_public. In DWARF2
19978 the default has always been DW_ACCESS_public. */
19979 if (TREE_PROTECTED (decl))
19980 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19981 else if (TREE_PRIVATE (decl))
19983 if (dwarf_version == 2
19984 || die->die_parent == NULL
19985 || die->die_parent->die_tag != DW_TAG_class_type)
19986 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19988 else if (dwarf_version > 2
19989 && die->die_parent
19990 && die->die_parent->die_tag == DW_TAG_class_type)
19991 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19994 /* Attach the specialized form of location attribute used for data members of
19995 struct and union types. In the special case of a FIELD_DECL node which
19996 represents a bit-field, the "offset" part of this special location
19997 descriptor must indicate the distance in bytes from the lowest-addressed
19998 byte of the containing struct or union type to the lowest-addressed byte of
19999 the "containing object" for the bit-field. (See the `field_byte_offset'
20000 function above).
20002 For any given bit-field, the "containing object" is a hypothetical object
20003 (of some integral or enum type) within which the given bit-field lives. The
20004 type of this hypothetical "containing object" is always the same as the
20005 declared type of the individual bit-field itself (for GCC anyway... the
20006 DWARF spec doesn't actually mandate this). Note that it is the size (in
20007 bytes) of the hypothetical "containing object" which will be given in the
20008 DW_AT_byte_size attribute for this bit-field. (See the
20009 `byte_size_attribute' function below.) It is also used when calculating the
20010 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
20011 function below.)
20013 CTX is required: see the comment for VLR_CONTEXT. */
20015 static void
20016 add_data_member_location_attribute (dw_die_ref die,
20017 tree decl,
20018 struct vlr_context *ctx)
20020 HOST_WIDE_INT offset;
20021 dw_loc_descr_ref loc_descr = 0;
20023 if (TREE_CODE (decl) == TREE_BINFO)
20025 /* We're working on the TAG_inheritance for a base class. */
20026 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20028 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20029 aren't at a fixed offset from all (sub)objects of the same
20030 type. We need to extract the appropriate offset from our
20031 vtable. The following dwarf expression means
20033 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20035 This is specific to the V3 ABI, of course. */
20037 dw_loc_descr_ref tmp;
20039 /* Make a copy of the object address. */
20040 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20041 add_loc_descr (&loc_descr, tmp);
20043 /* Extract the vtable address. */
20044 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20045 add_loc_descr (&loc_descr, tmp);
20047 /* Calculate the address of the offset. */
20048 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20049 gcc_assert (offset < 0);
20051 tmp = int_loc_descriptor (-offset);
20052 add_loc_descr (&loc_descr, tmp);
20053 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20054 add_loc_descr (&loc_descr, tmp);
20056 /* Extract the offset. */
20057 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20058 add_loc_descr (&loc_descr, tmp);
20060 /* Add it to the object address. */
20061 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20062 add_loc_descr (&loc_descr, tmp);
20064 else
20065 offset = tree_to_shwi (BINFO_OFFSET (decl));
20067 else
20069 loc_descr = field_byte_offset (decl, ctx, &offset);
20071 if (!loc_descr)
20074 /* If loc_descr is available, then we know the offset is dynamic. */
20075 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20077 loc_descr = NULL;
20078 offset = 0;
20081 /* Data member location evaluation starts with the base address on the
20082 stack. Compute the field offset and add it to this base address. */
20083 else
20084 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20087 if (!loc_descr)
20089 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20090 e.g. GDB only added support to it in November 2016. For DWARF5
20091 we need newer debug info consumers anyway. We might change this
20092 to dwarf_version >= 4 once most consumers catched up. */
20093 if (dwarf_version >= 5
20094 && TREE_CODE (decl) == FIELD_DECL
20095 && DECL_BIT_FIELD_TYPE (decl)
20096 && (ctx->variant_part_offset == NULL_TREE
20097 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20099 tree off = bit_position (decl);
20100 if (ctx->variant_part_offset)
20101 off = bit_from_pos (ctx->variant_part_offset, off);
20102 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20104 remove_AT (die, DW_AT_byte_size);
20105 remove_AT (die, DW_AT_bit_offset);
20106 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20107 return;
20110 if (dwarf_version > 2)
20112 /* Don't need to output a location expression, just the constant. */
20113 if (offset < 0)
20114 add_AT_int (die, DW_AT_data_member_location, offset);
20115 else
20116 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20117 return;
20119 else
20121 enum dwarf_location_atom op;
20123 /* The DWARF2 standard says that we should assume that the structure
20124 address is already on the stack, so we can specify a structure
20125 field address by using DW_OP_plus_uconst. */
20126 op = DW_OP_plus_uconst;
20127 loc_descr = new_loc_descr (op, offset, 0);
20131 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20134 /* Writes integer values to dw_vec_const array. */
20136 static void
20137 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20139 while (size != 0)
20141 *dest++ = val & 0xff;
20142 val >>= 8;
20143 --size;
20147 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20149 static HOST_WIDE_INT
20150 extract_int (const unsigned char *src, unsigned int size)
20152 HOST_WIDE_INT val = 0;
20154 src += size;
20155 while (size != 0)
20157 val <<= 8;
20158 val |= *--src & 0xff;
20159 --size;
20161 return val;
20164 /* Writes wide_int values to dw_vec_const array. */
20166 static void
20167 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20169 int i;
20171 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20173 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20174 return;
20177 /* We'd have to extend this code to support odd sizes. */
20178 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20180 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20182 if (WORDS_BIG_ENDIAN)
20183 for (i = n - 1; i >= 0; i--)
20185 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20186 dest += sizeof (HOST_WIDE_INT);
20188 else
20189 for (i = 0; i < n; i++)
20191 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20192 dest += sizeof (HOST_WIDE_INT);
20196 /* Writes floating point values to dw_vec_const array. */
20198 static unsigned
20199 insert_float (const_rtx rtl, unsigned char *array)
20201 long val[4];
20202 int i;
20203 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20205 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20207 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20208 if (GET_MODE_SIZE (mode) < 4)
20210 gcc_assert (GET_MODE_SIZE (mode) == 2);
20211 insert_int (val[0], 2, array);
20212 return 2;
20215 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20217 insert_int (val[i], 4, array);
20218 array += 4;
20220 return 4;
20223 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20224 does not have a "location" either in memory or in a register. These
20225 things can arise in GNU C when a constant is passed as an actual parameter
20226 to an inlined function. They can also arise in C++ where declared
20227 constants do not necessarily get memory "homes". */
20229 static bool
20230 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20232 scalar_mode int_mode;
20234 switch (GET_CODE (rtl))
20236 case CONST_INT:
20238 HOST_WIDE_INT val = INTVAL (rtl);
20240 if (val < 0)
20241 add_AT_int (die, DW_AT_const_value, val);
20242 else
20243 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20245 return true;
20247 case CONST_WIDE_INT:
20248 if (is_int_mode (mode, &int_mode)
20249 && (GET_MODE_PRECISION (int_mode)
20250 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20252 wide_int w = rtx_mode_t (rtl, int_mode);
20253 add_AT_wide (die, DW_AT_const_value, w);
20254 return true;
20256 return false;
20258 case CONST_DOUBLE:
20259 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20260 floating-point constant. A CONST_DOUBLE is used whenever the
20261 constant requires more than one word in order to be adequately
20262 represented. */
20263 if (TARGET_SUPPORTS_WIDE_INT == 0
20264 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20265 add_AT_double (die, DW_AT_const_value,
20266 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20267 else
20269 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20270 unsigned int length = GET_MODE_SIZE (mode);
20271 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20272 unsigned int elt_size = insert_float (rtl, array);
20274 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20275 array);
20277 return true;
20279 case CONST_VECTOR:
20281 unsigned int length;
20282 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20283 return false;
20285 machine_mode mode = GET_MODE (rtl);
20286 /* The combination of a length and byte elt_size doesn't extend
20287 naturally to boolean vectors, where several elements are packed
20288 into the same byte. */
20289 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20290 return false;
20292 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20293 unsigned char *array
20294 = ggc_vec_alloc<unsigned char> (length * elt_size);
20295 unsigned int i;
20296 unsigned char *p;
20297 machine_mode imode = GET_MODE_INNER (mode);
20299 switch (GET_MODE_CLASS (mode))
20301 case MODE_VECTOR_INT:
20302 for (i = 0, p = array; i < length; i++, p += elt_size)
20304 rtx elt = CONST_VECTOR_ELT (rtl, i);
20305 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20307 break;
20309 case MODE_VECTOR_FLOAT:
20310 for (i = 0, p = array; i < length; i++, p += elt_size)
20312 rtx elt = CONST_VECTOR_ELT (rtl, i);
20313 insert_float (elt, p);
20315 break;
20317 default:
20318 gcc_unreachable ();
20321 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20323 return true;
20325 case CONST_STRING:
20326 if (dwarf_version >= 4 || !dwarf_strict)
20328 dw_loc_descr_ref loc_result;
20329 resolve_one_addr (&rtl);
20330 rtl_addr:
20331 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20332 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20333 add_AT_loc (die, DW_AT_location, loc_result);
20334 vec_safe_push (used_rtx_array, rtl);
20335 return true;
20337 return false;
20339 case CONST:
20340 if (CONSTANT_P (XEXP (rtl, 0)))
20341 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20342 /* FALLTHROUGH */
20343 case SYMBOL_REF:
20344 if (!const_ok_for_output (rtl))
20345 return false;
20346 /* FALLTHROUGH */
20347 case LABEL_REF:
20348 if (dwarf_version >= 4 || !dwarf_strict)
20349 goto rtl_addr;
20350 return false;
20352 case PLUS:
20353 /* In cases where an inlined instance of an inline function is passed
20354 the address of an `auto' variable (which is local to the caller) we
20355 can get a situation where the DECL_RTL of the artificial local
20356 variable (for the inlining) which acts as a stand-in for the
20357 corresponding formal parameter (of the inline function) will look
20358 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20359 exactly a compile-time constant expression, but it isn't the address
20360 of the (artificial) local variable either. Rather, it represents the
20361 *value* which the artificial local variable always has during its
20362 lifetime. We currently have no way to represent such quasi-constant
20363 values in Dwarf, so for now we just punt and generate nothing. */
20364 return false;
20366 case HIGH:
20367 case CONST_FIXED:
20368 case MINUS:
20369 case SIGN_EXTEND:
20370 case ZERO_EXTEND:
20371 case CONST_POLY_INT:
20372 return false;
20374 case MEM:
20375 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20376 && MEM_READONLY_P (rtl)
20377 && GET_MODE (rtl) == BLKmode)
20379 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20380 return true;
20382 return false;
20384 default:
20385 /* No other kinds of rtx should be possible here. */
20386 gcc_unreachable ();
20390 /* Determine whether the evaluation of EXPR references any variables
20391 or functions which aren't otherwise used (and therefore may not be
20392 output). */
20393 static tree
20394 reference_to_unused (tree * tp, int * walk_subtrees,
20395 void * data ATTRIBUTE_UNUSED)
20397 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20398 *walk_subtrees = 0;
20400 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20401 && ! TREE_ASM_WRITTEN (*tp))
20402 return *tp;
20403 /* ??? The C++ FE emits debug information for using decls, so
20404 putting gcc_unreachable here falls over. See PR31899. For now
20405 be conservative. */
20406 else if (!symtab->global_info_ready && VAR_P (*tp))
20407 return *tp;
20408 else if (VAR_P (*tp))
20410 varpool_node *node = varpool_node::get (*tp);
20411 if (!node || !node->definition)
20412 return *tp;
20414 else if (TREE_CODE (*tp) == FUNCTION_DECL
20415 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20417 /* The call graph machinery must have finished analyzing,
20418 optimizing and gimplifying the CU by now.
20419 So if *TP has no call graph node associated
20420 to it, it means *TP will not be emitted. */
20421 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20422 return *tp;
20424 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20425 return *tp;
20427 return NULL_TREE;
20430 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20431 for use in a later add_const_value_attribute call. */
20433 static rtx
20434 rtl_for_decl_init (tree init, tree type)
20436 rtx rtl = NULL_RTX;
20438 STRIP_NOPS (init);
20440 /* If a variable is initialized with a string constant without embedded
20441 zeros, build CONST_STRING. */
20442 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20444 tree enttype = TREE_TYPE (type);
20445 tree domain = TYPE_DOMAIN (type);
20446 scalar_int_mode mode;
20448 if (is_int_mode (TYPE_MODE (enttype), &mode)
20449 && GET_MODE_SIZE (mode) == 1
20450 && domain
20451 && TYPE_MAX_VALUE (domain)
20452 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20453 && integer_zerop (TYPE_MIN_VALUE (domain))
20454 && compare_tree_int (TYPE_MAX_VALUE (domain),
20455 TREE_STRING_LENGTH (init) - 1) == 0
20456 && ((size_t) TREE_STRING_LENGTH (init)
20457 == strlen (TREE_STRING_POINTER (init)) + 1))
20459 rtl = gen_rtx_CONST_STRING (VOIDmode,
20460 ggc_strdup (TREE_STRING_POINTER (init)));
20461 rtl = gen_rtx_MEM (BLKmode, rtl);
20462 MEM_READONLY_P (rtl) = 1;
20465 /* Other aggregates, and complex values, could be represented using
20466 CONCAT: FIXME!
20467 If this changes, please adjust tree_add_const_value_attribute
20468 so that for early_dwarf it will for such initializers mangle referenced
20469 decls. */
20470 else if (AGGREGATE_TYPE_P (type)
20471 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20472 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20473 || TREE_CODE (type) == COMPLEX_TYPE)
20475 /* Vectors only work if their mode is supported by the target.
20476 FIXME: generic vectors ought to work too. */
20477 else if (TREE_CODE (type) == VECTOR_TYPE
20478 && !VECTOR_MODE_P (TYPE_MODE (type)))
20480 /* If the initializer is something that we know will expand into an
20481 immediate RTL constant, expand it now. We must be careful not to
20482 reference variables which won't be output. */
20483 else if (initializer_constant_valid_p (init, type)
20484 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20486 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20487 possible. */
20488 if (TREE_CODE (type) == VECTOR_TYPE)
20489 switch (TREE_CODE (init))
20491 case VECTOR_CST:
20492 break;
20493 case CONSTRUCTOR:
20494 if (TREE_CONSTANT (init))
20496 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20497 bool constant_p = true;
20498 tree value;
20499 unsigned HOST_WIDE_INT ix;
20501 /* Even when ctor is constant, it might contain non-*_CST
20502 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20503 belong into VECTOR_CST nodes. */
20504 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20505 if (!CONSTANT_CLASS_P (value))
20507 constant_p = false;
20508 break;
20511 if (constant_p)
20513 init = build_vector_from_ctor (type, elts);
20514 break;
20517 /* FALLTHRU */
20519 default:
20520 return NULL;
20523 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20525 /* If expand_expr returns a MEM, it wasn't immediate. */
20526 gcc_assert (!rtl || !MEM_P (rtl));
20529 return rtl;
20532 /* Generate RTL for the variable DECL to represent its location. */
20534 static rtx
20535 rtl_for_decl_location (tree decl)
20537 rtx rtl;
20539 /* Here we have to decide where we are going to say the parameter "lives"
20540 (as far as the debugger is concerned). We only have a couple of
20541 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20543 DECL_RTL normally indicates where the parameter lives during most of the
20544 activation of the function. If optimization is enabled however, this
20545 could be either NULL or else a pseudo-reg. Both of those cases indicate
20546 that the parameter doesn't really live anywhere (as far as the code
20547 generation parts of GCC are concerned) during most of the function's
20548 activation. That will happen (for example) if the parameter is never
20549 referenced within the function.
20551 We could just generate a location descriptor here for all non-NULL
20552 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20553 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20554 where DECL_RTL is NULL or is a pseudo-reg.
20556 Note however that we can only get away with using DECL_INCOMING_RTL as
20557 a backup substitute for DECL_RTL in certain limited cases. In cases
20558 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20559 we can be sure that the parameter was passed using the same type as it is
20560 declared to have within the function, and that its DECL_INCOMING_RTL
20561 points us to a place where a value of that type is passed.
20563 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20564 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20565 because in these cases DECL_INCOMING_RTL points us to a value of some
20566 type which is *different* from the type of the parameter itself. Thus,
20567 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20568 such cases, the debugger would end up (for example) trying to fetch a
20569 `float' from a place which actually contains the first part of a
20570 `double'. That would lead to really incorrect and confusing
20571 output at debug-time.
20573 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20574 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20575 are a couple of exceptions however. On little-endian machines we can
20576 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20577 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20578 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20579 when (on a little-endian machine) a non-prototyped function has a
20580 parameter declared to be of type `short' or `char'. In such cases,
20581 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20582 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20583 passed `int' value. If the debugger then uses that address to fetch
20584 a `short' or a `char' (on a little-endian machine) the result will be
20585 the correct data, so we allow for such exceptional cases below.
20587 Note that our goal here is to describe the place where the given formal
20588 parameter lives during most of the function's activation (i.e. between the
20589 end of the prologue and the start of the epilogue). We'll do that as best
20590 as we can. Note however that if the given formal parameter is modified
20591 sometime during the execution of the function, then a stack backtrace (at
20592 debug-time) will show the function as having been called with the *new*
20593 value rather than the value which was originally passed in. This happens
20594 rarely enough that it is not a major problem, but it *is* a problem, and
20595 I'd like to fix it.
20597 A future version of dwarf2out.cc may generate two additional attributes for
20598 any given DW_TAG_formal_parameter DIE which will describe the "passed
20599 type" and the "passed location" for the given formal parameter in addition
20600 to the attributes we now generate to indicate the "declared type" and the
20601 "active location" for each parameter. This additional set of attributes
20602 could be used by debuggers for stack backtraces. Separately, note that
20603 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20604 This happens (for example) for inlined-instances of inline function formal
20605 parameters which are never referenced. This really shouldn't be
20606 happening. All PARM_DECL nodes should get valid non-NULL
20607 DECL_INCOMING_RTL values. FIXME. */
20609 /* Use DECL_RTL as the "location" unless we find something better. */
20610 rtl = DECL_RTL_IF_SET (decl);
20612 /* When generating abstract instances, ignore everything except
20613 constants, symbols living in memory, and symbols living in
20614 fixed registers. */
20615 if (! reload_completed)
20617 if (rtl
20618 && (CONSTANT_P (rtl)
20619 || (MEM_P (rtl)
20620 && CONSTANT_P (XEXP (rtl, 0)))
20621 || (REG_P (rtl)
20622 && VAR_P (decl)
20623 && TREE_STATIC (decl))))
20625 rtl = targetm.delegitimize_address (rtl);
20626 return rtl;
20628 rtl = NULL_RTX;
20630 else if (TREE_CODE (decl) == PARM_DECL)
20632 if (rtl == NULL_RTX
20633 || is_pseudo_reg (rtl)
20634 || (MEM_P (rtl)
20635 && is_pseudo_reg (XEXP (rtl, 0))
20636 && DECL_INCOMING_RTL (decl)
20637 && MEM_P (DECL_INCOMING_RTL (decl))
20638 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20640 tree declared_type = TREE_TYPE (decl);
20641 tree passed_type = DECL_ARG_TYPE (decl);
20642 machine_mode dmode = TYPE_MODE (declared_type);
20643 machine_mode pmode = TYPE_MODE (passed_type);
20645 /* This decl represents a formal parameter which was optimized out.
20646 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20647 all cases where (rtl == NULL_RTX) just below. */
20648 if (dmode == pmode)
20649 rtl = DECL_INCOMING_RTL (decl);
20650 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20651 && SCALAR_INT_MODE_P (dmode)
20652 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20653 && DECL_INCOMING_RTL (decl))
20655 rtx inc = DECL_INCOMING_RTL (decl);
20656 if (REG_P (inc))
20657 rtl = inc;
20658 else if (MEM_P (inc))
20660 if (BYTES_BIG_ENDIAN)
20661 rtl = adjust_address_nv (inc, dmode,
20662 GET_MODE_SIZE (pmode)
20663 - GET_MODE_SIZE (dmode));
20664 else
20665 rtl = inc;
20670 /* If the parm was passed in registers, but lives on the stack, then
20671 make a big endian correction if the mode of the type of the
20672 parameter is not the same as the mode of the rtl. */
20673 /* ??? This is the same series of checks that are made in dbxout.cc before
20674 we reach the big endian correction code there. It isn't clear if all
20675 of these checks are necessary here, but keeping them all is the safe
20676 thing to do. */
20677 else if (MEM_P (rtl)
20678 && XEXP (rtl, 0) != const0_rtx
20679 && ! CONSTANT_P (XEXP (rtl, 0))
20680 /* Not passed in memory. */
20681 && !MEM_P (DECL_INCOMING_RTL (decl))
20682 /* Not passed by invisible reference. */
20683 && (!REG_P (XEXP (rtl, 0))
20684 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20685 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20686 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20687 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20688 #endif
20690 /* Big endian correction check. */
20691 && BYTES_BIG_ENDIAN
20692 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20693 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20694 UNITS_PER_WORD))
20696 machine_mode addr_mode = get_address_mode (rtl);
20697 poly_int64 offset = (UNITS_PER_WORD
20698 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20700 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20701 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20704 else if (VAR_P (decl)
20705 && rtl
20706 && MEM_P (rtl)
20707 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20709 machine_mode addr_mode = get_address_mode (rtl);
20710 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20711 GET_MODE (rtl));
20713 /* If a variable is declared "register" yet is smaller than
20714 a register, then if we store the variable to memory, it
20715 looks like we're storing a register-sized value, when in
20716 fact we are not. We need to adjust the offset of the
20717 storage location to reflect the actual value's bytes,
20718 else gdb will not be able to display it. */
20719 if (maybe_ne (offset, 0))
20720 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20721 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20724 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20725 and will have been substituted directly into all expressions that use it.
20726 C does not have such a concept, but C++ and other languages do. */
20727 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20728 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20730 if (rtl)
20731 rtl = targetm.delegitimize_address (rtl);
20733 /* If we don't look past the constant pool, we risk emitting a
20734 reference to a constant pool entry that isn't referenced from
20735 code, and thus is not emitted. */
20736 if (rtl)
20737 rtl = avoid_constant_pool_reference (rtl);
20739 /* Try harder to get a rtl. If this symbol ends up not being emitted
20740 in the current CU, resolve_addr will remove the expression referencing
20741 it. */
20742 if (rtl == NULL_RTX
20743 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20744 && VAR_P (decl)
20745 && !DECL_EXTERNAL (decl)
20746 && TREE_STATIC (decl)
20747 && DECL_NAME (decl)
20748 && !DECL_HARD_REGISTER (decl)
20749 && DECL_MODE (decl) != VOIDmode)
20751 rtl = make_decl_rtl_for_debug (decl);
20752 if (!MEM_P (rtl)
20753 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20754 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20755 rtl = NULL_RTX;
20758 return rtl;
20761 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20762 returned. If so, the decl for the COMMON block is returned, and the
20763 value is the offset into the common block for the symbol. */
20765 static tree
20766 fortran_common (tree decl, HOST_WIDE_INT *value)
20768 tree val_expr, cvar;
20769 machine_mode mode;
20770 poly_int64 bitsize, bitpos;
20771 tree offset;
20772 HOST_WIDE_INT cbitpos;
20773 int unsignedp, reversep, volatilep = 0;
20775 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20776 it does not have a value (the offset into the common area), or if it
20777 is thread local (as opposed to global) then it isn't common, and shouldn't
20778 be handled as such. */
20779 if (!VAR_P (decl)
20780 || !TREE_STATIC (decl)
20781 || !DECL_HAS_VALUE_EXPR_P (decl)
20782 || !is_fortran ())
20783 return NULL_TREE;
20785 val_expr = DECL_VALUE_EXPR (decl);
20786 if (TREE_CODE (val_expr) != COMPONENT_REF)
20787 return NULL_TREE;
20789 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20790 &unsignedp, &reversep, &volatilep);
20792 if (cvar == NULL_TREE
20793 || !VAR_P (cvar)
20794 || DECL_ARTIFICIAL (cvar)
20795 || !TREE_PUBLIC (cvar)
20796 /* We don't expect to have to cope with variable offsets,
20797 since at present all static data must have a constant size. */
20798 || !bitpos.is_constant (&cbitpos))
20799 return NULL_TREE;
20801 *value = 0;
20802 if (offset != NULL)
20804 if (!tree_fits_shwi_p (offset))
20805 return NULL_TREE;
20806 *value = tree_to_shwi (offset);
20808 if (cbitpos != 0)
20809 *value += cbitpos / BITS_PER_UNIT;
20811 return cvar;
20814 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20815 data attribute for a variable or a parameter. We generate the
20816 DW_AT_const_value attribute only in those cases where the given variable
20817 or parameter does not have a true "location" either in memory or in a
20818 register. This can happen (for example) when a constant is passed as an
20819 actual argument in a call to an inline function. (It's possible that
20820 these things can crop up in other ways also.) Note that one type of
20821 constant value which can be passed into an inlined function is a constant
20822 pointer. This can happen for example if an actual argument in an inlined
20823 function call evaluates to a compile-time constant address.
20825 CACHE_P is true if it is worth caching the location list for DECL,
20826 so that future calls can reuse it rather than regenerate it from scratch.
20827 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20828 since we will need to refer to them each time the function is inlined. */
20830 static bool
20831 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20833 rtx rtl;
20834 dw_loc_list_ref list;
20835 var_loc_list *loc_list;
20836 cached_dw_loc_list *cache;
20838 if (early_dwarf)
20839 return false;
20841 if (TREE_CODE (decl) == ERROR_MARK)
20842 return false;
20844 if (get_AT (die, DW_AT_location)
20845 || get_AT (die, DW_AT_const_value))
20846 return true;
20848 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20849 || TREE_CODE (decl) == RESULT_DECL);
20851 /* Try to get some constant RTL for this decl, and use that as the value of
20852 the location. */
20854 rtl = rtl_for_decl_location (decl);
20855 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20856 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20857 return true;
20859 /* See if we have single element location list that is equivalent to
20860 a constant value. That way we are better to use add_const_value_attribute
20861 rather than expanding constant value equivalent. */
20862 loc_list = lookup_decl_loc (decl);
20863 if (loc_list
20864 && loc_list->first
20865 && loc_list->first->next == NULL
20866 && NOTE_P (loc_list->first->loc)
20867 && NOTE_VAR_LOCATION (loc_list->first->loc)
20868 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20870 struct var_loc_node *node;
20872 node = loc_list->first;
20873 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20874 if (GET_CODE (rtl) == EXPR_LIST)
20875 rtl = XEXP (rtl, 0);
20876 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20877 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20878 return true;
20880 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20881 list several times. See if we've already cached the contents. */
20882 list = NULL;
20883 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20884 cache_p = false;
20885 if (cache_p)
20887 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20888 if (cache)
20889 list = cache->loc_list;
20891 if (list == NULL)
20893 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20894 NULL);
20895 /* It is usually worth caching this result if the decl is from
20896 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20897 if (cache_p && list && list->dw_loc_next)
20899 cached_dw_loc_list **slot
20900 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20901 DECL_UID (decl),
20902 INSERT);
20903 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20904 cache->decl_id = DECL_UID (decl);
20905 cache->loc_list = list;
20906 *slot = cache;
20909 if (list)
20911 add_AT_location_description (die, DW_AT_location, list);
20912 return true;
20914 /* None of that worked, so it must not really have a location;
20915 try adding a constant value attribute from the DECL_INITIAL. */
20916 return tree_add_const_value_attribute_for_decl (die, decl);
20919 /* Mangle referenced decls. */
20920 static tree
20921 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
20923 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20924 *walk_subtrees = 0;
20926 if (VAR_OR_FUNCTION_DECL_P (*tp))
20927 assign_assembler_name_if_needed (*tp);
20929 return NULL_TREE;
20932 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20933 attribute is the const value T. */
20935 static bool
20936 tree_add_const_value_attribute (dw_die_ref die, tree t)
20938 tree init;
20939 tree type = TREE_TYPE (t);
20941 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20942 return false;
20944 init = t;
20945 gcc_assert (!DECL_P (init));
20947 if (TREE_CODE (init) == INTEGER_CST)
20949 if (tree_fits_uhwi_p (init))
20951 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20952 return true;
20954 if (tree_fits_shwi_p (init))
20956 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20957 return true;
20960 if (!early_dwarf)
20962 rtx rtl = rtl_for_decl_init (init, type);
20963 if (rtl)
20964 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
20966 else
20968 /* For early_dwarf force mangling of all referenced symbols. */
20969 tree initializer = init;
20970 STRIP_NOPS (initializer);
20971 /* rtl_for_decl_init punts on other aggregates, and complex values. */
20972 if (AGGREGATE_TYPE_P (type)
20973 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
20974 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
20975 || TREE_CODE (type) == COMPLEX_TYPE)
20977 else if (initializer_constant_valid_p (initializer, type))
20978 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
20980 /* If the host and target are sane, try harder. */
20981 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20982 && initializer_constant_valid_p (init, type))
20984 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20985 if (size > 0 && (int) size == size)
20987 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20989 if (native_encode_initializer (init, array, size) == size)
20991 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20992 return true;
20994 ggc_free (array);
20997 return false;
21000 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
21001 attribute is the const value of T, where T is an integral constant
21002 variable with static storage duration
21003 (so it can't be a PARM_DECL or a RESULT_DECL). */
21005 static bool
21006 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
21009 if (!decl
21010 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
21011 || (VAR_P (decl) && !TREE_STATIC (decl)))
21012 return false;
21014 if (TREE_READONLY (decl)
21015 && ! TREE_THIS_VOLATILE (decl)
21016 && DECL_INITIAL (decl))
21017 /* OK */;
21018 else
21019 return false;
21021 /* Don't add DW_AT_const_value if abstract origin already has one. */
21022 if (get_AT (var_die, DW_AT_const_value))
21023 return false;
21025 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21028 /* Convert the CFI instructions for the current function into a
21029 location list. This is used for DW_AT_frame_base when we targeting
21030 a dwarf2 consumer that does not support the dwarf3
21031 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21032 expressions. */
21034 static dw_loc_list_ref
21035 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21037 int ix;
21038 dw_fde_ref fde;
21039 dw_loc_list_ref list, *list_tail;
21040 dw_cfi_ref cfi;
21041 dw_cfa_location last_cfa, next_cfa;
21042 const char *start_label, *last_label, *section;
21043 dw_cfa_location remember;
21045 fde = cfun->fde;
21046 gcc_assert (fde != NULL);
21048 section = secname_for_decl (current_function_decl);
21049 list_tail = &list;
21050 list = NULL;
21052 memset (&next_cfa, 0, sizeof (next_cfa));
21053 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21054 remember = next_cfa;
21056 start_label = fde->dw_fde_begin;
21058 /* ??? Bald assumption that the CIE opcode list does not contain
21059 advance opcodes. */
21060 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21061 lookup_cfa_1 (cfi, &next_cfa, &remember);
21063 last_cfa = next_cfa;
21064 last_label = start_label;
21066 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21068 /* If the first partition contained no CFI adjustments, the
21069 CIE opcodes apply to the whole first partition. */
21070 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21071 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21072 list_tail =&(*list_tail)->dw_loc_next;
21073 start_label = last_label = fde->dw_fde_second_begin;
21076 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21078 switch (cfi->dw_cfi_opc)
21080 case DW_CFA_set_loc:
21081 case DW_CFA_advance_loc1:
21082 case DW_CFA_advance_loc2:
21083 case DW_CFA_advance_loc4:
21084 if (!cfa_equal_p (&last_cfa, &next_cfa))
21086 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21087 start_label, 0, last_label, 0, section);
21089 list_tail = &(*list_tail)->dw_loc_next;
21090 last_cfa = next_cfa;
21091 start_label = last_label;
21093 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21094 break;
21096 case DW_CFA_advance_loc:
21097 /* The encoding is complex enough that we should never emit this. */
21098 gcc_unreachable ();
21100 default:
21101 lookup_cfa_1 (cfi, &next_cfa, &remember);
21102 break;
21104 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21106 if (!cfa_equal_p (&last_cfa, &next_cfa))
21108 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21109 start_label, 0, last_label, 0, section);
21111 list_tail = &(*list_tail)->dw_loc_next;
21112 last_cfa = next_cfa;
21113 start_label = last_label;
21115 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21116 start_label, 0, fde->dw_fde_end, 0, section);
21117 list_tail = &(*list_tail)->dw_loc_next;
21118 start_label = last_label = fde->dw_fde_second_begin;
21122 if (!cfa_equal_p (&last_cfa, &next_cfa))
21124 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21125 start_label, 0, last_label, 0, section);
21126 list_tail = &(*list_tail)->dw_loc_next;
21127 start_label = last_label;
21130 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21131 start_label, 0,
21132 fde->dw_fde_second_begin
21133 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21134 section);
21136 maybe_gen_llsym (list);
21138 return list;
21141 /* Compute a displacement from the "steady-state frame pointer" to the
21142 frame base (often the same as the CFA), and store it in
21143 frame_pointer_fb_offset. OFFSET is added to the displacement
21144 before the latter is negated. */
21146 static void
21147 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21149 rtx reg, elim;
21151 #ifdef FRAME_POINTER_CFA_OFFSET
21152 reg = frame_pointer_rtx;
21153 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21154 #else
21155 reg = arg_pointer_rtx;
21156 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21157 #endif
21159 elim = (ira_use_lra_p
21160 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21161 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21162 elim = strip_offset_and_add (elim, &offset);
21164 frame_pointer_fb_offset = -offset;
21166 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21167 in which to eliminate. This is because it's stack pointer isn't
21168 directly accessible as a register within the ISA. To work around
21169 this, assume that while we cannot provide a proper value for
21170 frame_pointer_fb_offset, we won't need one either. We can use
21171 hard frame pointer in debug info even if frame pointer isn't used
21172 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21173 which uses the DW_AT_frame_base attribute, not hard frame pointer
21174 directly. */
21175 frame_pointer_fb_offset_valid
21176 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21179 /* Generate a DW_AT_name attribute given some string value to be included as
21180 the value of the attribute. */
21182 void
21183 add_name_attribute (dw_die_ref die, const char *name_string)
21185 if (name_string != NULL && *name_string != 0)
21187 if (demangle_name_func)
21188 name_string = (*demangle_name_func) (name_string);
21190 add_AT_string (die, DW_AT_name, name_string);
21194 /* Generate a DW_AT_name attribute given some string value representing a
21195 file or filepath to be included as value of the attribute. */
21196 static void
21197 add_filename_attribute (dw_die_ref die, const char *name_string)
21199 if (name_string != NULL && *name_string != 0)
21200 add_filepath_AT_string (die, DW_AT_name, name_string);
21203 /* Generate a DW_AT_description attribute given some string value to be included
21204 as the value of the attribute. */
21206 static void
21207 add_desc_attribute (dw_die_ref die, const char *name_string)
21209 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21210 return;
21212 if (name_string == NULL || *name_string == 0)
21213 return;
21215 if (demangle_name_func)
21216 name_string = (*demangle_name_func) (name_string);
21218 add_AT_string (die, DW_AT_description, name_string);
21221 /* Generate a DW_AT_description attribute given some decl to be included
21222 as the value of the attribute. */
21224 static void
21225 add_desc_attribute (dw_die_ref die, tree decl)
21227 tree decl_name;
21229 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21230 return;
21232 if (decl == NULL_TREE || !DECL_P (decl))
21233 return;
21234 decl_name = DECL_NAME (decl);
21236 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21238 const char *name = dwarf2_name (decl, 0);
21239 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21241 else
21243 char *desc = print_generic_expr_to_str (decl);
21244 add_desc_attribute (die, desc);
21245 free (desc);
21249 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21250 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21251 of TYPE accordingly.
21253 ??? This is a temporary measure until after we're able to generate
21254 regular DWARF for the complex Ada type system. */
21256 static void
21257 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21258 dw_die_ref context_die)
21260 tree dtype;
21261 dw_die_ref dtype_die;
21263 if (!lang_hooks.types.descriptive_type)
21264 return;
21266 dtype = lang_hooks.types.descriptive_type (type);
21267 if (!dtype)
21268 return;
21270 dtype_die = lookup_type_die (dtype);
21271 if (!dtype_die)
21273 gen_type_die (dtype, context_die);
21274 dtype_die = lookup_type_die (dtype);
21275 gcc_assert (dtype_die);
21278 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21281 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21283 static const char *
21284 comp_dir_string (void)
21286 const char *wd;
21287 char *wd_plus_sep = NULL;
21288 static const char *cached_wd = NULL;
21290 if (cached_wd != NULL)
21291 return cached_wd;
21293 wd = get_src_pwd ();
21294 if (wd == NULL)
21295 return NULL;
21297 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21299 size_t wdlen = strlen (wd);
21300 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21301 strcpy (wd_plus_sep, wd);
21302 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21303 wd_plus_sep [wdlen + 1] = 0;
21304 wd = wd_plus_sep;
21307 cached_wd = remap_debug_filename (wd);
21309 /* remap_debug_filename can just pass through wd or return a new gc string.
21310 These two types can't be both stored in a GTY(())-tagged string, but since
21311 the cached value lives forever just copy it if needed. */
21312 if (cached_wd != wd)
21314 cached_wd = xstrdup (cached_wd);
21315 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21316 free (wd_plus_sep);
21319 return cached_wd;
21322 /* Generate a DW_AT_comp_dir attribute for DIE. */
21324 static void
21325 add_comp_dir_attribute (dw_die_ref die)
21327 const char * wd = comp_dir_string ();
21328 if (wd != NULL)
21329 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21332 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21333 pointer computation, ...), output a representation for that bound according
21334 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21335 loc_list_from_tree for the meaning of CONTEXT. */
21337 static void
21338 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21339 int forms, struct loc_descr_context *context)
21341 dw_die_ref context_die, decl_die = NULL;
21342 dw_loc_list_ref list;
21343 bool strip_conversions = true;
21344 bool placeholder_seen = false;
21346 while (strip_conversions)
21347 switch (TREE_CODE (value))
21349 case ERROR_MARK:
21350 case SAVE_EXPR:
21351 return;
21353 CASE_CONVERT:
21354 case VIEW_CONVERT_EXPR:
21355 value = TREE_OPERAND (value, 0);
21356 break;
21358 default:
21359 strip_conversions = false;
21360 break;
21363 /* If possible and permitted, output the attribute as a constant. */
21364 if ((forms & dw_scalar_form_constant) != 0
21365 && TREE_CODE (value) == INTEGER_CST)
21367 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21369 /* If HOST_WIDE_INT is big enough then represent the bound as
21370 a constant value. We need to choose a form based on
21371 whether the type is signed or unsigned. We cannot just
21372 call add_AT_unsigned if the value itself is positive
21373 (add_AT_unsigned might add the unsigned value encoded as
21374 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21375 bounds type and then sign extend any unsigned values found
21376 for signed types. This is needed only for
21377 DW_AT_{lower,upper}_bound, since for most other attributes,
21378 consumers will treat DW_FORM_data[1248] as unsigned values,
21379 regardless of the underlying type. */
21380 if (prec <= HOST_BITS_PER_WIDE_INT
21381 || tree_fits_uhwi_p (value))
21383 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21384 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21385 else
21386 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21388 else if (dwarf_version >= 5
21389 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21390 /* Otherwise represent the bound as an unsigned value with
21391 the precision of its type. The precision and signedness
21392 of the type will be necessary to re-interpret it
21393 unambiguously. */
21394 add_AT_wide (die, attr, wi::to_wide (value));
21395 else
21397 rtx v = immed_wide_int_const (wi::to_wide (value),
21398 TYPE_MODE (TREE_TYPE (value)));
21399 dw_loc_descr_ref loc
21400 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21401 VAR_INIT_STATUS_INITIALIZED);
21402 if (loc)
21403 add_AT_loc (die, attr, loc);
21405 return;
21408 /* Otherwise, if it's possible and permitted too, output a reference to
21409 another DIE. */
21410 if ((forms & dw_scalar_form_reference) != 0)
21412 tree decl = NULL_TREE;
21414 /* Some type attributes reference an outer type. For instance, the upper
21415 bound of an array may reference an embedding record (this happens in
21416 Ada). */
21417 if (TREE_CODE (value) == COMPONENT_REF
21418 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21419 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21420 decl = TREE_OPERAND (value, 1);
21422 else if (VAR_P (value)
21423 || TREE_CODE (value) == PARM_DECL
21424 || TREE_CODE (value) == RESULT_DECL)
21425 decl = value;
21427 if (decl != NULL_TREE)
21429 decl_die = lookup_decl_die (decl);
21431 /* ??? Can this happen, or should the variable have been bound
21432 first? Probably it can, since I imagine that we try to create
21433 the types of parameters in the order in which they exist in
21434 the list, and won't have created a forward reference to a
21435 later parameter. */
21436 if (decl_die != NULL)
21438 if (get_AT (decl_die, DW_AT_location)
21439 || get_AT (decl_die, DW_AT_data_member_location)
21440 || get_AT (decl_die, DW_AT_data_bit_offset)
21441 || get_AT (decl_die, DW_AT_const_value))
21443 add_AT_die_ref (die, attr, decl_die);
21444 return;
21450 /* Last chance: try to create a stack operation procedure to evaluate the
21451 value. Do nothing if even that is not possible or permitted. */
21452 if ((forms & dw_scalar_form_exprloc) == 0)
21453 return;
21455 list = loc_list_from_tree (value, 2, context);
21456 if (context && context->placeholder_arg)
21458 placeholder_seen = context->placeholder_seen;
21459 context->placeholder_seen = false;
21461 if (list == NULL || single_element_loc_list_p (list))
21463 /* If this attribute is not a reference nor constant, it is
21464 a DWARF expression rather than location description. For that
21465 loc_list_from_tree (value, 0, &context) is needed. */
21466 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21467 if (list2 && single_element_loc_list_p (list2))
21469 if (placeholder_seen)
21471 struct dwarf_procedure_info dpi;
21472 dpi.fndecl = NULL_TREE;
21473 dpi.args_count = 1;
21474 if (!resolve_args_picking (list2->expr, 1, &dpi))
21475 return;
21477 add_AT_loc (die, attr, list2->expr);
21478 return;
21482 /* If that failed to give a single element location list, fall back to
21483 outputting this as a reference... still if permitted. */
21484 if (list == NULL
21485 || (forms & dw_scalar_form_reference) == 0
21486 || placeholder_seen)
21487 return;
21489 if (!decl_die)
21491 if (current_function_decl == 0)
21492 context_die = comp_unit_die ();
21493 else
21494 context_die = lookup_decl_die (current_function_decl);
21496 decl_die = new_die (DW_TAG_variable, context_die, value);
21497 add_AT_flag (decl_die, DW_AT_artificial, 1);
21498 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21499 context_die);
21502 add_AT_location_description (decl_die, DW_AT_location, list);
21503 add_AT_die_ref (die, attr, decl_die);
21506 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21507 default. */
21509 static int
21510 lower_bound_default (void)
21512 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21514 case DW_LANG_C:
21515 case DW_LANG_C89:
21516 case DW_LANG_C99:
21517 case DW_LANG_C11:
21518 case DW_LANG_C_plus_plus:
21519 case DW_LANG_C_plus_plus_11:
21520 case DW_LANG_C_plus_plus_14:
21521 case DW_LANG_ObjC:
21522 case DW_LANG_ObjC_plus_plus:
21523 return 0;
21524 case DW_LANG_Fortran77:
21525 case DW_LANG_Fortran90:
21526 case DW_LANG_Fortran95:
21527 case DW_LANG_Fortran03:
21528 case DW_LANG_Fortran08:
21529 return 1;
21530 case DW_LANG_UPC:
21531 case DW_LANG_D:
21532 case DW_LANG_Python:
21533 return dwarf_version >= 4 ? 0 : -1;
21534 case DW_LANG_Ada95:
21535 case DW_LANG_Ada83:
21536 case DW_LANG_Cobol74:
21537 case DW_LANG_Cobol85:
21538 case DW_LANG_Modula2:
21539 case DW_LANG_PLI:
21540 return dwarf_version >= 4 ? 1 : -1;
21541 default:
21542 return -1;
21546 /* Given a tree node describing an array bound (either lower or upper) output
21547 a representation for that bound. */
21549 static void
21550 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21551 tree bound, struct loc_descr_context *context)
21553 int dflt;
21555 while (1)
21556 switch (TREE_CODE (bound))
21558 /* Strip all conversions. */
21559 CASE_CONVERT:
21560 case VIEW_CONVERT_EXPR:
21561 bound = TREE_OPERAND (bound, 0);
21562 break;
21564 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21565 are even omitted when they are the default. */
21566 case INTEGER_CST:
21567 /* If the value for this bound is the default one, we can even omit the
21568 attribute. */
21569 if (bound_attr == DW_AT_lower_bound
21570 && tree_fits_shwi_p (bound)
21571 && (dflt = lower_bound_default ()) != -1
21572 && tree_to_shwi (bound) == dflt)
21573 return;
21575 /* FALLTHRU */
21577 default:
21578 /* Let GNAT encodings do the magic for self-referential bounds. */
21579 if (is_ada ()
21580 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21581 && contains_placeholder_p (bound))
21582 return;
21584 add_scalar_info (subrange_die, bound_attr, bound,
21585 dw_scalar_form_constant
21586 | dw_scalar_form_exprloc
21587 | dw_scalar_form_reference,
21588 context);
21589 return;
21593 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21594 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21596 This function reuses previously set type and bound information if
21597 available. */
21599 static void
21600 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21602 dw_die_ref child = type_die->die_child;
21603 struct array_descr_info info;
21604 int dimension_number;
21606 if (lang_hooks.types.get_array_descr_info)
21608 memset (&info, 0, sizeof (info));
21609 if (lang_hooks.types.get_array_descr_info (type, &info))
21610 /* Fortran sometimes emits array types with no dimension. */
21611 gcc_assert (info.ndimensions >= 0
21612 && info.ndimensions
21613 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21615 else
21616 info.ndimensions = 0;
21618 for (dimension_number = 0;
21619 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21620 type = TREE_TYPE (type), dimension_number++)
21622 tree domain = TYPE_DOMAIN (type);
21624 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21625 break;
21627 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21628 and (in GNU C only) variable bounds. Handle all three forms
21629 here. */
21631 /* Find and reuse a previously generated DW_TAG_subrange_type if
21632 available.
21634 For multi-dimensional arrays, as we iterate through the
21635 various dimensions in the enclosing for loop above, we also
21636 iterate through the DIE children and pick at each
21637 DW_TAG_subrange_type previously generated (if available).
21638 Each child DW_TAG_subrange_type DIE describes the range of
21639 the current dimension. At this point we should have as many
21640 DW_TAG_subrange_type's as we have dimensions in the
21641 array. */
21642 dw_die_ref subrange_die = NULL;
21643 if (child)
21644 while (1)
21646 child = child->die_sib;
21647 if (child->die_tag == DW_TAG_subrange_type)
21648 subrange_die = child;
21649 if (child == type_die->die_child)
21651 /* If we wrapped around, stop looking next time. */
21652 child = NULL;
21653 break;
21655 if (child->die_tag == DW_TAG_subrange_type)
21656 break;
21658 if (!subrange_die)
21659 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21661 if (domain)
21663 /* We have an array type with specified bounds. */
21664 tree lower = TYPE_MIN_VALUE (domain);
21665 tree upper = TYPE_MAX_VALUE (domain);
21666 tree index_type = TREE_TYPE (domain);
21668 if (dimension_number <= info.ndimensions - 1)
21670 lower = info.dimen[dimension_number].lower_bound;
21671 upper = info.dimen[dimension_number].upper_bound;
21672 index_type = info.dimen[dimension_number].bounds_type;
21675 /* Define the index type. */
21676 if (index_type && !get_AT (subrange_die, DW_AT_type))
21677 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21678 false, type_die);
21680 /* ??? If upper is NULL, the array has unspecified length,
21681 but it does have a lower bound. This happens with Fortran
21682 dimension arr(N:*)
21683 Since the debugger is definitely going to need to know N
21684 to produce useful results, go ahead and output the lower
21685 bound solo, and hope the debugger can cope. */
21687 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21688 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21690 if (!get_AT (subrange_die, DW_AT_upper_bound)
21691 && !get_AT (subrange_die, DW_AT_count))
21693 if (upper)
21694 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21695 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21696 /* Zero-length array. */
21697 add_bound_info (subrange_die, DW_AT_count,
21698 build_int_cst (TREE_TYPE (lower), 0), NULL);
21702 /* Otherwise we have an array type with an unspecified length. The
21703 DWARF-2 spec does not say how to handle this; let's just leave out the
21704 bounds. */
21708 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21710 static void
21711 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21713 dw_die_ref decl_die;
21714 HOST_WIDE_INT size;
21716 switch (TREE_CODE (tree_node))
21718 case ERROR_MARK:
21719 size = 0;
21720 break;
21721 case ENUMERAL_TYPE:
21722 case RECORD_TYPE:
21723 case UNION_TYPE:
21724 case QUAL_UNION_TYPE:
21725 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21726 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21728 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21729 return;
21731 size = int_size_in_bytes (tree_node);
21732 break;
21733 case FIELD_DECL:
21734 /* For a data member of a struct or union, the DW_AT_byte_size is
21735 generally given as the number of bytes normally allocated for an
21736 object of the *declared* type of the member itself. This is true
21737 even for bit-fields. */
21738 size = int_size_in_bytes (field_type (tree_node));
21739 break;
21740 default:
21741 gcc_unreachable ();
21744 /* Note that `size' might be -1 when we get to this point. If it is, that
21745 indicates that the byte size of the entity in question is variable. */
21746 if (size >= 0)
21747 add_AT_unsigned (die, DW_AT_byte_size, size);
21749 /* Support for dynamically-sized objects was introduced in DWARF3. */
21750 else if (TYPE_P (tree_node)
21751 && (dwarf_version >= 3 || !dwarf_strict)
21752 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21754 struct loc_descr_context ctx = {
21755 const_cast<tree> (tree_node), /* context_type */
21756 NULL_TREE, /* base_decl */
21757 NULL, /* dpi */
21758 false, /* placeholder_arg */
21759 false, /* placeholder_seen */
21760 false /* strict_signedness */
21763 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21764 add_scalar_info (die, DW_AT_byte_size, tree_size,
21765 dw_scalar_form_constant
21766 | dw_scalar_form_exprloc
21767 | dw_scalar_form_reference,
21768 &ctx);
21772 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21773 alignment. */
21775 static void
21776 add_alignment_attribute (dw_die_ref die, tree tree_node)
21778 if (dwarf_version < 5 && dwarf_strict)
21779 return;
21781 unsigned align;
21783 if (DECL_P (tree_node))
21785 if (!DECL_USER_ALIGN (tree_node))
21786 return;
21788 align = DECL_ALIGN_UNIT (tree_node);
21790 else if (TYPE_P (tree_node))
21792 if (!TYPE_USER_ALIGN (tree_node))
21793 return;
21795 align = TYPE_ALIGN_UNIT (tree_node);
21797 else
21798 gcc_unreachable ();
21800 add_AT_unsigned (die, DW_AT_alignment, align);
21803 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21804 which specifies the distance in bits from the highest order bit of the
21805 "containing object" for the bit-field to the highest order bit of the
21806 bit-field itself.
21808 For any given bit-field, the "containing object" is a hypothetical object
21809 (of some integral or enum type) within which the given bit-field lives. The
21810 type of this hypothetical "containing object" is always the same as the
21811 declared type of the individual bit-field itself. The determination of the
21812 exact location of the "containing object" for a bit-field is rather
21813 complicated. It's handled by the `field_byte_offset' function (above).
21815 Note that it is the size (in bytes) of the hypothetical "containing object"
21816 which will be given in the DW_AT_byte_size attribute for this bit-field.
21817 (See `byte_size_attribute' above). */
21819 static inline void
21820 add_bit_offset_attribute (dw_die_ref die, tree decl)
21822 HOST_WIDE_INT object_offset_in_bytes;
21823 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21824 HOST_WIDE_INT bitpos_int;
21825 HOST_WIDE_INT highest_order_object_bit_offset;
21826 HOST_WIDE_INT highest_order_field_bit_offset;
21827 HOST_WIDE_INT bit_offset;
21829 /* The containing object is within the DECL_CONTEXT. */
21830 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21832 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21834 /* Must be a field and a bit field. */
21835 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21837 /* We can't yet handle bit-fields whose offsets are variable, so if we
21838 encounter such things, just return without generating any attribute
21839 whatsoever. Likewise for variable or too large size. */
21840 if (! tree_fits_shwi_p (bit_position (decl))
21841 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21842 return;
21844 bitpos_int = int_bit_position (decl);
21846 /* Note that the bit offset is always the distance (in bits) from the
21847 highest-order bit of the "containing object" to the highest-order bit of
21848 the bit-field itself. Since the "high-order end" of any object or field
21849 is different on big-endian and little-endian machines, the computation
21850 below must take account of these differences. */
21851 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21852 highest_order_field_bit_offset = bitpos_int;
21854 if (! BYTES_BIG_ENDIAN)
21856 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21857 highest_order_object_bit_offset +=
21858 simple_type_size_in_bits (original_type);
21861 bit_offset
21862 = (! BYTES_BIG_ENDIAN
21863 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21864 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21866 if (bit_offset < 0)
21867 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21868 else
21869 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21872 /* For a FIELD_DECL node which represents a bit field, output an attribute
21873 which specifies the length in bits of the given field. */
21875 static inline void
21876 add_bit_size_attribute (dw_die_ref die, tree decl)
21878 /* Must be a field and a bit field. */
21879 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21880 && DECL_BIT_FIELD_TYPE (decl));
21882 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21883 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21886 /* If the compiled language is ANSI C, then add a 'prototyped'
21887 attribute, if arg types are given for the parameters of a function. */
21889 static inline void
21890 add_prototyped_attribute (dw_die_ref die, tree func_type)
21892 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21894 case DW_LANG_C:
21895 case DW_LANG_C89:
21896 case DW_LANG_C99:
21897 case DW_LANG_C11:
21898 case DW_LANG_ObjC:
21899 if (prototype_p (func_type))
21900 add_AT_flag (die, DW_AT_prototyped, 1);
21901 break;
21902 default:
21903 break;
21907 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21908 by looking in the type declaration, the object declaration equate table or
21909 the block mapping. */
21911 static inline void
21912 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21914 dw_die_ref origin_die = NULL;
21916 /* For late LTO debug output we want to refer directly to the abstract
21917 DIE in the early debug rather to the possibly existing concrete
21918 instance and avoid creating that just for this purpose. */
21919 sym_off_pair *desc;
21920 if (in_lto_p
21921 && external_die_map
21922 && (desc = external_die_map->get (origin)))
21924 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21925 desc->sym, desc->off);
21926 return;
21929 if (DECL_P (origin))
21930 origin_die = lookup_decl_die (origin);
21931 else if (TYPE_P (origin))
21932 origin_die = lookup_type_die (origin);
21933 else if (TREE_CODE (origin) == BLOCK)
21934 origin_die = lookup_block_die (origin);
21936 /* XXX: Functions that are never lowered don't always have correct block
21937 trees (in the case of java, they simply have no block tree, in some other
21938 languages). For these functions, there is nothing we can really do to
21939 output correct debug info for inlined functions in all cases. Rather
21940 than die, we'll just produce deficient debug info now, in that we will
21941 have variables without a proper abstract origin. In the future, when all
21942 functions are lowered, we should re-add a gcc_assert (origin_die)
21943 here. */
21945 if (origin_die)
21947 dw_attr_node *a;
21948 /* Like above, if we already created a concrete instance DIE
21949 do not use that for the abstract origin but the early DIE
21950 if present. */
21951 if (in_lto_p
21952 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21953 origin_die = AT_ref (a);
21954 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21958 /* We do not currently support the pure_virtual attribute. */
21960 static inline void
21961 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21963 if (DECL_VINDEX (func_decl))
21965 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21967 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21968 add_AT_loc (die, DW_AT_vtable_elem_location,
21969 new_loc_descr (DW_OP_constu,
21970 tree_to_shwi (DECL_VINDEX (func_decl)),
21971 0));
21973 /* GNU extension: Record what type this method came from originally. */
21974 if (debug_info_level > DINFO_LEVEL_TERSE
21975 && DECL_CONTEXT (func_decl))
21976 add_AT_die_ref (die, DW_AT_containing_type,
21977 lookup_type_die (DECL_CONTEXT (func_decl)));
21981 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21982 given decl. This used to be a vendor extension until after DWARF 4
21983 standardized it. */
21985 static void
21986 add_linkage_attr (dw_die_ref die, tree decl)
21988 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21990 /* Mimic what assemble_name_raw does with a leading '*'. */
21991 if (name[0] == '*')
21992 name = &name[1];
21994 if (dwarf_version >= 4)
21995 add_AT_string (die, DW_AT_linkage_name, name);
21996 else
21997 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
22000 /* Add source coordinate attributes for the given decl. */
22002 static void
22003 add_src_coords_attributes (dw_die_ref die, tree decl)
22005 expanded_location s;
22007 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
22008 return;
22009 s = expand_location (DECL_SOURCE_LOCATION (decl));
22010 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
22011 add_AT_unsigned (die, DW_AT_decl_line, s.line);
22012 if (debug_column_info && s.column)
22013 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22016 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22018 static void
22019 add_linkage_name_raw (dw_die_ref die, tree decl)
22021 /* Defer until we have an assembler name set. */
22022 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22024 limbo_die_node *asm_name;
22026 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22027 asm_name->die = die;
22028 asm_name->created_for = decl;
22029 asm_name->next = deferred_asm_name;
22030 deferred_asm_name = asm_name;
22032 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22033 add_linkage_attr (die, decl);
22036 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22038 static void
22039 add_linkage_name (dw_die_ref die, tree decl)
22041 if (debug_info_level > DINFO_LEVEL_NONE
22042 && VAR_OR_FUNCTION_DECL_P (decl)
22043 && TREE_PUBLIC (decl)
22044 && !(VAR_P (decl) && DECL_REGISTER (decl))
22045 && die->die_tag != DW_TAG_member)
22046 add_linkage_name_raw (die, decl);
22049 /* Add a DW_AT_name attribute and source coordinate attribute for the
22050 given decl, but only if it actually has a name. */
22052 static void
22053 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22054 bool no_linkage_name)
22056 tree decl_name;
22058 decl_name = DECL_NAME (decl);
22059 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22061 const char *name = dwarf2_name (decl, 0);
22062 if (name)
22063 add_name_attribute (die, name);
22064 else
22065 add_desc_attribute (die, decl);
22067 if (! DECL_ARTIFICIAL (decl))
22068 add_src_coords_attributes (die, decl);
22070 if (!no_linkage_name)
22071 add_linkage_name (die, decl);
22073 else
22074 add_desc_attribute (die, decl);
22076 #ifdef VMS_DEBUGGING_INFO
22077 /* Get the function's name, as described by its RTL. This may be different
22078 from the DECL_NAME name used in the source file. */
22079 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22081 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22082 XEXP (DECL_RTL (decl), 0), false);
22083 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22085 #endif /* VMS_DEBUGGING_INFO */
22088 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22090 static void
22091 add_discr_value (dw_die_ref die, dw_discr_value *value)
22093 dw_attr_node attr;
22095 attr.dw_attr = DW_AT_discr_value;
22096 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22097 attr.dw_attr_val.val_entry = NULL;
22098 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22099 if (value->pos)
22100 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22101 else
22102 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22103 add_dwarf_attr (die, &attr);
22106 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22108 static void
22109 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22111 dw_attr_node attr;
22113 attr.dw_attr = DW_AT_discr_list;
22114 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22115 attr.dw_attr_val.val_entry = NULL;
22116 attr.dw_attr_val.v.val_discr_list = discr_list;
22117 add_dwarf_attr (die, &attr);
22120 static inline dw_discr_list_ref
22121 AT_discr_list (dw_attr_node *attr)
22123 return attr->dw_attr_val.v.val_discr_list;
22126 #ifdef VMS_DEBUGGING_INFO
22127 /* Output the debug main pointer die for VMS */
22129 void
22130 dwarf2out_vms_debug_main_pointer (void)
22132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22133 dw_die_ref die;
22135 /* Allocate the VMS debug main subprogram die. */
22136 die = new_die_raw (DW_TAG_subprogram);
22137 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22138 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22139 current_function_funcdef_no);
22140 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22142 /* Make it the first child of comp_unit_die (). */
22143 die->die_parent = comp_unit_die ();
22144 if (comp_unit_die ()->die_child)
22146 die->die_sib = comp_unit_die ()->die_child->die_sib;
22147 comp_unit_die ()->die_child->die_sib = die;
22149 else
22151 die->die_sib = die;
22152 comp_unit_die ()->die_child = die;
22155 #endif /* VMS_DEBUGGING_INFO */
22157 /* walk_tree helper function for uses_local_type, below. */
22159 static tree
22160 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22162 if (!TYPE_P (*tp))
22163 *walk_subtrees = 0;
22164 else
22166 tree name = TYPE_NAME (*tp);
22167 if (name && DECL_P (name) && decl_function_context (name))
22168 return *tp;
22170 return NULL_TREE;
22173 /* If TYPE involves a function-local type (including a local typedef to a
22174 non-local type), returns that type; otherwise returns NULL_TREE. */
22176 static tree
22177 uses_local_type (tree type)
22179 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22180 return used;
22183 /* Return the DIE for the scope that immediately contains this type.
22184 Non-named types that do not involve a function-local type get global
22185 scope. Named types nested in namespaces or other types get their
22186 containing scope. All other types (i.e. function-local named types) get
22187 the current active scope. */
22189 static dw_die_ref
22190 scope_die_for (tree t, dw_die_ref context_die)
22192 dw_die_ref scope_die = NULL;
22193 tree containing_scope;
22195 /* Non-types always go in the current scope. */
22196 gcc_assert (TYPE_P (t));
22198 /* Use the scope of the typedef, rather than the scope of the type
22199 it refers to. */
22200 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22201 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22202 else
22203 containing_scope = TYPE_CONTEXT (t);
22205 /* Use the containing namespace if there is one. */
22206 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22208 if (context_die == lookup_decl_die (containing_scope))
22209 /* OK */;
22210 else if (debug_info_level > DINFO_LEVEL_TERSE)
22211 context_die = get_context_die (containing_scope);
22212 else
22213 containing_scope = NULL_TREE;
22216 /* Ignore function type "scopes" from the C frontend. They mean that
22217 a tagged type is local to a parmlist of a function declarator, but
22218 that isn't useful to DWARF. */
22219 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22220 containing_scope = NULL_TREE;
22222 if (SCOPE_FILE_SCOPE_P (containing_scope))
22224 /* If T uses a local type keep it local as well, to avoid references
22225 to function-local DIEs from outside the function. */
22226 if (current_function_decl && uses_local_type (t))
22227 scope_die = context_die;
22228 else
22229 scope_die = comp_unit_die ();
22231 else if (TYPE_P (containing_scope))
22233 /* For types, we can just look up the appropriate DIE. */
22234 if (debug_info_level > DINFO_LEVEL_TERSE)
22235 scope_die = get_context_die (containing_scope);
22236 else
22238 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22239 if (scope_die == NULL)
22240 scope_die = comp_unit_die ();
22243 else
22244 scope_die = context_die;
22246 return scope_die;
22249 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22251 static inline int
22252 local_scope_p (dw_die_ref context_die)
22254 for (; context_die; context_die = context_die->die_parent)
22255 if (context_die->die_tag == DW_TAG_inlined_subroutine
22256 || context_die->die_tag == DW_TAG_subprogram)
22257 return 1;
22259 return 0;
22262 /* Returns nonzero if CONTEXT_DIE is a class. */
22264 static inline int
22265 class_scope_p (dw_die_ref context_die)
22267 return (context_die
22268 && (context_die->die_tag == DW_TAG_structure_type
22269 || context_die->die_tag == DW_TAG_class_type
22270 || context_die->die_tag == DW_TAG_interface_type
22271 || context_die->die_tag == DW_TAG_union_type));
22274 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22275 whether or not to treat a DIE in this context as a declaration. */
22277 static inline int
22278 class_or_namespace_scope_p (dw_die_ref context_die)
22280 return (class_scope_p (context_die)
22281 || (context_die && context_die->die_tag == DW_TAG_namespace));
22284 /* Many forms of DIEs require a "type description" attribute. This
22285 routine locates the proper "type descriptor" die for the type given
22286 by 'type' plus any additional qualifiers given by 'cv_quals', and
22287 adds a DW_AT_type attribute below the given die. */
22289 static void
22290 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22291 bool reverse, dw_die_ref context_die)
22293 enum tree_code code = TREE_CODE (type);
22294 dw_die_ref type_die = NULL;
22296 if (debug_info_level <= DINFO_LEVEL_TERSE)
22297 return;
22299 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22300 or fixed-point type, use the inner type. This is because we have no
22301 support for unnamed types in base_type_die. This can happen if this is
22302 an Ada subrange type. Correct solution is emit a subrange type die. */
22303 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22304 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22305 type = TREE_TYPE (type), code = TREE_CODE (type);
22307 if (code == ERROR_MARK
22308 /* Handle a special case. For functions whose return type is void, we
22309 generate *no* type attribute. (Note that no object may have type
22310 `void', so this only applies to function return types). */
22311 || code == VOID_TYPE)
22312 return;
22314 type_die = modified_type_die (type,
22315 cv_quals | TYPE_QUALS (type),
22316 reverse,
22317 context_die);
22319 if (type_die != NULL)
22320 add_AT_die_ref (object_die, DW_AT_type, type_die);
22323 /* Given an object die, add the calling convention attribute for the
22324 function call type. */
22325 static void
22326 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22328 enum dwarf_calling_convention value = DW_CC_normal;
22330 value = ((enum dwarf_calling_convention)
22331 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22333 if (is_fortran ()
22334 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22336 /* DWARF 2 doesn't provide a way to identify a program's source-level
22337 entry point. DW_AT_calling_convention attributes are only meant
22338 to describe functions' calling conventions. However, lacking a
22339 better way to signal the Fortran main program, we used this for
22340 a long time, following existing custom. Now, DWARF 4 has
22341 DW_AT_main_subprogram, which we add below, but some tools still
22342 rely on the old way, which we thus keep. */
22343 value = DW_CC_program;
22345 if (dwarf_version >= 4 || !dwarf_strict)
22346 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22349 /* Only add the attribute if the backend requests it, and
22350 is not DW_CC_normal. */
22351 if (value && (value != DW_CC_normal))
22352 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22355 /* Given a tree pointer to a struct, class, union, or enum type node, return
22356 a pointer to the (string) tag name for the given type, or zero if the type
22357 was declared without a tag. */
22359 static const char *
22360 type_tag (const_tree type)
22362 const char *name = 0;
22364 if (TYPE_NAME (type) != 0)
22366 tree t = 0;
22368 /* Find the IDENTIFIER_NODE for the type name. */
22369 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22370 && !TYPE_NAMELESS (type))
22371 t = TYPE_NAME (type);
22373 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22374 a TYPE_DECL node, regardless of whether or not a `typedef' was
22375 involved. */
22376 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22377 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22379 /* We want to be extra verbose. Don't call dwarf_name if
22380 DECL_NAME isn't set. The default hook for decl_printable_name
22381 doesn't like that, and in this context it's correct to return
22382 0, instead of "<anonymous>" or the like. */
22383 if (DECL_NAME (TYPE_NAME (type))
22384 && !DECL_NAMELESS (TYPE_NAME (type)))
22385 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22388 /* Now get the name as a string, or invent one. */
22389 if (!name && t != 0)
22390 name = IDENTIFIER_POINTER (t);
22393 return (name == 0 || *name == '\0') ? 0 : name;
22396 /* Return the type associated with a data member, make a special check
22397 for bit field types. */
22399 static inline tree
22400 member_declared_type (const_tree member)
22402 return (DECL_BIT_FIELD_TYPE (member)
22403 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22406 /* Get the decl's label, as described by its RTL. This may be different
22407 from the DECL_NAME name used in the source file. */
22409 #if 0
22410 static const char *
22411 decl_start_label (tree decl)
22413 rtx x;
22414 const char *fnname;
22416 x = DECL_RTL (decl);
22417 gcc_assert (MEM_P (x));
22419 x = XEXP (x, 0);
22420 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22422 fnname = XSTR (x, 0);
22423 return fnname;
22425 #endif
22427 /* For variable-length arrays that have been previously generated, but
22428 may be incomplete due to missing subscript info, fill the subscript
22429 info. Return TRUE if this is one of those cases. */
22431 static bool
22432 fill_variable_array_bounds (tree type)
22434 if (TREE_ASM_WRITTEN (type)
22435 && TREE_CODE (type) == ARRAY_TYPE
22436 && variably_modified_type_p (type, NULL))
22438 dw_die_ref array_die = lookup_type_die (type);
22439 if (!array_die)
22440 return false;
22441 add_subscript_info (array_die, type, !is_ada ());
22442 return true;
22444 return false;
22447 /* These routines generate the internal representation of the DIE's for
22448 the compilation unit. Debugging information is collected by walking
22449 the declaration trees passed in from dwarf2out_decl(). */
22451 static void
22452 gen_array_type_die (tree type, dw_die_ref context_die)
22454 dw_die_ref array_die;
22456 /* GNU compilers represent multidimensional array types as sequences of one
22457 dimensional array types whose element types are themselves array types.
22458 We sometimes squish that down to a single array_type DIE with multiple
22459 subscripts in the Dwarf debugging info. The draft Dwarf specification
22460 say that we are allowed to do this kind of compression in C, because
22461 there is no difference between an array of arrays and a multidimensional
22462 array. We don't do this for Ada to remain as close as possible to the
22463 actual representation, which is especially important against the language
22464 flexibilty wrt arrays of variable size. */
22466 bool collapse_nested_arrays = !is_ada ();
22468 if (fill_variable_array_bounds (type))
22469 return;
22471 dw_die_ref scope_die = scope_die_for (type, context_die);
22472 tree element_type;
22474 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22475 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22476 if (TREE_CODE (type) == ARRAY_TYPE
22477 && TYPE_STRING_FLAG (type)
22478 && is_fortran ()
22479 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22481 HOST_WIDE_INT size;
22483 array_die = new_die (DW_TAG_string_type, scope_die, type);
22484 add_name_attribute (array_die, type_tag (type));
22485 equate_type_number_to_die (type, array_die);
22486 size = int_size_in_bytes (type);
22487 if (size >= 0)
22488 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22489 /* ??? We can't annotate types late, but for LTO we may not
22490 generate a location early either (gfortran.dg/save_6.f90). */
22491 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22492 && TYPE_DOMAIN (type) != NULL_TREE
22493 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22495 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22496 tree rszdecl = szdecl;
22498 size = int_size_in_bytes (TREE_TYPE (szdecl));
22499 if (!DECL_P (szdecl))
22501 if (TREE_CODE (szdecl) == INDIRECT_REF
22502 && DECL_P (TREE_OPERAND (szdecl, 0)))
22504 rszdecl = TREE_OPERAND (szdecl, 0);
22505 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22506 != DWARF2_ADDR_SIZE)
22507 size = 0;
22509 else
22510 size = 0;
22512 if (size > 0)
22514 dw_loc_list_ref loc
22515 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22516 NULL);
22517 if (loc)
22519 add_AT_location_description (array_die, DW_AT_string_length,
22520 loc);
22521 if (size != DWARF2_ADDR_SIZE)
22522 add_AT_unsigned (array_die, dwarf_version >= 5
22523 ? DW_AT_string_length_byte_size
22524 : DW_AT_byte_size, size);
22528 return;
22531 array_die = new_die (DW_TAG_array_type, scope_die, type);
22532 add_name_attribute (array_die, type_tag (type));
22533 equate_type_number_to_die (type, array_die);
22535 if (TREE_CODE (type) == VECTOR_TYPE)
22536 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22538 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22539 if (is_fortran ()
22540 && TREE_CODE (type) == ARRAY_TYPE
22541 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22542 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22543 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22545 #if 0
22546 /* We default the array ordering. Debuggers will probably do the right
22547 things even if DW_AT_ordering is not present. It's not even an issue
22548 until we start to get into multidimensional arrays anyway. If a debugger
22549 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22550 then we'll have to put the DW_AT_ordering attribute back in. (But if
22551 and when we find out that we need to put these in, we will only do so
22552 for multidimensional arrays. */
22553 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22554 #endif
22556 if (TREE_CODE (type) == VECTOR_TYPE)
22558 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22559 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22560 int lb = lower_bound_default ();
22561 if (lb == -1)
22562 lb = 0;
22563 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22564 add_bound_info (subrange_die, DW_AT_upper_bound,
22565 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22567 else
22568 add_subscript_info (array_die, type, collapse_nested_arrays);
22570 /* Add representation of the type of the elements of this array type and
22571 emit the corresponding DIE if we haven't done it already. */
22572 element_type = TREE_TYPE (type);
22573 if (collapse_nested_arrays)
22574 while (TREE_CODE (element_type) == ARRAY_TYPE)
22576 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22577 break;
22578 element_type = TREE_TYPE (element_type);
22581 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22582 TREE_CODE (type) == ARRAY_TYPE
22583 && TYPE_REVERSE_STORAGE_ORDER (type),
22584 context_die);
22586 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22587 if (TYPE_ARTIFICIAL (type))
22588 add_AT_flag (array_die, DW_AT_artificial, 1);
22590 if (get_AT (array_die, DW_AT_name))
22591 add_pubtype (type, array_die);
22593 add_alignment_attribute (array_die, type);
22596 /* This routine generates DIE for array with hidden descriptor, details
22597 are filled into *info by a langhook. */
22599 static void
22600 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22601 dw_die_ref context_die)
22603 const dw_die_ref scope_die = scope_die_for (type, context_die);
22604 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22605 struct loc_descr_context context = {
22606 type, /* context_type */
22607 info->base_decl, /* base_decl */
22608 NULL, /* dpi */
22609 false, /* placeholder_arg */
22610 false, /* placeholder_seen */
22611 false /* strict_signedness */
22613 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22614 int dim;
22616 add_name_attribute (array_die, type_tag (type));
22617 equate_type_number_to_die (type, array_die);
22619 if (info->ndimensions > 1)
22620 switch (info->ordering)
22622 case array_descr_ordering_row_major:
22623 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22624 break;
22625 case array_descr_ordering_column_major:
22626 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22627 break;
22628 default:
22629 break;
22632 if (dwarf_version >= 3 || !dwarf_strict)
22634 if (info->data_location)
22635 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22636 dw_scalar_form_exprloc, &context);
22637 if (info->associated)
22638 add_scalar_info (array_die, DW_AT_associated, info->associated,
22639 dw_scalar_form_constant
22640 | dw_scalar_form_exprloc
22641 | dw_scalar_form_reference, &context);
22642 if (info->allocated)
22643 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22644 dw_scalar_form_constant
22645 | dw_scalar_form_exprloc
22646 | dw_scalar_form_reference, &context);
22647 if (info->stride)
22649 const enum dwarf_attribute attr
22650 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22651 const int forms
22652 = (info->stride_in_bits)
22653 ? dw_scalar_form_constant
22654 : (dw_scalar_form_constant
22655 | dw_scalar_form_exprloc
22656 | dw_scalar_form_reference);
22658 add_scalar_info (array_die, attr, info->stride, forms, &context);
22661 if (dwarf_version >= 5)
22663 if (info->rank)
22665 add_scalar_info (array_die, DW_AT_rank, info->rank,
22666 dw_scalar_form_constant
22667 | dw_scalar_form_exprloc, &context);
22668 subrange_tag = DW_TAG_generic_subrange;
22669 context.placeholder_arg = true;
22673 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22675 for (dim = 0; dim < info->ndimensions; dim++)
22677 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22679 if (info->dimen[dim].bounds_type)
22680 add_type_attribute (subrange_die,
22681 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22682 false, context_die);
22683 if (info->dimen[dim].lower_bound)
22684 add_bound_info (subrange_die, DW_AT_lower_bound,
22685 info->dimen[dim].lower_bound, &context);
22686 if (info->dimen[dim].upper_bound)
22687 add_bound_info (subrange_die, DW_AT_upper_bound,
22688 info->dimen[dim].upper_bound, &context);
22689 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22690 add_scalar_info (subrange_die, DW_AT_byte_stride,
22691 info->dimen[dim].stride,
22692 dw_scalar_form_constant
22693 | dw_scalar_form_exprloc
22694 | dw_scalar_form_reference,
22695 &context);
22698 gen_type_die (info->element_type, context_die);
22699 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22700 TREE_CODE (type) == ARRAY_TYPE
22701 && TYPE_REVERSE_STORAGE_ORDER (type),
22702 context_die);
22704 if (get_AT (array_die, DW_AT_name))
22705 add_pubtype (type, array_die);
22707 add_alignment_attribute (array_die, type);
22710 #if 0
22711 static void
22712 gen_entry_point_die (tree decl, dw_die_ref context_die)
22714 tree origin = decl_ultimate_origin (decl);
22715 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22717 if (origin != NULL)
22718 add_abstract_origin_attribute (decl_die, origin);
22719 else
22721 add_name_and_src_coords_attributes (decl_die, decl);
22722 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22723 TYPE_UNQUALIFIED, false, context_die);
22726 if (DECL_ABSTRACT_P (decl))
22727 equate_decl_number_to_die (decl, decl_die);
22728 else
22729 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22731 #endif
22733 /* Walk through the list of incomplete types again, trying once more to
22734 emit full debugging info for them. */
22736 static void
22737 retry_incomplete_types (void)
22739 set_early_dwarf s;
22740 int i;
22742 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22743 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22744 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22745 vec_safe_truncate (incomplete_types, 0);
22748 /* Determine what tag to use for a record type. */
22750 static enum dwarf_tag
22751 record_type_tag (tree type)
22753 if (! lang_hooks.types.classify_record)
22754 return DW_TAG_structure_type;
22756 switch (lang_hooks.types.classify_record (type))
22758 case RECORD_IS_STRUCT:
22759 return DW_TAG_structure_type;
22761 case RECORD_IS_CLASS:
22762 return DW_TAG_class_type;
22764 case RECORD_IS_INTERFACE:
22765 if (dwarf_version >= 3 || !dwarf_strict)
22766 return DW_TAG_interface_type;
22767 return DW_TAG_structure_type;
22769 default:
22770 gcc_unreachable ();
22774 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22775 include all of the information about the enumeration values also. Each
22776 enumerated type name/value is listed as a child of the enumerated type
22777 DIE. */
22779 static dw_die_ref
22780 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22782 dw_die_ref type_die = lookup_type_die (type);
22783 dw_die_ref orig_type_die = type_die;
22785 if (type_die == NULL)
22787 type_die = new_die (DW_TAG_enumeration_type,
22788 scope_die_for (type, context_die), type);
22789 equate_type_number_to_die (type, type_die);
22790 add_name_attribute (type_die, type_tag (type));
22791 if ((dwarf_version >= 4 || !dwarf_strict)
22792 && ENUM_IS_SCOPED (type))
22793 add_AT_flag (type_die, DW_AT_enum_class, 1);
22794 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22795 add_AT_flag (type_die, DW_AT_declaration, 1);
22796 if (!dwarf_strict)
22797 add_AT_unsigned (type_die, DW_AT_encoding,
22798 TYPE_UNSIGNED (type)
22799 ? DW_ATE_unsigned
22800 : DW_ATE_signed);
22802 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22803 return type_die;
22804 else
22805 remove_AT (type_die, DW_AT_declaration);
22807 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22808 given enum type is incomplete, do not generate the DW_AT_byte_size
22809 attribute or the DW_AT_element_list attribute. */
22810 if (TYPE_SIZE (type))
22812 tree link;
22814 if (!ENUM_IS_OPAQUE (type))
22815 TREE_ASM_WRITTEN (type) = 1;
22816 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22817 add_byte_size_attribute (type_die, type);
22818 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22819 add_alignment_attribute (type_die, type);
22820 if ((dwarf_version >= 3 || !dwarf_strict)
22821 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22823 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22824 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22825 context_die);
22827 if (TYPE_STUB_DECL (type) != NULL_TREE)
22829 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22830 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22831 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22832 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22835 /* If the first reference to this type was as the return type of an
22836 inline function, then it may not have a parent. Fix this now. */
22837 if (type_die->die_parent == NULL)
22838 add_child_die (scope_die_for (type, context_die), type_die);
22840 for (link = TYPE_VALUES (type);
22841 link != NULL; link = TREE_CHAIN (link))
22843 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22844 tree value = TREE_VALUE (link);
22846 if (DECL_P (value))
22847 equate_decl_number_to_die (value, enum_die);
22849 gcc_assert (!ENUM_IS_OPAQUE (type));
22850 add_name_attribute (enum_die,
22851 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22853 if (TREE_CODE (value) == CONST_DECL)
22854 value = DECL_INITIAL (value);
22856 if (simple_type_size_in_bits (TREE_TYPE (value))
22857 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22859 /* For constant forms created by add_AT_unsigned DWARF
22860 consumers (GDB, elfutils, etc.) always zero extend
22861 the value. Only when the actual value is negative
22862 do we need to use add_AT_int to generate a constant
22863 form that can represent negative values. */
22864 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22865 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22866 add_AT_unsigned (enum_die, DW_AT_const_value,
22867 (unsigned HOST_WIDE_INT) val);
22868 else
22869 add_AT_int (enum_die, DW_AT_const_value, val);
22871 else
22872 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22873 that here. TODO: This should be re-worked to use correct
22874 signed/unsigned double tags for all cases. */
22875 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22878 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22879 if (TYPE_ARTIFICIAL (type)
22880 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22881 add_AT_flag (type_die, DW_AT_artificial, 1);
22883 else
22884 add_AT_flag (type_die, DW_AT_declaration, 1);
22886 add_pubtype (type, type_die);
22888 return type_die;
22891 /* Generate a DIE to represent either a real live formal parameter decl or to
22892 represent just the type of some formal parameter position in some function
22893 type.
22895 Note that this routine is a bit unusual because its argument may be a
22896 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22897 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22898 node. If it's the former then this function is being called to output a
22899 DIE to represent a formal parameter object (or some inlining thereof). If
22900 it's the latter, then this function is only being called to output a
22901 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22902 argument type of some subprogram type.
22903 If EMIT_NAME_P is true, name and source coordinate attributes
22904 are emitted. */
22906 static dw_die_ref
22907 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22908 dw_die_ref context_die)
22910 tree node_or_origin = node ? node : origin;
22911 tree ultimate_origin;
22912 dw_die_ref parm_die = NULL;
22914 if (DECL_P (node_or_origin))
22916 parm_die = lookup_decl_die (node);
22918 /* If the contexts differ, we may not be talking about the same
22919 thing.
22920 ??? When in LTO the DIE parent is the "abstract" copy and the
22921 context_die is the specification "copy". */
22922 if (parm_die
22923 && parm_die->die_parent != context_die
22924 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22925 || parm_die->die_parent->die_parent != context_die)
22926 && !in_lto_p)
22928 gcc_assert (!DECL_ABSTRACT_P (node));
22929 /* This can happen when creating a concrete instance, in
22930 which case we need to create a new DIE that will get
22931 annotated with DW_AT_abstract_origin. */
22932 parm_die = NULL;
22935 if (parm_die && parm_die->die_parent == NULL)
22937 /* Check that parm_die already has the right attributes that
22938 we would have added below. If any attributes are
22939 missing, fall through to add them. */
22940 if (! DECL_ABSTRACT_P (node_or_origin)
22941 && !get_AT (parm_die, DW_AT_location)
22942 && !get_AT (parm_die, DW_AT_const_value))
22943 /* We are missing location info, and are about to add it. */
22945 else
22947 add_child_die (context_die, parm_die);
22948 return parm_die;
22953 /* If we have a previously generated DIE, use it, unless this is an
22954 concrete instance (origin != NULL), in which case we need a new
22955 DIE with a corresponding DW_AT_abstract_origin. */
22956 bool reusing_die;
22957 if (parm_die && origin == NULL)
22958 reusing_die = true;
22959 else
22961 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22962 reusing_die = false;
22965 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22967 case tcc_declaration:
22968 ultimate_origin = decl_ultimate_origin (node_or_origin);
22969 if (node || ultimate_origin)
22970 origin = ultimate_origin;
22972 if (reusing_die)
22973 goto add_location;
22975 if (origin != NULL)
22976 add_abstract_origin_attribute (parm_die, origin);
22977 else if (emit_name_p)
22978 add_name_and_src_coords_attributes (parm_die, node);
22979 if (origin == NULL
22980 || (! DECL_ABSTRACT_P (node_or_origin)
22981 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22982 decl_function_context
22983 (node_or_origin))))
22985 tree type = TREE_TYPE (node_or_origin);
22986 if (decl_by_reference_p (node_or_origin))
22987 add_type_attribute (parm_die, TREE_TYPE (type),
22988 TYPE_UNQUALIFIED,
22989 false, context_die);
22990 else
22991 add_type_attribute (parm_die, type,
22992 decl_quals (node_or_origin),
22993 false, context_die);
22995 if (origin == NULL && DECL_ARTIFICIAL (node))
22996 add_AT_flag (parm_die, DW_AT_artificial, 1);
22997 add_location:
22998 if (node && node != origin)
22999 equate_decl_number_to_die (node, parm_die);
23000 if (! DECL_ABSTRACT_P (node_or_origin))
23001 add_location_or_const_value_attribute (parm_die, node_or_origin,
23002 node == NULL);
23004 break;
23006 case tcc_type:
23007 /* We were called with some kind of a ..._TYPE node. */
23008 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
23009 context_die);
23010 break;
23012 default:
23013 gcc_unreachable ();
23016 return parm_die;
23019 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23020 children DW_TAG_formal_parameter DIEs representing the arguments of the
23021 parameter pack.
23023 PARM_PACK must be a function parameter pack.
23024 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23025 must point to the subsequent arguments of the function PACK_ARG belongs to.
23026 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23027 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23028 following the last one for which a DIE was generated. */
23030 static dw_die_ref
23031 gen_formal_parameter_pack_die (tree parm_pack,
23032 tree pack_arg,
23033 dw_die_ref subr_die,
23034 tree *next_arg)
23036 tree arg;
23037 dw_die_ref parm_pack_die;
23039 gcc_assert (parm_pack
23040 && lang_hooks.function_parameter_pack_p (parm_pack)
23041 && subr_die);
23043 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23044 add_src_coords_attributes (parm_pack_die, parm_pack);
23046 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23048 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23049 parm_pack))
23050 break;
23051 gen_formal_parameter_die (arg, NULL,
23052 false /* Don't emit name attribute. */,
23053 parm_pack_die);
23055 if (next_arg)
23056 *next_arg = arg;
23057 return parm_pack_die;
23060 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23061 at the end of an (ANSI prototyped) formal parameters list. */
23063 static void
23064 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23066 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23069 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23070 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23071 parameters as specified in some function type specification (except for
23072 those which appear as part of a function *definition*). */
23074 static void
23075 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23077 tree link;
23078 tree formal_type = NULL;
23079 tree first_parm_type;
23080 tree arg;
23082 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23084 arg = DECL_ARGUMENTS (function_or_method_type);
23085 function_or_method_type = TREE_TYPE (function_or_method_type);
23087 else
23088 arg = NULL_TREE;
23090 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23092 /* Make our first pass over the list of formal parameter types and output a
23093 DW_TAG_formal_parameter DIE for each one. */
23094 for (link = first_parm_type; link; )
23096 dw_die_ref parm_die;
23098 formal_type = TREE_VALUE (link);
23099 if (formal_type == void_type_node)
23100 break;
23102 /* Output a (nameless) DIE to represent the formal parameter itself. */
23103 parm_die = gen_formal_parameter_die (formal_type, NULL,
23104 true /* Emit name attribute. */,
23105 context_die);
23106 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23107 && link == first_parm_type)
23109 add_AT_flag (parm_die, DW_AT_artificial, 1);
23110 if (dwarf_version >= 3 || !dwarf_strict)
23111 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23113 else if (arg && DECL_ARTIFICIAL (arg))
23114 add_AT_flag (parm_die, DW_AT_artificial, 1);
23116 link = TREE_CHAIN (link);
23117 if (arg)
23118 arg = DECL_CHAIN (arg);
23121 /* If this function type has an ellipsis, add a
23122 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23123 if (formal_type != void_type_node)
23124 gen_unspecified_parameters_die (function_or_method_type, context_die);
23126 /* Make our second (and final) pass over the list of formal parameter types
23127 and output DIEs to represent those types (as necessary). */
23128 for (link = TYPE_ARG_TYPES (function_or_method_type);
23129 link && TREE_VALUE (link);
23130 link = TREE_CHAIN (link))
23131 gen_type_die (TREE_VALUE (link), context_die);
23134 /* We want to generate the DIE for TYPE so that we can generate the
23135 die for MEMBER, which has been defined; we will need to refer back
23136 to the member declaration nested within TYPE. If we're trying to
23137 generate minimal debug info for TYPE, processing TYPE won't do the
23138 trick; we need to attach the member declaration by hand. */
23140 static void
23141 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23143 gen_type_die (type, context_die);
23145 /* If we're trying to avoid duplicate debug info, we may not have
23146 emitted the member decl for this function. Emit it now. */
23147 if (TYPE_STUB_DECL (type)
23148 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23149 && ! lookup_decl_die (member))
23151 dw_die_ref type_die;
23152 gcc_assert (!decl_ultimate_origin (member));
23154 type_die = lookup_type_die_strip_naming_typedef (type);
23155 if (TREE_CODE (member) == FUNCTION_DECL)
23156 gen_subprogram_die (member, type_die);
23157 else if (TREE_CODE (member) == FIELD_DECL)
23159 /* Ignore the nameless fields that are used to skip bits but handle
23160 C++ anonymous unions and structs. */
23161 if (DECL_NAME (member) != NULL_TREE
23162 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23163 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23165 struct vlr_context vlr_ctx = {
23166 DECL_CONTEXT (member), /* struct_type */
23167 NULL_TREE /* variant_part_offset */
23169 gen_type_die (member_declared_type (member), type_die);
23170 gen_field_die (member, &vlr_ctx, type_die);
23173 else
23174 gen_variable_die (member, NULL_TREE, type_die);
23178 /* Forward declare these functions, because they are mutually recursive
23179 with their set_block_* pairing functions. */
23180 static void set_decl_origin_self (tree);
23182 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23183 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23184 that it points to the node itself, thus indicating that the node is its
23185 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23186 the given node is NULL, recursively descend the decl/block tree which
23187 it is the root of, and for each other ..._DECL or BLOCK node contained
23188 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23189 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23190 values to point to themselves. */
23192 static void
23193 set_block_origin_self (tree stmt)
23195 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23197 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23200 tree local_decl;
23202 for (local_decl = BLOCK_VARS (stmt);
23203 local_decl != NULL_TREE;
23204 local_decl = DECL_CHAIN (local_decl))
23205 /* Do not recurse on nested functions since the inlining status
23206 of parent and child can be different as per the DWARF spec. */
23207 if (TREE_CODE (local_decl) != FUNCTION_DECL
23208 && !DECL_EXTERNAL (local_decl))
23209 set_decl_origin_self (local_decl);
23213 tree subblock;
23215 for (subblock = BLOCK_SUBBLOCKS (stmt);
23216 subblock != NULL_TREE;
23217 subblock = BLOCK_CHAIN (subblock))
23218 set_block_origin_self (subblock); /* Recurse. */
23223 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23224 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23225 node to so that it points to the node itself, thus indicating that the
23226 node represents its own (abstract) origin. Additionally, if the
23227 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23228 the decl/block tree of which the given node is the root of, and for
23229 each other ..._DECL or BLOCK node contained therein whose
23230 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23231 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23232 point to themselves. */
23234 static void
23235 set_decl_origin_self (tree decl)
23237 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23239 DECL_ABSTRACT_ORIGIN (decl) = decl;
23240 if (TREE_CODE (decl) == FUNCTION_DECL)
23242 tree arg;
23244 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23245 DECL_ABSTRACT_ORIGIN (arg) = arg;
23246 if (DECL_INITIAL (decl) != NULL_TREE
23247 && DECL_INITIAL (decl) != error_mark_node)
23248 set_block_origin_self (DECL_INITIAL (decl));
23253 /* Mark the early DIE for DECL as the abstract instance. */
23255 static void
23256 dwarf2out_abstract_function (tree decl)
23258 dw_die_ref old_die;
23260 /* Make sure we have the actual abstract inline, not a clone. */
23261 decl = DECL_ORIGIN (decl);
23263 if (DECL_IGNORED_P (decl))
23264 return;
23266 /* In LTO we're all set. We already created abstract instances
23267 early and we want to avoid creating a concrete instance of that
23268 if we don't output it. */
23269 if (in_lto_p)
23270 return;
23272 old_die = lookup_decl_die (decl);
23273 gcc_assert (old_die != NULL);
23274 if (get_AT (old_die, DW_AT_inline))
23275 /* We've already generated the abstract instance. */
23276 return;
23278 /* Go ahead and put DW_AT_inline on the DIE. */
23279 if (DECL_DECLARED_INLINE_P (decl))
23281 if (cgraph_function_possibly_inlined_p (decl))
23282 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23283 else
23284 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23286 else
23288 if (cgraph_function_possibly_inlined_p (decl))
23289 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23290 else
23291 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23294 if (DECL_DECLARED_INLINE_P (decl)
23295 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23296 add_AT_flag (old_die, DW_AT_artificial, 1);
23298 set_decl_origin_self (decl);
23301 /* Helper function of premark_used_types() which gets called through
23302 htab_traverse.
23304 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23305 marked as unused by prune_unused_types. */
23307 bool
23308 premark_used_types_helper (tree const &type, void *)
23310 dw_die_ref die;
23312 die = lookup_type_die (type);
23313 if (die != NULL)
23314 die->die_perennial_p = 1;
23315 return true;
23318 /* Helper function of premark_types_used_by_global_vars which gets called
23319 through htab_traverse.
23321 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23322 marked as unused by prune_unused_types. The DIE of the type is marked
23323 only if the global variable using the type will actually be emitted. */
23326 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23327 void *)
23329 struct types_used_by_vars_entry *entry;
23330 dw_die_ref die;
23332 entry = (struct types_used_by_vars_entry *) *slot;
23333 gcc_assert (entry->type != NULL
23334 && entry->var_decl != NULL);
23335 die = lookup_type_die (entry->type);
23336 if (die)
23338 /* Ask cgraph if the global variable really is to be emitted.
23339 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23340 varpool_node *node = varpool_node::get (entry->var_decl);
23341 if (node && node->definition)
23343 die->die_perennial_p = 1;
23344 /* Keep the parent DIEs as well. */
23345 while ((die = die->die_parent) && die->die_perennial_p == 0)
23346 die->die_perennial_p = 1;
23349 return 1;
23352 /* Mark all members of used_types_hash as perennial. */
23354 static void
23355 premark_used_types (struct function *fun)
23357 if (fun && fun->used_types_hash)
23358 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23361 /* Mark all members of types_used_by_vars_entry as perennial. */
23363 static void
23364 premark_types_used_by_global_vars (void)
23366 if (types_used_by_vars_hash)
23367 types_used_by_vars_hash
23368 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23371 /* Mark all variables used by the symtab as perennial. */
23373 static void
23374 premark_used_variables (void)
23376 /* Mark DIEs in the symtab as used. */
23377 varpool_node *var;
23378 FOR_EACH_VARIABLE (var)
23380 dw_die_ref die = lookup_decl_die (var->decl);
23381 if (die)
23382 die->die_perennial_p = 1;
23386 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23387 for CA_LOC call arg loc node. */
23389 static dw_die_ref
23390 gen_call_site_die (tree decl, dw_die_ref subr_die,
23391 struct call_arg_loc_node *ca_loc)
23393 dw_die_ref stmt_die = NULL, die;
23394 tree block = ca_loc->block;
23396 while (block
23397 && block != DECL_INITIAL (decl)
23398 && TREE_CODE (block) == BLOCK)
23400 stmt_die = lookup_block_die (block);
23401 if (stmt_die)
23402 break;
23403 block = BLOCK_SUPERCONTEXT (block);
23405 if (stmt_die == NULL)
23406 stmt_die = subr_die;
23407 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23408 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23409 if (ca_loc->tail_call_p)
23410 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23411 if (ca_loc->symbol_ref)
23413 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23414 if (tdie)
23415 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23416 else
23417 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23418 false);
23420 return die;
23423 /* Generate a DIE to represent a declared function (either file-scope or
23424 block-local). */
23426 static void
23427 gen_subprogram_die (tree decl, dw_die_ref context_die)
23429 tree origin = decl_ultimate_origin (decl);
23430 dw_die_ref subr_die;
23431 dw_die_ref old_die = lookup_decl_die (decl);
23432 bool old_die_had_no_children = false;
23434 /* This function gets called multiple times for different stages of
23435 the debug process. For example, for func() in this code:
23437 namespace S
23439 void func() { ... }
23442 ...we get called 4 times. Twice in early debug and twice in
23443 late debug:
23445 Early debug
23446 -----------
23448 1. Once while generating func() within the namespace. This is
23449 the declaration. The declaration bit below is set, as the
23450 context is the namespace.
23452 A new DIE will be generated with DW_AT_declaration set.
23454 2. Once for func() itself. This is the specification. The
23455 declaration bit below is clear as the context is the CU.
23457 We will use the cached DIE from (1) to create a new DIE with
23458 DW_AT_specification pointing to the declaration in (1).
23460 Late debug via rest_of_handle_final()
23461 -------------------------------------
23463 3. Once generating func() within the namespace. This is also the
23464 declaration, as in (1), but this time we will early exit below
23465 as we have a cached DIE and a declaration needs no additional
23466 annotations (no locations), as the source declaration line
23467 info is enough.
23469 4. Once for func() itself. As in (2), this is the specification,
23470 but this time we will re-use the cached DIE, and just annotate
23471 it with the location information that should now be available.
23473 For something without namespaces, but with abstract instances, we
23474 are also called a multiple times:
23476 class Base
23478 public:
23479 Base (); // constructor declaration (1)
23482 Base::Base () { } // constructor specification (2)
23484 Early debug
23485 -----------
23487 1. Once for the Base() constructor by virtue of it being a
23488 member of the Base class. This is done via
23489 rest_of_type_compilation.
23491 This is a declaration, so a new DIE will be created with
23492 DW_AT_declaration.
23494 2. Once for the Base() constructor definition, but this time
23495 while generating the abstract instance of the base
23496 constructor (__base_ctor) which is being generated via early
23497 debug of reachable functions.
23499 Even though we have a cached version of the declaration (1),
23500 we will create a DW_AT_specification of the declaration DIE
23501 in (1).
23503 3. Once for the __base_ctor itself, but this time, we generate
23504 an DW_AT_abstract_origin version of the DW_AT_specification in
23505 (2).
23507 Late debug via rest_of_handle_final
23508 -----------------------------------
23510 4. One final time for the __base_ctor (which will have a cached
23511 DIE with DW_AT_abstract_origin created in (3). This time,
23512 we will just annotate the location information now
23513 available.
23515 int declaration = (current_function_decl != decl
23516 || (!DECL_INITIAL (decl) && !origin)
23517 || class_or_namespace_scope_p (context_die));
23519 /* A declaration that has been previously dumped needs no
23520 additional information. */
23521 if (old_die && declaration)
23522 return;
23524 if (in_lto_p && old_die && old_die->die_child == NULL)
23525 old_die_had_no_children = true;
23527 /* Now that the C++ front end lazily declares artificial member fns, we
23528 might need to retrofit the declaration into its class. */
23529 if (!declaration && !origin && !old_die
23530 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23531 && !class_or_namespace_scope_p (context_die)
23532 && debug_info_level > DINFO_LEVEL_TERSE)
23533 old_die = force_decl_die (decl);
23535 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23536 if (origin != NULL)
23538 gcc_assert (!declaration || local_scope_p (context_die));
23540 /* Fixup die_parent for the abstract instance of a nested
23541 inline function. */
23542 if (old_die && old_die->die_parent == NULL)
23543 add_child_die (context_die, old_die);
23545 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23547 /* If we have a DW_AT_abstract_origin we have a working
23548 cached version. */
23549 subr_die = old_die;
23551 else
23553 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23554 add_abstract_origin_attribute (subr_die, origin);
23555 /* This is where the actual code for a cloned function is.
23556 Let's emit linkage name attribute for it. This helps
23557 debuggers to e.g, set breakpoints into
23558 constructors/destructors when the user asks "break
23559 K::K". */
23560 add_linkage_name (subr_die, decl);
23563 /* A cached copy, possibly from early dwarf generation. Reuse as
23564 much as possible. */
23565 else if (old_die)
23567 if (!get_AT_flag (old_die, DW_AT_declaration)
23568 /* We can have a normal definition following an inline one in the
23569 case of redefinition of GNU C extern inlines.
23570 It seems reasonable to use AT_specification in this case. */
23571 && !get_AT (old_die, DW_AT_inline))
23573 /* Detect and ignore this case, where we are trying to output
23574 something we have already output. */
23575 if (get_AT (old_die, DW_AT_low_pc)
23576 || get_AT (old_die, DW_AT_ranges))
23577 return;
23579 /* If we have no location information, this must be a
23580 partially generated DIE from early dwarf generation.
23581 Fall through and generate it. */
23584 /* If the definition comes from the same place as the declaration,
23585 maybe use the old DIE. We always want the DIE for this function
23586 that has the *_pc attributes to be under comp_unit_die so the
23587 debugger can find it. We also need to do this for abstract
23588 instances of inlines, since the spec requires the out-of-line copy
23589 to have the same parent. For local class methods, this doesn't
23590 apply; we just use the old DIE. */
23591 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23592 struct dwarf_file_data * file_index = lookup_filename (s.file);
23593 if (((is_unit_die (old_die->die_parent)
23594 /* This condition fixes the inconsistency/ICE with the
23595 following Fortran test (or some derivative thereof) while
23596 building libgfortran:
23598 module some_m
23599 contains
23600 logical function funky (FLAG)
23601 funky = .true.
23602 end function
23603 end module
23605 || (old_die->die_parent
23606 && old_die->die_parent->die_tag == DW_TAG_module)
23607 || local_scope_p (old_die->die_parent)
23608 || context_die == NULL)
23609 && (DECL_ARTIFICIAL (decl)
23610 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23611 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23612 == (unsigned) s.line)
23613 && (!debug_column_info
23614 || s.column == 0
23615 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23616 == (unsigned) s.column)))))
23617 /* With LTO if there's an abstract instance for
23618 the old DIE, this is a concrete instance and
23619 thus re-use the DIE. */
23620 || get_AT (old_die, DW_AT_abstract_origin))
23622 subr_die = old_die;
23624 /* Clear out the declaration attribute, but leave the
23625 parameters so they can be augmented with location
23626 information later. Unless this was a declaration, in
23627 which case, wipe out the nameless parameters and recreate
23628 them further down. */
23629 if (remove_AT (subr_die, DW_AT_declaration))
23632 remove_AT (subr_die, DW_AT_object_pointer);
23633 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23636 /* Make a specification pointing to the previously built
23637 declaration. */
23638 else
23640 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23641 add_AT_specification (subr_die, old_die);
23642 add_pubname (decl, subr_die);
23643 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23644 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23645 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23646 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23647 if (debug_column_info
23648 && s.column
23649 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23650 != (unsigned) s.column))
23651 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23653 /* If the prototype had an 'auto' or 'decltype(auto)' in
23654 the return type, emit the real type on the definition die. */
23655 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23657 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23658 while (die
23659 && (die->die_tag == DW_TAG_reference_type
23660 || die->die_tag == DW_TAG_rvalue_reference_type
23661 || die->die_tag == DW_TAG_pointer_type
23662 || die->die_tag == DW_TAG_const_type
23663 || die->die_tag == DW_TAG_volatile_type
23664 || die->die_tag == DW_TAG_restrict_type
23665 || die->die_tag == DW_TAG_array_type
23666 || die->die_tag == DW_TAG_ptr_to_member_type
23667 || die->die_tag == DW_TAG_subroutine_type))
23668 die = get_AT_ref (die, DW_AT_type);
23669 if (die == auto_die || die == decltype_auto_die)
23670 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23671 TYPE_UNQUALIFIED, false, context_die);
23674 /* When we process the method declaration, we haven't seen
23675 the out-of-class defaulted definition yet, so we have to
23676 recheck now. */
23677 if ((dwarf_version >= 5 || ! dwarf_strict)
23678 && !get_AT (subr_die, DW_AT_defaulted))
23680 int defaulted
23681 = lang_hooks.decls.decl_dwarf_attribute (decl,
23682 DW_AT_defaulted);
23683 if (defaulted != -1)
23685 /* Other values must have been handled before. */
23686 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23687 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23692 /* Create a fresh DIE for anything else. */
23693 else
23695 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23697 if (TREE_PUBLIC (decl))
23698 add_AT_flag (subr_die, DW_AT_external, 1);
23700 add_name_and_src_coords_attributes (subr_die, decl);
23701 add_pubname (decl, subr_die);
23702 if (debug_info_level > DINFO_LEVEL_TERSE)
23704 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23705 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23706 TYPE_UNQUALIFIED, false, context_die);
23709 add_pure_or_virtual_attribute (subr_die, decl);
23710 if (DECL_ARTIFICIAL (decl))
23711 add_AT_flag (subr_die, DW_AT_artificial, 1);
23713 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23714 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23716 add_alignment_attribute (subr_die, decl);
23718 add_accessibility_attribute (subr_die, decl);
23721 /* Unless we have an existing non-declaration DIE, equate the new
23722 DIE. */
23723 if (!old_die || is_declaration_die (old_die))
23724 equate_decl_number_to_die (decl, subr_die);
23726 if (declaration)
23728 if (!old_die || !get_AT (old_die, DW_AT_inline))
23730 add_AT_flag (subr_die, DW_AT_declaration, 1);
23732 /* If this is an explicit function declaration then generate
23733 a DW_AT_explicit attribute. */
23734 if ((dwarf_version >= 3 || !dwarf_strict)
23735 && lang_hooks.decls.decl_dwarf_attribute (decl,
23736 DW_AT_explicit) == 1)
23737 add_AT_flag (subr_die, DW_AT_explicit, 1);
23739 /* If this is a C++11 deleted special function member then generate
23740 a DW_AT_deleted attribute. */
23741 if ((dwarf_version >= 5 || !dwarf_strict)
23742 && lang_hooks.decls.decl_dwarf_attribute (decl,
23743 DW_AT_deleted) == 1)
23744 add_AT_flag (subr_die, DW_AT_deleted, 1);
23746 /* If this is a C++11 defaulted special function member then
23747 generate a DW_AT_defaulted attribute. */
23748 if (dwarf_version >= 5 || !dwarf_strict)
23750 int defaulted
23751 = lang_hooks.decls.decl_dwarf_attribute (decl,
23752 DW_AT_defaulted);
23753 if (defaulted != -1)
23754 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23757 /* If this is a C++11 non-static member function with & ref-qualifier
23758 then generate a DW_AT_reference attribute. */
23759 if ((dwarf_version >= 5 || !dwarf_strict)
23760 && lang_hooks.decls.decl_dwarf_attribute (decl,
23761 DW_AT_reference) == 1)
23762 add_AT_flag (subr_die, DW_AT_reference, 1);
23764 /* If this is a C++11 non-static member function with &&
23765 ref-qualifier then generate a DW_AT_reference attribute. */
23766 if ((dwarf_version >= 5 || !dwarf_strict)
23767 && lang_hooks.decls.decl_dwarf_attribute (decl,
23768 DW_AT_rvalue_reference)
23769 == 1)
23770 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23773 /* For non DECL_EXTERNALs, if range information is available, fill
23774 the DIE with it. */
23775 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23777 HOST_WIDE_INT cfa_fb_offset;
23779 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23781 if (!crtl->has_bb_partition)
23783 dw_fde_ref fde = fun->fde;
23784 if (fde->dw_fde_begin)
23786 /* We have already generated the labels. */
23787 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23788 fde->dw_fde_end, false);
23790 else
23792 /* Create start/end labels and add the range. */
23793 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23794 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23795 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23796 current_function_funcdef_no);
23797 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23798 current_function_funcdef_no);
23799 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23800 false);
23803 #if VMS_DEBUGGING_INFO
23804 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23805 Section 2.3 Prologue and Epilogue Attributes:
23806 When a breakpoint is set on entry to a function, it is generally
23807 desirable for execution to be suspended, not on the very first
23808 instruction of the function, but rather at a point after the
23809 function's frame has been set up, after any language defined local
23810 declaration processing has been completed, and before execution of
23811 the first statement of the function begins. Debuggers generally
23812 cannot properly determine where this point is. Similarly for a
23813 breakpoint set on exit from a function. The prologue and epilogue
23814 attributes allow a compiler to communicate the location(s) to use. */
23817 if (fde->dw_fde_vms_end_prologue)
23818 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23819 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23821 if (fde->dw_fde_vms_begin_epilogue)
23822 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23823 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23825 #endif
23828 else
23830 /* Generate pubnames entries for the split function code ranges. */
23831 dw_fde_ref fde = fun->fde;
23833 if (fde->dw_fde_second_begin)
23835 if (dwarf_version >= 3 || !dwarf_strict)
23837 /* We should use ranges for non-contiguous code section
23838 addresses. Use the actual code range for the initial
23839 section, since the HOT/COLD labels might precede an
23840 alignment offset. */
23841 bool range_list_added = false;
23842 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23843 fde->dw_fde_end, &range_list_added,
23844 false);
23845 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23846 fde->dw_fde_second_end,
23847 &range_list_added, false);
23848 if (range_list_added)
23849 add_ranges (NULL);
23851 else
23853 /* There is no real support in DW2 for this .. so we make
23854 a work-around. First, emit the pub name for the segment
23855 containing the function label. Then make and emit a
23856 simplified subprogram DIE for the second segment with the
23857 name pre-fixed by __hot/cold_sect_of_. We use the same
23858 linkage name for the second die so that gdb will find both
23859 sections when given "b foo". */
23860 const char *name = NULL;
23861 tree decl_name = DECL_NAME (decl);
23862 dw_die_ref seg_die;
23864 /* Do the 'primary' section. */
23865 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23866 fde->dw_fde_end, false);
23868 /* Build a minimal DIE for the secondary section. */
23869 seg_die = new_die (DW_TAG_subprogram,
23870 subr_die->die_parent, decl);
23872 if (TREE_PUBLIC (decl))
23873 add_AT_flag (seg_die, DW_AT_external, 1);
23875 if (decl_name != NULL
23876 && IDENTIFIER_POINTER (decl_name) != NULL)
23878 name = dwarf2_name (decl, 1);
23879 if (! DECL_ARTIFICIAL (decl))
23880 add_src_coords_attributes (seg_die, decl);
23882 add_linkage_name (seg_die, decl);
23884 gcc_assert (name != NULL);
23885 add_pure_or_virtual_attribute (seg_die, decl);
23886 if (DECL_ARTIFICIAL (decl))
23887 add_AT_flag (seg_die, DW_AT_artificial, 1);
23889 name = concat ("__second_sect_of_", name, NULL);
23890 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23891 fde->dw_fde_second_end, false);
23892 add_name_attribute (seg_die, name);
23893 if (want_pubnames ())
23894 add_pubname_string (name, seg_die);
23897 else
23898 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23899 false);
23902 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23904 /* We define the "frame base" as the function's CFA. This is more
23905 convenient for several reasons: (1) It's stable across the prologue
23906 and epilogue, which makes it better than just a frame pointer,
23907 (2) With dwarf3, there exists a one-byte encoding that allows us
23908 to reference the .debug_frame data by proxy, but failing that,
23909 (3) We can at least reuse the code inspection and interpretation
23910 code that determines the CFA position at various points in the
23911 function. */
23912 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23914 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23915 add_AT_loc (subr_die, DW_AT_frame_base, op);
23917 else
23919 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23920 if (list->dw_loc_next)
23921 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23922 else
23923 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23926 /* Compute a displacement from the "steady-state frame pointer" to
23927 the CFA. The former is what all stack slots and argument slots
23928 will reference in the rtl; the latter is what we've told the
23929 debugger about. We'll need to adjust all frame_base references
23930 by this displacement. */
23931 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23933 if (fun->static_chain_decl)
23935 /* DWARF requires here a location expression that computes the
23936 address of the enclosing subprogram's frame base. The machinery
23937 in tree-nested.cc is supposed to store this specific address in the
23938 last field of the FRAME record. */
23939 const tree frame_type
23940 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23941 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23943 tree fb_expr
23944 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23945 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23946 fb_expr, fb_decl, NULL_TREE);
23948 add_AT_location_description (subr_die, DW_AT_static_link,
23949 loc_list_from_tree (fb_expr, 0, NULL));
23952 resolve_variable_values ();
23955 /* Generate child dies for template parameters. */
23956 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23957 gen_generic_params_dies (decl);
23959 /* Now output descriptions of the arguments for this function. This gets
23960 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23961 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23962 `...' at the end of the formal parameter list. In order to find out if
23963 there was a trailing ellipsis or not, we must instead look at the type
23964 associated with the FUNCTION_DECL. This will be a node of type
23965 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23966 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23967 an ellipsis at the end. */
23969 /* In the case where we are describing a mere function declaration, all we
23970 need to do here (and all we *can* do here) is to describe the *types* of
23971 its formal parameters. */
23972 if (debug_info_level <= DINFO_LEVEL_TERSE)
23974 else if (declaration)
23975 gen_formal_types_die (decl, subr_die);
23976 else
23978 /* Generate DIEs to represent all known formal parameters. */
23979 tree parm = DECL_ARGUMENTS (decl);
23980 tree generic_decl = early_dwarf
23981 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23982 tree generic_decl_parm = generic_decl
23983 ? DECL_ARGUMENTS (generic_decl)
23984 : NULL;
23986 /* Now we want to walk the list of parameters of the function and
23987 emit their relevant DIEs.
23989 We consider the case of DECL being an instance of a generic function
23990 as well as it being a normal function.
23992 If DECL is an instance of a generic function we walk the
23993 parameters of the generic function declaration _and_ the parameters of
23994 DECL itself. This is useful because we want to emit specific DIEs for
23995 function parameter packs and those are declared as part of the
23996 generic function declaration. In that particular case,
23997 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23998 That DIE has children DIEs representing the set of arguments
23999 of the pack. Note that the set of pack arguments can be empty.
24000 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
24001 children DIE.
24003 Otherwise, we just consider the parameters of DECL. */
24004 while (generic_decl_parm || parm)
24006 if (generic_decl_parm
24007 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
24008 gen_formal_parameter_pack_die (generic_decl_parm,
24009 parm, subr_die,
24010 &parm);
24011 else if (parm)
24013 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24015 if (early_dwarf
24016 && parm == DECL_ARGUMENTS (decl)
24017 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24018 && parm_die
24019 && (dwarf_version >= 3 || !dwarf_strict))
24020 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24022 parm = DECL_CHAIN (parm);
24025 if (generic_decl_parm)
24026 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24029 /* Decide whether we need an unspecified_parameters DIE at the end.
24030 There are 2 more cases to do this for: 1) the ansi ... declaration -
24031 this is detectable when the end of the arg list is not a
24032 void_type_node 2) an unprototyped function declaration (not a
24033 definition). This just means that we have no info about the
24034 parameters at all. */
24035 if (early_dwarf)
24037 if (prototype_p (TREE_TYPE (decl)))
24039 /* This is the prototyped case, check for.... */
24040 if (stdarg_p (TREE_TYPE (decl)))
24041 gen_unspecified_parameters_die (decl, subr_die);
24043 else if (DECL_INITIAL (decl) == NULL_TREE)
24044 gen_unspecified_parameters_die (decl, subr_die);
24046 else if ((subr_die != old_die || old_die_had_no_children)
24047 && prototype_p (TREE_TYPE (decl))
24048 && stdarg_p (TREE_TYPE (decl)))
24049 gen_unspecified_parameters_die (decl, subr_die);
24052 if (subr_die != old_die)
24053 /* Add the calling convention attribute if requested. */
24054 add_calling_convention_attribute (subr_die, decl);
24056 /* Output Dwarf info for all of the stuff within the body of the function
24057 (if it has one - it may be just a declaration).
24059 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24060 a function. This BLOCK actually represents the outermost binding contour
24061 for the function, i.e. the contour in which the function's formal
24062 parameters and labels get declared. Curiously, it appears that the front
24063 end doesn't actually put the PARM_DECL nodes for the current function onto
24064 the BLOCK_VARS list for this outer scope, but are strung off of the
24065 DECL_ARGUMENTS list for the function instead.
24067 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24068 the LABEL_DECL nodes for the function however, and we output DWARF info
24069 for those in decls_for_scope. Just within the `outer_scope' there will be
24070 a BLOCK node representing the function's outermost pair of curly braces,
24071 and any blocks used for the base and member initializers of a C++
24072 constructor function. */
24073 tree outer_scope = DECL_INITIAL (decl);
24074 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24076 int call_site_note_count = 0;
24077 int tail_call_site_note_count = 0;
24079 /* Emit a DW_TAG_variable DIE for a named return value. */
24080 if (DECL_NAME (DECL_RESULT (decl)))
24081 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24083 /* The first time through decls_for_scope we will generate the
24084 DIEs for the locals. The second time, we fill in the
24085 location info. */
24086 decls_for_scope (outer_scope, subr_die);
24088 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24090 struct call_arg_loc_node *ca_loc;
24091 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24093 dw_die_ref die = NULL;
24094 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24095 rtx arg, next_arg;
24096 tree arg_decl = NULL_TREE;
24098 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
24099 ? XEXP (ca_loc->call_arg_loc_note, 0)
24100 : NULL_RTX);
24101 arg; arg = next_arg)
24103 dw_loc_descr_ref reg, val;
24104 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24105 dw_die_ref cdie, tdie = NULL;
24107 next_arg = XEXP (arg, 1);
24108 if (REG_P (XEXP (XEXP (arg, 0), 0))
24109 && next_arg
24110 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24111 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24112 && REGNO (XEXP (XEXP (arg, 0), 0))
24113 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24114 next_arg = XEXP (next_arg, 1);
24115 if (mode == VOIDmode)
24117 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24118 if (mode == VOIDmode)
24119 mode = GET_MODE (XEXP (arg, 0));
24121 if (mode == VOIDmode || mode == BLKmode)
24122 continue;
24123 /* Get dynamic information about call target only if we
24124 have no static information: we cannot generate both
24125 DW_AT_call_origin and DW_AT_call_target
24126 attributes. */
24127 if (ca_loc->symbol_ref == NULL_RTX)
24129 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24131 tloc = XEXP (XEXP (arg, 0), 1);
24132 continue;
24134 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24135 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24137 tlocc = XEXP (XEXP (arg, 0), 1);
24138 continue;
24141 reg = NULL;
24142 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24143 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24144 VAR_INIT_STATUS_INITIALIZED);
24145 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24147 rtx mem = XEXP (XEXP (arg, 0), 0);
24148 reg = mem_loc_descriptor (XEXP (mem, 0),
24149 get_address_mode (mem),
24150 GET_MODE (mem),
24151 VAR_INIT_STATUS_INITIALIZED);
24153 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24154 == DEBUG_PARAMETER_REF)
24156 tree tdecl
24157 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24158 tdie = lookup_decl_die (tdecl);
24159 if (tdie == NULL)
24160 continue;
24161 arg_decl = tdecl;
24163 else
24164 continue;
24165 if (reg == NULL
24166 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24167 != DEBUG_PARAMETER_REF)
24168 continue;
24169 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24170 VOIDmode,
24171 VAR_INIT_STATUS_INITIALIZED);
24172 if (val == NULL)
24173 continue;
24174 if (die == NULL)
24175 die = gen_call_site_die (decl, subr_die, ca_loc);
24176 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24177 NULL_TREE);
24178 add_desc_attribute (cdie, arg_decl);
24179 if (reg != NULL)
24180 add_AT_loc (cdie, DW_AT_location, reg);
24181 else if (tdie != NULL)
24182 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24183 tdie);
24184 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24185 if (next_arg != XEXP (arg, 1))
24187 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24188 if (mode == VOIDmode)
24189 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24190 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24191 0), 1),
24192 mode, VOIDmode,
24193 VAR_INIT_STATUS_INITIALIZED);
24194 if (val != NULL)
24195 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24196 val);
24199 if (die == NULL
24200 && (ca_loc->symbol_ref || tloc))
24201 die = gen_call_site_die (decl, subr_die, ca_loc);
24202 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24204 dw_loc_descr_ref tval = NULL;
24206 if (tloc != NULL_RTX)
24207 tval = mem_loc_descriptor (tloc,
24208 GET_MODE (tloc) == VOIDmode
24209 ? Pmode : GET_MODE (tloc),
24210 VOIDmode,
24211 VAR_INIT_STATUS_INITIALIZED);
24212 if (tval)
24213 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24214 else if (tlocc != NULL_RTX)
24216 tval = mem_loc_descriptor (tlocc,
24217 GET_MODE (tlocc) == VOIDmode
24218 ? Pmode : GET_MODE (tlocc),
24219 VOIDmode,
24220 VAR_INIT_STATUS_INITIALIZED);
24221 if (tval)
24222 add_AT_loc (die,
24223 dwarf_AT (DW_AT_call_target_clobbered),
24224 tval);
24227 if (die != NULL)
24229 call_site_note_count++;
24230 if (ca_loc->tail_call_p)
24231 tail_call_site_note_count++;
24235 call_arg_locations = NULL;
24236 call_arg_loc_last = NULL;
24237 if (tail_call_site_count >= 0
24238 && tail_call_site_count == tail_call_site_note_count
24239 && (!dwarf_strict || dwarf_version >= 5))
24241 if (call_site_count >= 0
24242 && call_site_count == call_site_note_count)
24243 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24244 else
24245 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24247 call_site_count = -1;
24248 tail_call_site_count = -1;
24251 /* Mark used types after we have created DIEs for the functions scopes. */
24252 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24255 /* Returns a hash value for X (which really is a die_struct). */
24257 hashval_t
24258 block_die_hasher::hash (die_struct *d)
24260 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24263 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24264 as decl_id and die_parent of die_struct Y. */
24266 bool
24267 block_die_hasher::equal (die_struct *x, die_struct *y)
24269 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24272 /* Hold information about markers for inlined entry points. */
24273 struct GTY ((for_user)) inline_entry_data
24275 /* The block that's the inlined_function_outer_scope for an inlined
24276 function. */
24277 tree block;
24279 /* The label at the inlined entry point. */
24280 const char *label_pfx;
24281 unsigned int label_num;
24283 /* The view number to be used as the inlined entry point. */
24284 var_loc_view view;
24287 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24289 typedef tree compare_type;
24290 static inline hashval_t hash (const inline_entry_data *);
24291 static inline bool equal (const inline_entry_data *, const_tree);
24294 /* Hash table routines for inline_entry_data. */
24296 inline hashval_t
24297 inline_entry_data_hasher::hash (const inline_entry_data *data)
24299 return htab_hash_pointer (data->block);
24302 inline bool
24303 inline_entry_data_hasher::equal (const inline_entry_data *data,
24304 const_tree block)
24306 return data->block == block;
24309 /* Inlined entry points pending DIE creation in this compilation unit. */
24311 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24314 /* Return TRUE if DECL, which may have been previously generated as
24315 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24316 true if decl (or its origin) is either an extern declaration or a
24317 class/namespace scoped declaration.
24319 The declare_in_namespace support causes us to get two DIEs for one
24320 variable, both of which are declarations. We want to avoid
24321 considering one to be a specification, so we must test for
24322 DECLARATION and DW_AT_declaration. */
24323 static inline bool
24324 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24326 return (old_die && TREE_STATIC (decl) && !declaration
24327 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24330 /* Return true if DECL is a local static. */
24332 static inline bool
24333 local_function_static (tree decl)
24335 gcc_assert (VAR_P (decl));
24336 return TREE_STATIC (decl)
24337 && DECL_CONTEXT (decl)
24338 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24341 /* Return true iff DECL overrides (presumably completes) the type of
24342 OLD_DIE within CONTEXT_DIE. */
24344 static bool
24345 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24346 dw_die_ref context_die)
24348 tree type = TREE_TYPE (decl);
24349 int cv_quals;
24351 if (decl_by_reference_p (decl))
24353 type = TREE_TYPE (type);
24354 cv_quals = TYPE_UNQUALIFIED;
24356 else
24357 cv_quals = decl_quals (decl);
24359 dw_die_ref type_die = modified_type_die (type,
24360 cv_quals | TYPE_QUALS (type),
24361 false,
24362 context_die);
24364 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24366 return type_die != old_type_die;
24369 /* Generate a DIE to represent a declared data object.
24370 Either DECL or ORIGIN must be non-null. */
24372 static void
24373 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24375 HOST_WIDE_INT off = 0;
24376 tree com_decl;
24377 tree decl_or_origin = decl ? decl : origin;
24378 tree ultimate_origin;
24379 dw_die_ref var_die;
24380 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24381 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24382 || class_or_namespace_scope_p (context_die));
24383 bool specialization_p = false;
24384 bool no_linkage_name = false;
24386 /* While C++ inline static data members have definitions inside of the
24387 class, force the first DIE to be a declaration, then let gen_member_die
24388 reparent it to the class context and call gen_variable_die again
24389 to create the outside of the class DIE for the definition. */
24390 if (!declaration
24391 && old_die == NULL
24392 && decl
24393 && DECL_CONTEXT (decl)
24394 && TYPE_P (DECL_CONTEXT (decl))
24395 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24397 declaration = true;
24398 if (dwarf_version < 5)
24399 no_linkage_name = true;
24402 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24403 if (decl || ultimate_origin)
24404 origin = ultimate_origin;
24405 com_decl = fortran_common (decl_or_origin, &off);
24407 /* Symbol in common gets emitted as a child of the common block, in the form
24408 of a data member. */
24409 if (com_decl)
24411 dw_die_ref com_die;
24412 dw_loc_list_ref loc = NULL;
24413 die_node com_die_arg;
24415 var_die = lookup_decl_die (decl_or_origin);
24416 if (var_die)
24418 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24420 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24421 if (loc)
24423 if (off)
24425 /* Optimize the common case. */
24426 if (single_element_loc_list_p (loc)
24427 && loc->expr->dw_loc_opc == DW_OP_addr
24428 && loc->expr->dw_loc_next == NULL
24429 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24430 == SYMBOL_REF)
24432 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24433 loc->expr->dw_loc_oprnd1.v.val_addr
24434 = plus_constant (GET_MODE (x), x , off);
24436 else
24437 loc_list_plus_const (loc, off);
24439 add_AT_location_description (var_die, DW_AT_location, loc);
24440 remove_AT (var_die, DW_AT_declaration);
24443 return;
24446 if (common_block_die_table == NULL)
24447 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24449 com_die_arg.decl_id = DECL_UID (com_decl);
24450 com_die_arg.die_parent = context_die;
24451 com_die = common_block_die_table->find (&com_die_arg);
24452 if (! early_dwarf)
24453 loc = loc_list_from_tree (com_decl, 2, NULL);
24454 if (com_die == NULL)
24456 const char *cnam
24457 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24458 die_node **slot;
24460 com_die = new_die (DW_TAG_common_block, context_die, decl);
24461 add_name_and_src_coords_attributes (com_die, com_decl);
24462 if (loc)
24464 add_AT_location_description (com_die, DW_AT_location, loc);
24465 /* Avoid sharing the same loc descriptor between
24466 DW_TAG_common_block and DW_TAG_variable. */
24467 loc = loc_list_from_tree (com_decl, 2, NULL);
24469 else if (DECL_EXTERNAL (decl_or_origin))
24470 add_AT_flag (com_die, DW_AT_declaration, 1);
24471 if (want_pubnames ())
24472 add_pubname_string (cnam, com_die); /* ??? needed? */
24473 com_die->decl_id = DECL_UID (com_decl);
24474 slot = common_block_die_table->find_slot (com_die, INSERT);
24475 *slot = com_die;
24477 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24479 add_AT_location_description (com_die, DW_AT_location, loc);
24480 loc = loc_list_from_tree (com_decl, 2, NULL);
24481 remove_AT (com_die, DW_AT_declaration);
24483 var_die = new_die (DW_TAG_variable, com_die, decl);
24484 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24485 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24486 decl_quals (decl_or_origin), false,
24487 context_die);
24488 add_alignment_attribute (var_die, decl);
24489 add_AT_flag (var_die, DW_AT_external, 1);
24490 if (loc)
24492 if (off)
24494 /* Optimize the common case. */
24495 if (single_element_loc_list_p (loc)
24496 && loc->expr->dw_loc_opc == DW_OP_addr
24497 && loc->expr->dw_loc_next == NULL
24498 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24500 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24501 loc->expr->dw_loc_oprnd1.v.val_addr
24502 = plus_constant (GET_MODE (x), x, off);
24504 else
24505 loc_list_plus_const (loc, off);
24507 add_AT_location_description (var_die, DW_AT_location, loc);
24509 else if (DECL_EXTERNAL (decl_or_origin))
24510 add_AT_flag (var_die, DW_AT_declaration, 1);
24511 if (decl)
24512 equate_decl_number_to_die (decl, var_die);
24513 return;
24516 if (old_die)
24518 if (declaration)
24520 /* A declaration that has been previously dumped, needs no
24521 further annotations, since it doesn't need location on
24522 the second pass. */
24523 return;
24525 else if (decl_will_get_specification_p (old_die, decl, declaration)
24526 && !get_AT (old_die, DW_AT_specification))
24528 /* Fall-thru so we can make a new variable die along with a
24529 DW_AT_specification. */
24531 else if (origin && old_die->die_parent != context_die)
24533 /* If we will be creating an inlined instance, we need a
24534 new DIE that will get annotated with
24535 DW_AT_abstract_origin. */
24536 gcc_assert (!DECL_ABSTRACT_P (decl));
24538 else
24540 /* If a DIE was dumped early, it still needs location info.
24541 Skip to where we fill the location bits. */
24542 var_die = old_die;
24544 /* ??? In LTRANS we cannot annotate early created variably
24545 modified type DIEs without copying them and adjusting all
24546 references to them. Thus we dumped them again. Also add a
24547 reference to them but beware of -g0 compile and -g link
24548 in which case the reference will be already present. */
24549 tree type = TREE_TYPE (decl_or_origin);
24550 if (in_lto_p
24551 && ! get_AT (var_die, DW_AT_type)
24552 && variably_modified_type_p
24553 (type, decl_function_context (decl_or_origin)))
24555 if (decl_by_reference_p (decl_or_origin))
24556 add_type_attribute (var_die, TREE_TYPE (type),
24557 TYPE_UNQUALIFIED, false, context_die);
24558 else
24559 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24560 false, context_die);
24563 goto gen_variable_die_location;
24567 /* For static data members, the declaration in the class is supposed
24568 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24569 also in DWARF2; the specification should still be DW_TAG_variable
24570 referencing the DW_TAG_member DIE. */
24571 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24572 var_die = new_die (DW_TAG_member, context_die, decl);
24573 else
24574 var_die = new_die (DW_TAG_variable, context_die, decl);
24576 if (origin != NULL)
24577 add_abstract_origin_attribute (var_die, origin);
24579 /* Loop unrolling can create multiple blocks that refer to the same
24580 static variable, so we must test for the DW_AT_declaration flag.
24582 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24583 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24584 sharing them.
24586 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24587 else if (decl_will_get_specification_p (old_die, decl, declaration))
24589 /* This is a definition of a C++ class level static. */
24590 add_AT_specification (var_die, old_die);
24591 specialization_p = true;
24592 if (DECL_NAME (decl))
24594 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24595 struct dwarf_file_data * file_index = lookup_filename (s.file);
24597 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24598 add_AT_file (var_die, DW_AT_decl_file, file_index);
24600 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24601 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24603 if (debug_column_info
24604 && s.column
24605 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24606 != (unsigned) s.column))
24607 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24609 if (old_die->die_tag == DW_TAG_member)
24610 add_linkage_name (var_die, decl);
24613 else
24614 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24616 if ((origin == NULL && !specialization_p)
24617 || (origin != NULL
24618 && !DECL_ABSTRACT_P (decl_or_origin)
24619 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24620 decl_function_context
24621 (decl_or_origin)))
24622 || (old_die && specialization_p
24623 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24625 tree type = TREE_TYPE (decl_or_origin);
24627 if (decl_by_reference_p (decl_or_origin))
24628 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24629 context_die);
24630 else
24631 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24632 context_die);
24635 if (origin == NULL && !specialization_p)
24637 if (TREE_PUBLIC (decl))
24638 add_AT_flag (var_die, DW_AT_external, 1);
24640 if (DECL_ARTIFICIAL (decl))
24641 add_AT_flag (var_die, DW_AT_artificial, 1);
24643 add_alignment_attribute (var_die, decl);
24645 add_accessibility_attribute (var_die, decl);
24648 if (declaration)
24649 add_AT_flag (var_die, DW_AT_declaration, 1);
24651 if (decl && (DECL_ABSTRACT_P (decl)
24652 || !old_die || is_declaration_die (old_die)))
24653 equate_decl_number_to_die (decl, var_die);
24655 gen_variable_die_location:
24656 if (! declaration
24657 && (! DECL_ABSTRACT_P (decl_or_origin)
24658 /* Local static vars are shared between all clones/inlines,
24659 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24660 already set. */
24661 || (VAR_P (decl_or_origin)
24662 && TREE_STATIC (decl_or_origin)
24663 && DECL_RTL_SET_P (decl_or_origin))))
24665 if (early_dwarf)
24667 add_pubname (decl_or_origin, var_die);
24668 /* For global register variables, emit DW_AT_location if possible
24669 already during early_dwarf, as late_global_decl won't be usually
24670 called. */
24671 if (DECL_HARD_REGISTER (decl_or_origin)
24672 && TREE_STATIC (decl_or_origin)
24673 && !decl_by_reference_p (decl_or_origin)
24674 && !get_AT (var_die, DW_AT_location)
24675 && !get_AT (var_die, DW_AT_const_value)
24676 && DECL_RTL_SET_P (decl_or_origin)
24677 && REG_P (DECL_RTL (decl_or_origin)))
24679 dw_loc_descr_ref descr
24680 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24681 VAR_INIT_STATUS_INITIALIZED);
24682 if (descr)
24683 add_AT_loc (var_die, DW_AT_location, descr);
24686 else
24687 add_location_or_const_value_attribute (var_die, decl_or_origin,
24688 decl == NULL);
24690 else
24691 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24693 if ((dwarf_version >= 4 || !dwarf_strict)
24694 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24695 DW_AT_const_expr) == 1
24696 && !get_AT (var_die, DW_AT_const_expr)
24697 && !specialization_p)
24698 add_AT_flag (var_die, DW_AT_const_expr, 1);
24700 if (!dwarf_strict)
24702 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24703 DW_AT_inline);
24704 if (inl != -1
24705 && !get_AT (var_die, DW_AT_inline)
24706 && !specialization_p)
24707 add_AT_unsigned (var_die, DW_AT_inline, inl);
24711 /* Generate a DIE to represent a named constant. */
24713 static void
24714 gen_const_die (tree decl, dw_die_ref context_die)
24716 dw_die_ref const_die;
24717 tree type = TREE_TYPE (decl);
24719 const_die = lookup_decl_die (decl);
24720 if (const_die)
24721 return;
24723 const_die = new_die (DW_TAG_constant, context_die, decl);
24724 equate_decl_number_to_die (decl, const_die);
24725 add_name_and_src_coords_attributes (const_die, decl);
24726 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24727 if (TREE_PUBLIC (decl))
24728 add_AT_flag (const_die, DW_AT_external, 1);
24729 if (DECL_ARTIFICIAL (decl))
24730 add_AT_flag (const_die, DW_AT_artificial, 1);
24731 tree_add_const_value_attribute_for_decl (const_die, decl);
24734 /* Generate a DIE to represent a label identifier. */
24736 static void
24737 gen_label_die (tree decl, dw_die_ref context_die)
24739 tree origin = decl_ultimate_origin (decl);
24740 dw_die_ref lbl_die = lookup_decl_die (decl);
24741 rtx insn;
24742 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24744 if (!lbl_die)
24746 lbl_die = new_die (DW_TAG_label, context_die, decl);
24747 equate_decl_number_to_die (decl, lbl_die);
24749 if (origin != NULL)
24750 add_abstract_origin_attribute (lbl_die, origin);
24751 else
24752 add_name_and_src_coords_attributes (lbl_die, decl);
24755 if (DECL_ABSTRACT_P (decl))
24756 equate_decl_number_to_die (decl, lbl_die);
24757 else if (! early_dwarf)
24759 insn = DECL_RTL_IF_SET (decl);
24761 /* Deleted labels are programmer specified labels which have been
24762 eliminated because of various optimizations. We still emit them
24763 here so that it is possible to put breakpoints on them. */
24764 if (insn
24765 && (LABEL_P (insn)
24766 || ((NOTE_P (insn)
24767 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24769 /* When optimization is enabled (via -O) some parts of the compiler
24770 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24771 represent source-level labels which were explicitly declared by
24772 the user. This really shouldn't be happening though, so catch
24773 it if it ever does happen. */
24774 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24776 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24777 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24779 else if (insn
24780 && NOTE_P (insn)
24781 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24782 && CODE_LABEL_NUMBER (insn) != -1)
24784 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24785 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24790 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24791 attributes to the DIE for a block STMT, to describe where the inlined
24792 function was called from. This is similar to add_src_coords_attributes. */
24794 static inline void
24795 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24797 /* We can end up with BUILTINS_LOCATION here. */
24798 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24799 return;
24801 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24802 expanded_location s = expand_location (locus);
24804 if (dwarf_version >= 3 || !dwarf_strict)
24806 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24807 add_AT_unsigned (die, DW_AT_call_line, s.line);
24808 if (debug_column_info && s.column)
24809 add_AT_unsigned (die, DW_AT_call_column, s.column);
24810 unsigned discr = get_discriminator_from_loc (locus);
24811 if (discr != 0)
24812 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24817 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24818 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24820 static inline void
24821 add_high_low_attributes (tree stmt, dw_die_ref die)
24823 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24825 if (inline_entry_data **iedp
24826 = !inline_entry_data_table ? NULL
24827 : inline_entry_data_table->find_slot_with_hash (stmt,
24828 htab_hash_pointer (stmt),
24829 NO_INSERT))
24831 inline_entry_data *ied = *iedp;
24832 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24833 gcc_assert (debug_inline_points);
24834 gcc_assert (inlined_function_outer_scope_p (stmt));
24836 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24837 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24839 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24840 && !dwarf_strict)
24842 if (!output_asm_line_debug_info ())
24843 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24844 else
24846 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24847 /* FIXME: this will resolve to a small number. Could we
24848 possibly emit smaller data? Ideally we'd emit a
24849 uleb128, but that would make the size of DIEs
24850 impossible for the compiler to compute, since it's
24851 the assembler that computes the value of the view
24852 label in this case. Ideally, we'd have a single form
24853 encompassing both the address and the view, and
24854 indirecting them through a table might make things
24855 easier, but even that would be more wasteful,
24856 space-wise, than what we have now. */
24857 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24861 inline_entry_data_table->clear_slot (iedp);
24864 if (BLOCK_FRAGMENT_CHAIN (stmt)
24865 && (dwarf_version >= 3 || !dwarf_strict))
24867 tree chain, superblock = NULL_TREE;
24868 dw_die_ref pdie;
24869 dw_attr_node *attr = NULL;
24871 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24873 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24874 BLOCK_NUMBER (stmt));
24875 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24878 /* Optimize duplicate .debug_ranges lists or even tails of
24879 lists. If this BLOCK has same ranges as its supercontext,
24880 lookup DW_AT_ranges attribute in the supercontext (and
24881 recursively so), verify that the ranges_table contains the
24882 right values and use it instead of adding a new .debug_range. */
24883 for (chain = stmt, pdie = die;
24884 BLOCK_SAME_RANGE (chain);
24885 chain = BLOCK_SUPERCONTEXT (chain))
24887 dw_attr_node *new_attr;
24889 pdie = pdie->die_parent;
24890 if (pdie == NULL)
24891 break;
24892 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24893 break;
24894 new_attr = get_AT (pdie, DW_AT_ranges);
24895 if (new_attr == NULL
24896 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24897 break;
24898 attr = new_attr;
24899 superblock = BLOCK_SUPERCONTEXT (chain);
24901 if (attr != NULL
24902 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24903 == (int)BLOCK_NUMBER (superblock))
24904 && BLOCK_FRAGMENT_CHAIN (superblock))
24906 unsigned long off = attr->dw_attr_val.v.val_offset;
24907 unsigned long supercnt = 0, thiscnt = 0;
24908 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24909 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24911 ++supercnt;
24912 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24913 == (int)BLOCK_NUMBER (chain));
24915 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24916 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24917 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24918 ++thiscnt;
24919 gcc_assert (supercnt >= thiscnt);
24920 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24921 false);
24922 note_rnglist_head (off + supercnt - thiscnt);
24923 return;
24926 unsigned int offset = add_ranges (stmt, true);
24927 add_AT_range_list (die, DW_AT_ranges, offset, false);
24928 note_rnglist_head (offset);
24930 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24931 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24934 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24935 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24936 chain = BLOCK_FRAGMENT_CHAIN (chain);
24938 while (chain);
24939 add_ranges (NULL);
24941 else
24943 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24944 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24945 BLOCK_NUMBER (stmt));
24946 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24947 BLOCK_NUMBER (stmt));
24948 add_AT_low_high_pc (die, label, label_high, false);
24952 /* Generate a DIE for a lexical block. */
24954 static void
24955 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24957 dw_die_ref old_die = lookup_block_die (stmt);
24958 dw_die_ref stmt_die = NULL;
24959 if (!old_die)
24961 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24962 equate_block_to_die (stmt, stmt_die);
24965 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24967 /* If this is an inlined or conrecte instance, create a new lexical
24968 die for anything below to attach DW_AT_abstract_origin to. */
24969 if (old_die)
24970 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24972 tree origin = block_ultimate_origin (stmt);
24973 if (origin != NULL_TREE && (origin != stmt || old_die))
24974 add_abstract_origin_attribute (stmt_die, origin);
24976 old_die = NULL;
24979 if (old_die)
24980 stmt_die = old_die;
24982 /* A non abstract block whose blocks have already been reordered
24983 should have the instruction range for this block. If so, set the
24984 high/low attributes. */
24985 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24987 gcc_assert (stmt_die);
24988 add_high_low_attributes (stmt, stmt_die);
24991 decls_for_scope (stmt, stmt_die);
24994 /* Generate a DIE for an inlined subprogram. */
24996 static void
24997 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24999 tree decl = block_ultimate_origin (stmt);
25001 /* Make sure any inlined functions are known to be inlineable. */
25002 gcc_checking_assert (DECL_ABSTRACT_P (decl)
25003 || cgraph_function_possibly_inlined_p (decl));
25005 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
25007 if (call_arg_locations || debug_inline_points)
25008 equate_block_to_die (stmt, subr_die);
25009 add_abstract_origin_attribute (subr_die, decl);
25010 if (TREE_ASM_WRITTEN (stmt))
25011 add_high_low_attributes (stmt, subr_die);
25012 add_call_src_coords_attributes (stmt, subr_die);
25014 /* The inliner creates an extra BLOCK for the parameter setup,
25015 we want to merge that with the actual outermost BLOCK of the
25016 inlined function to avoid duplicate locals in consumers.
25017 Do that by doing the recursion to subblocks on the single subblock
25018 of STMT. */
25019 bool unwrap_one = false;
25020 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
25022 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
25023 if (origin
25024 && TREE_CODE (origin) == BLOCK
25025 && BLOCK_SUPERCONTEXT (origin) == decl)
25026 unwrap_one = true;
25028 decls_for_scope (stmt, subr_die, !unwrap_one);
25029 if (unwrap_one)
25030 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
25033 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25034 the comment for VLR_CONTEXT. */
25036 static void
25037 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25039 dw_die_ref decl_die;
25041 if (TREE_TYPE (decl) == error_mark_node)
25042 return;
25044 decl_die = new_die (DW_TAG_member, context_die, decl);
25045 add_name_and_src_coords_attributes (decl_die, decl);
25046 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25047 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25048 context_die);
25050 if (DECL_BIT_FIELD_TYPE (decl))
25052 add_byte_size_attribute (decl_die, decl);
25053 add_bit_size_attribute (decl_die, decl);
25054 add_bit_offset_attribute (decl_die, decl);
25057 add_alignment_attribute (decl_die, decl);
25059 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25060 add_data_member_location_attribute (decl_die, decl, ctx);
25062 if (DECL_ARTIFICIAL (decl))
25063 add_AT_flag (decl_die, DW_AT_artificial, 1);
25065 add_accessibility_attribute (decl_die, decl);
25067 /* Equate decl number to die, so that we can look up this decl later on. */
25068 equate_decl_number_to_die (decl, decl_die);
25071 /* Generate a DIE for a pointer to a member type. TYPE can be an
25072 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25073 pointer to member function. */
25075 static void
25076 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25078 if (lookup_type_die (type))
25079 return;
25081 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25082 scope_die_for (type, context_die), type);
25084 equate_type_number_to_die (type, ptr_die);
25085 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25086 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25087 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25088 context_die);
25089 add_alignment_attribute (ptr_die, type);
25091 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25092 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25094 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25095 add_AT_loc (ptr_die, DW_AT_use_location, op);
25099 static char *producer_string;
25101 /* Given a C and/or C++ language/version string return the "highest".
25102 C++ is assumed to be "higher" than C in this case. Used for merging
25103 LTO translation unit languages. */
25104 static const char *
25105 highest_c_language (const char *lang1, const char *lang2)
25107 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25108 return "GNU C++23";
25109 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25110 return "GNU C++20";
25111 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25112 return "GNU C++17";
25113 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25114 return "GNU C++14";
25115 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25116 return "GNU C++11";
25117 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25118 return "GNU C++98";
25120 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
25121 return "GNU C2X";
25122 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25123 return "GNU C17";
25124 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25125 return "GNU C11";
25126 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25127 return "GNU C99";
25128 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25129 return "GNU C89";
25131 gcc_unreachable ();
25135 /* Generate the DIE for the compilation unit. */
25137 static dw_die_ref
25138 gen_compile_unit_die (const char *filename)
25140 dw_die_ref die;
25141 const char *language_string = lang_hooks.name;
25142 int language;
25144 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25146 if (filename)
25148 add_filename_attribute (die, filename);
25149 /* Don't add cwd for <built-in>. */
25150 if (filename[0] != '<')
25151 add_comp_dir_attribute (die);
25154 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25156 /* If our producer is LTO try to figure out a common language to use
25157 from the global list of translation units. */
25158 if (strcmp (language_string, "GNU GIMPLE") == 0)
25160 unsigned i;
25161 tree t;
25162 const char *common_lang = NULL;
25164 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25166 if (!TRANSLATION_UNIT_LANGUAGE (t))
25167 continue;
25168 if (!common_lang)
25169 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25170 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25172 else if (startswith (common_lang, "GNU C")
25173 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25174 /* Mixing C and C++ is ok, use C++ in that case. */
25175 common_lang = highest_c_language (common_lang,
25176 TRANSLATION_UNIT_LANGUAGE (t));
25177 else
25179 /* Fall back to C. */
25180 common_lang = NULL;
25181 break;
25185 if (common_lang)
25186 language_string = common_lang;
25189 language = DW_LANG_C;
25190 if (startswith (language_string, "GNU C")
25191 && ISDIGIT (language_string[5]))
25193 language = DW_LANG_C89;
25194 if (dwarf_version >= 3 || !dwarf_strict)
25196 if (strcmp (language_string, "GNU C89") != 0)
25197 language = DW_LANG_C99;
25199 if (dwarf_version >= 5 /* || !dwarf_strict */)
25200 if (strcmp (language_string, "GNU C11") == 0
25201 || strcmp (language_string, "GNU C17") == 0
25202 || strcmp (language_string, "GNU C2X") == 0)
25203 language = DW_LANG_C11;
25206 else if (startswith (language_string, "GNU C++"))
25208 language = DW_LANG_C_plus_plus;
25209 if (dwarf_version >= 5 /* || !dwarf_strict */)
25211 if (strcmp (language_string, "GNU C++11") == 0)
25212 language = DW_LANG_C_plus_plus_11;
25213 else if (strcmp (language_string, "GNU C++14") == 0)
25214 language = DW_LANG_C_plus_plus_14;
25215 else if (strcmp (language_string, "GNU C++17") == 0
25216 || strcmp (language_string, "GNU C++20") == 0
25217 || strcmp (language_string, "GNU C++23") == 0)
25218 /* For now. */
25219 language = DW_LANG_C_plus_plus_14;
25222 else if (strcmp (language_string, "GNU F77") == 0)
25223 language = DW_LANG_Fortran77;
25224 else if (strcmp (language_string, "GNU Modula-2") == 0)
25225 language = DW_LANG_Modula2;
25226 else if (dwarf_version >= 3 || !dwarf_strict)
25228 if (strcmp (language_string, "GNU Ada") == 0)
25229 language = DW_LANG_Ada95;
25230 else if (startswith (language_string, "GNU Fortran"))
25232 language = DW_LANG_Fortran95;
25233 if (dwarf_version >= 5 /* || !dwarf_strict */)
25235 if (strcmp (language_string, "GNU Fortran2003") == 0)
25236 language = DW_LANG_Fortran03;
25237 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25238 language = DW_LANG_Fortran08;
25241 else if (strcmp (language_string, "GNU Objective-C") == 0)
25242 language = DW_LANG_ObjC;
25243 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25244 language = DW_LANG_ObjC_plus_plus;
25245 else if (strcmp (language_string, "GNU D") == 0)
25246 language = DW_LANG_D;
25247 else if (dwarf_version >= 5 || !dwarf_strict)
25249 if (strcmp (language_string, "GNU Go") == 0)
25250 language = DW_LANG_Go;
25251 else if (strcmp (language_string, "GNU Rust") == 0)
25252 language = DW_LANG_Rust;
25255 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25256 else if (startswith (language_string, "GNU Fortran"))
25257 language = DW_LANG_Fortran90;
25258 /* Likewise for Ada. */
25259 else if (strcmp (language_string, "GNU Ada") == 0)
25260 language = DW_LANG_Ada83;
25262 add_AT_unsigned (die, DW_AT_language, language);
25264 switch (language)
25266 case DW_LANG_Fortran77:
25267 case DW_LANG_Fortran90:
25268 case DW_LANG_Fortran95:
25269 case DW_LANG_Fortran03:
25270 case DW_LANG_Fortran08:
25271 /* Fortran has case insensitive identifiers and the front-end
25272 lowercases everything. */
25273 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25274 break;
25275 default:
25276 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25277 break;
25279 return die;
25282 /* Generate the DIE for a base class. */
25284 static void
25285 gen_inheritance_die (tree binfo, tree access, tree type,
25286 dw_die_ref context_die)
25288 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25289 struct vlr_context ctx = { type, NULL };
25291 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25292 context_die);
25293 add_data_member_location_attribute (die, binfo, &ctx);
25295 if (BINFO_VIRTUAL_P (binfo))
25296 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25298 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25299 children, otherwise the default is DW_ACCESS_public. In DWARF2
25300 the default has always been DW_ACCESS_private. */
25301 if (access == access_public_node)
25303 if (dwarf_version == 2
25304 || context_die->die_tag == DW_TAG_class_type)
25305 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25307 else if (access == access_protected_node)
25308 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25309 else if (dwarf_version > 2
25310 && context_die->die_tag != DW_TAG_class_type)
25311 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25314 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25315 structure. */
25317 static bool
25318 is_variant_part (tree decl)
25320 return (TREE_CODE (decl) == FIELD_DECL
25321 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25324 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25325 return the FIELD_DECL. Return NULL_TREE otherwise. */
25327 static tree
25328 analyze_discr_in_predicate (tree operand, tree struct_type)
25330 while (CONVERT_EXPR_P (operand))
25331 operand = TREE_OPERAND (operand, 0);
25333 /* Match field access to members of struct_type only. */
25334 if (TREE_CODE (operand) == COMPONENT_REF
25335 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25336 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25337 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25338 return TREE_OPERAND (operand, 1);
25339 else
25340 return NULL_TREE;
25343 /* Check that SRC is a constant integer that can be represented as a native
25344 integer constant (either signed or unsigned). If so, store it into DEST and
25345 return true. Return false otherwise. */
25347 static bool
25348 get_discr_value (tree src, dw_discr_value *dest)
25350 tree discr_type = TREE_TYPE (src);
25352 if (lang_hooks.types.get_debug_type)
25354 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25355 if (debug_type != NULL)
25356 discr_type = debug_type;
25359 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25360 return false;
25362 /* Signedness can vary between the original type and the debug type. This
25363 can happen for character types in Ada for instance: the character type
25364 used for code generation can be signed, to be compatible with the C one,
25365 but from a debugger point of view, it must be unsigned. */
25366 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25367 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25369 if (is_orig_unsigned != is_debug_unsigned)
25370 src = fold_convert (discr_type, src);
25372 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25373 return false;
25375 dest->pos = is_debug_unsigned;
25376 if (is_debug_unsigned)
25377 dest->v.uval = tree_to_uhwi (src);
25378 else
25379 dest->v.sval = tree_to_shwi (src);
25381 return true;
25384 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25385 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25386 store NULL_TREE in DISCR_DECL. Otherwise:
25388 - store the discriminant field in STRUCT_TYPE that controls the variant
25389 part to *DISCR_DECL
25391 - put in *DISCR_LISTS_P an array where for each variant, the item
25392 represents the corresponding matching list of discriminant values.
25394 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25395 the above array.
25397 Note that when the array is allocated (i.e. when the analysis is
25398 successful), it is up to the caller to free the array. */
25400 static void
25401 analyze_variants_discr (tree variant_part_decl,
25402 tree struct_type,
25403 tree *discr_decl,
25404 dw_discr_list_ref **discr_lists_p,
25405 unsigned *discr_lists_length)
25407 tree variant_part_type = TREE_TYPE (variant_part_decl);
25408 tree variant;
25409 dw_discr_list_ref *discr_lists;
25410 unsigned i;
25412 /* Compute how many variants there are in this variant part. */
25413 *discr_lists_length = 0;
25414 for (variant = TYPE_FIELDS (variant_part_type);
25415 variant != NULL_TREE;
25416 variant = DECL_CHAIN (variant))
25417 ++*discr_lists_length;
25419 *discr_decl = NULL_TREE;
25420 *discr_lists_p
25421 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25422 sizeof (**discr_lists_p));
25423 discr_lists = *discr_lists_p;
25425 /* And then analyze all variants to extract discriminant information for all
25426 of them. This analysis is conservative: as soon as we detect something we
25427 do not support, abort everything and pretend we found nothing. */
25428 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25429 variant != NULL_TREE;
25430 variant = DECL_CHAIN (variant), ++i)
25432 tree match_expr = DECL_QUALIFIER (variant);
25434 /* Now, try to analyze the predicate and deduce a discriminant for
25435 it. */
25436 if (match_expr == boolean_true_node)
25437 /* Typically happens for the default variant: it matches all cases that
25438 previous variants rejected. Don't output any matching value for
25439 this one. */
25440 continue;
25442 /* The following loop tries to iterate over each discriminant
25443 possibility: single values or ranges. */
25444 while (match_expr != NULL_TREE)
25446 tree next_round_match_expr;
25447 tree candidate_discr = NULL_TREE;
25448 dw_discr_list_ref new_node = NULL;
25450 /* Possibilities are matched one after the other by nested
25451 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25452 continue with the rest at next iteration. */
25453 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25455 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25456 match_expr = TREE_OPERAND (match_expr, 1);
25458 else
25459 next_round_match_expr = NULL_TREE;
25461 if (match_expr == boolean_false_node)
25462 /* This sub-expression matches nothing: just wait for the next
25463 one. */
25466 else if (TREE_CODE (match_expr) == EQ_EXPR)
25468 /* We are matching: <discr_field> == <integer_cst>
25469 This sub-expression matches a single value. */
25470 tree integer_cst = TREE_OPERAND (match_expr, 1);
25472 candidate_discr
25473 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25474 struct_type);
25476 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25477 if (!get_discr_value (integer_cst,
25478 &new_node->dw_discr_lower_bound))
25479 goto abort;
25480 new_node->dw_discr_range = false;
25483 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25485 /* We are matching:
25486 <discr_field> > <integer_cst>
25487 && <discr_field> < <integer_cst>.
25488 This sub-expression matches the range of values between the
25489 two matched integer constants. Note that comparisons can be
25490 inclusive or exclusive. */
25491 tree candidate_discr_1, candidate_discr_2;
25492 tree lower_cst, upper_cst;
25493 bool lower_cst_included, upper_cst_included;
25494 tree lower_op = TREE_OPERAND (match_expr, 0);
25495 tree upper_op = TREE_OPERAND (match_expr, 1);
25497 /* When the comparison is exclusive, the integer constant is not
25498 the discriminant range bound we are looking for: we will have
25499 to increment or decrement it. */
25500 if (TREE_CODE (lower_op) == GE_EXPR)
25501 lower_cst_included = true;
25502 else if (TREE_CODE (lower_op) == GT_EXPR)
25503 lower_cst_included = false;
25504 else
25505 goto abort;
25507 if (TREE_CODE (upper_op) == LE_EXPR)
25508 upper_cst_included = true;
25509 else if (TREE_CODE (upper_op) == LT_EXPR)
25510 upper_cst_included = false;
25511 else
25512 goto abort;
25514 /* Extract the discriminant from the first operand and check it
25515 is consistant with the same analysis in the second
25516 operand. */
25517 candidate_discr_1
25518 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25519 struct_type);
25520 candidate_discr_2
25521 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25522 struct_type);
25523 if (candidate_discr_1 == candidate_discr_2)
25524 candidate_discr = candidate_discr_1;
25525 else
25526 goto abort;
25528 /* Extract bounds from both. */
25529 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25530 lower_cst = TREE_OPERAND (lower_op, 1);
25531 upper_cst = TREE_OPERAND (upper_op, 1);
25533 if (!lower_cst_included)
25534 lower_cst
25535 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25536 build_int_cst (TREE_TYPE (lower_cst), 1));
25537 if (!upper_cst_included)
25538 upper_cst
25539 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25540 build_int_cst (TREE_TYPE (upper_cst), 1));
25542 if (!get_discr_value (lower_cst,
25543 &new_node->dw_discr_lower_bound)
25544 || !get_discr_value (upper_cst,
25545 &new_node->dw_discr_upper_bound))
25546 goto abort;
25548 new_node->dw_discr_range = true;
25551 else if ((candidate_discr
25552 = analyze_discr_in_predicate (match_expr, struct_type))
25553 && (TREE_TYPE (candidate_discr) == boolean_type_node
25554 || TREE_TYPE (TREE_TYPE (candidate_discr))
25555 == boolean_type_node))
25557 /* We are matching: <discr_field> for a boolean discriminant.
25558 This sub-expression matches boolean_true_node. */
25559 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25560 if (!get_discr_value (boolean_true_node,
25561 &new_node->dw_discr_lower_bound))
25562 goto abort;
25563 new_node->dw_discr_range = false;
25566 else
25567 /* Unsupported sub-expression: we cannot determine the set of
25568 matching discriminant values. Abort everything. */
25569 goto abort;
25571 /* If the discriminant info is not consistant with what we saw so
25572 far, consider the analysis failed and abort everything. */
25573 if (candidate_discr == NULL_TREE
25574 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25575 goto abort;
25576 else
25577 *discr_decl = candidate_discr;
25579 if (new_node != NULL)
25581 new_node->dw_discr_next = discr_lists[i];
25582 discr_lists[i] = new_node;
25584 match_expr = next_round_match_expr;
25588 /* If we reach this point, we could match everything we were interested
25589 in. */
25590 return;
25592 abort:
25593 /* Clean all data structure and return no result. */
25594 free (*discr_lists_p);
25595 *discr_lists_p = NULL;
25596 *discr_decl = NULL_TREE;
25599 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25600 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25601 under CONTEXT_DIE.
25603 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25604 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25605 this type, which are record types, represent the available variants and each
25606 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25607 values are inferred from these attributes.
25609 In trees, the offsets for the fields inside these sub-records are relative
25610 to the variant part itself, whereas the corresponding DIEs should have
25611 offset attributes that are relative to the embedding record base address.
25612 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25613 must be an expression that computes the offset of the variant part to
25614 describe in DWARF. */
25616 static void
25617 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25618 dw_die_ref context_die)
25620 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25621 tree variant_part_offset = vlr_ctx->variant_part_offset;
25623 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25624 NULL_TREE if there is no such field. */
25625 tree discr_decl = NULL_TREE;
25626 dw_discr_list_ref *discr_lists;
25627 unsigned discr_lists_length = 0;
25628 unsigned i;
25630 dw_die_ref dwarf_proc_die = NULL;
25631 dw_die_ref variant_part_die
25632 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25634 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25636 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25637 &discr_decl, &discr_lists, &discr_lists_length);
25639 if (discr_decl != NULL_TREE)
25641 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25643 if (discr_die)
25644 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25645 else
25646 /* We have no DIE for the discriminant, so just discard all
25647 discrimimant information in the output. */
25648 discr_decl = NULL_TREE;
25651 /* If the offset for this variant part is more complex than a constant,
25652 create a DWARF procedure for it so that we will not have to generate
25653 DWARF expressions for it for each member. */
25654 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25655 && (dwarf_version >= 3 || !dwarf_strict))
25657 struct loc_descr_context ctx = {
25658 vlr_ctx->struct_type, /* context_type */
25659 NULL_TREE, /* base_decl */
25660 NULL, /* dpi */
25661 false, /* placeholder_arg */
25662 false, /* placeholder_seen */
25663 false /* strict_signedness */
25665 const tree dwarf_proc_fndecl
25666 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25667 build_function_type (TREE_TYPE (variant_part_offset),
25668 NULL_TREE));
25669 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25670 const dw_loc_descr_ref dwarf_proc_body
25671 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25673 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25674 dwarf_proc_fndecl, context_die);
25675 if (dwarf_proc_die != NULL)
25676 variant_part_offset = dwarf_proc_call;
25679 /* Output DIEs for all variants. */
25680 i = 0;
25681 for (tree variant = TYPE_FIELDS (variant_part_type);
25682 variant != NULL_TREE;
25683 variant = DECL_CHAIN (variant), ++i)
25685 tree variant_type = TREE_TYPE (variant);
25686 dw_die_ref variant_die;
25688 /* All variants (i.e. members of a variant part) are supposed to be
25689 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25690 under these records. */
25691 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25693 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25694 equate_decl_number_to_die (variant, variant_die);
25696 /* Output discriminant values this variant matches, if any. */
25697 if (discr_decl == NULL || discr_lists[i] == NULL)
25698 /* In the case we have discriminant information at all, this is
25699 probably the default variant: as the standard says, don't
25700 output any discriminant value/list attribute. */
25702 else if (discr_lists[i]->dw_discr_next == NULL
25703 && !discr_lists[i]->dw_discr_range)
25704 /* If there is only one accepted value, don't bother outputting a
25705 list. */
25706 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25707 else
25708 add_discr_list (variant_die, discr_lists[i]);
25710 for (tree member = TYPE_FIELDS (variant_type);
25711 member != NULL_TREE;
25712 member = DECL_CHAIN (member))
25714 struct vlr_context vlr_sub_ctx = {
25715 vlr_ctx->struct_type, /* struct_type */
25716 NULL /* variant_part_offset */
25718 if (is_variant_part (member))
25720 /* All offsets for fields inside variant parts are relative to
25721 the top-level embedding RECORD_TYPE's base address. On the
25722 other hand, offsets in GCC's types are relative to the
25723 nested-most variant part. So we have to sum offsets each time
25724 we recurse. */
25726 vlr_sub_ctx.variant_part_offset
25727 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25728 variant_part_offset, byte_position (member));
25729 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25731 else
25733 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25734 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25739 free (discr_lists);
25742 /* Generate a DIE for a class member. */
25744 static void
25745 gen_member_die (tree type, dw_die_ref context_die)
25747 tree member;
25748 tree binfo = TYPE_BINFO (type);
25750 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25752 /* If this is not an incomplete type, output descriptions of each of its
25753 members. Note that as we output the DIEs necessary to represent the
25754 members of this record or union type, we will also be trying to output
25755 DIEs to represent the *types* of those members. However the `type'
25756 function (above) will specifically avoid generating type DIEs for member
25757 types *within* the list of member DIEs for this (containing) type except
25758 for those types (of members) which are explicitly marked as also being
25759 members of this (containing) type themselves. The g++ front- end can
25760 force any given type to be treated as a member of some other (containing)
25761 type by setting the TYPE_CONTEXT of the given (member) type to point to
25762 the TREE node representing the appropriate (containing) type. */
25764 /* First output info about the base classes. */
25765 if (binfo && early_dwarf)
25767 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25768 int i;
25769 tree base;
25771 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25772 gen_inheritance_die (base,
25773 (accesses ? (*accesses)[i] : access_public_node),
25774 type,
25775 context_die);
25778 /* Now output info about the members. */
25779 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25781 /* Ignore clones. */
25782 if (DECL_ABSTRACT_ORIGIN (member))
25783 continue;
25785 struct vlr_context vlr_ctx = { type, NULL_TREE };
25786 bool static_inline_p
25787 = (VAR_P (member)
25788 && TREE_STATIC (member)
25789 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25790 != -1));
25792 /* If we thought we were generating minimal debug info for TYPE
25793 and then changed our minds, some of the member declarations
25794 may have already been defined. Don't define them again, but
25795 do put them in the right order. */
25797 if (dw_die_ref child = lookup_decl_die (member))
25799 /* Handle inline static data members, which only have in-class
25800 declarations. */
25801 bool splice = true;
25803 dw_die_ref ref = NULL;
25804 if (child->die_tag == DW_TAG_variable
25805 && child->die_parent == comp_unit_die ())
25807 ref = get_AT_ref (child, DW_AT_specification);
25809 /* For C++17 inline static data members followed by redundant
25810 out of class redeclaration, we might get here with
25811 child being the DIE created for the out of class
25812 redeclaration and with its DW_AT_specification being
25813 the DIE created for in-class definition. We want to
25814 reparent the latter, and don't want to create another
25815 DIE with DW_AT_specification in that case, because
25816 we already have one. */
25817 if (ref
25818 && static_inline_p
25819 && ref->die_tag == DW_TAG_variable
25820 && ref->die_parent == comp_unit_die ()
25821 && get_AT (ref, DW_AT_specification) == NULL)
25823 child = ref;
25824 ref = NULL;
25825 static_inline_p = false;
25828 if (!ref)
25830 reparent_child (child, context_die);
25831 if (dwarf_version < 5)
25832 child->die_tag = DW_TAG_member;
25833 splice = false;
25836 else if (child->die_tag == DW_TAG_enumerator)
25837 /* Enumerators remain under their enumeration even if
25838 their names are introduced in the enclosing scope. */
25839 splice = false;
25841 if (splice)
25842 splice_child_die (context_die, child);
25845 /* Do not generate DWARF for variant parts if we are generating the
25846 corresponding GNAT encodings: DIEs generated for the two schemes
25847 would conflict in our mappings. */
25848 else if (is_variant_part (member)
25849 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25851 vlr_ctx.variant_part_offset = byte_position (member);
25852 gen_variant_part (member, &vlr_ctx, context_die);
25854 else
25856 vlr_ctx.variant_part_offset = NULL_TREE;
25857 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25860 /* For C++ inline static data members emit immediately a DW_TAG_variable
25861 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25862 DW_AT_specification. */
25863 if (static_inline_p)
25865 int old_extern = DECL_EXTERNAL (member);
25866 DECL_EXTERNAL (member) = 0;
25867 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25868 DECL_EXTERNAL (member) = old_extern;
25873 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25874 is set, we pretend that the type was never defined, so we only get the
25875 member DIEs needed by later specification DIEs. */
25877 static void
25878 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25879 enum debug_info_usage usage)
25881 if (TREE_ASM_WRITTEN (type))
25883 /* Fill in the bound of variable-length fields in late dwarf if
25884 still incomplete. */
25885 if (!early_dwarf && variably_modified_type_p (type, NULL))
25886 for (tree member = TYPE_FIELDS (type);
25887 member;
25888 member = DECL_CHAIN (member))
25889 fill_variable_array_bounds (TREE_TYPE (member));
25890 return;
25893 dw_die_ref type_die = lookup_type_die (type);
25894 dw_die_ref scope_die = 0;
25895 int nested = 0;
25896 int complete = (TYPE_SIZE (type)
25897 && (! TYPE_STUB_DECL (type)
25898 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25899 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25900 complete = complete && should_emit_struct_debug (type, usage);
25902 if (type_die && ! complete)
25903 return;
25905 if (TYPE_CONTEXT (type) != NULL_TREE
25906 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25907 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25908 nested = 1;
25910 scope_die = scope_die_for (type, context_die);
25912 /* Generate child dies for template parameters. */
25913 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25914 schedule_generic_params_dies_gen (type);
25916 if (! type_die || (nested && is_cu_die (scope_die)))
25917 /* First occurrence of type or toplevel definition of nested class. */
25919 dw_die_ref old_die = type_die;
25921 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25922 ? record_type_tag (type) : DW_TAG_union_type,
25923 scope_die, type);
25924 equate_type_number_to_die (type, type_die);
25925 if (old_die)
25926 add_AT_specification (type_die, old_die);
25927 else
25928 add_name_attribute (type_die, type_tag (type));
25930 else
25931 remove_AT (type_die, DW_AT_declaration);
25933 /* If this type has been completed, then give it a byte_size attribute and
25934 then give a list of members. */
25935 if (complete && !ns_decl)
25937 /* Prevent infinite recursion in cases where the type of some member of
25938 this type is expressed in terms of this type itself. */
25939 TREE_ASM_WRITTEN (type) = 1;
25940 add_byte_size_attribute (type_die, type);
25941 add_alignment_attribute (type_die, type);
25942 if (TYPE_STUB_DECL (type) != NULL_TREE)
25944 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25945 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25948 /* If the first reference to this type was as the return type of an
25949 inline function, then it may not have a parent. Fix this now. */
25950 if (type_die->die_parent == NULL)
25951 add_child_die (scope_die, type_die);
25953 gen_member_die (type, type_die);
25955 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25956 if (TYPE_ARTIFICIAL (type))
25957 add_AT_flag (type_die, DW_AT_artificial, 1);
25959 /* GNU extension: Record what type our vtable lives in. */
25960 if (TYPE_VFIELD (type))
25962 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25964 gen_type_die (vtype, context_die);
25965 add_AT_die_ref (type_die, DW_AT_containing_type,
25966 lookup_type_die (vtype));
25969 else
25971 add_AT_flag (type_die, DW_AT_declaration, 1);
25973 /* We don't need to do this for function-local types. */
25974 if (TYPE_STUB_DECL (type)
25975 && ! decl_function_context (TYPE_STUB_DECL (type)))
25976 vec_safe_push (incomplete_types, type);
25979 if (get_AT (type_die, DW_AT_name))
25980 add_pubtype (type, type_die);
25983 /* Generate a DIE for a subroutine _type_. */
25985 static void
25986 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25988 tree return_type = TREE_TYPE (type);
25989 dw_die_ref subr_die
25990 = new_die (DW_TAG_subroutine_type,
25991 scope_die_for (type, context_die), type);
25993 equate_type_number_to_die (type, subr_die);
25994 add_prototyped_attribute (subr_die, type);
25995 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25996 context_die);
25997 add_alignment_attribute (subr_die, type);
25998 gen_formal_types_die (type, subr_die);
26000 if (get_AT (subr_die, DW_AT_name))
26001 add_pubtype (type, subr_die);
26002 if ((dwarf_version >= 5 || !dwarf_strict)
26003 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
26004 add_AT_flag (subr_die, DW_AT_reference, 1);
26005 if ((dwarf_version >= 5 || !dwarf_strict)
26006 && lang_hooks.types.type_dwarf_attribute (type,
26007 DW_AT_rvalue_reference) != -1)
26008 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
26011 /* Generate a DIE for a type definition. */
26013 static void
26014 gen_typedef_die (tree decl, dw_die_ref context_die)
26016 dw_die_ref type_die;
26017 tree type;
26019 if (TREE_ASM_WRITTEN (decl))
26021 if (DECL_ORIGINAL_TYPE (decl))
26022 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26023 return;
26026 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26027 checks in process_scope_var and modified_type_die), this should be called
26028 only for original types. */
26029 gcc_assert (decl_ultimate_origin (decl) == NULL
26030 || decl_ultimate_origin (decl) == decl);
26032 TREE_ASM_WRITTEN (decl) = 1;
26033 type_die = new_die (DW_TAG_typedef, context_die, decl);
26035 add_name_and_src_coords_attributes (type_die, decl);
26036 if (DECL_ORIGINAL_TYPE (decl))
26038 type = DECL_ORIGINAL_TYPE (decl);
26039 if (type == error_mark_node)
26040 return;
26042 gcc_assert (type != TREE_TYPE (decl));
26043 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26045 else
26047 type = TREE_TYPE (decl);
26048 if (type == error_mark_node)
26049 return;
26051 if (is_naming_typedef_decl (TYPE_NAME (type)))
26053 /* Here, we are in the case of decl being a typedef naming
26054 an anonymous type, e.g:
26055 typedef struct {...} foo;
26056 In that case TREE_TYPE (decl) is not a typedef variant
26057 type and TYPE_NAME of the anonymous type is set to the
26058 TYPE_DECL of the typedef. This construct is emitted by
26059 the C++ FE.
26061 TYPE is the anonymous struct named by the typedef
26062 DECL. As we need the DW_AT_type attribute of the
26063 DW_TAG_typedef to point to the DIE of TYPE, let's
26064 generate that DIE right away. add_type_attribute
26065 called below will then pick (via lookup_type_die) that
26066 anonymous struct DIE. */
26067 if (!TREE_ASM_WRITTEN (type))
26068 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26070 /* This is a GNU Extension. We are adding a
26071 DW_AT_linkage_name attribute to the DIE of the
26072 anonymous struct TYPE. The value of that attribute
26073 is the name of the typedef decl naming the anonymous
26074 struct. This greatly eases the work of consumers of
26075 this debug info. */
26076 add_linkage_name_raw (lookup_type_die (type), decl);
26080 add_type_attribute (type_die, type, decl_quals (decl), false,
26081 context_die);
26083 if (is_naming_typedef_decl (decl))
26084 /* We want that all subsequent calls to lookup_type_die with
26085 TYPE in argument yield the DW_TAG_typedef we have just
26086 created. */
26087 equate_type_number_to_die (type, type_die);
26089 add_alignment_attribute (type_die, TREE_TYPE (decl));
26091 add_accessibility_attribute (type_die, decl);
26093 if (DECL_ABSTRACT_P (decl))
26094 equate_decl_number_to_die (decl, type_die);
26096 if (get_AT (type_die, DW_AT_name))
26097 add_pubtype (decl, type_die);
26100 /* Generate a DIE for a struct, class, enum or union type. */
26102 static void
26103 gen_tagged_type_die (tree type,
26104 dw_die_ref context_die,
26105 enum debug_info_usage usage)
26107 if (type == NULL_TREE
26108 || !is_tagged_type (type))
26109 return;
26111 if (TREE_ASM_WRITTEN (type))
26113 /* If this is a nested type whose containing class hasn't been written
26114 out yet, writing it out will cover this one, too. This does not apply
26115 to instantiations of member class templates; they need to be added to
26116 the containing class as they are generated. FIXME: This hurts the
26117 idea of combining type decls from multiple TUs, since we can't predict
26118 what set of template instantiations we'll get. */
26119 else if (TYPE_CONTEXT (type)
26120 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26121 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26123 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26125 if (TREE_ASM_WRITTEN (type))
26126 return;
26128 /* If that failed, attach ourselves to the stub. */
26129 context_die = lookup_type_die (TYPE_CONTEXT (type));
26131 else if (TYPE_CONTEXT (type) != NULL_TREE
26132 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26134 /* If this type is local to a function that hasn't been written
26135 out yet, use a NULL context for now; it will be fixed up in
26136 decls_for_scope. */
26137 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26138 /* A declaration DIE doesn't count; nested types need to go in the
26139 specification. */
26140 if (context_die && is_declaration_die (context_die))
26141 context_die = NULL;
26143 else
26144 context_die = declare_in_namespace (type, context_die);
26146 if (TREE_CODE (type) == ENUMERAL_TYPE)
26148 /* This might have been written out by the call to
26149 declare_in_namespace. */
26150 if (!TREE_ASM_WRITTEN (type))
26151 gen_enumeration_type_die (type, context_die);
26153 else
26154 gen_struct_or_union_type_die (type, context_die, usage);
26156 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26157 it up if it is ever completed. gen_*_type_die will set it for us
26158 when appropriate. */
26161 /* Generate a type description DIE. */
26163 static void
26164 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26165 enum debug_info_usage usage)
26167 struct array_descr_info info;
26169 if (type == NULL_TREE || type == error_mark_node)
26170 return;
26172 if (flag_checking && type)
26173 verify_type (type);
26175 if (TYPE_NAME (type) != NULL_TREE
26176 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26177 && is_redundant_typedef (TYPE_NAME (type))
26178 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26179 /* The DECL of this type is a typedef we don't want to emit debug
26180 info for but we want debug info for its underlying typedef.
26181 This can happen for e.g, the injected-class-name of a C++
26182 type. */
26183 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26185 /* If TYPE is a typedef type variant, let's generate debug info
26186 for the parent typedef which TYPE is a type of. */
26187 if (typedef_variant_p (type))
26189 if (TREE_ASM_WRITTEN (type))
26190 return;
26192 tree name = TYPE_NAME (type);
26193 tree origin = decl_ultimate_origin (name);
26194 if (origin != NULL && origin != name)
26196 gen_decl_die (origin, NULL, NULL, context_die);
26197 return;
26200 /* Prevent broken recursion; we can't hand off to the same type. */
26201 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26203 /* Give typedefs the right scope. */
26204 context_die = scope_die_for (type, context_die);
26206 TREE_ASM_WRITTEN (type) = 1;
26208 gen_decl_die (name, NULL, NULL, context_die);
26209 return;
26212 /* If type is an anonymous tagged type named by a typedef, let's
26213 generate debug info for the typedef. */
26214 if (is_naming_typedef_decl (TYPE_NAME (type)))
26216 /* Give typedefs the right scope. */
26217 context_die = scope_die_for (type, context_die);
26219 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26220 return;
26223 if (lang_hooks.types.get_debug_type)
26225 tree debug_type = lang_hooks.types.get_debug_type (type);
26227 if (debug_type != NULL_TREE && debug_type != type)
26229 gen_type_die_with_usage (debug_type, context_die, usage);
26230 return;
26234 /* We are going to output a DIE to represent the unqualified version
26235 of this type (i.e. without any const or volatile qualifiers) so
26236 get the main variant (i.e. the unqualified version) of this type
26237 now. (Vectors and arrays are special because the debugging info is in the
26238 cloned type itself. Similarly function/method types can contain extra
26239 ref-qualification). */
26240 if (TREE_CODE (type) == FUNCTION_TYPE
26241 || TREE_CODE (type) == METHOD_TYPE)
26243 /* For function/method types, can't use type_main_variant here,
26244 because that can have different ref-qualifiers for C++,
26245 but try to canonicalize. */
26246 tree main = TYPE_MAIN_VARIANT (type);
26247 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26248 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26249 && check_base_type (t, main)
26250 && check_lang_type (t, type))
26252 type = t;
26253 break;
26256 else if (TREE_CODE (type) != VECTOR_TYPE
26257 && TREE_CODE (type) != ARRAY_TYPE)
26258 type = type_main_variant (type);
26260 /* If this is an array type with hidden descriptor, handle it first. */
26261 if (!TREE_ASM_WRITTEN (type)
26262 && lang_hooks.types.get_array_descr_info)
26264 memset (&info, 0, sizeof (info));
26265 if (lang_hooks.types.get_array_descr_info (type, &info))
26267 /* Fortran sometimes emits array types with no dimension. */
26268 gcc_assert (info.ndimensions >= 0
26269 && (info.ndimensions
26270 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26271 gen_descr_array_type_die (type, &info, context_die);
26272 TREE_ASM_WRITTEN (type) = 1;
26273 return;
26277 if (TREE_ASM_WRITTEN (type))
26279 /* Variable-length types may be incomplete even if
26280 TREE_ASM_WRITTEN. For such types, fall through to
26281 gen_array_type_die() and possibly fill in
26282 DW_AT_{upper,lower}_bound attributes. */
26283 if ((TREE_CODE (type) != ARRAY_TYPE
26284 && TREE_CODE (type) != RECORD_TYPE
26285 && TREE_CODE (type) != UNION_TYPE
26286 && TREE_CODE (type) != QUAL_UNION_TYPE)
26287 || !variably_modified_type_p (type, NULL))
26288 return;
26291 switch (TREE_CODE (type))
26293 case ERROR_MARK:
26294 break;
26296 case POINTER_TYPE:
26297 case REFERENCE_TYPE:
26298 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26299 ensures that the gen_type_die recursion will terminate even if the
26300 type is recursive. Recursive types are possible in Ada. */
26301 /* ??? We could perhaps do this for all types before the switch
26302 statement. */
26303 TREE_ASM_WRITTEN (type) = 1;
26305 /* For these types, all that is required is that we output a DIE (or a
26306 set of DIEs) to represent the "basis" type. */
26307 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26308 DINFO_USAGE_IND_USE);
26309 break;
26311 case OFFSET_TYPE:
26312 /* This code is used for C++ pointer-to-data-member types.
26313 Output a description of the relevant class type. */
26314 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26315 DINFO_USAGE_IND_USE);
26317 /* Output a description of the type of the object pointed to. */
26318 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26319 DINFO_USAGE_IND_USE);
26321 /* Now output a DIE to represent this pointer-to-data-member type
26322 itself. */
26323 gen_ptr_to_mbr_type_die (type, context_die);
26324 break;
26326 case FUNCTION_TYPE:
26327 /* Force out return type (in case it wasn't forced out already). */
26328 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26329 DINFO_USAGE_DIR_USE);
26330 gen_subroutine_type_die (type, context_die);
26331 break;
26333 case METHOD_TYPE:
26334 /* Force out return type (in case it wasn't forced out already). */
26335 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26336 DINFO_USAGE_DIR_USE);
26337 gen_subroutine_type_die (type, context_die);
26338 break;
26340 case ARRAY_TYPE:
26341 case VECTOR_TYPE:
26342 gen_array_type_die (type, context_die);
26343 break;
26345 case ENUMERAL_TYPE:
26346 case RECORD_TYPE:
26347 case UNION_TYPE:
26348 case QUAL_UNION_TYPE:
26349 gen_tagged_type_die (type, context_die, usage);
26350 return;
26352 case VOID_TYPE:
26353 case OPAQUE_TYPE:
26354 case INTEGER_TYPE:
26355 case REAL_TYPE:
26356 case FIXED_POINT_TYPE:
26357 case COMPLEX_TYPE:
26358 case BOOLEAN_TYPE:
26359 /* No DIEs needed for fundamental types. */
26360 break;
26362 case NULLPTR_TYPE:
26363 case LANG_TYPE:
26364 /* Just use DW_TAG_unspecified_type. */
26366 dw_die_ref type_die = lookup_type_die (type);
26367 if (type_die == NULL)
26369 tree name = TYPE_IDENTIFIER (type);
26370 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26371 type);
26372 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26373 equate_type_number_to_die (type, type_die);
26376 break;
26378 default:
26379 if (is_cxx_auto (type))
26381 tree name = TYPE_IDENTIFIER (type);
26382 dw_die_ref *die = (name == get_identifier ("auto")
26383 ? &auto_die : &decltype_auto_die);
26384 if (!*die)
26386 *die = new_die (DW_TAG_unspecified_type,
26387 comp_unit_die (), NULL_TREE);
26388 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26390 equate_type_number_to_die (type, *die);
26391 break;
26393 gcc_unreachable ();
26396 TREE_ASM_WRITTEN (type) = 1;
26399 static void
26400 gen_type_die (tree type, dw_die_ref context_die)
26402 if (type != error_mark_node)
26404 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26405 if (flag_checking)
26407 dw_die_ref die = lookup_type_die (type);
26408 if (die)
26409 check_die (die);
26414 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26415 things which are local to the given block. */
26417 static void
26418 gen_block_die (tree stmt, dw_die_ref context_die)
26420 int must_output_die = 0;
26421 bool inlined_func;
26423 /* Ignore blocks that are NULL. */
26424 if (stmt == NULL_TREE)
26425 return;
26427 inlined_func = inlined_function_outer_scope_p (stmt);
26429 /* If the block is one fragment of a non-contiguous block, do not
26430 process the variables, since they will have been done by the
26431 origin block. Do process subblocks. */
26432 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26434 tree sub;
26436 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26437 gen_block_die (sub, context_die);
26439 return;
26442 /* Determine if we need to output any Dwarf DIEs at all to represent this
26443 block. */
26444 if (inlined_func)
26445 /* The outer scopes for inlinings *must* always be represented. We
26446 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26447 must_output_die = 1;
26448 else if (lookup_block_die (stmt))
26449 /* If we already have a DIE then it was filled early. Meanwhile
26450 we might have pruned all BLOCK_VARS as optimized out but we
26451 still want to generate high/low PC attributes so output it. */
26452 must_output_die = 1;
26453 else if (TREE_USED (stmt)
26454 || TREE_ASM_WRITTEN (stmt))
26456 /* Determine if this block directly contains any "significant"
26457 local declarations which we will need to output DIEs for. */
26458 if (debug_info_level > DINFO_LEVEL_TERSE)
26460 /* We are not in terse mode so any local declaration that
26461 is not ignored for debug purposes counts as being a
26462 "significant" one. */
26463 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26464 must_output_die = 1;
26465 else
26466 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26467 if (!DECL_IGNORED_P (var))
26469 must_output_die = 1;
26470 break;
26473 else if (!dwarf2out_ignore_block (stmt))
26474 must_output_die = 1;
26477 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26478 DIE for any block which contains no significant local declarations at
26479 all. Rather, in such cases we just call `decls_for_scope' so that any
26480 needed Dwarf info for any sub-blocks will get properly generated. Note
26481 that in terse mode, our definition of what constitutes a "significant"
26482 local declaration gets restricted to include only inlined function
26483 instances and local (nested) function definitions. */
26484 if (must_output_die)
26486 if (inlined_func)
26487 gen_inlined_subroutine_die (stmt, context_die);
26488 else
26489 gen_lexical_block_die (stmt, context_die);
26491 else
26492 decls_for_scope (stmt, context_die);
26495 /* Process variable DECL (or variable with origin ORIGIN) within
26496 block STMT and add it to CONTEXT_DIE. */
26497 static void
26498 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26500 dw_die_ref die;
26501 tree decl_or_origin = decl ? decl : origin;
26503 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26504 die = lookup_decl_die (decl_or_origin);
26505 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26507 if (TYPE_DECL_IS_STUB (decl_or_origin))
26508 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26509 else
26510 die = lookup_decl_die (decl_or_origin);
26511 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26512 if (! die && ! early_dwarf)
26513 return;
26515 else
26516 die = NULL;
26518 /* Avoid creating DIEs for local typedefs and concrete static variables that
26519 will only be pruned later. */
26520 if ((origin || decl_ultimate_origin (decl))
26521 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26522 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26524 origin = decl_ultimate_origin (decl_or_origin);
26525 if (decl && VAR_P (decl) && die != NULL)
26527 die = lookup_decl_die (origin);
26528 if (die != NULL)
26529 equate_decl_number_to_die (decl, die);
26531 return;
26534 if (die != NULL && die->die_parent == NULL)
26535 add_child_die (context_die, die);
26536 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26538 if (early_dwarf)
26539 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26540 stmt, context_die);
26542 else
26544 if (decl && DECL_P (decl))
26546 die = lookup_decl_die (decl);
26548 /* Early created DIEs do not have a parent as the decls refer
26549 to the function as DECL_CONTEXT rather than the BLOCK. */
26550 if (die && die->die_parent == NULL)
26552 gcc_assert (in_lto_p);
26553 add_child_die (context_die, die);
26557 gen_decl_die (decl, origin, NULL, context_die);
26561 /* Generate all of the decls declared within a given scope and (recursively)
26562 all of its sub-blocks. */
26564 static void
26565 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26567 tree decl;
26568 unsigned int i;
26569 tree subblocks;
26571 /* Ignore NULL blocks. */
26572 if (stmt == NULL_TREE)
26573 return;
26575 /* Output the DIEs to represent all of the data objects and typedefs
26576 declared directly within this block but not within any nested
26577 sub-blocks. Also, nested function and tag DIEs have been
26578 generated with a parent of NULL; fix that up now. We don't
26579 have to do this if we're at -g1. */
26580 if (debug_info_level > DINFO_LEVEL_TERSE)
26582 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26583 process_scope_var (stmt, decl, NULL_TREE, context_die);
26584 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26585 origin - avoid doing this twice as we have no good way to see
26586 if we've done it once already. */
26587 if (! early_dwarf)
26588 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26590 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26591 if (decl == current_function_decl)
26592 /* Ignore declarations of the current function, while they
26593 are declarations, gen_subprogram_die would treat them
26594 as definitions again, because they are equal to
26595 current_function_decl and endlessly recurse. */;
26596 else if (TREE_CODE (decl) == FUNCTION_DECL)
26597 process_scope_var (stmt, decl, NULL_TREE, context_die);
26598 else
26599 process_scope_var (stmt, NULL_TREE, decl, context_die);
26603 /* Even if we're at -g1, we need to process the subblocks in order to get
26604 inlined call information. */
26606 /* Output the DIEs to represent all sub-blocks (and the items declared
26607 therein) of this block. */
26608 if (recurse)
26609 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26610 subblocks != NULL;
26611 subblocks = BLOCK_CHAIN (subblocks))
26612 gen_block_die (subblocks, context_die);
26615 /* Is this a typedef we can avoid emitting? */
26617 static bool
26618 is_redundant_typedef (const_tree decl)
26620 if (TYPE_DECL_IS_STUB (decl))
26621 return true;
26623 if (DECL_ARTIFICIAL (decl)
26624 && DECL_CONTEXT (decl)
26625 && is_tagged_type (DECL_CONTEXT (decl))
26626 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26627 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26628 /* Also ignore the artificial member typedef for the class name. */
26629 return true;
26631 return false;
26634 /* Return TRUE if TYPE is a typedef that names a type for linkage
26635 purposes. This kind of typedefs is produced by the C++ FE for
26636 constructs like:
26638 typedef struct {...} foo;
26640 In that case, there is no typedef variant type produced for foo.
26641 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26642 struct type. */
26644 static bool
26645 is_naming_typedef_decl (const_tree decl)
26647 if (decl == NULL_TREE
26648 || TREE_CODE (decl) != TYPE_DECL
26649 || DECL_NAMELESS (decl)
26650 || !is_tagged_type (TREE_TYPE (decl))
26651 || DECL_IS_UNDECLARED_BUILTIN (decl)
26652 || is_redundant_typedef (decl)
26653 /* It looks like Ada produces TYPE_DECLs that are very similar
26654 to C++ naming typedefs but that have different
26655 semantics. Let's be specific to c++ for now. */
26656 || !is_cxx (decl))
26657 return FALSE;
26659 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26660 && TYPE_NAME (TREE_TYPE (decl)) == decl
26661 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26662 != TYPE_NAME (TREE_TYPE (decl))));
26665 /* Looks up the DIE for a context. */
26667 static inline dw_die_ref
26668 lookup_context_die (tree context)
26670 if (context)
26672 /* Find die that represents this context. */
26673 if (TYPE_P (context))
26675 context = TYPE_MAIN_VARIANT (context);
26676 dw_die_ref ctx = lookup_type_die (context);
26677 if (!ctx)
26678 return NULL;
26679 return strip_naming_typedef (context, ctx);
26681 else
26682 return lookup_decl_die (context);
26684 return comp_unit_die ();
26687 /* Returns the DIE for a context. */
26689 static inline dw_die_ref
26690 get_context_die (tree context)
26692 if (context)
26694 /* Find die that represents this context. */
26695 if (TYPE_P (context))
26697 context = TYPE_MAIN_VARIANT (context);
26698 return strip_naming_typedef (context, force_type_die (context));
26700 else
26701 return force_decl_die (context);
26703 return comp_unit_die ();
26706 /* Returns the DIE for decl. A DIE will always be returned. */
26708 static dw_die_ref
26709 force_decl_die (tree decl)
26711 dw_die_ref decl_die;
26712 unsigned saved_external_flag;
26713 tree save_fn = NULL_TREE;
26714 decl_die = lookup_decl_die (decl);
26715 if (!decl_die)
26717 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26719 decl_die = lookup_decl_die (decl);
26720 if (decl_die)
26721 return decl_die;
26723 switch (TREE_CODE (decl))
26725 case FUNCTION_DECL:
26726 /* Clear current_function_decl, so that gen_subprogram_die thinks
26727 that this is a declaration. At this point, we just want to force
26728 declaration die. */
26729 save_fn = current_function_decl;
26730 current_function_decl = NULL_TREE;
26731 gen_subprogram_die (decl, context_die);
26732 current_function_decl = save_fn;
26733 break;
26735 case VAR_DECL:
26736 /* Set external flag to force declaration die. Restore it after
26737 gen_decl_die() call. */
26738 saved_external_flag = DECL_EXTERNAL (decl);
26739 DECL_EXTERNAL (decl) = 1;
26740 gen_decl_die (decl, NULL, NULL, context_die);
26741 DECL_EXTERNAL (decl) = saved_external_flag;
26742 break;
26744 case NAMESPACE_DECL:
26745 if (dwarf_version >= 3 || !dwarf_strict)
26746 dwarf2out_decl (decl);
26747 else
26748 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26749 decl_die = comp_unit_die ();
26750 break;
26752 case CONST_DECL:
26753 /* Enumerators shouldn't need force_decl_die. */
26754 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26755 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26756 gen_decl_die (decl, NULL, NULL, context_die);
26757 break;
26759 case TRANSLATION_UNIT_DECL:
26760 decl_die = comp_unit_die ();
26761 break;
26763 default:
26764 gcc_unreachable ();
26767 /* We should be able to find the DIE now. */
26768 if (!decl_die)
26769 decl_die = lookup_decl_die (decl);
26770 gcc_assert (decl_die);
26773 return decl_die;
26776 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26777 always returned. */
26779 static dw_die_ref
26780 force_type_die (tree type)
26782 dw_die_ref type_die;
26784 type_die = lookup_type_die (type);
26785 if (!type_die)
26787 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26789 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26790 false, context_die);
26791 gcc_assert (type_die);
26793 return type_die;
26796 /* Force out any required namespaces to be able to output DECL,
26797 and return the new context_die for it, if it's changed. */
26799 static dw_die_ref
26800 setup_namespace_context (tree thing, dw_die_ref context_die)
26802 tree context = (DECL_P (thing)
26803 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26804 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26805 /* Force out the namespace. */
26806 context_die = force_decl_die (context);
26808 return context_die;
26811 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26812 type) within its namespace, if appropriate.
26814 For compatibility with older debuggers, namespace DIEs only contain
26815 declarations; all definitions are emitted at CU scope, with
26816 DW_AT_specification pointing to the declaration (like with class
26817 members). */
26819 static dw_die_ref
26820 declare_in_namespace (tree thing, dw_die_ref context_die)
26822 dw_die_ref ns_context;
26824 if (debug_info_level <= DINFO_LEVEL_TERSE)
26825 return context_die;
26827 /* External declarations in the local scope only need to be emitted
26828 once, not once in the namespace and once in the scope.
26830 This avoids declaring the `extern' below in the
26831 namespace DIE as well as in the innermost scope:
26833 namespace S
26835 int i=5;
26836 int foo()
26838 int i=8;
26839 extern int i;
26840 return i;
26844 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26845 return context_die;
26847 /* If this decl is from an inlined function, then don't try to emit it in its
26848 namespace, as we will get confused. It would have already been emitted
26849 when the abstract instance of the inline function was emitted anyways. */
26850 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26851 return context_die;
26853 ns_context = setup_namespace_context (thing, context_die);
26855 if (ns_context != context_die)
26857 if (is_fortran () || is_dlang ())
26858 return ns_context;
26859 if (DECL_P (thing))
26860 gen_decl_die (thing, NULL, NULL, ns_context);
26861 else
26862 gen_type_die (thing, ns_context);
26864 return context_die;
26867 /* Generate a DIE for a namespace or namespace alias. */
26869 static void
26870 gen_namespace_die (tree decl, dw_die_ref context_die)
26872 dw_die_ref namespace_die;
26874 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26875 they are an alias of. */
26876 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26878 /* Output a real namespace or module. */
26879 context_die = setup_namespace_context (decl, comp_unit_die ());
26880 namespace_die = new_die (is_fortran () || is_dlang ()
26881 ? DW_TAG_module : DW_TAG_namespace,
26882 context_die, decl);
26883 /* For Fortran modules defined in different CU don't add src coords. */
26884 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26886 const char *name = dwarf2_name (decl, 0);
26887 if (name)
26888 add_name_attribute (namespace_die, name);
26890 else
26891 add_name_and_src_coords_attributes (namespace_die, decl);
26892 if (DECL_EXTERNAL (decl))
26893 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26894 equate_decl_number_to_die (decl, namespace_die);
26896 else
26898 /* Output a namespace alias. */
26900 /* Force out the namespace we are an alias of, if necessary. */
26901 dw_die_ref origin_die
26902 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26904 if (DECL_FILE_SCOPE_P (decl)
26905 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26906 context_die = setup_namespace_context (decl, comp_unit_die ());
26907 /* Now create the namespace alias DIE. */
26908 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26909 add_name_and_src_coords_attributes (namespace_die, decl);
26910 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26911 equate_decl_number_to_die (decl, namespace_die);
26913 if ((dwarf_version >= 5 || !dwarf_strict)
26914 && lang_hooks.decls.decl_dwarf_attribute (decl,
26915 DW_AT_export_symbols) == 1)
26916 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26918 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26919 if (want_pubnames ())
26920 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26923 /* Generate Dwarf debug information for a decl described by DECL.
26924 The return value is currently only meaningful for PARM_DECLs,
26925 for all other decls it returns NULL.
26927 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26928 It can be NULL otherwise. */
26930 static dw_die_ref
26931 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26932 dw_die_ref context_die)
26934 tree decl_or_origin = decl ? decl : origin;
26935 tree class_origin = NULL, ultimate_origin;
26937 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26938 return NULL;
26940 switch (TREE_CODE (decl_or_origin))
26942 case ERROR_MARK:
26943 break;
26945 case CONST_DECL:
26946 if (!is_fortran () && !is_ada () && !is_dlang ())
26948 /* The individual enumerators of an enum type get output when we output
26949 the Dwarf representation of the relevant enum type itself. */
26950 break;
26953 /* Emit its type. */
26954 gen_type_die (TREE_TYPE (decl), context_die);
26956 /* And its containing namespace. */
26957 context_die = declare_in_namespace (decl, context_die);
26959 gen_const_die (decl, context_die);
26960 break;
26962 case FUNCTION_DECL:
26963 #if 0
26964 /* FIXME */
26965 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26966 on local redeclarations of global functions. That seems broken. */
26967 if (current_function_decl != decl)
26968 /* This is only a declaration. */;
26969 #endif
26971 /* We should have abstract copies already and should not generate
26972 stray type DIEs in late LTO dumping. */
26973 if (! early_dwarf)
26976 /* If we're emitting a clone, emit info for the abstract instance. */
26977 else if (origin || DECL_ORIGIN (decl) != decl)
26978 dwarf2out_abstract_function (origin
26979 ? DECL_ORIGIN (origin)
26980 : DECL_ABSTRACT_ORIGIN (decl));
26982 /* If we're emitting a possibly inlined function emit it as
26983 abstract instance. */
26984 else if (cgraph_function_possibly_inlined_p (decl)
26985 && ! DECL_ABSTRACT_P (decl)
26986 && ! class_or_namespace_scope_p (context_die)
26987 /* dwarf2out_abstract_function won't emit a die if this is just
26988 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26989 that case, because that works only if we have a die. */
26990 && DECL_INITIAL (decl) != NULL_TREE)
26991 dwarf2out_abstract_function (decl);
26993 /* Otherwise we're emitting the primary DIE for this decl. */
26994 else if (debug_info_level > DINFO_LEVEL_TERSE)
26996 /* Before we describe the FUNCTION_DECL itself, make sure that we
26997 have its containing type. */
26998 if (!origin)
26999 origin = decl_class_context (decl);
27000 if (origin != NULL_TREE)
27001 gen_type_die (origin, context_die);
27003 /* And its return type. */
27004 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
27006 /* And its virtual context. */
27007 if (DECL_VINDEX (decl) != NULL_TREE)
27008 gen_type_die (DECL_CONTEXT (decl), context_die);
27010 /* Make sure we have a member DIE for decl. */
27011 if (origin != NULL_TREE)
27012 gen_type_die_for_member (origin, decl, context_die);
27014 /* And its containing namespace. */
27015 context_die = declare_in_namespace (decl, context_die);
27018 /* Now output a DIE to represent the function itself. */
27019 if (decl)
27020 gen_subprogram_die (decl, context_die);
27021 break;
27023 case TYPE_DECL:
27024 /* If we are in terse mode, don't generate any DIEs to represent any
27025 actual typedefs. */
27026 if (debug_info_level <= DINFO_LEVEL_TERSE)
27027 break;
27029 /* In the special case of a TYPE_DECL node representing the declaration
27030 of some type tag, if the given TYPE_DECL is marked as having been
27031 instantiated from some other (original) TYPE_DECL node (e.g. one which
27032 was generated within the original definition of an inline function) we
27033 used to generate a special (abbreviated) DW_TAG_structure_type,
27034 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27035 should be actually referencing those DIEs, as variable DIEs with that
27036 type would be emitted already in the abstract origin, so it was always
27037 removed during unused type prunning. Don't add anything in this
27038 case. */
27039 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27040 break;
27042 if (is_redundant_typedef (decl))
27043 gen_type_die (TREE_TYPE (decl), context_die);
27044 else
27045 /* Output a DIE to represent the typedef itself. */
27046 gen_typedef_die (decl, context_die);
27047 break;
27049 case LABEL_DECL:
27050 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27051 gen_label_die (decl, context_die);
27052 break;
27054 case VAR_DECL:
27055 case RESULT_DECL:
27056 /* If we are in terse mode, don't generate any DIEs to represent any
27057 variable declarations or definitions unless it is external. */
27058 if (debug_info_level < DINFO_LEVEL_TERSE
27059 || (debug_info_level == DINFO_LEVEL_TERSE
27060 && !TREE_PUBLIC (decl_or_origin)))
27061 break;
27063 if (debug_info_level > DINFO_LEVEL_TERSE)
27065 /* Avoid generating stray type DIEs during late dwarf dumping.
27066 All types have been dumped early. */
27067 if (early_dwarf
27068 /* ??? But in LTRANS we cannot annotate early created variably
27069 modified type DIEs without copying them and adjusting all
27070 references to them. Dump them again as happens for inlining
27071 which copies both the decl and the types. */
27072 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27073 in VLA bound information for example. */
27074 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27075 current_function_decl)))
27077 /* Output any DIEs that are needed to specify the type of this data
27078 object. */
27079 if (decl_by_reference_p (decl_or_origin))
27080 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27081 else
27082 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27085 if (early_dwarf)
27087 /* And its containing type. */
27088 class_origin = decl_class_context (decl_or_origin);
27089 if (class_origin != NULL_TREE)
27090 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27092 /* And its containing namespace. */
27093 context_die = declare_in_namespace (decl_or_origin, context_die);
27097 /* Now output the DIE to represent the data object itself. This gets
27098 complicated because of the possibility that the VAR_DECL really
27099 represents an inlined instance of a formal parameter for an inline
27100 function. */
27101 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27102 if (ultimate_origin != NULL_TREE
27103 && TREE_CODE (ultimate_origin) == PARM_DECL)
27104 gen_formal_parameter_die (decl, origin,
27105 true /* Emit name attribute. */,
27106 context_die);
27107 else
27108 gen_variable_die (decl, origin, context_die);
27109 break;
27111 case FIELD_DECL:
27112 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27113 /* Ignore the nameless fields that are used to skip bits but handle C++
27114 anonymous unions and structs. */
27115 if (DECL_NAME (decl) != NULL_TREE
27116 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27117 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27119 gen_type_die (member_declared_type (decl), context_die);
27120 gen_field_die (decl, ctx, context_die);
27122 break;
27124 case PARM_DECL:
27125 /* Avoid generating stray type DIEs during late dwarf dumping.
27126 All types have been dumped early. */
27127 if (early_dwarf
27128 /* ??? But in LTRANS we cannot annotate early created variably
27129 modified type DIEs without copying them and adjusting all
27130 references to them. Dump them again as happens for inlining
27131 which copies both the decl and the types. */
27132 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27133 in VLA bound information for example. */
27134 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27135 current_function_decl)))
27137 if (DECL_BY_REFERENCE (decl_or_origin))
27138 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27139 else
27140 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27142 return gen_formal_parameter_die (decl, origin,
27143 true /* Emit name attribute. */,
27144 context_die);
27146 case NAMESPACE_DECL:
27147 if (dwarf_version >= 3 || !dwarf_strict)
27148 gen_namespace_die (decl, context_die);
27149 break;
27151 case IMPORTED_DECL:
27152 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27153 DECL_CONTEXT (decl), context_die);
27154 break;
27156 case NAMELIST_DECL:
27157 gen_namelist_decl (DECL_NAME (decl), context_die,
27158 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27159 break;
27161 default:
27162 /* Probably some frontend-internal decl. Assume we don't care. */
27163 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27164 break;
27167 return NULL;
27170 /* Output initial debug information for global DECL. Called at the
27171 end of the parsing process.
27173 This is the initial debug generation process. As such, the DIEs
27174 generated may be incomplete. A later debug generation pass
27175 (dwarf2out_late_global_decl) will augment the information generated
27176 in this pass (e.g., with complete location info). */
27178 static void
27179 dwarf2out_early_global_decl (tree decl)
27181 set_early_dwarf s;
27183 /* gen_decl_die() will set DECL_ABSTRACT because
27184 cgraph_function_possibly_inlined_p() returns true. This is in
27185 turn will cause DW_AT_inline attributes to be set.
27187 This happens because at early dwarf generation, there is no
27188 cgraph information, causing cgraph_function_possibly_inlined_p()
27189 to return true. Trick cgraph_function_possibly_inlined_p()
27190 while we generate dwarf early. */
27191 bool save = symtab->global_info_ready;
27192 symtab->global_info_ready = true;
27194 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27195 other DECLs and they can point to template types or other things
27196 that dwarf2out can't handle when done via dwarf2out_decl. */
27197 if (TREE_CODE (decl) != TYPE_DECL
27198 && TREE_CODE (decl) != PARM_DECL)
27200 if (TREE_CODE (decl) == FUNCTION_DECL)
27202 tree save_fndecl = current_function_decl;
27204 /* For nested functions, make sure we have DIEs for the parents first
27205 so that all nested DIEs are generated at the proper scope in the
27206 first shot. */
27207 tree context = decl_function_context (decl);
27208 if (context != NULL)
27210 dw_die_ref context_die = lookup_decl_die (context);
27211 current_function_decl = context;
27213 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27214 enough so that it lands in its own context. This avoids type
27215 pruning issues later on. */
27216 if (context_die == NULL || is_declaration_die (context_die))
27217 dwarf2out_early_global_decl (context);
27220 /* Emit an abstract origin of a function first. This happens
27221 with C++ constructor clones for example and makes
27222 dwarf2out_abstract_function happy which requires the early
27223 DIE of the abstract instance to be present. */
27224 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27225 dw_die_ref origin_die;
27226 if (origin != NULL
27227 /* Do not emit the DIE multiple times but make sure to
27228 process it fully here in case we just saw a declaration. */
27229 && ((origin_die = lookup_decl_die (origin)) == NULL
27230 || is_declaration_die (origin_die)))
27232 current_function_decl = origin;
27233 dwarf2out_decl (origin);
27236 /* Emit the DIE for decl but avoid doing that multiple times. */
27237 dw_die_ref old_die;
27238 if ((old_die = lookup_decl_die (decl)) == NULL
27239 || is_declaration_die (old_die))
27241 current_function_decl = decl;
27242 dwarf2out_decl (decl);
27245 current_function_decl = save_fndecl;
27247 else
27248 dwarf2out_decl (decl);
27250 symtab->global_info_ready = save;
27253 /* Return whether EXPR is an expression with the following pattern:
27254 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27256 static bool
27257 is_trivial_indirect_ref (tree expr)
27259 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27260 return false;
27262 tree nop = TREE_OPERAND (expr, 0);
27263 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27264 return false;
27266 tree int_cst = TREE_OPERAND (nop, 0);
27267 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27270 /* Output debug information for global decl DECL. Called from
27271 toplev.cc after compilation proper has finished. */
27273 static void
27274 dwarf2out_late_global_decl (tree decl)
27276 /* Fill-in any location information we were unable to determine
27277 on the first pass. */
27278 if (VAR_P (decl))
27280 dw_die_ref die = lookup_decl_die (decl);
27282 /* We may have to generate full debug late for LTO in case debug
27283 was not enabled at compile-time or the target doesn't support
27284 the LTO early debug scheme. */
27285 if (! die && in_lto_p)
27286 dwarf2out_decl (decl);
27287 else if (die)
27289 /* We get called via the symtab code invoking late_global_decl
27290 for symbols that are optimized out.
27292 Do not add locations for those, except if they have a
27293 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27294 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27295 INDIRECT_REF expression, as this could generate relocations to
27296 text symbols in LTO object files, which is invalid. */
27297 varpool_node *node = varpool_node::get (decl);
27298 if ((! node || ! node->definition)
27299 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27300 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27301 tree_add_const_value_attribute_for_decl (die, decl);
27302 else
27303 add_location_or_const_value_attribute (die, decl, false);
27308 /* Output debug information for type decl DECL. Called from toplev.cc
27309 and from language front ends (to record built-in types). */
27310 static void
27311 dwarf2out_type_decl (tree decl, int local)
27313 if (!local)
27315 set_early_dwarf s;
27316 dwarf2out_decl (decl);
27320 /* Output debug information for imported module or decl DECL.
27321 NAME is non-NULL name in the lexical block if the decl has been renamed.
27322 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27323 that DECL belongs to.
27324 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27325 static void
27326 dwarf2out_imported_module_or_decl_1 (tree decl,
27327 tree name,
27328 tree lexical_block,
27329 dw_die_ref lexical_block_die)
27331 expanded_location xloc;
27332 dw_die_ref imported_die = NULL;
27333 dw_die_ref at_import_die;
27335 if (TREE_CODE (decl) == IMPORTED_DECL)
27337 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27338 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27339 gcc_assert (decl);
27341 else
27342 xloc = expand_location (input_location);
27344 if (TREE_CODE (decl) == TYPE_DECL)
27346 at_import_die = force_type_die (TREE_TYPE (decl));
27347 /* For namespace N { typedef void T; } using N::T; base_type_die
27348 returns NULL, but DW_TAG_imported_declaration requires
27349 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27350 if (!at_import_die)
27352 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27353 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27354 at_import_die = lookup_type_die (TREE_TYPE (decl));
27355 gcc_assert (at_import_die);
27358 else
27360 at_import_die = lookup_decl_die (decl);
27361 if (!at_import_die)
27363 /* If we're trying to avoid duplicate debug info, we may not have
27364 emitted the member decl for this field. Emit it now. */
27365 if (TREE_CODE (decl) == FIELD_DECL)
27367 tree type = DECL_CONTEXT (decl);
27369 if (TYPE_CONTEXT (type)
27370 && TYPE_P (TYPE_CONTEXT (type))
27371 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27372 DINFO_USAGE_DIR_USE))
27373 return;
27374 gen_type_die_for_member (type, decl,
27375 get_context_die (TYPE_CONTEXT (type)));
27377 if (TREE_CODE (decl) == CONST_DECL)
27379 /* Individual enumerators of an enum type do not get output here
27380 (see gen_decl_die), so we cannot call force_decl_die. */
27381 if (!is_fortran () && !is_ada () && !is_dlang ())
27382 return;
27384 if (TREE_CODE (decl) == NAMELIST_DECL)
27385 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27386 get_context_die (DECL_CONTEXT (decl)),
27387 NULL_TREE);
27388 else
27389 at_import_die = force_decl_die (decl);
27393 if (TREE_CODE (decl) == NAMESPACE_DECL)
27395 if (dwarf_version >= 3 || !dwarf_strict)
27396 imported_die = new_die (DW_TAG_imported_module,
27397 lexical_block_die,
27398 lexical_block);
27399 else
27400 return;
27402 else
27403 imported_die = new_die (DW_TAG_imported_declaration,
27404 lexical_block_die,
27405 lexical_block);
27407 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27408 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27409 if (debug_column_info && xloc.column)
27410 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27411 if (name)
27412 add_AT_string (imported_die, DW_AT_name,
27413 IDENTIFIER_POINTER (name));
27414 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27417 /* Output debug information for imported module or decl DECL.
27418 NAME is non-NULL name in context if the decl has been renamed.
27419 CHILD is true if decl is one of the renamed decls as part of
27420 importing whole module.
27421 IMPLICIT is set if this hook is called for an implicit import
27422 such as inline namespace. */
27424 static void
27425 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27426 bool child, bool implicit)
27428 /* dw_die_ref at_import_die; */
27429 dw_die_ref scope_die;
27431 if (debug_info_level <= DINFO_LEVEL_TERSE)
27432 return;
27434 gcc_assert (decl);
27436 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27437 should be enough, for DWARF4 and older even if we emit as extension
27438 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27439 for the benefit of consumers unaware of DW_AT_export_symbols. */
27440 if (implicit
27441 && dwarf_version >= 5
27442 && lang_hooks.decls.decl_dwarf_attribute (decl,
27443 DW_AT_export_symbols) == 1)
27444 return;
27446 set_early_dwarf s;
27448 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27449 We need decl DIE for reference and scope die. First, get DIE for the decl
27450 itself. */
27452 /* Get the scope die for decl context. Use comp_unit_die for global module
27453 or decl. If die is not found for non globals, force new die. */
27454 if (context
27455 && TYPE_P (context)
27456 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27457 return;
27459 scope_die = get_context_die (context);
27461 if (child)
27463 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27464 there is nothing we can do, here. */
27465 if (dwarf_version < 3 && dwarf_strict)
27466 return;
27468 gcc_assert (scope_die->die_child);
27469 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27470 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27471 scope_die = scope_die->die_child;
27474 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27475 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27478 /* Output debug information for namelists. */
27480 static dw_die_ref
27481 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27483 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27484 tree value;
27485 unsigned i;
27487 if (debug_info_level <= DINFO_LEVEL_TERSE)
27488 return NULL;
27490 gcc_assert (scope_die != NULL);
27491 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27492 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27494 /* If there are no item_decls, we have a nondefining namelist, e.g.
27495 with USE association; hence, set DW_AT_declaration. */
27496 if (item_decls == NULL_TREE)
27498 add_AT_flag (nml_die, DW_AT_declaration, 1);
27499 return nml_die;
27502 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27504 nml_item_ref_die = lookup_decl_die (value);
27505 if (!nml_item_ref_die)
27506 nml_item_ref_die = force_decl_die (value);
27508 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27509 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27511 return nml_die;
27515 /* Write the debugging output for DECL and return the DIE. */
27517 static void
27518 dwarf2out_decl (tree decl)
27520 dw_die_ref context_die = comp_unit_die ();
27522 switch (TREE_CODE (decl))
27524 case ERROR_MARK:
27525 return;
27527 case FUNCTION_DECL:
27528 /* If we're a nested function, initially use a parent of NULL; if we're
27529 a plain function, this will be fixed up in decls_for_scope. If
27530 we're a method, it will be ignored, since we already have a DIE.
27531 Avoid doing this late though since clones of class methods may
27532 otherwise end up in limbo and create type DIEs late. */
27533 if (early_dwarf
27534 && decl_function_context (decl)
27535 /* But if we're in terse mode, we don't care about scope. */
27536 && debug_info_level > DINFO_LEVEL_TERSE)
27537 context_die = NULL;
27538 break;
27540 case VAR_DECL:
27541 /* For local statics lookup proper context die. */
27542 if (local_function_static (decl))
27543 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27545 /* If we are in terse mode, don't generate any DIEs to represent any
27546 variable declarations or definitions unless it is external. */
27547 if (debug_info_level < DINFO_LEVEL_TERSE
27548 || (debug_info_level == DINFO_LEVEL_TERSE
27549 && !TREE_PUBLIC (decl)))
27550 return;
27551 break;
27553 case CONST_DECL:
27554 if (debug_info_level <= DINFO_LEVEL_TERSE)
27555 return;
27556 if (!is_fortran () && !is_ada () && !is_dlang ())
27557 return;
27558 if (TREE_STATIC (decl) && decl_function_context (decl))
27559 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27560 break;
27562 case NAMESPACE_DECL:
27563 case IMPORTED_DECL:
27564 if (debug_info_level <= DINFO_LEVEL_TERSE)
27565 return;
27566 if (lookup_decl_die (decl) != NULL)
27567 return;
27568 break;
27570 case TYPE_DECL:
27571 /* Don't emit stubs for types unless they are needed by other DIEs. */
27572 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27573 return;
27575 /* Don't bother trying to generate any DIEs to represent any of the
27576 normal built-in types for the language we are compiling. */
27577 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27578 return;
27580 /* If we are in terse mode, don't generate any DIEs for types. */
27581 if (debug_info_level <= DINFO_LEVEL_TERSE)
27582 return;
27584 /* If we're a function-scope tag, initially use a parent of NULL;
27585 this will be fixed up in decls_for_scope. */
27586 if (decl_function_context (decl))
27587 context_die = NULL;
27589 break;
27591 case NAMELIST_DECL:
27592 break;
27594 default:
27595 return;
27598 gen_decl_die (decl, NULL, NULL, context_die);
27600 if (flag_checking)
27602 dw_die_ref die = lookup_decl_die (decl);
27603 if (die)
27604 check_die (die);
27608 /* Write the debugging output for DECL. */
27610 static void
27611 dwarf2out_function_decl (tree decl)
27613 dwarf2out_decl (decl);
27614 call_arg_locations = NULL;
27615 call_arg_loc_last = NULL;
27616 call_site_count = -1;
27617 tail_call_site_count = -1;
27618 decl_loc_table->empty ();
27619 cached_dw_loc_list_table->empty ();
27622 /* Output a marker (i.e. a label) for the beginning of the generated code for
27623 a lexical block. */
27625 static void
27626 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27627 unsigned int blocknum)
27629 switch_to_section (current_function_section ());
27630 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27633 /* Output a marker (i.e. a label) for the end of the generated code for a
27634 lexical block. */
27636 static void
27637 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27639 switch_to_section (current_function_section ());
27640 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27643 /* Returns nonzero if it is appropriate not to emit any debugging
27644 information for BLOCK, because it doesn't contain any instructions.
27646 Don't allow this for blocks with nested functions or local classes
27647 as we would end up with orphans, and in the presence of scheduling
27648 we may end up calling them anyway. */
27650 static bool
27651 dwarf2out_ignore_block (const_tree block)
27653 tree decl;
27654 unsigned int i;
27656 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27657 if (TREE_CODE (decl) == FUNCTION_DECL
27658 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27659 return 0;
27660 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27662 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27663 if (TREE_CODE (decl) == FUNCTION_DECL
27664 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27665 return 0;
27668 return 1;
27671 /* Hash table routines for file_hash. */
27673 bool
27674 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27676 return filename_cmp (p1->key, p2) == 0;
27679 hashval_t
27680 dwarf_file_hasher::hash (dwarf_file_data *p)
27682 return htab_hash_string (p->key);
27685 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27686 dwarf2out.cc) and return its "index". The index of each (known) filename is
27687 just a unique number which is associated with only that one filename. We
27688 need such numbers for the sake of generating labels (in the .debug_sfnames
27689 section) and references to those files numbers (in the .debug_srcinfo
27690 and .debug_macinfo sections). If the filename given as an argument is not
27691 found in our current list, add it to the list and assign it the next
27692 available unique index number. */
27694 static struct dwarf_file_data *
27695 lookup_filename (const char *file_name)
27697 struct dwarf_file_data * created;
27699 if (!file_name)
27700 return NULL;
27702 if (!file_name[0])
27703 file_name = "<stdin>";
27705 dwarf_file_data **slot
27706 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27707 INSERT);
27708 if (*slot)
27709 return *slot;
27711 created = ggc_alloc<dwarf_file_data> ();
27712 created->key = file_name;
27713 created->filename = remap_debug_filename (file_name);
27714 created->emitted_number = 0;
27715 *slot = created;
27716 return created;
27719 /* If the assembler will construct the file table, then translate the compiler
27720 internal file table number into the assembler file table number, and emit
27721 a .file directive if we haven't already emitted one yet. The file table
27722 numbers are different because we prune debug info for unused variables and
27723 types, which may include filenames. */
27725 static int
27726 maybe_emit_file (struct dwarf_file_data * fd)
27728 if (! fd->emitted_number)
27730 if (last_emitted_file)
27731 fd->emitted_number = last_emitted_file->emitted_number + 1;
27732 else
27733 fd->emitted_number = 1;
27734 last_emitted_file = fd;
27736 if (output_asm_line_debug_info ())
27738 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27739 output_quoted_string (asm_out_file, fd->filename);
27740 fputc ('\n', asm_out_file);
27744 return fd->emitted_number;
27747 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27748 That generation should happen after function debug info has been
27749 generated. The value of the attribute is the constant value of ARG. */
27751 static void
27752 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27754 die_arg_entry entry;
27756 if (!die || !arg)
27757 return;
27759 gcc_assert (early_dwarf);
27761 if (!tmpl_value_parm_die_table)
27762 vec_alloc (tmpl_value_parm_die_table, 32);
27764 entry.die = die;
27765 entry.arg = arg;
27766 vec_safe_push (tmpl_value_parm_die_table, entry);
27769 /* Return TRUE if T is an instance of generic type, FALSE
27770 otherwise. */
27772 static bool
27773 generic_type_p (tree t)
27775 if (t == NULL_TREE || !TYPE_P (t))
27776 return false;
27777 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27780 /* Schedule the generation of the generic parameter dies for the
27781 instance of generic type T. The proper generation itself is later
27782 done by gen_scheduled_generic_parms_dies. */
27784 static void
27785 schedule_generic_params_dies_gen (tree t)
27787 if (!generic_type_p (t))
27788 return;
27790 gcc_assert (early_dwarf);
27792 if (!generic_type_instances)
27793 vec_alloc (generic_type_instances, 256);
27795 vec_safe_push (generic_type_instances, t);
27798 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27799 by append_entry_to_tmpl_value_parm_die_table. This function must
27800 be called after function DIEs have been generated. */
27802 static void
27803 gen_remaining_tmpl_value_param_die_attribute (void)
27805 if (tmpl_value_parm_die_table)
27807 unsigned i, j;
27808 die_arg_entry *e;
27810 /* We do this in two phases - first get the cases we can
27811 handle during early-finish, preserving those we cannot
27812 (containing symbolic constants where we don't yet know
27813 whether we are going to output the referenced symbols).
27814 For those we try again at late-finish. */
27815 j = 0;
27816 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27818 if (!e->die->removed
27819 && !tree_add_const_value_attribute (e->die, e->arg))
27821 dw_loc_descr_ref loc = NULL;
27822 if (! early_dwarf
27823 && (dwarf_version >= 5 || !dwarf_strict))
27824 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27825 if (loc)
27826 add_AT_loc (e->die, DW_AT_location, loc);
27827 else
27828 (*tmpl_value_parm_die_table)[j++] = *e;
27831 tmpl_value_parm_die_table->truncate (j);
27835 /* Generate generic parameters DIEs for instances of generic types
27836 that have been previously scheduled by
27837 schedule_generic_params_dies_gen. This function must be called
27838 after all the types of the CU have been laid out. */
27840 static void
27841 gen_scheduled_generic_parms_dies (void)
27843 unsigned i;
27844 tree t;
27846 if (!generic_type_instances)
27847 return;
27849 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27850 if (COMPLETE_TYPE_P (t))
27851 gen_generic_params_dies (t);
27853 generic_type_instances = NULL;
27857 /* Replace DW_AT_name for the decl with name. */
27859 static void
27860 dwarf2out_set_name (tree decl, tree name)
27862 dw_die_ref die;
27863 dw_attr_node *attr;
27864 const char *dname;
27866 die = TYPE_SYMTAB_DIE (decl);
27867 if (!die)
27868 return;
27870 dname = dwarf2_name (name, 0);
27871 if (!dname)
27872 return;
27874 attr = get_AT (die, DW_AT_name);
27875 if (attr)
27877 struct indirect_string_node *node;
27879 node = find_AT_string (dname);
27880 /* replace the string. */
27881 attr->dw_attr_val.v.val_str = node;
27884 else
27885 add_name_attribute (die, dname);
27888 /* True if before or during processing of the first function being emitted. */
27889 static bool in_first_function_p = true;
27890 /* True if loc_note during dwarf2out_var_location call might still be
27891 before first real instruction at address equal to .Ltext0. */
27892 static bool maybe_at_text_label_p = true;
27893 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27894 static unsigned int first_loclabel_num_not_at_text_label;
27896 /* Look ahead for a real insn. */
27898 static rtx_insn *
27899 dwarf2out_next_real_insn (rtx_insn *loc_note)
27901 rtx_insn *next_real = NEXT_INSN (loc_note);
27903 while (next_real)
27904 if (INSN_P (next_real))
27905 break;
27906 else
27907 next_real = NEXT_INSN (next_real);
27909 return next_real;
27912 /* Called by the final INSN scan whenever we see a var location. We
27913 use it to drop labels in the right places, and throw the location in
27914 our lookup table. */
27916 static void
27917 dwarf2out_var_location (rtx_insn *loc_note)
27919 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27920 struct var_loc_node *newloc;
27921 rtx_insn *next_real;
27922 rtx_insn *call_insn = NULL;
27923 static const char *last_label;
27924 static const char *last_postcall_label;
27925 static bool last_in_cold_section_p;
27926 static rtx_insn *expected_next_loc_note;
27927 tree decl;
27928 bool var_loc_p;
27929 var_loc_view view = 0;
27931 if (!NOTE_P (loc_note))
27933 if (CALL_P (loc_note))
27935 maybe_reset_location_view (loc_note, cur_line_info_table);
27936 call_site_count++;
27937 if (SIBLING_CALL_P (loc_note))
27938 tail_call_site_count++;
27939 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27941 call_insn = loc_note;
27942 loc_note = NULL;
27943 var_loc_p = false;
27945 next_real = dwarf2out_next_real_insn (call_insn);
27946 cached_next_real_insn = NULL;
27947 goto create_label;
27949 if (optimize == 0 && !flag_var_tracking)
27951 /* When the var-tracking pass is not running, there is no note
27952 for indirect calls whose target is compile-time known. In this
27953 case, process such calls specifically so that we generate call
27954 sites for them anyway. */
27955 rtx x = PATTERN (loc_note);
27956 if (GET_CODE (x) == PARALLEL)
27957 x = XVECEXP (x, 0, 0);
27958 if (GET_CODE (x) == SET)
27959 x = SET_SRC (x);
27960 if (GET_CODE (x) == CALL)
27961 x = XEXP (x, 0);
27962 if (!MEM_P (x)
27963 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27964 || !SYMBOL_REF_DECL (XEXP (x, 0))
27965 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27966 != FUNCTION_DECL))
27968 call_insn = loc_note;
27969 loc_note = NULL;
27970 var_loc_p = false;
27972 next_real = dwarf2out_next_real_insn (call_insn);
27973 cached_next_real_insn = NULL;
27974 goto create_label;
27978 else if (!debug_variable_location_views)
27979 gcc_unreachable ();
27980 else
27981 maybe_reset_location_view (loc_note, cur_line_info_table);
27983 return;
27986 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27987 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27988 return;
27990 /* Optimize processing a large consecutive sequence of location
27991 notes so we don't spend too much time in next_real_insn. If the
27992 next insn is another location note, remember the next_real_insn
27993 calculation for next time. */
27994 next_real = cached_next_real_insn;
27995 if (next_real)
27997 if (expected_next_loc_note != loc_note)
27998 next_real = NULL;
28001 if (! next_real)
28002 next_real = dwarf2out_next_real_insn (loc_note);
28004 if (next_real)
28006 rtx_insn *next_note = NEXT_INSN (loc_note);
28007 while (next_note != next_real)
28009 if (! next_note->deleted ()
28010 && NOTE_P (next_note)
28011 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
28012 break;
28013 next_note = NEXT_INSN (next_note);
28016 if (next_note == next_real)
28017 cached_next_real_insn = NULL;
28018 else
28020 expected_next_loc_note = next_note;
28021 cached_next_real_insn = next_real;
28024 else
28025 cached_next_real_insn = NULL;
28027 /* If there are no instructions which would be affected by this note,
28028 don't do anything. */
28029 if (var_loc_p
28030 && next_real == NULL_RTX
28031 && !NOTE_DURING_CALL_P (loc_note))
28032 return;
28034 create_label:
28036 if (next_real == NULL_RTX)
28037 next_real = get_last_insn ();
28039 /* If there were any real insns between note we processed last time
28040 and this note (or if it is the first note), clear
28041 last_{,postcall_}label so that they are not reused this time. */
28042 if (last_var_location_insn == NULL_RTX
28043 || last_var_location_insn != next_real
28044 || last_in_cold_section_p != in_cold_section_p)
28046 last_label = NULL;
28047 last_postcall_label = NULL;
28050 if (var_loc_p)
28052 const char *label
28053 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28054 view = cur_line_info_table->view;
28055 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28056 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28057 if (newloc == NULL)
28058 return;
28060 else
28062 decl = NULL_TREE;
28063 newloc = NULL;
28066 /* If there were no real insns between note we processed last time
28067 and this note, use the label we emitted last time. Otherwise
28068 create a new label and emit it. */
28069 if (last_label == NULL)
28071 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28072 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28073 loclabel_num++;
28074 last_label = ggc_strdup (loclabel);
28075 /* See if loclabel might be equal to .Ltext0. If yes,
28076 bump first_loclabel_num_not_at_text_label. */
28077 if (!have_multiple_function_sections
28078 && in_first_function_p
28079 && maybe_at_text_label_p)
28081 static rtx_insn *last_start;
28082 rtx_insn *insn;
28083 for (insn = loc_note; insn; insn = previous_insn (insn))
28084 if (insn == last_start)
28085 break;
28086 else if (!NONDEBUG_INSN_P (insn))
28087 continue;
28088 else
28090 rtx body = PATTERN (insn);
28091 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28092 continue;
28093 /* Inline asm could occupy zero bytes. */
28094 else if (GET_CODE (body) == ASM_INPUT
28095 || asm_noperands (body) >= 0)
28096 continue;
28097 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28098 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28099 continue;
28100 #endif
28101 else
28103 /* Assume insn has non-zero length. */
28104 maybe_at_text_label_p = false;
28105 break;
28108 if (maybe_at_text_label_p)
28110 last_start = loc_note;
28111 first_loclabel_num_not_at_text_label = loclabel_num;
28116 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28117 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28119 if (!var_loc_p)
28121 struct call_arg_loc_node *ca_loc
28122 = ggc_cleared_alloc<call_arg_loc_node> ();
28123 rtx_insn *prev = call_insn;
28125 ca_loc->call_arg_loc_note
28126 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
28127 ca_loc->next = NULL;
28128 ca_loc->label = last_label;
28129 gcc_assert (prev
28130 && (CALL_P (prev)
28131 || (NONJUMP_INSN_P (prev)
28132 && GET_CODE (PATTERN (prev)) == SEQUENCE
28133 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28134 if (!CALL_P (prev))
28135 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28136 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28138 /* Look for a SYMBOL_REF in the "prev" instruction. */
28139 rtx x = get_call_rtx_from (prev);
28140 if (x)
28142 /* Try to get the call symbol, if any. */
28143 if (MEM_P (XEXP (x, 0)))
28144 x = XEXP (x, 0);
28145 /* First, look for a memory access to a symbol_ref. */
28146 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28147 && SYMBOL_REF_DECL (XEXP (x, 0))
28148 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28149 ca_loc->symbol_ref = XEXP (x, 0);
28150 /* Otherwise, look at a compile-time known user-level function
28151 declaration. */
28152 else if (MEM_P (x)
28153 && MEM_EXPR (x)
28154 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28155 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28158 ca_loc->block = insn_scope (prev);
28159 if (call_arg_locations)
28160 call_arg_loc_last->next = ca_loc;
28161 else
28162 call_arg_locations = ca_loc;
28163 call_arg_loc_last = ca_loc;
28165 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28167 newloc->label = last_label;
28168 newloc->view = view;
28170 else
28172 if (!last_postcall_label)
28174 sprintf (loclabel, "%s-1", last_label);
28175 last_postcall_label = ggc_strdup (loclabel);
28177 newloc->label = last_postcall_label;
28178 /* ??? This view is at last_label, not last_label-1, but we
28179 could only assume view at last_label-1 is zero if we could
28180 assume calls always have length greater than one. This is
28181 probably true in general, though there might be a rare
28182 exception to this rule, e.g. if a call insn is optimized out
28183 by target magic. Then, even the -1 in the label will be
28184 wrong, which might invalidate the range. Anyway, using view,
28185 though technically possibly incorrect, will work as far as
28186 ranges go: since L-1 is in the middle of the call insn,
28187 (L-1).0 and (L-1).V shouldn't make any difference, and having
28188 the loclist entry refer to the .loc entry might be useful, so
28189 leave it like this. */
28190 newloc->view = view;
28193 if (var_loc_p && flag_debug_asm)
28195 const char *name, *sep, *patstr;
28196 if (decl && DECL_NAME (decl))
28197 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28198 else
28199 name = "";
28200 if (NOTE_VAR_LOCATION_LOC (loc_note))
28202 sep = " => ";
28203 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28205 else
28207 sep = " ";
28208 patstr = "RESET";
28210 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28211 name, sep, patstr);
28214 last_var_location_insn = next_real;
28215 last_in_cold_section_p = in_cold_section_p;
28218 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28219 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28220 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28221 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28222 BLOCK_FRAGMENT_ORIGIN links. */
28223 static bool
28224 block_within_block_p (tree block, tree outer, bool bothways)
28226 if (block == outer)
28227 return true;
28229 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28230 for (tree context = BLOCK_SUPERCONTEXT (block);
28231 context != outer;
28232 context = BLOCK_SUPERCONTEXT (context))
28233 if (!context || TREE_CODE (context) != BLOCK)
28234 return false;
28236 if (!bothways)
28237 return true;
28239 /* Now check that each block is actually referenced by its
28240 parent. */
28241 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28242 context = BLOCK_SUPERCONTEXT (context))
28244 if (BLOCK_FRAGMENT_ORIGIN (context))
28246 gcc_assert (!BLOCK_SUBBLOCKS (context));
28247 context = BLOCK_FRAGMENT_ORIGIN (context);
28249 for (tree sub = BLOCK_SUBBLOCKS (context);
28250 sub != block;
28251 sub = BLOCK_CHAIN (sub))
28252 if (!sub)
28253 return false;
28254 if (context == outer)
28255 return true;
28256 else
28257 block = context;
28261 /* Called during final while assembling the marker of the entry point
28262 for an inlined function. */
28264 static void
28265 dwarf2out_inline_entry (tree block)
28267 gcc_assert (debug_inline_points);
28269 /* If we can't represent it, don't bother. */
28270 if (!(dwarf_version >= 3 || !dwarf_strict))
28271 return;
28273 gcc_assert (DECL_P (block_ultimate_origin (block)));
28275 /* Sanity check the block tree. This would catch a case in which
28276 BLOCK got removed from the tree reachable from the outermost
28277 lexical block, but got retained in markers. It would still link
28278 back to its parents, but some ancestor would be missing a link
28279 down the path to the sub BLOCK. If the block got removed, its
28280 BLOCK_NUMBER will not be a usable value. */
28281 if (flag_checking)
28282 gcc_assert (block_within_block_p (block,
28283 DECL_INITIAL (current_function_decl),
28284 true));
28286 gcc_assert (inlined_function_outer_scope_p (block));
28287 gcc_assert (!lookup_block_die (block));
28289 if (BLOCK_FRAGMENT_ORIGIN (block))
28290 block = BLOCK_FRAGMENT_ORIGIN (block);
28291 /* Can the entry point ever not be at the beginning of an
28292 unfragmented lexical block? */
28293 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28294 || (cur_line_info_table
28295 && !ZERO_VIEW_P (cur_line_info_table->view))))
28296 return;
28298 if (!inline_entry_data_table)
28299 inline_entry_data_table
28300 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28303 inline_entry_data **iedp
28304 = inline_entry_data_table->find_slot_with_hash (block,
28305 htab_hash_pointer (block),
28306 INSERT);
28307 if (*iedp)
28308 /* ??? Ideally, we'd record all entry points for the same inlined
28309 function (some may have been duplicated by e.g. unrolling), but
28310 we have no way to represent that ATM. */
28311 return;
28313 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28314 ied->block = block;
28315 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28316 ied->label_num = BLOCK_NUMBER (block);
28317 if (cur_line_info_table)
28318 ied->view = cur_line_info_table->view;
28320 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28321 BLOCK_NUMBER (block));
28324 /* Called from finalize_size_functions for size functions so that their body
28325 can be encoded in the debug info to describe the layout of variable-length
28326 structures. */
28328 static void
28329 dwarf2out_size_function (tree decl)
28331 set_early_dwarf s;
28332 function_to_dwarf_procedure (decl);
28335 /* Note in one location list that text section has changed. */
28338 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28340 var_loc_list *list = *slot;
28341 if (list->first)
28342 list->last_before_switch
28343 = list->last->next ? list->last->next : list->last;
28344 return 1;
28347 /* Note in all location lists that text section has changed. */
28349 static void
28350 var_location_switch_text_section (void)
28352 if (decl_loc_table == NULL)
28353 return;
28355 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28358 /* Create a new line number table. */
28360 static dw_line_info_table *
28361 new_line_info_table (void)
28363 dw_line_info_table *table;
28365 table = ggc_cleared_alloc<dw_line_info_table> ();
28366 table->file_num = 1;
28367 table->line_num = 1;
28368 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28369 FORCE_RESET_NEXT_VIEW (table->view);
28370 table->symviews_since_reset = 0;
28372 return table;
28375 /* Lookup the "current" table into which we emit line info, so
28376 that we don't have to do it for every source line. */
28378 static void
28379 set_cur_line_info_table (section *sec)
28381 dw_line_info_table *table;
28383 if (sec == text_section)
28384 table = text_section_line_info;
28385 else if (sec == cold_text_section)
28387 table = cold_text_section_line_info;
28388 if (!table)
28390 cold_text_section_line_info = table = new_line_info_table ();
28391 table->end_label = cold_end_label;
28394 else
28396 const char *end_label;
28398 if (crtl->has_bb_partition)
28400 if (in_cold_section_p)
28401 end_label = crtl->subsections.cold_section_end_label;
28402 else
28403 end_label = crtl->subsections.hot_section_end_label;
28405 else
28407 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28408 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28409 current_function_funcdef_no);
28410 end_label = ggc_strdup (label);
28413 table = new_line_info_table ();
28414 table->end_label = end_label;
28416 vec_safe_push (separate_line_info, table);
28419 if (output_asm_line_debug_info ())
28420 table->is_stmt = (cur_line_info_table
28421 ? cur_line_info_table->is_stmt
28422 : DWARF_LINE_DEFAULT_IS_STMT_START);
28423 cur_line_info_table = table;
28427 /* We need to reset the locations at the beginning of each
28428 function. We can't do this in the end_function hook, because the
28429 declarations that use the locations won't have been output when
28430 that hook is called. Also compute have_multiple_function_sections here. */
28432 static void
28433 dwarf2out_begin_function (tree fun)
28435 section *sec = function_section (fun);
28437 if (sec != text_section)
28438 have_multiple_function_sections = true;
28440 if (crtl->has_bb_partition && !cold_text_section)
28442 gcc_assert (current_function_decl == fun);
28443 cold_text_section = unlikely_text_section ();
28444 switch_to_section (cold_text_section);
28445 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28446 switch_to_section (sec);
28449 call_site_count = 0;
28450 tail_call_site_count = 0;
28452 set_cur_line_info_table (sec);
28453 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28456 /* Helper function of dwarf2out_end_function, called only after emitting
28457 the very first function into assembly. Check if some .debug_loc range
28458 might end with a .LVL* label that could be equal to .Ltext0.
28459 In that case we must force using absolute addresses in .debug_loc ranges,
28460 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28461 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28462 list terminator.
28463 Set have_multiple_function_sections to true in that case and
28464 terminate htab traversal. */
28467 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28469 var_loc_list *entry = *slot;
28470 struct var_loc_node *node;
28472 node = entry->first;
28473 if (node && node->next && node->next->label)
28475 unsigned int i;
28476 const char *label = node->next->label;
28477 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28479 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28481 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28482 if (strcmp (label, loclabel) == 0)
28484 have_multiple_function_sections = true;
28485 return 0;
28489 return 1;
28492 /* Hook called after emitting a function into assembly.
28493 This does something only for the very first function emitted. */
28495 static void
28496 dwarf2out_end_function (unsigned int)
28498 if (in_first_function_p
28499 && !have_multiple_function_sections
28500 && first_loclabel_num_not_at_text_label
28501 && decl_loc_table)
28502 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28503 in_first_function_p = false;
28504 maybe_at_text_label_p = false;
28507 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28508 front-ends register a translation unit even before dwarf2out_init is
28509 called. */
28510 static tree main_translation_unit = NULL_TREE;
28512 /* Hook called by front-ends after they built their main translation unit.
28513 Associate comp_unit_die to UNIT. */
28515 static void
28516 dwarf2out_register_main_translation_unit (tree unit)
28518 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28519 && main_translation_unit == NULL_TREE);
28520 main_translation_unit = unit;
28521 /* If dwarf2out_init has not been called yet, it will perform the association
28522 itself looking at main_translation_unit. */
28523 if (decl_die_table != NULL)
28524 equate_decl_number_to_die (unit, comp_unit_die ());
28527 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28529 static void
28530 push_dw_line_info_entry (dw_line_info_table *table,
28531 enum dw_line_info_opcode opcode, unsigned int val)
28533 dw_line_info_entry e;
28534 e.opcode = opcode;
28535 e.val = val;
28536 vec_safe_push (table->entries, e);
28539 /* Output a label to mark the beginning of a source code line entry
28540 and record information relating to this source line, in
28541 'line_info_table' for later output of the .debug_line section. */
28542 /* ??? The discriminator parameter ought to be unsigned. */
28544 static void
28545 dwarf2out_source_line (unsigned int line, unsigned int column,
28546 const char *filename,
28547 int discriminator, bool is_stmt)
28549 unsigned int file_num;
28550 dw_line_info_table *table;
28551 static var_loc_view lvugid;
28553 /* 'line_info_table' information gathering is not needed when the debug
28554 info level is set to the lowest value. Also, the current DWARF-based
28555 debug formats do not use this info. */
28556 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28557 return;
28559 table = cur_line_info_table;
28561 if (line == 0)
28563 if (debug_variable_location_views
28564 && output_asm_line_debug_info ()
28565 && table && !RESETTING_VIEW_P (table->view))
28567 /* If we're using the assembler to compute view numbers, we
28568 can't issue a .loc directive for line zero, so we can't
28569 get a view number at this point. We might attempt to
28570 compute it from the previous view, or equate it to a
28571 subsequent view (though it might not be there!), but
28572 since we're omitting the line number entry, we might as
28573 well omit the view number as well. That means pretending
28574 it's a view number zero, which might very well turn out
28575 to be correct. ??? Extend the assembler so that the
28576 compiler could emit e.g. ".locview .LVU#", to output a
28577 view without changing line number information. We'd then
28578 have to count it in symviews_since_reset; when it's omitted,
28579 it doesn't count. */
28580 if (!zero_view_p)
28581 zero_view_p = BITMAP_GGC_ALLOC ();
28582 bitmap_set_bit (zero_view_p, table->view);
28583 if (flag_debug_asm)
28585 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28586 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28587 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28588 ASM_COMMENT_START);
28589 assemble_name (asm_out_file, label);
28590 putc ('\n', asm_out_file);
28592 table->view = ++lvugid;
28594 return;
28597 /* The discriminator column was added in dwarf4. Simplify the below
28598 by simply removing it if we're not supposed to output it. */
28599 if (dwarf_version < 4 && dwarf_strict)
28600 discriminator = 0;
28602 if (!debug_column_info)
28603 column = 0;
28605 file_num = maybe_emit_file (lookup_filename (filename));
28607 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28608 the debugger has used the second (possibly duplicate) line number
28609 at the beginning of the function to mark the end of the prologue.
28610 We could eliminate any other duplicates within the function. For
28611 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28612 that second line number entry. */
28613 /* Recall that this end-of-prologue indication is *not* the same thing
28614 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28615 to which the hook corresponds, follows the last insn that was
28616 emitted by gen_prologue. What we need is to precede the first insn
28617 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28618 insn that corresponds to something the user wrote. These may be
28619 very different locations once scheduling is enabled. */
28621 if (0 && file_num == table->file_num
28622 && line == table->line_num
28623 && column == table->column_num
28624 && discriminator == table->discrim_num
28625 && is_stmt == table->is_stmt)
28626 return;
28628 switch_to_section (current_function_section ());
28630 /* If requested, emit something human-readable. */
28631 if (flag_debug_asm)
28633 if (debug_column_info)
28634 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28635 filename, line, column);
28636 else
28637 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28638 filename, line);
28641 if (output_asm_line_debug_info ())
28643 /* Emit the .loc directive understood by GNU as. */
28644 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28645 file_num, line, is_stmt, discriminator */
28646 fputs ("\t.loc ", asm_out_file);
28647 fprint_ul (asm_out_file, file_num);
28648 putc (' ', asm_out_file);
28649 fprint_ul (asm_out_file, line);
28650 putc (' ', asm_out_file);
28651 fprint_ul (asm_out_file, column);
28653 if (is_stmt != table->is_stmt)
28655 #if HAVE_GAS_LOC_STMT
28656 fputs (" is_stmt ", asm_out_file);
28657 putc (is_stmt ? '1' : '0', asm_out_file);
28658 #endif
28660 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28662 gcc_assert (discriminator > 0);
28663 fputs (" discriminator ", asm_out_file);
28664 fprint_ul (asm_out_file, (unsigned long) discriminator);
28666 if (debug_variable_location_views)
28668 if (!RESETTING_VIEW_P (table->view))
28670 table->symviews_since_reset++;
28671 if (table->symviews_since_reset > symview_upper_bound)
28672 symview_upper_bound = table->symviews_since_reset;
28673 /* When we're using the assembler to compute view
28674 numbers, we output symbolic labels after "view" in
28675 .loc directives, and the assembler will set them for
28676 us, so that we can refer to the view numbers in
28677 location lists. The only exceptions are when we know
28678 a view will be zero: "-0" is a forced reset, used
28679 e.g. in the beginning of functions, whereas "0" tells
28680 the assembler to check that there was a PC change
28681 since the previous view, in a way that implicitly
28682 resets the next view. */
28683 fputs (" view ", asm_out_file);
28684 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28685 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28686 assemble_name (asm_out_file, label);
28687 table->view = ++lvugid;
28689 else
28691 table->symviews_since_reset = 0;
28692 if (FORCE_RESETTING_VIEW_P (table->view))
28693 fputs (" view -0", asm_out_file);
28694 else
28695 fputs (" view 0", asm_out_file);
28696 /* Mark the present view as a zero view. Earlier debug
28697 binds may have already added its id to loclists to be
28698 emitted later, so we can't reuse the id for something
28699 else. However, it's good to know whether a view is
28700 known to be zero, because then we may be able to
28701 optimize out locviews that are all zeros, so take
28702 note of it in zero_view_p. */
28703 if (!zero_view_p)
28704 zero_view_p = BITMAP_GGC_ALLOC ();
28705 bitmap_set_bit (zero_view_p, lvugid);
28706 table->view = ++lvugid;
28709 putc ('\n', asm_out_file);
28711 else
28713 unsigned int label_num = ++line_info_label_num;
28715 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28717 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28718 push_dw_line_info_entry (table, LI_adv_address, label_num);
28719 else
28720 push_dw_line_info_entry (table, LI_set_address, label_num);
28721 if (debug_variable_location_views)
28723 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28724 if (resetting)
28725 table->view = 0;
28727 if (flag_debug_asm)
28728 fprintf (asm_out_file, "\t%s view %s%d\n",
28729 ASM_COMMENT_START,
28730 resetting ? "-" : "",
28731 table->view);
28733 table->view++;
28735 if (file_num != table->file_num)
28736 push_dw_line_info_entry (table, LI_set_file, file_num);
28737 if (discriminator != table->discrim_num)
28738 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28739 if (is_stmt != table->is_stmt)
28740 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28741 push_dw_line_info_entry (table, LI_set_line, line);
28742 if (debug_column_info)
28743 push_dw_line_info_entry (table, LI_set_column, column);
28746 table->file_num = file_num;
28747 table->line_num = line;
28748 table->column_num = column;
28749 table->discrim_num = discriminator;
28750 table->is_stmt = is_stmt;
28751 table->in_use = true;
28754 /* Record a source file location for a DECL_IGNORED_P function. */
28756 static void
28757 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28758 const char *filename)
28760 dw_fde_ref fde = cfun->fde;
28762 fde->ignored_debug = false;
28763 set_cur_line_info_table (function_section (fde->decl));
28765 dwarf2out_source_line (line, column, filename, 0, true);
28768 /* Record the beginning of a new source file. */
28770 static void
28771 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28773 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28775 macinfo_entry e;
28776 e.code = DW_MACINFO_start_file;
28777 e.lineno = lineno;
28778 e.info = ggc_strdup (filename);
28779 vec_safe_push (macinfo_table, e);
28783 /* Record the end of a source file. */
28785 static void
28786 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28788 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28790 macinfo_entry e;
28791 e.code = DW_MACINFO_end_file;
28792 e.lineno = lineno;
28793 e.info = NULL;
28794 vec_safe_push (macinfo_table, e);
28798 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
28799 the tail part of the directive line, i.e. the part which is past the
28800 initial whitespace, #, whitespace, directive-name, whitespace part. */
28802 static void
28803 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28804 const char *buffer ATTRIBUTE_UNUSED)
28806 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28808 macinfo_entry e;
28809 /* Insert a dummy first entry to be able to optimize the whole
28810 predefined macro block using DW_MACRO_import. */
28811 if (macinfo_table->is_empty () && lineno <= 1)
28813 e.code = 0;
28814 e.lineno = 0;
28815 e.info = NULL;
28816 vec_safe_push (macinfo_table, e);
28818 e.code = DW_MACINFO_define;
28819 e.lineno = lineno;
28820 e.info = ggc_strdup (buffer);
28821 vec_safe_push (macinfo_table, e);
28825 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
28826 the tail part of the directive line, i.e. the part which is past the
28827 initial whitespace, #, whitespace, directive-name, whitespace part. */
28829 static void
28830 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28831 const char *buffer ATTRIBUTE_UNUSED)
28833 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28835 macinfo_entry e;
28836 /* Insert a dummy first entry to be able to optimize the whole
28837 predefined macro block using DW_MACRO_import. */
28838 if (macinfo_table->is_empty () && lineno <= 1)
28840 e.code = 0;
28841 e.lineno = 0;
28842 e.info = NULL;
28843 vec_safe_push (macinfo_table, e);
28845 e.code = DW_MACINFO_undef;
28846 e.lineno = lineno;
28847 e.info = ggc_strdup (buffer);
28848 vec_safe_push (macinfo_table, e);
28852 /* Helpers to manipulate hash table of CUs. */
28854 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28856 static inline hashval_t hash (const macinfo_entry *);
28857 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28860 inline hashval_t
28861 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28863 return htab_hash_string (entry->info);
28866 inline bool
28867 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28868 const macinfo_entry *entry2)
28870 return !strcmp (entry1->info, entry2->info);
28873 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28875 /* Output a single .debug_macinfo entry. */
28877 static void
28878 output_macinfo_op (macinfo_entry *ref)
28880 int file_num;
28881 size_t len;
28882 struct indirect_string_node *node;
28883 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28884 struct dwarf_file_data *fd;
28886 switch (ref->code)
28888 case DW_MACINFO_start_file:
28889 fd = lookup_filename (ref->info);
28890 file_num = maybe_emit_file (fd);
28891 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28892 dw2_asm_output_data_uleb128 (ref->lineno,
28893 "Included from line number %lu",
28894 (unsigned long) ref->lineno);
28895 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28896 break;
28897 case DW_MACINFO_end_file:
28898 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28899 break;
28900 case DW_MACINFO_define:
28901 case DW_MACINFO_undef:
28902 len = strlen (ref->info) + 1;
28903 if ((!dwarf_strict || dwarf_version >= 5)
28904 && len > (size_t) dwarf_offset_size
28905 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28906 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28908 if (dwarf_split_debug_info && dwarf_version >= 5)
28909 ref->code = ref->code == DW_MACINFO_define
28910 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28911 else
28912 ref->code = ref->code == DW_MACINFO_define
28913 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28914 output_macinfo_op (ref);
28915 return;
28917 dw2_asm_output_data (1, ref->code,
28918 ref->code == DW_MACINFO_define
28919 ? "Define macro" : "Undefine macro");
28920 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28921 (unsigned long) ref->lineno);
28922 dw2_asm_output_nstring (ref->info, -1, "The macro");
28923 break;
28924 case DW_MACRO_define_strp:
28925 dw2_asm_output_data (1, ref->code, "Define macro strp");
28926 goto do_DW_MACRO_define_strpx;
28927 case DW_MACRO_undef_strp:
28928 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28929 goto do_DW_MACRO_define_strpx;
28930 case DW_MACRO_define_strx:
28931 dw2_asm_output_data (1, ref->code, "Define macro strx");
28932 goto do_DW_MACRO_define_strpx;
28933 case DW_MACRO_undef_strx:
28934 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28935 /* FALLTHRU */
28936 do_DW_MACRO_define_strpx:
28937 /* NB: dwarf2out_finish performs:
28938 1. save_macinfo_strings
28939 2. hash table traverse of index_string
28940 3. output_macinfo -> output_macinfo_op
28941 4. output_indirect_strings
28942 -> hash table traverse of output_index_string
28944 When output_macinfo_op is called, all index strings have been
28945 added to hash table by save_macinfo_strings and we can't pass
28946 INSERT to find_slot_with_hash which may expand hash table, even
28947 if no insertion is needed, and change hash table traverse order
28948 between index_string and output_index_string. */
28949 node = find_AT_string (ref->info, NO_INSERT);
28950 gcc_assert (node
28951 && (node->form == DW_FORM_strp
28952 || node->form == dwarf_FORM (DW_FORM_strx)));
28953 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28954 (unsigned long) ref->lineno);
28955 if (node->form == DW_FORM_strp)
28956 dw2_asm_output_offset (dwarf_offset_size, node->label,
28957 debug_str_section, "The macro: \"%s\"",
28958 ref->info);
28959 else
28960 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28961 ref->info);
28962 break;
28963 case DW_MACRO_import:
28964 dw2_asm_output_data (1, ref->code, "Import");
28965 ASM_GENERATE_INTERNAL_LABEL (label,
28966 DEBUG_MACRO_SECTION_LABEL,
28967 ref->lineno + macinfo_label_base);
28968 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28969 break;
28970 default:
28971 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28972 ASM_COMMENT_START, (unsigned long) ref->code);
28973 break;
28977 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28978 other compilation unit .debug_macinfo sections. IDX is the first
28979 index of a define/undef, return the number of ops that should be
28980 emitted in a comdat .debug_macinfo section and emit
28981 a DW_MACRO_import entry referencing it.
28982 If the define/undef entry should be emitted normally, return 0. */
28984 static unsigned
28985 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28986 macinfo_hash_type **macinfo_htab)
28988 macinfo_entry *first, *second, *cur, *inc;
28989 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28990 unsigned char checksum[16];
28991 struct md5_ctx ctx;
28992 char *grp_name, *tail;
28993 const char *base;
28994 unsigned int i, count, encoded_filename_len, linebuf_len;
28995 macinfo_entry **slot;
28997 first = &(*macinfo_table)[idx];
28998 second = &(*macinfo_table)[idx + 1];
29000 /* Optimize only if there are at least two consecutive define/undef ops,
29001 and either all of them are before first DW_MACINFO_start_file
29002 with lineno {0,1} (i.e. predefined macro block), or all of them are
29003 in some included header file. */
29004 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
29005 return 0;
29006 if (vec_safe_is_empty (files))
29008 if (first->lineno > 1 || second->lineno > 1)
29009 return 0;
29011 else if (first->lineno == 0)
29012 return 0;
29014 /* Find the last define/undef entry that can be grouped together
29015 with first and at the same time compute md5 checksum of their
29016 codes, linenumbers and strings. */
29017 md5_init_ctx (&ctx);
29018 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29019 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29020 break;
29021 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29022 break;
29023 else
29025 unsigned char code = cur->code;
29026 md5_process_bytes (&code, 1, &ctx);
29027 checksum_uleb128 (cur->lineno, &ctx);
29028 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29030 md5_finish_ctx (&ctx, checksum);
29031 count = i - idx;
29033 /* From the containing include filename (if any) pick up just
29034 usable characters from its basename. */
29035 if (vec_safe_is_empty (files))
29036 base = "";
29037 else
29038 base = lbasename (files->last ().info);
29039 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29040 if (ISIDNUM (base[i]) || base[i] == '.')
29041 encoded_filename_len++;
29042 /* Count . at the end. */
29043 if (encoded_filename_len)
29044 encoded_filename_len++;
29046 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29047 linebuf_len = strlen (linebuf);
29049 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29050 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29051 + 16 * 2 + 1);
29052 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29053 tail = grp_name + 4;
29054 if (encoded_filename_len)
29056 for (i = 0; base[i]; i++)
29057 if (ISIDNUM (base[i]) || base[i] == '.')
29058 *tail++ = base[i];
29059 *tail++ = '.';
29061 memcpy (tail, linebuf, linebuf_len);
29062 tail += linebuf_len;
29063 *tail++ = '.';
29064 for (i = 0; i < 16; i++)
29065 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29067 /* Construct a macinfo_entry for DW_MACRO_import
29068 in the empty vector entry before the first define/undef. */
29069 inc = &(*macinfo_table)[idx - 1];
29070 inc->code = DW_MACRO_import;
29071 inc->lineno = 0;
29072 inc->info = ggc_strdup (grp_name);
29073 if (!*macinfo_htab)
29074 *macinfo_htab = new macinfo_hash_type (10);
29075 /* Avoid emitting duplicates. */
29076 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29077 if (*slot != NULL)
29079 inc->code = 0;
29080 inc->info = NULL;
29081 /* If such an entry has been used before, just emit
29082 a DW_MACRO_import op. */
29083 inc = *slot;
29084 output_macinfo_op (inc);
29085 /* And clear all macinfo_entry in the range to avoid emitting them
29086 in the second pass. */
29087 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29089 cur->code = 0;
29090 cur->info = NULL;
29093 else
29095 *slot = inc;
29096 inc->lineno = (*macinfo_htab)->elements ();
29097 output_macinfo_op (inc);
29099 return count;
29102 /* Save any strings needed by the macinfo table in the debug str
29103 table. All strings must be collected into the table by the time
29104 index_string is called. */
29106 static void
29107 save_macinfo_strings (void)
29109 unsigned len;
29110 unsigned i;
29111 macinfo_entry *ref;
29113 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29115 switch (ref->code)
29117 /* Match the logic in output_macinfo_op to decide on
29118 indirect strings. */
29119 case DW_MACINFO_define:
29120 case DW_MACINFO_undef:
29121 len = strlen (ref->info) + 1;
29122 if ((!dwarf_strict || dwarf_version >= 5)
29123 && len > (unsigned) dwarf_offset_size
29124 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29125 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29126 set_indirect_string (find_AT_string (ref->info));
29127 break;
29128 case DW_MACINFO_start_file:
29129 /* -gsplit-dwarf -g3 will also output filename as indirect
29130 string. */
29131 if (!dwarf_split_debug_info)
29132 break;
29133 /* Fall through. */
29134 case DW_MACRO_define_strp:
29135 case DW_MACRO_undef_strp:
29136 case DW_MACRO_define_strx:
29137 case DW_MACRO_undef_strx:
29138 set_indirect_string (find_AT_string (ref->info));
29139 break;
29140 default:
29141 break;
29146 /* Output macinfo section(s). */
29148 static void
29149 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29151 unsigned i;
29152 unsigned long length = vec_safe_length (macinfo_table);
29153 macinfo_entry *ref;
29154 vec<macinfo_entry, va_gc> *files = NULL;
29155 macinfo_hash_type *macinfo_htab = NULL;
29156 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29158 if (! length)
29159 return;
29161 /* output_macinfo* uses these interchangeably. */
29162 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29163 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29164 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29165 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29167 /* AIX Assembler inserts the length, so adjust the reference to match the
29168 offset expected by debuggers. */
29169 strcpy (dl_section_ref, debug_line_label);
29170 if (XCOFF_DEBUGGING_INFO)
29171 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29173 /* For .debug_macro emit the section header. */
29174 if (!dwarf_strict || dwarf_version >= 5)
29176 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29177 "DWARF macro version number");
29178 if (dwarf_offset_size == 8)
29179 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29180 else
29181 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29182 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29183 debug_line_section, NULL);
29186 /* In the first loop, it emits the primary .debug_macinfo section
29187 and after each emitted op the macinfo_entry is cleared.
29188 If a longer range of define/undef ops can be optimized using
29189 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29190 the vector before the first define/undef in the range and the
29191 whole range of define/undef ops is not emitted and kept. */
29192 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29194 switch (ref->code)
29196 case DW_MACINFO_start_file:
29197 vec_safe_push (files, *ref);
29198 break;
29199 case DW_MACINFO_end_file:
29200 if (!vec_safe_is_empty (files))
29201 files->pop ();
29202 break;
29203 case DW_MACINFO_define:
29204 case DW_MACINFO_undef:
29205 if ((!dwarf_strict || dwarf_version >= 5)
29206 && HAVE_COMDAT_GROUP
29207 && vec_safe_length (files) != 1
29208 && i > 0
29209 && i + 1 < length
29210 && (*macinfo_table)[i - 1].code == 0)
29212 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29213 if (count)
29215 i += count - 1;
29216 continue;
29219 break;
29220 case 0:
29221 /* A dummy entry may be inserted at the beginning to be able
29222 to optimize the whole block of predefined macros. */
29223 if (i == 0)
29224 continue;
29225 default:
29226 break;
29228 output_macinfo_op (ref);
29229 ref->info = NULL;
29230 ref->code = 0;
29233 if (!macinfo_htab)
29234 return;
29236 /* Save the number of transparent includes so we can adjust the
29237 label number for the fat LTO object DWARF. */
29238 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29240 delete macinfo_htab;
29241 macinfo_htab = NULL;
29243 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29244 terminate the current chain and switch to a new comdat .debug_macinfo
29245 section and emit the define/undef entries within it. */
29246 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29247 switch (ref->code)
29249 case 0:
29250 continue;
29251 case DW_MACRO_import:
29253 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29254 tree comdat_key = get_identifier (ref->info);
29255 /* Terminate the previous .debug_macinfo section. */
29256 dw2_asm_output_data (1, 0, "End compilation unit");
29257 targetm.asm_out.named_section (debug_macinfo_section_name,
29258 SECTION_DEBUG
29259 | SECTION_LINKONCE
29260 | (early_lto_debug
29261 ? SECTION_EXCLUDE : 0),
29262 comdat_key);
29263 ASM_GENERATE_INTERNAL_LABEL (label,
29264 DEBUG_MACRO_SECTION_LABEL,
29265 ref->lineno + macinfo_label_base);
29266 ASM_OUTPUT_LABEL (asm_out_file, label);
29267 ref->code = 0;
29268 ref->info = NULL;
29269 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29270 "DWARF macro version number");
29271 if (dwarf_offset_size == 8)
29272 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29273 else
29274 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29276 break;
29277 case DW_MACINFO_define:
29278 case DW_MACINFO_undef:
29279 output_macinfo_op (ref);
29280 ref->code = 0;
29281 ref->info = NULL;
29282 break;
29283 default:
29284 gcc_unreachable ();
29287 macinfo_label_base += macinfo_label_base_adj;
29290 /* As init_sections_and_labels may get called multiple times, have a
29291 generation count for labels. */
29292 static unsigned init_sections_and_labels_generation;
29294 /* Initialize the various sections and labels for dwarf output and prefix
29295 them with PREFIX if non-NULL. Returns the generation (zero based
29296 number of times function was called). */
29298 static unsigned
29299 init_sections_and_labels (bool early_lto_debug)
29301 if (early_lto_debug)
29303 if (!dwarf_split_debug_info)
29305 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29306 SECTION_DEBUG | SECTION_EXCLUDE,
29307 NULL);
29308 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29309 SECTION_DEBUG | SECTION_EXCLUDE,
29310 NULL);
29311 debug_macinfo_section_name
29312 = ((dwarf_strict && dwarf_version < 5)
29313 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29314 debug_macinfo_section = get_section (debug_macinfo_section_name,
29315 SECTION_DEBUG
29316 | SECTION_EXCLUDE, NULL);
29318 else
29320 /* ??? Which of the following do we need early? */
29321 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29322 SECTION_DEBUG | SECTION_EXCLUDE,
29323 NULL);
29324 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29325 SECTION_DEBUG | SECTION_EXCLUDE,
29326 NULL);
29327 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29328 SECTION_DEBUG
29329 | SECTION_EXCLUDE, NULL);
29330 debug_skeleton_abbrev_section
29331 = get_section (DEBUG_LTO_ABBREV_SECTION,
29332 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29333 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29334 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29335 init_sections_and_labels_generation);
29337 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29338 stay in the main .o, but the skeleton_line goes into the split
29339 off dwo. */
29340 debug_skeleton_line_section
29341 = get_section (DEBUG_LTO_LINE_SECTION,
29342 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29343 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29344 DEBUG_SKELETON_LINE_SECTION_LABEL,
29345 init_sections_and_labels_generation);
29346 debug_str_offsets_section
29347 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29348 SECTION_DEBUG | SECTION_EXCLUDE,
29349 NULL);
29350 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29351 DEBUG_SKELETON_INFO_SECTION_LABEL,
29352 init_sections_and_labels_generation);
29353 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29354 DEBUG_STR_DWO_SECTION_FLAGS,
29355 NULL);
29356 debug_macinfo_section_name
29357 = ((dwarf_strict && dwarf_version < 5)
29358 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29359 debug_macinfo_section = get_section (debug_macinfo_section_name,
29360 SECTION_DEBUG | SECTION_EXCLUDE,
29361 NULL);
29363 /* For macro info and the file table we have to refer to a
29364 debug_line section. */
29365 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29366 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29367 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29368 DEBUG_LINE_SECTION_LABEL,
29369 init_sections_and_labels_generation);
29371 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29372 DEBUG_STR_SECTION_FLAGS
29373 | SECTION_EXCLUDE, NULL);
29374 if (!dwarf_split_debug_info)
29375 debug_line_str_section
29376 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29377 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29379 else
29381 if (!dwarf_split_debug_info)
29383 debug_info_section = get_section (DEBUG_INFO_SECTION,
29384 SECTION_DEBUG, NULL);
29385 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29386 SECTION_DEBUG, NULL);
29387 debug_loc_section = get_section (dwarf_version >= 5
29388 ? DEBUG_LOCLISTS_SECTION
29389 : DEBUG_LOC_SECTION,
29390 SECTION_DEBUG, NULL);
29391 debug_macinfo_section_name
29392 = ((dwarf_strict && dwarf_version < 5)
29393 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29394 debug_macinfo_section = get_section (debug_macinfo_section_name,
29395 SECTION_DEBUG, NULL);
29397 else
29399 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29400 SECTION_DEBUG | SECTION_EXCLUDE,
29401 NULL);
29402 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29403 SECTION_DEBUG | SECTION_EXCLUDE,
29404 NULL);
29405 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29406 SECTION_DEBUG, NULL);
29407 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29408 SECTION_DEBUG, NULL);
29409 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29410 SECTION_DEBUG, NULL);
29411 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29412 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29413 init_sections_and_labels_generation);
29415 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29416 stay in the main .o, but the skeleton_line goes into the
29417 split off dwo. */
29418 debug_skeleton_line_section
29419 = get_section (DEBUG_DWO_LINE_SECTION,
29420 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29421 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29422 DEBUG_SKELETON_LINE_SECTION_LABEL,
29423 init_sections_and_labels_generation);
29424 debug_str_offsets_section
29425 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29426 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29427 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29428 DEBUG_SKELETON_INFO_SECTION_LABEL,
29429 init_sections_and_labels_generation);
29430 debug_loc_section = get_section (dwarf_version >= 5
29431 ? DEBUG_DWO_LOCLISTS_SECTION
29432 : DEBUG_DWO_LOC_SECTION,
29433 SECTION_DEBUG | SECTION_EXCLUDE,
29434 NULL);
29435 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29436 DEBUG_STR_DWO_SECTION_FLAGS,
29437 NULL);
29438 debug_macinfo_section_name
29439 = ((dwarf_strict && dwarf_version < 5)
29440 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29441 debug_macinfo_section = get_section (debug_macinfo_section_name,
29442 SECTION_DEBUG | SECTION_EXCLUDE,
29443 NULL);
29444 if (dwarf_version >= 5)
29445 debug_ranges_dwo_section
29446 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29447 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29449 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29450 SECTION_DEBUG, NULL);
29451 debug_line_section = get_section (DEBUG_LINE_SECTION,
29452 SECTION_DEBUG, NULL);
29453 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29454 SECTION_DEBUG, NULL);
29455 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29456 SECTION_DEBUG, NULL);
29457 debug_str_section = get_section (DEBUG_STR_SECTION,
29458 DEBUG_STR_SECTION_FLAGS, NULL);
29459 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29460 || asm_outputs_debug_line_str ())
29461 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29462 DEBUG_STR_SECTION_FLAGS, NULL);
29464 debug_ranges_section = get_section (dwarf_version >= 5
29465 ? DEBUG_RNGLISTS_SECTION
29466 : DEBUG_RANGES_SECTION,
29467 SECTION_DEBUG, NULL);
29468 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29469 SECTION_DEBUG, NULL);
29472 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29473 DEBUG_ABBREV_SECTION_LABEL,
29474 init_sections_and_labels_generation);
29475 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29476 DEBUG_INFO_SECTION_LABEL,
29477 init_sections_and_labels_generation);
29478 info_section_emitted = false;
29479 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29480 DEBUG_LINE_SECTION_LABEL,
29481 init_sections_and_labels_generation);
29482 /* There are up to 6 unique ranges labels per generation.
29483 See also output_rnglists. */
29484 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29485 DEBUG_RANGES_SECTION_LABEL,
29486 init_sections_and_labels_generation * 6);
29487 if (dwarf_version >= 5 && dwarf_split_debug_info)
29488 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29489 DEBUG_RANGES_SECTION_LABEL,
29490 1 + init_sections_and_labels_generation * 6);
29491 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29492 DEBUG_ADDR_SECTION_LABEL,
29493 init_sections_and_labels_generation);
29494 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29495 (dwarf_strict && dwarf_version < 5)
29496 ? DEBUG_MACINFO_SECTION_LABEL
29497 : DEBUG_MACRO_SECTION_LABEL,
29498 init_sections_and_labels_generation);
29499 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29500 init_sections_and_labels_generation);
29502 ++init_sections_and_labels_generation;
29503 return init_sections_and_labels_generation - 1;
29506 /* Set up for Dwarf output at the start of compilation. */
29508 static void
29509 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29511 /* Allocate the file_table. */
29512 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29514 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29515 /* Allocate the decl_die_table. */
29516 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29518 /* Allocate the decl_loc_table. */
29519 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29521 /* Allocate the cached_dw_loc_list_table. */
29522 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29524 /* Allocate the initial hunk of the abbrev_die_table. */
29525 vec_alloc (abbrev_die_table, 256);
29526 /* Zero-th entry is allocated, but unused. */
29527 abbrev_die_table->quick_push (NULL);
29529 /* Allocate the dwarf_proc_stack_usage_map. */
29530 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29532 /* Allocate the pubtypes and pubnames vectors. */
29533 vec_alloc (pubname_table, 32);
29534 vec_alloc (pubtype_table, 32);
29536 vec_alloc (incomplete_types, 64);
29538 vec_alloc (used_rtx_array, 32);
29540 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29541 vec_alloc (macinfo_table, 64);
29542 #endif
29544 /* If front-ends already registered a main translation unit but we were not
29545 ready to perform the association, do this now. */
29546 if (main_translation_unit != NULL_TREE)
29547 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29550 /* Called before compile () starts outputtting functions, variables
29551 and toplevel asms into assembly. */
29553 static void
29554 dwarf2out_assembly_start (void)
29556 if (text_section_line_info)
29557 return;
29559 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29560 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29561 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29562 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29563 COLD_TEXT_SECTION_LABEL, 0);
29564 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29566 switch_to_section (text_section);
29567 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29568 #endif
29570 /* Make sure the line number table for .text always exists. */
29571 text_section_line_info = new_line_info_table ();
29572 text_section_line_info->end_label = text_end_label;
29574 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29575 cur_line_info_table = text_section_line_info;
29576 #endif
29578 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29579 && dwarf2out_do_cfi_asm ()
29580 && !dwarf2out_do_eh_frame ())
29581 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29583 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29584 if (output_asm_line_debug_info () && dwarf_version >= 5)
29586 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29587 tell it the comp_dir and main file name for the zero entry
29588 line table. */
29589 const char *comp_dir, *filename0;
29591 comp_dir = comp_dir_string ();
29592 if (comp_dir == NULL)
29593 comp_dir = "";
29595 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29596 if (filename0 == NULL)
29597 filename0 = "";
29599 fprintf (asm_out_file, "\t.file 0 ");
29600 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29601 fputc (' ', asm_out_file);
29602 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29603 fputc ('\n', asm_out_file);
29605 else
29606 #endif
29607 /* Work around for PR101575: output a dummy .file directive. */
29608 if (!last_emitted_file && dwarf_debuginfo_p ()
29609 && debug_info_level >= DINFO_LEVEL_TERSE)
29611 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29613 if (filename0 == NULL)
29614 filename0 = "<dummy>";
29615 maybe_emit_file (lookup_filename (filename0));
29619 /* A helper function for dwarf2out_finish called through
29620 htab_traverse. Assign a string its index. All strings must be
29621 collected into the table by the time index_string is called,
29622 because the indexing code relies on htab_traverse to traverse nodes
29623 in the same order for each run. */
29626 index_string (indirect_string_node **h, unsigned int *index)
29628 indirect_string_node *node = *h;
29630 find_string_form (node);
29631 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29633 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29634 node->index = *index;
29635 *index += 1;
29637 return 1;
29640 /* A helper function for output_indirect_strings called through
29641 htab_traverse. Output the offset to a string and update the
29642 current offset. */
29645 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29647 indirect_string_node *node = *h;
29649 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29651 /* Assert that this node has been assigned an index. */
29652 gcc_assert (node->index != NO_INDEX_ASSIGNED
29653 && node->index != NOT_INDEXED);
29654 dw2_asm_output_data (dwarf_offset_size, *offset,
29655 "indexed string 0x%x: %s", node->index, node->str);
29656 *offset += strlen (node->str) + 1;
29658 return 1;
29661 /* A helper function for dwarf2out_finish called through
29662 htab_traverse. Output the indexed string. */
29665 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29667 struct indirect_string_node *node = *h;
29669 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29671 /* Assert that the strings are output in the same order as their
29672 indexes were assigned. */
29673 gcc_assert (*cur_idx == node->index);
29674 assemble_string (node->str, strlen (node->str) + 1);
29675 *cur_idx += 1;
29677 return 1;
29680 /* A helper function for output_indirect_strings. Counts the number
29681 of index strings offsets. Must match the logic of the functions
29682 output_index_string[_offsets] above. */
29684 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29686 struct indirect_string_node *node = *h;
29688 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29689 *last_idx += 1;
29690 return 1;
29693 /* A helper function for dwarf2out_finish called through
29694 htab_traverse. Emit one queued .debug_str string. */
29697 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29699 struct indirect_string_node *node = *h;
29701 node->form = find_string_form (node);
29702 if (node->form == form && node->refcount > 0)
29704 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29705 assemble_string (node->str, strlen (node->str) + 1);
29708 return 1;
29711 /* Output the indexed string table. */
29713 static void
29714 output_indirect_strings (void)
29716 switch_to_section (debug_str_section);
29717 if (!dwarf_split_debug_info)
29718 debug_str_hash->traverse<enum dwarf_form,
29719 output_indirect_string> (DW_FORM_strp);
29720 else
29722 unsigned int offset = 0;
29723 unsigned int cur_idx = 0;
29725 if (skeleton_debug_str_hash)
29726 skeleton_debug_str_hash->traverse<enum dwarf_form,
29727 output_indirect_string> (DW_FORM_strp);
29729 switch_to_section (debug_str_offsets_section);
29730 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29731 header. Note that we don't need to generate a label to the
29732 actual index table following the header here, because this is
29733 for the split dwarf case only. In an .dwo file there is only
29734 one string offsets table (and one debug info section). But
29735 if we would start using string offset tables for the main (or
29736 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29737 pointing to the actual index after the header. Split dwarf
29738 units will never have a string offsets base attribute. When
29739 a split unit is moved into a .dwp file the string offsets can
29740 be found through the .debug_cu_index section table. */
29741 if (dwarf_version >= 5)
29743 unsigned int last_idx = 0;
29744 unsigned long str_offsets_length;
29746 debug_str_hash->traverse_noresize
29747 <unsigned int *, count_index_strings> (&last_idx);
29748 str_offsets_length = last_idx * dwarf_offset_size + 4;
29749 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29750 dw2_asm_output_data (4, 0xffffffff,
29751 "Escape value for 64-bit DWARF extension");
29752 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29753 "Length of string offsets unit");
29754 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29755 dw2_asm_output_data (2, 0, "Header zero padding");
29757 debug_str_hash->traverse_noresize
29758 <unsigned int *, output_index_string_offset> (&offset);
29759 switch_to_section (debug_str_dwo_section);
29760 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29761 (&cur_idx);
29765 /* Callback for htab_traverse to assign an index to an entry in the
29766 table, and to write that entry to the .debug_addr section. */
29769 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29771 addr_table_entry *entry = *slot;
29773 if (entry->refcount == 0)
29775 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29776 || entry->index == NOT_INDEXED);
29777 return 1;
29780 gcc_assert (entry->index == *cur_index);
29781 (*cur_index)++;
29783 switch (entry->kind)
29785 case ate_kind_rtx:
29786 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29787 "0x%x", entry->index);
29788 break;
29789 case ate_kind_rtx_dtprel:
29790 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29791 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29792 DWARF2_ADDR_SIZE,
29793 entry->addr.rtl);
29794 fputc ('\n', asm_out_file);
29795 break;
29796 case ate_kind_label:
29797 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29798 "0x%x", entry->index);
29799 break;
29800 default:
29801 gcc_unreachable ();
29803 return 1;
29806 /* A helper function for dwarf2out_finish. Counts the number
29807 of indexed addresses. Must match the logic of the functions
29808 output_addr_table_entry above. */
29810 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29812 addr_table_entry *entry = *slot;
29814 if (entry->refcount > 0)
29815 *last_idx += 1;
29816 return 1;
29819 /* Produce the .debug_addr section. */
29821 static void
29822 output_addr_table (void)
29824 unsigned int index = 0;
29825 if (addr_index_table == NULL || addr_index_table->size () == 0)
29826 return;
29828 switch_to_section (debug_addr_section);
29829 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29830 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29831 before DWARF5, didn't have a header for .debug_addr units.
29832 DWARF5 specifies a small header when address tables are used. */
29833 if (dwarf_version >= 5)
29835 unsigned int last_idx = 0;
29836 unsigned long addrs_length;
29838 addr_index_table->traverse_noresize
29839 <unsigned int *, count_index_addrs> (&last_idx);
29840 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29842 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29843 dw2_asm_output_data (4, 0xffffffff,
29844 "Escape value for 64-bit DWARF extension");
29845 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29846 "Length of Address Unit");
29847 dw2_asm_output_data (2, 5, "DWARF addr version");
29848 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29849 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29851 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29853 addr_index_table
29854 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29857 #if ENABLE_ASSERT_CHECKING
29858 /* Verify that all marks are clear. */
29860 static void
29861 verify_marks_clear (dw_die_ref die)
29863 dw_die_ref c;
29865 gcc_assert (! die->die_mark);
29866 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29868 #endif /* ENABLE_ASSERT_CHECKING */
29870 /* Clear the marks for a die and its children.
29871 Be cool if the mark isn't set. */
29873 static void
29874 prune_unmark_dies (dw_die_ref die)
29876 dw_die_ref c;
29878 if (die->die_mark)
29879 die->die_mark = 0;
29880 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29883 /* Given LOC that is referenced by a DIE we're marking as used, find all
29884 referenced DWARF procedures it references and mark them as used. */
29886 static void
29887 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29889 for (; loc != NULL; loc = loc->dw_loc_next)
29890 switch (loc->dw_loc_opc)
29892 case DW_OP_implicit_pointer:
29893 case DW_OP_convert:
29894 case DW_OP_reinterpret:
29895 case DW_OP_GNU_implicit_pointer:
29896 case DW_OP_GNU_convert:
29897 case DW_OP_GNU_reinterpret:
29898 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29899 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29900 break;
29901 case DW_OP_GNU_variable_value:
29902 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29904 dw_die_ref ref
29905 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29906 if (ref == NULL)
29907 break;
29908 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29909 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29910 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29912 /* FALLTHRU */
29913 case DW_OP_call2:
29914 case DW_OP_call4:
29915 case DW_OP_call_ref:
29916 case DW_OP_const_type:
29917 case DW_OP_GNU_const_type:
29918 case DW_OP_GNU_parameter_ref:
29919 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29920 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29921 break;
29922 case DW_OP_regval_type:
29923 case DW_OP_deref_type:
29924 case DW_OP_GNU_regval_type:
29925 case DW_OP_GNU_deref_type:
29926 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29927 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29928 break;
29929 case DW_OP_entry_value:
29930 case DW_OP_GNU_entry_value:
29931 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29932 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29933 break;
29934 default:
29935 break;
29939 /* Given DIE that we're marking as used, find any other dies
29940 it references as attributes and mark them as used. */
29942 static void
29943 prune_unused_types_walk_attribs (dw_die_ref die)
29945 dw_attr_node *a;
29946 unsigned ix;
29948 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29950 switch (AT_class (a))
29952 /* Make sure DWARF procedures referenced by location descriptions will
29953 get emitted. */
29954 case dw_val_class_loc:
29955 prune_unused_types_walk_loc_descr (AT_loc (a));
29956 break;
29957 case dw_val_class_loc_list:
29958 for (dw_loc_list_ref list = AT_loc_list (a);
29959 list != NULL;
29960 list = list->dw_loc_next)
29961 prune_unused_types_walk_loc_descr (list->expr);
29962 break;
29964 case dw_val_class_view_list:
29965 /* This points to a loc_list in another attribute, so it's
29966 already covered. */
29967 break;
29969 case dw_val_class_die_ref:
29970 /* A reference to another DIE.
29971 Make sure that it will get emitted.
29972 If it was broken out into a comdat group, don't follow it. */
29973 if (! AT_ref (a)->comdat_type_p
29974 || a->dw_attr == DW_AT_specification)
29975 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29976 break;
29978 case dw_val_class_str:
29979 /* Set the string's refcount to 0 so that prune_unused_types_mark
29980 accounts properly for it. */
29981 a->dw_attr_val.v.val_str->refcount = 0;
29982 break;
29984 default:
29985 break;
29990 /* Mark the generic parameters and arguments children DIEs of DIE. */
29992 static void
29993 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29995 dw_die_ref c;
29997 if (die == NULL || die->die_child == NULL)
29998 return;
29999 c = die->die_child;
30002 if (is_template_parameter (c))
30003 prune_unused_types_mark (c, 1);
30004 c = c->die_sib;
30005 } while (c && c != die->die_child);
30008 /* Mark DIE as being used. If DOKIDS is true, then walk down
30009 to DIE's children. */
30011 static void
30012 prune_unused_types_mark (dw_die_ref die, int dokids)
30014 dw_die_ref c;
30016 if (die->die_mark == 0)
30018 /* We haven't done this node yet. Mark it as used. */
30019 die->die_mark = 1;
30020 /* If this is the DIE of a generic type instantiation,
30021 mark the children DIEs that describe its generic parms and
30022 args. */
30023 prune_unused_types_mark_generic_parms_dies (die);
30025 /* We also have to mark its parents as used.
30026 (But we don't want to mark our parent's kids due to this,
30027 unless it is a class.) */
30028 if (die->die_parent)
30029 prune_unused_types_mark (die->die_parent,
30030 class_scope_p (die->die_parent));
30032 /* Mark any referenced nodes. */
30033 prune_unused_types_walk_attribs (die);
30035 /* If this node is a specification,
30036 also mark the definition, if it exists. */
30037 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30038 prune_unused_types_mark (die->die_definition, 1);
30041 if (dokids && die->die_mark != 2)
30043 /* We need to walk the children, but haven't done so yet.
30044 Remember that we've walked the kids. */
30045 die->die_mark = 2;
30047 /* If this is an array type, we need to make sure our
30048 kids get marked, even if they're types. If we're
30049 breaking out types into comdat sections, do this
30050 for all type definitions. */
30051 if (die->die_tag == DW_TAG_array_type
30052 || (use_debug_types
30053 && is_type_die (die) && ! is_declaration_die (die)))
30054 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30055 else
30056 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30060 /* For local classes, look if any static member functions were emitted
30061 and if so, mark them. */
30063 static void
30064 prune_unused_types_walk_local_classes (dw_die_ref die)
30066 dw_die_ref c;
30068 if (die->die_mark == 2)
30069 return;
30071 switch (die->die_tag)
30073 case DW_TAG_structure_type:
30074 case DW_TAG_union_type:
30075 case DW_TAG_class_type:
30076 case DW_TAG_interface_type:
30077 break;
30079 case DW_TAG_subprogram:
30080 if (!get_AT_flag (die, DW_AT_declaration)
30081 || die->die_definition != NULL)
30082 prune_unused_types_mark (die, 1);
30083 return;
30085 default:
30086 return;
30089 /* Mark children. */
30090 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30093 /* Walk the tree DIE and mark types that we actually use. */
30095 static void
30096 prune_unused_types_walk (dw_die_ref die)
30098 dw_die_ref c;
30100 /* Don't do anything if this node is already marked and
30101 children have been marked as well. */
30102 if (die->die_mark == 2)
30103 return;
30105 switch (die->die_tag)
30107 case DW_TAG_structure_type:
30108 case DW_TAG_union_type:
30109 case DW_TAG_class_type:
30110 case DW_TAG_interface_type:
30111 if (die->die_perennial_p)
30112 break;
30114 for (c = die->die_parent; c; c = c->die_parent)
30115 if (c->die_tag == DW_TAG_subprogram)
30116 break;
30118 /* Finding used static member functions inside of classes
30119 is needed just for local classes, because for other classes
30120 static member function DIEs with DW_AT_specification
30121 are emitted outside of the DW_TAG_*_type. If we ever change
30122 it, we'd need to call this even for non-local classes. */
30123 if (c)
30124 prune_unused_types_walk_local_classes (die);
30126 /* It's a type node --- don't mark it. */
30127 return;
30129 case DW_TAG_const_type:
30130 case DW_TAG_packed_type:
30131 case DW_TAG_pointer_type:
30132 case DW_TAG_reference_type:
30133 case DW_TAG_rvalue_reference_type:
30134 case DW_TAG_volatile_type:
30135 case DW_TAG_typedef:
30136 case DW_TAG_array_type:
30137 case DW_TAG_friend:
30138 case DW_TAG_enumeration_type:
30139 case DW_TAG_subroutine_type:
30140 case DW_TAG_string_type:
30141 case DW_TAG_set_type:
30142 case DW_TAG_subrange_type:
30143 case DW_TAG_ptr_to_member_type:
30144 case DW_TAG_file_type:
30145 /* Type nodes are useful only when other DIEs reference them --- don't
30146 mark them. */
30147 /* FALLTHROUGH */
30149 case DW_TAG_dwarf_procedure:
30150 /* Likewise for DWARF procedures. */
30152 if (die->die_perennial_p)
30153 break;
30155 return;
30157 case DW_TAG_variable:
30158 if (flag_debug_only_used_symbols)
30160 if (die->die_perennial_p)
30161 break;
30163 /* For static data members, the declaration in the class is supposed
30164 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30165 DWARF5. DW_TAG_member will be marked, so mark even such
30166 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30167 attribute. */
30168 if (dwarf_version >= 5
30169 && class_scope_p (die->die_parent)
30170 && get_AT (die, DW_AT_const_value))
30171 break;
30173 /* premark_used_variables marks external variables --- don't mark
30174 them here. But function-local externals are always considered
30175 used. */
30176 if (get_AT (die, DW_AT_external))
30178 for (c = die->die_parent; c; c = c->die_parent)
30179 if (c->die_tag == DW_TAG_subprogram)
30180 break;
30181 if (!c)
30182 return;
30185 /* FALLTHROUGH */
30187 default:
30188 /* Mark everything else. */
30189 break;
30192 if (die->die_mark == 0)
30194 die->die_mark = 1;
30196 /* Now, mark any dies referenced from here. */
30197 prune_unused_types_walk_attribs (die);
30200 die->die_mark = 2;
30202 /* Mark children. */
30203 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30206 /* Increment the string counts on strings referred to from DIE's
30207 attributes. */
30209 static void
30210 prune_unused_types_update_strings (dw_die_ref die)
30212 dw_attr_node *a;
30213 unsigned ix;
30215 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30216 if (AT_class (a) == dw_val_class_str)
30218 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30219 s->refcount++;
30220 /* Avoid unnecessarily putting strings that are used less than
30221 twice in the hash table. */
30222 if (s->form != DW_FORM_line_strp
30223 && (s->refcount
30224 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30226 indirect_string_node **slot
30227 = debug_str_hash->find_slot_with_hash (s->str,
30228 htab_hash_string (s->str),
30229 INSERT);
30230 gcc_assert (*slot == NULL);
30231 *slot = s;
30236 /* Mark DIE and its children as removed. */
30238 static void
30239 mark_removed (dw_die_ref die)
30241 dw_die_ref c;
30242 die->removed = true;
30243 FOR_EACH_CHILD (die, c, mark_removed (c));
30246 /* Remove from the tree DIE any dies that aren't marked. */
30248 static void
30249 prune_unused_types_prune (dw_die_ref die)
30251 dw_die_ref c;
30253 gcc_assert (die->die_mark);
30254 prune_unused_types_update_strings (die);
30256 if (! die->die_child)
30257 return;
30259 c = die->die_child;
30260 do {
30261 dw_die_ref prev = c, next;
30262 for (c = c->die_sib; ! c->die_mark; c = next)
30263 if (c == die->die_child)
30265 /* No marked children between 'prev' and the end of the list. */
30266 if (prev == c)
30267 /* No marked children at all. */
30268 die->die_child = NULL;
30269 else
30271 prev->die_sib = c->die_sib;
30272 die->die_child = prev;
30274 c->die_sib = NULL;
30275 mark_removed (c);
30276 return;
30278 else
30280 next = c->die_sib;
30281 c->die_sib = NULL;
30282 mark_removed (c);
30285 if (c != prev->die_sib)
30286 prev->die_sib = c;
30287 prune_unused_types_prune (c);
30288 } while (c != die->die_child);
30291 /* Remove dies representing declarations that we never use. */
30293 static void
30294 prune_unused_types (void)
30296 unsigned int i;
30297 limbo_die_node *node;
30298 comdat_type_node *ctnode;
30299 pubname_entry *pub;
30300 dw_die_ref base_type;
30302 #if ENABLE_ASSERT_CHECKING
30303 /* All the marks should already be clear. */
30304 verify_marks_clear (comp_unit_die ());
30305 for (node = limbo_die_list; node; node = node->next)
30306 verify_marks_clear (node->die);
30307 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30308 verify_marks_clear (ctnode->root_die);
30309 #endif /* ENABLE_ASSERT_CHECKING */
30311 /* Mark types that are used in global variables. */
30312 premark_types_used_by_global_vars ();
30314 /* Mark variables used in the symtab. */
30315 if (flag_debug_only_used_symbols)
30316 premark_used_variables ();
30318 /* Set the mark on nodes that are actually used. */
30319 prune_unused_types_walk (comp_unit_die ());
30320 for (node = limbo_die_list; node; node = node->next)
30321 prune_unused_types_walk (node->die);
30322 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30324 prune_unused_types_walk (ctnode->root_die);
30325 prune_unused_types_mark (ctnode->type_die, 1);
30328 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30329 are unusual in that they are pubnames that are the children of pubtypes.
30330 They should only be marked via their parent DW_TAG_enumeration_type die,
30331 not as roots in themselves. */
30332 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30333 if (pub->die->die_tag != DW_TAG_enumerator)
30334 prune_unused_types_mark (pub->die, 1);
30335 for (i = 0; base_types.iterate (i, &base_type); i++)
30336 prune_unused_types_mark (base_type, 1);
30338 /* Also set the mark on nodes that could be referenced by
30339 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30340 by DW_TAG_inlined_subroutine origins. */
30341 cgraph_node *cnode;
30342 FOR_EACH_FUNCTION (cnode)
30343 if (cnode->referred_to_p (false))
30345 dw_die_ref die = lookup_decl_die (cnode->decl);
30346 if (die == NULL || die->die_mark)
30347 continue;
30348 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30349 if (e->caller != cnode)
30351 prune_unused_types_mark (die, 1);
30352 break;
30356 if (debug_str_hash)
30357 debug_str_hash->empty ();
30358 if (skeleton_debug_str_hash)
30359 skeleton_debug_str_hash->empty ();
30360 prune_unused_types_prune (comp_unit_die ());
30361 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30363 node = *pnode;
30364 if (!node->die->die_mark)
30365 *pnode = node->next;
30366 else
30368 prune_unused_types_prune (node->die);
30369 pnode = &node->next;
30372 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30373 prune_unused_types_prune (ctnode->root_die);
30375 /* Leave the marks clear. */
30376 prune_unmark_dies (comp_unit_die ());
30377 for (node = limbo_die_list; node; node = node->next)
30378 prune_unmark_dies (node->die);
30379 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30380 prune_unmark_dies (ctnode->root_die);
30383 /* Helpers to manipulate hash table of comdat type units. */
30385 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30387 static inline hashval_t hash (const comdat_type_node *);
30388 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30391 inline hashval_t
30392 comdat_type_hasher::hash (const comdat_type_node *type_node)
30394 hashval_t h;
30395 memcpy (&h, type_node->signature, sizeof (h));
30396 return h;
30399 inline bool
30400 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30401 const comdat_type_node *type_node_2)
30403 return (! memcmp (type_node_1->signature, type_node_2->signature,
30404 DWARF_TYPE_SIGNATURE_SIZE));
30407 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30408 to the location it would have been added, should we know its
30409 DECL_ASSEMBLER_NAME when we added other attributes. This will
30410 probably improve compactness of debug info, removing equivalent
30411 abbrevs, and hide any differences caused by deferring the
30412 computation of the assembler name, triggered by e.g. PCH. */
30414 static inline void
30415 move_linkage_attr (dw_die_ref die)
30417 unsigned ix = vec_safe_length (die->die_attr);
30418 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30420 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30421 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30423 while (--ix > 0)
30425 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30427 if (prev->dw_attr == DW_AT_decl_line
30428 || prev->dw_attr == DW_AT_decl_column
30429 || prev->dw_attr == DW_AT_name)
30430 break;
30433 if (ix != vec_safe_length (die->die_attr) - 1)
30435 die->die_attr->pop ();
30436 die->die_attr->quick_insert (ix, linkage);
30440 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30441 referenced from typed stack ops and count how often they are used. */
30443 static void
30444 mark_base_types (dw_loc_descr_ref loc)
30446 dw_die_ref base_type = NULL;
30448 for (; loc; loc = loc->dw_loc_next)
30450 switch (loc->dw_loc_opc)
30452 case DW_OP_regval_type:
30453 case DW_OP_deref_type:
30454 case DW_OP_GNU_regval_type:
30455 case DW_OP_GNU_deref_type:
30456 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30457 break;
30458 case DW_OP_convert:
30459 case DW_OP_reinterpret:
30460 case DW_OP_GNU_convert:
30461 case DW_OP_GNU_reinterpret:
30462 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30463 continue;
30464 /* FALLTHRU */
30465 case DW_OP_const_type:
30466 case DW_OP_GNU_const_type:
30467 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30468 break;
30469 case DW_OP_entry_value:
30470 case DW_OP_GNU_entry_value:
30471 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30472 continue;
30473 default:
30474 continue;
30476 gcc_assert (base_type->die_parent == comp_unit_die ());
30477 if (base_type->die_mark)
30478 base_type->die_mark++;
30479 else
30481 base_types.safe_push (base_type);
30482 base_type->die_mark = 1;
30487 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30488 referenced from typed stack ops and count how often they are used. */
30490 static void
30491 mark_base_types (dw_die_ref die)
30493 dw_die_ref c;
30494 dw_attr_node *a;
30495 dw_loc_list_ref *curr;
30496 unsigned ix;
30498 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30499 switch (AT_class (a))
30501 case dw_val_class_loc_list:
30502 curr = AT_loc_list_ptr (a);
30503 while (*curr)
30505 mark_base_types ((*curr)->expr);
30506 curr = &(*curr)->dw_loc_next;
30508 break;
30510 case dw_val_class_loc:
30511 mark_base_types (AT_loc (a));
30512 break;
30514 default:
30515 break;
30518 FOR_EACH_CHILD (die, c, mark_base_types (c));
30521 /* Comparison function for sorting marked base types. */
30523 static int
30524 base_type_cmp (const void *x, const void *y)
30526 dw_die_ref dx = *(const dw_die_ref *) x;
30527 dw_die_ref dy = *(const dw_die_ref *) y;
30528 unsigned int byte_size1, byte_size2;
30529 unsigned int encoding1, encoding2;
30530 unsigned int align1, align2;
30531 if (dx->die_mark > dy->die_mark)
30532 return -1;
30533 if (dx->die_mark < dy->die_mark)
30534 return 1;
30535 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30536 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30537 if (byte_size1 < byte_size2)
30538 return 1;
30539 if (byte_size1 > byte_size2)
30540 return -1;
30541 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30542 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30543 if (encoding1 < encoding2)
30544 return 1;
30545 if (encoding1 > encoding2)
30546 return -1;
30547 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30548 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30549 if (align1 < align2)
30550 return 1;
30551 if (align1 > align2)
30552 return -1;
30553 return 0;
30556 /* Move base types marked by mark_base_types as early as possible
30557 in the CU, sorted by decreasing usage count both to make the
30558 uleb128 references as small as possible and to make sure they
30559 will have die_offset already computed by calc_die_sizes when
30560 sizes of typed stack loc ops is computed. */
30562 static void
30563 move_marked_base_types (void)
30565 unsigned int i;
30566 dw_die_ref base_type, die, c;
30568 if (base_types.is_empty ())
30569 return;
30571 /* Sort by decreasing usage count, they will be added again in that
30572 order later on. */
30573 base_types.qsort (base_type_cmp);
30574 die = comp_unit_die ();
30575 c = die->die_child;
30578 dw_die_ref prev = c;
30579 c = c->die_sib;
30580 while (c->die_mark)
30582 remove_child_with_prev (c, prev);
30583 /* As base types got marked, there must be at least
30584 one node other than DW_TAG_base_type. */
30585 gcc_assert (die->die_child != NULL);
30586 c = prev->die_sib;
30589 while (c != die->die_child);
30590 gcc_assert (die->die_child);
30591 c = die->die_child;
30592 for (i = 0; base_types.iterate (i, &base_type); i++)
30594 base_type->die_mark = 0;
30595 base_type->die_sib = c->die_sib;
30596 c->die_sib = base_type;
30597 c = base_type;
30601 /* Helper function for resolve_addr, attempt to resolve
30602 one CONST_STRING, return true if successful. Similarly verify that
30603 SYMBOL_REFs refer to variables emitted in the current CU. */
30605 static bool
30606 resolve_one_addr (rtx *addr)
30608 rtx rtl = *addr;
30610 if (GET_CODE (rtl) == CONST_STRING)
30612 size_t len = strlen (XSTR (rtl, 0)) + 1;
30613 tree t = build_string (len, XSTR (rtl, 0));
30614 tree tlen = size_int (len - 1);
30615 TREE_TYPE (t)
30616 = build_array_type (char_type_node, build_index_type (tlen));
30617 rtl = lookup_constant_def (t);
30618 if (!rtl || !MEM_P (rtl))
30619 return false;
30620 rtl = XEXP (rtl, 0);
30621 if (GET_CODE (rtl) == SYMBOL_REF
30622 && SYMBOL_REF_DECL (rtl)
30623 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30624 return false;
30625 vec_safe_push (used_rtx_array, rtl);
30626 *addr = rtl;
30627 return true;
30630 if (GET_CODE (rtl) == SYMBOL_REF
30631 && SYMBOL_REF_DECL (rtl))
30633 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30635 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30636 return false;
30638 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30639 return false;
30642 if (GET_CODE (rtl) == CONST)
30644 subrtx_ptr_iterator::array_type array;
30645 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30646 if (!resolve_one_addr (*iter))
30647 return false;
30650 return true;
30653 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30654 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30655 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30657 static rtx
30658 string_cst_pool_decl (tree t)
30660 rtx rtl = output_constant_def (t, 1);
30661 unsigned char *array;
30662 dw_loc_descr_ref l;
30663 tree decl;
30664 size_t len;
30665 dw_die_ref ref;
30667 if (!rtl || !MEM_P (rtl))
30668 return NULL_RTX;
30669 rtl = XEXP (rtl, 0);
30670 if (GET_CODE (rtl) != SYMBOL_REF
30671 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30672 return NULL_RTX;
30674 decl = SYMBOL_REF_DECL (rtl);
30675 if (!lookup_decl_die (decl))
30677 len = TREE_STRING_LENGTH (t);
30678 vec_safe_push (used_rtx_array, rtl);
30679 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30680 array = ggc_vec_alloc<unsigned char> (len);
30681 memcpy (array, TREE_STRING_POINTER (t), len);
30682 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30683 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30684 l->dw_loc_oprnd2.v.val_vec.length = len;
30685 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30686 l->dw_loc_oprnd2.v.val_vec.array = array;
30687 add_AT_loc (ref, DW_AT_location, l);
30688 equate_decl_number_to_die (decl, ref);
30690 return rtl;
30693 /* Helper function of resolve_addr_in_expr. LOC is
30694 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30695 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30696 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30697 with DW_OP_implicit_pointer if possible
30698 and return true, if unsuccessful, return false. */
30700 static bool
30701 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30703 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30704 HOST_WIDE_INT offset = 0;
30705 dw_die_ref ref = NULL;
30706 tree decl;
30708 if (GET_CODE (rtl) == CONST
30709 && GET_CODE (XEXP (rtl, 0)) == PLUS
30710 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30712 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30713 rtl = XEXP (XEXP (rtl, 0), 0);
30715 if (GET_CODE (rtl) == CONST_STRING)
30717 size_t len = strlen (XSTR (rtl, 0)) + 1;
30718 tree t = build_string (len, XSTR (rtl, 0));
30719 tree tlen = size_int (len - 1);
30721 TREE_TYPE (t)
30722 = build_array_type (char_type_node, build_index_type (tlen));
30723 rtl = string_cst_pool_decl (t);
30724 if (!rtl)
30725 return false;
30727 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30729 decl = SYMBOL_REF_DECL (rtl);
30730 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30732 ref = lookup_decl_die (decl);
30733 if (ref && (get_AT (ref, DW_AT_location)
30734 || get_AT (ref, DW_AT_const_value)))
30736 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30737 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30738 loc->dw_loc_oprnd1.val_entry = NULL;
30739 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30740 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30741 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30742 loc->dw_loc_oprnd2.v.val_int = offset;
30743 return true;
30747 return false;
30750 /* Helper function for resolve_addr, handle one location
30751 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30752 the location list couldn't be resolved. */
30754 static bool
30755 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30757 dw_loc_descr_ref keep = NULL;
30758 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30759 switch (loc->dw_loc_opc)
30761 case DW_OP_addr:
30762 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30764 if ((prev == NULL
30765 || prev->dw_loc_opc == DW_OP_piece
30766 || prev->dw_loc_opc == DW_OP_bit_piece)
30767 && loc->dw_loc_next
30768 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30769 && (!dwarf_strict || dwarf_version >= 5)
30770 && optimize_one_addr_into_implicit_ptr (loc))
30771 break;
30772 return false;
30774 break;
30775 case DW_OP_GNU_addr_index:
30776 case DW_OP_addrx:
30777 case DW_OP_GNU_const_index:
30778 case DW_OP_constx:
30779 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30780 || loc->dw_loc_opc == DW_OP_addrx)
30781 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30782 || loc->dw_loc_opc == DW_OP_constx)
30783 && loc->dtprel))
30785 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30786 if (!resolve_one_addr (&rtl))
30787 return false;
30788 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30789 loc->dw_loc_oprnd1.val_entry
30790 = add_addr_table_entry (rtl, ate_kind_rtx);
30792 break;
30793 case DW_OP_const4u:
30794 case DW_OP_const8u:
30795 if (loc->dtprel
30796 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30797 return false;
30798 break;
30799 case DW_OP_plus_uconst:
30800 if (size_of_loc_descr (loc)
30801 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30803 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30805 dw_loc_descr_ref repl
30806 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30807 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30808 add_loc_descr (&repl, loc->dw_loc_next);
30809 *loc = *repl;
30811 break;
30812 case DW_OP_implicit_value:
30813 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30814 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30815 return false;
30816 break;
30817 case DW_OP_implicit_pointer:
30818 case DW_OP_GNU_implicit_pointer:
30819 case DW_OP_GNU_parameter_ref:
30820 case DW_OP_GNU_variable_value:
30821 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30823 dw_die_ref ref
30824 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30825 if (ref == NULL)
30826 return false;
30827 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30828 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30829 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30831 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30833 if (prev == NULL
30834 && loc->dw_loc_next == NULL
30835 && AT_class (a) == dw_val_class_loc)
30836 switch (a->dw_attr)
30838 /* Following attributes allow both exprloc and reference,
30839 so if the whole expression is DW_OP_GNU_variable_value
30840 alone we could transform it into reference. */
30841 case DW_AT_byte_size:
30842 case DW_AT_bit_size:
30843 case DW_AT_lower_bound:
30844 case DW_AT_upper_bound:
30845 case DW_AT_bit_stride:
30846 case DW_AT_count:
30847 case DW_AT_allocated:
30848 case DW_AT_associated:
30849 case DW_AT_byte_stride:
30850 a->dw_attr_val.val_class = dw_val_class_die_ref;
30851 a->dw_attr_val.val_entry = NULL;
30852 a->dw_attr_val.v.val_die_ref.die
30853 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30854 a->dw_attr_val.v.val_die_ref.external = 0;
30855 return true;
30856 default:
30857 break;
30859 if (dwarf_strict)
30860 return false;
30862 break;
30863 case DW_OP_const_type:
30864 case DW_OP_regval_type:
30865 case DW_OP_deref_type:
30866 case DW_OP_convert:
30867 case DW_OP_reinterpret:
30868 case DW_OP_GNU_const_type:
30869 case DW_OP_GNU_regval_type:
30870 case DW_OP_GNU_deref_type:
30871 case DW_OP_GNU_convert:
30872 case DW_OP_GNU_reinterpret:
30873 while (loc->dw_loc_next
30874 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30875 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30877 dw_die_ref base1, base2;
30878 unsigned enc1, enc2, size1, size2;
30879 if (loc->dw_loc_opc == DW_OP_regval_type
30880 || loc->dw_loc_opc == DW_OP_deref_type
30881 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30882 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30883 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30884 else if (loc->dw_loc_oprnd1.val_class
30885 == dw_val_class_unsigned_const)
30886 break;
30887 else
30888 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30889 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30890 == dw_val_class_unsigned_const)
30891 break;
30892 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30893 gcc_assert (base1->die_tag == DW_TAG_base_type
30894 && base2->die_tag == DW_TAG_base_type);
30895 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30896 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30897 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30898 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30899 if (size1 == size2
30900 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30901 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30902 && loc != keep)
30903 || enc1 == enc2))
30905 /* Optimize away next DW_OP_convert after
30906 adjusting LOC's base type die reference. */
30907 if (loc->dw_loc_opc == DW_OP_regval_type
30908 || loc->dw_loc_opc == DW_OP_deref_type
30909 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30910 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30911 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30912 else
30913 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30914 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30915 continue;
30917 /* Don't change integer DW_OP_convert after e.g. floating
30918 point typed stack entry. */
30919 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30920 keep = loc->dw_loc_next;
30921 break;
30923 break;
30924 default:
30925 break;
30927 return true;
30930 /* Helper function of resolve_addr. DIE had DW_AT_location of
30931 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30932 and DW_OP_addr couldn't be resolved. resolve_addr has already
30933 removed the DW_AT_location attribute. This function attempts to
30934 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30935 to it or DW_AT_const_value attribute, if possible. */
30937 static void
30938 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30940 if (!VAR_P (decl)
30941 || lookup_decl_die (decl) != die
30942 || DECL_EXTERNAL (decl)
30943 || !TREE_STATIC (decl)
30944 || DECL_INITIAL (decl) == NULL_TREE
30945 || DECL_P (DECL_INITIAL (decl))
30946 || get_AT (die, DW_AT_const_value))
30947 return;
30949 tree init = DECL_INITIAL (decl);
30950 HOST_WIDE_INT offset = 0;
30951 /* For variables that have been optimized away and thus
30952 don't have a memory location, see if we can emit
30953 DW_AT_const_value instead. */
30954 if (tree_add_const_value_attribute (die, init))
30955 return;
30956 if (dwarf_strict && dwarf_version < 5)
30957 return;
30958 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30959 and ADDR_EXPR refers to a decl that has DW_AT_location or
30960 DW_AT_const_value (but isn't addressable, otherwise
30961 resolving the original DW_OP_addr wouldn't fail), see if
30962 we can add DW_OP_implicit_pointer. */
30963 STRIP_NOPS (init);
30964 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30965 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30967 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30968 init = TREE_OPERAND (init, 0);
30969 STRIP_NOPS (init);
30971 if (TREE_CODE (init) != ADDR_EXPR)
30972 return;
30973 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30974 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30975 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30976 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30977 && TREE_OPERAND (init, 0) != decl))
30979 dw_die_ref ref;
30980 dw_loc_descr_ref l;
30982 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30984 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30985 if (!rtl)
30986 return;
30987 decl = SYMBOL_REF_DECL (rtl);
30989 else
30990 decl = TREE_OPERAND (init, 0);
30991 ref = lookup_decl_die (decl);
30992 if (ref == NULL
30993 || (!get_AT (ref, DW_AT_location)
30994 && !get_AT (ref, DW_AT_const_value)))
30995 return;
30996 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30997 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30998 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30999 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31000 add_AT_loc (die, DW_AT_location, l);
31004 /* Return NULL if l is a DWARF expression, or first op that is not
31005 valid DWARF expression. */
31007 static dw_loc_descr_ref
31008 non_dwarf_expression (dw_loc_descr_ref l)
31010 while (l)
31012 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31013 return l;
31014 switch (l->dw_loc_opc)
31016 case DW_OP_regx:
31017 case DW_OP_implicit_value:
31018 case DW_OP_stack_value:
31019 case DW_OP_implicit_pointer:
31020 case DW_OP_GNU_implicit_pointer:
31021 case DW_OP_GNU_parameter_ref:
31022 case DW_OP_piece:
31023 case DW_OP_bit_piece:
31024 return l;
31025 default:
31026 break;
31028 l = l->dw_loc_next;
31030 return NULL;
31033 /* Return adjusted copy of EXPR:
31034 If it is empty DWARF expression, return it.
31035 If it is valid non-empty DWARF expression,
31036 return copy of EXPR with DW_OP_deref appended to it.
31037 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31038 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31039 If it is DWARF expression followed by DW_OP_stack_value, return
31040 copy of the DWARF expression without anything appended.
31041 Otherwise, return NULL. */
31043 static dw_loc_descr_ref
31044 copy_deref_exprloc (dw_loc_descr_ref expr)
31046 dw_loc_descr_ref tail = NULL;
31048 if (expr == NULL)
31049 return NULL;
31051 dw_loc_descr_ref l = non_dwarf_expression (expr);
31052 if (l && l->dw_loc_next)
31053 return NULL;
31055 if (l)
31057 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31058 tail = new_loc_descr ((enum dwarf_location_atom)
31059 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31060 0, 0);
31061 else
31062 switch (l->dw_loc_opc)
31064 case DW_OP_regx:
31065 tail = new_loc_descr (DW_OP_bregx,
31066 l->dw_loc_oprnd1.v.val_unsigned, 0);
31067 break;
31068 case DW_OP_stack_value:
31069 break;
31070 default:
31071 return NULL;
31074 else
31075 tail = new_loc_descr (DW_OP_deref, 0, 0);
31077 dw_loc_descr_ref ret = NULL, *p = &ret;
31078 while (expr != l)
31080 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31081 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31082 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31083 p = &(*p)->dw_loc_next;
31084 expr = expr->dw_loc_next;
31086 *p = tail;
31087 return ret;
31090 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31091 reference to a variable or argument, adjust it if needed and return:
31092 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31093 attribute if present should be removed
31094 0 keep the attribute perhaps with minor modifications, no need to rescan
31095 1 if the attribute has been successfully adjusted. */
31097 static int
31098 optimize_string_length (dw_attr_node *a)
31100 dw_loc_descr_ref l = AT_loc (a), lv;
31101 dw_die_ref die;
31102 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31104 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31105 die = lookup_decl_die (decl);
31106 if (die)
31108 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31109 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31110 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31112 else
31113 return -1;
31115 else
31116 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31118 /* DWARF5 allows reference class, so we can then reference the DIE.
31119 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31120 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31122 a->dw_attr_val.val_class = dw_val_class_die_ref;
31123 a->dw_attr_val.val_entry = NULL;
31124 a->dw_attr_val.v.val_die_ref.die = die;
31125 a->dw_attr_val.v.val_die_ref.external = 0;
31126 return 0;
31129 dw_attr_node *av = get_AT (die, DW_AT_location);
31130 dw_loc_list_ref d;
31131 bool non_dwarf_expr = false;
31133 if (av == NULL)
31134 return dwarf_strict ? -1 : 0;
31135 switch (AT_class (av))
31137 case dw_val_class_loc_list:
31138 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31139 if (d->expr && non_dwarf_expression (d->expr))
31140 non_dwarf_expr = true;
31141 break;
31142 case dw_val_class_view_list:
31143 gcc_unreachable ();
31144 case dw_val_class_loc:
31145 lv = AT_loc (av);
31146 if (lv == NULL)
31147 return dwarf_strict ? -1 : 0;
31148 if (non_dwarf_expression (lv))
31149 non_dwarf_expr = true;
31150 break;
31151 default:
31152 return dwarf_strict ? -1 : 0;
31155 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31156 into DW_OP_call4 or DW_OP_GNU_variable_value into
31157 DW_OP_call4 DW_OP_deref, do so. */
31158 if (!non_dwarf_expr
31159 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31161 l->dw_loc_opc = DW_OP_call4;
31162 if (l->dw_loc_next)
31163 l->dw_loc_next = NULL;
31164 else
31165 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31166 return 0;
31169 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31170 copy over the DW_AT_location attribute from die to a. */
31171 if (l->dw_loc_next != NULL)
31173 a->dw_attr_val = av->dw_attr_val;
31174 return 1;
31177 dw_loc_list_ref list, *p;
31178 switch (AT_class (av))
31180 case dw_val_class_loc_list:
31181 p = &list;
31182 list = NULL;
31183 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31185 lv = copy_deref_exprloc (d->expr);
31186 if (lv)
31188 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31189 p = &(*p)->dw_loc_next;
31191 else if (!dwarf_strict && d->expr)
31192 return 0;
31194 if (list == NULL)
31195 return dwarf_strict ? -1 : 0;
31196 a->dw_attr_val.val_class = dw_val_class_loc_list;
31197 gen_llsym (list);
31198 *AT_loc_list_ptr (a) = list;
31199 return 1;
31200 case dw_val_class_loc:
31201 lv = copy_deref_exprloc (AT_loc (av));
31202 if (lv == NULL)
31203 return dwarf_strict ? -1 : 0;
31204 a->dw_attr_val.v.val_loc = lv;
31205 return 1;
31206 default:
31207 gcc_unreachable ();
31211 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31212 an address in .rodata section if the string literal is emitted there,
31213 or remove the containing location list or replace DW_AT_const_value
31214 with DW_AT_location and empty location expression, if it isn't found
31215 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31216 to something that has been emitted in the current CU. */
31218 static void
31219 resolve_addr (dw_die_ref die)
31221 dw_die_ref c;
31222 dw_attr_node *a;
31223 dw_loc_list_ref *curr, *start, loc;
31224 unsigned ix;
31225 bool remove_AT_byte_size = false;
31227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31228 switch (AT_class (a))
31230 case dw_val_class_loc_list:
31231 start = curr = AT_loc_list_ptr (a);
31232 loc = *curr;
31233 gcc_assert (loc);
31234 /* The same list can be referenced more than once. See if we have
31235 already recorded the result from a previous pass. */
31236 if (loc->replaced)
31237 *curr = loc->dw_loc_next;
31238 else if (!loc->resolved_addr)
31240 /* As things stand, we do not expect or allow one die to
31241 reference a suffix of another die's location list chain.
31242 References must be identical or completely separate.
31243 There is therefore no need to cache the result of this
31244 pass on any list other than the first; doing so
31245 would lead to unnecessary writes. */
31246 while (*curr)
31248 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31249 if (!resolve_addr_in_expr (a, (*curr)->expr))
31251 dw_loc_list_ref next = (*curr)->dw_loc_next;
31252 dw_loc_descr_ref l = (*curr)->expr;
31254 if (next && (*curr)->ll_symbol)
31256 gcc_assert (!next->ll_symbol);
31257 next->ll_symbol = (*curr)->ll_symbol;
31258 next->vl_symbol = (*curr)->vl_symbol;
31260 if (dwarf_split_debug_info)
31261 remove_loc_list_addr_table_entries (l);
31262 *curr = next;
31264 else
31266 mark_base_types ((*curr)->expr);
31267 curr = &(*curr)->dw_loc_next;
31270 if (loc == *start)
31271 loc->resolved_addr = 1;
31272 else
31274 loc->replaced = 1;
31275 loc->dw_loc_next = *start;
31278 if (!*start)
31280 remove_AT (die, a->dw_attr);
31281 ix--;
31283 break;
31284 case dw_val_class_view_list:
31286 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31287 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31288 dw_val_node *llnode
31289 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31290 /* If we no longer have a loclist, or it no longer needs
31291 views, drop this attribute. */
31292 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31294 remove_AT (die, a->dw_attr);
31295 ix--;
31297 break;
31299 case dw_val_class_loc:
31301 dw_loc_descr_ref l = AT_loc (a);
31302 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31303 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31304 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31305 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31306 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31307 with DW_FORM_ref referencing the same DIE as
31308 DW_OP_GNU_variable_value used to reference. */
31309 if (a->dw_attr == DW_AT_string_length
31310 && l
31311 && l->dw_loc_opc == DW_OP_GNU_variable_value
31312 && (l->dw_loc_next == NULL
31313 || (l->dw_loc_next->dw_loc_next == NULL
31314 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31316 switch (optimize_string_length (a))
31318 case -1:
31319 remove_AT (die, a->dw_attr);
31320 ix--;
31321 /* If we drop DW_AT_string_length, we need to drop also
31322 DW_AT_{string_length_,}byte_size. */
31323 remove_AT_byte_size = true;
31324 continue;
31325 default:
31326 break;
31327 case 1:
31328 /* Even if we keep the optimized DW_AT_string_length,
31329 it might have changed AT_class, so process it again. */
31330 ix--;
31331 continue;
31334 /* For -gdwarf-2 don't attempt to optimize
31335 DW_AT_data_member_location containing
31336 DW_OP_plus_uconst - older consumers might
31337 rely on it being that op instead of a more complex,
31338 but shorter, location description. */
31339 if ((dwarf_version > 2
31340 || a->dw_attr != DW_AT_data_member_location
31341 || l == NULL
31342 || l->dw_loc_opc != DW_OP_plus_uconst
31343 || l->dw_loc_next != NULL)
31344 && !resolve_addr_in_expr (a, l))
31346 if (dwarf_split_debug_info)
31347 remove_loc_list_addr_table_entries (l);
31348 if (l != NULL
31349 && l->dw_loc_next == NULL
31350 && l->dw_loc_opc == DW_OP_addr
31351 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31352 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31353 && a->dw_attr == DW_AT_location)
31355 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31356 remove_AT (die, a->dw_attr);
31357 ix--;
31358 optimize_location_into_implicit_ptr (die, decl);
31359 break;
31361 if (a->dw_attr == DW_AT_string_length)
31362 /* If we drop DW_AT_string_length, we need to drop also
31363 DW_AT_{string_length_,}byte_size. */
31364 remove_AT_byte_size = true;
31365 remove_AT (die, a->dw_attr);
31366 ix--;
31368 else
31369 mark_base_types (l);
31371 break;
31372 case dw_val_class_addr:
31373 if (a->dw_attr == DW_AT_const_value
31374 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31376 if (AT_index (a) != NOT_INDEXED)
31377 remove_addr_table_entry (a->dw_attr_val.val_entry);
31378 remove_AT (die, a->dw_attr);
31379 ix--;
31381 if ((die->die_tag == DW_TAG_call_site
31382 && a->dw_attr == DW_AT_call_origin)
31383 || (die->die_tag == DW_TAG_GNU_call_site
31384 && a->dw_attr == DW_AT_abstract_origin))
31386 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31387 dw_die_ref tdie = lookup_decl_die (tdecl);
31388 dw_die_ref cdie;
31389 if (tdie == NULL
31390 && DECL_EXTERNAL (tdecl)
31391 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31392 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31394 dw_die_ref pdie = cdie;
31395 /* Make sure we don't add these DIEs into type units.
31396 We could emit skeleton DIEs for context (namespaces,
31397 outer structs/classes) and a skeleton DIE for the
31398 innermost context with DW_AT_signature pointing to the
31399 type unit. See PR78835. */
31400 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31401 pdie = pdie->die_parent;
31402 if (pdie == NULL)
31404 /* Creating a full DIE for tdecl is overly expensive and
31405 at this point even wrong when in the LTO phase
31406 as it can end up generating new type DIEs we didn't
31407 output and thus optimize_external_refs will crash. */
31408 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31409 add_AT_flag (tdie, DW_AT_external, 1);
31410 add_AT_flag (tdie, DW_AT_declaration, 1);
31411 add_linkage_attr (tdie, tdecl);
31412 add_name_and_src_coords_attributes (tdie, tdecl, true);
31413 equate_decl_number_to_die (tdecl, tdie);
31416 if (tdie)
31418 a->dw_attr_val.val_class = dw_val_class_die_ref;
31419 a->dw_attr_val.v.val_die_ref.die = tdie;
31420 a->dw_attr_val.v.val_die_ref.external = 0;
31422 else
31424 if (AT_index (a) != NOT_INDEXED)
31425 remove_addr_table_entry (a->dw_attr_val.val_entry);
31426 remove_AT (die, a->dw_attr);
31427 ix--;
31430 break;
31431 default:
31432 break;
31435 if (remove_AT_byte_size)
31436 remove_AT (die, dwarf_version >= 5
31437 ? DW_AT_string_length_byte_size
31438 : DW_AT_byte_size);
31440 FOR_EACH_CHILD (die, c, resolve_addr (c));
31443 /* Helper routines for optimize_location_lists.
31444 This pass tries to share identical local lists in .debug_loc
31445 section. */
31447 /* Iteratively hash operands of LOC opcode into HSTATE. */
31449 static void
31450 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31452 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31453 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31455 switch (loc->dw_loc_opc)
31457 case DW_OP_const4u:
31458 case DW_OP_const8u:
31459 if (loc->dtprel)
31460 goto hash_addr;
31461 /* FALLTHRU */
31462 case DW_OP_const1u:
31463 case DW_OP_const1s:
31464 case DW_OP_const2u:
31465 case DW_OP_const2s:
31466 case DW_OP_const4s:
31467 case DW_OP_const8s:
31468 case DW_OP_constu:
31469 case DW_OP_consts:
31470 case DW_OP_pick:
31471 case DW_OP_plus_uconst:
31472 case DW_OP_breg0:
31473 case DW_OP_breg1:
31474 case DW_OP_breg2:
31475 case DW_OP_breg3:
31476 case DW_OP_breg4:
31477 case DW_OP_breg5:
31478 case DW_OP_breg6:
31479 case DW_OP_breg7:
31480 case DW_OP_breg8:
31481 case DW_OP_breg9:
31482 case DW_OP_breg10:
31483 case DW_OP_breg11:
31484 case DW_OP_breg12:
31485 case DW_OP_breg13:
31486 case DW_OP_breg14:
31487 case DW_OP_breg15:
31488 case DW_OP_breg16:
31489 case DW_OP_breg17:
31490 case DW_OP_breg18:
31491 case DW_OP_breg19:
31492 case DW_OP_breg20:
31493 case DW_OP_breg21:
31494 case DW_OP_breg22:
31495 case DW_OP_breg23:
31496 case DW_OP_breg24:
31497 case DW_OP_breg25:
31498 case DW_OP_breg26:
31499 case DW_OP_breg27:
31500 case DW_OP_breg28:
31501 case DW_OP_breg29:
31502 case DW_OP_breg30:
31503 case DW_OP_breg31:
31504 case DW_OP_regx:
31505 case DW_OP_fbreg:
31506 case DW_OP_piece:
31507 case DW_OP_deref_size:
31508 case DW_OP_xderef_size:
31509 hstate.add_object (val1->v.val_int);
31510 break;
31511 case DW_OP_skip:
31512 case DW_OP_bra:
31514 int offset;
31516 gcc_assert (val1->val_class == dw_val_class_loc);
31517 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31518 hstate.add_object (offset);
31520 break;
31521 case DW_OP_implicit_value:
31522 hstate.add_object (val1->v.val_unsigned);
31523 switch (val2->val_class)
31525 case dw_val_class_const:
31526 hstate.add_object (val2->v.val_int);
31527 break;
31528 case dw_val_class_vec:
31530 unsigned int elt_size = val2->v.val_vec.elt_size;
31531 unsigned int len = val2->v.val_vec.length;
31533 hstate.add_int (elt_size);
31534 hstate.add_int (len);
31535 hstate.add (val2->v.val_vec.array, len * elt_size);
31537 break;
31538 case dw_val_class_const_double:
31539 hstate.add_object (val2->v.val_double.low);
31540 hstate.add_object (val2->v.val_double.high);
31541 break;
31542 case dw_val_class_wide_int:
31543 hstate.add (val2->v.val_wide->get_val (),
31544 get_full_len (*val2->v.val_wide)
31545 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31546 break;
31547 case dw_val_class_addr:
31548 inchash::add_rtx (val2->v.val_addr, hstate);
31549 break;
31550 default:
31551 gcc_unreachable ();
31553 break;
31554 case DW_OP_bregx:
31555 case DW_OP_bit_piece:
31556 hstate.add_object (val1->v.val_int);
31557 hstate.add_object (val2->v.val_int);
31558 break;
31559 case DW_OP_addr:
31560 hash_addr:
31561 if (loc->dtprel)
31563 unsigned char dtprel = 0xd1;
31564 hstate.add_object (dtprel);
31566 inchash::add_rtx (val1->v.val_addr, hstate);
31567 break;
31568 case DW_OP_GNU_addr_index:
31569 case DW_OP_addrx:
31570 case DW_OP_GNU_const_index:
31571 case DW_OP_constx:
31573 if (loc->dtprel)
31575 unsigned char dtprel = 0xd1;
31576 hstate.add_object (dtprel);
31578 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31580 break;
31581 case DW_OP_implicit_pointer:
31582 case DW_OP_GNU_implicit_pointer:
31583 hstate.add_int (val2->v.val_int);
31584 break;
31585 case DW_OP_entry_value:
31586 case DW_OP_GNU_entry_value:
31587 hstate.add_object (val1->v.val_loc);
31588 break;
31589 case DW_OP_regval_type:
31590 case DW_OP_deref_type:
31591 case DW_OP_GNU_regval_type:
31592 case DW_OP_GNU_deref_type:
31594 unsigned int byte_size
31595 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31596 unsigned int encoding
31597 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31598 hstate.add_object (val1->v.val_int);
31599 hstate.add_object (byte_size);
31600 hstate.add_object (encoding);
31602 break;
31603 case DW_OP_convert:
31604 case DW_OP_reinterpret:
31605 case DW_OP_GNU_convert:
31606 case DW_OP_GNU_reinterpret:
31607 if (val1->val_class == dw_val_class_unsigned_const)
31609 hstate.add_object (val1->v.val_unsigned);
31610 break;
31612 /* FALLTHRU */
31613 case DW_OP_const_type:
31614 case DW_OP_GNU_const_type:
31616 unsigned int byte_size
31617 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31618 unsigned int encoding
31619 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31620 hstate.add_object (byte_size);
31621 hstate.add_object (encoding);
31622 if (loc->dw_loc_opc != DW_OP_const_type
31623 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31624 break;
31625 hstate.add_object (val2->val_class);
31626 switch (val2->val_class)
31628 case dw_val_class_const:
31629 hstate.add_object (val2->v.val_int);
31630 break;
31631 case dw_val_class_vec:
31633 unsigned int elt_size = val2->v.val_vec.elt_size;
31634 unsigned int len = val2->v.val_vec.length;
31636 hstate.add_object (elt_size);
31637 hstate.add_object (len);
31638 hstate.add (val2->v.val_vec.array, len * elt_size);
31640 break;
31641 case dw_val_class_const_double:
31642 hstate.add_object (val2->v.val_double.low);
31643 hstate.add_object (val2->v.val_double.high);
31644 break;
31645 case dw_val_class_wide_int:
31646 hstate.add (val2->v.val_wide->get_val (),
31647 get_full_len (*val2->v.val_wide)
31648 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31649 break;
31650 default:
31651 gcc_unreachable ();
31654 break;
31656 default:
31657 /* Other codes have no operands. */
31658 break;
31662 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31664 static inline void
31665 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31667 dw_loc_descr_ref l;
31668 bool sizes_computed = false;
31669 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31670 size_of_locs (loc);
31672 for (l = loc; l != NULL; l = l->dw_loc_next)
31674 enum dwarf_location_atom opc = l->dw_loc_opc;
31675 hstate.add_object (opc);
31676 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31678 size_of_locs (loc);
31679 sizes_computed = true;
31681 hash_loc_operands (l, hstate);
31685 /* Compute hash of the whole location list LIST_HEAD. */
31687 static inline void
31688 hash_loc_list (dw_loc_list_ref list_head)
31690 dw_loc_list_ref curr = list_head;
31691 inchash::hash hstate;
31693 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31695 hstate.add (curr->begin, strlen (curr->begin) + 1);
31696 hstate.add (curr->end, strlen (curr->end) + 1);
31697 hstate.add_object (curr->vbegin);
31698 hstate.add_object (curr->vend);
31699 if (curr->section)
31700 hstate.add (curr->section, strlen (curr->section) + 1);
31701 hash_locs (curr->expr, hstate);
31703 list_head->hash = hstate.end ();
31706 /* Return true if X and Y opcodes have the same operands. */
31708 static inline bool
31709 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31711 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31712 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31713 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31714 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31716 switch (x->dw_loc_opc)
31718 case DW_OP_const4u:
31719 case DW_OP_const8u:
31720 if (x->dtprel)
31721 goto hash_addr;
31722 /* FALLTHRU */
31723 case DW_OP_const1u:
31724 case DW_OP_const1s:
31725 case DW_OP_const2u:
31726 case DW_OP_const2s:
31727 case DW_OP_const4s:
31728 case DW_OP_const8s:
31729 case DW_OP_constu:
31730 case DW_OP_consts:
31731 case DW_OP_pick:
31732 case DW_OP_plus_uconst:
31733 case DW_OP_breg0:
31734 case DW_OP_breg1:
31735 case DW_OP_breg2:
31736 case DW_OP_breg3:
31737 case DW_OP_breg4:
31738 case DW_OP_breg5:
31739 case DW_OP_breg6:
31740 case DW_OP_breg7:
31741 case DW_OP_breg8:
31742 case DW_OP_breg9:
31743 case DW_OP_breg10:
31744 case DW_OP_breg11:
31745 case DW_OP_breg12:
31746 case DW_OP_breg13:
31747 case DW_OP_breg14:
31748 case DW_OP_breg15:
31749 case DW_OP_breg16:
31750 case DW_OP_breg17:
31751 case DW_OP_breg18:
31752 case DW_OP_breg19:
31753 case DW_OP_breg20:
31754 case DW_OP_breg21:
31755 case DW_OP_breg22:
31756 case DW_OP_breg23:
31757 case DW_OP_breg24:
31758 case DW_OP_breg25:
31759 case DW_OP_breg26:
31760 case DW_OP_breg27:
31761 case DW_OP_breg28:
31762 case DW_OP_breg29:
31763 case DW_OP_breg30:
31764 case DW_OP_breg31:
31765 case DW_OP_regx:
31766 case DW_OP_fbreg:
31767 case DW_OP_piece:
31768 case DW_OP_deref_size:
31769 case DW_OP_xderef_size:
31770 return valx1->v.val_int == valy1->v.val_int;
31771 case DW_OP_skip:
31772 case DW_OP_bra:
31773 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31774 can cause irrelevant differences in dw_loc_addr. */
31775 gcc_assert (valx1->val_class == dw_val_class_loc
31776 && valy1->val_class == dw_val_class_loc
31777 && (dwarf_split_debug_info
31778 || x->dw_loc_addr == y->dw_loc_addr));
31779 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31780 case DW_OP_implicit_value:
31781 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31782 || valx2->val_class != valy2->val_class)
31783 return false;
31784 switch (valx2->val_class)
31786 case dw_val_class_const:
31787 return valx2->v.val_int == valy2->v.val_int;
31788 case dw_val_class_vec:
31789 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31790 && valx2->v.val_vec.length == valy2->v.val_vec.length
31791 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31792 valx2->v.val_vec.elt_size
31793 * valx2->v.val_vec.length) == 0;
31794 case dw_val_class_const_double:
31795 return valx2->v.val_double.low == valy2->v.val_double.low
31796 && valx2->v.val_double.high == valy2->v.val_double.high;
31797 case dw_val_class_wide_int:
31798 return *valx2->v.val_wide == *valy2->v.val_wide;
31799 case dw_val_class_addr:
31800 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31801 default:
31802 gcc_unreachable ();
31804 case DW_OP_bregx:
31805 case DW_OP_bit_piece:
31806 return valx1->v.val_int == valy1->v.val_int
31807 && valx2->v.val_int == valy2->v.val_int;
31808 case DW_OP_addr:
31809 hash_addr:
31810 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31811 case DW_OP_GNU_addr_index:
31812 case DW_OP_addrx:
31813 case DW_OP_GNU_const_index:
31814 case DW_OP_constx:
31816 rtx ax1 = valx1->val_entry->addr.rtl;
31817 rtx ay1 = valy1->val_entry->addr.rtl;
31818 return rtx_equal_p (ax1, ay1);
31820 case DW_OP_implicit_pointer:
31821 case DW_OP_GNU_implicit_pointer:
31822 return valx1->val_class == dw_val_class_die_ref
31823 && valx1->val_class == valy1->val_class
31824 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31825 && valx2->v.val_int == valy2->v.val_int;
31826 case DW_OP_entry_value:
31827 case DW_OP_GNU_entry_value:
31828 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31829 case DW_OP_const_type:
31830 case DW_OP_GNU_const_type:
31831 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31832 || valx2->val_class != valy2->val_class)
31833 return false;
31834 switch (valx2->val_class)
31836 case dw_val_class_const:
31837 return valx2->v.val_int == valy2->v.val_int;
31838 case dw_val_class_vec:
31839 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31840 && valx2->v.val_vec.length == valy2->v.val_vec.length
31841 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31842 valx2->v.val_vec.elt_size
31843 * valx2->v.val_vec.length) == 0;
31844 case dw_val_class_const_double:
31845 return valx2->v.val_double.low == valy2->v.val_double.low
31846 && valx2->v.val_double.high == valy2->v.val_double.high;
31847 case dw_val_class_wide_int:
31848 return *valx2->v.val_wide == *valy2->v.val_wide;
31849 default:
31850 gcc_unreachable ();
31852 case DW_OP_regval_type:
31853 case DW_OP_deref_type:
31854 case DW_OP_GNU_regval_type:
31855 case DW_OP_GNU_deref_type:
31856 return valx1->v.val_int == valy1->v.val_int
31857 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31858 case DW_OP_convert:
31859 case DW_OP_reinterpret:
31860 case DW_OP_GNU_convert:
31861 case DW_OP_GNU_reinterpret:
31862 if (valx1->val_class != valy1->val_class)
31863 return false;
31864 if (valx1->val_class == dw_val_class_unsigned_const)
31865 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31866 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31867 case DW_OP_GNU_parameter_ref:
31868 return valx1->val_class == dw_val_class_die_ref
31869 && valx1->val_class == valy1->val_class
31870 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31871 default:
31872 /* Other codes have no operands. */
31873 return true;
31877 /* Return true if DWARF location expressions X and Y are the same. */
31879 static inline bool
31880 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31882 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31883 if (x->dw_loc_opc != y->dw_loc_opc
31884 || x->dtprel != y->dtprel
31885 || !compare_loc_operands (x, y))
31886 break;
31887 return x == NULL && y == NULL;
31890 /* Hashtable helpers. */
31892 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31894 static inline hashval_t hash (const dw_loc_list_struct *);
31895 static inline bool equal (const dw_loc_list_struct *,
31896 const dw_loc_list_struct *);
31899 /* Return precomputed hash of location list X. */
31901 inline hashval_t
31902 loc_list_hasher::hash (const dw_loc_list_struct *x)
31904 return x->hash;
31907 /* Return true if location lists A and B are the same. */
31909 inline bool
31910 loc_list_hasher::equal (const dw_loc_list_struct *a,
31911 const dw_loc_list_struct *b)
31913 if (a == b)
31914 return 1;
31915 if (a->hash != b->hash)
31916 return 0;
31917 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31918 if (strcmp (a->begin, b->begin) != 0
31919 || strcmp (a->end, b->end) != 0
31920 || (a->section == NULL) != (b->section == NULL)
31921 || (a->section && strcmp (a->section, b->section) != 0)
31922 || a->vbegin != b->vbegin || a->vend != b->vend
31923 || !compare_locs (a->expr, b->expr))
31924 break;
31925 return a == NULL && b == NULL;
31928 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31931 /* Recursively optimize location lists referenced from DIE
31932 children and share them whenever possible. */
31934 static void
31935 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31937 dw_die_ref c;
31938 dw_attr_node *a;
31939 unsigned ix;
31940 dw_loc_list_struct **slot;
31941 bool drop_locviews = false;
31942 bool has_locviews = false;
31944 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31945 if (AT_class (a) == dw_val_class_loc_list)
31947 dw_loc_list_ref list = AT_loc_list (a);
31948 /* TODO: perform some optimizations here, before hashing
31949 it and storing into the hash table. */
31950 hash_loc_list (list);
31951 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31952 if (*slot == NULL)
31954 *slot = list;
31955 if (loc_list_has_views (list))
31956 gcc_assert (list->vl_symbol);
31957 else if (list->vl_symbol)
31959 drop_locviews = true;
31960 list->vl_symbol = NULL;
31963 else
31965 if (list->vl_symbol && !(*slot)->vl_symbol)
31966 drop_locviews = true;
31967 a->dw_attr_val.v.val_loc_list = *slot;
31970 else if (AT_class (a) == dw_val_class_view_list)
31972 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31973 has_locviews = true;
31977 if (drop_locviews && has_locviews)
31978 remove_AT (die, DW_AT_GNU_locviews);
31980 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31984 /* Recursively assign each location list a unique index into the debug_addr
31985 section. */
31987 static void
31988 index_location_lists (dw_die_ref die)
31990 dw_die_ref c;
31991 dw_attr_node *a;
31992 unsigned ix;
31994 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31995 if (AT_class (a) == dw_val_class_loc_list)
31997 dw_loc_list_ref list = AT_loc_list (a);
31998 dw_loc_list_ref curr;
31999 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
32001 /* Don't index an entry that has already been indexed
32002 or won't be output. Make sure skip_loc_list_entry doesn't
32003 call size_of_locs, because that might cause circular dependency,
32004 index_location_lists requiring address table indexes to be
32005 computed, but adding new indexes through add_addr_table_entry
32006 and address table index computation requiring no new additions
32007 to the hash table. In the rare case of DWARF[234] >= 64KB
32008 location expression, we'll just waste unused address table entry
32009 for it. */
32010 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
32011 continue;
32013 curr->begin_entry
32014 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
32015 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
32016 curr->end_entry
32017 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32021 FOR_EACH_CHILD (die, c, index_location_lists (c));
32024 /* Optimize location lists referenced from DIE
32025 children and share them whenever possible. */
32027 static void
32028 optimize_location_lists (dw_die_ref die)
32030 loc_list_hash_type htab (500);
32031 optimize_location_lists_1 (die, &htab);
32034 /* Traverse the limbo die list, and add parent/child links. The only
32035 dies without parents that should be here are concrete instances of
32036 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32037 For concrete instances, we can get the parent die from the abstract
32038 instance. */
32040 static void
32041 flush_limbo_die_list (void)
32043 limbo_die_node *node;
32045 /* get_context_die calls force_decl_die, which can put new DIEs on the
32046 limbo list in LTO mode when nested functions are put in a different
32047 partition than that of their parent function. */
32048 while ((node = limbo_die_list))
32050 dw_die_ref die = node->die;
32051 limbo_die_list = node->next;
32053 if (die->die_parent == NULL)
32055 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32057 if (origin && origin->die_parent)
32058 add_child_die (origin->die_parent, die);
32059 else if (is_cu_die (die))
32061 else if (seen_error ())
32062 /* It's OK to be confused by errors in the input. */
32063 add_child_die (comp_unit_die (), die);
32064 else
32066 /* In certain situations, the lexical block containing a
32067 nested function can be optimized away, which results
32068 in the nested function die being orphaned. Likewise
32069 with the return type of that nested function. Force
32070 this to be a child of the containing function.
32072 It may happen that even the containing function got fully
32073 inlined and optimized out. In that case we are lost and
32074 assign the empty child. This should not be big issue as
32075 the function is likely unreachable too. */
32076 gcc_assert (node->created_for);
32078 if (DECL_P (node->created_for))
32079 origin = get_context_die (DECL_CONTEXT (node->created_for));
32080 else if (TYPE_P (node->created_for))
32081 origin = scope_die_for (node->created_for, comp_unit_die ());
32082 else
32083 origin = comp_unit_die ();
32085 add_child_die (origin, die);
32091 /* Reset DIEs so we can output them again. */
32093 static void
32094 reset_dies (dw_die_ref die)
32096 dw_die_ref c;
32098 /* Remove stuff we re-generate. */
32099 die->die_mark = 0;
32100 die->die_offset = 0;
32101 die->die_abbrev = 0;
32102 remove_AT (die, DW_AT_sibling);
32104 FOR_EACH_CHILD (die, c, reset_dies (c));
32107 /* reset_indirect_string removed the references coming from DW_AT_name
32108 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32109 .debug_line_str strings again. */
32111 static void
32112 adjust_name_comp_dir (dw_die_ref die)
32114 for (int i = 0; i < 2; i++)
32116 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32117 dw_attr_node *a = get_AT (die, attr_kind);
32118 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32119 continue;
32121 if (!debug_line_str_hash)
32122 debug_line_str_hash
32123 = hash_table<indirect_string_hasher>::create_ggc (10);
32125 struct indirect_string_node *node
32126 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32127 debug_line_str_hash);
32128 set_indirect_string (node);
32129 node->form = DW_FORM_line_strp;
32130 a->dw_attr_val.v.val_str = node;
32134 /* Output stuff that dwarf requires at the end of every file,
32135 and generate the DWARF-2 debugging info. */
32137 static void
32138 dwarf2out_finish (const char *filename)
32140 comdat_type_node *ctnode;
32141 dw_die_ref main_comp_unit_die;
32142 unsigned char checksum[16];
32143 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32145 /* Generate CTF/BTF debug info. */
32146 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32147 || btf_debuginfo_p ()) && lang_GNU_C ())
32148 ctf_debug_finish (filename);
32150 /* Skip emitting DWARF if not required. */
32151 if (!dwarf_debuginfo_p ())
32152 return;
32154 /* Flush out any latecomers to the limbo party. */
32155 flush_limbo_die_list ();
32157 if (inline_entry_data_table)
32158 gcc_assert (inline_entry_data_table->is_empty ());
32160 if (flag_checking)
32162 verify_die (comp_unit_die ());
32163 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32164 verify_die (node->die);
32167 /* We shouldn't have any symbols with delayed asm names for
32168 DIEs generated after early finish. */
32169 gcc_assert (deferred_asm_name == NULL);
32171 gen_remaining_tmpl_value_param_die_attribute ();
32173 if (flag_generate_lto || flag_generate_offload)
32175 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32177 /* Prune stuff so that dwarf2out_finish runs successfully
32178 for the fat part of the object. */
32179 reset_dies (comp_unit_die ());
32180 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32181 reset_dies (node->die);
32183 hash_table<comdat_type_hasher> comdat_type_table (100);
32184 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32186 comdat_type_node **slot
32187 = comdat_type_table.find_slot (ctnode, INSERT);
32189 /* Don't reset types twice. */
32190 if (*slot != HTAB_EMPTY_ENTRY)
32191 continue;
32193 /* Remove the pointer to the line table. */
32194 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32196 if (debug_info_level >= DINFO_LEVEL_TERSE)
32197 reset_dies (ctnode->root_die);
32199 *slot = ctnode;
32202 /* Reset die CU symbol so we don't output it twice. */
32203 comp_unit_die ()->die_id.die_symbol = NULL;
32205 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32206 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32207 if (have_macinfo)
32208 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32210 /* Remove indirect string decisions. */
32211 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32212 if (debug_line_str_hash)
32214 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32215 debug_line_str_hash = NULL;
32216 if (asm_outputs_debug_line_str ())
32218 adjust_name_comp_dir (comp_unit_die ());
32219 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32220 adjust_name_comp_dir (node->die);
32225 #if ENABLE_ASSERT_CHECKING
32227 dw_die_ref die = comp_unit_die (), c;
32228 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32230 #endif
32231 base_types.truncate (0);
32232 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32233 resolve_addr (ctnode->root_die);
32234 resolve_addr (comp_unit_die ());
32235 move_marked_base_types ();
32237 if (dump_file)
32239 fprintf (dump_file, "DWARF for %s\n", filename);
32240 print_die (comp_unit_die (), dump_file);
32243 /* Initialize sections and labels used for actual assembler output. */
32244 unsigned generation = init_sections_and_labels (false);
32246 /* Traverse the DIE's and add sibling attributes to those DIE's that
32247 have children. */
32248 add_sibling_attributes (comp_unit_die ());
32249 limbo_die_node *node;
32250 for (node = cu_die_list; node; node = node->next)
32251 add_sibling_attributes (node->die);
32252 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32253 add_sibling_attributes (ctnode->root_die);
32255 /* When splitting DWARF info, we put some attributes in the
32256 skeleton compile_unit DIE that remains in the .o, while
32257 most attributes go in the DWO compile_unit_die. */
32258 if (dwarf_split_debug_info)
32260 limbo_die_node *cu;
32261 main_comp_unit_die = gen_compile_unit_die (NULL);
32262 if (dwarf_version >= 5)
32263 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32264 cu = limbo_die_list;
32265 gcc_assert (cu->die == main_comp_unit_die);
32266 limbo_die_list = limbo_die_list->next;
32267 cu->next = cu_die_list;
32268 cu_die_list = cu;
32270 else
32271 main_comp_unit_die = comp_unit_die ();
32273 /* Output a terminator label for the .text section. */
32274 switch_to_section (text_section);
32275 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32276 if (cold_text_section)
32278 switch_to_section (cold_text_section);
32279 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32282 /* We can only use the low/high_pc attributes if all of the code was
32283 in .text. */
32284 if ((!have_multiple_function_sections
32285 && vec_safe_length (switch_text_ranges) < 2)
32286 || (dwarf_version < 3 && dwarf_strict))
32288 const char *end_label = text_end_label;
32289 if (vec_safe_length (switch_text_ranges) == 1)
32290 end_label = (*switch_text_ranges)[0];
32291 /* Don't add if the CU has no associated code. */
32292 if (switch_text_ranges)
32293 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32294 end_label, true);
32296 else
32298 unsigned fde_idx;
32299 dw_fde_ref fde;
32300 bool range_list_added = false;
32301 if (switch_text_ranges)
32303 const char *prev_loc = text_section_label;
32304 const char *loc;
32305 unsigned idx;
32307 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32308 if (prev_loc)
32310 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32311 loc, &range_list_added, true);
32312 prev_loc = NULL;
32314 else
32315 prev_loc = loc;
32317 if (prev_loc)
32318 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32319 text_end_label, &range_list_added, true);
32322 if (switch_cold_ranges)
32324 const char *prev_loc = cold_text_section_label;
32325 const char *loc;
32326 unsigned idx;
32328 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32329 if (prev_loc)
32331 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32332 loc, &range_list_added, true);
32333 prev_loc = NULL;
32335 else
32336 prev_loc = loc;
32338 if (prev_loc)
32339 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32340 cold_end_label, &range_list_added, true);
32343 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32345 if (fde->ignored_debug)
32346 continue;
32347 if (!fde->in_std_section)
32348 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32349 fde->dw_fde_end, &range_list_added,
32350 true);
32351 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32352 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32353 fde->dw_fde_second_end, &range_list_added,
32354 true);
32357 if (range_list_added)
32359 /* We need to give .debug_loc and .debug_ranges an appropriate
32360 "base address". Use zero so that these addresses become
32361 absolute. Historically, we've emitted the unexpected
32362 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32363 Emit both to give time for other tools to adapt. */
32364 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32365 if (! dwarf_strict && dwarf_version < 4)
32366 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32368 add_ranges (NULL);
32369 have_multiple_function_sections = true;
32373 /* AIX Assembler inserts the length, so adjust the reference to match the
32374 offset expected by debuggers. */
32375 strcpy (dl_section_ref, debug_line_section_label);
32376 if (XCOFF_DEBUGGING_INFO)
32377 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32379 if (debug_info_level >= DINFO_LEVEL_TERSE)
32380 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32381 dl_section_ref);
32383 if (have_macinfo)
32384 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32385 macinfo_section_label);
32387 if (dwarf_split_debug_info)
32389 if (have_location_lists)
32391 /* Since we generate the loclists in the split DWARF .dwo
32392 file itself, we don't need to generate a loclists_base
32393 attribute for the split compile unit DIE. That attribute
32394 (and using relocatable sec_offset FORMs) isn't allowed
32395 for a split compile unit. Only if the .debug_loclists
32396 section was in the main file, would we need to generate a
32397 loclists_base attribute here (for the full or skeleton
32398 unit DIE). */
32400 /* optimize_location_lists calculates the size of the lists,
32401 so index them first, and assign indices to the entries.
32402 Although optimize_location_lists will remove entries from
32403 the table, it only does so for duplicates, and therefore
32404 only reduces ref_counts to 1. */
32405 index_location_lists (comp_unit_die ());
32408 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32409 index_rnglists ();
32411 if (addr_index_table != NULL)
32413 unsigned int index = 0;
32414 addr_index_table
32415 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32416 (&index);
32420 loc_list_idx = 0;
32421 if (have_location_lists)
32423 optimize_location_lists (comp_unit_die ());
32424 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32425 if (dwarf_version >= 5 && dwarf_split_debug_info)
32426 assign_location_list_indexes (comp_unit_die ());
32429 save_macinfo_strings ();
32431 if (dwarf_split_debug_info)
32433 unsigned int index = 0;
32435 /* Add attributes common to skeleton compile_units and
32436 type_units. Because these attributes include strings, it
32437 must be done before freezing the string table. Top-level
32438 skeleton die attrs are added when the skeleton type unit is
32439 created, so ensure it is created by this point. */
32440 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32441 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32444 /* Output all of the compilation units. We put the main one last so that
32445 the offsets are available to output_pubnames. */
32446 for (node = cu_die_list; node; node = node->next)
32447 output_comp_unit (node->die, 0, NULL);
32449 hash_table<comdat_type_hasher> comdat_type_table (100);
32450 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32452 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32454 /* Don't output duplicate types. */
32455 if (*slot != HTAB_EMPTY_ENTRY)
32456 continue;
32458 /* Add a pointer to the line table for the main compilation unit
32459 so that the debugger can make sense of DW_AT_decl_file
32460 attributes. */
32461 if (debug_info_level >= DINFO_LEVEL_TERSE)
32462 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32463 (!dwarf_split_debug_info
32464 ? dl_section_ref
32465 : debug_skeleton_line_section_label));
32467 output_comdat_type_unit (ctnode, false);
32468 *slot = ctnode;
32471 if (dwarf_split_debug_info)
32473 int mark;
32474 struct md5_ctx ctx;
32476 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32477 md5_init_ctx (&ctx);
32478 mark = 0;
32479 die_checksum (comp_unit_die (), &ctx, &mark);
32480 unmark_all_dies (comp_unit_die ());
32481 md5_finish_ctx (&ctx, checksum);
32483 if (dwarf_version < 5)
32485 /* Use the first 8 bytes of the checksum as the dwo_id,
32486 and add it to both comp-unit DIEs. */
32487 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32488 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32491 /* Add the base offset of the ranges table to the skeleton
32492 comp-unit DIE. */
32493 if (!vec_safe_is_empty (ranges_table))
32495 if (dwarf_version < 5)
32496 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32497 ranges_section_label);
32500 output_addr_table ();
32503 /* Output the main compilation unit if non-empty or if .debug_macinfo
32504 or .debug_macro will be emitted. */
32505 output_comp_unit (comp_unit_die (), have_macinfo,
32506 dwarf_split_debug_info ? checksum : NULL);
32508 if (dwarf_split_debug_info && info_section_emitted)
32509 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32511 /* Output the abbreviation table. */
32512 if (vec_safe_length (abbrev_die_table) != 1)
32514 switch_to_section (debug_abbrev_section);
32515 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32516 output_abbrev_section ();
32519 /* Output location list section if necessary. */
32520 if (have_location_lists)
32522 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32523 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32524 /* Output the location lists info. */
32525 switch_to_section (debug_loc_section);
32526 if (dwarf_version >= 5)
32528 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32529 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32530 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32531 dw2_asm_output_data (4, 0xffffffff,
32532 "Initial length escape value indicating "
32533 "64-bit DWARF extension");
32534 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32535 "Length of Location Lists");
32536 ASM_OUTPUT_LABEL (asm_out_file, l1);
32537 output_dwarf_version ();
32538 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32539 dw2_asm_output_data (1, 0, "Segment Size");
32540 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32541 "Offset Entry Count");
32543 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32544 if (dwarf_version >= 5 && dwarf_split_debug_info)
32546 unsigned int save_loc_list_idx = loc_list_idx;
32547 loc_list_idx = 0;
32548 output_loclists_offsets (comp_unit_die ());
32549 gcc_assert (save_loc_list_idx == loc_list_idx);
32551 output_location_lists (comp_unit_die ());
32552 if (dwarf_version >= 5)
32553 ASM_OUTPUT_LABEL (asm_out_file, l2);
32556 output_pubtables ();
32558 /* Output the address range information if a CU (.debug_info section)
32559 was emitted. We output an empty table even if we had no functions
32560 to put in it. This because the consumer has no way to tell the
32561 difference between an empty table that we omitted and failure to
32562 generate a table that would have contained data. */
32563 if (info_section_emitted)
32565 switch_to_section (debug_aranges_section);
32566 output_aranges ();
32569 /* Output ranges section if necessary. */
32570 if (!vec_safe_is_empty (ranges_table))
32572 if (dwarf_version >= 5)
32574 if (dwarf_split_debug_info)
32576 /* We don't know right now whether there are any
32577 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32578 Depending on into which of those two belongs the first
32579 ranges_table entry, emit that section first and that
32580 output_rnglists call will return true if the other kind of
32581 ranges needs to be emitted as well. */
32582 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32583 if (output_rnglists (generation, dwo))
32584 output_rnglists (generation, !dwo);
32586 else
32587 output_rnglists (generation, false);
32589 else
32590 output_ranges ();
32593 /* Have to end the macro section. */
32594 if (have_macinfo)
32596 switch_to_section (debug_macinfo_section);
32597 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32598 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32599 : debug_skeleton_line_section_label, false);
32600 dw2_asm_output_data (1, 0, "End compilation unit");
32603 /* Output the source line correspondence table. We must do this
32604 even if there is no line information. Otherwise, on an empty
32605 translation unit, we will generate a present, but empty,
32606 .debug_info section. IRIX 6.5 `nm' will then complain when
32607 examining the file. This is done late so that any filenames
32608 used by the debug_info section are marked as 'used'. */
32609 switch_to_section (debug_line_section);
32610 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32611 if (! output_asm_line_debug_info ())
32612 output_line_info (false);
32614 if (dwarf_split_debug_info && info_section_emitted)
32616 switch_to_section (debug_skeleton_line_section);
32617 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32618 output_line_info (true);
32621 /* If we emitted any indirect strings, output the string table too. */
32622 if (debug_str_hash || skeleton_debug_str_hash)
32623 output_indirect_strings ();
32624 if (debug_line_str_hash)
32626 switch_to_section (debug_line_str_section);
32627 const enum dwarf_form form = DW_FORM_line_strp;
32628 debug_line_str_hash->traverse<enum dwarf_form,
32629 output_indirect_string> (form);
32632 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32633 symview_upper_bound = 0;
32634 if (zero_view_p)
32635 bitmap_clear (zero_view_p);
32638 /* Returns a hash value for X (which really is a variable_value_struct). */
32640 inline hashval_t
32641 variable_value_hasher::hash (variable_value_struct *x)
32643 return (hashval_t) x->decl_id;
32646 /* Return nonzero if decl_id of variable_value_struct X is the same as
32647 UID of decl Y. */
32649 inline bool
32650 variable_value_hasher::equal (variable_value_struct *x, tree y)
32652 return x->decl_id == DECL_UID (y);
32655 /* Helper function for resolve_variable_value, handle
32656 DW_OP_GNU_variable_value in one location expression.
32657 Return true if exprloc has been changed into loclist. */
32659 static bool
32660 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32662 dw_loc_descr_ref next;
32663 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32665 next = loc->dw_loc_next;
32666 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32667 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32668 continue;
32670 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32671 if (DECL_CONTEXT (decl) != current_function_decl)
32672 continue;
32674 dw_die_ref ref = lookup_decl_die (decl);
32675 if (ref)
32677 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32678 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32679 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32680 continue;
32682 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32683 if (l == NULL)
32684 continue;
32685 if (l->dw_loc_next)
32687 if (AT_class (a) != dw_val_class_loc)
32688 continue;
32689 switch (a->dw_attr)
32691 /* Following attributes allow both exprloc and loclist
32692 classes, so we can change them into a loclist. */
32693 case DW_AT_location:
32694 case DW_AT_string_length:
32695 case DW_AT_return_addr:
32696 case DW_AT_data_member_location:
32697 case DW_AT_frame_base:
32698 case DW_AT_segment:
32699 case DW_AT_static_link:
32700 case DW_AT_use_location:
32701 case DW_AT_vtable_elem_location:
32702 if (prev)
32704 prev->dw_loc_next = NULL;
32705 prepend_loc_descr_to_each (l, AT_loc (a));
32707 if (next)
32708 add_loc_descr_to_each (l, next);
32709 a->dw_attr_val.val_class = dw_val_class_loc_list;
32710 a->dw_attr_val.val_entry = NULL;
32711 a->dw_attr_val.v.val_loc_list = l;
32712 have_location_lists = true;
32713 return true;
32714 /* Following attributes allow both exprloc and reference,
32715 so if the whole expression is DW_OP_GNU_variable_value alone
32716 we could transform it into reference. */
32717 case DW_AT_byte_size:
32718 case DW_AT_bit_size:
32719 case DW_AT_lower_bound:
32720 case DW_AT_upper_bound:
32721 case DW_AT_bit_stride:
32722 case DW_AT_count:
32723 case DW_AT_allocated:
32724 case DW_AT_associated:
32725 case DW_AT_byte_stride:
32726 if (prev == NULL && next == NULL)
32727 break;
32728 /* FALLTHRU */
32729 default:
32730 if (dwarf_strict)
32731 continue;
32732 break;
32734 /* Create DW_TAG_variable that we can refer to. */
32735 gen_decl_die (decl, NULL_TREE, NULL,
32736 lookup_decl_die (current_function_decl));
32737 ref = lookup_decl_die (decl);
32738 if (ref)
32740 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32741 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32742 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32744 continue;
32746 if (prev)
32748 prev->dw_loc_next = l->expr;
32749 add_loc_descr (&prev->dw_loc_next, next);
32750 free_loc_descr (loc, NULL);
32751 next = prev->dw_loc_next;
32753 else
32755 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32756 add_loc_descr (&loc, next);
32757 next = loc;
32759 loc = prev;
32761 return false;
32764 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32766 static void
32767 resolve_variable_value (dw_die_ref die)
32769 dw_attr_node *a;
32770 dw_loc_list_ref loc;
32771 unsigned ix;
32773 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32774 switch (AT_class (a))
32776 case dw_val_class_loc:
32777 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32778 break;
32779 /* FALLTHRU */
32780 case dw_val_class_loc_list:
32781 loc = AT_loc_list (a);
32782 gcc_assert (loc);
32783 for (; loc; loc = loc->dw_loc_next)
32784 resolve_variable_value_in_expr (a, loc->expr);
32785 break;
32786 default:
32787 break;
32791 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32792 temporaries in the current function. */
32794 static void
32795 resolve_variable_values (void)
32797 if (!variable_value_hash || !current_function_decl)
32798 return;
32800 struct variable_value_struct *node
32801 = variable_value_hash->find_with_hash (current_function_decl,
32802 DECL_UID (current_function_decl));
32804 if (node == NULL)
32805 return;
32807 unsigned int i;
32808 dw_die_ref die;
32809 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32810 resolve_variable_value (die);
32813 /* Helper function for note_variable_value, handle one location
32814 expression. */
32816 static void
32817 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32819 for (; loc; loc = loc->dw_loc_next)
32820 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32821 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32823 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32824 dw_die_ref ref = lookup_decl_die (decl);
32825 if (! ref && (flag_generate_lto || flag_generate_offload))
32827 /* ??? This is somewhat a hack because we do not create DIEs
32828 for variables not in BLOCK trees early but when generating
32829 early LTO output we need the dw_val_class_decl_ref to be
32830 fully resolved. For fat LTO objects we'd also like to
32831 undo this after LTO dwarf output. */
32832 gcc_assert (DECL_CONTEXT (decl));
32833 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32834 gcc_assert (ctx != NULL);
32835 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32836 ref = lookup_decl_die (decl);
32837 gcc_assert (ref != NULL);
32839 if (ref)
32841 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32842 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32843 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32844 continue;
32846 if (VAR_P (decl)
32847 && DECL_CONTEXT (decl)
32848 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32849 && lookup_decl_die (DECL_CONTEXT (decl)))
32851 if (!variable_value_hash)
32852 variable_value_hash
32853 = hash_table<variable_value_hasher>::create_ggc (10);
32855 tree fndecl = DECL_CONTEXT (decl);
32856 struct variable_value_struct *node;
32857 struct variable_value_struct **slot
32858 = variable_value_hash->find_slot_with_hash (fndecl,
32859 DECL_UID (fndecl),
32860 INSERT);
32861 if (*slot == NULL)
32863 node = ggc_cleared_alloc<variable_value_struct> ();
32864 node->decl_id = DECL_UID (fndecl);
32865 *slot = node;
32867 else
32868 node = *slot;
32870 vec_safe_push (node->dies, die);
32875 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32876 with dw_val_class_decl_ref operand. */
32878 static void
32879 note_variable_value (dw_die_ref die)
32881 dw_die_ref c;
32882 dw_attr_node *a;
32883 dw_loc_list_ref loc;
32884 unsigned ix;
32886 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32887 switch (AT_class (a))
32889 case dw_val_class_loc_list:
32890 loc = AT_loc_list (a);
32891 gcc_assert (loc);
32892 if (!loc->noted_variable_value)
32894 loc->noted_variable_value = 1;
32895 for (; loc; loc = loc->dw_loc_next)
32896 note_variable_value_in_expr (die, loc->expr);
32898 break;
32899 case dw_val_class_loc:
32900 note_variable_value_in_expr (die, AT_loc (a));
32901 break;
32902 default:
32903 break;
32906 /* Mark children. */
32907 FOR_EACH_CHILD (die, c, note_variable_value (c));
32910 /* Process DWARF dies for CTF generation. */
32912 static void
32913 ctf_debug_do_cu (dw_die_ref die)
32915 dw_die_ref c;
32917 if (!ctf_do_die (die))
32918 return;
32920 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32923 /* Perform any cleanups needed after the early debug generation pass
32924 has run. */
32926 static void
32927 dwarf2out_early_finish (const char *filename)
32929 comdat_type_node *ctnode;
32930 set_early_dwarf s;
32931 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32933 /* PCH might result in DW_AT_producer string being restored from the
32934 header compilation, so always fill it with empty string initially
32935 and overwrite only here. */
32936 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32938 if (dwarf_record_gcc_switches)
32939 producer_string = gen_producer_string (lang_hooks.name,
32940 save_decoded_options,
32941 save_decoded_options_count);
32942 else
32943 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32945 producer->dw_attr_val.v.val_str->refcount--;
32946 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32948 /* Add the name for the main input file now. We delayed this from
32949 dwarf2out_init to avoid complications with PCH. */
32950 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32951 add_comp_dir_attribute (comp_unit_die ());
32953 /* With LTO early dwarf was really finished at compile-time, so make
32954 sure to adjust the phase after annotating the LTRANS CU DIE. */
32955 if (in_lto_p)
32957 early_dwarf_finished = true;
32958 if (dump_file)
32960 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32961 print_die (comp_unit_die (), dump_file);
32963 return;
32966 /* Walk through the list of incomplete types again, trying once more to
32967 emit full debugging info for them. */
32968 retry_incomplete_types ();
32970 gen_scheduled_generic_parms_dies ();
32971 gen_remaining_tmpl_value_param_die_attribute ();
32973 /* The point here is to flush out the limbo list so that it is empty
32974 and we don't need to stream it for LTO. */
32975 flush_limbo_die_list ();
32977 /* Add DW_AT_linkage_name for all deferred DIEs. */
32978 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32980 tree decl = node->created_for;
32981 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32982 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32983 ended up in deferred_asm_name before we knew it was
32984 constant and never written to disk. */
32985 && DECL_ASSEMBLER_NAME (decl))
32987 add_linkage_attr (node->die, decl);
32988 move_linkage_attr (node->die);
32991 deferred_asm_name = NULL;
32993 if (flag_eliminate_unused_debug_types)
32994 prune_unused_types ();
32996 /* Generate separate COMDAT sections for type DIEs. */
32997 if (use_debug_types)
32999 break_out_comdat_types (comp_unit_die ());
33001 /* Each new type_unit DIE was added to the limbo die list when created.
33002 Since these have all been added to comdat_type_list, clear the
33003 limbo die list. */
33004 limbo_die_list = NULL;
33006 /* For each new comdat type unit, copy declarations for incomplete
33007 types to make the new unit self-contained (i.e., no direct
33008 references to the main compile unit). */
33009 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33010 copy_decls_for_unworthy_types (ctnode->root_die);
33011 copy_decls_for_unworthy_types (comp_unit_die ());
33013 /* In the process of copying declarations from one unit to another,
33014 we may have left some declarations behind that are no longer
33015 referenced. Prune them. */
33016 prune_unused_types ();
33019 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33020 with dw_val_class_decl_ref operand. */
33021 note_variable_value (comp_unit_die ());
33022 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33023 note_variable_value (node->die);
33024 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33025 note_variable_value (ctnode->root_die);
33026 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33027 note_variable_value (node->die);
33029 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33030 both the main_cu and all skeleton TUs. Making this call unconditional
33031 would end up either adding a second copy of the AT_pubnames attribute, or
33032 requiring a special case in add_top_level_skeleton_die_attrs. */
33033 if (!dwarf_split_debug_info)
33034 add_AT_pubnames (comp_unit_die ());
33036 /* The early debug phase is now finished. */
33037 early_dwarf_finished = true;
33038 if (dump_file)
33040 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33041 print_die (comp_unit_die (), dump_file);
33044 /* Generate CTF/BTF debug info. */
33045 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33046 || btf_debuginfo_p ()) && lang_GNU_C ())
33048 ctf_debug_init ();
33049 ctf_debug_do_cu (comp_unit_die ());
33050 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33051 ctf_debug_do_cu (node->die);
33052 /* Post process the debug data in the CTF container if necessary. */
33053 ctf_debug_init_postprocess (btf_debuginfo_p ());
33055 ctf_debug_early_finish (filename);
33058 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33059 if ((!flag_generate_lto && !flag_generate_offload)
33060 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33061 copy_lto_debug_sections operation of the simple object support in
33062 libiberty is not implemented for them yet. */
33063 || TARGET_PECOFF || TARGET_COFF)
33064 return;
33066 /* Now as we are going to output for LTO initialize sections and labels
33067 to the LTO variants. We don't need a random-seed postfix as other
33068 LTO sections as linking the LTO debug sections into one in a partial
33069 link is fine. */
33070 init_sections_and_labels (true);
33072 /* The output below is modeled after dwarf2out_finish with all
33073 location related output removed and some LTO specific changes.
33074 Some refactoring might make both smaller and easier to match up. */
33076 base_types.truncate (0);
33077 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33078 mark_base_types (ctnode->root_die);
33079 mark_base_types (comp_unit_die ());
33080 move_marked_base_types ();
33082 /* Traverse the DIE's and add sibling attributes to those DIE's
33083 that have children. */
33084 add_sibling_attributes (comp_unit_die ());
33085 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33086 add_sibling_attributes (node->die);
33087 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33088 add_sibling_attributes (ctnode->root_die);
33090 /* AIX Assembler inserts the length, so adjust the reference to match the
33091 offset expected by debuggers. */
33092 strcpy (dl_section_ref, debug_line_section_label);
33093 if (XCOFF_DEBUGGING_INFO)
33094 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33096 if (debug_info_level >= DINFO_LEVEL_TERSE)
33097 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33099 if (have_macinfo)
33100 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33101 macinfo_section_label);
33103 save_macinfo_strings ();
33105 if (dwarf_split_debug_info)
33107 unsigned int index = 0;
33108 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33111 /* Output all of the compilation units. We put the main one last so that
33112 the offsets are available to output_pubnames. */
33113 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33114 output_comp_unit (node->die, 0, NULL);
33116 hash_table<comdat_type_hasher> comdat_type_table (100);
33117 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33119 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33121 /* Don't output duplicate types. */
33122 if (*slot != HTAB_EMPTY_ENTRY)
33123 continue;
33125 /* Add a pointer to the line table for the main compilation unit
33126 so that the debugger can make sense of DW_AT_decl_file
33127 attributes. */
33128 if (debug_info_level >= DINFO_LEVEL_TERSE)
33129 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33130 (!dwarf_split_debug_info
33131 ? debug_line_section_label
33132 : debug_skeleton_line_section_label));
33134 output_comdat_type_unit (ctnode, true);
33135 *slot = ctnode;
33138 /* Stick a unique symbol to the main debuginfo section. */
33139 compute_comp_unit_symbol (comp_unit_die ());
33141 /* Output the main compilation unit. We always need it if only for
33142 the CU symbol. */
33143 output_comp_unit (comp_unit_die (), true, NULL);
33145 /* Output the abbreviation table. */
33146 if (vec_safe_length (abbrev_die_table) != 1)
33148 switch_to_section (debug_abbrev_section);
33149 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33150 output_abbrev_section ();
33153 /* Have to end the macro section. */
33154 if (have_macinfo)
33156 /* We have to save macinfo state if we need to output it again
33157 for the FAT part of the object. */
33158 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33159 if (flag_fat_lto_objects)
33160 macinfo_table = macinfo_table->copy ();
33162 switch_to_section (debug_macinfo_section);
33163 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33164 output_macinfo (debug_line_section_label, true);
33165 dw2_asm_output_data (1, 0, "End compilation unit");
33167 if (flag_fat_lto_objects)
33169 vec_free (macinfo_table);
33170 macinfo_table = saved_macinfo_table;
33174 /* Emit a skeleton debug_line section. */
33175 switch_to_section (debug_line_section);
33176 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33177 output_line_info (true);
33179 /* If we emitted any indirect strings, output the string table too. */
33180 if (debug_str_hash || skeleton_debug_str_hash)
33181 output_indirect_strings ();
33182 if (debug_line_str_hash)
33184 switch_to_section (debug_line_str_section);
33185 const enum dwarf_form form = DW_FORM_line_strp;
33186 debug_line_str_hash->traverse<enum dwarf_form,
33187 output_indirect_string> (form);
33190 /* Switch back to the text section. */
33191 switch_to_section (text_section);
33194 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33195 within the same process. For use by toplev::finalize. */
33197 void
33198 dwarf2out_cc_finalize (void)
33200 last_var_location_insn = NULL;
33201 cached_next_real_insn = NULL;
33202 used_rtx_array = NULL;
33203 incomplete_types = NULL;
33204 debug_info_section = NULL;
33205 debug_skeleton_info_section = NULL;
33206 debug_abbrev_section = NULL;
33207 debug_skeleton_abbrev_section = NULL;
33208 debug_aranges_section = NULL;
33209 debug_addr_section = NULL;
33210 debug_macinfo_section = NULL;
33211 debug_line_section = NULL;
33212 debug_skeleton_line_section = NULL;
33213 debug_loc_section = NULL;
33214 debug_pubnames_section = NULL;
33215 debug_pubtypes_section = NULL;
33216 debug_str_section = NULL;
33217 debug_line_str_section = NULL;
33218 debug_str_dwo_section = NULL;
33219 debug_str_offsets_section = NULL;
33220 debug_ranges_section = NULL;
33221 debug_ranges_dwo_section = NULL;
33222 debug_frame_section = NULL;
33223 fde_vec = NULL;
33224 debug_str_hash = NULL;
33225 debug_line_str_hash = NULL;
33226 skeleton_debug_str_hash = NULL;
33227 dw2_string_counter = 0;
33228 have_multiple_function_sections = false;
33229 in_text_section_p = false;
33230 cold_text_section = NULL;
33231 last_text_label = NULL;
33232 last_cold_label = NULL;
33233 switch_text_ranges = NULL;
33234 switch_cold_ranges = NULL;
33235 current_unit_personality = NULL;
33237 early_dwarf = false;
33238 early_dwarf_finished = false;
33240 next_die_offset = 0;
33241 single_comp_unit_die = NULL;
33242 comdat_type_list = NULL;
33243 limbo_die_list = NULL;
33244 file_table = NULL;
33245 decl_die_table = NULL;
33246 common_block_die_table = NULL;
33247 decl_loc_table = NULL;
33248 call_arg_locations = NULL;
33249 call_arg_loc_last = NULL;
33250 call_site_count = -1;
33251 tail_call_site_count = -1;
33252 cached_dw_loc_list_table = NULL;
33253 abbrev_die_table = NULL;
33254 delete dwarf_proc_stack_usage_map;
33255 dwarf_proc_stack_usage_map = NULL;
33256 line_info_label_num = 0;
33257 cur_line_info_table = NULL;
33258 text_section_line_info = NULL;
33259 cold_text_section_line_info = NULL;
33260 separate_line_info = NULL;
33261 info_section_emitted = false;
33262 pubname_table = NULL;
33263 pubtype_table = NULL;
33264 macinfo_table = NULL;
33265 ranges_table = NULL;
33266 ranges_by_label = NULL;
33267 rnglist_idx = 0;
33268 have_location_lists = false;
33269 loclabel_num = 0;
33270 poc_label_num = 0;
33271 last_emitted_file = NULL;
33272 label_num = 0;
33273 tmpl_value_parm_die_table = NULL;
33274 generic_type_instances = NULL;
33275 frame_pointer_fb_offset = 0;
33276 frame_pointer_fb_offset_valid = false;
33277 base_types.release ();
33278 XDELETEVEC (producer_string);
33279 producer_string = NULL;
33280 output_line_info_generation = 0;
33281 init_sections_and_labels_generation = 0;
33284 #include "gt-dwarf2out.h"