Skip gcc.dg/guality/example.c on hppa-linux.
[official-gcc.git] / gcc / dwarf2out.c
blobd6513d389e5e1ac07687b3658de5368e8eeb42d7
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2021 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.c 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 dbx_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 /* Standard ELF section names for compiled code and data. */
4190 #ifndef TEXT_SECTION_NAME
4191 #define TEXT_SECTION_NAME ".text"
4192 #endif
4194 /* Section flags for .debug_str section. */
4195 #define DEBUG_STR_SECTION_FLAGS \
4196 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4197 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4198 : SECTION_DEBUG)
4200 /* Section flags for .debug_str.dwo section. */
4201 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4203 /* Attribute used to refer to the macro section. */
4204 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4205 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4207 /* Labels we insert at beginning sections we can reference instead of
4208 the section names themselves. */
4210 #ifndef TEXT_SECTION_LABEL
4211 #define TEXT_SECTION_LABEL "Ltext"
4212 #endif
4213 #ifndef COLD_TEXT_SECTION_LABEL
4214 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4215 #endif
4216 #ifndef DEBUG_LINE_SECTION_LABEL
4217 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4218 #endif
4219 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4220 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4221 #endif
4222 #ifndef DEBUG_INFO_SECTION_LABEL
4223 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4224 #endif
4225 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4226 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4227 #endif
4228 #ifndef DEBUG_ABBREV_SECTION_LABEL
4229 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4230 #endif
4231 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4232 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4233 #endif
4234 #ifndef DEBUG_ADDR_SECTION_LABEL
4235 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4236 #endif
4237 #ifndef DEBUG_LOC_SECTION_LABEL
4238 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4239 #endif
4240 #ifndef DEBUG_RANGES_SECTION_LABEL
4241 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4242 #endif
4243 #ifndef DEBUG_MACINFO_SECTION_LABEL
4244 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4245 #endif
4246 #ifndef DEBUG_MACRO_SECTION_LABEL
4247 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4248 #endif
4249 #define SKELETON_COMP_DIE_ABBREV 1
4250 #define SKELETON_TYPE_DIE_ABBREV 2
4252 /* Definitions of defaults for formats and names of various special
4253 (artificial) labels which may be generated within this file (when the -g
4254 options is used and DWARF2_DEBUGGING_INFO is in effect.
4255 If necessary, these may be overridden from within the tm.h file, but
4256 typically, overriding these defaults is unnecessary. */
4258 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4266 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4268 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4269 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4270 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4271 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4272 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4274 #ifndef TEXT_END_LABEL
4275 #define TEXT_END_LABEL "Letext"
4276 #endif
4277 #ifndef COLD_END_LABEL
4278 #define COLD_END_LABEL "Letext_cold"
4279 #endif
4280 #ifndef BLOCK_BEGIN_LABEL
4281 #define BLOCK_BEGIN_LABEL "LBB"
4282 #endif
4283 #ifndef BLOCK_INLINE_ENTRY_LABEL
4284 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4285 #endif
4286 #ifndef BLOCK_END_LABEL
4287 #define BLOCK_END_LABEL "LBE"
4288 #endif
4289 #ifndef LINE_CODE_LABEL
4290 #define LINE_CODE_LABEL "LM"
4291 #endif
4294 /* Return the root of the DIE's built for the current compilation unit. */
4295 static dw_die_ref
4296 comp_unit_die (void)
4298 if (!single_comp_unit_die)
4299 single_comp_unit_die = gen_compile_unit_die (NULL);
4300 return single_comp_unit_die;
4303 /* We allow a language front-end to designate a function that is to be
4304 called to "demangle" any name before it is put into a DIE. */
4306 static const char *(*demangle_name_func) (const char *);
4308 void
4309 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4311 demangle_name_func = func;
4314 /* Test if rtl node points to a pseudo register. */
4316 static inline int
4317 is_pseudo_reg (const_rtx rtl)
4319 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4320 || (GET_CODE (rtl) == SUBREG
4321 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4324 /* Return a reference to a type, with its const and volatile qualifiers
4325 removed. */
4327 static inline tree
4328 type_main_variant (tree type)
4330 type = TYPE_MAIN_VARIANT (type);
4332 /* ??? There really should be only one main variant among any group of
4333 variants of a given type (and all of the MAIN_VARIANT values for all
4334 members of the group should point to that one type) but sometimes the C
4335 front-end messes this up for array types, so we work around that bug
4336 here. */
4337 if (TREE_CODE (type) == ARRAY_TYPE)
4338 while (type != TYPE_MAIN_VARIANT (type))
4339 type = TYPE_MAIN_VARIANT (type);
4341 return type;
4344 /* Return nonzero if the given type node represents a tagged type. */
4346 static inline int
4347 is_tagged_type (const_tree type)
4349 enum tree_code code = TREE_CODE (type);
4351 return (code == RECORD_TYPE || code == UNION_TYPE
4352 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4355 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4357 static void
4358 get_ref_die_offset_label (char *label, dw_die_ref ref)
4360 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4363 /* Return die_offset of a DIE reference to a base type. */
4365 static unsigned long int
4366 get_base_type_offset (dw_die_ref ref)
4368 if (ref->die_offset)
4369 return ref->die_offset;
4370 if (comp_unit_die ()->die_abbrev)
4372 calc_base_type_die_sizes ();
4373 gcc_assert (ref->die_offset);
4375 return ref->die_offset;
4378 /* Return die_offset of a DIE reference other than base type. */
4380 static unsigned long int
4381 get_ref_die_offset (dw_die_ref ref)
4383 gcc_assert (ref->die_offset);
4384 return ref->die_offset;
4387 /* Convert a DIE tag into its string name. */
4389 static const char *
4390 dwarf_tag_name (unsigned int tag)
4392 const char *name = get_DW_TAG_name (tag);
4394 if (name != NULL)
4395 return name;
4397 return "DW_TAG_<unknown>";
4400 /* Convert a DWARF attribute code into its string name. */
4402 static const char *
4403 dwarf_attr_name (unsigned int attr)
4405 const char *name;
4407 switch (attr)
4409 #if VMS_DEBUGGING_INFO
4410 case DW_AT_HP_prologue:
4411 return "DW_AT_HP_prologue";
4412 #else
4413 case DW_AT_MIPS_loop_unroll_factor:
4414 return "DW_AT_MIPS_loop_unroll_factor";
4415 #endif
4417 #if VMS_DEBUGGING_INFO
4418 case DW_AT_HP_epilogue:
4419 return "DW_AT_HP_epilogue";
4420 #else
4421 case DW_AT_MIPS_stride:
4422 return "DW_AT_MIPS_stride";
4423 #endif
4426 name = get_DW_AT_name (attr);
4428 if (name != NULL)
4429 return name;
4431 return "DW_AT_<unknown>";
4434 /* Convert a DWARF value form code into its string name. */
4436 static const char *
4437 dwarf_form_name (unsigned int form)
4439 const char *name = get_DW_FORM_name (form);
4441 if (name != NULL)
4442 return name;
4444 return "DW_FORM_<unknown>";
4447 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4448 instance of an inlined instance of a decl which is local to an inline
4449 function, so we have to trace all of the way back through the origin chain
4450 to find out what sort of node actually served as the original seed for the
4451 given block. */
4453 static tree
4454 decl_ultimate_origin (const_tree decl)
4456 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4457 return NULL_TREE;
4459 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4460 we're trying to output the abstract instance of this function. */
4461 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4462 return NULL_TREE;
4464 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4465 most distant ancestor, this should never happen. */
4466 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4468 return DECL_ABSTRACT_ORIGIN (decl);
4471 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4472 of a virtual function may refer to a base class, so we check the 'this'
4473 parameter. */
4475 static tree
4476 decl_class_context (tree decl)
4478 tree context = NULL_TREE;
4480 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4481 context = DECL_CONTEXT (decl);
4482 else
4483 context = TYPE_MAIN_VARIANT
4484 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4486 if (context && !TYPE_P (context))
4487 context = NULL_TREE;
4489 return context;
4492 /* Add an attribute/value pair to a DIE. */
4494 static inline void
4495 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4497 /* Maybe this should be an assert? */
4498 if (die == NULL)
4499 return;
4501 if (flag_checking)
4503 /* Check we do not add duplicate attrs. Can't use get_AT here
4504 because that recurses to the specification/abstract origin DIE. */
4505 dw_attr_node *a;
4506 unsigned ix;
4507 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4508 gcc_assert (a->dw_attr != attr->dw_attr);
4511 vec_safe_reserve (die->die_attr, 1);
4512 vec_safe_push (die->die_attr, *attr);
4515 enum dw_val_class
4516 AT_class (dw_attr_node *a)
4518 return a->dw_attr_val.val_class;
4521 /* Return the index for any attribute that will be referenced with a
4522 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4523 indices are stored in dw_attr_val.v.val_str for reference counting
4524 pruning. */
4526 static inline unsigned int
4527 AT_index (dw_attr_node *a)
4529 if (AT_class (a) == dw_val_class_str)
4530 return a->dw_attr_val.v.val_str->index;
4531 else if (a->dw_attr_val.val_entry != NULL)
4532 return a->dw_attr_val.val_entry->index;
4533 return NOT_INDEXED;
4536 /* Add a flag value attribute to a DIE. */
4538 static inline void
4539 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4541 dw_attr_node attr;
4543 attr.dw_attr = attr_kind;
4544 attr.dw_attr_val.val_class = dw_val_class_flag;
4545 attr.dw_attr_val.val_entry = NULL;
4546 attr.dw_attr_val.v.val_flag = flag;
4547 add_dwarf_attr (die, &attr);
4550 static inline unsigned
4551 AT_flag (dw_attr_node *a)
4553 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4554 return a->dw_attr_val.v.val_flag;
4557 /* Add a signed integer attribute value to a DIE. */
4559 static inline void
4560 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4562 dw_attr_node attr;
4564 attr.dw_attr = attr_kind;
4565 attr.dw_attr_val.val_class = dw_val_class_const;
4566 attr.dw_attr_val.val_entry = NULL;
4567 attr.dw_attr_val.v.val_int = int_val;
4568 add_dwarf_attr (die, &attr);
4571 HOST_WIDE_INT
4572 AT_int (dw_attr_node *a)
4574 gcc_assert (a && (AT_class (a) == dw_val_class_const
4575 || AT_class (a) == dw_val_class_const_implicit));
4576 return a->dw_attr_val.v.val_int;
4579 /* Add an unsigned integer attribute value to a DIE. */
4581 static inline void
4582 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4583 unsigned HOST_WIDE_INT unsigned_val)
4585 dw_attr_node attr;
4587 attr.dw_attr = attr_kind;
4588 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4589 attr.dw_attr_val.val_entry = NULL;
4590 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4591 add_dwarf_attr (die, &attr);
4594 unsigned HOST_WIDE_INT
4595 AT_unsigned (dw_attr_node *a)
4597 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4598 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4599 return a->dw_attr_val.v.val_unsigned;
4602 /* Add an unsigned wide integer attribute value to a DIE. */
4604 static inline void
4605 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4606 const wide_int& w)
4608 dw_attr_node attr;
4610 attr.dw_attr = attr_kind;
4611 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4612 attr.dw_attr_val.val_entry = NULL;
4613 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4614 *attr.dw_attr_val.v.val_wide = w;
4615 add_dwarf_attr (die, &attr);
4618 /* Add an unsigned double integer attribute value to a DIE. */
4620 static inline void
4621 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4622 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4624 dw_attr_node attr;
4626 attr.dw_attr = attr_kind;
4627 attr.dw_attr_val.val_class = dw_val_class_const_double;
4628 attr.dw_attr_val.val_entry = NULL;
4629 attr.dw_attr_val.v.val_double.high = high;
4630 attr.dw_attr_val.v.val_double.low = low;
4631 add_dwarf_attr (die, &attr);
4634 /* Add a floating point attribute value to a DIE and return it. */
4636 static inline void
4637 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4638 unsigned int length, unsigned int elt_size, unsigned char *array)
4640 dw_attr_node attr;
4642 attr.dw_attr = attr_kind;
4643 attr.dw_attr_val.val_class = dw_val_class_vec;
4644 attr.dw_attr_val.val_entry = NULL;
4645 attr.dw_attr_val.v.val_vec.length = length;
4646 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4647 attr.dw_attr_val.v.val_vec.array = array;
4648 add_dwarf_attr (die, &attr);
4651 /* Add an 8-byte data attribute value to a DIE. */
4653 static inline void
4654 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4655 unsigned char data8[8])
4657 dw_attr_node attr;
4659 attr.dw_attr = attr_kind;
4660 attr.dw_attr_val.val_class = dw_val_class_data8;
4661 attr.dw_attr_val.val_entry = NULL;
4662 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4663 add_dwarf_attr (die, &attr);
4666 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4667 dwarf_split_debug_info, address attributes in dies destined for the
4668 final executable have force_direct set to avoid using indexed
4669 references. */
4671 static inline void
4672 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4673 bool force_direct)
4675 dw_attr_node attr;
4676 char * lbl_id;
4678 lbl_id = xstrdup (lbl_low);
4679 attr.dw_attr = DW_AT_low_pc;
4680 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4681 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4682 if (dwarf_split_debug_info && !force_direct)
4683 attr.dw_attr_val.val_entry
4684 = add_addr_table_entry (lbl_id, ate_kind_label);
4685 else
4686 attr.dw_attr_val.val_entry = NULL;
4687 add_dwarf_attr (die, &attr);
4689 attr.dw_attr = DW_AT_high_pc;
4690 if (dwarf_version < 4)
4691 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4692 else
4693 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4694 lbl_id = xstrdup (lbl_high);
4695 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4696 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4697 && dwarf_split_debug_info && !force_direct)
4698 attr.dw_attr_val.val_entry
4699 = add_addr_table_entry (lbl_id, ate_kind_label);
4700 else
4701 attr.dw_attr_val.val_entry = NULL;
4702 add_dwarf_attr (die, &attr);
4705 /* Hash and equality functions for debug_str_hash. */
4707 hashval_t
4708 indirect_string_hasher::hash (indirect_string_node *x)
4710 return htab_hash_string (x->str);
4713 bool
4714 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4716 return strcmp (x1->str, x2) == 0;
4719 /* Add STR to the given string hash table. */
4721 static struct indirect_string_node *
4722 find_AT_string_in_table (const char *str,
4723 hash_table<indirect_string_hasher> *table,
4724 enum insert_option insert = INSERT)
4726 struct indirect_string_node *node;
4728 indirect_string_node **slot
4729 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4730 if (*slot == NULL)
4732 node = ggc_cleared_alloc<indirect_string_node> ();
4733 node->str = ggc_strdup (str);
4734 *slot = node;
4736 else
4737 node = *slot;
4739 node->refcount++;
4740 return node;
4743 /* Add STR to the indirect string hash table. */
4745 static struct indirect_string_node *
4746 find_AT_string (const char *str, enum insert_option insert = INSERT)
4748 if (! debug_str_hash)
4749 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4751 return find_AT_string_in_table (str, debug_str_hash, insert);
4754 /* Add a string attribute value to a DIE. */
4756 static inline void
4757 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4759 dw_attr_node attr;
4760 struct indirect_string_node *node;
4762 node = find_AT_string (str);
4764 attr.dw_attr = attr_kind;
4765 attr.dw_attr_val.val_class = dw_val_class_str;
4766 attr.dw_attr_val.val_entry = NULL;
4767 attr.dw_attr_val.v.val_str = node;
4768 add_dwarf_attr (die, &attr);
4771 static inline const char *
4772 AT_string (dw_attr_node *a)
4774 gcc_assert (a && AT_class (a) == dw_val_class_str);
4775 return a->dw_attr_val.v.val_str->str;
4778 /* Call this function directly to bypass AT_string_form's logic to put
4779 the string inline in the die. */
4781 static void
4782 set_indirect_string (struct indirect_string_node *node)
4784 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4785 /* Already indirect is a no op. */
4786 if (node->form == DW_FORM_strp
4787 || node->form == DW_FORM_line_strp
4788 || node->form == dwarf_FORM (DW_FORM_strx))
4790 gcc_assert (node->label);
4791 return;
4793 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4794 ++dw2_string_counter;
4795 node->label = xstrdup (label);
4797 if (!dwarf_split_debug_info)
4799 node->form = DW_FORM_strp;
4800 node->index = NOT_INDEXED;
4802 else
4804 node->form = dwarf_FORM (DW_FORM_strx);
4805 node->index = NO_INDEX_ASSIGNED;
4809 /* A helper function for dwarf2out_finish, called to reset indirect
4810 string decisions done for early LTO dwarf output before fat object
4811 dwarf output. */
4814 reset_indirect_string (indirect_string_node **h, void *)
4816 struct indirect_string_node *node = *h;
4817 if (node->form == DW_FORM_strp
4818 || node->form == DW_FORM_line_strp
4819 || node->form == dwarf_FORM (DW_FORM_strx))
4821 free (node->label);
4822 node->label = NULL;
4823 node->form = (dwarf_form) 0;
4824 node->index = 0;
4826 return 1;
4829 /* Add a string representing a file or filepath attribute value to a DIE. */
4831 static inline void
4832 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4833 const char *str)
4835 if (! asm_outputs_debug_line_str ())
4836 add_AT_string (die, attr_kind, str);
4837 else
4839 dw_attr_node attr;
4840 struct indirect_string_node *node;
4842 if (!debug_line_str_hash)
4843 debug_line_str_hash
4844 = hash_table<indirect_string_hasher>::create_ggc (10);
4846 node = find_AT_string_in_table (str, debug_line_str_hash);
4847 set_indirect_string (node);
4848 node->form = DW_FORM_line_strp;
4850 attr.dw_attr = attr_kind;
4851 attr.dw_attr_val.val_class = dw_val_class_str;
4852 attr.dw_attr_val.val_entry = NULL;
4853 attr.dw_attr_val.v.val_str = node;
4854 add_dwarf_attr (die, &attr);
4858 /* Find out whether a string should be output inline in DIE
4859 or out-of-line in .debug_str section. */
4861 static enum dwarf_form
4862 find_string_form (struct indirect_string_node *node)
4864 unsigned int len;
4866 if (node->form)
4867 return node->form;
4869 len = strlen (node->str) + 1;
4871 /* If the string is shorter or equal to the size of the reference, it is
4872 always better to put it inline. */
4873 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4874 return node->form = DW_FORM_string;
4876 /* If we cannot expect the linker to merge strings in .debug_str
4877 section, only put it into .debug_str if it is worth even in this
4878 single module. */
4879 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4880 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4881 && (len - dwarf_offset_size) * node->refcount <= len))
4882 return node->form = DW_FORM_string;
4884 set_indirect_string (node);
4886 return node->form;
4889 /* Find out whether the string referenced from the attribute should be
4890 output inline in DIE or out-of-line in .debug_str section. */
4892 static enum dwarf_form
4893 AT_string_form (dw_attr_node *a)
4895 gcc_assert (a && AT_class (a) == dw_val_class_str);
4896 return find_string_form (a->dw_attr_val.v.val_str);
4899 /* Add a DIE reference attribute value to a DIE. */
4901 static inline void
4902 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4904 dw_attr_node attr;
4905 gcc_checking_assert (targ_die != NULL);
4907 /* With LTO we can end up trying to reference something we didn't create
4908 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4909 if (targ_die == NULL)
4910 return;
4912 attr.dw_attr = attr_kind;
4913 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4914 attr.dw_attr_val.val_entry = NULL;
4915 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4916 attr.dw_attr_val.v.val_die_ref.external = 0;
4917 add_dwarf_attr (die, &attr);
4920 /* Change DIE reference REF to point to NEW_DIE instead. */
4922 static inline void
4923 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4925 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4926 ref->dw_attr_val.v.val_die_ref.die = new_die;
4927 ref->dw_attr_val.v.val_die_ref.external = 0;
4930 /* Add an AT_specification attribute to a DIE, and also make the back
4931 pointer from the specification to the definition. */
4933 static inline void
4934 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4936 add_AT_die_ref (die, DW_AT_specification, targ_die);
4937 gcc_assert (!targ_die->die_definition);
4938 targ_die->die_definition = die;
4941 static inline dw_die_ref
4942 AT_ref (dw_attr_node *a)
4944 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4945 return a->dw_attr_val.v.val_die_ref.die;
4948 static inline int
4949 AT_ref_external (dw_attr_node *a)
4951 if (a && AT_class (a) == dw_val_class_die_ref)
4952 return a->dw_attr_val.v.val_die_ref.external;
4954 return 0;
4957 static inline void
4958 set_AT_ref_external (dw_attr_node *a, int i)
4960 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4961 a->dw_attr_val.v.val_die_ref.external = i;
4964 /* Add a location description attribute value to a DIE. */
4966 static inline void
4967 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4969 dw_attr_node attr;
4971 attr.dw_attr = attr_kind;
4972 attr.dw_attr_val.val_class = dw_val_class_loc;
4973 attr.dw_attr_val.val_entry = NULL;
4974 attr.dw_attr_val.v.val_loc = loc;
4975 add_dwarf_attr (die, &attr);
4978 dw_loc_descr_ref
4979 AT_loc (dw_attr_node *a)
4981 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4982 return a->dw_attr_val.v.val_loc;
4985 static inline void
4986 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4988 dw_attr_node attr;
4990 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4991 return;
4993 attr.dw_attr = attr_kind;
4994 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4995 attr.dw_attr_val.val_entry = NULL;
4996 attr.dw_attr_val.v.val_loc_list = loc_list;
4997 add_dwarf_attr (die, &attr);
4998 have_location_lists = true;
5001 static inline dw_loc_list_ref
5002 AT_loc_list (dw_attr_node *a)
5004 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5005 return a->dw_attr_val.v.val_loc_list;
5008 /* Add a view list attribute to DIE. It must have a DW_AT_location
5009 attribute, because the view list complements the location list. */
5011 static inline void
5012 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5014 dw_attr_node attr;
5016 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5017 return;
5019 attr.dw_attr = attr_kind;
5020 attr.dw_attr_val.val_class = dw_val_class_view_list;
5021 attr.dw_attr_val.val_entry = NULL;
5022 attr.dw_attr_val.v.val_view_list = die;
5023 add_dwarf_attr (die, &attr);
5024 gcc_checking_assert (get_AT (die, DW_AT_location));
5025 gcc_assert (have_location_lists);
5028 /* Return a pointer to the location list referenced by the attribute.
5029 If the named attribute is a view list, look up the corresponding
5030 DW_AT_location attribute and return its location list. */
5032 static inline dw_loc_list_ref *
5033 AT_loc_list_ptr (dw_attr_node *a)
5035 gcc_assert (a);
5036 switch (AT_class (a))
5038 case dw_val_class_loc_list:
5039 return &a->dw_attr_val.v.val_loc_list;
5040 case dw_val_class_view_list:
5042 dw_attr_node *l;
5043 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5044 if (!l)
5045 return NULL;
5046 gcc_checking_assert (l + 1 == a);
5047 return AT_loc_list_ptr (l);
5049 default:
5050 gcc_unreachable ();
5054 /* Return the location attribute value associated with a view list
5055 attribute value. */
5057 static inline dw_val_node *
5058 view_list_to_loc_list_val_node (dw_val_node *val)
5060 gcc_assert (val->val_class == dw_val_class_view_list);
5061 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5062 if (!loc)
5063 return NULL;
5064 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5065 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5066 return &loc->dw_attr_val;
5069 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5071 static hashval_t hash (addr_table_entry *);
5072 static bool equal (addr_table_entry *, addr_table_entry *);
5075 /* Table of entries into the .debug_addr section. */
5077 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5079 /* Hash an address_table_entry. */
5081 hashval_t
5082 addr_hasher::hash (addr_table_entry *a)
5084 inchash::hash hstate;
5085 switch (a->kind)
5087 case ate_kind_rtx:
5088 hstate.add_int (0);
5089 break;
5090 case ate_kind_rtx_dtprel:
5091 hstate.add_int (1);
5092 break;
5093 case ate_kind_label:
5094 return htab_hash_string (a->addr.label);
5095 default:
5096 gcc_unreachable ();
5098 inchash::add_rtx (a->addr.rtl, hstate);
5099 return hstate.end ();
5102 /* Determine equality for two address_table_entries. */
5104 bool
5105 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5107 if (a1->kind != a2->kind)
5108 return 0;
5109 switch (a1->kind)
5111 case ate_kind_rtx:
5112 case ate_kind_rtx_dtprel:
5113 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5114 case ate_kind_label:
5115 return strcmp (a1->addr.label, a2->addr.label) == 0;
5116 default:
5117 gcc_unreachable ();
5121 /* Initialize an addr_table_entry. */
5123 void
5124 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5126 e->kind = kind;
5127 switch (kind)
5129 case ate_kind_rtx:
5130 case ate_kind_rtx_dtprel:
5131 e->addr.rtl = (rtx) addr;
5132 break;
5133 case ate_kind_label:
5134 e->addr.label = (char *) addr;
5135 break;
5137 e->refcount = 0;
5138 e->index = NO_INDEX_ASSIGNED;
5141 /* Add attr to the address table entry to the table. Defer setting an
5142 index until output time. */
5144 static addr_table_entry *
5145 add_addr_table_entry (void *addr, enum ate_kind kind)
5147 addr_table_entry *node;
5148 addr_table_entry finder;
5150 gcc_assert (dwarf_split_debug_info);
5151 if (! addr_index_table)
5152 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5153 init_addr_table_entry (&finder, kind, addr);
5154 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5156 if (*slot == HTAB_EMPTY_ENTRY)
5158 node = ggc_cleared_alloc<addr_table_entry> ();
5159 init_addr_table_entry (node, kind, addr);
5160 *slot = node;
5162 else
5163 node = *slot;
5165 node->refcount++;
5166 return node;
5169 /* Remove an entry from the addr table by decrementing its refcount.
5170 Strictly, decrementing the refcount would be enough, but the
5171 assertion that the entry is actually in the table has found
5172 bugs. */
5174 static void
5175 remove_addr_table_entry (addr_table_entry *entry)
5177 gcc_assert (dwarf_split_debug_info && addr_index_table);
5178 /* After an index is assigned, the table is frozen. */
5179 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5180 entry->refcount--;
5183 /* Given a location list, remove all addresses it refers to from the
5184 address_table. */
5186 static void
5187 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5189 for (; descr; descr = descr->dw_loc_next)
5190 if (descr->dw_loc_oprnd1.val_entry != NULL)
5192 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5193 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5197 /* A helper function for dwarf2out_finish called through
5198 htab_traverse. Assign an addr_table_entry its index. All entries
5199 must be collected into the table when this function is called,
5200 because the indexing code relies on htab_traverse to traverse nodes
5201 in the same order for each run. */
5204 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5206 addr_table_entry *node = *h;
5208 /* Don't index unreferenced nodes. */
5209 if (node->refcount == 0)
5210 return 1;
5212 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5213 node->index = *index;
5214 *index += 1;
5216 return 1;
5219 /* Return the tag of a given DIE. */
5221 enum dwarf_tag
5222 dw_get_die_tag (dw_die_ref die)
5224 return die->die_tag;
5227 /* Return a reference to the children list of a given DIE. */
5229 dw_die_ref
5230 dw_get_die_child (dw_die_ref die)
5232 return die->die_child;
5235 /* Return a reference to the sibling of a given DIE. */
5237 dw_die_ref
5238 dw_get_die_sib (dw_die_ref die)
5240 return die->die_sib;
5243 /* Add an address constant attribute value to a DIE. When using
5244 dwarf_split_debug_info, address attributes in dies destined for the
5245 final executable should be direct references--setting the parameter
5246 force_direct ensures this behavior. */
5248 static inline void
5249 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5250 bool force_direct)
5252 dw_attr_node attr;
5254 attr.dw_attr = attr_kind;
5255 attr.dw_attr_val.val_class = dw_val_class_addr;
5256 attr.dw_attr_val.v.val_addr = addr;
5257 if (dwarf_split_debug_info && !force_direct)
5258 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5259 else
5260 attr.dw_attr_val.val_entry = NULL;
5261 add_dwarf_attr (die, &attr);
5264 /* Get the RTX from to an address DIE attribute. */
5266 static inline rtx
5267 AT_addr (dw_attr_node *a)
5269 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5270 return a->dw_attr_val.v.val_addr;
5273 /* Add a file attribute value to a DIE. */
5275 static inline void
5276 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5277 struct dwarf_file_data *fd)
5279 dw_attr_node attr;
5281 attr.dw_attr = attr_kind;
5282 attr.dw_attr_val.val_class = dw_val_class_file;
5283 attr.dw_attr_val.val_entry = NULL;
5284 attr.dw_attr_val.v.val_file = fd;
5285 add_dwarf_attr (die, &attr);
5288 /* Get the dwarf_file_data from a file DIE attribute. */
5290 static inline struct dwarf_file_data *
5291 AT_file (dw_attr_node *a)
5293 gcc_assert (a && (AT_class (a) == dw_val_class_file
5294 || AT_class (a) == dw_val_class_file_implicit));
5295 return a->dw_attr_val.v.val_file;
5298 #if VMS_DEBUGGING_INFO
5299 /* Add a vms delta attribute value to a DIE. */
5301 static inline void
5302 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5303 const char *lbl1, const char *lbl2)
5305 dw_attr_node attr;
5307 attr.dw_attr = attr_kind;
5308 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5309 attr.dw_attr_val.val_entry = NULL;
5310 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5311 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5312 add_dwarf_attr (die, &attr);
5314 #endif
5316 /* Add a symbolic view identifier attribute value to a DIE. */
5318 static inline void
5319 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5320 const char *view_label)
5322 dw_attr_node attr;
5324 attr.dw_attr = attr_kind;
5325 attr.dw_attr_val.val_class = dw_val_class_symview;
5326 attr.dw_attr_val.val_entry = NULL;
5327 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5328 add_dwarf_attr (die, &attr);
5331 /* Add a label identifier attribute value to a DIE. */
5333 static inline void
5334 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5335 const char *lbl_id)
5337 dw_attr_node attr;
5339 attr.dw_attr = attr_kind;
5340 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5341 attr.dw_attr_val.val_entry = NULL;
5342 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5343 if (dwarf_split_debug_info)
5344 attr.dw_attr_val.val_entry
5345 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5346 ate_kind_label);
5347 add_dwarf_attr (die, &attr);
5350 /* Add a section offset attribute value to a DIE, an offset into the
5351 debug_line section. */
5353 static inline void
5354 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5355 const char *label)
5357 dw_attr_node attr;
5359 attr.dw_attr = attr_kind;
5360 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5361 attr.dw_attr_val.val_entry = NULL;
5362 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5363 add_dwarf_attr (die, &attr);
5366 /* Add a section offset attribute value to a DIE, an offset into the
5367 debug_macinfo section. */
5369 static inline void
5370 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5371 const char *label)
5373 dw_attr_node attr;
5375 attr.dw_attr = attr_kind;
5376 attr.dw_attr_val.val_class = dw_val_class_macptr;
5377 attr.dw_attr_val.val_entry = NULL;
5378 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5379 add_dwarf_attr (die, &attr);
5382 /* Add a range_list attribute value to a DIE. When using
5383 dwarf_split_debug_info, address attributes in dies destined for the
5384 final executable should be direct references--setting the parameter
5385 force_direct ensures this behavior. */
5387 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5388 #define RELOCATED_OFFSET (NULL)
5390 static void
5391 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5392 long unsigned int offset, bool force_direct)
5394 dw_attr_node attr;
5396 attr.dw_attr = attr_kind;
5397 attr.dw_attr_val.val_class = dw_val_class_range_list;
5398 /* For the range_list attribute, use val_entry to store whether the
5399 offset should follow split-debug-info or normal semantics. This
5400 value is read in output_range_list_offset. */
5401 if (dwarf_split_debug_info && !force_direct)
5402 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5403 else
5404 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5405 attr.dw_attr_val.v.val_offset = offset;
5406 add_dwarf_attr (die, &attr);
5409 /* Return the start label of a delta attribute. */
5411 static inline const char *
5412 AT_vms_delta1 (dw_attr_node *a)
5414 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5415 return a->dw_attr_val.v.val_vms_delta.lbl1;
5418 /* Return the end label of a delta attribute. */
5420 static inline const char *
5421 AT_vms_delta2 (dw_attr_node *a)
5423 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5424 return a->dw_attr_val.v.val_vms_delta.lbl2;
5427 static inline const char *
5428 AT_lbl (dw_attr_node *a)
5430 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5431 || AT_class (a) == dw_val_class_lineptr
5432 || AT_class (a) == dw_val_class_macptr
5433 || AT_class (a) == dw_val_class_loclistsptr
5434 || AT_class (a) == dw_val_class_high_pc));
5435 return a->dw_attr_val.v.val_lbl_id;
5438 /* Get the attribute of type attr_kind. */
5440 dw_attr_node *
5441 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5443 dw_attr_node *a;
5444 unsigned ix;
5445 dw_die_ref spec = NULL;
5447 if (! die)
5448 return NULL;
5450 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5451 if (a->dw_attr == attr_kind)
5452 return a;
5453 else if (a->dw_attr == DW_AT_specification
5454 || a->dw_attr == DW_AT_abstract_origin)
5455 spec = AT_ref (a);
5457 if (spec)
5458 return get_AT (spec, attr_kind);
5460 return NULL;
5463 /* Returns the parent of the declaration of DIE. */
5465 static dw_die_ref
5466 get_die_parent (dw_die_ref die)
5468 dw_die_ref t;
5470 if (!die)
5471 return NULL;
5473 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5474 || (t = get_AT_ref (die, DW_AT_specification)))
5475 die = t;
5477 return die->die_parent;
5480 /* Return the "low pc" attribute value, typically associated with a subprogram
5481 DIE. Return null if the "low pc" attribute is either not present, or if it
5482 cannot be represented as an assembler label identifier. */
5484 static inline const char *
5485 get_AT_low_pc (dw_die_ref die)
5487 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5489 return a ? AT_lbl (a) : NULL;
5492 /* Return the value of the string attribute designated by ATTR_KIND, or
5493 NULL if it is not present. */
5495 const char *
5496 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5498 dw_attr_node *a = get_AT (die, attr_kind);
5500 return a ? AT_string (a) : NULL;
5503 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5504 if it is not present. */
5507 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5509 dw_attr_node *a = get_AT (die, attr_kind);
5511 return a ? AT_flag (a) : 0;
5514 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5515 if it is not present. */
5517 unsigned
5518 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5520 dw_attr_node *a = get_AT (die, attr_kind);
5522 return a ? AT_unsigned (a) : 0;
5525 dw_die_ref
5526 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5528 dw_attr_node *a = get_AT (die, attr_kind);
5530 return a ? AT_ref (a) : NULL;
5533 struct dwarf_file_data *
5534 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5536 dw_attr_node *a = get_AT (die, attr_kind);
5538 return a ? AT_file (a) : NULL;
5541 /* Return TRUE if the language is C. */
5543 static inline bool
5544 is_c (void)
5546 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5548 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5549 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5554 /* Return TRUE if the language is C++. */
5556 static inline bool
5557 is_cxx (void)
5559 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5561 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5562 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5565 /* Return TRUE if DECL was created by the C++ frontend. */
5567 static bool
5568 is_cxx (const_tree decl)
5570 if (in_lto_p)
5572 const_tree context = get_ultimate_context (decl);
5573 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5574 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5576 return is_cxx ();
5579 /* Return TRUE if the language is Fortran. */
5581 static inline bool
5582 is_fortran (void)
5584 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5586 return (lang == DW_LANG_Fortran77
5587 || lang == DW_LANG_Fortran90
5588 || lang == DW_LANG_Fortran95
5589 || lang == DW_LANG_Fortran03
5590 || lang == DW_LANG_Fortran08);
5593 static inline bool
5594 is_fortran (const_tree decl)
5596 if (in_lto_p)
5598 const_tree context = get_ultimate_context (decl);
5599 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5600 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5601 "GNU Fortran", 11) == 0
5602 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5603 "GNU F77") == 0);
5605 return is_fortran ();
5608 /* Return TRUE if the language is Ada. */
5610 static inline bool
5611 is_ada (void)
5613 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5615 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5618 /* Return TRUE if the language is D. */
5620 static inline bool
5621 is_dlang (void)
5623 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5625 return lang == DW_LANG_D;
5628 /* Remove the specified attribute if present. Return TRUE if removal
5629 was successful. */
5631 static bool
5632 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5634 dw_attr_node *a;
5635 unsigned ix;
5637 if (! die)
5638 return false;
5640 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5641 if (a->dw_attr == attr_kind)
5643 if (AT_class (a) == dw_val_class_str)
5644 if (a->dw_attr_val.v.val_str->refcount)
5645 a->dw_attr_val.v.val_str->refcount--;
5647 /* vec::ordered_remove should help reduce the number of abbrevs
5648 that are needed. */
5649 die->die_attr->ordered_remove (ix);
5650 return true;
5652 return false;
5655 /* Remove CHILD from its parent. PREV must have the property that
5656 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5658 static void
5659 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5661 gcc_assert (child->die_parent == prev->die_parent);
5662 gcc_assert (prev->die_sib == child);
5663 if (prev == child)
5665 gcc_assert (child->die_parent->die_child == child);
5666 prev = NULL;
5668 else
5669 prev->die_sib = child->die_sib;
5670 if (child->die_parent->die_child == child)
5671 child->die_parent->die_child = prev;
5672 child->die_sib = NULL;
5675 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5676 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5678 static void
5679 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5681 dw_die_ref parent = old_child->die_parent;
5683 gcc_assert (parent == prev->die_parent);
5684 gcc_assert (prev->die_sib == old_child);
5686 new_child->die_parent = parent;
5687 if (prev == old_child)
5689 gcc_assert (parent->die_child == old_child);
5690 new_child->die_sib = new_child;
5692 else
5694 prev->die_sib = new_child;
5695 new_child->die_sib = old_child->die_sib;
5697 if (old_child->die_parent->die_child == old_child)
5698 old_child->die_parent->die_child = new_child;
5699 old_child->die_sib = NULL;
5702 /* Move all children from OLD_PARENT to NEW_PARENT. */
5704 static void
5705 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5707 dw_die_ref c;
5708 new_parent->die_child = old_parent->die_child;
5709 old_parent->die_child = NULL;
5710 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5713 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5714 matches TAG. */
5716 static void
5717 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5719 dw_die_ref c;
5721 c = die->die_child;
5722 if (c) do {
5723 dw_die_ref prev = c;
5724 c = c->die_sib;
5725 while (c->die_tag == tag)
5727 remove_child_with_prev (c, prev);
5728 c->die_parent = NULL;
5729 /* Might have removed every child. */
5730 if (die->die_child == NULL)
5731 return;
5732 c = prev->die_sib;
5734 } while (c != die->die_child);
5737 /* Add a CHILD_DIE as the last child of DIE. */
5739 static void
5740 add_child_die (dw_die_ref die, dw_die_ref child_die)
5742 /* FIXME this should probably be an assert. */
5743 if (! die || ! child_die)
5744 return;
5745 gcc_assert (die != child_die);
5747 child_die->die_parent = die;
5748 if (die->die_child)
5750 child_die->die_sib = die->die_child->die_sib;
5751 die->die_child->die_sib = child_die;
5753 else
5754 child_die->die_sib = child_die;
5755 die->die_child = child_die;
5758 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5760 static void
5761 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5762 dw_die_ref after_die)
5764 gcc_assert (die
5765 && child_die
5766 && after_die
5767 && die->die_child
5768 && die != child_die);
5770 child_die->die_parent = die;
5771 child_die->die_sib = after_die->die_sib;
5772 after_die->die_sib = child_die;
5773 if (die->die_child == after_die)
5774 die->die_child = child_die;
5777 /* Unassociate CHILD from its parent, and make its parent be
5778 NEW_PARENT. */
5780 static void
5781 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5783 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5784 if (p->die_sib == child)
5786 remove_child_with_prev (child, p);
5787 break;
5789 add_child_die (new_parent, child);
5792 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5793 is the specification, to the end of PARENT's list of children.
5794 This is done by removing and re-adding it. */
5796 static void
5797 splice_child_die (dw_die_ref parent, dw_die_ref child)
5799 /* We want the declaration DIE from inside the class, not the
5800 specification DIE at toplevel. */
5801 if (child->die_parent != parent)
5803 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5805 if (tmp)
5806 child = tmp;
5809 gcc_assert (child->die_parent == parent
5810 || (child->die_parent
5811 == get_AT_ref (parent, DW_AT_specification)));
5813 reparent_child (child, parent);
5816 /* Create and return a new die with TAG_VALUE as tag. */
5818 dw_die_ref
5819 new_die_raw (enum dwarf_tag tag_value)
5821 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5822 die->die_tag = tag_value;
5823 return die;
5826 /* Create and return a new die with a parent of PARENT_DIE. If
5827 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5828 associated tree T must be supplied to determine parenthood
5829 later. */
5831 static inline dw_die_ref
5832 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5834 dw_die_ref die = new_die_raw (tag_value);
5836 if (parent_die != NULL)
5837 add_child_die (parent_die, die);
5838 else
5840 limbo_die_node *limbo_node;
5842 /* No DIEs created after early dwarf should end up in limbo,
5843 because the limbo list should not persist past LTO
5844 streaming. */
5845 if (tag_value != DW_TAG_compile_unit
5846 /* These are allowed because they're generated while
5847 breaking out COMDAT units late. */
5848 && tag_value != DW_TAG_type_unit
5849 && tag_value != DW_TAG_skeleton_unit
5850 && !early_dwarf
5851 /* Allow nested functions to live in limbo because they will
5852 only temporarily live there, as decls_for_scope will fix
5853 them up. */
5854 && (TREE_CODE (t) != FUNCTION_DECL
5855 || !decl_function_context (t))
5856 /* Same as nested functions above but for types. Types that
5857 are local to a function will be fixed in
5858 decls_for_scope. */
5859 && (!RECORD_OR_UNION_TYPE_P (t)
5860 || !TYPE_CONTEXT (t)
5861 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5862 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5863 especially in the ltrans stage, but once we implement LTO
5864 dwarf streaming, we should remove this exception. */
5865 && !in_lto_p)
5867 fprintf (stderr, "symbol ended up in limbo too late:");
5868 debug_generic_stmt (t);
5869 gcc_unreachable ();
5872 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5873 limbo_node->die = die;
5874 limbo_node->created_for = t;
5875 limbo_node->next = limbo_die_list;
5876 limbo_die_list = limbo_node;
5879 return die;
5882 /* Return the DIE associated with the given type specifier. */
5884 dw_die_ref
5885 lookup_type_die (tree type)
5887 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5888 if (die && die->removed)
5890 TYPE_SYMTAB_DIE (type) = NULL;
5891 return NULL;
5893 return die;
5896 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5897 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5898 anonymous type instead the one of the naming typedef. */
5900 static inline dw_die_ref
5901 strip_naming_typedef (tree type, dw_die_ref type_die)
5903 if (type
5904 && TREE_CODE (type) == RECORD_TYPE
5905 && type_die
5906 && type_die->die_tag == DW_TAG_typedef
5907 && is_naming_typedef_decl (TYPE_NAME (type)))
5908 type_die = get_AT_ref (type_die, DW_AT_type);
5909 return type_die;
5912 /* Like lookup_type_die, but if type is an anonymous type named by a
5913 typedef[1], return the DIE of the anonymous type instead the one of
5914 the naming typedef. This is because in gen_typedef_die, we did
5915 equate the anonymous struct named by the typedef with the DIE of
5916 the naming typedef. So by default, lookup_type_die on an anonymous
5917 struct yields the DIE of the naming typedef.
5919 [1]: Read the comment of is_naming_typedef_decl to learn about what
5920 a naming typedef is. */
5922 static inline dw_die_ref
5923 lookup_type_die_strip_naming_typedef (tree type)
5925 dw_die_ref die = lookup_type_die (type);
5926 return strip_naming_typedef (type, die);
5929 /* Equate a DIE to a given type specifier. */
5931 static inline void
5932 equate_type_number_to_die (tree type, dw_die_ref type_die)
5934 TYPE_SYMTAB_DIE (type) = type_die;
5937 static dw_die_ref maybe_create_die_with_external_ref (tree);
5938 struct GTY(()) sym_off_pair
5940 const char * GTY((skip)) sym;
5941 unsigned HOST_WIDE_INT off;
5943 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5945 /* Returns a hash value for X (which really is a die_struct). */
5947 inline hashval_t
5948 decl_die_hasher::hash (die_node *x)
5950 return (hashval_t) x->decl_id;
5953 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5955 inline bool
5956 decl_die_hasher::equal (die_node *x, tree y)
5958 return (x->decl_id == DECL_UID (y));
5961 /* Return the DIE associated with a given declaration. */
5963 dw_die_ref
5964 lookup_decl_die (tree decl)
5966 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5967 NO_INSERT);
5968 if (!die)
5970 if (in_lto_p)
5971 return maybe_create_die_with_external_ref (decl);
5972 return NULL;
5974 if ((*die)->removed)
5976 decl_die_table->clear_slot (die);
5977 return NULL;
5979 return *die;
5983 /* Return the DIE associated with BLOCK. */
5985 static inline dw_die_ref
5986 lookup_block_die (tree block)
5988 dw_die_ref die = BLOCK_DIE (block);
5989 if (!die && in_lto_p)
5990 return maybe_create_die_with_external_ref (block);
5991 return die;
5994 /* Associate DIE with BLOCK. */
5996 static inline void
5997 equate_block_to_die (tree block, dw_die_ref die)
5999 BLOCK_DIE (block) = die;
6001 #undef BLOCK_DIE
6004 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6005 style reference. Return true if we found one refering to a DIE for
6006 DECL, otherwise return false. */
6008 static bool
6009 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6010 unsigned HOST_WIDE_INT *off)
6012 dw_die_ref die;
6014 if (in_lto_p)
6016 /* During WPA stage and incremental linking we use a hash-map
6017 to store the decl <-> label + offset map. */
6018 if (!external_die_map)
6019 return false;
6020 sym_off_pair *desc = external_die_map->get (decl);
6021 if (!desc)
6022 return false;
6023 *sym = desc->sym;
6024 *off = desc->off;
6025 return true;
6028 if (TREE_CODE (decl) == BLOCK)
6029 die = lookup_block_die (decl);
6030 else
6031 die = lookup_decl_die (decl);
6032 if (!die)
6033 return false;
6035 /* Similar to get_ref_die_offset_label, but using the "correct"
6036 label. */
6037 *off = die->die_offset;
6038 while (die->die_parent)
6039 die = die->die_parent;
6040 /* For the containing CU DIE we compute a die_symbol in
6041 compute_comp_unit_symbol. */
6042 gcc_assert (die->die_tag == DW_TAG_compile_unit
6043 && die->die_id.die_symbol != NULL);
6044 *sym = die->die_id.die_symbol;
6045 return true;
6048 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6050 static void
6051 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6052 const char *symbol, HOST_WIDE_INT offset)
6054 /* Create a fake DIE that contains the reference. Don't use
6055 new_die because we don't want to end up in the limbo list. */
6056 /* ??? We probably want to share these, thus put a ref to the DIE
6057 we create here to the external_die_map entry. */
6058 dw_die_ref ref = new_die_raw (die->die_tag);
6059 ref->die_id.die_symbol = symbol;
6060 ref->die_offset = offset;
6061 ref->with_offset = 1;
6062 add_AT_die_ref (die, attr_kind, ref);
6065 /* Create a DIE for DECL if required and add a reference to a DIE
6066 at SYMBOL + OFFSET which contains attributes dumped early. */
6068 static void
6069 dwarf2out_register_external_die (tree decl, const char *sym,
6070 unsigned HOST_WIDE_INT off)
6072 if (debug_info_level == DINFO_LEVEL_NONE)
6073 return;
6075 if (!external_die_map)
6076 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6077 gcc_checking_assert (!external_die_map->get (decl));
6078 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6079 external_die_map->put (decl, p);
6082 /* If we have a registered external DIE for DECL return a new DIE for
6083 the concrete instance with an appropriate abstract origin. */
6085 static dw_die_ref
6086 maybe_create_die_with_external_ref (tree decl)
6088 if (!external_die_map)
6089 return NULL;
6090 sym_off_pair *desc = external_die_map->get (decl);
6091 if (!desc)
6092 return NULL;
6094 const char *sym = desc->sym;
6095 unsigned HOST_WIDE_INT off = desc->off;
6096 external_die_map->remove (decl);
6098 in_lto_p = false;
6099 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6100 ? lookup_block_die (decl) : lookup_decl_die (decl));
6101 gcc_assert (!die);
6102 in_lto_p = true;
6104 tree ctx;
6105 dw_die_ref parent = NULL;
6106 /* Need to lookup a DIE for the decls context - the containing
6107 function or translation unit. */
6108 if (TREE_CODE (decl) == BLOCK)
6110 ctx = BLOCK_SUPERCONTEXT (decl);
6111 /* ??? We do not output DIEs for all scopes thus skip as
6112 many DIEs as needed. */
6113 while (TREE_CODE (ctx) == BLOCK
6114 && !lookup_block_die (ctx))
6115 ctx = BLOCK_SUPERCONTEXT (ctx);
6117 else
6118 ctx = DECL_CONTEXT (decl);
6119 /* Peel types in the context stack. */
6120 while (ctx && TYPE_P (ctx))
6121 ctx = TYPE_CONTEXT (ctx);
6122 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6123 if (debug_info_level <= DINFO_LEVEL_TERSE)
6124 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6125 ctx = DECL_CONTEXT (ctx);
6126 if (ctx)
6128 if (TREE_CODE (ctx) == BLOCK)
6129 parent = lookup_block_die (ctx);
6130 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6131 /* Keep the 1:1 association during WPA. */
6132 && !flag_wpa
6133 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6134 /* Otherwise all late annotations go to the main CU which
6135 imports the original CUs. */
6136 parent = comp_unit_die ();
6137 else if (TREE_CODE (ctx) == FUNCTION_DECL
6138 && TREE_CODE (decl) != FUNCTION_DECL
6139 && TREE_CODE (decl) != PARM_DECL
6140 && TREE_CODE (decl) != RESULT_DECL
6141 && TREE_CODE (decl) != BLOCK)
6142 /* Leave function local entities parent determination to when
6143 we process scope vars. */
6145 else
6146 parent = lookup_decl_die (ctx);
6148 else
6149 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6150 Handle this case gracefully by globalizing stuff. */
6151 parent = comp_unit_die ();
6152 /* Create a DIE "stub". */
6153 switch (TREE_CODE (decl))
6155 case TRANSLATION_UNIT_DECL:
6157 die = comp_unit_die ();
6158 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6159 to create a DIE for the original CUs. */
6160 return die;
6162 case NAMESPACE_DECL:
6163 if (is_fortran (decl))
6164 die = new_die (DW_TAG_module, parent, decl);
6165 else
6166 die = new_die (DW_TAG_namespace, parent, decl);
6167 break;
6168 case FUNCTION_DECL:
6169 die = new_die (DW_TAG_subprogram, parent, decl);
6170 break;
6171 case VAR_DECL:
6172 die = new_die (DW_TAG_variable, parent, decl);
6173 break;
6174 case RESULT_DECL:
6175 die = new_die (DW_TAG_variable, parent, decl);
6176 break;
6177 case PARM_DECL:
6178 die = new_die (DW_TAG_formal_parameter, parent, decl);
6179 break;
6180 case CONST_DECL:
6181 die = new_die (DW_TAG_constant, parent, decl);
6182 break;
6183 case LABEL_DECL:
6184 die = new_die (DW_TAG_label, parent, decl);
6185 break;
6186 case BLOCK:
6187 die = new_die (DW_TAG_lexical_block, parent, decl);
6188 break;
6189 default:
6190 gcc_unreachable ();
6192 if (TREE_CODE (decl) == BLOCK)
6193 equate_block_to_die (decl, die);
6194 else
6195 equate_decl_number_to_die (decl, die);
6197 add_desc_attribute (die, decl);
6199 /* Add a reference to the DIE providing early debug at $sym + off. */
6200 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6202 return die;
6205 /* Returns a hash value for X (which really is a var_loc_list). */
6207 inline hashval_t
6208 decl_loc_hasher::hash (var_loc_list *x)
6210 return (hashval_t) x->decl_id;
6213 /* Return nonzero if decl_id of var_loc_list X is the same as
6214 UID of decl *Y. */
6216 inline bool
6217 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6219 return (x->decl_id == DECL_UID (y));
6222 /* Return the var_loc list associated with a given declaration. */
6224 static inline var_loc_list *
6225 lookup_decl_loc (const_tree decl)
6227 if (!decl_loc_table)
6228 return NULL;
6229 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6232 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6234 inline hashval_t
6235 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6237 return (hashval_t) x->decl_id;
6240 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6241 UID of decl *Y. */
6243 inline bool
6244 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6246 return (x->decl_id == DECL_UID (y));
6249 /* Equate a DIE to a particular declaration. */
6251 static void
6252 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6254 unsigned int decl_id = DECL_UID (decl);
6256 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6257 decl_die->decl_id = decl_id;
6260 /* Return how many bits covers PIECE EXPR_LIST. */
6262 static HOST_WIDE_INT
6263 decl_piece_bitsize (rtx piece)
6265 int ret = (int) GET_MODE (piece);
6266 if (ret)
6267 return ret;
6268 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6269 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6270 return INTVAL (XEXP (XEXP (piece, 0), 0));
6273 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6275 static rtx *
6276 decl_piece_varloc_ptr (rtx piece)
6278 if ((int) GET_MODE (piece))
6279 return &XEXP (piece, 0);
6280 else
6281 return &XEXP (XEXP (piece, 0), 1);
6284 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6285 Next is the chain of following piece nodes. */
6287 static rtx_expr_list *
6288 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6290 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6291 return alloc_EXPR_LIST (bitsize, loc_note, next);
6292 else
6293 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6294 GEN_INT (bitsize),
6295 loc_note), next);
6298 /* Return rtx that should be stored into loc field for
6299 LOC_NOTE and BITPOS/BITSIZE. */
6301 static rtx
6302 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6303 HOST_WIDE_INT bitsize)
6305 if (bitsize != -1)
6307 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6308 if (bitpos != 0)
6309 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6311 return loc_note;
6314 /* This function either modifies location piece list *DEST in
6315 place (if SRC and INNER is NULL), or copies location piece list
6316 *SRC to *DEST while modifying it. Location BITPOS is modified
6317 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6318 not copied and if needed some padding around it is added.
6319 When modifying in place, DEST should point to EXPR_LIST where
6320 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6321 to the start of the whole list and INNER points to the EXPR_LIST
6322 where earlier pieces cover PIECE_BITPOS bits. */
6324 static void
6325 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6326 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6327 HOST_WIDE_INT bitsize, rtx loc_note)
6329 HOST_WIDE_INT diff;
6330 bool copy = inner != NULL;
6332 if (copy)
6334 /* First copy all nodes preceding the current bitpos. */
6335 while (src != inner)
6337 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6338 decl_piece_bitsize (*src), NULL_RTX);
6339 dest = &XEXP (*dest, 1);
6340 src = &XEXP (*src, 1);
6343 /* Add padding if needed. */
6344 if (bitpos != piece_bitpos)
6346 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6347 copy ? NULL_RTX : *dest);
6348 dest = &XEXP (*dest, 1);
6350 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6352 gcc_assert (!copy);
6353 /* A piece with correct bitpos and bitsize already exist,
6354 just update the location for it and return. */
6355 *decl_piece_varloc_ptr (*dest) = loc_note;
6356 return;
6358 /* Add the piece that changed. */
6359 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6360 dest = &XEXP (*dest, 1);
6361 /* Skip over pieces that overlap it. */
6362 diff = bitpos - piece_bitpos + bitsize;
6363 if (!copy)
6364 src = dest;
6365 while (diff > 0 && *src)
6367 rtx piece = *src;
6368 diff -= decl_piece_bitsize (piece);
6369 if (copy)
6370 src = &XEXP (piece, 1);
6371 else
6373 *src = XEXP (piece, 1);
6374 free_EXPR_LIST_node (piece);
6377 /* Add padding if needed. */
6378 if (diff < 0 && *src)
6380 if (!copy)
6381 dest = src;
6382 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6383 dest = &XEXP (*dest, 1);
6385 if (!copy)
6386 return;
6387 /* Finally copy all nodes following it. */
6388 while (*src)
6390 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6391 decl_piece_bitsize (*src), NULL_RTX);
6392 dest = &XEXP (*dest, 1);
6393 src = &XEXP (*src, 1);
6397 /* Add a variable location node to the linked list for DECL. */
6399 static struct var_loc_node *
6400 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6402 unsigned int decl_id;
6403 var_loc_list *temp;
6404 struct var_loc_node *loc = NULL;
6405 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6407 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6409 tree realdecl = DECL_DEBUG_EXPR (decl);
6410 if (handled_component_p (realdecl)
6411 || (TREE_CODE (realdecl) == MEM_REF
6412 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6414 bool reverse;
6415 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6416 &bitsize, &reverse);
6417 if (!innerdecl
6418 || !DECL_P (innerdecl)
6419 || DECL_IGNORED_P (innerdecl)
6420 || TREE_STATIC (innerdecl)
6421 || bitsize == 0
6422 || bitpos + bitsize > 256)
6423 return NULL;
6424 decl = innerdecl;
6428 decl_id = DECL_UID (decl);
6429 var_loc_list **slot
6430 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6431 if (*slot == NULL)
6433 temp = ggc_cleared_alloc<var_loc_list> ();
6434 temp->decl_id = decl_id;
6435 *slot = temp;
6437 else
6438 temp = *slot;
6440 /* For PARM_DECLs try to keep around the original incoming value,
6441 even if that means we'll emit a zero-range .debug_loc entry. */
6442 if (temp->last
6443 && temp->first == temp->last
6444 && TREE_CODE (decl) == PARM_DECL
6445 && NOTE_P (temp->first->loc)
6446 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6447 && DECL_INCOMING_RTL (decl)
6448 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6449 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6450 == GET_CODE (DECL_INCOMING_RTL (decl))
6451 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6452 && (bitsize != -1
6453 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6454 NOTE_VAR_LOCATION_LOC (loc_note))
6455 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6456 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6458 loc = ggc_cleared_alloc<var_loc_node> ();
6459 temp->first->next = loc;
6460 temp->last = loc;
6461 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6463 else if (temp->last)
6465 struct var_loc_node *last = temp->last, *unused = NULL;
6466 rtx *piece_loc = NULL, last_loc_note;
6467 HOST_WIDE_INT piece_bitpos = 0;
6468 if (last->next)
6470 last = last->next;
6471 gcc_assert (last->next == NULL);
6473 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6475 piece_loc = &last->loc;
6478 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6479 if (piece_bitpos + cur_bitsize > bitpos)
6480 break;
6481 piece_bitpos += cur_bitsize;
6482 piece_loc = &XEXP (*piece_loc, 1);
6484 while (*piece_loc);
6486 /* TEMP->LAST here is either pointer to the last but one or
6487 last element in the chained list, LAST is pointer to the
6488 last element. */
6489 if (label && strcmp (last->label, label) == 0 && last->view == view)
6491 /* For SRA optimized variables if there weren't any real
6492 insns since last note, just modify the last node. */
6493 if (piece_loc != NULL)
6495 adjust_piece_list (piece_loc, NULL, NULL,
6496 bitpos, piece_bitpos, bitsize, loc_note);
6497 return NULL;
6499 /* If the last note doesn't cover any instructions, remove it. */
6500 if (temp->last != last)
6502 temp->last->next = NULL;
6503 unused = last;
6504 last = temp->last;
6505 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6507 else
6509 gcc_assert (temp->first == temp->last
6510 || (temp->first->next == temp->last
6511 && TREE_CODE (decl) == PARM_DECL));
6512 memset (temp->last, '\0', sizeof (*temp->last));
6513 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6514 return temp->last;
6517 if (bitsize == -1 && NOTE_P (last->loc))
6518 last_loc_note = last->loc;
6519 else if (piece_loc != NULL
6520 && *piece_loc != NULL_RTX
6521 && piece_bitpos == bitpos
6522 && decl_piece_bitsize (*piece_loc) == bitsize)
6523 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6524 else
6525 last_loc_note = NULL_RTX;
6526 /* If the current location is the same as the end of the list,
6527 and either both or neither of the locations is uninitialized,
6528 we have nothing to do. */
6529 if (last_loc_note == NULL_RTX
6530 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6531 NOTE_VAR_LOCATION_LOC (loc_note)))
6532 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6533 != NOTE_VAR_LOCATION_STATUS (loc_note))
6534 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6535 == VAR_INIT_STATUS_UNINITIALIZED)
6536 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6537 == VAR_INIT_STATUS_UNINITIALIZED))))
6539 /* Add LOC to the end of list and update LAST. If the last
6540 element of the list has been removed above, reuse its
6541 memory for the new node, otherwise allocate a new one. */
6542 if (unused)
6544 loc = unused;
6545 memset (loc, '\0', sizeof (*loc));
6547 else
6548 loc = ggc_cleared_alloc<var_loc_node> ();
6549 if (bitsize == -1 || piece_loc == NULL)
6550 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6551 else
6552 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6553 bitpos, piece_bitpos, bitsize, loc_note);
6554 last->next = loc;
6555 /* Ensure TEMP->LAST will point either to the new last but one
6556 element of the chain, or to the last element in it. */
6557 if (last != temp->last)
6558 temp->last = last;
6560 else if (unused)
6561 ggc_free (unused);
6563 else
6565 loc = ggc_cleared_alloc<var_loc_node> ();
6566 temp->first = loc;
6567 temp->last = loc;
6568 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6570 return loc;
6573 /* Keep track of the number of spaces used to indent the
6574 output of the debugging routines that print the structure of
6575 the DIE internal representation. */
6576 static int print_indent;
6578 /* Indent the line the number of spaces given by print_indent. */
6580 static inline void
6581 print_spaces (FILE *outfile)
6583 fprintf (outfile, "%*s", print_indent, "");
6586 /* Print a type signature in hex. */
6588 static inline void
6589 print_signature (FILE *outfile, char *sig)
6591 int i;
6593 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6594 fprintf (outfile, "%02x", sig[i] & 0xff);
6597 static inline void
6598 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6600 if (discr_value->pos)
6601 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6602 else
6603 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6606 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6608 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6609 RECURSE, output location descriptor operations. */
6611 static void
6612 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6614 switch (val->val_class)
6616 case dw_val_class_addr:
6617 fprintf (outfile, "address");
6618 break;
6619 case dw_val_class_offset:
6620 fprintf (outfile, "offset");
6621 break;
6622 case dw_val_class_loc:
6623 fprintf (outfile, "location descriptor");
6624 if (val->v.val_loc == NULL)
6625 fprintf (outfile, " -> <null>\n");
6626 else if (recurse)
6628 fprintf (outfile, ":\n");
6629 print_indent += 4;
6630 print_loc_descr (val->v.val_loc, outfile);
6631 print_indent -= 4;
6633 else
6635 if (flag_dump_noaddr || flag_dump_unnumbered)
6636 fprintf (outfile, " #\n");
6637 else
6638 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6640 break;
6641 case dw_val_class_loc_list:
6642 fprintf (outfile, "location list -> label:%s",
6643 val->v.val_loc_list->ll_symbol);
6644 break;
6645 case dw_val_class_view_list:
6646 val = view_list_to_loc_list_val_node (val);
6647 fprintf (outfile, "location list with views -> labels:%s and %s",
6648 val->v.val_loc_list->ll_symbol,
6649 val->v.val_loc_list->vl_symbol);
6650 break;
6651 case dw_val_class_range_list:
6652 fprintf (outfile, "range list");
6653 break;
6654 case dw_val_class_const:
6655 case dw_val_class_const_implicit:
6656 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6657 break;
6658 case dw_val_class_unsigned_const:
6659 case dw_val_class_unsigned_const_implicit:
6660 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6661 break;
6662 case dw_val_class_const_double:
6663 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6664 HOST_WIDE_INT_PRINT_UNSIGNED")",
6665 val->v.val_double.high,
6666 val->v.val_double.low);
6667 break;
6668 case dw_val_class_wide_int:
6670 int i = val->v.val_wide->get_len ();
6671 fprintf (outfile, "constant (");
6672 gcc_assert (i > 0);
6673 if (val->v.val_wide->elt (i - 1) == 0)
6674 fprintf (outfile, "0x");
6675 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6676 val->v.val_wide->elt (--i));
6677 while (--i >= 0)
6678 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6679 val->v.val_wide->elt (i));
6680 fprintf (outfile, ")");
6681 break;
6683 case dw_val_class_vec:
6684 fprintf (outfile, "floating-point or vector constant");
6685 break;
6686 case dw_val_class_flag:
6687 fprintf (outfile, "%u", val->v.val_flag);
6688 break;
6689 case dw_val_class_die_ref:
6690 if (val->v.val_die_ref.die != NULL)
6692 dw_die_ref die = val->v.val_die_ref.die;
6694 if (die->comdat_type_p)
6696 fprintf (outfile, "die -> signature: ");
6697 print_signature (outfile,
6698 die->die_id.die_type_node->signature);
6700 else if (die->die_id.die_symbol)
6702 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6703 if (die->with_offset)
6704 fprintf (outfile, " + %ld", die->die_offset);
6706 else
6707 fprintf (outfile, "die -> %ld", die->die_offset);
6708 if (flag_dump_noaddr || flag_dump_unnumbered)
6709 fprintf (outfile, " #");
6710 else
6711 fprintf (outfile, " (%p)", (void *) die);
6713 else
6714 fprintf (outfile, "die -> <null>");
6715 break;
6716 case dw_val_class_vms_delta:
6717 fprintf (outfile, "delta: @slotcount(%s-%s)",
6718 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6719 break;
6720 case dw_val_class_symview:
6721 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6722 break;
6723 case dw_val_class_lbl_id:
6724 case dw_val_class_lineptr:
6725 case dw_val_class_macptr:
6726 case dw_val_class_loclistsptr:
6727 case dw_val_class_high_pc:
6728 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6729 break;
6730 case dw_val_class_str:
6731 if (val->v.val_str->str != NULL)
6732 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6733 else
6734 fprintf (outfile, "<null>");
6735 break;
6736 case dw_val_class_file:
6737 case dw_val_class_file_implicit:
6738 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6739 val->v.val_file->emitted_number);
6740 break;
6741 case dw_val_class_data8:
6743 int i;
6745 for (i = 0; i < 8; i++)
6746 fprintf (outfile, "%02x", val->v.val_data8[i]);
6747 break;
6749 case dw_val_class_discr_value:
6750 print_discr_value (outfile, &val->v.val_discr_value);
6751 break;
6752 case dw_val_class_discr_list:
6753 for (dw_discr_list_ref node = val->v.val_discr_list;
6754 node != NULL;
6755 node = node->dw_discr_next)
6757 if (node->dw_discr_range)
6759 fprintf (outfile, " .. ");
6760 print_discr_value (outfile, &node->dw_discr_lower_bound);
6761 print_discr_value (outfile, &node->dw_discr_upper_bound);
6763 else
6764 print_discr_value (outfile, &node->dw_discr_lower_bound);
6766 if (node->dw_discr_next != NULL)
6767 fprintf (outfile, " | ");
6769 default:
6770 break;
6774 /* Likewise, for a DIE attribute. */
6776 static void
6777 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6779 print_dw_val (&a->dw_attr_val, recurse, outfile);
6783 /* Print the list of operands in the LOC location description to OUTFILE. This
6784 routine is a debugging aid only. */
6786 static void
6787 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6789 dw_loc_descr_ref l = loc;
6791 if (loc == NULL)
6793 print_spaces (outfile);
6794 fprintf (outfile, "<null>\n");
6795 return;
6798 for (l = loc; l != NULL; l = l->dw_loc_next)
6800 print_spaces (outfile);
6801 if (flag_dump_noaddr || flag_dump_unnumbered)
6802 fprintf (outfile, "#");
6803 else
6804 fprintf (outfile, "(%p)", (void *) l);
6805 fprintf (outfile, " %s",
6806 dwarf_stack_op_name (l->dw_loc_opc));
6807 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6809 fprintf (outfile, " ");
6810 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6812 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6814 fprintf (outfile, ", ");
6815 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6817 fprintf (outfile, "\n");
6821 /* Print the information associated with a given DIE, and its children.
6822 This routine is a debugging aid only. */
6824 static void
6825 print_die (dw_die_ref die, FILE *outfile)
6827 dw_attr_node *a;
6828 dw_die_ref c;
6829 unsigned ix;
6831 print_spaces (outfile);
6832 fprintf (outfile, "DIE %4ld: %s ",
6833 die->die_offset, dwarf_tag_name (die->die_tag));
6834 if (flag_dump_noaddr || flag_dump_unnumbered)
6835 fprintf (outfile, "#\n");
6836 else
6837 fprintf (outfile, "(%p)\n", (void*) die);
6838 print_spaces (outfile);
6839 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6840 fprintf (outfile, " offset: %ld", die->die_offset);
6841 fprintf (outfile, " mark: %d\n", die->die_mark);
6843 if (die->comdat_type_p)
6845 print_spaces (outfile);
6846 fprintf (outfile, " signature: ");
6847 print_signature (outfile, die->die_id.die_type_node->signature);
6848 fprintf (outfile, "\n");
6851 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6853 print_spaces (outfile);
6854 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6856 print_attribute (a, true, outfile);
6857 fprintf (outfile, "\n");
6860 if (die->die_child != NULL)
6862 print_indent += 4;
6863 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6864 print_indent -= 4;
6866 if (print_indent == 0)
6867 fprintf (outfile, "\n");
6870 /* Print the list of operations in the LOC location description. */
6872 DEBUG_FUNCTION void
6873 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6875 print_loc_descr (loc, stderr);
6878 /* Print the information collected for a given DIE. */
6880 DEBUG_FUNCTION void
6881 debug_dwarf_die (dw_die_ref die)
6883 print_die (die, stderr);
6886 DEBUG_FUNCTION void
6887 debug (die_struct &ref)
6889 print_die (&ref, stderr);
6892 DEBUG_FUNCTION void
6893 debug (die_struct *ptr)
6895 if (ptr)
6896 debug (*ptr);
6897 else
6898 fprintf (stderr, "<nil>\n");
6902 /* Print all DWARF information collected for the compilation unit.
6903 This routine is a debugging aid only. */
6905 DEBUG_FUNCTION void
6906 debug_dwarf (void)
6908 print_indent = 0;
6909 print_die (comp_unit_die (), stderr);
6912 /* Verify the DIE tree structure. */
6914 DEBUG_FUNCTION void
6915 verify_die (dw_die_ref die)
6917 gcc_assert (!die->die_mark);
6918 if (die->die_parent == NULL
6919 && die->die_sib == NULL)
6920 return;
6921 /* Verify the die_sib list is cyclic. */
6922 dw_die_ref x = die;
6925 x->die_mark = 1;
6926 x = x->die_sib;
6928 while (x && !x->die_mark);
6929 gcc_assert (x == die);
6930 x = die;
6933 /* Verify all dies have the same parent. */
6934 gcc_assert (x->die_parent == die->die_parent);
6935 if (x->die_child)
6937 /* Verify the child has the proper parent and recurse. */
6938 gcc_assert (x->die_child->die_parent == x);
6939 verify_die (x->die_child);
6941 x->die_mark = 0;
6942 x = x->die_sib;
6944 while (x && x->die_mark);
6947 /* Sanity checks on DIEs. */
6949 static void
6950 check_die (dw_die_ref die)
6952 unsigned ix;
6953 dw_attr_node *a;
6954 bool inline_found = false;
6955 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6956 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6959 switch (a->dw_attr)
6961 case DW_AT_inline:
6962 if (a->dw_attr_val.v.val_unsigned)
6963 inline_found = true;
6964 break;
6965 case DW_AT_location:
6966 ++n_location;
6967 break;
6968 case DW_AT_low_pc:
6969 ++n_low_pc;
6970 break;
6971 case DW_AT_high_pc:
6972 ++n_high_pc;
6973 break;
6974 case DW_AT_artificial:
6975 ++n_artificial;
6976 break;
6977 case DW_AT_decl_column:
6978 ++n_decl_column;
6979 break;
6980 case DW_AT_decl_line:
6981 ++n_decl_line;
6982 break;
6983 case DW_AT_decl_file:
6984 ++n_decl_file;
6985 break;
6986 default:
6987 break;
6990 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6991 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6993 fprintf (stderr, "Duplicate attributes in DIE:\n");
6994 debug_dwarf_die (die);
6995 gcc_unreachable ();
6997 if (inline_found)
6999 /* A debugging information entry that is a member of an abstract
7000 instance tree [that has DW_AT_inline] should not contain any
7001 attributes which describe aspects of the subroutine which vary
7002 between distinct inlined expansions or distinct out-of-line
7003 expansions. */
7004 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7005 gcc_assert (a->dw_attr != DW_AT_low_pc
7006 && a->dw_attr != DW_AT_high_pc
7007 && a->dw_attr != DW_AT_location
7008 && a->dw_attr != DW_AT_frame_base
7009 && a->dw_attr != DW_AT_call_all_calls
7010 && a->dw_attr != DW_AT_GNU_all_call_sites);
7014 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7015 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7016 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7018 /* Calculate the checksum of a location expression. */
7020 static inline void
7021 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7023 int tem;
7024 inchash::hash hstate;
7025 hashval_t hash;
7027 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7028 CHECKSUM (tem);
7029 hash_loc_operands (loc, hstate);
7030 hash = hstate.end();
7031 CHECKSUM (hash);
7034 /* Calculate the checksum of an attribute. */
7036 static void
7037 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7039 dw_loc_descr_ref loc;
7040 rtx r;
7042 CHECKSUM (at->dw_attr);
7044 /* We don't care that this was compiled with a different compiler
7045 snapshot; if the output is the same, that's what matters. */
7046 if (at->dw_attr == DW_AT_producer)
7047 return;
7049 switch (AT_class (at))
7051 case dw_val_class_const:
7052 case dw_val_class_const_implicit:
7053 CHECKSUM (at->dw_attr_val.v.val_int);
7054 break;
7055 case dw_val_class_unsigned_const:
7056 case dw_val_class_unsigned_const_implicit:
7057 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7058 break;
7059 case dw_val_class_const_double:
7060 CHECKSUM (at->dw_attr_val.v.val_double);
7061 break;
7062 case dw_val_class_wide_int:
7063 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7064 get_full_len (*at->dw_attr_val.v.val_wide)
7065 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7066 break;
7067 case dw_val_class_vec:
7068 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7069 (at->dw_attr_val.v.val_vec.length
7070 * at->dw_attr_val.v.val_vec.elt_size));
7071 break;
7072 case dw_val_class_flag:
7073 CHECKSUM (at->dw_attr_val.v.val_flag);
7074 break;
7075 case dw_val_class_str:
7076 CHECKSUM_STRING (AT_string (at));
7077 break;
7079 case dw_val_class_addr:
7080 r = AT_addr (at);
7081 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7082 CHECKSUM_STRING (XSTR (r, 0));
7083 break;
7085 case dw_val_class_offset:
7086 CHECKSUM (at->dw_attr_val.v.val_offset);
7087 break;
7089 case dw_val_class_loc:
7090 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7091 loc_checksum (loc, ctx);
7092 break;
7094 case dw_val_class_die_ref:
7095 die_checksum (AT_ref (at), ctx, mark);
7096 break;
7098 case dw_val_class_fde_ref:
7099 case dw_val_class_vms_delta:
7100 case dw_val_class_symview:
7101 case dw_val_class_lbl_id:
7102 case dw_val_class_lineptr:
7103 case dw_val_class_macptr:
7104 case dw_val_class_loclistsptr:
7105 case dw_val_class_high_pc:
7106 break;
7108 case dw_val_class_file:
7109 case dw_val_class_file_implicit:
7110 CHECKSUM_STRING (AT_file (at)->filename);
7111 break;
7113 case dw_val_class_data8:
7114 CHECKSUM (at->dw_attr_val.v.val_data8);
7115 break;
7117 default:
7118 break;
7122 /* Calculate the checksum of a DIE. */
7124 static void
7125 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7127 dw_die_ref c;
7128 dw_attr_node *a;
7129 unsigned ix;
7131 /* To avoid infinite recursion. */
7132 if (die->die_mark)
7134 CHECKSUM (die->die_mark);
7135 return;
7137 die->die_mark = ++(*mark);
7139 CHECKSUM (die->die_tag);
7141 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7142 attr_checksum (a, ctx, mark);
7144 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7147 #undef CHECKSUM
7148 #undef CHECKSUM_BLOCK
7149 #undef CHECKSUM_STRING
7151 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7152 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7153 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7154 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7155 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7156 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7157 #define CHECKSUM_ATTR(FOO) \
7158 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7160 /* Calculate the checksum of a number in signed LEB128 format. */
7162 static void
7163 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7165 unsigned char byte;
7166 bool more;
7168 while (1)
7170 byte = (value & 0x7f);
7171 value >>= 7;
7172 more = !((value == 0 && (byte & 0x40) == 0)
7173 || (value == -1 && (byte & 0x40) != 0));
7174 if (more)
7175 byte |= 0x80;
7176 CHECKSUM (byte);
7177 if (!more)
7178 break;
7182 /* Calculate the checksum of a number in unsigned LEB128 format. */
7184 static void
7185 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7187 while (1)
7189 unsigned char byte = (value & 0x7f);
7190 value >>= 7;
7191 if (value != 0)
7192 /* More bytes to follow. */
7193 byte |= 0x80;
7194 CHECKSUM (byte);
7195 if (value == 0)
7196 break;
7200 /* Checksum the context of the DIE. This adds the names of any
7201 surrounding namespaces or structures to the checksum. */
7203 static void
7204 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7206 const char *name;
7207 dw_die_ref spec;
7208 int tag = die->die_tag;
7210 if (tag != DW_TAG_namespace
7211 && tag != DW_TAG_structure_type
7212 && tag != DW_TAG_class_type)
7213 return;
7215 name = get_AT_string (die, DW_AT_name);
7217 spec = get_AT_ref (die, DW_AT_specification);
7218 if (spec != NULL)
7219 die = spec;
7221 if (die->die_parent != NULL)
7222 checksum_die_context (die->die_parent, ctx);
7224 CHECKSUM_ULEB128 ('C');
7225 CHECKSUM_ULEB128 (tag);
7226 if (name != NULL)
7227 CHECKSUM_STRING (name);
7230 /* Calculate the checksum of a location expression. */
7232 static inline void
7233 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7235 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7236 were emitted as a DW_FORM_sdata instead of a location expression. */
7237 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7239 CHECKSUM_ULEB128 (DW_FORM_sdata);
7240 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7241 return;
7244 /* Otherwise, just checksum the raw location expression. */
7245 while (loc != NULL)
7247 inchash::hash hstate;
7248 hashval_t hash;
7250 CHECKSUM_ULEB128 (loc->dtprel);
7251 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7252 hash_loc_operands (loc, hstate);
7253 hash = hstate.end ();
7254 CHECKSUM (hash);
7255 loc = loc->dw_loc_next;
7259 /* Calculate the checksum of an attribute. */
7261 static void
7262 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7263 struct md5_ctx *ctx, int *mark)
7265 dw_loc_descr_ref loc;
7266 rtx r;
7268 if (AT_class (at) == dw_val_class_die_ref)
7270 dw_die_ref target_die = AT_ref (at);
7272 /* For pointer and reference types, we checksum only the (qualified)
7273 name of the target type (if there is a name). For friend entries,
7274 we checksum only the (qualified) name of the target type or function.
7275 This allows the checksum to remain the same whether the target type
7276 is complete or not. */
7277 if ((at->dw_attr == DW_AT_type
7278 && (tag == DW_TAG_pointer_type
7279 || tag == DW_TAG_reference_type
7280 || tag == DW_TAG_rvalue_reference_type
7281 || tag == DW_TAG_ptr_to_member_type))
7282 || (at->dw_attr == DW_AT_friend
7283 && tag == DW_TAG_friend))
7285 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7287 if (name_attr != NULL)
7289 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7291 if (decl == NULL)
7292 decl = target_die;
7293 CHECKSUM_ULEB128 ('N');
7294 CHECKSUM_ULEB128 (at->dw_attr);
7295 if (decl->die_parent != NULL)
7296 checksum_die_context (decl->die_parent, ctx);
7297 CHECKSUM_ULEB128 ('E');
7298 CHECKSUM_STRING (AT_string (name_attr));
7299 return;
7303 /* For all other references to another DIE, we check to see if the
7304 target DIE has already been visited. If it has, we emit a
7305 backward reference; if not, we descend recursively. */
7306 if (target_die->die_mark > 0)
7308 CHECKSUM_ULEB128 ('R');
7309 CHECKSUM_ULEB128 (at->dw_attr);
7310 CHECKSUM_ULEB128 (target_die->die_mark);
7312 else
7314 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7316 if (decl == NULL)
7317 decl = target_die;
7318 target_die->die_mark = ++(*mark);
7319 CHECKSUM_ULEB128 ('T');
7320 CHECKSUM_ULEB128 (at->dw_attr);
7321 if (decl->die_parent != NULL)
7322 checksum_die_context (decl->die_parent, ctx);
7323 die_checksum_ordered (target_die, ctx, mark);
7325 return;
7328 CHECKSUM_ULEB128 ('A');
7329 CHECKSUM_ULEB128 (at->dw_attr);
7331 switch (AT_class (at))
7333 case dw_val_class_const:
7334 case dw_val_class_const_implicit:
7335 CHECKSUM_ULEB128 (DW_FORM_sdata);
7336 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7337 break;
7339 case dw_val_class_unsigned_const:
7340 case dw_val_class_unsigned_const_implicit:
7341 CHECKSUM_ULEB128 (DW_FORM_sdata);
7342 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7343 break;
7345 case dw_val_class_const_double:
7346 CHECKSUM_ULEB128 (DW_FORM_block);
7347 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7348 CHECKSUM (at->dw_attr_val.v.val_double);
7349 break;
7351 case dw_val_class_wide_int:
7352 CHECKSUM_ULEB128 (DW_FORM_block);
7353 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7354 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7355 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7356 get_full_len (*at->dw_attr_val.v.val_wide)
7357 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7358 break;
7360 case dw_val_class_vec:
7361 CHECKSUM_ULEB128 (DW_FORM_block);
7362 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7363 * at->dw_attr_val.v.val_vec.elt_size);
7364 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7365 (at->dw_attr_val.v.val_vec.length
7366 * at->dw_attr_val.v.val_vec.elt_size));
7367 break;
7369 case dw_val_class_flag:
7370 CHECKSUM_ULEB128 (DW_FORM_flag);
7371 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7372 break;
7374 case dw_val_class_str:
7375 CHECKSUM_ULEB128 (DW_FORM_string);
7376 CHECKSUM_STRING (AT_string (at));
7377 break;
7379 case dw_val_class_addr:
7380 r = AT_addr (at);
7381 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7382 CHECKSUM_ULEB128 (DW_FORM_string);
7383 CHECKSUM_STRING (XSTR (r, 0));
7384 break;
7386 case dw_val_class_offset:
7387 CHECKSUM_ULEB128 (DW_FORM_sdata);
7388 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7389 break;
7391 case dw_val_class_loc:
7392 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7393 loc_checksum_ordered (loc, ctx);
7394 break;
7396 case dw_val_class_fde_ref:
7397 case dw_val_class_symview:
7398 case dw_val_class_lbl_id:
7399 case dw_val_class_lineptr:
7400 case dw_val_class_macptr:
7401 case dw_val_class_loclistsptr:
7402 case dw_val_class_high_pc:
7403 break;
7405 case dw_val_class_file:
7406 case dw_val_class_file_implicit:
7407 CHECKSUM_ULEB128 (DW_FORM_string);
7408 CHECKSUM_STRING (AT_file (at)->filename);
7409 break;
7411 case dw_val_class_data8:
7412 CHECKSUM (at->dw_attr_val.v.val_data8);
7413 break;
7415 default:
7416 break;
7420 struct checksum_attributes
7422 dw_attr_node *at_name;
7423 dw_attr_node *at_type;
7424 dw_attr_node *at_friend;
7425 dw_attr_node *at_accessibility;
7426 dw_attr_node *at_address_class;
7427 dw_attr_node *at_alignment;
7428 dw_attr_node *at_allocated;
7429 dw_attr_node *at_artificial;
7430 dw_attr_node *at_associated;
7431 dw_attr_node *at_binary_scale;
7432 dw_attr_node *at_bit_offset;
7433 dw_attr_node *at_bit_size;
7434 dw_attr_node *at_bit_stride;
7435 dw_attr_node *at_byte_size;
7436 dw_attr_node *at_byte_stride;
7437 dw_attr_node *at_const_value;
7438 dw_attr_node *at_containing_type;
7439 dw_attr_node *at_count;
7440 dw_attr_node *at_data_location;
7441 dw_attr_node *at_data_member_location;
7442 dw_attr_node *at_decimal_scale;
7443 dw_attr_node *at_decimal_sign;
7444 dw_attr_node *at_default_value;
7445 dw_attr_node *at_digit_count;
7446 dw_attr_node *at_discr;
7447 dw_attr_node *at_discr_list;
7448 dw_attr_node *at_discr_value;
7449 dw_attr_node *at_encoding;
7450 dw_attr_node *at_endianity;
7451 dw_attr_node *at_explicit;
7452 dw_attr_node *at_is_optional;
7453 dw_attr_node *at_location;
7454 dw_attr_node *at_lower_bound;
7455 dw_attr_node *at_mutable;
7456 dw_attr_node *at_ordering;
7457 dw_attr_node *at_picture_string;
7458 dw_attr_node *at_prototyped;
7459 dw_attr_node *at_small;
7460 dw_attr_node *at_segment;
7461 dw_attr_node *at_string_length;
7462 dw_attr_node *at_string_length_bit_size;
7463 dw_attr_node *at_string_length_byte_size;
7464 dw_attr_node *at_threads_scaled;
7465 dw_attr_node *at_upper_bound;
7466 dw_attr_node *at_use_location;
7467 dw_attr_node *at_use_UTF8;
7468 dw_attr_node *at_variable_parameter;
7469 dw_attr_node *at_virtuality;
7470 dw_attr_node *at_visibility;
7471 dw_attr_node *at_vtable_elem_location;
7474 /* Collect the attributes that we will want to use for the checksum. */
7476 static void
7477 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7479 dw_attr_node *a;
7480 unsigned ix;
7482 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7484 switch (a->dw_attr)
7486 case DW_AT_name:
7487 attrs->at_name = a;
7488 break;
7489 case DW_AT_type:
7490 attrs->at_type = a;
7491 break;
7492 case DW_AT_friend:
7493 attrs->at_friend = a;
7494 break;
7495 case DW_AT_accessibility:
7496 attrs->at_accessibility = a;
7497 break;
7498 case DW_AT_address_class:
7499 attrs->at_address_class = a;
7500 break;
7501 case DW_AT_alignment:
7502 attrs->at_alignment = a;
7503 break;
7504 case DW_AT_allocated:
7505 attrs->at_allocated = a;
7506 break;
7507 case DW_AT_artificial:
7508 attrs->at_artificial = a;
7509 break;
7510 case DW_AT_associated:
7511 attrs->at_associated = a;
7512 break;
7513 case DW_AT_binary_scale:
7514 attrs->at_binary_scale = a;
7515 break;
7516 case DW_AT_bit_offset:
7517 attrs->at_bit_offset = a;
7518 break;
7519 case DW_AT_bit_size:
7520 attrs->at_bit_size = a;
7521 break;
7522 case DW_AT_bit_stride:
7523 attrs->at_bit_stride = a;
7524 break;
7525 case DW_AT_byte_size:
7526 attrs->at_byte_size = a;
7527 break;
7528 case DW_AT_byte_stride:
7529 attrs->at_byte_stride = a;
7530 break;
7531 case DW_AT_const_value:
7532 attrs->at_const_value = a;
7533 break;
7534 case DW_AT_containing_type:
7535 attrs->at_containing_type = a;
7536 break;
7537 case DW_AT_count:
7538 attrs->at_count = a;
7539 break;
7540 case DW_AT_data_location:
7541 attrs->at_data_location = a;
7542 break;
7543 case DW_AT_data_member_location:
7544 attrs->at_data_member_location = a;
7545 break;
7546 case DW_AT_decimal_scale:
7547 attrs->at_decimal_scale = a;
7548 break;
7549 case DW_AT_decimal_sign:
7550 attrs->at_decimal_sign = a;
7551 break;
7552 case DW_AT_default_value:
7553 attrs->at_default_value = a;
7554 break;
7555 case DW_AT_digit_count:
7556 attrs->at_digit_count = a;
7557 break;
7558 case DW_AT_discr:
7559 attrs->at_discr = a;
7560 break;
7561 case DW_AT_discr_list:
7562 attrs->at_discr_list = a;
7563 break;
7564 case DW_AT_discr_value:
7565 attrs->at_discr_value = a;
7566 break;
7567 case DW_AT_encoding:
7568 attrs->at_encoding = a;
7569 break;
7570 case DW_AT_endianity:
7571 attrs->at_endianity = a;
7572 break;
7573 case DW_AT_explicit:
7574 attrs->at_explicit = a;
7575 break;
7576 case DW_AT_is_optional:
7577 attrs->at_is_optional = a;
7578 break;
7579 case DW_AT_location:
7580 attrs->at_location = a;
7581 break;
7582 case DW_AT_lower_bound:
7583 attrs->at_lower_bound = a;
7584 break;
7585 case DW_AT_mutable:
7586 attrs->at_mutable = a;
7587 break;
7588 case DW_AT_ordering:
7589 attrs->at_ordering = a;
7590 break;
7591 case DW_AT_picture_string:
7592 attrs->at_picture_string = a;
7593 break;
7594 case DW_AT_prototyped:
7595 attrs->at_prototyped = a;
7596 break;
7597 case DW_AT_small:
7598 attrs->at_small = a;
7599 break;
7600 case DW_AT_segment:
7601 attrs->at_segment = a;
7602 break;
7603 case DW_AT_string_length:
7604 attrs->at_string_length = a;
7605 break;
7606 case DW_AT_string_length_bit_size:
7607 attrs->at_string_length_bit_size = a;
7608 break;
7609 case DW_AT_string_length_byte_size:
7610 attrs->at_string_length_byte_size = a;
7611 break;
7612 case DW_AT_threads_scaled:
7613 attrs->at_threads_scaled = a;
7614 break;
7615 case DW_AT_upper_bound:
7616 attrs->at_upper_bound = a;
7617 break;
7618 case DW_AT_use_location:
7619 attrs->at_use_location = a;
7620 break;
7621 case DW_AT_use_UTF8:
7622 attrs->at_use_UTF8 = a;
7623 break;
7624 case DW_AT_variable_parameter:
7625 attrs->at_variable_parameter = a;
7626 break;
7627 case DW_AT_virtuality:
7628 attrs->at_virtuality = a;
7629 break;
7630 case DW_AT_visibility:
7631 attrs->at_visibility = a;
7632 break;
7633 case DW_AT_vtable_elem_location:
7634 attrs->at_vtable_elem_location = a;
7635 break;
7636 default:
7637 break;
7642 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7644 static void
7645 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7647 dw_die_ref c;
7648 dw_die_ref decl;
7649 struct checksum_attributes attrs;
7651 CHECKSUM_ULEB128 ('D');
7652 CHECKSUM_ULEB128 (die->die_tag);
7654 memset (&attrs, 0, sizeof (attrs));
7656 decl = get_AT_ref (die, DW_AT_specification);
7657 if (decl != NULL)
7658 collect_checksum_attributes (&attrs, decl);
7659 collect_checksum_attributes (&attrs, die);
7661 CHECKSUM_ATTR (attrs.at_name);
7662 CHECKSUM_ATTR (attrs.at_accessibility);
7663 CHECKSUM_ATTR (attrs.at_address_class);
7664 CHECKSUM_ATTR (attrs.at_allocated);
7665 CHECKSUM_ATTR (attrs.at_artificial);
7666 CHECKSUM_ATTR (attrs.at_associated);
7667 CHECKSUM_ATTR (attrs.at_binary_scale);
7668 CHECKSUM_ATTR (attrs.at_bit_offset);
7669 CHECKSUM_ATTR (attrs.at_bit_size);
7670 CHECKSUM_ATTR (attrs.at_bit_stride);
7671 CHECKSUM_ATTR (attrs.at_byte_size);
7672 CHECKSUM_ATTR (attrs.at_byte_stride);
7673 CHECKSUM_ATTR (attrs.at_const_value);
7674 CHECKSUM_ATTR (attrs.at_containing_type);
7675 CHECKSUM_ATTR (attrs.at_count);
7676 CHECKSUM_ATTR (attrs.at_data_location);
7677 CHECKSUM_ATTR (attrs.at_data_member_location);
7678 CHECKSUM_ATTR (attrs.at_decimal_scale);
7679 CHECKSUM_ATTR (attrs.at_decimal_sign);
7680 CHECKSUM_ATTR (attrs.at_default_value);
7681 CHECKSUM_ATTR (attrs.at_digit_count);
7682 CHECKSUM_ATTR (attrs.at_discr);
7683 CHECKSUM_ATTR (attrs.at_discr_list);
7684 CHECKSUM_ATTR (attrs.at_discr_value);
7685 CHECKSUM_ATTR (attrs.at_encoding);
7686 CHECKSUM_ATTR (attrs.at_endianity);
7687 CHECKSUM_ATTR (attrs.at_explicit);
7688 CHECKSUM_ATTR (attrs.at_is_optional);
7689 CHECKSUM_ATTR (attrs.at_location);
7690 CHECKSUM_ATTR (attrs.at_lower_bound);
7691 CHECKSUM_ATTR (attrs.at_mutable);
7692 CHECKSUM_ATTR (attrs.at_ordering);
7693 CHECKSUM_ATTR (attrs.at_picture_string);
7694 CHECKSUM_ATTR (attrs.at_prototyped);
7695 CHECKSUM_ATTR (attrs.at_small);
7696 CHECKSUM_ATTR (attrs.at_segment);
7697 CHECKSUM_ATTR (attrs.at_string_length);
7698 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7699 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7700 CHECKSUM_ATTR (attrs.at_threads_scaled);
7701 CHECKSUM_ATTR (attrs.at_upper_bound);
7702 CHECKSUM_ATTR (attrs.at_use_location);
7703 CHECKSUM_ATTR (attrs.at_use_UTF8);
7704 CHECKSUM_ATTR (attrs.at_variable_parameter);
7705 CHECKSUM_ATTR (attrs.at_virtuality);
7706 CHECKSUM_ATTR (attrs.at_visibility);
7707 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7708 CHECKSUM_ATTR (attrs.at_type);
7709 CHECKSUM_ATTR (attrs.at_friend);
7710 CHECKSUM_ATTR (attrs.at_alignment);
7712 /* Checksum the child DIEs. */
7713 c = die->die_child;
7714 if (c) do {
7715 dw_attr_node *name_attr;
7717 c = c->die_sib;
7718 name_attr = get_AT (c, DW_AT_name);
7719 if (is_template_instantiation (c))
7721 /* Ignore instantiations of member type and function templates. */
7723 else if (name_attr != NULL
7724 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7726 /* Use a shallow checksum for named nested types and member
7727 functions. */
7728 CHECKSUM_ULEB128 ('S');
7729 CHECKSUM_ULEB128 (c->die_tag);
7730 CHECKSUM_STRING (AT_string (name_attr));
7732 else
7734 /* Use a deep checksum for other children. */
7735 /* Mark this DIE so it gets processed when unmarking. */
7736 if (c->die_mark == 0)
7737 c->die_mark = -1;
7738 die_checksum_ordered (c, ctx, mark);
7740 } while (c != die->die_child);
7742 CHECKSUM_ULEB128 (0);
7745 /* Add a type name and tag to a hash. */
7746 static void
7747 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7749 CHECKSUM_ULEB128 (tag);
7750 CHECKSUM_STRING (name);
7753 #undef CHECKSUM
7754 #undef CHECKSUM_STRING
7755 #undef CHECKSUM_ATTR
7756 #undef CHECKSUM_LEB128
7757 #undef CHECKSUM_ULEB128
7759 /* Generate the type signature for DIE. This is computed by generating an
7760 MD5 checksum over the DIE's tag, its relevant attributes, and its
7761 children. Attributes that are references to other DIEs are processed
7762 by recursion, using the MARK field to prevent infinite recursion.
7763 If the DIE is nested inside a namespace or another type, we also
7764 need to include that context in the signature. The lower 64 bits
7765 of the resulting MD5 checksum comprise the signature. */
7767 static void
7768 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7770 int mark;
7771 const char *name;
7772 unsigned char checksum[16];
7773 struct md5_ctx ctx;
7774 dw_die_ref decl;
7775 dw_die_ref parent;
7777 name = get_AT_string (die, DW_AT_name);
7778 decl = get_AT_ref (die, DW_AT_specification);
7779 parent = get_die_parent (die);
7781 /* First, compute a signature for just the type name (and its surrounding
7782 context, if any. This is stored in the type unit DIE for link-time
7783 ODR (one-definition rule) checking. */
7785 if (is_cxx () && name != NULL)
7787 md5_init_ctx (&ctx);
7789 /* Checksum the names of surrounding namespaces and structures. */
7790 if (parent != NULL)
7791 checksum_die_context (parent, &ctx);
7793 /* Checksum the current DIE. */
7794 die_odr_checksum (die->die_tag, name, &ctx);
7795 md5_finish_ctx (&ctx, checksum);
7797 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7800 /* Next, compute the complete type signature. */
7802 md5_init_ctx (&ctx);
7803 mark = 1;
7804 die->die_mark = mark;
7806 /* Checksum the names of surrounding namespaces and structures. */
7807 if (parent != NULL)
7808 checksum_die_context (parent, &ctx);
7810 /* Checksum the DIE and its children. */
7811 die_checksum_ordered (die, &ctx, &mark);
7812 unmark_all_dies (die);
7813 md5_finish_ctx (&ctx, checksum);
7815 /* Store the signature in the type node and link the type DIE and the
7816 type node together. */
7817 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7818 DWARF_TYPE_SIGNATURE_SIZE);
7819 die->comdat_type_p = true;
7820 die->die_id.die_type_node = type_node;
7821 type_node->type_die = die;
7823 /* If the DIE is a specification, link its declaration to the type node
7824 as well. */
7825 if (decl != NULL)
7827 decl->comdat_type_p = true;
7828 decl->die_id.die_type_node = type_node;
7832 /* Do the location expressions look same? */
7833 static inline int
7834 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7836 return loc1->dw_loc_opc == loc2->dw_loc_opc
7837 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7838 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7841 /* Do the values look the same? */
7842 static int
7843 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7845 dw_loc_descr_ref loc1, loc2;
7846 rtx r1, r2;
7848 if (v1->val_class != v2->val_class)
7849 return 0;
7851 switch (v1->val_class)
7853 case dw_val_class_const:
7854 case dw_val_class_const_implicit:
7855 return v1->v.val_int == v2->v.val_int;
7856 case dw_val_class_unsigned_const:
7857 case dw_val_class_unsigned_const_implicit:
7858 return v1->v.val_unsigned == v2->v.val_unsigned;
7859 case dw_val_class_const_double:
7860 return v1->v.val_double.high == v2->v.val_double.high
7861 && v1->v.val_double.low == v2->v.val_double.low;
7862 case dw_val_class_wide_int:
7863 return *v1->v.val_wide == *v2->v.val_wide;
7864 case dw_val_class_vec:
7865 if (v1->v.val_vec.length != v2->v.val_vec.length
7866 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7867 return 0;
7868 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7869 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7870 return 0;
7871 return 1;
7872 case dw_val_class_flag:
7873 return v1->v.val_flag == v2->v.val_flag;
7874 case dw_val_class_str:
7875 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7877 case dw_val_class_addr:
7878 r1 = v1->v.val_addr;
7879 r2 = v2->v.val_addr;
7880 if (GET_CODE (r1) != GET_CODE (r2))
7881 return 0;
7882 return !rtx_equal_p (r1, r2);
7884 case dw_val_class_offset:
7885 return v1->v.val_offset == v2->v.val_offset;
7887 case dw_val_class_loc:
7888 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7889 loc1 && loc2;
7890 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7891 if (!same_loc_p (loc1, loc2, mark))
7892 return 0;
7893 return !loc1 && !loc2;
7895 case dw_val_class_die_ref:
7896 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7898 case dw_val_class_symview:
7899 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7901 case dw_val_class_fde_ref:
7902 case dw_val_class_vms_delta:
7903 case dw_val_class_lbl_id:
7904 case dw_val_class_lineptr:
7905 case dw_val_class_macptr:
7906 case dw_val_class_loclistsptr:
7907 case dw_val_class_high_pc:
7908 return 1;
7910 case dw_val_class_file:
7911 case dw_val_class_file_implicit:
7912 return v1->v.val_file == v2->v.val_file;
7914 case dw_val_class_data8:
7915 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7917 default:
7918 return 1;
7922 /* Do the attributes look the same? */
7924 static int
7925 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7927 if (at1->dw_attr != at2->dw_attr)
7928 return 0;
7930 /* We don't care that this was compiled with a different compiler
7931 snapshot; if the output is the same, that's what matters. */
7932 if (at1->dw_attr == DW_AT_producer)
7933 return 1;
7935 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7938 /* Do the dies look the same? */
7940 static int
7941 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7943 dw_die_ref c1, c2;
7944 dw_attr_node *a1;
7945 unsigned ix;
7947 /* To avoid infinite recursion. */
7948 if (die1->die_mark)
7949 return die1->die_mark == die2->die_mark;
7950 die1->die_mark = die2->die_mark = ++(*mark);
7952 if (die1->die_tag != die2->die_tag)
7953 return 0;
7955 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7956 return 0;
7958 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7959 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7960 return 0;
7962 c1 = die1->die_child;
7963 c2 = die2->die_child;
7964 if (! c1)
7966 if (c2)
7967 return 0;
7969 else
7970 for (;;)
7972 if (!same_die_p (c1, c2, mark))
7973 return 0;
7974 c1 = c1->die_sib;
7975 c2 = c2->die_sib;
7976 if (c1 == die1->die_child)
7978 if (c2 == die2->die_child)
7979 break;
7980 else
7981 return 0;
7985 return 1;
7988 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7989 children, and set die_symbol. */
7991 static void
7992 compute_comp_unit_symbol (dw_die_ref unit_die)
7994 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7995 const char *base = die_name ? lbasename (die_name) : "anonymous";
7996 char *name = XALLOCAVEC (char, strlen (base) + 64);
7997 char *p;
7998 int i, mark;
7999 unsigned char checksum[16];
8000 struct md5_ctx ctx;
8002 /* Compute the checksum of the DIE, then append part of it as hex digits to
8003 the name filename of the unit. */
8005 md5_init_ctx (&ctx);
8006 mark = 0;
8007 die_checksum (unit_die, &ctx, &mark);
8008 unmark_all_dies (unit_die);
8009 md5_finish_ctx (&ctx, checksum);
8011 /* When we this for comp_unit_die () we have a DW_AT_name that might
8012 not start with a letter but with anything valid for filenames and
8013 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8014 character is not a letter. */
8015 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8016 clean_symbol_name (name);
8018 p = name + strlen (name);
8019 for (i = 0; i < 4; i++)
8021 sprintf (p, "%.2x", checksum[i]);
8022 p += 2;
8025 unit_die->die_id.die_symbol = xstrdup (name);
8028 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
8030 static int
8031 is_type_die (dw_die_ref die)
8033 switch (die->die_tag)
8035 case DW_TAG_array_type:
8036 case DW_TAG_class_type:
8037 case DW_TAG_interface_type:
8038 case DW_TAG_enumeration_type:
8039 case DW_TAG_pointer_type:
8040 case DW_TAG_reference_type:
8041 case DW_TAG_rvalue_reference_type:
8042 case DW_TAG_string_type:
8043 case DW_TAG_structure_type:
8044 case DW_TAG_subroutine_type:
8045 case DW_TAG_union_type:
8046 case DW_TAG_ptr_to_member_type:
8047 case DW_TAG_set_type:
8048 case DW_TAG_subrange_type:
8049 case DW_TAG_base_type:
8050 case DW_TAG_const_type:
8051 case DW_TAG_file_type:
8052 case DW_TAG_packed_type:
8053 case DW_TAG_volatile_type:
8054 case DW_TAG_typedef:
8055 return 1;
8056 default:
8057 return 0;
8061 /* Returns true iff C is a compile-unit DIE. */
8063 static inline bool
8064 is_cu_die (dw_die_ref c)
8066 return c && (c->die_tag == DW_TAG_compile_unit
8067 || c->die_tag == DW_TAG_skeleton_unit);
8070 /* Returns true iff C is a unit DIE of some sort. */
8072 static inline bool
8073 is_unit_die (dw_die_ref c)
8075 return c && (c->die_tag == DW_TAG_compile_unit
8076 || c->die_tag == DW_TAG_partial_unit
8077 || c->die_tag == DW_TAG_type_unit
8078 || c->die_tag == DW_TAG_skeleton_unit);
8081 /* Returns true iff C is a namespace DIE. */
8083 static inline bool
8084 is_namespace_die (dw_die_ref c)
8086 return c && c->die_tag == DW_TAG_namespace;
8089 /* Return non-zero if this DIE is a template parameter. */
8091 static inline bool
8092 is_template_parameter (dw_die_ref die)
8094 switch (die->die_tag)
8096 case DW_TAG_template_type_param:
8097 case DW_TAG_template_value_param:
8098 case DW_TAG_GNU_template_template_param:
8099 case DW_TAG_GNU_template_parameter_pack:
8100 return true;
8101 default:
8102 return false;
8106 /* Return non-zero if this DIE represents a template instantiation. */
8108 static inline bool
8109 is_template_instantiation (dw_die_ref die)
8111 dw_die_ref c;
8113 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8114 return false;
8115 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8116 return false;
8119 static char *
8120 gen_internal_sym (const char *prefix)
8122 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8124 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8125 return xstrdup (buf);
8128 /* Return non-zero if this DIE is a declaration. */
8130 static int
8131 is_declaration_die (dw_die_ref die)
8133 dw_attr_node *a;
8134 unsigned ix;
8136 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8137 if (a->dw_attr == DW_AT_declaration)
8138 return 1;
8140 return 0;
8143 /* Return non-zero if this DIE is nested inside a subprogram. */
8145 static int
8146 is_nested_in_subprogram (dw_die_ref die)
8148 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8150 if (decl == NULL)
8151 decl = die;
8152 return local_scope_p (decl);
8155 /* Return non-zero if this DIE contains a defining declaration of a
8156 subprogram. */
8158 static int
8159 contains_subprogram_definition (dw_die_ref die)
8161 dw_die_ref c;
8163 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8164 return 1;
8165 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8166 return 0;
8169 /* Return non-zero if this is a type DIE that should be moved to a
8170 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8171 unit type. */
8173 static int
8174 should_move_die_to_comdat (dw_die_ref die)
8176 switch (die->die_tag)
8178 case DW_TAG_class_type:
8179 case DW_TAG_structure_type:
8180 case DW_TAG_enumeration_type:
8181 case DW_TAG_union_type:
8182 /* Don't move declarations, inlined instances, types nested in a
8183 subprogram, or types that contain subprogram definitions. */
8184 if (is_declaration_die (die)
8185 || get_AT (die, DW_AT_abstract_origin)
8186 || is_nested_in_subprogram (die)
8187 || contains_subprogram_definition (die))
8188 return 0;
8189 return 1;
8190 case DW_TAG_array_type:
8191 case DW_TAG_interface_type:
8192 case DW_TAG_pointer_type:
8193 case DW_TAG_reference_type:
8194 case DW_TAG_rvalue_reference_type:
8195 case DW_TAG_string_type:
8196 case DW_TAG_subroutine_type:
8197 case DW_TAG_ptr_to_member_type:
8198 case DW_TAG_set_type:
8199 case DW_TAG_subrange_type:
8200 case DW_TAG_base_type:
8201 case DW_TAG_const_type:
8202 case DW_TAG_file_type:
8203 case DW_TAG_packed_type:
8204 case DW_TAG_volatile_type:
8205 case DW_TAG_typedef:
8206 default:
8207 return 0;
8211 /* Make a clone of DIE. */
8213 static dw_die_ref
8214 clone_die (dw_die_ref die)
8216 dw_die_ref clone = new_die_raw (die->die_tag);
8217 dw_attr_node *a;
8218 unsigned ix;
8220 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8221 add_dwarf_attr (clone, a);
8223 return clone;
8226 /* Make a clone of the tree rooted at DIE. */
8228 static dw_die_ref
8229 clone_tree (dw_die_ref die)
8231 dw_die_ref c;
8232 dw_die_ref clone = clone_die (die);
8234 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8236 return clone;
8239 /* Make a clone of DIE as a declaration. */
8241 static dw_die_ref
8242 clone_as_declaration (dw_die_ref die)
8244 dw_die_ref clone;
8245 dw_die_ref decl;
8246 dw_attr_node *a;
8247 unsigned ix;
8249 /* If the DIE is already a declaration, just clone it. */
8250 if (is_declaration_die (die))
8251 return clone_die (die);
8253 /* If the DIE is a specification, just clone its declaration DIE. */
8254 decl = get_AT_ref (die, DW_AT_specification);
8255 if (decl != NULL)
8257 clone = clone_die (decl);
8258 if (die->comdat_type_p)
8259 add_AT_die_ref (clone, DW_AT_signature, die);
8260 return clone;
8263 clone = new_die_raw (die->die_tag);
8265 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8267 /* We don't want to copy over all attributes.
8268 For example we don't want DW_AT_byte_size because otherwise we will no
8269 longer have a declaration and GDB will treat it as a definition. */
8271 switch (a->dw_attr)
8273 case DW_AT_abstract_origin:
8274 case DW_AT_artificial:
8275 case DW_AT_containing_type:
8276 case DW_AT_external:
8277 case DW_AT_name:
8278 case DW_AT_type:
8279 case DW_AT_virtuality:
8280 case DW_AT_linkage_name:
8281 case DW_AT_MIPS_linkage_name:
8282 add_dwarf_attr (clone, a);
8283 break;
8284 case DW_AT_byte_size:
8285 case DW_AT_alignment:
8286 default:
8287 break;
8291 if (die->comdat_type_p)
8292 add_AT_die_ref (clone, DW_AT_signature, die);
8294 add_AT_flag (clone, DW_AT_declaration, 1);
8295 return clone;
8299 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8301 struct decl_table_entry
8303 dw_die_ref orig;
8304 dw_die_ref copy;
8307 /* Helpers to manipulate hash table of copied declarations. */
8309 /* Hashtable helpers. */
8311 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8313 typedef die_struct *compare_type;
8314 static inline hashval_t hash (const decl_table_entry *);
8315 static inline bool equal (const decl_table_entry *, const die_struct *);
8318 inline hashval_t
8319 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8321 return htab_hash_pointer (entry->orig);
8324 inline bool
8325 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8326 const die_struct *entry2)
8328 return entry1->orig == entry2;
8331 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8333 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8334 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8335 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8336 to check if the ancestor has already been copied into UNIT. */
8338 static dw_die_ref
8339 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8340 decl_hash_type *decl_table)
8342 dw_die_ref parent = die->die_parent;
8343 dw_die_ref new_parent = unit;
8344 dw_die_ref copy;
8345 decl_table_entry **slot = NULL;
8346 struct decl_table_entry *entry = NULL;
8348 /* If DIE refers to a stub unfold that so we get the appropriate
8349 DIE registered as orig in decl_table. */
8350 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8351 die = c;
8353 if (decl_table)
8355 /* Check if the entry has already been copied to UNIT. */
8356 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8357 INSERT);
8358 if (*slot != HTAB_EMPTY_ENTRY)
8360 entry = *slot;
8361 return entry->copy;
8364 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8365 entry = XCNEW (struct decl_table_entry);
8366 entry->orig = die;
8367 entry->copy = NULL;
8368 *slot = entry;
8371 if (parent != NULL)
8373 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8374 if (spec != NULL)
8375 parent = spec;
8376 if (!is_unit_die (parent))
8377 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8380 copy = clone_as_declaration (die);
8381 add_child_die (new_parent, copy);
8383 if (decl_table)
8385 /* Record the pointer to the copy. */
8386 entry->copy = copy;
8389 return copy;
8391 /* Copy the declaration context to the new type unit DIE. This includes
8392 any surrounding namespace or type declarations. If the DIE has an
8393 AT_specification attribute, it also includes attributes and children
8394 attached to the specification, and returns a pointer to the original
8395 parent of the declaration DIE. Returns NULL otherwise. */
8397 static dw_die_ref
8398 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8400 dw_die_ref decl;
8401 dw_die_ref new_decl;
8402 dw_die_ref orig_parent = NULL;
8404 decl = get_AT_ref (die, DW_AT_specification);
8405 if (decl == NULL)
8406 decl = die;
8407 else
8409 unsigned ix;
8410 dw_die_ref c;
8411 dw_attr_node *a;
8413 /* The original DIE will be changed to a declaration, and must
8414 be moved to be a child of the original declaration DIE. */
8415 orig_parent = decl->die_parent;
8417 /* Copy the type node pointer from the new DIE to the original
8418 declaration DIE so we can forward references later. */
8419 decl->comdat_type_p = true;
8420 decl->die_id.die_type_node = die->die_id.die_type_node;
8422 remove_AT (die, DW_AT_specification);
8424 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8426 if (a->dw_attr != DW_AT_name
8427 && a->dw_attr != DW_AT_declaration
8428 && a->dw_attr != DW_AT_external)
8429 add_dwarf_attr (die, a);
8432 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8435 if (decl->die_parent != NULL
8436 && !is_unit_die (decl->die_parent))
8438 new_decl = copy_ancestor_tree (unit, decl, NULL);
8439 if (new_decl != NULL)
8441 remove_AT (new_decl, DW_AT_signature);
8442 add_AT_specification (die, new_decl);
8446 return orig_parent;
8449 /* Generate the skeleton ancestor tree for the given NODE, then clone
8450 the DIE and add the clone into the tree. */
8452 static void
8453 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8455 if (node->new_die != NULL)
8456 return;
8458 node->new_die = clone_as_declaration (node->old_die);
8460 if (node->parent != NULL)
8462 generate_skeleton_ancestor_tree (node->parent);
8463 add_child_die (node->parent->new_die, node->new_die);
8467 /* Generate a skeleton tree of DIEs containing any declarations that are
8468 found in the original tree. We traverse the tree looking for declaration
8469 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8471 static void
8472 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8474 skeleton_chain_node node;
8475 dw_die_ref c;
8476 dw_die_ref first;
8477 dw_die_ref prev = NULL;
8478 dw_die_ref next = NULL;
8480 node.parent = parent;
8482 first = c = parent->old_die->die_child;
8483 if (c)
8484 next = c->die_sib;
8485 if (c) do {
8486 if (prev == NULL || prev->die_sib == c)
8487 prev = c;
8488 c = next;
8489 next = (c == first ? NULL : c->die_sib);
8490 node.old_die = c;
8491 node.new_die = NULL;
8492 if (is_declaration_die (c))
8494 if (is_template_instantiation (c))
8496 /* Instantiated templates do not need to be cloned into the
8497 type unit. Just move the DIE and its children back to
8498 the skeleton tree (in the main CU). */
8499 remove_child_with_prev (c, prev);
8500 add_child_die (parent->new_die, c);
8501 c = prev;
8503 else if (c->comdat_type_p)
8505 /* This is the skeleton of earlier break_out_comdat_types
8506 type. Clone the existing DIE, but keep the children
8507 under the original (which is in the main CU). */
8508 dw_die_ref clone = clone_die (c);
8510 replace_child (c, clone, prev);
8511 generate_skeleton_ancestor_tree (parent);
8512 add_child_die (parent->new_die, c);
8513 c = clone;
8514 continue;
8516 else
8518 /* Clone the existing DIE, move the original to the skeleton
8519 tree (which is in the main CU), and put the clone, with
8520 all the original's children, where the original came from
8521 (which is about to be moved to the type unit). */
8522 dw_die_ref clone = clone_die (c);
8523 move_all_children (c, clone);
8525 /* If the original has a DW_AT_object_pointer attribute,
8526 it would now point to a child DIE just moved to the
8527 cloned tree, so we need to remove that attribute from
8528 the original. */
8529 remove_AT (c, DW_AT_object_pointer);
8531 replace_child (c, clone, prev);
8532 generate_skeleton_ancestor_tree (parent);
8533 add_child_die (parent->new_die, c);
8534 node.old_die = clone;
8535 node.new_die = c;
8536 c = clone;
8539 generate_skeleton_bottom_up (&node);
8540 } while (next != NULL);
8543 /* Wrapper function for generate_skeleton_bottom_up. */
8545 static dw_die_ref
8546 generate_skeleton (dw_die_ref die)
8548 skeleton_chain_node node;
8550 node.old_die = die;
8551 node.new_die = NULL;
8552 node.parent = NULL;
8554 /* If this type definition is nested inside another type,
8555 and is not an instantiation of a template, always leave
8556 at least a declaration in its place. */
8557 if (die->die_parent != NULL
8558 && is_type_die (die->die_parent)
8559 && !is_template_instantiation (die))
8560 node.new_die = clone_as_declaration (die);
8562 generate_skeleton_bottom_up (&node);
8563 return node.new_die;
8566 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8567 declaration. The original DIE is moved to a new compile unit so that
8568 existing references to it follow it to the new location. If any of the
8569 original DIE's descendants is a declaration, we need to replace the
8570 original DIE with a skeleton tree and move the declarations back into the
8571 skeleton tree. */
8573 static dw_die_ref
8574 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8575 dw_die_ref prev)
8577 dw_die_ref skeleton, orig_parent;
8579 /* Copy the declaration context to the type unit DIE. If the returned
8580 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8581 that DIE. */
8582 orig_parent = copy_declaration_context (unit, child);
8584 skeleton = generate_skeleton (child);
8585 if (skeleton == NULL)
8586 remove_child_with_prev (child, prev);
8587 else
8589 skeleton->comdat_type_p = true;
8590 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8592 /* If the original DIE was a specification, we need to put
8593 the skeleton under the parent DIE of the declaration.
8594 This leaves the original declaration in the tree, but
8595 it will be pruned later since there are no longer any
8596 references to it. */
8597 if (orig_parent != NULL)
8599 remove_child_with_prev (child, prev);
8600 add_child_die (orig_parent, skeleton);
8602 else
8603 replace_child (child, skeleton, prev);
8606 return skeleton;
8609 static void
8610 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8611 comdat_type_node *type_node,
8612 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8614 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8615 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8616 DWARF procedure references in the DW_AT_location attribute. */
8618 static dw_die_ref
8619 copy_dwarf_procedure (dw_die_ref die,
8620 comdat_type_node *type_node,
8621 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8623 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8625 /* DWARF procedures are not supposed to have children... */
8626 gcc_assert (die->die_child == NULL);
8628 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8629 gcc_assert (vec_safe_length (die->die_attr) == 1
8630 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8632 /* Do not copy more than once DWARF procedures. */
8633 bool existed;
8634 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8635 if (existed)
8636 return die_copy;
8638 die_copy = clone_die (die);
8639 add_child_die (type_node->root_die, die_copy);
8640 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8641 return die_copy;
8644 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8645 procedures in DIE's attributes. */
8647 static void
8648 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8649 comdat_type_node *type_node,
8650 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8652 dw_attr_node *a;
8653 unsigned i;
8655 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8657 dw_loc_descr_ref loc;
8659 if (a->dw_attr_val.val_class != dw_val_class_loc)
8660 continue;
8662 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8664 switch (loc->dw_loc_opc)
8666 case DW_OP_call2:
8667 case DW_OP_call4:
8668 case DW_OP_call_ref:
8669 gcc_assert (loc->dw_loc_oprnd1.val_class
8670 == dw_val_class_die_ref);
8671 loc->dw_loc_oprnd1.v.val_die_ref.die
8672 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8673 type_node,
8674 copied_dwarf_procs);
8676 default:
8677 break;
8683 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8684 rewrite references to point to the copies.
8686 References are looked for in DIE's attributes and recursively in all its
8687 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8688 mapping from old DWARF procedures to their copy. It is used not to copy
8689 twice the same DWARF procedure under TYPE_NODE. */
8691 static void
8692 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8693 comdat_type_node *type_node,
8694 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8696 dw_die_ref c;
8698 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8699 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8700 type_node,
8701 copied_dwarf_procs));
8704 /* Traverse the DIE and set up additional .debug_types or .debug_info
8705 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8706 section. */
8708 static void
8709 break_out_comdat_types (dw_die_ref die)
8711 dw_die_ref c;
8712 dw_die_ref first;
8713 dw_die_ref prev = NULL;
8714 dw_die_ref next = NULL;
8715 dw_die_ref unit = NULL;
8717 first = c = die->die_child;
8718 if (c)
8719 next = c->die_sib;
8720 if (c) do {
8721 if (prev == NULL || prev->die_sib == c)
8722 prev = c;
8723 c = next;
8724 next = (c == first ? NULL : c->die_sib);
8725 if (should_move_die_to_comdat (c))
8727 dw_die_ref replacement;
8728 comdat_type_node *type_node;
8730 /* Break out nested types into their own type units. */
8731 break_out_comdat_types (c);
8733 /* Create a new type unit DIE as the root for the new tree. */
8734 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8735 add_AT_unsigned (unit, DW_AT_language,
8736 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8738 /* Add the new unit's type DIE into the comdat type list. */
8739 type_node = ggc_cleared_alloc<comdat_type_node> ();
8740 type_node->root_die = unit;
8741 type_node->next = comdat_type_list;
8742 comdat_type_list = type_node;
8744 /* Generate the type signature. */
8745 generate_type_signature (c, type_node);
8747 /* Copy the declaration context, attributes, and children of the
8748 declaration into the new type unit DIE, then remove this DIE
8749 from the main CU (or replace it with a skeleton if necessary). */
8750 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8751 type_node->skeleton_die = replacement;
8753 /* Add the DIE to the new compunit. */
8754 add_child_die (unit, c);
8756 /* Types can reference DWARF procedures for type size or data location
8757 expressions. Calls in DWARF expressions cannot target procedures
8758 that are not in the same section. So we must copy DWARF procedures
8759 along with this type and then rewrite references to them. */
8760 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8761 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8763 if (replacement != NULL)
8764 c = replacement;
8766 else if (c->die_tag == DW_TAG_namespace
8767 || c->die_tag == DW_TAG_class_type
8768 || c->die_tag == DW_TAG_structure_type
8769 || c->die_tag == DW_TAG_union_type)
8771 /* Look for nested types that can be broken out. */
8772 break_out_comdat_types (c);
8774 } while (next != NULL);
8777 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8778 Enter all the cloned children into the hash table decl_table. */
8780 static dw_die_ref
8781 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8783 dw_die_ref c;
8784 dw_die_ref clone;
8785 struct decl_table_entry *entry;
8786 decl_table_entry **slot;
8788 if (die->die_tag == DW_TAG_subprogram)
8789 clone = clone_as_declaration (die);
8790 else
8791 clone = clone_die (die);
8793 slot = decl_table->find_slot_with_hash (die,
8794 htab_hash_pointer (die), INSERT);
8796 /* Assert that DIE isn't in the hash table yet. If it would be there
8797 before, the ancestors would be necessarily there as well, therefore
8798 clone_tree_partial wouldn't be called. */
8799 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8801 entry = XCNEW (struct decl_table_entry);
8802 entry->orig = die;
8803 entry->copy = clone;
8804 *slot = entry;
8806 if (die->die_tag != DW_TAG_subprogram)
8807 FOR_EACH_CHILD (die, c,
8808 add_child_die (clone, clone_tree_partial (c, decl_table)));
8810 return clone;
8813 /* Walk the DIE and its children, looking for references to incomplete
8814 or trivial types that are unmarked (i.e., that are not in the current
8815 type_unit). */
8817 static void
8818 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8820 dw_die_ref c;
8821 dw_attr_node *a;
8822 unsigned ix;
8824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8826 if (AT_class (a) == dw_val_class_die_ref)
8828 dw_die_ref targ = AT_ref (a);
8829 decl_table_entry **slot;
8830 struct decl_table_entry *entry;
8832 if (targ->die_mark != 0 || targ->comdat_type_p)
8833 continue;
8835 slot = decl_table->find_slot_with_hash (targ,
8836 htab_hash_pointer (targ),
8837 INSERT);
8839 if (*slot != HTAB_EMPTY_ENTRY)
8841 /* TARG has already been copied, so we just need to
8842 modify the reference to point to the copy. */
8843 entry = *slot;
8844 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8846 else
8848 dw_die_ref parent = unit;
8849 dw_die_ref copy = clone_die (targ);
8851 /* Record in DECL_TABLE that TARG has been copied.
8852 Need to do this now, before the recursive call,
8853 because DECL_TABLE may be expanded and SLOT
8854 would no longer be a valid pointer. */
8855 entry = XCNEW (struct decl_table_entry);
8856 entry->orig = targ;
8857 entry->copy = copy;
8858 *slot = entry;
8860 /* If TARG is not a declaration DIE, we need to copy its
8861 children. */
8862 if (!is_declaration_die (targ))
8864 FOR_EACH_CHILD (
8865 targ, c,
8866 add_child_die (copy,
8867 clone_tree_partial (c, decl_table)));
8870 /* Make sure the cloned tree is marked as part of the
8871 type unit. */
8872 mark_dies (copy);
8874 /* If TARG has surrounding context, copy its ancestor tree
8875 into the new type unit. */
8876 if (targ->die_parent != NULL
8877 && !is_unit_die (targ->die_parent))
8878 parent = copy_ancestor_tree (unit, targ->die_parent,
8879 decl_table);
8881 add_child_die (parent, copy);
8882 a->dw_attr_val.v.val_die_ref.die = copy;
8884 /* Make sure the newly-copied DIE is walked. If it was
8885 installed in a previously-added context, it won't
8886 get visited otherwise. */
8887 if (parent != unit)
8889 /* Find the highest point of the newly-added tree,
8890 mark each node along the way, and walk from there. */
8891 parent->die_mark = 1;
8892 while (parent->die_parent
8893 && parent->die_parent->die_mark == 0)
8895 parent = parent->die_parent;
8896 parent->die_mark = 1;
8898 copy_decls_walk (unit, parent, decl_table);
8904 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8907 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8908 and record them in DECL_TABLE. */
8910 static void
8911 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8913 dw_die_ref c;
8915 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8917 dw_die_ref targ = AT_ref (a);
8918 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8919 decl_table_entry **slot
8920 = decl_table->find_slot_with_hash (targ,
8921 htab_hash_pointer (targ),
8922 INSERT);
8923 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8924 /* Record in DECL_TABLE that TARG has been already copied
8925 by remove_child_or_replace_with_skeleton. */
8926 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8927 entry->orig = targ;
8928 entry->copy = die;
8929 *slot = entry;
8931 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8934 /* Copy declarations for "unworthy" types into the new comdat section.
8935 Incomplete types, modified types, and certain other types aren't broken
8936 out into comdat sections of their own, so they don't have a signature,
8937 and we need to copy the declaration into the same section so that we
8938 don't have an external reference. */
8940 static void
8941 copy_decls_for_unworthy_types (dw_die_ref unit)
8943 mark_dies (unit);
8944 decl_hash_type decl_table (10);
8945 collect_skeleton_dies (unit, &decl_table);
8946 copy_decls_walk (unit, unit, &decl_table);
8947 unmark_dies (unit);
8950 /* Traverse the DIE and add a sibling attribute if it may have the
8951 effect of speeding up access to siblings. To save some space,
8952 avoid generating sibling attributes for DIE's without children. */
8954 static void
8955 add_sibling_attributes (dw_die_ref die)
8957 dw_die_ref c;
8959 if (! die->die_child)
8960 return;
8962 if (die->die_parent && die != die->die_parent->die_child)
8963 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8965 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8968 /* Output all location lists for the DIE and its children. */
8970 static void
8971 output_location_lists (dw_die_ref die)
8973 dw_die_ref c;
8974 dw_attr_node *a;
8975 unsigned ix;
8977 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8978 if (AT_class (a) == dw_val_class_loc_list)
8979 output_loc_list (AT_loc_list (a));
8981 FOR_EACH_CHILD (die, c, output_location_lists (c));
8984 /* During assign_location_list_indexes and output_loclists_offset the
8985 current index, after it the number of assigned indexes (i.e. how
8986 large the .debug_loclists* offset table should be). */
8987 static unsigned int loc_list_idx;
8989 /* Output all location list offsets for the DIE and its children. */
8991 static void
8992 output_loclists_offsets (dw_die_ref die)
8994 dw_die_ref c;
8995 dw_attr_node *a;
8996 unsigned ix;
8998 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8999 if (AT_class (a) == dw_val_class_loc_list)
9001 dw_loc_list_ref l = AT_loc_list (a);
9002 if (l->offset_emitted)
9003 continue;
9004 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9005 loc_section_label, NULL);
9006 gcc_assert (l->hash == loc_list_idx);
9007 loc_list_idx++;
9008 l->offset_emitted = true;
9011 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9014 /* Recursively set indexes of location lists. */
9016 static void
9017 assign_location_list_indexes (dw_die_ref die)
9019 dw_die_ref c;
9020 dw_attr_node *a;
9021 unsigned ix;
9023 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9024 if (AT_class (a) == dw_val_class_loc_list)
9026 dw_loc_list_ref list = AT_loc_list (a);
9027 if (!list->num_assigned)
9029 list->num_assigned = true;
9030 list->hash = loc_list_idx++;
9034 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9037 /* We want to limit the number of external references, because they are
9038 larger than local references: a relocation takes multiple words, and
9039 even a sig8 reference is always eight bytes, whereas a local reference
9040 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9041 So if we encounter multiple external references to the same type DIE, we
9042 make a local typedef stub for it and redirect all references there.
9044 This is the element of the hash table for keeping track of these
9045 references. */
9047 struct external_ref
9049 dw_die_ref type;
9050 dw_die_ref stub;
9051 unsigned n_refs;
9054 /* Hashtable helpers. */
9056 struct external_ref_hasher : free_ptr_hash <external_ref>
9058 static inline hashval_t hash (const external_ref *);
9059 static inline bool equal (const external_ref *, const external_ref *);
9062 inline hashval_t
9063 external_ref_hasher::hash (const external_ref *r)
9065 dw_die_ref die = r->type;
9066 hashval_t h = 0;
9068 /* We can't use the address of the DIE for hashing, because
9069 that will make the order of the stub DIEs non-deterministic. */
9070 if (! die->comdat_type_p)
9071 /* We have a symbol; use it to compute a hash. */
9072 h = htab_hash_string (die->die_id.die_symbol);
9073 else
9075 /* We have a type signature; use a subset of the bits as the hash.
9076 The 8-byte signature is at least as large as hashval_t. */
9077 comdat_type_node *type_node = die->die_id.die_type_node;
9078 memcpy (&h, type_node->signature, sizeof (h));
9080 return h;
9083 inline bool
9084 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9086 return r1->type == r2->type;
9089 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9091 /* Return a pointer to the external_ref for references to DIE. */
9093 static struct external_ref *
9094 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9096 struct external_ref ref, *ref_p;
9097 external_ref **slot;
9099 ref.type = die;
9100 slot = map->find_slot (&ref, INSERT);
9101 if (*slot != HTAB_EMPTY_ENTRY)
9102 return *slot;
9104 ref_p = XCNEW (struct external_ref);
9105 ref_p->type = die;
9106 *slot = ref_p;
9107 return ref_p;
9110 /* Subroutine of optimize_external_refs, below.
9112 If we see a type skeleton, record it as our stub. If we see external
9113 references, remember how many we've seen. */
9115 static void
9116 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9118 dw_die_ref c;
9119 dw_attr_node *a;
9120 unsigned ix;
9121 struct external_ref *ref_p;
9123 if (is_type_die (die)
9124 && (c = get_AT_ref (die, DW_AT_signature)))
9126 /* This is a local skeleton; use it for local references. */
9127 ref_p = lookup_external_ref (map, c);
9128 ref_p->stub = die;
9131 /* Scan the DIE references, and remember any that refer to DIEs from
9132 other CUs (i.e. those which are not marked). */
9133 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9134 if (AT_class (a) == dw_val_class_die_ref
9135 && (c = AT_ref (a))->die_mark == 0
9136 && is_type_die (c))
9138 ref_p = lookup_external_ref (map, c);
9139 ref_p->n_refs++;
9142 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9145 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9146 points to an external_ref, DATA is the CU we're processing. If we don't
9147 already have a local stub, and we have multiple refs, build a stub. */
9150 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9152 struct external_ref *ref_p = *slot;
9154 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9156 /* We have multiple references to this type, so build a small stub.
9157 Both of these forms are a bit dodgy from the perspective of the
9158 DWARF standard, since technically they should have names. */
9159 dw_die_ref cu = data;
9160 dw_die_ref type = ref_p->type;
9161 dw_die_ref stub = NULL;
9163 if (type->comdat_type_p)
9165 /* If we refer to this type via sig8, use AT_signature. */
9166 stub = new_die (type->die_tag, cu, NULL_TREE);
9167 add_AT_die_ref (stub, DW_AT_signature, type);
9169 else
9171 /* Otherwise, use a typedef with no name. */
9172 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9173 add_AT_die_ref (stub, DW_AT_type, type);
9176 stub->die_mark++;
9177 ref_p->stub = stub;
9179 return 1;
9182 /* DIE is a unit; look through all the DIE references to see if there are
9183 any external references to types, and if so, create local stubs for
9184 them which will be applied in build_abbrev_table. This is useful because
9185 references to local DIEs are smaller. */
9187 static external_ref_hash_type *
9188 optimize_external_refs (dw_die_ref die)
9190 external_ref_hash_type *map = new external_ref_hash_type (10);
9191 optimize_external_refs_1 (die, map);
9192 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9193 return map;
9196 /* The following 3 variables are temporaries that are computed only during the
9197 build_abbrev_table call and used and released during the following
9198 optimize_abbrev_table call. */
9200 /* First abbrev_id that can be optimized based on usage. */
9201 static unsigned int abbrev_opt_start;
9203 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9204 abbrev_id smaller than this, because they must be already sized
9205 during build_abbrev_table). */
9206 static unsigned int abbrev_opt_base_type_end;
9208 /* Vector of usage counts during build_abbrev_table. Indexed by
9209 abbrev_id - abbrev_opt_start. */
9210 static vec<unsigned int> abbrev_usage_count;
9212 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9213 static vec<dw_die_ref> sorted_abbrev_dies;
9215 /* The format of each DIE (and its attribute value pairs) is encoded in an
9216 abbreviation table. This routine builds the abbreviation table and assigns
9217 a unique abbreviation id for each abbreviation entry. The children of each
9218 die are visited recursively. */
9220 static void
9221 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9223 unsigned int abbrev_id = 0;
9224 dw_die_ref c;
9225 dw_attr_node *a;
9226 unsigned ix;
9227 dw_die_ref abbrev;
9229 /* Scan the DIE references, and replace any that refer to
9230 DIEs from other CUs (i.e. those which are not marked) with
9231 the local stubs we built in optimize_external_refs. */
9232 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9233 if (AT_class (a) == dw_val_class_die_ref
9234 && (c = AT_ref (a))->die_mark == 0)
9236 struct external_ref *ref_p;
9237 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9239 if (is_type_die (c)
9240 && (ref_p = lookup_external_ref (extern_map, c))
9241 && ref_p->stub && ref_p->stub != die)
9243 gcc_assert (a->dw_attr != DW_AT_signature);
9244 change_AT_die_ref (a, ref_p->stub);
9246 else
9247 /* We aren't changing this reference, so mark it external. */
9248 set_AT_ref_external (a, 1);
9251 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9253 dw_attr_node *die_a, *abbrev_a;
9254 unsigned ix;
9255 bool ok = true;
9257 if (abbrev_id == 0)
9258 continue;
9259 if (abbrev->die_tag != die->die_tag)
9260 continue;
9261 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9262 continue;
9264 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9265 continue;
9267 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9269 abbrev_a = &(*abbrev->die_attr)[ix];
9270 if ((abbrev_a->dw_attr != die_a->dw_attr)
9271 || (value_format (abbrev_a) != value_format (die_a)))
9273 ok = false;
9274 break;
9277 if (ok)
9278 break;
9281 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9283 vec_safe_push (abbrev_die_table, die);
9284 if (abbrev_opt_start)
9285 abbrev_usage_count.safe_push (0);
9287 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9289 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9290 sorted_abbrev_dies.safe_push (die);
9293 die->die_abbrev = abbrev_id;
9294 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9297 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9298 by die_abbrev's usage count, from the most commonly used
9299 abbreviation to the least. */
9301 static int
9302 die_abbrev_cmp (const void *p1, const void *p2)
9304 dw_die_ref die1 = *(const dw_die_ref *) p1;
9305 dw_die_ref die2 = *(const dw_die_ref *) p2;
9307 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9308 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9310 if (die1->die_abbrev >= abbrev_opt_base_type_end
9311 && die2->die_abbrev >= abbrev_opt_base_type_end)
9313 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9314 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9315 return -1;
9316 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9317 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9318 return 1;
9321 /* Stabilize the sort. */
9322 if (die1->die_abbrev < die2->die_abbrev)
9323 return -1;
9324 if (die1->die_abbrev > die2->die_abbrev)
9325 return 1;
9327 return 0;
9330 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9331 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9332 into dw_val_class_const_implicit or
9333 dw_val_class_unsigned_const_implicit. */
9335 static void
9336 optimize_implicit_const (unsigned int first_id, unsigned int end,
9337 vec<bool> &implicit_consts)
9339 /* It never makes sense if there is just one DIE using the abbreviation. */
9340 if (end < first_id + 2)
9341 return;
9343 dw_attr_node *a;
9344 unsigned ix, i;
9345 dw_die_ref die = sorted_abbrev_dies[first_id];
9346 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9347 if (implicit_consts[ix])
9349 enum dw_val_class new_class = dw_val_class_none;
9350 switch (AT_class (a))
9352 case dw_val_class_unsigned_const:
9353 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9354 continue;
9356 /* The .debug_abbrev section will grow by
9357 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9358 in all the DIEs using that abbreviation. */
9359 if (constant_size (AT_unsigned (a)) * (end - first_id)
9360 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9361 continue;
9363 new_class = dw_val_class_unsigned_const_implicit;
9364 break;
9366 case dw_val_class_const:
9367 new_class = dw_val_class_const_implicit;
9368 break;
9370 case dw_val_class_file:
9371 new_class = dw_val_class_file_implicit;
9372 break;
9374 default:
9375 continue;
9377 for (i = first_id; i < end; i++)
9378 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9379 = new_class;
9383 /* Attempt to optimize abbreviation table from abbrev_opt_start
9384 abbreviation above. */
9386 static void
9387 optimize_abbrev_table (void)
9389 if (abbrev_opt_start
9390 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9391 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9393 auto_vec<bool, 32> implicit_consts;
9394 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9396 unsigned int abbrev_id = abbrev_opt_start - 1;
9397 unsigned int first_id = ~0U;
9398 unsigned int last_abbrev_id = 0;
9399 unsigned int i;
9400 dw_die_ref die;
9401 if (abbrev_opt_base_type_end > abbrev_opt_start)
9402 abbrev_id = abbrev_opt_base_type_end - 1;
9403 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9404 most commonly used abbreviations come first. */
9405 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9407 dw_attr_node *a;
9408 unsigned ix;
9410 /* If calc_base_type_die_sizes has been called, the CU and
9411 base types after it can't be optimized, because we've already
9412 calculated their DIE offsets. We've sorted them first. */
9413 if (die->die_abbrev < abbrev_opt_base_type_end)
9414 continue;
9415 if (die->die_abbrev != last_abbrev_id)
9417 last_abbrev_id = die->die_abbrev;
9418 if (dwarf_version >= 5 && first_id != ~0U)
9419 optimize_implicit_const (first_id, i, implicit_consts);
9420 abbrev_id++;
9421 (*abbrev_die_table)[abbrev_id] = die;
9422 if (dwarf_version >= 5)
9424 first_id = i;
9425 implicit_consts.truncate (0);
9427 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9428 switch (AT_class (a))
9430 case dw_val_class_const:
9431 case dw_val_class_unsigned_const:
9432 case dw_val_class_file:
9433 implicit_consts.safe_push (true);
9434 break;
9435 default:
9436 implicit_consts.safe_push (false);
9437 break;
9441 else if (dwarf_version >= 5)
9443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9444 if (!implicit_consts[ix])
9445 continue;
9446 else
9448 dw_attr_node *other_a
9449 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9450 if (!dw_val_equal_p (&a->dw_attr_val,
9451 &other_a->dw_attr_val))
9452 implicit_consts[ix] = false;
9455 die->die_abbrev = abbrev_id;
9457 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9458 if (dwarf_version >= 5 && first_id != ~0U)
9459 optimize_implicit_const (first_id, i, implicit_consts);
9462 abbrev_opt_start = 0;
9463 abbrev_opt_base_type_end = 0;
9464 abbrev_usage_count.release ();
9465 sorted_abbrev_dies.release ();
9468 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9470 static int
9471 constant_size (unsigned HOST_WIDE_INT value)
9473 int log;
9475 if (value == 0)
9476 log = 0;
9477 else
9478 log = floor_log2 (value);
9480 log = log / 8;
9481 log = 1 << (floor_log2 (log) + 1);
9483 return log;
9486 /* Return the size of a DIE as it is represented in the
9487 .debug_info section. */
9489 static unsigned long
9490 size_of_die (dw_die_ref die)
9492 unsigned long size = 0;
9493 dw_attr_node *a;
9494 unsigned ix;
9495 enum dwarf_form form;
9497 size += size_of_uleb128 (die->die_abbrev);
9498 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9500 switch (AT_class (a))
9502 case dw_val_class_addr:
9503 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9505 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9506 size += size_of_uleb128 (AT_index (a));
9508 else
9509 size += DWARF2_ADDR_SIZE;
9510 break;
9511 case dw_val_class_offset:
9512 size += dwarf_offset_size;
9513 break;
9514 case dw_val_class_loc:
9516 unsigned long lsize = size_of_locs (AT_loc (a));
9518 /* Block length. */
9519 if (dwarf_version >= 4)
9520 size += size_of_uleb128 (lsize);
9521 else
9522 size += constant_size (lsize);
9523 size += lsize;
9525 break;
9526 case dw_val_class_loc_list:
9527 if (dwarf_split_debug_info && dwarf_version >= 5)
9529 gcc_assert (AT_loc_list (a)->num_assigned);
9530 size += size_of_uleb128 (AT_loc_list (a)->hash);
9532 else
9533 size += dwarf_offset_size;
9534 break;
9535 case dw_val_class_view_list:
9536 size += dwarf_offset_size;
9537 break;
9538 case dw_val_class_range_list:
9539 if (value_format (a) == DW_FORM_rnglistx)
9541 gcc_assert (rnglist_idx);
9542 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9543 size += size_of_uleb128 (r->idx);
9545 else
9546 size += dwarf_offset_size;
9547 break;
9548 case dw_val_class_const:
9549 size += size_of_sleb128 (AT_int (a));
9550 break;
9551 case dw_val_class_unsigned_const:
9553 int csize = constant_size (AT_unsigned (a));
9554 if (dwarf_version == 3
9555 && a->dw_attr == DW_AT_data_member_location
9556 && csize >= 4)
9557 size += size_of_uleb128 (AT_unsigned (a));
9558 else
9559 size += csize;
9561 break;
9562 case dw_val_class_symview:
9563 if (symview_upper_bound <= 0xff)
9564 size += 1;
9565 else if (symview_upper_bound <= 0xffff)
9566 size += 2;
9567 else if (symview_upper_bound <= 0xffffffff)
9568 size += 4;
9569 else
9570 size += 8;
9571 break;
9572 case dw_val_class_const_implicit:
9573 case dw_val_class_unsigned_const_implicit:
9574 case dw_val_class_file_implicit:
9575 /* These occupy no size in the DIE, just an extra sleb128 in
9576 .debug_abbrev. */
9577 break;
9578 case dw_val_class_const_double:
9579 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9580 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9581 size++; /* block */
9582 break;
9583 case dw_val_class_wide_int:
9584 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9585 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9586 if (get_full_len (*a->dw_attr_val.v.val_wide)
9587 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9588 size++; /* block */
9589 break;
9590 case dw_val_class_vec:
9591 size += constant_size (a->dw_attr_val.v.val_vec.length
9592 * a->dw_attr_val.v.val_vec.elt_size)
9593 + a->dw_attr_val.v.val_vec.length
9594 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9595 break;
9596 case dw_val_class_flag:
9597 if (dwarf_version >= 4)
9598 /* Currently all add_AT_flag calls pass in 1 as last argument,
9599 so DW_FORM_flag_present can be used. If that ever changes,
9600 we'll need to use DW_FORM_flag and have some optimization
9601 in build_abbrev_table that will change those to
9602 DW_FORM_flag_present if it is set to 1 in all DIEs using
9603 the same abbrev entry. */
9604 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9605 else
9606 size += 1;
9607 break;
9608 case dw_val_class_die_ref:
9609 if (AT_ref_external (a))
9611 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9612 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9613 is sized by target address length, whereas in DWARF3
9614 it's always sized as an offset. */
9615 if (AT_ref (a)->comdat_type_p)
9616 size += DWARF_TYPE_SIGNATURE_SIZE;
9617 else if (dwarf_version == 2)
9618 size += DWARF2_ADDR_SIZE;
9619 else
9620 size += dwarf_offset_size;
9622 else
9623 size += dwarf_offset_size;
9624 break;
9625 case dw_val_class_fde_ref:
9626 size += dwarf_offset_size;
9627 break;
9628 case dw_val_class_lbl_id:
9629 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9631 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9632 size += size_of_uleb128 (AT_index (a));
9634 else
9635 size += DWARF2_ADDR_SIZE;
9636 break;
9637 case dw_val_class_lineptr:
9638 case dw_val_class_macptr:
9639 case dw_val_class_loclistsptr:
9640 size += dwarf_offset_size;
9641 break;
9642 case dw_val_class_str:
9643 form = AT_string_form (a);
9644 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9645 size += dwarf_offset_size;
9646 else if (form == dwarf_FORM (DW_FORM_strx))
9647 size += size_of_uleb128 (AT_index (a));
9648 else
9649 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9650 break;
9651 case dw_val_class_file:
9652 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9653 break;
9654 case dw_val_class_data8:
9655 size += 8;
9656 break;
9657 case dw_val_class_vms_delta:
9658 size += dwarf_offset_size;
9659 break;
9660 case dw_val_class_high_pc:
9661 size += DWARF2_ADDR_SIZE;
9662 break;
9663 case dw_val_class_discr_value:
9664 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9665 break;
9666 case dw_val_class_discr_list:
9668 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9670 /* This is a block, so we have the block length and then its
9671 data. */
9672 size += constant_size (block_size) + block_size;
9674 break;
9675 default:
9676 gcc_unreachable ();
9680 return size;
9683 /* Size the debugging information associated with a given DIE. Visits the
9684 DIE's children recursively. Updates the global variable next_die_offset, on
9685 each time through. Uses the current value of next_die_offset to update the
9686 die_offset field in each DIE. */
9688 static void
9689 calc_die_sizes (dw_die_ref die)
9691 dw_die_ref c;
9693 gcc_assert (die->die_offset == 0
9694 || (unsigned long int) die->die_offset == next_die_offset);
9695 die->die_offset = next_die_offset;
9696 next_die_offset += size_of_die (die);
9698 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9700 if (die->die_child != NULL)
9701 /* Count the null byte used to terminate sibling lists. */
9702 next_die_offset += 1;
9705 /* Size just the base type children at the start of the CU.
9706 This is needed because build_abbrev needs to size locs
9707 and sizing of type based stack ops needs to know die_offset
9708 values for the base types. */
9710 static void
9711 calc_base_type_die_sizes (void)
9713 unsigned long die_offset = (dwarf_split_debug_info
9714 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9715 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9716 unsigned int i;
9717 dw_die_ref base_type;
9718 #if ENABLE_ASSERT_CHECKING
9719 dw_die_ref prev = comp_unit_die ()->die_child;
9720 #endif
9722 die_offset += size_of_die (comp_unit_die ());
9723 for (i = 0; base_types.iterate (i, &base_type); i++)
9725 #if ENABLE_ASSERT_CHECKING
9726 gcc_assert (base_type->die_offset == 0
9727 && prev->die_sib == base_type
9728 && base_type->die_child == NULL
9729 && base_type->die_abbrev);
9730 prev = base_type;
9731 #endif
9732 if (abbrev_opt_start
9733 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9734 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9735 base_type->die_offset = die_offset;
9736 die_offset += size_of_die (base_type);
9740 /* Set the marks for a die and its children. We do this so
9741 that we know whether or not a reference needs to use FORM_ref_addr; only
9742 DIEs in the same CU will be marked. We used to clear out the offset
9743 and use that as the flag, but ran into ordering problems. */
9745 static void
9746 mark_dies (dw_die_ref die)
9748 dw_die_ref c;
9750 gcc_assert (!die->die_mark);
9752 die->die_mark = 1;
9753 FOR_EACH_CHILD (die, c, mark_dies (c));
9756 /* Clear the marks for a die and its children. */
9758 static void
9759 unmark_dies (dw_die_ref die)
9761 dw_die_ref c;
9763 if (! use_debug_types)
9764 gcc_assert (die->die_mark);
9766 die->die_mark = 0;
9767 FOR_EACH_CHILD (die, c, unmark_dies (c));
9770 /* Clear the marks for a die, its children and referred dies. */
9772 static void
9773 unmark_all_dies (dw_die_ref die)
9775 dw_die_ref c;
9776 dw_attr_node *a;
9777 unsigned ix;
9779 if (!die->die_mark)
9780 return;
9781 die->die_mark = 0;
9783 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9786 if (AT_class (a) == dw_val_class_die_ref)
9787 unmark_all_dies (AT_ref (a));
9790 /* Calculate if the entry should appear in the final output file. It may be
9791 from a pruned a type. */
9793 static bool
9794 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9796 /* By limiting gnu pubnames to definitions only, gold can generate a
9797 gdb index without entries for declarations, which don't include
9798 enough information to be useful. */
9799 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9800 return false;
9802 if (table == pubname_table)
9804 /* Enumerator names are part of the pubname table, but the
9805 parent DW_TAG_enumeration_type die may have been pruned.
9806 Don't output them if that is the case. */
9807 if (p->die->die_tag == DW_TAG_enumerator &&
9808 (p->die->die_parent == NULL
9809 || !p->die->die_parent->die_perennial_p))
9810 return false;
9812 /* Everything else in the pubname table is included. */
9813 return true;
9816 /* The pubtypes table shouldn't include types that have been
9817 pruned. */
9818 return (p->die->die_offset != 0
9819 || !flag_eliminate_unused_debug_types);
9822 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9823 generated for the compilation unit. */
9825 static unsigned long
9826 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9828 unsigned long size;
9829 unsigned i;
9830 pubname_entry *p;
9831 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9833 size = DWARF_PUBNAMES_HEADER_SIZE;
9834 FOR_EACH_VEC_ELT (*names, i, p)
9835 if (include_pubname_in_output (names, p))
9836 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9838 size += dwarf_offset_size;
9839 return size;
9842 /* Return the size of the information in the .debug_aranges section. */
9844 static unsigned long
9845 size_of_aranges (void)
9847 unsigned long size;
9849 size = DWARF_ARANGES_HEADER_SIZE;
9851 /* Count the address/length pair for this compilation unit. */
9852 if (switch_text_ranges)
9853 size += 2 * DWARF2_ADDR_SIZE
9854 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9855 if (switch_cold_ranges)
9856 size += 2 * DWARF2_ADDR_SIZE
9857 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9858 if (have_multiple_function_sections)
9860 unsigned fde_idx;
9861 dw_fde_ref fde;
9863 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9865 if (fde->ignored_debug)
9866 continue;
9867 if (!fde->in_std_section)
9868 size += 2 * DWARF2_ADDR_SIZE;
9869 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9870 size += 2 * DWARF2_ADDR_SIZE;
9874 /* Count the two zero words used to terminated the address range table. */
9875 size += 2 * DWARF2_ADDR_SIZE;
9876 return size;
9879 /* Select the encoding of an attribute value. */
9881 static enum dwarf_form
9882 value_format (dw_attr_node *a)
9884 switch (AT_class (a))
9886 case dw_val_class_addr:
9887 /* Only very few attributes allow DW_FORM_addr. */
9888 switch (a->dw_attr)
9890 case DW_AT_low_pc:
9891 case DW_AT_high_pc:
9892 case DW_AT_entry_pc:
9893 case DW_AT_trampoline:
9894 return (AT_index (a) == NOT_INDEXED
9895 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9896 default:
9897 break;
9899 switch (DWARF2_ADDR_SIZE)
9901 case 1:
9902 return DW_FORM_data1;
9903 case 2:
9904 return DW_FORM_data2;
9905 case 4:
9906 return DW_FORM_data4;
9907 case 8:
9908 return DW_FORM_data8;
9909 default:
9910 gcc_unreachable ();
9912 case dw_val_class_loc_list:
9913 if (dwarf_split_debug_info
9914 && dwarf_version >= 5
9915 && AT_loc_list (a)->num_assigned)
9916 return DW_FORM_loclistx;
9917 /* FALLTHRU */
9918 case dw_val_class_view_list:
9919 case dw_val_class_range_list:
9920 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9921 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9922 care about sizes of .debug* sections in shared libraries and
9923 executables and don't take into account relocations that affect just
9924 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9925 table in the .debug_rnglists section. */
9926 if (dwarf_split_debug_info
9927 && dwarf_version >= 5
9928 && AT_class (a) == dw_val_class_range_list
9929 && rnglist_idx
9930 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9931 return DW_FORM_rnglistx;
9932 if (dwarf_version >= 4)
9933 return DW_FORM_sec_offset;
9934 /* FALLTHRU */
9935 case dw_val_class_vms_delta:
9936 case dw_val_class_offset:
9937 switch (dwarf_offset_size)
9939 case 4:
9940 return DW_FORM_data4;
9941 case 8:
9942 return DW_FORM_data8;
9943 default:
9944 gcc_unreachable ();
9946 case dw_val_class_loc:
9947 if (dwarf_version >= 4)
9948 return DW_FORM_exprloc;
9949 switch (constant_size (size_of_locs (AT_loc (a))))
9951 case 1:
9952 return DW_FORM_block1;
9953 case 2:
9954 return DW_FORM_block2;
9955 case 4:
9956 return DW_FORM_block4;
9957 default:
9958 gcc_unreachable ();
9960 case dw_val_class_const:
9961 return DW_FORM_sdata;
9962 case dw_val_class_unsigned_const:
9963 switch (constant_size (AT_unsigned (a)))
9965 case 1:
9966 return DW_FORM_data1;
9967 case 2:
9968 return DW_FORM_data2;
9969 case 4:
9970 /* In DWARF3 DW_AT_data_member_location with
9971 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9972 constant, so we need to use DW_FORM_udata if we need
9973 a large constant. */
9974 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9975 return DW_FORM_udata;
9976 return DW_FORM_data4;
9977 case 8:
9978 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9979 return DW_FORM_udata;
9980 return DW_FORM_data8;
9981 default:
9982 gcc_unreachable ();
9984 case dw_val_class_const_implicit:
9985 case dw_val_class_unsigned_const_implicit:
9986 case dw_val_class_file_implicit:
9987 return DW_FORM_implicit_const;
9988 case dw_val_class_const_double:
9989 switch (HOST_BITS_PER_WIDE_INT)
9991 case 8:
9992 return DW_FORM_data2;
9993 case 16:
9994 return DW_FORM_data4;
9995 case 32:
9996 return DW_FORM_data8;
9997 case 64:
9998 if (dwarf_version >= 5)
9999 return DW_FORM_data16;
10000 /* FALLTHRU */
10001 default:
10002 return DW_FORM_block1;
10004 case dw_val_class_wide_int:
10005 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10007 case 8:
10008 return DW_FORM_data1;
10009 case 16:
10010 return DW_FORM_data2;
10011 case 32:
10012 return DW_FORM_data4;
10013 case 64:
10014 return DW_FORM_data8;
10015 case 128:
10016 if (dwarf_version >= 5)
10017 return DW_FORM_data16;
10018 /* FALLTHRU */
10019 default:
10020 return DW_FORM_block1;
10022 case dw_val_class_symview:
10023 /* ??? We might use uleb128, but then we'd have to compute
10024 .debug_info offsets in the assembler. */
10025 if (symview_upper_bound <= 0xff)
10026 return DW_FORM_data1;
10027 else if (symview_upper_bound <= 0xffff)
10028 return DW_FORM_data2;
10029 else if (symview_upper_bound <= 0xffffffff)
10030 return DW_FORM_data4;
10031 else
10032 return DW_FORM_data8;
10033 case dw_val_class_vec:
10034 switch (constant_size (a->dw_attr_val.v.val_vec.length
10035 * a->dw_attr_val.v.val_vec.elt_size))
10037 case 1:
10038 return DW_FORM_block1;
10039 case 2:
10040 return DW_FORM_block2;
10041 case 4:
10042 return DW_FORM_block4;
10043 default:
10044 gcc_unreachable ();
10046 case dw_val_class_flag:
10047 if (dwarf_version >= 4)
10049 /* Currently all add_AT_flag calls pass in 1 as last argument,
10050 so DW_FORM_flag_present can be used. If that ever changes,
10051 we'll need to use DW_FORM_flag and have some optimization
10052 in build_abbrev_table that will change those to
10053 DW_FORM_flag_present if it is set to 1 in all DIEs using
10054 the same abbrev entry. */
10055 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10056 return DW_FORM_flag_present;
10058 return DW_FORM_flag;
10059 case dw_val_class_die_ref:
10060 if (AT_ref_external (a))
10062 if (AT_ref (a)->comdat_type_p)
10063 return DW_FORM_ref_sig8;
10064 else
10065 return DW_FORM_ref_addr;
10067 else
10068 return DW_FORM_ref;
10069 case dw_val_class_fde_ref:
10070 return DW_FORM_data;
10071 case dw_val_class_lbl_id:
10072 return (AT_index (a) == NOT_INDEXED
10073 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10074 case dw_val_class_lineptr:
10075 case dw_val_class_macptr:
10076 case dw_val_class_loclistsptr:
10077 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10078 case dw_val_class_str:
10079 return AT_string_form (a);
10080 case dw_val_class_file:
10081 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10083 case 1:
10084 return DW_FORM_data1;
10085 case 2:
10086 return DW_FORM_data2;
10087 case 4:
10088 return DW_FORM_data4;
10089 default:
10090 gcc_unreachable ();
10093 case dw_val_class_data8:
10094 return DW_FORM_data8;
10096 case dw_val_class_high_pc:
10097 switch (DWARF2_ADDR_SIZE)
10099 case 1:
10100 return DW_FORM_data1;
10101 case 2:
10102 return DW_FORM_data2;
10103 case 4:
10104 return DW_FORM_data4;
10105 case 8:
10106 return DW_FORM_data8;
10107 default:
10108 gcc_unreachable ();
10111 case dw_val_class_discr_value:
10112 return (a->dw_attr_val.v.val_discr_value.pos
10113 ? DW_FORM_udata
10114 : DW_FORM_sdata);
10115 case dw_val_class_discr_list:
10116 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10118 case 1:
10119 return DW_FORM_block1;
10120 case 2:
10121 return DW_FORM_block2;
10122 case 4:
10123 return DW_FORM_block4;
10124 default:
10125 gcc_unreachable ();
10128 default:
10129 gcc_unreachable ();
10133 /* Output the encoding of an attribute value. */
10135 static void
10136 output_value_format (dw_attr_node *a)
10138 enum dwarf_form form = value_format (a);
10140 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10143 /* Given a die and id, produce the appropriate abbreviations. */
10145 static void
10146 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10148 unsigned ix;
10149 dw_attr_node *a_attr;
10151 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10152 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10153 dwarf_tag_name (abbrev->die_tag));
10155 if (abbrev->die_child != NULL)
10156 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10157 else
10158 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10160 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10162 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10163 dwarf_attr_name (a_attr->dw_attr));
10164 output_value_format (a_attr);
10165 if (value_format (a_attr) == DW_FORM_implicit_const)
10167 if (AT_class (a_attr) == dw_val_class_file_implicit)
10169 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10170 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10171 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10173 else
10174 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10178 dw2_asm_output_data (1, 0, NULL);
10179 dw2_asm_output_data (1, 0, NULL);
10183 /* Output the .debug_abbrev section which defines the DIE abbreviation
10184 table. */
10186 static void
10187 output_abbrev_section (void)
10189 unsigned int abbrev_id;
10190 dw_die_ref abbrev;
10192 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10193 if (abbrev_id != 0)
10194 output_die_abbrevs (abbrev_id, abbrev);
10196 /* Terminate the table. */
10197 dw2_asm_output_data (1, 0, NULL);
10200 /* Return a new location list, given the begin and end range, and the
10201 expression. */
10203 static inline dw_loc_list_ref
10204 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10205 const char *end, var_loc_view vend,
10206 const char *section)
10208 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10210 retlist->begin = begin;
10211 retlist->begin_entry = NULL;
10212 retlist->end = end;
10213 retlist->end_entry = NULL;
10214 retlist->expr = expr;
10215 retlist->section = section;
10216 retlist->vbegin = vbegin;
10217 retlist->vend = vend;
10219 return retlist;
10222 /* Return true iff there's any nonzero view number in the loc list.
10224 ??? When views are not enabled, we'll often extend a single range
10225 to the entire function, so that we emit a single location
10226 expression rather than a location list. With views, even with a
10227 single range, we'll output a list if start or end have a nonzero
10228 view. If we change this, we may want to stop splitting a single
10229 range in dw_loc_list just because of a nonzero view, even if it
10230 straddles across hot/cold partitions. */
10232 static bool
10233 loc_list_has_views (dw_loc_list_ref list)
10235 if (!debug_variable_location_views)
10236 return false;
10238 for (dw_loc_list_ref loc = list;
10239 loc != NULL; loc = loc->dw_loc_next)
10240 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10241 return true;
10243 return false;
10246 /* Generate a new internal symbol for this location list node, if it
10247 hasn't got one yet. */
10249 static inline void
10250 gen_llsym (dw_loc_list_ref list)
10252 gcc_assert (!list->ll_symbol);
10253 list->ll_symbol = gen_internal_sym ("LLST");
10255 if (!loc_list_has_views (list))
10256 return;
10258 if (dwarf2out_locviews_in_attribute ())
10260 /* Use the same label_num for the view list. */
10261 label_num--;
10262 list->vl_symbol = gen_internal_sym ("LVUS");
10264 else
10265 list->vl_symbol = list->ll_symbol;
10268 /* Generate a symbol for the list, but only if we really want to emit
10269 it as a list. */
10271 static inline void
10272 maybe_gen_llsym (dw_loc_list_ref list)
10274 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10275 return;
10277 gen_llsym (list);
10280 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10281 NULL, don't consider size of the location expression. If we're not
10282 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10283 representation in *SIZEP. */
10285 static bool
10286 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10288 /* Don't output an entry that starts and ends at the same address. */
10289 if (strcmp (curr->begin, curr->end) == 0
10290 && curr->vbegin == curr->vend && !curr->force)
10291 return true;
10293 if (!sizep)
10294 return false;
10296 unsigned long size = size_of_locs (curr->expr);
10298 /* If the expression is too large, drop it on the floor. We could
10299 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10300 in the expression, but >= 64KB expressions for a single value
10301 in a single range are unlikely very useful. */
10302 if (dwarf_version < 5 && size > 0xffff)
10303 return true;
10305 *sizep = size;
10307 return false;
10310 /* Output a view pair loclist entry for CURR, if it requires one. */
10312 static void
10313 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10315 if (!dwarf2out_locviews_in_loclist ())
10316 return;
10318 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10319 return;
10321 #ifdef DW_LLE_view_pair
10322 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10324 if (dwarf2out_as_locview_support)
10326 if (ZERO_VIEW_P (curr->vbegin))
10327 dw2_asm_output_data_uleb128 (0, "Location view begin");
10328 else
10330 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10331 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10332 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10335 if (ZERO_VIEW_P (curr->vend))
10336 dw2_asm_output_data_uleb128 (0, "Location view end");
10337 else
10339 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10340 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10341 dw2_asm_output_symname_uleb128 (label, "Location view end");
10344 else
10346 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10347 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10349 #endif /* DW_LLE_view_pair */
10351 return;
10354 /* Output the location list given to us. */
10356 static void
10357 output_loc_list (dw_loc_list_ref list_head)
10359 int vcount = 0, lcount = 0;
10361 if (list_head->emitted)
10362 return;
10363 list_head->emitted = true;
10365 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10367 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10369 for (dw_loc_list_ref curr = list_head; curr != NULL;
10370 curr = curr->dw_loc_next)
10372 unsigned long size;
10374 if (skip_loc_list_entry (curr, &size))
10375 continue;
10377 vcount++;
10379 /* ?? dwarf_split_debug_info? */
10380 if (dwarf2out_as_locview_support)
10382 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10384 if (!ZERO_VIEW_P (curr->vbegin))
10386 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10387 dw2_asm_output_symname_uleb128 (label,
10388 "View list begin (%s)",
10389 list_head->vl_symbol);
10391 else
10392 dw2_asm_output_data_uleb128 (0,
10393 "View list begin (%s)",
10394 list_head->vl_symbol);
10396 if (!ZERO_VIEW_P (curr->vend))
10398 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10399 dw2_asm_output_symname_uleb128 (label,
10400 "View list end (%s)",
10401 list_head->vl_symbol);
10403 else
10404 dw2_asm_output_data_uleb128 (0,
10405 "View list end (%s)",
10406 list_head->vl_symbol);
10408 else
10410 dw2_asm_output_data_uleb128 (curr->vbegin,
10411 "View list begin (%s)",
10412 list_head->vl_symbol);
10413 dw2_asm_output_data_uleb128 (curr->vend,
10414 "View list end (%s)",
10415 list_head->vl_symbol);
10420 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10422 const char *last_section = NULL;
10423 const char *base_label = NULL;
10425 /* Walk the location list, and output each range + expression. */
10426 for (dw_loc_list_ref curr = list_head; curr != NULL;
10427 curr = curr->dw_loc_next)
10429 unsigned long size;
10431 /* Skip this entry? If we skip it here, we must skip it in the
10432 view list above as well. */
10433 if (skip_loc_list_entry (curr, &size))
10434 continue;
10436 lcount++;
10438 if (dwarf_version >= 5)
10440 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10442 dwarf2out_maybe_output_loclist_view_pair (curr);
10443 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10444 uleb128 index into .debug_addr and uleb128 length. */
10445 dw2_asm_output_data (1, DW_LLE_startx_length,
10446 "DW_LLE_startx_length (%s)",
10447 list_head->ll_symbol);
10448 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10449 "Location list range start index "
10450 "(%s)", curr->begin);
10451 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10452 "Location list length (%s)",
10453 list_head->ll_symbol);
10455 else if (dwarf_split_debug_info)
10457 dwarf2out_maybe_output_loclist_view_pair (curr);
10458 /* For -gsplit-dwarf without usable .uleb128 support, emit
10459 DW_LLE_startx_endx, which has two uleb128 indexes into
10460 .debug_addr. */
10461 dw2_asm_output_data (1, DW_LLE_startx_endx,
10462 "DW_LLE_startx_endx (%s)",
10463 list_head->ll_symbol);
10464 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10465 "Location list range start index "
10466 "(%s)", curr->begin);
10467 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10468 "Location list range end index "
10469 "(%s)", curr->end);
10471 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10473 dwarf2out_maybe_output_loclist_view_pair (curr);
10474 /* If all code is in .text section, the base address is
10475 already provided by the CU attributes. Use
10476 DW_LLE_offset_pair where both addresses are uleb128 encoded
10477 offsets against that base. */
10478 dw2_asm_output_data (1, DW_LLE_offset_pair,
10479 "DW_LLE_offset_pair (%s)",
10480 list_head->ll_symbol);
10481 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10482 "Location list begin address (%s)",
10483 list_head->ll_symbol);
10484 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10485 "Location list end address (%s)",
10486 list_head->ll_symbol);
10488 else if (HAVE_AS_LEB128)
10490 /* Otherwise, find out how many consecutive entries could share
10491 the same base entry. If just one, emit DW_LLE_start_length,
10492 otherwise emit DW_LLE_base_address for the base address
10493 followed by a series of DW_LLE_offset_pair. */
10494 if (last_section == NULL || curr->section != last_section)
10496 dw_loc_list_ref curr2;
10497 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10498 curr2 = curr2->dw_loc_next)
10500 if (strcmp (curr2->begin, curr2->end) == 0
10501 && !curr2->force)
10502 continue;
10503 break;
10505 if (curr2 == NULL || curr->section != curr2->section)
10506 last_section = NULL;
10507 else
10509 last_section = curr->section;
10510 base_label = curr->begin;
10511 dw2_asm_output_data (1, DW_LLE_base_address,
10512 "DW_LLE_base_address (%s)",
10513 list_head->ll_symbol);
10514 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10515 "Base address (%s)",
10516 list_head->ll_symbol);
10519 /* Only one entry with the same base address. Use
10520 DW_LLE_start_length with absolute address and uleb128
10521 length. */
10522 if (last_section == NULL)
10524 dwarf2out_maybe_output_loclist_view_pair (curr);
10525 dw2_asm_output_data (1, DW_LLE_start_length,
10526 "DW_LLE_start_length (%s)",
10527 list_head->ll_symbol);
10528 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10529 "Location list begin address (%s)",
10530 list_head->ll_symbol);
10531 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10532 "Location list length "
10533 "(%s)", list_head->ll_symbol);
10535 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10536 DW_LLE_base_address. */
10537 else
10539 dwarf2out_maybe_output_loclist_view_pair (curr);
10540 dw2_asm_output_data (1, DW_LLE_offset_pair,
10541 "DW_LLE_offset_pair (%s)",
10542 list_head->ll_symbol);
10543 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10544 "Location list begin address "
10545 "(%s)", list_head->ll_symbol);
10546 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10547 "Location list end address "
10548 "(%s)", list_head->ll_symbol);
10551 /* The assembler does not support .uleb128 directive. Emit
10552 DW_LLE_start_end with a pair of absolute addresses. */
10553 else
10555 dwarf2out_maybe_output_loclist_view_pair (curr);
10556 dw2_asm_output_data (1, DW_LLE_start_end,
10557 "DW_LLE_start_end (%s)",
10558 list_head->ll_symbol);
10559 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10560 "Location list begin address (%s)",
10561 list_head->ll_symbol);
10562 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10563 "Location list end address (%s)",
10564 list_head->ll_symbol);
10567 else if (dwarf_split_debug_info)
10569 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10570 and 4 byte length. */
10571 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10572 "Location list start/length entry (%s)",
10573 list_head->ll_symbol);
10574 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10575 "Location list range start index (%s)",
10576 curr->begin);
10577 /* The length field is 4 bytes. If we ever need to support
10578 an 8-byte length, we can add a new DW_LLE code or fall back
10579 to DW_LLE_GNU_start_end_entry. */
10580 dw2_asm_output_delta (4, curr->end, curr->begin,
10581 "Location list range length (%s)",
10582 list_head->ll_symbol);
10584 else if (!have_multiple_function_sections)
10586 /* Pair of relative addresses against start of text section. */
10587 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10588 "Location list begin address (%s)",
10589 list_head->ll_symbol);
10590 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10591 "Location list end address (%s)",
10592 list_head->ll_symbol);
10594 else
10596 /* Pair of absolute addresses. */
10597 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10598 "Location list begin address (%s)",
10599 list_head->ll_symbol);
10600 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10601 "Location list end address (%s)",
10602 list_head->ll_symbol);
10605 /* Output the block length for this list of location operations. */
10606 if (dwarf_version >= 5)
10607 dw2_asm_output_data_uleb128 (size, "Location expression size");
10608 else
10610 gcc_assert (size <= 0xffff);
10611 dw2_asm_output_data (2, size, "Location expression size");
10614 output_loc_sequence (curr->expr, -1);
10617 /* And finally list termination. */
10618 if (dwarf_version >= 5)
10619 dw2_asm_output_data (1, DW_LLE_end_of_list,
10620 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10621 else if (dwarf_split_debug_info)
10622 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10623 "Location list terminator (%s)",
10624 list_head->ll_symbol);
10625 else
10627 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10628 "Location list terminator begin (%s)",
10629 list_head->ll_symbol);
10630 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10631 "Location list terminator end (%s)",
10632 list_head->ll_symbol);
10635 gcc_assert (!list_head->vl_symbol
10636 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10639 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10640 section. Emit a relocated reference if val_entry is NULL, otherwise,
10641 emit an indirect reference. */
10643 static void
10644 output_range_list_offset (dw_attr_node *a)
10646 const char *name = dwarf_attr_name (a->dw_attr);
10648 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10650 if (dwarf_version >= 5)
10652 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10653 dw2_asm_output_offset (dwarf_offset_size, r->label,
10654 debug_ranges_section, "%s", name);
10656 else
10658 char *p = strchr (ranges_section_label, '\0');
10659 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10660 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10661 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10662 debug_ranges_section, "%s", name);
10663 *p = '\0';
10666 else if (dwarf_version >= 5)
10668 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10669 gcc_assert (rnglist_idx);
10670 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10672 else
10673 dw2_asm_output_data (dwarf_offset_size,
10674 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10675 "%s (offset from %s)", name, ranges_section_label);
10678 /* Output the offset into the debug_loc section. */
10680 static void
10681 output_loc_list_offset (dw_attr_node *a)
10683 char *sym = AT_loc_list (a)->ll_symbol;
10685 gcc_assert (sym);
10686 if (!dwarf_split_debug_info)
10687 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10688 "%s", dwarf_attr_name (a->dw_attr));
10689 else if (dwarf_version >= 5)
10691 gcc_assert (AT_loc_list (a)->num_assigned);
10692 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10693 dwarf_attr_name (a->dw_attr),
10694 sym);
10696 else
10697 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10698 "%s", dwarf_attr_name (a->dw_attr));
10701 /* Output the offset into the debug_loc section. */
10703 static void
10704 output_view_list_offset (dw_attr_node *a)
10706 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10708 gcc_assert (sym);
10709 if (dwarf_split_debug_info)
10710 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10711 "%s", dwarf_attr_name (a->dw_attr));
10712 else
10713 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10714 "%s", dwarf_attr_name (a->dw_attr));
10717 /* Output an attribute's index or value appropriately. */
10719 static void
10720 output_attr_index_or_value (dw_attr_node *a)
10722 const char *name = dwarf_attr_name (a->dw_attr);
10724 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10726 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10727 return;
10729 switch (AT_class (a))
10731 case dw_val_class_addr:
10732 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10733 break;
10734 case dw_val_class_high_pc:
10735 case dw_val_class_lbl_id:
10736 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10737 break;
10738 default:
10739 gcc_unreachable ();
10743 /* Output a type signature. */
10745 static inline void
10746 output_signature (const char *sig, const char *name)
10748 int i;
10750 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10751 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10754 /* Output a discriminant value. */
10756 static inline void
10757 output_discr_value (dw_discr_value *discr_value, const char *name)
10759 if (discr_value->pos)
10760 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10761 else
10762 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10765 /* Output the DIE and its attributes. Called recursively to generate
10766 the definitions of each child DIE. */
10768 static void
10769 output_die (dw_die_ref die)
10771 dw_attr_node *a;
10772 dw_die_ref c;
10773 unsigned long size;
10774 unsigned ix;
10776 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10777 (unsigned long)die->die_offset,
10778 dwarf_tag_name (die->die_tag));
10780 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10782 const char *name = dwarf_attr_name (a->dw_attr);
10784 switch (AT_class (a))
10786 case dw_val_class_addr:
10787 output_attr_index_or_value (a);
10788 break;
10790 case dw_val_class_offset:
10791 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10792 "%s", name);
10793 break;
10795 case dw_val_class_range_list:
10796 output_range_list_offset (a);
10797 break;
10799 case dw_val_class_loc:
10800 size = size_of_locs (AT_loc (a));
10802 /* Output the block length for this list of location operations. */
10803 if (dwarf_version >= 4)
10804 dw2_asm_output_data_uleb128 (size, "%s", name);
10805 else
10806 dw2_asm_output_data (constant_size (size), size, "%s", name);
10808 output_loc_sequence (AT_loc (a), -1);
10809 break;
10811 case dw_val_class_const:
10812 /* ??? It would be slightly more efficient to use a scheme like is
10813 used for unsigned constants below, but gdb 4.x does not sign
10814 extend. Gdb 5.x does sign extend. */
10815 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10816 break;
10818 case dw_val_class_unsigned_const:
10820 int csize = constant_size (AT_unsigned (a));
10821 if (dwarf_version == 3
10822 && a->dw_attr == DW_AT_data_member_location
10823 && csize >= 4)
10824 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10825 else
10826 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10828 break;
10830 case dw_val_class_symview:
10832 int vsize;
10833 if (symview_upper_bound <= 0xff)
10834 vsize = 1;
10835 else if (symview_upper_bound <= 0xffff)
10836 vsize = 2;
10837 else if (symview_upper_bound <= 0xffffffff)
10838 vsize = 4;
10839 else
10840 vsize = 8;
10841 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10842 "%s", name);
10844 break;
10846 case dw_val_class_const_implicit:
10847 if (flag_debug_asm)
10848 fprintf (asm_out_file, "\t\t\t%s %s ("
10849 HOST_WIDE_INT_PRINT_DEC ")\n",
10850 ASM_COMMENT_START, name, AT_int (a));
10851 break;
10853 case dw_val_class_unsigned_const_implicit:
10854 if (flag_debug_asm)
10855 fprintf (asm_out_file, "\t\t\t%s %s ("
10856 HOST_WIDE_INT_PRINT_HEX ")\n",
10857 ASM_COMMENT_START, name, AT_unsigned (a));
10858 break;
10860 case dw_val_class_const_double:
10862 unsigned HOST_WIDE_INT first, second;
10864 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10865 dw2_asm_output_data (1,
10866 HOST_BITS_PER_DOUBLE_INT
10867 / HOST_BITS_PER_CHAR,
10868 NULL);
10870 if (WORDS_BIG_ENDIAN)
10872 first = a->dw_attr_val.v.val_double.high;
10873 second = a->dw_attr_val.v.val_double.low;
10875 else
10877 first = a->dw_attr_val.v.val_double.low;
10878 second = a->dw_attr_val.v.val_double.high;
10881 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10882 first, "%s", name);
10883 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10884 second, NULL);
10886 break;
10888 case dw_val_class_wide_int:
10890 int i;
10891 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10892 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10893 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10894 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10895 * l, NULL);
10897 if (WORDS_BIG_ENDIAN)
10898 for (i = len - 1; i >= 0; --i)
10900 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10901 "%s", name);
10902 name = "";
10904 else
10905 for (i = 0; i < len; ++i)
10907 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10908 "%s", name);
10909 name = "";
10912 break;
10914 case dw_val_class_vec:
10916 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10917 unsigned int len = a->dw_attr_val.v.val_vec.length;
10918 unsigned int i;
10919 unsigned char *p;
10921 dw2_asm_output_data (constant_size (len * elt_size),
10922 len * elt_size, "%s", name);
10923 if (elt_size > sizeof (HOST_WIDE_INT))
10925 elt_size /= 2;
10926 len *= 2;
10928 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10929 i < len;
10930 i++, p += elt_size)
10931 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10932 "fp or vector constant word %u", i);
10933 break;
10936 case dw_val_class_flag:
10937 if (dwarf_version >= 4)
10939 /* Currently all add_AT_flag calls pass in 1 as last argument,
10940 so DW_FORM_flag_present can be used. If that ever changes,
10941 we'll need to use DW_FORM_flag and have some optimization
10942 in build_abbrev_table that will change those to
10943 DW_FORM_flag_present if it is set to 1 in all DIEs using
10944 the same abbrev entry. */
10945 gcc_assert (AT_flag (a) == 1);
10946 if (flag_debug_asm)
10947 fprintf (asm_out_file, "\t\t\t%s %s\n",
10948 ASM_COMMENT_START, name);
10949 break;
10951 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10952 break;
10954 case dw_val_class_loc_list:
10955 output_loc_list_offset (a);
10956 break;
10958 case dw_val_class_view_list:
10959 output_view_list_offset (a);
10960 break;
10962 case dw_val_class_die_ref:
10963 if (AT_ref_external (a))
10965 if (AT_ref (a)->comdat_type_p)
10967 comdat_type_node *type_node
10968 = AT_ref (a)->die_id.die_type_node;
10970 gcc_assert (type_node);
10971 output_signature (type_node->signature, name);
10973 else
10975 const char *sym = AT_ref (a)->die_id.die_symbol;
10976 int size;
10978 gcc_assert (sym);
10979 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10980 length, whereas in DWARF3 it's always sized as an
10981 offset. */
10982 if (dwarf_version == 2)
10983 size = DWARF2_ADDR_SIZE;
10984 else
10985 size = dwarf_offset_size;
10986 /* ??? We cannot unconditionally output die_offset if
10987 non-zero - others might create references to those
10988 DIEs via symbols.
10989 And we do not clear its DIE offset after outputting it
10990 (and the label refers to the actual DIEs, not the
10991 DWARF CU unit header which is when using label + offset
10992 would be the correct thing to do).
10993 ??? This is the reason for the with_offset flag. */
10994 if (AT_ref (a)->with_offset)
10995 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10996 debug_info_section, "%s", name);
10997 else
10998 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10999 name);
11002 else
11004 gcc_assert (AT_ref (a)->die_offset);
11005 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11006 "%s", name);
11008 break;
11010 case dw_val_class_fde_ref:
11012 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11014 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11015 a->dw_attr_val.v.val_fde_index * 2);
11016 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11017 "%s", name);
11019 break;
11021 case dw_val_class_vms_delta:
11022 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11023 dw2_asm_output_vms_delta (dwarf_offset_size,
11024 AT_vms_delta2 (a), AT_vms_delta1 (a),
11025 "%s", name);
11026 #else
11027 dw2_asm_output_delta (dwarf_offset_size,
11028 AT_vms_delta2 (a), AT_vms_delta1 (a),
11029 "%s", name);
11030 #endif
11031 break;
11033 case dw_val_class_lbl_id:
11034 output_attr_index_or_value (a);
11035 break;
11037 case dw_val_class_lineptr:
11038 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11039 debug_line_section, "%s", name);
11040 break;
11042 case dw_val_class_macptr:
11043 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11044 debug_macinfo_section, "%s", name);
11045 break;
11047 case dw_val_class_loclistsptr:
11048 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11049 debug_loc_section, "%s", name);
11050 break;
11052 case dw_val_class_str:
11053 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11054 dw2_asm_output_offset (dwarf_offset_size,
11055 a->dw_attr_val.v.val_str->label,
11056 debug_str_section,
11057 "%s: \"%s\"", name, AT_string (a));
11058 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11059 dw2_asm_output_offset (dwarf_offset_size,
11060 a->dw_attr_val.v.val_str->label,
11061 debug_line_str_section,
11062 "%s: \"%s\"", name, AT_string (a));
11063 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11064 dw2_asm_output_data_uleb128 (AT_index (a),
11065 "%s: \"%s\"", name, AT_string (a));
11066 else
11067 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11068 break;
11070 case dw_val_class_file:
11072 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11074 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11075 a->dw_attr_val.v.val_file->filename);
11076 break;
11079 case dw_val_class_file_implicit:
11080 if (flag_debug_asm)
11081 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11082 ASM_COMMENT_START, name,
11083 maybe_emit_file (a->dw_attr_val.v.val_file),
11084 a->dw_attr_val.v.val_file->filename);
11085 break;
11087 case dw_val_class_data8:
11089 int i;
11091 for (i = 0; i < 8; i++)
11092 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11093 i == 0 ? "%s" : NULL, name);
11094 break;
11097 case dw_val_class_high_pc:
11098 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11099 get_AT_low_pc (die), "DW_AT_high_pc");
11100 break;
11102 case dw_val_class_discr_value:
11103 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11104 break;
11106 case dw_val_class_discr_list:
11108 dw_discr_list_ref list = AT_discr_list (a);
11109 const int size = size_of_discr_list (list);
11111 /* This is a block, so output its length first. */
11112 dw2_asm_output_data (constant_size (size), size,
11113 "%s: block size", name);
11115 for (; list != NULL; list = list->dw_discr_next)
11117 /* One byte for the discriminant value descriptor, and then as
11118 many LEB128 numbers as required. */
11119 if (list->dw_discr_range)
11120 dw2_asm_output_data (1, DW_DSC_range,
11121 "%s: DW_DSC_range", name);
11122 else
11123 dw2_asm_output_data (1, DW_DSC_label,
11124 "%s: DW_DSC_label", name);
11126 output_discr_value (&list->dw_discr_lower_bound, name);
11127 if (list->dw_discr_range)
11128 output_discr_value (&list->dw_discr_upper_bound, name);
11130 break;
11133 default:
11134 gcc_unreachable ();
11138 FOR_EACH_CHILD (die, c, output_die (c));
11140 /* Add null byte to terminate sibling list. */
11141 if (die->die_child != NULL)
11142 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11143 (unsigned long) die->die_offset);
11146 /* Output the dwarf version number. */
11148 static void
11149 output_dwarf_version ()
11151 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11152 views in loclist. That will change eventually. */
11153 if (dwarf_version == 6)
11155 static bool once;
11156 if (!once)
11158 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11159 "incompatibilities");
11160 once = true;
11162 dw2_asm_output_data (2, 5, "DWARF version number");
11164 else
11165 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11168 /* Output the compilation unit that appears at the beginning of the
11169 .debug_info section, and precedes the DIE descriptions. */
11171 static void
11172 output_compilation_unit_header (enum dwarf_unit_type ut)
11174 if (!XCOFF_DEBUGGING_INFO)
11176 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11177 dw2_asm_output_data (4, 0xffffffff,
11178 "Initial length escape value indicating 64-bit DWARF extension");
11179 dw2_asm_output_data (dwarf_offset_size,
11180 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11181 "Length of Compilation Unit Info");
11184 output_dwarf_version ();
11185 if (dwarf_version >= 5)
11187 const char *name;
11188 switch (ut)
11190 case DW_UT_compile: name = "DW_UT_compile"; break;
11191 case DW_UT_type: name = "DW_UT_type"; break;
11192 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11193 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11194 default: gcc_unreachable ();
11196 dw2_asm_output_data (1, ut, "%s", name);
11197 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11199 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11200 debug_abbrev_section,
11201 "Offset Into Abbrev. Section");
11202 if (dwarf_version < 5)
11203 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11206 /* Output the compilation unit DIE and its children. */
11208 static void
11209 output_comp_unit (dw_die_ref die, int output_if_empty,
11210 const unsigned char *dwo_id)
11212 const char *secname, *oldsym;
11213 char *tmp;
11215 /* Unless we are outputting main CU, we may throw away empty ones. */
11216 if (!output_if_empty && die->die_child == NULL)
11217 return;
11219 /* Even if there are no children of this DIE, we must output the information
11220 about the compilation unit. Otherwise, on an empty translation unit, we
11221 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11222 will then complain when examining the file. First mark all the DIEs in
11223 this CU so we know which get local refs. */
11224 mark_dies (die);
11226 external_ref_hash_type *extern_map = optimize_external_refs (die);
11228 /* For now, optimize only the main CU, in order to optimize the rest
11229 we'd need to see all of them earlier. Leave the rest for post-linking
11230 tools like DWZ. */
11231 if (die == comp_unit_die ())
11232 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11234 build_abbrev_table (die, extern_map);
11236 optimize_abbrev_table ();
11238 delete extern_map;
11240 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11241 next_die_offset = (dwo_id
11242 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11243 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11244 calc_die_sizes (die);
11246 oldsym = die->die_id.die_symbol;
11247 if (oldsym && die->comdat_type_p)
11249 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11251 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11252 secname = tmp;
11253 die->die_id.die_symbol = NULL;
11254 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11256 else
11258 switch_to_section (debug_info_section);
11259 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11260 info_section_emitted = true;
11263 /* For LTO cross unit DIE refs we want a symbol on the start of the
11264 debuginfo section, not on the CU DIE. */
11265 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11267 /* ??? No way to get visibility assembled without a decl. */
11268 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11269 get_identifier (oldsym), char_type_node);
11270 TREE_PUBLIC (decl) = true;
11271 TREE_STATIC (decl) = true;
11272 DECL_ARTIFICIAL (decl) = true;
11273 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11274 DECL_VISIBILITY_SPECIFIED (decl) = true;
11275 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11276 #ifdef ASM_WEAKEN_LABEL
11277 /* We prefer a .weak because that handles duplicates from duplicate
11278 archive members in a graceful way. */
11279 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11280 #else
11281 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11282 #endif
11283 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11286 /* Output debugging information. */
11287 output_compilation_unit_header (dwo_id
11288 ? DW_UT_split_compile : DW_UT_compile);
11289 if (dwarf_version >= 5)
11291 if (dwo_id != NULL)
11292 for (int i = 0; i < 8; i++)
11293 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11295 output_die (die);
11297 /* Leave the marks on the main CU, so we can check them in
11298 output_pubnames. */
11299 if (oldsym)
11301 unmark_dies (die);
11302 die->die_id.die_symbol = oldsym;
11306 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11307 and .debug_pubtypes. This is configured per-target, but can be
11308 overridden by the -gpubnames or -gno-pubnames options. */
11310 static inline bool
11311 want_pubnames (void)
11313 if (debug_info_level <= DINFO_LEVEL_TERSE
11314 /* Names and types go to the early debug part only. */
11315 || in_lto_p)
11316 return false;
11317 if (debug_generate_pub_sections != -1)
11318 return debug_generate_pub_sections;
11319 return targetm.want_debug_pub_sections;
11322 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11324 static void
11325 add_AT_pubnames (dw_die_ref die)
11327 if (want_pubnames ())
11328 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11331 /* Add a string attribute value to a skeleton DIE. */
11333 static inline void
11334 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11335 const char *str)
11337 dw_attr_node attr;
11338 struct indirect_string_node *node;
11340 if (! skeleton_debug_str_hash)
11341 skeleton_debug_str_hash
11342 = hash_table<indirect_string_hasher>::create_ggc (10);
11344 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11345 find_string_form (node);
11346 if (node->form == dwarf_FORM (DW_FORM_strx))
11347 node->form = DW_FORM_strp;
11349 attr.dw_attr = attr_kind;
11350 attr.dw_attr_val.val_class = dw_val_class_str;
11351 attr.dw_attr_val.val_entry = NULL;
11352 attr.dw_attr_val.v.val_str = node;
11353 add_dwarf_attr (die, &attr);
11356 /* Helper function to generate top-level dies for skeleton debug_info and
11357 debug_types. */
11359 static void
11360 add_top_level_skeleton_die_attrs (dw_die_ref die)
11362 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11363 const char *comp_dir = comp_dir_string ();
11365 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11366 if (comp_dir != NULL)
11367 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11368 add_AT_pubnames (die);
11369 if (addr_index_table != NULL && addr_index_table->size () > 0)
11370 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11373 /* Output skeleton debug sections that point to the dwo file. */
11375 static void
11376 output_skeleton_debug_sections (dw_die_ref comp_unit,
11377 const unsigned char *dwo_id)
11379 /* These attributes will be found in the full debug_info section. */
11380 remove_AT (comp_unit, DW_AT_producer);
11381 remove_AT (comp_unit, DW_AT_language);
11383 switch_to_section (debug_skeleton_info_section);
11384 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11386 /* Produce the skeleton compilation-unit header. This one differs enough from
11387 a normal CU header that it's better not to call output_compilation_unit
11388 header. */
11389 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11390 dw2_asm_output_data (4, 0xffffffff,
11391 "Initial length escape value indicating 64-bit "
11392 "DWARF extension");
11394 dw2_asm_output_data (dwarf_offset_size,
11395 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11396 - DWARF_INITIAL_LENGTH_SIZE
11397 + size_of_die (comp_unit),
11398 "Length of Compilation Unit Info");
11399 output_dwarf_version ();
11400 if (dwarf_version >= 5)
11402 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11403 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11405 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11406 debug_skeleton_abbrev_section,
11407 "Offset Into Abbrev. Section");
11408 if (dwarf_version < 5)
11409 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11410 else
11411 for (int i = 0; i < 8; i++)
11412 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11414 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11415 output_die (comp_unit);
11417 /* Build the skeleton debug_abbrev section. */
11418 switch_to_section (debug_skeleton_abbrev_section);
11419 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11421 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11423 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11426 /* Output a comdat type unit DIE and its children. */
11428 static void
11429 output_comdat_type_unit (comdat_type_node *node,
11430 bool early_lto_debug ATTRIBUTE_UNUSED)
11432 const char *secname;
11433 char *tmp;
11434 int i;
11435 #if defined (OBJECT_FORMAT_ELF)
11436 tree comdat_key;
11437 #endif
11439 /* First mark all the DIEs in this CU so we know which get local refs. */
11440 mark_dies (node->root_die);
11442 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11444 build_abbrev_table (node->root_die, extern_map);
11446 delete extern_map;
11447 extern_map = NULL;
11449 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11450 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11451 calc_die_sizes (node->root_die);
11453 #if defined (OBJECT_FORMAT_ELF)
11454 if (dwarf_version >= 5)
11456 if (!dwarf_split_debug_info)
11457 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11458 else
11459 secname = (early_lto_debug
11460 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11462 else if (!dwarf_split_debug_info)
11463 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11464 else
11465 secname = (early_lto_debug
11466 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11468 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11469 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11470 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11471 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11472 comdat_key = get_identifier (tmp);
11473 targetm.asm_out.named_section (secname,
11474 SECTION_DEBUG | SECTION_LINKONCE,
11475 comdat_key);
11476 #else
11477 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11478 sprintf (tmp, (dwarf_version >= 5
11479 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11480 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11481 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11482 secname = tmp;
11483 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11484 #endif
11486 /* Output debugging information. */
11487 output_compilation_unit_header (dwarf_split_debug_info
11488 ? DW_UT_split_type : DW_UT_type);
11489 output_signature (node->signature, "Type Signature");
11490 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11491 "Offset to Type DIE");
11492 output_die (node->root_die);
11494 unmark_dies (node->root_die);
11497 /* Return the DWARF2/3 pubname associated with a decl. */
11499 static const char *
11500 dwarf2_name (tree decl, int scope)
11502 if (DECL_NAMELESS (decl))
11503 return NULL;
11504 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11507 /* Add a new entry to .debug_pubnames if appropriate. */
11509 static void
11510 add_pubname_string (const char *str, dw_die_ref die)
11512 pubname_entry e;
11514 e.die = die;
11515 e.name = xstrdup (str);
11516 vec_safe_push (pubname_table, e);
11519 static void
11520 add_pubname (tree decl, dw_die_ref die)
11522 if (!want_pubnames ())
11523 return;
11525 /* Don't add items to the table when we expect that the consumer will have
11526 just read the enclosing die. For example, if the consumer is looking at a
11527 class_member, it will either be inside the class already, or will have just
11528 looked up the class to find the member. Either way, searching the class is
11529 faster than searching the index. */
11530 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11531 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11533 const char *name = dwarf2_name (decl, 1);
11535 if (name)
11536 add_pubname_string (name, die);
11540 /* Add an enumerator to the pubnames section. */
11542 static void
11543 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11545 pubname_entry e;
11547 gcc_assert (scope_name);
11548 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11549 e.die = die;
11550 vec_safe_push (pubname_table, e);
11553 /* Add a new entry to .debug_pubtypes if appropriate. */
11555 static void
11556 add_pubtype (tree decl, dw_die_ref die)
11558 pubname_entry e;
11560 if (!want_pubnames ())
11561 return;
11563 if ((TREE_PUBLIC (decl)
11564 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11565 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11567 tree scope = NULL;
11568 const char *scope_name = "";
11569 const char *sep = is_cxx () ? "::" : ".";
11570 const char *name;
11572 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11573 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11575 scope_name = lang_hooks.dwarf_name (scope, 1);
11576 if (scope_name != NULL && scope_name[0] != '\0')
11577 scope_name = concat (scope_name, sep, NULL);
11578 else
11579 scope_name = "";
11582 if (TYPE_P (decl))
11583 name = type_tag (decl);
11584 else
11585 name = lang_hooks.dwarf_name (decl, 1);
11587 /* If we don't have a name for the type, there's no point in adding
11588 it to the table. */
11589 if (name != NULL && name[0] != '\0')
11591 e.die = die;
11592 e.name = concat (scope_name, name, NULL);
11593 vec_safe_push (pubtype_table, e);
11596 /* Although it might be more consistent to add the pubinfo for the
11597 enumerators as their dies are created, they should only be added if the
11598 enum type meets the criteria above. So rather than re-check the parent
11599 enum type whenever an enumerator die is created, just output them all
11600 here. This isn't protected by the name conditional because anonymous
11601 enums don't have names. */
11602 if (die->die_tag == DW_TAG_enumeration_type)
11604 dw_die_ref c;
11606 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11611 /* Output a single entry in the pubnames table. */
11613 static void
11614 output_pubname (dw_offset die_offset, pubname_entry *entry)
11616 dw_die_ref die = entry->die;
11617 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11619 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11621 if (debug_generate_pub_sections == 2)
11623 /* This logic follows gdb's method for determining the value of the flag
11624 byte. */
11625 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11626 switch (die->die_tag)
11628 case DW_TAG_typedef:
11629 case DW_TAG_base_type:
11630 case DW_TAG_subrange_type:
11631 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11632 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11633 break;
11634 case DW_TAG_enumerator:
11635 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11636 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11637 if (!is_cxx ())
11638 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11639 break;
11640 case DW_TAG_subprogram:
11641 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11642 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11643 if (!is_ada ())
11644 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11645 break;
11646 case DW_TAG_constant:
11647 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11648 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11649 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11650 break;
11651 case DW_TAG_variable:
11652 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11653 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11654 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11655 break;
11656 case DW_TAG_namespace:
11657 case DW_TAG_imported_declaration:
11658 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11659 break;
11660 case DW_TAG_class_type:
11661 case DW_TAG_interface_type:
11662 case DW_TAG_structure_type:
11663 case DW_TAG_union_type:
11664 case DW_TAG_enumeration_type:
11665 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11666 if (!is_cxx ())
11667 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11668 break;
11669 default:
11670 /* An unusual tag. Leave the flag-byte empty. */
11671 break;
11673 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11674 "GDB-index flags");
11677 dw2_asm_output_nstring (entry->name, -1, "external name");
11681 /* Output the public names table used to speed up access to externally
11682 visible names; or the public types table used to find type definitions. */
11684 static void
11685 output_pubnames (vec<pubname_entry, va_gc> *names)
11687 unsigned i;
11688 unsigned long pubnames_length = size_of_pubnames (names);
11689 pubname_entry *pub;
11691 if (!XCOFF_DEBUGGING_INFO)
11693 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11694 dw2_asm_output_data (4, 0xffffffff,
11695 "Initial length escape value indicating 64-bit DWARF extension");
11696 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11697 "Pub Info Length");
11700 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11701 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11703 if (dwarf_split_debug_info)
11704 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11705 debug_skeleton_info_section,
11706 "Offset of Compilation Unit Info");
11707 else
11708 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11709 debug_info_section,
11710 "Offset of Compilation Unit Info");
11711 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11712 "Compilation Unit Length");
11714 FOR_EACH_VEC_ELT (*names, i, pub)
11716 if (include_pubname_in_output (names, pub))
11718 dw_offset die_offset = pub->die->die_offset;
11720 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11721 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11722 gcc_assert (pub->die->die_mark);
11724 /* If we're putting types in their own .debug_types sections,
11725 the .debug_pubtypes table will still point to the compile
11726 unit (not the type unit), so we want to use the offset of
11727 the skeleton DIE (if there is one). */
11728 if (pub->die->comdat_type_p && names == pubtype_table)
11730 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11732 if (type_node != NULL)
11733 die_offset = (type_node->skeleton_die != NULL
11734 ? type_node->skeleton_die->die_offset
11735 : comp_unit_die ()->die_offset);
11738 output_pubname (die_offset, pub);
11742 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11745 /* Output public names and types tables if necessary. */
11747 static void
11748 output_pubtables (void)
11750 if (!want_pubnames () || !info_section_emitted)
11751 return;
11753 switch_to_section (debug_pubnames_section);
11754 output_pubnames (pubname_table);
11755 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11756 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11757 simply won't look for the section. */
11758 switch_to_section (debug_pubtypes_section);
11759 output_pubnames (pubtype_table);
11763 /* Output the information that goes into the .debug_aranges table.
11764 Namely, define the beginning and ending address range of the
11765 text section generated for this compilation unit. */
11767 static void
11768 output_aranges (void)
11770 unsigned i;
11771 unsigned long aranges_length = size_of_aranges ();
11773 if (!XCOFF_DEBUGGING_INFO)
11775 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11776 dw2_asm_output_data (4, 0xffffffff,
11777 "Initial length escape value indicating 64-bit DWARF extension");
11778 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11779 "Length of Address Ranges Info");
11782 /* Version number for aranges is still 2, even up to DWARF5. */
11783 dw2_asm_output_data (2, 2, "DWARF aranges version");
11784 if (dwarf_split_debug_info)
11785 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11786 debug_skeleton_info_section,
11787 "Offset of Compilation Unit Info");
11788 else
11789 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11790 debug_info_section,
11791 "Offset of Compilation Unit Info");
11792 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11793 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11795 /* We need to align to twice the pointer size here. */
11796 if (DWARF_ARANGES_PAD_SIZE)
11798 /* Pad using a 2 byte words so that padding is correct for any
11799 pointer size. */
11800 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11801 2 * DWARF2_ADDR_SIZE);
11802 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11803 dw2_asm_output_data (2, 0, NULL);
11806 /* It is necessary not to output these entries if the sections were
11807 not used; if the sections were not used, the length will be 0 and
11808 the address may end up as 0 if the section is discarded by ld
11809 --gc-sections, leaving an invalid (0, 0) entry that can be
11810 confused with the terminator. */
11811 if (switch_text_ranges)
11813 const char *prev_loc = text_section_label;
11814 const char *loc;
11815 unsigned idx;
11817 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11818 if (prev_loc)
11820 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11821 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11822 prev_loc = NULL;
11824 else
11825 prev_loc = loc;
11827 if (prev_loc)
11829 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11830 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11831 prev_loc, "Length");
11835 if (switch_cold_ranges)
11837 const char *prev_loc = cold_text_section_label;
11838 const char *loc;
11839 unsigned idx;
11841 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11842 if (prev_loc)
11844 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11845 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11846 prev_loc = NULL;
11848 else
11849 prev_loc = loc;
11851 if (prev_loc)
11853 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11854 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11855 prev_loc, "Length");
11859 if (have_multiple_function_sections)
11861 unsigned fde_idx;
11862 dw_fde_ref fde;
11864 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11866 if (fde->ignored_debug)
11867 continue;
11868 if (!fde->in_std_section)
11870 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11871 "Address");
11872 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11873 fde->dw_fde_begin, "Length");
11875 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11877 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11878 "Address");
11879 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11880 fde->dw_fde_second_begin, "Length");
11885 /* Output the terminator words. */
11886 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11887 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11890 /* Add a new entry to .debug_ranges. Return its index into
11891 ranges_table vector. */
11893 static unsigned int
11894 add_ranges_num (int num, bool maybe_new_sec)
11896 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11897 vec_safe_push (ranges_table, r);
11898 return vec_safe_length (ranges_table) - 1;
11901 /* Add a new entry to .debug_ranges corresponding to a block, or a
11902 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11903 this entry might be in a different section from previous range. */
11905 static unsigned int
11906 add_ranges (const_tree block, bool maybe_new_sec)
11908 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11911 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11912 chain, or middle entry of a chain that will be directly referred to. */
11914 static void
11915 note_rnglist_head (unsigned int offset)
11917 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11918 return;
11919 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11922 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11923 When using dwarf_split_debug_info, address attributes in dies destined
11924 for the final executable should be direct references--setting the
11925 parameter force_direct ensures this behavior. */
11927 static void
11928 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11929 bool *added, bool force_direct)
11931 unsigned int in_use = vec_safe_length (ranges_by_label);
11932 unsigned int offset;
11933 dw_ranges_by_label rbl = { begin, end };
11934 vec_safe_push (ranges_by_label, rbl);
11935 offset = add_ranges_num (-(int)in_use - 1, true);
11936 if (!*added)
11938 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11939 *added = true;
11940 note_rnglist_head (offset);
11941 if (dwarf_split_debug_info && force_direct)
11942 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11946 /* Emit .debug_ranges section. */
11948 static void
11949 output_ranges (void)
11951 unsigned i;
11952 static const char *const start_fmt = "Offset %#x";
11953 const char *fmt = start_fmt;
11954 dw_ranges *r;
11956 switch_to_section (debug_ranges_section);
11957 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11958 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11960 int block_num = r->num;
11962 if (block_num > 0)
11964 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11965 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11967 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11968 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11970 /* If all code is in the text section, then the compilation
11971 unit base address defaults to DW_AT_low_pc, which is the
11972 base of the text section. */
11973 if (!have_multiple_function_sections)
11975 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11976 text_section_label,
11977 fmt, i * 2 * DWARF2_ADDR_SIZE);
11978 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11979 text_section_label, NULL);
11982 /* Otherwise, the compilation unit base address is zero,
11983 which allows us to use absolute addresses, and not worry
11984 about whether the target supports cross-section
11985 arithmetic. */
11986 else
11988 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11989 fmt, i * 2 * DWARF2_ADDR_SIZE);
11990 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11993 fmt = NULL;
11996 /* Negative block_num stands for an index into ranges_by_label. */
11997 else if (block_num < 0)
11999 int lab_idx = - block_num - 1;
12001 if (!have_multiple_function_sections)
12003 gcc_unreachable ();
12004 #if 0
12005 /* If we ever use add_ranges_by_labels () for a single
12006 function section, all we have to do is to take out
12007 the #if 0 above. */
12008 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12009 (*ranges_by_label)[lab_idx].begin,
12010 text_section_label,
12011 fmt, i * 2 * DWARF2_ADDR_SIZE);
12012 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12013 (*ranges_by_label)[lab_idx].end,
12014 text_section_label, NULL);
12015 #endif
12017 else
12019 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12020 (*ranges_by_label)[lab_idx].begin,
12021 fmt, i * 2 * DWARF2_ADDR_SIZE);
12022 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12023 (*ranges_by_label)[lab_idx].end,
12024 NULL);
12027 else
12029 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12030 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12031 fmt = start_fmt;
12036 /* Non-zero if .debug_line_str should be used for .debug_line section
12037 strings or strings that are likely shareable with those. */
12038 #define DWARF5_USE_DEBUG_LINE_STR \
12039 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12040 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12041 /* FIXME: there is no .debug_line_str.dwo section, \
12042 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12043 && !dwarf_split_debug_info)
12046 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12047 DWARF5 .debug_line tables using .debug_line_str or we generate
12048 it ourselves, except for split-dwarf which doesn't have a
12049 .debug_line_str. */
12050 static bool
12051 asm_outputs_debug_line_str (void)
12053 if (dwarf_version >= 5
12054 && ! output_asm_line_debug_info ()
12055 && DWARF5_USE_DEBUG_LINE_STR)
12056 return true;
12057 else
12059 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12060 return !dwarf_split_debug_info && dwarf_version >= 5;
12061 #else
12062 return false;
12063 #endif
12067 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12068 I is index of the following range. */
12070 static bool
12071 use_distinct_base_address_for_range (unsigned int i)
12073 if (i >= vec_safe_length (ranges_table))
12074 return false;
12076 dw_ranges *r2 = &(*ranges_table)[i];
12077 /* Use DW_RLE_base_address{,x} if there is a next range in the
12078 range list and is guaranteed to be in the same section. */
12079 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12082 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12083 section when needed. */
12085 static void
12086 index_rnglists (void)
12088 unsigned i;
12089 dw_ranges *r;
12090 bool base = false;
12092 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12094 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12095 r->idx = rnglist_idx++;
12097 if (!have_multiple_function_sections)
12098 continue;
12099 int block_num = r->num;
12100 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12101 base = false;
12102 if (block_num > 0)
12104 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12105 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12107 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12108 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12110 if (HAVE_AS_LEB128)
12112 if (!base && use_distinct_base_address_for_range (i + 1))
12114 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12115 ate_kind_label);
12116 base = true;
12118 if (base)
12119 /* If we have a base, no need for further
12120 begin_entry/end_entry, as DW_RLE_offset_pair will be
12121 used. */
12122 continue;
12123 r->begin_entry
12124 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12125 /* No need for end_entry, DW_RLE_start{,x}_length will use
12126 length as opposed to a pair of addresses. */
12128 else
12130 r->begin_entry
12131 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12132 r->end_entry
12133 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12137 /* Negative block_num stands for an index into ranges_by_label. */
12138 else if (block_num < 0)
12140 int lab_idx = - block_num - 1;
12141 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12142 const char *elabel = (*ranges_by_label)[lab_idx].end;
12144 r->begin_entry
12145 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12146 if (!HAVE_AS_LEB128)
12147 r->end_entry
12148 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12153 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12155 static bool
12156 output_rnglists (unsigned generation, bool dwo)
12158 unsigned i;
12159 dw_ranges *r;
12160 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12161 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12162 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12164 if (dwo)
12165 switch_to_section (debug_ranges_dwo_section);
12166 else
12168 switch_to_section (debug_ranges_section);
12169 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12171 /* There are up to 4 unique ranges labels per generation.
12172 See also init_sections_and_labels. */
12173 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12174 2 + 2 * dwo + generation * 6);
12175 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12176 3 + 2 * dwo + generation * 6);
12177 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12178 dw2_asm_output_data (4, 0xffffffff,
12179 "Initial length escape value indicating "
12180 "64-bit DWARF extension");
12181 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12182 "Length of Range Lists");
12183 ASM_OUTPUT_LABEL (asm_out_file, l1);
12184 output_dwarf_version ();
12185 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12186 dw2_asm_output_data (1, 0, "Segment Size");
12187 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12188 about relocation sizes and primarily care about the size of .debug*
12189 sections in linked shared libraries and executables, then
12190 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12191 into it are usually larger than just DW_FORM_sec_offset offsets
12192 into the .debug_rnglists section. */
12193 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12194 "Offset Entry Count");
12195 if (dwo)
12197 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12198 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12199 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12200 dw2_asm_output_delta (dwarf_offset_size, r->label,
12201 ranges_base_label, NULL);
12204 const char *lab = "";
12205 const char *base = NULL;
12206 bool skipping = false;
12207 bool ret = false;
12208 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12210 int block_num = r->num;
12212 if (r->label)
12214 if (dwarf_split_debug_info
12215 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12217 ret = true;
12218 skipping = true;
12219 continue;
12221 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12222 lab = r->label;
12224 if (skipping)
12226 if (block_num == 0)
12227 skipping = false;
12228 continue;
12230 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12231 base = NULL;
12232 if (block_num > 0)
12234 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12235 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12237 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12238 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12240 if (HAVE_AS_LEB128)
12242 /* If all code is in the text section, then the compilation
12243 unit base address defaults to DW_AT_low_pc, which is the
12244 base of the text section. */
12245 if (!have_multiple_function_sections)
12247 dw2_asm_output_data (1, DW_RLE_offset_pair,
12248 "DW_RLE_offset_pair (%s)", lab);
12249 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12250 "Range begin address (%s)", lab);
12251 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12252 "Range end address (%s)", lab);
12253 continue;
12255 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12257 if (dwarf_split_debug_info)
12259 dw2_asm_output_data (1, DW_RLE_base_addressx,
12260 "DW_RLE_base_addressx (%s)", lab);
12261 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12262 "Base address index (%s)",
12263 blabel);
12265 else
12267 dw2_asm_output_data (1, DW_RLE_base_address,
12268 "DW_RLE_base_address (%s)", lab);
12269 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12270 "Base address (%s)", lab);
12272 strcpy (basebuf, blabel);
12273 base = basebuf;
12275 if (base)
12277 dw2_asm_output_data (1, DW_RLE_offset_pair,
12278 "DW_RLE_offset_pair (%s)", lab);
12279 dw2_asm_output_delta_uleb128 (blabel, base,
12280 "Range begin address (%s)", lab);
12281 dw2_asm_output_delta_uleb128 (elabel, base,
12282 "Range end address (%s)", lab);
12283 continue;
12285 if (dwarf_split_debug_info)
12287 dw2_asm_output_data (1, DW_RLE_startx_length,
12288 "DW_RLE_startx_length (%s)", lab);
12289 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12290 "Range begin address index "
12291 "(%s)", blabel);
12293 else
12295 dw2_asm_output_data (1, DW_RLE_start_length,
12296 "DW_RLE_start_length (%s)", lab);
12297 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12298 "Range begin address (%s)", lab);
12300 dw2_asm_output_delta_uleb128 (elabel, blabel,
12301 "Range length (%s)", lab);
12303 else if (dwarf_split_debug_info)
12305 dw2_asm_output_data (1, DW_RLE_startx_endx,
12306 "DW_RLE_startx_endx (%s)", lab);
12307 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12308 "Range begin address index "
12309 "(%s)", blabel);
12310 dw2_asm_output_data_uleb128 (r->end_entry->index,
12311 "Range end address index "
12312 "(%s)", elabel);
12314 else
12316 dw2_asm_output_data (1, DW_RLE_start_end,
12317 "DW_RLE_start_end (%s)", lab);
12318 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12319 "Range begin address (%s)", lab);
12320 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12321 "Range end address (%s)", lab);
12325 /* Negative block_num stands for an index into ranges_by_label. */
12326 else if (block_num < 0)
12328 int lab_idx = - block_num - 1;
12329 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12330 const char *elabel = (*ranges_by_label)[lab_idx].end;
12332 if (!have_multiple_function_sections)
12333 gcc_unreachable ();
12334 if (HAVE_AS_LEB128)
12336 if (dwarf_split_debug_info)
12338 dw2_asm_output_data (1, DW_RLE_startx_length,
12339 "DW_RLE_startx_length (%s)", lab);
12340 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12341 "Range begin address index "
12342 "(%s)", blabel);
12344 else
12346 dw2_asm_output_data (1, DW_RLE_start_length,
12347 "DW_RLE_start_length (%s)", lab);
12348 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12349 "Range begin address (%s)", lab);
12351 dw2_asm_output_delta_uleb128 (elabel, blabel,
12352 "Range length (%s)", lab);
12354 else if (dwarf_split_debug_info)
12356 dw2_asm_output_data (1, DW_RLE_startx_endx,
12357 "DW_RLE_startx_endx (%s)", lab);
12358 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12359 "Range begin address index "
12360 "(%s)", blabel);
12361 dw2_asm_output_data_uleb128 (r->end_entry->index,
12362 "Range end address index "
12363 "(%s)", elabel);
12365 else
12367 dw2_asm_output_data (1, DW_RLE_start_end,
12368 "DW_RLE_start_end (%s)", lab);
12369 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12370 "Range begin address (%s)", lab);
12371 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12372 "Range end address (%s)", lab);
12375 else
12376 dw2_asm_output_data (1, DW_RLE_end_of_list,
12377 "DW_RLE_end_of_list (%s)", lab);
12379 ASM_OUTPUT_LABEL (asm_out_file, l2);
12380 return ret;
12383 /* Data structure containing information about input files. */
12384 struct file_info
12386 const char *path; /* Complete file name. */
12387 const char *fname; /* File name part. */
12388 int length; /* Length of entire string. */
12389 struct dwarf_file_data * file_idx; /* Index in input file table. */
12390 int dir_idx; /* Index in directory table. */
12393 /* Data structure containing information about directories with source
12394 files. */
12395 struct dir_info
12397 const char *path; /* Path including directory name. */
12398 int length; /* Path length. */
12399 int prefix; /* Index of directory entry which is a prefix. */
12400 int count; /* Number of files in this directory. */
12401 int dir_idx; /* Index of directory used as base. */
12404 /* Callback function for file_info comparison. We sort by looking at
12405 the directories in the path. */
12407 static int
12408 file_info_cmp (const void *p1, const void *p2)
12410 const struct file_info *const s1 = (const struct file_info *) p1;
12411 const struct file_info *const s2 = (const struct file_info *) p2;
12412 const unsigned char *cp1;
12413 const unsigned char *cp2;
12415 /* Take care of file names without directories. We need to make sure that
12416 we return consistent values to qsort since some will get confused if
12417 we return the same value when identical operands are passed in opposite
12418 orders. So if neither has a directory, return 0 and otherwise return
12419 1 or -1 depending on which one has the directory. We want the one with
12420 the directory to sort after the one without, so all no directory files
12421 are at the start (normally only the compilation unit file). */
12422 if ((s1->path == s1->fname || s2->path == s2->fname))
12423 return (s2->path == s2->fname) - (s1->path == s1->fname);
12425 cp1 = (const unsigned char *) s1->path;
12426 cp2 = (const unsigned char *) s2->path;
12428 while (1)
12430 ++cp1;
12431 ++cp2;
12432 /* Reached the end of the first path? If so, handle like above,
12433 but now we want longer directory prefixes before shorter ones. */
12434 if ((cp1 == (const unsigned char *) s1->fname)
12435 || (cp2 == (const unsigned char *) s2->fname))
12436 return ((cp1 == (const unsigned char *) s1->fname)
12437 - (cp2 == (const unsigned char *) s2->fname));
12439 /* Character of current path component the same? */
12440 else if (*cp1 != *cp2)
12441 return *cp1 - *cp2;
12445 struct file_name_acquire_data
12447 struct file_info *files;
12448 int used_files;
12449 int max_files;
12452 /* Traversal function for the hash table. */
12455 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12457 struct dwarf_file_data *d = *slot;
12458 struct file_info *fi;
12459 const char *f;
12461 gcc_assert (fnad->max_files >= d->emitted_number);
12463 if (! d->emitted_number)
12464 return 1;
12466 gcc_assert (fnad->max_files != fnad->used_files);
12468 fi = fnad->files + fnad->used_files++;
12470 f = d->filename;
12472 /* Skip all leading "./". */
12473 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12474 f += 2;
12476 /* Create a new array entry. */
12477 fi->path = f;
12478 fi->length = strlen (f);
12479 fi->file_idx = d;
12481 /* Search for the file name part. */
12482 f = strrchr (f, DIR_SEPARATOR);
12483 #if defined (DIR_SEPARATOR_2)
12485 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12487 if (g != NULL)
12489 if (f == NULL || f < g)
12490 f = g;
12493 #endif
12495 fi->fname = f == NULL ? fi->path : f + 1;
12496 return 1;
12499 /* Helper function for output_file_names. Emit a FORM encoded
12500 string STR, with assembly comment start ENTRY_KIND and
12501 index IDX */
12503 static void
12504 output_line_string (enum dwarf_form form, const char *str,
12505 const char *entry_kind, unsigned int idx)
12507 switch (form)
12509 case DW_FORM_string:
12510 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12511 break;
12512 case DW_FORM_line_strp:
12513 if (!debug_line_str_hash)
12514 debug_line_str_hash
12515 = hash_table<indirect_string_hasher>::create_ggc (10);
12517 struct indirect_string_node *node;
12518 node = find_AT_string_in_table (str, debug_line_str_hash);
12519 set_indirect_string (node);
12520 node->form = form;
12521 dw2_asm_output_offset (dwarf_offset_size, node->label,
12522 debug_line_str_section, "%s: %#x: \"%s\"",
12523 entry_kind, 0, node->str);
12524 break;
12525 default:
12526 gcc_unreachable ();
12530 /* Output the directory table and the file name table. We try to minimize
12531 the total amount of memory needed. A heuristic is used to avoid large
12532 slowdowns with many input files. */
12534 static void
12535 output_file_names (void)
12537 struct file_name_acquire_data fnad;
12538 int numfiles;
12539 struct file_info *files;
12540 struct dir_info *dirs;
12541 int *saved;
12542 int *savehere;
12543 int *backmap;
12544 int ndirs;
12545 int idx_offset;
12546 int i;
12548 if (!last_emitted_file)
12550 if (dwarf_version >= 5)
12552 const char *comp_dir = comp_dir_string ();
12553 if (comp_dir == NULL)
12554 comp_dir = "";
12555 dw2_asm_output_data (1, 1, "Directory entry format count");
12556 enum dwarf_form str_form = DW_FORM_string;
12557 if (DWARF5_USE_DEBUG_LINE_STR)
12558 str_form = DW_FORM_line_strp;
12559 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12560 dw2_asm_output_data_uleb128 (str_form, "%s",
12561 get_DW_FORM_name (str_form));
12562 dw2_asm_output_data_uleb128 (1, "Directories count");
12563 if (str_form == DW_FORM_string)
12564 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12565 else
12566 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12567 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12568 if (filename0 == NULL)
12569 filename0 = "";
12570 #ifdef VMS_DEBUGGING_INFO
12571 dw2_asm_output_data (1, 4, "File name entry format count");
12572 #else
12573 dw2_asm_output_data (1, 2, "File name entry format count");
12574 #endif
12575 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12576 dw2_asm_output_data_uleb128 (str_form, "%s",
12577 get_DW_FORM_name (str_form));
12578 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12579 "DW_LNCT_directory_index");
12580 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12581 get_DW_FORM_name (DW_FORM_data1));
12582 #ifdef VMS_DEBUGGING_INFO
12583 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12584 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12585 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12586 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12587 #endif
12588 dw2_asm_output_data_uleb128 (1, "File names count");
12590 output_line_string (str_form, filename0, "File Entry", 0);
12591 dw2_asm_output_data (1, 0, NULL);
12592 #ifdef VMS_DEBUGGING_INFO
12593 dw2_asm_output_data_uleb128 (0, NULL);
12594 dw2_asm_output_data_uleb128 (0, NULL);
12595 #endif
12597 else
12599 dw2_asm_output_data (1, 0, "End directory table");
12600 dw2_asm_output_data (1, 0, "End file name table");
12602 return;
12605 numfiles = last_emitted_file->emitted_number;
12607 /* Allocate the various arrays we need. */
12608 files = XALLOCAVEC (struct file_info, numfiles);
12609 dirs = XALLOCAVEC (struct dir_info, numfiles);
12611 fnad.files = files;
12612 fnad.used_files = 0;
12613 fnad.max_files = numfiles;
12614 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12615 gcc_assert (fnad.used_files == fnad.max_files);
12617 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12619 /* Find all the different directories used. */
12620 dirs[0].path = files[0].path;
12621 dirs[0].length = files[0].fname - files[0].path;
12622 dirs[0].prefix = -1;
12623 dirs[0].count = 1;
12624 dirs[0].dir_idx = 0;
12625 files[0].dir_idx = 0;
12626 ndirs = 1;
12628 for (i = 1; i < numfiles; i++)
12629 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12630 && memcmp (dirs[ndirs - 1].path, files[i].path,
12631 dirs[ndirs - 1].length) == 0)
12633 /* Same directory as last entry. */
12634 files[i].dir_idx = ndirs - 1;
12635 ++dirs[ndirs - 1].count;
12637 else
12639 int j;
12641 /* This is a new directory. */
12642 dirs[ndirs].path = files[i].path;
12643 dirs[ndirs].length = files[i].fname - files[i].path;
12644 dirs[ndirs].count = 1;
12645 dirs[ndirs].dir_idx = ndirs;
12646 files[i].dir_idx = ndirs;
12648 /* Search for a prefix. */
12649 dirs[ndirs].prefix = -1;
12650 for (j = 0; j < ndirs; j++)
12651 if (dirs[j].length < dirs[ndirs].length
12652 && dirs[j].length > 1
12653 && (dirs[ndirs].prefix == -1
12654 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12655 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12656 dirs[ndirs].prefix = j;
12658 ++ndirs;
12661 /* Now to the actual work. We have to find a subset of the directories which
12662 allow expressing the file name using references to the directory table
12663 with the least amount of characters. We do not do an exhaustive search
12664 where we would have to check out every combination of every single
12665 possible prefix. Instead we use a heuristic which provides nearly optimal
12666 results in most cases and never is much off. */
12667 saved = XALLOCAVEC (int, ndirs);
12668 savehere = XALLOCAVEC (int, ndirs);
12670 memset (saved, '\0', ndirs * sizeof (saved[0]));
12671 for (i = 0; i < ndirs; i++)
12673 int j;
12674 int total;
12676 /* We can always save some space for the current directory. But this
12677 does not mean it will be enough to justify adding the directory. */
12678 savehere[i] = dirs[i].length;
12679 total = (savehere[i] - saved[i]) * dirs[i].count;
12681 for (j = i + 1; j < ndirs; j++)
12683 savehere[j] = 0;
12684 if (saved[j] < dirs[i].length)
12686 /* Determine whether the dirs[i] path is a prefix of the
12687 dirs[j] path. */
12688 int k;
12690 k = dirs[j].prefix;
12691 while (k != -1 && k != (int) i)
12692 k = dirs[k].prefix;
12694 if (k == (int) i)
12696 /* Yes it is. We can possibly save some memory by
12697 writing the filenames in dirs[j] relative to
12698 dirs[i]. */
12699 savehere[j] = dirs[i].length;
12700 total += (savehere[j] - saved[j]) * dirs[j].count;
12705 /* Check whether we can save enough to justify adding the dirs[i]
12706 directory. */
12707 if (total > dirs[i].length + 1)
12709 /* It's worthwhile adding. */
12710 for (j = i; j < ndirs; j++)
12711 if (savehere[j] > 0)
12713 /* Remember how much we saved for this directory so far. */
12714 saved[j] = savehere[j];
12716 /* Remember the prefix directory. */
12717 dirs[j].dir_idx = i;
12722 /* Emit the directory name table. */
12723 idx_offset = dirs[0].length > 0 ? 1 : 0;
12724 enum dwarf_form str_form = DW_FORM_string;
12725 enum dwarf_form idx_form = DW_FORM_udata;
12726 if (dwarf_version >= 5)
12728 const char *comp_dir = comp_dir_string ();
12729 if (comp_dir == NULL)
12730 comp_dir = "";
12731 dw2_asm_output_data (1, 1, "Directory entry format count");
12732 if (DWARF5_USE_DEBUG_LINE_STR)
12733 str_form = DW_FORM_line_strp;
12734 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12735 dw2_asm_output_data_uleb128 (str_form, "%s",
12736 get_DW_FORM_name (str_form));
12737 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12738 if (str_form == DW_FORM_string)
12740 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12741 for (i = 1 - idx_offset; i < ndirs; i++)
12742 dw2_asm_output_nstring (dirs[i].path,
12743 dirs[i].length
12744 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12745 "Directory Entry: %#x", i + idx_offset);
12747 else
12749 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12750 for (i = 1 - idx_offset; i < ndirs; i++)
12752 const char *str
12753 = ggc_alloc_string (dirs[i].path,
12754 dirs[i].length
12755 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12756 output_line_string (str_form, str, "Directory Entry",
12757 (unsigned) i + idx_offset);
12761 else
12763 for (i = 1 - idx_offset; i < ndirs; i++)
12764 dw2_asm_output_nstring (dirs[i].path,
12765 dirs[i].length
12766 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12767 "Directory Entry: %#x", i + idx_offset);
12769 dw2_asm_output_data (1, 0, "End directory table");
12772 /* We have to emit them in the order of emitted_number since that's
12773 used in the debug info generation. To do this efficiently we
12774 generate a back-mapping of the indices first. */
12775 backmap = XALLOCAVEC (int, numfiles);
12776 for (i = 0; i < numfiles; i++)
12777 backmap[files[i].file_idx->emitted_number - 1] = i;
12779 if (dwarf_version >= 5)
12781 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12782 if (filename0 == NULL)
12783 filename0 = "";
12784 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12785 DW_FORM_data2. Choose one based on the number of directories
12786 and how much space would they occupy in each encoding.
12787 If we have at most 256 directories, all indexes fit into
12788 a single byte, so DW_FORM_data1 is most compact (if there
12789 are at most 128 directories, DW_FORM_udata would be as
12790 compact as that, but not shorter and slower to decode). */
12791 if (ndirs + idx_offset <= 256)
12792 idx_form = DW_FORM_data1;
12793 /* If there are more than 65536 directories, we have to use
12794 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12795 Otherwise, compute what space would occupy if all the indexes
12796 used DW_FORM_udata - sum - and compare that to how large would
12797 be DW_FORM_data2 encoding, and pick the more efficient one. */
12798 else if (ndirs + idx_offset <= 65536)
12800 unsigned HOST_WIDE_INT sum = 1;
12801 for (i = 0; i < numfiles; i++)
12803 int file_idx = backmap[i];
12804 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12805 sum += size_of_uleb128 (dir_idx);
12807 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12808 idx_form = DW_FORM_data2;
12810 #ifdef VMS_DEBUGGING_INFO
12811 dw2_asm_output_data (1, 4, "File name entry format count");
12812 #else
12813 dw2_asm_output_data (1, 2, "File name entry format count");
12814 #endif
12815 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12816 dw2_asm_output_data_uleb128 (str_form, "%s",
12817 get_DW_FORM_name (str_form));
12818 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12819 "DW_LNCT_directory_index");
12820 dw2_asm_output_data_uleb128 (idx_form, "%s",
12821 get_DW_FORM_name (idx_form));
12822 #ifdef VMS_DEBUGGING_INFO
12823 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12824 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12825 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12826 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12827 #endif
12828 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12830 output_line_string (str_form, filename0, "File Entry", 0);
12832 /* Include directory index. */
12833 if (idx_form != DW_FORM_udata)
12834 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12835 0, NULL);
12836 else
12837 dw2_asm_output_data_uleb128 (0, NULL);
12839 #ifdef VMS_DEBUGGING_INFO
12840 dw2_asm_output_data_uleb128 (0, NULL);
12841 dw2_asm_output_data_uleb128 (0, NULL);
12842 #endif
12845 /* Now write all the file names. */
12846 for (i = 0; i < numfiles; i++)
12848 int file_idx = backmap[i];
12849 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12851 #ifdef VMS_DEBUGGING_INFO
12852 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12854 /* Setting these fields can lead to debugger miscomparisons,
12855 but VMS Debug requires them to be set correctly. */
12857 int ver;
12858 long long cdt;
12859 long siz;
12860 int maxfilelen = (strlen (files[file_idx].path)
12861 + dirs[dir_idx].length
12862 + MAX_VMS_VERSION_LEN + 1);
12863 char *filebuf = XALLOCAVEC (char, maxfilelen);
12865 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12866 snprintf (filebuf, maxfilelen, "%s;%d",
12867 files[file_idx].path + dirs[dir_idx].length, ver);
12869 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12871 /* Include directory index. */
12872 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12873 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12874 dir_idx + idx_offset, NULL);
12875 else
12876 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12878 /* Modification time. */
12879 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12880 &cdt, 0, 0, 0) == 0)
12881 ? cdt : 0, NULL);
12883 /* File length in bytes. */
12884 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12885 0, &siz, 0, 0) == 0)
12886 ? siz : 0, NULL);
12887 #else
12888 output_line_string (str_form,
12889 files[file_idx].path + dirs[dir_idx].length,
12890 "File Entry", (unsigned) i + 1);
12892 /* Include directory index. */
12893 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12894 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12895 dir_idx + idx_offset, NULL);
12896 else
12897 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12899 if (dwarf_version >= 5)
12900 continue;
12902 /* Modification time. */
12903 dw2_asm_output_data_uleb128 (0, NULL);
12905 /* File length in bytes. */
12906 dw2_asm_output_data_uleb128 (0, NULL);
12907 #endif /* VMS_DEBUGGING_INFO */
12910 if (dwarf_version < 5)
12911 dw2_asm_output_data (1, 0, "End file name table");
12915 /* Output one line number table into the .debug_line section. */
12917 static void
12918 output_one_line_info_table (dw_line_info_table *table)
12920 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12921 unsigned int current_line = 1;
12922 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12923 dw_line_info_entry *ent, *prev_addr;
12924 size_t i;
12925 unsigned int view;
12927 view = 0;
12929 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12931 switch (ent->opcode)
12933 case LI_set_address:
12934 /* ??? Unfortunately, we have little choice here currently, and
12935 must always use the most general form. GCC does not know the
12936 address delta itself, so we can't use DW_LNS_advance_pc. Many
12937 ports do have length attributes which will give an upper bound
12938 on the address range. We could perhaps use length attributes
12939 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12940 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12942 view = 0;
12944 /* This can handle any delta. This takes
12945 4+DWARF2_ADDR_SIZE bytes. */
12946 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12947 debug_variable_location_views
12948 ? ", reset view to 0" : "");
12949 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12950 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12951 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12953 prev_addr = ent;
12954 break;
12956 case LI_adv_address:
12958 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12959 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12960 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12962 view++;
12964 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12965 dw2_asm_output_delta (2, line_label, prev_label,
12966 "from %s to %s", prev_label, line_label);
12968 prev_addr = ent;
12969 break;
12972 case LI_set_line:
12973 if (ent->val == current_line)
12975 /* We still need to start a new row, so output a copy insn. */
12976 dw2_asm_output_data (1, DW_LNS_copy,
12977 "copy line %u", current_line);
12979 else
12981 int line_offset = ent->val - current_line;
12982 int line_delta = line_offset - DWARF_LINE_BASE;
12984 current_line = ent->val;
12985 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12987 /* This can handle deltas from -10 to 234, using the current
12988 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12989 This takes 1 byte. */
12990 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12991 "line %u", current_line);
12993 else
12995 /* This can handle any delta. This takes at least 4 bytes,
12996 depending on the value being encoded. */
12997 dw2_asm_output_data (1, DW_LNS_advance_line,
12998 "advance to line %u", current_line);
12999 dw2_asm_output_data_sleb128 (line_offset, NULL);
13000 dw2_asm_output_data (1, DW_LNS_copy, NULL);
13003 break;
13005 case LI_set_file:
13006 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13007 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13008 break;
13010 case LI_set_column:
13011 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13012 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13013 break;
13015 case LI_negate_stmt:
13016 current_is_stmt = !current_is_stmt;
13017 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13018 "is_stmt %d", current_is_stmt);
13019 break;
13021 case LI_set_prologue_end:
13022 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13023 "set prologue end");
13024 break;
13026 case LI_set_epilogue_begin:
13027 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13028 "set epilogue begin");
13029 break;
13031 case LI_set_discriminator:
13032 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13033 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13034 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13035 dw2_asm_output_data_uleb128 (ent->val, NULL);
13036 break;
13040 /* Emit debug info for the address of the end of the table. */
13041 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13042 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13043 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13044 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13046 dw2_asm_output_data (1, 0, "end sequence");
13047 dw2_asm_output_data_uleb128 (1, NULL);
13048 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13051 static unsigned int output_line_info_generation;
13053 /* Output the source line number correspondence information. This
13054 information goes into the .debug_line section. */
13056 static void
13057 output_line_info (bool prologue_only)
13059 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13060 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13061 bool saw_one = false;
13062 int opc;
13064 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13065 output_line_info_generation);
13066 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13067 output_line_info_generation);
13068 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13069 output_line_info_generation);
13070 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13071 output_line_info_generation++);
13073 if (!XCOFF_DEBUGGING_INFO)
13075 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13076 dw2_asm_output_data (4, 0xffffffff,
13077 "Initial length escape value indicating 64-bit DWARF extension");
13078 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13079 "Length of Source Line Info");
13082 ASM_OUTPUT_LABEL (asm_out_file, l1);
13084 output_dwarf_version ();
13085 if (dwarf_version >= 5)
13087 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13088 dw2_asm_output_data (1, 0, "Segment Size");
13090 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13091 ASM_OUTPUT_LABEL (asm_out_file, p1);
13093 /* Define the architecture-dependent minimum instruction length (in bytes).
13094 In this implementation of DWARF, this field is used for information
13095 purposes only. Since GCC generates assembly language, we have no
13096 a priori knowledge of how many instruction bytes are generated for each
13097 source line, and therefore can use only the DW_LNE_set_address and
13098 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13099 this as '1', which is "correct enough" for all architectures,
13100 and don't let the target override. */
13101 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13103 if (dwarf_version >= 4)
13104 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13105 "Maximum Operations Per Instruction");
13106 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13107 "Default is_stmt_start flag");
13108 dw2_asm_output_data (1, DWARF_LINE_BASE,
13109 "Line Base Value (Special Opcodes)");
13110 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13111 "Line Range Value (Special Opcodes)");
13112 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13113 "Special Opcode Base");
13115 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13117 int n_op_args;
13118 switch (opc)
13120 case DW_LNS_advance_pc:
13121 case DW_LNS_advance_line:
13122 case DW_LNS_set_file:
13123 case DW_LNS_set_column:
13124 case DW_LNS_fixed_advance_pc:
13125 case DW_LNS_set_isa:
13126 n_op_args = 1;
13127 break;
13128 default:
13129 n_op_args = 0;
13130 break;
13133 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13134 opc, n_op_args);
13137 /* Write out the information about the files we use. */
13138 output_file_names ();
13139 ASM_OUTPUT_LABEL (asm_out_file, p2);
13140 if (prologue_only)
13142 /* Output the marker for the end of the line number info. */
13143 ASM_OUTPUT_LABEL (asm_out_file, l2);
13144 return;
13147 if (separate_line_info)
13149 dw_line_info_table *table;
13150 size_t i;
13152 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13153 if (table->in_use)
13155 output_one_line_info_table (table);
13156 saw_one = true;
13159 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13161 output_one_line_info_table (cold_text_section_line_info);
13162 saw_one = true;
13165 /* ??? Some Darwin linkers crash on a .debug_line section with no
13166 sequences. Further, merely a DW_LNE_end_sequence entry is not
13167 sufficient -- the address column must also be initialized.
13168 Make sure to output at least one set_address/end_sequence pair,
13169 choosing .text since that section is always present. */
13170 if (text_section_line_info->in_use || !saw_one)
13171 output_one_line_info_table (text_section_line_info);
13173 /* Output the marker for the end of the line number info. */
13174 ASM_OUTPUT_LABEL (asm_out_file, l2);
13177 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13179 static inline bool
13180 need_endianity_attribute_p (bool reverse)
13182 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13185 /* Given a pointer to a tree node for some base type, return a pointer to
13186 a DIE that describes the given type. REVERSE is true if the type is
13187 to be interpreted in the reverse storage order wrt the target order.
13189 This routine must only be called for GCC type nodes that correspond to
13190 Dwarf base (fundamental) types. */
13192 dw_die_ref
13193 base_type_die (tree type, bool reverse)
13195 dw_die_ref base_type_result;
13196 enum dwarf_type encoding;
13197 bool fpt_used = false;
13198 struct fixed_point_type_info fpt_info;
13199 tree type_bias = NULL_TREE;
13201 /* If this is a subtype that should not be emitted as a subrange type,
13202 use the base type. See subrange_type_for_debug_p. */
13203 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13204 type = TREE_TYPE (type);
13206 switch (TREE_CODE (type))
13208 case INTEGER_TYPE:
13209 if ((dwarf_version >= 4 || !dwarf_strict)
13210 && TYPE_NAME (type)
13211 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13212 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13213 && DECL_NAME (TYPE_NAME (type)))
13215 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13216 if (strcmp (name, "char16_t") == 0
13217 || strcmp (name, "char32_t") == 0)
13219 encoding = DW_ATE_UTF;
13220 break;
13223 if ((dwarf_version >= 3 || !dwarf_strict)
13224 && lang_hooks.types.get_fixed_point_type_info)
13226 memset (&fpt_info, 0, sizeof (fpt_info));
13227 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13229 fpt_used = true;
13230 encoding = ((TYPE_UNSIGNED (type))
13231 ? DW_ATE_unsigned_fixed
13232 : DW_ATE_signed_fixed);
13233 break;
13236 if (TYPE_STRING_FLAG (type))
13238 if (TYPE_UNSIGNED (type))
13239 encoding = DW_ATE_unsigned_char;
13240 else
13241 encoding = DW_ATE_signed_char;
13243 else if (TYPE_UNSIGNED (type))
13244 encoding = DW_ATE_unsigned;
13245 else
13246 encoding = DW_ATE_signed;
13248 if (!dwarf_strict
13249 && lang_hooks.types.get_type_bias)
13250 type_bias = lang_hooks.types.get_type_bias (type);
13251 break;
13253 case REAL_TYPE:
13254 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13256 if (dwarf_version >= 3 || !dwarf_strict)
13257 encoding = DW_ATE_decimal_float;
13258 else
13259 encoding = DW_ATE_lo_user;
13261 else
13262 encoding = DW_ATE_float;
13263 break;
13265 case FIXED_POINT_TYPE:
13266 if (!(dwarf_version >= 3 || !dwarf_strict))
13267 encoding = DW_ATE_lo_user;
13268 else if (TYPE_UNSIGNED (type))
13269 encoding = DW_ATE_unsigned_fixed;
13270 else
13271 encoding = DW_ATE_signed_fixed;
13272 break;
13274 /* Dwarf2 doesn't know anything about complex ints, so use
13275 a user defined type for it. */
13276 case COMPLEX_TYPE:
13277 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13278 encoding = DW_ATE_complex_float;
13279 else
13280 encoding = DW_ATE_lo_user;
13281 break;
13283 case BOOLEAN_TYPE:
13284 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13285 encoding = DW_ATE_boolean;
13286 break;
13288 default:
13289 /* No other TREE_CODEs are Dwarf fundamental types. */
13290 gcc_unreachable ();
13293 base_type_result = new_die_raw (DW_TAG_base_type);
13295 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13296 int_size_in_bytes (type));
13297 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13299 if (need_endianity_attribute_p (reverse))
13300 add_AT_unsigned (base_type_result, DW_AT_endianity,
13301 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13303 add_alignment_attribute (base_type_result, type);
13305 if (fpt_used)
13307 switch (fpt_info.scale_factor_kind)
13309 case fixed_point_scale_factor_binary:
13310 add_AT_int (base_type_result, DW_AT_binary_scale,
13311 fpt_info.scale_factor.binary);
13312 break;
13314 case fixed_point_scale_factor_decimal:
13315 add_AT_int (base_type_result, DW_AT_decimal_scale,
13316 fpt_info.scale_factor.decimal);
13317 break;
13319 case fixed_point_scale_factor_arbitrary:
13320 /* Arbitrary scale factors cannot be described in standard DWARF. */
13321 if (!dwarf_strict)
13323 /* Describe the scale factor as a rational constant. */
13324 const dw_die_ref scale_factor
13325 = new_die (DW_TAG_constant, comp_unit_die (), type);
13327 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13328 fpt_info.scale_factor.arbitrary.numerator,
13329 dw_scalar_form_constant, NULL);
13330 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13331 fpt_info.scale_factor.arbitrary.denominator,
13332 dw_scalar_form_constant, NULL);
13334 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13336 break;
13338 default:
13339 gcc_unreachable ();
13343 if (type_bias)
13344 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13345 dw_scalar_form_constant
13346 | dw_scalar_form_exprloc
13347 | dw_scalar_form_reference,
13348 NULL);
13350 return base_type_result;
13353 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13354 named 'auto' in its type: return true for it, false otherwise. */
13356 static inline bool
13357 is_cxx_auto (tree type)
13359 if (is_cxx ())
13361 tree name = TYPE_IDENTIFIER (type);
13362 if (name == get_identifier ("auto")
13363 || name == get_identifier ("decltype(auto)"))
13364 return true;
13366 return false;
13369 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13370 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13372 static inline int
13373 is_base_type (tree type)
13375 switch (TREE_CODE (type))
13377 case INTEGER_TYPE:
13378 case REAL_TYPE:
13379 case FIXED_POINT_TYPE:
13380 case COMPLEX_TYPE:
13381 case BOOLEAN_TYPE:
13382 return 1;
13384 case VOID_TYPE:
13385 case OPAQUE_TYPE:
13386 case ARRAY_TYPE:
13387 case RECORD_TYPE:
13388 case UNION_TYPE:
13389 case QUAL_UNION_TYPE:
13390 case ENUMERAL_TYPE:
13391 case FUNCTION_TYPE:
13392 case METHOD_TYPE:
13393 case POINTER_TYPE:
13394 case REFERENCE_TYPE:
13395 case NULLPTR_TYPE:
13396 case OFFSET_TYPE:
13397 case LANG_TYPE:
13398 case VECTOR_TYPE:
13399 return 0;
13401 default:
13402 if (is_cxx_auto (type))
13403 return 0;
13404 gcc_unreachable ();
13408 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13409 node, return the size in bits for the type if it is a constant, or else
13410 return the alignment for the type if the type's size is not constant, or
13411 else return BITS_PER_WORD if the type actually turns out to be an
13412 ERROR_MARK node. */
13414 static inline unsigned HOST_WIDE_INT
13415 simple_type_size_in_bits (const_tree type)
13417 if (TREE_CODE (type) == ERROR_MARK)
13418 return BITS_PER_WORD;
13419 else if (TYPE_SIZE (type) == NULL_TREE)
13420 return 0;
13421 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13422 return tree_to_uhwi (TYPE_SIZE (type));
13423 else
13424 return TYPE_ALIGN (type);
13427 /* Similarly, but return an offset_int instead of UHWI. */
13429 static inline offset_int
13430 offset_int_type_size_in_bits (const_tree type)
13432 if (TREE_CODE (type) == ERROR_MARK)
13433 return BITS_PER_WORD;
13434 else if (TYPE_SIZE (type) == NULL_TREE)
13435 return 0;
13436 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13437 return wi::to_offset (TYPE_SIZE (type));
13438 else
13439 return TYPE_ALIGN (type);
13442 /* Given a pointer to a tree node for a subrange type, return a pointer
13443 to a DIE that describes the given type. */
13445 static dw_die_ref
13446 subrange_type_die (tree type, tree low, tree high, tree bias,
13447 dw_die_ref context_die)
13449 dw_die_ref subrange_die;
13450 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13452 if (context_die == NULL)
13453 context_die = comp_unit_die ();
13455 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13457 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13459 /* The size of the subrange type and its base type do not match,
13460 so we need to generate a size attribute for the subrange type. */
13461 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13464 add_alignment_attribute (subrange_die, type);
13466 if (low)
13467 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13468 if (high)
13469 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13470 if (bias && !dwarf_strict)
13471 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13472 dw_scalar_form_constant
13473 | dw_scalar_form_exprloc
13474 | dw_scalar_form_reference,
13475 NULL);
13477 return subrange_die;
13480 /* Returns the (const and/or volatile) cv_qualifiers associated with
13481 the decl node. This will normally be augmented with the
13482 cv_qualifiers of the underlying type in add_type_attribute. */
13484 static int
13485 decl_quals (const_tree decl)
13487 return ((TREE_READONLY (decl)
13488 /* The C++ front-end correctly marks reference-typed
13489 variables as readonly, but from a language (and debug
13490 info) standpoint they are not const-qualified. */
13491 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13492 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13493 | (TREE_THIS_VOLATILE (decl)
13494 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13497 /* Determine the TYPE whose qualifiers match the largest strict subset
13498 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13499 qualifiers outside QUAL_MASK. */
13501 static int
13502 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13504 tree t;
13505 int best_rank = 0, best_qual = 0, max_rank;
13507 type_quals &= qual_mask;
13508 max_rank = popcount_hwi (type_quals) - 1;
13510 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13511 t = TYPE_NEXT_VARIANT (t))
13513 int q = TYPE_QUALS (t) & qual_mask;
13515 if ((q & type_quals) == q && q != type_quals
13516 && check_base_type (t, type))
13518 int rank = popcount_hwi (q);
13520 if (rank > best_rank)
13522 best_rank = rank;
13523 best_qual = q;
13528 return best_qual;
13531 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13532 static const dwarf_qual_info_t dwarf_qual_info[] =
13534 { TYPE_QUAL_CONST, DW_TAG_const_type },
13535 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13536 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13537 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13539 static const unsigned int dwarf_qual_info_size
13540 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13542 /* If DIE is a qualified DIE of some base DIE with the same parent,
13543 return the base DIE, otherwise return NULL. Set MASK to the
13544 qualifiers added compared to the returned DIE. */
13546 static dw_die_ref
13547 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13549 unsigned int i;
13550 for (i = 0; i < dwarf_qual_info_size; i++)
13551 if (die->die_tag == dwarf_qual_info[i].t)
13552 break;
13553 if (i == dwarf_qual_info_size)
13554 return NULL;
13555 if (vec_safe_length (die->die_attr) != 1)
13556 return NULL;
13557 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13558 if (type == NULL || type->die_parent != die->die_parent)
13559 return NULL;
13560 *mask |= dwarf_qual_info[i].q;
13561 if (depth)
13563 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13564 if (ret)
13565 return ret;
13567 return type;
13570 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13571 entry that chains the modifiers specified by CV_QUALS in front of the
13572 given type. REVERSE is true if the type is to be interpreted in the
13573 reverse storage order wrt the target order. */
13575 static dw_die_ref
13576 modified_type_die (tree type, int cv_quals, bool reverse,
13577 dw_die_ref context_die)
13579 enum tree_code code = TREE_CODE (type);
13580 dw_die_ref mod_type_die;
13581 dw_die_ref sub_die = NULL;
13582 tree item_type = NULL;
13583 tree qualified_type;
13584 tree name, low, high;
13585 dw_die_ref mod_scope;
13586 struct array_descr_info info;
13587 /* Only these cv-qualifiers are currently handled. */
13588 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13589 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13590 ENCODE_QUAL_ADDR_SPACE(~0U));
13591 const bool reverse_base_type
13592 = need_endianity_attribute_p (reverse) && is_base_type (type);
13594 if (code == ERROR_MARK)
13595 return NULL;
13597 if (lang_hooks.types.get_debug_type)
13599 tree debug_type = lang_hooks.types.get_debug_type (type);
13601 if (debug_type != NULL_TREE && debug_type != type)
13602 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13605 cv_quals &= cv_qual_mask;
13607 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13608 tag modifier (and not an attribute) old consumers won't be able
13609 to handle it. */
13610 if (dwarf_version < 3)
13611 cv_quals &= ~TYPE_QUAL_RESTRICT;
13613 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13614 if (dwarf_version < 5)
13615 cv_quals &= ~TYPE_QUAL_ATOMIC;
13617 /* See if we already have the appropriately qualified variant of
13618 this type. */
13619 qualified_type = get_qualified_type (type, cv_quals);
13621 if (qualified_type == sizetype)
13623 /* Try not to expose the internal sizetype type's name. */
13624 if (TYPE_NAME (qualified_type)
13625 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13627 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13629 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13630 && (TYPE_PRECISION (t)
13631 == TYPE_PRECISION (qualified_type))
13632 && (TYPE_UNSIGNED (t)
13633 == TYPE_UNSIGNED (qualified_type)));
13634 qualified_type = t;
13636 else if (qualified_type == sizetype
13637 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13638 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13639 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13640 qualified_type = size_type_node;
13641 if (type == sizetype)
13642 type = qualified_type;
13645 /* If we do, then we can just use its DIE, if it exists. */
13646 if (qualified_type)
13648 mod_type_die = lookup_type_die (qualified_type);
13650 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13651 dealt with specially: the DIE with the attribute, if it exists, is
13652 placed immediately after the regular DIE for the same base type. */
13653 if (mod_type_die
13654 && (!reverse_base_type
13655 || ((mod_type_die = mod_type_die->die_sib) != NULL
13656 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13657 return mod_type_die;
13660 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13662 /* Handle C typedef types. */
13663 if (name
13664 && TREE_CODE (name) == TYPE_DECL
13665 && DECL_ORIGINAL_TYPE (name)
13666 && !DECL_ARTIFICIAL (name))
13668 tree dtype = TREE_TYPE (name);
13670 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13671 if (qualified_type == dtype && !reverse_base_type)
13673 tree origin = decl_ultimate_origin (name);
13675 /* Typedef variants that have an abstract origin don't get their own
13676 type DIE (see gen_typedef_die), so fall back on the ultimate
13677 abstract origin instead. */
13678 if (origin != NULL && origin != name)
13679 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13680 context_die);
13682 /* For a named type, use the typedef. */
13683 gen_type_die (qualified_type, context_die);
13684 return lookup_type_die (qualified_type);
13686 else
13688 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13689 dquals &= cv_qual_mask;
13690 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13691 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13692 /* cv-unqualified version of named type. Just use
13693 the unnamed type to which it refers. */
13694 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13695 reverse, context_die);
13696 /* Else cv-qualified version of named type; fall through. */
13700 mod_scope = scope_die_for (type, context_die);
13702 if (cv_quals)
13704 int sub_quals = 0, first_quals = 0;
13705 unsigned i;
13706 dw_die_ref first = NULL, last = NULL;
13708 /* Determine a lesser qualified type that most closely matches
13709 this one. Then generate DW_TAG_* entries for the remaining
13710 qualifiers. */
13711 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13712 cv_qual_mask);
13713 if (sub_quals && use_debug_types)
13715 bool needed = false;
13716 /* If emitting type units, make sure the order of qualifiers
13717 is canonical. Thus, start from unqualified type if
13718 an earlier qualifier is missing in sub_quals, but some later
13719 one is present there. */
13720 for (i = 0; i < dwarf_qual_info_size; i++)
13721 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13722 needed = true;
13723 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13725 sub_quals = 0;
13726 break;
13729 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13730 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13732 /* As not all intermediate qualified DIEs have corresponding
13733 tree types, ensure that qualified DIEs in the same scope
13734 as their DW_AT_type are emitted after their DW_AT_type,
13735 only with other qualified DIEs for the same type possibly
13736 in between them. Determine the range of such qualified
13737 DIEs now (first being the base type, last being corresponding
13738 last qualified DIE for it). */
13739 unsigned int count = 0;
13740 first = qualified_die_p (mod_type_die, &first_quals,
13741 dwarf_qual_info_size);
13742 if (first == NULL)
13743 first = mod_type_die;
13744 gcc_assert ((first_quals & ~sub_quals) == 0);
13745 for (count = 0, last = first;
13746 count < (1U << dwarf_qual_info_size);
13747 count++, last = last->die_sib)
13749 int quals = 0;
13750 if (last == mod_scope->die_child)
13751 break;
13752 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13753 != first)
13754 break;
13758 for (i = 0; i < dwarf_qual_info_size; i++)
13759 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13761 dw_die_ref d;
13762 if (first && first != last)
13764 for (d = first->die_sib; ; d = d->die_sib)
13766 int quals = 0;
13767 qualified_die_p (d, &quals, dwarf_qual_info_size);
13768 if (quals == (first_quals | dwarf_qual_info[i].q))
13769 break;
13770 if (d == last)
13772 d = NULL;
13773 break;
13776 if (d)
13778 mod_type_die = d;
13779 continue;
13782 if (first)
13784 d = new_die_raw (dwarf_qual_info[i].t);
13785 add_child_die_after (mod_scope, d, last);
13786 last = d;
13788 else
13789 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13790 if (mod_type_die)
13791 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13792 mod_type_die = d;
13793 first_quals |= dwarf_qual_info[i].q;
13796 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13798 dwarf_tag tag = DW_TAG_pointer_type;
13799 if (code == REFERENCE_TYPE)
13801 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13802 tag = DW_TAG_rvalue_reference_type;
13803 else
13804 tag = DW_TAG_reference_type;
13806 mod_type_die = new_die (tag, mod_scope, type);
13808 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13809 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13810 add_alignment_attribute (mod_type_die, type);
13811 item_type = TREE_TYPE (type);
13813 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13814 if (!ADDR_SPACE_GENERIC_P (as))
13816 int action = targetm.addr_space.debug (as);
13817 if (action >= 0)
13819 /* Positive values indicate an address_class. */
13820 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13822 else
13824 /* Negative values indicate an (inverted) segment base reg. */
13825 dw_loc_descr_ref d
13826 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13827 add_AT_loc (mod_type_die, DW_AT_segment, d);
13831 else if (code == ARRAY_TYPE
13832 || (lang_hooks.types.get_array_descr_info
13833 && lang_hooks.types.get_array_descr_info (type, &info)))
13835 gen_type_die (type, context_die);
13836 return lookup_type_die (type);
13838 else if (code == INTEGER_TYPE
13839 && TREE_TYPE (type) != NULL_TREE
13840 && subrange_type_for_debug_p (type, &low, &high))
13842 tree bias = NULL_TREE;
13843 if (lang_hooks.types.get_type_bias)
13844 bias = lang_hooks.types.get_type_bias (type);
13845 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13846 item_type = TREE_TYPE (type);
13848 else if (is_base_type (type))
13850 mod_type_die = base_type_die (type, reverse);
13852 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13853 if (reverse_base_type)
13855 dw_die_ref after_die
13856 = modified_type_die (type, cv_quals, false, context_die);
13857 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13859 else
13860 add_child_die (comp_unit_die (), mod_type_die);
13862 add_pubtype (type, mod_type_die);
13864 else
13866 gen_type_die (type, context_die);
13868 /* We have to get the type_main_variant here (and pass that to the
13869 `lookup_type_die' routine) because the ..._TYPE node we have
13870 might simply be a *copy* of some original type node (where the
13871 copy was created to help us keep track of typedef names) and
13872 that copy might have a different TYPE_UID from the original
13873 ..._TYPE node. */
13874 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13876 /* For function/method types, can't just use type_main_variant here,
13877 because that can have different ref-qualifiers for C++,
13878 but try to canonicalize. */
13879 tree main = TYPE_MAIN_VARIANT (type);
13880 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13881 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13882 && check_base_type (t, main)
13883 && check_lang_type (t, type))
13884 return lookup_type_die (t);
13885 return lookup_type_die (type);
13887 /* Vectors have the debugging information in the type,
13888 not the main variant. */
13889 else if (code == VECTOR_TYPE)
13890 return lookup_type_die (type);
13891 else
13892 return lookup_type_die (type_main_variant (type));
13895 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13896 don't output a DW_TAG_typedef, since there isn't one in the
13897 user's program; just attach a DW_AT_name to the type.
13898 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13899 if the base type already has the same name. */
13900 if (name
13901 && ((TREE_CODE (name) != TYPE_DECL
13902 && (qualified_type == TYPE_MAIN_VARIANT (type)
13903 || (cv_quals == TYPE_UNQUALIFIED)))
13904 || (TREE_CODE (name) == TYPE_DECL
13905 && TREE_TYPE (name) == qualified_type
13906 && DECL_NAME (name))))
13908 if (TREE_CODE (name) == TYPE_DECL)
13909 /* Could just call add_name_and_src_coords_attributes here,
13910 but since this is a builtin type it doesn't have any
13911 useful source coordinates anyway. */
13912 name = DECL_NAME (name);
13913 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13915 /* This probably indicates a bug. */
13916 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13918 name = TYPE_IDENTIFIER (type);
13919 add_name_attribute (mod_type_die,
13920 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13923 if (qualified_type && !reverse_base_type)
13924 equate_type_number_to_die (qualified_type, mod_type_die);
13926 if (item_type)
13927 /* We must do this after the equate_type_number_to_die call, in case
13928 this is a recursive type. This ensures that the modified_type_die
13929 recursion will terminate even if the type is recursive. Recursive
13930 types are possible in Ada. */
13931 sub_die = modified_type_die (item_type,
13932 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13933 reverse,
13934 context_die);
13936 if (sub_die != NULL)
13937 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13939 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13940 if (TYPE_ARTIFICIAL (type))
13941 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13943 return mod_type_die;
13946 /* Generate DIEs for the generic parameters of T.
13947 T must be either a generic type or a generic function.
13948 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13950 static void
13951 gen_generic_params_dies (tree t)
13953 tree parms, args;
13954 int parms_num, i;
13955 dw_die_ref die = NULL;
13956 int non_default;
13958 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13959 return;
13961 if (TYPE_P (t))
13962 die = lookup_type_die (t);
13963 else if (DECL_P (t))
13964 die = lookup_decl_die (t);
13966 gcc_assert (die);
13968 parms = lang_hooks.get_innermost_generic_parms (t);
13969 if (!parms)
13970 /* T has no generic parameter. It means T is neither a generic type
13971 or function. End of story. */
13972 return;
13974 parms_num = TREE_VEC_LENGTH (parms);
13975 args = lang_hooks.get_innermost_generic_args (t);
13976 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13977 non_default = int_cst_value (TREE_CHAIN (args));
13978 else
13979 non_default = TREE_VEC_LENGTH (args);
13980 for (i = 0; i < parms_num; i++)
13982 tree parm, arg, arg_pack_elems;
13983 dw_die_ref parm_die;
13985 parm = TREE_VEC_ELT (parms, i);
13986 arg = TREE_VEC_ELT (args, i);
13987 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13988 gcc_assert (parm && TREE_VALUE (parm) && arg);
13990 if (parm && TREE_VALUE (parm) && arg)
13992 /* If PARM represents a template parameter pack,
13993 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13994 by DW_TAG_template_*_parameter DIEs for the argument
13995 pack elements of ARG. Note that ARG would then be
13996 an argument pack. */
13997 if (arg_pack_elems)
13998 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13999 arg_pack_elems,
14000 die);
14001 else
14002 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14003 true /* emit name */, die);
14004 if (i >= non_default)
14005 add_AT_flag (parm_die, DW_AT_default_value, 1);
14010 /* Create and return a DIE for PARM which should be
14011 the representation of a generic type parameter.
14012 For instance, in the C++ front end, PARM would be a template parameter.
14013 ARG is the argument to PARM.
14014 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14015 name of the PARM.
14016 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14017 as a child node. */
14019 static dw_die_ref
14020 generic_parameter_die (tree parm, tree arg,
14021 bool emit_name_p,
14022 dw_die_ref parent_die)
14024 dw_die_ref tmpl_die = NULL;
14025 const char *name = NULL;
14027 /* C++20 accepts class literals as template parameters, and var
14028 decls with initializers represent them. The VAR_DECLs would be
14029 rejected, but we can take the DECL_INITIAL constructor and
14030 attempt to expand it. */
14031 if (arg && VAR_P (arg))
14032 arg = DECL_INITIAL (arg);
14034 if (!parm || !DECL_NAME (parm) || !arg)
14035 return NULL;
14037 /* We support non-type generic parameters and arguments,
14038 type generic parameters and arguments, as well as
14039 generic generic parameters (a.k.a. template template parameters in C++)
14040 and arguments. */
14041 if (TREE_CODE (parm) == PARM_DECL)
14042 /* PARM is a nontype generic parameter */
14043 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14044 else if (TREE_CODE (parm) == TYPE_DECL)
14045 /* PARM is a type generic parameter. */
14046 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14047 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14048 /* PARM is a generic generic parameter.
14049 Its DIE is a GNU extension. It shall have a
14050 DW_AT_name attribute to represent the name of the template template
14051 parameter, and a DW_AT_GNU_template_name attribute to represent the
14052 name of the template template argument. */
14053 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14054 parent_die, parm);
14055 else
14056 gcc_unreachable ();
14058 if (tmpl_die)
14060 tree tmpl_type;
14062 /* If PARM is a generic parameter pack, it means we are
14063 emitting debug info for a template argument pack element.
14064 In other terms, ARG is a template argument pack element.
14065 In that case, we don't emit any DW_AT_name attribute for
14066 the die. */
14067 if (emit_name_p)
14069 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14070 gcc_assert (name);
14071 add_AT_string (tmpl_die, DW_AT_name, name);
14074 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14076 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14077 TMPL_DIE should have a child DW_AT_type attribute that is set
14078 to the type of the argument to PARM, which is ARG.
14079 If PARM is a type generic parameter, TMPL_DIE should have a
14080 child DW_AT_type that is set to ARG. */
14081 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14082 add_type_attribute (tmpl_die, tmpl_type,
14083 (TREE_THIS_VOLATILE (tmpl_type)
14084 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14085 false, parent_die);
14087 else
14089 /* So TMPL_DIE is a DIE representing a
14090 a generic generic template parameter, a.k.a template template
14091 parameter in C++ and arg is a template. */
14093 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14094 to the name of the argument. */
14095 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14096 if (name)
14097 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14100 if (TREE_CODE (parm) == PARM_DECL)
14101 /* So PARM is a non-type generic parameter.
14102 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14103 attribute of TMPL_DIE which value represents the value
14104 of ARG.
14105 We must be careful here:
14106 The value of ARG might reference some function decls.
14107 We might currently be emitting debug info for a generic
14108 type and types are emitted before function decls, we don't
14109 know if the function decls referenced by ARG will actually be
14110 emitted after cgraph computations.
14111 So must defer the generation of the DW_AT_const_value to
14112 after cgraph is ready. */
14113 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14116 return tmpl_die;
14119 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14120 PARM_PACK must be a template parameter pack. The returned DIE
14121 will be child DIE of PARENT_DIE. */
14123 static dw_die_ref
14124 template_parameter_pack_die (tree parm_pack,
14125 tree parm_pack_args,
14126 dw_die_ref parent_die)
14128 dw_die_ref die;
14129 int j;
14131 gcc_assert (parent_die && parm_pack);
14133 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14134 add_name_and_src_coords_attributes (die, parm_pack);
14135 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14136 generic_parameter_die (parm_pack,
14137 TREE_VEC_ELT (parm_pack_args, j),
14138 false /* Don't emit DW_AT_name */,
14139 die);
14140 return die;
14143 /* Return the DBX register number described by a given RTL node. */
14145 static unsigned int
14146 dbx_reg_number (const_rtx rtl)
14148 unsigned regno = REGNO (rtl);
14150 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14152 #ifdef LEAF_REG_REMAP
14153 if (crtl->uses_only_leaf_regs)
14155 int leaf_reg = LEAF_REG_REMAP (regno);
14156 if (leaf_reg != -1)
14157 regno = (unsigned) leaf_reg;
14159 #endif
14161 regno = DBX_REGISTER_NUMBER (regno);
14162 gcc_assert (regno != INVALID_REGNUM);
14163 return regno;
14166 /* Optionally add a DW_OP_piece term to a location description expression.
14167 DW_OP_piece is only added if the location description expression already
14168 doesn't end with DW_OP_piece. */
14170 static void
14171 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14173 dw_loc_descr_ref loc;
14175 if (*list_head != NULL)
14177 /* Find the end of the chain. */
14178 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14181 if (loc->dw_loc_opc != DW_OP_piece)
14182 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14186 /* Return a location descriptor that designates a machine register or
14187 zero if there is none. */
14189 static dw_loc_descr_ref
14190 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14192 rtx regs;
14194 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14195 return 0;
14197 /* We only use "frame base" when we're sure we're talking about the
14198 post-prologue local stack frame. We do this by *not* running
14199 register elimination until this point, and recognizing the special
14200 argument pointer and soft frame pointer rtx's.
14201 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14202 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14203 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14205 dw_loc_descr_ref result = NULL;
14207 if (dwarf_version >= 4 || !dwarf_strict)
14209 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14210 initialized);
14211 if (result)
14212 add_loc_descr (&result,
14213 new_loc_descr (DW_OP_stack_value, 0, 0));
14215 return result;
14218 regs = targetm.dwarf_register_span (rtl);
14220 if (REG_NREGS (rtl) > 1 || regs)
14221 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14222 else
14224 unsigned int dbx_regnum = dbx_reg_number (rtl);
14225 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14226 return 0;
14227 return one_reg_loc_descriptor (dbx_regnum, initialized);
14231 /* Return a location descriptor that designates a machine register for
14232 a given hard register number. */
14234 static dw_loc_descr_ref
14235 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14237 dw_loc_descr_ref reg_loc_descr;
14239 if (regno <= 31)
14240 reg_loc_descr
14241 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14242 else
14243 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14245 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14246 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14248 return reg_loc_descr;
14251 /* Given an RTL of a register, return a location descriptor that
14252 designates a value that spans more than one register. */
14254 static dw_loc_descr_ref
14255 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14256 enum var_init_status initialized)
14258 int size, i;
14259 dw_loc_descr_ref loc_result = NULL;
14261 /* Simple, contiguous registers. */
14262 if (regs == NULL_RTX)
14264 unsigned reg = REGNO (rtl);
14265 int nregs;
14267 #ifdef LEAF_REG_REMAP
14268 if (crtl->uses_only_leaf_regs)
14270 int leaf_reg = LEAF_REG_REMAP (reg);
14271 if (leaf_reg != -1)
14272 reg = (unsigned) leaf_reg;
14274 #endif
14276 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
14277 nregs = REG_NREGS (rtl);
14279 /* At present we only track constant-sized pieces. */
14280 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14281 return NULL;
14282 size /= nregs;
14284 loc_result = NULL;
14285 while (nregs--)
14287 dw_loc_descr_ref t;
14289 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
14290 VAR_INIT_STATUS_INITIALIZED);
14291 add_loc_descr (&loc_result, t);
14292 add_loc_descr_op_piece (&loc_result, size);
14293 ++reg;
14295 return loc_result;
14298 /* Now onto stupid register sets in non contiguous locations. */
14300 gcc_assert (GET_CODE (regs) == PARALLEL);
14302 /* At present we only track constant-sized pieces. */
14303 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14304 return NULL;
14305 loc_result = NULL;
14307 for (i = 0; i < XVECLEN (regs, 0); ++i)
14309 dw_loc_descr_ref t;
14311 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
14312 VAR_INIT_STATUS_INITIALIZED);
14313 add_loc_descr (&loc_result, t);
14314 add_loc_descr_op_piece (&loc_result, size);
14317 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14318 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14319 return loc_result;
14322 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14324 /* Return a location descriptor that designates a constant i,
14325 as a compound operation from constant (i >> shift), constant shift
14326 and DW_OP_shl. */
14328 static dw_loc_descr_ref
14329 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14331 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14332 add_loc_descr (&ret, int_loc_descriptor (shift));
14333 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14334 return ret;
14337 /* Return a location descriptor that designates constant POLY_I. */
14339 static dw_loc_descr_ref
14340 int_loc_descriptor (poly_int64 poly_i)
14342 enum dwarf_location_atom op;
14344 HOST_WIDE_INT i;
14345 if (!poly_i.is_constant (&i))
14347 /* Create location descriptions for the non-constant part and
14348 add any constant offset at the end. */
14349 dw_loc_descr_ref ret = NULL;
14350 HOST_WIDE_INT constant = poly_i.coeffs[0];
14351 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14353 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14354 if (coeff != 0)
14356 dw_loc_descr_ref start = ret;
14357 unsigned int factor;
14358 int bias;
14359 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14360 (j, &factor, &bias);
14362 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14363 add COEFF * (REGNO / FACTOR) now and subtract
14364 COEFF * BIAS from the final constant part. */
14365 constant -= coeff * bias;
14366 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14367 if (coeff % factor == 0)
14368 coeff /= factor;
14369 else
14371 int amount = exact_log2 (factor);
14372 gcc_assert (amount >= 0);
14373 add_loc_descr (&ret, int_loc_descriptor (amount));
14374 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14376 if (coeff != 1)
14378 add_loc_descr (&ret, int_loc_descriptor (coeff));
14379 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14381 if (start)
14382 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14385 loc_descr_plus_const (&ret, constant);
14386 return ret;
14389 /* Pick the smallest representation of a constant, rather than just
14390 defaulting to the LEB encoding. */
14391 if (i >= 0)
14393 int clz = clz_hwi (i);
14394 int ctz = ctz_hwi (i);
14395 if (i <= 31)
14396 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14397 else if (i <= 0xff)
14398 op = DW_OP_const1u;
14399 else if (i <= 0xffff)
14400 op = DW_OP_const2u;
14401 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14402 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14403 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14404 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14405 while DW_OP_const4u is 5 bytes. */
14406 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14407 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14408 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14409 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14410 while DW_OP_const4u is 5 bytes. */
14411 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14413 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14414 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14415 <= 4)
14417 /* As i >= 2**31, the double cast above will yield a negative number.
14418 Since wrapping is defined in DWARF expressions we can output big
14419 positive integers as small negative ones, regardless of the size
14420 of host wide ints.
14422 Here, since the evaluator will handle 32-bit values and since i >=
14423 2**31, we know it's going to be interpreted as a negative literal:
14424 store it this way if we can do better than 5 bytes this way. */
14425 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14427 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14428 op = DW_OP_const4u;
14430 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14431 least 6 bytes: see if we can do better before falling back to it. */
14432 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14433 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14434 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14435 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14436 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14437 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14438 >= HOST_BITS_PER_WIDE_INT)
14439 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14440 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14441 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14442 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14443 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14444 && size_of_uleb128 (i) > 6)
14445 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14446 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14447 else
14448 op = DW_OP_constu;
14450 else
14452 if (i >= -0x80)
14453 op = DW_OP_const1s;
14454 else if (i >= -0x8000)
14455 op = DW_OP_const2s;
14456 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14458 if (size_of_int_loc_descriptor (i) < 5)
14460 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14461 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14462 return ret;
14464 op = DW_OP_const4s;
14466 else
14468 if (size_of_int_loc_descriptor (i)
14469 < (unsigned long) 1 + size_of_sleb128 (i))
14471 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14472 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14473 return ret;
14475 op = DW_OP_consts;
14479 return new_loc_descr (op, i, 0);
14482 /* Likewise, for unsigned constants. */
14484 static dw_loc_descr_ref
14485 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14487 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14488 const unsigned HOST_WIDE_INT max_uint
14489 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14491 /* If possible, use the clever signed constants handling. */
14492 if (i <= max_int)
14493 return int_loc_descriptor ((HOST_WIDE_INT) i);
14495 /* Here, we are left with positive numbers that cannot be represented as
14496 HOST_WIDE_INT, i.e.:
14497 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14499 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14500 whereas may be better to output a negative integer: thanks to integer
14501 wrapping, we know that:
14502 x = x - 2 ** DWARF2_ADDR_SIZE
14503 = x - 2 * (max (HOST_WIDE_INT) + 1)
14504 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14505 small negative integers. Let's try that in cases it will clearly improve
14506 the encoding: there is no gain turning DW_OP_const4u into
14507 DW_OP_const4s. */
14508 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14509 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14510 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14512 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14514 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14515 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14516 const HOST_WIDE_INT second_shift
14517 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14519 /* So we finally have:
14520 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14521 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14522 return int_loc_descriptor (second_shift);
14525 /* Last chance: fallback to a simple constant operation. */
14526 return new_loc_descr
14527 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14528 ? DW_OP_const4u
14529 : DW_OP_const8u,
14530 i, 0);
14533 /* Generate and return a location description that computes the unsigned
14534 comparison of the two stack top entries (a OP b where b is the top-most
14535 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14536 LE_EXPR, GT_EXPR or GE_EXPR. */
14538 static dw_loc_descr_ref
14539 uint_comparison_loc_list (enum tree_code kind)
14541 enum dwarf_location_atom op, flip_op;
14542 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14544 switch (kind)
14546 case LT_EXPR:
14547 op = DW_OP_lt;
14548 break;
14549 case LE_EXPR:
14550 op = DW_OP_le;
14551 break;
14552 case GT_EXPR:
14553 op = DW_OP_gt;
14554 break;
14555 case GE_EXPR:
14556 op = DW_OP_ge;
14557 break;
14558 default:
14559 gcc_unreachable ();
14562 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14563 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14565 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14566 possible to perform unsigned comparisons: we just have to distinguish
14567 three cases:
14569 1. when a and b have the same sign (as signed integers); then we should
14570 return: a OP(signed) b;
14572 2. when a is a negative signed integer while b is a positive one, then a
14573 is a greater unsigned integer than b; likewise when a and b's roles
14574 are flipped.
14576 So first, compare the sign of the two operands. */
14577 ret = new_loc_descr (DW_OP_over, 0, 0);
14578 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14579 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14580 /* If they have different signs (i.e. they have different sign bits), then
14581 the stack top value has now the sign bit set and thus it's smaller than
14582 zero. */
14583 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14584 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14585 add_loc_descr (&ret, bra_node);
14587 /* We are in case 1. At this point, we know both operands have the same
14588 sign, to it's safe to use the built-in signed comparison. */
14589 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14590 add_loc_descr (&ret, jmp_node);
14592 /* We are in case 2. Here, we know both operands do not have the same sign,
14593 so we have to flip the signed comparison. */
14594 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14595 tmp = new_loc_descr (flip_op, 0, 0);
14596 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14597 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14598 add_loc_descr (&ret, tmp);
14600 /* This dummy operation is necessary to make the two branches join. */
14601 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14602 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14603 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14604 add_loc_descr (&ret, tmp);
14606 return ret;
14609 /* Likewise, but takes the location description lists (might be destructive on
14610 them). Return NULL if either is NULL or if concatenation fails. */
14612 static dw_loc_list_ref
14613 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14614 enum tree_code kind)
14616 if (left == NULL || right == NULL)
14617 return NULL;
14619 add_loc_list (&left, right);
14620 if (left == NULL)
14621 return NULL;
14623 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14624 return left;
14627 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14628 without actually allocating it. */
14630 static unsigned long
14631 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14633 return size_of_int_loc_descriptor (i >> shift)
14634 + size_of_int_loc_descriptor (shift)
14635 + 1;
14638 /* Return size_of_locs (int_loc_descriptor (i)) without
14639 actually allocating it. */
14641 static unsigned long
14642 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14644 unsigned long s;
14646 if (i >= 0)
14648 int clz, ctz;
14649 if (i <= 31)
14650 return 1;
14651 else if (i <= 0xff)
14652 return 2;
14653 else if (i <= 0xffff)
14654 return 3;
14655 clz = clz_hwi (i);
14656 ctz = ctz_hwi (i);
14657 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14658 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14659 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14660 - clz - 5);
14661 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14662 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14663 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14664 - clz - 8);
14665 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14666 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14667 <= 4)
14668 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14669 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14670 return 5;
14671 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14672 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14673 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14674 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14675 - clz - 8);
14676 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14677 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14678 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14679 - clz - 16);
14680 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14681 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14682 && s > 6)
14683 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14684 - clz - 32);
14685 else
14686 return 1 + s;
14688 else
14690 if (i >= -0x80)
14691 return 2;
14692 else if (i >= -0x8000)
14693 return 3;
14694 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14696 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14698 s = size_of_int_loc_descriptor (-i) + 1;
14699 if (s < 5)
14700 return s;
14702 return 5;
14704 else
14706 unsigned long r = 1 + size_of_sleb128 (i);
14707 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14709 s = size_of_int_loc_descriptor (-i) + 1;
14710 if (s < r)
14711 return s;
14713 return r;
14718 /* Return loc description representing "address" of integer value.
14719 This can appear only as toplevel expression. */
14721 static dw_loc_descr_ref
14722 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14724 int litsize;
14725 dw_loc_descr_ref loc_result = NULL;
14727 if (!(dwarf_version >= 4 || !dwarf_strict))
14728 return NULL;
14730 litsize = size_of_int_loc_descriptor (i);
14731 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14732 is more compact. For DW_OP_stack_value we need:
14733 litsize + 1 (DW_OP_stack_value)
14734 and for DW_OP_implicit_value:
14735 1 (DW_OP_implicit_value) + 1 (length) + size. */
14736 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14738 loc_result = int_loc_descriptor (i);
14739 add_loc_descr (&loc_result,
14740 new_loc_descr (DW_OP_stack_value, 0, 0));
14741 return loc_result;
14744 loc_result = new_loc_descr (DW_OP_implicit_value,
14745 size, 0);
14746 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14747 loc_result->dw_loc_oprnd2.v.val_int = i;
14748 return loc_result;
14751 /* Return a location descriptor that designates a base+offset location. */
14753 static dw_loc_descr_ref
14754 based_loc_descr (rtx reg, poly_int64 offset,
14755 enum var_init_status initialized)
14757 unsigned int regno;
14758 dw_loc_descr_ref result;
14759 dw_fde_ref fde = cfun->fde;
14761 /* We only use "frame base" when we're sure we're talking about the
14762 post-prologue local stack frame. We do this by *not* running
14763 register elimination until this point, and recognizing the special
14764 argument pointer and soft frame pointer rtx's. */
14765 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14767 rtx elim = (ira_use_lra_p
14768 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14769 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14771 if (elim != reg)
14773 /* Allow hard frame pointer here even if frame pointer
14774 isn't used since hard frame pointer is encoded with
14775 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14776 not hard frame pointer directly. */
14777 elim = strip_offset_and_add (elim, &offset);
14778 gcc_assert (elim == hard_frame_pointer_rtx
14779 || elim == stack_pointer_rtx);
14781 /* If drap register is used to align stack, use frame
14782 pointer + offset to access stack variables. If stack
14783 is aligned without drap, use stack pointer + offset to
14784 access stack variables. */
14785 if (crtl->stack_realign_tried
14786 && reg == frame_pointer_rtx)
14788 int base_reg
14789 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14790 ? HARD_FRAME_POINTER_REGNUM
14791 : REGNO (elim));
14792 return new_reg_loc_descr (base_reg, offset);
14795 gcc_assert (frame_pointer_fb_offset_valid);
14796 offset += frame_pointer_fb_offset;
14797 HOST_WIDE_INT const_offset;
14798 if (offset.is_constant (&const_offset))
14799 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14800 else
14802 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14803 loc_descr_plus_const (&ret, offset);
14804 return ret;
14809 regno = REGNO (reg);
14810 #ifdef LEAF_REG_REMAP
14811 if (crtl->uses_only_leaf_regs)
14813 int leaf_reg = LEAF_REG_REMAP (regno);
14814 if (leaf_reg != -1)
14815 regno = (unsigned) leaf_reg;
14817 #endif
14818 regno = DWARF_FRAME_REGNUM (regno);
14820 HOST_WIDE_INT const_offset;
14821 if (!optimize && fde
14822 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14823 && offset.is_constant (&const_offset))
14825 /* Use cfa+offset to represent the location of arguments passed
14826 on the stack when drap is used to align stack.
14827 Only do this when not optimizing, for optimized code var-tracking
14828 is supposed to track where the arguments live and the register
14829 used as vdrap or drap in some spot might be used for something
14830 else in other part of the routine. */
14831 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14834 result = new_reg_loc_descr (regno, offset);
14836 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14837 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14839 return result;
14842 /* Return true if this RTL expression describes a base+offset calculation. */
14844 static inline int
14845 is_based_loc (const_rtx rtl)
14847 return (GET_CODE (rtl) == PLUS
14848 && ((REG_P (XEXP (rtl, 0))
14849 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14850 && CONST_INT_P (XEXP (rtl, 1)))));
14853 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14854 failed. */
14856 static dw_loc_descr_ref
14857 tls_mem_loc_descriptor (rtx mem)
14859 tree base;
14860 dw_loc_descr_ref loc_result;
14862 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14863 return NULL;
14865 base = get_base_address (MEM_EXPR (mem));
14866 if (base == NULL
14867 || !VAR_P (base)
14868 || !DECL_THREAD_LOCAL_P (base))
14869 return NULL;
14871 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14872 if (loc_result == NULL)
14873 return NULL;
14875 if (maybe_ne (MEM_OFFSET (mem), 0))
14876 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14878 return loc_result;
14881 /* Output debug info about reason why we failed to expand expression as dwarf
14882 expression. */
14884 static void
14885 expansion_failed (tree expr, rtx rtl, char const *reason)
14887 if (dump_file && (dump_flags & TDF_DETAILS))
14889 fprintf (dump_file, "Failed to expand as dwarf: ");
14890 if (expr)
14891 print_generic_expr (dump_file, expr, dump_flags);
14892 if (rtl)
14894 fprintf (dump_file, "\n");
14895 print_rtl (dump_file, rtl);
14897 fprintf (dump_file, "\nReason: %s\n", reason);
14901 /* Helper function for const_ok_for_output. */
14903 static bool
14904 const_ok_for_output_1 (rtx rtl)
14906 if (targetm.const_not_ok_for_debug_p (rtl))
14908 if (GET_CODE (rtl) != UNSPEC)
14910 expansion_failed (NULL_TREE, rtl,
14911 "Expression rejected for debug by the backend.\n");
14912 return false;
14915 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14916 the target hook doesn't explicitly allow it in debug info, assume
14917 we can't express it in the debug info. */
14918 /* Don't complain about TLS UNSPECs, those are just too hard to
14919 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14920 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14921 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14922 if (flag_checking
14923 && (XVECLEN (rtl, 0) == 0
14924 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14925 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14926 inform (current_function_decl
14927 ? DECL_SOURCE_LOCATION (current_function_decl)
14928 : UNKNOWN_LOCATION,
14929 #if NUM_UNSPEC_VALUES > 0
14930 "non-delegitimized UNSPEC %s (%d) found in variable location",
14931 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14932 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14933 #else
14934 "non-delegitimized UNSPEC %d found in variable location",
14935 #endif
14936 XINT (rtl, 1));
14937 expansion_failed (NULL_TREE, rtl,
14938 "UNSPEC hasn't been delegitimized.\n");
14939 return false;
14942 if (CONST_POLY_INT_P (rtl))
14943 return false;
14945 /* FIXME: Refer to PR60655. It is possible for simplification
14946 of rtl expressions in var tracking to produce such expressions.
14947 We should really identify / validate expressions
14948 enclosed in CONST that can be handled by assemblers on various
14949 targets and only handle legitimate cases here. */
14950 switch (GET_CODE (rtl))
14952 case SYMBOL_REF:
14953 break;
14954 case NOT:
14955 case NEG:
14956 return false;
14957 case PLUS:
14959 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14960 operands. */
14961 subrtx_var_iterator::array_type array;
14962 bool first = false;
14963 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14964 if (SYMBOL_REF_P (*iter)
14965 || LABEL_P (*iter)
14966 || GET_CODE (*iter) == UNSPEC)
14968 first = true;
14969 break;
14971 if (!first)
14972 return true;
14973 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14974 if (SYMBOL_REF_P (*iter)
14975 || LABEL_P (*iter)
14976 || GET_CODE (*iter) == UNSPEC)
14977 return false;
14978 return true;
14980 case MINUS:
14982 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14983 appear in the second operand of MINUS. */
14984 subrtx_var_iterator::array_type array;
14985 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14986 if (SYMBOL_REF_P (*iter)
14987 || LABEL_P (*iter)
14988 || GET_CODE (*iter) == UNSPEC)
14989 return false;
14990 return true;
14992 default:
14993 return true;
14996 if (CONSTANT_POOL_ADDRESS_P (rtl))
14998 bool marked;
14999 get_pool_constant_mark (rtl, &marked);
15000 /* If all references to this pool constant were optimized away,
15001 it was not output and thus we can't represent it. */
15002 if (!marked)
15004 expansion_failed (NULL_TREE, rtl,
15005 "Constant was removed from constant pool.\n");
15006 return false;
15010 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15011 return false;
15013 /* Avoid references to external symbols in debug info, on several targets
15014 the linker might even refuse to link when linking a shared library,
15015 and in many other cases the relocations for .debug_info/.debug_loc are
15016 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15017 to be defined within the same shared library or executable are fine. */
15018 if (SYMBOL_REF_EXTERNAL_P (rtl))
15020 tree decl = SYMBOL_REF_DECL (rtl);
15022 if (decl == NULL || !targetm.binds_local_p (decl))
15024 expansion_failed (NULL_TREE, rtl,
15025 "Symbol not defined in current TU.\n");
15026 return false;
15030 return true;
15033 /* Return true if constant RTL can be emitted in DW_OP_addr or
15034 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15035 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15037 static bool
15038 const_ok_for_output (rtx rtl)
15040 if (GET_CODE (rtl) == SYMBOL_REF)
15041 return const_ok_for_output_1 (rtl);
15043 if (GET_CODE (rtl) == CONST)
15045 subrtx_var_iterator::array_type array;
15046 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15047 if (!const_ok_for_output_1 (*iter))
15048 return false;
15049 return true;
15052 return true;
15055 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15056 if possible, NULL otherwise. */
15058 static dw_die_ref
15059 base_type_for_mode (machine_mode mode, bool unsignedp)
15061 dw_die_ref type_die;
15062 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15064 if (type == NULL)
15065 return NULL;
15066 switch (TREE_CODE (type))
15068 case INTEGER_TYPE:
15069 case REAL_TYPE:
15070 break;
15071 default:
15072 return NULL;
15074 type_die = lookup_type_die (type);
15075 if (!type_die)
15076 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15077 comp_unit_die ());
15078 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15079 return NULL;
15080 return type_die;
15083 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15084 type matching MODE, or, if MODE is narrower than or as wide as
15085 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15086 possible. */
15088 static dw_loc_descr_ref
15089 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15091 machine_mode outer_mode = mode;
15092 dw_die_ref type_die;
15093 dw_loc_descr_ref cvt;
15095 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15097 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15098 return op;
15100 type_die = base_type_for_mode (outer_mode, 1);
15101 if (type_die == NULL)
15102 return NULL;
15103 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15104 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15105 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15106 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15107 add_loc_descr (&op, cvt);
15108 return op;
15111 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15113 static dw_loc_descr_ref
15114 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15115 dw_loc_descr_ref op1)
15117 dw_loc_descr_ref ret = op0;
15118 add_loc_descr (&ret, op1);
15119 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15120 if (STORE_FLAG_VALUE != 1)
15122 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15123 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15125 return ret;
15128 /* Subroutine of scompare_loc_descriptor for the case in which we're
15129 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15130 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15132 static dw_loc_descr_ref
15133 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15134 scalar_int_mode op_mode,
15135 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15137 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15138 dw_loc_descr_ref cvt;
15140 if (type_die == NULL)
15141 return NULL;
15142 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15143 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15144 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15145 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15146 add_loc_descr (&op0, cvt);
15147 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15148 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15149 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15150 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15151 add_loc_descr (&op1, cvt);
15152 return compare_loc_descriptor (op, op0, op1);
15155 /* Subroutine of scompare_loc_descriptor for the case in which we're
15156 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15157 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15159 static dw_loc_descr_ref
15160 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15161 scalar_int_mode op_mode,
15162 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15164 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15165 /* For eq/ne, if the operands are known to be zero-extended,
15166 there is no need to do the fancy shifting up. */
15167 if (op == DW_OP_eq || op == DW_OP_ne)
15169 dw_loc_descr_ref last0, last1;
15170 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15172 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15174 /* deref_size zero extends, and for constants we can check
15175 whether they are zero extended or not. */
15176 if (((last0->dw_loc_opc == DW_OP_deref_size
15177 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15178 || (CONST_INT_P (XEXP (rtl, 0))
15179 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15180 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15181 && ((last1->dw_loc_opc == DW_OP_deref_size
15182 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15183 || (CONST_INT_P (XEXP (rtl, 1))
15184 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15185 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15186 return compare_loc_descriptor (op, op0, op1);
15188 /* EQ/NE comparison against constant in narrower type than
15189 DWARF2_ADDR_SIZE can be performed either as
15190 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15191 DW_OP_{eq,ne}
15193 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15194 DW_OP_{eq,ne}. Pick whatever is shorter. */
15195 if (CONST_INT_P (XEXP (rtl, 1))
15196 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15197 && (size_of_int_loc_descriptor (shift) + 1
15198 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15199 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15200 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15201 & GET_MODE_MASK (op_mode))))
15203 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15204 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15205 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15206 & GET_MODE_MASK (op_mode));
15207 return compare_loc_descriptor (op, op0, op1);
15210 add_loc_descr (&op0, int_loc_descriptor (shift));
15211 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15212 if (CONST_INT_P (XEXP (rtl, 1)))
15213 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15214 else
15216 add_loc_descr (&op1, int_loc_descriptor (shift));
15217 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15219 return compare_loc_descriptor (op, op0, op1);
15222 /* Return location descriptor for signed comparison OP RTL. */
15224 static dw_loc_descr_ref
15225 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15226 machine_mode mem_mode)
15228 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15229 dw_loc_descr_ref op0, op1;
15231 if (op_mode == VOIDmode)
15232 op_mode = GET_MODE (XEXP (rtl, 1));
15233 if (op_mode == VOIDmode)
15234 return NULL;
15236 scalar_int_mode int_op_mode;
15237 if (dwarf_strict
15238 && dwarf_version < 5
15239 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15240 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15241 return NULL;
15243 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15244 VAR_INIT_STATUS_INITIALIZED);
15245 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15246 VAR_INIT_STATUS_INITIALIZED);
15248 if (op0 == NULL || op1 == NULL)
15249 return NULL;
15251 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15253 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15254 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15256 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15257 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15259 return compare_loc_descriptor (op, op0, op1);
15262 /* Return location descriptor for unsigned comparison OP RTL. */
15264 static dw_loc_descr_ref
15265 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15266 machine_mode mem_mode)
15268 dw_loc_descr_ref op0, op1;
15270 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15271 if (test_op_mode == VOIDmode)
15272 test_op_mode = GET_MODE (XEXP (rtl, 1));
15274 scalar_int_mode op_mode;
15275 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15276 return NULL;
15278 if (dwarf_strict
15279 && dwarf_version < 5
15280 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15281 return NULL;
15283 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15284 VAR_INIT_STATUS_INITIALIZED);
15285 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15286 VAR_INIT_STATUS_INITIALIZED);
15288 if (op0 == NULL || op1 == NULL)
15289 return NULL;
15291 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15293 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15294 dw_loc_descr_ref last0, last1;
15295 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15297 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15299 if (CONST_INT_P (XEXP (rtl, 0)))
15300 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15301 /* deref_size zero extends, so no need to mask it again. */
15302 else if (last0->dw_loc_opc != DW_OP_deref_size
15303 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15305 add_loc_descr (&op0, int_loc_descriptor (mask));
15306 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15308 if (CONST_INT_P (XEXP (rtl, 1)))
15309 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15310 /* deref_size zero extends, so no need to mask it again. */
15311 else if (last1->dw_loc_opc != DW_OP_deref_size
15312 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15314 add_loc_descr (&op1, int_loc_descriptor (mask));
15315 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15318 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15320 HOST_WIDE_INT bias = 1;
15321 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15322 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15323 if (CONST_INT_P (XEXP (rtl, 1)))
15324 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15325 + INTVAL (XEXP (rtl, 1)));
15326 else
15327 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15328 bias, 0));
15330 return compare_loc_descriptor (op, op0, op1);
15333 /* Return location descriptor for {U,S}{MIN,MAX}. */
15335 static dw_loc_descr_ref
15336 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15337 machine_mode mem_mode)
15339 enum dwarf_location_atom op;
15340 dw_loc_descr_ref op0, op1, ret;
15341 dw_loc_descr_ref bra_node, drop_node;
15343 scalar_int_mode int_mode;
15344 if (dwarf_strict
15345 && dwarf_version < 5
15346 && (!is_a <scalar_int_mode> (mode, &int_mode)
15347 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15348 return NULL;
15350 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15351 VAR_INIT_STATUS_INITIALIZED);
15352 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15353 VAR_INIT_STATUS_INITIALIZED);
15355 if (op0 == NULL || op1 == NULL)
15356 return NULL;
15358 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15359 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15360 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15361 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15363 /* Checked by the caller. */
15364 int_mode = as_a <scalar_int_mode> (mode);
15365 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15367 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15368 add_loc_descr (&op0, int_loc_descriptor (mask));
15369 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15370 add_loc_descr (&op1, int_loc_descriptor (mask));
15371 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15373 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15375 HOST_WIDE_INT bias = 1;
15376 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15377 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15378 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15381 else if (is_a <scalar_int_mode> (mode, &int_mode)
15382 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15384 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15385 add_loc_descr (&op0, int_loc_descriptor (shift));
15386 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15387 add_loc_descr (&op1, int_loc_descriptor (shift));
15388 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15390 else if (is_a <scalar_int_mode> (mode, &int_mode)
15391 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15393 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15394 dw_loc_descr_ref cvt;
15395 if (type_die == NULL)
15396 return NULL;
15397 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15398 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15399 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15400 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15401 add_loc_descr (&op0, cvt);
15402 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15403 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15404 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15405 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15406 add_loc_descr (&op1, cvt);
15409 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15410 op = DW_OP_lt;
15411 else
15412 op = DW_OP_gt;
15413 ret = op0;
15414 add_loc_descr (&ret, op1);
15415 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15416 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15417 add_loc_descr (&ret, bra_node);
15418 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15419 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15420 add_loc_descr (&ret, drop_node);
15421 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15422 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15423 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15424 && is_a <scalar_int_mode> (mode, &int_mode)
15425 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15426 ret = convert_descriptor_to_mode (int_mode, ret);
15427 return ret;
15430 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15431 but after converting arguments to type_die, afterwards
15432 convert back to unsigned. */
15434 static dw_loc_descr_ref
15435 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15436 scalar_int_mode mode, machine_mode mem_mode)
15438 dw_loc_descr_ref cvt, op0, op1;
15440 if (type_die == NULL)
15441 return NULL;
15442 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15443 VAR_INIT_STATUS_INITIALIZED);
15444 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15445 VAR_INIT_STATUS_INITIALIZED);
15446 if (op0 == NULL || op1 == NULL)
15447 return NULL;
15448 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15449 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15450 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15451 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15452 add_loc_descr (&op0, cvt);
15453 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15454 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15455 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15456 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15457 add_loc_descr (&op1, cvt);
15458 add_loc_descr (&op0, op1);
15459 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15460 return convert_descriptor_to_mode (mode, op0);
15463 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15464 const0 is DW_OP_lit0 or corresponding typed constant,
15465 const1 is DW_OP_lit1 or corresponding typed constant
15466 and constMSB is constant with just the MSB bit set
15467 for the mode):
15468 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15469 L1: const0 DW_OP_swap
15470 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15471 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15472 L3: DW_OP_drop
15473 L4: DW_OP_nop
15475 CTZ is similar:
15476 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15477 L1: const0 DW_OP_swap
15478 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15479 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15480 L3: DW_OP_drop
15481 L4: DW_OP_nop
15483 FFS is similar:
15484 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15485 L1: const1 DW_OP_swap
15486 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15487 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15488 L3: DW_OP_drop
15489 L4: DW_OP_nop */
15491 static dw_loc_descr_ref
15492 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15493 machine_mode mem_mode)
15495 dw_loc_descr_ref op0, ret, tmp;
15496 HOST_WIDE_INT valv;
15497 dw_loc_descr_ref l1jump, l1label;
15498 dw_loc_descr_ref l2jump, l2label;
15499 dw_loc_descr_ref l3jump, l3label;
15500 dw_loc_descr_ref l4jump, l4label;
15501 rtx msb;
15503 if (GET_MODE (XEXP (rtl, 0)) != mode)
15504 return NULL;
15506 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15507 VAR_INIT_STATUS_INITIALIZED);
15508 if (op0 == NULL)
15509 return NULL;
15510 ret = op0;
15511 if (GET_CODE (rtl) == CLZ)
15513 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15514 valv = GET_MODE_BITSIZE (mode);
15516 else if (GET_CODE (rtl) == FFS)
15517 valv = 0;
15518 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15519 valv = GET_MODE_BITSIZE (mode);
15520 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15521 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15522 add_loc_descr (&ret, l1jump);
15523 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15524 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15525 VAR_INIT_STATUS_INITIALIZED);
15526 if (tmp == NULL)
15527 return NULL;
15528 add_loc_descr (&ret, tmp);
15529 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15530 add_loc_descr (&ret, l4jump);
15531 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15532 ? const1_rtx : const0_rtx,
15533 mode, mem_mode,
15534 VAR_INIT_STATUS_INITIALIZED);
15535 if (l1label == NULL)
15536 return NULL;
15537 add_loc_descr (&ret, l1label);
15538 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15539 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15540 add_loc_descr (&ret, l2label);
15541 if (GET_CODE (rtl) != CLZ)
15542 msb = const1_rtx;
15543 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15544 msb = GEN_INT (HOST_WIDE_INT_1U
15545 << (GET_MODE_BITSIZE (mode) - 1));
15546 else
15547 msb = immed_wide_int_const
15548 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15549 GET_MODE_PRECISION (mode)), mode);
15550 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15551 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15552 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15553 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15554 else
15555 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15556 VAR_INIT_STATUS_INITIALIZED);
15557 if (tmp == NULL)
15558 return NULL;
15559 add_loc_descr (&ret, tmp);
15560 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15561 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15562 add_loc_descr (&ret, l3jump);
15563 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15564 VAR_INIT_STATUS_INITIALIZED);
15565 if (tmp == NULL)
15566 return NULL;
15567 add_loc_descr (&ret, tmp);
15568 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15569 ? DW_OP_shl : DW_OP_shr, 0, 0));
15570 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15571 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15572 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15573 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15574 add_loc_descr (&ret, l2jump);
15575 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15576 add_loc_descr (&ret, l3label);
15577 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15578 add_loc_descr (&ret, l4label);
15579 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15580 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15581 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15582 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15583 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15584 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15585 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15586 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15587 return ret;
15590 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15591 const1 is DW_OP_lit1 or corresponding typed constant):
15592 const0 DW_OP_swap
15593 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15594 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15595 L2: DW_OP_drop
15597 PARITY is similar:
15598 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15599 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15600 L2: DW_OP_drop */
15602 static dw_loc_descr_ref
15603 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15604 machine_mode mem_mode)
15606 dw_loc_descr_ref op0, ret, tmp;
15607 dw_loc_descr_ref l1jump, l1label;
15608 dw_loc_descr_ref l2jump, l2label;
15610 if (GET_MODE (XEXP (rtl, 0)) != mode)
15611 return NULL;
15613 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15614 VAR_INIT_STATUS_INITIALIZED);
15615 if (op0 == NULL)
15616 return NULL;
15617 ret = op0;
15618 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15619 VAR_INIT_STATUS_INITIALIZED);
15620 if (tmp == NULL)
15621 return NULL;
15622 add_loc_descr (&ret, tmp);
15623 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15624 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15625 add_loc_descr (&ret, l1label);
15626 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15627 add_loc_descr (&ret, l2jump);
15628 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15629 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15630 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15631 VAR_INIT_STATUS_INITIALIZED);
15632 if (tmp == NULL)
15633 return NULL;
15634 add_loc_descr (&ret, tmp);
15635 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15636 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15637 ? DW_OP_plus : DW_OP_xor, 0, 0));
15638 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15639 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15640 VAR_INIT_STATUS_INITIALIZED);
15641 add_loc_descr (&ret, tmp);
15642 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15643 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15644 add_loc_descr (&ret, l1jump);
15645 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15646 add_loc_descr (&ret, l2label);
15647 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15648 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15649 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15650 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15651 return ret;
15654 /* BSWAP (constS is initial shift count, either 56 or 24):
15655 constS const0
15656 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15657 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15658 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15659 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15660 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15662 static dw_loc_descr_ref
15663 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15664 machine_mode mem_mode)
15666 dw_loc_descr_ref op0, ret, tmp;
15667 dw_loc_descr_ref l1jump, l1label;
15668 dw_loc_descr_ref l2jump, l2label;
15670 if (BITS_PER_UNIT != 8
15671 || (GET_MODE_BITSIZE (mode) != 32
15672 && GET_MODE_BITSIZE (mode) != 64))
15673 return NULL;
15675 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15676 VAR_INIT_STATUS_INITIALIZED);
15677 if (op0 == NULL)
15678 return NULL;
15680 ret = op0;
15681 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15682 mode, mem_mode,
15683 VAR_INIT_STATUS_INITIALIZED);
15684 if (tmp == NULL)
15685 return NULL;
15686 add_loc_descr (&ret, tmp);
15687 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15688 VAR_INIT_STATUS_INITIALIZED);
15689 if (tmp == NULL)
15690 return NULL;
15691 add_loc_descr (&ret, tmp);
15692 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15693 add_loc_descr (&ret, l1label);
15694 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15695 mode, mem_mode,
15696 VAR_INIT_STATUS_INITIALIZED);
15697 add_loc_descr (&ret, tmp);
15698 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15699 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15700 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15701 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15702 VAR_INIT_STATUS_INITIALIZED);
15703 if (tmp == NULL)
15704 return NULL;
15705 add_loc_descr (&ret, tmp);
15706 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15707 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15708 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15709 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15710 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15711 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15712 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15713 VAR_INIT_STATUS_INITIALIZED);
15714 add_loc_descr (&ret, tmp);
15715 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15716 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15717 add_loc_descr (&ret, l2jump);
15718 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15719 VAR_INIT_STATUS_INITIALIZED);
15720 add_loc_descr (&ret, tmp);
15721 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15722 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15723 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15724 add_loc_descr (&ret, l1jump);
15725 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15726 add_loc_descr (&ret, l2label);
15727 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15728 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15729 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15730 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15731 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15732 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15733 return ret;
15736 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15737 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15738 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15739 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15741 ROTATERT is similar:
15742 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15743 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15744 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15746 static dw_loc_descr_ref
15747 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15748 machine_mode mem_mode)
15750 rtx rtlop1 = XEXP (rtl, 1);
15751 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15752 int i;
15754 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15755 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15756 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15757 VAR_INIT_STATUS_INITIALIZED);
15758 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15759 VAR_INIT_STATUS_INITIALIZED);
15760 if (op0 == NULL || op1 == NULL)
15761 return NULL;
15762 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15763 for (i = 0; i < 2; i++)
15765 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15766 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15767 mode, mem_mode,
15768 VAR_INIT_STATUS_INITIALIZED);
15769 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15770 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15771 ? DW_OP_const4u
15772 : HOST_BITS_PER_WIDE_INT == 64
15773 ? DW_OP_const8u : DW_OP_constu,
15774 GET_MODE_MASK (mode), 0);
15775 else
15776 mask[i] = NULL;
15777 if (mask[i] == NULL)
15778 return NULL;
15779 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15781 ret = op0;
15782 add_loc_descr (&ret, op1);
15783 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15784 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15785 if (GET_CODE (rtl) == ROTATERT)
15787 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15788 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15789 GET_MODE_BITSIZE (mode), 0));
15791 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15792 if (mask[0] != NULL)
15793 add_loc_descr (&ret, mask[0]);
15794 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15795 if (mask[1] != NULL)
15797 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15798 add_loc_descr (&ret, mask[1]);
15799 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15801 if (GET_CODE (rtl) == ROTATE)
15803 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15804 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15805 GET_MODE_BITSIZE (mode), 0));
15807 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15808 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15809 return ret;
15812 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15813 for DEBUG_PARAMETER_REF RTL. */
15815 static dw_loc_descr_ref
15816 parameter_ref_descriptor (rtx rtl)
15818 dw_loc_descr_ref ret;
15819 dw_die_ref ref;
15821 if (dwarf_strict)
15822 return NULL;
15823 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15824 /* With LTO during LTRANS we get the late DIE that refers to the early
15825 DIE, thus we add another indirection here. This seems to confuse
15826 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15827 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15828 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15829 if (ref)
15831 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15832 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15833 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15835 else
15837 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15838 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15840 return ret;
15843 /* The following routine converts the RTL for a variable or parameter
15844 (resident in memory) into an equivalent Dwarf representation of a
15845 mechanism for getting the address of that same variable onto the top of a
15846 hypothetical "address evaluation" stack.
15848 When creating memory location descriptors, we are effectively transforming
15849 the RTL for a memory-resident object into its Dwarf postfix expression
15850 equivalent. This routine recursively descends an RTL tree, turning
15851 it into Dwarf postfix code as it goes.
15853 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15855 MEM_MODE is the mode of the memory reference, needed to handle some
15856 autoincrement addressing modes.
15858 Return 0 if we can't represent the location. */
15860 dw_loc_descr_ref
15861 mem_loc_descriptor (rtx rtl, machine_mode mode,
15862 machine_mode mem_mode,
15863 enum var_init_status initialized)
15865 dw_loc_descr_ref mem_loc_result = NULL;
15866 enum dwarf_location_atom op;
15867 dw_loc_descr_ref op0, op1;
15868 rtx inner = NULL_RTX;
15869 poly_int64 offset;
15871 if (mode == VOIDmode)
15872 mode = GET_MODE (rtl);
15874 /* Note that for a dynamically sized array, the location we will generate a
15875 description of here will be the lowest numbered location which is
15876 actually within the array. That's *not* necessarily the same as the
15877 zeroth element of the array. */
15879 rtl = targetm.delegitimize_address (rtl);
15881 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15882 return NULL;
15884 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15885 switch (GET_CODE (rtl))
15887 case POST_INC:
15888 case POST_DEC:
15889 case POST_MODIFY:
15890 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15892 case SUBREG:
15893 /* The case of a subreg may arise when we have a local (register)
15894 variable or a formal (register) parameter which doesn't quite fill
15895 up an entire register. For now, just assume that it is
15896 legitimate to make the Dwarf info refer to the whole register which
15897 contains the given subreg. */
15898 if (!subreg_lowpart_p (rtl))
15899 break;
15900 inner = SUBREG_REG (rtl);
15901 /* FALLTHRU */
15902 case TRUNCATE:
15903 if (inner == NULL_RTX)
15904 inner = XEXP (rtl, 0);
15905 if (is_a <scalar_int_mode> (mode, &int_mode)
15906 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15907 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15908 #ifdef POINTERS_EXTEND_UNSIGNED
15909 || (int_mode == Pmode && mem_mode != VOIDmode)
15910 #endif
15912 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15914 mem_loc_result = mem_loc_descriptor (inner,
15915 inner_mode,
15916 mem_mode, initialized);
15917 break;
15919 if (dwarf_strict && dwarf_version < 5)
15920 break;
15921 if (is_a <scalar_int_mode> (mode, &int_mode)
15922 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15923 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15924 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15926 dw_die_ref type_die;
15927 dw_loc_descr_ref cvt;
15929 mem_loc_result = mem_loc_descriptor (inner,
15930 GET_MODE (inner),
15931 mem_mode, initialized);
15932 if (mem_loc_result == NULL)
15933 break;
15934 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15935 if (type_die == NULL)
15937 mem_loc_result = NULL;
15938 break;
15940 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15941 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15942 else
15943 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15944 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15945 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15946 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15947 add_loc_descr (&mem_loc_result, cvt);
15948 if (is_a <scalar_int_mode> (mode, &int_mode)
15949 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15951 /* Convert it to untyped afterwards. */
15952 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15953 add_loc_descr (&mem_loc_result, cvt);
15956 break;
15958 case REG:
15959 if (!is_a <scalar_int_mode> (mode, &int_mode)
15960 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15961 && rtl != arg_pointer_rtx
15962 && rtl != frame_pointer_rtx
15963 #ifdef POINTERS_EXTEND_UNSIGNED
15964 && (int_mode != Pmode || mem_mode == VOIDmode)
15965 #endif
15968 dw_die_ref type_die;
15969 unsigned int dbx_regnum;
15971 if (dwarf_strict && dwarf_version < 5)
15972 break;
15973 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15974 break;
15975 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15976 if (type_die == NULL)
15977 break;
15979 dbx_regnum = dbx_reg_number (rtl);
15980 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15981 break;
15982 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15983 dbx_regnum, 0);
15984 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15985 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15986 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15987 break;
15989 /* Whenever a register number forms a part of the description of the
15990 method for calculating the (dynamic) address of a memory resident
15991 object, DWARF rules require the register number be referred to as
15992 a "base register". This distinction is not based in any way upon
15993 what category of register the hardware believes the given register
15994 belongs to. This is strictly DWARF terminology we're dealing with
15995 here. Note that in cases where the location of a memory-resident
15996 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15997 OP_CONST (0)) the actual DWARF location descriptor that we generate
15998 may just be OP_BASEREG (basereg). This may look deceptively like
15999 the object in question was allocated to a register (rather than in
16000 memory) so DWARF consumers need to be aware of the subtle
16001 distinction between OP_REG and OP_BASEREG. */
16002 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16003 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16004 else if (stack_realign_drap
16005 && crtl->drap_reg
16006 && crtl->args.internal_arg_pointer == rtl
16007 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16009 /* If RTL is internal_arg_pointer, which has been optimized
16010 out, use DRAP instead. */
16011 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16012 VAR_INIT_STATUS_INITIALIZED);
16014 break;
16016 case SIGN_EXTEND:
16017 case ZERO_EXTEND:
16018 if (!is_a <scalar_int_mode> (mode, &int_mode)
16019 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16020 break;
16021 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16022 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16023 if (op0 == 0)
16024 break;
16025 else if (GET_CODE (rtl) == ZERO_EXTEND
16026 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16027 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16028 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16029 to expand zero extend as two shifts instead of
16030 masking. */
16031 && GET_MODE_SIZE (inner_mode) <= 4)
16033 mem_loc_result = op0;
16034 add_loc_descr (&mem_loc_result,
16035 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16036 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16038 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16040 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16041 shift *= BITS_PER_UNIT;
16042 if (GET_CODE (rtl) == SIGN_EXTEND)
16043 op = DW_OP_shra;
16044 else
16045 op = DW_OP_shr;
16046 mem_loc_result = op0;
16047 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16048 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16049 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16050 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16052 else if (!dwarf_strict || dwarf_version >= 5)
16054 dw_die_ref type_die1, type_die2;
16055 dw_loc_descr_ref cvt;
16057 type_die1 = base_type_for_mode (inner_mode,
16058 GET_CODE (rtl) == ZERO_EXTEND);
16059 if (type_die1 == NULL)
16060 break;
16061 type_die2 = base_type_for_mode (int_mode, 1);
16062 if (type_die2 == NULL)
16063 break;
16064 mem_loc_result = op0;
16065 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16066 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16067 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16068 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16069 add_loc_descr (&mem_loc_result, cvt);
16070 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16071 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16072 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16073 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16074 add_loc_descr (&mem_loc_result, cvt);
16076 break;
16078 case MEM:
16080 rtx new_rtl = avoid_constant_pool_reference (rtl);
16081 if (new_rtl != rtl)
16083 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16084 initialized);
16085 if (mem_loc_result != NULL)
16086 return mem_loc_result;
16089 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16090 get_address_mode (rtl), mode,
16091 VAR_INIT_STATUS_INITIALIZED);
16092 if (mem_loc_result == NULL)
16093 mem_loc_result = tls_mem_loc_descriptor (rtl);
16094 if (mem_loc_result != NULL)
16096 if (!is_a <scalar_int_mode> (mode, &int_mode)
16097 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16099 dw_die_ref type_die;
16100 dw_loc_descr_ref deref;
16101 HOST_WIDE_INT size;
16103 if (dwarf_strict && dwarf_version < 5)
16104 return NULL;
16105 if (!GET_MODE_SIZE (mode).is_constant (&size))
16106 return NULL;
16107 type_die
16108 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16109 if (type_die == NULL)
16110 return NULL;
16111 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16112 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16113 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16114 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16115 add_loc_descr (&mem_loc_result, deref);
16117 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16118 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16119 else
16120 add_loc_descr (&mem_loc_result,
16121 new_loc_descr (DW_OP_deref_size,
16122 GET_MODE_SIZE (int_mode), 0));
16124 break;
16126 case LO_SUM:
16127 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16129 case LABEL_REF:
16130 /* Some ports can transform a symbol ref into a label ref, because
16131 the symbol ref is too far away and has to be dumped into a constant
16132 pool. */
16133 case CONST:
16134 case SYMBOL_REF:
16135 case UNSPEC:
16136 if (!is_a <scalar_int_mode> (mode, &int_mode)
16137 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16138 #ifdef POINTERS_EXTEND_UNSIGNED
16139 && (int_mode != Pmode || mem_mode == VOIDmode)
16140 #endif
16142 break;
16144 if (GET_CODE (rtl) == UNSPEC)
16146 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16147 can't express it in the debug info. This can happen e.g. with some
16148 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16149 approves. */
16150 bool not_ok = false;
16151 subrtx_var_iterator::array_type array;
16152 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16153 if (*iter != rtl && !CONSTANT_P (*iter))
16155 not_ok = true;
16156 break;
16159 if (not_ok)
16160 break;
16162 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16163 if (!const_ok_for_output_1 (*iter))
16165 not_ok = true;
16166 break;
16169 if (not_ok)
16170 break;
16172 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16173 goto symref;
16176 if (GET_CODE (rtl) == SYMBOL_REF
16177 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16179 dw_loc_descr_ref temp;
16181 /* If this is not defined, we have no way to emit the data. */
16182 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16183 break;
16185 temp = new_addr_loc_descr (rtl, dtprel_true);
16187 /* We check for DWARF 5 here because gdb did not implement
16188 DW_OP_form_tls_address until after 7.12. */
16189 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16190 ? DW_OP_form_tls_address
16191 : DW_OP_GNU_push_tls_address),
16192 0, 0);
16193 add_loc_descr (&mem_loc_result, temp);
16195 break;
16198 if (!const_ok_for_output (rtl))
16200 if (GET_CODE (rtl) == CONST)
16201 switch (GET_CODE (XEXP (rtl, 0)))
16203 case NOT:
16204 op = DW_OP_not;
16205 goto try_const_unop;
16206 case NEG:
16207 op = DW_OP_neg;
16208 goto try_const_unop;
16209 try_const_unop:
16210 rtx arg;
16211 arg = XEXP (XEXP (rtl, 0), 0);
16212 if (!CONSTANT_P (arg))
16213 arg = gen_rtx_CONST (int_mode, arg);
16214 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16215 initialized);
16216 if (op0)
16218 mem_loc_result = op0;
16219 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16221 break;
16222 default:
16223 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16224 mem_mode, initialized);
16225 break;
16227 break;
16230 symref:
16231 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16232 vec_safe_push (used_rtx_array, rtl);
16233 break;
16235 case CONCAT:
16236 case CONCATN:
16237 case VAR_LOCATION:
16238 case DEBUG_IMPLICIT_PTR:
16239 expansion_failed (NULL_TREE, rtl,
16240 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16241 return 0;
16243 case ENTRY_VALUE:
16244 if (dwarf_strict && dwarf_version < 5)
16245 return NULL;
16246 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16248 if (!is_a <scalar_int_mode> (mode, &int_mode)
16249 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16250 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16251 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16252 else
16254 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
16255 if (dbx_regnum == IGNORED_DWARF_REGNUM)
16256 return NULL;
16257 op0 = one_reg_loc_descriptor (dbx_regnum,
16258 VAR_INIT_STATUS_INITIALIZED);
16261 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16262 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16264 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16265 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16266 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16267 return NULL;
16269 else
16270 gcc_unreachable ();
16271 if (op0 == NULL)
16272 return NULL;
16273 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16274 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16275 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16276 break;
16278 case DEBUG_PARAMETER_REF:
16279 mem_loc_result = parameter_ref_descriptor (rtl);
16280 break;
16282 case PRE_MODIFY:
16283 /* Extract the PLUS expression nested inside and fall into
16284 PLUS code below. */
16285 rtl = XEXP (rtl, 1);
16286 goto plus;
16288 case PRE_INC:
16289 case PRE_DEC:
16290 /* Turn these into a PLUS expression and fall into the PLUS code
16291 below. */
16292 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16293 gen_int_mode (GET_CODE (rtl) == PRE_INC
16294 ? GET_MODE_UNIT_SIZE (mem_mode)
16295 : -GET_MODE_UNIT_SIZE (mem_mode),
16296 mode));
16298 /* fall through */
16300 case PLUS:
16301 plus:
16302 if (is_based_loc (rtl)
16303 && is_a <scalar_int_mode> (mode, &int_mode)
16304 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16305 || XEXP (rtl, 0) == arg_pointer_rtx
16306 || XEXP (rtl, 0) == frame_pointer_rtx))
16307 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16308 INTVAL (XEXP (rtl, 1)),
16309 VAR_INIT_STATUS_INITIALIZED);
16310 else
16312 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16313 VAR_INIT_STATUS_INITIALIZED);
16314 if (mem_loc_result == 0)
16315 break;
16317 if (CONST_INT_P (XEXP (rtl, 1))
16318 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16319 <= DWARF2_ADDR_SIZE))
16320 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16321 else
16323 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16324 VAR_INIT_STATUS_INITIALIZED);
16325 if (op1 == 0)
16326 return NULL;
16327 add_loc_descr (&mem_loc_result, op1);
16328 add_loc_descr (&mem_loc_result,
16329 new_loc_descr (DW_OP_plus, 0, 0));
16332 break;
16334 /* If a pseudo-reg is optimized away, it is possible for it to
16335 be replaced with a MEM containing a multiply or shift. */
16336 case MINUS:
16337 op = DW_OP_minus;
16338 goto do_binop;
16340 case MULT:
16341 op = DW_OP_mul;
16342 goto do_binop;
16344 case DIV:
16345 if ((!dwarf_strict || dwarf_version >= 5)
16346 && is_a <scalar_int_mode> (mode, &int_mode)
16347 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16349 mem_loc_result = typed_binop (DW_OP_div, rtl,
16350 base_type_for_mode (mode, 0),
16351 int_mode, mem_mode);
16352 break;
16354 op = DW_OP_div;
16355 goto do_binop;
16357 case UMOD:
16358 op = DW_OP_mod;
16359 goto do_binop;
16361 case ASHIFT:
16362 op = DW_OP_shl;
16363 goto do_shift;
16365 case ASHIFTRT:
16366 op = DW_OP_shra;
16367 goto do_shift;
16369 case LSHIFTRT:
16370 op = DW_OP_shr;
16371 goto do_shift;
16373 do_shift:
16374 if (!is_a <scalar_int_mode> (mode, &int_mode))
16375 break;
16376 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16377 VAR_INIT_STATUS_INITIALIZED);
16379 rtx rtlop1 = XEXP (rtl, 1);
16380 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16381 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16382 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16383 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16384 VAR_INIT_STATUS_INITIALIZED);
16387 if (op0 == 0 || op1 == 0)
16388 break;
16390 mem_loc_result = op0;
16391 add_loc_descr (&mem_loc_result, op1);
16392 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16393 break;
16395 case AND:
16396 op = DW_OP_and;
16397 goto do_binop;
16399 case IOR:
16400 op = DW_OP_or;
16401 goto do_binop;
16403 case XOR:
16404 op = DW_OP_xor;
16405 goto do_binop;
16407 do_binop:
16408 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16409 VAR_INIT_STATUS_INITIALIZED);
16410 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16412 if (op0 == 0)
16413 break;
16414 mem_loc_result = op0;
16415 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16416 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16417 break;
16419 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16420 VAR_INIT_STATUS_INITIALIZED);
16422 if (op0 == 0 || op1 == 0)
16423 break;
16425 mem_loc_result = op0;
16426 add_loc_descr (&mem_loc_result, op1);
16427 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16428 break;
16430 case MOD:
16431 if ((!dwarf_strict || dwarf_version >= 5)
16432 && is_a <scalar_int_mode> (mode, &int_mode)
16433 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16435 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16436 base_type_for_mode (mode, 0),
16437 int_mode, mem_mode);
16438 break;
16441 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16442 VAR_INIT_STATUS_INITIALIZED);
16443 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16444 VAR_INIT_STATUS_INITIALIZED);
16446 if (op0 == 0 || op1 == 0)
16447 break;
16449 mem_loc_result = op0;
16450 add_loc_descr (&mem_loc_result, op1);
16451 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16452 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16453 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16454 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16455 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16456 break;
16458 case UDIV:
16459 if ((!dwarf_strict || dwarf_version >= 5)
16460 && is_a <scalar_int_mode> (mode, &int_mode))
16462 /* We can use a signed divide if the sign bit is not set. */
16463 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16465 op = DW_OP_div;
16466 goto do_binop;
16469 mem_loc_result = typed_binop (DW_OP_div, rtl,
16470 base_type_for_mode (int_mode, 1),
16471 int_mode, mem_mode);
16473 break;
16475 case NOT:
16476 op = DW_OP_not;
16477 goto do_unop;
16479 case ABS:
16480 op = DW_OP_abs;
16481 goto do_unop;
16483 case NEG:
16484 op = DW_OP_neg;
16485 goto do_unop;
16487 do_unop:
16488 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16489 VAR_INIT_STATUS_INITIALIZED);
16491 if (op0 == 0)
16492 break;
16494 mem_loc_result = op0;
16495 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16496 break;
16498 case CONST_INT:
16499 if (!is_a <scalar_int_mode> (mode, &int_mode)
16500 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16501 #ifdef POINTERS_EXTEND_UNSIGNED
16502 || (int_mode == Pmode
16503 && mem_mode != VOIDmode
16504 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16505 #endif
16508 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16509 break;
16511 if ((!dwarf_strict || dwarf_version >= 5)
16512 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16513 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16515 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16516 scalar_int_mode amode;
16517 if (type_die == NULL)
16518 return NULL;
16519 if (INTVAL (rtl) >= 0
16520 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16521 .exists (&amode))
16522 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16523 /* const DW_OP_convert <XXX> vs.
16524 DW_OP_const_type <XXX, 1, const>. */
16525 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16526 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16528 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16529 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16530 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16531 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16532 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16533 add_loc_descr (&mem_loc_result, op0);
16534 return mem_loc_result;
16536 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16537 INTVAL (rtl));
16538 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16539 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16540 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16541 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16542 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16543 else
16545 mem_loc_result->dw_loc_oprnd2.val_class
16546 = dw_val_class_const_double;
16547 mem_loc_result->dw_loc_oprnd2.v.val_double
16548 = double_int::from_shwi (INTVAL (rtl));
16551 break;
16553 case CONST_DOUBLE:
16554 if (!dwarf_strict || dwarf_version >= 5)
16556 dw_die_ref type_die;
16558 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16559 CONST_DOUBLE rtx could represent either a large integer
16560 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16561 the value is always a floating point constant.
16563 When it is an integer, a CONST_DOUBLE is used whenever
16564 the constant requires 2 HWIs to be adequately represented.
16565 We output CONST_DOUBLEs as blocks. */
16566 if (mode == VOIDmode
16567 || (GET_MODE (rtl) == VOIDmode
16568 && maybe_ne (GET_MODE_BITSIZE (mode),
16569 HOST_BITS_PER_DOUBLE_INT)))
16570 break;
16571 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16572 if (type_die == NULL)
16573 return NULL;
16574 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16575 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16576 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16577 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16578 #if TARGET_SUPPORTS_WIDE_INT == 0
16579 if (!SCALAR_FLOAT_MODE_P (mode))
16581 mem_loc_result->dw_loc_oprnd2.val_class
16582 = dw_val_class_const_double;
16583 mem_loc_result->dw_loc_oprnd2.v.val_double
16584 = rtx_to_double_int (rtl);
16586 else
16587 #endif
16589 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16590 unsigned int length = GET_MODE_SIZE (float_mode);
16591 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16592 unsigned int elt_size = insert_float (rtl, array);
16594 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16595 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16596 = length / elt_size;
16597 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16598 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16601 break;
16603 case CONST_WIDE_INT:
16604 if (!dwarf_strict || dwarf_version >= 5)
16606 dw_die_ref type_die;
16608 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16609 if (type_die == NULL)
16610 return NULL;
16611 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16612 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16613 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16614 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16615 mem_loc_result->dw_loc_oprnd2.val_class
16616 = dw_val_class_wide_int;
16617 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16618 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16620 break;
16622 case CONST_POLY_INT:
16623 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16624 break;
16626 case EQ:
16627 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16628 break;
16630 case GE:
16631 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16632 break;
16634 case GT:
16635 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16636 break;
16638 case LE:
16639 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16640 break;
16642 case LT:
16643 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16644 break;
16646 case NE:
16647 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16648 break;
16650 case GEU:
16651 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16652 break;
16654 case GTU:
16655 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16656 break;
16658 case LEU:
16659 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16660 break;
16662 case LTU:
16663 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16664 break;
16666 case UMIN:
16667 case UMAX:
16668 if (!SCALAR_INT_MODE_P (mode))
16669 break;
16670 /* FALLTHRU */
16671 case SMIN:
16672 case SMAX:
16673 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16674 break;
16676 case ZERO_EXTRACT:
16677 case SIGN_EXTRACT:
16678 if (CONST_INT_P (XEXP (rtl, 1))
16679 && CONST_INT_P (XEXP (rtl, 2))
16680 && is_a <scalar_int_mode> (mode, &int_mode)
16681 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16682 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16683 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16684 && ((unsigned) INTVAL (XEXP (rtl, 1))
16685 + (unsigned) INTVAL (XEXP (rtl, 2))
16686 <= GET_MODE_BITSIZE (int_mode)))
16688 int shift, size;
16689 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16690 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16691 if (op0 == 0)
16692 break;
16693 if (GET_CODE (rtl) == SIGN_EXTRACT)
16694 op = DW_OP_shra;
16695 else
16696 op = DW_OP_shr;
16697 mem_loc_result = op0;
16698 size = INTVAL (XEXP (rtl, 1));
16699 shift = INTVAL (XEXP (rtl, 2));
16700 if (BITS_BIG_ENDIAN)
16701 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16702 if (shift + size != (int) DWARF2_ADDR_SIZE)
16704 add_loc_descr (&mem_loc_result,
16705 int_loc_descriptor (DWARF2_ADDR_SIZE
16706 - shift - size));
16707 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16709 if (size != (int) DWARF2_ADDR_SIZE)
16711 add_loc_descr (&mem_loc_result,
16712 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16713 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16716 break;
16718 case IF_THEN_ELSE:
16720 dw_loc_descr_ref op2, bra_node, drop_node;
16721 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16722 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16723 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16724 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16725 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16726 VAR_INIT_STATUS_INITIALIZED);
16727 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16728 VAR_INIT_STATUS_INITIALIZED);
16729 if (op0 == NULL || op1 == NULL || op2 == NULL)
16730 break;
16732 mem_loc_result = op1;
16733 add_loc_descr (&mem_loc_result, op2);
16734 add_loc_descr (&mem_loc_result, op0);
16735 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16736 add_loc_descr (&mem_loc_result, bra_node);
16737 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16738 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16739 add_loc_descr (&mem_loc_result, drop_node);
16740 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16741 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16743 break;
16745 case FLOAT_EXTEND:
16746 case FLOAT_TRUNCATE:
16747 case FLOAT:
16748 case UNSIGNED_FLOAT:
16749 case FIX:
16750 case UNSIGNED_FIX:
16751 if (!dwarf_strict || dwarf_version >= 5)
16753 dw_die_ref type_die;
16754 dw_loc_descr_ref cvt;
16756 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16757 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16758 if (op0 == NULL)
16759 break;
16760 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16761 && (GET_CODE (rtl) == FLOAT
16762 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16764 type_die = base_type_for_mode (int_mode,
16765 GET_CODE (rtl) == UNSIGNED_FLOAT);
16766 if (type_die == NULL)
16767 break;
16768 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16769 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16770 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16771 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16772 add_loc_descr (&op0, cvt);
16774 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16775 if (type_die == NULL)
16776 break;
16777 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16778 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16779 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16780 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16781 add_loc_descr (&op0, cvt);
16782 if (is_a <scalar_int_mode> (mode, &int_mode)
16783 && (GET_CODE (rtl) == FIX
16784 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16786 op0 = convert_descriptor_to_mode (int_mode, op0);
16787 if (op0 == NULL)
16788 break;
16790 mem_loc_result = op0;
16792 break;
16794 case CLZ:
16795 case CTZ:
16796 case FFS:
16797 if (is_a <scalar_int_mode> (mode, &int_mode))
16798 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16799 break;
16801 case POPCOUNT:
16802 case PARITY:
16803 if (is_a <scalar_int_mode> (mode, &int_mode))
16804 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16805 break;
16807 case BSWAP:
16808 if (is_a <scalar_int_mode> (mode, &int_mode))
16809 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16810 break;
16812 case ROTATE:
16813 case ROTATERT:
16814 if (is_a <scalar_int_mode> (mode, &int_mode))
16815 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16816 break;
16818 case COMPARE:
16819 /* In theory, we could implement the above. */
16820 /* DWARF cannot represent the unsigned compare operations
16821 natively. */
16822 case SS_MULT:
16823 case US_MULT:
16824 case SS_DIV:
16825 case US_DIV:
16826 case SS_PLUS:
16827 case US_PLUS:
16828 case SS_MINUS:
16829 case US_MINUS:
16830 case SS_NEG:
16831 case US_NEG:
16832 case SS_ABS:
16833 case SS_ASHIFT:
16834 case US_ASHIFT:
16835 case SS_TRUNCATE:
16836 case US_TRUNCATE:
16837 case UNORDERED:
16838 case ORDERED:
16839 case UNEQ:
16840 case UNGE:
16841 case UNGT:
16842 case UNLE:
16843 case UNLT:
16844 case LTGT:
16845 case FRACT_CONVERT:
16846 case UNSIGNED_FRACT_CONVERT:
16847 case SAT_FRACT:
16848 case UNSIGNED_SAT_FRACT:
16849 case SQRT:
16850 case ASM_OPERANDS:
16851 case VEC_MERGE:
16852 case VEC_SELECT:
16853 case VEC_CONCAT:
16854 case VEC_DUPLICATE:
16855 case VEC_SERIES:
16856 case HIGH:
16857 case FMA:
16858 case STRICT_LOW_PART:
16859 case CONST_VECTOR:
16860 case CONST_FIXED:
16861 case CLRSB:
16862 case CLOBBER:
16863 case SMUL_HIGHPART:
16864 case UMUL_HIGHPART:
16865 break;
16867 case CONST_STRING:
16868 resolve_one_addr (&rtl);
16869 goto symref;
16871 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16872 the expression. An UNSPEC rtx represents a raw DWARF operation,
16873 new_loc_descr is called for it to build the operation directly.
16874 Otherwise mem_loc_descriptor is called recursively. */
16875 case PARALLEL:
16877 int index = 0;
16878 dw_loc_descr_ref exp_result = NULL;
16880 for (; index < XVECLEN (rtl, 0); index++)
16882 rtx elem = XVECEXP (rtl, 0, index);
16883 if (GET_CODE (elem) == UNSPEC)
16885 /* Each DWARF operation UNSPEC contain two operands, if
16886 one operand is not used for the operation, const0_rtx is
16887 passed. */
16888 gcc_assert (XVECLEN (elem, 0) == 2);
16890 HOST_WIDE_INT dw_op = XINT (elem, 1);
16891 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16892 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16893 exp_result
16894 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16895 oprnd2);
16897 else
16898 exp_result
16899 = mem_loc_descriptor (elem, mode, mem_mode,
16900 VAR_INIT_STATUS_INITIALIZED);
16902 if (!mem_loc_result)
16903 mem_loc_result = exp_result;
16904 else
16905 add_loc_descr (&mem_loc_result, exp_result);
16908 break;
16911 default:
16912 if (flag_checking)
16914 print_rtl (stderr, rtl);
16915 gcc_unreachable ();
16917 break;
16920 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16921 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16923 return mem_loc_result;
16926 /* Return a descriptor that describes the concatenation of two locations.
16927 This is typically a complex variable. */
16929 static dw_loc_descr_ref
16930 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16932 /* At present we only track constant-sized pieces. */
16933 unsigned int size0, size1;
16934 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16935 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16936 return 0;
16938 dw_loc_descr_ref cc_loc_result = NULL;
16939 dw_loc_descr_ref x0_ref
16940 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16941 dw_loc_descr_ref x1_ref
16942 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16944 if (x0_ref == 0 || x1_ref == 0)
16945 return 0;
16947 cc_loc_result = x0_ref;
16948 add_loc_descr_op_piece (&cc_loc_result, size0);
16950 add_loc_descr (&cc_loc_result, x1_ref);
16951 add_loc_descr_op_piece (&cc_loc_result, size1);
16953 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16954 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16956 return cc_loc_result;
16959 /* Return a descriptor that describes the concatenation of N
16960 locations. */
16962 static dw_loc_descr_ref
16963 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16965 unsigned int i;
16966 dw_loc_descr_ref cc_loc_result = NULL;
16967 unsigned int n = XVECLEN (concatn, 0);
16968 unsigned int size;
16970 for (i = 0; i < n; ++i)
16972 dw_loc_descr_ref ref;
16973 rtx x = XVECEXP (concatn, 0, i);
16975 /* At present we only track constant-sized pieces. */
16976 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16977 return NULL;
16979 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16980 if (ref == NULL)
16981 return NULL;
16983 add_loc_descr (&cc_loc_result, ref);
16984 add_loc_descr_op_piece (&cc_loc_result, size);
16987 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16988 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16990 return cc_loc_result;
16993 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16994 for DEBUG_IMPLICIT_PTR RTL. */
16996 static dw_loc_descr_ref
16997 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16999 dw_loc_descr_ref ret;
17000 dw_die_ref ref;
17002 if (dwarf_strict && dwarf_version < 5)
17003 return NULL;
17004 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
17005 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17006 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17007 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17008 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17009 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17010 if (ref)
17012 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17013 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17014 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17016 else
17018 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17019 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17021 return ret;
17024 /* Output a proper Dwarf location descriptor for a variable or parameter
17025 which is either allocated in a register or in a memory location. For a
17026 register, we just generate an OP_REG and the register number. For a
17027 memory location we provide a Dwarf postfix expression describing how to
17028 generate the (dynamic) address of the object onto the address stack.
17030 MODE is mode of the decl if this loc_descriptor is going to be used in
17031 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17032 allowed, VOIDmode otherwise.
17034 If we don't know how to describe it, return 0. */
17036 static dw_loc_descr_ref
17037 loc_descriptor (rtx rtl, machine_mode mode,
17038 enum var_init_status initialized)
17040 dw_loc_descr_ref loc_result = NULL;
17041 scalar_int_mode int_mode;
17043 switch (GET_CODE (rtl))
17045 case SUBREG:
17046 /* The case of a subreg may arise when we have a local (register)
17047 variable or a formal (register) parameter which doesn't quite fill
17048 up an entire register. For now, just assume that it is
17049 legitimate to make the Dwarf info refer to the whole register which
17050 contains the given subreg. */
17051 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17052 loc_result = loc_descriptor (SUBREG_REG (rtl),
17053 GET_MODE (SUBREG_REG (rtl)), initialized);
17054 else
17055 goto do_default;
17056 break;
17058 case REG:
17059 loc_result = reg_loc_descriptor (rtl, initialized);
17060 break;
17062 case MEM:
17063 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17064 GET_MODE (rtl), initialized);
17065 if (loc_result == NULL)
17066 loc_result = tls_mem_loc_descriptor (rtl);
17067 if (loc_result == NULL)
17069 rtx new_rtl = avoid_constant_pool_reference (rtl);
17070 if (new_rtl != rtl)
17071 loc_result = loc_descriptor (new_rtl, mode, initialized);
17073 break;
17075 case CONCAT:
17076 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17077 initialized);
17078 break;
17080 case CONCATN:
17081 loc_result = concatn_loc_descriptor (rtl, initialized);
17082 break;
17084 case VAR_LOCATION:
17085 /* Single part. */
17086 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17088 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17089 if (GET_CODE (loc) == EXPR_LIST)
17090 loc = XEXP (loc, 0);
17091 loc_result = loc_descriptor (loc, mode, initialized);
17092 break;
17095 rtl = XEXP (rtl, 1);
17096 /* FALLTHRU */
17098 case PARALLEL:
17100 rtvec par_elems = XVEC (rtl, 0);
17101 int num_elem = GET_NUM_ELEM (par_elems);
17102 machine_mode mode;
17103 int i, size;
17105 /* Create the first one, so we have something to add to. */
17106 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17107 VOIDmode, initialized);
17108 if (loc_result == NULL)
17109 return NULL;
17110 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17111 /* At present we only track constant-sized pieces. */
17112 if (!GET_MODE_SIZE (mode).is_constant (&size))
17113 return NULL;
17114 add_loc_descr_op_piece (&loc_result, size);
17115 for (i = 1; i < num_elem; i++)
17117 dw_loc_descr_ref temp;
17119 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17120 VOIDmode, initialized);
17121 if (temp == NULL)
17122 return NULL;
17123 add_loc_descr (&loc_result, temp);
17124 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17125 /* At present we only track constant-sized pieces. */
17126 if (!GET_MODE_SIZE (mode).is_constant (&size))
17127 return NULL;
17128 add_loc_descr_op_piece (&loc_result, size);
17131 break;
17133 case CONST_INT:
17134 if (mode != VOIDmode && mode != BLKmode)
17136 int_mode = as_a <scalar_int_mode> (mode);
17137 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17138 INTVAL (rtl));
17140 break;
17142 case CONST_DOUBLE:
17143 if (mode == VOIDmode)
17144 mode = GET_MODE (rtl);
17146 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17148 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17150 /* Note that a CONST_DOUBLE rtx could represent either an integer
17151 or a floating-point constant. A CONST_DOUBLE is used whenever
17152 the constant requires more than one word in order to be
17153 adequately represented. We output CONST_DOUBLEs as blocks. */
17154 scalar_mode smode = as_a <scalar_mode> (mode);
17155 loc_result = new_loc_descr (DW_OP_implicit_value,
17156 GET_MODE_SIZE (smode), 0);
17157 #if TARGET_SUPPORTS_WIDE_INT == 0
17158 if (!SCALAR_FLOAT_MODE_P (smode))
17160 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17161 loc_result->dw_loc_oprnd2.v.val_double
17162 = rtx_to_double_int (rtl);
17164 else
17165 #endif
17167 unsigned int length = GET_MODE_SIZE (smode);
17168 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17169 unsigned int elt_size = insert_float (rtl, array);
17171 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17172 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17173 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17174 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17177 break;
17179 case CONST_WIDE_INT:
17180 if (mode == VOIDmode)
17181 mode = GET_MODE (rtl);
17183 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17185 int_mode = as_a <scalar_int_mode> (mode);
17186 loc_result = new_loc_descr (DW_OP_implicit_value,
17187 GET_MODE_SIZE (int_mode), 0);
17188 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17189 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17190 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17192 break;
17194 case CONST_VECTOR:
17195 if (mode == VOIDmode)
17196 mode = GET_MODE (rtl);
17198 if (mode != VOIDmode
17199 /* The combination of a length and byte elt_size doesn't extend
17200 naturally to boolean vectors, where several elements are packed
17201 into the same byte. */
17202 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17203 && (dwarf_version >= 4 || !dwarf_strict))
17205 unsigned int length;
17206 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17207 return NULL;
17209 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17210 unsigned char *array
17211 = ggc_vec_alloc<unsigned char> (length * elt_size);
17212 unsigned int i;
17213 unsigned char *p;
17214 machine_mode imode = GET_MODE_INNER (mode);
17216 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17217 switch (GET_MODE_CLASS (mode))
17219 case MODE_VECTOR_INT:
17220 for (i = 0, p = array; i < length; i++, p += elt_size)
17222 rtx elt = CONST_VECTOR_ELT (rtl, i);
17223 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17225 break;
17227 case MODE_VECTOR_FLOAT:
17228 for (i = 0, p = array; i < length; i++, p += elt_size)
17230 rtx elt = CONST_VECTOR_ELT (rtl, i);
17231 insert_float (elt, p);
17233 break;
17235 default:
17236 gcc_unreachable ();
17239 loc_result = new_loc_descr (DW_OP_implicit_value,
17240 length * elt_size, 0);
17241 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17242 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17243 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17244 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17246 break;
17248 case CONST:
17249 if (mode == VOIDmode
17250 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17251 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17252 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17254 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17255 break;
17257 /* FALLTHROUGH */
17258 case SYMBOL_REF:
17259 if (!const_ok_for_output (rtl))
17260 break;
17261 /* FALLTHROUGH */
17262 case LABEL_REF:
17263 if (is_a <scalar_int_mode> (mode, &int_mode)
17264 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17265 && (dwarf_version >= 4 || !dwarf_strict))
17267 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17268 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17269 vec_safe_push (used_rtx_array, rtl);
17271 break;
17273 case DEBUG_IMPLICIT_PTR:
17274 loc_result = implicit_ptr_descriptor (rtl, 0);
17275 break;
17277 case PLUS:
17278 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17279 && CONST_INT_P (XEXP (rtl, 1)))
17281 loc_result
17282 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17283 break;
17285 /* FALLTHRU */
17286 do_default:
17287 default:
17288 if ((is_a <scalar_int_mode> (mode, &int_mode)
17289 && GET_MODE (rtl) == int_mode
17290 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17291 && dwarf_version >= 4)
17292 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17294 /* Value expression. */
17295 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17296 if (loc_result)
17297 add_loc_descr (&loc_result,
17298 new_loc_descr (DW_OP_stack_value, 0, 0));
17300 break;
17303 return loc_result;
17306 /* We need to figure out what section we should use as the base for the
17307 address ranges where a given location is valid.
17308 1. If this particular DECL has a section associated with it, use that.
17309 2. If this function has a section associated with it, use that.
17310 3. Otherwise, use the text section.
17311 XXX: If you split a variable across multiple sections, we won't notice. */
17313 static const char *
17314 secname_for_decl (const_tree decl)
17316 const char *secname;
17318 if (VAR_OR_FUNCTION_DECL_P (decl)
17319 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17320 && DECL_SECTION_NAME (decl))
17321 secname = DECL_SECTION_NAME (decl);
17322 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17324 if (in_cold_section_p)
17326 section *sec = current_function_section ();
17327 if (sec->common.flags & SECTION_NAMED)
17328 return sec->named.name;
17330 secname = DECL_SECTION_NAME (current_function_decl);
17332 else if (cfun && in_cold_section_p)
17333 secname = crtl->subsections.cold_section_label;
17334 else
17335 secname = text_section_label;
17337 return secname;
17340 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17342 static bool
17343 decl_by_reference_p (tree decl)
17345 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17346 || VAR_P (decl))
17347 && DECL_BY_REFERENCE (decl));
17350 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17351 for VARLOC. */
17353 static dw_loc_descr_ref
17354 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17355 enum var_init_status initialized)
17357 int have_address = 0;
17358 dw_loc_descr_ref descr;
17359 machine_mode mode;
17361 if (want_address != 2)
17363 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17364 /* Single part. */
17365 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17367 varloc = PAT_VAR_LOCATION_LOC (varloc);
17368 if (GET_CODE (varloc) == EXPR_LIST)
17369 varloc = XEXP (varloc, 0);
17370 mode = GET_MODE (varloc);
17371 if (MEM_P (varloc))
17373 rtx addr = XEXP (varloc, 0);
17374 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17375 mode, initialized);
17376 if (descr)
17377 have_address = 1;
17378 else
17380 rtx x = avoid_constant_pool_reference (varloc);
17381 if (x != varloc)
17382 descr = mem_loc_descriptor (x, mode, VOIDmode,
17383 initialized);
17386 else
17387 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17389 else
17390 return 0;
17392 else
17394 if (GET_CODE (varloc) == VAR_LOCATION)
17395 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17396 else
17397 mode = DECL_MODE (loc);
17398 descr = loc_descriptor (varloc, mode, initialized);
17399 have_address = 1;
17402 if (!descr)
17403 return 0;
17405 if (want_address == 2 && !have_address
17406 && (dwarf_version >= 4 || !dwarf_strict))
17408 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17410 expansion_failed (loc, NULL_RTX,
17411 "DWARF address size mismatch");
17412 return 0;
17414 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17415 have_address = 1;
17417 /* Show if we can't fill the request for an address. */
17418 if (want_address && !have_address)
17420 expansion_failed (loc, NULL_RTX,
17421 "Want address and only have value");
17422 return 0;
17425 /* If we've got an address and don't want one, dereference. */
17426 if (!want_address && have_address)
17428 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17429 enum dwarf_location_atom op;
17431 if (size > DWARF2_ADDR_SIZE || size == -1)
17433 expansion_failed (loc, NULL_RTX,
17434 "DWARF address size mismatch");
17435 return 0;
17437 else if (size == DWARF2_ADDR_SIZE)
17438 op = DW_OP_deref;
17439 else
17440 op = DW_OP_deref_size;
17442 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17445 return descr;
17448 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17449 if it is not possible. */
17451 static dw_loc_descr_ref
17452 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17454 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17455 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17456 else if (dwarf_version >= 3 || !dwarf_strict)
17457 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17458 else
17459 return NULL;
17462 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17463 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17465 static dw_loc_descr_ref
17466 dw_sra_loc_expr (tree decl, rtx loc)
17468 rtx p;
17469 unsigned HOST_WIDE_INT padsize = 0;
17470 dw_loc_descr_ref descr, *descr_tail;
17471 unsigned HOST_WIDE_INT decl_size;
17472 rtx varloc;
17473 enum var_init_status initialized;
17475 if (DECL_SIZE (decl) == NULL
17476 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17477 return NULL;
17479 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17480 descr = NULL;
17481 descr_tail = &descr;
17483 for (p = loc; p; p = XEXP (p, 1))
17485 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17486 rtx loc_note = *decl_piece_varloc_ptr (p);
17487 dw_loc_descr_ref cur_descr;
17488 dw_loc_descr_ref *tail, last = NULL;
17489 unsigned HOST_WIDE_INT opsize = 0;
17491 if (loc_note == NULL_RTX
17492 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17494 padsize += bitsize;
17495 continue;
17497 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17498 varloc = NOTE_VAR_LOCATION (loc_note);
17499 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17500 if (cur_descr == NULL)
17502 padsize += bitsize;
17503 continue;
17506 /* Check that cur_descr either doesn't use
17507 DW_OP_*piece operations, or their sum is equal
17508 to bitsize. Otherwise we can't embed it. */
17509 for (tail = &cur_descr; *tail != NULL;
17510 tail = &(*tail)->dw_loc_next)
17511 if ((*tail)->dw_loc_opc == DW_OP_piece)
17513 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17514 * BITS_PER_UNIT;
17515 last = *tail;
17517 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17519 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17520 last = *tail;
17523 if (last != NULL && opsize != bitsize)
17525 padsize += bitsize;
17526 /* Discard the current piece of the descriptor and release any
17527 addr_table entries it uses. */
17528 remove_loc_list_addr_table_entries (cur_descr);
17529 continue;
17532 /* If there is a hole, add DW_OP_*piece after empty DWARF
17533 expression, which means that those bits are optimized out. */
17534 if (padsize)
17536 if (padsize > decl_size)
17538 remove_loc_list_addr_table_entries (cur_descr);
17539 goto discard_descr;
17541 decl_size -= padsize;
17542 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17543 if (*descr_tail == NULL)
17545 remove_loc_list_addr_table_entries (cur_descr);
17546 goto discard_descr;
17548 descr_tail = &(*descr_tail)->dw_loc_next;
17549 padsize = 0;
17551 *descr_tail = cur_descr;
17552 descr_tail = tail;
17553 if (bitsize > decl_size)
17554 goto discard_descr;
17555 decl_size -= bitsize;
17556 if (last == NULL)
17558 HOST_WIDE_INT offset = 0;
17559 if (GET_CODE (varloc) == VAR_LOCATION
17560 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17562 varloc = PAT_VAR_LOCATION_LOC (varloc);
17563 if (GET_CODE (varloc) == EXPR_LIST)
17564 varloc = XEXP (varloc, 0);
17568 if (GET_CODE (varloc) == CONST
17569 || GET_CODE (varloc) == SIGN_EXTEND
17570 || GET_CODE (varloc) == ZERO_EXTEND)
17571 varloc = XEXP (varloc, 0);
17572 else if (GET_CODE (varloc) == SUBREG)
17573 varloc = SUBREG_REG (varloc);
17574 else
17575 break;
17577 while (1);
17578 /* DW_OP_bit_size offset should be zero for register
17579 or implicit location descriptions and empty location
17580 descriptions, but for memory addresses needs big endian
17581 adjustment. */
17582 if (MEM_P (varloc))
17584 unsigned HOST_WIDE_INT memsize;
17585 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17586 goto discard_descr;
17587 memsize *= BITS_PER_UNIT;
17588 if (memsize != bitsize)
17590 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17591 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17592 goto discard_descr;
17593 if (memsize < bitsize)
17594 goto discard_descr;
17595 if (BITS_BIG_ENDIAN)
17596 offset = memsize - bitsize;
17600 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17601 if (*descr_tail == NULL)
17602 goto discard_descr;
17603 descr_tail = &(*descr_tail)->dw_loc_next;
17607 /* If there were any non-empty expressions, add padding till the end of
17608 the decl. */
17609 if (descr != NULL && decl_size != 0)
17611 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17612 if (*descr_tail == NULL)
17613 goto discard_descr;
17615 return descr;
17617 discard_descr:
17618 /* Discard the descriptor and release any addr_table entries it uses. */
17619 remove_loc_list_addr_table_entries (descr);
17620 return NULL;
17623 /* Return the dwarf representation of the location list LOC_LIST of
17624 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17625 function. */
17627 static dw_loc_list_ref
17628 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17630 const char *endname, *secname;
17631 var_loc_view endview;
17632 rtx varloc;
17633 enum var_init_status initialized;
17634 struct var_loc_node *node;
17635 dw_loc_descr_ref descr;
17636 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17637 dw_loc_list_ref list = NULL;
17638 dw_loc_list_ref *listp = &list;
17640 /* Now that we know what section we are using for a base,
17641 actually construct the list of locations.
17642 The first location information is what is passed to the
17643 function that creates the location list, and the remaining
17644 locations just get added on to that list.
17645 Note that we only know the start address for a location
17646 (IE location changes), so to build the range, we use
17647 the range [current location start, next location start].
17648 This means we have to special case the last node, and generate
17649 a range of [last location start, end of function label]. */
17651 if (cfun && crtl->has_bb_partition)
17653 bool save_in_cold_section_p = in_cold_section_p;
17654 in_cold_section_p = first_function_block_is_cold;
17655 if (loc_list->last_before_switch == NULL)
17656 in_cold_section_p = !in_cold_section_p;
17657 secname = secname_for_decl (decl);
17658 in_cold_section_p = save_in_cold_section_p;
17660 else
17661 secname = secname_for_decl (decl);
17663 for (node = loc_list->first; node; node = node->next)
17665 bool range_across_switch = false;
17666 if (GET_CODE (node->loc) == EXPR_LIST
17667 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17669 if (GET_CODE (node->loc) == EXPR_LIST)
17671 descr = NULL;
17672 /* This requires DW_OP_{,bit_}piece, which is not usable
17673 inside DWARF expressions. */
17674 if (want_address == 2)
17675 descr = dw_sra_loc_expr (decl, node->loc);
17677 else
17679 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17680 varloc = NOTE_VAR_LOCATION (node->loc);
17681 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17683 if (descr)
17685 /* If section switch happens in between node->label
17686 and node->next->label (or end of function) and
17687 we can't emit it as a single entry list,
17688 emit two ranges, first one ending at the end
17689 of first partition and second one starting at the
17690 beginning of second partition. */
17691 if (node == loc_list->last_before_switch
17692 && (node != loc_list->first || loc_list->first->next
17693 /* If we are to emit a view number, we will emit
17694 a loclist rather than a single location
17695 expression for the entire function (see
17696 loc_list_has_views), so we have to split the
17697 range that straddles across partitions. */
17698 || !ZERO_VIEW_P (node->view))
17699 && current_function_decl)
17701 endname = cfun->fde->dw_fde_end;
17702 endview = 0;
17703 range_across_switch = true;
17705 /* The variable has a location between NODE->LABEL and
17706 NODE->NEXT->LABEL. */
17707 else if (node->next)
17708 endname = node->next->label, endview = node->next->view;
17709 /* If the variable has a location at the last label
17710 it keeps its location until the end of function. */
17711 else if (!current_function_decl)
17712 endname = text_end_label, endview = 0;
17713 else
17715 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17716 current_function_funcdef_no);
17717 endname = ggc_strdup (label_id);
17718 endview = 0;
17721 *listp = new_loc_list (descr, node->label, node->view,
17722 endname, endview, secname);
17723 if (TREE_CODE (decl) == PARM_DECL
17724 && node == loc_list->first
17725 && NOTE_P (node->loc)
17726 && strcmp (node->label, endname) == 0)
17727 (*listp)->force = true;
17728 listp = &(*listp)->dw_loc_next;
17732 if (cfun
17733 && crtl->has_bb_partition
17734 && node == loc_list->last_before_switch)
17736 bool save_in_cold_section_p = in_cold_section_p;
17737 in_cold_section_p = !first_function_block_is_cold;
17738 secname = secname_for_decl (decl);
17739 in_cold_section_p = save_in_cold_section_p;
17742 if (range_across_switch)
17744 if (GET_CODE (node->loc) == EXPR_LIST)
17745 descr = dw_sra_loc_expr (decl, node->loc);
17746 else
17748 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17749 varloc = NOTE_VAR_LOCATION (node->loc);
17750 descr = dw_loc_list_1 (decl, varloc, want_address,
17751 initialized);
17753 gcc_assert (descr);
17754 /* The variable has a location between NODE->LABEL and
17755 NODE->NEXT->LABEL. */
17756 if (node->next)
17757 endname = node->next->label, endview = node->next->view;
17758 else
17759 endname = cfun->fde->dw_fde_second_end, endview = 0;
17760 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17761 endname, endview, secname);
17762 listp = &(*listp)->dw_loc_next;
17766 /* Try to avoid the overhead of a location list emitting a location
17767 expression instead, but only if we didn't have more than one
17768 location entry in the first place. If some entries were not
17769 representable, we don't want to pretend a single entry that was
17770 applies to the entire scope in which the variable is
17771 available. */
17772 if (list && loc_list->first->next)
17773 gen_llsym (list);
17774 else
17775 maybe_gen_llsym (list);
17777 return list;
17780 /* Return if the loc_list has only single element and thus can be represented
17781 as location description. */
17783 static bool
17784 single_element_loc_list_p (dw_loc_list_ref list)
17786 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17787 return !list->ll_symbol;
17790 /* Duplicate a single element of location list. */
17792 static inline dw_loc_descr_ref
17793 copy_loc_descr (dw_loc_descr_ref ref)
17795 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17796 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17797 return copy;
17800 /* To each location in list LIST append loc descr REF. */
17802 static void
17803 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17805 dw_loc_descr_ref copy;
17806 add_loc_descr (&list->expr, ref);
17807 list = list->dw_loc_next;
17808 while (list)
17810 copy = copy_loc_descr (ref);
17811 add_loc_descr (&list->expr, copy);
17812 while (copy->dw_loc_next)
17813 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17814 list = list->dw_loc_next;
17818 /* To each location in list LIST prepend loc descr REF. */
17820 static void
17821 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17823 dw_loc_descr_ref copy;
17824 dw_loc_descr_ref ref_end = list->expr;
17825 add_loc_descr (&ref, list->expr);
17826 list->expr = ref;
17827 list = list->dw_loc_next;
17828 while (list)
17830 dw_loc_descr_ref end = list->expr;
17831 list->expr = copy = copy_loc_descr (ref);
17832 while (copy->dw_loc_next != ref_end)
17833 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17834 copy->dw_loc_next = end;
17835 list = list->dw_loc_next;
17839 /* Given two lists RET and LIST
17840 produce location list that is result of adding expression in LIST
17841 to expression in RET on each position in program.
17842 Might be destructive on both RET and LIST.
17844 TODO: We handle only simple cases of RET or LIST having at most one
17845 element. General case would involve sorting the lists in program order
17846 and merging them that will need some additional work.
17847 Adding that will improve quality of debug info especially for SRA-ed
17848 structures. */
17850 static void
17851 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17853 if (!list)
17854 return;
17855 if (!*ret)
17857 *ret = list;
17858 return;
17860 if (!list->dw_loc_next)
17862 add_loc_descr_to_each (*ret, list->expr);
17863 return;
17865 if (!(*ret)->dw_loc_next)
17867 prepend_loc_descr_to_each (list, (*ret)->expr);
17868 *ret = list;
17869 return;
17871 expansion_failed (NULL_TREE, NULL_RTX,
17872 "Don't know how to merge two non-trivial"
17873 " location lists.\n");
17874 *ret = NULL;
17875 return;
17878 /* LOC is constant expression. Try a luck, look it up in constant
17879 pool and return its loc_descr of its address. */
17881 static dw_loc_descr_ref
17882 cst_pool_loc_descr (tree loc)
17884 /* Get an RTL for this, if something has been emitted. */
17885 rtx rtl = lookup_constant_def (loc);
17887 if (!rtl || !MEM_P (rtl))
17889 gcc_assert (!rtl);
17890 return 0;
17892 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17894 /* TODO: We might get more coverage if we was actually delaying expansion
17895 of all expressions till end of compilation when constant pools are fully
17896 populated. */
17897 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17899 expansion_failed (loc, NULL_RTX,
17900 "CST value in contant pool but not marked.");
17901 return 0;
17903 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17904 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17907 /* Return dw_loc_list representing address of addr_expr LOC
17908 by looking for inner INDIRECT_REF expression and turning
17909 it into simple arithmetics.
17911 See loc_list_from_tree for the meaning of CONTEXT. */
17913 static dw_loc_list_ref
17914 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17915 loc_descr_context *context)
17917 tree obj, offset;
17918 poly_int64 bitsize, bitpos, bytepos;
17919 machine_mode mode;
17920 int unsignedp, reversep, volatilep = 0;
17921 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17923 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17924 &bitsize, &bitpos, &offset, &mode,
17925 &unsignedp, &reversep, &volatilep);
17926 STRIP_NOPS (obj);
17927 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17929 expansion_failed (loc, NULL_RTX, "bitfield access");
17930 return 0;
17932 if (!INDIRECT_REF_P (obj))
17934 expansion_failed (obj,
17935 NULL_RTX, "no indirect ref in inner refrence");
17936 return 0;
17938 if (!offset && known_eq (bitpos, 0))
17939 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17940 context);
17941 else if (toplev
17942 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17943 && (dwarf_version >= 4 || !dwarf_strict))
17945 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17946 if (!list_ret)
17947 return 0;
17948 if (offset)
17950 /* Variable offset. */
17951 list_ret1 = loc_list_from_tree (offset, 0, context);
17952 if (list_ret1 == 0)
17953 return 0;
17954 add_loc_list (&list_ret, list_ret1);
17955 if (!list_ret)
17956 return 0;
17957 add_loc_descr_to_each (list_ret,
17958 new_loc_descr (DW_OP_plus, 0, 0));
17960 HOST_WIDE_INT value;
17961 if (bytepos.is_constant (&value) && value > 0)
17962 add_loc_descr_to_each (list_ret,
17963 new_loc_descr (DW_OP_plus_uconst, value, 0));
17964 else if (maybe_ne (bytepos, 0))
17965 loc_list_plus_const (list_ret, bytepos);
17966 add_loc_descr_to_each (list_ret,
17967 new_loc_descr (DW_OP_stack_value, 0, 0));
17969 return list_ret;
17972 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17973 all operations from LOC are nops, move to the last one. Insert in NOPS all
17974 operations that are skipped. */
17976 static void
17977 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17978 hash_set<dw_loc_descr_ref> &nops)
17980 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17982 nops.add (loc);
17983 loc = loc->dw_loc_next;
17987 /* Helper for loc_descr_without_nops: free the location description operation
17988 P. */
17990 bool
17991 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17993 ggc_free (loc);
17994 return true;
17997 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17998 finishes LOC. */
18000 static void
18001 loc_descr_without_nops (dw_loc_descr_ref &loc)
18003 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18004 return;
18006 /* Set of all DW_OP_nop operations we remove. */
18007 hash_set<dw_loc_descr_ref> nops;
18009 /* First, strip all prefix NOP operations in order to keep the head of the
18010 operations list. */
18011 loc_descr_to_next_no_nop (loc, nops);
18013 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18015 /* For control flow operations: strip "prefix" nops in destination
18016 labels. */
18017 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18018 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18019 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18020 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18022 /* Do the same for the operations that follow, then move to the next
18023 iteration. */
18024 if (cur->dw_loc_next != NULL)
18025 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18026 cur = cur->dw_loc_next;
18029 nops.traverse<void *, free_loc_descr> (NULL);
18033 struct dwarf_procedure_info;
18035 /* Helper structure for location descriptions generation. */
18036 struct loc_descr_context
18038 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18039 NULL_TREE if DW_OP_push_object_address in invalid for this location
18040 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18041 tree context_type;
18042 /* The ..._DECL node that should be translated as a
18043 DW_OP_push_object_address operation. */
18044 tree base_decl;
18045 /* Information about the DWARF procedure we are currently generating. NULL if
18046 we are not generating a DWARF procedure. */
18047 struct dwarf_procedure_info *dpi;
18048 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18049 by consumer. Used for DW_TAG_generic_subrange attributes. */
18050 bool placeholder_arg;
18051 /* True if PLACEHOLDER_EXPR has been seen. */
18052 bool placeholder_seen;
18053 /* True if strict preservation of signedness has been requested. */
18054 bool strict_signedness;
18057 /* DWARF procedures generation
18059 DWARF expressions (aka. location descriptions) are used to encode variable
18060 things such as sizes or offsets. Such computations can have redundant parts
18061 that can be factorized in order to reduce the size of the output debug
18062 information. This is the whole point of DWARF procedures.
18064 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
18065 already factorized into functions ("size functions") in order to handle very
18066 big and complex types. Such functions are quite simple: they have integral
18067 arguments, they return an integral result and their body contains only a
18068 return statement with arithmetic expressions. This is the only kind of
18069 function we are interested in translating into DWARF procedures, here.
18071 DWARF expressions and DWARF procedure are executed using a stack, so we have
18072 to define some calling convention for them to interact. Let's say that:
18074 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18075 all arguments in reverse order (right-to-left) so that when the DWARF
18076 procedure execution starts, the first argument is the top of the stack.
18078 - Then, when returning, the DWARF procedure must have consumed all arguments
18079 on the stack, must have pushed the result and touched nothing else.
18081 - Each integral argument and the result are integral types can be hold in a
18082 single stack slot.
18084 - We call "frame offset" the number of stack slots that are "under DWARF
18085 procedure control": it includes the arguments slots, the temporaries and
18086 the result slot. Thus, it is equal to the number of arguments when the
18087 procedure execution starts and must be equal to one (the result) when it
18088 returns. */
18090 /* Helper structure used when generating operations for a DWARF procedure. */
18091 struct dwarf_procedure_info
18093 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18094 currently translated. */
18095 tree fndecl;
18096 /* The number of arguments FNDECL takes. */
18097 unsigned args_count;
18100 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18101 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18102 equate it to this DIE. */
18104 static dw_die_ref
18105 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18106 dw_die_ref parent_die)
18108 dw_die_ref dwarf_proc_die;
18110 if ((dwarf_version < 3 && dwarf_strict)
18111 || location == NULL)
18112 return NULL;
18114 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18115 if (fndecl)
18116 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18117 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18118 return dwarf_proc_die;
18121 /* Return whether TYPE is a supported type as a DWARF procedure argument
18122 type or return type (we handle only scalar types and pointer types that
18123 aren't wider than the DWARF expression evaluation stack). */
18125 static bool
18126 is_handled_procedure_type (tree type)
18128 return ((INTEGRAL_TYPE_P (type)
18129 || TREE_CODE (type) == OFFSET_TYPE
18130 || TREE_CODE (type) == POINTER_TYPE)
18131 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18134 /* Helper for resolve_args_picking: do the same but stop when coming across
18135 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18136 offset *before* evaluating the corresponding operation. */
18138 static bool
18139 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18140 struct dwarf_procedure_info *dpi,
18141 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18143 /* The "frame_offset" identifier is already used to name a macro... */
18144 unsigned frame_offset_ = initial_frame_offset;
18145 dw_loc_descr_ref l;
18147 for (l = loc; l != NULL;)
18149 bool existed;
18150 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18152 /* If we already met this node, there is nothing to compute anymore. */
18153 if (existed)
18155 /* Make sure that the stack size is consistent wherever the execution
18156 flow comes from. */
18157 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18158 break;
18160 l_frame_offset = frame_offset_;
18162 /* If needed, relocate the picking offset with respect to the frame
18163 offset. */
18164 if (l->frame_offset_rel)
18166 unsigned HOST_WIDE_INT off;
18167 switch (l->dw_loc_opc)
18169 case DW_OP_pick:
18170 off = l->dw_loc_oprnd1.v.val_unsigned;
18171 break;
18172 case DW_OP_dup:
18173 off = 0;
18174 break;
18175 case DW_OP_over:
18176 off = 1;
18177 break;
18178 default:
18179 gcc_unreachable ();
18181 /* frame_offset_ is the size of the current stack frame, including
18182 incoming arguments. Besides, the arguments are pushed
18183 right-to-left. Thus, in order to access the Nth argument from
18184 this operation node, the picking has to skip temporaries *plus*
18185 one stack slot per argument (0 for the first one, 1 for the second
18186 one, etc.).
18188 The targetted argument number (N) is already set as the operand,
18189 and the number of temporaries can be computed with:
18190 frame_offsets_ - dpi->args_count */
18191 off += frame_offset_ - dpi->args_count;
18193 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18194 if (off > 255)
18195 return false;
18197 if (off == 0)
18199 l->dw_loc_opc = DW_OP_dup;
18200 l->dw_loc_oprnd1.v.val_unsigned = 0;
18202 else if (off == 1)
18204 l->dw_loc_opc = DW_OP_over;
18205 l->dw_loc_oprnd1.v.val_unsigned = 0;
18207 else
18209 l->dw_loc_opc = DW_OP_pick;
18210 l->dw_loc_oprnd1.v.val_unsigned = off;
18214 /* Update frame_offset according to the effect the current operation has
18215 on the stack. */
18216 switch (l->dw_loc_opc)
18218 case DW_OP_deref:
18219 case DW_OP_swap:
18220 case DW_OP_rot:
18221 case DW_OP_abs:
18222 case DW_OP_neg:
18223 case DW_OP_not:
18224 case DW_OP_plus_uconst:
18225 case DW_OP_skip:
18226 case DW_OP_reg0:
18227 case DW_OP_reg1:
18228 case DW_OP_reg2:
18229 case DW_OP_reg3:
18230 case DW_OP_reg4:
18231 case DW_OP_reg5:
18232 case DW_OP_reg6:
18233 case DW_OP_reg7:
18234 case DW_OP_reg8:
18235 case DW_OP_reg9:
18236 case DW_OP_reg10:
18237 case DW_OP_reg11:
18238 case DW_OP_reg12:
18239 case DW_OP_reg13:
18240 case DW_OP_reg14:
18241 case DW_OP_reg15:
18242 case DW_OP_reg16:
18243 case DW_OP_reg17:
18244 case DW_OP_reg18:
18245 case DW_OP_reg19:
18246 case DW_OP_reg20:
18247 case DW_OP_reg21:
18248 case DW_OP_reg22:
18249 case DW_OP_reg23:
18250 case DW_OP_reg24:
18251 case DW_OP_reg25:
18252 case DW_OP_reg26:
18253 case DW_OP_reg27:
18254 case DW_OP_reg28:
18255 case DW_OP_reg29:
18256 case DW_OP_reg30:
18257 case DW_OP_reg31:
18258 case DW_OP_bregx:
18259 case DW_OP_piece:
18260 case DW_OP_deref_size:
18261 case DW_OP_nop:
18262 case DW_OP_bit_piece:
18263 case DW_OP_implicit_value:
18264 case DW_OP_stack_value:
18265 case DW_OP_deref_type:
18266 case DW_OP_convert:
18267 case DW_OP_reinterpret:
18268 case DW_OP_GNU_deref_type:
18269 case DW_OP_GNU_convert:
18270 case DW_OP_GNU_reinterpret:
18271 break;
18273 case DW_OP_addr:
18274 case DW_OP_const1u:
18275 case DW_OP_const1s:
18276 case DW_OP_const2u:
18277 case DW_OP_const2s:
18278 case DW_OP_const4u:
18279 case DW_OP_const4s:
18280 case DW_OP_const8u:
18281 case DW_OP_const8s:
18282 case DW_OP_constu:
18283 case DW_OP_consts:
18284 case DW_OP_dup:
18285 case DW_OP_over:
18286 case DW_OP_pick:
18287 case DW_OP_lit0:
18288 case DW_OP_lit1:
18289 case DW_OP_lit2:
18290 case DW_OP_lit3:
18291 case DW_OP_lit4:
18292 case DW_OP_lit5:
18293 case DW_OP_lit6:
18294 case DW_OP_lit7:
18295 case DW_OP_lit8:
18296 case DW_OP_lit9:
18297 case DW_OP_lit10:
18298 case DW_OP_lit11:
18299 case DW_OP_lit12:
18300 case DW_OP_lit13:
18301 case DW_OP_lit14:
18302 case DW_OP_lit15:
18303 case DW_OP_lit16:
18304 case DW_OP_lit17:
18305 case DW_OP_lit18:
18306 case DW_OP_lit19:
18307 case DW_OP_lit20:
18308 case DW_OP_lit21:
18309 case DW_OP_lit22:
18310 case DW_OP_lit23:
18311 case DW_OP_lit24:
18312 case DW_OP_lit25:
18313 case DW_OP_lit26:
18314 case DW_OP_lit27:
18315 case DW_OP_lit28:
18316 case DW_OP_lit29:
18317 case DW_OP_lit30:
18318 case DW_OP_lit31:
18319 case DW_OP_breg0:
18320 case DW_OP_breg1:
18321 case DW_OP_breg2:
18322 case DW_OP_breg3:
18323 case DW_OP_breg4:
18324 case DW_OP_breg5:
18325 case DW_OP_breg6:
18326 case DW_OP_breg7:
18327 case DW_OP_breg8:
18328 case DW_OP_breg9:
18329 case DW_OP_breg10:
18330 case DW_OP_breg11:
18331 case DW_OP_breg12:
18332 case DW_OP_breg13:
18333 case DW_OP_breg14:
18334 case DW_OP_breg15:
18335 case DW_OP_breg16:
18336 case DW_OP_breg17:
18337 case DW_OP_breg18:
18338 case DW_OP_breg19:
18339 case DW_OP_breg20:
18340 case DW_OP_breg21:
18341 case DW_OP_breg22:
18342 case DW_OP_breg23:
18343 case DW_OP_breg24:
18344 case DW_OP_breg25:
18345 case DW_OP_breg26:
18346 case DW_OP_breg27:
18347 case DW_OP_breg28:
18348 case DW_OP_breg29:
18349 case DW_OP_breg30:
18350 case DW_OP_breg31:
18351 case DW_OP_fbreg:
18352 case DW_OP_push_object_address:
18353 case DW_OP_call_frame_cfa:
18354 case DW_OP_GNU_variable_value:
18355 case DW_OP_GNU_addr_index:
18356 case DW_OP_GNU_const_index:
18357 ++frame_offset_;
18358 break;
18360 case DW_OP_drop:
18361 case DW_OP_xderef:
18362 case DW_OP_and:
18363 case DW_OP_div:
18364 case DW_OP_minus:
18365 case DW_OP_mod:
18366 case DW_OP_mul:
18367 case DW_OP_or:
18368 case DW_OP_plus:
18369 case DW_OP_shl:
18370 case DW_OP_shr:
18371 case DW_OP_shra:
18372 case DW_OP_xor:
18373 case DW_OP_bra:
18374 case DW_OP_eq:
18375 case DW_OP_ge:
18376 case DW_OP_gt:
18377 case DW_OP_le:
18378 case DW_OP_lt:
18379 case DW_OP_ne:
18380 case DW_OP_regx:
18381 case DW_OP_xderef_size:
18382 --frame_offset_;
18383 break;
18385 case DW_OP_call2:
18386 case DW_OP_call4:
18387 case DW_OP_call_ref:
18389 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18390 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18392 if (stack_usage == NULL)
18393 return false;
18394 frame_offset_ += *stack_usage;
18395 break;
18398 case DW_OP_implicit_pointer:
18399 case DW_OP_entry_value:
18400 case DW_OP_const_type:
18401 case DW_OP_regval_type:
18402 case DW_OP_form_tls_address:
18403 case DW_OP_GNU_push_tls_address:
18404 case DW_OP_GNU_uninit:
18405 case DW_OP_GNU_encoded_addr:
18406 case DW_OP_GNU_implicit_pointer:
18407 case DW_OP_GNU_entry_value:
18408 case DW_OP_GNU_const_type:
18409 case DW_OP_GNU_regval_type:
18410 case DW_OP_GNU_parameter_ref:
18411 /* loc_list_from_tree will probably not output these operations for
18412 size functions, so assume they will not appear here. */
18413 /* Fall through... */
18415 default:
18416 gcc_unreachable ();
18419 /* Now, follow the control flow (except subroutine calls). */
18420 switch (l->dw_loc_opc)
18422 case DW_OP_bra:
18423 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18424 frame_offsets))
18425 return false;
18426 /* Fall through. */
18428 case DW_OP_skip:
18429 l = l->dw_loc_oprnd1.v.val_loc;
18430 break;
18432 case DW_OP_stack_value:
18433 return true;
18435 default:
18436 l = l->dw_loc_next;
18437 break;
18441 return true;
18444 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18445 operations) in order to resolve the operand of DW_OP_pick operations that
18446 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18447 offset *before* LOC is executed. Return if all relocations were
18448 successful. */
18450 static bool
18451 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18452 struct dwarf_procedure_info *dpi)
18454 /* Associate to all visited operations the frame offset *before* evaluating
18455 this operation. */
18456 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18458 return
18459 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18462 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18463 Return NULL if it is not possible. */
18465 static dw_die_ref
18466 function_to_dwarf_procedure (tree fndecl)
18468 struct dwarf_procedure_info dpi;
18469 struct loc_descr_context ctx = {
18470 NULL_TREE, /* context_type */
18471 NULL_TREE, /* base_decl */
18472 &dpi, /* dpi */
18473 false, /* placeholder_arg */
18474 false, /* placeholder_seen */
18475 true /* strict_signedness */
18477 dw_die_ref dwarf_proc_die;
18478 tree tree_body = DECL_SAVED_TREE (fndecl);
18479 dw_loc_descr_ref loc_body, epilogue;
18481 tree cursor;
18482 unsigned i;
18484 /* Do not generate multiple DWARF procedures for the same function
18485 declaration. */
18486 dwarf_proc_die = lookup_decl_die (fndecl);
18487 if (dwarf_proc_die != NULL)
18488 return dwarf_proc_die;
18490 /* DWARF procedures are available starting with the DWARFv3 standard. */
18491 if (dwarf_version < 3 && dwarf_strict)
18492 return NULL;
18494 /* We handle only functions for which we still have a body, that return a
18495 supported type and that takes arguments with supported types. Note that
18496 there is no point translating functions that return nothing. */
18497 if (tree_body == NULL_TREE
18498 || DECL_RESULT (fndecl) == NULL_TREE
18499 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18500 return NULL;
18502 for (cursor = DECL_ARGUMENTS (fndecl);
18503 cursor != NULL_TREE;
18504 cursor = TREE_CHAIN (cursor))
18505 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18506 return NULL;
18508 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18509 if (TREE_CODE (tree_body) != RETURN_EXPR)
18510 return NULL;
18511 tree_body = TREE_OPERAND (tree_body, 0);
18512 if (TREE_CODE (tree_body) != MODIFY_EXPR
18513 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18514 return NULL;
18515 tree_body = TREE_OPERAND (tree_body, 1);
18517 /* Try to translate the body expression itself. Note that this will probably
18518 cause an infinite recursion if its call graph has a cycle. This is very
18519 unlikely for size functions, however, so don't bother with such things at
18520 the moment. */
18521 dpi.fndecl = fndecl;
18522 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18523 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18524 if (!loc_body)
18525 return NULL;
18527 /* After evaluating all operands in "loc_body", we should still have on the
18528 stack all arguments plus the desired function result (top of the stack).
18529 Generate code in order to keep only the result in our stack frame. */
18530 epilogue = NULL;
18531 for (i = 0; i < dpi.args_count; ++i)
18533 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18534 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18535 op_couple->dw_loc_next->dw_loc_next = epilogue;
18536 epilogue = op_couple;
18538 add_loc_descr (&loc_body, epilogue);
18539 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18540 return NULL;
18542 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18543 because they are considered useful. Now there is an epilogue, they are
18544 not anymore, so give it another try. */
18545 loc_descr_without_nops (loc_body);
18547 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18548 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18549 though, given that size functions do not come from source, so they should
18550 not have a dedicated DW_TAG_subprogram DIE. */
18551 dwarf_proc_die
18552 = new_dwarf_proc_die (loc_body, fndecl,
18553 get_context_die (DECL_CONTEXT (fndecl)));
18555 /* The called DWARF procedure consumes one stack slot per argument and
18556 returns one stack slot. */
18557 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18559 return dwarf_proc_die;
18562 /* Helper function for loc_list_from_tree. Perform OP binary op,
18563 but after converting arguments to type_die, afterwards convert
18564 back to unsigned. */
18566 static dw_loc_list_ref
18567 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18568 dw_die_ref type_die, scalar_int_mode mode,
18569 struct loc_descr_context *context)
18571 dw_loc_list_ref op0, op1;
18572 dw_loc_descr_ref cvt, binop;
18574 if (type_die == NULL)
18575 return NULL;
18577 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18578 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18579 if (op0 == NULL || op1 == NULL)
18580 return NULL;
18582 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18583 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18584 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18585 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18586 add_loc_descr_to_each (op0, cvt);
18588 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18589 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18590 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18591 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18592 add_loc_descr_to_each (op1, cvt);
18594 add_loc_list (&op0, op1);
18595 if (op0 == NULL)
18596 return NULL;
18598 binop = new_loc_descr (op, 0, 0);
18599 convert_descriptor_to_mode (mode, binop);
18600 add_loc_descr_to_each (op0, binop);
18602 return op0;
18605 /* Generate Dwarf location list representing LOC.
18606 If WANT_ADDRESS is false, expression computing LOC will be computed
18607 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18608 if WANT_ADDRESS is 2, expression computing address useable in location
18609 will be returned (i.e. DW_OP_reg can be used
18610 to refer to register values).
18612 CONTEXT provides information to customize the location descriptions
18613 generation. Its context_type field specifies what type is implicitly
18614 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18615 will not be generated.
18617 Its DPI field determines whether we are generating a DWARF expression for a
18618 DWARF procedure, so PARM_DECL references are processed specifically.
18620 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18621 and dpi fields were null. */
18623 static dw_loc_list_ref
18624 loc_list_from_tree_1 (tree loc, int want_address,
18625 struct loc_descr_context *context)
18627 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18628 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18629 int have_address = 0;
18630 enum dwarf_location_atom op;
18632 /* ??? Most of the time we do not take proper care for sign/zero
18633 extending the values properly. Hopefully this won't be a real
18634 problem... */
18636 if (context != NULL
18637 && context->base_decl == loc
18638 && want_address == 0)
18640 if (dwarf_version >= 3 || !dwarf_strict)
18641 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18642 NULL, 0, NULL, 0, NULL);
18643 else
18644 return NULL;
18647 switch (TREE_CODE (loc))
18649 case ERROR_MARK:
18650 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18651 return 0;
18653 case PLACEHOLDER_EXPR:
18654 /* This case involves extracting fields from an object to determine the
18655 position of other fields. It is supposed to appear only as the first
18656 operand of COMPONENT_REF nodes and to reference precisely the type
18657 that the context allows or its enclosing type. */
18658 if (context != NULL
18659 && (TREE_TYPE (loc) == context->context_type
18660 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18661 && want_address >= 1)
18663 if (dwarf_version >= 3 || !dwarf_strict)
18665 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18666 have_address = 1;
18667 break;
18669 else
18670 return NULL;
18672 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18673 the single argument passed by consumer. */
18674 else if (context != NULL
18675 && context->placeholder_arg
18676 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18677 && want_address == 0)
18679 ret = new_loc_descr (DW_OP_pick, 0, 0);
18680 ret->frame_offset_rel = 1;
18681 context->placeholder_seen = true;
18682 break;
18684 else
18685 expansion_failed (loc, NULL_RTX,
18686 "PLACEHOLDER_EXPR for an unexpected type");
18687 break;
18689 case CALL_EXPR:
18691 tree callee = get_callee_fndecl (loc);
18692 dw_die_ref dwarf_proc;
18694 if (callee
18695 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18696 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18698 /* DWARF procedures are used for size functions, which are built
18699 when size expressions contain conditional constructs, so we
18700 request strict preservation of signedness for comparisons. */
18701 bool old_strict_signedness;
18702 if (context)
18704 old_strict_signedness = context->strict_signedness;
18705 context->strict_signedness = true;
18708 /* Evaluate arguments right-to-left so that the first argument
18709 will be the top-most one on the stack. */
18710 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18712 tree arg = CALL_EXPR_ARG (loc, i);
18713 ret1 = loc_descriptor_from_tree (arg, 0, context);
18714 if (!ret1)
18716 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18717 return NULL;
18719 add_loc_descr (&ret, ret1);
18722 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18723 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18724 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18725 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18726 add_loc_descr (&ret, ret1);
18727 if (context)
18728 context->strict_signedness = old_strict_signedness;
18730 else
18731 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18732 break;
18735 case PREINCREMENT_EXPR:
18736 case PREDECREMENT_EXPR:
18737 case POSTINCREMENT_EXPR:
18738 case POSTDECREMENT_EXPR:
18739 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18740 /* There are no opcodes for these operations. */
18741 return 0;
18743 case ADDR_EXPR:
18744 /* If we already want an address, see if there is INDIRECT_REF inside
18745 e.g. for &this->field. */
18746 if (want_address)
18748 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18749 (loc, want_address == 2, context);
18750 if (list_ret)
18751 have_address = 1;
18752 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18753 && (ret = cst_pool_loc_descr (loc)))
18754 have_address = 1;
18756 /* Otherwise, process the argument and look for the address. */
18757 if (!list_ret && !ret)
18758 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18759 else
18761 if (want_address)
18762 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18763 return NULL;
18765 break;
18767 case VAR_DECL:
18768 if (DECL_THREAD_LOCAL_P (loc))
18770 rtx rtl;
18771 enum dwarf_location_atom tls_op;
18772 enum dtprel_bool dtprel = dtprel_false;
18774 if (targetm.have_tls)
18776 /* If this is not defined, we have no way to emit the
18777 data. */
18778 if (!targetm.asm_out.output_dwarf_dtprel)
18779 return 0;
18781 /* The way DW_OP_GNU_push_tls_address is specified, we
18782 can only look up addresses of objects in the current
18783 module. We used DW_OP_addr as first op, but that's
18784 wrong, because DW_OP_addr is relocated by the debug
18785 info consumer, while DW_OP_GNU_push_tls_address
18786 operand shouldn't be. */
18787 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18788 return 0;
18789 dtprel = dtprel_true;
18790 /* We check for DWARF 5 here because gdb did not implement
18791 DW_OP_form_tls_address until after 7.12. */
18792 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18793 : DW_OP_GNU_push_tls_address);
18795 else
18797 if (!targetm.emutls.debug_form_tls_address
18798 || !(dwarf_version >= 3 || !dwarf_strict))
18799 return 0;
18800 /* We stuffed the control variable into the DECL_VALUE_EXPR
18801 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18802 no longer appear in gimple code. We used the control
18803 variable in specific so that we could pick it up here. */
18804 loc = DECL_VALUE_EXPR (loc);
18805 tls_op = DW_OP_form_tls_address;
18808 rtl = rtl_for_decl_location (loc);
18809 if (rtl == NULL_RTX)
18810 return 0;
18812 if (!MEM_P (rtl))
18813 return 0;
18814 rtl = XEXP (rtl, 0);
18815 if (! CONSTANT_P (rtl))
18816 return 0;
18818 ret = new_addr_loc_descr (rtl, dtprel);
18819 ret1 = new_loc_descr (tls_op, 0, 0);
18820 add_loc_descr (&ret, ret1);
18822 have_address = 1;
18823 break;
18825 /* FALLTHRU */
18827 case PARM_DECL:
18828 if (context != NULL && context->dpi != NULL
18829 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18831 /* We are generating code for a DWARF procedure and we want to access
18832 one of its arguments: find the appropriate argument offset and let
18833 the resolve_args_picking pass compute the offset that complies
18834 with the stack frame size. */
18835 unsigned i = 0;
18836 tree cursor;
18838 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18839 cursor != NULL_TREE && cursor != loc;
18840 cursor = TREE_CHAIN (cursor), ++i)
18842 /* If we are translating a DWARF procedure, all referenced parameters
18843 must belong to the current function. */
18844 gcc_assert (cursor != NULL_TREE);
18846 ret = new_loc_descr (DW_OP_pick, i, 0);
18847 ret->frame_offset_rel = 1;
18848 break;
18850 /* FALLTHRU */
18852 case RESULT_DECL:
18853 if (DECL_HAS_VALUE_EXPR_P (loc))
18855 tree value_expr = DECL_VALUE_EXPR (loc);
18857 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18858 to wait until they get an RTX in order to reference them. */
18859 if (early_dwarf
18860 && TREE_CODE (value_expr) == COMPONENT_REF
18861 && VAR_P (TREE_OPERAND (value_expr, 0))
18862 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18864 else
18865 return loc_list_from_tree_1 (value_expr, want_address, context);
18868 /* FALLTHRU */
18870 case FUNCTION_DECL:
18872 rtx rtl;
18873 var_loc_list *loc_list = lookup_decl_loc (loc);
18875 if (loc_list && loc_list->first)
18877 list_ret = dw_loc_list (loc_list, loc, want_address);
18878 have_address = want_address != 0;
18879 break;
18881 rtl = rtl_for_decl_location (loc);
18882 if (rtl == NULL_RTX)
18884 if (TREE_CODE (loc) != FUNCTION_DECL
18885 && early_dwarf
18886 && want_address != 1
18887 && ! DECL_IGNORED_P (loc)
18888 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18889 || POINTER_TYPE_P (TREE_TYPE (loc)))
18890 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18891 <= DWARF2_ADDR_SIZE))
18893 dw_die_ref ref = lookup_decl_die (loc);
18894 if (ref)
18896 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18897 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18898 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18899 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18901 else if (current_function_decl
18902 && DECL_CONTEXT (loc) == current_function_decl)
18904 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18905 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18906 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18908 break;
18910 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18911 return 0;
18913 else if (CONST_INT_P (rtl))
18915 HOST_WIDE_INT val = INTVAL (rtl);
18916 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18917 val &= GET_MODE_MASK (DECL_MODE (loc));
18918 ret = int_loc_descriptor (val);
18920 else if (GET_CODE (rtl) == CONST_STRING)
18922 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18923 return 0;
18925 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18926 ret = new_addr_loc_descr (rtl, dtprel_false);
18927 else
18929 machine_mode mode, mem_mode;
18931 /* Certain constructs can only be represented at top-level. */
18932 if (want_address == 2)
18934 ret = loc_descriptor (rtl, VOIDmode,
18935 VAR_INIT_STATUS_INITIALIZED);
18936 have_address = 1;
18938 else
18940 mode = GET_MODE (rtl);
18941 mem_mode = VOIDmode;
18942 if (MEM_P (rtl))
18944 mem_mode = mode;
18945 mode = get_address_mode (rtl);
18946 rtl = XEXP (rtl, 0);
18947 have_address = 1;
18949 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18950 VAR_INIT_STATUS_INITIALIZED);
18952 if (!ret)
18953 expansion_failed (loc, rtl,
18954 "failed to produce loc descriptor for rtl");
18957 break;
18959 case MEM_REF:
18960 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18962 have_address = 1;
18963 goto do_plus;
18965 /* Fallthru. */
18966 case INDIRECT_REF:
18967 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18968 have_address = 1;
18969 break;
18971 case TARGET_MEM_REF:
18972 case SSA_NAME:
18973 case DEBUG_EXPR_DECL:
18974 return NULL;
18976 case COMPOUND_EXPR:
18977 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18978 context);
18980 CASE_CONVERT:
18981 case VIEW_CONVERT_EXPR:
18982 case SAVE_EXPR:
18983 case MODIFY_EXPR:
18984 case NON_LVALUE_EXPR:
18985 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18986 context);
18988 case COMPONENT_REF:
18989 case BIT_FIELD_REF:
18990 case ARRAY_REF:
18991 case ARRAY_RANGE_REF:
18992 case REALPART_EXPR:
18993 case IMAGPART_EXPR:
18995 tree obj, offset;
18996 poly_int64 bitsize, bitpos, bytepos;
18997 machine_mode mode;
18998 int unsignedp, reversep, volatilep = 0;
19000 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19001 &unsignedp, &reversep, &volatilep);
19003 gcc_assert (obj != loc);
19005 list_ret = loc_list_from_tree_1 (obj,
19006 want_address == 2
19007 && known_eq (bitpos, 0)
19008 && !offset ? 2 : 1,
19009 context);
19010 /* TODO: We can extract value of the small expression via shifting even
19011 for nonzero bitpos. */
19012 if (list_ret == 0)
19013 return 0;
19014 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19015 || !multiple_p (bitsize, BITS_PER_UNIT))
19017 expansion_failed (loc, NULL_RTX,
19018 "bitfield access");
19019 return 0;
19022 if (offset != NULL_TREE)
19024 /* Variable offset. */
19025 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19026 if (list_ret1 == 0)
19027 return 0;
19028 add_loc_list (&list_ret, list_ret1);
19029 if (!list_ret)
19030 return 0;
19031 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19034 HOST_WIDE_INT value;
19035 if (bytepos.is_constant (&value) && value > 0)
19036 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19037 value, 0));
19038 else if (maybe_ne (bytepos, 0))
19039 loc_list_plus_const (list_ret, bytepos);
19041 have_address = 1;
19042 break;
19045 case INTEGER_CST:
19046 if ((want_address || !tree_fits_shwi_p (loc))
19047 && (ret = cst_pool_loc_descr (loc)))
19048 have_address = 1;
19049 else if (want_address == 2
19050 && tree_fits_shwi_p (loc)
19051 && (ret = address_of_int_loc_descriptor
19052 (int_size_in_bytes (TREE_TYPE (loc)),
19053 tree_to_shwi (loc))))
19054 have_address = 1;
19055 else if (tree_fits_shwi_p (loc))
19056 ret = int_loc_descriptor (tree_to_shwi (loc));
19057 else if (tree_fits_uhwi_p (loc))
19058 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19059 else
19061 expansion_failed (loc, NULL_RTX,
19062 "Integer operand is not host integer");
19063 return 0;
19065 break;
19067 case POLY_INT_CST:
19069 if (want_address)
19071 expansion_failed (loc, NULL_RTX,
19072 "constant address with a runtime component");
19073 return 0;
19075 poly_int64 value;
19076 if (!poly_int_tree_p (loc, &value))
19078 expansion_failed (loc, NULL_RTX, "constant too big");
19079 return 0;
19081 ret = int_loc_descriptor (value);
19083 break;
19085 case CONSTRUCTOR:
19086 case REAL_CST:
19087 case STRING_CST:
19088 case COMPLEX_CST:
19089 if ((ret = cst_pool_loc_descr (loc)))
19090 have_address = 1;
19091 else if (TREE_CODE (loc) == CONSTRUCTOR)
19093 tree type = TREE_TYPE (loc);
19094 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19095 unsigned HOST_WIDE_INT offset = 0;
19096 unsigned HOST_WIDE_INT cnt;
19097 constructor_elt *ce;
19099 if (TREE_CODE (type) == RECORD_TYPE)
19101 /* This is very limited, but it's enough to output
19102 pointers to member functions, as long as the
19103 referenced function is defined in the current
19104 translation unit. */
19105 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19107 tree val = ce->value;
19109 tree field = ce->index;
19111 if (val)
19112 STRIP_NOPS (val);
19114 if (!field || DECL_BIT_FIELD (field))
19116 expansion_failed (loc, NULL_RTX,
19117 "bitfield in record type constructor");
19118 size = offset = (unsigned HOST_WIDE_INT)-1;
19119 ret = NULL;
19120 break;
19123 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19124 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19125 gcc_assert (pos + fieldsize <= size);
19126 if (pos < offset)
19128 expansion_failed (loc, NULL_RTX,
19129 "out-of-order fields in record constructor");
19130 size = offset = (unsigned HOST_WIDE_INT)-1;
19131 ret = NULL;
19132 break;
19134 if (pos > offset)
19136 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19137 add_loc_descr (&ret, ret1);
19138 offset = pos;
19140 if (val && fieldsize != 0)
19142 ret1 = loc_descriptor_from_tree (val, want_address, context);
19143 if (!ret1)
19145 expansion_failed (loc, NULL_RTX,
19146 "unsupported expression in field");
19147 size = offset = (unsigned HOST_WIDE_INT)-1;
19148 ret = NULL;
19149 break;
19151 add_loc_descr (&ret, ret1);
19153 if (fieldsize)
19155 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19156 add_loc_descr (&ret, ret1);
19157 offset = pos + fieldsize;
19161 if (offset != size)
19163 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19164 add_loc_descr (&ret, ret1);
19165 offset = size;
19168 have_address = !!want_address;
19170 else
19171 expansion_failed (loc, NULL_RTX,
19172 "constructor of non-record type");
19174 else
19175 /* We can construct small constants here using int_loc_descriptor. */
19176 expansion_failed (loc, NULL_RTX,
19177 "constructor or constant not in constant pool");
19178 break;
19180 case TRUTH_AND_EXPR:
19181 case TRUTH_ANDIF_EXPR:
19182 case BIT_AND_EXPR:
19183 op = DW_OP_and;
19184 goto do_binop;
19186 case TRUTH_XOR_EXPR:
19187 case BIT_XOR_EXPR:
19188 op = DW_OP_xor;
19189 goto do_binop;
19191 case TRUTH_OR_EXPR:
19192 case TRUTH_ORIF_EXPR:
19193 case BIT_IOR_EXPR:
19194 op = DW_OP_or;
19195 goto do_binop;
19197 case EXACT_DIV_EXPR:
19198 case FLOOR_DIV_EXPR:
19199 case TRUNC_DIV_EXPR:
19200 /* Turn a divide by a power of 2 into a shift when possible. */
19201 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19202 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19204 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19205 if (log2 > 0)
19207 list_ret
19208 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19209 if (list_ret == 0)
19210 return 0;
19212 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19213 add_loc_descr_to_each (list_ret,
19214 new_loc_descr (DW_OP_shr, 0, 0));
19215 break;
19219 /* fall through */
19221 case CEIL_DIV_EXPR:
19222 case ROUND_DIV_EXPR:
19223 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19225 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19226 scalar_int_mode int_mode;
19228 if ((dwarf_strict && dwarf_version < 5)
19229 || !is_a <scalar_int_mode> (mode, &int_mode))
19230 return 0;
19232 /* We can use a signed divide if the sign bit is not set. */
19233 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19235 op = DW_OP_div;
19236 goto do_binop;
19239 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19240 base_type_for_mode (int_mode, 1),
19241 int_mode, context);
19242 break;
19244 op = DW_OP_div;
19245 goto do_binop;
19247 case MINUS_EXPR:
19248 op = DW_OP_minus;
19249 goto do_binop;
19251 case FLOOR_MOD_EXPR:
19252 case CEIL_MOD_EXPR:
19253 case ROUND_MOD_EXPR:
19254 case TRUNC_MOD_EXPR:
19255 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19257 op = DW_OP_mod;
19258 goto do_binop;
19260 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19261 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19262 if (list_ret == 0 || list_ret1 == 0)
19263 return 0;
19265 add_loc_list (&list_ret, list_ret1);
19266 if (list_ret == 0)
19267 return 0;
19268 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19269 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19270 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19271 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19272 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19273 break;
19275 case MULT_EXPR:
19276 op = DW_OP_mul;
19277 goto do_binop;
19279 case LSHIFT_EXPR:
19280 op = DW_OP_shl;
19281 goto do_binop;
19283 case RSHIFT_EXPR:
19284 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19285 goto do_binop;
19287 case POINTER_PLUS_EXPR:
19288 case PLUS_EXPR:
19289 do_plus:
19290 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19292 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19293 smarter to encode their opposite. The DW_OP_plus_uconst operation
19294 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19295 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19296 bytes, Y being the size of the operation that pushes the opposite
19297 of the addend. So let's choose the smallest representation. */
19298 const tree tree_addend = TREE_OPERAND (loc, 1);
19299 offset_int wi_addend;
19300 HOST_WIDE_INT shwi_addend;
19301 dw_loc_descr_ref loc_naddend;
19303 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19304 if (list_ret == 0)
19305 return 0;
19307 /* Try to get the literal to push. It is the opposite of the addend,
19308 so as we rely on wrapping during DWARF evaluation, first decode
19309 the literal as a "DWARF-sized" signed number. */
19310 wi_addend = wi::to_offset (tree_addend);
19311 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19312 shwi_addend = wi_addend.to_shwi ();
19313 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19314 ? int_loc_descriptor (-shwi_addend)
19315 : NULL;
19317 if (loc_naddend != NULL
19318 && ((unsigned) size_of_uleb128 (shwi_addend)
19319 > size_of_loc_descr (loc_naddend)))
19321 add_loc_descr_to_each (list_ret, loc_naddend);
19322 add_loc_descr_to_each (list_ret,
19323 new_loc_descr (DW_OP_minus, 0, 0));
19325 else
19327 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19329 loc_naddend = loc_cur;
19330 loc_cur = loc_cur->dw_loc_next;
19331 ggc_free (loc_naddend);
19333 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19335 break;
19338 op = DW_OP_plus;
19339 goto do_binop;
19341 case LE_EXPR:
19342 op = DW_OP_le;
19343 goto do_comp_binop;
19345 case GE_EXPR:
19346 op = DW_OP_ge;
19347 goto do_comp_binop;
19349 case LT_EXPR:
19350 op = DW_OP_lt;
19351 goto do_comp_binop;
19353 case GT_EXPR:
19354 op = DW_OP_gt;
19355 goto do_comp_binop;
19357 do_comp_binop:
19358 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19360 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19361 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19362 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19363 TREE_CODE (loc));
19364 break;
19366 else
19367 goto do_binop;
19369 case EQ_EXPR:
19370 op = DW_OP_eq;
19371 goto do_binop;
19373 case NE_EXPR:
19374 op = DW_OP_ne;
19375 goto do_binop;
19377 do_binop:
19378 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19379 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19380 if (list_ret == 0 || list_ret1 == 0)
19381 return 0;
19383 add_loc_list (&list_ret, list_ret1);
19384 if (list_ret == 0)
19385 return 0;
19386 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19387 break;
19389 case TRUTH_NOT_EXPR:
19390 case BIT_NOT_EXPR:
19391 op = DW_OP_not;
19392 goto do_unop;
19394 case ABS_EXPR:
19395 op = DW_OP_abs;
19396 goto do_unop;
19398 case NEGATE_EXPR:
19399 op = DW_OP_neg;
19400 goto do_unop;
19402 do_unop:
19403 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19404 if (list_ret == 0)
19405 return 0;
19407 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19408 break;
19410 case MIN_EXPR:
19411 case MAX_EXPR:
19413 const enum tree_code code =
19414 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19416 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19417 build2 (code, integer_type_node,
19418 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19419 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19422 /* fall through */
19424 case COND_EXPR:
19426 dw_loc_descr_ref lhs
19427 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19428 dw_loc_list_ref rhs
19429 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19430 dw_loc_descr_ref bra_node, jump_node, tmp;
19432 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19433 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19434 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19435 list_ret
19436 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19437 0, context);
19438 else
19439 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19440 if (list_ret == 0 || lhs == 0 || rhs == 0)
19441 return 0;
19443 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19444 add_loc_descr_to_each (list_ret, bra_node);
19446 add_loc_list (&list_ret, rhs);
19447 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19448 add_loc_descr_to_each (list_ret, jump_node);
19450 add_loc_descr_to_each (list_ret, lhs);
19451 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19452 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19454 /* ??? Need a node to point the skip at. Use a nop. */
19455 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19456 add_loc_descr_to_each (list_ret, tmp);
19457 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19458 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19460 break;
19462 case FIX_TRUNC_EXPR:
19463 return 0;
19465 case COMPOUND_LITERAL_EXPR:
19466 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19467 0, context);
19469 default:
19470 /* Leave front-end specific codes as simply unknown. This comes
19471 up, for instance, with the C STMT_EXPR. */
19472 if ((unsigned int) TREE_CODE (loc)
19473 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19475 expansion_failed (loc, NULL_RTX,
19476 "language specific tree node");
19477 return 0;
19480 /* Otherwise this is a generic code; we should just lists all of
19481 these explicitly. We forgot one. */
19482 if (flag_checking)
19483 gcc_unreachable ();
19485 /* In a release build, we want to degrade gracefully: better to
19486 generate incomplete debugging information than to crash. */
19487 return NULL;
19490 if (!ret && !list_ret)
19491 return 0;
19493 if (want_address == 2 && !have_address
19494 && (dwarf_version >= 4 || !dwarf_strict))
19496 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19498 expansion_failed (loc, NULL_RTX,
19499 "DWARF address size mismatch");
19500 return 0;
19502 if (ret)
19503 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19504 else
19505 add_loc_descr_to_each (list_ret,
19506 new_loc_descr (DW_OP_stack_value, 0, 0));
19507 have_address = 1;
19509 /* Show if we can't fill the request for an address. */
19510 if (want_address && !have_address)
19512 expansion_failed (loc, NULL_RTX,
19513 "Want address and only have value");
19514 return 0;
19517 gcc_assert (!ret || !list_ret);
19519 /* If we've got an address and don't want one, dereference. */
19520 if (!want_address && have_address)
19522 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19523 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19524 scalar_int_mode int_mode;
19525 dw_die_ref type_die;
19526 dw_loc_descr_ref deref;
19528 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19529 if (size > DWARF2_ADDR_SIZE || size == -1)
19531 expansion_failed (loc, NULL_RTX,
19532 "DWARF address size mismatch");
19533 return 0;
19536 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19537 else if (size == DWARF2_ADDR_SIZE)
19538 deref = new_loc_descr (DW_OP_deref, size, 0);
19540 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19541 extend the value, which is really OK for unsigned types only. */
19542 else if (!(context && context->strict_signedness)
19543 || TYPE_UNSIGNED (TREE_TYPE (loc))
19544 || (dwarf_strict && dwarf_version < 5)
19545 || !is_a <scalar_int_mode> (mode, &int_mode)
19546 || !(type_die = base_type_for_mode (mode, false)))
19547 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19549 /* Use DW_OP_deref_type for signed integral types if possible, but
19550 convert back to the generic type to avoid type mismatches later. */
19551 else
19553 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19554 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19555 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19556 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19557 add_loc_descr (&deref,
19558 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19561 if (ret)
19562 add_loc_descr (&ret, deref);
19563 else
19564 add_loc_descr_to_each (list_ret, deref);
19567 if (ret)
19568 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19570 return list_ret;
19573 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19574 expressions. */
19576 static dw_loc_list_ref
19577 loc_list_from_tree (tree loc, int want_address,
19578 struct loc_descr_context *context)
19580 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19582 for (dw_loc_list_ref loc_cur = result;
19583 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19584 loc_descr_without_nops (loc_cur->expr);
19585 return result;
19588 /* Same as above but return only single location expression. */
19589 static dw_loc_descr_ref
19590 loc_descriptor_from_tree (tree loc, int want_address,
19591 struct loc_descr_context *context)
19593 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19594 if (!ret)
19595 return NULL;
19596 if (ret->dw_loc_next)
19598 expansion_failed (loc, NULL_RTX,
19599 "Location list where only loc descriptor needed");
19600 return NULL;
19602 return ret->expr;
19605 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19606 pointer to the declared type for the relevant field variable, or return
19607 `integer_type_node' if the given node turns out to be an
19608 ERROR_MARK node. */
19610 static inline tree
19611 field_type (const_tree decl)
19613 tree type;
19615 if (TREE_CODE (decl) == ERROR_MARK)
19616 return integer_type_node;
19618 type = DECL_BIT_FIELD_TYPE (decl);
19619 if (type == NULL_TREE)
19620 type = TREE_TYPE (decl);
19622 return type;
19625 /* Given a pointer to a tree node, return the alignment in bits for
19626 it, or else return BITS_PER_WORD if the node actually turns out to
19627 be an ERROR_MARK node. */
19629 static inline unsigned
19630 simple_type_align_in_bits (const_tree type)
19632 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19635 static inline unsigned
19636 simple_decl_align_in_bits (const_tree decl)
19638 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19641 /* Return the result of rounding T up to ALIGN. */
19643 static inline offset_int
19644 round_up_to_align (const offset_int &t, unsigned int align)
19646 return wi::udiv_trunc (t + align - 1, align) * align;
19649 /* Helper structure for RECORD_TYPE processing. */
19650 struct vlr_context
19652 /* Root RECORD_TYPE. It is needed to generate data member location
19653 descriptions in variable-length records (VLR), but also to cope with
19654 variants, which are composed of nested structures multiplexed with
19655 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19656 function processing a FIELD_DECL, it is required to be non null. */
19657 tree struct_type;
19659 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19660 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19661 this variant part as part of the root record (in storage units). For
19662 regular records, it must be NULL_TREE. */
19663 tree variant_part_offset;
19666 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19667 addressed byte of the "containing object" for the given FIELD_DECL. If
19668 possible, return a native constant through CST_OFFSET (in which case NULL is
19669 returned); otherwise return a DWARF expression that computes the offset.
19671 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19672 that offset is, either because the argument turns out to be a pointer to an
19673 ERROR_MARK node, or because the offset expression is too complex for us.
19675 CTX is required: see the comment for VLR_CONTEXT. */
19677 static dw_loc_descr_ref
19678 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19679 HOST_WIDE_INT *cst_offset)
19681 tree tree_result;
19682 dw_loc_list_ref loc_result;
19684 *cst_offset = 0;
19686 if (TREE_CODE (decl) == ERROR_MARK)
19687 return NULL;
19688 else
19689 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19691 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19692 case. */
19693 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19694 return NULL;
19696 /* We used to handle only constant offsets in all cases. Now, we handle
19697 properly dynamic byte offsets only when PCC bitfield type doesn't
19698 matter. */
19699 if (PCC_BITFIELD_TYPE_MATTERS
19700 && DECL_BIT_FIELD_TYPE (decl)
19701 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19703 offset_int object_offset_in_bits;
19704 offset_int object_offset_in_bytes;
19705 offset_int bitpos_int;
19706 tree type;
19707 tree field_size_tree;
19708 offset_int deepest_bitpos;
19709 offset_int field_size_in_bits;
19710 unsigned int type_align_in_bits;
19711 unsigned int decl_align_in_bits;
19712 offset_int type_size_in_bits;
19714 bitpos_int = wi::to_offset (bit_position (decl));
19715 type = field_type (decl);
19716 type_size_in_bits = offset_int_type_size_in_bits (type);
19717 type_align_in_bits = simple_type_align_in_bits (type);
19719 field_size_tree = DECL_SIZE (decl);
19721 /* The size could be unspecified if there was an error, or for
19722 a flexible array member. */
19723 if (!field_size_tree)
19724 field_size_tree = bitsize_zero_node;
19726 /* If the size of the field is not constant, use the type size. */
19727 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19728 field_size_in_bits = wi::to_offset (field_size_tree);
19729 else
19730 field_size_in_bits = type_size_in_bits;
19732 decl_align_in_bits = simple_decl_align_in_bits (decl);
19734 /* The GCC front-end doesn't make any attempt to keep track of the
19735 starting bit offset (relative to the start of the containing
19736 structure type) of the hypothetical "containing object" for a
19737 bit-field. Thus, when computing the byte offset value for the
19738 start of the "containing object" of a bit-field, we must deduce
19739 this information on our own. This can be rather tricky to do in
19740 some cases. For example, handling the following structure type
19741 definition when compiling for an i386/i486 target (which only
19742 aligns long long's to 32-bit boundaries) can be very tricky:
19744 struct S { int field1; long long field2:31; };
19746 Fortunately, there is a simple rule-of-thumb which can be used
19747 in such cases. When compiling for an i386/i486, GCC will
19748 allocate 8 bytes for the structure shown above. It decides to
19749 do this based upon one simple rule for bit-field allocation.
19750 GCC allocates each "containing object" for each bit-field at
19751 the first (i.e. lowest addressed) legitimate alignment boundary
19752 (based upon the required minimum alignment for the declared
19753 type of the field) which it can possibly use, subject to the
19754 condition that there is still enough available space remaining
19755 in the containing object (when allocated at the selected point)
19756 to fully accommodate all of the bits of the bit-field itself.
19758 This simple rule makes it obvious why GCC allocates 8 bytes for
19759 each object of the structure type shown above. When looking
19760 for a place to allocate the "containing object" for `field2',
19761 the compiler simply tries to allocate a 64-bit "containing
19762 object" at each successive 32-bit boundary (starting at zero)
19763 until it finds a place to allocate that 64- bit field such that
19764 at least 31 contiguous (and previously unallocated) bits remain
19765 within that selected 64 bit field. (As it turns out, for the
19766 example above, the compiler finds it is OK to allocate the
19767 "containing object" 64-bit field at bit-offset zero within the
19768 structure type.)
19770 Here we attempt to work backwards from the limited set of facts
19771 we're given, and we try to deduce from those facts, where GCC
19772 must have believed that the containing object started (within
19773 the structure type). The value we deduce is then used (by the
19774 callers of this routine) to generate DW_AT_location and
19775 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19776 the case of DW_AT_location, regular fields as well). */
19778 /* Figure out the bit-distance from the start of the structure to
19779 the "deepest" bit of the bit-field. */
19780 deepest_bitpos = bitpos_int + field_size_in_bits;
19782 /* This is the tricky part. Use some fancy footwork to deduce
19783 where the lowest addressed bit of the containing object must
19784 be. */
19785 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19787 /* Round up to type_align by default. This works best for
19788 bitfields. */
19789 object_offset_in_bits
19790 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19792 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19794 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19796 /* Round up to decl_align instead. */
19797 object_offset_in_bits
19798 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19801 object_offset_in_bytes
19802 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19803 if (ctx->variant_part_offset == NULL_TREE)
19805 *cst_offset = object_offset_in_bytes.to_shwi ();
19806 return NULL;
19808 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19810 else
19811 tree_result = byte_position (decl);
19813 if (ctx->variant_part_offset != NULL_TREE)
19814 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19815 ctx->variant_part_offset, tree_result);
19817 /* If the byte offset is a constant, it's simplier to handle a native
19818 constant rather than a DWARF expression. */
19819 if (TREE_CODE (tree_result) == INTEGER_CST)
19821 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19822 return NULL;
19825 struct loc_descr_context loc_ctx = {
19826 ctx->struct_type, /* context_type */
19827 NULL_TREE, /* base_decl */
19828 NULL, /* dpi */
19829 false, /* placeholder_arg */
19830 false, /* placeholder_seen */
19831 false /* strict_signedness */
19833 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19835 /* We want a DWARF expression: abort if we only have a location list with
19836 multiple elements. */
19837 if (!loc_result || !single_element_loc_list_p (loc_result))
19838 return NULL;
19839 else
19840 return loc_result->expr;
19843 /* The following routines define various Dwarf attributes and any data
19844 associated with them. */
19846 /* Add a location description attribute value to a DIE.
19848 This emits location attributes suitable for whole variables and
19849 whole parameters. Note that the location attributes for struct fields are
19850 generated by the routine `data_member_location_attribute' below. */
19852 static inline void
19853 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19854 dw_loc_list_ref descr)
19856 bool check_no_locviews = true;
19857 if (descr == 0)
19858 return;
19859 if (single_element_loc_list_p (descr))
19860 add_AT_loc (die, attr_kind, descr->expr);
19861 else
19863 add_AT_loc_list (die, attr_kind, descr);
19864 gcc_assert (descr->ll_symbol);
19865 if (attr_kind == DW_AT_location && descr->vl_symbol
19866 && dwarf2out_locviews_in_attribute ())
19868 add_AT_view_list (die, DW_AT_GNU_locviews);
19869 check_no_locviews = false;
19873 if (check_no_locviews)
19874 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19877 /* Add DW_AT_accessibility attribute to DIE if needed. */
19879 static void
19880 add_accessibility_attribute (dw_die_ref die, tree decl)
19882 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19883 children, otherwise the default is DW_ACCESS_public. In DWARF2
19884 the default has always been DW_ACCESS_public. */
19885 if (TREE_PROTECTED (decl))
19886 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19887 else if (TREE_PRIVATE (decl))
19889 if (dwarf_version == 2
19890 || die->die_parent == NULL
19891 || die->die_parent->die_tag != DW_TAG_class_type)
19892 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19894 else if (dwarf_version > 2
19895 && die->die_parent
19896 && die->die_parent->die_tag == DW_TAG_class_type)
19897 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19900 /* Attach the specialized form of location attribute used for data members of
19901 struct and union types. In the special case of a FIELD_DECL node which
19902 represents a bit-field, the "offset" part of this special location
19903 descriptor must indicate the distance in bytes from the lowest-addressed
19904 byte of the containing struct or union type to the lowest-addressed byte of
19905 the "containing object" for the bit-field. (See the `field_byte_offset'
19906 function above).
19908 For any given bit-field, the "containing object" is a hypothetical object
19909 (of some integral or enum type) within which the given bit-field lives. The
19910 type of this hypothetical "containing object" is always the same as the
19911 declared type of the individual bit-field itself (for GCC anyway... the
19912 DWARF spec doesn't actually mandate this). Note that it is the size (in
19913 bytes) of the hypothetical "containing object" which will be given in the
19914 DW_AT_byte_size attribute for this bit-field. (See the
19915 `byte_size_attribute' function below.) It is also used when calculating the
19916 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19917 function below.)
19919 CTX is required: see the comment for VLR_CONTEXT. */
19921 static void
19922 add_data_member_location_attribute (dw_die_ref die,
19923 tree decl,
19924 struct vlr_context *ctx)
19926 HOST_WIDE_INT offset;
19927 dw_loc_descr_ref loc_descr = 0;
19929 if (TREE_CODE (decl) == TREE_BINFO)
19931 /* We're working on the TAG_inheritance for a base class. */
19932 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19934 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19935 aren't at a fixed offset from all (sub)objects of the same
19936 type. We need to extract the appropriate offset from our
19937 vtable. The following dwarf expression means
19939 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19941 This is specific to the V3 ABI, of course. */
19943 dw_loc_descr_ref tmp;
19945 /* Make a copy of the object address. */
19946 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19947 add_loc_descr (&loc_descr, tmp);
19949 /* Extract the vtable address. */
19950 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19951 add_loc_descr (&loc_descr, tmp);
19953 /* Calculate the address of the offset. */
19954 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19955 gcc_assert (offset < 0);
19957 tmp = int_loc_descriptor (-offset);
19958 add_loc_descr (&loc_descr, tmp);
19959 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19960 add_loc_descr (&loc_descr, tmp);
19962 /* Extract the offset. */
19963 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19964 add_loc_descr (&loc_descr, tmp);
19966 /* Add it to the object address. */
19967 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19968 add_loc_descr (&loc_descr, tmp);
19970 else
19971 offset = tree_to_shwi (BINFO_OFFSET (decl));
19973 else
19975 loc_descr = field_byte_offset (decl, ctx, &offset);
19977 if (!loc_descr)
19980 /* If loc_descr is available, then we know the offset is dynamic. */
19981 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
19983 loc_descr = NULL;
19984 offset = 0;
19987 /* Data member location evaluation starts with the base address on the
19988 stack. Compute the field offset and add it to this base address. */
19989 else
19990 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19993 if (!loc_descr)
19995 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19996 e.g. GDB only added support to it in November 2016. For DWARF5
19997 we need newer debug info consumers anyway. We might change this
19998 to dwarf_version >= 4 once most consumers catched up. */
19999 if (dwarf_version >= 5
20000 && TREE_CODE (decl) == FIELD_DECL
20001 && DECL_BIT_FIELD_TYPE (decl)
20002 && (ctx->variant_part_offset == NULL_TREE
20003 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20005 tree off = bit_position (decl);
20006 if (ctx->variant_part_offset)
20007 off = bit_from_pos (ctx->variant_part_offset, off);
20008 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20010 remove_AT (die, DW_AT_byte_size);
20011 remove_AT (die, DW_AT_bit_offset);
20012 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20013 return;
20016 if (dwarf_version > 2)
20018 /* Don't need to output a location expression, just the constant. */
20019 if (offset < 0)
20020 add_AT_int (die, DW_AT_data_member_location, offset);
20021 else
20022 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20023 return;
20025 else
20027 enum dwarf_location_atom op;
20029 /* The DWARF2 standard says that we should assume that the structure
20030 address is already on the stack, so we can specify a structure
20031 field address by using DW_OP_plus_uconst. */
20032 op = DW_OP_plus_uconst;
20033 loc_descr = new_loc_descr (op, offset, 0);
20037 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20040 /* Writes integer values to dw_vec_const array. */
20042 static void
20043 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20045 while (size != 0)
20047 *dest++ = val & 0xff;
20048 val >>= 8;
20049 --size;
20053 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20055 static HOST_WIDE_INT
20056 extract_int (const unsigned char *src, unsigned int size)
20058 HOST_WIDE_INT val = 0;
20060 src += size;
20061 while (size != 0)
20063 val <<= 8;
20064 val |= *--src & 0xff;
20065 --size;
20067 return val;
20070 /* Writes wide_int values to dw_vec_const array. */
20072 static void
20073 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20075 int i;
20077 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20079 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20080 return;
20083 /* We'd have to extend this code to support odd sizes. */
20084 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20086 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20088 if (WORDS_BIG_ENDIAN)
20089 for (i = n - 1; i >= 0; i--)
20091 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20092 dest += sizeof (HOST_WIDE_INT);
20094 else
20095 for (i = 0; i < n; i++)
20097 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20098 dest += sizeof (HOST_WIDE_INT);
20102 /* Writes floating point values to dw_vec_const array. */
20104 static unsigned
20105 insert_float (const_rtx rtl, unsigned char *array)
20107 long val[4];
20108 int i;
20109 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20111 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20113 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20114 if (GET_MODE_SIZE (mode) < 4)
20116 gcc_assert (GET_MODE_SIZE (mode) == 2);
20117 insert_int (val[0], 2, array);
20118 return 2;
20121 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20123 insert_int (val[i], 4, array);
20124 array += 4;
20126 return 4;
20129 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20130 does not have a "location" either in memory or in a register. These
20131 things can arise in GNU C when a constant is passed as an actual parameter
20132 to an inlined function. They can also arise in C++ where declared
20133 constants do not necessarily get memory "homes". */
20135 static bool
20136 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20138 scalar_mode int_mode;
20140 switch (GET_CODE (rtl))
20142 case CONST_INT:
20144 HOST_WIDE_INT val = INTVAL (rtl);
20146 if (val < 0)
20147 add_AT_int (die, DW_AT_const_value, val);
20148 else
20149 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20151 return true;
20153 case CONST_WIDE_INT:
20154 if (is_int_mode (mode, &int_mode)
20155 && (GET_MODE_PRECISION (int_mode)
20156 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20158 wide_int w = rtx_mode_t (rtl, int_mode);
20159 add_AT_wide (die, DW_AT_const_value, w);
20160 return true;
20162 return false;
20164 case CONST_DOUBLE:
20165 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20166 floating-point constant. A CONST_DOUBLE is used whenever the
20167 constant requires more than one word in order to be adequately
20168 represented. */
20169 if (TARGET_SUPPORTS_WIDE_INT == 0
20170 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20171 add_AT_double (die, DW_AT_const_value,
20172 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20173 else
20175 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20176 unsigned int length = GET_MODE_SIZE (mode);
20177 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20178 unsigned int elt_size = insert_float (rtl, array);
20180 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20181 array);
20183 return true;
20185 case CONST_VECTOR:
20187 unsigned int length;
20188 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20189 return false;
20191 machine_mode mode = GET_MODE (rtl);
20192 /* The combination of a length and byte elt_size doesn't extend
20193 naturally to boolean vectors, where several elements are packed
20194 into the same byte. */
20195 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20196 return false;
20198 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20199 unsigned char *array
20200 = ggc_vec_alloc<unsigned char> (length * elt_size);
20201 unsigned int i;
20202 unsigned char *p;
20203 machine_mode imode = GET_MODE_INNER (mode);
20205 switch (GET_MODE_CLASS (mode))
20207 case MODE_VECTOR_INT:
20208 for (i = 0, p = array; i < length; i++, p += elt_size)
20210 rtx elt = CONST_VECTOR_ELT (rtl, i);
20211 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20213 break;
20215 case MODE_VECTOR_FLOAT:
20216 for (i = 0, p = array; i < length; i++, p += elt_size)
20218 rtx elt = CONST_VECTOR_ELT (rtl, i);
20219 insert_float (elt, p);
20221 break;
20223 default:
20224 gcc_unreachable ();
20227 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20229 return true;
20231 case CONST_STRING:
20232 if (dwarf_version >= 4 || !dwarf_strict)
20234 dw_loc_descr_ref loc_result;
20235 resolve_one_addr (&rtl);
20236 rtl_addr:
20237 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20238 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20239 add_AT_loc (die, DW_AT_location, loc_result);
20240 vec_safe_push (used_rtx_array, rtl);
20241 return true;
20243 return false;
20245 case CONST:
20246 if (CONSTANT_P (XEXP (rtl, 0)))
20247 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20248 /* FALLTHROUGH */
20249 case SYMBOL_REF:
20250 if (!const_ok_for_output (rtl))
20251 return false;
20252 /* FALLTHROUGH */
20253 case LABEL_REF:
20254 if (dwarf_version >= 4 || !dwarf_strict)
20255 goto rtl_addr;
20256 return false;
20258 case PLUS:
20259 /* In cases where an inlined instance of an inline function is passed
20260 the address of an `auto' variable (which is local to the caller) we
20261 can get a situation where the DECL_RTL of the artificial local
20262 variable (for the inlining) which acts as a stand-in for the
20263 corresponding formal parameter (of the inline function) will look
20264 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20265 exactly a compile-time constant expression, but it isn't the address
20266 of the (artificial) local variable either. Rather, it represents the
20267 *value* which the artificial local variable always has during its
20268 lifetime. We currently have no way to represent such quasi-constant
20269 values in Dwarf, so for now we just punt and generate nothing. */
20270 return false;
20272 case HIGH:
20273 case CONST_FIXED:
20274 case MINUS:
20275 case SIGN_EXTEND:
20276 case ZERO_EXTEND:
20277 case CONST_POLY_INT:
20278 return false;
20280 case MEM:
20281 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20282 && MEM_READONLY_P (rtl)
20283 && GET_MODE (rtl) == BLKmode)
20285 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20286 return true;
20288 return false;
20290 default:
20291 /* No other kinds of rtx should be possible here. */
20292 gcc_unreachable ();
20296 /* Determine whether the evaluation of EXPR references any variables
20297 or functions which aren't otherwise used (and therefore may not be
20298 output). */
20299 static tree
20300 reference_to_unused (tree * tp, int * walk_subtrees,
20301 void * data ATTRIBUTE_UNUSED)
20303 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20304 *walk_subtrees = 0;
20306 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20307 && ! TREE_ASM_WRITTEN (*tp))
20308 return *tp;
20309 /* ??? The C++ FE emits debug information for using decls, so
20310 putting gcc_unreachable here falls over. See PR31899. For now
20311 be conservative. */
20312 else if (!symtab->global_info_ready && VAR_P (*tp))
20313 return *tp;
20314 else if (VAR_P (*tp))
20316 varpool_node *node = varpool_node::get (*tp);
20317 if (!node || !node->definition)
20318 return *tp;
20320 else if (TREE_CODE (*tp) == FUNCTION_DECL
20321 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20323 /* The call graph machinery must have finished analyzing,
20324 optimizing and gimplifying the CU by now.
20325 So if *TP has no call graph node associated
20326 to it, it means *TP will not be emitted. */
20327 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20328 return *tp;
20330 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20331 return *tp;
20333 return NULL_TREE;
20336 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20337 for use in a later add_const_value_attribute call. */
20339 static rtx
20340 rtl_for_decl_init (tree init, tree type)
20342 rtx rtl = NULL_RTX;
20344 STRIP_NOPS (init);
20346 /* If a variable is initialized with a string constant without embedded
20347 zeros, build CONST_STRING. */
20348 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20350 tree enttype = TREE_TYPE (type);
20351 tree domain = TYPE_DOMAIN (type);
20352 scalar_int_mode mode;
20354 if (is_int_mode (TYPE_MODE (enttype), &mode)
20355 && GET_MODE_SIZE (mode) == 1
20356 && domain
20357 && TYPE_MAX_VALUE (domain)
20358 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20359 && integer_zerop (TYPE_MIN_VALUE (domain))
20360 && compare_tree_int (TYPE_MAX_VALUE (domain),
20361 TREE_STRING_LENGTH (init) - 1) == 0
20362 && ((size_t) TREE_STRING_LENGTH (init)
20363 == strlen (TREE_STRING_POINTER (init)) + 1))
20365 rtl = gen_rtx_CONST_STRING (VOIDmode,
20366 ggc_strdup (TREE_STRING_POINTER (init)));
20367 rtl = gen_rtx_MEM (BLKmode, rtl);
20368 MEM_READONLY_P (rtl) = 1;
20371 /* Other aggregates, and complex values, could be represented using
20372 CONCAT: FIXME! */
20373 else if (AGGREGATE_TYPE_P (type)
20374 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20375 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20376 || TREE_CODE (type) == COMPLEX_TYPE)
20378 /* Vectors only work if their mode is supported by the target.
20379 FIXME: generic vectors ought to work too. */
20380 else if (TREE_CODE (type) == VECTOR_TYPE
20381 && !VECTOR_MODE_P (TYPE_MODE (type)))
20383 /* If the initializer is something that we know will expand into an
20384 immediate RTL constant, expand it now. We must be careful not to
20385 reference variables which won't be output. */
20386 else if (initializer_constant_valid_p (init, type)
20387 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20389 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20390 possible. */
20391 if (TREE_CODE (type) == VECTOR_TYPE)
20392 switch (TREE_CODE (init))
20394 case VECTOR_CST:
20395 break;
20396 case CONSTRUCTOR:
20397 if (TREE_CONSTANT (init))
20399 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20400 bool constant_p = true;
20401 tree value;
20402 unsigned HOST_WIDE_INT ix;
20404 /* Even when ctor is constant, it might contain non-*_CST
20405 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20406 belong into VECTOR_CST nodes. */
20407 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20408 if (!CONSTANT_CLASS_P (value))
20410 constant_p = false;
20411 break;
20414 if (constant_p)
20416 init = build_vector_from_ctor (type, elts);
20417 break;
20420 /* FALLTHRU */
20422 default:
20423 return NULL;
20426 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20428 /* If expand_expr returns a MEM, it wasn't immediate. */
20429 gcc_assert (!rtl || !MEM_P (rtl));
20432 return rtl;
20435 /* Generate RTL for the variable DECL to represent its location. */
20437 static rtx
20438 rtl_for_decl_location (tree decl)
20440 rtx rtl;
20442 /* Here we have to decide where we are going to say the parameter "lives"
20443 (as far as the debugger is concerned). We only have a couple of
20444 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20446 DECL_RTL normally indicates where the parameter lives during most of the
20447 activation of the function. If optimization is enabled however, this
20448 could be either NULL or else a pseudo-reg. Both of those cases indicate
20449 that the parameter doesn't really live anywhere (as far as the code
20450 generation parts of GCC are concerned) during most of the function's
20451 activation. That will happen (for example) if the parameter is never
20452 referenced within the function.
20454 We could just generate a location descriptor here for all non-NULL
20455 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20456 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20457 where DECL_RTL is NULL or is a pseudo-reg.
20459 Note however that we can only get away with using DECL_INCOMING_RTL as
20460 a backup substitute for DECL_RTL in certain limited cases. In cases
20461 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20462 we can be sure that the parameter was passed using the same type as it is
20463 declared to have within the function, and that its DECL_INCOMING_RTL
20464 points us to a place where a value of that type is passed.
20466 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20467 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20468 because in these cases DECL_INCOMING_RTL points us to a value of some
20469 type which is *different* from the type of the parameter itself. Thus,
20470 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20471 such cases, the debugger would end up (for example) trying to fetch a
20472 `float' from a place which actually contains the first part of a
20473 `double'. That would lead to really incorrect and confusing
20474 output at debug-time.
20476 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20477 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20478 are a couple of exceptions however. On little-endian machines we can
20479 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20480 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20481 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20482 when (on a little-endian machine) a non-prototyped function has a
20483 parameter declared to be of type `short' or `char'. In such cases,
20484 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20485 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20486 passed `int' value. If the debugger then uses that address to fetch
20487 a `short' or a `char' (on a little-endian machine) the result will be
20488 the correct data, so we allow for such exceptional cases below.
20490 Note that our goal here is to describe the place where the given formal
20491 parameter lives during most of the function's activation (i.e. between the
20492 end of the prologue and the start of the epilogue). We'll do that as best
20493 as we can. Note however that if the given formal parameter is modified
20494 sometime during the execution of the function, then a stack backtrace (at
20495 debug-time) will show the function as having been called with the *new*
20496 value rather than the value which was originally passed in. This happens
20497 rarely enough that it is not a major problem, but it *is* a problem, and
20498 I'd like to fix it.
20500 A future version of dwarf2out.c may generate two additional attributes for
20501 any given DW_TAG_formal_parameter DIE which will describe the "passed
20502 type" and the "passed location" for the given formal parameter in addition
20503 to the attributes we now generate to indicate the "declared type" and the
20504 "active location" for each parameter. This additional set of attributes
20505 could be used by debuggers for stack backtraces. Separately, note that
20506 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20507 This happens (for example) for inlined-instances of inline function formal
20508 parameters which are never referenced. This really shouldn't be
20509 happening. All PARM_DECL nodes should get valid non-NULL
20510 DECL_INCOMING_RTL values. FIXME. */
20512 /* Use DECL_RTL as the "location" unless we find something better. */
20513 rtl = DECL_RTL_IF_SET (decl);
20515 /* When generating abstract instances, ignore everything except
20516 constants, symbols living in memory, and symbols living in
20517 fixed registers. */
20518 if (! reload_completed)
20520 if (rtl
20521 && (CONSTANT_P (rtl)
20522 || (MEM_P (rtl)
20523 && CONSTANT_P (XEXP (rtl, 0)))
20524 || (REG_P (rtl)
20525 && VAR_P (decl)
20526 && TREE_STATIC (decl))))
20528 rtl = targetm.delegitimize_address (rtl);
20529 return rtl;
20531 rtl = NULL_RTX;
20533 else if (TREE_CODE (decl) == PARM_DECL)
20535 if (rtl == NULL_RTX
20536 || is_pseudo_reg (rtl)
20537 || (MEM_P (rtl)
20538 && is_pseudo_reg (XEXP (rtl, 0))
20539 && DECL_INCOMING_RTL (decl)
20540 && MEM_P (DECL_INCOMING_RTL (decl))
20541 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20543 tree declared_type = TREE_TYPE (decl);
20544 tree passed_type = DECL_ARG_TYPE (decl);
20545 machine_mode dmode = TYPE_MODE (declared_type);
20546 machine_mode pmode = TYPE_MODE (passed_type);
20548 /* This decl represents a formal parameter which was optimized out.
20549 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20550 all cases where (rtl == NULL_RTX) just below. */
20551 if (dmode == pmode)
20552 rtl = DECL_INCOMING_RTL (decl);
20553 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20554 && SCALAR_INT_MODE_P (dmode)
20555 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20556 && DECL_INCOMING_RTL (decl))
20558 rtx inc = DECL_INCOMING_RTL (decl);
20559 if (REG_P (inc))
20560 rtl = inc;
20561 else if (MEM_P (inc))
20563 if (BYTES_BIG_ENDIAN)
20564 rtl = adjust_address_nv (inc, dmode,
20565 GET_MODE_SIZE (pmode)
20566 - GET_MODE_SIZE (dmode));
20567 else
20568 rtl = inc;
20573 /* If the parm was passed in registers, but lives on the stack, then
20574 make a big endian correction if the mode of the type of the
20575 parameter is not the same as the mode of the rtl. */
20576 /* ??? This is the same series of checks that are made in dbxout.c before
20577 we reach the big endian correction code there. It isn't clear if all
20578 of these checks are necessary here, but keeping them all is the safe
20579 thing to do. */
20580 else if (MEM_P (rtl)
20581 && XEXP (rtl, 0) != const0_rtx
20582 && ! CONSTANT_P (XEXP (rtl, 0))
20583 /* Not passed in memory. */
20584 && !MEM_P (DECL_INCOMING_RTL (decl))
20585 /* Not passed by invisible reference. */
20586 && (!REG_P (XEXP (rtl, 0))
20587 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20588 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20589 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20590 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20591 #endif
20593 /* Big endian correction check. */
20594 && BYTES_BIG_ENDIAN
20595 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20596 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20597 UNITS_PER_WORD))
20599 machine_mode addr_mode = get_address_mode (rtl);
20600 poly_int64 offset = (UNITS_PER_WORD
20601 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20603 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20604 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20607 else if (VAR_P (decl)
20608 && rtl
20609 && MEM_P (rtl)
20610 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20612 machine_mode addr_mode = get_address_mode (rtl);
20613 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20614 GET_MODE (rtl));
20616 /* If a variable is declared "register" yet is smaller than
20617 a register, then if we store the variable to memory, it
20618 looks like we're storing a register-sized value, when in
20619 fact we are not. We need to adjust the offset of the
20620 storage location to reflect the actual value's bytes,
20621 else gdb will not be able to display it. */
20622 if (maybe_ne (offset, 0))
20623 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20624 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20627 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20628 and will have been substituted directly into all expressions that use it.
20629 C does not have such a concept, but C++ and other languages do. */
20630 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20631 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20633 if (rtl)
20634 rtl = targetm.delegitimize_address (rtl);
20636 /* If we don't look past the constant pool, we risk emitting a
20637 reference to a constant pool entry that isn't referenced from
20638 code, and thus is not emitted. */
20639 if (rtl)
20640 rtl = avoid_constant_pool_reference (rtl);
20642 /* Try harder to get a rtl. If this symbol ends up not being emitted
20643 in the current CU, resolve_addr will remove the expression referencing
20644 it. */
20645 if (rtl == NULL_RTX
20646 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20647 && VAR_P (decl)
20648 && !DECL_EXTERNAL (decl)
20649 && TREE_STATIC (decl)
20650 && DECL_NAME (decl)
20651 && !DECL_HARD_REGISTER (decl)
20652 && DECL_MODE (decl) != VOIDmode)
20654 rtl = make_decl_rtl_for_debug (decl);
20655 if (!MEM_P (rtl)
20656 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20657 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20658 rtl = NULL_RTX;
20661 return rtl;
20664 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20665 returned. If so, the decl for the COMMON block is returned, and the
20666 value is the offset into the common block for the symbol. */
20668 static tree
20669 fortran_common (tree decl, HOST_WIDE_INT *value)
20671 tree val_expr, cvar;
20672 machine_mode mode;
20673 poly_int64 bitsize, bitpos;
20674 tree offset;
20675 HOST_WIDE_INT cbitpos;
20676 int unsignedp, reversep, volatilep = 0;
20678 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20679 it does not have a value (the offset into the common area), or if it
20680 is thread local (as opposed to global) then it isn't common, and shouldn't
20681 be handled as such. */
20682 if (!VAR_P (decl)
20683 || !TREE_STATIC (decl)
20684 || !DECL_HAS_VALUE_EXPR_P (decl)
20685 || !is_fortran ())
20686 return NULL_TREE;
20688 val_expr = DECL_VALUE_EXPR (decl);
20689 if (TREE_CODE (val_expr) != COMPONENT_REF)
20690 return NULL_TREE;
20692 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20693 &unsignedp, &reversep, &volatilep);
20695 if (cvar == NULL_TREE
20696 || !VAR_P (cvar)
20697 || DECL_ARTIFICIAL (cvar)
20698 || !TREE_PUBLIC (cvar)
20699 /* We don't expect to have to cope with variable offsets,
20700 since at present all static data must have a constant size. */
20701 || !bitpos.is_constant (&cbitpos))
20702 return NULL_TREE;
20704 *value = 0;
20705 if (offset != NULL)
20707 if (!tree_fits_shwi_p (offset))
20708 return NULL_TREE;
20709 *value = tree_to_shwi (offset);
20711 if (cbitpos != 0)
20712 *value += cbitpos / BITS_PER_UNIT;
20714 return cvar;
20717 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20718 data attribute for a variable or a parameter. We generate the
20719 DW_AT_const_value attribute only in those cases where the given variable
20720 or parameter does not have a true "location" either in memory or in a
20721 register. This can happen (for example) when a constant is passed as an
20722 actual argument in a call to an inline function. (It's possible that
20723 these things can crop up in other ways also.) Note that one type of
20724 constant value which can be passed into an inlined function is a constant
20725 pointer. This can happen for example if an actual argument in an inlined
20726 function call evaluates to a compile-time constant address.
20728 CACHE_P is true if it is worth caching the location list for DECL,
20729 so that future calls can reuse it rather than regenerate it from scratch.
20730 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20731 since we will need to refer to them each time the function is inlined. */
20733 static bool
20734 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20736 rtx rtl;
20737 dw_loc_list_ref list;
20738 var_loc_list *loc_list;
20739 cached_dw_loc_list *cache;
20741 if (early_dwarf)
20742 return false;
20744 if (TREE_CODE (decl) == ERROR_MARK)
20745 return false;
20747 if (get_AT (die, DW_AT_location)
20748 || get_AT (die, DW_AT_const_value))
20749 return true;
20751 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20752 || TREE_CODE (decl) == RESULT_DECL);
20754 /* Try to get some constant RTL for this decl, and use that as the value of
20755 the location. */
20757 rtl = rtl_for_decl_location (decl);
20758 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20759 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20760 return true;
20762 /* See if we have single element location list that is equivalent to
20763 a constant value. That way we are better to use add_const_value_attribute
20764 rather than expanding constant value equivalent. */
20765 loc_list = lookup_decl_loc (decl);
20766 if (loc_list
20767 && loc_list->first
20768 && loc_list->first->next == NULL
20769 && NOTE_P (loc_list->first->loc)
20770 && NOTE_VAR_LOCATION (loc_list->first->loc)
20771 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20773 struct var_loc_node *node;
20775 node = loc_list->first;
20776 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20777 if (GET_CODE (rtl) == EXPR_LIST)
20778 rtl = XEXP (rtl, 0);
20779 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20780 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20781 return true;
20783 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20784 list several times. See if we've already cached the contents. */
20785 list = NULL;
20786 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20787 cache_p = false;
20788 if (cache_p)
20790 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20791 if (cache)
20792 list = cache->loc_list;
20794 if (list == NULL)
20796 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20797 NULL);
20798 /* It is usually worth caching this result if the decl is from
20799 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20800 if (cache_p && list && list->dw_loc_next)
20802 cached_dw_loc_list **slot
20803 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20804 DECL_UID (decl),
20805 INSERT);
20806 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20807 cache->decl_id = DECL_UID (decl);
20808 cache->loc_list = list;
20809 *slot = cache;
20812 if (list)
20814 add_AT_location_description (die, DW_AT_location, list);
20815 return true;
20817 /* None of that worked, so it must not really have a location;
20818 try adding a constant value attribute from the DECL_INITIAL. */
20819 return tree_add_const_value_attribute_for_decl (die, decl);
20822 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20823 attribute is the const value T. */
20825 static bool
20826 tree_add_const_value_attribute (dw_die_ref die, tree t)
20828 tree init;
20829 tree type = TREE_TYPE (t);
20830 rtx rtl;
20832 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20833 return false;
20835 init = t;
20836 gcc_assert (!DECL_P (init));
20838 if (TREE_CODE (init) == INTEGER_CST)
20840 if (tree_fits_uhwi_p (init))
20842 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20843 return true;
20845 if (tree_fits_shwi_p (init))
20847 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20848 return true;
20851 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20852 symbols. */
20853 rtl = rtl_for_decl_init (init, type);
20854 if (rtl && !early_dwarf)
20855 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
20856 /* If the host and target are sane, try harder. */
20857 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20858 && initializer_constant_valid_p (init, type))
20860 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20861 if (size > 0 && (int) size == size)
20863 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20865 if (native_encode_initializer (init, array, size) == size)
20867 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20868 return true;
20870 ggc_free (array);
20873 return false;
20876 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20877 attribute is the const value of T, where T is an integral constant
20878 variable with static storage duration
20879 (so it can't be a PARM_DECL or a RESULT_DECL). */
20881 static bool
20882 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20885 if (!decl
20886 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20887 || (VAR_P (decl) && !TREE_STATIC (decl)))
20888 return false;
20890 if (TREE_READONLY (decl)
20891 && ! TREE_THIS_VOLATILE (decl)
20892 && DECL_INITIAL (decl))
20893 /* OK */;
20894 else
20895 return false;
20897 /* Don't add DW_AT_const_value if abstract origin already has one. */
20898 if (get_AT (var_die, DW_AT_const_value))
20899 return false;
20901 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20904 /* Convert the CFI instructions for the current function into a
20905 location list. This is used for DW_AT_frame_base when we targeting
20906 a dwarf2 consumer that does not support the dwarf3
20907 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20908 expressions. */
20910 static dw_loc_list_ref
20911 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20913 int ix;
20914 dw_fde_ref fde;
20915 dw_loc_list_ref list, *list_tail;
20916 dw_cfi_ref cfi;
20917 dw_cfa_location last_cfa, next_cfa;
20918 const char *start_label, *last_label, *section;
20919 dw_cfa_location remember;
20921 fde = cfun->fde;
20922 gcc_assert (fde != NULL);
20924 section = secname_for_decl (current_function_decl);
20925 list_tail = &list;
20926 list = NULL;
20928 memset (&next_cfa, 0, sizeof (next_cfa));
20929 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
20930 remember = next_cfa;
20932 start_label = fde->dw_fde_begin;
20934 /* ??? Bald assumption that the CIE opcode list does not contain
20935 advance opcodes. */
20936 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20937 lookup_cfa_1 (cfi, &next_cfa, &remember);
20939 last_cfa = next_cfa;
20940 last_label = start_label;
20942 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20944 /* If the first partition contained no CFI adjustments, the
20945 CIE opcodes apply to the whole first partition. */
20946 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20947 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20948 list_tail =&(*list_tail)->dw_loc_next;
20949 start_label = last_label = fde->dw_fde_second_begin;
20952 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20954 switch (cfi->dw_cfi_opc)
20956 case DW_CFA_set_loc:
20957 case DW_CFA_advance_loc1:
20958 case DW_CFA_advance_loc2:
20959 case DW_CFA_advance_loc4:
20960 if (!cfa_equal_p (&last_cfa, &next_cfa))
20962 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20963 start_label, 0, last_label, 0, section);
20965 list_tail = &(*list_tail)->dw_loc_next;
20966 last_cfa = next_cfa;
20967 start_label = last_label;
20969 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20970 break;
20972 case DW_CFA_advance_loc:
20973 /* The encoding is complex enough that we should never emit this. */
20974 gcc_unreachable ();
20976 default:
20977 lookup_cfa_1 (cfi, &next_cfa, &remember);
20978 break;
20980 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20982 if (!cfa_equal_p (&last_cfa, &next_cfa))
20984 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20985 start_label, 0, last_label, 0, section);
20987 list_tail = &(*list_tail)->dw_loc_next;
20988 last_cfa = next_cfa;
20989 start_label = last_label;
20991 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20992 start_label, 0, fde->dw_fde_end, 0, section);
20993 list_tail = &(*list_tail)->dw_loc_next;
20994 start_label = last_label = fde->dw_fde_second_begin;
20998 if (!cfa_equal_p (&last_cfa, &next_cfa))
21000 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21001 start_label, 0, last_label, 0, section);
21002 list_tail = &(*list_tail)->dw_loc_next;
21003 start_label = last_label;
21006 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21007 start_label, 0,
21008 fde->dw_fde_second_begin
21009 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21010 section);
21012 maybe_gen_llsym (list);
21014 return list;
21017 /* Compute a displacement from the "steady-state frame pointer" to the
21018 frame base (often the same as the CFA), and store it in
21019 frame_pointer_fb_offset. OFFSET is added to the displacement
21020 before the latter is negated. */
21022 static void
21023 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21025 rtx reg, elim;
21027 #ifdef FRAME_POINTER_CFA_OFFSET
21028 reg = frame_pointer_rtx;
21029 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21030 #else
21031 reg = arg_pointer_rtx;
21032 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21033 #endif
21035 elim = (ira_use_lra_p
21036 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21037 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21038 elim = strip_offset_and_add (elim, &offset);
21040 frame_pointer_fb_offset = -offset;
21042 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21043 in which to eliminate. This is because it's stack pointer isn't
21044 directly accessible as a register within the ISA. To work around
21045 this, assume that while we cannot provide a proper value for
21046 frame_pointer_fb_offset, we won't need one either. We can use
21047 hard frame pointer in debug info even if frame pointer isn't used
21048 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21049 which uses the DW_AT_frame_base attribute, not hard frame pointer
21050 directly. */
21051 frame_pointer_fb_offset_valid
21052 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21055 /* Generate a DW_AT_name attribute given some string value to be included as
21056 the value of the attribute. */
21058 void
21059 add_name_attribute (dw_die_ref die, const char *name_string)
21061 if (name_string != NULL && *name_string != 0)
21063 if (demangle_name_func)
21064 name_string = (*demangle_name_func) (name_string);
21066 add_AT_string (die, DW_AT_name, name_string);
21070 /* Generate a DW_AT_name attribute given some string value representing a
21071 file or filepath to be included as value of the attribute. */
21072 static void
21073 add_filename_attribute (dw_die_ref die, const char *name_string)
21075 if (name_string != NULL && *name_string != 0)
21076 add_filepath_AT_string (die, DW_AT_name, name_string);
21079 /* Generate a DW_AT_description attribute given some string value to be included
21080 as the value of the attribute. */
21082 static void
21083 add_desc_attribute (dw_die_ref die, const char *name_string)
21085 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21086 return;
21088 if (name_string == NULL || *name_string == 0)
21089 return;
21091 if (demangle_name_func)
21092 name_string = (*demangle_name_func) (name_string);
21094 add_AT_string (die, DW_AT_description, name_string);
21097 /* Generate a DW_AT_description attribute given some decl to be included
21098 as the value of the attribute. */
21100 static void
21101 add_desc_attribute (dw_die_ref die, tree decl)
21103 tree decl_name;
21105 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21106 return;
21108 if (decl == NULL_TREE || !DECL_P (decl))
21109 return;
21110 decl_name = DECL_NAME (decl);
21112 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21114 const char *name = dwarf2_name (decl, 0);
21115 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21117 else
21119 char *desc = print_generic_expr_to_str (decl);
21120 add_desc_attribute (die, desc);
21121 free (desc);
21125 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21126 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21127 of TYPE accordingly.
21129 ??? This is a temporary measure until after we're able to generate
21130 regular DWARF for the complex Ada type system. */
21132 static void
21133 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21134 dw_die_ref context_die)
21136 tree dtype;
21137 dw_die_ref dtype_die;
21139 if (!lang_hooks.types.descriptive_type)
21140 return;
21142 dtype = lang_hooks.types.descriptive_type (type);
21143 if (!dtype)
21144 return;
21146 dtype_die = lookup_type_die (dtype);
21147 if (!dtype_die)
21149 gen_type_die (dtype, context_die);
21150 dtype_die = lookup_type_die (dtype);
21151 gcc_assert (dtype_die);
21154 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21157 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21159 static const char *
21160 comp_dir_string (void)
21162 const char *wd;
21163 char *wd_plus_sep = NULL;
21164 static const char *cached_wd = NULL;
21166 if (cached_wd != NULL)
21167 return cached_wd;
21169 wd = get_src_pwd ();
21170 if (wd == NULL)
21171 return NULL;
21173 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21175 size_t wdlen = strlen (wd);
21176 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21177 strcpy (wd_plus_sep, wd);
21178 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21179 wd_plus_sep [wdlen + 1] = 0;
21180 wd = wd_plus_sep;
21183 cached_wd = remap_debug_filename (wd);
21185 /* remap_debug_filename can just pass through wd or return a new gc string.
21186 These two types can't be both stored in a GTY(())-tagged string, but since
21187 the cached value lives forever just copy it if needed. */
21188 if (cached_wd != wd)
21190 cached_wd = xstrdup (cached_wd);
21191 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21192 free (wd_plus_sep);
21195 return cached_wd;
21198 /* Generate a DW_AT_comp_dir attribute for DIE. */
21200 static void
21201 add_comp_dir_attribute (dw_die_ref die)
21203 const char * wd = comp_dir_string ();
21204 if (wd != NULL)
21205 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21208 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21209 pointer computation, ...), output a representation for that bound according
21210 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21211 loc_list_from_tree for the meaning of CONTEXT. */
21213 static void
21214 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21215 int forms, struct loc_descr_context *context)
21217 dw_die_ref context_die, decl_die = NULL;
21218 dw_loc_list_ref list;
21219 bool strip_conversions = true;
21220 bool placeholder_seen = false;
21222 while (strip_conversions)
21223 switch (TREE_CODE (value))
21225 case ERROR_MARK:
21226 case SAVE_EXPR:
21227 return;
21229 CASE_CONVERT:
21230 case VIEW_CONVERT_EXPR:
21231 value = TREE_OPERAND (value, 0);
21232 break;
21234 default:
21235 strip_conversions = false;
21236 break;
21239 /* If possible and permitted, output the attribute as a constant. */
21240 if ((forms & dw_scalar_form_constant) != 0
21241 && TREE_CODE (value) == INTEGER_CST)
21243 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21245 /* If HOST_WIDE_INT is big enough then represent the bound as
21246 a constant value. We need to choose a form based on
21247 whether the type is signed or unsigned. We cannot just
21248 call add_AT_unsigned if the value itself is positive
21249 (add_AT_unsigned might add the unsigned value encoded as
21250 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21251 bounds type and then sign extend any unsigned values found
21252 for signed types. This is needed only for
21253 DW_AT_{lower,upper}_bound, since for most other attributes,
21254 consumers will treat DW_FORM_data[1248] as unsigned values,
21255 regardless of the underlying type. */
21256 if (prec <= HOST_BITS_PER_WIDE_INT
21257 || tree_fits_uhwi_p (value))
21259 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21260 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21261 else
21262 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21264 else if (dwarf_version >= 5
21265 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21266 /* Otherwise represent the bound as an unsigned value with
21267 the precision of its type. The precision and signedness
21268 of the type will be necessary to re-interpret it
21269 unambiguously. */
21270 add_AT_wide (die, attr, wi::to_wide (value));
21271 else
21273 rtx v = immed_wide_int_const (wi::to_wide (value),
21274 TYPE_MODE (TREE_TYPE (value)));
21275 dw_loc_descr_ref loc
21276 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21277 VAR_INIT_STATUS_INITIALIZED);
21278 if (loc)
21279 add_AT_loc (die, attr, loc);
21281 return;
21284 /* Otherwise, if it's possible and permitted too, output a reference to
21285 another DIE. */
21286 if ((forms & dw_scalar_form_reference) != 0)
21288 tree decl = NULL_TREE;
21290 /* Some type attributes reference an outer type. For instance, the upper
21291 bound of an array may reference an embedding record (this happens in
21292 Ada). */
21293 if (TREE_CODE (value) == COMPONENT_REF
21294 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21295 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21296 decl = TREE_OPERAND (value, 1);
21298 else if (VAR_P (value)
21299 || TREE_CODE (value) == PARM_DECL
21300 || TREE_CODE (value) == RESULT_DECL)
21301 decl = value;
21303 if (decl != NULL_TREE)
21305 decl_die = lookup_decl_die (decl);
21307 /* ??? Can this happen, or should the variable have been bound
21308 first? Probably it can, since I imagine that we try to create
21309 the types of parameters in the order in which they exist in
21310 the list, and won't have created a forward reference to a
21311 later parameter. */
21312 if (decl_die != NULL)
21314 if (get_AT (decl_die, DW_AT_location)
21315 || get_AT (decl_die, DW_AT_data_member_location)
21316 || get_AT (decl_die, DW_AT_data_bit_offset)
21317 || get_AT (decl_die, DW_AT_const_value))
21319 add_AT_die_ref (die, attr, decl_die);
21320 return;
21326 /* Last chance: try to create a stack operation procedure to evaluate the
21327 value. Do nothing if even that is not possible or permitted. */
21328 if ((forms & dw_scalar_form_exprloc) == 0)
21329 return;
21331 list = loc_list_from_tree (value, 2, context);
21332 if (context && context->placeholder_arg)
21334 placeholder_seen = context->placeholder_seen;
21335 context->placeholder_seen = false;
21337 if (list == NULL || single_element_loc_list_p (list))
21339 /* If this attribute is not a reference nor constant, it is
21340 a DWARF expression rather than location description. For that
21341 loc_list_from_tree (value, 0, &context) is needed. */
21342 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21343 if (list2 && single_element_loc_list_p (list2))
21345 if (placeholder_seen)
21347 struct dwarf_procedure_info dpi;
21348 dpi.fndecl = NULL_TREE;
21349 dpi.args_count = 1;
21350 if (!resolve_args_picking (list2->expr, 1, &dpi))
21351 return;
21353 add_AT_loc (die, attr, list2->expr);
21354 return;
21358 /* If that failed to give a single element location list, fall back to
21359 outputting this as a reference... still if permitted. */
21360 if (list == NULL
21361 || (forms & dw_scalar_form_reference) == 0
21362 || placeholder_seen)
21363 return;
21365 if (!decl_die)
21367 if (current_function_decl == 0)
21368 context_die = comp_unit_die ();
21369 else
21370 context_die = lookup_decl_die (current_function_decl);
21372 decl_die = new_die (DW_TAG_variable, context_die, value);
21373 add_AT_flag (decl_die, DW_AT_artificial, 1);
21374 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21375 context_die);
21378 add_AT_location_description (decl_die, DW_AT_location, list);
21379 add_AT_die_ref (die, attr, decl_die);
21382 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21383 default. */
21385 static int
21386 lower_bound_default (void)
21388 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21390 case DW_LANG_C:
21391 case DW_LANG_C89:
21392 case DW_LANG_C99:
21393 case DW_LANG_C11:
21394 case DW_LANG_C_plus_plus:
21395 case DW_LANG_C_plus_plus_11:
21396 case DW_LANG_C_plus_plus_14:
21397 case DW_LANG_ObjC:
21398 case DW_LANG_ObjC_plus_plus:
21399 return 0;
21400 case DW_LANG_Fortran77:
21401 case DW_LANG_Fortran90:
21402 case DW_LANG_Fortran95:
21403 case DW_LANG_Fortran03:
21404 case DW_LANG_Fortran08:
21405 return 1;
21406 case DW_LANG_UPC:
21407 case DW_LANG_D:
21408 case DW_LANG_Python:
21409 return dwarf_version >= 4 ? 0 : -1;
21410 case DW_LANG_Ada95:
21411 case DW_LANG_Ada83:
21412 case DW_LANG_Cobol74:
21413 case DW_LANG_Cobol85:
21414 case DW_LANG_Modula2:
21415 case DW_LANG_PLI:
21416 return dwarf_version >= 4 ? 1 : -1;
21417 default:
21418 return -1;
21422 /* Given a tree node describing an array bound (either lower or upper) output
21423 a representation for that bound. */
21425 static void
21426 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21427 tree bound, struct loc_descr_context *context)
21429 int dflt;
21431 while (1)
21432 switch (TREE_CODE (bound))
21434 /* Strip all conversions. */
21435 CASE_CONVERT:
21436 case VIEW_CONVERT_EXPR:
21437 bound = TREE_OPERAND (bound, 0);
21438 break;
21440 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21441 are even omitted when they are the default. */
21442 case INTEGER_CST:
21443 /* If the value for this bound is the default one, we can even omit the
21444 attribute. */
21445 if (bound_attr == DW_AT_lower_bound
21446 && tree_fits_shwi_p (bound)
21447 && (dflt = lower_bound_default ()) != -1
21448 && tree_to_shwi (bound) == dflt)
21449 return;
21451 /* FALLTHRU */
21453 default:
21454 /* Let GNAT encodings do the magic for self-referential bounds. */
21455 if (is_ada ()
21456 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21457 && contains_placeholder_p (bound))
21458 return;
21460 add_scalar_info (subrange_die, bound_attr, bound,
21461 dw_scalar_form_constant
21462 | dw_scalar_form_exprloc
21463 | dw_scalar_form_reference,
21464 context);
21465 return;
21469 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21470 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21472 This function reuses previously set type and bound information if
21473 available. */
21475 static void
21476 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21478 dw_die_ref child = type_die->die_child;
21479 struct array_descr_info info;
21480 int dimension_number;
21482 if (lang_hooks.types.get_array_descr_info)
21484 memset (&info, 0, sizeof (info));
21485 if (lang_hooks.types.get_array_descr_info (type, &info))
21486 /* Fortran sometimes emits array types with no dimension. */
21487 gcc_assert (info.ndimensions >= 0
21488 && info.ndimensions
21489 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21491 else
21492 info.ndimensions = 0;
21494 for (dimension_number = 0;
21495 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21496 type = TREE_TYPE (type), dimension_number++)
21498 tree domain = TYPE_DOMAIN (type);
21500 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21501 break;
21503 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21504 and (in GNU C only) variable bounds. Handle all three forms
21505 here. */
21507 /* Find and reuse a previously generated DW_TAG_subrange_type if
21508 available.
21510 For multi-dimensional arrays, as we iterate through the
21511 various dimensions in the enclosing for loop above, we also
21512 iterate through the DIE children and pick at each
21513 DW_TAG_subrange_type previously generated (if available).
21514 Each child DW_TAG_subrange_type DIE describes the range of
21515 the current dimension. At this point we should have as many
21516 DW_TAG_subrange_type's as we have dimensions in the
21517 array. */
21518 dw_die_ref subrange_die = NULL;
21519 if (child)
21520 while (1)
21522 child = child->die_sib;
21523 if (child->die_tag == DW_TAG_subrange_type)
21524 subrange_die = child;
21525 if (child == type_die->die_child)
21527 /* If we wrapped around, stop looking next time. */
21528 child = NULL;
21529 break;
21531 if (child->die_tag == DW_TAG_subrange_type)
21532 break;
21534 if (!subrange_die)
21535 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21537 if (domain)
21539 /* We have an array type with specified bounds. */
21540 tree lower = TYPE_MIN_VALUE (domain);
21541 tree upper = TYPE_MAX_VALUE (domain);
21542 tree index_type = TREE_TYPE (domain);
21544 if (dimension_number <= info.ndimensions - 1)
21546 lower = info.dimen[dimension_number].lower_bound;
21547 upper = info.dimen[dimension_number].upper_bound;
21548 index_type = info.dimen[dimension_number].bounds_type;
21551 /* Define the index type. */
21552 if (index_type && !get_AT (subrange_die, DW_AT_type))
21553 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21554 false, type_die);
21556 /* ??? If upper is NULL, the array has unspecified length,
21557 but it does have a lower bound. This happens with Fortran
21558 dimension arr(N:*)
21559 Since the debugger is definitely going to need to know N
21560 to produce useful results, go ahead and output the lower
21561 bound solo, and hope the debugger can cope. */
21563 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21564 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21566 if (!get_AT (subrange_die, DW_AT_upper_bound)
21567 && !get_AT (subrange_die, DW_AT_count))
21569 if (upper)
21570 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21571 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21572 /* Zero-length array. */
21573 add_bound_info (subrange_die, DW_AT_count,
21574 build_int_cst (TREE_TYPE (lower), 0), NULL);
21578 /* Otherwise we have an array type with an unspecified length. The
21579 DWARF-2 spec does not say how to handle this; let's just leave out the
21580 bounds. */
21584 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21586 static void
21587 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21589 dw_die_ref decl_die;
21590 HOST_WIDE_INT size;
21592 switch (TREE_CODE (tree_node))
21594 case ERROR_MARK:
21595 size = 0;
21596 break;
21597 case ENUMERAL_TYPE:
21598 case RECORD_TYPE:
21599 case UNION_TYPE:
21600 case QUAL_UNION_TYPE:
21601 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21602 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21604 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21605 return;
21607 size = int_size_in_bytes (tree_node);
21608 break;
21609 case FIELD_DECL:
21610 /* For a data member of a struct or union, the DW_AT_byte_size is
21611 generally given as the number of bytes normally allocated for an
21612 object of the *declared* type of the member itself. This is true
21613 even for bit-fields. */
21614 size = int_size_in_bytes (field_type (tree_node));
21615 break;
21616 default:
21617 gcc_unreachable ();
21620 /* Note that `size' might be -1 when we get to this point. If it is, that
21621 indicates that the byte size of the entity in question is variable. */
21622 if (size >= 0)
21623 add_AT_unsigned (die, DW_AT_byte_size, size);
21625 /* Support for dynamically-sized objects was introduced in DWARF3. */
21626 else if (TYPE_P (tree_node)
21627 && (dwarf_version >= 3 || !dwarf_strict)
21628 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21630 struct loc_descr_context ctx = {
21631 const_cast<tree> (tree_node), /* context_type */
21632 NULL_TREE, /* base_decl */
21633 NULL, /* dpi */
21634 false, /* placeholder_arg */
21635 false, /* placeholder_seen */
21636 false /* strict_signedness */
21639 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21640 add_scalar_info (die, DW_AT_byte_size, tree_size,
21641 dw_scalar_form_constant
21642 | dw_scalar_form_exprloc
21643 | dw_scalar_form_reference,
21644 &ctx);
21648 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21649 alignment. */
21651 static void
21652 add_alignment_attribute (dw_die_ref die, tree tree_node)
21654 if (dwarf_version < 5 && dwarf_strict)
21655 return;
21657 unsigned align;
21659 if (DECL_P (tree_node))
21661 if (!DECL_USER_ALIGN (tree_node))
21662 return;
21664 align = DECL_ALIGN_UNIT (tree_node);
21666 else if (TYPE_P (tree_node))
21668 if (!TYPE_USER_ALIGN (tree_node))
21669 return;
21671 align = TYPE_ALIGN_UNIT (tree_node);
21673 else
21674 gcc_unreachable ();
21676 add_AT_unsigned (die, DW_AT_alignment, align);
21679 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21680 which specifies the distance in bits from the highest order bit of the
21681 "containing object" for the bit-field to the highest order bit of the
21682 bit-field itself.
21684 For any given bit-field, the "containing object" is a hypothetical object
21685 (of some integral or enum type) within which the given bit-field lives. The
21686 type of this hypothetical "containing object" is always the same as the
21687 declared type of the individual bit-field itself. The determination of the
21688 exact location of the "containing object" for a bit-field is rather
21689 complicated. It's handled by the `field_byte_offset' function (above).
21691 Note that it is the size (in bytes) of the hypothetical "containing object"
21692 which will be given in the DW_AT_byte_size attribute for this bit-field.
21693 (See `byte_size_attribute' above). */
21695 static inline void
21696 add_bit_offset_attribute (dw_die_ref die, tree decl)
21698 HOST_WIDE_INT object_offset_in_bytes;
21699 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21700 HOST_WIDE_INT bitpos_int;
21701 HOST_WIDE_INT highest_order_object_bit_offset;
21702 HOST_WIDE_INT highest_order_field_bit_offset;
21703 HOST_WIDE_INT bit_offset;
21705 /* The containing object is within the DECL_CONTEXT. */
21706 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21708 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21710 /* Must be a field and a bit field. */
21711 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21713 /* We can't yet handle bit-fields whose offsets are variable, so if we
21714 encounter such things, just return without generating any attribute
21715 whatsoever. Likewise for variable or too large size. */
21716 if (! tree_fits_shwi_p (bit_position (decl))
21717 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21718 return;
21720 bitpos_int = int_bit_position (decl);
21722 /* Note that the bit offset is always the distance (in bits) from the
21723 highest-order bit of the "containing object" to the highest-order bit of
21724 the bit-field itself. Since the "high-order end" of any object or field
21725 is different on big-endian and little-endian machines, the computation
21726 below must take account of these differences. */
21727 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21728 highest_order_field_bit_offset = bitpos_int;
21730 if (! BYTES_BIG_ENDIAN)
21732 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21733 highest_order_object_bit_offset +=
21734 simple_type_size_in_bits (original_type);
21737 bit_offset
21738 = (! BYTES_BIG_ENDIAN
21739 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21740 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21742 if (bit_offset < 0)
21743 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21744 else
21745 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21748 /* For a FIELD_DECL node which represents a bit field, output an attribute
21749 which specifies the length in bits of the given field. */
21751 static inline void
21752 add_bit_size_attribute (dw_die_ref die, tree decl)
21754 /* Must be a field and a bit field. */
21755 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21756 && DECL_BIT_FIELD_TYPE (decl));
21758 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21759 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21762 /* If the compiled language is ANSI C, then add a 'prototyped'
21763 attribute, if arg types are given for the parameters of a function. */
21765 static inline void
21766 add_prototyped_attribute (dw_die_ref die, tree func_type)
21768 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21770 case DW_LANG_C:
21771 case DW_LANG_C89:
21772 case DW_LANG_C99:
21773 case DW_LANG_C11:
21774 case DW_LANG_ObjC:
21775 if (prototype_p (func_type))
21776 add_AT_flag (die, DW_AT_prototyped, 1);
21777 break;
21778 default:
21779 break;
21783 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21784 by looking in the type declaration, the object declaration equate table or
21785 the block mapping. */
21787 static inline void
21788 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21790 dw_die_ref origin_die = NULL;
21792 /* For late LTO debug output we want to refer directly to the abstract
21793 DIE in the early debug rather to the possibly existing concrete
21794 instance and avoid creating that just for this purpose. */
21795 sym_off_pair *desc;
21796 if (in_lto_p
21797 && external_die_map
21798 && (desc = external_die_map->get (origin)))
21800 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21801 desc->sym, desc->off);
21802 return;
21805 if (DECL_P (origin))
21806 origin_die = lookup_decl_die (origin);
21807 else if (TYPE_P (origin))
21808 origin_die = lookup_type_die (origin);
21809 else if (TREE_CODE (origin) == BLOCK)
21810 origin_die = lookup_block_die (origin);
21812 /* XXX: Functions that are never lowered don't always have correct block
21813 trees (in the case of java, they simply have no block tree, in some other
21814 languages). For these functions, there is nothing we can really do to
21815 output correct debug info for inlined functions in all cases. Rather
21816 than die, we'll just produce deficient debug info now, in that we will
21817 have variables without a proper abstract origin. In the future, when all
21818 functions are lowered, we should re-add a gcc_assert (origin_die)
21819 here. */
21821 if (origin_die)
21823 dw_attr_node *a;
21824 /* Like above, if we already created a concrete instance DIE
21825 do not use that for the abstract origin but the early DIE
21826 if present. */
21827 if (in_lto_p
21828 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21829 origin_die = AT_ref (a);
21830 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21834 /* We do not currently support the pure_virtual attribute. */
21836 static inline void
21837 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21839 if (DECL_VINDEX (func_decl))
21841 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21843 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21844 add_AT_loc (die, DW_AT_vtable_elem_location,
21845 new_loc_descr (DW_OP_constu,
21846 tree_to_shwi (DECL_VINDEX (func_decl)),
21847 0));
21849 /* GNU extension: Record what type this method came from originally. */
21850 if (debug_info_level > DINFO_LEVEL_TERSE
21851 && DECL_CONTEXT (func_decl))
21852 add_AT_die_ref (die, DW_AT_containing_type,
21853 lookup_type_die (DECL_CONTEXT (func_decl)));
21857 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21858 given decl. This used to be a vendor extension until after DWARF 4
21859 standardized it. */
21861 static void
21862 add_linkage_attr (dw_die_ref die, tree decl)
21864 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21866 /* Mimic what assemble_name_raw does with a leading '*'. */
21867 if (name[0] == '*')
21868 name = &name[1];
21870 if (dwarf_version >= 4)
21871 add_AT_string (die, DW_AT_linkage_name, name);
21872 else
21873 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21876 /* Add source coordinate attributes for the given decl. */
21878 static void
21879 add_src_coords_attributes (dw_die_ref die, tree decl)
21881 expanded_location s;
21883 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21884 return;
21885 s = expand_location (DECL_SOURCE_LOCATION (decl));
21886 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21887 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21888 if (debug_column_info && s.column)
21889 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21892 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21894 static void
21895 add_linkage_name_raw (dw_die_ref die, tree decl)
21897 /* Defer until we have an assembler name set. */
21898 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21900 limbo_die_node *asm_name;
21902 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21903 asm_name->die = die;
21904 asm_name->created_for = decl;
21905 asm_name->next = deferred_asm_name;
21906 deferred_asm_name = asm_name;
21908 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21909 add_linkage_attr (die, decl);
21912 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21914 static void
21915 add_linkage_name (dw_die_ref die, tree decl)
21917 if (debug_info_level > DINFO_LEVEL_NONE
21918 && VAR_OR_FUNCTION_DECL_P (decl)
21919 && TREE_PUBLIC (decl)
21920 && !(VAR_P (decl) && DECL_REGISTER (decl))
21921 && die->die_tag != DW_TAG_member)
21922 add_linkage_name_raw (die, decl);
21925 /* Add a DW_AT_name attribute and source coordinate attribute for the
21926 given decl, but only if it actually has a name. */
21928 static void
21929 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21930 bool no_linkage_name)
21932 tree decl_name;
21934 decl_name = DECL_NAME (decl);
21935 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21937 const char *name = dwarf2_name (decl, 0);
21938 if (name)
21939 add_name_attribute (die, name);
21940 else
21941 add_desc_attribute (die, decl);
21943 if (! DECL_ARTIFICIAL (decl))
21944 add_src_coords_attributes (die, decl);
21946 if (!no_linkage_name)
21947 add_linkage_name (die, decl);
21949 else
21950 add_desc_attribute (die, decl);
21952 #ifdef VMS_DEBUGGING_INFO
21953 /* Get the function's name, as described by its RTL. This may be different
21954 from the DECL_NAME name used in the source file. */
21955 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21957 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21958 XEXP (DECL_RTL (decl), 0), false);
21959 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21961 #endif /* VMS_DEBUGGING_INFO */
21964 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21966 static void
21967 add_discr_value (dw_die_ref die, dw_discr_value *value)
21969 dw_attr_node attr;
21971 attr.dw_attr = DW_AT_discr_value;
21972 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21973 attr.dw_attr_val.val_entry = NULL;
21974 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21975 if (value->pos)
21976 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21977 else
21978 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21979 add_dwarf_attr (die, &attr);
21982 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21984 static void
21985 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21987 dw_attr_node attr;
21989 attr.dw_attr = DW_AT_discr_list;
21990 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21991 attr.dw_attr_val.val_entry = NULL;
21992 attr.dw_attr_val.v.val_discr_list = discr_list;
21993 add_dwarf_attr (die, &attr);
21996 static inline dw_discr_list_ref
21997 AT_discr_list (dw_attr_node *attr)
21999 return attr->dw_attr_val.v.val_discr_list;
22002 #ifdef VMS_DEBUGGING_INFO
22003 /* Output the debug main pointer die for VMS */
22005 void
22006 dwarf2out_vms_debug_main_pointer (void)
22008 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22009 dw_die_ref die;
22011 /* Allocate the VMS debug main subprogram die. */
22012 die = new_die_raw (DW_TAG_subprogram);
22013 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22014 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22015 current_function_funcdef_no);
22016 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22018 /* Make it the first child of comp_unit_die (). */
22019 die->die_parent = comp_unit_die ();
22020 if (comp_unit_die ()->die_child)
22022 die->die_sib = comp_unit_die ()->die_child->die_sib;
22023 comp_unit_die ()->die_child->die_sib = die;
22025 else
22027 die->die_sib = die;
22028 comp_unit_die ()->die_child = die;
22031 #endif /* VMS_DEBUGGING_INFO */
22033 /* walk_tree helper function for uses_local_type, below. */
22035 static tree
22036 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22038 if (!TYPE_P (*tp))
22039 *walk_subtrees = 0;
22040 else
22042 tree name = TYPE_NAME (*tp);
22043 if (name && DECL_P (name) && decl_function_context (name))
22044 return *tp;
22046 return NULL_TREE;
22049 /* If TYPE involves a function-local type (including a local typedef to a
22050 non-local type), returns that type; otherwise returns NULL_TREE. */
22052 static tree
22053 uses_local_type (tree type)
22055 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22056 return used;
22059 /* Return the DIE for the scope that immediately contains this type.
22060 Non-named types that do not involve a function-local type get global
22061 scope. Named types nested in namespaces or other types get their
22062 containing scope. All other types (i.e. function-local named types) get
22063 the current active scope. */
22065 static dw_die_ref
22066 scope_die_for (tree t, dw_die_ref context_die)
22068 dw_die_ref scope_die = NULL;
22069 tree containing_scope;
22071 /* Non-types always go in the current scope. */
22072 gcc_assert (TYPE_P (t));
22074 /* Use the scope of the typedef, rather than the scope of the type
22075 it refers to. */
22076 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22077 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22078 else
22079 containing_scope = TYPE_CONTEXT (t);
22081 /* Use the containing namespace if there is one. */
22082 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22084 if (context_die == lookup_decl_die (containing_scope))
22085 /* OK */;
22086 else if (debug_info_level > DINFO_LEVEL_TERSE)
22087 context_die = get_context_die (containing_scope);
22088 else
22089 containing_scope = NULL_TREE;
22092 /* Ignore function type "scopes" from the C frontend. They mean that
22093 a tagged type is local to a parmlist of a function declarator, but
22094 that isn't useful to DWARF. */
22095 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22096 containing_scope = NULL_TREE;
22098 if (SCOPE_FILE_SCOPE_P (containing_scope))
22100 /* If T uses a local type keep it local as well, to avoid references
22101 to function-local DIEs from outside the function. */
22102 if (current_function_decl && uses_local_type (t))
22103 scope_die = context_die;
22104 else
22105 scope_die = comp_unit_die ();
22107 else if (TYPE_P (containing_scope))
22109 /* For types, we can just look up the appropriate DIE. */
22110 if (debug_info_level > DINFO_LEVEL_TERSE)
22111 scope_die = get_context_die (containing_scope);
22112 else
22114 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22115 if (scope_die == NULL)
22116 scope_die = comp_unit_die ();
22119 else
22120 scope_die = context_die;
22122 return scope_die;
22125 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22127 static inline int
22128 local_scope_p (dw_die_ref context_die)
22130 for (; context_die; context_die = context_die->die_parent)
22131 if (context_die->die_tag == DW_TAG_inlined_subroutine
22132 || context_die->die_tag == DW_TAG_subprogram)
22133 return 1;
22135 return 0;
22138 /* Returns nonzero if CONTEXT_DIE is a class. */
22140 static inline int
22141 class_scope_p (dw_die_ref context_die)
22143 return (context_die
22144 && (context_die->die_tag == DW_TAG_structure_type
22145 || context_die->die_tag == DW_TAG_class_type
22146 || context_die->die_tag == DW_TAG_interface_type
22147 || context_die->die_tag == DW_TAG_union_type));
22150 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22151 whether or not to treat a DIE in this context as a declaration. */
22153 static inline int
22154 class_or_namespace_scope_p (dw_die_ref context_die)
22156 return (class_scope_p (context_die)
22157 || (context_die && context_die->die_tag == DW_TAG_namespace));
22160 /* Many forms of DIEs require a "type description" attribute. This
22161 routine locates the proper "type descriptor" die for the type given
22162 by 'type' plus any additional qualifiers given by 'cv_quals', and
22163 adds a DW_AT_type attribute below the given die. */
22165 static void
22166 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22167 bool reverse, dw_die_ref context_die)
22169 enum tree_code code = TREE_CODE (type);
22170 dw_die_ref type_die = NULL;
22172 if (debug_info_level <= DINFO_LEVEL_TERSE)
22173 return;
22175 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22176 or fixed-point type, use the inner type. This is because we have no
22177 support for unnamed types in base_type_die. This can happen if this is
22178 an Ada subrange type. Correct solution is emit a subrange type die. */
22179 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22180 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22181 type = TREE_TYPE (type), code = TREE_CODE (type);
22183 if (code == ERROR_MARK
22184 /* Handle a special case. For functions whose return type is void, we
22185 generate *no* type attribute. (Note that no object may have type
22186 `void', so this only applies to function return types). */
22187 || code == VOID_TYPE)
22188 return;
22190 type_die = modified_type_die (type,
22191 cv_quals | TYPE_QUALS (type),
22192 reverse,
22193 context_die);
22195 if (type_die != NULL)
22196 add_AT_die_ref (object_die, DW_AT_type, type_die);
22199 /* Given an object die, add the calling convention attribute for the
22200 function call type. */
22201 static void
22202 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22204 enum dwarf_calling_convention value = DW_CC_normal;
22206 value = ((enum dwarf_calling_convention)
22207 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22209 if (is_fortran ()
22210 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22212 /* DWARF 2 doesn't provide a way to identify a program's source-level
22213 entry point. DW_AT_calling_convention attributes are only meant
22214 to describe functions' calling conventions. However, lacking a
22215 better way to signal the Fortran main program, we used this for
22216 a long time, following existing custom. Now, DWARF 4 has
22217 DW_AT_main_subprogram, which we add below, but some tools still
22218 rely on the old way, which we thus keep. */
22219 value = DW_CC_program;
22221 if (dwarf_version >= 4 || !dwarf_strict)
22222 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22225 /* Only add the attribute if the backend requests it, and
22226 is not DW_CC_normal. */
22227 if (value && (value != DW_CC_normal))
22228 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22231 /* Given a tree pointer to a struct, class, union, or enum type node, return
22232 a pointer to the (string) tag name for the given type, or zero if the type
22233 was declared without a tag. */
22235 static const char *
22236 type_tag (const_tree type)
22238 const char *name = 0;
22240 if (TYPE_NAME (type) != 0)
22242 tree t = 0;
22244 /* Find the IDENTIFIER_NODE for the type name. */
22245 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22246 && !TYPE_NAMELESS (type))
22247 t = TYPE_NAME (type);
22249 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22250 a TYPE_DECL node, regardless of whether or not a `typedef' was
22251 involved. */
22252 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22253 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22255 /* We want to be extra verbose. Don't call dwarf_name if
22256 DECL_NAME isn't set. The default hook for decl_printable_name
22257 doesn't like that, and in this context it's correct to return
22258 0, instead of "<anonymous>" or the like. */
22259 if (DECL_NAME (TYPE_NAME (type))
22260 && !DECL_NAMELESS (TYPE_NAME (type)))
22261 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22264 /* Now get the name as a string, or invent one. */
22265 if (!name && t != 0)
22266 name = IDENTIFIER_POINTER (t);
22269 return (name == 0 || *name == '\0') ? 0 : name;
22272 /* Return the type associated with a data member, make a special check
22273 for bit field types. */
22275 static inline tree
22276 member_declared_type (const_tree member)
22278 return (DECL_BIT_FIELD_TYPE (member)
22279 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22282 /* Get the decl's label, as described by its RTL. This may be different
22283 from the DECL_NAME name used in the source file. */
22285 #if 0
22286 static const char *
22287 decl_start_label (tree decl)
22289 rtx x;
22290 const char *fnname;
22292 x = DECL_RTL (decl);
22293 gcc_assert (MEM_P (x));
22295 x = XEXP (x, 0);
22296 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22298 fnname = XSTR (x, 0);
22299 return fnname;
22301 #endif
22303 /* For variable-length arrays that have been previously generated, but
22304 may be incomplete due to missing subscript info, fill the subscript
22305 info. Return TRUE if this is one of those cases. */
22307 static bool
22308 fill_variable_array_bounds (tree type)
22310 if (TREE_ASM_WRITTEN (type)
22311 && TREE_CODE (type) == ARRAY_TYPE
22312 && variably_modified_type_p (type, NULL))
22314 dw_die_ref array_die = lookup_type_die (type);
22315 if (!array_die)
22316 return false;
22317 add_subscript_info (array_die, type, !is_ada ());
22318 return true;
22320 return false;
22323 /* These routines generate the internal representation of the DIE's for
22324 the compilation unit. Debugging information is collected by walking
22325 the declaration trees passed in from dwarf2out_decl(). */
22327 static void
22328 gen_array_type_die (tree type, dw_die_ref context_die)
22330 dw_die_ref array_die;
22332 /* GNU compilers represent multidimensional array types as sequences of one
22333 dimensional array types whose element types are themselves array types.
22334 We sometimes squish that down to a single array_type DIE with multiple
22335 subscripts in the Dwarf debugging info. The draft Dwarf specification
22336 say that we are allowed to do this kind of compression in C, because
22337 there is no difference between an array of arrays and a multidimensional
22338 array. We don't do this for Ada to remain as close as possible to the
22339 actual representation, which is especially important against the language
22340 flexibilty wrt arrays of variable size. */
22342 bool collapse_nested_arrays = !is_ada ();
22344 if (fill_variable_array_bounds (type))
22345 return;
22347 dw_die_ref scope_die = scope_die_for (type, context_die);
22348 tree element_type;
22350 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22351 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22352 if (TREE_CODE (type) == ARRAY_TYPE
22353 && TYPE_STRING_FLAG (type)
22354 && is_fortran ()
22355 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22357 HOST_WIDE_INT size;
22359 array_die = new_die (DW_TAG_string_type, scope_die, type);
22360 add_name_attribute (array_die, type_tag (type));
22361 equate_type_number_to_die (type, array_die);
22362 size = int_size_in_bytes (type);
22363 if (size >= 0)
22364 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22365 /* ??? We can't annotate types late, but for LTO we may not
22366 generate a location early either (gfortran.dg/save_6.f90). */
22367 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22368 && TYPE_DOMAIN (type) != NULL_TREE
22369 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22371 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22372 tree rszdecl = szdecl;
22374 size = int_size_in_bytes (TREE_TYPE (szdecl));
22375 if (!DECL_P (szdecl))
22377 if (TREE_CODE (szdecl) == INDIRECT_REF
22378 && DECL_P (TREE_OPERAND (szdecl, 0)))
22380 rszdecl = TREE_OPERAND (szdecl, 0);
22381 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22382 != DWARF2_ADDR_SIZE)
22383 size = 0;
22385 else
22386 size = 0;
22388 if (size > 0)
22390 dw_loc_list_ref loc
22391 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22392 NULL);
22393 if (loc)
22395 add_AT_location_description (array_die, DW_AT_string_length,
22396 loc);
22397 if (size != DWARF2_ADDR_SIZE)
22398 add_AT_unsigned (array_die, dwarf_version >= 5
22399 ? DW_AT_string_length_byte_size
22400 : DW_AT_byte_size, size);
22404 return;
22407 array_die = new_die (DW_TAG_array_type, scope_die, type);
22408 add_name_attribute (array_die, type_tag (type));
22409 equate_type_number_to_die (type, array_die);
22411 if (TREE_CODE (type) == VECTOR_TYPE)
22412 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22414 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22415 if (is_fortran ()
22416 && TREE_CODE (type) == ARRAY_TYPE
22417 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22418 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22419 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22421 #if 0
22422 /* We default the array ordering. Debuggers will probably do the right
22423 things even if DW_AT_ordering is not present. It's not even an issue
22424 until we start to get into multidimensional arrays anyway. If a debugger
22425 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22426 then we'll have to put the DW_AT_ordering attribute back in. (But if
22427 and when we find out that we need to put these in, we will only do so
22428 for multidimensional arrays. */
22429 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22430 #endif
22432 if (TREE_CODE (type) == VECTOR_TYPE)
22434 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22435 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22436 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22437 add_bound_info (subrange_die, DW_AT_upper_bound,
22438 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22440 else
22441 add_subscript_info (array_die, type, collapse_nested_arrays);
22443 /* Add representation of the type of the elements of this array type and
22444 emit the corresponding DIE if we haven't done it already. */
22445 element_type = TREE_TYPE (type);
22446 if (collapse_nested_arrays)
22447 while (TREE_CODE (element_type) == ARRAY_TYPE)
22449 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22450 break;
22451 element_type = TREE_TYPE (element_type);
22454 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22455 TREE_CODE (type) == ARRAY_TYPE
22456 && TYPE_REVERSE_STORAGE_ORDER (type),
22457 context_die);
22459 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22460 if (TYPE_ARTIFICIAL (type))
22461 add_AT_flag (array_die, DW_AT_artificial, 1);
22463 if (get_AT (array_die, DW_AT_name))
22464 add_pubtype (type, array_die);
22466 add_alignment_attribute (array_die, type);
22469 /* This routine generates DIE for array with hidden descriptor, details
22470 are filled into *info by a langhook. */
22472 static void
22473 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22474 dw_die_ref context_die)
22476 const dw_die_ref scope_die = scope_die_for (type, context_die);
22477 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22478 struct loc_descr_context context = {
22479 type, /* context_type */
22480 info->base_decl, /* base_decl */
22481 NULL, /* dpi */
22482 false, /* placeholder_arg */
22483 false, /* placeholder_seen */
22484 false /* strict_signedness */
22486 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22487 int dim;
22489 add_name_attribute (array_die, type_tag (type));
22490 equate_type_number_to_die (type, array_die);
22492 if (info->ndimensions > 1)
22493 switch (info->ordering)
22495 case array_descr_ordering_row_major:
22496 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22497 break;
22498 case array_descr_ordering_column_major:
22499 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22500 break;
22501 default:
22502 break;
22505 if (dwarf_version >= 3 || !dwarf_strict)
22507 if (info->data_location)
22508 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22509 dw_scalar_form_exprloc, &context);
22510 if (info->associated)
22511 add_scalar_info (array_die, DW_AT_associated, info->associated,
22512 dw_scalar_form_constant
22513 | dw_scalar_form_exprloc
22514 | dw_scalar_form_reference, &context);
22515 if (info->allocated)
22516 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22517 dw_scalar_form_constant
22518 | dw_scalar_form_exprloc
22519 | dw_scalar_form_reference, &context);
22520 if (info->stride)
22522 const enum dwarf_attribute attr
22523 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22524 const int forms
22525 = (info->stride_in_bits)
22526 ? dw_scalar_form_constant
22527 : (dw_scalar_form_constant
22528 | dw_scalar_form_exprloc
22529 | dw_scalar_form_reference);
22531 add_scalar_info (array_die, attr, info->stride, forms, &context);
22534 if (dwarf_version >= 5)
22536 if (info->rank)
22538 add_scalar_info (array_die, DW_AT_rank, info->rank,
22539 dw_scalar_form_constant
22540 | dw_scalar_form_exprloc, &context);
22541 subrange_tag = DW_TAG_generic_subrange;
22542 context.placeholder_arg = true;
22546 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22548 for (dim = 0; dim < info->ndimensions; dim++)
22550 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22552 if (info->dimen[dim].bounds_type)
22553 add_type_attribute (subrange_die,
22554 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22555 false, context_die);
22556 if (info->dimen[dim].lower_bound)
22557 add_bound_info (subrange_die, DW_AT_lower_bound,
22558 info->dimen[dim].lower_bound, &context);
22559 if (info->dimen[dim].upper_bound)
22560 add_bound_info (subrange_die, DW_AT_upper_bound,
22561 info->dimen[dim].upper_bound, &context);
22562 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22563 add_scalar_info (subrange_die, DW_AT_byte_stride,
22564 info->dimen[dim].stride,
22565 dw_scalar_form_constant
22566 | dw_scalar_form_exprloc
22567 | dw_scalar_form_reference,
22568 &context);
22571 gen_type_die (info->element_type, context_die);
22572 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22573 TREE_CODE (type) == ARRAY_TYPE
22574 && TYPE_REVERSE_STORAGE_ORDER (type),
22575 context_die);
22577 if (get_AT (array_die, DW_AT_name))
22578 add_pubtype (type, array_die);
22580 add_alignment_attribute (array_die, type);
22583 #if 0
22584 static void
22585 gen_entry_point_die (tree decl, dw_die_ref context_die)
22587 tree origin = decl_ultimate_origin (decl);
22588 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22590 if (origin != NULL)
22591 add_abstract_origin_attribute (decl_die, origin);
22592 else
22594 add_name_and_src_coords_attributes (decl_die, decl);
22595 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22596 TYPE_UNQUALIFIED, false, context_die);
22599 if (DECL_ABSTRACT_P (decl))
22600 equate_decl_number_to_die (decl, decl_die);
22601 else
22602 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22604 #endif
22606 /* Walk through the list of incomplete types again, trying once more to
22607 emit full debugging info for them. */
22609 static void
22610 retry_incomplete_types (void)
22612 set_early_dwarf s;
22613 int i;
22615 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22616 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22617 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22618 vec_safe_truncate (incomplete_types, 0);
22621 /* Determine what tag to use for a record type. */
22623 static enum dwarf_tag
22624 record_type_tag (tree type)
22626 if (! lang_hooks.types.classify_record)
22627 return DW_TAG_structure_type;
22629 switch (lang_hooks.types.classify_record (type))
22631 case RECORD_IS_STRUCT:
22632 return DW_TAG_structure_type;
22634 case RECORD_IS_CLASS:
22635 return DW_TAG_class_type;
22637 case RECORD_IS_INTERFACE:
22638 if (dwarf_version >= 3 || !dwarf_strict)
22639 return DW_TAG_interface_type;
22640 return DW_TAG_structure_type;
22642 default:
22643 gcc_unreachable ();
22647 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22648 include all of the information about the enumeration values also. Each
22649 enumerated type name/value is listed as a child of the enumerated type
22650 DIE. */
22652 static dw_die_ref
22653 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22655 dw_die_ref type_die = lookup_type_die (type);
22656 dw_die_ref orig_type_die = type_die;
22658 if (type_die == NULL)
22660 type_die = new_die (DW_TAG_enumeration_type,
22661 scope_die_for (type, context_die), type);
22662 equate_type_number_to_die (type, type_die);
22663 add_name_attribute (type_die, type_tag (type));
22664 if ((dwarf_version >= 4 || !dwarf_strict)
22665 && ENUM_IS_SCOPED (type))
22666 add_AT_flag (type_die, DW_AT_enum_class, 1);
22667 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22668 add_AT_flag (type_die, DW_AT_declaration, 1);
22669 if (!dwarf_strict)
22670 add_AT_unsigned (type_die, DW_AT_encoding,
22671 TYPE_UNSIGNED (type)
22672 ? DW_ATE_unsigned
22673 : DW_ATE_signed);
22675 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22676 return type_die;
22677 else
22678 remove_AT (type_die, DW_AT_declaration);
22680 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22681 given enum type is incomplete, do not generate the DW_AT_byte_size
22682 attribute or the DW_AT_element_list attribute. */
22683 if (TYPE_SIZE (type))
22685 tree link;
22687 if (!ENUM_IS_OPAQUE (type))
22688 TREE_ASM_WRITTEN (type) = 1;
22689 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22690 add_byte_size_attribute (type_die, type);
22691 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22692 add_alignment_attribute (type_die, type);
22693 if ((dwarf_version >= 3 || !dwarf_strict)
22694 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22696 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22697 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22698 context_die);
22700 if (TYPE_STUB_DECL (type) != NULL_TREE)
22702 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22703 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22704 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22705 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22708 /* If the first reference to this type was as the return type of an
22709 inline function, then it may not have a parent. Fix this now. */
22710 if (type_die->die_parent == NULL)
22711 add_child_die (scope_die_for (type, context_die), type_die);
22713 for (link = TYPE_VALUES (type);
22714 link != NULL; link = TREE_CHAIN (link))
22716 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22717 tree value = TREE_VALUE (link);
22719 if (DECL_P (value))
22720 equate_decl_number_to_die (value, enum_die);
22722 gcc_assert (!ENUM_IS_OPAQUE (type));
22723 add_name_attribute (enum_die,
22724 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22726 if (TREE_CODE (value) == CONST_DECL)
22727 value = DECL_INITIAL (value);
22729 if (simple_type_size_in_bits (TREE_TYPE (value))
22730 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22732 /* For constant forms created by add_AT_unsigned DWARF
22733 consumers (GDB, elfutils, etc.) always zero extend
22734 the value. Only when the actual value is negative
22735 do we need to use add_AT_int to generate a constant
22736 form that can represent negative values. */
22737 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22738 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22739 add_AT_unsigned (enum_die, DW_AT_const_value,
22740 (unsigned HOST_WIDE_INT) val);
22741 else
22742 add_AT_int (enum_die, DW_AT_const_value, val);
22744 else
22745 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22746 that here. TODO: This should be re-worked to use correct
22747 signed/unsigned double tags for all cases. */
22748 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22751 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22752 if (TYPE_ARTIFICIAL (type)
22753 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22754 add_AT_flag (type_die, DW_AT_artificial, 1);
22756 else
22757 add_AT_flag (type_die, DW_AT_declaration, 1);
22759 add_pubtype (type, type_die);
22761 return type_die;
22764 /* Generate a DIE to represent either a real live formal parameter decl or to
22765 represent just the type of some formal parameter position in some function
22766 type.
22768 Note that this routine is a bit unusual because its argument may be a
22769 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22770 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22771 node. If it's the former then this function is being called to output a
22772 DIE to represent a formal parameter object (or some inlining thereof). If
22773 it's the latter, then this function is only being called to output a
22774 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22775 argument type of some subprogram type.
22776 If EMIT_NAME_P is true, name and source coordinate attributes
22777 are emitted. */
22779 static dw_die_ref
22780 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22781 dw_die_ref context_die)
22783 tree node_or_origin = node ? node : origin;
22784 tree ultimate_origin;
22785 dw_die_ref parm_die = NULL;
22787 if (DECL_P (node_or_origin))
22789 parm_die = lookup_decl_die (node);
22791 /* If the contexts differ, we may not be talking about the same
22792 thing.
22793 ??? When in LTO the DIE parent is the "abstract" copy and the
22794 context_die is the specification "copy". */
22795 if (parm_die
22796 && parm_die->die_parent != context_die
22797 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22798 || parm_die->die_parent->die_parent != context_die)
22799 && !in_lto_p)
22801 gcc_assert (!DECL_ABSTRACT_P (node));
22802 /* This can happen when creating a concrete instance, in
22803 which case we need to create a new DIE that will get
22804 annotated with DW_AT_abstract_origin. */
22805 parm_die = NULL;
22808 if (parm_die && parm_die->die_parent == NULL)
22810 /* Check that parm_die already has the right attributes that
22811 we would have added below. If any attributes are
22812 missing, fall through to add them. */
22813 if (! DECL_ABSTRACT_P (node_or_origin)
22814 && !get_AT (parm_die, DW_AT_location)
22815 && !get_AT (parm_die, DW_AT_const_value))
22816 /* We are missing location info, and are about to add it. */
22818 else
22820 add_child_die (context_die, parm_die);
22821 return parm_die;
22826 /* If we have a previously generated DIE, use it, unless this is an
22827 concrete instance (origin != NULL), in which case we need a new
22828 DIE with a corresponding DW_AT_abstract_origin. */
22829 bool reusing_die;
22830 if (parm_die && origin == NULL)
22831 reusing_die = true;
22832 else
22834 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22835 reusing_die = false;
22838 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22840 case tcc_declaration:
22841 ultimate_origin = decl_ultimate_origin (node_or_origin);
22842 if (node || ultimate_origin)
22843 origin = ultimate_origin;
22845 if (reusing_die)
22846 goto add_location;
22848 if (origin != NULL)
22849 add_abstract_origin_attribute (parm_die, origin);
22850 else if (emit_name_p)
22851 add_name_and_src_coords_attributes (parm_die, node);
22852 if (origin == NULL
22853 || (! DECL_ABSTRACT_P (node_or_origin)
22854 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22855 decl_function_context
22856 (node_or_origin))))
22858 tree type = TREE_TYPE (node_or_origin);
22859 if (decl_by_reference_p (node_or_origin))
22860 add_type_attribute (parm_die, TREE_TYPE (type),
22861 TYPE_UNQUALIFIED,
22862 false, context_die);
22863 else
22864 add_type_attribute (parm_die, type,
22865 decl_quals (node_or_origin),
22866 false, context_die);
22868 if (origin == NULL && DECL_ARTIFICIAL (node))
22869 add_AT_flag (parm_die, DW_AT_artificial, 1);
22870 add_location:
22871 if (node && node != origin)
22872 equate_decl_number_to_die (node, parm_die);
22873 if (! DECL_ABSTRACT_P (node_or_origin))
22874 add_location_or_const_value_attribute (parm_die, node_or_origin,
22875 node == NULL);
22877 break;
22879 case tcc_type:
22880 /* We were called with some kind of a ..._TYPE node. */
22881 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22882 context_die);
22883 break;
22885 default:
22886 gcc_unreachable ();
22889 return parm_die;
22892 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22893 children DW_TAG_formal_parameter DIEs representing the arguments of the
22894 parameter pack.
22896 PARM_PACK must be a function parameter pack.
22897 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22898 must point to the subsequent arguments of the function PACK_ARG belongs to.
22899 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22900 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22901 following the last one for which a DIE was generated. */
22903 static dw_die_ref
22904 gen_formal_parameter_pack_die (tree parm_pack,
22905 tree pack_arg,
22906 dw_die_ref subr_die,
22907 tree *next_arg)
22909 tree arg;
22910 dw_die_ref parm_pack_die;
22912 gcc_assert (parm_pack
22913 && lang_hooks.function_parameter_pack_p (parm_pack)
22914 && subr_die);
22916 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22917 add_src_coords_attributes (parm_pack_die, parm_pack);
22919 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22921 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22922 parm_pack))
22923 break;
22924 gen_formal_parameter_die (arg, NULL,
22925 false /* Don't emit name attribute. */,
22926 parm_pack_die);
22928 if (next_arg)
22929 *next_arg = arg;
22930 return parm_pack_die;
22933 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22934 at the end of an (ANSI prototyped) formal parameters list. */
22936 static void
22937 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22939 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22942 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22943 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22944 parameters as specified in some function type specification (except for
22945 those which appear as part of a function *definition*). */
22947 static void
22948 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22950 tree link;
22951 tree formal_type = NULL;
22952 tree first_parm_type;
22953 tree arg;
22955 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22957 arg = DECL_ARGUMENTS (function_or_method_type);
22958 function_or_method_type = TREE_TYPE (function_or_method_type);
22960 else
22961 arg = NULL_TREE;
22963 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22965 /* Make our first pass over the list of formal parameter types and output a
22966 DW_TAG_formal_parameter DIE for each one. */
22967 for (link = first_parm_type; link; )
22969 dw_die_ref parm_die;
22971 formal_type = TREE_VALUE (link);
22972 if (formal_type == void_type_node)
22973 break;
22975 /* Output a (nameless) DIE to represent the formal parameter itself. */
22976 parm_die = gen_formal_parameter_die (formal_type, NULL,
22977 true /* Emit name attribute. */,
22978 context_die);
22979 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22980 && link == first_parm_type)
22982 add_AT_flag (parm_die, DW_AT_artificial, 1);
22983 if (dwarf_version >= 3 || !dwarf_strict)
22984 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22986 else if (arg && DECL_ARTIFICIAL (arg))
22987 add_AT_flag (parm_die, DW_AT_artificial, 1);
22989 link = TREE_CHAIN (link);
22990 if (arg)
22991 arg = DECL_CHAIN (arg);
22994 /* If this function type has an ellipsis, add a
22995 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22996 if (formal_type != void_type_node)
22997 gen_unspecified_parameters_die (function_or_method_type, context_die);
22999 /* Make our second (and final) pass over the list of formal parameter types
23000 and output DIEs to represent those types (as necessary). */
23001 for (link = TYPE_ARG_TYPES (function_or_method_type);
23002 link && TREE_VALUE (link);
23003 link = TREE_CHAIN (link))
23004 gen_type_die (TREE_VALUE (link), context_die);
23007 /* We want to generate the DIE for TYPE so that we can generate the
23008 die for MEMBER, which has been defined; we will need to refer back
23009 to the member declaration nested within TYPE. If we're trying to
23010 generate minimal debug info for TYPE, processing TYPE won't do the
23011 trick; we need to attach the member declaration by hand. */
23013 static void
23014 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23016 gen_type_die (type, context_die);
23018 /* If we're trying to avoid duplicate debug info, we may not have
23019 emitted the member decl for this function. Emit it now. */
23020 if (TYPE_STUB_DECL (type)
23021 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23022 && ! lookup_decl_die (member))
23024 dw_die_ref type_die;
23025 gcc_assert (!decl_ultimate_origin (member));
23027 type_die = lookup_type_die_strip_naming_typedef (type);
23028 if (TREE_CODE (member) == FUNCTION_DECL)
23029 gen_subprogram_die (member, type_die);
23030 else if (TREE_CODE (member) == FIELD_DECL)
23032 /* Ignore the nameless fields that are used to skip bits but handle
23033 C++ anonymous unions and structs. */
23034 if (DECL_NAME (member) != NULL_TREE
23035 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23036 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23038 struct vlr_context vlr_ctx = {
23039 DECL_CONTEXT (member), /* struct_type */
23040 NULL_TREE /* variant_part_offset */
23042 gen_type_die (member_declared_type (member), type_die);
23043 gen_field_die (member, &vlr_ctx, type_die);
23046 else
23047 gen_variable_die (member, NULL_TREE, type_die);
23051 /* Forward declare these functions, because they are mutually recursive
23052 with their set_block_* pairing functions. */
23053 static void set_decl_origin_self (tree);
23055 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23056 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23057 that it points to the node itself, thus indicating that the node is its
23058 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23059 the given node is NULL, recursively descend the decl/block tree which
23060 it is the root of, and for each other ..._DECL or BLOCK node contained
23061 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23062 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23063 values to point to themselves. */
23065 static void
23066 set_block_origin_self (tree stmt)
23068 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23070 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23073 tree local_decl;
23075 for (local_decl = BLOCK_VARS (stmt);
23076 local_decl != NULL_TREE;
23077 local_decl = DECL_CHAIN (local_decl))
23078 /* Do not recurse on nested functions since the inlining status
23079 of parent and child can be different as per the DWARF spec. */
23080 if (TREE_CODE (local_decl) != FUNCTION_DECL
23081 && !DECL_EXTERNAL (local_decl))
23082 set_decl_origin_self (local_decl);
23086 tree subblock;
23088 for (subblock = BLOCK_SUBBLOCKS (stmt);
23089 subblock != NULL_TREE;
23090 subblock = BLOCK_CHAIN (subblock))
23091 set_block_origin_self (subblock); /* Recurse. */
23096 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23097 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23098 node to so that it points to the node itself, thus indicating that the
23099 node represents its own (abstract) origin. Additionally, if the
23100 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23101 the decl/block tree of which the given node is the root of, and for
23102 each other ..._DECL or BLOCK node contained therein whose
23103 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23104 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23105 point to themselves. */
23107 static void
23108 set_decl_origin_self (tree decl)
23110 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23112 DECL_ABSTRACT_ORIGIN (decl) = decl;
23113 if (TREE_CODE (decl) == FUNCTION_DECL)
23115 tree arg;
23117 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23118 DECL_ABSTRACT_ORIGIN (arg) = arg;
23119 if (DECL_INITIAL (decl) != NULL_TREE
23120 && DECL_INITIAL (decl) != error_mark_node)
23121 set_block_origin_self (DECL_INITIAL (decl));
23126 /* Mark the early DIE for DECL as the abstract instance. */
23128 static void
23129 dwarf2out_abstract_function (tree decl)
23131 dw_die_ref old_die;
23133 /* Make sure we have the actual abstract inline, not a clone. */
23134 decl = DECL_ORIGIN (decl);
23136 if (DECL_IGNORED_P (decl))
23137 return;
23139 /* In LTO we're all set. We already created abstract instances
23140 early and we want to avoid creating a concrete instance of that
23141 if we don't output it. */
23142 if (in_lto_p)
23143 return;
23145 old_die = lookup_decl_die (decl);
23146 gcc_assert (old_die != NULL);
23147 if (get_AT (old_die, DW_AT_inline))
23148 /* We've already generated the abstract instance. */
23149 return;
23151 /* Go ahead and put DW_AT_inline on the DIE. */
23152 if (DECL_DECLARED_INLINE_P (decl))
23154 if (cgraph_function_possibly_inlined_p (decl))
23155 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23156 else
23157 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23159 else
23161 if (cgraph_function_possibly_inlined_p (decl))
23162 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23163 else
23164 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23167 if (DECL_DECLARED_INLINE_P (decl)
23168 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23169 add_AT_flag (old_die, DW_AT_artificial, 1);
23171 set_decl_origin_self (decl);
23174 /* Helper function of premark_used_types() which gets called through
23175 htab_traverse.
23177 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23178 marked as unused by prune_unused_types. */
23180 bool
23181 premark_used_types_helper (tree const &type, void *)
23183 dw_die_ref die;
23185 die = lookup_type_die (type);
23186 if (die != NULL)
23187 die->die_perennial_p = 1;
23188 return true;
23191 /* Helper function of premark_types_used_by_global_vars which gets called
23192 through htab_traverse.
23194 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23195 marked as unused by prune_unused_types. The DIE of the type is marked
23196 only if the global variable using the type will actually be emitted. */
23199 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23200 void *)
23202 struct types_used_by_vars_entry *entry;
23203 dw_die_ref die;
23205 entry = (struct types_used_by_vars_entry *) *slot;
23206 gcc_assert (entry->type != NULL
23207 && entry->var_decl != NULL);
23208 die = lookup_type_die (entry->type);
23209 if (die)
23211 /* Ask cgraph if the global variable really is to be emitted.
23212 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23213 varpool_node *node = varpool_node::get (entry->var_decl);
23214 if (node && node->definition)
23216 die->die_perennial_p = 1;
23217 /* Keep the parent DIEs as well. */
23218 while ((die = die->die_parent) && die->die_perennial_p == 0)
23219 die->die_perennial_p = 1;
23222 return 1;
23225 /* Mark all members of used_types_hash as perennial. */
23227 static void
23228 premark_used_types (struct function *fun)
23230 if (fun && fun->used_types_hash)
23231 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23234 /* Mark all members of types_used_by_vars_entry as perennial. */
23236 static void
23237 premark_types_used_by_global_vars (void)
23239 if (types_used_by_vars_hash)
23240 types_used_by_vars_hash
23241 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23244 /* Mark all variables used by the symtab as perennial. */
23246 static void
23247 premark_used_variables (void)
23249 /* Mark DIEs in the symtab as used. */
23250 varpool_node *var;
23251 FOR_EACH_VARIABLE (var)
23253 dw_die_ref die = lookup_decl_die (var->decl);
23254 if (die)
23255 die->die_perennial_p = 1;
23259 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23260 for CA_LOC call arg loc node. */
23262 static dw_die_ref
23263 gen_call_site_die (tree decl, dw_die_ref subr_die,
23264 struct call_arg_loc_node *ca_loc)
23266 dw_die_ref stmt_die = NULL, die;
23267 tree block = ca_loc->block;
23269 while (block
23270 && block != DECL_INITIAL (decl)
23271 && TREE_CODE (block) == BLOCK)
23273 stmt_die = lookup_block_die (block);
23274 if (stmt_die)
23275 break;
23276 block = BLOCK_SUPERCONTEXT (block);
23278 if (stmt_die == NULL)
23279 stmt_die = subr_die;
23280 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23281 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23282 if (ca_loc->tail_call_p)
23283 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23284 if (ca_loc->symbol_ref)
23286 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23287 if (tdie)
23288 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23289 else
23290 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23291 false);
23293 return die;
23296 /* Generate a DIE to represent a declared function (either file-scope or
23297 block-local). */
23299 static void
23300 gen_subprogram_die (tree decl, dw_die_ref context_die)
23302 tree origin = decl_ultimate_origin (decl);
23303 dw_die_ref subr_die;
23304 dw_die_ref old_die = lookup_decl_die (decl);
23305 bool old_die_had_no_children = false;
23307 /* This function gets called multiple times for different stages of
23308 the debug process. For example, for func() in this code:
23310 namespace S
23312 void func() { ... }
23315 ...we get called 4 times. Twice in early debug and twice in
23316 late debug:
23318 Early debug
23319 -----------
23321 1. Once while generating func() within the namespace. This is
23322 the declaration. The declaration bit below is set, as the
23323 context is the namespace.
23325 A new DIE will be generated with DW_AT_declaration set.
23327 2. Once for func() itself. This is the specification. The
23328 declaration bit below is clear as the context is the CU.
23330 We will use the cached DIE from (1) to create a new DIE with
23331 DW_AT_specification pointing to the declaration in (1).
23333 Late debug via rest_of_handle_final()
23334 -------------------------------------
23336 3. Once generating func() within the namespace. This is also the
23337 declaration, as in (1), but this time we will early exit below
23338 as we have a cached DIE and a declaration needs no additional
23339 annotations (no locations), as the source declaration line
23340 info is enough.
23342 4. Once for func() itself. As in (2), this is the specification,
23343 but this time we will re-use the cached DIE, and just annotate
23344 it with the location information that should now be available.
23346 For something without namespaces, but with abstract instances, we
23347 are also called a multiple times:
23349 class Base
23351 public:
23352 Base (); // constructor declaration (1)
23355 Base::Base () { } // constructor specification (2)
23357 Early debug
23358 -----------
23360 1. Once for the Base() constructor by virtue of it being a
23361 member of the Base class. This is done via
23362 rest_of_type_compilation.
23364 This is a declaration, so a new DIE will be created with
23365 DW_AT_declaration.
23367 2. Once for the Base() constructor definition, but this time
23368 while generating the abstract instance of the base
23369 constructor (__base_ctor) which is being generated via early
23370 debug of reachable functions.
23372 Even though we have a cached version of the declaration (1),
23373 we will create a DW_AT_specification of the declaration DIE
23374 in (1).
23376 3. Once for the __base_ctor itself, but this time, we generate
23377 an DW_AT_abstract_origin version of the DW_AT_specification in
23378 (2).
23380 Late debug via rest_of_handle_final
23381 -----------------------------------
23383 4. One final time for the __base_ctor (which will have a cached
23384 DIE with DW_AT_abstract_origin created in (3). This time,
23385 we will just annotate the location information now
23386 available.
23388 int declaration = (current_function_decl != decl
23389 || (!DECL_INITIAL (decl) && !origin)
23390 || class_or_namespace_scope_p (context_die));
23392 /* A declaration that has been previously dumped needs no
23393 additional information. */
23394 if (old_die && declaration)
23395 return;
23397 if (in_lto_p && old_die && old_die->die_child == NULL)
23398 old_die_had_no_children = true;
23400 /* Now that the C++ front end lazily declares artificial member fns, we
23401 might need to retrofit the declaration into its class. */
23402 if (!declaration && !origin && !old_die
23403 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23404 && !class_or_namespace_scope_p (context_die)
23405 && debug_info_level > DINFO_LEVEL_TERSE)
23406 old_die = force_decl_die (decl);
23408 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23409 if (origin != NULL)
23411 gcc_assert (!declaration || local_scope_p (context_die));
23413 /* Fixup die_parent for the abstract instance of a nested
23414 inline function. */
23415 if (old_die && old_die->die_parent == NULL)
23416 add_child_die (context_die, old_die);
23418 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23420 /* If we have a DW_AT_abstract_origin we have a working
23421 cached version. */
23422 subr_die = old_die;
23424 else
23426 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23427 add_abstract_origin_attribute (subr_die, origin);
23428 /* This is where the actual code for a cloned function is.
23429 Let's emit linkage name attribute for it. This helps
23430 debuggers to e.g, set breakpoints into
23431 constructors/destructors when the user asks "break
23432 K::K". */
23433 add_linkage_name (subr_die, decl);
23436 /* A cached copy, possibly from early dwarf generation. Reuse as
23437 much as possible. */
23438 else if (old_die)
23440 if (!get_AT_flag (old_die, DW_AT_declaration)
23441 /* We can have a normal definition following an inline one in the
23442 case of redefinition of GNU C extern inlines.
23443 It seems reasonable to use AT_specification in this case. */
23444 && !get_AT (old_die, DW_AT_inline))
23446 /* Detect and ignore this case, where we are trying to output
23447 something we have already output. */
23448 if (get_AT (old_die, DW_AT_low_pc)
23449 || get_AT (old_die, DW_AT_ranges))
23450 return;
23452 /* If we have no location information, this must be a
23453 partially generated DIE from early dwarf generation.
23454 Fall through and generate it. */
23457 /* If the definition comes from the same place as the declaration,
23458 maybe use the old DIE. We always want the DIE for this function
23459 that has the *_pc attributes to be under comp_unit_die so the
23460 debugger can find it. We also need to do this for abstract
23461 instances of inlines, since the spec requires the out-of-line copy
23462 to have the same parent. For local class methods, this doesn't
23463 apply; we just use the old DIE. */
23464 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23465 struct dwarf_file_data * file_index = lookup_filename (s.file);
23466 if (((is_unit_die (old_die->die_parent)
23467 /* This condition fixes the inconsistency/ICE with the
23468 following Fortran test (or some derivative thereof) while
23469 building libgfortran:
23471 module some_m
23472 contains
23473 logical function funky (FLAG)
23474 funky = .true.
23475 end function
23476 end module
23478 || (old_die->die_parent
23479 && old_die->die_parent->die_tag == DW_TAG_module)
23480 || local_scope_p (old_die->die_parent)
23481 || context_die == NULL)
23482 && (DECL_ARTIFICIAL (decl)
23483 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23484 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23485 == (unsigned) s.line)
23486 && (!debug_column_info
23487 || s.column == 0
23488 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23489 == (unsigned) s.column)))))
23490 /* With LTO if there's an abstract instance for
23491 the old DIE, this is a concrete instance and
23492 thus re-use the DIE. */
23493 || get_AT (old_die, DW_AT_abstract_origin))
23495 subr_die = old_die;
23497 /* Clear out the declaration attribute, but leave the
23498 parameters so they can be augmented with location
23499 information later. Unless this was a declaration, in
23500 which case, wipe out the nameless parameters and recreate
23501 them further down. */
23502 if (remove_AT (subr_die, DW_AT_declaration))
23505 remove_AT (subr_die, DW_AT_object_pointer);
23506 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23509 /* Make a specification pointing to the previously built
23510 declaration. */
23511 else
23513 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23514 add_AT_specification (subr_die, old_die);
23515 add_pubname (decl, subr_die);
23516 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23517 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23518 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23519 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23520 if (debug_column_info
23521 && s.column
23522 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23523 != (unsigned) s.column))
23524 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23526 /* If the prototype had an 'auto' or 'decltype(auto)' in
23527 the return type, emit the real type on the definition die. */
23528 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23530 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23531 while (die
23532 && (die->die_tag == DW_TAG_reference_type
23533 || die->die_tag == DW_TAG_rvalue_reference_type
23534 || die->die_tag == DW_TAG_pointer_type
23535 || die->die_tag == DW_TAG_const_type
23536 || die->die_tag == DW_TAG_volatile_type
23537 || die->die_tag == DW_TAG_restrict_type
23538 || die->die_tag == DW_TAG_array_type
23539 || die->die_tag == DW_TAG_ptr_to_member_type
23540 || die->die_tag == DW_TAG_subroutine_type))
23541 die = get_AT_ref (die, DW_AT_type);
23542 if (die == auto_die || die == decltype_auto_die)
23543 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23544 TYPE_UNQUALIFIED, false, context_die);
23547 /* When we process the method declaration, we haven't seen
23548 the out-of-class defaulted definition yet, so we have to
23549 recheck now. */
23550 if ((dwarf_version >= 5 || ! dwarf_strict)
23551 && !get_AT (subr_die, DW_AT_defaulted))
23553 int defaulted
23554 = lang_hooks.decls.decl_dwarf_attribute (decl,
23555 DW_AT_defaulted);
23556 if (defaulted != -1)
23558 /* Other values must have been handled before. */
23559 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23560 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23565 /* Create a fresh DIE for anything else. */
23566 else
23568 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23570 if (TREE_PUBLIC (decl))
23571 add_AT_flag (subr_die, DW_AT_external, 1);
23573 add_name_and_src_coords_attributes (subr_die, decl);
23574 add_pubname (decl, subr_die);
23575 if (debug_info_level > DINFO_LEVEL_TERSE)
23577 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23578 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23579 TYPE_UNQUALIFIED, false, context_die);
23582 add_pure_or_virtual_attribute (subr_die, decl);
23583 if (DECL_ARTIFICIAL (decl))
23584 add_AT_flag (subr_die, DW_AT_artificial, 1);
23586 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23587 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23589 add_alignment_attribute (subr_die, decl);
23591 add_accessibility_attribute (subr_die, decl);
23594 /* Unless we have an existing non-declaration DIE, equate the new
23595 DIE. */
23596 if (!old_die || is_declaration_die (old_die))
23597 equate_decl_number_to_die (decl, subr_die);
23599 if (declaration)
23601 if (!old_die || !get_AT (old_die, DW_AT_inline))
23603 add_AT_flag (subr_die, DW_AT_declaration, 1);
23605 /* If this is an explicit function declaration then generate
23606 a DW_AT_explicit attribute. */
23607 if ((dwarf_version >= 3 || !dwarf_strict)
23608 && lang_hooks.decls.decl_dwarf_attribute (decl,
23609 DW_AT_explicit) == 1)
23610 add_AT_flag (subr_die, DW_AT_explicit, 1);
23612 /* If this is a C++11 deleted special function member then generate
23613 a DW_AT_deleted attribute. */
23614 if ((dwarf_version >= 5 || !dwarf_strict)
23615 && lang_hooks.decls.decl_dwarf_attribute (decl,
23616 DW_AT_deleted) == 1)
23617 add_AT_flag (subr_die, DW_AT_deleted, 1);
23619 /* If this is a C++11 defaulted special function member then
23620 generate a DW_AT_defaulted attribute. */
23621 if (dwarf_version >= 5 || !dwarf_strict)
23623 int defaulted
23624 = lang_hooks.decls.decl_dwarf_attribute (decl,
23625 DW_AT_defaulted);
23626 if (defaulted != -1)
23627 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23630 /* If this is a C++11 non-static member function with & ref-qualifier
23631 then generate a DW_AT_reference attribute. */
23632 if ((dwarf_version >= 5 || !dwarf_strict)
23633 && lang_hooks.decls.decl_dwarf_attribute (decl,
23634 DW_AT_reference) == 1)
23635 add_AT_flag (subr_die, DW_AT_reference, 1);
23637 /* If this is a C++11 non-static member function with &&
23638 ref-qualifier then generate a DW_AT_reference attribute. */
23639 if ((dwarf_version >= 5 || !dwarf_strict)
23640 && lang_hooks.decls.decl_dwarf_attribute (decl,
23641 DW_AT_rvalue_reference)
23642 == 1)
23643 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23646 /* For non DECL_EXTERNALs, if range information is available, fill
23647 the DIE with it. */
23648 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23650 HOST_WIDE_INT cfa_fb_offset;
23652 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23654 if (!crtl->has_bb_partition)
23656 dw_fde_ref fde = fun->fde;
23657 if (fde->dw_fde_begin)
23659 /* We have already generated the labels. */
23660 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23661 fde->dw_fde_end, false);
23663 else
23665 /* Create start/end labels and add the range. */
23666 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23667 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23668 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23669 current_function_funcdef_no);
23670 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23671 current_function_funcdef_no);
23672 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23673 false);
23676 #if VMS_DEBUGGING_INFO
23677 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23678 Section 2.3 Prologue and Epilogue Attributes:
23679 When a breakpoint is set on entry to a function, it is generally
23680 desirable for execution to be suspended, not on the very first
23681 instruction of the function, but rather at a point after the
23682 function's frame has been set up, after any language defined local
23683 declaration processing has been completed, and before execution of
23684 the first statement of the function begins. Debuggers generally
23685 cannot properly determine where this point is. Similarly for a
23686 breakpoint set on exit from a function. The prologue and epilogue
23687 attributes allow a compiler to communicate the location(s) to use. */
23690 if (fde->dw_fde_vms_end_prologue)
23691 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23692 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23694 if (fde->dw_fde_vms_begin_epilogue)
23695 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23696 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23698 #endif
23701 else
23703 /* Generate pubnames entries for the split function code ranges. */
23704 dw_fde_ref fde = fun->fde;
23706 if (fde->dw_fde_second_begin)
23708 if (dwarf_version >= 3 || !dwarf_strict)
23710 /* We should use ranges for non-contiguous code section
23711 addresses. Use the actual code range for the initial
23712 section, since the HOT/COLD labels might precede an
23713 alignment offset. */
23714 bool range_list_added = false;
23715 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23716 fde->dw_fde_end, &range_list_added,
23717 false);
23718 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23719 fde->dw_fde_second_end,
23720 &range_list_added, false);
23721 if (range_list_added)
23722 add_ranges (NULL);
23724 else
23726 /* There is no real support in DW2 for this .. so we make
23727 a work-around. First, emit the pub name for the segment
23728 containing the function label. Then make and emit a
23729 simplified subprogram DIE for the second segment with the
23730 name pre-fixed by __hot/cold_sect_of_. We use the same
23731 linkage name for the second die so that gdb will find both
23732 sections when given "b foo". */
23733 const char *name = NULL;
23734 tree decl_name = DECL_NAME (decl);
23735 dw_die_ref seg_die;
23737 /* Do the 'primary' section. */
23738 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23739 fde->dw_fde_end, false);
23741 /* Build a minimal DIE for the secondary section. */
23742 seg_die = new_die (DW_TAG_subprogram,
23743 subr_die->die_parent, decl);
23745 if (TREE_PUBLIC (decl))
23746 add_AT_flag (seg_die, DW_AT_external, 1);
23748 if (decl_name != NULL
23749 && IDENTIFIER_POINTER (decl_name) != NULL)
23751 name = dwarf2_name (decl, 1);
23752 if (! DECL_ARTIFICIAL (decl))
23753 add_src_coords_attributes (seg_die, decl);
23755 add_linkage_name (seg_die, decl);
23757 gcc_assert (name != NULL);
23758 add_pure_or_virtual_attribute (seg_die, decl);
23759 if (DECL_ARTIFICIAL (decl))
23760 add_AT_flag (seg_die, DW_AT_artificial, 1);
23762 name = concat ("__second_sect_of_", name, NULL);
23763 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23764 fde->dw_fde_second_end, false);
23765 add_name_attribute (seg_die, name);
23766 if (want_pubnames ())
23767 add_pubname_string (name, seg_die);
23770 else
23771 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23772 false);
23775 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23777 /* We define the "frame base" as the function's CFA. This is more
23778 convenient for several reasons: (1) It's stable across the prologue
23779 and epilogue, which makes it better than just a frame pointer,
23780 (2) With dwarf3, there exists a one-byte encoding that allows us
23781 to reference the .debug_frame data by proxy, but failing that,
23782 (3) We can at least reuse the code inspection and interpretation
23783 code that determines the CFA position at various points in the
23784 function. */
23785 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23787 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23788 add_AT_loc (subr_die, DW_AT_frame_base, op);
23790 else
23792 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23793 if (list->dw_loc_next)
23794 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23795 else
23796 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23799 /* Compute a displacement from the "steady-state frame pointer" to
23800 the CFA. The former is what all stack slots and argument slots
23801 will reference in the rtl; the latter is what we've told the
23802 debugger about. We'll need to adjust all frame_base references
23803 by this displacement. */
23804 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23806 if (fun->static_chain_decl)
23808 /* DWARF requires here a location expression that computes the
23809 address of the enclosing subprogram's frame base. The machinery
23810 in tree-nested.c is supposed to store this specific address in the
23811 last field of the FRAME record. */
23812 const tree frame_type
23813 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23814 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23816 tree fb_expr
23817 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23818 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23819 fb_expr, fb_decl, NULL_TREE);
23821 add_AT_location_description (subr_die, DW_AT_static_link,
23822 loc_list_from_tree (fb_expr, 0, NULL));
23825 resolve_variable_values ();
23828 /* Generate child dies for template parameters. */
23829 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23830 gen_generic_params_dies (decl);
23832 /* Now output descriptions of the arguments for this function. This gets
23833 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23834 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23835 `...' at the end of the formal parameter list. In order to find out if
23836 there was a trailing ellipsis or not, we must instead look at the type
23837 associated with the FUNCTION_DECL. This will be a node of type
23838 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23839 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23840 an ellipsis at the end. */
23842 /* In the case where we are describing a mere function declaration, all we
23843 need to do here (and all we *can* do here) is to describe the *types* of
23844 its formal parameters. */
23845 if (debug_info_level <= DINFO_LEVEL_TERSE)
23847 else if (declaration)
23848 gen_formal_types_die (decl, subr_die);
23849 else
23851 /* Generate DIEs to represent all known formal parameters. */
23852 tree parm = DECL_ARGUMENTS (decl);
23853 tree generic_decl = early_dwarf
23854 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23855 tree generic_decl_parm = generic_decl
23856 ? DECL_ARGUMENTS (generic_decl)
23857 : NULL;
23859 /* Now we want to walk the list of parameters of the function and
23860 emit their relevant DIEs.
23862 We consider the case of DECL being an instance of a generic function
23863 as well as it being a normal function.
23865 If DECL is an instance of a generic function we walk the
23866 parameters of the generic function declaration _and_ the parameters of
23867 DECL itself. This is useful because we want to emit specific DIEs for
23868 function parameter packs and those are declared as part of the
23869 generic function declaration. In that particular case,
23870 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23871 That DIE has children DIEs representing the set of arguments
23872 of the pack. Note that the set of pack arguments can be empty.
23873 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23874 children DIE.
23876 Otherwise, we just consider the parameters of DECL. */
23877 while (generic_decl_parm || parm)
23879 if (generic_decl_parm
23880 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23881 gen_formal_parameter_pack_die (generic_decl_parm,
23882 parm, subr_die,
23883 &parm);
23884 else if (parm)
23886 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23888 if (early_dwarf
23889 && parm == DECL_ARGUMENTS (decl)
23890 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23891 && parm_die
23892 && (dwarf_version >= 3 || !dwarf_strict))
23893 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23895 parm = DECL_CHAIN (parm);
23898 if (generic_decl_parm)
23899 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23902 /* Decide whether we need an unspecified_parameters DIE at the end.
23903 There are 2 more cases to do this for: 1) the ansi ... declaration -
23904 this is detectable when the end of the arg list is not a
23905 void_type_node 2) an unprototyped function declaration (not a
23906 definition). This just means that we have no info about the
23907 parameters at all. */
23908 if (early_dwarf)
23910 if (prototype_p (TREE_TYPE (decl)))
23912 /* This is the prototyped case, check for.... */
23913 if (stdarg_p (TREE_TYPE (decl)))
23914 gen_unspecified_parameters_die (decl, subr_die);
23916 else if (DECL_INITIAL (decl) == NULL_TREE)
23917 gen_unspecified_parameters_die (decl, subr_die);
23919 else if ((subr_die != old_die || old_die_had_no_children)
23920 && prototype_p (TREE_TYPE (decl))
23921 && stdarg_p (TREE_TYPE (decl)))
23922 gen_unspecified_parameters_die (decl, subr_die);
23925 if (subr_die != old_die)
23926 /* Add the calling convention attribute if requested. */
23927 add_calling_convention_attribute (subr_die, decl);
23929 /* Output Dwarf info for all of the stuff within the body of the function
23930 (if it has one - it may be just a declaration).
23932 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23933 a function. This BLOCK actually represents the outermost binding contour
23934 for the function, i.e. the contour in which the function's formal
23935 parameters and labels get declared. Curiously, it appears that the front
23936 end doesn't actually put the PARM_DECL nodes for the current function onto
23937 the BLOCK_VARS list for this outer scope, but are strung off of the
23938 DECL_ARGUMENTS list for the function instead.
23940 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23941 the LABEL_DECL nodes for the function however, and we output DWARF info
23942 for those in decls_for_scope. Just within the `outer_scope' there will be
23943 a BLOCK node representing the function's outermost pair of curly braces,
23944 and any blocks used for the base and member initializers of a C++
23945 constructor function. */
23946 tree outer_scope = DECL_INITIAL (decl);
23947 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23949 int call_site_note_count = 0;
23950 int tail_call_site_note_count = 0;
23952 /* Emit a DW_TAG_variable DIE for a named return value. */
23953 if (DECL_NAME (DECL_RESULT (decl)))
23954 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23956 /* The first time through decls_for_scope we will generate the
23957 DIEs for the locals. The second time, we fill in the
23958 location info. */
23959 decls_for_scope (outer_scope, subr_die);
23961 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23963 struct call_arg_loc_node *ca_loc;
23964 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23966 dw_die_ref die = NULL;
23967 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23968 rtx arg, next_arg;
23969 tree arg_decl = NULL_TREE;
23971 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23972 ? XEXP (ca_loc->call_arg_loc_note, 0)
23973 : NULL_RTX);
23974 arg; arg = next_arg)
23976 dw_loc_descr_ref reg, val;
23977 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23978 dw_die_ref cdie, tdie = NULL;
23980 next_arg = XEXP (arg, 1);
23981 if (REG_P (XEXP (XEXP (arg, 0), 0))
23982 && next_arg
23983 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23984 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23985 && REGNO (XEXP (XEXP (arg, 0), 0))
23986 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23987 next_arg = XEXP (next_arg, 1);
23988 if (mode == VOIDmode)
23990 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23991 if (mode == VOIDmode)
23992 mode = GET_MODE (XEXP (arg, 0));
23994 if (mode == VOIDmode || mode == BLKmode)
23995 continue;
23996 /* Get dynamic information about call target only if we
23997 have no static information: we cannot generate both
23998 DW_AT_call_origin and DW_AT_call_target
23999 attributes. */
24000 if (ca_loc->symbol_ref == NULL_RTX)
24002 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24004 tloc = XEXP (XEXP (arg, 0), 1);
24005 continue;
24007 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24008 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24010 tlocc = XEXP (XEXP (arg, 0), 1);
24011 continue;
24014 reg = NULL;
24015 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24016 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24017 VAR_INIT_STATUS_INITIALIZED);
24018 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24020 rtx mem = XEXP (XEXP (arg, 0), 0);
24021 reg = mem_loc_descriptor (XEXP (mem, 0),
24022 get_address_mode (mem),
24023 GET_MODE (mem),
24024 VAR_INIT_STATUS_INITIALIZED);
24026 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24027 == DEBUG_PARAMETER_REF)
24029 tree tdecl
24030 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24031 tdie = lookup_decl_die (tdecl);
24032 if (tdie == NULL)
24033 continue;
24034 arg_decl = tdecl;
24036 else
24037 continue;
24038 if (reg == NULL
24039 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24040 != DEBUG_PARAMETER_REF)
24041 continue;
24042 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24043 VOIDmode,
24044 VAR_INIT_STATUS_INITIALIZED);
24045 if (val == NULL)
24046 continue;
24047 if (die == NULL)
24048 die = gen_call_site_die (decl, subr_die, ca_loc);
24049 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24050 NULL_TREE);
24051 add_desc_attribute (cdie, arg_decl);
24052 if (reg != NULL)
24053 add_AT_loc (cdie, DW_AT_location, reg);
24054 else if (tdie != NULL)
24055 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24056 tdie);
24057 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24058 if (next_arg != XEXP (arg, 1))
24060 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24061 if (mode == VOIDmode)
24062 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24063 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24064 0), 1),
24065 mode, VOIDmode,
24066 VAR_INIT_STATUS_INITIALIZED);
24067 if (val != NULL)
24068 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24069 val);
24072 if (die == NULL
24073 && (ca_loc->symbol_ref || tloc))
24074 die = gen_call_site_die (decl, subr_die, ca_loc);
24075 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24077 dw_loc_descr_ref tval = NULL;
24079 if (tloc != NULL_RTX)
24080 tval = mem_loc_descriptor (tloc,
24081 GET_MODE (tloc) == VOIDmode
24082 ? Pmode : GET_MODE (tloc),
24083 VOIDmode,
24084 VAR_INIT_STATUS_INITIALIZED);
24085 if (tval)
24086 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24087 else if (tlocc != NULL_RTX)
24089 tval = mem_loc_descriptor (tlocc,
24090 GET_MODE (tlocc) == VOIDmode
24091 ? Pmode : GET_MODE (tlocc),
24092 VOIDmode,
24093 VAR_INIT_STATUS_INITIALIZED);
24094 if (tval)
24095 add_AT_loc (die,
24096 dwarf_AT (DW_AT_call_target_clobbered),
24097 tval);
24100 if (die != NULL)
24102 call_site_note_count++;
24103 if (ca_loc->tail_call_p)
24104 tail_call_site_note_count++;
24108 call_arg_locations = NULL;
24109 call_arg_loc_last = NULL;
24110 if (tail_call_site_count >= 0
24111 && tail_call_site_count == tail_call_site_note_count
24112 && (!dwarf_strict || dwarf_version >= 5))
24114 if (call_site_count >= 0
24115 && call_site_count == call_site_note_count)
24116 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24117 else
24118 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24120 call_site_count = -1;
24121 tail_call_site_count = -1;
24124 /* Mark used types after we have created DIEs for the functions scopes. */
24125 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24128 /* Returns a hash value for X (which really is a die_struct). */
24130 hashval_t
24131 block_die_hasher::hash (die_struct *d)
24133 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24136 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24137 as decl_id and die_parent of die_struct Y. */
24139 bool
24140 block_die_hasher::equal (die_struct *x, die_struct *y)
24142 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24145 /* Hold information about markers for inlined entry points. */
24146 struct GTY ((for_user)) inline_entry_data
24148 /* The block that's the inlined_function_outer_scope for an inlined
24149 function. */
24150 tree block;
24152 /* The label at the inlined entry point. */
24153 const char *label_pfx;
24154 unsigned int label_num;
24156 /* The view number to be used as the inlined entry point. */
24157 var_loc_view view;
24160 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24162 typedef tree compare_type;
24163 static inline hashval_t hash (const inline_entry_data *);
24164 static inline bool equal (const inline_entry_data *, const_tree);
24167 /* Hash table routines for inline_entry_data. */
24169 inline hashval_t
24170 inline_entry_data_hasher::hash (const inline_entry_data *data)
24172 return htab_hash_pointer (data->block);
24175 inline bool
24176 inline_entry_data_hasher::equal (const inline_entry_data *data,
24177 const_tree block)
24179 return data->block == block;
24182 /* Inlined entry points pending DIE creation in this compilation unit. */
24184 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24187 /* Return TRUE if DECL, which may have been previously generated as
24188 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24189 true if decl (or its origin) is either an extern declaration or a
24190 class/namespace scoped declaration.
24192 The declare_in_namespace support causes us to get two DIEs for one
24193 variable, both of which are declarations. We want to avoid
24194 considering one to be a specification, so we must test for
24195 DECLARATION and DW_AT_declaration. */
24196 static inline bool
24197 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24199 return (old_die && TREE_STATIC (decl) && !declaration
24200 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24203 /* Return true if DECL is a local static. */
24205 static inline bool
24206 local_function_static (tree decl)
24208 gcc_assert (VAR_P (decl));
24209 return TREE_STATIC (decl)
24210 && DECL_CONTEXT (decl)
24211 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24214 /* Return true iff DECL overrides (presumably completes) the type of
24215 OLD_DIE within CONTEXT_DIE. */
24217 static bool
24218 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24219 dw_die_ref context_die)
24221 tree type = TREE_TYPE (decl);
24222 int cv_quals;
24224 if (decl_by_reference_p (decl))
24226 type = TREE_TYPE (type);
24227 cv_quals = TYPE_UNQUALIFIED;
24229 else
24230 cv_quals = decl_quals (decl);
24232 dw_die_ref type_die = modified_type_die (type,
24233 cv_quals | TYPE_QUALS (type),
24234 false,
24235 context_die);
24237 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24239 return type_die != old_type_die;
24242 /* Generate a DIE to represent a declared data object.
24243 Either DECL or ORIGIN must be non-null. */
24245 static void
24246 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24248 HOST_WIDE_INT off = 0;
24249 tree com_decl;
24250 tree decl_or_origin = decl ? decl : origin;
24251 tree ultimate_origin;
24252 dw_die_ref var_die;
24253 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24254 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24255 || class_or_namespace_scope_p (context_die));
24256 bool specialization_p = false;
24257 bool no_linkage_name = false;
24259 /* While C++ inline static data members have definitions inside of the
24260 class, force the first DIE to be a declaration, then let gen_member_die
24261 reparent it to the class context and call gen_variable_die again
24262 to create the outside of the class DIE for the definition. */
24263 if (!declaration
24264 && old_die == NULL
24265 && decl
24266 && DECL_CONTEXT (decl)
24267 && TYPE_P (DECL_CONTEXT (decl))
24268 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24270 declaration = true;
24271 if (dwarf_version < 5)
24272 no_linkage_name = true;
24275 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24276 if (decl || ultimate_origin)
24277 origin = ultimate_origin;
24278 com_decl = fortran_common (decl_or_origin, &off);
24280 /* Symbol in common gets emitted as a child of the common block, in the form
24281 of a data member. */
24282 if (com_decl)
24284 dw_die_ref com_die;
24285 dw_loc_list_ref loc = NULL;
24286 die_node com_die_arg;
24288 var_die = lookup_decl_die (decl_or_origin);
24289 if (var_die)
24291 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24293 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24294 if (loc)
24296 if (off)
24298 /* Optimize the common case. */
24299 if (single_element_loc_list_p (loc)
24300 && loc->expr->dw_loc_opc == DW_OP_addr
24301 && loc->expr->dw_loc_next == NULL
24302 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24303 == SYMBOL_REF)
24305 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24306 loc->expr->dw_loc_oprnd1.v.val_addr
24307 = plus_constant (GET_MODE (x), x , off);
24309 else
24310 loc_list_plus_const (loc, off);
24312 add_AT_location_description (var_die, DW_AT_location, loc);
24313 remove_AT (var_die, DW_AT_declaration);
24316 return;
24319 if (common_block_die_table == NULL)
24320 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24322 com_die_arg.decl_id = DECL_UID (com_decl);
24323 com_die_arg.die_parent = context_die;
24324 com_die = common_block_die_table->find (&com_die_arg);
24325 if (! early_dwarf)
24326 loc = loc_list_from_tree (com_decl, 2, NULL);
24327 if (com_die == NULL)
24329 const char *cnam
24330 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24331 die_node **slot;
24333 com_die = new_die (DW_TAG_common_block, context_die, decl);
24334 add_name_and_src_coords_attributes (com_die, com_decl);
24335 if (loc)
24337 add_AT_location_description (com_die, DW_AT_location, loc);
24338 /* Avoid sharing the same loc descriptor between
24339 DW_TAG_common_block and DW_TAG_variable. */
24340 loc = loc_list_from_tree (com_decl, 2, NULL);
24342 else if (DECL_EXTERNAL (decl_or_origin))
24343 add_AT_flag (com_die, DW_AT_declaration, 1);
24344 if (want_pubnames ())
24345 add_pubname_string (cnam, com_die); /* ??? needed? */
24346 com_die->decl_id = DECL_UID (com_decl);
24347 slot = common_block_die_table->find_slot (com_die, INSERT);
24348 *slot = com_die;
24350 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24352 add_AT_location_description (com_die, DW_AT_location, loc);
24353 loc = loc_list_from_tree (com_decl, 2, NULL);
24354 remove_AT (com_die, DW_AT_declaration);
24356 var_die = new_die (DW_TAG_variable, com_die, decl);
24357 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24358 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24359 decl_quals (decl_or_origin), false,
24360 context_die);
24361 add_alignment_attribute (var_die, decl);
24362 add_AT_flag (var_die, DW_AT_external, 1);
24363 if (loc)
24365 if (off)
24367 /* Optimize the common case. */
24368 if (single_element_loc_list_p (loc)
24369 && loc->expr->dw_loc_opc == DW_OP_addr
24370 && loc->expr->dw_loc_next == NULL
24371 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24373 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24374 loc->expr->dw_loc_oprnd1.v.val_addr
24375 = plus_constant (GET_MODE (x), x, off);
24377 else
24378 loc_list_plus_const (loc, off);
24380 add_AT_location_description (var_die, DW_AT_location, loc);
24382 else if (DECL_EXTERNAL (decl_or_origin))
24383 add_AT_flag (var_die, DW_AT_declaration, 1);
24384 if (decl)
24385 equate_decl_number_to_die (decl, var_die);
24386 return;
24389 if (old_die)
24391 if (declaration)
24393 /* A declaration that has been previously dumped, needs no
24394 further annotations, since it doesn't need location on
24395 the second pass. */
24396 return;
24398 else if (decl_will_get_specification_p (old_die, decl, declaration)
24399 && !get_AT (old_die, DW_AT_specification))
24401 /* Fall-thru so we can make a new variable die along with a
24402 DW_AT_specification. */
24404 else if (origin && old_die->die_parent != context_die)
24406 /* If we will be creating an inlined instance, we need a
24407 new DIE that will get annotated with
24408 DW_AT_abstract_origin. */
24409 gcc_assert (!DECL_ABSTRACT_P (decl));
24411 else
24413 /* If a DIE was dumped early, it still needs location info.
24414 Skip to where we fill the location bits. */
24415 var_die = old_die;
24417 /* ??? In LTRANS we cannot annotate early created variably
24418 modified type DIEs without copying them and adjusting all
24419 references to them. Thus we dumped them again. Also add a
24420 reference to them but beware of -g0 compile and -g link
24421 in which case the reference will be already present. */
24422 tree type = TREE_TYPE (decl_or_origin);
24423 if (in_lto_p
24424 && ! get_AT (var_die, DW_AT_type)
24425 && variably_modified_type_p
24426 (type, decl_function_context (decl_or_origin)))
24428 if (decl_by_reference_p (decl_or_origin))
24429 add_type_attribute (var_die, TREE_TYPE (type),
24430 TYPE_UNQUALIFIED, false, context_die);
24431 else
24432 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24433 false, context_die);
24436 goto gen_variable_die_location;
24440 /* For static data members, the declaration in the class is supposed
24441 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24442 also in DWARF2; the specification should still be DW_TAG_variable
24443 referencing the DW_TAG_member DIE. */
24444 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24445 var_die = new_die (DW_TAG_member, context_die, decl);
24446 else
24447 var_die = new_die (DW_TAG_variable, context_die, decl);
24449 if (origin != NULL)
24450 add_abstract_origin_attribute (var_die, origin);
24452 /* Loop unrolling can create multiple blocks that refer to the same
24453 static variable, so we must test for the DW_AT_declaration flag.
24455 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24456 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24457 sharing them.
24459 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24460 else if (decl_will_get_specification_p (old_die, decl, declaration))
24462 /* This is a definition of a C++ class level static. */
24463 add_AT_specification (var_die, old_die);
24464 specialization_p = true;
24465 if (DECL_NAME (decl))
24467 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24468 struct dwarf_file_data * file_index = lookup_filename (s.file);
24470 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24471 add_AT_file (var_die, DW_AT_decl_file, file_index);
24473 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24474 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24476 if (debug_column_info
24477 && s.column
24478 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24479 != (unsigned) s.column))
24480 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24482 if (old_die->die_tag == DW_TAG_member)
24483 add_linkage_name (var_die, decl);
24486 else
24487 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24489 if ((origin == NULL && !specialization_p)
24490 || (origin != NULL
24491 && !DECL_ABSTRACT_P (decl_or_origin)
24492 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24493 decl_function_context
24494 (decl_or_origin)))
24495 || (old_die && specialization_p
24496 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24498 tree type = TREE_TYPE (decl_or_origin);
24500 if (decl_by_reference_p (decl_or_origin))
24501 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24502 context_die);
24503 else
24504 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24505 context_die);
24508 if (origin == NULL && !specialization_p)
24510 if (TREE_PUBLIC (decl))
24511 add_AT_flag (var_die, DW_AT_external, 1);
24513 if (DECL_ARTIFICIAL (decl))
24514 add_AT_flag (var_die, DW_AT_artificial, 1);
24516 add_alignment_attribute (var_die, decl);
24518 add_accessibility_attribute (var_die, decl);
24521 if (declaration)
24522 add_AT_flag (var_die, DW_AT_declaration, 1);
24524 if (decl && (DECL_ABSTRACT_P (decl)
24525 || !old_die || is_declaration_die (old_die)))
24526 equate_decl_number_to_die (decl, var_die);
24528 gen_variable_die_location:
24529 if (! declaration
24530 && (! DECL_ABSTRACT_P (decl_or_origin)
24531 /* Local static vars are shared between all clones/inlines,
24532 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24533 already set. */
24534 || (VAR_P (decl_or_origin)
24535 && TREE_STATIC (decl_or_origin)
24536 && DECL_RTL_SET_P (decl_or_origin))))
24538 if (early_dwarf)
24540 add_pubname (decl_or_origin, var_die);
24541 /* For global register variables, emit DW_AT_location if possible
24542 already during early_dwarf, as late_global_decl won't be usually
24543 called. */
24544 if (DECL_HARD_REGISTER (decl_or_origin)
24545 && TREE_STATIC (decl_or_origin)
24546 && !decl_by_reference_p (decl_or_origin)
24547 && !get_AT (var_die, DW_AT_location)
24548 && !get_AT (var_die, DW_AT_const_value)
24549 && DECL_RTL_SET_P (decl_or_origin)
24550 && REG_P (DECL_RTL (decl_or_origin)))
24552 dw_loc_descr_ref descr
24553 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24554 VAR_INIT_STATUS_INITIALIZED);
24555 if (descr)
24556 add_AT_loc (var_die, DW_AT_location, descr);
24559 else
24560 add_location_or_const_value_attribute (var_die, decl_or_origin,
24561 decl == NULL);
24563 else
24564 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24566 if ((dwarf_version >= 4 || !dwarf_strict)
24567 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24568 DW_AT_const_expr) == 1
24569 && !get_AT (var_die, DW_AT_const_expr)
24570 && !specialization_p)
24571 add_AT_flag (var_die, DW_AT_const_expr, 1);
24573 if (!dwarf_strict)
24575 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24576 DW_AT_inline);
24577 if (inl != -1
24578 && !get_AT (var_die, DW_AT_inline)
24579 && !specialization_p)
24580 add_AT_unsigned (var_die, DW_AT_inline, inl);
24584 /* Generate a DIE to represent a named constant. */
24586 static void
24587 gen_const_die (tree decl, dw_die_ref context_die)
24589 dw_die_ref const_die;
24590 tree type = TREE_TYPE (decl);
24592 const_die = lookup_decl_die (decl);
24593 if (const_die)
24594 return;
24596 const_die = new_die (DW_TAG_constant, context_die, decl);
24597 equate_decl_number_to_die (decl, const_die);
24598 add_name_and_src_coords_attributes (const_die, decl);
24599 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24600 if (TREE_PUBLIC (decl))
24601 add_AT_flag (const_die, DW_AT_external, 1);
24602 if (DECL_ARTIFICIAL (decl))
24603 add_AT_flag (const_die, DW_AT_artificial, 1);
24604 tree_add_const_value_attribute_for_decl (const_die, decl);
24607 /* Generate a DIE to represent a label identifier. */
24609 static void
24610 gen_label_die (tree decl, dw_die_ref context_die)
24612 tree origin = decl_ultimate_origin (decl);
24613 dw_die_ref lbl_die = lookup_decl_die (decl);
24614 rtx insn;
24615 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24617 if (!lbl_die)
24619 lbl_die = new_die (DW_TAG_label, context_die, decl);
24620 equate_decl_number_to_die (decl, lbl_die);
24622 if (origin != NULL)
24623 add_abstract_origin_attribute (lbl_die, origin);
24624 else
24625 add_name_and_src_coords_attributes (lbl_die, decl);
24628 if (DECL_ABSTRACT_P (decl))
24629 equate_decl_number_to_die (decl, lbl_die);
24630 else if (! early_dwarf)
24632 insn = DECL_RTL_IF_SET (decl);
24634 /* Deleted labels are programmer specified labels which have been
24635 eliminated because of various optimizations. We still emit them
24636 here so that it is possible to put breakpoints on them. */
24637 if (insn
24638 && (LABEL_P (insn)
24639 || ((NOTE_P (insn)
24640 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24642 /* When optimization is enabled (via -O) some parts of the compiler
24643 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24644 represent source-level labels which were explicitly declared by
24645 the user. This really shouldn't be happening though, so catch
24646 it if it ever does happen. */
24647 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24649 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24650 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24652 else if (insn
24653 && NOTE_P (insn)
24654 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24655 && CODE_LABEL_NUMBER (insn) != -1)
24657 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24658 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24663 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24664 attributes to the DIE for a block STMT, to describe where the inlined
24665 function was called from. This is similar to add_src_coords_attributes. */
24667 static inline void
24668 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24670 /* We can end up with BUILTINS_LOCATION here. */
24671 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24672 return;
24674 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24676 if (dwarf_version >= 3 || !dwarf_strict)
24678 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24679 add_AT_unsigned (die, DW_AT_call_line, s.line);
24680 if (debug_column_info && s.column)
24681 add_AT_unsigned (die, DW_AT_call_column, s.column);
24686 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24687 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24689 static inline void
24690 add_high_low_attributes (tree stmt, dw_die_ref die)
24692 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24694 if (inline_entry_data **iedp
24695 = !inline_entry_data_table ? NULL
24696 : inline_entry_data_table->find_slot_with_hash (stmt,
24697 htab_hash_pointer (stmt),
24698 NO_INSERT))
24700 inline_entry_data *ied = *iedp;
24701 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24702 gcc_assert (debug_inline_points);
24703 gcc_assert (inlined_function_outer_scope_p (stmt));
24705 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24706 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24708 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24709 && !dwarf_strict)
24711 if (!output_asm_line_debug_info ())
24712 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24713 else
24715 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24716 /* FIXME: this will resolve to a small number. Could we
24717 possibly emit smaller data? Ideally we'd emit a
24718 uleb128, but that would make the size of DIEs
24719 impossible for the compiler to compute, since it's
24720 the assembler that computes the value of the view
24721 label in this case. Ideally, we'd have a single form
24722 encompassing both the address and the view, and
24723 indirecting them through a table might make things
24724 easier, but even that would be more wasteful,
24725 space-wise, than what we have now. */
24726 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24730 inline_entry_data_table->clear_slot (iedp);
24733 if (BLOCK_FRAGMENT_CHAIN (stmt)
24734 && (dwarf_version >= 3 || !dwarf_strict))
24736 tree chain, superblock = NULL_TREE;
24737 dw_die_ref pdie;
24738 dw_attr_node *attr = NULL;
24740 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24742 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24743 BLOCK_NUMBER (stmt));
24744 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24747 /* Optimize duplicate .debug_ranges lists or even tails of
24748 lists. If this BLOCK has same ranges as its supercontext,
24749 lookup DW_AT_ranges attribute in the supercontext (and
24750 recursively so), verify that the ranges_table contains the
24751 right values and use it instead of adding a new .debug_range. */
24752 for (chain = stmt, pdie = die;
24753 BLOCK_SAME_RANGE (chain);
24754 chain = BLOCK_SUPERCONTEXT (chain))
24756 dw_attr_node *new_attr;
24758 pdie = pdie->die_parent;
24759 if (pdie == NULL)
24760 break;
24761 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24762 break;
24763 new_attr = get_AT (pdie, DW_AT_ranges);
24764 if (new_attr == NULL
24765 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24766 break;
24767 attr = new_attr;
24768 superblock = BLOCK_SUPERCONTEXT (chain);
24770 if (attr != NULL
24771 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24772 == (int)BLOCK_NUMBER (superblock))
24773 && BLOCK_FRAGMENT_CHAIN (superblock))
24775 unsigned long off = attr->dw_attr_val.v.val_offset;
24776 unsigned long supercnt = 0, thiscnt = 0;
24777 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24778 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24780 ++supercnt;
24781 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24782 == (int)BLOCK_NUMBER (chain));
24784 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24785 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24786 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24787 ++thiscnt;
24788 gcc_assert (supercnt >= thiscnt);
24789 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24790 false);
24791 note_rnglist_head (off + supercnt - thiscnt);
24792 return;
24795 unsigned int offset = add_ranges (stmt, true);
24796 add_AT_range_list (die, DW_AT_ranges, offset, false);
24797 note_rnglist_head (offset);
24799 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24800 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24803 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24804 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24805 chain = BLOCK_FRAGMENT_CHAIN (chain);
24807 while (chain);
24808 add_ranges (NULL);
24810 else
24812 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24813 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24814 BLOCK_NUMBER (stmt));
24815 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24816 BLOCK_NUMBER (stmt));
24817 add_AT_low_high_pc (die, label, label_high, false);
24821 /* Generate a DIE for a lexical block. */
24823 static void
24824 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24826 dw_die_ref old_die = lookup_block_die (stmt);
24827 dw_die_ref stmt_die = NULL;
24828 if (!old_die)
24830 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24831 equate_block_to_die (stmt, stmt_die);
24834 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24836 /* If this is an inlined or conrecte instance, create a new lexical
24837 die for anything below to attach DW_AT_abstract_origin to. */
24838 if (old_die)
24839 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24841 tree origin = block_ultimate_origin (stmt);
24842 if (origin != NULL_TREE && (origin != stmt || old_die))
24843 add_abstract_origin_attribute (stmt_die, origin);
24845 old_die = NULL;
24848 if (old_die)
24849 stmt_die = old_die;
24851 /* A non abstract block whose blocks have already been reordered
24852 should have the instruction range for this block. If so, set the
24853 high/low attributes. */
24854 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24856 gcc_assert (stmt_die);
24857 add_high_low_attributes (stmt, stmt_die);
24860 decls_for_scope (stmt, stmt_die);
24863 /* Generate a DIE for an inlined subprogram. */
24865 static void
24866 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24868 tree decl = block_ultimate_origin (stmt);
24870 /* Make sure any inlined functions are known to be inlineable. */
24871 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24872 || cgraph_function_possibly_inlined_p (decl));
24874 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24876 if (call_arg_locations || debug_inline_points)
24877 equate_block_to_die (stmt, subr_die);
24878 add_abstract_origin_attribute (subr_die, decl);
24879 if (TREE_ASM_WRITTEN (stmt))
24880 add_high_low_attributes (stmt, subr_die);
24881 add_call_src_coords_attributes (stmt, subr_die);
24883 /* The inliner creates an extra BLOCK for the parameter setup,
24884 we want to merge that with the actual outermost BLOCK of the
24885 inlined function to avoid duplicate locals in consumers.
24886 Do that by doing the recursion to subblocks on the single subblock
24887 of STMT. */
24888 bool unwrap_one = false;
24889 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24891 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24892 if (origin
24893 && TREE_CODE (origin) == BLOCK
24894 && BLOCK_SUPERCONTEXT (origin) == decl)
24895 unwrap_one = true;
24897 decls_for_scope (stmt, subr_die, !unwrap_one);
24898 if (unwrap_one)
24899 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24902 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24903 the comment for VLR_CONTEXT. */
24905 static void
24906 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24908 dw_die_ref decl_die;
24910 if (TREE_TYPE (decl) == error_mark_node)
24911 return;
24913 decl_die = new_die (DW_TAG_member, context_die, decl);
24914 add_name_and_src_coords_attributes (decl_die, decl);
24915 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24916 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24917 context_die);
24919 if (DECL_BIT_FIELD_TYPE (decl))
24921 add_byte_size_attribute (decl_die, decl);
24922 add_bit_size_attribute (decl_die, decl);
24923 add_bit_offset_attribute (decl_die, decl);
24926 add_alignment_attribute (decl_die, decl);
24928 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24929 add_data_member_location_attribute (decl_die, decl, ctx);
24931 if (DECL_ARTIFICIAL (decl))
24932 add_AT_flag (decl_die, DW_AT_artificial, 1);
24934 add_accessibility_attribute (decl_die, decl);
24936 /* Equate decl number to die, so that we can look up this decl later on. */
24937 equate_decl_number_to_die (decl, decl_die);
24940 /* Generate a DIE for a pointer to a member type. TYPE can be an
24941 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24942 pointer to member function. */
24944 static void
24945 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24947 if (lookup_type_die (type))
24948 return;
24950 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24951 scope_die_for (type, context_die), type);
24953 equate_type_number_to_die (type, ptr_die);
24954 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24955 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24956 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24957 context_die);
24958 add_alignment_attribute (ptr_die, type);
24960 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24961 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24963 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24964 add_AT_loc (ptr_die, DW_AT_use_location, op);
24968 static char *producer_string;
24970 /* Given a C and/or C++ language/version string return the "highest".
24971 C++ is assumed to be "higher" than C in this case. Used for merging
24972 LTO translation unit languages. */
24973 static const char *
24974 highest_c_language (const char *lang1, const char *lang2)
24976 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24977 return "GNU C++23";
24978 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24979 return "GNU C++20";
24980 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24981 return "GNU C++17";
24982 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24983 return "GNU C++14";
24984 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24985 return "GNU C++11";
24986 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24987 return "GNU C++98";
24989 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24990 return "GNU C2X";
24991 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24992 return "GNU C17";
24993 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24994 return "GNU C11";
24995 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24996 return "GNU C99";
24997 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24998 return "GNU C89";
25000 gcc_unreachable ();
25004 /* Generate the DIE for the compilation unit. */
25006 static dw_die_ref
25007 gen_compile_unit_die (const char *filename)
25009 dw_die_ref die;
25010 const char *language_string = lang_hooks.name;
25011 int language;
25013 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25015 if (filename)
25017 add_filename_attribute (die, filename);
25018 /* Don't add cwd for <built-in>. */
25019 if (filename[0] != '<')
25020 add_comp_dir_attribute (die);
25023 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25025 /* If our producer is LTO try to figure out a common language to use
25026 from the global list of translation units. */
25027 if (strcmp (language_string, "GNU GIMPLE") == 0)
25029 unsigned i;
25030 tree t;
25031 const char *common_lang = NULL;
25033 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25035 if (!TRANSLATION_UNIT_LANGUAGE (t))
25036 continue;
25037 if (!common_lang)
25038 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25039 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25041 else if (startswith (common_lang, "GNU C")
25042 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25043 /* Mixing C and C++ is ok, use C++ in that case. */
25044 common_lang = highest_c_language (common_lang,
25045 TRANSLATION_UNIT_LANGUAGE (t));
25046 else
25048 /* Fall back to C. */
25049 common_lang = NULL;
25050 break;
25054 if (common_lang)
25055 language_string = common_lang;
25058 language = DW_LANG_C;
25059 if (startswith (language_string, "GNU C")
25060 && ISDIGIT (language_string[5]))
25062 language = DW_LANG_C89;
25063 if (dwarf_version >= 3 || !dwarf_strict)
25065 if (strcmp (language_string, "GNU C89") != 0)
25066 language = DW_LANG_C99;
25068 if (dwarf_version >= 5 /* || !dwarf_strict */)
25069 if (strcmp (language_string, "GNU C11") == 0
25070 || strcmp (language_string, "GNU C17") == 0
25071 || strcmp (language_string, "GNU C2X") == 0)
25072 language = DW_LANG_C11;
25075 else if (startswith (language_string, "GNU C++"))
25077 language = DW_LANG_C_plus_plus;
25078 if (dwarf_version >= 5 /* || !dwarf_strict */)
25080 if (strcmp (language_string, "GNU C++11") == 0)
25081 language = DW_LANG_C_plus_plus_11;
25082 else if (strcmp (language_string, "GNU C++14") == 0)
25083 language = DW_LANG_C_plus_plus_14;
25084 else if (strcmp (language_string, "GNU C++17") == 0
25085 || strcmp (language_string, "GNU C++20") == 0
25086 || strcmp (language_string, "GNU C++23") == 0)
25087 /* For now. */
25088 language = DW_LANG_C_plus_plus_14;
25091 else if (strcmp (language_string, "GNU F77") == 0)
25092 language = DW_LANG_Fortran77;
25093 else if (dwarf_version >= 3 || !dwarf_strict)
25095 if (strcmp (language_string, "GNU Ada") == 0)
25096 language = DW_LANG_Ada95;
25097 else if (startswith (language_string, "GNU Fortran"))
25099 language = DW_LANG_Fortran95;
25100 if (dwarf_version >= 5 /* || !dwarf_strict */)
25102 if (strcmp (language_string, "GNU Fortran2003") == 0)
25103 language = DW_LANG_Fortran03;
25104 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25105 language = DW_LANG_Fortran08;
25108 else if (strcmp (language_string, "GNU Objective-C") == 0)
25109 language = DW_LANG_ObjC;
25110 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25111 language = DW_LANG_ObjC_plus_plus;
25112 else if (strcmp (language_string, "GNU D") == 0)
25113 language = DW_LANG_D;
25114 else if (dwarf_version >= 5 || !dwarf_strict)
25116 if (strcmp (language_string, "GNU Go") == 0)
25117 language = DW_LANG_Go;
25120 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25121 else if (startswith (language_string, "GNU Fortran"))
25122 language = DW_LANG_Fortran90;
25123 /* Likewise for Ada. */
25124 else if (strcmp (language_string, "GNU Ada") == 0)
25125 language = DW_LANG_Ada83;
25127 add_AT_unsigned (die, DW_AT_language, language);
25129 switch (language)
25131 case DW_LANG_Fortran77:
25132 case DW_LANG_Fortran90:
25133 case DW_LANG_Fortran95:
25134 case DW_LANG_Fortran03:
25135 case DW_LANG_Fortran08:
25136 /* Fortran has case insensitive identifiers and the front-end
25137 lowercases everything. */
25138 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25139 break;
25140 default:
25141 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25142 break;
25144 return die;
25147 /* Generate the DIE for a base class. */
25149 static void
25150 gen_inheritance_die (tree binfo, tree access, tree type,
25151 dw_die_ref context_die)
25153 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25154 struct vlr_context ctx = { type, NULL };
25156 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25157 context_die);
25158 add_data_member_location_attribute (die, binfo, &ctx);
25160 if (BINFO_VIRTUAL_P (binfo))
25161 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25163 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25164 children, otherwise the default is DW_ACCESS_public. In DWARF2
25165 the default has always been DW_ACCESS_private. */
25166 if (access == access_public_node)
25168 if (dwarf_version == 2
25169 || context_die->die_tag == DW_TAG_class_type)
25170 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25172 else if (access == access_protected_node)
25173 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25174 else if (dwarf_version > 2
25175 && context_die->die_tag != DW_TAG_class_type)
25176 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25179 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25180 structure. */
25182 static bool
25183 is_variant_part (tree decl)
25185 return (TREE_CODE (decl) == FIELD_DECL
25186 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25189 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25190 return the FIELD_DECL. Return NULL_TREE otherwise. */
25192 static tree
25193 analyze_discr_in_predicate (tree operand, tree struct_type)
25195 while (CONVERT_EXPR_P (operand))
25196 operand = TREE_OPERAND (operand, 0);
25198 /* Match field access to members of struct_type only. */
25199 if (TREE_CODE (operand) == COMPONENT_REF
25200 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25201 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25202 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25203 return TREE_OPERAND (operand, 1);
25204 else
25205 return NULL_TREE;
25208 /* Check that SRC is a constant integer that can be represented as a native
25209 integer constant (either signed or unsigned). If so, store it into DEST and
25210 return true. Return false otherwise. */
25212 static bool
25213 get_discr_value (tree src, dw_discr_value *dest)
25215 tree discr_type = TREE_TYPE (src);
25217 if (lang_hooks.types.get_debug_type)
25219 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25220 if (debug_type != NULL)
25221 discr_type = debug_type;
25224 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25225 return false;
25227 /* Signedness can vary between the original type and the debug type. This
25228 can happen for character types in Ada for instance: the character type
25229 used for code generation can be signed, to be compatible with the C one,
25230 but from a debugger point of view, it must be unsigned. */
25231 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25232 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25234 if (is_orig_unsigned != is_debug_unsigned)
25235 src = fold_convert (discr_type, src);
25237 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25238 return false;
25240 dest->pos = is_debug_unsigned;
25241 if (is_debug_unsigned)
25242 dest->v.uval = tree_to_uhwi (src);
25243 else
25244 dest->v.sval = tree_to_shwi (src);
25246 return true;
25249 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25250 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25251 store NULL_TREE in DISCR_DECL. Otherwise:
25253 - store the discriminant field in STRUCT_TYPE that controls the variant
25254 part to *DISCR_DECL
25256 - put in *DISCR_LISTS_P an array where for each variant, the item
25257 represents the corresponding matching list of discriminant values.
25259 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25260 the above array.
25262 Note that when the array is allocated (i.e. when the analysis is
25263 successful), it is up to the caller to free the array. */
25265 static void
25266 analyze_variants_discr (tree variant_part_decl,
25267 tree struct_type,
25268 tree *discr_decl,
25269 dw_discr_list_ref **discr_lists_p,
25270 unsigned *discr_lists_length)
25272 tree variant_part_type = TREE_TYPE (variant_part_decl);
25273 tree variant;
25274 dw_discr_list_ref *discr_lists;
25275 unsigned i;
25277 /* Compute how many variants there are in this variant part. */
25278 *discr_lists_length = 0;
25279 for (variant = TYPE_FIELDS (variant_part_type);
25280 variant != NULL_TREE;
25281 variant = DECL_CHAIN (variant))
25282 ++*discr_lists_length;
25284 *discr_decl = NULL_TREE;
25285 *discr_lists_p
25286 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25287 sizeof (**discr_lists_p));
25288 discr_lists = *discr_lists_p;
25290 /* And then analyze all variants to extract discriminant information for all
25291 of them. This analysis is conservative: as soon as we detect something we
25292 do not support, abort everything and pretend we found nothing. */
25293 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25294 variant != NULL_TREE;
25295 variant = DECL_CHAIN (variant), ++i)
25297 tree match_expr = DECL_QUALIFIER (variant);
25299 /* Now, try to analyze the predicate and deduce a discriminant for
25300 it. */
25301 if (match_expr == boolean_true_node)
25302 /* Typically happens for the default variant: it matches all cases that
25303 previous variants rejected. Don't output any matching value for
25304 this one. */
25305 continue;
25307 /* The following loop tries to iterate over each discriminant
25308 possibility: single values or ranges. */
25309 while (match_expr != NULL_TREE)
25311 tree next_round_match_expr;
25312 tree candidate_discr = NULL_TREE;
25313 dw_discr_list_ref new_node = NULL;
25315 /* Possibilities are matched one after the other by nested
25316 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25317 continue with the rest at next iteration. */
25318 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25320 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25321 match_expr = TREE_OPERAND (match_expr, 1);
25323 else
25324 next_round_match_expr = NULL_TREE;
25326 if (match_expr == boolean_false_node)
25327 /* This sub-expression matches nothing: just wait for the next
25328 one. */
25331 else if (TREE_CODE (match_expr) == EQ_EXPR)
25333 /* We are matching: <discr_field> == <integer_cst>
25334 This sub-expression matches a single value. */
25335 tree integer_cst = TREE_OPERAND (match_expr, 1);
25337 candidate_discr
25338 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25339 struct_type);
25341 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25342 if (!get_discr_value (integer_cst,
25343 &new_node->dw_discr_lower_bound))
25344 goto abort;
25345 new_node->dw_discr_range = false;
25348 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25350 /* We are matching:
25351 <discr_field> > <integer_cst>
25352 && <discr_field> < <integer_cst>.
25353 This sub-expression matches the range of values between the
25354 two matched integer constants. Note that comparisons can be
25355 inclusive or exclusive. */
25356 tree candidate_discr_1, candidate_discr_2;
25357 tree lower_cst, upper_cst;
25358 bool lower_cst_included, upper_cst_included;
25359 tree lower_op = TREE_OPERAND (match_expr, 0);
25360 tree upper_op = TREE_OPERAND (match_expr, 1);
25362 /* When the comparison is exclusive, the integer constant is not
25363 the discriminant range bound we are looking for: we will have
25364 to increment or decrement it. */
25365 if (TREE_CODE (lower_op) == GE_EXPR)
25366 lower_cst_included = true;
25367 else if (TREE_CODE (lower_op) == GT_EXPR)
25368 lower_cst_included = false;
25369 else
25370 goto abort;
25372 if (TREE_CODE (upper_op) == LE_EXPR)
25373 upper_cst_included = true;
25374 else if (TREE_CODE (upper_op) == LT_EXPR)
25375 upper_cst_included = false;
25376 else
25377 goto abort;
25379 /* Extract the discriminant from the first operand and check it
25380 is consistant with the same analysis in the second
25381 operand. */
25382 candidate_discr_1
25383 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25384 struct_type);
25385 candidate_discr_2
25386 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25387 struct_type);
25388 if (candidate_discr_1 == candidate_discr_2)
25389 candidate_discr = candidate_discr_1;
25390 else
25391 goto abort;
25393 /* Extract bounds from both. */
25394 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25395 lower_cst = TREE_OPERAND (lower_op, 1);
25396 upper_cst = TREE_OPERAND (upper_op, 1);
25398 if (!lower_cst_included)
25399 lower_cst
25400 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25401 build_int_cst (TREE_TYPE (lower_cst), 1));
25402 if (!upper_cst_included)
25403 upper_cst
25404 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25405 build_int_cst (TREE_TYPE (upper_cst), 1));
25407 if (!get_discr_value (lower_cst,
25408 &new_node->dw_discr_lower_bound)
25409 || !get_discr_value (upper_cst,
25410 &new_node->dw_discr_upper_bound))
25411 goto abort;
25413 new_node->dw_discr_range = true;
25416 else if ((candidate_discr
25417 = analyze_discr_in_predicate (match_expr, struct_type))
25418 && (TREE_TYPE (candidate_discr) == boolean_type_node
25419 || TREE_TYPE (TREE_TYPE (candidate_discr))
25420 == boolean_type_node))
25422 /* We are matching: <discr_field> for a boolean discriminant.
25423 This sub-expression matches boolean_true_node. */
25424 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25425 if (!get_discr_value (boolean_true_node,
25426 &new_node->dw_discr_lower_bound))
25427 goto abort;
25428 new_node->dw_discr_range = false;
25431 else
25432 /* Unsupported sub-expression: we cannot determine the set of
25433 matching discriminant values. Abort everything. */
25434 goto abort;
25436 /* If the discriminant info is not consistant with what we saw so
25437 far, consider the analysis failed and abort everything. */
25438 if (candidate_discr == NULL_TREE
25439 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25440 goto abort;
25441 else
25442 *discr_decl = candidate_discr;
25444 if (new_node != NULL)
25446 new_node->dw_discr_next = discr_lists[i];
25447 discr_lists[i] = new_node;
25449 match_expr = next_round_match_expr;
25453 /* If we reach this point, we could match everything we were interested
25454 in. */
25455 return;
25457 abort:
25458 /* Clean all data structure and return no result. */
25459 free (*discr_lists_p);
25460 *discr_lists_p = NULL;
25461 *discr_decl = NULL_TREE;
25464 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25465 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25466 under CONTEXT_DIE.
25468 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25469 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25470 this type, which are record types, represent the available variants and each
25471 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25472 values are inferred from these attributes.
25474 In trees, the offsets for the fields inside these sub-records are relative
25475 to the variant part itself, whereas the corresponding DIEs should have
25476 offset attributes that are relative to the embedding record base address.
25477 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25478 must be an expression that computes the offset of the variant part to
25479 describe in DWARF. */
25481 static void
25482 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25483 dw_die_ref context_die)
25485 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25486 tree variant_part_offset = vlr_ctx->variant_part_offset;
25488 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25489 NULL_TREE if there is no such field. */
25490 tree discr_decl = NULL_TREE;
25491 dw_discr_list_ref *discr_lists;
25492 unsigned discr_lists_length = 0;
25493 unsigned i;
25495 dw_die_ref dwarf_proc_die = NULL;
25496 dw_die_ref variant_part_die
25497 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25499 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25501 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25502 &discr_decl, &discr_lists, &discr_lists_length);
25504 if (discr_decl != NULL_TREE)
25506 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25508 if (discr_die)
25509 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25510 else
25511 /* We have no DIE for the discriminant, so just discard all
25512 discrimimant information in the output. */
25513 discr_decl = NULL_TREE;
25516 /* If the offset for this variant part is more complex than a constant,
25517 create a DWARF procedure for it so that we will not have to generate
25518 DWARF expressions for it for each member. */
25519 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25520 && (dwarf_version >= 3 || !dwarf_strict))
25522 struct loc_descr_context ctx = {
25523 vlr_ctx->struct_type, /* context_type */
25524 NULL_TREE, /* base_decl */
25525 NULL, /* dpi */
25526 false, /* placeholder_arg */
25527 false, /* placeholder_seen */
25528 false /* strict_signedness */
25530 const tree dwarf_proc_fndecl
25531 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25532 build_function_type (TREE_TYPE (variant_part_offset),
25533 NULL_TREE));
25534 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25535 const dw_loc_descr_ref dwarf_proc_body
25536 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25538 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25539 dwarf_proc_fndecl, context_die);
25540 if (dwarf_proc_die != NULL)
25541 variant_part_offset = dwarf_proc_call;
25544 /* Output DIEs for all variants. */
25545 i = 0;
25546 for (tree variant = TYPE_FIELDS (variant_part_type);
25547 variant != NULL_TREE;
25548 variant = DECL_CHAIN (variant), ++i)
25550 tree variant_type = TREE_TYPE (variant);
25551 dw_die_ref variant_die;
25553 /* All variants (i.e. members of a variant part) are supposed to be
25554 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25555 under these records. */
25556 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25558 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25559 equate_decl_number_to_die (variant, variant_die);
25561 /* Output discriminant values this variant matches, if any. */
25562 if (discr_decl == NULL || discr_lists[i] == NULL)
25563 /* In the case we have discriminant information at all, this is
25564 probably the default variant: as the standard says, don't
25565 output any discriminant value/list attribute. */
25567 else if (discr_lists[i]->dw_discr_next == NULL
25568 && !discr_lists[i]->dw_discr_range)
25569 /* If there is only one accepted value, don't bother outputting a
25570 list. */
25571 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25572 else
25573 add_discr_list (variant_die, discr_lists[i]);
25575 for (tree member = TYPE_FIELDS (variant_type);
25576 member != NULL_TREE;
25577 member = DECL_CHAIN (member))
25579 struct vlr_context vlr_sub_ctx = {
25580 vlr_ctx->struct_type, /* struct_type */
25581 NULL /* variant_part_offset */
25583 if (is_variant_part (member))
25585 /* All offsets for fields inside variant parts are relative to
25586 the top-level embedding RECORD_TYPE's base address. On the
25587 other hand, offsets in GCC's types are relative to the
25588 nested-most variant part. So we have to sum offsets each time
25589 we recurse. */
25591 vlr_sub_ctx.variant_part_offset
25592 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25593 variant_part_offset, byte_position (member));
25594 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25596 else
25598 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25599 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25604 free (discr_lists);
25607 /* Generate a DIE for a class member. */
25609 static void
25610 gen_member_die (tree type, dw_die_ref context_die)
25612 tree member;
25613 tree binfo = TYPE_BINFO (type);
25615 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25617 /* If this is not an incomplete type, output descriptions of each of its
25618 members. Note that as we output the DIEs necessary to represent the
25619 members of this record or union type, we will also be trying to output
25620 DIEs to represent the *types* of those members. However the `type'
25621 function (above) will specifically avoid generating type DIEs for member
25622 types *within* the list of member DIEs for this (containing) type except
25623 for those types (of members) which are explicitly marked as also being
25624 members of this (containing) type themselves. The g++ front- end can
25625 force any given type to be treated as a member of some other (containing)
25626 type by setting the TYPE_CONTEXT of the given (member) type to point to
25627 the TREE node representing the appropriate (containing) type. */
25629 /* First output info about the base classes. */
25630 if (binfo && early_dwarf)
25632 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25633 int i;
25634 tree base;
25636 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25637 gen_inheritance_die (base,
25638 (accesses ? (*accesses)[i] : access_public_node),
25639 type,
25640 context_die);
25643 /* Now output info about the members. */
25644 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25646 /* Ignore clones. */
25647 if (DECL_ABSTRACT_ORIGIN (member))
25648 continue;
25650 struct vlr_context vlr_ctx = { type, NULL_TREE };
25651 bool static_inline_p
25652 = (VAR_P (member)
25653 && TREE_STATIC (member)
25654 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25655 != -1));
25657 /* If we thought we were generating minimal debug info for TYPE
25658 and then changed our minds, some of the member declarations
25659 may have already been defined. Don't define them again, but
25660 do put them in the right order. */
25662 if (dw_die_ref child = lookup_decl_die (member))
25664 /* Handle inline static data members, which only have in-class
25665 declarations. */
25666 bool splice = true;
25668 dw_die_ref ref = NULL;
25669 if (child->die_tag == DW_TAG_variable
25670 && child->die_parent == comp_unit_die ())
25672 ref = get_AT_ref (child, DW_AT_specification);
25674 /* For C++17 inline static data members followed by redundant
25675 out of class redeclaration, we might get here with
25676 child being the DIE created for the out of class
25677 redeclaration and with its DW_AT_specification being
25678 the DIE created for in-class definition. We want to
25679 reparent the latter, and don't want to create another
25680 DIE with DW_AT_specification in that case, because
25681 we already have one. */
25682 if (ref
25683 && static_inline_p
25684 && ref->die_tag == DW_TAG_variable
25685 && ref->die_parent == comp_unit_die ()
25686 && get_AT (ref, DW_AT_specification) == NULL)
25688 child = ref;
25689 ref = NULL;
25690 static_inline_p = false;
25693 if (!ref)
25695 reparent_child (child, context_die);
25696 if (dwarf_version < 5)
25697 child->die_tag = DW_TAG_member;
25698 splice = false;
25701 else if (child->die_tag == DW_TAG_enumerator)
25702 /* Enumerators remain under their enumeration even if
25703 their names are introduced in the enclosing scope. */
25704 splice = false;
25706 if (splice)
25707 splice_child_die (context_die, child);
25710 /* Do not generate DWARF for variant parts if we are generating the
25711 corresponding GNAT encodings: DIEs generated for the two schemes
25712 would conflict in our mappings. */
25713 else if (is_variant_part (member)
25714 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25716 vlr_ctx.variant_part_offset = byte_position (member);
25717 gen_variant_part (member, &vlr_ctx, context_die);
25719 else
25721 vlr_ctx.variant_part_offset = NULL_TREE;
25722 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25725 /* For C++ inline static data members emit immediately a DW_TAG_variable
25726 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25727 DW_AT_specification. */
25728 if (static_inline_p)
25730 int old_extern = DECL_EXTERNAL (member);
25731 DECL_EXTERNAL (member) = 0;
25732 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25733 DECL_EXTERNAL (member) = old_extern;
25738 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25739 is set, we pretend that the type was never defined, so we only get the
25740 member DIEs needed by later specification DIEs. */
25742 static void
25743 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25744 enum debug_info_usage usage)
25746 if (TREE_ASM_WRITTEN (type))
25748 /* Fill in the bound of variable-length fields in late dwarf if
25749 still incomplete. */
25750 if (!early_dwarf && variably_modified_type_p (type, NULL))
25751 for (tree member = TYPE_FIELDS (type);
25752 member;
25753 member = DECL_CHAIN (member))
25754 fill_variable_array_bounds (TREE_TYPE (member));
25755 return;
25758 dw_die_ref type_die = lookup_type_die (type);
25759 dw_die_ref scope_die = 0;
25760 int nested = 0;
25761 int complete = (TYPE_SIZE (type)
25762 && (! TYPE_STUB_DECL (type)
25763 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25764 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25765 complete = complete && should_emit_struct_debug (type, usage);
25767 if (type_die && ! complete)
25768 return;
25770 if (TYPE_CONTEXT (type) != NULL_TREE
25771 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25772 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25773 nested = 1;
25775 scope_die = scope_die_for (type, context_die);
25777 /* Generate child dies for template parameters. */
25778 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25779 schedule_generic_params_dies_gen (type);
25781 if (! type_die || (nested && is_cu_die (scope_die)))
25782 /* First occurrence of type or toplevel definition of nested class. */
25784 dw_die_ref old_die = type_die;
25786 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25787 ? record_type_tag (type) : DW_TAG_union_type,
25788 scope_die, type);
25789 equate_type_number_to_die (type, type_die);
25790 if (old_die)
25791 add_AT_specification (type_die, old_die);
25792 else
25793 add_name_attribute (type_die, type_tag (type));
25795 else
25796 remove_AT (type_die, DW_AT_declaration);
25798 /* If this type has been completed, then give it a byte_size attribute and
25799 then give a list of members. */
25800 if (complete && !ns_decl)
25802 /* Prevent infinite recursion in cases where the type of some member of
25803 this type is expressed in terms of this type itself. */
25804 TREE_ASM_WRITTEN (type) = 1;
25805 add_byte_size_attribute (type_die, type);
25806 add_alignment_attribute (type_die, type);
25807 if (TYPE_STUB_DECL (type) != NULL_TREE)
25809 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25810 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25813 /* If the first reference to this type was as the return type of an
25814 inline function, then it may not have a parent. Fix this now. */
25815 if (type_die->die_parent == NULL)
25816 add_child_die (scope_die, type_die);
25818 gen_member_die (type, type_die);
25820 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25821 if (TYPE_ARTIFICIAL (type))
25822 add_AT_flag (type_die, DW_AT_artificial, 1);
25824 /* GNU extension: Record what type our vtable lives in. */
25825 if (TYPE_VFIELD (type))
25827 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25829 gen_type_die (vtype, context_die);
25830 add_AT_die_ref (type_die, DW_AT_containing_type,
25831 lookup_type_die (vtype));
25834 else
25836 add_AT_flag (type_die, DW_AT_declaration, 1);
25838 /* We don't need to do this for function-local types. */
25839 if (TYPE_STUB_DECL (type)
25840 && ! decl_function_context (TYPE_STUB_DECL (type)))
25841 vec_safe_push (incomplete_types, type);
25844 if (get_AT (type_die, DW_AT_name))
25845 add_pubtype (type, type_die);
25848 /* Generate a DIE for a subroutine _type_. */
25850 static void
25851 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25853 tree return_type = TREE_TYPE (type);
25854 dw_die_ref subr_die
25855 = new_die (DW_TAG_subroutine_type,
25856 scope_die_for (type, context_die), type);
25858 equate_type_number_to_die (type, subr_die);
25859 add_prototyped_attribute (subr_die, type);
25860 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25861 context_die);
25862 add_alignment_attribute (subr_die, type);
25863 gen_formal_types_die (type, subr_die);
25865 if (get_AT (subr_die, DW_AT_name))
25866 add_pubtype (type, subr_die);
25867 if ((dwarf_version >= 5 || !dwarf_strict)
25868 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25869 add_AT_flag (subr_die, DW_AT_reference, 1);
25870 if ((dwarf_version >= 5 || !dwarf_strict)
25871 && lang_hooks.types.type_dwarf_attribute (type,
25872 DW_AT_rvalue_reference) != -1)
25873 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25876 /* Generate a DIE for a type definition. */
25878 static void
25879 gen_typedef_die (tree decl, dw_die_ref context_die)
25881 dw_die_ref type_die;
25882 tree type;
25884 if (TREE_ASM_WRITTEN (decl))
25886 if (DECL_ORIGINAL_TYPE (decl))
25887 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25888 return;
25891 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25892 checks in process_scope_var and modified_type_die), this should be called
25893 only for original types. */
25894 gcc_assert (decl_ultimate_origin (decl) == NULL
25895 || decl_ultimate_origin (decl) == decl);
25897 TREE_ASM_WRITTEN (decl) = 1;
25898 type_die = new_die (DW_TAG_typedef, context_die, decl);
25900 add_name_and_src_coords_attributes (type_die, decl);
25901 if (DECL_ORIGINAL_TYPE (decl))
25903 type = DECL_ORIGINAL_TYPE (decl);
25904 if (type == error_mark_node)
25905 return;
25907 gcc_assert (type != TREE_TYPE (decl));
25908 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25910 else
25912 type = TREE_TYPE (decl);
25913 if (type == error_mark_node)
25914 return;
25916 if (is_naming_typedef_decl (TYPE_NAME (type)))
25918 /* Here, we are in the case of decl being a typedef naming
25919 an anonymous type, e.g:
25920 typedef struct {...} foo;
25921 In that case TREE_TYPE (decl) is not a typedef variant
25922 type and TYPE_NAME of the anonymous type is set to the
25923 TYPE_DECL of the typedef. This construct is emitted by
25924 the C++ FE.
25926 TYPE is the anonymous struct named by the typedef
25927 DECL. As we need the DW_AT_type attribute of the
25928 DW_TAG_typedef to point to the DIE of TYPE, let's
25929 generate that DIE right away. add_type_attribute
25930 called below will then pick (via lookup_type_die) that
25931 anonymous struct DIE. */
25932 if (!TREE_ASM_WRITTEN (type))
25933 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25935 /* This is a GNU Extension. We are adding a
25936 DW_AT_linkage_name attribute to the DIE of the
25937 anonymous struct TYPE. The value of that attribute
25938 is the name of the typedef decl naming the anonymous
25939 struct. This greatly eases the work of consumers of
25940 this debug info. */
25941 add_linkage_name_raw (lookup_type_die (type), decl);
25945 add_type_attribute (type_die, type, decl_quals (decl), false,
25946 context_die);
25948 if (is_naming_typedef_decl (decl))
25949 /* We want that all subsequent calls to lookup_type_die with
25950 TYPE in argument yield the DW_TAG_typedef we have just
25951 created. */
25952 equate_type_number_to_die (type, type_die);
25954 add_alignment_attribute (type_die, TREE_TYPE (decl));
25956 add_accessibility_attribute (type_die, decl);
25958 if (DECL_ABSTRACT_P (decl))
25959 equate_decl_number_to_die (decl, type_die);
25961 if (get_AT (type_die, DW_AT_name))
25962 add_pubtype (decl, type_die);
25965 /* Generate a DIE for a struct, class, enum or union type. */
25967 static void
25968 gen_tagged_type_die (tree type,
25969 dw_die_ref context_die,
25970 enum debug_info_usage usage)
25972 if (type == NULL_TREE
25973 || !is_tagged_type (type))
25974 return;
25976 if (TREE_ASM_WRITTEN (type))
25978 /* If this is a nested type whose containing class hasn't been written
25979 out yet, writing it out will cover this one, too. This does not apply
25980 to instantiations of member class templates; they need to be added to
25981 the containing class as they are generated. FIXME: This hurts the
25982 idea of combining type decls from multiple TUs, since we can't predict
25983 what set of template instantiations we'll get. */
25984 else if (TYPE_CONTEXT (type)
25985 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25986 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25988 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25990 if (TREE_ASM_WRITTEN (type))
25991 return;
25993 /* If that failed, attach ourselves to the stub. */
25994 context_die = lookup_type_die (TYPE_CONTEXT (type));
25996 else if (TYPE_CONTEXT (type) != NULL_TREE
25997 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25999 /* If this type is local to a function that hasn't been written
26000 out yet, use a NULL context for now; it will be fixed up in
26001 decls_for_scope. */
26002 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26003 /* A declaration DIE doesn't count; nested types need to go in the
26004 specification. */
26005 if (context_die && is_declaration_die (context_die))
26006 context_die = NULL;
26008 else
26009 context_die = declare_in_namespace (type, context_die);
26011 if (TREE_CODE (type) == ENUMERAL_TYPE)
26013 /* This might have been written out by the call to
26014 declare_in_namespace. */
26015 if (!TREE_ASM_WRITTEN (type))
26016 gen_enumeration_type_die (type, context_die);
26018 else
26019 gen_struct_or_union_type_die (type, context_die, usage);
26021 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26022 it up if it is ever completed. gen_*_type_die will set it for us
26023 when appropriate. */
26026 /* Generate a type description DIE. */
26028 static void
26029 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26030 enum debug_info_usage usage)
26032 struct array_descr_info info;
26034 if (type == NULL_TREE || type == error_mark_node)
26035 return;
26037 if (flag_checking && type)
26038 verify_type (type);
26040 if (TYPE_NAME (type) != NULL_TREE
26041 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26042 && is_redundant_typedef (TYPE_NAME (type))
26043 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26044 /* The DECL of this type is a typedef we don't want to emit debug
26045 info for but we want debug info for its underlying typedef.
26046 This can happen for e.g, the injected-class-name of a C++
26047 type. */
26048 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26050 /* If TYPE is a typedef type variant, let's generate debug info
26051 for the parent typedef which TYPE is a type of. */
26052 if (typedef_variant_p (type))
26054 if (TREE_ASM_WRITTEN (type))
26055 return;
26057 tree name = TYPE_NAME (type);
26058 tree origin = decl_ultimate_origin (name);
26059 if (origin != NULL && origin != name)
26061 gen_decl_die (origin, NULL, NULL, context_die);
26062 return;
26065 /* Prevent broken recursion; we can't hand off to the same type. */
26066 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26068 /* Give typedefs the right scope. */
26069 context_die = scope_die_for (type, context_die);
26071 TREE_ASM_WRITTEN (type) = 1;
26073 gen_decl_die (name, NULL, NULL, context_die);
26074 return;
26077 /* If type is an anonymous tagged type named by a typedef, let's
26078 generate debug info for the typedef. */
26079 if (is_naming_typedef_decl (TYPE_NAME (type)))
26081 /* Give typedefs the right scope. */
26082 context_die = scope_die_for (type, context_die);
26084 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26085 return;
26088 if (lang_hooks.types.get_debug_type)
26090 tree debug_type = lang_hooks.types.get_debug_type (type);
26092 if (debug_type != NULL_TREE && debug_type != type)
26094 gen_type_die_with_usage (debug_type, context_die, usage);
26095 return;
26099 /* We are going to output a DIE to represent the unqualified version
26100 of this type (i.e. without any const or volatile qualifiers) so
26101 get the main variant (i.e. the unqualified version) of this type
26102 now. (Vectors and arrays are special because the debugging info is in the
26103 cloned type itself. Similarly function/method types can contain extra
26104 ref-qualification). */
26105 if (TREE_CODE (type) == FUNCTION_TYPE
26106 || TREE_CODE (type) == METHOD_TYPE)
26108 /* For function/method types, can't use type_main_variant here,
26109 because that can have different ref-qualifiers for C++,
26110 but try to canonicalize. */
26111 tree main = TYPE_MAIN_VARIANT (type);
26112 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26113 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26114 && check_base_type (t, main)
26115 && check_lang_type (t, type))
26117 type = t;
26118 break;
26121 else if (TREE_CODE (type) != VECTOR_TYPE
26122 && TREE_CODE (type) != ARRAY_TYPE)
26123 type = type_main_variant (type);
26125 /* If this is an array type with hidden descriptor, handle it first. */
26126 if (!TREE_ASM_WRITTEN (type)
26127 && lang_hooks.types.get_array_descr_info)
26129 memset (&info, 0, sizeof (info));
26130 if (lang_hooks.types.get_array_descr_info (type, &info))
26132 /* Fortran sometimes emits array types with no dimension. */
26133 gcc_assert (info.ndimensions >= 0
26134 && (info.ndimensions
26135 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26136 gen_descr_array_type_die (type, &info, context_die);
26137 TREE_ASM_WRITTEN (type) = 1;
26138 return;
26142 if (TREE_ASM_WRITTEN (type))
26144 /* Variable-length types may be incomplete even if
26145 TREE_ASM_WRITTEN. For such types, fall through to
26146 gen_array_type_die() and possibly fill in
26147 DW_AT_{upper,lower}_bound attributes. */
26148 if ((TREE_CODE (type) != ARRAY_TYPE
26149 && TREE_CODE (type) != RECORD_TYPE
26150 && TREE_CODE (type) != UNION_TYPE
26151 && TREE_CODE (type) != QUAL_UNION_TYPE)
26152 || !variably_modified_type_p (type, NULL))
26153 return;
26156 switch (TREE_CODE (type))
26158 case ERROR_MARK:
26159 break;
26161 case POINTER_TYPE:
26162 case REFERENCE_TYPE:
26163 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26164 ensures that the gen_type_die recursion will terminate even if the
26165 type is recursive. Recursive types are possible in Ada. */
26166 /* ??? We could perhaps do this for all types before the switch
26167 statement. */
26168 TREE_ASM_WRITTEN (type) = 1;
26170 /* For these types, all that is required is that we output a DIE (or a
26171 set of DIEs) to represent the "basis" type. */
26172 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26173 DINFO_USAGE_IND_USE);
26174 break;
26176 case OFFSET_TYPE:
26177 /* This code is used for C++ pointer-to-data-member types.
26178 Output a description of the relevant class type. */
26179 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26180 DINFO_USAGE_IND_USE);
26182 /* Output a description of the type of the object pointed to. */
26183 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26184 DINFO_USAGE_IND_USE);
26186 /* Now output a DIE to represent this pointer-to-data-member type
26187 itself. */
26188 gen_ptr_to_mbr_type_die (type, context_die);
26189 break;
26191 case FUNCTION_TYPE:
26192 /* Force out return type (in case it wasn't forced out already). */
26193 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26194 DINFO_USAGE_DIR_USE);
26195 gen_subroutine_type_die (type, context_die);
26196 break;
26198 case METHOD_TYPE:
26199 /* Force out return type (in case it wasn't forced out already). */
26200 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26201 DINFO_USAGE_DIR_USE);
26202 gen_subroutine_type_die (type, context_die);
26203 break;
26205 case ARRAY_TYPE:
26206 case VECTOR_TYPE:
26207 gen_array_type_die (type, context_die);
26208 break;
26210 case ENUMERAL_TYPE:
26211 case RECORD_TYPE:
26212 case UNION_TYPE:
26213 case QUAL_UNION_TYPE:
26214 gen_tagged_type_die (type, context_die, usage);
26215 return;
26217 case VOID_TYPE:
26218 case OPAQUE_TYPE:
26219 case INTEGER_TYPE:
26220 case REAL_TYPE:
26221 case FIXED_POINT_TYPE:
26222 case COMPLEX_TYPE:
26223 case BOOLEAN_TYPE:
26224 /* No DIEs needed for fundamental types. */
26225 break;
26227 case NULLPTR_TYPE:
26228 case LANG_TYPE:
26229 /* Just use DW_TAG_unspecified_type. */
26231 dw_die_ref type_die = lookup_type_die (type);
26232 if (type_die == NULL)
26234 tree name = TYPE_IDENTIFIER (type);
26235 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26236 type);
26237 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26238 equate_type_number_to_die (type, type_die);
26241 break;
26243 default:
26244 if (is_cxx_auto (type))
26246 tree name = TYPE_IDENTIFIER (type);
26247 dw_die_ref *die = (name == get_identifier ("auto")
26248 ? &auto_die : &decltype_auto_die);
26249 if (!*die)
26251 *die = new_die (DW_TAG_unspecified_type,
26252 comp_unit_die (), NULL_TREE);
26253 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26255 equate_type_number_to_die (type, *die);
26256 break;
26258 gcc_unreachable ();
26261 TREE_ASM_WRITTEN (type) = 1;
26264 static void
26265 gen_type_die (tree type, dw_die_ref context_die)
26267 if (type != error_mark_node)
26269 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26270 if (flag_checking)
26272 dw_die_ref die = lookup_type_die (type);
26273 if (die)
26274 check_die (die);
26279 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26280 things which are local to the given block. */
26282 static void
26283 gen_block_die (tree stmt, dw_die_ref context_die)
26285 int must_output_die = 0;
26286 bool inlined_func;
26288 /* Ignore blocks that are NULL. */
26289 if (stmt == NULL_TREE)
26290 return;
26292 inlined_func = inlined_function_outer_scope_p (stmt);
26294 /* If the block is one fragment of a non-contiguous block, do not
26295 process the variables, since they will have been done by the
26296 origin block. Do process subblocks. */
26297 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26299 tree sub;
26301 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26302 gen_block_die (sub, context_die);
26304 return;
26307 /* Determine if we need to output any Dwarf DIEs at all to represent this
26308 block. */
26309 if (inlined_func)
26310 /* The outer scopes for inlinings *must* always be represented. We
26311 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26312 must_output_die = 1;
26313 else if (lookup_block_die (stmt))
26314 /* If we already have a DIE then it was filled early. Meanwhile
26315 we might have pruned all BLOCK_VARS as optimized out but we
26316 still want to generate high/low PC attributes so output it. */
26317 must_output_die = 1;
26318 else if (TREE_USED (stmt)
26319 || TREE_ASM_WRITTEN (stmt))
26321 /* Determine if this block directly contains any "significant"
26322 local declarations which we will need to output DIEs for. */
26323 if (debug_info_level > DINFO_LEVEL_TERSE)
26325 /* We are not in terse mode so any local declaration that
26326 is not ignored for debug purposes counts as being a
26327 "significant" one. */
26328 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26329 must_output_die = 1;
26330 else
26331 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26332 if (!DECL_IGNORED_P (var))
26334 must_output_die = 1;
26335 break;
26338 else if (!dwarf2out_ignore_block (stmt))
26339 must_output_die = 1;
26342 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26343 DIE for any block which contains no significant local declarations at
26344 all. Rather, in such cases we just call `decls_for_scope' so that any
26345 needed Dwarf info for any sub-blocks will get properly generated. Note
26346 that in terse mode, our definition of what constitutes a "significant"
26347 local declaration gets restricted to include only inlined function
26348 instances and local (nested) function definitions. */
26349 if (must_output_die)
26351 if (inlined_func)
26352 gen_inlined_subroutine_die (stmt, context_die);
26353 else
26354 gen_lexical_block_die (stmt, context_die);
26356 else
26357 decls_for_scope (stmt, context_die);
26360 /* Process variable DECL (or variable with origin ORIGIN) within
26361 block STMT and add it to CONTEXT_DIE. */
26362 static void
26363 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26365 dw_die_ref die;
26366 tree decl_or_origin = decl ? decl : origin;
26368 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26369 die = lookup_decl_die (decl_or_origin);
26370 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26372 if (TYPE_DECL_IS_STUB (decl_or_origin))
26373 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26374 else
26375 die = lookup_decl_die (decl_or_origin);
26376 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26377 if (! die && ! early_dwarf)
26378 return;
26380 else
26381 die = NULL;
26383 /* Avoid creating DIEs for local typedefs and concrete static variables that
26384 will only be pruned later. */
26385 if ((origin || decl_ultimate_origin (decl))
26386 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26387 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26389 origin = decl_ultimate_origin (decl_or_origin);
26390 if (decl && VAR_P (decl) && die != NULL)
26392 die = lookup_decl_die (origin);
26393 if (die != NULL)
26394 equate_decl_number_to_die (decl, die);
26396 return;
26399 if (die != NULL && die->die_parent == NULL)
26400 add_child_die (context_die, die);
26401 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26403 if (early_dwarf)
26404 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26405 stmt, context_die);
26407 else
26409 if (decl && DECL_P (decl))
26411 die = lookup_decl_die (decl);
26413 /* Early created DIEs do not have a parent as the decls refer
26414 to the function as DECL_CONTEXT rather than the BLOCK. */
26415 if (die && die->die_parent == NULL)
26417 gcc_assert (in_lto_p);
26418 add_child_die (context_die, die);
26422 gen_decl_die (decl, origin, NULL, context_die);
26426 /* Generate all of the decls declared within a given scope and (recursively)
26427 all of its sub-blocks. */
26429 static void
26430 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26432 tree decl;
26433 unsigned int i;
26434 tree subblocks;
26436 /* Ignore NULL blocks. */
26437 if (stmt == NULL_TREE)
26438 return;
26440 /* Output the DIEs to represent all of the data objects and typedefs
26441 declared directly within this block but not within any nested
26442 sub-blocks. Also, nested function and tag DIEs have been
26443 generated with a parent of NULL; fix that up now. We don't
26444 have to do this if we're at -g1. */
26445 if (debug_info_level > DINFO_LEVEL_TERSE)
26447 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26448 process_scope_var (stmt, decl, NULL_TREE, context_die);
26449 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26450 origin - avoid doing this twice as we have no good way to see
26451 if we've done it once already. */
26452 if (! early_dwarf)
26453 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26455 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26456 if (decl == current_function_decl)
26457 /* Ignore declarations of the current function, while they
26458 are declarations, gen_subprogram_die would treat them
26459 as definitions again, because they are equal to
26460 current_function_decl and endlessly recurse. */;
26461 else if (TREE_CODE (decl) == FUNCTION_DECL)
26462 process_scope_var (stmt, decl, NULL_TREE, context_die);
26463 else
26464 process_scope_var (stmt, NULL_TREE, decl, context_die);
26468 /* Even if we're at -g1, we need to process the subblocks in order to get
26469 inlined call information. */
26471 /* Output the DIEs to represent all sub-blocks (and the items declared
26472 therein) of this block. */
26473 if (recurse)
26474 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26475 subblocks != NULL;
26476 subblocks = BLOCK_CHAIN (subblocks))
26477 gen_block_die (subblocks, context_die);
26480 /* Is this a typedef we can avoid emitting? */
26482 static bool
26483 is_redundant_typedef (const_tree decl)
26485 if (TYPE_DECL_IS_STUB (decl))
26486 return true;
26488 if (DECL_ARTIFICIAL (decl)
26489 && DECL_CONTEXT (decl)
26490 && is_tagged_type (DECL_CONTEXT (decl))
26491 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26492 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26493 /* Also ignore the artificial member typedef for the class name. */
26494 return true;
26496 return false;
26499 /* Return TRUE if TYPE is a typedef that names a type for linkage
26500 purposes. This kind of typedefs is produced by the C++ FE for
26501 constructs like:
26503 typedef struct {...} foo;
26505 In that case, there is no typedef variant type produced for foo.
26506 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26507 struct type. */
26509 static bool
26510 is_naming_typedef_decl (const_tree decl)
26512 if (decl == NULL_TREE
26513 || TREE_CODE (decl) != TYPE_DECL
26514 || DECL_NAMELESS (decl)
26515 || !is_tagged_type (TREE_TYPE (decl))
26516 || DECL_IS_UNDECLARED_BUILTIN (decl)
26517 || is_redundant_typedef (decl)
26518 /* It looks like Ada produces TYPE_DECLs that are very similar
26519 to C++ naming typedefs but that have different
26520 semantics. Let's be specific to c++ for now. */
26521 || !is_cxx (decl))
26522 return FALSE;
26524 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26525 && TYPE_NAME (TREE_TYPE (decl)) == decl
26526 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26527 != TYPE_NAME (TREE_TYPE (decl))));
26530 /* Looks up the DIE for a context. */
26532 static inline dw_die_ref
26533 lookup_context_die (tree context)
26535 if (context)
26537 /* Find die that represents this context. */
26538 if (TYPE_P (context))
26540 context = TYPE_MAIN_VARIANT (context);
26541 dw_die_ref ctx = lookup_type_die (context);
26542 if (!ctx)
26543 return NULL;
26544 return strip_naming_typedef (context, ctx);
26546 else
26547 return lookup_decl_die (context);
26549 return comp_unit_die ();
26552 /* Returns the DIE for a context. */
26554 static inline dw_die_ref
26555 get_context_die (tree context)
26557 if (context)
26559 /* Find die that represents this context. */
26560 if (TYPE_P (context))
26562 context = TYPE_MAIN_VARIANT (context);
26563 return strip_naming_typedef (context, force_type_die (context));
26565 else
26566 return force_decl_die (context);
26568 return comp_unit_die ();
26571 /* Returns the DIE for decl. A DIE will always be returned. */
26573 static dw_die_ref
26574 force_decl_die (tree decl)
26576 dw_die_ref decl_die;
26577 unsigned saved_external_flag;
26578 tree save_fn = NULL_TREE;
26579 decl_die = lookup_decl_die (decl);
26580 if (!decl_die)
26582 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26584 decl_die = lookup_decl_die (decl);
26585 if (decl_die)
26586 return decl_die;
26588 switch (TREE_CODE (decl))
26590 case FUNCTION_DECL:
26591 /* Clear current_function_decl, so that gen_subprogram_die thinks
26592 that this is a declaration. At this point, we just want to force
26593 declaration die. */
26594 save_fn = current_function_decl;
26595 current_function_decl = NULL_TREE;
26596 gen_subprogram_die (decl, context_die);
26597 current_function_decl = save_fn;
26598 break;
26600 case VAR_DECL:
26601 /* Set external flag to force declaration die. Restore it after
26602 gen_decl_die() call. */
26603 saved_external_flag = DECL_EXTERNAL (decl);
26604 DECL_EXTERNAL (decl) = 1;
26605 gen_decl_die (decl, NULL, NULL, context_die);
26606 DECL_EXTERNAL (decl) = saved_external_flag;
26607 break;
26609 case NAMESPACE_DECL:
26610 if (dwarf_version >= 3 || !dwarf_strict)
26611 dwarf2out_decl (decl);
26612 else
26613 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26614 decl_die = comp_unit_die ();
26615 break;
26617 case CONST_DECL:
26618 /* Enumerators shouldn't need force_decl_die. */
26619 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26620 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26621 gen_decl_die (decl, NULL, NULL, context_die);
26622 break;
26624 case TRANSLATION_UNIT_DECL:
26625 decl_die = comp_unit_die ();
26626 break;
26628 default:
26629 gcc_unreachable ();
26632 /* We should be able to find the DIE now. */
26633 if (!decl_die)
26634 decl_die = lookup_decl_die (decl);
26635 gcc_assert (decl_die);
26638 return decl_die;
26641 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26642 always returned. */
26644 static dw_die_ref
26645 force_type_die (tree type)
26647 dw_die_ref type_die;
26649 type_die = lookup_type_die (type);
26650 if (!type_die)
26652 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26654 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26655 false, context_die);
26656 gcc_assert (type_die);
26658 return type_die;
26661 /* Force out any required namespaces to be able to output DECL,
26662 and return the new context_die for it, if it's changed. */
26664 static dw_die_ref
26665 setup_namespace_context (tree thing, dw_die_ref context_die)
26667 tree context = (DECL_P (thing)
26668 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26669 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26670 /* Force out the namespace. */
26671 context_die = force_decl_die (context);
26673 return context_die;
26676 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26677 type) within its namespace, if appropriate.
26679 For compatibility with older debuggers, namespace DIEs only contain
26680 declarations; all definitions are emitted at CU scope, with
26681 DW_AT_specification pointing to the declaration (like with class
26682 members). */
26684 static dw_die_ref
26685 declare_in_namespace (tree thing, dw_die_ref context_die)
26687 dw_die_ref ns_context;
26689 if (debug_info_level <= DINFO_LEVEL_TERSE)
26690 return context_die;
26692 /* External declarations in the local scope only need to be emitted
26693 once, not once in the namespace and once in the scope.
26695 This avoids declaring the `extern' below in the
26696 namespace DIE as well as in the innermost scope:
26698 namespace S
26700 int i=5;
26701 int foo()
26703 int i=8;
26704 extern int i;
26705 return i;
26709 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26710 return context_die;
26712 /* If this decl is from an inlined function, then don't try to emit it in its
26713 namespace, as we will get confused. It would have already been emitted
26714 when the abstract instance of the inline function was emitted anyways. */
26715 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26716 return context_die;
26718 ns_context = setup_namespace_context (thing, context_die);
26720 if (ns_context != context_die)
26722 if (is_fortran () || is_dlang ())
26723 return ns_context;
26724 if (DECL_P (thing))
26725 gen_decl_die (thing, NULL, NULL, ns_context);
26726 else
26727 gen_type_die (thing, ns_context);
26729 return context_die;
26732 /* Generate a DIE for a namespace or namespace alias. */
26734 static void
26735 gen_namespace_die (tree decl, dw_die_ref context_die)
26737 dw_die_ref namespace_die;
26739 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26740 they are an alias of. */
26741 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26743 /* Output a real namespace or module. */
26744 context_die = setup_namespace_context (decl, comp_unit_die ());
26745 namespace_die = new_die (is_fortran () || is_dlang ()
26746 ? DW_TAG_module : DW_TAG_namespace,
26747 context_die, decl);
26748 /* For Fortran modules defined in different CU don't add src coords. */
26749 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26751 const char *name = dwarf2_name (decl, 0);
26752 if (name)
26753 add_name_attribute (namespace_die, name);
26755 else
26756 add_name_and_src_coords_attributes (namespace_die, decl);
26757 if (DECL_EXTERNAL (decl))
26758 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26759 equate_decl_number_to_die (decl, namespace_die);
26761 else
26763 /* Output a namespace alias. */
26765 /* Force out the namespace we are an alias of, if necessary. */
26766 dw_die_ref origin_die
26767 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26769 if (DECL_FILE_SCOPE_P (decl)
26770 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26771 context_die = setup_namespace_context (decl, comp_unit_die ());
26772 /* Now create the namespace alias DIE. */
26773 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26774 add_name_and_src_coords_attributes (namespace_die, decl);
26775 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26776 equate_decl_number_to_die (decl, namespace_die);
26778 if ((dwarf_version >= 5 || !dwarf_strict)
26779 && lang_hooks.decls.decl_dwarf_attribute (decl,
26780 DW_AT_export_symbols) == 1)
26781 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26783 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26784 if (want_pubnames ())
26785 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26788 /* Generate Dwarf debug information for a decl described by DECL.
26789 The return value is currently only meaningful for PARM_DECLs,
26790 for all other decls it returns NULL.
26792 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26793 It can be NULL otherwise. */
26795 static dw_die_ref
26796 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26797 dw_die_ref context_die)
26799 tree decl_or_origin = decl ? decl : origin;
26800 tree class_origin = NULL, ultimate_origin;
26802 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26803 return NULL;
26805 switch (TREE_CODE (decl_or_origin))
26807 case ERROR_MARK:
26808 break;
26810 case CONST_DECL:
26811 if (!is_fortran () && !is_ada () && !is_dlang ())
26813 /* The individual enumerators of an enum type get output when we output
26814 the Dwarf representation of the relevant enum type itself. */
26815 break;
26818 /* Emit its type. */
26819 gen_type_die (TREE_TYPE (decl), context_die);
26821 /* And its containing namespace. */
26822 context_die = declare_in_namespace (decl, context_die);
26824 gen_const_die (decl, context_die);
26825 break;
26827 case FUNCTION_DECL:
26828 #if 0
26829 /* FIXME */
26830 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26831 on local redeclarations of global functions. That seems broken. */
26832 if (current_function_decl != decl)
26833 /* This is only a declaration. */;
26834 #endif
26836 /* We should have abstract copies already and should not generate
26837 stray type DIEs in late LTO dumping. */
26838 if (! early_dwarf)
26841 /* If we're emitting a clone, emit info for the abstract instance. */
26842 else if (origin || DECL_ORIGIN (decl) != decl)
26843 dwarf2out_abstract_function (origin
26844 ? DECL_ORIGIN (origin)
26845 : DECL_ABSTRACT_ORIGIN (decl));
26847 /* If we're emitting a possibly inlined function emit it as
26848 abstract instance. */
26849 else if (cgraph_function_possibly_inlined_p (decl)
26850 && ! DECL_ABSTRACT_P (decl)
26851 && ! class_or_namespace_scope_p (context_die)
26852 /* dwarf2out_abstract_function won't emit a die if this is just
26853 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26854 that case, because that works only if we have a die. */
26855 && DECL_INITIAL (decl) != NULL_TREE)
26856 dwarf2out_abstract_function (decl);
26858 /* Otherwise we're emitting the primary DIE for this decl. */
26859 else if (debug_info_level > DINFO_LEVEL_TERSE)
26861 /* Before we describe the FUNCTION_DECL itself, make sure that we
26862 have its containing type. */
26863 if (!origin)
26864 origin = decl_class_context (decl);
26865 if (origin != NULL_TREE)
26866 gen_type_die (origin, context_die);
26868 /* And its return type. */
26869 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26871 /* And its virtual context. */
26872 if (DECL_VINDEX (decl) != NULL_TREE)
26873 gen_type_die (DECL_CONTEXT (decl), context_die);
26875 /* Make sure we have a member DIE for decl. */
26876 if (origin != NULL_TREE)
26877 gen_type_die_for_member (origin, decl, context_die);
26879 /* And its containing namespace. */
26880 context_die = declare_in_namespace (decl, context_die);
26883 /* Now output a DIE to represent the function itself. */
26884 if (decl)
26885 gen_subprogram_die (decl, context_die);
26886 break;
26888 case TYPE_DECL:
26889 /* If we are in terse mode, don't generate any DIEs to represent any
26890 actual typedefs. */
26891 if (debug_info_level <= DINFO_LEVEL_TERSE)
26892 break;
26894 /* In the special case of a TYPE_DECL node representing the declaration
26895 of some type tag, if the given TYPE_DECL is marked as having been
26896 instantiated from some other (original) TYPE_DECL node (e.g. one which
26897 was generated within the original definition of an inline function) we
26898 used to generate a special (abbreviated) DW_TAG_structure_type,
26899 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26900 should be actually referencing those DIEs, as variable DIEs with that
26901 type would be emitted already in the abstract origin, so it was always
26902 removed during unused type prunning. Don't add anything in this
26903 case. */
26904 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26905 break;
26907 if (is_redundant_typedef (decl))
26908 gen_type_die (TREE_TYPE (decl), context_die);
26909 else
26910 /* Output a DIE to represent the typedef itself. */
26911 gen_typedef_die (decl, context_die);
26912 break;
26914 case LABEL_DECL:
26915 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26916 gen_label_die (decl, context_die);
26917 break;
26919 case VAR_DECL:
26920 case RESULT_DECL:
26921 /* If we are in terse mode, don't generate any DIEs to represent any
26922 variable declarations or definitions unless it is external. */
26923 if (debug_info_level < DINFO_LEVEL_TERSE
26924 || (debug_info_level == DINFO_LEVEL_TERSE
26925 && !TREE_PUBLIC (decl_or_origin)))
26926 break;
26928 if (debug_info_level > DINFO_LEVEL_TERSE)
26930 /* Avoid generating stray type DIEs during late dwarf dumping.
26931 All types have been dumped early. */
26932 if (early_dwarf
26933 /* ??? But in LTRANS we cannot annotate early created variably
26934 modified type DIEs without copying them and adjusting all
26935 references to them. Dump them again as happens for inlining
26936 which copies both the decl and the types. */
26937 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26938 in VLA bound information for example. */
26939 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26940 current_function_decl)))
26942 /* Output any DIEs that are needed to specify the type of this data
26943 object. */
26944 if (decl_by_reference_p (decl_or_origin))
26945 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26946 else
26947 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26950 if (early_dwarf)
26952 /* And its containing type. */
26953 class_origin = decl_class_context (decl_or_origin);
26954 if (class_origin != NULL_TREE)
26955 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26957 /* And its containing namespace. */
26958 context_die = declare_in_namespace (decl_or_origin, context_die);
26962 /* Now output the DIE to represent the data object itself. This gets
26963 complicated because of the possibility that the VAR_DECL really
26964 represents an inlined instance of a formal parameter for an inline
26965 function. */
26966 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26967 if (ultimate_origin != NULL_TREE
26968 && TREE_CODE (ultimate_origin) == PARM_DECL)
26969 gen_formal_parameter_die (decl, origin,
26970 true /* Emit name attribute. */,
26971 context_die);
26972 else
26973 gen_variable_die (decl, origin, context_die);
26974 break;
26976 case FIELD_DECL:
26977 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26978 /* Ignore the nameless fields that are used to skip bits but handle C++
26979 anonymous unions and structs. */
26980 if (DECL_NAME (decl) != NULL_TREE
26981 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26982 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26984 gen_type_die (member_declared_type (decl), context_die);
26985 gen_field_die (decl, ctx, context_die);
26987 break;
26989 case PARM_DECL:
26990 /* Avoid generating stray type DIEs during late dwarf dumping.
26991 All types have been dumped early. */
26992 if (early_dwarf
26993 /* ??? But in LTRANS we cannot annotate early created variably
26994 modified type DIEs without copying them and adjusting all
26995 references to them. Dump them again as happens for inlining
26996 which copies both the decl and the types. */
26997 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26998 in VLA bound information for example. */
26999 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27000 current_function_decl)))
27002 if (DECL_BY_REFERENCE (decl_or_origin))
27003 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27004 else
27005 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27007 return gen_formal_parameter_die (decl, origin,
27008 true /* Emit name attribute. */,
27009 context_die);
27011 case NAMESPACE_DECL:
27012 if (dwarf_version >= 3 || !dwarf_strict)
27013 gen_namespace_die (decl, context_die);
27014 break;
27016 case IMPORTED_DECL:
27017 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27018 DECL_CONTEXT (decl), context_die);
27019 break;
27021 case NAMELIST_DECL:
27022 gen_namelist_decl (DECL_NAME (decl), context_die,
27023 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27024 break;
27026 default:
27027 /* Probably some frontend-internal decl. Assume we don't care. */
27028 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27029 break;
27032 return NULL;
27035 /* Output initial debug information for global DECL. Called at the
27036 end of the parsing process.
27038 This is the initial debug generation process. As such, the DIEs
27039 generated may be incomplete. A later debug generation pass
27040 (dwarf2out_late_global_decl) will augment the information generated
27041 in this pass (e.g., with complete location info). */
27043 static void
27044 dwarf2out_early_global_decl (tree decl)
27046 set_early_dwarf s;
27048 /* gen_decl_die() will set DECL_ABSTRACT because
27049 cgraph_function_possibly_inlined_p() returns true. This is in
27050 turn will cause DW_AT_inline attributes to be set.
27052 This happens because at early dwarf generation, there is no
27053 cgraph information, causing cgraph_function_possibly_inlined_p()
27054 to return true. Trick cgraph_function_possibly_inlined_p()
27055 while we generate dwarf early. */
27056 bool save = symtab->global_info_ready;
27057 symtab->global_info_ready = true;
27059 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27060 other DECLs and they can point to template types or other things
27061 that dwarf2out can't handle when done via dwarf2out_decl. */
27062 if (TREE_CODE (decl) != TYPE_DECL
27063 && TREE_CODE (decl) != PARM_DECL)
27065 if (TREE_CODE (decl) == FUNCTION_DECL)
27067 tree save_fndecl = current_function_decl;
27069 /* For nested functions, make sure we have DIEs for the parents first
27070 so that all nested DIEs are generated at the proper scope in the
27071 first shot. */
27072 tree context = decl_function_context (decl);
27073 if (context != NULL)
27075 dw_die_ref context_die = lookup_decl_die (context);
27076 current_function_decl = context;
27078 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27079 enough so that it lands in its own context. This avoids type
27080 pruning issues later on. */
27081 if (context_die == NULL || is_declaration_die (context_die))
27082 dwarf2out_early_global_decl (context);
27085 /* Emit an abstract origin of a function first. This happens
27086 with C++ constructor clones for example and makes
27087 dwarf2out_abstract_function happy which requires the early
27088 DIE of the abstract instance to be present. */
27089 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27090 dw_die_ref origin_die;
27091 if (origin != NULL
27092 /* Do not emit the DIE multiple times but make sure to
27093 process it fully here in case we just saw a declaration. */
27094 && ((origin_die = lookup_decl_die (origin)) == NULL
27095 || is_declaration_die (origin_die)))
27097 current_function_decl = origin;
27098 dwarf2out_decl (origin);
27101 /* Emit the DIE for decl but avoid doing that multiple times. */
27102 dw_die_ref old_die;
27103 if ((old_die = lookup_decl_die (decl)) == NULL
27104 || is_declaration_die (old_die))
27106 current_function_decl = decl;
27107 dwarf2out_decl (decl);
27110 current_function_decl = save_fndecl;
27112 else
27113 dwarf2out_decl (decl);
27115 symtab->global_info_ready = save;
27118 /* Return whether EXPR is an expression with the following pattern:
27119 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27121 static bool
27122 is_trivial_indirect_ref (tree expr)
27124 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27125 return false;
27127 tree nop = TREE_OPERAND (expr, 0);
27128 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27129 return false;
27131 tree int_cst = TREE_OPERAND (nop, 0);
27132 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27135 /* Output debug information for global decl DECL. Called from
27136 toplev.c after compilation proper has finished. */
27138 static void
27139 dwarf2out_late_global_decl (tree decl)
27141 /* Fill-in any location information we were unable to determine
27142 on the first pass. */
27143 if (VAR_P (decl))
27145 dw_die_ref die = lookup_decl_die (decl);
27147 /* We may have to generate full debug late for LTO in case debug
27148 was not enabled at compile-time or the target doesn't support
27149 the LTO early debug scheme. */
27150 if (! die && in_lto_p)
27151 dwarf2out_decl (decl);
27152 else if (die)
27154 /* We get called via the symtab code invoking late_global_decl
27155 for symbols that are optimized out.
27157 Do not add locations for those, except if they have a
27158 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27159 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27160 INDIRECT_REF expression, as this could generate relocations to
27161 text symbols in LTO object files, which is invalid. */
27162 varpool_node *node = varpool_node::get (decl);
27163 if ((! node || ! node->definition)
27164 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27165 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27166 tree_add_const_value_attribute_for_decl (die, decl);
27167 else
27168 add_location_or_const_value_attribute (die, decl, false);
27173 /* Output debug information for type decl DECL. Called from toplev.c
27174 and from language front ends (to record built-in types). */
27175 static void
27176 dwarf2out_type_decl (tree decl, int local)
27178 if (!local)
27180 set_early_dwarf s;
27181 dwarf2out_decl (decl);
27185 /* Output debug information for imported module or decl DECL.
27186 NAME is non-NULL name in the lexical block if the decl has been renamed.
27187 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27188 that DECL belongs to.
27189 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27190 static void
27191 dwarf2out_imported_module_or_decl_1 (tree decl,
27192 tree name,
27193 tree lexical_block,
27194 dw_die_ref lexical_block_die)
27196 expanded_location xloc;
27197 dw_die_ref imported_die = NULL;
27198 dw_die_ref at_import_die;
27200 if (TREE_CODE (decl) == IMPORTED_DECL)
27202 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27203 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27204 gcc_assert (decl);
27206 else
27207 xloc = expand_location (input_location);
27209 if (TREE_CODE (decl) == TYPE_DECL)
27211 at_import_die = force_type_die (TREE_TYPE (decl));
27212 /* For namespace N { typedef void T; } using N::T; base_type_die
27213 returns NULL, but DW_TAG_imported_declaration requires
27214 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27215 if (!at_import_die)
27217 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27218 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27219 at_import_die = lookup_type_die (TREE_TYPE (decl));
27220 gcc_assert (at_import_die);
27223 else
27225 at_import_die = lookup_decl_die (decl);
27226 if (!at_import_die)
27228 /* If we're trying to avoid duplicate debug info, we may not have
27229 emitted the member decl for this field. Emit it now. */
27230 if (TREE_CODE (decl) == FIELD_DECL)
27232 tree type = DECL_CONTEXT (decl);
27234 if (TYPE_CONTEXT (type)
27235 && TYPE_P (TYPE_CONTEXT (type))
27236 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27237 DINFO_USAGE_DIR_USE))
27238 return;
27239 gen_type_die_for_member (type, decl,
27240 get_context_die (TYPE_CONTEXT (type)));
27242 if (TREE_CODE (decl) == CONST_DECL)
27244 /* Individual enumerators of an enum type do not get output here
27245 (see gen_decl_die), so we cannot call force_decl_die. */
27246 if (!is_fortran () && !is_ada () && !is_dlang ())
27247 return;
27249 if (TREE_CODE (decl) == NAMELIST_DECL)
27250 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27251 get_context_die (DECL_CONTEXT (decl)),
27252 NULL_TREE);
27253 else
27254 at_import_die = force_decl_die (decl);
27258 if (TREE_CODE (decl) == NAMESPACE_DECL)
27260 if (dwarf_version >= 3 || !dwarf_strict)
27261 imported_die = new_die (DW_TAG_imported_module,
27262 lexical_block_die,
27263 lexical_block);
27264 else
27265 return;
27267 else
27268 imported_die = new_die (DW_TAG_imported_declaration,
27269 lexical_block_die,
27270 lexical_block);
27272 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27273 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27274 if (debug_column_info && xloc.column)
27275 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27276 if (name)
27277 add_AT_string (imported_die, DW_AT_name,
27278 IDENTIFIER_POINTER (name));
27279 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27282 /* Output debug information for imported module or decl DECL.
27283 NAME is non-NULL name in context if the decl has been renamed.
27284 CHILD is true if decl is one of the renamed decls as part of
27285 importing whole module.
27286 IMPLICIT is set if this hook is called for an implicit import
27287 such as inline namespace. */
27289 static void
27290 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27291 bool child, bool implicit)
27293 /* dw_die_ref at_import_die; */
27294 dw_die_ref scope_die;
27296 if (debug_info_level <= DINFO_LEVEL_TERSE)
27297 return;
27299 gcc_assert (decl);
27301 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27302 should be enough, for DWARF4 and older even if we emit as extension
27303 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27304 for the benefit of consumers unaware of DW_AT_export_symbols. */
27305 if (implicit
27306 && dwarf_version >= 5
27307 && lang_hooks.decls.decl_dwarf_attribute (decl,
27308 DW_AT_export_symbols) == 1)
27309 return;
27311 set_early_dwarf s;
27313 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27314 We need decl DIE for reference and scope die. First, get DIE for the decl
27315 itself. */
27317 /* Get the scope die for decl context. Use comp_unit_die for global module
27318 or decl. If die is not found for non globals, force new die. */
27319 if (context
27320 && TYPE_P (context)
27321 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27322 return;
27324 scope_die = get_context_die (context);
27326 if (child)
27328 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27329 there is nothing we can do, here. */
27330 if (dwarf_version < 3 && dwarf_strict)
27331 return;
27333 gcc_assert (scope_die->die_child);
27334 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27335 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27336 scope_die = scope_die->die_child;
27339 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27340 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27343 /* Output debug information for namelists. */
27345 static dw_die_ref
27346 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27348 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27349 tree value;
27350 unsigned i;
27352 if (debug_info_level <= DINFO_LEVEL_TERSE)
27353 return NULL;
27355 gcc_assert (scope_die != NULL);
27356 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27357 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27359 /* If there are no item_decls, we have a nondefining namelist, e.g.
27360 with USE association; hence, set DW_AT_declaration. */
27361 if (item_decls == NULL_TREE)
27363 add_AT_flag (nml_die, DW_AT_declaration, 1);
27364 return nml_die;
27367 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27369 nml_item_ref_die = lookup_decl_die (value);
27370 if (!nml_item_ref_die)
27371 nml_item_ref_die = force_decl_die (value);
27373 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27374 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27376 return nml_die;
27380 /* Write the debugging output for DECL and return the DIE. */
27382 static void
27383 dwarf2out_decl (tree decl)
27385 dw_die_ref context_die = comp_unit_die ();
27387 switch (TREE_CODE (decl))
27389 case ERROR_MARK:
27390 return;
27392 case FUNCTION_DECL:
27393 /* If we're a nested function, initially use a parent of NULL; if we're
27394 a plain function, this will be fixed up in decls_for_scope. If
27395 we're a method, it will be ignored, since we already have a DIE.
27396 Avoid doing this late though since clones of class methods may
27397 otherwise end up in limbo and create type DIEs late. */
27398 if (early_dwarf
27399 && decl_function_context (decl)
27400 /* But if we're in terse mode, we don't care about scope. */
27401 && debug_info_level > DINFO_LEVEL_TERSE)
27402 context_die = NULL;
27403 break;
27405 case VAR_DECL:
27406 /* For local statics lookup proper context die. */
27407 if (local_function_static (decl))
27408 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27410 /* If we are in terse mode, don't generate any DIEs to represent any
27411 variable declarations or definitions unless it is external. */
27412 if (debug_info_level < DINFO_LEVEL_TERSE
27413 || (debug_info_level == DINFO_LEVEL_TERSE
27414 && !TREE_PUBLIC (decl)))
27415 return;
27416 break;
27418 case CONST_DECL:
27419 if (debug_info_level <= DINFO_LEVEL_TERSE)
27420 return;
27421 if (!is_fortran () && !is_ada () && !is_dlang ())
27422 return;
27423 if (TREE_STATIC (decl) && decl_function_context (decl))
27424 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27425 break;
27427 case NAMESPACE_DECL:
27428 case IMPORTED_DECL:
27429 if (debug_info_level <= DINFO_LEVEL_TERSE)
27430 return;
27431 if (lookup_decl_die (decl) != NULL)
27432 return;
27433 break;
27435 case TYPE_DECL:
27436 /* Don't emit stubs for types unless they are needed by other DIEs. */
27437 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27438 return;
27440 /* Don't bother trying to generate any DIEs to represent any of the
27441 normal built-in types for the language we are compiling. */
27442 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27443 return;
27445 /* If we are in terse mode, don't generate any DIEs for types. */
27446 if (debug_info_level <= DINFO_LEVEL_TERSE)
27447 return;
27449 /* If we're a function-scope tag, initially use a parent of NULL;
27450 this will be fixed up in decls_for_scope. */
27451 if (decl_function_context (decl))
27452 context_die = NULL;
27454 break;
27456 case NAMELIST_DECL:
27457 break;
27459 default:
27460 return;
27463 gen_decl_die (decl, NULL, NULL, context_die);
27465 if (flag_checking)
27467 dw_die_ref die = lookup_decl_die (decl);
27468 if (die)
27469 check_die (die);
27473 /* Write the debugging output for DECL. */
27475 static void
27476 dwarf2out_function_decl (tree decl)
27478 dwarf2out_decl (decl);
27479 call_arg_locations = NULL;
27480 call_arg_loc_last = NULL;
27481 call_site_count = -1;
27482 tail_call_site_count = -1;
27483 decl_loc_table->empty ();
27484 cached_dw_loc_list_table->empty ();
27487 /* Output a marker (i.e. a label) for the beginning of the generated code for
27488 a lexical block. */
27490 static void
27491 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27492 unsigned int blocknum)
27494 switch_to_section (current_function_section ());
27495 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27498 /* Output a marker (i.e. a label) for the end of the generated code for a
27499 lexical block. */
27501 static void
27502 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27504 switch_to_section (current_function_section ());
27505 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27508 /* Returns nonzero if it is appropriate not to emit any debugging
27509 information for BLOCK, because it doesn't contain any instructions.
27511 Don't allow this for blocks with nested functions or local classes
27512 as we would end up with orphans, and in the presence of scheduling
27513 we may end up calling them anyway. */
27515 static bool
27516 dwarf2out_ignore_block (const_tree block)
27518 tree decl;
27519 unsigned int i;
27521 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27522 if (TREE_CODE (decl) == FUNCTION_DECL
27523 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27524 return 0;
27525 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27527 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27528 if (TREE_CODE (decl) == FUNCTION_DECL
27529 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27530 return 0;
27533 return 1;
27536 /* Hash table routines for file_hash. */
27538 bool
27539 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27541 return filename_cmp (p1->key, p2) == 0;
27544 hashval_t
27545 dwarf_file_hasher::hash (dwarf_file_data *p)
27547 return htab_hash_string (p->key);
27550 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27551 dwarf2out.c) and return its "index". The index of each (known) filename is
27552 just a unique number which is associated with only that one filename. We
27553 need such numbers for the sake of generating labels (in the .debug_sfnames
27554 section) and references to those files numbers (in the .debug_srcinfo
27555 and .debug_macinfo sections). If the filename given as an argument is not
27556 found in our current list, add it to the list and assign it the next
27557 available unique index number. */
27559 static struct dwarf_file_data *
27560 lookup_filename (const char *file_name)
27562 struct dwarf_file_data * created;
27564 if (!file_name)
27565 return NULL;
27567 if (!file_name[0])
27568 file_name = "<stdin>";
27570 dwarf_file_data **slot
27571 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27572 INSERT);
27573 if (*slot)
27574 return *slot;
27576 created = ggc_alloc<dwarf_file_data> ();
27577 created->key = file_name;
27578 created->filename = remap_debug_filename (file_name);
27579 created->emitted_number = 0;
27580 *slot = created;
27581 return created;
27584 /* If the assembler will construct the file table, then translate the compiler
27585 internal file table number into the assembler file table number, and emit
27586 a .file directive if we haven't already emitted one yet. The file table
27587 numbers are different because we prune debug info for unused variables and
27588 types, which may include filenames. */
27590 static int
27591 maybe_emit_file (struct dwarf_file_data * fd)
27593 if (! fd->emitted_number)
27595 if (last_emitted_file)
27596 fd->emitted_number = last_emitted_file->emitted_number + 1;
27597 else
27598 fd->emitted_number = 1;
27599 last_emitted_file = fd;
27601 if (output_asm_line_debug_info ())
27603 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27604 output_quoted_string (asm_out_file, fd->filename);
27605 fputc ('\n', asm_out_file);
27609 return fd->emitted_number;
27612 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27613 That generation should happen after function debug info has been
27614 generated. The value of the attribute is the constant value of ARG. */
27616 static void
27617 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27619 die_arg_entry entry;
27621 if (!die || !arg)
27622 return;
27624 gcc_assert (early_dwarf);
27626 if (!tmpl_value_parm_die_table)
27627 vec_alloc (tmpl_value_parm_die_table, 32);
27629 entry.die = die;
27630 entry.arg = arg;
27631 vec_safe_push (tmpl_value_parm_die_table, entry);
27634 /* Return TRUE if T is an instance of generic type, FALSE
27635 otherwise. */
27637 static bool
27638 generic_type_p (tree t)
27640 if (t == NULL_TREE || !TYPE_P (t))
27641 return false;
27642 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27645 /* Schedule the generation of the generic parameter dies for the
27646 instance of generic type T. The proper generation itself is later
27647 done by gen_scheduled_generic_parms_dies. */
27649 static void
27650 schedule_generic_params_dies_gen (tree t)
27652 if (!generic_type_p (t))
27653 return;
27655 gcc_assert (early_dwarf);
27657 if (!generic_type_instances)
27658 vec_alloc (generic_type_instances, 256);
27660 vec_safe_push (generic_type_instances, t);
27663 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27664 by append_entry_to_tmpl_value_parm_die_table. This function must
27665 be called after function DIEs have been generated. */
27667 static void
27668 gen_remaining_tmpl_value_param_die_attribute (void)
27670 if (tmpl_value_parm_die_table)
27672 unsigned i, j;
27673 die_arg_entry *e;
27675 /* We do this in two phases - first get the cases we can
27676 handle during early-finish, preserving those we cannot
27677 (containing symbolic constants where we don't yet know
27678 whether we are going to output the referenced symbols).
27679 For those we try again at late-finish. */
27680 j = 0;
27681 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27683 if (!e->die->removed
27684 && !tree_add_const_value_attribute (e->die, e->arg))
27686 dw_loc_descr_ref loc = NULL;
27687 if (! early_dwarf
27688 && (dwarf_version >= 5 || !dwarf_strict))
27689 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27690 if (loc)
27691 add_AT_loc (e->die, DW_AT_location, loc);
27692 else
27693 (*tmpl_value_parm_die_table)[j++] = *e;
27696 tmpl_value_parm_die_table->truncate (j);
27700 /* Generate generic parameters DIEs for instances of generic types
27701 that have been previously scheduled by
27702 schedule_generic_params_dies_gen. This function must be called
27703 after all the types of the CU have been laid out. */
27705 static void
27706 gen_scheduled_generic_parms_dies (void)
27708 unsigned i;
27709 tree t;
27711 if (!generic_type_instances)
27712 return;
27714 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27715 if (COMPLETE_TYPE_P (t))
27716 gen_generic_params_dies (t);
27718 generic_type_instances = NULL;
27722 /* Replace DW_AT_name for the decl with name. */
27724 static void
27725 dwarf2out_set_name (tree decl, tree name)
27727 dw_die_ref die;
27728 dw_attr_node *attr;
27729 const char *dname;
27731 die = TYPE_SYMTAB_DIE (decl);
27732 if (!die)
27733 return;
27735 dname = dwarf2_name (name, 0);
27736 if (!dname)
27737 return;
27739 attr = get_AT (die, DW_AT_name);
27740 if (attr)
27742 struct indirect_string_node *node;
27744 node = find_AT_string (dname);
27745 /* replace the string. */
27746 attr->dw_attr_val.v.val_str = node;
27749 else
27750 add_name_attribute (die, dname);
27753 /* True if before or during processing of the first function being emitted. */
27754 static bool in_first_function_p = true;
27755 /* True if loc_note during dwarf2out_var_location call might still be
27756 before first real instruction at address equal to .Ltext0. */
27757 static bool maybe_at_text_label_p = true;
27758 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27759 static unsigned int first_loclabel_num_not_at_text_label;
27761 /* Look ahead for a real insn. */
27763 static rtx_insn *
27764 dwarf2out_next_real_insn (rtx_insn *loc_note)
27766 rtx_insn *next_real = NEXT_INSN (loc_note);
27768 while (next_real)
27769 if (INSN_P (next_real))
27770 break;
27771 else
27772 next_real = NEXT_INSN (next_real);
27774 return next_real;
27777 /* Called by the final INSN scan whenever we see a var location. We
27778 use it to drop labels in the right places, and throw the location in
27779 our lookup table. */
27781 static void
27782 dwarf2out_var_location (rtx_insn *loc_note)
27784 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27785 struct var_loc_node *newloc;
27786 rtx_insn *next_real;
27787 rtx_insn *call_insn = NULL;
27788 static const char *last_label;
27789 static const char *last_postcall_label;
27790 static bool last_in_cold_section_p;
27791 static rtx_insn *expected_next_loc_note;
27792 tree decl;
27793 bool var_loc_p;
27794 var_loc_view view = 0;
27796 if (!NOTE_P (loc_note))
27798 if (CALL_P (loc_note))
27800 maybe_reset_location_view (loc_note, cur_line_info_table);
27801 call_site_count++;
27802 if (SIBLING_CALL_P (loc_note))
27803 tail_call_site_count++;
27804 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27806 call_insn = loc_note;
27807 loc_note = NULL;
27808 var_loc_p = false;
27810 next_real = dwarf2out_next_real_insn (call_insn);
27811 cached_next_real_insn = NULL;
27812 goto create_label;
27814 if (optimize == 0 && !flag_var_tracking)
27816 /* When the var-tracking pass is not running, there is no note
27817 for indirect calls whose target is compile-time known. In this
27818 case, process such calls specifically so that we generate call
27819 sites for them anyway. */
27820 rtx x = PATTERN (loc_note);
27821 if (GET_CODE (x) == PARALLEL)
27822 x = XVECEXP (x, 0, 0);
27823 if (GET_CODE (x) == SET)
27824 x = SET_SRC (x);
27825 if (GET_CODE (x) == CALL)
27826 x = XEXP (x, 0);
27827 if (!MEM_P (x)
27828 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27829 || !SYMBOL_REF_DECL (XEXP (x, 0))
27830 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27831 != FUNCTION_DECL))
27833 call_insn = loc_note;
27834 loc_note = NULL;
27835 var_loc_p = false;
27837 next_real = dwarf2out_next_real_insn (call_insn);
27838 cached_next_real_insn = NULL;
27839 goto create_label;
27843 else if (!debug_variable_location_views)
27844 gcc_unreachable ();
27845 else
27846 maybe_reset_location_view (loc_note, cur_line_info_table);
27848 return;
27851 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27852 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27853 return;
27855 /* Optimize processing a large consecutive sequence of location
27856 notes so we don't spend too much time in next_real_insn. If the
27857 next insn is another location note, remember the next_real_insn
27858 calculation for next time. */
27859 next_real = cached_next_real_insn;
27860 if (next_real)
27862 if (expected_next_loc_note != loc_note)
27863 next_real = NULL;
27866 if (! next_real)
27867 next_real = dwarf2out_next_real_insn (loc_note);
27869 if (next_real)
27871 rtx_insn *next_note = NEXT_INSN (loc_note);
27872 while (next_note != next_real)
27874 if (! next_note->deleted ()
27875 && NOTE_P (next_note)
27876 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27877 break;
27878 next_note = NEXT_INSN (next_note);
27881 if (next_note == next_real)
27882 cached_next_real_insn = NULL;
27883 else
27885 expected_next_loc_note = next_note;
27886 cached_next_real_insn = next_real;
27889 else
27890 cached_next_real_insn = NULL;
27892 /* If there are no instructions which would be affected by this note,
27893 don't do anything. */
27894 if (var_loc_p
27895 && next_real == NULL_RTX
27896 && !NOTE_DURING_CALL_P (loc_note))
27897 return;
27899 create_label:
27901 if (next_real == NULL_RTX)
27902 next_real = get_last_insn ();
27904 /* If there were any real insns between note we processed last time
27905 and this note (or if it is the first note), clear
27906 last_{,postcall_}label so that they are not reused this time. */
27907 if (last_var_location_insn == NULL_RTX
27908 || last_var_location_insn != next_real
27909 || last_in_cold_section_p != in_cold_section_p)
27911 last_label = NULL;
27912 last_postcall_label = NULL;
27915 if (var_loc_p)
27917 const char *label
27918 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27919 view = cur_line_info_table->view;
27920 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27921 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27922 if (newloc == NULL)
27923 return;
27925 else
27927 decl = NULL_TREE;
27928 newloc = NULL;
27931 /* If there were no real insns between note we processed last time
27932 and this note, use the label we emitted last time. Otherwise
27933 create a new label and emit it. */
27934 if (last_label == NULL)
27936 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27937 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27938 loclabel_num++;
27939 last_label = ggc_strdup (loclabel);
27940 /* See if loclabel might be equal to .Ltext0. If yes,
27941 bump first_loclabel_num_not_at_text_label. */
27942 if (!have_multiple_function_sections
27943 && in_first_function_p
27944 && maybe_at_text_label_p)
27946 static rtx_insn *last_start;
27947 rtx_insn *insn;
27948 for (insn = loc_note; insn; insn = previous_insn (insn))
27949 if (insn == last_start)
27950 break;
27951 else if (!NONDEBUG_INSN_P (insn))
27952 continue;
27953 else
27955 rtx body = PATTERN (insn);
27956 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27957 continue;
27958 /* Inline asm could occupy zero bytes. */
27959 else if (GET_CODE (body) == ASM_INPUT
27960 || asm_noperands (body) >= 0)
27961 continue;
27962 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27963 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27964 continue;
27965 #endif
27966 else
27968 /* Assume insn has non-zero length. */
27969 maybe_at_text_label_p = false;
27970 break;
27973 if (maybe_at_text_label_p)
27975 last_start = loc_note;
27976 first_loclabel_num_not_at_text_label = loclabel_num;
27981 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27982 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27984 if (!var_loc_p)
27986 struct call_arg_loc_node *ca_loc
27987 = ggc_cleared_alloc<call_arg_loc_node> ();
27988 rtx_insn *prev = call_insn;
27990 ca_loc->call_arg_loc_note
27991 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27992 ca_loc->next = NULL;
27993 ca_loc->label = last_label;
27994 gcc_assert (prev
27995 && (CALL_P (prev)
27996 || (NONJUMP_INSN_P (prev)
27997 && GET_CODE (PATTERN (prev)) == SEQUENCE
27998 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27999 if (!CALL_P (prev))
28000 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28001 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28003 /* Look for a SYMBOL_REF in the "prev" instruction. */
28004 rtx x = get_call_rtx_from (prev);
28005 if (x)
28007 /* Try to get the call symbol, if any. */
28008 if (MEM_P (XEXP (x, 0)))
28009 x = XEXP (x, 0);
28010 /* First, look for a memory access to a symbol_ref. */
28011 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28012 && SYMBOL_REF_DECL (XEXP (x, 0))
28013 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28014 ca_loc->symbol_ref = XEXP (x, 0);
28015 /* Otherwise, look at a compile-time known user-level function
28016 declaration. */
28017 else if (MEM_P (x)
28018 && MEM_EXPR (x)
28019 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28020 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28023 ca_loc->block = insn_scope (prev);
28024 if (call_arg_locations)
28025 call_arg_loc_last->next = ca_loc;
28026 else
28027 call_arg_locations = ca_loc;
28028 call_arg_loc_last = ca_loc;
28030 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28032 newloc->label = last_label;
28033 newloc->view = view;
28035 else
28037 if (!last_postcall_label)
28039 sprintf (loclabel, "%s-1", last_label);
28040 last_postcall_label = ggc_strdup (loclabel);
28042 newloc->label = last_postcall_label;
28043 /* ??? This view is at last_label, not last_label-1, but we
28044 could only assume view at last_label-1 is zero if we could
28045 assume calls always have length greater than one. This is
28046 probably true in general, though there might be a rare
28047 exception to this rule, e.g. if a call insn is optimized out
28048 by target magic. Then, even the -1 in the label will be
28049 wrong, which might invalidate the range. Anyway, using view,
28050 though technically possibly incorrect, will work as far as
28051 ranges go: since L-1 is in the middle of the call insn,
28052 (L-1).0 and (L-1).V shouldn't make any difference, and having
28053 the loclist entry refer to the .loc entry might be useful, so
28054 leave it like this. */
28055 newloc->view = view;
28058 if (var_loc_p && flag_debug_asm)
28060 const char *name, *sep, *patstr;
28061 if (decl && DECL_NAME (decl))
28062 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28063 else
28064 name = "";
28065 if (NOTE_VAR_LOCATION_LOC (loc_note))
28067 sep = " => ";
28068 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28070 else
28072 sep = " ";
28073 patstr = "RESET";
28075 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28076 name, sep, patstr);
28079 last_var_location_insn = next_real;
28080 last_in_cold_section_p = in_cold_section_p;
28083 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28084 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28085 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28086 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28087 BLOCK_FRAGMENT_ORIGIN links. */
28088 static bool
28089 block_within_block_p (tree block, tree outer, bool bothways)
28091 if (block == outer)
28092 return true;
28094 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28095 for (tree context = BLOCK_SUPERCONTEXT (block);
28096 context != outer;
28097 context = BLOCK_SUPERCONTEXT (context))
28098 if (!context || TREE_CODE (context) != BLOCK)
28099 return false;
28101 if (!bothways)
28102 return true;
28104 /* Now check that each block is actually referenced by its
28105 parent. */
28106 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28107 context = BLOCK_SUPERCONTEXT (context))
28109 if (BLOCK_FRAGMENT_ORIGIN (context))
28111 gcc_assert (!BLOCK_SUBBLOCKS (context));
28112 context = BLOCK_FRAGMENT_ORIGIN (context);
28114 for (tree sub = BLOCK_SUBBLOCKS (context);
28115 sub != block;
28116 sub = BLOCK_CHAIN (sub))
28117 if (!sub)
28118 return false;
28119 if (context == outer)
28120 return true;
28121 else
28122 block = context;
28126 /* Called during final while assembling the marker of the entry point
28127 for an inlined function. */
28129 static void
28130 dwarf2out_inline_entry (tree block)
28132 gcc_assert (debug_inline_points);
28134 /* If we can't represent it, don't bother. */
28135 if (!(dwarf_version >= 3 || !dwarf_strict))
28136 return;
28138 gcc_assert (DECL_P (block_ultimate_origin (block)));
28140 /* Sanity check the block tree. This would catch a case in which
28141 BLOCK got removed from the tree reachable from the outermost
28142 lexical block, but got retained in markers. It would still link
28143 back to its parents, but some ancestor would be missing a link
28144 down the path to the sub BLOCK. If the block got removed, its
28145 BLOCK_NUMBER will not be a usable value. */
28146 if (flag_checking)
28147 gcc_assert (block_within_block_p (block,
28148 DECL_INITIAL (current_function_decl),
28149 true));
28151 gcc_assert (inlined_function_outer_scope_p (block));
28152 gcc_assert (!lookup_block_die (block));
28154 if (BLOCK_FRAGMENT_ORIGIN (block))
28155 block = BLOCK_FRAGMENT_ORIGIN (block);
28156 /* Can the entry point ever not be at the beginning of an
28157 unfragmented lexical block? */
28158 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28159 || (cur_line_info_table
28160 && !ZERO_VIEW_P (cur_line_info_table->view))))
28161 return;
28163 if (!inline_entry_data_table)
28164 inline_entry_data_table
28165 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28168 inline_entry_data **iedp
28169 = inline_entry_data_table->find_slot_with_hash (block,
28170 htab_hash_pointer (block),
28171 INSERT);
28172 if (*iedp)
28173 /* ??? Ideally, we'd record all entry points for the same inlined
28174 function (some may have been duplicated by e.g. unrolling), but
28175 we have no way to represent that ATM. */
28176 return;
28178 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28179 ied->block = block;
28180 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28181 ied->label_num = BLOCK_NUMBER (block);
28182 if (cur_line_info_table)
28183 ied->view = cur_line_info_table->view;
28185 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28186 BLOCK_NUMBER (block));
28189 /* Called from finalize_size_functions for size functions so that their body
28190 can be encoded in the debug info to describe the layout of variable-length
28191 structures. */
28193 static void
28194 dwarf2out_size_function (tree decl)
28196 set_early_dwarf s;
28197 function_to_dwarf_procedure (decl);
28200 /* Note in one location list that text section has changed. */
28203 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28205 var_loc_list *list = *slot;
28206 if (list->first)
28207 list->last_before_switch
28208 = list->last->next ? list->last->next : list->last;
28209 return 1;
28212 /* Note in all location lists that text section has changed. */
28214 static void
28215 var_location_switch_text_section (void)
28217 if (decl_loc_table == NULL)
28218 return;
28220 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28223 /* Create a new line number table. */
28225 static dw_line_info_table *
28226 new_line_info_table (void)
28228 dw_line_info_table *table;
28230 table = ggc_cleared_alloc<dw_line_info_table> ();
28231 table->file_num = 1;
28232 table->line_num = 1;
28233 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28234 FORCE_RESET_NEXT_VIEW (table->view);
28235 table->symviews_since_reset = 0;
28237 return table;
28240 /* Lookup the "current" table into which we emit line info, so
28241 that we don't have to do it for every source line. */
28243 static void
28244 set_cur_line_info_table (section *sec)
28246 dw_line_info_table *table;
28248 if (sec == text_section)
28249 table = text_section_line_info;
28250 else if (sec == cold_text_section)
28252 table = cold_text_section_line_info;
28253 if (!table)
28255 cold_text_section_line_info = table = new_line_info_table ();
28256 table->end_label = cold_end_label;
28259 else
28261 const char *end_label;
28263 if (crtl->has_bb_partition)
28265 if (in_cold_section_p)
28266 end_label = crtl->subsections.cold_section_end_label;
28267 else
28268 end_label = crtl->subsections.hot_section_end_label;
28270 else
28272 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28273 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28274 current_function_funcdef_no);
28275 end_label = ggc_strdup (label);
28278 table = new_line_info_table ();
28279 table->end_label = end_label;
28281 vec_safe_push (separate_line_info, table);
28284 if (output_asm_line_debug_info ())
28285 table->is_stmt = (cur_line_info_table
28286 ? cur_line_info_table->is_stmt
28287 : DWARF_LINE_DEFAULT_IS_STMT_START);
28288 cur_line_info_table = table;
28292 /* We need to reset the locations at the beginning of each
28293 function. We can't do this in the end_function hook, because the
28294 declarations that use the locations won't have been output when
28295 that hook is called. Also compute have_multiple_function_sections here. */
28297 static void
28298 dwarf2out_begin_function (tree fun)
28300 section *sec = function_section (fun);
28302 if (sec != text_section)
28303 have_multiple_function_sections = true;
28305 if (crtl->has_bb_partition && !cold_text_section)
28307 gcc_assert (current_function_decl == fun);
28308 cold_text_section = unlikely_text_section ();
28309 switch_to_section (cold_text_section);
28310 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28311 switch_to_section (sec);
28314 call_site_count = 0;
28315 tail_call_site_count = 0;
28317 set_cur_line_info_table (sec);
28318 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28321 /* Helper function of dwarf2out_end_function, called only after emitting
28322 the very first function into assembly. Check if some .debug_loc range
28323 might end with a .LVL* label that could be equal to .Ltext0.
28324 In that case we must force using absolute addresses in .debug_loc ranges,
28325 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28326 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28327 list terminator.
28328 Set have_multiple_function_sections to true in that case and
28329 terminate htab traversal. */
28332 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28334 var_loc_list *entry = *slot;
28335 struct var_loc_node *node;
28337 node = entry->first;
28338 if (node && node->next && node->next->label)
28340 unsigned int i;
28341 const char *label = node->next->label;
28342 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28344 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28346 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28347 if (strcmp (label, loclabel) == 0)
28349 have_multiple_function_sections = true;
28350 return 0;
28354 return 1;
28357 /* Hook called after emitting a function into assembly.
28358 This does something only for the very first function emitted. */
28360 static void
28361 dwarf2out_end_function (unsigned int)
28363 if (in_first_function_p
28364 && !have_multiple_function_sections
28365 && first_loclabel_num_not_at_text_label
28366 && decl_loc_table)
28367 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28368 in_first_function_p = false;
28369 maybe_at_text_label_p = false;
28372 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28373 front-ends register a translation unit even before dwarf2out_init is
28374 called. */
28375 static tree main_translation_unit = NULL_TREE;
28377 /* Hook called by front-ends after they built their main translation unit.
28378 Associate comp_unit_die to UNIT. */
28380 static void
28381 dwarf2out_register_main_translation_unit (tree unit)
28383 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28384 && main_translation_unit == NULL_TREE);
28385 main_translation_unit = unit;
28386 /* If dwarf2out_init has not been called yet, it will perform the association
28387 itself looking at main_translation_unit. */
28388 if (decl_die_table != NULL)
28389 equate_decl_number_to_die (unit, comp_unit_die ());
28392 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28394 static void
28395 push_dw_line_info_entry (dw_line_info_table *table,
28396 enum dw_line_info_opcode opcode, unsigned int val)
28398 dw_line_info_entry e;
28399 e.opcode = opcode;
28400 e.val = val;
28401 vec_safe_push (table->entries, e);
28404 /* Output a label to mark the beginning of a source code line entry
28405 and record information relating to this source line, in
28406 'line_info_table' for later output of the .debug_line section. */
28407 /* ??? The discriminator parameter ought to be unsigned. */
28409 static void
28410 dwarf2out_source_line (unsigned int line, unsigned int column,
28411 const char *filename,
28412 int discriminator, bool is_stmt)
28414 unsigned int file_num;
28415 dw_line_info_table *table;
28416 static var_loc_view lvugid;
28418 /* 'line_info_table' information gathering is not needed when the debug
28419 info level is set to the lowest value. Also, the current DWARF-based
28420 debug formats do not use this info. */
28421 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28422 return;
28424 table = cur_line_info_table;
28426 if (line == 0)
28428 if (debug_variable_location_views
28429 && output_asm_line_debug_info ()
28430 && table && !RESETTING_VIEW_P (table->view))
28432 /* If we're using the assembler to compute view numbers, we
28433 can't issue a .loc directive for line zero, so we can't
28434 get a view number at this point. We might attempt to
28435 compute it from the previous view, or equate it to a
28436 subsequent view (though it might not be there!), but
28437 since we're omitting the line number entry, we might as
28438 well omit the view number as well. That means pretending
28439 it's a view number zero, which might very well turn out
28440 to be correct. ??? Extend the assembler so that the
28441 compiler could emit e.g. ".locview .LVU#", to output a
28442 view without changing line number information. We'd then
28443 have to count it in symviews_since_reset; when it's omitted,
28444 it doesn't count. */
28445 if (!zero_view_p)
28446 zero_view_p = BITMAP_GGC_ALLOC ();
28447 bitmap_set_bit (zero_view_p, table->view);
28448 if (flag_debug_asm)
28450 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28451 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28452 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28453 ASM_COMMENT_START);
28454 assemble_name (asm_out_file, label);
28455 putc ('\n', asm_out_file);
28457 table->view = ++lvugid;
28459 return;
28462 /* The discriminator column was added in dwarf4. Simplify the below
28463 by simply removing it if we're not supposed to output it. */
28464 if (dwarf_version < 4 && dwarf_strict)
28465 discriminator = 0;
28467 if (!debug_column_info)
28468 column = 0;
28470 file_num = maybe_emit_file (lookup_filename (filename));
28472 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28473 the debugger has used the second (possibly duplicate) line number
28474 at the beginning of the function to mark the end of the prologue.
28475 We could eliminate any other duplicates within the function. For
28476 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28477 that second line number entry. */
28478 /* Recall that this end-of-prologue indication is *not* the same thing
28479 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28480 to which the hook corresponds, follows the last insn that was
28481 emitted by gen_prologue. What we need is to precede the first insn
28482 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28483 insn that corresponds to something the user wrote. These may be
28484 very different locations once scheduling is enabled. */
28486 if (0 && file_num == table->file_num
28487 && line == table->line_num
28488 && column == table->column_num
28489 && discriminator == table->discrim_num
28490 && is_stmt == table->is_stmt)
28491 return;
28493 switch_to_section (current_function_section ());
28495 /* If requested, emit something human-readable. */
28496 if (flag_debug_asm)
28498 if (debug_column_info)
28499 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28500 filename, line, column);
28501 else
28502 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28503 filename, line);
28506 if (output_asm_line_debug_info ())
28508 /* Emit the .loc directive understood by GNU as. */
28509 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28510 file_num, line, is_stmt, discriminator */
28511 fputs ("\t.loc ", asm_out_file);
28512 fprint_ul (asm_out_file, file_num);
28513 putc (' ', asm_out_file);
28514 fprint_ul (asm_out_file, line);
28515 putc (' ', asm_out_file);
28516 fprint_ul (asm_out_file, column);
28518 if (is_stmt != table->is_stmt)
28520 #if HAVE_GAS_LOC_STMT
28521 fputs (" is_stmt ", asm_out_file);
28522 putc (is_stmt ? '1' : '0', asm_out_file);
28523 #endif
28525 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28527 gcc_assert (discriminator > 0);
28528 fputs (" discriminator ", asm_out_file);
28529 fprint_ul (asm_out_file, (unsigned long) discriminator);
28531 if (debug_variable_location_views)
28533 if (!RESETTING_VIEW_P (table->view))
28535 table->symviews_since_reset++;
28536 if (table->symviews_since_reset > symview_upper_bound)
28537 symview_upper_bound = table->symviews_since_reset;
28538 /* When we're using the assembler to compute view
28539 numbers, we output symbolic labels after "view" in
28540 .loc directives, and the assembler will set them for
28541 us, so that we can refer to the view numbers in
28542 location lists. The only exceptions are when we know
28543 a view will be zero: "-0" is a forced reset, used
28544 e.g. in the beginning of functions, whereas "0" tells
28545 the assembler to check that there was a PC change
28546 since the previous view, in a way that implicitly
28547 resets the next view. */
28548 fputs (" view ", asm_out_file);
28549 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28550 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28551 assemble_name (asm_out_file, label);
28552 table->view = ++lvugid;
28554 else
28556 table->symviews_since_reset = 0;
28557 if (FORCE_RESETTING_VIEW_P (table->view))
28558 fputs (" view -0", asm_out_file);
28559 else
28560 fputs (" view 0", asm_out_file);
28561 /* Mark the present view as a zero view. Earlier debug
28562 binds may have already added its id to loclists to be
28563 emitted later, so we can't reuse the id for something
28564 else. However, it's good to know whether a view is
28565 known to be zero, because then we may be able to
28566 optimize out locviews that are all zeros, so take
28567 note of it in zero_view_p. */
28568 if (!zero_view_p)
28569 zero_view_p = BITMAP_GGC_ALLOC ();
28570 bitmap_set_bit (zero_view_p, lvugid);
28571 table->view = ++lvugid;
28574 putc ('\n', asm_out_file);
28576 else
28578 unsigned int label_num = ++line_info_label_num;
28580 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28582 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28583 push_dw_line_info_entry (table, LI_adv_address, label_num);
28584 else
28585 push_dw_line_info_entry (table, LI_set_address, label_num);
28586 if (debug_variable_location_views)
28588 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28589 if (resetting)
28590 table->view = 0;
28592 if (flag_debug_asm)
28593 fprintf (asm_out_file, "\t%s view %s%d\n",
28594 ASM_COMMENT_START,
28595 resetting ? "-" : "",
28596 table->view);
28598 table->view++;
28600 if (file_num != table->file_num)
28601 push_dw_line_info_entry (table, LI_set_file, file_num);
28602 if (discriminator != table->discrim_num)
28603 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28604 if (is_stmt != table->is_stmt)
28605 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28606 push_dw_line_info_entry (table, LI_set_line, line);
28607 if (debug_column_info)
28608 push_dw_line_info_entry (table, LI_set_column, column);
28611 table->file_num = file_num;
28612 table->line_num = line;
28613 table->column_num = column;
28614 table->discrim_num = discriminator;
28615 table->is_stmt = is_stmt;
28616 table->in_use = true;
28619 /* Record a source file location for a DECL_IGNORED_P function. */
28621 static void
28622 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28623 const char *filename)
28625 dw_fde_ref fde = cfun->fde;
28627 fde->ignored_debug = false;
28628 set_cur_line_info_table (function_section (fde->decl));
28630 dwarf2out_source_line (line, column, filename, 0, true);
28633 /* Record the beginning of a new source file. */
28635 static void
28636 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28638 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28640 macinfo_entry e;
28641 e.code = DW_MACINFO_start_file;
28642 e.lineno = lineno;
28643 e.info = ggc_strdup (filename);
28644 vec_safe_push (macinfo_table, e);
28648 /* Record the end of a source file. */
28650 static void
28651 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28653 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28655 macinfo_entry e;
28656 e.code = DW_MACINFO_end_file;
28657 e.lineno = lineno;
28658 e.info = NULL;
28659 vec_safe_push (macinfo_table, e);
28663 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28664 the tail part of the directive line, i.e. the part which is past the
28665 initial whitespace, #, whitespace, directive-name, whitespace part. */
28667 static void
28668 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28669 const char *buffer ATTRIBUTE_UNUSED)
28671 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28673 macinfo_entry e;
28674 /* Insert a dummy first entry to be able to optimize the whole
28675 predefined macro block using DW_MACRO_import. */
28676 if (macinfo_table->is_empty () && lineno <= 1)
28678 e.code = 0;
28679 e.lineno = 0;
28680 e.info = NULL;
28681 vec_safe_push (macinfo_table, e);
28683 e.code = DW_MACINFO_define;
28684 e.lineno = lineno;
28685 e.info = ggc_strdup (buffer);
28686 vec_safe_push (macinfo_table, e);
28690 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28691 the tail part of the directive line, i.e. the part which is past the
28692 initial whitespace, #, whitespace, directive-name, whitespace part. */
28694 static void
28695 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28696 const char *buffer ATTRIBUTE_UNUSED)
28698 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28700 macinfo_entry e;
28701 /* Insert a dummy first entry to be able to optimize the whole
28702 predefined macro block using DW_MACRO_import. */
28703 if (macinfo_table->is_empty () && lineno <= 1)
28705 e.code = 0;
28706 e.lineno = 0;
28707 e.info = NULL;
28708 vec_safe_push (macinfo_table, e);
28710 e.code = DW_MACINFO_undef;
28711 e.lineno = lineno;
28712 e.info = ggc_strdup (buffer);
28713 vec_safe_push (macinfo_table, e);
28717 /* Helpers to manipulate hash table of CUs. */
28719 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28721 static inline hashval_t hash (const macinfo_entry *);
28722 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28725 inline hashval_t
28726 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28728 return htab_hash_string (entry->info);
28731 inline bool
28732 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28733 const macinfo_entry *entry2)
28735 return !strcmp (entry1->info, entry2->info);
28738 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28740 /* Output a single .debug_macinfo entry. */
28742 static void
28743 output_macinfo_op (macinfo_entry *ref)
28745 int file_num;
28746 size_t len;
28747 struct indirect_string_node *node;
28748 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28749 struct dwarf_file_data *fd;
28751 switch (ref->code)
28753 case DW_MACINFO_start_file:
28754 fd = lookup_filename (ref->info);
28755 file_num = maybe_emit_file (fd);
28756 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28757 dw2_asm_output_data_uleb128 (ref->lineno,
28758 "Included from line number %lu",
28759 (unsigned long) ref->lineno);
28760 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28761 break;
28762 case DW_MACINFO_end_file:
28763 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28764 break;
28765 case DW_MACINFO_define:
28766 case DW_MACINFO_undef:
28767 len = strlen (ref->info) + 1;
28768 if ((!dwarf_strict || dwarf_version >= 5)
28769 && len > (size_t) dwarf_offset_size
28770 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28771 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28773 if (dwarf_split_debug_info && dwarf_version >= 5)
28774 ref->code = ref->code == DW_MACINFO_define
28775 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28776 else
28777 ref->code = ref->code == DW_MACINFO_define
28778 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28779 output_macinfo_op (ref);
28780 return;
28782 dw2_asm_output_data (1, ref->code,
28783 ref->code == DW_MACINFO_define
28784 ? "Define macro" : "Undefine macro");
28785 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28786 (unsigned long) ref->lineno);
28787 dw2_asm_output_nstring (ref->info, -1, "The macro");
28788 break;
28789 case DW_MACRO_define_strp:
28790 dw2_asm_output_data (1, ref->code, "Define macro strp");
28791 goto do_DW_MACRO_define_strpx;
28792 case DW_MACRO_undef_strp:
28793 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28794 goto do_DW_MACRO_define_strpx;
28795 case DW_MACRO_define_strx:
28796 dw2_asm_output_data (1, ref->code, "Define macro strx");
28797 goto do_DW_MACRO_define_strpx;
28798 case DW_MACRO_undef_strx:
28799 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28800 /* FALLTHRU */
28801 do_DW_MACRO_define_strpx:
28802 /* NB: dwarf2out_finish performs:
28803 1. save_macinfo_strings
28804 2. hash table traverse of index_string
28805 3. output_macinfo -> output_macinfo_op
28806 4. output_indirect_strings
28807 -> hash table traverse of output_index_string
28809 When output_macinfo_op is called, all index strings have been
28810 added to hash table by save_macinfo_strings and we can't pass
28811 INSERT to find_slot_with_hash which may expand hash table, even
28812 if no insertion is needed, and change hash table traverse order
28813 between index_string and output_index_string. */
28814 node = find_AT_string (ref->info, NO_INSERT);
28815 gcc_assert (node
28816 && (node->form == DW_FORM_strp
28817 || node->form == dwarf_FORM (DW_FORM_strx)));
28818 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28819 (unsigned long) ref->lineno);
28820 if (node->form == DW_FORM_strp)
28821 dw2_asm_output_offset (dwarf_offset_size, node->label,
28822 debug_str_section, "The macro: \"%s\"",
28823 ref->info);
28824 else
28825 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28826 ref->info);
28827 break;
28828 case DW_MACRO_import:
28829 dw2_asm_output_data (1, ref->code, "Import");
28830 ASM_GENERATE_INTERNAL_LABEL (label,
28831 DEBUG_MACRO_SECTION_LABEL,
28832 ref->lineno + macinfo_label_base);
28833 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28834 break;
28835 default:
28836 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28837 ASM_COMMENT_START, (unsigned long) ref->code);
28838 break;
28842 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28843 other compilation unit .debug_macinfo sections. IDX is the first
28844 index of a define/undef, return the number of ops that should be
28845 emitted in a comdat .debug_macinfo section and emit
28846 a DW_MACRO_import entry referencing it.
28847 If the define/undef entry should be emitted normally, return 0. */
28849 static unsigned
28850 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28851 macinfo_hash_type **macinfo_htab)
28853 macinfo_entry *first, *second, *cur, *inc;
28854 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28855 unsigned char checksum[16];
28856 struct md5_ctx ctx;
28857 char *grp_name, *tail;
28858 const char *base;
28859 unsigned int i, count, encoded_filename_len, linebuf_len;
28860 macinfo_entry **slot;
28862 first = &(*macinfo_table)[idx];
28863 second = &(*macinfo_table)[idx + 1];
28865 /* Optimize only if there are at least two consecutive define/undef ops,
28866 and either all of them are before first DW_MACINFO_start_file
28867 with lineno {0,1} (i.e. predefined macro block), or all of them are
28868 in some included header file. */
28869 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28870 return 0;
28871 if (vec_safe_is_empty (files))
28873 if (first->lineno > 1 || second->lineno > 1)
28874 return 0;
28876 else if (first->lineno == 0)
28877 return 0;
28879 /* Find the last define/undef entry that can be grouped together
28880 with first and at the same time compute md5 checksum of their
28881 codes, linenumbers and strings. */
28882 md5_init_ctx (&ctx);
28883 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28884 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28885 break;
28886 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28887 break;
28888 else
28890 unsigned char code = cur->code;
28891 md5_process_bytes (&code, 1, &ctx);
28892 checksum_uleb128 (cur->lineno, &ctx);
28893 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28895 md5_finish_ctx (&ctx, checksum);
28896 count = i - idx;
28898 /* From the containing include filename (if any) pick up just
28899 usable characters from its basename. */
28900 if (vec_safe_is_empty (files))
28901 base = "";
28902 else
28903 base = lbasename (files->last ().info);
28904 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28905 if (ISIDNUM (base[i]) || base[i] == '.')
28906 encoded_filename_len++;
28907 /* Count . at the end. */
28908 if (encoded_filename_len)
28909 encoded_filename_len++;
28911 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28912 linebuf_len = strlen (linebuf);
28914 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28915 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28916 + 16 * 2 + 1);
28917 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28918 tail = grp_name + 4;
28919 if (encoded_filename_len)
28921 for (i = 0; base[i]; i++)
28922 if (ISIDNUM (base[i]) || base[i] == '.')
28923 *tail++ = base[i];
28924 *tail++ = '.';
28926 memcpy (tail, linebuf, linebuf_len);
28927 tail += linebuf_len;
28928 *tail++ = '.';
28929 for (i = 0; i < 16; i++)
28930 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28932 /* Construct a macinfo_entry for DW_MACRO_import
28933 in the empty vector entry before the first define/undef. */
28934 inc = &(*macinfo_table)[idx - 1];
28935 inc->code = DW_MACRO_import;
28936 inc->lineno = 0;
28937 inc->info = ggc_strdup (grp_name);
28938 if (!*macinfo_htab)
28939 *macinfo_htab = new macinfo_hash_type (10);
28940 /* Avoid emitting duplicates. */
28941 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28942 if (*slot != NULL)
28944 inc->code = 0;
28945 inc->info = NULL;
28946 /* If such an entry has been used before, just emit
28947 a DW_MACRO_import op. */
28948 inc = *slot;
28949 output_macinfo_op (inc);
28950 /* And clear all macinfo_entry in the range to avoid emitting them
28951 in the second pass. */
28952 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28954 cur->code = 0;
28955 cur->info = NULL;
28958 else
28960 *slot = inc;
28961 inc->lineno = (*macinfo_htab)->elements ();
28962 output_macinfo_op (inc);
28964 return count;
28967 /* Save any strings needed by the macinfo table in the debug str
28968 table. All strings must be collected into the table by the time
28969 index_string is called. */
28971 static void
28972 save_macinfo_strings (void)
28974 unsigned len;
28975 unsigned i;
28976 macinfo_entry *ref;
28978 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28980 switch (ref->code)
28982 /* Match the logic in output_macinfo_op to decide on
28983 indirect strings. */
28984 case DW_MACINFO_define:
28985 case DW_MACINFO_undef:
28986 len = strlen (ref->info) + 1;
28987 if ((!dwarf_strict || dwarf_version >= 5)
28988 && len > (unsigned) dwarf_offset_size
28989 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28990 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28991 set_indirect_string (find_AT_string (ref->info));
28992 break;
28993 case DW_MACINFO_start_file:
28994 /* -gsplit-dwarf -g3 will also output filename as indirect
28995 string. */
28996 if (!dwarf_split_debug_info)
28997 break;
28998 /* Fall through. */
28999 case DW_MACRO_define_strp:
29000 case DW_MACRO_undef_strp:
29001 case DW_MACRO_define_strx:
29002 case DW_MACRO_undef_strx:
29003 set_indirect_string (find_AT_string (ref->info));
29004 break;
29005 default:
29006 break;
29011 /* Output macinfo section(s). */
29013 static void
29014 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29016 unsigned i;
29017 unsigned long length = vec_safe_length (macinfo_table);
29018 macinfo_entry *ref;
29019 vec<macinfo_entry, va_gc> *files = NULL;
29020 macinfo_hash_type *macinfo_htab = NULL;
29021 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29023 if (! length)
29024 return;
29026 /* output_macinfo* uses these interchangeably. */
29027 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29028 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29029 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29030 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29032 /* AIX Assembler inserts the length, so adjust the reference to match the
29033 offset expected by debuggers. */
29034 strcpy (dl_section_ref, debug_line_label);
29035 if (XCOFF_DEBUGGING_INFO)
29036 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29038 /* For .debug_macro emit the section header. */
29039 if (!dwarf_strict || dwarf_version >= 5)
29041 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29042 "DWARF macro version number");
29043 if (dwarf_offset_size == 8)
29044 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29045 else
29046 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29047 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29048 debug_line_section, NULL);
29051 /* In the first loop, it emits the primary .debug_macinfo section
29052 and after each emitted op the macinfo_entry is cleared.
29053 If a longer range of define/undef ops can be optimized using
29054 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29055 the vector before the first define/undef in the range and the
29056 whole range of define/undef ops is not emitted and kept. */
29057 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29059 switch (ref->code)
29061 case DW_MACINFO_start_file:
29062 vec_safe_push (files, *ref);
29063 break;
29064 case DW_MACINFO_end_file:
29065 if (!vec_safe_is_empty (files))
29066 files->pop ();
29067 break;
29068 case DW_MACINFO_define:
29069 case DW_MACINFO_undef:
29070 if ((!dwarf_strict || dwarf_version >= 5)
29071 && HAVE_COMDAT_GROUP
29072 && vec_safe_length (files) != 1
29073 && i > 0
29074 && i + 1 < length
29075 && (*macinfo_table)[i - 1].code == 0)
29077 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29078 if (count)
29080 i += count - 1;
29081 continue;
29084 break;
29085 case 0:
29086 /* A dummy entry may be inserted at the beginning to be able
29087 to optimize the whole block of predefined macros. */
29088 if (i == 0)
29089 continue;
29090 default:
29091 break;
29093 output_macinfo_op (ref);
29094 ref->info = NULL;
29095 ref->code = 0;
29098 if (!macinfo_htab)
29099 return;
29101 /* Save the number of transparent includes so we can adjust the
29102 label number for the fat LTO object DWARF. */
29103 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29105 delete macinfo_htab;
29106 macinfo_htab = NULL;
29108 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29109 terminate the current chain and switch to a new comdat .debug_macinfo
29110 section and emit the define/undef entries within it. */
29111 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29112 switch (ref->code)
29114 case 0:
29115 continue;
29116 case DW_MACRO_import:
29118 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29119 tree comdat_key = get_identifier (ref->info);
29120 /* Terminate the previous .debug_macinfo section. */
29121 dw2_asm_output_data (1, 0, "End compilation unit");
29122 targetm.asm_out.named_section (debug_macinfo_section_name,
29123 SECTION_DEBUG
29124 | SECTION_LINKONCE
29125 | (early_lto_debug
29126 ? SECTION_EXCLUDE : 0),
29127 comdat_key);
29128 ASM_GENERATE_INTERNAL_LABEL (label,
29129 DEBUG_MACRO_SECTION_LABEL,
29130 ref->lineno + macinfo_label_base);
29131 ASM_OUTPUT_LABEL (asm_out_file, label);
29132 ref->code = 0;
29133 ref->info = NULL;
29134 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29135 "DWARF macro version number");
29136 if (dwarf_offset_size == 8)
29137 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29138 else
29139 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29141 break;
29142 case DW_MACINFO_define:
29143 case DW_MACINFO_undef:
29144 output_macinfo_op (ref);
29145 ref->code = 0;
29146 ref->info = NULL;
29147 break;
29148 default:
29149 gcc_unreachable ();
29152 macinfo_label_base += macinfo_label_base_adj;
29155 /* As init_sections_and_labels may get called multiple times, have a
29156 generation count for labels. */
29157 static unsigned init_sections_and_labels_generation;
29159 /* Initialize the various sections and labels for dwarf output and prefix
29160 them with PREFIX if non-NULL. Returns the generation (zero based
29161 number of times function was called). */
29163 static unsigned
29164 init_sections_and_labels (bool early_lto_debug)
29166 if (early_lto_debug)
29168 if (!dwarf_split_debug_info)
29170 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29171 SECTION_DEBUG | SECTION_EXCLUDE,
29172 NULL);
29173 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29174 SECTION_DEBUG | SECTION_EXCLUDE,
29175 NULL);
29176 debug_macinfo_section_name
29177 = ((dwarf_strict && dwarf_version < 5)
29178 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29179 debug_macinfo_section = get_section (debug_macinfo_section_name,
29180 SECTION_DEBUG
29181 | SECTION_EXCLUDE, NULL);
29183 else
29185 /* ??? Which of the following do we need early? */
29186 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29187 SECTION_DEBUG | SECTION_EXCLUDE,
29188 NULL);
29189 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29190 SECTION_DEBUG | SECTION_EXCLUDE,
29191 NULL);
29192 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29193 SECTION_DEBUG
29194 | SECTION_EXCLUDE, NULL);
29195 debug_skeleton_abbrev_section
29196 = get_section (DEBUG_LTO_ABBREV_SECTION,
29197 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29198 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29199 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29200 init_sections_and_labels_generation);
29202 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29203 stay in the main .o, but the skeleton_line goes into the split
29204 off dwo. */
29205 debug_skeleton_line_section
29206 = get_section (DEBUG_LTO_LINE_SECTION,
29207 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29208 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29209 DEBUG_SKELETON_LINE_SECTION_LABEL,
29210 init_sections_and_labels_generation);
29211 debug_str_offsets_section
29212 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29213 SECTION_DEBUG | SECTION_EXCLUDE,
29214 NULL);
29215 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29216 DEBUG_SKELETON_INFO_SECTION_LABEL,
29217 init_sections_and_labels_generation);
29218 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29219 DEBUG_STR_DWO_SECTION_FLAGS,
29220 NULL);
29221 debug_macinfo_section_name
29222 = ((dwarf_strict && dwarf_version < 5)
29223 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29224 debug_macinfo_section = get_section (debug_macinfo_section_name,
29225 SECTION_DEBUG | SECTION_EXCLUDE,
29226 NULL);
29228 /* For macro info and the file table we have to refer to a
29229 debug_line section. */
29230 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29231 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29232 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29233 DEBUG_LINE_SECTION_LABEL,
29234 init_sections_and_labels_generation);
29236 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29237 DEBUG_STR_SECTION_FLAGS
29238 | SECTION_EXCLUDE, NULL);
29239 if (!dwarf_split_debug_info)
29240 debug_line_str_section
29241 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29242 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29244 else
29246 if (!dwarf_split_debug_info)
29248 debug_info_section = get_section (DEBUG_INFO_SECTION,
29249 SECTION_DEBUG, NULL);
29250 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29251 SECTION_DEBUG, NULL);
29252 debug_loc_section = get_section (dwarf_version >= 5
29253 ? DEBUG_LOCLISTS_SECTION
29254 : DEBUG_LOC_SECTION,
29255 SECTION_DEBUG, NULL);
29256 debug_macinfo_section_name
29257 = ((dwarf_strict && dwarf_version < 5)
29258 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29259 debug_macinfo_section = get_section (debug_macinfo_section_name,
29260 SECTION_DEBUG, NULL);
29262 else
29264 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29265 SECTION_DEBUG | SECTION_EXCLUDE,
29266 NULL);
29267 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29268 SECTION_DEBUG | SECTION_EXCLUDE,
29269 NULL);
29270 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29271 SECTION_DEBUG, NULL);
29272 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29273 SECTION_DEBUG, NULL);
29274 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29275 SECTION_DEBUG, NULL);
29276 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29277 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29278 init_sections_and_labels_generation);
29280 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29281 stay in the main .o, but the skeleton_line goes into the
29282 split off dwo. */
29283 debug_skeleton_line_section
29284 = get_section (DEBUG_DWO_LINE_SECTION,
29285 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29286 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29287 DEBUG_SKELETON_LINE_SECTION_LABEL,
29288 init_sections_and_labels_generation);
29289 debug_str_offsets_section
29290 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29291 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29292 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29293 DEBUG_SKELETON_INFO_SECTION_LABEL,
29294 init_sections_and_labels_generation);
29295 debug_loc_section = get_section (dwarf_version >= 5
29296 ? DEBUG_DWO_LOCLISTS_SECTION
29297 : DEBUG_DWO_LOC_SECTION,
29298 SECTION_DEBUG | SECTION_EXCLUDE,
29299 NULL);
29300 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29301 DEBUG_STR_DWO_SECTION_FLAGS,
29302 NULL);
29303 debug_macinfo_section_name
29304 = ((dwarf_strict && dwarf_version < 5)
29305 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29306 debug_macinfo_section = get_section (debug_macinfo_section_name,
29307 SECTION_DEBUG | SECTION_EXCLUDE,
29308 NULL);
29309 if (dwarf_version >= 5)
29310 debug_ranges_dwo_section
29311 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29312 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29314 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29315 SECTION_DEBUG, NULL);
29316 debug_line_section = get_section (DEBUG_LINE_SECTION,
29317 SECTION_DEBUG, NULL);
29318 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29319 SECTION_DEBUG, NULL);
29320 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29321 SECTION_DEBUG, NULL);
29322 debug_str_section = get_section (DEBUG_STR_SECTION,
29323 DEBUG_STR_SECTION_FLAGS, NULL);
29324 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29325 || asm_outputs_debug_line_str ())
29326 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29327 DEBUG_STR_SECTION_FLAGS, NULL);
29329 debug_ranges_section = get_section (dwarf_version >= 5
29330 ? DEBUG_RNGLISTS_SECTION
29331 : DEBUG_RANGES_SECTION,
29332 SECTION_DEBUG, NULL);
29333 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29334 SECTION_DEBUG, NULL);
29337 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29338 DEBUG_ABBREV_SECTION_LABEL,
29339 init_sections_and_labels_generation);
29340 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29341 DEBUG_INFO_SECTION_LABEL,
29342 init_sections_and_labels_generation);
29343 info_section_emitted = false;
29344 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29345 DEBUG_LINE_SECTION_LABEL,
29346 init_sections_and_labels_generation);
29347 /* There are up to 6 unique ranges labels per generation.
29348 See also output_rnglists. */
29349 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29350 DEBUG_RANGES_SECTION_LABEL,
29351 init_sections_and_labels_generation * 6);
29352 if (dwarf_version >= 5 && dwarf_split_debug_info)
29353 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29354 DEBUG_RANGES_SECTION_LABEL,
29355 1 + init_sections_and_labels_generation * 6);
29356 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29357 DEBUG_ADDR_SECTION_LABEL,
29358 init_sections_and_labels_generation);
29359 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29360 (dwarf_strict && dwarf_version < 5)
29361 ? DEBUG_MACINFO_SECTION_LABEL
29362 : DEBUG_MACRO_SECTION_LABEL,
29363 init_sections_and_labels_generation);
29364 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29365 init_sections_and_labels_generation);
29367 ++init_sections_and_labels_generation;
29368 return init_sections_and_labels_generation - 1;
29371 /* Set up for Dwarf output at the start of compilation. */
29373 static void
29374 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29376 /* Allocate the file_table. */
29377 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29379 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29380 /* Allocate the decl_die_table. */
29381 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29383 /* Allocate the decl_loc_table. */
29384 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29386 /* Allocate the cached_dw_loc_list_table. */
29387 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29389 /* Allocate the initial hunk of the abbrev_die_table. */
29390 vec_alloc (abbrev_die_table, 256);
29391 /* Zero-th entry is allocated, but unused. */
29392 abbrev_die_table->quick_push (NULL);
29394 /* Allocate the dwarf_proc_stack_usage_map. */
29395 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29397 /* Allocate the pubtypes and pubnames vectors. */
29398 vec_alloc (pubname_table, 32);
29399 vec_alloc (pubtype_table, 32);
29401 vec_alloc (incomplete_types, 64);
29403 vec_alloc (used_rtx_array, 32);
29405 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29406 vec_alloc (macinfo_table, 64);
29407 #endif
29409 /* If front-ends already registered a main translation unit but we were not
29410 ready to perform the association, do this now. */
29411 if (main_translation_unit != NULL_TREE)
29412 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29415 /* Called before compile () starts outputtting functions, variables
29416 and toplevel asms into assembly. */
29418 static void
29419 dwarf2out_assembly_start (void)
29421 if (text_section_line_info)
29422 return;
29424 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29425 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29426 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29427 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29428 COLD_TEXT_SECTION_LABEL, 0);
29429 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29431 switch_to_section (text_section);
29432 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29433 #endif
29435 /* Make sure the line number table for .text always exists. */
29436 text_section_line_info = new_line_info_table ();
29437 text_section_line_info->end_label = text_end_label;
29439 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29440 cur_line_info_table = text_section_line_info;
29441 #endif
29443 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29444 && dwarf2out_do_cfi_asm ()
29445 && !dwarf2out_do_eh_frame ())
29446 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29448 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29449 if (output_asm_line_debug_info () && dwarf_version >= 5)
29451 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29452 tell it the comp_dir and main file name for the zero entry
29453 line table. */
29454 const char *comp_dir, *filename0;
29456 comp_dir = comp_dir_string ();
29457 if (comp_dir == NULL)
29458 comp_dir = "";
29460 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29461 if (filename0 == NULL)
29462 filename0 = "";
29464 fprintf (asm_out_file, "\t.file 0 ");
29465 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29466 fputc (' ', asm_out_file);
29467 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29468 fputc ('\n', asm_out_file);
29470 else
29471 #endif
29472 /* Work around for PR101575: output a dummy .file directive. */
29473 if (!last_emitted_file && dwarf_debuginfo_p ()
29474 && debug_info_level >= DINFO_LEVEL_TERSE)
29476 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29478 if (filename0 == NULL)
29479 filename0 = "<dummy>";
29480 maybe_emit_file (lookup_filename (filename0));
29484 /* A helper function for dwarf2out_finish called through
29485 htab_traverse. Assign a string its index. All strings must be
29486 collected into the table by the time index_string is called,
29487 because the indexing code relies on htab_traverse to traverse nodes
29488 in the same order for each run. */
29491 index_string (indirect_string_node **h, unsigned int *index)
29493 indirect_string_node *node = *h;
29495 find_string_form (node);
29496 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29498 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29499 node->index = *index;
29500 *index += 1;
29502 return 1;
29505 /* A helper function for output_indirect_strings called through
29506 htab_traverse. Output the offset to a string and update the
29507 current offset. */
29510 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29512 indirect_string_node *node = *h;
29514 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29516 /* Assert that this node has been assigned an index. */
29517 gcc_assert (node->index != NO_INDEX_ASSIGNED
29518 && node->index != NOT_INDEXED);
29519 dw2_asm_output_data (dwarf_offset_size, *offset,
29520 "indexed string 0x%x: %s", node->index, node->str);
29521 *offset += strlen (node->str) + 1;
29523 return 1;
29526 /* A helper function for dwarf2out_finish called through
29527 htab_traverse. Output the indexed string. */
29530 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29532 struct indirect_string_node *node = *h;
29534 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29536 /* Assert that the strings are output in the same order as their
29537 indexes were assigned. */
29538 gcc_assert (*cur_idx == node->index);
29539 assemble_string (node->str, strlen (node->str) + 1);
29540 *cur_idx += 1;
29542 return 1;
29545 /* A helper function for output_indirect_strings. Counts the number
29546 of index strings offsets. Must match the logic of the functions
29547 output_index_string[_offsets] above. */
29549 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29551 struct indirect_string_node *node = *h;
29553 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29554 *last_idx += 1;
29555 return 1;
29558 /* A helper function for dwarf2out_finish called through
29559 htab_traverse. Emit one queued .debug_str string. */
29562 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29564 struct indirect_string_node *node = *h;
29566 node->form = find_string_form (node);
29567 if (node->form == form && node->refcount > 0)
29569 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29570 assemble_string (node->str, strlen (node->str) + 1);
29573 return 1;
29576 /* Output the indexed string table. */
29578 static void
29579 output_indirect_strings (void)
29581 switch_to_section (debug_str_section);
29582 if (!dwarf_split_debug_info)
29583 debug_str_hash->traverse<enum dwarf_form,
29584 output_indirect_string> (DW_FORM_strp);
29585 else
29587 unsigned int offset = 0;
29588 unsigned int cur_idx = 0;
29590 if (skeleton_debug_str_hash)
29591 skeleton_debug_str_hash->traverse<enum dwarf_form,
29592 output_indirect_string> (DW_FORM_strp);
29594 switch_to_section (debug_str_offsets_section);
29595 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29596 header. Note that we don't need to generate a label to the
29597 actual index table following the header here, because this is
29598 for the split dwarf case only. In an .dwo file there is only
29599 one string offsets table (and one debug info section). But
29600 if we would start using string offset tables for the main (or
29601 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29602 pointing to the actual index after the header. Split dwarf
29603 units will never have a string offsets base attribute. When
29604 a split unit is moved into a .dwp file the string offsets can
29605 be found through the .debug_cu_index section table. */
29606 if (dwarf_version >= 5)
29608 unsigned int last_idx = 0;
29609 unsigned long str_offsets_length;
29611 debug_str_hash->traverse_noresize
29612 <unsigned int *, count_index_strings> (&last_idx);
29613 str_offsets_length = last_idx * dwarf_offset_size + 4;
29614 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29615 dw2_asm_output_data (4, 0xffffffff,
29616 "Escape value for 64-bit DWARF extension");
29617 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29618 "Length of string offsets unit");
29619 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29620 dw2_asm_output_data (2, 0, "Header zero padding");
29622 debug_str_hash->traverse_noresize
29623 <unsigned int *, output_index_string_offset> (&offset);
29624 switch_to_section (debug_str_dwo_section);
29625 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29626 (&cur_idx);
29630 /* Callback for htab_traverse to assign an index to an entry in the
29631 table, and to write that entry to the .debug_addr section. */
29634 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29636 addr_table_entry *entry = *slot;
29638 if (entry->refcount == 0)
29640 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29641 || entry->index == NOT_INDEXED);
29642 return 1;
29645 gcc_assert (entry->index == *cur_index);
29646 (*cur_index)++;
29648 switch (entry->kind)
29650 case ate_kind_rtx:
29651 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29652 "0x%x", entry->index);
29653 break;
29654 case ate_kind_rtx_dtprel:
29655 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29656 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29657 DWARF2_ADDR_SIZE,
29658 entry->addr.rtl);
29659 fputc ('\n', asm_out_file);
29660 break;
29661 case ate_kind_label:
29662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29663 "0x%x", entry->index);
29664 break;
29665 default:
29666 gcc_unreachable ();
29668 return 1;
29671 /* A helper function for dwarf2out_finish. Counts the number
29672 of indexed addresses. Must match the logic of the functions
29673 output_addr_table_entry above. */
29675 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29677 addr_table_entry *entry = *slot;
29679 if (entry->refcount > 0)
29680 *last_idx += 1;
29681 return 1;
29684 /* Produce the .debug_addr section. */
29686 static void
29687 output_addr_table (void)
29689 unsigned int index = 0;
29690 if (addr_index_table == NULL || addr_index_table->size () == 0)
29691 return;
29693 switch_to_section (debug_addr_section);
29694 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29695 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29696 before DWARF5, didn't have a header for .debug_addr units.
29697 DWARF5 specifies a small header when address tables are used. */
29698 if (dwarf_version >= 5)
29700 unsigned int last_idx = 0;
29701 unsigned long addrs_length;
29703 addr_index_table->traverse_noresize
29704 <unsigned int *, count_index_addrs> (&last_idx);
29705 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29707 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29708 dw2_asm_output_data (4, 0xffffffff,
29709 "Escape value for 64-bit DWARF extension");
29710 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29711 "Length of Address Unit");
29712 dw2_asm_output_data (2, 5, "DWARF addr version");
29713 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29714 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29716 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29718 addr_index_table
29719 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29722 #if ENABLE_ASSERT_CHECKING
29723 /* Verify that all marks are clear. */
29725 static void
29726 verify_marks_clear (dw_die_ref die)
29728 dw_die_ref c;
29730 gcc_assert (! die->die_mark);
29731 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29733 #endif /* ENABLE_ASSERT_CHECKING */
29735 /* Clear the marks for a die and its children.
29736 Be cool if the mark isn't set. */
29738 static void
29739 prune_unmark_dies (dw_die_ref die)
29741 dw_die_ref c;
29743 if (die->die_mark)
29744 die->die_mark = 0;
29745 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29748 /* Given LOC that is referenced by a DIE we're marking as used, find all
29749 referenced DWARF procedures it references and mark them as used. */
29751 static void
29752 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29754 for (; loc != NULL; loc = loc->dw_loc_next)
29755 switch (loc->dw_loc_opc)
29757 case DW_OP_implicit_pointer:
29758 case DW_OP_convert:
29759 case DW_OP_reinterpret:
29760 case DW_OP_GNU_implicit_pointer:
29761 case DW_OP_GNU_convert:
29762 case DW_OP_GNU_reinterpret:
29763 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29764 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29765 break;
29766 case DW_OP_GNU_variable_value:
29767 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29769 dw_die_ref ref
29770 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29771 if (ref == NULL)
29772 break;
29773 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29774 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29775 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29777 /* FALLTHRU */
29778 case DW_OP_call2:
29779 case DW_OP_call4:
29780 case DW_OP_call_ref:
29781 case DW_OP_const_type:
29782 case DW_OP_GNU_const_type:
29783 case DW_OP_GNU_parameter_ref:
29784 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29785 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29786 break;
29787 case DW_OP_regval_type:
29788 case DW_OP_deref_type:
29789 case DW_OP_GNU_regval_type:
29790 case DW_OP_GNU_deref_type:
29791 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29792 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29793 break;
29794 case DW_OP_entry_value:
29795 case DW_OP_GNU_entry_value:
29796 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29797 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29798 break;
29799 default:
29800 break;
29804 /* Given DIE that we're marking as used, find any other dies
29805 it references as attributes and mark them as used. */
29807 static void
29808 prune_unused_types_walk_attribs (dw_die_ref die)
29810 dw_attr_node *a;
29811 unsigned ix;
29813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29815 switch (AT_class (a))
29817 /* Make sure DWARF procedures referenced by location descriptions will
29818 get emitted. */
29819 case dw_val_class_loc:
29820 prune_unused_types_walk_loc_descr (AT_loc (a));
29821 break;
29822 case dw_val_class_loc_list:
29823 for (dw_loc_list_ref list = AT_loc_list (a);
29824 list != NULL;
29825 list = list->dw_loc_next)
29826 prune_unused_types_walk_loc_descr (list->expr);
29827 break;
29829 case dw_val_class_view_list:
29830 /* This points to a loc_list in another attribute, so it's
29831 already covered. */
29832 break;
29834 case dw_val_class_die_ref:
29835 /* A reference to another DIE.
29836 Make sure that it will get emitted.
29837 If it was broken out into a comdat group, don't follow it. */
29838 if (! AT_ref (a)->comdat_type_p
29839 || a->dw_attr == DW_AT_specification)
29840 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29841 break;
29843 case dw_val_class_str:
29844 /* Set the string's refcount to 0 so that prune_unused_types_mark
29845 accounts properly for it. */
29846 a->dw_attr_val.v.val_str->refcount = 0;
29847 break;
29849 default:
29850 break;
29855 /* Mark the generic parameters and arguments children DIEs of DIE. */
29857 static void
29858 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29860 dw_die_ref c;
29862 if (die == NULL || die->die_child == NULL)
29863 return;
29864 c = die->die_child;
29867 if (is_template_parameter (c))
29868 prune_unused_types_mark (c, 1);
29869 c = c->die_sib;
29870 } while (c && c != die->die_child);
29873 /* Mark DIE as being used. If DOKIDS is true, then walk down
29874 to DIE's children. */
29876 static void
29877 prune_unused_types_mark (dw_die_ref die, int dokids)
29879 dw_die_ref c;
29881 if (die->die_mark == 0)
29883 /* We haven't done this node yet. Mark it as used. */
29884 die->die_mark = 1;
29885 /* If this is the DIE of a generic type instantiation,
29886 mark the children DIEs that describe its generic parms and
29887 args. */
29888 prune_unused_types_mark_generic_parms_dies (die);
29890 /* We also have to mark its parents as used.
29891 (But we don't want to mark our parent's kids due to this,
29892 unless it is a class.) */
29893 if (die->die_parent)
29894 prune_unused_types_mark (die->die_parent,
29895 class_scope_p (die->die_parent));
29897 /* Mark any referenced nodes. */
29898 prune_unused_types_walk_attribs (die);
29900 /* If this node is a specification,
29901 also mark the definition, if it exists. */
29902 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29903 prune_unused_types_mark (die->die_definition, 1);
29906 if (dokids && die->die_mark != 2)
29908 /* We need to walk the children, but haven't done so yet.
29909 Remember that we've walked the kids. */
29910 die->die_mark = 2;
29912 /* If this is an array type, we need to make sure our
29913 kids get marked, even if they're types. If we're
29914 breaking out types into comdat sections, do this
29915 for all type definitions. */
29916 if (die->die_tag == DW_TAG_array_type
29917 || (use_debug_types
29918 && is_type_die (die) && ! is_declaration_die (die)))
29919 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29920 else
29921 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29925 /* For local classes, look if any static member functions were emitted
29926 and if so, mark them. */
29928 static void
29929 prune_unused_types_walk_local_classes (dw_die_ref die)
29931 dw_die_ref c;
29933 if (die->die_mark == 2)
29934 return;
29936 switch (die->die_tag)
29938 case DW_TAG_structure_type:
29939 case DW_TAG_union_type:
29940 case DW_TAG_class_type:
29941 case DW_TAG_interface_type:
29942 break;
29944 case DW_TAG_subprogram:
29945 if (!get_AT_flag (die, DW_AT_declaration)
29946 || die->die_definition != NULL)
29947 prune_unused_types_mark (die, 1);
29948 return;
29950 default:
29951 return;
29954 /* Mark children. */
29955 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29958 /* Walk the tree DIE and mark types that we actually use. */
29960 static void
29961 prune_unused_types_walk (dw_die_ref die)
29963 dw_die_ref c;
29965 /* Don't do anything if this node is already marked and
29966 children have been marked as well. */
29967 if (die->die_mark == 2)
29968 return;
29970 switch (die->die_tag)
29972 case DW_TAG_structure_type:
29973 case DW_TAG_union_type:
29974 case DW_TAG_class_type:
29975 case DW_TAG_interface_type:
29976 if (die->die_perennial_p)
29977 break;
29979 for (c = die->die_parent; c; c = c->die_parent)
29980 if (c->die_tag == DW_TAG_subprogram)
29981 break;
29983 /* Finding used static member functions inside of classes
29984 is needed just for local classes, because for other classes
29985 static member function DIEs with DW_AT_specification
29986 are emitted outside of the DW_TAG_*_type. If we ever change
29987 it, we'd need to call this even for non-local classes. */
29988 if (c)
29989 prune_unused_types_walk_local_classes (die);
29991 /* It's a type node --- don't mark it. */
29992 return;
29994 case DW_TAG_const_type:
29995 case DW_TAG_packed_type:
29996 case DW_TAG_pointer_type:
29997 case DW_TAG_reference_type:
29998 case DW_TAG_rvalue_reference_type:
29999 case DW_TAG_volatile_type:
30000 case DW_TAG_typedef:
30001 case DW_TAG_array_type:
30002 case DW_TAG_friend:
30003 case DW_TAG_enumeration_type:
30004 case DW_TAG_subroutine_type:
30005 case DW_TAG_string_type:
30006 case DW_TAG_set_type:
30007 case DW_TAG_subrange_type:
30008 case DW_TAG_ptr_to_member_type:
30009 case DW_TAG_file_type:
30010 /* Type nodes are useful only when other DIEs reference them --- don't
30011 mark them. */
30012 /* FALLTHROUGH */
30014 case DW_TAG_dwarf_procedure:
30015 /* Likewise for DWARF procedures. */
30017 if (die->die_perennial_p)
30018 break;
30020 return;
30022 case DW_TAG_variable:
30023 if (flag_debug_only_used_symbols)
30025 if (die->die_perennial_p)
30026 break;
30028 /* For static data members, the declaration in the class is supposed
30029 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30030 DWARF5. DW_TAG_member will be marked, so mark even such
30031 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30032 attribute. */
30033 if (dwarf_version >= 5
30034 && class_scope_p (die->die_parent)
30035 && get_AT (die, DW_AT_const_value))
30036 break;
30038 /* premark_used_variables marks external variables --- don't mark
30039 them here. But function-local externals are always considered
30040 used. */
30041 if (get_AT (die, DW_AT_external))
30043 for (c = die->die_parent; c; c = c->die_parent)
30044 if (c->die_tag == DW_TAG_subprogram)
30045 break;
30046 if (!c)
30047 return;
30050 /* FALLTHROUGH */
30052 default:
30053 /* Mark everything else. */
30054 break;
30057 if (die->die_mark == 0)
30059 die->die_mark = 1;
30061 /* Now, mark any dies referenced from here. */
30062 prune_unused_types_walk_attribs (die);
30065 die->die_mark = 2;
30067 /* Mark children. */
30068 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30071 /* Increment the string counts on strings referred to from DIE's
30072 attributes. */
30074 static void
30075 prune_unused_types_update_strings (dw_die_ref die)
30077 dw_attr_node *a;
30078 unsigned ix;
30080 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30081 if (AT_class (a) == dw_val_class_str)
30083 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30084 s->refcount++;
30085 /* Avoid unnecessarily putting strings that are used less than
30086 twice in the hash table. */
30087 if (s->form != DW_FORM_line_strp
30088 && (s->refcount
30089 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30091 indirect_string_node **slot
30092 = debug_str_hash->find_slot_with_hash (s->str,
30093 htab_hash_string (s->str),
30094 INSERT);
30095 gcc_assert (*slot == NULL);
30096 *slot = s;
30101 /* Mark DIE and its children as removed. */
30103 static void
30104 mark_removed (dw_die_ref die)
30106 dw_die_ref c;
30107 die->removed = true;
30108 FOR_EACH_CHILD (die, c, mark_removed (c));
30111 /* Remove from the tree DIE any dies that aren't marked. */
30113 static void
30114 prune_unused_types_prune (dw_die_ref die)
30116 dw_die_ref c;
30118 gcc_assert (die->die_mark);
30119 prune_unused_types_update_strings (die);
30121 if (! die->die_child)
30122 return;
30124 c = die->die_child;
30125 do {
30126 dw_die_ref prev = c, next;
30127 for (c = c->die_sib; ! c->die_mark; c = next)
30128 if (c == die->die_child)
30130 /* No marked children between 'prev' and the end of the list. */
30131 if (prev == c)
30132 /* No marked children at all. */
30133 die->die_child = NULL;
30134 else
30136 prev->die_sib = c->die_sib;
30137 die->die_child = prev;
30139 c->die_sib = NULL;
30140 mark_removed (c);
30141 return;
30143 else
30145 next = c->die_sib;
30146 c->die_sib = NULL;
30147 mark_removed (c);
30150 if (c != prev->die_sib)
30151 prev->die_sib = c;
30152 prune_unused_types_prune (c);
30153 } while (c != die->die_child);
30156 /* Remove dies representing declarations that we never use. */
30158 static void
30159 prune_unused_types (void)
30161 unsigned int i;
30162 limbo_die_node *node;
30163 comdat_type_node *ctnode;
30164 pubname_entry *pub;
30165 dw_die_ref base_type;
30167 #if ENABLE_ASSERT_CHECKING
30168 /* All the marks should already be clear. */
30169 verify_marks_clear (comp_unit_die ());
30170 for (node = limbo_die_list; node; node = node->next)
30171 verify_marks_clear (node->die);
30172 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30173 verify_marks_clear (ctnode->root_die);
30174 #endif /* ENABLE_ASSERT_CHECKING */
30176 /* Mark types that are used in global variables. */
30177 premark_types_used_by_global_vars ();
30179 /* Mark variables used in the symtab. */
30180 if (flag_debug_only_used_symbols)
30181 premark_used_variables ();
30183 /* Set the mark on nodes that are actually used. */
30184 prune_unused_types_walk (comp_unit_die ());
30185 for (node = limbo_die_list; node; node = node->next)
30186 prune_unused_types_walk (node->die);
30187 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30189 prune_unused_types_walk (ctnode->root_die);
30190 prune_unused_types_mark (ctnode->type_die, 1);
30193 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30194 are unusual in that they are pubnames that are the children of pubtypes.
30195 They should only be marked via their parent DW_TAG_enumeration_type die,
30196 not as roots in themselves. */
30197 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30198 if (pub->die->die_tag != DW_TAG_enumerator)
30199 prune_unused_types_mark (pub->die, 1);
30200 for (i = 0; base_types.iterate (i, &base_type); i++)
30201 prune_unused_types_mark (base_type, 1);
30203 /* Also set the mark on nodes that could be referenced by
30204 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30205 by DW_TAG_inlined_subroutine origins. */
30206 cgraph_node *cnode;
30207 FOR_EACH_FUNCTION (cnode)
30208 if (cnode->referred_to_p (false))
30210 dw_die_ref die = lookup_decl_die (cnode->decl);
30211 if (die == NULL || die->die_mark)
30212 continue;
30213 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30214 if (e->caller != cnode)
30216 prune_unused_types_mark (die, 1);
30217 break;
30221 if (debug_str_hash)
30222 debug_str_hash->empty ();
30223 if (skeleton_debug_str_hash)
30224 skeleton_debug_str_hash->empty ();
30225 prune_unused_types_prune (comp_unit_die ());
30226 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30228 node = *pnode;
30229 if (!node->die->die_mark)
30230 *pnode = node->next;
30231 else
30233 prune_unused_types_prune (node->die);
30234 pnode = &node->next;
30237 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30238 prune_unused_types_prune (ctnode->root_die);
30240 /* Leave the marks clear. */
30241 prune_unmark_dies (comp_unit_die ());
30242 for (node = limbo_die_list; node; node = node->next)
30243 prune_unmark_dies (node->die);
30244 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30245 prune_unmark_dies (ctnode->root_die);
30248 /* Helpers to manipulate hash table of comdat type units. */
30250 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30252 static inline hashval_t hash (const comdat_type_node *);
30253 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30256 inline hashval_t
30257 comdat_type_hasher::hash (const comdat_type_node *type_node)
30259 hashval_t h;
30260 memcpy (&h, type_node->signature, sizeof (h));
30261 return h;
30264 inline bool
30265 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30266 const comdat_type_node *type_node_2)
30268 return (! memcmp (type_node_1->signature, type_node_2->signature,
30269 DWARF_TYPE_SIGNATURE_SIZE));
30272 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30273 to the location it would have been added, should we know its
30274 DECL_ASSEMBLER_NAME when we added other attributes. This will
30275 probably improve compactness of debug info, removing equivalent
30276 abbrevs, and hide any differences caused by deferring the
30277 computation of the assembler name, triggered by e.g. PCH. */
30279 static inline void
30280 move_linkage_attr (dw_die_ref die)
30282 unsigned ix = vec_safe_length (die->die_attr);
30283 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30285 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30286 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30288 while (--ix > 0)
30290 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30292 if (prev->dw_attr == DW_AT_decl_line
30293 || prev->dw_attr == DW_AT_decl_column
30294 || prev->dw_attr == DW_AT_name)
30295 break;
30298 if (ix != vec_safe_length (die->die_attr) - 1)
30300 die->die_attr->pop ();
30301 die->die_attr->quick_insert (ix, linkage);
30305 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30306 referenced from typed stack ops and count how often they are used. */
30308 static void
30309 mark_base_types (dw_loc_descr_ref loc)
30311 dw_die_ref base_type = NULL;
30313 for (; loc; loc = loc->dw_loc_next)
30315 switch (loc->dw_loc_opc)
30317 case DW_OP_regval_type:
30318 case DW_OP_deref_type:
30319 case DW_OP_GNU_regval_type:
30320 case DW_OP_GNU_deref_type:
30321 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30322 break;
30323 case DW_OP_convert:
30324 case DW_OP_reinterpret:
30325 case DW_OP_GNU_convert:
30326 case DW_OP_GNU_reinterpret:
30327 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30328 continue;
30329 /* FALLTHRU */
30330 case DW_OP_const_type:
30331 case DW_OP_GNU_const_type:
30332 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30333 break;
30334 case DW_OP_entry_value:
30335 case DW_OP_GNU_entry_value:
30336 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30337 continue;
30338 default:
30339 continue;
30341 gcc_assert (base_type->die_parent == comp_unit_die ());
30342 if (base_type->die_mark)
30343 base_type->die_mark++;
30344 else
30346 base_types.safe_push (base_type);
30347 base_type->die_mark = 1;
30352 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30353 referenced from typed stack ops and count how often they are used. */
30355 static void
30356 mark_base_types (dw_die_ref die)
30358 dw_die_ref c;
30359 dw_attr_node *a;
30360 dw_loc_list_ref *curr;
30361 unsigned ix;
30363 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30364 switch (AT_class (a))
30366 case dw_val_class_loc_list:
30367 curr = AT_loc_list_ptr (a);
30368 while (*curr)
30370 mark_base_types ((*curr)->expr);
30371 curr = &(*curr)->dw_loc_next;
30373 break;
30375 case dw_val_class_loc:
30376 mark_base_types (AT_loc (a));
30377 break;
30379 default:
30380 break;
30383 FOR_EACH_CHILD (die, c, mark_base_types (c));
30386 /* Comparison function for sorting marked base types. */
30388 static int
30389 base_type_cmp (const void *x, const void *y)
30391 dw_die_ref dx = *(const dw_die_ref *) x;
30392 dw_die_ref dy = *(const dw_die_ref *) y;
30393 unsigned int byte_size1, byte_size2;
30394 unsigned int encoding1, encoding2;
30395 unsigned int align1, align2;
30396 if (dx->die_mark > dy->die_mark)
30397 return -1;
30398 if (dx->die_mark < dy->die_mark)
30399 return 1;
30400 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30401 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30402 if (byte_size1 < byte_size2)
30403 return 1;
30404 if (byte_size1 > byte_size2)
30405 return -1;
30406 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30407 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30408 if (encoding1 < encoding2)
30409 return 1;
30410 if (encoding1 > encoding2)
30411 return -1;
30412 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30413 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30414 if (align1 < align2)
30415 return 1;
30416 if (align1 > align2)
30417 return -1;
30418 return 0;
30421 /* Move base types marked by mark_base_types as early as possible
30422 in the CU, sorted by decreasing usage count both to make the
30423 uleb128 references as small as possible and to make sure they
30424 will have die_offset already computed by calc_die_sizes when
30425 sizes of typed stack loc ops is computed. */
30427 static void
30428 move_marked_base_types (void)
30430 unsigned int i;
30431 dw_die_ref base_type, die, c;
30433 if (base_types.is_empty ())
30434 return;
30436 /* Sort by decreasing usage count, they will be added again in that
30437 order later on. */
30438 base_types.qsort (base_type_cmp);
30439 die = comp_unit_die ();
30440 c = die->die_child;
30443 dw_die_ref prev = c;
30444 c = c->die_sib;
30445 while (c->die_mark)
30447 remove_child_with_prev (c, prev);
30448 /* As base types got marked, there must be at least
30449 one node other than DW_TAG_base_type. */
30450 gcc_assert (die->die_child != NULL);
30451 c = prev->die_sib;
30454 while (c != die->die_child);
30455 gcc_assert (die->die_child);
30456 c = die->die_child;
30457 for (i = 0; base_types.iterate (i, &base_type); i++)
30459 base_type->die_mark = 0;
30460 base_type->die_sib = c->die_sib;
30461 c->die_sib = base_type;
30462 c = base_type;
30466 /* Helper function for resolve_addr, attempt to resolve
30467 one CONST_STRING, return true if successful. Similarly verify that
30468 SYMBOL_REFs refer to variables emitted in the current CU. */
30470 static bool
30471 resolve_one_addr (rtx *addr)
30473 rtx rtl = *addr;
30475 if (GET_CODE (rtl) == CONST_STRING)
30477 size_t len = strlen (XSTR (rtl, 0)) + 1;
30478 tree t = build_string (len, XSTR (rtl, 0));
30479 tree tlen = size_int (len - 1);
30480 TREE_TYPE (t)
30481 = build_array_type (char_type_node, build_index_type (tlen));
30482 rtl = lookup_constant_def (t);
30483 if (!rtl || !MEM_P (rtl))
30484 return false;
30485 rtl = XEXP (rtl, 0);
30486 if (GET_CODE (rtl) == SYMBOL_REF
30487 && SYMBOL_REF_DECL (rtl)
30488 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30489 return false;
30490 vec_safe_push (used_rtx_array, rtl);
30491 *addr = rtl;
30492 return true;
30495 if (GET_CODE (rtl) == SYMBOL_REF
30496 && SYMBOL_REF_DECL (rtl))
30498 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30500 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30501 return false;
30503 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30504 return false;
30507 if (GET_CODE (rtl) == CONST)
30509 subrtx_ptr_iterator::array_type array;
30510 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30511 if (!resolve_one_addr (*iter))
30512 return false;
30515 return true;
30518 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30519 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30520 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30522 static rtx
30523 string_cst_pool_decl (tree t)
30525 rtx rtl = output_constant_def (t, 1);
30526 unsigned char *array;
30527 dw_loc_descr_ref l;
30528 tree decl;
30529 size_t len;
30530 dw_die_ref ref;
30532 if (!rtl || !MEM_P (rtl))
30533 return NULL_RTX;
30534 rtl = XEXP (rtl, 0);
30535 if (GET_CODE (rtl) != SYMBOL_REF
30536 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30537 return NULL_RTX;
30539 decl = SYMBOL_REF_DECL (rtl);
30540 if (!lookup_decl_die (decl))
30542 len = TREE_STRING_LENGTH (t);
30543 vec_safe_push (used_rtx_array, rtl);
30544 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30545 array = ggc_vec_alloc<unsigned char> (len);
30546 memcpy (array, TREE_STRING_POINTER (t), len);
30547 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30548 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30549 l->dw_loc_oprnd2.v.val_vec.length = len;
30550 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30551 l->dw_loc_oprnd2.v.val_vec.array = array;
30552 add_AT_loc (ref, DW_AT_location, l);
30553 equate_decl_number_to_die (decl, ref);
30555 return rtl;
30558 /* Helper function of resolve_addr_in_expr. LOC is
30559 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30560 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30561 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30562 with DW_OP_implicit_pointer if possible
30563 and return true, if unsuccessful, return false. */
30565 static bool
30566 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30568 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30569 HOST_WIDE_INT offset = 0;
30570 dw_die_ref ref = NULL;
30571 tree decl;
30573 if (GET_CODE (rtl) == CONST
30574 && GET_CODE (XEXP (rtl, 0)) == PLUS
30575 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30577 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30578 rtl = XEXP (XEXP (rtl, 0), 0);
30580 if (GET_CODE (rtl) == CONST_STRING)
30582 size_t len = strlen (XSTR (rtl, 0)) + 1;
30583 tree t = build_string (len, XSTR (rtl, 0));
30584 tree tlen = size_int (len - 1);
30586 TREE_TYPE (t)
30587 = build_array_type (char_type_node, build_index_type (tlen));
30588 rtl = string_cst_pool_decl (t);
30589 if (!rtl)
30590 return false;
30592 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30594 decl = SYMBOL_REF_DECL (rtl);
30595 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30597 ref = lookup_decl_die (decl);
30598 if (ref && (get_AT (ref, DW_AT_location)
30599 || get_AT (ref, DW_AT_const_value)))
30601 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30602 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30603 loc->dw_loc_oprnd1.val_entry = NULL;
30604 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30605 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30606 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30607 loc->dw_loc_oprnd2.v.val_int = offset;
30608 return true;
30612 return false;
30615 /* Helper function for resolve_addr, handle one location
30616 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30617 the location list couldn't be resolved. */
30619 static bool
30620 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30622 dw_loc_descr_ref keep = NULL;
30623 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30624 switch (loc->dw_loc_opc)
30626 case DW_OP_addr:
30627 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30629 if ((prev == NULL
30630 || prev->dw_loc_opc == DW_OP_piece
30631 || prev->dw_loc_opc == DW_OP_bit_piece)
30632 && loc->dw_loc_next
30633 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30634 && (!dwarf_strict || dwarf_version >= 5)
30635 && optimize_one_addr_into_implicit_ptr (loc))
30636 break;
30637 return false;
30639 break;
30640 case DW_OP_GNU_addr_index:
30641 case DW_OP_addrx:
30642 case DW_OP_GNU_const_index:
30643 case DW_OP_constx:
30644 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30645 || loc->dw_loc_opc == DW_OP_addrx)
30646 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30647 || loc->dw_loc_opc == DW_OP_constx)
30648 && loc->dtprel))
30650 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30651 if (!resolve_one_addr (&rtl))
30652 return false;
30653 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30654 loc->dw_loc_oprnd1.val_entry
30655 = add_addr_table_entry (rtl, ate_kind_rtx);
30657 break;
30658 case DW_OP_const4u:
30659 case DW_OP_const8u:
30660 if (loc->dtprel
30661 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30662 return false;
30663 break;
30664 case DW_OP_plus_uconst:
30665 if (size_of_loc_descr (loc)
30666 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30668 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30670 dw_loc_descr_ref repl
30671 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30672 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30673 add_loc_descr (&repl, loc->dw_loc_next);
30674 *loc = *repl;
30676 break;
30677 case DW_OP_implicit_value:
30678 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30679 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30680 return false;
30681 break;
30682 case DW_OP_implicit_pointer:
30683 case DW_OP_GNU_implicit_pointer:
30684 case DW_OP_GNU_parameter_ref:
30685 case DW_OP_GNU_variable_value:
30686 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30688 dw_die_ref ref
30689 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30690 if (ref == NULL)
30691 return false;
30692 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30693 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30694 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30696 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30698 if (prev == NULL
30699 && loc->dw_loc_next == NULL
30700 && AT_class (a) == dw_val_class_loc)
30701 switch (a->dw_attr)
30703 /* Following attributes allow both exprloc and reference,
30704 so if the whole expression is DW_OP_GNU_variable_value
30705 alone we could transform it into reference. */
30706 case DW_AT_byte_size:
30707 case DW_AT_bit_size:
30708 case DW_AT_lower_bound:
30709 case DW_AT_upper_bound:
30710 case DW_AT_bit_stride:
30711 case DW_AT_count:
30712 case DW_AT_allocated:
30713 case DW_AT_associated:
30714 case DW_AT_byte_stride:
30715 a->dw_attr_val.val_class = dw_val_class_die_ref;
30716 a->dw_attr_val.val_entry = NULL;
30717 a->dw_attr_val.v.val_die_ref.die
30718 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30719 a->dw_attr_val.v.val_die_ref.external = 0;
30720 return true;
30721 default:
30722 break;
30724 if (dwarf_strict)
30725 return false;
30727 break;
30728 case DW_OP_const_type:
30729 case DW_OP_regval_type:
30730 case DW_OP_deref_type:
30731 case DW_OP_convert:
30732 case DW_OP_reinterpret:
30733 case DW_OP_GNU_const_type:
30734 case DW_OP_GNU_regval_type:
30735 case DW_OP_GNU_deref_type:
30736 case DW_OP_GNU_convert:
30737 case DW_OP_GNU_reinterpret:
30738 while (loc->dw_loc_next
30739 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30740 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30742 dw_die_ref base1, base2;
30743 unsigned enc1, enc2, size1, size2;
30744 if (loc->dw_loc_opc == DW_OP_regval_type
30745 || loc->dw_loc_opc == DW_OP_deref_type
30746 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30747 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30748 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30749 else if (loc->dw_loc_oprnd1.val_class
30750 == dw_val_class_unsigned_const)
30751 break;
30752 else
30753 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30754 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30755 == dw_val_class_unsigned_const)
30756 break;
30757 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30758 gcc_assert (base1->die_tag == DW_TAG_base_type
30759 && base2->die_tag == DW_TAG_base_type);
30760 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30761 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30762 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30763 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30764 if (size1 == size2
30765 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30766 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30767 && loc != keep)
30768 || enc1 == enc2))
30770 /* Optimize away next DW_OP_convert after
30771 adjusting LOC's base type die reference. */
30772 if (loc->dw_loc_opc == DW_OP_regval_type
30773 || loc->dw_loc_opc == DW_OP_deref_type
30774 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30775 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30776 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30777 else
30778 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30779 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30780 continue;
30782 /* Don't change integer DW_OP_convert after e.g. floating
30783 point typed stack entry. */
30784 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30785 keep = loc->dw_loc_next;
30786 break;
30788 break;
30789 default:
30790 break;
30792 return true;
30795 /* Helper function of resolve_addr. DIE had DW_AT_location of
30796 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30797 and DW_OP_addr couldn't be resolved. resolve_addr has already
30798 removed the DW_AT_location attribute. This function attempts to
30799 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30800 to it or DW_AT_const_value attribute, if possible. */
30802 static void
30803 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30805 if (!VAR_P (decl)
30806 || lookup_decl_die (decl) != die
30807 || DECL_EXTERNAL (decl)
30808 || !TREE_STATIC (decl)
30809 || DECL_INITIAL (decl) == NULL_TREE
30810 || DECL_P (DECL_INITIAL (decl))
30811 || get_AT (die, DW_AT_const_value))
30812 return;
30814 tree init = DECL_INITIAL (decl);
30815 HOST_WIDE_INT offset = 0;
30816 /* For variables that have been optimized away and thus
30817 don't have a memory location, see if we can emit
30818 DW_AT_const_value instead. */
30819 if (tree_add_const_value_attribute (die, init))
30820 return;
30821 if (dwarf_strict && dwarf_version < 5)
30822 return;
30823 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30824 and ADDR_EXPR refers to a decl that has DW_AT_location or
30825 DW_AT_const_value (but isn't addressable, otherwise
30826 resolving the original DW_OP_addr wouldn't fail), see if
30827 we can add DW_OP_implicit_pointer. */
30828 STRIP_NOPS (init);
30829 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30830 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30832 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30833 init = TREE_OPERAND (init, 0);
30834 STRIP_NOPS (init);
30836 if (TREE_CODE (init) != ADDR_EXPR)
30837 return;
30838 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30839 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30840 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30841 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30842 && TREE_OPERAND (init, 0) != decl))
30844 dw_die_ref ref;
30845 dw_loc_descr_ref l;
30847 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30849 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30850 if (!rtl)
30851 return;
30852 decl = SYMBOL_REF_DECL (rtl);
30854 else
30855 decl = TREE_OPERAND (init, 0);
30856 ref = lookup_decl_die (decl);
30857 if (ref == NULL
30858 || (!get_AT (ref, DW_AT_location)
30859 && !get_AT (ref, DW_AT_const_value)))
30860 return;
30861 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30862 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30863 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30864 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30865 add_AT_loc (die, DW_AT_location, l);
30869 /* Return NULL if l is a DWARF expression, or first op that is not
30870 valid DWARF expression. */
30872 static dw_loc_descr_ref
30873 non_dwarf_expression (dw_loc_descr_ref l)
30875 while (l)
30877 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30878 return l;
30879 switch (l->dw_loc_opc)
30881 case DW_OP_regx:
30882 case DW_OP_implicit_value:
30883 case DW_OP_stack_value:
30884 case DW_OP_implicit_pointer:
30885 case DW_OP_GNU_implicit_pointer:
30886 case DW_OP_GNU_parameter_ref:
30887 case DW_OP_piece:
30888 case DW_OP_bit_piece:
30889 return l;
30890 default:
30891 break;
30893 l = l->dw_loc_next;
30895 return NULL;
30898 /* Return adjusted copy of EXPR:
30899 If it is empty DWARF expression, return it.
30900 If it is valid non-empty DWARF expression,
30901 return copy of EXPR with DW_OP_deref appended to it.
30902 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30903 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30904 If it is DWARF expression followed by DW_OP_stack_value, return
30905 copy of the DWARF expression without anything appended.
30906 Otherwise, return NULL. */
30908 static dw_loc_descr_ref
30909 copy_deref_exprloc (dw_loc_descr_ref expr)
30911 dw_loc_descr_ref tail = NULL;
30913 if (expr == NULL)
30914 return NULL;
30916 dw_loc_descr_ref l = non_dwarf_expression (expr);
30917 if (l && l->dw_loc_next)
30918 return NULL;
30920 if (l)
30922 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30923 tail = new_loc_descr ((enum dwarf_location_atom)
30924 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30925 0, 0);
30926 else
30927 switch (l->dw_loc_opc)
30929 case DW_OP_regx:
30930 tail = new_loc_descr (DW_OP_bregx,
30931 l->dw_loc_oprnd1.v.val_unsigned, 0);
30932 break;
30933 case DW_OP_stack_value:
30934 break;
30935 default:
30936 return NULL;
30939 else
30940 tail = new_loc_descr (DW_OP_deref, 0, 0);
30942 dw_loc_descr_ref ret = NULL, *p = &ret;
30943 while (expr != l)
30945 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30946 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30947 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30948 p = &(*p)->dw_loc_next;
30949 expr = expr->dw_loc_next;
30951 *p = tail;
30952 return ret;
30955 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30956 reference to a variable or argument, adjust it if needed and return:
30957 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30958 attribute if present should be removed
30959 0 keep the attribute perhaps with minor modifications, no need to rescan
30960 1 if the attribute has been successfully adjusted. */
30962 static int
30963 optimize_string_length (dw_attr_node *a)
30965 dw_loc_descr_ref l = AT_loc (a), lv;
30966 dw_die_ref die;
30967 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30969 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30970 die = lookup_decl_die (decl);
30971 if (die)
30973 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30974 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30975 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30977 else
30978 return -1;
30980 else
30981 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30983 /* DWARF5 allows reference class, so we can then reference the DIE.
30984 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30985 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30987 a->dw_attr_val.val_class = dw_val_class_die_ref;
30988 a->dw_attr_val.val_entry = NULL;
30989 a->dw_attr_val.v.val_die_ref.die = die;
30990 a->dw_attr_val.v.val_die_ref.external = 0;
30991 return 0;
30994 dw_attr_node *av = get_AT (die, DW_AT_location);
30995 dw_loc_list_ref d;
30996 bool non_dwarf_expr = false;
30998 if (av == NULL)
30999 return dwarf_strict ? -1 : 0;
31000 switch (AT_class (av))
31002 case dw_val_class_loc_list:
31003 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31004 if (d->expr && non_dwarf_expression (d->expr))
31005 non_dwarf_expr = true;
31006 break;
31007 case dw_val_class_view_list:
31008 gcc_unreachable ();
31009 case dw_val_class_loc:
31010 lv = AT_loc (av);
31011 if (lv == NULL)
31012 return dwarf_strict ? -1 : 0;
31013 if (non_dwarf_expression (lv))
31014 non_dwarf_expr = true;
31015 break;
31016 default:
31017 return dwarf_strict ? -1 : 0;
31020 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31021 into DW_OP_call4 or DW_OP_GNU_variable_value into
31022 DW_OP_call4 DW_OP_deref, do so. */
31023 if (!non_dwarf_expr
31024 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31026 l->dw_loc_opc = DW_OP_call4;
31027 if (l->dw_loc_next)
31028 l->dw_loc_next = NULL;
31029 else
31030 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31031 return 0;
31034 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31035 copy over the DW_AT_location attribute from die to a. */
31036 if (l->dw_loc_next != NULL)
31038 a->dw_attr_val = av->dw_attr_val;
31039 return 1;
31042 dw_loc_list_ref list, *p;
31043 switch (AT_class (av))
31045 case dw_val_class_loc_list:
31046 p = &list;
31047 list = NULL;
31048 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31050 lv = copy_deref_exprloc (d->expr);
31051 if (lv)
31053 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31054 p = &(*p)->dw_loc_next;
31056 else if (!dwarf_strict && d->expr)
31057 return 0;
31059 if (list == NULL)
31060 return dwarf_strict ? -1 : 0;
31061 a->dw_attr_val.val_class = dw_val_class_loc_list;
31062 gen_llsym (list);
31063 *AT_loc_list_ptr (a) = list;
31064 return 1;
31065 case dw_val_class_loc:
31066 lv = copy_deref_exprloc (AT_loc (av));
31067 if (lv == NULL)
31068 return dwarf_strict ? -1 : 0;
31069 a->dw_attr_val.v.val_loc = lv;
31070 return 1;
31071 default:
31072 gcc_unreachable ();
31076 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31077 an address in .rodata section if the string literal is emitted there,
31078 or remove the containing location list or replace DW_AT_const_value
31079 with DW_AT_location and empty location expression, if it isn't found
31080 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31081 to something that has been emitted in the current CU. */
31083 static void
31084 resolve_addr (dw_die_ref die)
31086 dw_die_ref c;
31087 dw_attr_node *a;
31088 dw_loc_list_ref *curr, *start, loc;
31089 unsigned ix;
31090 bool remove_AT_byte_size = false;
31092 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31093 switch (AT_class (a))
31095 case dw_val_class_loc_list:
31096 start = curr = AT_loc_list_ptr (a);
31097 loc = *curr;
31098 gcc_assert (loc);
31099 /* The same list can be referenced more than once. See if we have
31100 already recorded the result from a previous pass. */
31101 if (loc->replaced)
31102 *curr = loc->dw_loc_next;
31103 else if (!loc->resolved_addr)
31105 /* As things stand, we do not expect or allow one die to
31106 reference a suffix of another die's location list chain.
31107 References must be identical or completely separate.
31108 There is therefore no need to cache the result of this
31109 pass on any list other than the first; doing so
31110 would lead to unnecessary writes. */
31111 while (*curr)
31113 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31114 if (!resolve_addr_in_expr (a, (*curr)->expr))
31116 dw_loc_list_ref next = (*curr)->dw_loc_next;
31117 dw_loc_descr_ref l = (*curr)->expr;
31119 if (next && (*curr)->ll_symbol)
31121 gcc_assert (!next->ll_symbol);
31122 next->ll_symbol = (*curr)->ll_symbol;
31123 next->vl_symbol = (*curr)->vl_symbol;
31125 if (dwarf_split_debug_info)
31126 remove_loc_list_addr_table_entries (l);
31127 *curr = next;
31129 else
31131 mark_base_types ((*curr)->expr);
31132 curr = &(*curr)->dw_loc_next;
31135 if (loc == *start)
31136 loc->resolved_addr = 1;
31137 else
31139 loc->replaced = 1;
31140 loc->dw_loc_next = *start;
31143 if (!*start)
31145 remove_AT (die, a->dw_attr);
31146 ix--;
31148 break;
31149 case dw_val_class_view_list:
31151 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31152 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31153 dw_val_node *llnode
31154 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31155 /* If we no longer have a loclist, or it no longer needs
31156 views, drop this attribute. */
31157 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31159 remove_AT (die, a->dw_attr);
31160 ix--;
31162 break;
31164 case dw_val_class_loc:
31166 dw_loc_descr_ref l = AT_loc (a);
31167 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31168 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31169 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31170 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31171 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31172 with DW_FORM_ref referencing the same DIE as
31173 DW_OP_GNU_variable_value used to reference. */
31174 if (a->dw_attr == DW_AT_string_length
31175 && l
31176 && l->dw_loc_opc == DW_OP_GNU_variable_value
31177 && (l->dw_loc_next == NULL
31178 || (l->dw_loc_next->dw_loc_next == NULL
31179 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31181 switch (optimize_string_length (a))
31183 case -1:
31184 remove_AT (die, a->dw_attr);
31185 ix--;
31186 /* If we drop DW_AT_string_length, we need to drop also
31187 DW_AT_{string_length_,}byte_size. */
31188 remove_AT_byte_size = true;
31189 continue;
31190 default:
31191 break;
31192 case 1:
31193 /* Even if we keep the optimized DW_AT_string_length,
31194 it might have changed AT_class, so process it again. */
31195 ix--;
31196 continue;
31199 /* For -gdwarf-2 don't attempt to optimize
31200 DW_AT_data_member_location containing
31201 DW_OP_plus_uconst - older consumers might
31202 rely on it being that op instead of a more complex,
31203 but shorter, location description. */
31204 if ((dwarf_version > 2
31205 || a->dw_attr != DW_AT_data_member_location
31206 || l == NULL
31207 || l->dw_loc_opc != DW_OP_plus_uconst
31208 || l->dw_loc_next != NULL)
31209 && !resolve_addr_in_expr (a, l))
31211 if (dwarf_split_debug_info)
31212 remove_loc_list_addr_table_entries (l);
31213 if (l != NULL
31214 && l->dw_loc_next == NULL
31215 && l->dw_loc_opc == DW_OP_addr
31216 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31217 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31218 && a->dw_attr == DW_AT_location)
31220 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31221 remove_AT (die, a->dw_attr);
31222 ix--;
31223 optimize_location_into_implicit_ptr (die, decl);
31224 break;
31226 if (a->dw_attr == DW_AT_string_length)
31227 /* If we drop DW_AT_string_length, we need to drop also
31228 DW_AT_{string_length_,}byte_size. */
31229 remove_AT_byte_size = true;
31230 remove_AT (die, a->dw_attr);
31231 ix--;
31233 else
31234 mark_base_types (l);
31236 break;
31237 case dw_val_class_addr:
31238 if (a->dw_attr == DW_AT_const_value
31239 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31241 if (AT_index (a) != NOT_INDEXED)
31242 remove_addr_table_entry (a->dw_attr_val.val_entry);
31243 remove_AT (die, a->dw_attr);
31244 ix--;
31246 if ((die->die_tag == DW_TAG_call_site
31247 && a->dw_attr == DW_AT_call_origin)
31248 || (die->die_tag == DW_TAG_GNU_call_site
31249 && a->dw_attr == DW_AT_abstract_origin))
31251 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31252 dw_die_ref tdie = lookup_decl_die (tdecl);
31253 dw_die_ref cdie;
31254 if (tdie == NULL
31255 && DECL_EXTERNAL (tdecl)
31256 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31257 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31259 dw_die_ref pdie = cdie;
31260 /* Make sure we don't add these DIEs into type units.
31261 We could emit skeleton DIEs for context (namespaces,
31262 outer structs/classes) and a skeleton DIE for the
31263 innermost context with DW_AT_signature pointing to the
31264 type unit. See PR78835. */
31265 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31266 pdie = pdie->die_parent;
31267 if (pdie == NULL)
31269 /* Creating a full DIE for tdecl is overly expensive and
31270 at this point even wrong when in the LTO phase
31271 as it can end up generating new type DIEs we didn't
31272 output and thus optimize_external_refs will crash. */
31273 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31274 add_AT_flag (tdie, DW_AT_external, 1);
31275 add_AT_flag (tdie, DW_AT_declaration, 1);
31276 add_linkage_attr (tdie, tdecl);
31277 add_name_and_src_coords_attributes (tdie, tdecl, true);
31278 equate_decl_number_to_die (tdecl, tdie);
31281 if (tdie)
31283 a->dw_attr_val.val_class = dw_val_class_die_ref;
31284 a->dw_attr_val.v.val_die_ref.die = tdie;
31285 a->dw_attr_val.v.val_die_ref.external = 0;
31287 else
31289 if (AT_index (a) != NOT_INDEXED)
31290 remove_addr_table_entry (a->dw_attr_val.val_entry);
31291 remove_AT (die, a->dw_attr);
31292 ix--;
31295 break;
31296 default:
31297 break;
31300 if (remove_AT_byte_size)
31301 remove_AT (die, dwarf_version >= 5
31302 ? DW_AT_string_length_byte_size
31303 : DW_AT_byte_size);
31305 FOR_EACH_CHILD (die, c, resolve_addr (c));
31308 /* Helper routines for optimize_location_lists.
31309 This pass tries to share identical local lists in .debug_loc
31310 section. */
31312 /* Iteratively hash operands of LOC opcode into HSTATE. */
31314 static void
31315 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31317 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31318 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31320 switch (loc->dw_loc_opc)
31322 case DW_OP_const4u:
31323 case DW_OP_const8u:
31324 if (loc->dtprel)
31325 goto hash_addr;
31326 /* FALLTHRU */
31327 case DW_OP_const1u:
31328 case DW_OP_const1s:
31329 case DW_OP_const2u:
31330 case DW_OP_const2s:
31331 case DW_OP_const4s:
31332 case DW_OP_const8s:
31333 case DW_OP_constu:
31334 case DW_OP_consts:
31335 case DW_OP_pick:
31336 case DW_OP_plus_uconst:
31337 case DW_OP_breg0:
31338 case DW_OP_breg1:
31339 case DW_OP_breg2:
31340 case DW_OP_breg3:
31341 case DW_OP_breg4:
31342 case DW_OP_breg5:
31343 case DW_OP_breg6:
31344 case DW_OP_breg7:
31345 case DW_OP_breg8:
31346 case DW_OP_breg9:
31347 case DW_OP_breg10:
31348 case DW_OP_breg11:
31349 case DW_OP_breg12:
31350 case DW_OP_breg13:
31351 case DW_OP_breg14:
31352 case DW_OP_breg15:
31353 case DW_OP_breg16:
31354 case DW_OP_breg17:
31355 case DW_OP_breg18:
31356 case DW_OP_breg19:
31357 case DW_OP_breg20:
31358 case DW_OP_breg21:
31359 case DW_OP_breg22:
31360 case DW_OP_breg23:
31361 case DW_OP_breg24:
31362 case DW_OP_breg25:
31363 case DW_OP_breg26:
31364 case DW_OP_breg27:
31365 case DW_OP_breg28:
31366 case DW_OP_breg29:
31367 case DW_OP_breg30:
31368 case DW_OP_breg31:
31369 case DW_OP_regx:
31370 case DW_OP_fbreg:
31371 case DW_OP_piece:
31372 case DW_OP_deref_size:
31373 case DW_OP_xderef_size:
31374 hstate.add_object (val1->v.val_int);
31375 break;
31376 case DW_OP_skip:
31377 case DW_OP_bra:
31379 int offset;
31381 gcc_assert (val1->val_class == dw_val_class_loc);
31382 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31383 hstate.add_object (offset);
31385 break;
31386 case DW_OP_implicit_value:
31387 hstate.add_object (val1->v.val_unsigned);
31388 switch (val2->val_class)
31390 case dw_val_class_const:
31391 hstate.add_object (val2->v.val_int);
31392 break;
31393 case dw_val_class_vec:
31395 unsigned int elt_size = val2->v.val_vec.elt_size;
31396 unsigned int len = val2->v.val_vec.length;
31398 hstate.add_int (elt_size);
31399 hstate.add_int (len);
31400 hstate.add (val2->v.val_vec.array, len * elt_size);
31402 break;
31403 case dw_val_class_const_double:
31404 hstate.add_object (val2->v.val_double.low);
31405 hstate.add_object (val2->v.val_double.high);
31406 break;
31407 case dw_val_class_wide_int:
31408 hstate.add (val2->v.val_wide->get_val (),
31409 get_full_len (*val2->v.val_wide)
31410 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31411 break;
31412 case dw_val_class_addr:
31413 inchash::add_rtx (val2->v.val_addr, hstate);
31414 break;
31415 default:
31416 gcc_unreachable ();
31418 break;
31419 case DW_OP_bregx:
31420 case DW_OP_bit_piece:
31421 hstate.add_object (val1->v.val_int);
31422 hstate.add_object (val2->v.val_int);
31423 break;
31424 case DW_OP_addr:
31425 hash_addr:
31426 if (loc->dtprel)
31428 unsigned char dtprel = 0xd1;
31429 hstate.add_object (dtprel);
31431 inchash::add_rtx (val1->v.val_addr, hstate);
31432 break;
31433 case DW_OP_GNU_addr_index:
31434 case DW_OP_addrx:
31435 case DW_OP_GNU_const_index:
31436 case DW_OP_constx:
31438 if (loc->dtprel)
31440 unsigned char dtprel = 0xd1;
31441 hstate.add_object (dtprel);
31443 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31445 break;
31446 case DW_OP_implicit_pointer:
31447 case DW_OP_GNU_implicit_pointer:
31448 hstate.add_int (val2->v.val_int);
31449 break;
31450 case DW_OP_entry_value:
31451 case DW_OP_GNU_entry_value:
31452 hstate.add_object (val1->v.val_loc);
31453 break;
31454 case DW_OP_regval_type:
31455 case DW_OP_deref_type:
31456 case DW_OP_GNU_regval_type:
31457 case DW_OP_GNU_deref_type:
31459 unsigned int byte_size
31460 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31461 unsigned int encoding
31462 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31463 hstate.add_object (val1->v.val_int);
31464 hstate.add_object (byte_size);
31465 hstate.add_object (encoding);
31467 break;
31468 case DW_OP_convert:
31469 case DW_OP_reinterpret:
31470 case DW_OP_GNU_convert:
31471 case DW_OP_GNU_reinterpret:
31472 if (val1->val_class == dw_val_class_unsigned_const)
31474 hstate.add_object (val1->v.val_unsigned);
31475 break;
31477 /* FALLTHRU */
31478 case DW_OP_const_type:
31479 case DW_OP_GNU_const_type:
31481 unsigned int byte_size
31482 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31483 unsigned int encoding
31484 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31485 hstate.add_object (byte_size);
31486 hstate.add_object (encoding);
31487 if (loc->dw_loc_opc != DW_OP_const_type
31488 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31489 break;
31490 hstate.add_object (val2->val_class);
31491 switch (val2->val_class)
31493 case dw_val_class_const:
31494 hstate.add_object (val2->v.val_int);
31495 break;
31496 case dw_val_class_vec:
31498 unsigned int elt_size = val2->v.val_vec.elt_size;
31499 unsigned int len = val2->v.val_vec.length;
31501 hstate.add_object (elt_size);
31502 hstate.add_object (len);
31503 hstate.add (val2->v.val_vec.array, len * elt_size);
31505 break;
31506 case dw_val_class_const_double:
31507 hstate.add_object (val2->v.val_double.low);
31508 hstate.add_object (val2->v.val_double.high);
31509 break;
31510 case dw_val_class_wide_int:
31511 hstate.add (val2->v.val_wide->get_val (),
31512 get_full_len (*val2->v.val_wide)
31513 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31514 break;
31515 default:
31516 gcc_unreachable ();
31519 break;
31521 default:
31522 /* Other codes have no operands. */
31523 break;
31527 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31529 static inline void
31530 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31532 dw_loc_descr_ref l;
31533 bool sizes_computed = false;
31534 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31535 size_of_locs (loc);
31537 for (l = loc; l != NULL; l = l->dw_loc_next)
31539 enum dwarf_location_atom opc = l->dw_loc_opc;
31540 hstate.add_object (opc);
31541 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31543 size_of_locs (loc);
31544 sizes_computed = true;
31546 hash_loc_operands (l, hstate);
31550 /* Compute hash of the whole location list LIST_HEAD. */
31552 static inline void
31553 hash_loc_list (dw_loc_list_ref list_head)
31555 dw_loc_list_ref curr = list_head;
31556 inchash::hash hstate;
31558 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31560 hstate.add (curr->begin, strlen (curr->begin) + 1);
31561 hstate.add (curr->end, strlen (curr->end) + 1);
31562 hstate.add_object (curr->vbegin);
31563 hstate.add_object (curr->vend);
31564 if (curr->section)
31565 hstate.add (curr->section, strlen (curr->section) + 1);
31566 hash_locs (curr->expr, hstate);
31568 list_head->hash = hstate.end ();
31571 /* Return true if X and Y opcodes have the same operands. */
31573 static inline bool
31574 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31576 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31577 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31578 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31579 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31581 switch (x->dw_loc_opc)
31583 case DW_OP_const4u:
31584 case DW_OP_const8u:
31585 if (x->dtprel)
31586 goto hash_addr;
31587 /* FALLTHRU */
31588 case DW_OP_const1u:
31589 case DW_OP_const1s:
31590 case DW_OP_const2u:
31591 case DW_OP_const2s:
31592 case DW_OP_const4s:
31593 case DW_OP_const8s:
31594 case DW_OP_constu:
31595 case DW_OP_consts:
31596 case DW_OP_pick:
31597 case DW_OP_plus_uconst:
31598 case DW_OP_breg0:
31599 case DW_OP_breg1:
31600 case DW_OP_breg2:
31601 case DW_OP_breg3:
31602 case DW_OP_breg4:
31603 case DW_OP_breg5:
31604 case DW_OP_breg6:
31605 case DW_OP_breg7:
31606 case DW_OP_breg8:
31607 case DW_OP_breg9:
31608 case DW_OP_breg10:
31609 case DW_OP_breg11:
31610 case DW_OP_breg12:
31611 case DW_OP_breg13:
31612 case DW_OP_breg14:
31613 case DW_OP_breg15:
31614 case DW_OP_breg16:
31615 case DW_OP_breg17:
31616 case DW_OP_breg18:
31617 case DW_OP_breg19:
31618 case DW_OP_breg20:
31619 case DW_OP_breg21:
31620 case DW_OP_breg22:
31621 case DW_OP_breg23:
31622 case DW_OP_breg24:
31623 case DW_OP_breg25:
31624 case DW_OP_breg26:
31625 case DW_OP_breg27:
31626 case DW_OP_breg28:
31627 case DW_OP_breg29:
31628 case DW_OP_breg30:
31629 case DW_OP_breg31:
31630 case DW_OP_regx:
31631 case DW_OP_fbreg:
31632 case DW_OP_piece:
31633 case DW_OP_deref_size:
31634 case DW_OP_xderef_size:
31635 return valx1->v.val_int == valy1->v.val_int;
31636 case DW_OP_skip:
31637 case DW_OP_bra:
31638 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31639 can cause irrelevant differences in dw_loc_addr. */
31640 gcc_assert (valx1->val_class == dw_val_class_loc
31641 && valy1->val_class == dw_val_class_loc
31642 && (dwarf_split_debug_info
31643 || x->dw_loc_addr == y->dw_loc_addr));
31644 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31645 case DW_OP_implicit_value:
31646 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31647 || valx2->val_class != valy2->val_class)
31648 return false;
31649 switch (valx2->val_class)
31651 case dw_val_class_const:
31652 return valx2->v.val_int == valy2->v.val_int;
31653 case dw_val_class_vec:
31654 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31655 && valx2->v.val_vec.length == valy2->v.val_vec.length
31656 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31657 valx2->v.val_vec.elt_size
31658 * valx2->v.val_vec.length) == 0;
31659 case dw_val_class_const_double:
31660 return valx2->v.val_double.low == valy2->v.val_double.low
31661 && valx2->v.val_double.high == valy2->v.val_double.high;
31662 case dw_val_class_wide_int:
31663 return *valx2->v.val_wide == *valy2->v.val_wide;
31664 case dw_val_class_addr:
31665 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31666 default:
31667 gcc_unreachable ();
31669 case DW_OP_bregx:
31670 case DW_OP_bit_piece:
31671 return valx1->v.val_int == valy1->v.val_int
31672 && valx2->v.val_int == valy2->v.val_int;
31673 case DW_OP_addr:
31674 hash_addr:
31675 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31676 case DW_OP_GNU_addr_index:
31677 case DW_OP_addrx:
31678 case DW_OP_GNU_const_index:
31679 case DW_OP_constx:
31681 rtx ax1 = valx1->val_entry->addr.rtl;
31682 rtx ay1 = valy1->val_entry->addr.rtl;
31683 return rtx_equal_p (ax1, ay1);
31685 case DW_OP_implicit_pointer:
31686 case DW_OP_GNU_implicit_pointer:
31687 return valx1->val_class == dw_val_class_die_ref
31688 && valx1->val_class == valy1->val_class
31689 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31690 && valx2->v.val_int == valy2->v.val_int;
31691 case DW_OP_entry_value:
31692 case DW_OP_GNU_entry_value:
31693 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31694 case DW_OP_const_type:
31695 case DW_OP_GNU_const_type:
31696 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31697 || valx2->val_class != valy2->val_class)
31698 return false;
31699 switch (valx2->val_class)
31701 case dw_val_class_const:
31702 return valx2->v.val_int == valy2->v.val_int;
31703 case dw_val_class_vec:
31704 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31705 && valx2->v.val_vec.length == valy2->v.val_vec.length
31706 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31707 valx2->v.val_vec.elt_size
31708 * valx2->v.val_vec.length) == 0;
31709 case dw_val_class_const_double:
31710 return valx2->v.val_double.low == valy2->v.val_double.low
31711 && valx2->v.val_double.high == valy2->v.val_double.high;
31712 case dw_val_class_wide_int:
31713 return *valx2->v.val_wide == *valy2->v.val_wide;
31714 default:
31715 gcc_unreachable ();
31717 case DW_OP_regval_type:
31718 case DW_OP_deref_type:
31719 case DW_OP_GNU_regval_type:
31720 case DW_OP_GNU_deref_type:
31721 return valx1->v.val_int == valy1->v.val_int
31722 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31723 case DW_OP_convert:
31724 case DW_OP_reinterpret:
31725 case DW_OP_GNU_convert:
31726 case DW_OP_GNU_reinterpret:
31727 if (valx1->val_class != valy1->val_class)
31728 return false;
31729 if (valx1->val_class == dw_val_class_unsigned_const)
31730 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31731 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31732 case DW_OP_GNU_parameter_ref:
31733 return valx1->val_class == dw_val_class_die_ref
31734 && valx1->val_class == valy1->val_class
31735 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31736 default:
31737 /* Other codes have no operands. */
31738 return true;
31742 /* Return true if DWARF location expressions X and Y are the same. */
31744 static inline bool
31745 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31747 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31748 if (x->dw_loc_opc != y->dw_loc_opc
31749 || x->dtprel != y->dtprel
31750 || !compare_loc_operands (x, y))
31751 break;
31752 return x == NULL && y == NULL;
31755 /* Hashtable helpers. */
31757 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31759 static inline hashval_t hash (const dw_loc_list_struct *);
31760 static inline bool equal (const dw_loc_list_struct *,
31761 const dw_loc_list_struct *);
31764 /* Return precomputed hash of location list X. */
31766 inline hashval_t
31767 loc_list_hasher::hash (const dw_loc_list_struct *x)
31769 return x->hash;
31772 /* Return true if location lists A and B are the same. */
31774 inline bool
31775 loc_list_hasher::equal (const dw_loc_list_struct *a,
31776 const dw_loc_list_struct *b)
31778 if (a == b)
31779 return 1;
31780 if (a->hash != b->hash)
31781 return 0;
31782 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31783 if (strcmp (a->begin, b->begin) != 0
31784 || strcmp (a->end, b->end) != 0
31785 || (a->section == NULL) != (b->section == NULL)
31786 || (a->section && strcmp (a->section, b->section) != 0)
31787 || a->vbegin != b->vbegin || a->vend != b->vend
31788 || !compare_locs (a->expr, b->expr))
31789 break;
31790 return a == NULL && b == NULL;
31793 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31796 /* Recursively optimize location lists referenced from DIE
31797 children and share them whenever possible. */
31799 static void
31800 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31802 dw_die_ref c;
31803 dw_attr_node *a;
31804 unsigned ix;
31805 dw_loc_list_struct **slot;
31806 bool drop_locviews = false;
31807 bool has_locviews = false;
31809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31810 if (AT_class (a) == dw_val_class_loc_list)
31812 dw_loc_list_ref list = AT_loc_list (a);
31813 /* TODO: perform some optimizations here, before hashing
31814 it and storing into the hash table. */
31815 hash_loc_list (list);
31816 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31817 if (*slot == NULL)
31819 *slot = list;
31820 if (loc_list_has_views (list))
31821 gcc_assert (list->vl_symbol);
31822 else if (list->vl_symbol)
31824 drop_locviews = true;
31825 list->vl_symbol = NULL;
31828 else
31830 if (list->vl_symbol && !(*slot)->vl_symbol)
31831 drop_locviews = true;
31832 a->dw_attr_val.v.val_loc_list = *slot;
31835 else if (AT_class (a) == dw_val_class_view_list)
31837 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31838 has_locviews = true;
31842 if (drop_locviews && has_locviews)
31843 remove_AT (die, DW_AT_GNU_locviews);
31845 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31849 /* Recursively assign each location list a unique index into the debug_addr
31850 section. */
31852 static void
31853 index_location_lists (dw_die_ref die)
31855 dw_die_ref c;
31856 dw_attr_node *a;
31857 unsigned ix;
31859 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31860 if (AT_class (a) == dw_val_class_loc_list)
31862 dw_loc_list_ref list = AT_loc_list (a);
31863 dw_loc_list_ref curr;
31864 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31866 /* Don't index an entry that has already been indexed
31867 or won't be output. Make sure skip_loc_list_entry doesn't
31868 call size_of_locs, because that might cause circular dependency,
31869 index_location_lists requiring address table indexes to be
31870 computed, but adding new indexes through add_addr_table_entry
31871 and address table index computation requiring no new additions
31872 to the hash table. In the rare case of DWARF[234] >= 64KB
31873 location expression, we'll just waste unused address table entry
31874 for it. */
31875 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31876 continue;
31878 curr->begin_entry
31879 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31880 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31881 curr->end_entry
31882 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31886 FOR_EACH_CHILD (die, c, index_location_lists (c));
31889 /* Optimize location lists referenced from DIE
31890 children and share them whenever possible. */
31892 static void
31893 optimize_location_lists (dw_die_ref die)
31895 loc_list_hash_type htab (500);
31896 optimize_location_lists_1 (die, &htab);
31899 /* Traverse the limbo die list, and add parent/child links. The only
31900 dies without parents that should be here are concrete instances of
31901 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31902 For concrete instances, we can get the parent die from the abstract
31903 instance. */
31905 static void
31906 flush_limbo_die_list (void)
31908 limbo_die_node *node;
31910 /* get_context_die calls force_decl_die, which can put new DIEs on the
31911 limbo list in LTO mode when nested functions are put in a different
31912 partition than that of their parent function. */
31913 while ((node = limbo_die_list))
31915 dw_die_ref die = node->die;
31916 limbo_die_list = node->next;
31918 if (die->die_parent == NULL)
31920 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31922 if (origin && origin->die_parent)
31923 add_child_die (origin->die_parent, die);
31924 else if (is_cu_die (die))
31926 else if (seen_error ())
31927 /* It's OK to be confused by errors in the input. */
31928 add_child_die (comp_unit_die (), die);
31929 else
31931 /* In certain situations, the lexical block containing a
31932 nested function can be optimized away, which results
31933 in the nested function die being orphaned. Likewise
31934 with the return type of that nested function. Force
31935 this to be a child of the containing function.
31937 It may happen that even the containing function got fully
31938 inlined and optimized out. In that case we are lost and
31939 assign the empty child. This should not be big issue as
31940 the function is likely unreachable too. */
31941 gcc_assert (node->created_for);
31943 if (DECL_P (node->created_for))
31944 origin = get_context_die (DECL_CONTEXT (node->created_for));
31945 else if (TYPE_P (node->created_for))
31946 origin = scope_die_for (node->created_for, comp_unit_die ());
31947 else
31948 origin = comp_unit_die ();
31950 add_child_die (origin, die);
31956 /* Reset DIEs so we can output them again. */
31958 static void
31959 reset_dies (dw_die_ref die)
31961 dw_die_ref c;
31963 /* Remove stuff we re-generate. */
31964 die->die_mark = 0;
31965 die->die_offset = 0;
31966 die->die_abbrev = 0;
31967 remove_AT (die, DW_AT_sibling);
31969 FOR_EACH_CHILD (die, c, reset_dies (c));
31972 /* reset_indirect_string removed the references coming from DW_AT_name
31973 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31974 .debug_line_str strings again. */
31976 static void
31977 adjust_name_comp_dir (dw_die_ref die)
31979 for (int i = 0; i < 2; i++)
31981 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31982 dw_attr_node *a = get_AT (die, attr_kind);
31983 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31984 continue;
31986 if (!debug_line_str_hash)
31987 debug_line_str_hash
31988 = hash_table<indirect_string_hasher>::create_ggc (10);
31990 struct indirect_string_node *node
31991 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31992 debug_line_str_hash);
31993 set_indirect_string (node);
31994 node->form = DW_FORM_line_strp;
31995 a->dw_attr_val.v.val_str = node;
31999 /* Output stuff that dwarf requires at the end of every file,
32000 and generate the DWARF-2 debugging info. */
32002 static void
32003 dwarf2out_finish (const char *filename)
32005 comdat_type_node *ctnode;
32006 dw_die_ref main_comp_unit_die;
32007 unsigned char checksum[16];
32008 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32010 /* Generate CTF/BTF debug info. */
32011 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32012 || btf_debuginfo_p ()) && lang_GNU_C ())
32013 ctf_debug_finish (filename);
32015 /* Skip emitting DWARF if not required. */
32016 if (!dwarf_debuginfo_p ())
32017 return;
32019 /* Flush out any latecomers to the limbo party. */
32020 flush_limbo_die_list ();
32022 if (inline_entry_data_table)
32023 gcc_assert (inline_entry_data_table->is_empty ());
32025 if (flag_checking)
32027 verify_die (comp_unit_die ());
32028 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32029 verify_die (node->die);
32032 /* We shouldn't have any symbols with delayed asm names for
32033 DIEs generated after early finish. */
32034 gcc_assert (deferred_asm_name == NULL);
32036 gen_remaining_tmpl_value_param_die_attribute ();
32038 if (flag_generate_lto || flag_generate_offload)
32040 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32042 /* Prune stuff so that dwarf2out_finish runs successfully
32043 for the fat part of the object. */
32044 reset_dies (comp_unit_die ());
32045 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32046 reset_dies (node->die);
32048 hash_table<comdat_type_hasher> comdat_type_table (100);
32049 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32051 comdat_type_node **slot
32052 = comdat_type_table.find_slot (ctnode, INSERT);
32054 /* Don't reset types twice. */
32055 if (*slot != HTAB_EMPTY_ENTRY)
32056 continue;
32058 /* Remove the pointer to the line table. */
32059 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32061 if (debug_info_level >= DINFO_LEVEL_TERSE)
32062 reset_dies (ctnode->root_die);
32064 *slot = ctnode;
32067 /* Reset die CU symbol so we don't output it twice. */
32068 comp_unit_die ()->die_id.die_symbol = NULL;
32070 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32071 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32072 if (have_macinfo)
32073 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32075 /* Remove indirect string decisions. */
32076 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32077 if (debug_line_str_hash)
32079 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32080 debug_line_str_hash = NULL;
32081 if (asm_outputs_debug_line_str ())
32083 adjust_name_comp_dir (comp_unit_die ());
32084 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32085 adjust_name_comp_dir (node->die);
32090 #if ENABLE_ASSERT_CHECKING
32092 dw_die_ref die = comp_unit_die (), c;
32093 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32095 #endif
32096 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32097 resolve_addr (ctnode->root_die);
32098 resolve_addr (comp_unit_die ());
32099 move_marked_base_types ();
32101 if (dump_file)
32103 fprintf (dump_file, "DWARF for %s\n", filename);
32104 print_die (comp_unit_die (), dump_file);
32107 /* Initialize sections and labels used for actual assembler output. */
32108 unsigned generation = init_sections_and_labels (false);
32110 /* Traverse the DIE's and add sibling attributes to those DIE's that
32111 have children. */
32112 add_sibling_attributes (comp_unit_die ());
32113 limbo_die_node *node;
32114 for (node = cu_die_list; node; node = node->next)
32115 add_sibling_attributes (node->die);
32116 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32117 add_sibling_attributes (ctnode->root_die);
32119 /* When splitting DWARF info, we put some attributes in the
32120 skeleton compile_unit DIE that remains in the .o, while
32121 most attributes go in the DWO compile_unit_die. */
32122 if (dwarf_split_debug_info)
32124 limbo_die_node *cu;
32125 main_comp_unit_die = gen_compile_unit_die (NULL);
32126 if (dwarf_version >= 5)
32127 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32128 cu = limbo_die_list;
32129 gcc_assert (cu->die == main_comp_unit_die);
32130 limbo_die_list = limbo_die_list->next;
32131 cu->next = cu_die_list;
32132 cu_die_list = cu;
32134 else
32135 main_comp_unit_die = comp_unit_die ();
32137 /* Output a terminator label for the .text section. */
32138 switch_to_section (text_section);
32139 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32140 if (cold_text_section)
32142 switch_to_section (cold_text_section);
32143 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32146 /* We can only use the low/high_pc attributes if all of the code was
32147 in .text. */
32148 if ((!have_multiple_function_sections
32149 && vec_safe_length (switch_text_ranges) < 2)
32150 || (dwarf_version < 3 && dwarf_strict))
32152 const char *end_label = text_end_label;
32153 if (vec_safe_length (switch_text_ranges) == 1)
32154 end_label = (*switch_text_ranges)[0];
32155 /* Don't add if the CU has no associated code. */
32156 if (switch_text_ranges)
32157 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32158 end_label, true);
32160 else
32162 unsigned fde_idx;
32163 dw_fde_ref fde;
32164 bool range_list_added = false;
32165 if (switch_text_ranges)
32167 const char *prev_loc = text_section_label;
32168 const char *loc;
32169 unsigned idx;
32171 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32172 if (prev_loc)
32174 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32175 loc, &range_list_added, true);
32176 prev_loc = NULL;
32178 else
32179 prev_loc = loc;
32181 if (prev_loc)
32182 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32183 text_end_label, &range_list_added, true);
32186 if (switch_cold_ranges)
32188 const char *prev_loc = cold_text_section_label;
32189 const char *loc;
32190 unsigned idx;
32192 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32193 if (prev_loc)
32195 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32196 loc, &range_list_added, true);
32197 prev_loc = NULL;
32199 else
32200 prev_loc = loc;
32202 if (prev_loc)
32203 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32204 cold_end_label, &range_list_added, true);
32207 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32209 if (fde->ignored_debug)
32210 continue;
32211 if (!fde->in_std_section)
32212 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32213 fde->dw_fde_end, &range_list_added,
32214 true);
32215 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32216 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32217 fde->dw_fde_second_end, &range_list_added,
32218 true);
32221 if (range_list_added)
32223 /* We need to give .debug_loc and .debug_ranges an appropriate
32224 "base address". Use zero so that these addresses become
32225 absolute. Historically, we've emitted the unexpected
32226 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32227 Emit both to give time for other tools to adapt. */
32228 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32229 if (! dwarf_strict && dwarf_version < 4)
32230 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32232 add_ranges (NULL);
32233 have_multiple_function_sections = true;
32237 /* AIX Assembler inserts the length, so adjust the reference to match the
32238 offset expected by debuggers. */
32239 strcpy (dl_section_ref, debug_line_section_label);
32240 if (XCOFF_DEBUGGING_INFO)
32241 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32243 if (debug_info_level >= DINFO_LEVEL_TERSE)
32244 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32245 dl_section_ref);
32247 if (have_macinfo)
32248 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32249 macinfo_section_label);
32251 if (dwarf_split_debug_info)
32253 if (have_location_lists)
32255 /* Since we generate the loclists in the split DWARF .dwo
32256 file itself, we don't need to generate a loclists_base
32257 attribute for the split compile unit DIE. That attribute
32258 (and using relocatable sec_offset FORMs) isn't allowed
32259 for a split compile unit. Only if the .debug_loclists
32260 section was in the main file, would we need to generate a
32261 loclists_base attribute here (for the full or skeleton
32262 unit DIE). */
32264 /* optimize_location_lists calculates the size of the lists,
32265 so index them first, and assign indices to the entries.
32266 Although optimize_location_lists will remove entries from
32267 the table, it only does so for duplicates, and therefore
32268 only reduces ref_counts to 1. */
32269 index_location_lists (comp_unit_die ());
32272 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32273 index_rnglists ();
32275 if (addr_index_table != NULL)
32277 unsigned int index = 0;
32278 addr_index_table
32279 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32280 (&index);
32284 loc_list_idx = 0;
32285 if (have_location_lists)
32287 optimize_location_lists (comp_unit_die ());
32288 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32289 if (dwarf_version >= 5 && dwarf_split_debug_info)
32290 assign_location_list_indexes (comp_unit_die ());
32293 save_macinfo_strings ();
32295 if (dwarf_split_debug_info)
32297 unsigned int index = 0;
32299 /* Add attributes common to skeleton compile_units and
32300 type_units. Because these attributes include strings, it
32301 must be done before freezing the string table. Top-level
32302 skeleton die attrs are added when the skeleton type unit is
32303 created, so ensure it is created by this point. */
32304 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32305 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32308 /* Output all of the compilation units. We put the main one last so that
32309 the offsets are available to output_pubnames. */
32310 for (node = cu_die_list; node; node = node->next)
32311 output_comp_unit (node->die, 0, NULL);
32313 hash_table<comdat_type_hasher> comdat_type_table (100);
32314 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32316 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32318 /* Don't output duplicate types. */
32319 if (*slot != HTAB_EMPTY_ENTRY)
32320 continue;
32322 /* Add a pointer to the line table for the main compilation unit
32323 so that the debugger can make sense of DW_AT_decl_file
32324 attributes. */
32325 if (debug_info_level >= DINFO_LEVEL_TERSE)
32326 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32327 (!dwarf_split_debug_info
32328 ? dl_section_ref
32329 : debug_skeleton_line_section_label));
32331 output_comdat_type_unit (ctnode, false);
32332 *slot = ctnode;
32335 if (dwarf_split_debug_info)
32337 int mark;
32338 struct md5_ctx ctx;
32340 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32341 md5_init_ctx (&ctx);
32342 mark = 0;
32343 die_checksum (comp_unit_die (), &ctx, &mark);
32344 unmark_all_dies (comp_unit_die ());
32345 md5_finish_ctx (&ctx, checksum);
32347 if (dwarf_version < 5)
32349 /* Use the first 8 bytes of the checksum as the dwo_id,
32350 and add it to both comp-unit DIEs. */
32351 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32352 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32355 /* Add the base offset of the ranges table to the skeleton
32356 comp-unit DIE. */
32357 if (!vec_safe_is_empty (ranges_table))
32359 if (dwarf_version < 5)
32360 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32361 ranges_section_label);
32364 output_addr_table ();
32367 /* Output the main compilation unit if non-empty or if .debug_macinfo
32368 or .debug_macro will be emitted. */
32369 output_comp_unit (comp_unit_die (), have_macinfo,
32370 dwarf_split_debug_info ? checksum : NULL);
32372 if (dwarf_split_debug_info && info_section_emitted)
32373 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32375 /* Output the abbreviation table. */
32376 if (vec_safe_length (abbrev_die_table) != 1)
32378 switch_to_section (debug_abbrev_section);
32379 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32380 output_abbrev_section ();
32383 /* Output location list section if necessary. */
32384 if (have_location_lists)
32386 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32387 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32388 /* Output the location lists info. */
32389 switch_to_section (debug_loc_section);
32390 if (dwarf_version >= 5)
32392 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32393 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32394 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32395 dw2_asm_output_data (4, 0xffffffff,
32396 "Initial length escape value indicating "
32397 "64-bit DWARF extension");
32398 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32399 "Length of Location Lists");
32400 ASM_OUTPUT_LABEL (asm_out_file, l1);
32401 output_dwarf_version ();
32402 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32403 dw2_asm_output_data (1, 0, "Segment Size");
32404 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32405 "Offset Entry Count");
32407 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32408 if (dwarf_version >= 5 && dwarf_split_debug_info)
32410 unsigned int save_loc_list_idx = loc_list_idx;
32411 loc_list_idx = 0;
32412 output_loclists_offsets (comp_unit_die ());
32413 gcc_assert (save_loc_list_idx == loc_list_idx);
32415 output_location_lists (comp_unit_die ());
32416 if (dwarf_version >= 5)
32417 ASM_OUTPUT_LABEL (asm_out_file, l2);
32420 output_pubtables ();
32422 /* Output the address range information if a CU (.debug_info section)
32423 was emitted. We output an empty table even if we had no functions
32424 to put in it. This because the consumer has no way to tell the
32425 difference between an empty table that we omitted and failure to
32426 generate a table that would have contained data. */
32427 if (info_section_emitted)
32429 switch_to_section (debug_aranges_section);
32430 output_aranges ();
32433 /* Output ranges section if necessary. */
32434 if (!vec_safe_is_empty (ranges_table))
32436 if (dwarf_version >= 5)
32438 if (dwarf_split_debug_info)
32440 /* We don't know right now whether there are any
32441 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32442 Depending on into which of those two belongs the first
32443 ranges_table entry, emit that section first and that
32444 output_rnglists call will return true if the other kind of
32445 ranges needs to be emitted as well. */
32446 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32447 if (output_rnglists (generation, dwo))
32448 output_rnglists (generation, !dwo);
32450 else
32451 output_rnglists (generation, false);
32453 else
32454 output_ranges ();
32457 /* Have to end the macro section. */
32458 if (have_macinfo)
32460 switch_to_section (debug_macinfo_section);
32461 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32462 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32463 : debug_skeleton_line_section_label, false);
32464 dw2_asm_output_data (1, 0, "End compilation unit");
32467 /* Output the source line correspondence table. We must do this
32468 even if there is no line information. Otherwise, on an empty
32469 translation unit, we will generate a present, but empty,
32470 .debug_info section. IRIX 6.5 `nm' will then complain when
32471 examining the file. This is done late so that any filenames
32472 used by the debug_info section are marked as 'used'. */
32473 switch_to_section (debug_line_section);
32474 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32475 if (! output_asm_line_debug_info ())
32476 output_line_info (false);
32478 if (dwarf_split_debug_info && info_section_emitted)
32480 switch_to_section (debug_skeleton_line_section);
32481 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32482 output_line_info (true);
32485 /* If we emitted any indirect strings, output the string table too. */
32486 if (debug_str_hash || skeleton_debug_str_hash)
32487 output_indirect_strings ();
32488 if (debug_line_str_hash)
32490 switch_to_section (debug_line_str_section);
32491 const enum dwarf_form form = DW_FORM_line_strp;
32492 debug_line_str_hash->traverse<enum dwarf_form,
32493 output_indirect_string> (form);
32496 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32497 symview_upper_bound = 0;
32498 if (zero_view_p)
32499 bitmap_clear (zero_view_p);
32502 /* Returns a hash value for X (which really is a variable_value_struct). */
32504 inline hashval_t
32505 variable_value_hasher::hash (variable_value_struct *x)
32507 return (hashval_t) x->decl_id;
32510 /* Return nonzero if decl_id of variable_value_struct X is the same as
32511 UID of decl Y. */
32513 inline bool
32514 variable_value_hasher::equal (variable_value_struct *x, tree y)
32516 return x->decl_id == DECL_UID (y);
32519 /* Helper function for resolve_variable_value, handle
32520 DW_OP_GNU_variable_value in one location expression.
32521 Return true if exprloc has been changed into loclist. */
32523 static bool
32524 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32526 dw_loc_descr_ref next;
32527 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32529 next = loc->dw_loc_next;
32530 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32531 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32532 continue;
32534 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32535 if (DECL_CONTEXT (decl) != current_function_decl)
32536 continue;
32538 dw_die_ref ref = lookup_decl_die (decl);
32539 if (ref)
32541 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32542 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32543 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32544 continue;
32546 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32547 if (l == NULL)
32548 continue;
32549 if (l->dw_loc_next)
32551 if (AT_class (a) != dw_val_class_loc)
32552 continue;
32553 switch (a->dw_attr)
32555 /* Following attributes allow both exprloc and loclist
32556 classes, so we can change them into a loclist. */
32557 case DW_AT_location:
32558 case DW_AT_string_length:
32559 case DW_AT_return_addr:
32560 case DW_AT_data_member_location:
32561 case DW_AT_frame_base:
32562 case DW_AT_segment:
32563 case DW_AT_static_link:
32564 case DW_AT_use_location:
32565 case DW_AT_vtable_elem_location:
32566 if (prev)
32568 prev->dw_loc_next = NULL;
32569 prepend_loc_descr_to_each (l, AT_loc (a));
32571 if (next)
32572 add_loc_descr_to_each (l, next);
32573 a->dw_attr_val.val_class = dw_val_class_loc_list;
32574 a->dw_attr_val.val_entry = NULL;
32575 a->dw_attr_val.v.val_loc_list = l;
32576 have_location_lists = true;
32577 return true;
32578 /* Following attributes allow both exprloc and reference,
32579 so if the whole expression is DW_OP_GNU_variable_value alone
32580 we could transform it into reference. */
32581 case DW_AT_byte_size:
32582 case DW_AT_bit_size:
32583 case DW_AT_lower_bound:
32584 case DW_AT_upper_bound:
32585 case DW_AT_bit_stride:
32586 case DW_AT_count:
32587 case DW_AT_allocated:
32588 case DW_AT_associated:
32589 case DW_AT_byte_stride:
32590 if (prev == NULL && next == NULL)
32591 break;
32592 /* FALLTHRU */
32593 default:
32594 if (dwarf_strict)
32595 continue;
32596 break;
32598 /* Create DW_TAG_variable that we can refer to. */
32599 gen_decl_die (decl, NULL_TREE, NULL,
32600 lookup_decl_die (current_function_decl));
32601 ref = lookup_decl_die (decl);
32602 if (ref)
32604 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32605 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32606 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32608 continue;
32610 if (prev)
32612 prev->dw_loc_next = l->expr;
32613 add_loc_descr (&prev->dw_loc_next, next);
32614 free_loc_descr (loc, NULL);
32615 next = prev->dw_loc_next;
32617 else
32619 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32620 add_loc_descr (&loc, next);
32621 next = loc;
32623 loc = prev;
32625 return false;
32628 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32630 static void
32631 resolve_variable_value (dw_die_ref die)
32633 dw_attr_node *a;
32634 dw_loc_list_ref loc;
32635 unsigned ix;
32637 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32638 switch (AT_class (a))
32640 case dw_val_class_loc:
32641 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32642 break;
32643 /* FALLTHRU */
32644 case dw_val_class_loc_list:
32645 loc = AT_loc_list (a);
32646 gcc_assert (loc);
32647 for (; loc; loc = loc->dw_loc_next)
32648 resolve_variable_value_in_expr (a, loc->expr);
32649 break;
32650 default:
32651 break;
32655 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32656 temporaries in the current function. */
32658 static void
32659 resolve_variable_values (void)
32661 if (!variable_value_hash || !current_function_decl)
32662 return;
32664 struct variable_value_struct *node
32665 = variable_value_hash->find_with_hash (current_function_decl,
32666 DECL_UID (current_function_decl));
32668 if (node == NULL)
32669 return;
32671 unsigned int i;
32672 dw_die_ref die;
32673 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32674 resolve_variable_value (die);
32677 /* Helper function for note_variable_value, handle one location
32678 expression. */
32680 static void
32681 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32683 for (; loc; loc = loc->dw_loc_next)
32684 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32685 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32687 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32688 dw_die_ref ref = lookup_decl_die (decl);
32689 if (! ref && (flag_generate_lto || flag_generate_offload))
32691 /* ??? This is somewhat a hack because we do not create DIEs
32692 for variables not in BLOCK trees early but when generating
32693 early LTO output we need the dw_val_class_decl_ref to be
32694 fully resolved. For fat LTO objects we'd also like to
32695 undo this after LTO dwarf output. */
32696 gcc_assert (DECL_CONTEXT (decl));
32697 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32698 gcc_assert (ctx != NULL);
32699 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32700 ref = lookup_decl_die (decl);
32701 gcc_assert (ref != NULL);
32703 if (ref)
32705 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32706 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32707 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32708 continue;
32710 if (VAR_P (decl)
32711 && DECL_CONTEXT (decl)
32712 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32713 && lookup_decl_die (DECL_CONTEXT (decl)))
32715 if (!variable_value_hash)
32716 variable_value_hash
32717 = hash_table<variable_value_hasher>::create_ggc (10);
32719 tree fndecl = DECL_CONTEXT (decl);
32720 struct variable_value_struct *node;
32721 struct variable_value_struct **slot
32722 = variable_value_hash->find_slot_with_hash (fndecl,
32723 DECL_UID (fndecl),
32724 INSERT);
32725 if (*slot == NULL)
32727 node = ggc_cleared_alloc<variable_value_struct> ();
32728 node->decl_id = DECL_UID (fndecl);
32729 *slot = node;
32731 else
32732 node = *slot;
32734 vec_safe_push (node->dies, die);
32739 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32740 with dw_val_class_decl_ref operand. */
32742 static void
32743 note_variable_value (dw_die_ref die)
32745 dw_die_ref c;
32746 dw_attr_node *a;
32747 dw_loc_list_ref loc;
32748 unsigned ix;
32750 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32751 switch (AT_class (a))
32753 case dw_val_class_loc_list:
32754 loc = AT_loc_list (a);
32755 gcc_assert (loc);
32756 if (!loc->noted_variable_value)
32758 loc->noted_variable_value = 1;
32759 for (; loc; loc = loc->dw_loc_next)
32760 note_variable_value_in_expr (die, loc->expr);
32762 break;
32763 case dw_val_class_loc:
32764 note_variable_value_in_expr (die, AT_loc (a));
32765 break;
32766 default:
32767 break;
32770 /* Mark children. */
32771 FOR_EACH_CHILD (die, c, note_variable_value (c));
32774 /* Process DWARF dies for CTF generation. */
32776 static void
32777 ctf_debug_do_cu (dw_die_ref die)
32779 dw_die_ref c;
32781 if (!ctf_do_die (die))
32782 return;
32784 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32787 /* Perform any cleanups needed after the early debug generation pass
32788 has run. */
32790 static void
32791 dwarf2out_early_finish (const char *filename)
32793 comdat_type_node *ctnode;
32794 set_early_dwarf s;
32795 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32797 /* PCH might result in DW_AT_producer string being restored from the
32798 header compilation, so always fill it with empty string initially
32799 and overwrite only here. */
32800 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32802 if (dwarf_record_gcc_switches)
32803 producer_string = gen_producer_string (lang_hooks.name,
32804 save_decoded_options,
32805 save_decoded_options_count);
32806 else
32807 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32809 producer->dw_attr_val.v.val_str->refcount--;
32810 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32812 /* Add the name for the main input file now. We delayed this from
32813 dwarf2out_init to avoid complications with PCH. */
32814 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32815 add_comp_dir_attribute (comp_unit_die ());
32817 /* With LTO early dwarf was really finished at compile-time, so make
32818 sure to adjust the phase after annotating the LTRANS CU DIE. */
32819 if (in_lto_p)
32821 early_dwarf_finished = true;
32822 if (dump_file)
32824 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32825 print_die (comp_unit_die (), dump_file);
32827 return;
32830 /* Walk through the list of incomplete types again, trying once more to
32831 emit full debugging info for them. */
32832 retry_incomplete_types ();
32834 gen_scheduled_generic_parms_dies ();
32835 gen_remaining_tmpl_value_param_die_attribute ();
32837 /* The point here is to flush out the limbo list so that it is empty
32838 and we don't need to stream it for LTO. */
32839 flush_limbo_die_list ();
32841 /* Add DW_AT_linkage_name for all deferred DIEs. */
32842 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32844 tree decl = node->created_for;
32845 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32846 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32847 ended up in deferred_asm_name before we knew it was
32848 constant and never written to disk. */
32849 && DECL_ASSEMBLER_NAME (decl))
32851 add_linkage_attr (node->die, decl);
32852 move_linkage_attr (node->die);
32855 deferred_asm_name = NULL;
32857 if (flag_eliminate_unused_debug_types)
32858 prune_unused_types ();
32860 /* Generate separate COMDAT sections for type DIEs. */
32861 if (use_debug_types)
32863 break_out_comdat_types (comp_unit_die ());
32865 /* Each new type_unit DIE was added to the limbo die list when created.
32866 Since these have all been added to comdat_type_list, clear the
32867 limbo die list. */
32868 limbo_die_list = NULL;
32870 /* For each new comdat type unit, copy declarations for incomplete
32871 types to make the new unit self-contained (i.e., no direct
32872 references to the main compile unit). */
32873 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32874 copy_decls_for_unworthy_types (ctnode->root_die);
32875 copy_decls_for_unworthy_types (comp_unit_die ());
32877 /* In the process of copying declarations from one unit to another,
32878 we may have left some declarations behind that are no longer
32879 referenced. Prune them. */
32880 prune_unused_types ();
32883 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32884 with dw_val_class_decl_ref operand. */
32885 note_variable_value (comp_unit_die ());
32886 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32887 note_variable_value (node->die);
32888 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32889 note_variable_value (ctnode->root_die);
32890 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32891 note_variable_value (node->die);
32893 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32894 both the main_cu and all skeleton TUs. Making this call unconditional
32895 would end up either adding a second copy of the AT_pubnames attribute, or
32896 requiring a special case in add_top_level_skeleton_die_attrs. */
32897 if (!dwarf_split_debug_info)
32898 add_AT_pubnames (comp_unit_die ());
32900 /* The early debug phase is now finished. */
32901 early_dwarf_finished = true;
32902 if (dump_file)
32904 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32905 print_die (comp_unit_die (), dump_file);
32908 /* Generate CTF/BTF debug info. */
32909 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32910 || btf_debuginfo_p ()) && lang_GNU_C ())
32912 ctf_debug_init ();
32913 ctf_debug_do_cu (comp_unit_die ());
32914 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32915 ctf_debug_do_cu (node->die);
32916 /* Post process the debug data in the CTF container if necessary. */
32917 ctf_debug_init_postprocess (btf_debuginfo_p ());
32919 ctf_debug_early_finish (filename);
32922 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32923 if ((!flag_generate_lto && !flag_generate_offload)
32924 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32925 copy_lto_debug_sections operation of the simple object support in
32926 libiberty is not implemented for them yet. */
32927 || TARGET_PECOFF || TARGET_COFF)
32928 return;
32930 /* Now as we are going to output for LTO initialize sections and labels
32931 to the LTO variants. We don't need a random-seed postfix as other
32932 LTO sections as linking the LTO debug sections into one in a partial
32933 link is fine. */
32934 init_sections_and_labels (true);
32936 /* The output below is modeled after dwarf2out_finish with all
32937 location related output removed and some LTO specific changes.
32938 Some refactoring might make both smaller and easier to match up. */
32940 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32941 mark_base_types (ctnode->root_die);
32942 mark_base_types (comp_unit_die ());
32943 move_marked_base_types ();
32945 /* Traverse the DIE's and add sibling attributes to those DIE's
32946 that have children. */
32947 add_sibling_attributes (comp_unit_die ());
32948 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32949 add_sibling_attributes (node->die);
32950 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32951 add_sibling_attributes (ctnode->root_die);
32953 /* AIX Assembler inserts the length, so adjust the reference to match the
32954 offset expected by debuggers. */
32955 strcpy (dl_section_ref, debug_line_section_label);
32956 if (XCOFF_DEBUGGING_INFO)
32957 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32959 if (debug_info_level >= DINFO_LEVEL_TERSE)
32960 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32962 if (have_macinfo)
32963 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32964 macinfo_section_label);
32966 save_macinfo_strings ();
32968 if (dwarf_split_debug_info)
32970 unsigned int index = 0;
32971 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32974 /* Output all of the compilation units. We put the main one last so that
32975 the offsets are available to output_pubnames. */
32976 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32977 output_comp_unit (node->die, 0, NULL);
32979 hash_table<comdat_type_hasher> comdat_type_table (100);
32980 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32982 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32984 /* Don't output duplicate types. */
32985 if (*slot != HTAB_EMPTY_ENTRY)
32986 continue;
32988 /* Add a pointer to the line table for the main compilation unit
32989 so that the debugger can make sense of DW_AT_decl_file
32990 attributes. */
32991 if (debug_info_level >= DINFO_LEVEL_TERSE)
32992 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32993 (!dwarf_split_debug_info
32994 ? debug_line_section_label
32995 : debug_skeleton_line_section_label));
32997 output_comdat_type_unit (ctnode, true);
32998 *slot = ctnode;
33001 /* Stick a unique symbol to the main debuginfo section. */
33002 compute_comp_unit_symbol (comp_unit_die ());
33004 /* Output the main compilation unit. We always need it if only for
33005 the CU symbol. */
33006 output_comp_unit (comp_unit_die (), true, NULL);
33008 /* Output the abbreviation table. */
33009 if (vec_safe_length (abbrev_die_table) != 1)
33011 switch_to_section (debug_abbrev_section);
33012 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33013 output_abbrev_section ();
33016 /* Have to end the macro section. */
33017 if (have_macinfo)
33019 /* We have to save macinfo state if we need to output it again
33020 for the FAT part of the object. */
33021 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33022 if (flag_fat_lto_objects)
33023 macinfo_table = macinfo_table->copy ();
33025 switch_to_section (debug_macinfo_section);
33026 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33027 output_macinfo (debug_line_section_label, true);
33028 dw2_asm_output_data (1, 0, "End compilation unit");
33030 if (flag_fat_lto_objects)
33032 vec_free (macinfo_table);
33033 macinfo_table = saved_macinfo_table;
33037 /* Emit a skeleton debug_line section. */
33038 switch_to_section (debug_line_section);
33039 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33040 output_line_info (true);
33042 /* If we emitted any indirect strings, output the string table too. */
33043 if (debug_str_hash || skeleton_debug_str_hash)
33044 output_indirect_strings ();
33045 if (debug_line_str_hash)
33047 switch_to_section (debug_line_str_section);
33048 const enum dwarf_form form = DW_FORM_line_strp;
33049 debug_line_str_hash->traverse<enum dwarf_form,
33050 output_indirect_string> (form);
33053 /* Switch back to the text section. */
33054 switch_to_section (text_section);
33057 /* Reset all state within dwarf2out.c so that we can rerun the compiler
33058 within the same process. For use by toplev::finalize. */
33060 void
33061 dwarf2out_c_finalize (void)
33063 last_var_location_insn = NULL;
33064 cached_next_real_insn = NULL;
33065 used_rtx_array = NULL;
33066 incomplete_types = NULL;
33067 debug_info_section = NULL;
33068 debug_skeleton_info_section = NULL;
33069 debug_abbrev_section = NULL;
33070 debug_skeleton_abbrev_section = NULL;
33071 debug_aranges_section = NULL;
33072 debug_addr_section = NULL;
33073 debug_macinfo_section = NULL;
33074 debug_line_section = NULL;
33075 debug_skeleton_line_section = NULL;
33076 debug_loc_section = NULL;
33077 debug_pubnames_section = NULL;
33078 debug_pubtypes_section = NULL;
33079 debug_str_section = NULL;
33080 debug_line_str_section = NULL;
33081 debug_str_dwo_section = NULL;
33082 debug_str_offsets_section = NULL;
33083 debug_ranges_section = NULL;
33084 debug_ranges_dwo_section = NULL;
33085 debug_frame_section = NULL;
33086 fde_vec = NULL;
33087 debug_str_hash = NULL;
33088 debug_line_str_hash = NULL;
33089 skeleton_debug_str_hash = NULL;
33090 dw2_string_counter = 0;
33091 have_multiple_function_sections = false;
33092 in_text_section_p = false;
33093 cold_text_section = NULL;
33094 last_text_label = NULL;
33095 last_cold_label = NULL;
33096 switch_text_ranges = NULL;
33097 switch_cold_ranges = NULL;
33098 current_unit_personality = NULL;
33100 early_dwarf = false;
33101 early_dwarf_finished = false;
33103 next_die_offset = 0;
33104 single_comp_unit_die = NULL;
33105 comdat_type_list = NULL;
33106 limbo_die_list = NULL;
33107 file_table = NULL;
33108 decl_die_table = NULL;
33109 common_block_die_table = NULL;
33110 decl_loc_table = NULL;
33111 call_arg_locations = NULL;
33112 call_arg_loc_last = NULL;
33113 call_site_count = -1;
33114 tail_call_site_count = -1;
33115 cached_dw_loc_list_table = NULL;
33116 abbrev_die_table = NULL;
33117 delete dwarf_proc_stack_usage_map;
33118 dwarf_proc_stack_usage_map = NULL;
33119 line_info_label_num = 0;
33120 cur_line_info_table = NULL;
33121 text_section_line_info = NULL;
33122 cold_text_section_line_info = NULL;
33123 separate_line_info = NULL;
33124 info_section_emitted = false;
33125 pubname_table = NULL;
33126 pubtype_table = NULL;
33127 macinfo_table = NULL;
33128 ranges_table = NULL;
33129 ranges_by_label = NULL;
33130 rnglist_idx = 0;
33131 have_location_lists = false;
33132 loclabel_num = 0;
33133 poc_label_num = 0;
33134 last_emitted_file = NULL;
33135 label_num = 0;
33136 tmpl_value_parm_die_table = NULL;
33137 generic_type_instances = NULL;
33138 frame_pointer_fb_offset = 0;
33139 frame_pointer_fb_offset_valid = false;
33140 base_types.release ();
33141 XDELETEVEC (producer_string);
33142 producer_string = NULL;
33143 output_line_info_generation = 0;
33144 init_sections_and_labels_generation = 0;
33147 #include "gt-dwarf2out.h"