Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / dwarf2out.c
blobba0a6d6ed60206804336343f759d1537d23f2c35
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 /* This function builds a dwarf location descriptor sequence from a
2784 dw_cfa_location, adding the given OFFSET to the result of the
2785 expression. */
2787 struct dw_loc_descr_node *
2788 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2790 struct dw_loc_descr_node *head, *tmp;
2792 offset += cfa->offset;
2794 if (cfa->indirect)
2796 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2797 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2798 head->dw_loc_oprnd1.val_entry = NULL;
2799 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2800 add_loc_descr (&head, tmp);
2801 loc_descr_plus_const (&head, offset);
2803 else
2804 head = new_reg_loc_descr (cfa->reg, offset);
2806 return head;
2809 /* This function builds a dwarf location descriptor sequence for
2810 the address at OFFSET from the CFA when stack is aligned to
2811 ALIGNMENT byte. */
2813 struct dw_loc_descr_node *
2814 build_cfa_aligned_loc (dw_cfa_location *cfa,
2815 poly_int64 offset, HOST_WIDE_INT alignment)
2817 struct dw_loc_descr_node *head;
2818 unsigned int dwarf_fp
2819 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2821 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2822 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2824 head = new_reg_loc_descr (dwarf_fp, 0);
2825 add_loc_descr (&head, int_loc_descriptor (alignment));
2826 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2827 loc_descr_plus_const (&head, offset);
2829 else
2830 head = new_reg_loc_descr (dwarf_fp, offset);
2831 return head;
2834 /* And now, the support for symbolic debugging information. */
2836 /* .debug_str support. */
2838 static void dwarf2out_init (const char *);
2839 static void dwarf2out_finish (const char *);
2840 static void dwarf2out_early_finish (const char *);
2841 static void dwarf2out_assembly_start (void);
2842 static void dwarf2out_define (unsigned int, const char *);
2843 static void dwarf2out_undef (unsigned int, const char *);
2844 static void dwarf2out_start_source_file (unsigned, const char *);
2845 static void dwarf2out_end_source_file (unsigned);
2846 static void dwarf2out_function_decl (tree);
2847 static void dwarf2out_begin_block (unsigned, unsigned);
2848 static void dwarf2out_end_block (unsigned, unsigned);
2849 static bool dwarf2out_ignore_block (const_tree);
2850 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2851 static void dwarf2out_early_global_decl (tree);
2852 static void dwarf2out_late_global_decl (tree);
2853 static void dwarf2out_type_decl (tree, int);
2854 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2855 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2856 dw_die_ref);
2857 static void dwarf2out_abstract_function (tree);
2858 static void dwarf2out_var_location (rtx_insn *);
2859 static void dwarf2out_inline_entry (tree);
2860 static void dwarf2out_size_function (tree);
2861 static void dwarf2out_begin_function (tree);
2862 static void dwarf2out_end_function (unsigned int);
2863 static void dwarf2out_register_main_translation_unit (tree unit);
2864 static void dwarf2out_set_name (tree, tree);
2865 static void dwarf2out_register_external_die (tree decl, const char *sym,
2866 unsigned HOST_WIDE_INT off);
2867 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2868 unsigned HOST_WIDE_INT *off);
2870 /* The debug hooks structure. */
2872 const struct gcc_debug_hooks dwarf2_debug_hooks =
2874 dwarf2out_init,
2875 dwarf2out_finish,
2876 dwarf2out_early_finish,
2877 dwarf2out_assembly_start,
2878 dwarf2out_define,
2879 dwarf2out_undef,
2880 dwarf2out_start_source_file,
2881 dwarf2out_end_source_file,
2882 dwarf2out_begin_block,
2883 dwarf2out_end_block,
2884 dwarf2out_ignore_block,
2885 dwarf2out_source_line,
2886 dwarf2out_set_ignored_loc,
2887 dwarf2out_begin_prologue,
2888 #if VMS_DEBUGGING_INFO
2889 dwarf2out_vms_end_prologue,
2890 dwarf2out_vms_begin_epilogue,
2891 #else
2892 debug_nothing_int_charstar,
2893 debug_nothing_int_charstar,
2894 #endif
2895 dwarf2out_end_epilogue,
2896 dwarf2out_begin_function,
2897 dwarf2out_end_function, /* end_function */
2898 dwarf2out_register_main_translation_unit,
2899 dwarf2out_function_decl, /* function_decl */
2900 dwarf2out_early_global_decl,
2901 dwarf2out_late_global_decl,
2902 dwarf2out_type_decl, /* type_decl */
2903 dwarf2out_imported_module_or_decl,
2904 dwarf2out_die_ref_for_decl,
2905 dwarf2out_register_external_die,
2906 debug_nothing_tree, /* deferred_inline_function */
2907 /* The DWARF 2 backend tries to reduce debugging bloat by not
2908 emitting the abstract description of inline functions until
2909 something tries to reference them. */
2910 dwarf2out_abstract_function, /* outlining_inline_function */
2911 debug_nothing_rtx_code_label, /* label */
2912 debug_nothing_int, /* handle_pch */
2913 dwarf2out_var_location,
2914 dwarf2out_inline_entry, /* inline_entry */
2915 dwarf2out_size_function, /* size_function */
2916 dwarf2out_switch_text_section,
2917 dwarf2out_set_name,
2918 1, /* start_end_main_source_file */
2919 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2922 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2924 dwarf2out_init,
2925 debug_nothing_charstar,
2926 debug_nothing_charstar,
2927 dwarf2out_assembly_start,
2928 debug_nothing_int_charstar,
2929 debug_nothing_int_charstar,
2930 debug_nothing_int_charstar,
2931 debug_nothing_int,
2932 debug_nothing_int_int, /* begin_block */
2933 debug_nothing_int_int, /* end_block */
2934 debug_true_const_tree, /* ignore_block */
2935 dwarf2out_source_line, /* source_line */
2936 debug_nothing_int_int_charstar, /* set_ignored_loc */
2937 debug_nothing_int_int_charstar, /* begin_prologue */
2938 debug_nothing_int_charstar, /* end_prologue */
2939 debug_nothing_int_charstar, /* begin_epilogue */
2940 debug_nothing_int_charstar, /* end_epilogue */
2941 debug_nothing_tree, /* begin_function */
2942 debug_nothing_int, /* end_function */
2943 debug_nothing_tree, /* register_main_translation_unit */
2944 debug_nothing_tree, /* function_decl */
2945 debug_nothing_tree, /* early_global_decl */
2946 debug_nothing_tree, /* late_global_decl */
2947 debug_nothing_tree_int, /* type_decl */
2948 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2949 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2950 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2951 debug_nothing_tree, /* deferred_inline_function */
2952 debug_nothing_tree, /* outlining_inline_function */
2953 debug_nothing_rtx_code_label, /* label */
2954 debug_nothing_int, /* handle_pch */
2955 debug_nothing_rtx_insn, /* var_location */
2956 debug_nothing_tree, /* inline_entry */
2957 debug_nothing_tree, /* size_function */
2958 debug_nothing_void, /* switch_text_section */
2959 debug_nothing_tree_tree, /* set_name */
2960 0, /* start_end_main_source_file */
2961 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2964 /* NOTE: In the comments in this file, many references are made to
2965 "Debugging Information Entries". This term is abbreviated as `DIE'
2966 throughout the remainder of this file. */
2968 /* An internal representation of the DWARF output is built, and then
2969 walked to generate the DWARF debugging info. The walk of the internal
2970 representation is done after the entire program has been compiled.
2971 The types below are used to describe the internal representation. */
2973 /* Whether to put type DIEs into their own section .debug_types instead
2974 of making them part of the .debug_info section. Only supported for
2975 Dwarf V4 or higher and the user didn't disable them through
2976 -fno-debug-types-section. It is more efficient to put them in a
2977 separate comdat sections since the linker will then be able to
2978 remove duplicates. But not all tools support .debug_types sections
2979 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2980 it is DW_UT_type unit type in .debug_info section. For late LTO
2981 debug there should be almost no types emitted so avoid enabling
2982 -fdebug-types-section there. */
2984 #define use_debug_types (dwarf_version >= 4 \
2985 && flag_debug_types_section \
2986 && !in_lto_p)
2988 /* Various DIE's use offsets relative to the beginning of the
2989 .debug_info section to refer to each other. */
2991 typedef long int dw_offset;
2993 struct comdat_type_node;
2995 /* The entries in the line_info table more-or-less mirror the opcodes
2996 that are used in the real dwarf line table. Arrays of these entries
2997 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2998 supported. */
3000 enum dw_line_info_opcode {
3001 /* Emit DW_LNE_set_address; the operand is the label index. */
3002 LI_set_address,
3004 /* Emit a row to the matrix with the given line. This may be done
3005 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3006 special opcodes. */
3007 LI_set_line,
3009 /* Emit a DW_LNS_set_file. */
3010 LI_set_file,
3012 /* Emit a DW_LNS_set_column. */
3013 LI_set_column,
3015 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3016 LI_negate_stmt,
3018 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3019 LI_set_prologue_end,
3020 LI_set_epilogue_begin,
3022 /* Emit a DW_LNE_set_discriminator. */
3023 LI_set_discriminator,
3025 /* Output a Fixed Advance PC; the target PC is the label index; the
3026 base PC is the previous LI_adv_address or LI_set_address entry.
3027 We only use this when emitting debug views without assembler
3028 support, at explicit user request. Ideally, we should only use
3029 it when the offset might be zero but we can't tell: it's the only
3030 way to maybe change the PC without resetting the view number. */
3031 LI_adv_address
3034 typedef struct GTY(()) dw_line_info_struct {
3035 enum dw_line_info_opcode opcode;
3036 unsigned int val;
3037 } dw_line_info_entry;
3040 struct GTY(()) dw_line_info_table {
3041 /* The label that marks the end of this section. */
3042 const char *end_label;
3044 /* The values for the last row of the matrix, as collected in the table.
3045 These are used to minimize the changes to the next row. */
3046 unsigned int file_num;
3047 unsigned int line_num;
3048 unsigned int column_num;
3049 int discrim_num;
3050 bool is_stmt;
3051 bool in_use;
3053 /* This denotes the NEXT view number.
3055 If it is 0, it is known that the NEXT view will be the first view
3056 at the given PC.
3058 If it is -1, we're forcing the view number to be reset, e.g. at a
3059 function entry.
3061 The meaning of other nonzero values depends on whether we're
3062 computing views internally or leaving it for the assembler to do
3063 so. If we're emitting them internally, view denotes the view
3064 number since the last known advance of PC. If we're leaving it
3065 for the assembler, it denotes the LVU label number that we're
3066 going to ask the assembler to assign. */
3067 var_loc_view view;
3069 /* This counts the number of symbolic views emitted in this table
3070 since the latest view reset. Its max value, over all tables,
3071 sets symview_upper_bound. */
3072 var_loc_view symviews_since_reset;
3074 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3075 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3076 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3077 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3079 vec<dw_line_info_entry, va_gc> *entries;
3082 /* This is an upper bound for view numbers that the assembler may
3083 assign to symbolic views output in this translation. It is used to
3084 decide how big a field to use to represent view numbers in
3085 symview-classed attributes. */
3087 static var_loc_view symview_upper_bound;
3089 /* If we're keep track of location views and their reset points, and
3090 INSN is a reset point (i.e., it necessarily advances the PC), mark
3091 the next view in TABLE as reset. */
3093 static void
3094 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3096 if (!debug_internal_reset_location_views)
3097 return;
3099 /* Maybe turn (part of?) this test into a default target hook. */
3100 int reset = 0;
3102 if (targetm.reset_location_view)
3103 reset = targetm.reset_location_view (insn);
3105 if (reset)
3107 else if (JUMP_TABLE_DATA_P (insn))
3108 reset = 1;
3109 else if (GET_CODE (insn) == USE
3110 || GET_CODE (insn) == CLOBBER
3111 || GET_CODE (insn) == ASM_INPUT
3112 || asm_noperands (insn) >= 0)
3114 else if (get_attr_min_length (insn) > 0)
3115 reset = 1;
3117 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3118 RESET_NEXT_VIEW (table->view);
3121 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3122 The children of each node form a circular list linked by
3123 die_sib. die_child points to the node *before* the "first" child node. */
3125 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3126 union die_symbol_or_type_node
3128 const char * GTY ((tag ("0"))) die_symbol;
3129 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3131 GTY ((desc ("%0.comdat_type_p"))) die_id;
3132 vec<dw_attr_node, va_gc> *die_attr;
3133 dw_die_ref die_parent;
3134 dw_die_ref die_child;
3135 dw_die_ref die_sib;
3136 dw_die_ref die_definition; /* ref from a specification to its definition */
3137 dw_offset die_offset;
3138 unsigned long die_abbrev;
3139 int die_mark;
3140 unsigned int decl_id;
3141 enum dwarf_tag die_tag;
3142 /* Die is used and must not be pruned as unused. */
3143 BOOL_BITFIELD die_perennial_p : 1;
3144 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3145 /* For an external ref to die_symbol if die_offset contains an extra
3146 offset to that symbol. */
3147 BOOL_BITFIELD with_offset : 1;
3148 /* Whether this DIE was removed from the DIE tree, for example via
3149 prune_unused_types. We don't consider those present from the
3150 DIE lookup routines. */
3151 BOOL_BITFIELD removed : 1;
3152 /* Lots of spare bits. */
3154 die_node;
3156 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3157 static bool early_dwarf;
3158 static bool early_dwarf_finished;
3159 class set_early_dwarf {
3160 public:
3161 bool saved;
3162 set_early_dwarf () : saved(early_dwarf)
3164 gcc_assert (! early_dwarf_finished);
3165 early_dwarf = true;
3167 ~set_early_dwarf () { early_dwarf = saved; }
3170 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3171 #define FOR_EACH_CHILD(die, c, expr) do { \
3172 c = die->die_child; \
3173 if (c) do { \
3174 c = c->die_sib; \
3175 expr; \
3176 } while (c != die->die_child); \
3177 } while (0)
3179 /* The pubname structure */
3181 typedef struct GTY(()) pubname_struct {
3182 dw_die_ref die;
3183 const char *name;
3185 pubname_entry;
3188 struct GTY(()) dw_ranges {
3189 const char *label;
3190 /* If this is positive, it's a block number, otherwise it's a
3191 bitwise-negated index into dw_ranges_by_label. */
3192 int num;
3193 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3194 into .debug_rnglists section rather than .debug_rnglists.dwo
3195 for -gsplit-dwarf and DWARF >= 5. */
3196 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3197 /* Index for the range list for DW_FORM_rnglistx. */
3198 unsigned int idx : 31;
3199 /* True if this range might be possibly in a different section
3200 from previous entry. */
3201 unsigned int maybe_new_sec : 1;
3202 addr_table_entry *begin_entry;
3203 addr_table_entry *end_entry;
3206 /* A structure to hold a macinfo entry. */
3208 typedef struct GTY(()) macinfo_struct {
3209 unsigned char code;
3210 unsigned HOST_WIDE_INT lineno;
3211 const char *info;
3213 macinfo_entry;
3216 struct GTY(()) dw_ranges_by_label {
3217 const char *begin;
3218 const char *end;
3221 /* The comdat type node structure. */
3222 struct GTY(()) comdat_type_node
3224 dw_die_ref root_die;
3225 dw_die_ref type_die;
3226 dw_die_ref skeleton_die;
3227 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3228 comdat_type_node *next;
3231 /* A list of DIEs for which we can't determine ancestry (parent_die
3232 field) just yet. Later in dwarf2out_finish we will fill in the
3233 missing bits. */
3234 typedef struct GTY(()) limbo_die_struct {
3235 dw_die_ref die;
3236 /* The tree for which this DIE was created. We use this to
3237 determine ancestry later. */
3238 tree created_for;
3239 struct limbo_die_struct *next;
3241 limbo_die_node;
3243 typedef struct skeleton_chain_struct
3245 dw_die_ref old_die;
3246 dw_die_ref new_die;
3247 struct skeleton_chain_struct *parent;
3249 skeleton_chain_node;
3251 /* Define a macro which returns nonzero for a TYPE_DECL which was
3252 implicitly generated for a type.
3254 Note that, unlike the C front-end (which generates a NULL named
3255 TYPE_DECL node for each complete tagged type, each array type,
3256 and each function type node created) the C++ front-end generates
3257 a _named_ TYPE_DECL node for each tagged type node created.
3258 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3259 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3260 front-end, but for each type, tagged or not. */
3262 #define TYPE_DECL_IS_STUB(decl) \
3263 (DECL_NAME (decl) == NULL_TREE \
3264 || (DECL_ARTIFICIAL (decl) \
3265 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3266 /* This is necessary for stub decls that \
3267 appear in nested inline functions. */ \
3268 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3269 && (decl_ultimate_origin (decl) \
3270 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3272 /* Information concerning the compilation unit's programming
3273 language, and compiler version. */
3275 /* Fixed size portion of the DWARF compilation unit header. */
3276 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3277 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3278 + (dwarf_version >= 5 ? 4 : 3))
3280 /* Fixed size portion of the DWARF comdat type unit header. */
3281 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3282 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3283 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3285 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3286 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3287 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3289 /* Fixed size portion of public names info. */
3290 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3292 /* Fixed size portion of the address range info. */
3293 #define DWARF_ARANGES_HEADER_SIZE \
3294 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3295 DWARF2_ADDR_SIZE * 2) \
3296 - DWARF_INITIAL_LENGTH_SIZE)
3298 /* Size of padding portion in the address range info. It must be
3299 aligned to twice the pointer size. */
3300 #define DWARF_ARANGES_PAD_SIZE \
3301 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3302 DWARF2_ADDR_SIZE * 2) \
3303 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3305 /* Use assembler line directives if available. */
3306 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3307 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3308 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3309 #else
3310 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3311 #endif
3312 #endif
3314 /* Use assembler views in line directives if available. */
3315 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3316 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3317 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3318 #else
3319 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3320 #endif
3321 #endif
3323 /* Return true if GCC configure detected assembler support for .loc. */
3325 bool
3326 dwarf2out_default_as_loc_support (void)
3328 return DWARF2_ASM_LINE_DEBUG_INFO;
3329 #if (GCC_VERSION >= 3000)
3330 # undef DWARF2_ASM_LINE_DEBUG_INFO
3331 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3332 #endif
3335 /* Return true if GCC configure detected assembler support for views
3336 in .loc directives. */
3338 bool
3339 dwarf2out_default_as_locview_support (void)
3341 return DWARF2_ASM_VIEW_DEBUG_INFO;
3342 #if (GCC_VERSION >= 3000)
3343 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3344 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3345 #endif
3348 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3349 view computation, and it refers to a view identifier for which we
3350 will not emit a label because it is known to map to a view number
3351 zero. We won't allocate the bitmap if we're not using assembler
3352 support for location views, but we have to make the variable
3353 visible for GGC and for code that will be optimized out for lack of
3354 support but that's still parsed and compiled. We could abstract it
3355 out with macros, but it's not worth it. */
3356 static GTY(()) bitmap zero_view_p;
3358 /* Evaluate to TRUE iff N is known to identify the first location view
3359 at its PC. When not using assembler location view computation,
3360 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3361 and views label numbers recorded in it are the ones known to be
3362 zero. */
3363 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3364 || (N) == (var_loc_view)-1 \
3365 || (zero_view_p \
3366 && bitmap_bit_p (zero_view_p, (N))))
3368 /* Return true iff we're to emit .loc directives for the assembler to
3369 generate line number sections.
3371 When we're not emitting views, all we need from the assembler is
3372 support for .loc directives.
3374 If we are emitting views, we can only use the assembler's .loc
3375 support if it also supports views.
3377 When the compiler is emitting the line number programs and
3378 computing view numbers itself, it resets view numbers at known PC
3379 changes and counts from that, and then it emits view numbers as
3380 literal constants in locviewlists. There are cases in which the
3381 compiler is not sure about PC changes, e.g. when extra alignment is
3382 requested for a label. In these cases, the compiler may not reset
3383 the view counter, and the potential PC advance in the line number
3384 program will use an opcode that does not reset the view counter
3385 even if the PC actually changes, so that compiler and debug info
3386 consumer can keep view numbers in sync.
3388 When the compiler defers view computation to the assembler, it
3389 emits symbolic view numbers in locviewlists, with the exception of
3390 views known to be zero (forced resets, or reset after
3391 compiler-visible PC changes): instead of emitting symbols for
3392 these, we emit literal zero and assert the assembler agrees with
3393 the compiler's assessment. We could use symbolic views everywhere,
3394 instead of special-casing zero views, but then we'd be unable to
3395 optimize out locviewlists that contain only zeros. */
3397 static bool
3398 output_asm_line_debug_info (void)
3400 return (dwarf2out_as_loc_support
3401 && (dwarf2out_as_locview_support
3402 || !debug_variable_location_views));
3405 static bool asm_outputs_debug_line_str (void);
3407 /* Minimum line offset in a special line info. opcode.
3408 This value was chosen to give a reasonable range of values. */
3409 #define DWARF_LINE_BASE -10
3411 /* First special line opcode - leave room for the standard opcodes. */
3412 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3414 /* Range of line offsets in a special line info. opcode. */
3415 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3417 /* Flag that indicates the initial value of the is_stmt_start flag.
3418 In the present implementation, we do not mark any lines as
3419 the beginning of a source statement, because that information
3420 is not made available by the GCC front-end. */
3421 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3423 /* Maximum number of operations per instruction bundle. */
3424 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3425 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3426 #endif
3428 /* This location is used by calc_die_sizes() to keep track
3429 the offset of each DIE within the .debug_info section. */
3430 static unsigned long next_die_offset;
3432 /* Record the root of the DIE's built for the current compilation unit. */
3433 static GTY(()) dw_die_ref single_comp_unit_die;
3435 /* A list of type DIEs that have been separated into comdat sections. */
3436 static GTY(()) comdat_type_node *comdat_type_list;
3438 /* A list of CU DIEs that have been separated. */
3439 static GTY(()) limbo_die_node *cu_die_list;
3441 /* A list of DIEs with a NULL parent waiting to be relocated. */
3442 static GTY(()) limbo_die_node *limbo_die_list;
3444 /* A list of DIEs for which we may have to generate
3445 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3446 static GTY(()) limbo_die_node *deferred_asm_name;
3448 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3450 typedef const char *compare_type;
3452 static hashval_t hash (dwarf_file_data *);
3453 static bool equal (dwarf_file_data *, const char *);
3456 /* Filenames referenced by this compilation unit. */
3457 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3459 struct decl_die_hasher : ggc_ptr_hash<die_node>
3461 typedef tree compare_type;
3463 static hashval_t hash (die_node *);
3464 static bool equal (die_node *, tree);
3466 /* A hash table of references to DIE's that describe declarations.
3467 The key is a DECL_UID() which is a unique number identifying each decl. */
3468 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3470 struct GTY ((for_user)) variable_value_struct {
3471 unsigned int decl_id;
3472 vec<dw_die_ref, va_gc> *dies;
3475 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3477 typedef tree compare_type;
3479 static hashval_t hash (variable_value_struct *);
3480 static bool equal (variable_value_struct *, tree);
3482 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3483 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3484 DECL_CONTEXT of the referenced VAR_DECLs. */
3485 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3487 struct block_die_hasher : ggc_ptr_hash<die_struct>
3489 static hashval_t hash (die_struct *);
3490 static bool equal (die_struct *, die_struct *);
3493 /* A hash table of references to DIE's that describe COMMON blocks.
3494 The key is DECL_UID() ^ die_parent. */
3495 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3497 typedef struct GTY(()) die_arg_entry_struct {
3498 dw_die_ref die;
3499 tree arg;
3500 } die_arg_entry;
3503 /* Node of the variable location list. */
3504 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3505 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3506 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3507 in mode of the EXPR_LIST node and first EXPR_LIST operand
3508 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3509 location or NULL for padding. For larger bitsizes,
3510 mode is 0 and first operand is a CONCAT with bitsize
3511 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3512 NULL as second operand. */
3513 rtx GTY (()) loc;
3514 const char * GTY (()) label;
3515 struct var_loc_node * GTY (()) next;
3516 var_loc_view view;
3519 /* Variable location list. */
3520 struct GTY ((for_user)) var_loc_list_def {
3521 struct var_loc_node * GTY (()) first;
3523 /* Pointer to the last but one or last element of the
3524 chained list. If the list is empty, both first and
3525 last are NULL, if the list contains just one node
3526 or the last node certainly is not redundant, it points
3527 to the last node, otherwise points to the last but one.
3528 Do not mark it for GC because it is marked through the chain. */
3529 struct var_loc_node * GTY ((skip ("%h"))) last;
3531 /* Pointer to the last element before section switch,
3532 if NULL, either sections weren't switched or first
3533 is after section switch. */
3534 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3536 /* DECL_UID of the variable decl. */
3537 unsigned int decl_id;
3539 typedef struct var_loc_list_def var_loc_list;
3541 /* Call argument location list. */
3542 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3543 rtx GTY (()) call_arg_loc_note;
3544 const char * GTY (()) label;
3545 tree GTY (()) block;
3546 bool tail_call_p;
3547 rtx GTY (()) symbol_ref;
3548 struct call_arg_loc_node * GTY (()) next;
3552 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3554 typedef const_tree compare_type;
3556 static hashval_t hash (var_loc_list *);
3557 static bool equal (var_loc_list *, const_tree);
3560 /* Table of decl location linked lists. */
3561 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3563 /* Head and tail of call_arg_loc chain. */
3564 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3565 static struct call_arg_loc_node *call_arg_loc_last;
3567 /* Number of call sites in the current function. */
3568 static int call_site_count = -1;
3569 /* Number of tail call sites in the current function. */
3570 static int tail_call_site_count = -1;
3572 /* A cached location list. */
3573 struct GTY ((for_user)) cached_dw_loc_list_def {
3574 /* The DECL_UID of the decl that this entry describes. */
3575 unsigned int decl_id;
3577 /* The cached location list. */
3578 dw_loc_list_ref loc_list;
3580 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3582 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3585 typedef const_tree compare_type;
3587 static hashval_t hash (cached_dw_loc_list *);
3588 static bool equal (cached_dw_loc_list *, const_tree);
3591 /* Table of cached location lists. */
3592 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3594 /* A vector of references to DIE's that are uniquely identified by their tag,
3595 presence/absence of children DIE's, and list of attribute/value pairs. */
3596 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3598 /* A hash map to remember the stack usage for DWARF procedures. The value
3599 stored is the stack size difference between before the DWARF procedure
3600 invokation and after it returned. In other words, for a DWARF procedure
3601 that consumes N stack slots and that pushes M ones, this stores M - N. */
3602 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3604 /* A global counter for generating labels for line number data. */
3605 static unsigned int line_info_label_num;
3607 /* The current table to which we should emit line number information
3608 for the current function. This will be set up at the beginning of
3609 assembly for the function. */
3610 static GTY(()) dw_line_info_table *cur_line_info_table;
3612 /* The two default tables of line number info. */
3613 static GTY(()) dw_line_info_table *text_section_line_info;
3614 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3616 /* The set of all non-default tables of line number info. */
3617 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3619 /* A flag to tell pubnames/types export if there is an info section to
3620 refer to. */
3621 static bool info_section_emitted;
3623 /* A pointer to the base of a table that contains a list of publicly
3624 accessible names. */
3625 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3627 /* A pointer to the base of a table that contains a list of publicly
3628 accessible types. */
3629 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3631 /* A pointer to the base of a table that contains a list of macro
3632 defines/undefines (and file start/end markers). */
3633 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3635 /* True if .debug_macinfo or .debug_macros section is going to be
3636 emitted. */
3637 #define have_macinfo \
3638 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3639 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3640 && !macinfo_table->is_empty ())
3642 /* Vector of dies for which we should generate .debug_ranges info. */
3643 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3645 /* Vector of pairs of labels referenced in ranges_table. */
3646 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3648 /* Whether we have location lists that need outputting */
3649 static GTY(()) bool have_location_lists;
3651 /* Unique label counter. */
3652 static GTY(()) unsigned int loclabel_num;
3654 /* Unique label counter for point-of-call tables. */
3655 static GTY(()) unsigned int poc_label_num;
3657 /* The last file entry emitted by maybe_emit_file(). */
3658 static GTY(()) struct dwarf_file_data * last_emitted_file;
3660 /* Number of internal labels generated by gen_internal_sym(). */
3661 static GTY(()) int label_num;
3663 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3665 /* Instances of generic types for which we need to generate debug
3666 info that describe their generic parameters and arguments. That
3667 generation needs to happen once all types are properly laid out so
3668 we do it at the end of compilation. */
3669 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3671 /* Offset from the "steady-state frame pointer" to the frame base,
3672 within the current function. */
3673 static poly_int64 frame_pointer_fb_offset;
3674 static bool frame_pointer_fb_offset_valid;
3676 static vec<dw_die_ref> base_types;
3678 /* Flags to represent a set of attribute classes for attributes that represent
3679 a scalar value (bounds, pointers, ...). */
3680 enum dw_scalar_form
3682 dw_scalar_form_constant = 0x01,
3683 dw_scalar_form_exprloc = 0x02,
3684 dw_scalar_form_reference = 0x04
3687 /* Forward declarations for functions defined in this file. */
3689 static int is_pseudo_reg (const_rtx);
3690 static tree type_main_variant (tree);
3691 static int is_tagged_type (const_tree);
3692 static const char *dwarf_tag_name (unsigned);
3693 static const char *dwarf_attr_name (unsigned);
3694 static const char *dwarf_form_name (unsigned);
3695 static tree decl_ultimate_origin (const_tree);
3696 static tree decl_class_context (tree);
3697 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3698 static inline unsigned int AT_index (dw_attr_node *);
3699 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3700 static inline unsigned AT_flag (dw_attr_node *);
3701 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3702 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3703 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3704 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3705 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3706 unsigned int, unsigned char *);
3707 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3708 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3709 static inline const char *AT_string (dw_attr_node *);
3710 static enum dwarf_form AT_string_form (dw_attr_node *);
3711 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3712 static void add_AT_specification (dw_die_ref, dw_die_ref);
3713 static inline dw_die_ref AT_ref (dw_attr_node *);
3714 static inline int AT_ref_external (dw_attr_node *);
3715 static inline void set_AT_ref_external (dw_attr_node *, int);
3716 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3717 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3718 dw_loc_list_ref);
3719 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3720 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3721 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3722 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3723 static void remove_addr_table_entry (addr_table_entry *);
3724 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3725 static inline rtx AT_addr (dw_attr_node *);
3726 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3727 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3728 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3729 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3730 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3731 unsigned long, bool);
3732 static inline const char *AT_lbl (dw_attr_node *);
3733 static const char *get_AT_low_pc (dw_die_ref);
3734 static bool is_c (void);
3735 static bool is_cxx (void);
3736 static bool is_cxx (const_tree);
3737 static bool is_fortran (void);
3738 static bool is_ada (void);
3739 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3740 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3741 static void add_child_die (dw_die_ref, dw_die_ref);
3742 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3743 static dw_die_ref lookup_type_die (tree);
3744 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3745 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3746 static void equate_type_number_to_die (tree, dw_die_ref);
3747 static var_loc_list *lookup_decl_loc (const_tree);
3748 static void equate_decl_number_to_die (tree, dw_die_ref);
3749 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3750 static void print_spaces (FILE *);
3751 static void print_die (dw_die_ref, FILE *);
3752 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3753 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3754 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3755 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3756 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3757 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3758 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3759 struct md5_ctx *, int *);
3760 struct checksum_attributes;
3761 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3762 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3763 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3764 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3765 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3766 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3767 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3768 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3769 static int is_type_die (dw_die_ref);
3770 static inline bool is_template_instantiation (dw_die_ref);
3771 static int is_declaration_die (dw_die_ref);
3772 static int should_move_die_to_comdat (dw_die_ref);
3773 static dw_die_ref clone_as_declaration (dw_die_ref);
3774 static dw_die_ref clone_die (dw_die_ref);
3775 static dw_die_ref clone_tree (dw_die_ref);
3776 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3777 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3778 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3779 static dw_die_ref generate_skeleton (dw_die_ref);
3780 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3781 dw_die_ref,
3782 dw_die_ref);
3783 static void break_out_comdat_types (dw_die_ref);
3784 static void copy_decls_for_unworthy_types (dw_die_ref);
3786 static void add_sibling_attributes (dw_die_ref);
3787 static void output_location_lists (dw_die_ref);
3788 static int constant_size (unsigned HOST_WIDE_INT);
3789 static unsigned long size_of_die (dw_die_ref);
3790 static void calc_die_sizes (dw_die_ref);
3791 static void calc_base_type_die_sizes (void);
3792 static void mark_dies (dw_die_ref);
3793 static void unmark_dies (dw_die_ref);
3794 static void unmark_all_dies (dw_die_ref);
3795 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3796 static unsigned long size_of_aranges (void);
3797 static enum dwarf_form value_format (dw_attr_node *);
3798 static void output_value_format (dw_attr_node *);
3799 static void output_abbrev_section (void);
3800 static void output_die_abbrevs (unsigned long, dw_die_ref);
3801 static void output_die (dw_die_ref);
3802 static void output_compilation_unit_header (enum dwarf_unit_type);
3803 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3804 static void output_comdat_type_unit (comdat_type_node *, bool);
3805 static const char *dwarf2_name (tree, int);
3806 static void add_pubname (tree, dw_die_ref);
3807 static void add_enumerator_pubname (const char *, dw_die_ref);
3808 static void add_pubname_string (const char *, dw_die_ref);
3809 static void add_pubtype (tree, dw_die_ref);
3810 static void output_pubnames (vec<pubname_entry, va_gc> *);
3811 static void output_aranges (void);
3812 static unsigned int add_ranges (const_tree, bool = false);
3813 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3814 bool *, bool);
3815 static void output_ranges (void);
3816 static dw_line_info_table *new_line_info_table (void);
3817 static void output_line_info (bool);
3818 static void output_file_names (void);
3819 static int is_base_type (tree);
3820 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3821 static int decl_quals (const_tree);
3822 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3823 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3824 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3825 static unsigned int dbx_reg_number (const_rtx);
3826 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3827 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3828 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3829 enum var_init_status);
3830 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3831 enum var_init_status);
3832 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3833 enum var_init_status);
3834 static int is_based_loc (const_rtx);
3835 static bool resolve_one_addr (rtx *);
3836 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3837 enum var_init_status);
3838 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3839 enum var_init_status);
3840 struct loc_descr_context;
3841 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3842 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3843 static dw_loc_list_ref loc_list_from_tree (tree, int,
3844 struct loc_descr_context *);
3845 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3846 struct loc_descr_context *);
3847 static tree field_type (const_tree);
3848 static unsigned int simple_type_align_in_bits (const_tree);
3849 static unsigned int simple_decl_align_in_bits (const_tree);
3850 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3851 struct vlr_context;
3852 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3853 HOST_WIDE_INT *);
3854 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3855 dw_loc_list_ref);
3856 static void add_data_member_location_attribute (dw_die_ref, tree,
3857 struct vlr_context *);
3858 static bool add_const_value_attribute (dw_die_ref, rtx);
3859 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3860 static void insert_wide_int (const wide_int &, unsigned char *, int);
3861 static unsigned insert_float (const_rtx, unsigned char *);
3862 static rtx rtl_for_decl_location (tree);
3863 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3864 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3865 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3866 static void add_desc_attribute (dw_die_ref, tree);
3867 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3868 static void add_comp_dir_attribute (dw_die_ref);
3869 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3870 struct loc_descr_context *);
3871 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3872 struct loc_descr_context *);
3873 static void add_subscript_info (dw_die_ref, tree, bool);
3874 static void add_byte_size_attribute (dw_die_ref, tree);
3875 static void add_alignment_attribute (dw_die_ref, tree);
3876 static void add_bit_offset_attribute (dw_die_ref, tree);
3877 static void add_bit_size_attribute (dw_die_ref, tree);
3878 static void add_prototyped_attribute (dw_die_ref, tree);
3879 static void add_abstract_origin_attribute (dw_die_ref, tree);
3880 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3881 static void add_src_coords_attributes (dw_die_ref, tree);
3882 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3883 static void add_discr_value (dw_die_ref, dw_discr_value *);
3884 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3885 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3886 static dw_die_ref scope_die_for (tree, dw_die_ref);
3887 static inline int local_scope_p (dw_die_ref);
3888 static inline int class_scope_p (dw_die_ref);
3889 static inline int class_or_namespace_scope_p (dw_die_ref);
3890 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3891 static void add_calling_convention_attribute (dw_die_ref, tree);
3892 static const char *type_tag (const_tree);
3893 static tree member_declared_type (const_tree);
3894 #if 0
3895 static const char *decl_start_label (tree);
3896 #endif
3897 static void gen_array_type_die (tree, dw_die_ref);
3898 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3899 #if 0
3900 static void gen_entry_point_die (tree, dw_die_ref);
3901 #endif
3902 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3903 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3904 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3905 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3906 static void gen_formal_types_die (tree, dw_die_ref);
3907 static void gen_subprogram_die (tree, dw_die_ref);
3908 static void gen_variable_die (tree, tree, dw_die_ref);
3909 static void gen_const_die (tree, dw_die_ref);
3910 static void gen_label_die (tree, dw_die_ref);
3911 static void gen_lexical_block_die (tree, dw_die_ref);
3912 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3913 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3914 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3915 static dw_die_ref gen_compile_unit_die (const char *);
3916 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3917 static void gen_member_die (tree, dw_die_ref);
3918 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3919 enum debug_info_usage);
3920 static void gen_subroutine_type_die (tree, dw_die_ref);
3921 static void gen_typedef_die (tree, dw_die_ref);
3922 static void gen_type_die (tree, dw_die_ref);
3923 static void gen_block_die (tree, dw_die_ref);
3924 static void decls_for_scope (tree, dw_die_ref, bool = true);
3925 static bool is_naming_typedef_decl (const_tree);
3926 static inline dw_die_ref get_context_die (tree);
3927 static void gen_namespace_die (tree, dw_die_ref);
3928 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3929 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3930 static dw_die_ref force_decl_die (tree);
3931 static dw_die_ref force_type_die (tree);
3932 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3933 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3934 static struct dwarf_file_data * lookup_filename (const char *);
3935 static void retry_incomplete_types (void);
3936 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3937 static void gen_generic_params_dies (tree);
3938 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3939 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3940 static void splice_child_die (dw_die_ref, dw_die_ref);
3941 static int file_info_cmp (const void *, const void *);
3942 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3943 const char *, var_loc_view, const char *);
3944 static void output_loc_list (dw_loc_list_ref);
3945 static char *gen_internal_sym (const char *);
3946 static bool want_pubnames (void);
3948 static void prune_unmark_dies (dw_die_ref);
3949 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3950 static void prune_unused_types_mark (dw_die_ref, int);
3951 static void prune_unused_types_walk (dw_die_ref);
3952 static void prune_unused_types_walk_attribs (dw_die_ref);
3953 static void prune_unused_types_prune (dw_die_ref);
3954 static void prune_unused_types (void);
3955 static int maybe_emit_file (struct dwarf_file_data *fd);
3956 static inline const char *AT_vms_delta1 (dw_attr_node *);
3957 static inline const char *AT_vms_delta2 (dw_attr_node *);
3958 #if VMS_DEBUGGING_INFO
3959 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3960 const char *, const char *);
3961 #endif
3962 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3963 static void gen_remaining_tmpl_value_param_die_attribute (void);
3964 static bool generic_type_p (tree);
3965 static void schedule_generic_params_dies_gen (tree t);
3966 static void gen_scheduled_generic_parms_dies (void);
3967 static void resolve_variable_values (void);
3969 static const char *comp_dir_string (void);
3971 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3973 /* enum for tracking thread-local variables whose address is really an offset
3974 relative to the TLS pointer, which will need link-time relocation, but will
3975 not need relocation by the DWARF consumer. */
3977 enum dtprel_bool
3979 dtprel_false = 0,
3980 dtprel_true = 1
3983 /* Return the operator to use for an address of a variable. For dtprel_true, we
3984 use DW_OP_const*. For regular variables, which need both link-time
3985 relocation and consumer-level relocation (e.g., to account for shared objects
3986 loaded at a random address), we use DW_OP_addr*. */
3988 static inline enum dwarf_location_atom
3989 dw_addr_op (enum dtprel_bool dtprel)
3991 if (dtprel == dtprel_true)
3992 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3993 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3994 else
3995 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3998 /* Return a pointer to a newly allocated address location description. If
3999 dwarf_split_debug_info is true, then record the address with the appropriate
4000 relocation. */
4001 static inline dw_loc_descr_ref
4002 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4004 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4006 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4007 ref->dw_loc_oprnd1.v.val_addr = addr;
4008 ref->dtprel = dtprel;
4009 if (dwarf_split_debug_info)
4010 ref->dw_loc_oprnd1.val_entry
4011 = add_addr_table_entry (addr,
4012 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4013 else
4014 ref->dw_loc_oprnd1.val_entry = NULL;
4016 return ref;
4019 /* Section names used to hold DWARF debugging information. */
4021 #ifndef DEBUG_INFO_SECTION
4022 #define DEBUG_INFO_SECTION ".debug_info"
4023 #endif
4024 #ifndef DEBUG_DWO_INFO_SECTION
4025 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4026 #endif
4027 #ifndef DEBUG_LTO_INFO_SECTION
4028 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4029 #endif
4030 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4031 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4032 #endif
4033 #ifndef DEBUG_ABBREV_SECTION
4034 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4035 #endif
4036 #ifndef DEBUG_LTO_ABBREV_SECTION
4037 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4038 #endif
4039 #ifndef DEBUG_DWO_ABBREV_SECTION
4040 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4041 #endif
4042 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4043 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4044 #endif
4045 #ifndef DEBUG_ARANGES_SECTION
4046 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4047 #endif
4048 #ifndef DEBUG_ADDR_SECTION
4049 #define DEBUG_ADDR_SECTION ".debug_addr"
4050 #endif
4051 #ifndef DEBUG_MACINFO_SECTION
4052 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4053 #endif
4054 #ifndef DEBUG_LTO_MACINFO_SECTION
4055 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4056 #endif
4057 #ifndef DEBUG_DWO_MACINFO_SECTION
4058 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4059 #endif
4060 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4061 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4062 #endif
4063 #ifndef DEBUG_MACRO_SECTION
4064 #define DEBUG_MACRO_SECTION ".debug_macro"
4065 #endif
4066 #ifndef DEBUG_LTO_MACRO_SECTION
4067 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4068 #endif
4069 #ifndef DEBUG_DWO_MACRO_SECTION
4070 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4071 #endif
4072 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4073 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4074 #endif
4075 #ifndef DEBUG_LINE_SECTION
4076 #define DEBUG_LINE_SECTION ".debug_line"
4077 #endif
4078 #ifndef DEBUG_LTO_LINE_SECTION
4079 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4080 #endif
4081 #ifndef DEBUG_DWO_LINE_SECTION
4082 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4083 #endif
4084 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4085 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4086 #endif
4087 #ifndef DEBUG_LOC_SECTION
4088 #define DEBUG_LOC_SECTION ".debug_loc"
4089 #endif
4090 #ifndef DEBUG_DWO_LOC_SECTION
4091 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4092 #endif
4093 #ifndef DEBUG_LOCLISTS_SECTION
4094 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4095 #endif
4096 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4097 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4098 #endif
4099 #ifndef DEBUG_PUBNAMES_SECTION
4100 #define DEBUG_PUBNAMES_SECTION \
4101 ((debug_generate_pub_sections == 2) \
4102 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4103 #endif
4104 #ifndef DEBUG_PUBTYPES_SECTION
4105 #define DEBUG_PUBTYPES_SECTION \
4106 ((debug_generate_pub_sections == 2) \
4107 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4108 #endif
4109 #ifndef DEBUG_STR_OFFSETS_SECTION
4110 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4111 #endif
4112 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4113 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4114 #endif
4115 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4116 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4117 #endif
4118 #ifndef DEBUG_STR_SECTION
4119 #define DEBUG_STR_SECTION ".debug_str"
4120 #endif
4121 #ifndef DEBUG_LTO_STR_SECTION
4122 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4123 #endif
4124 #ifndef DEBUG_STR_DWO_SECTION
4125 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4126 #endif
4127 #ifndef DEBUG_LTO_STR_DWO_SECTION
4128 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4129 #endif
4130 #ifndef DEBUG_RANGES_SECTION
4131 #define DEBUG_RANGES_SECTION ".debug_ranges"
4132 #endif
4133 #ifndef DEBUG_RNGLISTS_SECTION
4134 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4135 #endif
4136 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4137 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4138 #endif
4139 #ifndef DEBUG_LINE_STR_SECTION
4140 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4141 #endif
4142 #ifndef DEBUG_LTO_LINE_STR_SECTION
4143 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4144 #endif
4146 /* Standard ELF section names for compiled code and data. */
4147 #ifndef TEXT_SECTION_NAME
4148 #define TEXT_SECTION_NAME ".text"
4149 #endif
4151 /* Section flags for .debug_str section. */
4152 #define DEBUG_STR_SECTION_FLAGS \
4153 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4154 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4155 : SECTION_DEBUG)
4157 /* Section flags for .debug_str.dwo section. */
4158 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4160 /* Attribute used to refer to the macro section. */
4161 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4162 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4164 /* Labels we insert at beginning sections we can reference instead of
4165 the section names themselves. */
4167 #ifndef TEXT_SECTION_LABEL
4168 #define TEXT_SECTION_LABEL "Ltext"
4169 #endif
4170 #ifndef COLD_TEXT_SECTION_LABEL
4171 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4172 #endif
4173 #ifndef DEBUG_LINE_SECTION_LABEL
4174 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4175 #endif
4176 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4177 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4178 #endif
4179 #ifndef DEBUG_INFO_SECTION_LABEL
4180 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4181 #endif
4182 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4183 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4184 #endif
4185 #ifndef DEBUG_ABBREV_SECTION_LABEL
4186 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4187 #endif
4188 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4189 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4190 #endif
4191 #ifndef DEBUG_ADDR_SECTION_LABEL
4192 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4193 #endif
4194 #ifndef DEBUG_LOC_SECTION_LABEL
4195 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4196 #endif
4197 #ifndef DEBUG_RANGES_SECTION_LABEL
4198 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4199 #endif
4200 #ifndef DEBUG_MACINFO_SECTION_LABEL
4201 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4202 #endif
4203 #ifndef DEBUG_MACRO_SECTION_LABEL
4204 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4205 #endif
4206 #define SKELETON_COMP_DIE_ABBREV 1
4207 #define SKELETON_TYPE_DIE_ABBREV 2
4209 /* Definitions of defaults for formats and names of various special
4210 (artificial) labels which may be generated within this file (when the -g
4211 options is used and DWARF2_DEBUGGING_INFO is in effect.
4212 If necessary, these may be overridden from within the tm.h file, but
4213 typically, overriding these defaults is unnecessary. */
4215 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4216 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4217 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4218 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4219 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4220 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4221 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4222 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4223 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4224 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4225 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4226 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4227 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4228 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4229 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4231 #ifndef TEXT_END_LABEL
4232 #define TEXT_END_LABEL "Letext"
4233 #endif
4234 #ifndef COLD_END_LABEL
4235 #define COLD_END_LABEL "Letext_cold"
4236 #endif
4237 #ifndef BLOCK_BEGIN_LABEL
4238 #define BLOCK_BEGIN_LABEL "LBB"
4239 #endif
4240 #ifndef BLOCK_INLINE_ENTRY_LABEL
4241 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4242 #endif
4243 #ifndef BLOCK_END_LABEL
4244 #define BLOCK_END_LABEL "LBE"
4245 #endif
4246 #ifndef LINE_CODE_LABEL
4247 #define LINE_CODE_LABEL "LM"
4248 #endif
4251 /* Return the root of the DIE's built for the current compilation unit. */
4252 static dw_die_ref
4253 comp_unit_die (void)
4255 if (!single_comp_unit_die)
4256 single_comp_unit_die = gen_compile_unit_die (NULL);
4257 return single_comp_unit_die;
4260 /* We allow a language front-end to designate a function that is to be
4261 called to "demangle" any name before it is put into a DIE. */
4263 static const char *(*demangle_name_func) (const char *);
4265 void
4266 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4268 demangle_name_func = func;
4271 /* Test if rtl node points to a pseudo register. */
4273 static inline int
4274 is_pseudo_reg (const_rtx rtl)
4276 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4277 || (GET_CODE (rtl) == SUBREG
4278 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4281 /* Return a reference to a type, with its const and volatile qualifiers
4282 removed. */
4284 static inline tree
4285 type_main_variant (tree type)
4287 type = TYPE_MAIN_VARIANT (type);
4289 /* ??? There really should be only one main variant among any group of
4290 variants of a given type (and all of the MAIN_VARIANT values for all
4291 members of the group should point to that one type) but sometimes the C
4292 front-end messes this up for array types, so we work around that bug
4293 here. */
4294 if (TREE_CODE (type) == ARRAY_TYPE)
4295 while (type != TYPE_MAIN_VARIANT (type))
4296 type = TYPE_MAIN_VARIANT (type);
4298 return type;
4301 /* Return nonzero if the given type node represents a tagged type. */
4303 static inline int
4304 is_tagged_type (const_tree type)
4306 enum tree_code code = TREE_CODE (type);
4308 return (code == RECORD_TYPE || code == UNION_TYPE
4309 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4312 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4314 static void
4315 get_ref_die_offset_label (char *label, dw_die_ref ref)
4317 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4320 /* Return die_offset of a DIE reference to a base type. */
4322 static unsigned long int
4323 get_base_type_offset (dw_die_ref ref)
4325 if (ref->die_offset)
4326 return ref->die_offset;
4327 if (comp_unit_die ()->die_abbrev)
4329 calc_base_type_die_sizes ();
4330 gcc_assert (ref->die_offset);
4332 return ref->die_offset;
4335 /* Return die_offset of a DIE reference other than base type. */
4337 static unsigned long int
4338 get_ref_die_offset (dw_die_ref ref)
4340 gcc_assert (ref->die_offset);
4341 return ref->die_offset;
4344 /* Convert a DIE tag into its string name. */
4346 static const char *
4347 dwarf_tag_name (unsigned int tag)
4349 const char *name = get_DW_TAG_name (tag);
4351 if (name != NULL)
4352 return name;
4354 return "DW_TAG_<unknown>";
4357 /* Convert a DWARF attribute code into its string name. */
4359 static const char *
4360 dwarf_attr_name (unsigned int attr)
4362 const char *name;
4364 switch (attr)
4366 #if VMS_DEBUGGING_INFO
4367 case DW_AT_HP_prologue:
4368 return "DW_AT_HP_prologue";
4369 #else
4370 case DW_AT_MIPS_loop_unroll_factor:
4371 return "DW_AT_MIPS_loop_unroll_factor";
4372 #endif
4374 #if VMS_DEBUGGING_INFO
4375 case DW_AT_HP_epilogue:
4376 return "DW_AT_HP_epilogue";
4377 #else
4378 case DW_AT_MIPS_stride:
4379 return "DW_AT_MIPS_stride";
4380 #endif
4383 name = get_DW_AT_name (attr);
4385 if (name != NULL)
4386 return name;
4388 return "DW_AT_<unknown>";
4391 /* Convert a DWARF value form code into its string name. */
4393 static const char *
4394 dwarf_form_name (unsigned int form)
4396 const char *name = get_DW_FORM_name (form);
4398 if (name != NULL)
4399 return name;
4401 return "DW_FORM_<unknown>";
4404 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4405 instance of an inlined instance of a decl which is local to an inline
4406 function, so we have to trace all of the way back through the origin chain
4407 to find out what sort of node actually served as the original seed for the
4408 given block. */
4410 static tree
4411 decl_ultimate_origin (const_tree decl)
4413 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4414 return NULL_TREE;
4416 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4417 we're trying to output the abstract instance of this function. */
4418 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4419 return NULL_TREE;
4421 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4422 most distant ancestor, this should never happen. */
4423 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4425 return DECL_ABSTRACT_ORIGIN (decl);
4428 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4429 of a virtual function may refer to a base class, so we check the 'this'
4430 parameter. */
4432 static tree
4433 decl_class_context (tree decl)
4435 tree context = NULL_TREE;
4437 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4438 context = DECL_CONTEXT (decl);
4439 else
4440 context = TYPE_MAIN_VARIANT
4441 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4443 if (context && !TYPE_P (context))
4444 context = NULL_TREE;
4446 return context;
4449 /* Add an attribute/value pair to a DIE. */
4451 static inline void
4452 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4454 /* Maybe this should be an assert? */
4455 if (die == NULL)
4456 return;
4458 if (flag_checking)
4460 /* Check we do not add duplicate attrs. Can't use get_AT here
4461 because that recurses to the specification/abstract origin DIE. */
4462 dw_attr_node *a;
4463 unsigned ix;
4464 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4465 gcc_assert (a->dw_attr != attr->dw_attr);
4468 vec_safe_reserve (die->die_attr, 1);
4469 vec_safe_push (die->die_attr, *attr);
4472 enum dw_val_class
4473 AT_class (dw_attr_node *a)
4475 return a->dw_attr_val.val_class;
4478 /* Return the index for any attribute that will be referenced with a
4479 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4480 indices are stored in dw_attr_val.v.val_str for reference counting
4481 pruning. */
4483 static inline unsigned int
4484 AT_index (dw_attr_node *a)
4486 if (AT_class (a) == dw_val_class_str)
4487 return a->dw_attr_val.v.val_str->index;
4488 else if (a->dw_attr_val.val_entry != NULL)
4489 return a->dw_attr_val.val_entry->index;
4490 return NOT_INDEXED;
4493 /* Add a flag value attribute to a DIE. */
4495 static inline void
4496 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4498 dw_attr_node attr;
4500 attr.dw_attr = attr_kind;
4501 attr.dw_attr_val.val_class = dw_val_class_flag;
4502 attr.dw_attr_val.val_entry = NULL;
4503 attr.dw_attr_val.v.val_flag = flag;
4504 add_dwarf_attr (die, &attr);
4507 static inline unsigned
4508 AT_flag (dw_attr_node *a)
4510 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4511 return a->dw_attr_val.v.val_flag;
4514 /* Add a signed integer attribute value to a DIE. */
4516 static inline void
4517 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4519 dw_attr_node attr;
4521 attr.dw_attr = attr_kind;
4522 attr.dw_attr_val.val_class = dw_val_class_const;
4523 attr.dw_attr_val.val_entry = NULL;
4524 attr.dw_attr_val.v.val_int = int_val;
4525 add_dwarf_attr (die, &attr);
4528 HOST_WIDE_INT
4529 AT_int (dw_attr_node *a)
4531 gcc_assert (a && (AT_class (a) == dw_val_class_const
4532 || AT_class (a) == dw_val_class_const_implicit));
4533 return a->dw_attr_val.v.val_int;
4536 /* Add an unsigned integer attribute value to a DIE. */
4538 static inline void
4539 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4540 unsigned HOST_WIDE_INT unsigned_val)
4542 dw_attr_node attr;
4544 attr.dw_attr = attr_kind;
4545 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4546 attr.dw_attr_val.val_entry = NULL;
4547 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4548 add_dwarf_attr (die, &attr);
4551 unsigned HOST_WIDE_INT
4552 AT_unsigned (dw_attr_node *a)
4554 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4555 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4556 return a->dw_attr_val.v.val_unsigned;
4559 /* Add an unsigned wide integer attribute value to a DIE. */
4561 static inline void
4562 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4563 const wide_int& w)
4565 dw_attr_node attr;
4567 attr.dw_attr = attr_kind;
4568 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4569 attr.dw_attr_val.val_entry = NULL;
4570 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4571 *attr.dw_attr_val.v.val_wide = w;
4572 add_dwarf_attr (die, &attr);
4575 /* Add an unsigned double integer attribute value to a DIE. */
4577 static inline void
4578 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4579 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4581 dw_attr_node attr;
4583 attr.dw_attr = attr_kind;
4584 attr.dw_attr_val.val_class = dw_val_class_const_double;
4585 attr.dw_attr_val.val_entry = NULL;
4586 attr.dw_attr_val.v.val_double.high = high;
4587 attr.dw_attr_val.v.val_double.low = low;
4588 add_dwarf_attr (die, &attr);
4591 /* Add a floating point attribute value to a DIE and return it. */
4593 static inline void
4594 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4595 unsigned int length, unsigned int elt_size, unsigned char *array)
4597 dw_attr_node attr;
4599 attr.dw_attr = attr_kind;
4600 attr.dw_attr_val.val_class = dw_val_class_vec;
4601 attr.dw_attr_val.val_entry = NULL;
4602 attr.dw_attr_val.v.val_vec.length = length;
4603 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4604 attr.dw_attr_val.v.val_vec.array = array;
4605 add_dwarf_attr (die, &attr);
4608 /* Add an 8-byte data attribute value to a DIE. */
4610 static inline void
4611 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4612 unsigned char data8[8])
4614 dw_attr_node attr;
4616 attr.dw_attr = attr_kind;
4617 attr.dw_attr_val.val_class = dw_val_class_data8;
4618 attr.dw_attr_val.val_entry = NULL;
4619 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4620 add_dwarf_attr (die, &attr);
4623 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4624 dwarf_split_debug_info, address attributes in dies destined for the
4625 final executable have force_direct set to avoid using indexed
4626 references. */
4628 static inline void
4629 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4630 bool force_direct)
4632 dw_attr_node attr;
4633 char * lbl_id;
4635 lbl_id = xstrdup (lbl_low);
4636 attr.dw_attr = DW_AT_low_pc;
4637 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4638 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4639 if (dwarf_split_debug_info && !force_direct)
4640 attr.dw_attr_val.val_entry
4641 = add_addr_table_entry (lbl_id, ate_kind_label);
4642 else
4643 attr.dw_attr_val.val_entry = NULL;
4644 add_dwarf_attr (die, &attr);
4646 attr.dw_attr = DW_AT_high_pc;
4647 if (dwarf_version < 4)
4648 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4649 else
4650 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4651 lbl_id = xstrdup (lbl_high);
4652 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4653 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4654 && dwarf_split_debug_info && !force_direct)
4655 attr.dw_attr_val.val_entry
4656 = add_addr_table_entry (lbl_id, ate_kind_label);
4657 else
4658 attr.dw_attr_val.val_entry = NULL;
4659 add_dwarf_attr (die, &attr);
4662 /* Hash and equality functions for debug_str_hash. */
4664 hashval_t
4665 indirect_string_hasher::hash (indirect_string_node *x)
4667 return htab_hash_string (x->str);
4670 bool
4671 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4673 return strcmp (x1->str, x2) == 0;
4676 /* Add STR to the given string hash table. */
4678 static struct indirect_string_node *
4679 find_AT_string_in_table (const char *str,
4680 hash_table<indirect_string_hasher> *table,
4681 enum insert_option insert = INSERT)
4683 struct indirect_string_node *node;
4685 indirect_string_node **slot
4686 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4687 if (*slot == NULL)
4689 node = ggc_cleared_alloc<indirect_string_node> ();
4690 node->str = ggc_strdup (str);
4691 *slot = node;
4693 else
4694 node = *slot;
4696 node->refcount++;
4697 return node;
4700 /* Add STR to the indirect string hash table. */
4702 static struct indirect_string_node *
4703 find_AT_string (const char *str, enum insert_option insert = INSERT)
4705 if (! debug_str_hash)
4706 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4708 return find_AT_string_in_table (str, debug_str_hash, insert);
4711 /* Add a string attribute value to a DIE. */
4713 static inline void
4714 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4716 dw_attr_node attr;
4717 struct indirect_string_node *node;
4719 node = find_AT_string (str);
4721 attr.dw_attr = attr_kind;
4722 attr.dw_attr_val.val_class = dw_val_class_str;
4723 attr.dw_attr_val.val_entry = NULL;
4724 attr.dw_attr_val.v.val_str = node;
4725 add_dwarf_attr (die, &attr);
4728 static inline const char *
4729 AT_string (dw_attr_node *a)
4731 gcc_assert (a && AT_class (a) == dw_val_class_str);
4732 return a->dw_attr_val.v.val_str->str;
4735 /* Call this function directly to bypass AT_string_form's logic to put
4736 the string inline in the die. */
4738 static void
4739 set_indirect_string (struct indirect_string_node *node)
4741 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4742 /* Already indirect is a no op. */
4743 if (node->form == DW_FORM_strp
4744 || node->form == DW_FORM_line_strp
4745 || node->form == dwarf_FORM (DW_FORM_strx))
4747 gcc_assert (node->label);
4748 return;
4750 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4751 ++dw2_string_counter;
4752 node->label = xstrdup (label);
4754 if (!dwarf_split_debug_info)
4756 node->form = DW_FORM_strp;
4757 node->index = NOT_INDEXED;
4759 else
4761 node->form = dwarf_FORM (DW_FORM_strx);
4762 node->index = NO_INDEX_ASSIGNED;
4766 /* A helper function for dwarf2out_finish, called to reset indirect
4767 string decisions done for early LTO dwarf output before fat object
4768 dwarf output. */
4771 reset_indirect_string (indirect_string_node **h, void *)
4773 struct indirect_string_node *node = *h;
4774 if (node->form == DW_FORM_strp
4775 || node->form == DW_FORM_line_strp
4776 || node->form == dwarf_FORM (DW_FORM_strx))
4778 free (node->label);
4779 node->label = NULL;
4780 node->form = (dwarf_form) 0;
4781 node->index = 0;
4783 return 1;
4786 /* Add a string representing a file or filepath attribute value to a DIE. */
4788 static inline void
4789 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4790 const char *str)
4792 if (! asm_outputs_debug_line_str ())
4793 add_AT_string (die, attr_kind, str);
4794 else
4796 dw_attr_node attr;
4797 struct indirect_string_node *node;
4799 if (!debug_line_str_hash)
4800 debug_line_str_hash
4801 = hash_table<indirect_string_hasher>::create_ggc (10);
4803 node = find_AT_string_in_table (str, debug_line_str_hash);
4804 set_indirect_string (node);
4805 node->form = DW_FORM_line_strp;
4807 attr.dw_attr = attr_kind;
4808 attr.dw_attr_val.val_class = dw_val_class_str;
4809 attr.dw_attr_val.val_entry = NULL;
4810 attr.dw_attr_val.v.val_str = node;
4811 add_dwarf_attr (die, &attr);
4815 /* Find out whether a string should be output inline in DIE
4816 or out-of-line in .debug_str section. */
4818 static enum dwarf_form
4819 find_string_form (struct indirect_string_node *node)
4821 unsigned int len;
4823 if (node->form)
4824 return node->form;
4826 len = strlen (node->str) + 1;
4828 /* If the string is shorter or equal to the size of the reference, it is
4829 always better to put it inline. */
4830 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4831 return node->form = DW_FORM_string;
4833 /* If we cannot expect the linker to merge strings in .debug_str
4834 section, only put it into .debug_str if it is worth even in this
4835 single module. */
4836 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4837 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4838 && (len - dwarf_offset_size) * node->refcount <= len))
4839 return node->form = DW_FORM_string;
4841 set_indirect_string (node);
4843 return node->form;
4846 /* Find out whether the string referenced from the attribute should be
4847 output inline in DIE or out-of-line in .debug_str section. */
4849 static enum dwarf_form
4850 AT_string_form (dw_attr_node *a)
4852 gcc_assert (a && AT_class (a) == dw_val_class_str);
4853 return find_string_form (a->dw_attr_val.v.val_str);
4856 /* Add a DIE reference attribute value to a DIE. */
4858 static inline void
4859 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4861 dw_attr_node attr;
4862 gcc_checking_assert (targ_die != NULL);
4864 /* With LTO we can end up trying to reference something we didn't create
4865 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4866 if (targ_die == NULL)
4867 return;
4869 attr.dw_attr = attr_kind;
4870 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4871 attr.dw_attr_val.val_entry = NULL;
4872 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4873 attr.dw_attr_val.v.val_die_ref.external = 0;
4874 add_dwarf_attr (die, &attr);
4877 /* Change DIE reference REF to point to NEW_DIE instead. */
4879 static inline void
4880 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4882 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4883 ref->dw_attr_val.v.val_die_ref.die = new_die;
4884 ref->dw_attr_val.v.val_die_ref.external = 0;
4887 /* Add an AT_specification attribute to a DIE, and also make the back
4888 pointer from the specification to the definition. */
4890 static inline void
4891 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4893 add_AT_die_ref (die, DW_AT_specification, targ_die);
4894 gcc_assert (!targ_die->die_definition);
4895 targ_die->die_definition = die;
4898 static inline dw_die_ref
4899 AT_ref (dw_attr_node *a)
4901 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4902 return a->dw_attr_val.v.val_die_ref.die;
4905 static inline int
4906 AT_ref_external (dw_attr_node *a)
4908 if (a && AT_class (a) == dw_val_class_die_ref)
4909 return a->dw_attr_val.v.val_die_ref.external;
4911 return 0;
4914 static inline void
4915 set_AT_ref_external (dw_attr_node *a, int i)
4917 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4918 a->dw_attr_val.v.val_die_ref.external = i;
4921 /* Add a location description attribute value to a DIE. */
4923 static inline void
4924 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4926 dw_attr_node attr;
4928 attr.dw_attr = attr_kind;
4929 attr.dw_attr_val.val_class = dw_val_class_loc;
4930 attr.dw_attr_val.val_entry = NULL;
4931 attr.dw_attr_val.v.val_loc = loc;
4932 add_dwarf_attr (die, &attr);
4935 dw_loc_descr_ref
4936 AT_loc (dw_attr_node *a)
4938 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4939 return a->dw_attr_val.v.val_loc;
4942 static inline void
4943 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4945 dw_attr_node attr;
4947 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4948 return;
4950 attr.dw_attr = attr_kind;
4951 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4952 attr.dw_attr_val.val_entry = NULL;
4953 attr.dw_attr_val.v.val_loc_list = loc_list;
4954 add_dwarf_attr (die, &attr);
4955 have_location_lists = true;
4958 static inline dw_loc_list_ref
4959 AT_loc_list (dw_attr_node *a)
4961 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4962 return a->dw_attr_val.v.val_loc_list;
4965 /* Add a view list attribute to DIE. It must have a DW_AT_location
4966 attribute, because the view list complements the location list. */
4968 static inline void
4969 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4971 dw_attr_node attr;
4973 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4974 return;
4976 attr.dw_attr = attr_kind;
4977 attr.dw_attr_val.val_class = dw_val_class_view_list;
4978 attr.dw_attr_val.val_entry = NULL;
4979 attr.dw_attr_val.v.val_view_list = die;
4980 add_dwarf_attr (die, &attr);
4981 gcc_checking_assert (get_AT (die, DW_AT_location));
4982 gcc_assert (have_location_lists);
4985 /* Return a pointer to the location list referenced by the attribute.
4986 If the named attribute is a view list, look up the corresponding
4987 DW_AT_location attribute and return its location list. */
4989 static inline dw_loc_list_ref *
4990 AT_loc_list_ptr (dw_attr_node *a)
4992 gcc_assert (a);
4993 switch (AT_class (a))
4995 case dw_val_class_loc_list:
4996 return &a->dw_attr_val.v.val_loc_list;
4997 case dw_val_class_view_list:
4999 dw_attr_node *l;
5000 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5001 if (!l)
5002 return NULL;
5003 gcc_checking_assert (l + 1 == a);
5004 return AT_loc_list_ptr (l);
5006 default:
5007 gcc_unreachable ();
5011 /* Return the location attribute value associated with a view list
5012 attribute value. */
5014 static inline dw_val_node *
5015 view_list_to_loc_list_val_node (dw_val_node *val)
5017 gcc_assert (val->val_class == dw_val_class_view_list);
5018 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5019 if (!loc)
5020 return NULL;
5021 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5022 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5023 return &loc->dw_attr_val;
5026 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5028 static hashval_t hash (addr_table_entry *);
5029 static bool equal (addr_table_entry *, addr_table_entry *);
5032 /* Table of entries into the .debug_addr section. */
5034 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5036 /* Hash an address_table_entry. */
5038 hashval_t
5039 addr_hasher::hash (addr_table_entry *a)
5041 inchash::hash hstate;
5042 switch (a->kind)
5044 case ate_kind_rtx:
5045 hstate.add_int (0);
5046 break;
5047 case ate_kind_rtx_dtprel:
5048 hstate.add_int (1);
5049 break;
5050 case ate_kind_label:
5051 return htab_hash_string (a->addr.label);
5052 default:
5053 gcc_unreachable ();
5055 inchash::add_rtx (a->addr.rtl, hstate);
5056 return hstate.end ();
5059 /* Determine equality for two address_table_entries. */
5061 bool
5062 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5064 if (a1->kind != a2->kind)
5065 return 0;
5066 switch (a1->kind)
5068 case ate_kind_rtx:
5069 case ate_kind_rtx_dtprel:
5070 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5071 case ate_kind_label:
5072 return strcmp (a1->addr.label, a2->addr.label) == 0;
5073 default:
5074 gcc_unreachable ();
5078 /* Initialize an addr_table_entry. */
5080 void
5081 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5083 e->kind = kind;
5084 switch (kind)
5086 case ate_kind_rtx:
5087 case ate_kind_rtx_dtprel:
5088 e->addr.rtl = (rtx) addr;
5089 break;
5090 case ate_kind_label:
5091 e->addr.label = (char *) addr;
5092 break;
5094 e->refcount = 0;
5095 e->index = NO_INDEX_ASSIGNED;
5098 /* Add attr to the address table entry to the table. Defer setting an
5099 index until output time. */
5101 static addr_table_entry *
5102 add_addr_table_entry (void *addr, enum ate_kind kind)
5104 addr_table_entry *node;
5105 addr_table_entry finder;
5107 gcc_assert (dwarf_split_debug_info);
5108 if (! addr_index_table)
5109 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5110 init_addr_table_entry (&finder, kind, addr);
5111 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5113 if (*slot == HTAB_EMPTY_ENTRY)
5115 node = ggc_cleared_alloc<addr_table_entry> ();
5116 init_addr_table_entry (node, kind, addr);
5117 *slot = node;
5119 else
5120 node = *slot;
5122 node->refcount++;
5123 return node;
5126 /* Remove an entry from the addr table by decrementing its refcount.
5127 Strictly, decrementing the refcount would be enough, but the
5128 assertion that the entry is actually in the table has found
5129 bugs. */
5131 static void
5132 remove_addr_table_entry (addr_table_entry *entry)
5134 gcc_assert (dwarf_split_debug_info && addr_index_table);
5135 /* After an index is assigned, the table is frozen. */
5136 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5137 entry->refcount--;
5140 /* Given a location list, remove all addresses it refers to from the
5141 address_table. */
5143 static void
5144 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5146 for (; descr; descr = descr->dw_loc_next)
5147 if (descr->dw_loc_oprnd1.val_entry != NULL)
5149 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5150 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5154 /* A helper function for dwarf2out_finish called through
5155 htab_traverse. Assign an addr_table_entry its index. All entries
5156 must be collected into the table when this function is called,
5157 because the indexing code relies on htab_traverse to traverse nodes
5158 in the same order for each run. */
5161 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5163 addr_table_entry *node = *h;
5165 /* Don't index unreferenced nodes. */
5166 if (node->refcount == 0)
5167 return 1;
5169 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5170 node->index = *index;
5171 *index += 1;
5173 return 1;
5176 /* Return the tag of a given DIE. */
5178 enum dwarf_tag
5179 dw_get_die_tag (dw_die_ref die)
5181 return die->die_tag;
5184 /* Return a reference to the children list of a given DIE. */
5186 dw_die_ref
5187 dw_get_die_child (dw_die_ref die)
5189 return die->die_child;
5192 /* Return a reference to the sibling of a given DIE. */
5194 dw_die_ref
5195 dw_get_die_sib (dw_die_ref die)
5197 return die->die_sib;
5200 /* Add an address constant attribute value to a DIE. When using
5201 dwarf_split_debug_info, address attributes in dies destined for the
5202 final executable should be direct references--setting the parameter
5203 force_direct ensures this behavior. */
5205 static inline void
5206 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5207 bool force_direct)
5209 dw_attr_node attr;
5211 attr.dw_attr = attr_kind;
5212 attr.dw_attr_val.val_class = dw_val_class_addr;
5213 attr.dw_attr_val.v.val_addr = addr;
5214 if (dwarf_split_debug_info && !force_direct)
5215 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5216 else
5217 attr.dw_attr_val.val_entry = NULL;
5218 add_dwarf_attr (die, &attr);
5221 /* Get the RTX from to an address DIE attribute. */
5223 static inline rtx
5224 AT_addr (dw_attr_node *a)
5226 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5227 return a->dw_attr_val.v.val_addr;
5230 /* Add a file attribute value to a DIE. */
5232 static inline void
5233 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5234 struct dwarf_file_data *fd)
5236 dw_attr_node attr;
5238 attr.dw_attr = attr_kind;
5239 attr.dw_attr_val.val_class = dw_val_class_file;
5240 attr.dw_attr_val.val_entry = NULL;
5241 attr.dw_attr_val.v.val_file = fd;
5242 add_dwarf_attr (die, &attr);
5245 /* Get the dwarf_file_data from a file DIE attribute. */
5247 static inline struct dwarf_file_data *
5248 AT_file (dw_attr_node *a)
5250 gcc_assert (a && (AT_class (a) == dw_val_class_file
5251 || AT_class (a) == dw_val_class_file_implicit));
5252 return a->dw_attr_val.v.val_file;
5255 #if VMS_DEBUGGING_INFO
5256 /* Add a vms delta attribute value to a DIE. */
5258 static inline void
5259 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5260 const char *lbl1, const char *lbl2)
5262 dw_attr_node attr;
5264 attr.dw_attr = attr_kind;
5265 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5266 attr.dw_attr_val.val_entry = NULL;
5267 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5268 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5269 add_dwarf_attr (die, &attr);
5271 #endif
5273 /* Add a symbolic view identifier attribute value to a DIE. */
5275 static inline void
5276 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5277 const char *view_label)
5279 dw_attr_node attr;
5281 attr.dw_attr = attr_kind;
5282 attr.dw_attr_val.val_class = dw_val_class_symview;
5283 attr.dw_attr_val.val_entry = NULL;
5284 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5285 add_dwarf_attr (die, &attr);
5288 /* Add a label identifier attribute value to a DIE. */
5290 static inline void
5291 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5292 const char *lbl_id)
5294 dw_attr_node attr;
5296 attr.dw_attr = attr_kind;
5297 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5298 attr.dw_attr_val.val_entry = NULL;
5299 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5300 if (dwarf_split_debug_info)
5301 attr.dw_attr_val.val_entry
5302 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5303 ate_kind_label);
5304 add_dwarf_attr (die, &attr);
5307 /* Add a section offset attribute value to a DIE, an offset into the
5308 debug_line section. */
5310 static inline void
5311 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5312 const char *label)
5314 dw_attr_node attr;
5316 attr.dw_attr = attr_kind;
5317 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5318 attr.dw_attr_val.val_entry = NULL;
5319 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5320 add_dwarf_attr (die, &attr);
5323 /* Add a section offset attribute value to a DIE, an offset into the
5324 debug_macinfo section. */
5326 static inline void
5327 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5328 const char *label)
5330 dw_attr_node attr;
5332 attr.dw_attr = attr_kind;
5333 attr.dw_attr_val.val_class = dw_val_class_macptr;
5334 attr.dw_attr_val.val_entry = NULL;
5335 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5336 add_dwarf_attr (die, &attr);
5339 /* Add a range_list attribute value to a DIE. When using
5340 dwarf_split_debug_info, address attributes in dies destined for the
5341 final executable should be direct references--setting the parameter
5342 force_direct ensures this behavior. */
5344 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5345 #define RELOCATED_OFFSET (NULL)
5347 static void
5348 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5349 long unsigned int offset, bool force_direct)
5351 dw_attr_node attr;
5353 attr.dw_attr = attr_kind;
5354 attr.dw_attr_val.val_class = dw_val_class_range_list;
5355 /* For the range_list attribute, use val_entry to store whether the
5356 offset should follow split-debug-info or normal semantics. This
5357 value is read in output_range_list_offset. */
5358 if (dwarf_split_debug_info && !force_direct)
5359 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5360 else
5361 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5362 attr.dw_attr_val.v.val_offset = offset;
5363 add_dwarf_attr (die, &attr);
5366 /* Return the start label of a delta attribute. */
5368 static inline const char *
5369 AT_vms_delta1 (dw_attr_node *a)
5371 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5372 return a->dw_attr_val.v.val_vms_delta.lbl1;
5375 /* Return the end label of a delta attribute. */
5377 static inline const char *
5378 AT_vms_delta2 (dw_attr_node *a)
5380 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5381 return a->dw_attr_val.v.val_vms_delta.lbl2;
5384 static inline const char *
5385 AT_lbl (dw_attr_node *a)
5387 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5388 || AT_class (a) == dw_val_class_lineptr
5389 || AT_class (a) == dw_val_class_macptr
5390 || AT_class (a) == dw_val_class_loclistsptr
5391 || AT_class (a) == dw_val_class_high_pc));
5392 return a->dw_attr_val.v.val_lbl_id;
5395 /* Get the attribute of type attr_kind. */
5397 dw_attr_node *
5398 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5400 dw_attr_node *a;
5401 unsigned ix;
5402 dw_die_ref spec = NULL;
5404 if (! die)
5405 return NULL;
5407 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5408 if (a->dw_attr == attr_kind)
5409 return a;
5410 else if (a->dw_attr == DW_AT_specification
5411 || a->dw_attr == DW_AT_abstract_origin)
5412 spec = AT_ref (a);
5414 if (spec)
5415 return get_AT (spec, attr_kind);
5417 return NULL;
5420 /* Returns the parent of the declaration of DIE. */
5422 static dw_die_ref
5423 get_die_parent (dw_die_ref die)
5425 dw_die_ref t;
5427 if (!die)
5428 return NULL;
5430 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5431 || (t = get_AT_ref (die, DW_AT_specification)))
5432 die = t;
5434 return die->die_parent;
5437 /* Return the "low pc" attribute value, typically associated with a subprogram
5438 DIE. Return null if the "low pc" attribute is either not present, or if it
5439 cannot be represented as an assembler label identifier. */
5441 static inline const char *
5442 get_AT_low_pc (dw_die_ref die)
5444 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5446 return a ? AT_lbl (a) : NULL;
5449 /* Return the value of the string attribute designated by ATTR_KIND, or
5450 NULL if it is not present. */
5452 const char *
5453 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5455 dw_attr_node *a = get_AT (die, attr_kind);
5457 return a ? AT_string (a) : NULL;
5460 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5461 if it is not present. */
5464 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5466 dw_attr_node *a = get_AT (die, attr_kind);
5468 return a ? AT_flag (a) : 0;
5471 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5472 if it is not present. */
5474 unsigned
5475 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5477 dw_attr_node *a = get_AT (die, attr_kind);
5479 return a ? AT_unsigned (a) : 0;
5482 dw_die_ref
5483 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5485 dw_attr_node *a = get_AT (die, attr_kind);
5487 return a ? AT_ref (a) : NULL;
5490 struct dwarf_file_data *
5491 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5493 dw_attr_node *a = get_AT (die, attr_kind);
5495 return a ? AT_file (a) : NULL;
5498 /* Return TRUE if the language is C. */
5500 static inline bool
5501 is_c (void)
5503 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5505 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5506 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5511 /* Return TRUE if the language is C++. */
5513 static inline bool
5514 is_cxx (void)
5516 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5518 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5519 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5522 /* Return TRUE if DECL was created by the C++ frontend. */
5524 static bool
5525 is_cxx (const_tree decl)
5527 if (in_lto_p)
5529 const_tree context = get_ultimate_context (decl);
5530 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5531 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5533 return is_cxx ();
5536 /* Return TRUE if the language is Fortran. */
5538 static inline bool
5539 is_fortran (void)
5541 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5543 return (lang == DW_LANG_Fortran77
5544 || lang == DW_LANG_Fortran90
5545 || lang == DW_LANG_Fortran95
5546 || lang == DW_LANG_Fortran03
5547 || lang == DW_LANG_Fortran08);
5550 static inline bool
5551 is_fortran (const_tree decl)
5553 if (in_lto_p)
5555 const_tree context = get_ultimate_context (decl);
5556 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5557 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5558 "GNU Fortran", 11) == 0
5559 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5560 "GNU F77") == 0);
5562 return is_fortran ();
5565 /* Return TRUE if the language is Ada. */
5567 static inline bool
5568 is_ada (void)
5570 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5572 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5575 /* Return TRUE if the language is D. */
5577 static inline bool
5578 is_dlang (void)
5580 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5582 return lang == DW_LANG_D;
5585 /* Remove the specified attribute if present. Return TRUE if removal
5586 was successful. */
5588 static bool
5589 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5591 dw_attr_node *a;
5592 unsigned ix;
5594 if (! die)
5595 return false;
5597 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5598 if (a->dw_attr == attr_kind)
5600 if (AT_class (a) == dw_val_class_str)
5601 if (a->dw_attr_val.v.val_str->refcount)
5602 a->dw_attr_val.v.val_str->refcount--;
5604 /* vec::ordered_remove should help reduce the number of abbrevs
5605 that are needed. */
5606 die->die_attr->ordered_remove (ix);
5607 return true;
5609 return false;
5612 /* Remove CHILD from its parent. PREV must have the property that
5613 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5615 static void
5616 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5618 gcc_assert (child->die_parent == prev->die_parent);
5619 gcc_assert (prev->die_sib == child);
5620 if (prev == child)
5622 gcc_assert (child->die_parent->die_child == child);
5623 prev = NULL;
5625 else
5626 prev->die_sib = child->die_sib;
5627 if (child->die_parent->die_child == child)
5628 child->die_parent->die_child = prev;
5629 child->die_sib = NULL;
5632 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5633 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5635 static void
5636 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5638 dw_die_ref parent = old_child->die_parent;
5640 gcc_assert (parent == prev->die_parent);
5641 gcc_assert (prev->die_sib == old_child);
5643 new_child->die_parent = parent;
5644 if (prev == old_child)
5646 gcc_assert (parent->die_child == old_child);
5647 new_child->die_sib = new_child;
5649 else
5651 prev->die_sib = new_child;
5652 new_child->die_sib = old_child->die_sib;
5654 if (old_child->die_parent->die_child == old_child)
5655 old_child->die_parent->die_child = new_child;
5656 old_child->die_sib = NULL;
5659 /* Move all children from OLD_PARENT to NEW_PARENT. */
5661 static void
5662 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5664 dw_die_ref c;
5665 new_parent->die_child = old_parent->die_child;
5666 old_parent->die_child = NULL;
5667 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5670 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5671 matches TAG. */
5673 static void
5674 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5676 dw_die_ref c;
5678 c = die->die_child;
5679 if (c) do {
5680 dw_die_ref prev = c;
5681 c = c->die_sib;
5682 while (c->die_tag == tag)
5684 remove_child_with_prev (c, prev);
5685 c->die_parent = NULL;
5686 /* Might have removed every child. */
5687 if (die->die_child == NULL)
5688 return;
5689 c = prev->die_sib;
5691 } while (c != die->die_child);
5694 /* Add a CHILD_DIE as the last child of DIE. */
5696 static void
5697 add_child_die (dw_die_ref die, dw_die_ref child_die)
5699 /* FIXME this should probably be an assert. */
5700 if (! die || ! child_die)
5701 return;
5702 gcc_assert (die != child_die);
5704 child_die->die_parent = die;
5705 if (die->die_child)
5707 child_die->die_sib = die->die_child->die_sib;
5708 die->die_child->die_sib = child_die;
5710 else
5711 child_die->die_sib = child_die;
5712 die->die_child = child_die;
5715 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5717 static void
5718 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5719 dw_die_ref after_die)
5721 gcc_assert (die
5722 && child_die
5723 && after_die
5724 && die->die_child
5725 && die != child_die);
5727 child_die->die_parent = die;
5728 child_die->die_sib = after_die->die_sib;
5729 after_die->die_sib = child_die;
5730 if (die->die_child == after_die)
5731 die->die_child = child_die;
5734 /* Unassociate CHILD from its parent, and make its parent be
5735 NEW_PARENT. */
5737 static void
5738 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5740 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5741 if (p->die_sib == child)
5743 remove_child_with_prev (child, p);
5744 break;
5746 add_child_die (new_parent, child);
5749 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5750 is the specification, to the end of PARENT's list of children.
5751 This is done by removing and re-adding it. */
5753 static void
5754 splice_child_die (dw_die_ref parent, dw_die_ref child)
5756 /* We want the declaration DIE from inside the class, not the
5757 specification DIE at toplevel. */
5758 if (child->die_parent != parent)
5760 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5762 if (tmp)
5763 child = tmp;
5766 gcc_assert (child->die_parent == parent
5767 || (child->die_parent
5768 == get_AT_ref (parent, DW_AT_specification)));
5770 reparent_child (child, parent);
5773 /* Create and return a new die with TAG_VALUE as tag. */
5775 dw_die_ref
5776 new_die_raw (enum dwarf_tag tag_value)
5778 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5779 die->die_tag = tag_value;
5780 return die;
5783 /* Create and return a new die with a parent of PARENT_DIE. If
5784 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5785 associated tree T must be supplied to determine parenthood
5786 later. */
5788 static inline dw_die_ref
5789 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5791 dw_die_ref die = new_die_raw (tag_value);
5793 if (parent_die != NULL)
5794 add_child_die (parent_die, die);
5795 else
5797 limbo_die_node *limbo_node;
5799 /* No DIEs created after early dwarf should end up in limbo,
5800 because the limbo list should not persist past LTO
5801 streaming. */
5802 if (tag_value != DW_TAG_compile_unit
5803 /* These are allowed because they're generated while
5804 breaking out COMDAT units late. */
5805 && tag_value != DW_TAG_type_unit
5806 && tag_value != DW_TAG_skeleton_unit
5807 && !early_dwarf
5808 /* Allow nested functions to live in limbo because they will
5809 only temporarily live there, as decls_for_scope will fix
5810 them up. */
5811 && (TREE_CODE (t) != FUNCTION_DECL
5812 || !decl_function_context (t))
5813 /* Same as nested functions above but for types. Types that
5814 are local to a function will be fixed in
5815 decls_for_scope. */
5816 && (!RECORD_OR_UNION_TYPE_P (t)
5817 || !TYPE_CONTEXT (t)
5818 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5819 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5820 especially in the ltrans stage, but once we implement LTO
5821 dwarf streaming, we should remove this exception. */
5822 && !in_lto_p)
5824 fprintf (stderr, "symbol ended up in limbo too late:");
5825 debug_generic_stmt (t);
5826 gcc_unreachable ();
5829 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5830 limbo_node->die = die;
5831 limbo_node->created_for = t;
5832 limbo_node->next = limbo_die_list;
5833 limbo_die_list = limbo_node;
5836 return die;
5839 /* Return the DIE associated with the given type specifier. */
5841 static inline dw_die_ref
5842 lookup_type_die (tree type)
5844 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5845 if (die && die->removed)
5847 TYPE_SYMTAB_DIE (type) = NULL;
5848 return NULL;
5850 return die;
5853 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5854 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5855 anonymous type instead the one of the naming typedef. */
5857 static inline dw_die_ref
5858 strip_naming_typedef (tree type, dw_die_ref type_die)
5860 if (type
5861 && TREE_CODE (type) == RECORD_TYPE
5862 && type_die
5863 && type_die->die_tag == DW_TAG_typedef
5864 && is_naming_typedef_decl (TYPE_NAME (type)))
5865 type_die = get_AT_ref (type_die, DW_AT_type);
5866 return type_die;
5869 /* Like lookup_type_die, but if type is an anonymous type named by a
5870 typedef[1], return the DIE of the anonymous type instead the one of
5871 the naming typedef. This is because in gen_typedef_die, we did
5872 equate the anonymous struct named by the typedef with the DIE of
5873 the naming typedef. So by default, lookup_type_die on an anonymous
5874 struct yields the DIE of the naming typedef.
5876 [1]: Read the comment of is_naming_typedef_decl to learn about what
5877 a naming typedef is. */
5879 static inline dw_die_ref
5880 lookup_type_die_strip_naming_typedef (tree type)
5882 dw_die_ref die = lookup_type_die (type);
5883 return strip_naming_typedef (type, die);
5886 /* Equate a DIE to a given type specifier. */
5888 static inline void
5889 equate_type_number_to_die (tree type, dw_die_ref type_die)
5891 TYPE_SYMTAB_DIE (type) = type_die;
5894 static dw_die_ref maybe_create_die_with_external_ref (tree);
5895 struct GTY(()) sym_off_pair
5897 const char * GTY((skip)) sym;
5898 unsigned HOST_WIDE_INT off;
5900 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5902 /* Returns a hash value for X (which really is a die_struct). */
5904 inline hashval_t
5905 decl_die_hasher::hash (die_node *x)
5907 return (hashval_t) x->decl_id;
5910 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5912 inline bool
5913 decl_die_hasher::equal (die_node *x, tree y)
5915 return (x->decl_id == DECL_UID (y));
5918 /* Return the DIE associated with a given declaration. */
5920 dw_die_ref
5921 lookup_decl_die (tree decl)
5923 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5924 NO_INSERT);
5925 if (!die)
5927 if (in_lto_p)
5928 return maybe_create_die_with_external_ref (decl);
5929 return NULL;
5931 if ((*die)->removed)
5933 decl_die_table->clear_slot (die);
5934 return NULL;
5936 return *die;
5940 /* Return the DIE associated with BLOCK. */
5942 static inline dw_die_ref
5943 lookup_block_die (tree block)
5945 dw_die_ref die = BLOCK_DIE (block);
5946 if (!die && in_lto_p)
5947 return maybe_create_die_with_external_ref (block);
5948 return die;
5951 /* Associate DIE with BLOCK. */
5953 static inline void
5954 equate_block_to_die (tree block, dw_die_ref die)
5956 BLOCK_DIE (block) = die;
5958 #undef BLOCK_DIE
5961 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5962 style reference. Return true if we found one refering to a DIE for
5963 DECL, otherwise return false. */
5965 static bool
5966 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5967 unsigned HOST_WIDE_INT *off)
5969 dw_die_ref die;
5971 if (in_lto_p)
5973 /* During WPA stage and incremental linking we use a hash-map
5974 to store the decl <-> label + offset map. */
5975 if (!external_die_map)
5976 return false;
5977 sym_off_pair *desc = external_die_map->get (decl);
5978 if (!desc)
5979 return false;
5980 *sym = desc->sym;
5981 *off = desc->off;
5982 return true;
5985 if (TREE_CODE (decl) == BLOCK)
5986 die = lookup_block_die (decl);
5987 else
5988 die = lookup_decl_die (decl);
5989 if (!die)
5990 return false;
5992 /* Similar to get_ref_die_offset_label, but using the "correct"
5993 label. */
5994 *off = die->die_offset;
5995 while (die->die_parent)
5996 die = die->die_parent;
5997 /* For the containing CU DIE we compute a die_symbol in
5998 compute_comp_unit_symbol. */
5999 gcc_assert (die->die_tag == DW_TAG_compile_unit
6000 && die->die_id.die_symbol != NULL);
6001 *sym = die->die_id.die_symbol;
6002 return true;
6005 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6007 static void
6008 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6009 const char *symbol, HOST_WIDE_INT offset)
6011 /* Create a fake DIE that contains the reference. Don't use
6012 new_die because we don't want to end up in the limbo list. */
6013 /* ??? We probably want to share these, thus put a ref to the DIE
6014 we create here to the external_die_map entry. */
6015 dw_die_ref ref = new_die_raw (die->die_tag);
6016 ref->die_id.die_symbol = symbol;
6017 ref->die_offset = offset;
6018 ref->with_offset = 1;
6019 add_AT_die_ref (die, attr_kind, ref);
6022 /* Create a DIE for DECL if required and add a reference to a DIE
6023 at SYMBOL + OFFSET which contains attributes dumped early. */
6025 static void
6026 dwarf2out_register_external_die (tree decl, const char *sym,
6027 unsigned HOST_WIDE_INT off)
6029 if (debug_info_level == DINFO_LEVEL_NONE)
6030 return;
6032 if (!external_die_map)
6033 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6034 gcc_checking_assert (!external_die_map->get (decl));
6035 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6036 external_die_map->put (decl, p);
6039 /* If we have a registered external DIE for DECL return a new DIE for
6040 the concrete instance with an appropriate abstract origin. */
6042 static dw_die_ref
6043 maybe_create_die_with_external_ref (tree decl)
6045 if (!external_die_map)
6046 return NULL;
6047 sym_off_pair *desc = external_die_map->get (decl);
6048 if (!desc)
6049 return NULL;
6051 const char *sym = desc->sym;
6052 unsigned HOST_WIDE_INT off = desc->off;
6053 external_die_map->remove (decl);
6055 in_lto_p = false;
6056 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6057 ? lookup_block_die (decl) : lookup_decl_die (decl));
6058 gcc_assert (!die);
6059 in_lto_p = true;
6061 tree ctx;
6062 dw_die_ref parent = NULL;
6063 /* Need to lookup a DIE for the decls context - the containing
6064 function or translation unit. */
6065 if (TREE_CODE (decl) == BLOCK)
6067 ctx = BLOCK_SUPERCONTEXT (decl);
6068 /* ??? We do not output DIEs for all scopes thus skip as
6069 many DIEs as needed. */
6070 while (TREE_CODE (ctx) == BLOCK
6071 && !lookup_block_die (ctx))
6072 ctx = BLOCK_SUPERCONTEXT (ctx);
6074 else
6075 ctx = DECL_CONTEXT (decl);
6076 /* Peel types in the context stack. */
6077 while (ctx && TYPE_P (ctx))
6078 ctx = TYPE_CONTEXT (ctx);
6079 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6080 if (debug_info_level <= DINFO_LEVEL_TERSE)
6081 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6082 ctx = DECL_CONTEXT (ctx);
6083 if (ctx)
6085 if (TREE_CODE (ctx) == BLOCK)
6086 parent = lookup_block_die (ctx);
6087 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6088 /* Keep the 1:1 association during WPA. */
6089 && !flag_wpa
6090 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6091 /* Otherwise all late annotations go to the main CU which
6092 imports the original CUs. */
6093 parent = comp_unit_die ();
6094 else if (TREE_CODE (ctx) == FUNCTION_DECL
6095 && TREE_CODE (decl) != FUNCTION_DECL
6096 && TREE_CODE (decl) != PARM_DECL
6097 && TREE_CODE (decl) != RESULT_DECL
6098 && TREE_CODE (decl) != BLOCK)
6099 /* Leave function local entities parent determination to when
6100 we process scope vars. */
6102 else
6103 parent = lookup_decl_die (ctx);
6105 else
6106 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6107 Handle this case gracefully by globalizing stuff. */
6108 parent = comp_unit_die ();
6109 /* Create a DIE "stub". */
6110 switch (TREE_CODE (decl))
6112 case TRANSLATION_UNIT_DECL:
6114 die = comp_unit_die ();
6115 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6116 to create a DIE for the original CUs. */
6117 return die;
6119 case NAMESPACE_DECL:
6120 if (is_fortran (decl))
6121 die = new_die (DW_TAG_module, parent, decl);
6122 else
6123 die = new_die (DW_TAG_namespace, parent, decl);
6124 break;
6125 case FUNCTION_DECL:
6126 die = new_die (DW_TAG_subprogram, parent, decl);
6127 break;
6128 case VAR_DECL:
6129 die = new_die (DW_TAG_variable, parent, decl);
6130 break;
6131 case RESULT_DECL:
6132 die = new_die (DW_TAG_variable, parent, decl);
6133 break;
6134 case PARM_DECL:
6135 die = new_die (DW_TAG_formal_parameter, parent, decl);
6136 break;
6137 case CONST_DECL:
6138 die = new_die (DW_TAG_constant, parent, decl);
6139 break;
6140 case LABEL_DECL:
6141 die = new_die (DW_TAG_label, parent, decl);
6142 break;
6143 case BLOCK:
6144 die = new_die (DW_TAG_lexical_block, parent, decl);
6145 break;
6146 default:
6147 gcc_unreachable ();
6149 if (TREE_CODE (decl) == BLOCK)
6150 equate_block_to_die (decl, die);
6151 else
6152 equate_decl_number_to_die (decl, die);
6154 add_desc_attribute (die, decl);
6156 /* Add a reference to the DIE providing early debug at $sym + off. */
6157 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6159 return die;
6162 /* Returns a hash value for X (which really is a var_loc_list). */
6164 inline hashval_t
6165 decl_loc_hasher::hash (var_loc_list *x)
6167 return (hashval_t) x->decl_id;
6170 /* Return nonzero if decl_id of var_loc_list X is the same as
6171 UID of decl *Y. */
6173 inline bool
6174 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6176 return (x->decl_id == DECL_UID (y));
6179 /* Return the var_loc list associated with a given declaration. */
6181 static inline var_loc_list *
6182 lookup_decl_loc (const_tree decl)
6184 if (!decl_loc_table)
6185 return NULL;
6186 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6189 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6191 inline hashval_t
6192 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6194 return (hashval_t) x->decl_id;
6197 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6198 UID of decl *Y. */
6200 inline bool
6201 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6203 return (x->decl_id == DECL_UID (y));
6206 /* Equate a DIE to a particular declaration. */
6208 static void
6209 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6211 unsigned int decl_id = DECL_UID (decl);
6213 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6214 decl_die->decl_id = decl_id;
6217 /* Return how many bits covers PIECE EXPR_LIST. */
6219 static HOST_WIDE_INT
6220 decl_piece_bitsize (rtx piece)
6222 int ret = (int) GET_MODE (piece);
6223 if (ret)
6224 return ret;
6225 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6226 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6227 return INTVAL (XEXP (XEXP (piece, 0), 0));
6230 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6232 static rtx *
6233 decl_piece_varloc_ptr (rtx piece)
6235 if ((int) GET_MODE (piece))
6236 return &XEXP (piece, 0);
6237 else
6238 return &XEXP (XEXP (piece, 0), 1);
6241 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6242 Next is the chain of following piece nodes. */
6244 static rtx_expr_list *
6245 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6247 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6248 return alloc_EXPR_LIST (bitsize, loc_note, next);
6249 else
6250 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6251 GEN_INT (bitsize),
6252 loc_note), next);
6255 /* Return rtx that should be stored into loc field for
6256 LOC_NOTE and BITPOS/BITSIZE. */
6258 static rtx
6259 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6260 HOST_WIDE_INT bitsize)
6262 if (bitsize != -1)
6264 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6265 if (bitpos != 0)
6266 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6268 return loc_note;
6271 /* This function either modifies location piece list *DEST in
6272 place (if SRC and INNER is NULL), or copies location piece list
6273 *SRC to *DEST while modifying it. Location BITPOS is modified
6274 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6275 not copied and if needed some padding around it is added.
6276 When modifying in place, DEST should point to EXPR_LIST where
6277 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6278 to the start of the whole list and INNER points to the EXPR_LIST
6279 where earlier pieces cover PIECE_BITPOS bits. */
6281 static void
6282 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6283 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6284 HOST_WIDE_INT bitsize, rtx loc_note)
6286 HOST_WIDE_INT diff;
6287 bool copy = inner != NULL;
6289 if (copy)
6291 /* First copy all nodes preceding the current bitpos. */
6292 while (src != inner)
6294 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6295 decl_piece_bitsize (*src), NULL_RTX);
6296 dest = &XEXP (*dest, 1);
6297 src = &XEXP (*src, 1);
6300 /* Add padding if needed. */
6301 if (bitpos != piece_bitpos)
6303 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6304 copy ? NULL_RTX : *dest);
6305 dest = &XEXP (*dest, 1);
6307 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6309 gcc_assert (!copy);
6310 /* A piece with correct bitpos and bitsize already exist,
6311 just update the location for it and return. */
6312 *decl_piece_varloc_ptr (*dest) = loc_note;
6313 return;
6315 /* Add the piece that changed. */
6316 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6317 dest = &XEXP (*dest, 1);
6318 /* Skip over pieces that overlap it. */
6319 diff = bitpos - piece_bitpos + bitsize;
6320 if (!copy)
6321 src = dest;
6322 while (diff > 0 && *src)
6324 rtx piece = *src;
6325 diff -= decl_piece_bitsize (piece);
6326 if (copy)
6327 src = &XEXP (piece, 1);
6328 else
6330 *src = XEXP (piece, 1);
6331 free_EXPR_LIST_node (piece);
6334 /* Add padding if needed. */
6335 if (diff < 0 && *src)
6337 if (!copy)
6338 dest = src;
6339 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6340 dest = &XEXP (*dest, 1);
6342 if (!copy)
6343 return;
6344 /* Finally copy all nodes following it. */
6345 while (*src)
6347 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6348 decl_piece_bitsize (*src), NULL_RTX);
6349 dest = &XEXP (*dest, 1);
6350 src = &XEXP (*src, 1);
6354 /* Add a variable location node to the linked list for DECL. */
6356 static struct var_loc_node *
6357 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6359 unsigned int decl_id;
6360 var_loc_list *temp;
6361 struct var_loc_node *loc = NULL;
6362 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6364 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6366 tree realdecl = DECL_DEBUG_EXPR (decl);
6367 if (handled_component_p (realdecl)
6368 || (TREE_CODE (realdecl) == MEM_REF
6369 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6371 bool reverse;
6372 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6373 &bitsize, &reverse);
6374 if (!innerdecl
6375 || !DECL_P (innerdecl)
6376 || DECL_IGNORED_P (innerdecl)
6377 || TREE_STATIC (innerdecl)
6378 || bitsize == 0
6379 || bitpos + bitsize > 256)
6380 return NULL;
6381 decl = innerdecl;
6385 decl_id = DECL_UID (decl);
6386 var_loc_list **slot
6387 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6388 if (*slot == NULL)
6390 temp = ggc_cleared_alloc<var_loc_list> ();
6391 temp->decl_id = decl_id;
6392 *slot = temp;
6394 else
6395 temp = *slot;
6397 /* For PARM_DECLs try to keep around the original incoming value,
6398 even if that means we'll emit a zero-range .debug_loc entry. */
6399 if (temp->last
6400 && temp->first == temp->last
6401 && TREE_CODE (decl) == PARM_DECL
6402 && NOTE_P (temp->first->loc)
6403 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6404 && DECL_INCOMING_RTL (decl)
6405 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6406 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6407 == GET_CODE (DECL_INCOMING_RTL (decl))
6408 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6409 && (bitsize != -1
6410 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6411 NOTE_VAR_LOCATION_LOC (loc_note))
6412 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6413 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6415 loc = ggc_cleared_alloc<var_loc_node> ();
6416 temp->first->next = loc;
6417 temp->last = loc;
6418 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6420 else if (temp->last)
6422 struct var_loc_node *last = temp->last, *unused = NULL;
6423 rtx *piece_loc = NULL, last_loc_note;
6424 HOST_WIDE_INT piece_bitpos = 0;
6425 if (last->next)
6427 last = last->next;
6428 gcc_assert (last->next == NULL);
6430 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6432 piece_loc = &last->loc;
6435 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6436 if (piece_bitpos + cur_bitsize > bitpos)
6437 break;
6438 piece_bitpos += cur_bitsize;
6439 piece_loc = &XEXP (*piece_loc, 1);
6441 while (*piece_loc);
6443 /* TEMP->LAST here is either pointer to the last but one or
6444 last element in the chained list, LAST is pointer to the
6445 last element. */
6446 if (label && strcmp (last->label, label) == 0 && last->view == view)
6448 /* For SRA optimized variables if there weren't any real
6449 insns since last note, just modify the last node. */
6450 if (piece_loc != NULL)
6452 adjust_piece_list (piece_loc, NULL, NULL,
6453 bitpos, piece_bitpos, bitsize, loc_note);
6454 return NULL;
6456 /* If the last note doesn't cover any instructions, remove it. */
6457 if (temp->last != last)
6459 temp->last->next = NULL;
6460 unused = last;
6461 last = temp->last;
6462 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6464 else
6466 gcc_assert (temp->first == temp->last
6467 || (temp->first->next == temp->last
6468 && TREE_CODE (decl) == PARM_DECL));
6469 memset (temp->last, '\0', sizeof (*temp->last));
6470 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6471 return temp->last;
6474 if (bitsize == -1 && NOTE_P (last->loc))
6475 last_loc_note = last->loc;
6476 else if (piece_loc != NULL
6477 && *piece_loc != NULL_RTX
6478 && piece_bitpos == bitpos
6479 && decl_piece_bitsize (*piece_loc) == bitsize)
6480 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6481 else
6482 last_loc_note = NULL_RTX;
6483 /* If the current location is the same as the end of the list,
6484 and either both or neither of the locations is uninitialized,
6485 we have nothing to do. */
6486 if (last_loc_note == NULL_RTX
6487 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6488 NOTE_VAR_LOCATION_LOC (loc_note)))
6489 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6490 != NOTE_VAR_LOCATION_STATUS (loc_note))
6491 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6492 == VAR_INIT_STATUS_UNINITIALIZED)
6493 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6494 == VAR_INIT_STATUS_UNINITIALIZED))))
6496 /* Add LOC to the end of list and update LAST. If the last
6497 element of the list has been removed above, reuse its
6498 memory for the new node, otherwise allocate a new one. */
6499 if (unused)
6501 loc = unused;
6502 memset (loc, '\0', sizeof (*loc));
6504 else
6505 loc = ggc_cleared_alloc<var_loc_node> ();
6506 if (bitsize == -1 || piece_loc == NULL)
6507 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6508 else
6509 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6510 bitpos, piece_bitpos, bitsize, loc_note);
6511 last->next = loc;
6512 /* Ensure TEMP->LAST will point either to the new last but one
6513 element of the chain, or to the last element in it. */
6514 if (last != temp->last)
6515 temp->last = last;
6517 else if (unused)
6518 ggc_free (unused);
6520 else
6522 loc = ggc_cleared_alloc<var_loc_node> ();
6523 temp->first = loc;
6524 temp->last = loc;
6525 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6527 return loc;
6530 /* Keep track of the number of spaces used to indent the
6531 output of the debugging routines that print the structure of
6532 the DIE internal representation. */
6533 static int print_indent;
6535 /* Indent the line the number of spaces given by print_indent. */
6537 static inline void
6538 print_spaces (FILE *outfile)
6540 fprintf (outfile, "%*s", print_indent, "");
6543 /* Print a type signature in hex. */
6545 static inline void
6546 print_signature (FILE *outfile, char *sig)
6548 int i;
6550 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6551 fprintf (outfile, "%02x", sig[i] & 0xff);
6554 static inline void
6555 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6557 if (discr_value->pos)
6558 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6559 else
6560 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6563 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6565 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6566 RECURSE, output location descriptor operations. */
6568 static void
6569 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6571 switch (val->val_class)
6573 case dw_val_class_addr:
6574 fprintf (outfile, "address");
6575 break;
6576 case dw_val_class_offset:
6577 fprintf (outfile, "offset");
6578 break;
6579 case dw_val_class_loc:
6580 fprintf (outfile, "location descriptor");
6581 if (val->v.val_loc == NULL)
6582 fprintf (outfile, " -> <null>\n");
6583 else if (recurse)
6585 fprintf (outfile, ":\n");
6586 print_indent += 4;
6587 print_loc_descr (val->v.val_loc, outfile);
6588 print_indent -= 4;
6590 else
6592 if (flag_dump_noaddr || flag_dump_unnumbered)
6593 fprintf (outfile, " #\n");
6594 else
6595 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6597 break;
6598 case dw_val_class_loc_list:
6599 fprintf (outfile, "location list -> label:%s",
6600 val->v.val_loc_list->ll_symbol);
6601 break;
6602 case dw_val_class_view_list:
6603 val = view_list_to_loc_list_val_node (val);
6604 fprintf (outfile, "location list with views -> labels:%s and %s",
6605 val->v.val_loc_list->ll_symbol,
6606 val->v.val_loc_list->vl_symbol);
6607 break;
6608 case dw_val_class_range_list:
6609 fprintf (outfile, "range list");
6610 break;
6611 case dw_val_class_const:
6612 case dw_val_class_const_implicit:
6613 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6614 break;
6615 case dw_val_class_unsigned_const:
6616 case dw_val_class_unsigned_const_implicit:
6617 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6618 break;
6619 case dw_val_class_const_double:
6620 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6621 HOST_WIDE_INT_PRINT_UNSIGNED")",
6622 val->v.val_double.high,
6623 val->v.val_double.low);
6624 break;
6625 case dw_val_class_wide_int:
6627 int i = val->v.val_wide->get_len ();
6628 fprintf (outfile, "constant (");
6629 gcc_assert (i > 0);
6630 if (val->v.val_wide->elt (i - 1) == 0)
6631 fprintf (outfile, "0x");
6632 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6633 val->v.val_wide->elt (--i));
6634 while (--i >= 0)
6635 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6636 val->v.val_wide->elt (i));
6637 fprintf (outfile, ")");
6638 break;
6640 case dw_val_class_vec:
6641 fprintf (outfile, "floating-point or vector constant");
6642 break;
6643 case dw_val_class_flag:
6644 fprintf (outfile, "%u", val->v.val_flag);
6645 break;
6646 case dw_val_class_die_ref:
6647 if (val->v.val_die_ref.die != NULL)
6649 dw_die_ref die = val->v.val_die_ref.die;
6651 if (die->comdat_type_p)
6653 fprintf (outfile, "die -> signature: ");
6654 print_signature (outfile,
6655 die->die_id.die_type_node->signature);
6657 else if (die->die_id.die_symbol)
6659 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6660 if (die->with_offset)
6661 fprintf (outfile, " + %ld", die->die_offset);
6663 else
6664 fprintf (outfile, "die -> %ld", die->die_offset);
6665 if (flag_dump_noaddr || flag_dump_unnumbered)
6666 fprintf (outfile, " #");
6667 else
6668 fprintf (outfile, " (%p)", (void *) die);
6670 else
6671 fprintf (outfile, "die -> <null>");
6672 break;
6673 case dw_val_class_vms_delta:
6674 fprintf (outfile, "delta: @slotcount(%s-%s)",
6675 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6676 break;
6677 case dw_val_class_symview:
6678 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6679 break;
6680 case dw_val_class_lbl_id:
6681 case dw_val_class_lineptr:
6682 case dw_val_class_macptr:
6683 case dw_val_class_loclistsptr:
6684 case dw_val_class_high_pc:
6685 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6686 break;
6687 case dw_val_class_str:
6688 if (val->v.val_str->str != NULL)
6689 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6690 else
6691 fprintf (outfile, "<null>");
6692 break;
6693 case dw_val_class_file:
6694 case dw_val_class_file_implicit:
6695 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6696 val->v.val_file->emitted_number);
6697 break;
6698 case dw_val_class_data8:
6700 int i;
6702 for (i = 0; i < 8; i++)
6703 fprintf (outfile, "%02x", val->v.val_data8[i]);
6704 break;
6706 case dw_val_class_discr_value:
6707 print_discr_value (outfile, &val->v.val_discr_value);
6708 break;
6709 case dw_val_class_discr_list:
6710 for (dw_discr_list_ref node = val->v.val_discr_list;
6711 node != NULL;
6712 node = node->dw_discr_next)
6714 if (node->dw_discr_range)
6716 fprintf (outfile, " .. ");
6717 print_discr_value (outfile, &node->dw_discr_lower_bound);
6718 print_discr_value (outfile, &node->dw_discr_upper_bound);
6720 else
6721 print_discr_value (outfile, &node->dw_discr_lower_bound);
6723 if (node->dw_discr_next != NULL)
6724 fprintf (outfile, " | ");
6726 default:
6727 break;
6731 /* Likewise, for a DIE attribute. */
6733 static void
6734 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6736 print_dw_val (&a->dw_attr_val, recurse, outfile);
6740 /* Print the list of operands in the LOC location description to OUTFILE. This
6741 routine is a debugging aid only. */
6743 static void
6744 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6746 dw_loc_descr_ref l = loc;
6748 if (loc == NULL)
6750 print_spaces (outfile);
6751 fprintf (outfile, "<null>\n");
6752 return;
6755 for (l = loc; l != NULL; l = l->dw_loc_next)
6757 print_spaces (outfile);
6758 if (flag_dump_noaddr || flag_dump_unnumbered)
6759 fprintf (outfile, "#");
6760 else
6761 fprintf (outfile, "(%p)", (void *) l);
6762 fprintf (outfile, " %s",
6763 dwarf_stack_op_name (l->dw_loc_opc));
6764 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6766 fprintf (outfile, " ");
6767 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6769 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6771 fprintf (outfile, ", ");
6772 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6774 fprintf (outfile, "\n");
6778 /* Print the information associated with a given DIE, and its children.
6779 This routine is a debugging aid only. */
6781 static void
6782 print_die (dw_die_ref die, FILE *outfile)
6784 dw_attr_node *a;
6785 dw_die_ref c;
6786 unsigned ix;
6788 print_spaces (outfile);
6789 fprintf (outfile, "DIE %4ld: %s ",
6790 die->die_offset, dwarf_tag_name (die->die_tag));
6791 if (flag_dump_noaddr || flag_dump_unnumbered)
6792 fprintf (outfile, "#\n");
6793 else
6794 fprintf (outfile, "(%p)\n", (void*) die);
6795 print_spaces (outfile);
6796 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6797 fprintf (outfile, " offset: %ld", die->die_offset);
6798 fprintf (outfile, " mark: %d\n", die->die_mark);
6800 if (die->comdat_type_p)
6802 print_spaces (outfile);
6803 fprintf (outfile, " signature: ");
6804 print_signature (outfile, die->die_id.die_type_node->signature);
6805 fprintf (outfile, "\n");
6808 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6810 print_spaces (outfile);
6811 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6813 print_attribute (a, true, outfile);
6814 fprintf (outfile, "\n");
6817 if (die->die_child != NULL)
6819 print_indent += 4;
6820 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6821 print_indent -= 4;
6823 if (print_indent == 0)
6824 fprintf (outfile, "\n");
6827 /* Print the list of operations in the LOC location description. */
6829 DEBUG_FUNCTION void
6830 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6832 print_loc_descr (loc, stderr);
6835 /* Print the information collected for a given DIE. */
6837 DEBUG_FUNCTION void
6838 debug_dwarf_die (dw_die_ref die)
6840 print_die (die, stderr);
6843 DEBUG_FUNCTION void
6844 debug (die_struct &ref)
6846 print_die (&ref, stderr);
6849 DEBUG_FUNCTION void
6850 debug (die_struct *ptr)
6852 if (ptr)
6853 debug (*ptr);
6854 else
6855 fprintf (stderr, "<nil>\n");
6859 /* Print all DWARF information collected for the compilation unit.
6860 This routine is a debugging aid only. */
6862 DEBUG_FUNCTION void
6863 debug_dwarf (void)
6865 print_indent = 0;
6866 print_die (comp_unit_die (), stderr);
6869 /* Verify the DIE tree structure. */
6871 DEBUG_FUNCTION void
6872 verify_die (dw_die_ref die)
6874 gcc_assert (!die->die_mark);
6875 if (die->die_parent == NULL
6876 && die->die_sib == NULL)
6877 return;
6878 /* Verify the die_sib list is cyclic. */
6879 dw_die_ref x = die;
6882 x->die_mark = 1;
6883 x = x->die_sib;
6885 while (x && !x->die_mark);
6886 gcc_assert (x == die);
6887 x = die;
6890 /* Verify all dies have the same parent. */
6891 gcc_assert (x->die_parent == die->die_parent);
6892 if (x->die_child)
6894 /* Verify the child has the proper parent and recurse. */
6895 gcc_assert (x->die_child->die_parent == x);
6896 verify_die (x->die_child);
6898 x->die_mark = 0;
6899 x = x->die_sib;
6901 while (x && x->die_mark);
6904 /* Sanity checks on DIEs. */
6906 static void
6907 check_die (dw_die_ref die)
6909 unsigned ix;
6910 dw_attr_node *a;
6911 bool inline_found = false;
6912 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6913 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6914 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6916 switch (a->dw_attr)
6918 case DW_AT_inline:
6919 if (a->dw_attr_val.v.val_unsigned)
6920 inline_found = true;
6921 break;
6922 case DW_AT_location:
6923 ++n_location;
6924 break;
6925 case DW_AT_low_pc:
6926 ++n_low_pc;
6927 break;
6928 case DW_AT_high_pc:
6929 ++n_high_pc;
6930 break;
6931 case DW_AT_artificial:
6932 ++n_artificial;
6933 break;
6934 case DW_AT_decl_column:
6935 ++n_decl_column;
6936 break;
6937 case DW_AT_decl_line:
6938 ++n_decl_line;
6939 break;
6940 case DW_AT_decl_file:
6941 ++n_decl_file;
6942 break;
6943 default:
6944 break;
6947 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6948 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6950 fprintf (stderr, "Duplicate attributes in DIE:\n");
6951 debug_dwarf_die (die);
6952 gcc_unreachable ();
6954 if (inline_found)
6956 /* A debugging information entry that is a member of an abstract
6957 instance tree [that has DW_AT_inline] should not contain any
6958 attributes which describe aspects of the subroutine which vary
6959 between distinct inlined expansions or distinct out-of-line
6960 expansions. */
6961 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6962 gcc_assert (a->dw_attr != DW_AT_low_pc
6963 && a->dw_attr != DW_AT_high_pc
6964 && a->dw_attr != DW_AT_location
6965 && a->dw_attr != DW_AT_frame_base
6966 && a->dw_attr != DW_AT_call_all_calls
6967 && a->dw_attr != DW_AT_GNU_all_call_sites);
6971 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6972 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6973 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6975 /* Calculate the checksum of a location expression. */
6977 static inline void
6978 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6980 int tem;
6981 inchash::hash hstate;
6982 hashval_t hash;
6984 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6985 CHECKSUM (tem);
6986 hash_loc_operands (loc, hstate);
6987 hash = hstate.end();
6988 CHECKSUM (hash);
6991 /* Calculate the checksum of an attribute. */
6993 static void
6994 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6996 dw_loc_descr_ref loc;
6997 rtx r;
6999 CHECKSUM (at->dw_attr);
7001 /* We don't care that this was compiled with a different compiler
7002 snapshot; if the output is the same, that's what matters. */
7003 if (at->dw_attr == DW_AT_producer)
7004 return;
7006 switch (AT_class (at))
7008 case dw_val_class_const:
7009 case dw_val_class_const_implicit:
7010 CHECKSUM (at->dw_attr_val.v.val_int);
7011 break;
7012 case dw_val_class_unsigned_const:
7013 case dw_val_class_unsigned_const_implicit:
7014 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7015 break;
7016 case dw_val_class_const_double:
7017 CHECKSUM (at->dw_attr_val.v.val_double);
7018 break;
7019 case dw_val_class_wide_int:
7020 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7021 get_full_len (*at->dw_attr_val.v.val_wide)
7022 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7023 break;
7024 case dw_val_class_vec:
7025 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7026 (at->dw_attr_val.v.val_vec.length
7027 * at->dw_attr_val.v.val_vec.elt_size));
7028 break;
7029 case dw_val_class_flag:
7030 CHECKSUM (at->dw_attr_val.v.val_flag);
7031 break;
7032 case dw_val_class_str:
7033 CHECKSUM_STRING (AT_string (at));
7034 break;
7036 case dw_val_class_addr:
7037 r = AT_addr (at);
7038 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7039 CHECKSUM_STRING (XSTR (r, 0));
7040 break;
7042 case dw_val_class_offset:
7043 CHECKSUM (at->dw_attr_val.v.val_offset);
7044 break;
7046 case dw_val_class_loc:
7047 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7048 loc_checksum (loc, ctx);
7049 break;
7051 case dw_val_class_die_ref:
7052 die_checksum (AT_ref (at), ctx, mark);
7053 break;
7055 case dw_val_class_fde_ref:
7056 case dw_val_class_vms_delta:
7057 case dw_val_class_symview:
7058 case dw_val_class_lbl_id:
7059 case dw_val_class_lineptr:
7060 case dw_val_class_macptr:
7061 case dw_val_class_loclistsptr:
7062 case dw_val_class_high_pc:
7063 break;
7065 case dw_val_class_file:
7066 case dw_val_class_file_implicit:
7067 CHECKSUM_STRING (AT_file (at)->filename);
7068 break;
7070 case dw_val_class_data8:
7071 CHECKSUM (at->dw_attr_val.v.val_data8);
7072 break;
7074 default:
7075 break;
7079 /* Calculate the checksum of a DIE. */
7081 static void
7082 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7084 dw_die_ref c;
7085 dw_attr_node *a;
7086 unsigned ix;
7088 /* To avoid infinite recursion. */
7089 if (die->die_mark)
7091 CHECKSUM (die->die_mark);
7092 return;
7094 die->die_mark = ++(*mark);
7096 CHECKSUM (die->die_tag);
7098 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7099 attr_checksum (a, ctx, mark);
7101 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7104 #undef CHECKSUM
7105 #undef CHECKSUM_BLOCK
7106 #undef CHECKSUM_STRING
7108 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7109 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7110 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7111 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7112 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7113 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7114 #define CHECKSUM_ATTR(FOO) \
7115 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7117 /* Calculate the checksum of a number in signed LEB128 format. */
7119 static void
7120 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7122 unsigned char byte;
7123 bool more;
7125 while (1)
7127 byte = (value & 0x7f);
7128 value >>= 7;
7129 more = !((value == 0 && (byte & 0x40) == 0)
7130 || (value == -1 && (byte & 0x40) != 0));
7131 if (more)
7132 byte |= 0x80;
7133 CHECKSUM (byte);
7134 if (!more)
7135 break;
7139 /* Calculate the checksum of a number in unsigned LEB128 format. */
7141 static void
7142 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7144 while (1)
7146 unsigned char byte = (value & 0x7f);
7147 value >>= 7;
7148 if (value != 0)
7149 /* More bytes to follow. */
7150 byte |= 0x80;
7151 CHECKSUM (byte);
7152 if (value == 0)
7153 break;
7157 /* Checksum the context of the DIE. This adds the names of any
7158 surrounding namespaces or structures to the checksum. */
7160 static void
7161 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7163 const char *name;
7164 dw_die_ref spec;
7165 int tag = die->die_tag;
7167 if (tag != DW_TAG_namespace
7168 && tag != DW_TAG_structure_type
7169 && tag != DW_TAG_class_type)
7170 return;
7172 name = get_AT_string (die, DW_AT_name);
7174 spec = get_AT_ref (die, DW_AT_specification);
7175 if (spec != NULL)
7176 die = spec;
7178 if (die->die_parent != NULL)
7179 checksum_die_context (die->die_parent, ctx);
7181 CHECKSUM_ULEB128 ('C');
7182 CHECKSUM_ULEB128 (tag);
7183 if (name != NULL)
7184 CHECKSUM_STRING (name);
7187 /* Calculate the checksum of a location expression. */
7189 static inline void
7190 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7192 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7193 were emitted as a DW_FORM_sdata instead of a location expression. */
7194 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7196 CHECKSUM_ULEB128 (DW_FORM_sdata);
7197 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7198 return;
7201 /* Otherwise, just checksum the raw location expression. */
7202 while (loc != NULL)
7204 inchash::hash hstate;
7205 hashval_t hash;
7207 CHECKSUM_ULEB128 (loc->dtprel);
7208 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7209 hash_loc_operands (loc, hstate);
7210 hash = hstate.end ();
7211 CHECKSUM (hash);
7212 loc = loc->dw_loc_next;
7216 /* Calculate the checksum of an attribute. */
7218 static void
7219 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7220 struct md5_ctx *ctx, int *mark)
7222 dw_loc_descr_ref loc;
7223 rtx r;
7225 if (AT_class (at) == dw_val_class_die_ref)
7227 dw_die_ref target_die = AT_ref (at);
7229 /* For pointer and reference types, we checksum only the (qualified)
7230 name of the target type (if there is a name). For friend entries,
7231 we checksum only the (qualified) name of the target type or function.
7232 This allows the checksum to remain the same whether the target type
7233 is complete or not. */
7234 if ((at->dw_attr == DW_AT_type
7235 && (tag == DW_TAG_pointer_type
7236 || tag == DW_TAG_reference_type
7237 || tag == DW_TAG_rvalue_reference_type
7238 || tag == DW_TAG_ptr_to_member_type))
7239 || (at->dw_attr == DW_AT_friend
7240 && tag == DW_TAG_friend))
7242 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7244 if (name_attr != NULL)
7246 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7248 if (decl == NULL)
7249 decl = target_die;
7250 CHECKSUM_ULEB128 ('N');
7251 CHECKSUM_ULEB128 (at->dw_attr);
7252 if (decl->die_parent != NULL)
7253 checksum_die_context (decl->die_parent, ctx);
7254 CHECKSUM_ULEB128 ('E');
7255 CHECKSUM_STRING (AT_string (name_attr));
7256 return;
7260 /* For all other references to another DIE, we check to see if the
7261 target DIE has already been visited. If it has, we emit a
7262 backward reference; if not, we descend recursively. */
7263 if (target_die->die_mark > 0)
7265 CHECKSUM_ULEB128 ('R');
7266 CHECKSUM_ULEB128 (at->dw_attr);
7267 CHECKSUM_ULEB128 (target_die->die_mark);
7269 else
7271 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7273 if (decl == NULL)
7274 decl = target_die;
7275 target_die->die_mark = ++(*mark);
7276 CHECKSUM_ULEB128 ('T');
7277 CHECKSUM_ULEB128 (at->dw_attr);
7278 if (decl->die_parent != NULL)
7279 checksum_die_context (decl->die_parent, ctx);
7280 die_checksum_ordered (target_die, ctx, mark);
7282 return;
7285 CHECKSUM_ULEB128 ('A');
7286 CHECKSUM_ULEB128 (at->dw_attr);
7288 switch (AT_class (at))
7290 case dw_val_class_const:
7291 case dw_val_class_const_implicit:
7292 CHECKSUM_ULEB128 (DW_FORM_sdata);
7293 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7294 break;
7296 case dw_val_class_unsigned_const:
7297 case dw_val_class_unsigned_const_implicit:
7298 CHECKSUM_ULEB128 (DW_FORM_sdata);
7299 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7300 break;
7302 case dw_val_class_const_double:
7303 CHECKSUM_ULEB128 (DW_FORM_block);
7304 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7305 CHECKSUM (at->dw_attr_val.v.val_double);
7306 break;
7308 case dw_val_class_wide_int:
7309 CHECKSUM_ULEB128 (DW_FORM_block);
7310 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7311 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7312 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7313 get_full_len (*at->dw_attr_val.v.val_wide)
7314 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7315 break;
7317 case dw_val_class_vec:
7318 CHECKSUM_ULEB128 (DW_FORM_block);
7319 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7320 * at->dw_attr_val.v.val_vec.elt_size);
7321 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7322 (at->dw_attr_val.v.val_vec.length
7323 * at->dw_attr_val.v.val_vec.elt_size));
7324 break;
7326 case dw_val_class_flag:
7327 CHECKSUM_ULEB128 (DW_FORM_flag);
7328 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7329 break;
7331 case dw_val_class_str:
7332 CHECKSUM_ULEB128 (DW_FORM_string);
7333 CHECKSUM_STRING (AT_string (at));
7334 break;
7336 case dw_val_class_addr:
7337 r = AT_addr (at);
7338 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7339 CHECKSUM_ULEB128 (DW_FORM_string);
7340 CHECKSUM_STRING (XSTR (r, 0));
7341 break;
7343 case dw_val_class_offset:
7344 CHECKSUM_ULEB128 (DW_FORM_sdata);
7345 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7346 break;
7348 case dw_val_class_loc:
7349 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7350 loc_checksum_ordered (loc, ctx);
7351 break;
7353 case dw_val_class_fde_ref:
7354 case dw_val_class_symview:
7355 case dw_val_class_lbl_id:
7356 case dw_val_class_lineptr:
7357 case dw_val_class_macptr:
7358 case dw_val_class_loclistsptr:
7359 case dw_val_class_high_pc:
7360 break;
7362 case dw_val_class_file:
7363 case dw_val_class_file_implicit:
7364 CHECKSUM_ULEB128 (DW_FORM_string);
7365 CHECKSUM_STRING (AT_file (at)->filename);
7366 break;
7368 case dw_val_class_data8:
7369 CHECKSUM (at->dw_attr_val.v.val_data8);
7370 break;
7372 default:
7373 break;
7377 struct checksum_attributes
7379 dw_attr_node *at_name;
7380 dw_attr_node *at_type;
7381 dw_attr_node *at_friend;
7382 dw_attr_node *at_accessibility;
7383 dw_attr_node *at_address_class;
7384 dw_attr_node *at_alignment;
7385 dw_attr_node *at_allocated;
7386 dw_attr_node *at_artificial;
7387 dw_attr_node *at_associated;
7388 dw_attr_node *at_binary_scale;
7389 dw_attr_node *at_bit_offset;
7390 dw_attr_node *at_bit_size;
7391 dw_attr_node *at_bit_stride;
7392 dw_attr_node *at_byte_size;
7393 dw_attr_node *at_byte_stride;
7394 dw_attr_node *at_const_value;
7395 dw_attr_node *at_containing_type;
7396 dw_attr_node *at_count;
7397 dw_attr_node *at_data_location;
7398 dw_attr_node *at_data_member_location;
7399 dw_attr_node *at_decimal_scale;
7400 dw_attr_node *at_decimal_sign;
7401 dw_attr_node *at_default_value;
7402 dw_attr_node *at_digit_count;
7403 dw_attr_node *at_discr;
7404 dw_attr_node *at_discr_list;
7405 dw_attr_node *at_discr_value;
7406 dw_attr_node *at_encoding;
7407 dw_attr_node *at_endianity;
7408 dw_attr_node *at_explicit;
7409 dw_attr_node *at_is_optional;
7410 dw_attr_node *at_location;
7411 dw_attr_node *at_lower_bound;
7412 dw_attr_node *at_mutable;
7413 dw_attr_node *at_ordering;
7414 dw_attr_node *at_picture_string;
7415 dw_attr_node *at_prototyped;
7416 dw_attr_node *at_small;
7417 dw_attr_node *at_segment;
7418 dw_attr_node *at_string_length;
7419 dw_attr_node *at_string_length_bit_size;
7420 dw_attr_node *at_string_length_byte_size;
7421 dw_attr_node *at_threads_scaled;
7422 dw_attr_node *at_upper_bound;
7423 dw_attr_node *at_use_location;
7424 dw_attr_node *at_use_UTF8;
7425 dw_attr_node *at_variable_parameter;
7426 dw_attr_node *at_virtuality;
7427 dw_attr_node *at_visibility;
7428 dw_attr_node *at_vtable_elem_location;
7431 /* Collect the attributes that we will want to use for the checksum. */
7433 static void
7434 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7436 dw_attr_node *a;
7437 unsigned ix;
7439 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7441 switch (a->dw_attr)
7443 case DW_AT_name:
7444 attrs->at_name = a;
7445 break;
7446 case DW_AT_type:
7447 attrs->at_type = a;
7448 break;
7449 case DW_AT_friend:
7450 attrs->at_friend = a;
7451 break;
7452 case DW_AT_accessibility:
7453 attrs->at_accessibility = a;
7454 break;
7455 case DW_AT_address_class:
7456 attrs->at_address_class = a;
7457 break;
7458 case DW_AT_alignment:
7459 attrs->at_alignment = a;
7460 break;
7461 case DW_AT_allocated:
7462 attrs->at_allocated = a;
7463 break;
7464 case DW_AT_artificial:
7465 attrs->at_artificial = a;
7466 break;
7467 case DW_AT_associated:
7468 attrs->at_associated = a;
7469 break;
7470 case DW_AT_binary_scale:
7471 attrs->at_binary_scale = a;
7472 break;
7473 case DW_AT_bit_offset:
7474 attrs->at_bit_offset = a;
7475 break;
7476 case DW_AT_bit_size:
7477 attrs->at_bit_size = a;
7478 break;
7479 case DW_AT_bit_stride:
7480 attrs->at_bit_stride = a;
7481 break;
7482 case DW_AT_byte_size:
7483 attrs->at_byte_size = a;
7484 break;
7485 case DW_AT_byte_stride:
7486 attrs->at_byte_stride = a;
7487 break;
7488 case DW_AT_const_value:
7489 attrs->at_const_value = a;
7490 break;
7491 case DW_AT_containing_type:
7492 attrs->at_containing_type = a;
7493 break;
7494 case DW_AT_count:
7495 attrs->at_count = a;
7496 break;
7497 case DW_AT_data_location:
7498 attrs->at_data_location = a;
7499 break;
7500 case DW_AT_data_member_location:
7501 attrs->at_data_member_location = a;
7502 break;
7503 case DW_AT_decimal_scale:
7504 attrs->at_decimal_scale = a;
7505 break;
7506 case DW_AT_decimal_sign:
7507 attrs->at_decimal_sign = a;
7508 break;
7509 case DW_AT_default_value:
7510 attrs->at_default_value = a;
7511 break;
7512 case DW_AT_digit_count:
7513 attrs->at_digit_count = a;
7514 break;
7515 case DW_AT_discr:
7516 attrs->at_discr = a;
7517 break;
7518 case DW_AT_discr_list:
7519 attrs->at_discr_list = a;
7520 break;
7521 case DW_AT_discr_value:
7522 attrs->at_discr_value = a;
7523 break;
7524 case DW_AT_encoding:
7525 attrs->at_encoding = a;
7526 break;
7527 case DW_AT_endianity:
7528 attrs->at_endianity = a;
7529 break;
7530 case DW_AT_explicit:
7531 attrs->at_explicit = a;
7532 break;
7533 case DW_AT_is_optional:
7534 attrs->at_is_optional = a;
7535 break;
7536 case DW_AT_location:
7537 attrs->at_location = a;
7538 break;
7539 case DW_AT_lower_bound:
7540 attrs->at_lower_bound = a;
7541 break;
7542 case DW_AT_mutable:
7543 attrs->at_mutable = a;
7544 break;
7545 case DW_AT_ordering:
7546 attrs->at_ordering = a;
7547 break;
7548 case DW_AT_picture_string:
7549 attrs->at_picture_string = a;
7550 break;
7551 case DW_AT_prototyped:
7552 attrs->at_prototyped = a;
7553 break;
7554 case DW_AT_small:
7555 attrs->at_small = a;
7556 break;
7557 case DW_AT_segment:
7558 attrs->at_segment = a;
7559 break;
7560 case DW_AT_string_length:
7561 attrs->at_string_length = a;
7562 break;
7563 case DW_AT_string_length_bit_size:
7564 attrs->at_string_length_bit_size = a;
7565 break;
7566 case DW_AT_string_length_byte_size:
7567 attrs->at_string_length_byte_size = a;
7568 break;
7569 case DW_AT_threads_scaled:
7570 attrs->at_threads_scaled = a;
7571 break;
7572 case DW_AT_upper_bound:
7573 attrs->at_upper_bound = a;
7574 break;
7575 case DW_AT_use_location:
7576 attrs->at_use_location = a;
7577 break;
7578 case DW_AT_use_UTF8:
7579 attrs->at_use_UTF8 = a;
7580 break;
7581 case DW_AT_variable_parameter:
7582 attrs->at_variable_parameter = a;
7583 break;
7584 case DW_AT_virtuality:
7585 attrs->at_virtuality = a;
7586 break;
7587 case DW_AT_visibility:
7588 attrs->at_visibility = a;
7589 break;
7590 case DW_AT_vtable_elem_location:
7591 attrs->at_vtable_elem_location = a;
7592 break;
7593 default:
7594 break;
7599 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7601 static void
7602 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7604 dw_die_ref c;
7605 dw_die_ref decl;
7606 struct checksum_attributes attrs;
7608 CHECKSUM_ULEB128 ('D');
7609 CHECKSUM_ULEB128 (die->die_tag);
7611 memset (&attrs, 0, sizeof (attrs));
7613 decl = get_AT_ref (die, DW_AT_specification);
7614 if (decl != NULL)
7615 collect_checksum_attributes (&attrs, decl);
7616 collect_checksum_attributes (&attrs, die);
7618 CHECKSUM_ATTR (attrs.at_name);
7619 CHECKSUM_ATTR (attrs.at_accessibility);
7620 CHECKSUM_ATTR (attrs.at_address_class);
7621 CHECKSUM_ATTR (attrs.at_allocated);
7622 CHECKSUM_ATTR (attrs.at_artificial);
7623 CHECKSUM_ATTR (attrs.at_associated);
7624 CHECKSUM_ATTR (attrs.at_binary_scale);
7625 CHECKSUM_ATTR (attrs.at_bit_offset);
7626 CHECKSUM_ATTR (attrs.at_bit_size);
7627 CHECKSUM_ATTR (attrs.at_bit_stride);
7628 CHECKSUM_ATTR (attrs.at_byte_size);
7629 CHECKSUM_ATTR (attrs.at_byte_stride);
7630 CHECKSUM_ATTR (attrs.at_const_value);
7631 CHECKSUM_ATTR (attrs.at_containing_type);
7632 CHECKSUM_ATTR (attrs.at_count);
7633 CHECKSUM_ATTR (attrs.at_data_location);
7634 CHECKSUM_ATTR (attrs.at_data_member_location);
7635 CHECKSUM_ATTR (attrs.at_decimal_scale);
7636 CHECKSUM_ATTR (attrs.at_decimal_sign);
7637 CHECKSUM_ATTR (attrs.at_default_value);
7638 CHECKSUM_ATTR (attrs.at_digit_count);
7639 CHECKSUM_ATTR (attrs.at_discr);
7640 CHECKSUM_ATTR (attrs.at_discr_list);
7641 CHECKSUM_ATTR (attrs.at_discr_value);
7642 CHECKSUM_ATTR (attrs.at_encoding);
7643 CHECKSUM_ATTR (attrs.at_endianity);
7644 CHECKSUM_ATTR (attrs.at_explicit);
7645 CHECKSUM_ATTR (attrs.at_is_optional);
7646 CHECKSUM_ATTR (attrs.at_location);
7647 CHECKSUM_ATTR (attrs.at_lower_bound);
7648 CHECKSUM_ATTR (attrs.at_mutable);
7649 CHECKSUM_ATTR (attrs.at_ordering);
7650 CHECKSUM_ATTR (attrs.at_picture_string);
7651 CHECKSUM_ATTR (attrs.at_prototyped);
7652 CHECKSUM_ATTR (attrs.at_small);
7653 CHECKSUM_ATTR (attrs.at_segment);
7654 CHECKSUM_ATTR (attrs.at_string_length);
7655 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7656 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7657 CHECKSUM_ATTR (attrs.at_threads_scaled);
7658 CHECKSUM_ATTR (attrs.at_upper_bound);
7659 CHECKSUM_ATTR (attrs.at_use_location);
7660 CHECKSUM_ATTR (attrs.at_use_UTF8);
7661 CHECKSUM_ATTR (attrs.at_variable_parameter);
7662 CHECKSUM_ATTR (attrs.at_virtuality);
7663 CHECKSUM_ATTR (attrs.at_visibility);
7664 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7665 CHECKSUM_ATTR (attrs.at_type);
7666 CHECKSUM_ATTR (attrs.at_friend);
7667 CHECKSUM_ATTR (attrs.at_alignment);
7669 /* Checksum the child DIEs. */
7670 c = die->die_child;
7671 if (c) do {
7672 dw_attr_node *name_attr;
7674 c = c->die_sib;
7675 name_attr = get_AT (c, DW_AT_name);
7676 if (is_template_instantiation (c))
7678 /* Ignore instantiations of member type and function templates. */
7680 else if (name_attr != NULL
7681 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7683 /* Use a shallow checksum for named nested types and member
7684 functions. */
7685 CHECKSUM_ULEB128 ('S');
7686 CHECKSUM_ULEB128 (c->die_tag);
7687 CHECKSUM_STRING (AT_string (name_attr));
7689 else
7691 /* Use a deep checksum for other children. */
7692 /* Mark this DIE so it gets processed when unmarking. */
7693 if (c->die_mark == 0)
7694 c->die_mark = -1;
7695 die_checksum_ordered (c, ctx, mark);
7697 } while (c != die->die_child);
7699 CHECKSUM_ULEB128 (0);
7702 /* Add a type name and tag to a hash. */
7703 static void
7704 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7706 CHECKSUM_ULEB128 (tag);
7707 CHECKSUM_STRING (name);
7710 #undef CHECKSUM
7711 #undef CHECKSUM_STRING
7712 #undef CHECKSUM_ATTR
7713 #undef CHECKSUM_LEB128
7714 #undef CHECKSUM_ULEB128
7716 /* Generate the type signature for DIE. This is computed by generating an
7717 MD5 checksum over the DIE's tag, its relevant attributes, and its
7718 children. Attributes that are references to other DIEs are processed
7719 by recursion, using the MARK field to prevent infinite recursion.
7720 If the DIE is nested inside a namespace or another type, we also
7721 need to include that context in the signature. The lower 64 bits
7722 of the resulting MD5 checksum comprise the signature. */
7724 static void
7725 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7727 int mark;
7728 const char *name;
7729 unsigned char checksum[16];
7730 struct md5_ctx ctx;
7731 dw_die_ref decl;
7732 dw_die_ref parent;
7734 name = get_AT_string (die, DW_AT_name);
7735 decl = get_AT_ref (die, DW_AT_specification);
7736 parent = get_die_parent (die);
7738 /* First, compute a signature for just the type name (and its surrounding
7739 context, if any. This is stored in the type unit DIE for link-time
7740 ODR (one-definition rule) checking. */
7742 if (is_cxx () && name != NULL)
7744 md5_init_ctx (&ctx);
7746 /* Checksum the names of surrounding namespaces and structures. */
7747 if (parent != NULL)
7748 checksum_die_context (parent, &ctx);
7750 /* Checksum the current DIE. */
7751 die_odr_checksum (die->die_tag, name, &ctx);
7752 md5_finish_ctx (&ctx, checksum);
7754 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7757 /* Next, compute the complete type signature. */
7759 md5_init_ctx (&ctx);
7760 mark = 1;
7761 die->die_mark = mark;
7763 /* Checksum the names of surrounding namespaces and structures. */
7764 if (parent != NULL)
7765 checksum_die_context (parent, &ctx);
7767 /* Checksum the DIE and its children. */
7768 die_checksum_ordered (die, &ctx, &mark);
7769 unmark_all_dies (die);
7770 md5_finish_ctx (&ctx, checksum);
7772 /* Store the signature in the type node and link the type DIE and the
7773 type node together. */
7774 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7775 DWARF_TYPE_SIGNATURE_SIZE);
7776 die->comdat_type_p = true;
7777 die->die_id.die_type_node = type_node;
7778 type_node->type_die = die;
7780 /* If the DIE is a specification, link its declaration to the type node
7781 as well. */
7782 if (decl != NULL)
7784 decl->comdat_type_p = true;
7785 decl->die_id.die_type_node = type_node;
7789 /* Do the location expressions look same? */
7790 static inline int
7791 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7793 return loc1->dw_loc_opc == loc2->dw_loc_opc
7794 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7795 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7798 /* Do the values look the same? */
7799 static int
7800 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7802 dw_loc_descr_ref loc1, loc2;
7803 rtx r1, r2;
7805 if (v1->val_class != v2->val_class)
7806 return 0;
7808 switch (v1->val_class)
7810 case dw_val_class_const:
7811 case dw_val_class_const_implicit:
7812 return v1->v.val_int == v2->v.val_int;
7813 case dw_val_class_unsigned_const:
7814 case dw_val_class_unsigned_const_implicit:
7815 return v1->v.val_unsigned == v2->v.val_unsigned;
7816 case dw_val_class_const_double:
7817 return v1->v.val_double.high == v2->v.val_double.high
7818 && v1->v.val_double.low == v2->v.val_double.low;
7819 case dw_val_class_wide_int:
7820 return *v1->v.val_wide == *v2->v.val_wide;
7821 case dw_val_class_vec:
7822 if (v1->v.val_vec.length != v2->v.val_vec.length
7823 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7824 return 0;
7825 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7826 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7827 return 0;
7828 return 1;
7829 case dw_val_class_flag:
7830 return v1->v.val_flag == v2->v.val_flag;
7831 case dw_val_class_str:
7832 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7834 case dw_val_class_addr:
7835 r1 = v1->v.val_addr;
7836 r2 = v2->v.val_addr;
7837 if (GET_CODE (r1) != GET_CODE (r2))
7838 return 0;
7839 return !rtx_equal_p (r1, r2);
7841 case dw_val_class_offset:
7842 return v1->v.val_offset == v2->v.val_offset;
7844 case dw_val_class_loc:
7845 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7846 loc1 && loc2;
7847 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7848 if (!same_loc_p (loc1, loc2, mark))
7849 return 0;
7850 return !loc1 && !loc2;
7852 case dw_val_class_die_ref:
7853 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7855 case dw_val_class_symview:
7856 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7858 case dw_val_class_fde_ref:
7859 case dw_val_class_vms_delta:
7860 case dw_val_class_lbl_id:
7861 case dw_val_class_lineptr:
7862 case dw_val_class_macptr:
7863 case dw_val_class_loclistsptr:
7864 case dw_val_class_high_pc:
7865 return 1;
7867 case dw_val_class_file:
7868 case dw_val_class_file_implicit:
7869 return v1->v.val_file == v2->v.val_file;
7871 case dw_val_class_data8:
7872 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7874 default:
7875 return 1;
7879 /* Do the attributes look the same? */
7881 static int
7882 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7884 if (at1->dw_attr != at2->dw_attr)
7885 return 0;
7887 /* We don't care that this was compiled with a different compiler
7888 snapshot; if the output is the same, that's what matters. */
7889 if (at1->dw_attr == DW_AT_producer)
7890 return 1;
7892 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7895 /* Do the dies look the same? */
7897 static int
7898 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7900 dw_die_ref c1, c2;
7901 dw_attr_node *a1;
7902 unsigned ix;
7904 /* To avoid infinite recursion. */
7905 if (die1->die_mark)
7906 return die1->die_mark == die2->die_mark;
7907 die1->die_mark = die2->die_mark = ++(*mark);
7909 if (die1->die_tag != die2->die_tag)
7910 return 0;
7912 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7913 return 0;
7915 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7916 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7917 return 0;
7919 c1 = die1->die_child;
7920 c2 = die2->die_child;
7921 if (! c1)
7923 if (c2)
7924 return 0;
7926 else
7927 for (;;)
7929 if (!same_die_p (c1, c2, mark))
7930 return 0;
7931 c1 = c1->die_sib;
7932 c2 = c2->die_sib;
7933 if (c1 == die1->die_child)
7935 if (c2 == die2->die_child)
7936 break;
7937 else
7938 return 0;
7942 return 1;
7945 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7946 children, and set die_symbol. */
7948 static void
7949 compute_comp_unit_symbol (dw_die_ref unit_die)
7951 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7952 const char *base = die_name ? lbasename (die_name) : "anonymous";
7953 char *name = XALLOCAVEC (char, strlen (base) + 64);
7954 char *p;
7955 int i, mark;
7956 unsigned char checksum[16];
7957 struct md5_ctx ctx;
7959 /* Compute the checksum of the DIE, then append part of it as hex digits to
7960 the name filename of the unit. */
7962 md5_init_ctx (&ctx);
7963 mark = 0;
7964 die_checksum (unit_die, &ctx, &mark);
7965 unmark_all_dies (unit_die);
7966 md5_finish_ctx (&ctx, checksum);
7968 /* When we this for comp_unit_die () we have a DW_AT_name that might
7969 not start with a letter but with anything valid for filenames and
7970 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7971 character is not a letter. */
7972 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7973 clean_symbol_name (name);
7975 p = name + strlen (name);
7976 for (i = 0; i < 4; i++)
7978 sprintf (p, "%.2x", checksum[i]);
7979 p += 2;
7982 unit_die->die_id.die_symbol = xstrdup (name);
7985 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7987 static int
7988 is_type_die (dw_die_ref die)
7990 switch (die->die_tag)
7992 case DW_TAG_array_type:
7993 case DW_TAG_class_type:
7994 case DW_TAG_interface_type:
7995 case DW_TAG_enumeration_type:
7996 case DW_TAG_pointer_type:
7997 case DW_TAG_reference_type:
7998 case DW_TAG_rvalue_reference_type:
7999 case DW_TAG_string_type:
8000 case DW_TAG_structure_type:
8001 case DW_TAG_subroutine_type:
8002 case DW_TAG_union_type:
8003 case DW_TAG_ptr_to_member_type:
8004 case DW_TAG_set_type:
8005 case DW_TAG_subrange_type:
8006 case DW_TAG_base_type:
8007 case DW_TAG_const_type:
8008 case DW_TAG_file_type:
8009 case DW_TAG_packed_type:
8010 case DW_TAG_volatile_type:
8011 case DW_TAG_typedef:
8012 return 1;
8013 default:
8014 return 0;
8018 /* Returns true iff C is a compile-unit DIE. */
8020 static inline bool
8021 is_cu_die (dw_die_ref c)
8023 return c && (c->die_tag == DW_TAG_compile_unit
8024 || c->die_tag == DW_TAG_skeleton_unit);
8027 /* Returns true iff C is a unit DIE of some sort. */
8029 static inline bool
8030 is_unit_die (dw_die_ref c)
8032 return c && (c->die_tag == DW_TAG_compile_unit
8033 || c->die_tag == DW_TAG_partial_unit
8034 || c->die_tag == DW_TAG_type_unit
8035 || c->die_tag == DW_TAG_skeleton_unit);
8038 /* Returns true iff C is a namespace DIE. */
8040 static inline bool
8041 is_namespace_die (dw_die_ref c)
8043 return c && c->die_tag == DW_TAG_namespace;
8046 /* Return non-zero if this DIE is a template parameter. */
8048 static inline bool
8049 is_template_parameter (dw_die_ref die)
8051 switch (die->die_tag)
8053 case DW_TAG_template_type_param:
8054 case DW_TAG_template_value_param:
8055 case DW_TAG_GNU_template_template_param:
8056 case DW_TAG_GNU_template_parameter_pack:
8057 return true;
8058 default:
8059 return false;
8063 /* Return non-zero if this DIE represents a template instantiation. */
8065 static inline bool
8066 is_template_instantiation (dw_die_ref die)
8068 dw_die_ref c;
8070 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8071 return false;
8072 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8073 return false;
8076 static char *
8077 gen_internal_sym (const char *prefix)
8079 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8081 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8082 return xstrdup (buf);
8085 /* Return non-zero if this DIE is a declaration. */
8087 static int
8088 is_declaration_die (dw_die_ref die)
8090 dw_attr_node *a;
8091 unsigned ix;
8093 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8094 if (a->dw_attr == DW_AT_declaration)
8095 return 1;
8097 return 0;
8100 /* Return non-zero if this DIE is nested inside a subprogram. */
8102 static int
8103 is_nested_in_subprogram (dw_die_ref die)
8105 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8107 if (decl == NULL)
8108 decl = die;
8109 return local_scope_p (decl);
8112 /* Return non-zero if this DIE contains a defining declaration of a
8113 subprogram. */
8115 static int
8116 contains_subprogram_definition (dw_die_ref die)
8118 dw_die_ref c;
8120 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8121 return 1;
8122 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8123 return 0;
8126 /* Return non-zero if this is a type DIE that should be moved to a
8127 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8128 unit type. */
8130 static int
8131 should_move_die_to_comdat (dw_die_ref die)
8133 switch (die->die_tag)
8135 case DW_TAG_class_type:
8136 case DW_TAG_structure_type:
8137 case DW_TAG_enumeration_type:
8138 case DW_TAG_union_type:
8139 /* Don't move declarations, inlined instances, types nested in a
8140 subprogram, or types that contain subprogram definitions. */
8141 if (is_declaration_die (die)
8142 || get_AT (die, DW_AT_abstract_origin)
8143 || is_nested_in_subprogram (die)
8144 || contains_subprogram_definition (die))
8145 return 0;
8146 return 1;
8147 case DW_TAG_array_type:
8148 case DW_TAG_interface_type:
8149 case DW_TAG_pointer_type:
8150 case DW_TAG_reference_type:
8151 case DW_TAG_rvalue_reference_type:
8152 case DW_TAG_string_type:
8153 case DW_TAG_subroutine_type:
8154 case DW_TAG_ptr_to_member_type:
8155 case DW_TAG_set_type:
8156 case DW_TAG_subrange_type:
8157 case DW_TAG_base_type:
8158 case DW_TAG_const_type:
8159 case DW_TAG_file_type:
8160 case DW_TAG_packed_type:
8161 case DW_TAG_volatile_type:
8162 case DW_TAG_typedef:
8163 default:
8164 return 0;
8168 /* Make a clone of DIE. */
8170 static dw_die_ref
8171 clone_die (dw_die_ref die)
8173 dw_die_ref clone = new_die_raw (die->die_tag);
8174 dw_attr_node *a;
8175 unsigned ix;
8177 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8178 add_dwarf_attr (clone, a);
8180 return clone;
8183 /* Make a clone of the tree rooted at DIE. */
8185 static dw_die_ref
8186 clone_tree (dw_die_ref die)
8188 dw_die_ref c;
8189 dw_die_ref clone = clone_die (die);
8191 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8193 return clone;
8196 /* Make a clone of DIE as a declaration. */
8198 static dw_die_ref
8199 clone_as_declaration (dw_die_ref die)
8201 dw_die_ref clone;
8202 dw_die_ref decl;
8203 dw_attr_node *a;
8204 unsigned ix;
8206 /* If the DIE is already a declaration, just clone it. */
8207 if (is_declaration_die (die))
8208 return clone_die (die);
8210 /* If the DIE is a specification, just clone its declaration DIE. */
8211 decl = get_AT_ref (die, DW_AT_specification);
8212 if (decl != NULL)
8214 clone = clone_die (decl);
8215 if (die->comdat_type_p)
8216 add_AT_die_ref (clone, DW_AT_signature, die);
8217 return clone;
8220 clone = new_die_raw (die->die_tag);
8222 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8224 /* We don't want to copy over all attributes.
8225 For example we don't want DW_AT_byte_size because otherwise we will no
8226 longer have a declaration and GDB will treat it as a definition. */
8228 switch (a->dw_attr)
8230 case DW_AT_abstract_origin:
8231 case DW_AT_artificial:
8232 case DW_AT_containing_type:
8233 case DW_AT_external:
8234 case DW_AT_name:
8235 case DW_AT_type:
8236 case DW_AT_virtuality:
8237 case DW_AT_linkage_name:
8238 case DW_AT_MIPS_linkage_name:
8239 add_dwarf_attr (clone, a);
8240 break;
8241 case DW_AT_byte_size:
8242 case DW_AT_alignment:
8243 default:
8244 break;
8248 if (die->comdat_type_p)
8249 add_AT_die_ref (clone, DW_AT_signature, die);
8251 add_AT_flag (clone, DW_AT_declaration, 1);
8252 return clone;
8256 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8258 struct decl_table_entry
8260 dw_die_ref orig;
8261 dw_die_ref copy;
8264 /* Helpers to manipulate hash table of copied declarations. */
8266 /* Hashtable helpers. */
8268 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8270 typedef die_struct *compare_type;
8271 static inline hashval_t hash (const decl_table_entry *);
8272 static inline bool equal (const decl_table_entry *, const die_struct *);
8275 inline hashval_t
8276 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8278 return htab_hash_pointer (entry->orig);
8281 inline bool
8282 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8283 const die_struct *entry2)
8285 return entry1->orig == entry2;
8288 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8290 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8291 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8292 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8293 to check if the ancestor has already been copied into UNIT. */
8295 static dw_die_ref
8296 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8297 decl_hash_type *decl_table)
8299 dw_die_ref parent = die->die_parent;
8300 dw_die_ref new_parent = unit;
8301 dw_die_ref copy;
8302 decl_table_entry **slot = NULL;
8303 struct decl_table_entry *entry = NULL;
8305 /* If DIE refers to a stub unfold that so we get the appropriate
8306 DIE registered as orig in decl_table. */
8307 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8308 die = c;
8310 if (decl_table)
8312 /* Check if the entry has already been copied to UNIT. */
8313 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8314 INSERT);
8315 if (*slot != HTAB_EMPTY_ENTRY)
8317 entry = *slot;
8318 return entry->copy;
8321 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8322 entry = XCNEW (struct decl_table_entry);
8323 entry->orig = die;
8324 entry->copy = NULL;
8325 *slot = entry;
8328 if (parent != NULL)
8330 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8331 if (spec != NULL)
8332 parent = spec;
8333 if (!is_unit_die (parent))
8334 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8337 copy = clone_as_declaration (die);
8338 add_child_die (new_parent, copy);
8340 if (decl_table)
8342 /* Record the pointer to the copy. */
8343 entry->copy = copy;
8346 return copy;
8348 /* Copy the declaration context to the new type unit DIE. This includes
8349 any surrounding namespace or type declarations. If the DIE has an
8350 AT_specification attribute, it also includes attributes and children
8351 attached to the specification, and returns a pointer to the original
8352 parent of the declaration DIE. Returns NULL otherwise. */
8354 static dw_die_ref
8355 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8357 dw_die_ref decl;
8358 dw_die_ref new_decl;
8359 dw_die_ref orig_parent = NULL;
8361 decl = get_AT_ref (die, DW_AT_specification);
8362 if (decl == NULL)
8363 decl = die;
8364 else
8366 unsigned ix;
8367 dw_die_ref c;
8368 dw_attr_node *a;
8370 /* The original DIE will be changed to a declaration, and must
8371 be moved to be a child of the original declaration DIE. */
8372 orig_parent = decl->die_parent;
8374 /* Copy the type node pointer from the new DIE to the original
8375 declaration DIE so we can forward references later. */
8376 decl->comdat_type_p = true;
8377 decl->die_id.die_type_node = die->die_id.die_type_node;
8379 remove_AT (die, DW_AT_specification);
8381 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8383 if (a->dw_attr != DW_AT_name
8384 && a->dw_attr != DW_AT_declaration
8385 && a->dw_attr != DW_AT_external)
8386 add_dwarf_attr (die, a);
8389 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8392 if (decl->die_parent != NULL
8393 && !is_unit_die (decl->die_parent))
8395 new_decl = copy_ancestor_tree (unit, decl, NULL);
8396 if (new_decl != NULL)
8398 remove_AT (new_decl, DW_AT_signature);
8399 add_AT_specification (die, new_decl);
8403 return orig_parent;
8406 /* Generate the skeleton ancestor tree for the given NODE, then clone
8407 the DIE and add the clone into the tree. */
8409 static void
8410 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8412 if (node->new_die != NULL)
8413 return;
8415 node->new_die = clone_as_declaration (node->old_die);
8417 if (node->parent != NULL)
8419 generate_skeleton_ancestor_tree (node->parent);
8420 add_child_die (node->parent->new_die, node->new_die);
8424 /* Generate a skeleton tree of DIEs containing any declarations that are
8425 found in the original tree. We traverse the tree looking for declaration
8426 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8428 static void
8429 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8431 skeleton_chain_node node;
8432 dw_die_ref c;
8433 dw_die_ref first;
8434 dw_die_ref prev = NULL;
8435 dw_die_ref next = NULL;
8437 node.parent = parent;
8439 first = c = parent->old_die->die_child;
8440 if (c)
8441 next = c->die_sib;
8442 if (c) do {
8443 if (prev == NULL || prev->die_sib == c)
8444 prev = c;
8445 c = next;
8446 next = (c == first ? NULL : c->die_sib);
8447 node.old_die = c;
8448 node.new_die = NULL;
8449 if (is_declaration_die (c))
8451 if (is_template_instantiation (c))
8453 /* Instantiated templates do not need to be cloned into the
8454 type unit. Just move the DIE and its children back to
8455 the skeleton tree (in the main CU). */
8456 remove_child_with_prev (c, prev);
8457 add_child_die (parent->new_die, c);
8458 c = prev;
8460 else if (c->comdat_type_p)
8462 /* This is the skeleton of earlier break_out_comdat_types
8463 type. Clone the existing DIE, but keep the children
8464 under the original (which is in the main CU). */
8465 dw_die_ref clone = clone_die (c);
8467 replace_child (c, clone, prev);
8468 generate_skeleton_ancestor_tree (parent);
8469 add_child_die (parent->new_die, c);
8470 c = clone;
8471 continue;
8473 else
8475 /* Clone the existing DIE, move the original to the skeleton
8476 tree (which is in the main CU), and put the clone, with
8477 all the original's children, where the original came from
8478 (which is about to be moved to the type unit). */
8479 dw_die_ref clone = clone_die (c);
8480 move_all_children (c, clone);
8482 /* If the original has a DW_AT_object_pointer attribute,
8483 it would now point to a child DIE just moved to the
8484 cloned tree, so we need to remove that attribute from
8485 the original. */
8486 remove_AT (c, DW_AT_object_pointer);
8488 replace_child (c, clone, prev);
8489 generate_skeleton_ancestor_tree (parent);
8490 add_child_die (parent->new_die, c);
8491 node.old_die = clone;
8492 node.new_die = c;
8493 c = clone;
8496 generate_skeleton_bottom_up (&node);
8497 } while (next != NULL);
8500 /* Wrapper function for generate_skeleton_bottom_up. */
8502 static dw_die_ref
8503 generate_skeleton (dw_die_ref die)
8505 skeleton_chain_node node;
8507 node.old_die = die;
8508 node.new_die = NULL;
8509 node.parent = NULL;
8511 /* If this type definition is nested inside another type,
8512 and is not an instantiation of a template, always leave
8513 at least a declaration in its place. */
8514 if (die->die_parent != NULL
8515 && is_type_die (die->die_parent)
8516 && !is_template_instantiation (die))
8517 node.new_die = clone_as_declaration (die);
8519 generate_skeleton_bottom_up (&node);
8520 return node.new_die;
8523 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8524 declaration. The original DIE is moved to a new compile unit so that
8525 existing references to it follow it to the new location. If any of the
8526 original DIE's descendants is a declaration, we need to replace the
8527 original DIE with a skeleton tree and move the declarations back into the
8528 skeleton tree. */
8530 static dw_die_ref
8531 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8532 dw_die_ref prev)
8534 dw_die_ref skeleton, orig_parent;
8536 /* Copy the declaration context to the type unit DIE. If the returned
8537 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8538 that DIE. */
8539 orig_parent = copy_declaration_context (unit, child);
8541 skeleton = generate_skeleton (child);
8542 if (skeleton == NULL)
8543 remove_child_with_prev (child, prev);
8544 else
8546 skeleton->comdat_type_p = true;
8547 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8549 /* If the original DIE was a specification, we need to put
8550 the skeleton under the parent DIE of the declaration.
8551 This leaves the original declaration in the tree, but
8552 it will be pruned later since there are no longer any
8553 references to it. */
8554 if (orig_parent != NULL)
8556 remove_child_with_prev (child, prev);
8557 add_child_die (orig_parent, skeleton);
8559 else
8560 replace_child (child, skeleton, prev);
8563 return skeleton;
8566 static void
8567 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8568 comdat_type_node *type_node,
8569 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8571 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8572 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8573 DWARF procedure references in the DW_AT_location attribute. */
8575 static dw_die_ref
8576 copy_dwarf_procedure (dw_die_ref die,
8577 comdat_type_node *type_node,
8578 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8580 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8582 /* DWARF procedures are not supposed to have children... */
8583 gcc_assert (die->die_child == NULL);
8585 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8586 gcc_assert (vec_safe_length (die->die_attr) == 1
8587 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8589 /* Do not copy more than once DWARF procedures. */
8590 bool existed;
8591 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8592 if (existed)
8593 return die_copy;
8595 die_copy = clone_die (die);
8596 add_child_die (type_node->root_die, die_copy);
8597 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8598 return die_copy;
8601 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8602 procedures in DIE's attributes. */
8604 static void
8605 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8606 comdat_type_node *type_node,
8607 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8609 dw_attr_node *a;
8610 unsigned i;
8612 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8614 dw_loc_descr_ref loc;
8616 if (a->dw_attr_val.val_class != dw_val_class_loc)
8617 continue;
8619 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8621 switch (loc->dw_loc_opc)
8623 case DW_OP_call2:
8624 case DW_OP_call4:
8625 case DW_OP_call_ref:
8626 gcc_assert (loc->dw_loc_oprnd1.val_class
8627 == dw_val_class_die_ref);
8628 loc->dw_loc_oprnd1.v.val_die_ref.die
8629 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8630 type_node,
8631 copied_dwarf_procs);
8633 default:
8634 break;
8640 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8641 rewrite references to point to the copies.
8643 References are looked for in DIE's attributes and recursively in all its
8644 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8645 mapping from old DWARF procedures to their copy. It is used not to copy
8646 twice the same DWARF procedure under TYPE_NODE. */
8648 static void
8649 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8650 comdat_type_node *type_node,
8651 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8653 dw_die_ref c;
8655 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8656 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8657 type_node,
8658 copied_dwarf_procs));
8661 /* Traverse the DIE and set up additional .debug_types or .debug_info
8662 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8663 section. */
8665 static void
8666 break_out_comdat_types (dw_die_ref die)
8668 dw_die_ref c;
8669 dw_die_ref first;
8670 dw_die_ref prev = NULL;
8671 dw_die_ref next = NULL;
8672 dw_die_ref unit = NULL;
8674 first = c = die->die_child;
8675 if (c)
8676 next = c->die_sib;
8677 if (c) do {
8678 if (prev == NULL || prev->die_sib == c)
8679 prev = c;
8680 c = next;
8681 next = (c == first ? NULL : c->die_sib);
8682 if (should_move_die_to_comdat (c))
8684 dw_die_ref replacement;
8685 comdat_type_node *type_node;
8687 /* Break out nested types into their own type units. */
8688 break_out_comdat_types (c);
8690 /* Create a new type unit DIE as the root for the new tree. */
8691 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8692 add_AT_unsigned (unit, DW_AT_language,
8693 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8695 /* Add the new unit's type DIE into the comdat type list. */
8696 type_node = ggc_cleared_alloc<comdat_type_node> ();
8697 type_node->root_die = unit;
8698 type_node->next = comdat_type_list;
8699 comdat_type_list = type_node;
8701 /* Generate the type signature. */
8702 generate_type_signature (c, type_node);
8704 /* Copy the declaration context, attributes, and children of the
8705 declaration into the new type unit DIE, then remove this DIE
8706 from the main CU (or replace it with a skeleton if necessary). */
8707 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8708 type_node->skeleton_die = replacement;
8710 /* Add the DIE to the new compunit. */
8711 add_child_die (unit, c);
8713 /* Types can reference DWARF procedures for type size or data location
8714 expressions. Calls in DWARF expressions cannot target procedures
8715 that are not in the same section. So we must copy DWARF procedures
8716 along with this type and then rewrite references to them. */
8717 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8718 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8720 if (replacement != NULL)
8721 c = replacement;
8723 else if (c->die_tag == DW_TAG_namespace
8724 || c->die_tag == DW_TAG_class_type
8725 || c->die_tag == DW_TAG_structure_type
8726 || c->die_tag == DW_TAG_union_type)
8728 /* Look for nested types that can be broken out. */
8729 break_out_comdat_types (c);
8731 } while (next != NULL);
8734 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8735 Enter all the cloned children into the hash table decl_table. */
8737 static dw_die_ref
8738 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8740 dw_die_ref c;
8741 dw_die_ref clone;
8742 struct decl_table_entry *entry;
8743 decl_table_entry **slot;
8745 if (die->die_tag == DW_TAG_subprogram)
8746 clone = clone_as_declaration (die);
8747 else
8748 clone = clone_die (die);
8750 slot = decl_table->find_slot_with_hash (die,
8751 htab_hash_pointer (die), INSERT);
8753 /* Assert that DIE isn't in the hash table yet. If it would be there
8754 before, the ancestors would be necessarily there as well, therefore
8755 clone_tree_partial wouldn't be called. */
8756 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8758 entry = XCNEW (struct decl_table_entry);
8759 entry->orig = die;
8760 entry->copy = clone;
8761 *slot = entry;
8763 if (die->die_tag != DW_TAG_subprogram)
8764 FOR_EACH_CHILD (die, c,
8765 add_child_die (clone, clone_tree_partial (c, decl_table)));
8767 return clone;
8770 /* Walk the DIE and its children, looking for references to incomplete
8771 or trivial types that are unmarked (i.e., that are not in the current
8772 type_unit). */
8774 static void
8775 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8777 dw_die_ref c;
8778 dw_attr_node *a;
8779 unsigned ix;
8781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8783 if (AT_class (a) == dw_val_class_die_ref)
8785 dw_die_ref targ = AT_ref (a);
8786 decl_table_entry **slot;
8787 struct decl_table_entry *entry;
8789 if (targ->die_mark != 0 || targ->comdat_type_p)
8790 continue;
8792 slot = decl_table->find_slot_with_hash (targ,
8793 htab_hash_pointer (targ),
8794 INSERT);
8796 if (*slot != HTAB_EMPTY_ENTRY)
8798 /* TARG has already been copied, so we just need to
8799 modify the reference to point to the copy. */
8800 entry = *slot;
8801 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8803 else
8805 dw_die_ref parent = unit;
8806 dw_die_ref copy = clone_die (targ);
8808 /* Record in DECL_TABLE that TARG has been copied.
8809 Need to do this now, before the recursive call,
8810 because DECL_TABLE may be expanded and SLOT
8811 would no longer be a valid pointer. */
8812 entry = XCNEW (struct decl_table_entry);
8813 entry->orig = targ;
8814 entry->copy = copy;
8815 *slot = entry;
8817 /* If TARG is not a declaration DIE, we need to copy its
8818 children. */
8819 if (!is_declaration_die (targ))
8821 FOR_EACH_CHILD (
8822 targ, c,
8823 add_child_die (copy,
8824 clone_tree_partial (c, decl_table)));
8827 /* Make sure the cloned tree is marked as part of the
8828 type unit. */
8829 mark_dies (copy);
8831 /* If TARG has surrounding context, copy its ancestor tree
8832 into the new type unit. */
8833 if (targ->die_parent != NULL
8834 && !is_unit_die (targ->die_parent))
8835 parent = copy_ancestor_tree (unit, targ->die_parent,
8836 decl_table);
8838 add_child_die (parent, copy);
8839 a->dw_attr_val.v.val_die_ref.die = copy;
8841 /* Make sure the newly-copied DIE is walked. If it was
8842 installed in a previously-added context, it won't
8843 get visited otherwise. */
8844 if (parent != unit)
8846 /* Find the highest point of the newly-added tree,
8847 mark each node along the way, and walk from there. */
8848 parent->die_mark = 1;
8849 while (parent->die_parent
8850 && parent->die_parent->die_mark == 0)
8852 parent = parent->die_parent;
8853 parent->die_mark = 1;
8855 copy_decls_walk (unit, parent, decl_table);
8861 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8864 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8865 and record them in DECL_TABLE. */
8867 static void
8868 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8870 dw_die_ref c;
8872 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8874 dw_die_ref targ = AT_ref (a);
8875 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8876 decl_table_entry **slot
8877 = decl_table->find_slot_with_hash (targ,
8878 htab_hash_pointer (targ),
8879 INSERT);
8880 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8881 /* Record in DECL_TABLE that TARG has been already copied
8882 by remove_child_or_replace_with_skeleton. */
8883 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8884 entry->orig = targ;
8885 entry->copy = die;
8886 *slot = entry;
8888 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8891 /* Copy declarations for "unworthy" types into the new comdat section.
8892 Incomplete types, modified types, and certain other types aren't broken
8893 out into comdat sections of their own, so they don't have a signature,
8894 and we need to copy the declaration into the same section so that we
8895 don't have an external reference. */
8897 static void
8898 copy_decls_for_unworthy_types (dw_die_ref unit)
8900 mark_dies (unit);
8901 decl_hash_type decl_table (10);
8902 collect_skeleton_dies (unit, &decl_table);
8903 copy_decls_walk (unit, unit, &decl_table);
8904 unmark_dies (unit);
8907 /* Traverse the DIE and add a sibling attribute if it may have the
8908 effect of speeding up access to siblings. To save some space,
8909 avoid generating sibling attributes for DIE's without children. */
8911 static void
8912 add_sibling_attributes (dw_die_ref die)
8914 dw_die_ref c;
8916 if (! die->die_child)
8917 return;
8919 if (die->die_parent && die != die->die_parent->die_child)
8920 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8922 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8925 /* Output all location lists for the DIE and its children. */
8927 static void
8928 output_location_lists (dw_die_ref die)
8930 dw_die_ref c;
8931 dw_attr_node *a;
8932 unsigned ix;
8934 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8935 if (AT_class (a) == dw_val_class_loc_list)
8936 output_loc_list (AT_loc_list (a));
8938 FOR_EACH_CHILD (die, c, output_location_lists (c));
8941 /* During assign_location_list_indexes and output_loclists_offset the
8942 current index, after it the number of assigned indexes (i.e. how
8943 large the .debug_loclists* offset table should be). */
8944 static unsigned int loc_list_idx;
8946 /* Output all location list offsets for the DIE and its children. */
8948 static void
8949 output_loclists_offsets (dw_die_ref die)
8951 dw_die_ref c;
8952 dw_attr_node *a;
8953 unsigned ix;
8955 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8956 if (AT_class (a) == dw_val_class_loc_list)
8958 dw_loc_list_ref l = AT_loc_list (a);
8959 if (l->offset_emitted)
8960 continue;
8961 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
8962 loc_section_label, NULL);
8963 gcc_assert (l->hash == loc_list_idx);
8964 loc_list_idx++;
8965 l->offset_emitted = true;
8968 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8971 /* Recursively set indexes of location lists. */
8973 static void
8974 assign_location_list_indexes (dw_die_ref die)
8976 dw_die_ref c;
8977 dw_attr_node *a;
8978 unsigned ix;
8980 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8981 if (AT_class (a) == dw_val_class_loc_list)
8983 dw_loc_list_ref list = AT_loc_list (a);
8984 if (!list->num_assigned)
8986 list->num_assigned = true;
8987 list->hash = loc_list_idx++;
8991 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8994 /* We want to limit the number of external references, because they are
8995 larger than local references: a relocation takes multiple words, and
8996 even a sig8 reference is always eight bytes, whereas a local reference
8997 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8998 So if we encounter multiple external references to the same type DIE, we
8999 make a local typedef stub for it and redirect all references there.
9001 This is the element of the hash table for keeping track of these
9002 references. */
9004 struct external_ref
9006 dw_die_ref type;
9007 dw_die_ref stub;
9008 unsigned n_refs;
9011 /* Hashtable helpers. */
9013 struct external_ref_hasher : free_ptr_hash <external_ref>
9015 static inline hashval_t hash (const external_ref *);
9016 static inline bool equal (const external_ref *, const external_ref *);
9019 inline hashval_t
9020 external_ref_hasher::hash (const external_ref *r)
9022 dw_die_ref die = r->type;
9023 hashval_t h = 0;
9025 /* We can't use the address of the DIE for hashing, because
9026 that will make the order of the stub DIEs non-deterministic. */
9027 if (! die->comdat_type_p)
9028 /* We have a symbol; use it to compute a hash. */
9029 h = htab_hash_string (die->die_id.die_symbol);
9030 else
9032 /* We have a type signature; use a subset of the bits as the hash.
9033 The 8-byte signature is at least as large as hashval_t. */
9034 comdat_type_node *type_node = die->die_id.die_type_node;
9035 memcpy (&h, type_node->signature, sizeof (h));
9037 return h;
9040 inline bool
9041 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9043 return r1->type == r2->type;
9046 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9048 /* Return a pointer to the external_ref for references to DIE. */
9050 static struct external_ref *
9051 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9053 struct external_ref ref, *ref_p;
9054 external_ref **slot;
9056 ref.type = die;
9057 slot = map->find_slot (&ref, INSERT);
9058 if (*slot != HTAB_EMPTY_ENTRY)
9059 return *slot;
9061 ref_p = XCNEW (struct external_ref);
9062 ref_p->type = die;
9063 *slot = ref_p;
9064 return ref_p;
9067 /* Subroutine of optimize_external_refs, below.
9069 If we see a type skeleton, record it as our stub. If we see external
9070 references, remember how many we've seen. */
9072 static void
9073 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9075 dw_die_ref c;
9076 dw_attr_node *a;
9077 unsigned ix;
9078 struct external_ref *ref_p;
9080 if (is_type_die (die)
9081 && (c = get_AT_ref (die, DW_AT_signature)))
9083 /* This is a local skeleton; use it for local references. */
9084 ref_p = lookup_external_ref (map, c);
9085 ref_p->stub = die;
9088 /* Scan the DIE references, and remember any that refer to DIEs from
9089 other CUs (i.e. those which are not marked). */
9090 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9091 if (AT_class (a) == dw_val_class_die_ref
9092 && (c = AT_ref (a))->die_mark == 0
9093 && is_type_die (c))
9095 ref_p = lookup_external_ref (map, c);
9096 ref_p->n_refs++;
9099 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9102 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9103 points to an external_ref, DATA is the CU we're processing. If we don't
9104 already have a local stub, and we have multiple refs, build a stub. */
9107 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9109 struct external_ref *ref_p = *slot;
9111 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9113 /* We have multiple references to this type, so build a small stub.
9114 Both of these forms are a bit dodgy from the perspective of the
9115 DWARF standard, since technically they should have names. */
9116 dw_die_ref cu = data;
9117 dw_die_ref type = ref_p->type;
9118 dw_die_ref stub = NULL;
9120 if (type->comdat_type_p)
9122 /* If we refer to this type via sig8, use AT_signature. */
9123 stub = new_die (type->die_tag, cu, NULL_TREE);
9124 add_AT_die_ref (stub, DW_AT_signature, type);
9126 else
9128 /* Otherwise, use a typedef with no name. */
9129 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9130 add_AT_die_ref (stub, DW_AT_type, type);
9133 stub->die_mark++;
9134 ref_p->stub = stub;
9136 return 1;
9139 /* DIE is a unit; look through all the DIE references to see if there are
9140 any external references to types, and if so, create local stubs for
9141 them which will be applied in build_abbrev_table. This is useful because
9142 references to local DIEs are smaller. */
9144 static external_ref_hash_type *
9145 optimize_external_refs (dw_die_ref die)
9147 external_ref_hash_type *map = new external_ref_hash_type (10);
9148 optimize_external_refs_1 (die, map);
9149 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9150 return map;
9153 /* The following 3 variables are temporaries that are computed only during the
9154 build_abbrev_table call and used and released during the following
9155 optimize_abbrev_table call. */
9157 /* First abbrev_id that can be optimized based on usage. */
9158 static unsigned int abbrev_opt_start;
9160 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9161 abbrev_id smaller than this, because they must be already sized
9162 during build_abbrev_table). */
9163 static unsigned int abbrev_opt_base_type_end;
9165 /* Vector of usage counts during build_abbrev_table. Indexed by
9166 abbrev_id - abbrev_opt_start. */
9167 static vec<unsigned int> abbrev_usage_count;
9169 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9170 static vec<dw_die_ref> sorted_abbrev_dies;
9172 /* The format of each DIE (and its attribute value pairs) is encoded in an
9173 abbreviation table. This routine builds the abbreviation table and assigns
9174 a unique abbreviation id for each abbreviation entry. The children of each
9175 die are visited recursively. */
9177 static void
9178 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9180 unsigned int abbrev_id = 0;
9181 dw_die_ref c;
9182 dw_attr_node *a;
9183 unsigned ix;
9184 dw_die_ref abbrev;
9186 /* Scan the DIE references, and replace any that refer to
9187 DIEs from other CUs (i.e. those which are not marked) with
9188 the local stubs we built in optimize_external_refs. */
9189 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9190 if (AT_class (a) == dw_val_class_die_ref
9191 && (c = AT_ref (a))->die_mark == 0)
9193 struct external_ref *ref_p;
9194 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9196 if (is_type_die (c)
9197 && (ref_p = lookup_external_ref (extern_map, c))
9198 && ref_p->stub && ref_p->stub != die)
9200 gcc_assert (a->dw_attr != DW_AT_signature);
9201 change_AT_die_ref (a, ref_p->stub);
9203 else
9204 /* We aren't changing this reference, so mark it external. */
9205 set_AT_ref_external (a, 1);
9208 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9210 dw_attr_node *die_a, *abbrev_a;
9211 unsigned ix;
9212 bool ok = true;
9214 if (abbrev_id == 0)
9215 continue;
9216 if (abbrev->die_tag != die->die_tag)
9217 continue;
9218 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9219 continue;
9221 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9222 continue;
9224 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9226 abbrev_a = &(*abbrev->die_attr)[ix];
9227 if ((abbrev_a->dw_attr != die_a->dw_attr)
9228 || (value_format (abbrev_a) != value_format (die_a)))
9230 ok = false;
9231 break;
9234 if (ok)
9235 break;
9238 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9240 vec_safe_push (abbrev_die_table, die);
9241 if (abbrev_opt_start)
9242 abbrev_usage_count.safe_push (0);
9244 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9246 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9247 sorted_abbrev_dies.safe_push (die);
9250 die->die_abbrev = abbrev_id;
9251 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9254 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9255 by die_abbrev's usage count, from the most commonly used
9256 abbreviation to the least. */
9258 static int
9259 die_abbrev_cmp (const void *p1, const void *p2)
9261 dw_die_ref die1 = *(const dw_die_ref *) p1;
9262 dw_die_ref die2 = *(const dw_die_ref *) p2;
9264 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9265 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9267 if (die1->die_abbrev >= abbrev_opt_base_type_end
9268 && die2->die_abbrev >= abbrev_opt_base_type_end)
9270 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9271 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9272 return -1;
9273 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9274 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9275 return 1;
9278 /* Stabilize the sort. */
9279 if (die1->die_abbrev < die2->die_abbrev)
9280 return -1;
9281 if (die1->die_abbrev > die2->die_abbrev)
9282 return 1;
9284 return 0;
9287 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9288 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9289 into dw_val_class_const_implicit or
9290 dw_val_class_unsigned_const_implicit. */
9292 static void
9293 optimize_implicit_const (unsigned int first_id, unsigned int end,
9294 vec<bool> &implicit_consts)
9296 /* It never makes sense if there is just one DIE using the abbreviation. */
9297 if (end < first_id + 2)
9298 return;
9300 dw_attr_node *a;
9301 unsigned ix, i;
9302 dw_die_ref die = sorted_abbrev_dies[first_id];
9303 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9304 if (implicit_consts[ix])
9306 enum dw_val_class new_class = dw_val_class_none;
9307 switch (AT_class (a))
9309 case dw_val_class_unsigned_const:
9310 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9311 continue;
9313 /* The .debug_abbrev section will grow by
9314 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9315 in all the DIEs using that abbreviation. */
9316 if (constant_size (AT_unsigned (a)) * (end - first_id)
9317 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9318 continue;
9320 new_class = dw_val_class_unsigned_const_implicit;
9321 break;
9323 case dw_val_class_const:
9324 new_class = dw_val_class_const_implicit;
9325 break;
9327 case dw_val_class_file:
9328 new_class = dw_val_class_file_implicit;
9329 break;
9331 default:
9332 continue;
9334 for (i = first_id; i < end; i++)
9335 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9336 = new_class;
9340 /* Attempt to optimize abbreviation table from abbrev_opt_start
9341 abbreviation above. */
9343 static void
9344 optimize_abbrev_table (void)
9346 if (abbrev_opt_start
9347 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9348 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9350 auto_vec<bool, 32> implicit_consts;
9351 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9353 unsigned int abbrev_id = abbrev_opt_start - 1;
9354 unsigned int first_id = ~0U;
9355 unsigned int last_abbrev_id = 0;
9356 unsigned int i;
9357 dw_die_ref die;
9358 if (abbrev_opt_base_type_end > abbrev_opt_start)
9359 abbrev_id = abbrev_opt_base_type_end - 1;
9360 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9361 most commonly used abbreviations come first. */
9362 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9364 dw_attr_node *a;
9365 unsigned ix;
9367 /* If calc_base_type_die_sizes has been called, the CU and
9368 base types after it can't be optimized, because we've already
9369 calculated their DIE offsets. We've sorted them first. */
9370 if (die->die_abbrev < abbrev_opt_base_type_end)
9371 continue;
9372 if (die->die_abbrev != last_abbrev_id)
9374 last_abbrev_id = die->die_abbrev;
9375 if (dwarf_version >= 5 && first_id != ~0U)
9376 optimize_implicit_const (first_id, i, implicit_consts);
9377 abbrev_id++;
9378 (*abbrev_die_table)[abbrev_id] = die;
9379 if (dwarf_version >= 5)
9381 first_id = i;
9382 implicit_consts.truncate (0);
9384 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9385 switch (AT_class (a))
9387 case dw_val_class_const:
9388 case dw_val_class_unsigned_const:
9389 case dw_val_class_file:
9390 implicit_consts.safe_push (true);
9391 break;
9392 default:
9393 implicit_consts.safe_push (false);
9394 break;
9398 else if (dwarf_version >= 5)
9400 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9401 if (!implicit_consts[ix])
9402 continue;
9403 else
9405 dw_attr_node *other_a
9406 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9407 if (!dw_val_equal_p (&a->dw_attr_val,
9408 &other_a->dw_attr_val))
9409 implicit_consts[ix] = false;
9412 die->die_abbrev = abbrev_id;
9414 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9415 if (dwarf_version >= 5 && first_id != ~0U)
9416 optimize_implicit_const (first_id, i, implicit_consts);
9419 abbrev_opt_start = 0;
9420 abbrev_opt_base_type_end = 0;
9421 abbrev_usage_count.release ();
9422 sorted_abbrev_dies.release ();
9425 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9427 static int
9428 constant_size (unsigned HOST_WIDE_INT value)
9430 int log;
9432 if (value == 0)
9433 log = 0;
9434 else
9435 log = floor_log2 (value);
9437 log = log / 8;
9438 log = 1 << (floor_log2 (log) + 1);
9440 return log;
9443 /* Return the size of a DIE as it is represented in the
9444 .debug_info section. */
9446 static unsigned long
9447 size_of_die (dw_die_ref die)
9449 unsigned long size = 0;
9450 dw_attr_node *a;
9451 unsigned ix;
9452 enum dwarf_form form;
9454 size += size_of_uleb128 (die->die_abbrev);
9455 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9457 switch (AT_class (a))
9459 case dw_val_class_addr:
9460 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9462 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9463 size += size_of_uleb128 (AT_index (a));
9465 else
9466 size += DWARF2_ADDR_SIZE;
9467 break;
9468 case dw_val_class_offset:
9469 size += dwarf_offset_size;
9470 break;
9471 case dw_val_class_loc:
9473 unsigned long lsize = size_of_locs (AT_loc (a));
9475 /* Block length. */
9476 if (dwarf_version >= 4)
9477 size += size_of_uleb128 (lsize);
9478 else
9479 size += constant_size (lsize);
9480 size += lsize;
9482 break;
9483 case dw_val_class_loc_list:
9484 if (dwarf_split_debug_info && dwarf_version >= 5)
9486 gcc_assert (AT_loc_list (a)->num_assigned);
9487 size += size_of_uleb128 (AT_loc_list (a)->hash);
9489 else
9490 size += dwarf_offset_size;
9491 break;
9492 case dw_val_class_view_list:
9493 size += dwarf_offset_size;
9494 break;
9495 case dw_val_class_range_list:
9496 if (value_format (a) == DW_FORM_rnglistx)
9498 gcc_assert (rnglist_idx);
9499 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9500 size += size_of_uleb128 (r->idx);
9502 else
9503 size += dwarf_offset_size;
9504 break;
9505 case dw_val_class_const:
9506 size += size_of_sleb128 (AT_int (a));
9507 break;
9508 case dw_val_class_unsigned_const:
9510 int csize = constant_size (AT_unsigned (a));
9511 if (dwarf_version == 3
9512 && a->dw_attr == DW_AT_data_member_location
9513 && csize >= 4)
9514 size += size_of_uleb128 (AT_unsigned (a));
9515 else
9516 size += csize;
9518 break;
9519 case dw_val_class_symview:
9520 if (symview_upper_bound <= 0xff)
9521 size += 1;
9522 else if (symview_upper_bound <= 0xffff)
9523 size += 2;
9524 else if (symview_upper_bound <= 0xffffffff)
9525 size += 4;
9526 else
9527 size += 8;
9528 break;
9529 case dw_val_class_const_implicit:
9530 case dw_val_class_unsigned_const_implicit:
9531 case dw_val_class_file_implicit:
9532 /* These occupy no size in the DIE, just an extra sleb128 in
9533 .debug_abbrev. */
9534 break;
9535 case dw_val_class_const_double:
9536 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9537 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9538 size++; /* block */
9539 break;
9540 case dw_val_class_wide_int:
9541 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9542 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9543 if (get_full_len (*a->dw_attr_val.v.val_wide)
9544 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9545 size++; /* block */
9546 break;
9547 case dw_val_class_vec:
9548 size += constant_size (a->dw_attr_val.v.val_vec.length
9549 * a->dw_attr_val.v.val_vec.elt_size)
9550 + a->dw_attr_val.v.val_vec.length
9551 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9552 break;
9553 case dw_val_class_flag:
9554 if (dwarf_version >= 4)
9555 /* Currently all add_AT_flag calls pass in 1 as last argument,
9556 so DW_FORM_flag_present can be used. If that ever changes,
9557 we'll need to use DW_FORM_flag and have some optimization
9558 in build_abbrev_table that will change those to
9559 DW_FORM_flag_present if it is set to 1 in all DIEs using
9560 the same abbrev entry. */
9561 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9562 else
9563 size += 1;
9564 break;
9565 case dw_val_class_die_ref:
9566 if (AT_ref_external (a))
9568 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9569 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9570 is sized by target address length, whereas in DWARF3
9571 it's always sized as an offset. */
9572 if (AT_ref (a)->comdat_type_p)
9573 size += DWARF_TYPE_SIGNATURE_SIZE;
9574 else if (dwarf_version == 2)
9575 size += DWARF2_ADDR_SIZE;
9576 else
9577 size += dwarf_offset_size;
9579 else
9580 size += dwarf_offset_size;
9581 break;
9582 case dw_val_class_fde_ref:
9583 size += dwarf_offset_size;
9584 break;
9585 case dw_val_class_lbl_id:
9586 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9588 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9589 size += size_of_uleb128 (AT_index (a));
9591 else
9592 size += DWARF2_ADDR_SIZE;
9593 break;
9594 case dw_val_class_lineptr:
9595 case dw_val_class_macptr:
9596 case dw_val_class_loclistsptr:
9597 size += dwarf_offset_size;
9598 break;
9599 case dw_val_class_str:
9600 form = AT_string_form (a);
9601 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9602 size += dwarf_offset_size;
9603 else if (form == dwarf_FORM (DW_FORM_strx))
9604 size += size_of_uleb128 (AT_index (a));
9605 else
9606 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9607 break;
9608 case dw_val_class_file:
9609 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9610 break;
9611 case dw_val_class_data8:
9612 size += 8;
9613 break;
9614 case dw_val_class_vms_delta:
9615 size += dwarf_offset_size;
9616 break;
9617 case dw_val_class_high_pc:
9618 size += DWARF2_ADDR_SIZE;
9619 break;
9620 case dw_val_class_discr_value:
9621 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9622 break;
9623 case dw_val_class_discr_list:
9625 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9627 /* This is a block, so we have the block length and then its
9628 data. */
9629 size += constant_size (block_size) + block_size;
9631 break;
9632 default:
9633 gcc_unreachable ();
9637 return size;
9640 /* Size the debugging information associated with a given DIE. Visits the
9641 DIE's children recursively. Updates the global variable next_die_offset, on
9642 each time through. Uses the current value of next_die_offset to update the
9643 die_offset field in each DIE. */
9645 static void
9646 calc_die_sizes (dw_die_ref die)
9648 dw_die_ref c;
9650 gcc_assert (die->die_offset == 0
9651 || (unsigned long int) die->die_offset == next_die_offset);
9652 die->die_offset = next_die_offset;
9653 next_die_offset += size_of_die (die);
9655 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9657 if (die->die_child != NULL)
9658 /* Count the null byte used to terminate sibling lists. */
9659 next_die_offset += 1;
9662 /* Size just the base type children at the start of the CU.
9663 This is needed because build_abbrev needs to size locs
9664 and sizing of type based stack ops needs to know die_offset
9665 values for the base types. */
9667 static void
9668 calc_base_type_die_sizes (void)
9670 unsigned long die_offset = (dwarf_split_debug_info
9671 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9672 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9673 unsigned int i;
9674 dw_die_ref base_type;
9675 #if ENABLE_ASSERT_CHECKING
9676 dw_die_ref prev = comp_unit_die ()->die_child;
9677 #endif
9679 die_offset += size_of_die (comp_unit_die ());
9680 for (i = 0; base_types.iterate (i, &base_type); i++)
9682 #if ENABLE_ASSERT_CHECKING
9683 gcc_assert (base_type->die_offset == 0
9684 && prev->die_sib == base_type
9685 && base_type->die_child == NULL
9686 && base_type->die_abbrev);
9687 prev = base_type;
9688 #endif
9689 if (abbrev_opt_start
9690 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9691 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9692 base_type->die_offset = die_offset;
9693 die_offset += size_of_die (base_type);
9697 /* Set the marks for a die and its children. We do this so
9698 that we know whether or not a reference needs to use FORM_ref_addr; only
9699 DIEs in the same CU will be marked. We used to clear out the offset
9700 and use that as the flag, but ran into ordering problems. */
9702 static void
9703 mark_dies (dw_die_ref die)
9705 dw_die_ref c;
9707 gcc_assert (!die->die_mark);
9709 die->die_mark = 1;
9710 FOR_EACH_CHILD (die, c, mark_dies (c));
9713 /* Clear the marks for a die and its children. */
9715 static void
9716 unmark_dies (dw_die_ref die)
9718 dw_die_ref c;
9720 if (! use_debug_types)
9721 gcc_assert (die->die_mark);
9723 die->die_mark = 0;
9724 FOR_EACH_CHILD (die, c, unmark_dies (c));
9727 /* Clear the marks for a die, its children and referred dies. */
9729 static void
9730 unmark_all_dies (dw_die_ref die)
9732 dw_die_ref c;
9733 dw_attr_node *a;
9734 unsigned ix;
9736 if (!die->die_mark)
9737 return;
9738 die->die_mark = 0;
9740 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9742 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9743 if (AT_class (a) == dw_val_class_die_ref)
9744 unmark_all_dies (AT_ref (a));
9747 /* Calculate if the entry should appear in the final output file. It may be
9748 from a pruned a type. */
9750 static bool
9751 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9753 /* By limiting gnu pubnames to definitions only, gold can generate a
9754 gdb index without entries for declarations, which don't include
9755 enough information to be useful. */
9756 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9757 return false;
9759 if (table == pubname_table)
9761 /* Enumerator names are part of the pubname table, but the
9762 parent DW_TAG_enumeration_type die may have been pruned.
9763 Don't output them if that is the case. */
9764 if (p->die->die_tag == DW_TAG_enumerator &&
9765 (p->die->die_parent == NULL
9766 || !p->die->die_parent->die_perennial_p))
9767 return false;
9769 /* Everything else in the pubname table is included. */
9770 return true;
9773 /* The pubtypes table shouldn't include types that have been
9774 pruned. */
9775 return (p->die->die_offset != 0
9776 || !flag_eliminate_unused_debug_types);
9779 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9780 generated for the compilation unit. */
9782 static unsigned long
9783 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9785 unsigned long size;
9786 unsigned i;
9787 pubname_entry *p;
9788 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9790 size = DWARF_PUBNAMES_HEADER_SIZE;
9791 FOR_EACH_VEC_ELT (*names, i, p)
9792 if (include_pubname_in_output (names, p))
9793 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9795 size += dwarf_offset_size;
9796 return size;
9799 /* Return the size of the information in the .debug_aranges section. */
9801 static unsigned long
9802 size_of_aranges (void)
9804 unsigned long size;
9806 size = DWARF_ARANGES_HEADER_SIZE;
9808 /* Count the address/length pair for this compilation unit. */
9809 if (switch_text_ranges)
9810 size += 2 * DWARF2_ADDR_SIZE
9811 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9812 if (switch_cold_ranges)
9813 size += 2 * DWARF2_ADDR_SIZE
9814 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9815 if (have_multiple_function_sections)
9817 unsigned fde_idx;
9818 dw_fde_ref fde;
9820 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9822 if (fde->ignored_debug)
9823 continue;
9824 if (!fde->in_std_section)
9825 size += 2 * DWARF2_ADDR_SIZE;
9826 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9827 size += 2 * DWARF2_ADDR_SIZE;
9831 /* Count the two zero words used to terminated the address range table. */
9832 size += 2 * DWARF2_ADDR_SIZE;
9833 return size;
9836 /* Select the encoding of an attribute value. */
9838 static enum dwarf_form
9839 value_format (dw_attr_node *a)
9841 switch (AT_class (a))
9843 case dw_val_class_addr:
9844 /* Only very few attributes allow DW_FORM_addr. */
9845 switch (a->dw_attr)
9847 case DW_AT_low_pc:
9848 case DW_AT_high_pc:
9849 case DW_AT_entry_pc:
9850 case DW_AT_trampoline:
9851 return (AT_index (a) == NOT_INDEXED
9852 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9853 default:
9854 break;
9856 switch (DWARF2_ADDR_SIZE)
9858 case 1:
9859 return DW_FORM_data1;
9860 case 2:
9861 return DW_FORM_data2;
9862 case 4:
9863 return DW_FORM_data4;
9864 case 8:
9865 return DW_FORM_data8;
9866 default:
9867 gcc_unreachable ();
9869 case dw_val_class_loc_list:
9870 if (dwarf_split_debug_info
9871 && dwarf_version >= 5
9872 && AT_loc_list (a)->num_assigned)
9873 return DW_FORM_loclistx;
9874 /* FALLTHRU */
9875 case dw_val_class_view_list:
9876 case dw_val_class_range_list:
9877 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9878 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9879 care about sizes of .debug* sections in shared libraries and
9880 executables and don't take into account relocations that affect just
9881 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9882 table in the .debug_rnglists section. */
9883 if (dwarf_split_debug_info
9884 && dwarf_version >= 5
9885 && AT_class (a) == dw_val_class_range_list
9886 && rnglist_idx
9887 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9888 return DW_FORM_rnglistx;
9889 if (dwarf_version >= 4)
9890 return DW_FORM_sec_offset;
9891 /* FALLTHRU */
9892 case dw_val_class_vms_delta:
9893 case dw_val_class_offset:
9894 switch (dwarf_offset_size)
9896 case 4:
9897 return DW_FORM_data4;
9898 case 8:
9899 return DW_FORM_data8;
9900 default:
9901 gcc_unreachable ();
9903 case dw_val_class_loc:
9904 if (dwarf_version >= 4)
9905 return DW_FORM_exprloc;
9906 switch (constant_size (size_of_locs (AT_loc (a))))
9908 case 1:
9909 return DW_FORM_block1;
9910 case 2:
9911 return DW_FORM_block2;
9912 case 4:
9913 return DW_FORM_block4;
9914 default:
9915 gcc_unreachable ();
9917 case dw_val_class_const:
9918 return DW_FORM_sdata;
9919 case dw_val_class_unsigned_const:
9920 switch (constant_size (AT_unsigned (a)))
9922 case 1:
9923 return DW_FORM_data1;
9924 case 2:
9925 return DW_FORM_data2;
9926 case 4:
9927 /* In DWARF3 DW_AT_data_member_location with
9928 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9929 constant, so we need to use DW_FORM_udata if we need
9930 a large constant. */
9931 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9932 return DW_FORM_udata;
9933 return DW_FORM_data4;
9934 case 8:
9935 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9936 return DW_FORM_udata;
9937 return DW_FORM_data8;
9938 default:
9939 gcc_unreachable ();
9941 case dw_val_class_const_implicit:
9942 case dw_val_class_unsigned_const_implicit:
9943 case dw_val_class_file_implicit:
9944 return DW_FORM_implicit_const;
9945 case dw_val_class_const_double:
9946 switch (HOST_BITS_PER_WIDE_INT)
9948 case 8:
9949 return DW_FORM_data2;
9950 case 16:
9951 return DW_FORM_data4;
9952 case 32:
9953 return DW_FORM_data8;
9954 case 64:
9955 if (dwarf_version >= 5)
9956 return DW_FORM_data16;
9957 /* FALLTHRU */
9958 default:
9959 return DW_FORM_block1;
9961 case dw_val_class_wide_int:
9962 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9964 case 8:
9965 return DW_FORM_data1;
9966 case 16:
9967 return DW_FORM_data2;
9968 case 32:
9969 return DW_FORM_data4;
9970 case 64:
9971 return DW_FORM_data8;
9972 case 128:
9973 if (dwarf_version >= 5)
9974 return DW_FORM_data16;
9975 /* FALLTHRU */
9976 default:
9977 return DW_FORM_block1;
9979 case dw_val_class_symview:
9980 /* ??? We might use uleb128, but then we'd have to compute
9981 .debug_info offsets in the assembler. */
9982 if (symview_upper_bound <= 0xff)
9983 return DW_FORM_data1;
9984 else if (symview_upper_bound <= 0xffff)
9985 return DW_FORM_data2;
9986 else if (symview_upper_bound <= 0xffffffff)
9987 return DW_FORM_data4;
9988 else
9989 return DW_FORM_data8;
9990 case dw_val_class_vec:
9991 switch (constant_size (a->dw_attr_val.v.val_vec.length
9992 * a->dw_attr_val.v.val_vec.elt_size))
9994 case 1:
9995 return DW_FORM_block1;
9996 case 2:
9997 return DW_FORM_block2;
9998 case 4:
9999 return DW_FORM_block4;
10000 default:
10001 gcc_unreachable ();
10003 case dw_val_class_flag:
10004 if (dwarf_version >= 4)
10006 /* Currently all add_AT_flag calls pass in 1 as last argument,
10007 so DW_FORM_flag_present can be used. If that ever changes,
10008 we'll need to use DW_FORM_flag and have some optimization
10009 in build_abbrev_table that will change those to
10010 DW_FORM_flag_present if it is set to 1 in all DIEs using
10011 the same abbrev entry. */
10012 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10013 return DW_FORM_flag_present;
10015 return DW_FORM_flag;
10016 case dw_val_class_die_ref:
10017 if (AT_ref_external (a))
10019 if (AT_ref (a)->comdat_type_p)
10020 return DW_FORM_ref_sig8;
10021 else
10022 return DW_FORM_ref_addr;
10024 else
10025 return DW_FORM_ref;
10026 case dw_val_class_fde_ref:
10027 return DW_FORM_data;
10028 case dw_val_class_lbl_id:
10029 return (AT_index (a) == NOT_INDEXED
10030 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10031 case dw_val_class_lineptr:
10032 case dw_val_class_macptr:
10033 case dw_val_class_loclistsptr:
10034 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10035 case dw_val_class_str:
10036 return AT_string_form (a);
10037 case dw_val_class_file:
10038 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10040 case 1:
10041 return DW_FORM_data1;
10042 case 2:
10043 return DW_FORM_data2;
10044 case 4:
10045 return DW_FORM_data4;
10046 default:
10047 gcc_unreachable ();
10050 case dw_val_class_data8:
10051 return DW_FORM_data8;
10053 case dw_val_class_high_pc:
10054 switch (DWARF2_ADDR_SIZE)
10056 case 1:
10057 return DW_FORM_data1;
10058 case 2:
10059 return DW_FORM_data2;
10060 case 4:
10061 return DW_FORM_data4;
10062 case 8:
10063 return DW_FORM_data8;
10064 default:
10065 gcc_unreachable ();
10068 case dw_val_class_discr_value:
10069 return (a->dw_attr_val.v.val_discr_value.pos
10070 ? DW_FORM_udata
10071 : DW_FORM_sdata);
10072 case dw_val_class_discr_list:
10073 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10075 case 1:
10076 return DW_FORM_block1;
10077 case 2:
10078 return DW_FORM_block2;
10079 case 4:
10080 return DW_FORM_block4;
10081 default:
10082 gcc_unreachable ();
10085 default:
10086 gcc_unreachable ();
10090 /* Output the encoding of an attribute value. */
10092 static void
10093 output_value_format (dw_attr_node *a)
10095 enum dwarf_form form = value_format (a);
10097 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10100 /* Given a die and id, produce the appropriate abbreviations. */
10102 static void
10103 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10105 unsigned ix;
10106 dw_attr_node *a_attr;
10108 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10109 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10110 dwarf_tag_name (abbrev->die_tag));
10112 if (abbrev->die_child != NULL)
10113 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10114 else
10115 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10117 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10119 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10120 dwarf_attr_name (a_attr->dw_attr));
10121 output_value_format (a_attr);
10122 if (value_format (a_attr) == DW_FORM_implicit_const)
10124 if (AT_class (a_attr) == dw_val_class_file_implicit)
10126 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10127 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10128 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10130 else
10131 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10135 dw2_asm_output_data (1, 0, NULL);
10136 dw2_asm_output_data (1, 0, NULL);
10140 /* Output the .debug_abbrev section which defines the DIE abbreviation
10141 table. */
10143 static void
10144 output_abbrev_section (void)
10146 unsigned int abbrev_id;
10147 dw_die_ref abbrev;
10149 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10150 if (abbrev_id != 0)
10151 output_die_abbrevs (abbrev_id, abbrev);
10153 /* Terminate the table. */
10154 dw2_asm_output_data (1, 0, NULL);
10157 /* Return a new location list, given the begin and end range, and the
10158 expression. */
10160 static inline dw_loc_list_ref
10161 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10162 const char *end, var_loc_view vend,
10163 const char *section)
10165 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10167 retlist->begin = begin;
10168 retlist->begin_entry = NULL;
10169 retlist->end = end;
10170 retlist->end_entry = NULL;
10171 retlist->expr = expr;
10172 retlist->section = section;
10173 retlist->vbegin = vbegin;
10174 retlist->vend = vend;
10176 return retlist;
10179 /* Return true iff there's any nonzero view number in the loc list.
10181 ??? When views are not enabled, we'll often extend a single range
10182 to the entire function, so that we emit a single location
10183 expression rather than a location list. With views, even with a
10184 single range, we'll output a list if start or end have a nonzero
10185 view. If we change this, we may want to stop splitting a single
10186 range in dw_loc_list just because of a nonzero view, even if it
10187 straddles across hot/cold partitions. */
10189 static bool
10190 loc_list_has_views (dw_loc_list_ref list)
10192 if (!debug_variable_location_views)
10193 return false;
10195 for (dw_loc_list_ref loc = list;
10196 loc != NULL; loc = loc->dw_loc_next)
10197 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10198 return true;
10200 return false;
10203 /* Generate a new internal symbol for this location list node, if it
10204 hasn't got one yet. */
10206 static inline void
10207 gen_llsym (dw_loc_list_ref list)
10209 gcc_assert (!list->ll_symbol);
10210 list->ll_symbol = gen_internal_sym ("LLST");
10212 if (!loc_list_has_views (list))
10213 return;
10215 if (dwarf2out_locviews_in_attribute ())
10217 /* Use the same label_num for the view list. */
10218 label_num--;
10219 list->vl_symbol = gen_internal_sym ("LVUS");
10221 else
10222 list->vl_symbol = list->ll_symbol;
10225 /* Generate a symbol for the list, but only if we really want to emit
10226 it as a list. */
10228 static inline void
10229 maybe_gen_llsym (dw_loc_list_ref list)
10231 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10232 return;
10234 gen_llsym (list);
10237 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10238 NULL, don't consider size of the location expression. If we're not
10239 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10240 representation in *SIZEP. */
10242 static bool
10243 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10245 /* Don't output an entry that starts and ends at the same address. */
10246 if (strcmp (curr->begin, curr->end) == 0
10247 && curr->vbegin == curr->vend && !curr->force)
10248 return true;
10250 if (!sizep)
10251 return false;
10253 unsigned long size = size_of_locs (curr->expr);
10255 /* If the expression is too large, drop it on the floor. We could
10256 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10257 in the expression, but >= 64KB expressions for a single value
10258 in a single range are unlikely very useful. */
10259 if (dwarf_version < 5 && size > 0xffff)
10260 return true;
10262 *sizep = size;
10264 return false;
10267 /* Output a view pair loclist entry for CURR, if it requires one. */
10269 static void
10270 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10272 if (!dwarf2out_locviews_in_loclist ())
10273 return;
10275 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10276 return;
10278 #ifdef DW_LLE_view_pair
10279 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10281 if (dwarf2out_as_locview_support)
10283 if (ZERO_VIEW_P (curr->vbegin))
10284 dw2_asm_output_data_uleb128 (0, "Location view begin");
10285 else
10287 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10288 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10289 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10292 if (ZERO_VIEW_P (curr->vend))
10293 dw2_asm_output_data_uleb128 (0, "Location view end");
10294 else
10296 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10297 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10298 dw2_asm_output_symname_uleb128 (label, "Location view end");
10301 else
10303 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10304 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10306 #endif /* DW_LLE_view_pair */
10308 return;
10311 /* Output the location list given to us. */
10313 static void
10314 output_loc_list (dw_loc_list_ref list_head)
10316 int vcount = 0, lcount = 0;
10318 if (list_head->emitted)
10319 return;
10320 list_head->emitted = true;
10322 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10324 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10326 for (dw_loc_list_ref curr = list_head; curr != NULL;
10327 curr = curr->dw_loc_next)
10329 unsigned long size;
10331 if (skip_loc_list_entry (curr, &size))
10332 continue;
10334 vcount++;
10336 /* ?? dwarf_split_debug_info? */
10337 if (dwarf2out_as_locview_support)
10339 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10341 if (!ZERO_VIEW_P (curr->vbegin))
10343 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10344 dw2_asm_output_symname_uleb128 (label,
10345 "View list begin (%s)",
10346 list_head->vl_symbol);
10348 else
10349 dw2_asm_output_data_uleb128 (0,
10350 "View list begin (%s)",
10351 list_head->vl_symbol);
10353 if (!ZERO_VIEW_P (curr->vend))
10355 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10356 dw2_asm_output_symname_uleb128 (label,
10357 "View list end (%s)",
10358 list_head->vl_symbol);
10360 else
10361 dw2_asm_output_data_uleb128 (0,
10362 "View list end (%s)",
10363 list_head->vl_symbol);
10365 else
10367 dw2_asm_output_data_uleb128 (curr->vbegin,
10368 "View list begin (%s)",
10369 list_head->vl_symbol);
10370 dw2_asm_output_data_uleb128 (curr->vend,
10371 "View list end (%s)",
10372 list_head->vl_symbol);
10377 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10379 const char *last_section = NULL;
10380 const char *base_label = NULL;
10382 /* Walk the location list, and output each range + expression. */
10383 for (dw_loc_list_ref curr = list_head; curr != NULL;
10384 curr = curr->dw_loc_next)
10386 unsigned long size;
10388 /* Skip this entry? If we skip it here, we must skip it in the
10389 view list above as well. */
10390 if (skip_loc_list_entry (curr, &size))
10391 continue;
10393 lcount++;
10395 if (dwarf_version >= 5)
10397 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10399 dwarf2out_maybe_output_loclist_view_pair (curr);
10400 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10401 uleb128 index into .debug_addr and uleb128 length. */
10402 dw2_asm_output_data (1, DW_LLE_startx_length,
10403 "DW_LLE_startx_length (%s)",
10404 list_head->ll_symbol);
10405 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10406 "Location list range start index "
10407 "(%s)", curr->begin);
10408 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10409 "Location list length (%s)",
10410 list_head->ll_symbol);
10412 else if (dwarf_split_debug_info)
10414 dwarf2out_maybe_output_loclist_view_pair (curr);
10415 /* For -gsplit-dwarf without usable .uleb128 support, emit
10416 DW_LLE_startx_endx, which has two uleb128 indexes into
10417 .debug_addr. */
10418 dw2_asm_output_data (1, DW_LLE_startx_endx,
10419 "DW_LLE_startx_endx (%s)",
10420 list_head->ll_symbol);
10421 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10422 "Location list range start index "
10423 "(%s)", curr->begin);
10424 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10425 "Location list range end index "
10426 "(%s)", curr->end);
10428 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10430 dwarf2out_maybe_output_loclist_view_pair (curr);
10431 /* If all code is in .text section, the base address is
10432 already provided by the CU attributes. Use
10433 DW_LLE_offset_pair where both addresses are uleb128 encoded
10434 offsets against that base. */
10435 dw2_asm_output_data (1, DW_LLE_offset_pair,
10436 "DW_LLE_offset_pair (%s)",
10437 list_head->ll_symbol);
10438 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10439 "Location list begin address (%s)",
10440 list_head->ll_symbol);
10441 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10442 "Location list end address (%s)",
10443 list_head->ll_symbol);
10445 else if (HAVE_AS_LEB128)
10447 /* Otherwise, find out how many consecutive entries could share
10448 the same base entry. If just one, emit DW_LLE_start_length,
10449 otherwise emit DW_LLE_base_address for the base address
10450 followed by a series of DW_LLE_offset_pair. */
10451 if (last_section == NULL || curr->section != last_section)
10453 dw_loc_list_ref curr2;
10454 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10455 curr2 = curr2->dw_loc_next)
10457 if (strcmp (curr2->begin, curr2->end) == 0
10458 && !curr2->force)
10459 continue;
10460 break;
10462 if (curr2 == NULL || curr->section != curr2->section)
10463 last_section = NULL;
10464 else
10466 last_section = curr->section;
10467 base_label = curr->begin;
10468 dw2_asm_output_data (1, DW_LLE_base_address,
10469 "DW_LLE_base_address (%s)",
10470 list_head->ll_symbol);
10471 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10472 "Base address (%s)",
10473 list_head->ll_symbol);
10476 /* Only one entry with the same base address. Use
10477 DW_LLE_start_length with absolute address and uleb128
10478 length. */
10479 if (last_section == NULL)
10481 dwarf2out_maybe_output_loclist_view_pair (curr);
10482 dw2_asm_output_data (1, DW_LLE_start_length,
10483 "DW_LLE_start_length (%s)",
10484 list_head->ll_symbol);
10485 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10486 "Location list begin address (%s)",
10487 list_head->ll_symbol);
10488 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10489 "Location list length "
10490 "(%s)", list_head->ll_symbol);
10492 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10493 DW_LLE_base_address. */
10494 else
10496 dwarf2out_maybe_output_loclist_view_pair (curr);
10497 dw2_asm_output_data (1, DW_LLE_offset_pair,
10498 "DW_LLE_offset_pair (%s)",
10499 list_head->ll_symbol);
10500 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10501 "Location list begin address "
10502 "(%s)", list_head->ll_symbol);
10503 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10504 "Location list end address "
10505 "(%s)", list_head->ll_symbol);
10508 /* The assembler does not support .uleb128 directive. Emit
10509 DW_LLE_start_end with a pair of absolute addresses. */
10510 else
10512 dwarf2out_maybe_output_loclist_view_pair (curr);
10513 dw2_asm_output_data (1, DW_LLE_start_end,
10514 "DW_LLE_start_end (%s)",
10515 list_head->ll_symbol);
10516 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10517 "Location list begin address (%s)",
10518 list_head->ll_symbol);
10519 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10520 "Location list end address (%s)",
10521 list_head->ll_symbol);
10524 else if (dwarf_split_debug_info)
10526 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10527 and 4 byte length. */
10528 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10529 "Location list start/length entry (%s)",
10530 list_head->ll_symbol);
10531 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10532 "Location list range start index (%s)",
10533 curr->begin);
10534 /* The length field is 4 bytes. If we ever need to support
10535 an 8-byte length, we can add a new DW_LLE code or fall back
10536 to DW_LLE_GNU_start_end_entry. */
10537 dw2_asm_output_delta (4, curr->end, curr->begin,
10538 "Location list range length (%s)",
10539 list_head->ll_symbol);
10541 else if (!have_multiple_function_sections)
10543 /* Pair of relative addresses against start of text section. */
10544 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10545 "Location list begin address (%s)",
10546 list_head->ll_symbol);
10547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10548 "Location list end address (%s)",
10549 list_head->ll_symbol);
10551 else
10553 /* Pair of absolute addresses. */
10554 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10555 "Location list begin address (%s)",
10556 list_head->ll_symbol);
10557 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10558 "Location list end address (%s)",
10559 list_head->ll_symbol);
10562 /* Output the block length for this list of location operations. */
10563 if (dwarf_version >= 5)
10564 dw2_asm_output_data_uleb128 (size, "Location expression size");
10565 else
10567 gcc_assert (size <= 0xffff);
10568 dw2_asm_output_data (2, size, "Location expression size");
10571 output_loc_sequence (curr->expr, -1);
10574 /* And finally list termination. */
10575 if (dwarf_version >= 5)
10576 dw2_asm_output_data (1, DW_LLE_end_of_list,
10577 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10578 else if (dwarf_split_debug_info)
10579 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10580 "Location list terminator (%s)",
10581 list_head->ll_symbol);
10582 else
10584 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10585 "Location list terminator begin (%s)",
10586 list_head->ll_symbol);
10587 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10588 "Location list terminator end (%s)",
10589 list_head->ll_symbol);
10592 gcc_assert (!list_head->vl_symbol
10593 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10596 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10597 section. Emit a relocated reference if val_entry is NULL, otherwise,
10598 emit an indirect reference. */
10600 static void
10601 output_range_list_offset (dw_attr_node *a)
10603 const char *name = dwarf_attr_name (a->dw_attr);
10605 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10607 if (dwarf_version >= 5)
10609 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10610 dw2_asm_output_offset (dwarf_offset_size, r->label,
10611 debug_ranges_section, "%s", name);
10613 else
10615 char *p = strchr (ranges_section_label, '\0');
10616 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10617 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10618 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10619 debug_ranges_section, "%s", name);
10620 *p = '\0';
10623 else if (dwarf_version >= 5)
10625 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10626 gcc_assert (rnglist_idx);
10627 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10629 else
10630 dw2_asm_output_data (dwarf_offset_size,
10631 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10632 "%s (offset from %s)", name, ranges_section_label);
10635 /* Output the offset into the debug_loc section. */
10637 static void
10638 output_loc_list_offset (dw_attr_node *a)
10640 char *sym = AT_loc_list (a)->ll_symbol;
10642 gcc_assert (sym);
10643 if (!dwarf_split_debug_info)
10644 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10645 "%s", dwarf_attr_name (a->dw_attr));
10646 else if (dwarf_version >= 5)
10648 gcc_assert (AT_loc_list (a)->num_assigned);
10649 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10650 dwarf_attr_name (a->dw_attr),
10651 sym);
10653 else
10654 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10655 "%s", dwarf_attr_name (a->dw_attr));
10658 /* Output the offset into the debug_loc section. */
10660 static void
10661 output_view_list_offset (dw_attr_node *a)
10663 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10665 gcc_assert (sym);
10666 if (dwarf_split_debug_info)
10667 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10668 "%s", dwarf_attr_name (a->dw_attr));
10669 else
10670 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10671 "%s", dwarf_attr_name (a->dw_attr));
10674 /* Output an attribute's index or value appropriately. */
10676 static void
10677 output_attr_index_or_value (dw_attr_node *a)
10679 const char *name = dwarf_attr_name (a->dw_attr);
10681 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10683 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10684 return;
10686 switch (AT_class (a))
10688 case dw_val_class_addr:
10689 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10690 break;
10691 case dw_val_class_high_pc:
10692 case dw_val_class_lbl_id:
10693 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10694 break;
10695 default:
10696 gcc_unreachable ();
10700 /* Output a type signature. */
10702 static inline void
10703 output_signature (const char *sig, const char *name)
10705 int i;
10707 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10708 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10711 /* Output a discriminant value. */
10713 static inline void
10714 output_discr_value (dw_discr_value *discr_value, const char *name)
10716 if (discr_value->pos)
10717 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10718 else
10719 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10722 /* Output the DIE and its attributes. Called recursively to generate
10723 the definitions of each child DIE. */
10725 static void
10726 output_die (dw_die_ref die)
10728 dw_attr_node *a;
10729 dw_die_ref c;
10730 unsigned long size;
10731 unsigned ix;
10733 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10734 (unsigned long)die->die_offset,
10735 dwarf_tag_name (die->die_tag));
10737 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10739 const char *name = dwarf_attr_name (a->dw_attr);
10741 switch (AT_class (a))
10743 case dw_val_class_addr:
10744 output_attr_index_or_value (a);
10745 break;
10747 case dw_val_class_offset:
10748 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10749 "%s", name);
10750 break;
10752 case dw_val_class_range_list:
10753 output_range_list_offset (a);
10754 break;
10756 case dw_val_class_loc:
10757 size = size_of_locs (AT_loc (a));
10759 /* Output the block length for this list of location operations. */
10760 if (dwarf_version >= 4)
10761 dw2_asm_output_data_uleb128 (size, "%s", name);
10762 else
10763 dw2_asm_output_data (constant_size (size), size, "%s", name);
10765 output_loc_sequence (AT_loc (a), -1);
10766 break;
10768 case dw_val_class_const:
10769 /* ??? It would be slightly more efficient to use a scheme like is
10770 used for unsigned constants below, but gdb 4.x does not sign
10771 extend. Gdb 5.x does sign extend. */
10772 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10773 break;
10775 case dw_val_class_unsigned_const:
10777 int csize = constant_size (AT_unsigned (a));
10778 if (dwarf_version == 3
10779 && a->dw_attr == DW_AT_data_member_location
10780 && csize >= 4)
10781 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10782 else
10783 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10785 break;
10787 case dw_val_class_symview:
10789 int vsize;
10790 if (symview_upper_bound <= 0xff)
10791 vsize = 1;
10792 else if (symview_upper_bound <= 0xffff)
10793 vsize = 2;
10794 else if (symview_upper_bound <= 0xffffffff)
10795 vsize = 4;
10796 else
10797 vsize = 8;
10798 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10799 "%s", name);
10801 break;
10803 case dw_val_class_const_implicit:
10804 if (flag_debug_asm)
10805 fprintf (asm_out_file, "\t\t\t%s %s ("
10806 HOST_WIDE_INT_PRINT_DEC ")\n",
10807 ASM_COMMENT_START, name, AT_int (a));
10808 break;
10810 case dw_val_class_unsigned_const_implicit:
10811 if (flag_debug_asm)
10812 fprintf (asm_out_file, "\t\t\t%s %s ("
10813 HOST_WIDE_INT_PRINT_HEX ")\n",
10814 ASM_COMMENT_START, name, AT_unsigned (a));
10815 break;
10817 case dw_val_class_const_double:
10819 unsigned HOST_WIDE_INT first, second;
10821 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10822 dw2_asm_output_data (1,
10823 HOST_BITS_PER_DOUBLE_INT
10824 / HOST_BITS_PER_CHAR,
10825 NULL);
10827 if (WORDS_BIG_ENDIAN)
10829 first = a->dw_attr_val.v.val_double.high;
10830 second = a->dw_attr_val.v.val_double.low;
10832 else
10834 first = a->dw_attr_val.v.val_double.low;
10835 second = a->dw_attr_val.v.val_double.high;
10838 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10839 first, "%s", name);
10840 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10841 second, NULL);
10843 break;
10845 case dw_val_class_wide_int:
10847 int i;
10848 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10849 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10850 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10851 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10852 * l, NULL);
10854 if (WORDS_BIG_ENDIAN)
10855 for (i = len - 1; i >= 0; --i)
10857 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10858 "%s", name);
10859 name = "";
10861 else
10862 for (i = 0; i < len; ++i)
10864 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10865 "%s", name);
10866 name = "";
10869 break;
10871 case dw_val_class_vec:
10873 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10874 unsigned int len = a->dw_attr_val.v.val_vec.length;
10875 unsigned int i;
10876 unsigned char *p;
10878 dw2_asm_output_data (constant_size (len * elt_size),
10879 len * elt_size, "%s", name);
10880 if (elt_size > sizeof (HOST_WIDE_INT))
10882 elt_size /= 2;
10883 len *= 2;
10885 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10886 i < len;
10887 i++, p += elt_size)
10888 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10889 "fp or vector constant word %u", i);
10890 break;
10893 case dw_val_class_flag:
10894 if (dwarf_version >= 4)
10896 /* Currently all add_AT_flag calls pass in 1 as last argument,
10897 so DW_FORM_flag_present can be used. If that ever changes,
10898 we'll need to use DW_FORM_flag and have some optimization
10899 in build_abbrev_table that will change those to
10900 DW_FORM_flag_present if it is set to 1 in all DIEs using
10901 the same abbrev entry. */
10902 gcc_assert (AT_flag (a) == 1);
10903 if (flag_debug_asm)
10904 fprintf (asm_out_file, "\t\t\t%s %s\n",
10905 ASM_COMMENT_START, name);
10906 break;
10908 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10909 break;
10911 case dw_val_class_loc_list:
10912 output_loc_list_offset (a);
10913 break;
10915 case dw_val_class_view_list:
10916 output_view_list_offset (a);
10917 break;
10919 case dw_val_class_die_ref:
10920 if (AT_ref_external (a))
10922 if (AT_ref (a)->comdat_type_p)
10924 comdat_type_node *type_node
10925 = AT_ref (a)->die_id.die_type_node;
10927 gcc_assert (type_node);
10928 output_signature (type_node->signature, name);
10930 else
10932 const char *sym = AT_ref (a)->die_id.die_symbol;
10933 int size;
10935 gcc_assert (sym);
10936 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10937 length, whereas in DWARF3 it's always sized as an
10938 offset. */
10939 if (dwarf_version == 2)
10940 size = DWARF2_ADDR_SIZE;
10941 else
10942 size = dwarf_offset_size;
10943 /* ??? We cannot unconditionally output die_offset if
10944 non-zero - others might create references to those
10945 DIEs via symbols.
10946 And we do not clear its DIE offset after outputting it
10947 (and the label refers to the actual DIEs, not the
10948 DWARF CU unit header which is when using label + offset
10949 would be the correct thing to do).
10950 ??? This is the reason for the with_offset flag. */
10951 if (AT_ref (a)->with_offset)
10952 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10953 debug_info_section, "%s", name);
10954 else
10955 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10956 name);
10959 else
10961 gcc_assert (AT_ref (a)->die_offset);
10962 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
10963 "%s", name);
10965 break;
10967 case dw_val_class_fde_ref:
10969 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10971 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10972 a->dw_attr_val.v.val_fde_index * 2);
10973 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
10974 "%s", name);
10976 break;
10978 case dw_val_class_vms_delta:
10979 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10980 dw2_asm_output_vms_delta (dwarf_offset_size,
10981 AT_vms_delta2 (a), AT_vms_delta1 (a),
10982 "%s", name);
10983 #else
10984 dw2_asm_output_delta (dwarf_offset_size,
10985 AT_vms_delta2 (a), AT_vms_delta1 (a),
10986 "%s", name);
10987 #endif
10988 break;
10990 case dw_val_class_lbl_id:
10991 output_attr_index_or_value (a);
10992 break;
10994 case dw_val_class_lineptr:
10995 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
10996 debug_line_section, "%s", name);
10997 break;
10999 case dw_val_class_macptr:
11000 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11001 debug_macinfo_section, "%s", name);
11002 break;
11004 case dw_val_class_loclistsptr:
11005 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11006 debug_loc_section, "%s", name);
11007 break;
11009 case dw_val_class_str:
11010 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11011 dw2_asm_output_offset (dwarf_offset_size,
11012 a->dw_attr_val.v.val_str->label,
11013 debug_str_section,
11014 "%s: \"%s\"", name, AT_string (a));
11015 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11016 dw2_asm_output_offset (dwarf_offset_size,
11017 a->dw_attr_val.v.val_str->label,
11018 debug_line_str_section,
11019 "%s: \"%s\"", name, AT_string (a));
11020 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11021 dw2_asm_output_data_uleb128 (AT_index (a),
11022 "%s: \"%s\"", name, AT_string (a));
11023 else
11024 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11025 break;
11027 case dw_val_class_file:
11029 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11031 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11032 a->dw_attr_val.v.val_file->filename);
11033 break;
11036 case dw_val_class_file_implicit:
11037 if (flag_debug_asm)
11038 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11039 ASM_COMMENT_START, name,
11040 maybe_emit_file (a->dw_attr_val.v.val_file),
11041 a->dw_attr_val.v.val_file->filename);
11042 break;
11044 case dw_val_class_data8:
11046 int i;
11048 for (i = 0; i < 8; i++)
11049 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11050 i == 0 ? "%s" : NULL, name);
11051 break;
11054 case dw_val_class_high_pc:
11055 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11056 get_AT_low_pc (die), "DW_AT_high_pc");
11057 break;
11059 case dw_val_class_discr_value:
11060 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11061 break;
11063 case dw_val_class_discr_list:
11065 dw_discr_list_ref list = AT_discr_list (a);
11066 const int size = size_of_discr_list (list);
11068 /* This is a block, so output its length first. */
11069 dw2_asm_output_data (constant_size (size), size,
11070 "%s: block size", name);
11072 for (; list != NULL; list = list->dw_discr_next)
11074 /* One byte for the discriminant value descriptor, and then as
11075 many LEB128 numbers as required. */
11076 if (list->dw_discr_range)
11077 dw2_asm_output_data (1, DW_DSC_range,
11078 "%s: DW_DSC_range", name);
11079 else
11080 dw2_asm_output_data (1, DW_DSC_label,
11081 "%s: DW_DSC_label", name);
11083 output_discr_value (&list->dw_discr_lower_bound, name);
11084 if (list->dw_discr_range)
11085 output_discr_value (&list->dw_discr_upper_bound, name);
11087 break;
11090 default:
11091 gcc_unreachable ();
11095 FOR_EACH_CHILD (die, c, output_die (c));
11097 /* Add null byte to terminate sibling list. */
11098 if (die->die_child != NULL)
11099 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11100 (unsigned long) die->die_offset);
11103 /* Output the dwarf version number. */
11105 static void
11106 output_dwarf_version ()
11108 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11109 views in loclist. That will change eventually. */
11110 if (dwarf_version == 6)
11112 static bool once;
11113 if (!once)
11115 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11116 "incompatibilities");
11117 once = true;
11119 dw2_asm_output_data (2, 5, "DWARF version number");
11121 else
11122 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11125 /* Output the compilation unit that appears at the beginning of the
11126 .debug_info section, and precedes the DIE descriptions. */
11128 static void
11129 output_compilation_unit_header (enum dwarf_unit_type ut)
11131 if (!XCOFF_DEBUGGING_INFO)
11133 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11134 dw2_asm_output_data (4, 0xffffffff,
11135 "Initial length escape value indicating 64-bit DWARF extension");
11136 dw2_asm_output_data (dwarf_offset_size,
11137 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11138 "Length of Compilation Unit Info");
11141 output_dwarf_version ();
11142 if (dwarf_version >= 5)
11144 const char *name;
11145 switch (ut)
11147 case DW_UT_compile: name = "DW_UT_compile"; break;
11148 case DW_UT_type: name = "DW_UT_type"; break;
11149 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11150 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11151 default: gcc_unreachable ();
11153 dw2_asm_output_data (1, ut, "%s", name);
11154 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11156 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11157 debug_abbrev_section,
11158 "Offset Into Abbrev. Section");
11159 if (dwarf_version < 5)
11160 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11163 /* Output the compilation unit DIE and its children. */
11165 static void
11166 output_comp_unit (dw_die_ref die, int output_if_empty,
11167 const unsigned char *dwo_id)
11169 const char *secname, *oldsym;
11170 char *tmp;
11172 /* Unless we are outputting main CU, we may throw away empty ones. */
11173 if (!output_if_empty && die->die_child == NULL)
11174 return;
11176 /* Even if there are no children of this DIE, we must output the information
11177 about the compilation unit. Otherwise, on an empty translation unit, we
11178 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11179 will then complain when examining the file. First mark all the DIEs in
11180 this CU so we know which get local refs. */
11181 mark_dies (die);
11183 external_ref_hash_type *extern_map = optimize_external_refs (die);
11185 /* For now, optimize only the main CU, in order to optimize the rest
11186 we'd need to see all of them earlier. Leave the rest for post-linking
11187 tools like DWZ. */
11188 if (die == comp_unit_die ())
11189 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11191 build_abbrev_table (die, extern_map);
11193 optimize_abbrev_table ();
11195 delete extern_map;
11197 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11198 next_die_offset = (dwo_id
11199 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11200 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11201 calc_die_sizes (die);
11203 oldsym = die->die_id.die_symbol;
11204 if (oldsym && die->comdat_type_p)
11206 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11208 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11209 secname = tmp;
11210 die->die_id.die_symbol = NULL;
11211 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11213 else
11215 switch_to_section (debug_info_section);
11216 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11217 info_section_emitted = true;
11220 /* For LTO cross unit DIE refs we want a symbol on the start of the
11221 debuginfo section, not on the CU DIE. */
11222 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11224 /* ??? No way to get visibility assembled without a decl. */
11225 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11226 get_identifier (oldsym), char_type_node);
11227 TREE_PUBLIC (decl) = true;
11228 TREE_STATIC (decl) = true;
11229 DECL_ARTIFICIAL (decl) = true;
11230 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11231 DECL_VISIBILITY_SPECIFIED (decl) = true;
11232 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11233 #ifdef ASM_WEAKEN_LABEL
11234 /* We prefer a .weak because that handles duplicates from duplicate
11235 archive members in a graceful way. */
11236 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11237 #else
11238 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11239 #endif
11240 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11243 /* Output debugging information. */
11244 output_compilation_unit_header (dwo_id
11245 ? DW_UT_split_compile : DW_UT_compile);
11246 if (dwarf_version >= 5)
11248 if (dwo_id != NULL)
11249 for (int i = 0; i < 8; i++)
11250 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11252 output_die (die);
11254 /* Leave the marks on the main CU, so we can check them in
11255 output_pubnames. */
11256 if (oldsym)
11258 unmark_dies (die);
11259 die->die_id.die_symbol = oldsym;
11263 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11264 and .debug_pubtypes. This is configured per-target, but can be
11265 overridden by the -gpubnames or -gno-pubnames options. */
11267 static inline bool
11268 want_pubnames (void)
11270 if (debug_info_level <= DINFO_LEVEL_TERSE
11271 /* Names and types go to the early debug part only. */
11272 || in_lto_p)
11273 return false;
11274 if (debug_generate_pub_sections != -1)
11275 return debug_generate_pub_sections;
11276 return targetm.want_debug_pub_sections;
11279 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11281 static void
11282 add_AT_pubnames (dw_die_ref die)
11284 if (want_pubnames ())
11285 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11288 /* Add a string attribute value to a skeleton DIE. */
11290 static inline void
11291 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11292 const char *str)
11294 dw_attr_node attr;
11295 struct indirect_string_node *node;
11297 if (! skeleton_debug_str_hash)
11298 skeleton_debug_str_hash
11299 = hash_table<indirect_string_hasher>::create_ggc (10);
11301 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11302 find_string_form (node);
11303 if (node->form == dwarf_FORM (DW_FORM_strx))
11304 node->form = DW_FORM_strp;
11306 attr.dw_attr = attr_kind;
11307 attr.dw_attr_val.val_class = dw_val_class_str;
11308 attr.dw_attr_val.val_entry = NULL;
11309 attr.dw_attr_val.v.val_str = node;
11310 add_dwarf_attr (die, &attr);
11313 /* Helper function to generate top-level dies for skeleton debug_info and
11314 debug_types. */
11316 static void
11317 add_top_level_skeleton_die_attrs (dw_die_ref die)
11319 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11320 const char *comp_dir = comp_dir_string ();
11322 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11323 if (comp_dir != NULL)
11324 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11325 add_AT_pubnames (die);
11326 if (addr_index_table != NULL && addr_index_table->size () > 0)
11327 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11330 /* Output skeleton debug sections that point to the dwo file. */
11332 static void
11333 output_skeleton_debug_sections (dw_die_ref comp_unit,
11334 const unsigned char *dwo_id)
11336 /* These attributes will be found in the full debug_info section. */
11337 remove_AT (comp_unit, DW_AT_producer);
11338 remove_AT (comp_unit, DW_AT_language);
11340 switch_to_section (debug_skeleton_info_section);
11341 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11343 /* Produce the skeleton compilation-unit header. This one differs enough from
11344 a normal CU header that it's better not to call output_compilation_unit
11345 header. */
11346 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11347 dw2_asm_output_data (4, 0xffffffff,
11348 "Initial length escape value indicating 64-bit "
11349 "DWARF extension");
11351 dw2_asm_output_data (dwarf_offset_size,
11352 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11353 - DWARF_INITIAL_LENGTH_SIZE
11354 + size_of_die (comp_unit),
11355 "Length of Compilation Unit Info");
11356 output_dwarf_version ();
11357 if (dwarf_version >= 5)
11359 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11360 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11362 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11363 debug_skeleton_abbrev_section,
11364 "Offset Into Abbrev. Section");
11365 if (dwarf_version < 5)
11366 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11367 else
11368 for (int i = 0; i < 8; i++)
11369 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11371 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11372 output_die (comp_unit);
11374 /* Build the skeleton debug_abbrev section. */
11375 switch_to_section (debug_skeleton_abbrev_section);
11376 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11378 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11380 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11383 /* Output a comdat type unit DIE and its children. */
11385 static void
11386 output_comdat_type_unit (comdat_type_node *node,
11387 bool early_lto_debug ATTRIBUTE_UNUSED)
11389 const char *secname;
11390 char *tmp;
11391 int i;
11392 #if defined (OBJECT_FORMAT_ELF)
11393 tree comdat_key;
11394 #endif
11396 /* First mark all the DIEs in this CU so we know which get local refs. */
11397 mark_dies (node->root_die);
11399 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11401 build_abbrev_table (node->root_die, extern_map);
11403 delete extern_map;
11404 extern_map = NULL;
11406 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11407 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11408 calc_die_sizes (node->root_die);
11410 #if defined (OBJECT_FORMAT_ELF)
11411 if (dwarf_version >= 5)
11413 if (!dwarf_split_debug_info)
11414 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11415 else
11416 secname = (early_lto_debug
11417 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11419 else if (!dwarf_split_debug_info)
11420 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11421 else
11422 secname = (early_lto_debug
11423 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11425 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11426 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11427 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11428 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11429 comdat_key = get_identifier (tmp);
11430 targetm.asm_out.named_section (secname,
11431 SECTION_DEBUG | SECTION_LINKONCE,
11432 comdat_key);
11433 #else
11434 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11435 sprintf (tmp, (dwarf_version >= 5
11436 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11437 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11438 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11439 secname = tmp;
11440 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11441 #endif
11443 /* Output debugging information. */
11444 output_compilation_unit_header (dwarf_split_debug_info
11445 ? DW_UT_split_type : DW_UT_type);
11446 output_signature (node->signature, "Type Signature");
11447 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11448 "Offset to Type DIE");
11449 output_die (node->root_die);
11451 unmark_dies (node->root_die);
11454 /* Return the DWARF2/3 pubname associated with a decl. */
11456 static const char *
11457 dwarf2_name (tree decl, int scope)
11459 if (DECL_NAMELESS (decl))
11460 return NULL;
11461 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11464 /* Add a new entry to .debug_pubnames if appropriate. */
11466 static void
11467 add_pubname_string (const char *str, dw_die_ref die)
11469 pubname_entry e;
11471 e.die = die;
11472 e.name = xstrdup (str);
11473 vec_safe_push (pubname_table, e);
11476 static void
11477 add_pubname (tree decl, dw_die_ref die)
11479 if (!want_pubnames ())
11480 return;
11482 /* Don't add items to the table when we expect that the consumer will have
11483 just read the enclosing die. For example, if the consumer is looking at a
11484 class_member, it will either be inside the class already, or will have just
11485 looked up the class to find the member. Either way, searching the class is
11486 faster than searching the index. */
11487 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11488 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11490 const char *name = dwarf2_name (decl, 1);
11492 if (name)
11493 add_pubname_string (name, die);
11497 /* Add an enumerator to the pubnames section. */
11499 static void
11500 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11502 pubname_entry e;
11504 gcc_assert (scope_name);
11505 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11506 e.die = die;
11507 vec_safe_push (pubname_table, e);
11510 /* Add a new entry to .debug_pubtypes if appropriate. */
11512 static void
11513 add_pubtype (tree decl, dw_die_ref die)
11515 pubname_entry e;
11517 if (!want_pubnames ())
11518 return;
11520 if ((TREE_PUBLIC (decl)
11521 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11522 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11524 tree scope = NULL;
11525 const char *scope_name = "";
11526 const char *sep = is_cxx () ? "::" : ".";
11527 const char *name;
11529 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11530 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11532 scope_name = lang_hooks.dwarf_name (scope, 1);
11533 if (scope_name != NULL && scope_name[0] != '\0')
11534 scope_name = concat (scope_name, sep, NULL);
11535 else
11536 scope_name = "";
11539 if (TYPE_P (decl))
11540 name = type_tag (decl);
11541 else
11542 name = lang_hooks.dwarf_name (decl, 1);
11544 /* If we don't have a name for the type, there's no point in adding
11545 it to the table. */
11546 if (name != NULL && name[0] != '\0')
11548 e.die = die;
11549 e.name = concat (scope_name, name, NULL);
11550 vec_safe_push (pubtype_table, e);
11553 /* Although it might be more consistent to add the pubinfo for the
11554 enumerators as their dies are created, they should only be added if the
11555 enum type meets the criteria above. So rather than re-check the parent
11556 enum type whenever an enumerator die is created, just output them all
11557 here. This isn't protected by the name conditional because anonymous
11558 enums don't have names. */
11559 if (die->die_tag == DW_TAG_enumeration_type)
11561 dw_die_ref c;
11563 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11568 /* Output a single entry in the pubnames table. */
11570 static void
11571 output_pubname (dw_offset die_offset, pubname_entry *entry)
11573 dw_die_ref die = entry->die;
11574 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11576 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11578 if (debug_generate_pub_sections == 2)
11580 /* This logic follows gdb's method for determining the value of the flag
11581 byte. */
11582 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11583 switch (die->die_tag)
11585 case DW_TAG_typedef:
11586 case DW_TAG_base_type:
11587 case DW_TAG_subrange_type:
11588 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11589 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11590 break;
11591 case DW_TAG_enumerator:
11592 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11593 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11594 if (!is_cxx ())
11595 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11596 break;
11597 case DW_TAG_subprogram:
11598 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11599 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11600 if (!is_ada ())
11601 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11602 break;
11603 case DW_TAG_constant:
11604 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11605 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11606 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11607 break;
11608 case DW_TAG_variable:
11609 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11610 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11611 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11612 break;
11613 case DW_TAG_namespace:
11614 case DW_TAG_imported_declaration:
11615 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11616 break;
11617 case DW_TAG_class_type:
11618 case DW_TAG_interface_type:
11619 case DW_TAG_structure_type:
11620 case DW_TAG_union_type:
11621 case DW_TAG_enumeration_type:
11622 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11623 if (!is_cxx ())
11624 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11625 break;
11626 default:
11627 /* An unusual tag. Leave the flag-byte empty. */
11628 break;
11630 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11631 "GDB-index flags");
11634 dw2_asm_output_nstring (entry->name, -1, "external name");
11638 /* Output the public names table used to speed up access to externally
11639 visible names; or the public types table used to find type definitions. */
11641 static void
11642 output_pubnames (vec<pubname_entry, va_gc> *names)
11644 unsigned i;
11645 unsigned long pubnames_length = size_of_pubnames (names);
11646 pubname_entry *pub;
11648 if (!XCOFF_DEBUGGING_INFO)
11650 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11651 dw2_asm_output_data (4, 0xffffffff,
11652 "Initial length escape value indicating 64-bit DWARF extension");
11653 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11654 "Pub Info Length");
11657 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11658 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11660 if (dwarf_split_debug_info)
11661 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11662 debug_skeleton_info_section,
11663 "Offset of Compilation Unit Info");
11664 else
11665 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11666 debug_info_section,
11667 "Offset of Compilation Unit Info");
11668 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11669 "Compilation Unit Length");
11671 FOR_EACH_VEC_ELT (*names, i, pub)
11673 if (include_pubname_in_output (names, pub))
11675 dw_offset die_offset = pub->die->die_offset;
11677 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11678 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11679 gcc_assert (pub->die->die_mark);
11681 /* If we're putting types in their own .debug_types sections,
11682 the .debug_pubtypes table will still point to the compile
11683 unit (not the type unit), so we want to use the offset of
11684 the skeleton DIE (if there is one). */
11685 if (pub->die->comdat_type_p && names == pubtype_table)
11687 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11689 if (type_node != NULL)
11690 die_offset = (type_node->skeleton_die != NULL
11691 ? type_node->skeleton_die->die_offset
11692 : comp_unit_die ()->die_offset);
11695 output_pubname (die_offset, pub);
11699 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11702 /* Output public names and types tables if necessary. */
11704 static void
11705 output_pubtables (void)
11707 if (!want_pubnames () || !info_section_emitted)
11708 return;
11710 switch_to_section (debug_pubnames_section);
11711 output_pubnames (pubname_table);
11712 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11713 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11714 simply won't look for the section. */
11715 switch_to_section (debug_pubtypes_section);
11716 output_pubnames (pubtype_table);
11720 /* Output the information that goes into the .debug_aranges table.
11721 Namely, define the beginning and ending address range of the
11722 text section generated for this compilation unit. */
11724 static void
11725 output_aranges (void)
11727 unsigned i;
11728 unsigned long aranges_length = size_of_aranges ();
11730 if (!XCOFF_DEBUGGING_INFO)
11732 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11733 dw2_asm_output_data (4, 0xffffffff,
11734 "Initial length escape value indicating 64-bit DWARF extension");
11735 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11736 "Length of Address Ranges Info");
11739 /* Version number for aranges is still 2, even up to DWARF5. */
11740 dw2_asm_output_data (2, 2, "DWARF aranges version");
11741 if (dwarf_split_debug_info)
11742 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11743 debug_skeleton_info_section,
11744 "Offset of Compilation Unit Info");
11745 else
11746 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11747 debug_info_section,
11748 "Offset of Compilation Unit Info");
11749 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11750 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11752 /* We need to align to twice the pointer size here. */
11753 if (DWARF_ARANGES_PAD_SIZE)
11755 /* Pad using a 2 byte words so that padding is correct for any
11756 pointer size. */
11757 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11758 2 * DWARF2_ADDR_SIZE);
11759 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11760 dw2_asm_output_data (2, 0, NULL);
11763 /* It is necessary not to output these entries if the sections were
11764 not used; if the sections were not used, the length will be 0 and
11765 the address may end up as 0 if the section is discarded by ld
11766 --gc-sections, leaving an invalid (0, 0) entry that can be
11767 confused with the terminator. */
11768 if (switch_text_ranges)
11770 const char *prev_loc = text_section_label;
11771 const char *loc;
11772 unsigned idx;
11774 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11775 if (prev_loc)
11777 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11778 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11779 prev_loc = NULL;
11781 else
11782 prev_loc = loc;
11784 if (prev_loc)
11786 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11787 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11788 prev_loc, "Length");
11792 if (switch_cold_ranges)
11794 const char *prev_loc = cold_text_section_label;
11795 const char *loc;
11796 unsigned idx;
11798 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11799 if (prev_loc)
11801 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11802 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11803 prev_loc = NULL;
11805 else
11806 prev_loc = loc;
11808 if (prev_loc)
11810 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11811 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11812 prev_loc, "Length");
11816 if (have_multiple_function_sections)
11818 unsigned fde_idx;
11819 dw_fde_ref fde;
11821 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11823 if (fde->ignored_debug)
11824 continue;
11825 if (!fde->in_std_section)
11827 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11828 "Address");
11829 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11830 fde->dw_fde_begin, "Length");
11832 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11834 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11835 "Address");
11836 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11837 fde->dw_fde_second_begin, "Length");
11842 /* Output the terminator words. */
11843 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11844 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11847 /* Add a new entry to .debug_ranges. Return its index into
11848 ranges_table vector. */
11850 static unsigned int
11851 add_ranges_num (int num, bool maybe_new_sec)
11853 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11854 vec_safe_push (ranges_table, r);
11855 return vec_safe_length (ranges_table) - 1;
11858 /* Add a new entry to .debug_ranges corresponding to a block, or a
11859 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11860 this entry might be in a different section from previous range. */
11862 static unsigned int
11863 add_ranges (const_tree block, bool maybe_new_sec)
11865 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11868 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11869 chain, or middle entry of a chain that will be directly referred to. */
11871 static void
11872 note_rnglist_head (unsigned int offset)
11874 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11875 return;
11876 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11879 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11880 When using dwarf_split_debug_info, address attributes in dies destined
11881 for the final executable should be direct references--setting the
11882 parameter force_direct ensures this behavior. */
11884 static void
11885 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11886 bool *added, bool force_direct)
11888 unsigned int in_use = vec_safe_length (ranges_by_label);
11889 unsigned int offset;
11890 dw_ranges_by_label rbl = { begin, end };
11891 vec_safe_push (ranges_by_label, rbl);
11892 offset = add_ranges_num (-(int)in_use - 1, true);
11893 if (!*added)
11895 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11896 *added = true;
11897 note_rnglist_head (offset);
11898 if (dwarf_split_debug_info && force_direct)
11899 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11903 /* Emit .debug_ranges section. */
11905 static void
11906 output_ranges (void)
11908 unsigned i;
11909 static const char *const start_fmt = "Offset %#x";
11910 const char *fmt = start_fmt;
11911 dw_ranges *r;
11913 switch_to_section (debug_ranges_section);
11914 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11915 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11917 int block_num = r->num;
11919 if (block_num > 0)
11921 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11922 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11924 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11925 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11927 /* If all code is in the text section, then the compilation
11928 unit base address defaults to DW_AT_low_pc, which is the
11929 base of the text section. */
11930 if (!have_multiple_function_sections)
11932 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11933 text_section_label,
11934 fmt, i * 2 * DWARF2_ADDR_SIZE);
11935 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11936 text_section_label, NULL);
11939 /* Otherwise, the compilation unit base address is zero,
11940 which allows us to use absolute addresses, and not worry
11941 about whether the target supports cross-section
11942 arithmetic. */
11943 else
11945 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11946 fmt, i * 2 * DWARF2_ADDR_SIZE);
11947 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11950 fmt = NULL;
11953 /* Negative block_num stands for an index into ranges_by_label. */
11954 else if (block_num < 0)
11956 int lab_idx = - block_num - 1;
11958 if (!have_multiple_function_sections)
11960 gcc_unreachable ();
11961 #if 0
11962 /* If we ever use add_ranges_by_labels () for a single
11963 function section, all we have to do is to take out
11964 the #if 0 above. */
11965 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11966 (*ranges_by_label)[lab_idx].begin,
11967 text_section_label,
11968 fmt, i * 2 * DWARF2_ADDR_SIZE);
11969 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11970 (*ranges_by_label)[lab_idx].end,
11971 text_section_label, NULL);
11972 #endif
11974 else
11976 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11977 (*ranges_by_label)[lab_idx].begin,
11978 fmt, i * 2 * DWARF2_ADDR_SIZE);
11979 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11980 (*ranges_by_label)[lab_idx].end,
11981 NULL);
11984 else
11986 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11987 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11988 fmt = start_fmt;
11993 /* Non-zero if .debug_line_str should be used for .debug_line section
11994 strings or strings that are likely shareable with those. */
11995 #define DWARF5_USE_DEBUG_LINE_STR \
11996 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11997 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11998 /* FIXME: there is no .debug_line_str.dwo section, \
11999 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12000 && !dwarf_split_debug_info)
12003 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12004 DWARF5 .debug_line tables using .debug_line_str or we generate
12005 it ourselves, except for split-dwarf which doesn't have a
12006 .debug_line_str. */
12007 static bool
12008 asm_outputs_debug_line_str (void)
12010 if (dwarf_version >= 5
12011 && ! output_asm_line_debug_info ()
12012 && DWARF5_USE_DEBUG_LINE_STR)
12013 return true;
12014 else
12016 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12017 return !dwarf_split_debug_info && dwarf_version >= 5;
12018 #else
12019 return false;
12020 #endif
12024 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12025 I is index of the following range. */
12027 static bool
12028 use_distinct_base_address_for_range (unsigned int i)
12030 if (i >= vec_safe_length (ranges_table))
12031 return false;
12033 dw_ranges *r2 = &(*ranges_table)[i];
12034 /* Use DW_RLE_base_address{,x} if there is a next range in the
12035 range list and is guaranteed to be in the same section. */
12036 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12039 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12040 section when needed. */
12042 static void
12043 index_rnglists (void)
12045 unsigned i;
12046 dw_ranges *r;
12047 bool base = false;
12049 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12051 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12052 r->idx = rnglist_idx++;
12054 if (!have_multiple_function_sections)
12055 continue;
12056 int block_num = r->num;
12057 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12058 base = false;
12059 if (block_num > 0)
12061 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12062 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12064 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12065 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12067 if (HAVE_AS_LEB128)
12069 if (!base && use_distinct_base_address_for_range (i + 1))
12071 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12072 ate_kind_label);
12073 base = true;
12075 if (base)
12076 /* If we have a base, no need for further
12077 begin_entry/end_entry, as DW_RLE_offset_pair will be
12078 used. */
12079 continue;
12080 r->begin_entry
12081 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12082 /* No need for end_entry, DW_RLE_start{,x}_length will use
12083 length as opposed to a pair of addresses. */
12085 else
12087 r->begin_entry
12088 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12089 r->end_entry
12090 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12094 /* Negative block_num stands for an index into ranges_by_label. */
12095 else if (block_num < 0)
12097 int lab_idx = - block_num - 1;
12098 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12099 const char *elabel = (*ranges_by_label)[lab_idx].end;
12101 r->begin_entry
12102 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12103 if (!HAVE_AS_LEB128)
12104 r->end_entry
12105 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12110 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12112 static bool
12113 output_rnglists (unsigned generation, bool dwo)
12115 unsigned i;
12116 dw_ranges *r;
12117 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12118 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12119 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12121 if (dwo)
12122 switch_to_section (debug_ranges_dwo_section);
12123 else
12125 switch_to_section (debug_ranges_section);
12126 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12128 /* There are up to 4 unique ranges labels per generation.
12129 See also init_sections_and_labels. */
12130 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12131 2 + 2 * dwo + generation * 6);
12132 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12133 3 + 2 * dwo + generation * 6);
12134 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12135 dw2_asm_output_data (4, 0xffffffff,
12136 "Initial length escape value indicating "
12137 "64-bit DWARF extension");
12138 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12139 "Length of Range Lists");
12140 ASM_OUTPUT_LABEL (asm_out_file, l1);
12141 output_dwarf_version ();
12142 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12143 dw2_asm_output_data (1, 0, "Segment Size");
12144 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12145 about relocation sizes and primarily care about the size of .debug*
12146 sections in linked shared libraries and executables, then
12147 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12148 into it are usually larger than just DW_FORM_sec_offset offsets
12149 into the .debug_rnglists section. */
12150 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12151 "Offset Entry Count");
12152 if (dwo)
12154 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12155 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12156 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12157 dw2_asm_output_delta (dwarf_offset_size, r->label,
12158 ranges_base_label, NULL);
12161 const char *lab = "";
12162 const char *base = NULL;
12163 bool skipping = false;
12164 bool ret = false;
12165 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12167 int block_num = r->num;
12169 if (r->label)
12171 if (dwarf_split_debug_info
12172 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12174 ret = true;
12175 skipping = true;
12176 continue;
12178 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12179 lab = r->label;
12181 if (skipping)
12183 if (block_num == 0)
12184 skipping = false;
12185 continue;
12187 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12188 base = NULL;
12189 if (block_num > 0)
12191 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12192 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12194 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12195 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12197 if (HAVE_AS_LEB128)
12199 /* If all code is in the text section, then the compilation
12200 unit base address defaults to DW_AT_low_pc, which is the
12201 base of the text section. */
12202 if (!have_multiple_function_sections)
12204 dw2_asm_output_data (1, DW_RLE_offset_pair,
12205 "DW_RLE_offset_pair (%s)", lab);
12206 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12207 "Range begin address (%s)", lab);
12208 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12209 "Range end address (%s)", lab);
12210 continue;
12212 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12214 if (dwarf_split_debug_info)
12216 dw2_asm_output_data (1, DW_RLE_base_addressx,
12217 "DW_RLE_base_addressx (%s)", lab);
12218 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12219 "Base address index (%s)",
12220 blabel);
12222 else
12224 dw2_asm_output_data (1, DW_RLE_base_address,
12225 "DW_RLE_base_address (%s)", lab);
12226 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12227 "Base address (%s)", lab);
12229 strcpy (basebuf, blabel);
12230 base = basebuf;
12232 if (base)
12234 dw2_asm_output_data (1, DW_RLE_offset_pair,
12235 "DW_RLE_offset_pair (%s)", lab);
12236 dw2_asm_output_delta_uleb128 (blabel, base,
12237 "Range begin address (%s)", lab);
12238 dw2_asm_output_delta_uleb128 (elabel, base,
12239 "Range end address (%s)", lab);
12240 continue;
12242 if (dwarf_split_debug_info)
12244 dw2_asm_output_data (1, DW_RLE_startx_length,
12245 "DW_RLE_startx_length (%s)", lab);
12246 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12247 "Range begin address index "
12248 "(%s)", blabel);
12250 else
12252 dw2_asm_output_data (1, DW_RLE_start_length,
12253 "DW_RLE_start_length (%s)", lab);
12254 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12255 "Range begin address (%s)", lab);
12257 dw2_asm_output_delta_uleb128 (elabel, blabel,
12258 "Range length (%s)", lab);
12260 else if (dwarf_split_debug_info)
12262 dw2_asm_output_data (1, DW_RLE_startx_endx,
12263 "DW_RLE_startx_endx (%s)", lab);
12264 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12265 "Range begin address index "
12266 "(%s)", blabel);
12267 dw2_asm_output_data_uleb128 (r->end_entry->index,
12268 "Range end address index "
12269 "(%s)", elabel);
12271 else
12273 dw2_asm_output_data (1, DW_RLE_start_end,
12274 "DW_RLE_start_end (%s)", lab);
12275 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12276 "Range begin address (%s)", lab);
12277 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12278 "Range end address (%s)", lab);
12282 /* Negative block_num stands for an index into ranges_by_label. */
12283 else if (block_num < 0)
12285 int lab_idx = - block_num - 1;
12286 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12287 const char *elabel = (*ranges_by_label)[lab_idx].end;
12289 if (!have_multiple_function_sections)
12290 gcc_unreachable ();
12291 if (HAVE_AS_LEB128)
12293 if (dwarf_split_debug_info)
12295 dw2_asm_output_data (1, DW_RLE_startx_length,
12296 "DW_RLE_startx_length (%s)", lab);
12297 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12298 "Range begin address index "
12299 "(%s)", blabel);
12301 else
12303 dw2_asm_output_data (1, DW_RLE_start_length,
12304 "DW_RLE_start_length (%s)", lab);
12305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12306 "Range begin address (%s)", lab);
12308 dw2_asm_output_delta_uleb128 (elabel, blabel,
12309 "Range length (%s)", lab);
12311 else if (dwarf_split_debug_info)
12313 dw2_asm_output_data (1, DW_RLE_startx_endx,
12314 "DW_RLE_startx_endx (%s)", lab);
12315 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12316 "Range begin address index "
12317 "(%s)", blabel);
12318 dw2_asm_output_data_uleb128 (r->end_entry->index,
12319 "Range end address index "
12320 "(%s)", elabel);
12322 else
12324 dw2_asm_output_data (1, DW_RLE_start_end,
12325 "DW_RLE_start_end (%s)", lab);
12326 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12327 "Range begin address (%s)", lab);
12328 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12329 "Range end address (%s)", lab);
12332 else
12333 dw2_asm_output_data (1, DW_RLE_end_of_list,
12334 "DW_RLE_end_of_list (%s)", lab);
12336 ASM_OUTPUT_LABEL (asm_out_file, l2);
12337 return ret;
12340 /* Data structure containing information about input files. */
12341 struct file_info
12343 const char *path; /* Complete file name. */
12344 const char *fname; /* File name part. */
12345 int length; /* Length of entire string. */
12346 struct dwarf_file_data * file_idx; /* Index in input file table. */
12347 int dir_idx; /* Index in directory table. */
12350 /* Data structure containing information about directories with source
12351 files. */
12352 struct dir_info
12354 const char *path; /* Path including directory name. */
12355 int length; /* Path length. */
12356 int prefix; /* Index of directory entry which is a prefix. */
12357 int count; /* Number of files in this directory. */
12358 int dir_idx; /* Index of directory used as base. */
12361 /* Callback function for file_info comparison. We sort by looking at
12362 the directories in the path. */
12364 static int
12365 file_info_cmp (const void *p1, const void *p2)
12367 const struct file_info *const s1 = (const struct file_info *) p1;
12368 const struct file_info *const s2 = (const struct file_info *) p2;
12369 const unsigned char *cp1;
12370 const unsigned char *cp2;
12372 /* Take care of file names without directories. We need to make sure that
12373 we return consistent values to qsort since some will get confused if
12374 we return the same value when identical operands are passed in opposite
12375 orders. So if neither has a directory, return 0 and otherwise return
12376 1 or -1 depending on which one has the directory. We want the one with
12377 the directory to sort after the one without, so all no directory files
12378 are at the start (normally only the compilation unit file). */
12379 if ((s1->path == s1->fname || s2->path == s2->fname))
12380 return (s2->path == s2->fname) - (s1->path == s1->fname);
12382 cp1 = (const unsigned char *) s1->path;
12383 cp2 = (const unsigned char *) s2->path;
12385 while (1)
12387 ++cp1;
12388 ++cp2;
12389 /* Reached the end of the first path? If so, handle like above,
12390 but now we want longer directory prefixes before shorter ones. */
12391 if ((cp1 == (const unsigned char *) s1->fname)
12392 || (cp2 == (const unsigned char *) s2->fname))
12393 return ((cp1 == (const unsigned char *) s1->fname)
12394 - (cp2 == (const unsigned char *) s2->fname));
12396 /* Character of current path component the same? */
12397 else if (*cp1 != *cp2)
12398 return *cp1 - *cp2;
12402 struct file_name_acquire_data
12404 struct file_info *files;
12405 int used_files;
12406 int max_files;
12409 /* Traversal function for the hash table. */
12412 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12414 struct dwarf_file_data *d = *slot;
12415 struct file_info *fi;
12416 const char *f;
12418 gcc_assert (fnad->max_files >= d->emitted_number);
12420 if (! d->emitted_number)
12421 return 1;
12423 gcc_assert (fnad->max_files != fnad->used_files);
12425 fi = fnad->files + fnad->used_files++;
12427 f = d->filename;
12429 /* Skip all leading "./". */
12430 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12431 f += 2;
12433 /* Create a new array entry. */
12434 fi->path = f;
12435 fi->length = strlen (f);
12436 fi->file_idx = d;
12438 /* Search for the file name part. */
12439 f = strrchr (f, DIR_SEPARATOR);
12440 #if defined (DIR_SEPARATOR_2)
12442 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12444 if (g != NULL)
12446 if (f == NULL || f < g)
12447 f = g;
12450 #endif
12452 fi->fname = f == NULL ? fi->path : f + 1;
12453 return 1;
12456 /* Helper function for output_file_names. Emit a FORM encoded
12457 string STR, with assembly comment start ENTRY_KIND and
12458 index IDX */
12460 static void
12461 output_line_string (enum dwarf_form form, const char *str,
12462 const char *entry_kind, unsigned int idx)
12464 switch (form)
12466 case DW_FORM_string:
12467 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12468 break;
12469 case DW_FORM_line_strp:
12470 if (!debug_line_str_hash)
12471 debug_line_str_hash
12472 = hash_table<indirect_string_hasher>::create_ggc (10);
12474 struct indirect_string_node *node;
12475 node = find_AT_string_in_table (str, debug_line_str_hash);
12476 set_indirect_string (node);
12477 node->form = form;
12478 dw2_asm_output_offset (dwarf_offset_size, node->label,
12479 debug_line_str_section, "%s: %#x: \"%s\"",
12480 entry_kind, 0, node->str);
12481 break;
12482 default:
12483 gcc_unreachable ();
12487 /* Output the directory table and the file name table. We try to minimize
12488 the total amount of memory needed. A heuristic is used to avoid large
12489 slowdowns with many input files. */
12491 static void
12492 output_file_names (void)
12494 struct file_name_acquire_data fnad;
12495 int numfiles;
12496 struct file_info *files;
12497 struct dir_info *dirs;
12498 int *saved;
12499 int *savehere;
12500 int *backmap;
12501 int ndirs;
12502 int idx_offset;
12503 int i;
12505 if (!last_emitted_file)
12507 if (dwarf_version >= 5)
12509 const char *comp_dir = comp_dir_string ();
12510 if (comp_dir == NULL)
12511 comp_dir = "";
12512 dw2_asm_output_data (1, 1, "Directory entry format count");
12513 enum dwarf_form str_form = DW_FORM_string;
12514 if (DWARF5_USE_DEBUG_LINE_STR)
12515 str_form = DW_FORM_line_strp;
12516 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12517 dw2_asm_output_data_uleb128 (str_form, "%s",
12518 get_DW_FORM_name (str_form));
12519 dw2_asm_output_data_uleb128 (1, "Directories count");
12520 if (str_form == DW_FORM_string)
12521 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12522 else
12523 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12524 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12525 if (filename0 == NULL)
12526 filename0 = "";
12527 #ifdef VMS_DEBUGGING_INFO
12528 dw2_asm_output_data (1, 4, "File name entry format count");
12529 #else
12530 dw2_asm_output_data (1, 2, "File name entry format count");
12531 #endif
12532 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12533 dw2_asm_output_data_uleb128 (str_form, "%s",
12534 get_DW_FORM_name (str_form));
12535 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12536 "DW_LNCT_directory_index");
12537 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12538 get_DW_FORM_name (DW_FORM_data1));
12539 #ifdef VMS_DEBUGGING_INFO
12540 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12541 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12542 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12543 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12544 #endif
12545 dw2_asm_output_data_uleb128 (1, "File names count");
12547 output_line_string (str_form, filename0, "File Entry", 0);
12548 dw2_asm_output_data (1, 0, NULL);
12549 #ifdef VMS_DEBUGGING_INFO
12550 dw2_asm_output_data_uleb128 (0, NULL);
12551 dw2_asm_output_data_uleb128 (0, NULL);
12552 #endif
12554 else
12556 dw2_asm_output_data (1, 0, "End directory table");
12557 dw2_asm_output_data (1, 0, "End file name table");
12559 return;
12562 numfiles = last_emitted_file->emitted_number;
12564 /* Allocate the various arrays we need. */
12565 files = XALLOCAVEC (struct file_info, numfiles);
12566 dirs = XALLOCAVEC (struct dir_info, numfiles);
12568 fnad.files = files;
12569 fnad.used_files = 0;
12570 fnad.max_files = numfiles;
12571 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12572 gcc_assert (fnad.used_files == fnad.max_files);
12574 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12576 /* Find all the different directories used. */
12577 dirs[0].path = files[0].path;
12578 dirs[0].length = files[0].fname - files[0].path;
12579 dirs[0].prefix = -1;
12580 dirs[0].count = 1;
12581 dirs[0].dir_idx = 0;
12582 files[0].dir_idx = 0;
12583 ndirs = 1;
12585 for (i = 1; i < numfiles; i++)
12586 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12587 && memcmp (dirs[ndirs - 1].path, files[i].path,
12588 dirs[ndirs - 1].length) == 0)
12590 /* Same directory as last entry. */
12591 files[i].dir_idx = ndirs - 1;
12592 ++dirs[ndirs - 1].count;
12594 else
12596 int j;
12598 /* This is a new directory. */
12599 dirs[ndirs].path = files[i].path;
12600 dirs[ndirs].length = files[i].fname - files[i].path;
12601 dirs[ndirs].count = 1;
12602 dirs[ndirs].dir_idx = ndirs;
12603 files[i].dir_idx = ndirs;
12605 /* Search for a prefix. */
12606 dirs[ndirs].prefix = -1;
12607 for (j = 0; j < ndirs; j++)
12608 if (dirs[j].length < dirs[ndirs].length
12609 && dirs[j].length > 1
12610 && (dirs[ndirs].prefix == -1
12611 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12612 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12613 dirs[ndirs].prefix = j;
12615 ++ndirs;
12618 /* Now to the actual work. We have to find a subset of the directories which
12619 allow expressing the file name using references to the directory table
12620 with the least amount of characters. We do not do an exhaustive search
12621 where we would have to check out every combination of every single
12622 possible prefix. Instead we use a heuristic which provides nearly optimal
12623 results in most cases and never is much off. */
12624 saved = XALLOCAVEC (int, ndirs);
12625 savehere = XALLOCAVEC (int, ndirs);
12627 memset (saved, '\0', ndirs * sizeof (saved[0]));
12628 for (i = 0; i < ndirs; i++)
12630 int j;
12631 int total;
12633 /* We can always save some space for the current directory. But this
12634 does not mean it will be enough to justify adding the directory. */
12635 savehere[i] = dirs[i].length;
12636 total = (savehere[i] - saved[i]) * dirs[i].count;
12638 for (j = i + 1; j < ndirs; j++)
12640 savehere[j] = 0;
12641 if (saved[j] < dirs[i].length)
12643 /* Determine whether the dirs[i] path is a prefix of the
12644 dirs[j] path. */
12645 int k;
12647 k = dirs[j].prefix;
12648 while (k != -1 && k != (int) i)
12649 k = dirs[k].prefix;
12651 if (k == (int) i)
12653 /* Yes it is. We can possibly save some memory by
12654 writing the filenames in dirs[j] relative to
12655 dirs[i]. */
12656 savehere[j] = dirs[i].length;
12657 total += (savehere[j] - saved[j]) * dirs[j].count;
12662 /* Check whether we can save enough to justify adding the dirs[i]
12663 directory. */
12664 if (total > dirs[i].length + 1)
12666 /* It's worthwhile adding. */
12667 for (j = i; j < ndirs; j++)
12668 if (savehere[j] > 0)
12670 /* Remember how much we saved for this directory so far. */
12671 saved[j] = savehere[j];
12673 /* Remember the prefix directory. */
12674 dirs[j].dir_idx = i;
12679 /* Emit the directory name table. */
12680 idx_offset = dirs[0].length > 0 ? 1 : 0;
12681 enum dwarf_form str_form = DW_FORM_string;
12682 enum dwarf_form idx_form = DW_FORM_udata;
12683 if (dwarf_version >= 5)
12685 const char *comp_dir = comp_dir_string ();
12686 if (comp_dir == NULL)
12687 comp_dir = "";
12688 dw2_asm_output_data (1, 1, "Directory entry format count");
12689 if (DWARF5_USE_DEBUG_LINE_STR)
12690 str_form = DW_FORM_line_strp;
12691 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12692 dw2_asm_output_data_uleb128 (str_form, "%s",
12693 get_DW_FORM_name (str_form));
12694 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12695 if (str_form == DW_FORM_string)
12697 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12698 for (i = 1 - idx_offset; i < ndirs; i++)
12699 dw2_asm_output_nstring (dirs[i].path,
12700 dirs[i].length
12701 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12702 "Directory Entry: %#x", i + idx_offset);
12704 else
12706 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12707 for (i = 1 - idx_offset; i < ndirs; i++)
12709 const char *str
12710 = ggc_alloc_string (dirs[i].path,
12711 dirs[i].length
12712 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12713 output_line_string (str_form, str, "Directory Entry",
12714 (unsigned) i + idx_offset);
12718 else
12720 for (i = 1 - idx_offset; i < ndirs; i++)
12721 dw2_asm_output_nstring (dirs[i].path,
12722 dirs[i].length
12723 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12724 "Directory Entry: %#x", i + idx_offset);
12726 dw2_asm_output_data (1, 0, "End directory table");
12729 /* We have to emit them in the order of emitted_number since that's
12730 used in the debug info generation. To do this efficiently we
12731 generate a back-mapping of the indices first. */
12732 backmap = XALLOCAVEC (int, numfiles);
12733 for (i = 0; i < numfiles; i++)
12734 backmap[files[i].file_idx->emitted_number - 1] = i;
12736 if (dwarf_version >= 5)
12738 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12739 if (filename0 == NULL)
12740 filename0 = "";
12741 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12742 DW_FORM_data2. Choose one based on the number of directories
12743 and how much space would they occupy in each encoding.
12744 If we have at most 256 directories, all indexes fit into
12745 a single byte, so DW_FORM_data1 is most compact (if there
12746 are at most 128 directories, DW_FORM_udata would be as
12747 compact as that, but not shorter and slower to decode). */
12748 if (ndirs + idx_offset <= 256)
12749 idx_form = DW_FORM_data1;
12750 /* If there are more than 65536 directories, we have to use
12751 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12752 Otherwise, compute what space would occupy if all the indexes
12753 used DW_FORM_udata - sum - and compare that to how large would
12754 be DW_FORM_data2 encoding, and pick the more efficient one. */
12755 else if (ndirs + idx_offset <= 65536)
12757 unsigned HOST_WIDE_INT sum = 1;
12758 for (i = 0; i < numfiles; i++)
12760 int file_idx = backmap[i];
12761 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12762 sum += size_of_uleb128 (dir_idx);
12764 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12765 idx_form = DW_FORM_data2;
12767 #ifdef VMS_DEBUGGING_INFO
12768 dw2_asm_output_data (1, 4, "File name entry format count");
12769 #else
12770 dw2_asm_output_data (1, 2, "File name entry format count");
12771 #endif
12772 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12773 dw2_asm_output_data_uleb128 (str_form, "%s",
12774 get_DW_FORM_name (str_form));
12775 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12776 "DW_LNCT_directory_index");
12777 dw2_asm_output_data_uleb128 (idx_form, "%s",
12778 get_DW_FORM_name (idx_form));
12779 #ifdef VMS_DEBUGGING_INFO
12780 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12781 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12782 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12783 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12784 #endif
12785 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12787 output_line_string (str_form, filename0, "File Entry", 0);
12789 /* Include directory index. */
12790 if (idx_form != DW_FORM_udata)
12791 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12792 0, NULL);
12793 else
12794 dw2_asm_output_data_uleb128 (0, NULL);
12796 #ifdef VMS_DEBUGGING_INFO
12797 dw2_asm_output_data_uleb128 (0, NULL);
12798 dw2_asm_output_data_uleb128 (0, NULL);
12799 #endif
12802 /* Now write all the file names. */
12803 for (i = 0; i < numfiles; i++)
12805 int file_idx = backmap[i];
12806 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12808 #ifdef VMS_DEBUGGING_INFO
12809 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12811 /* Setting these fields can lead to debugger miscomparisons,
12812 but VMS Debug requires them to be set correctly. */
12814 int ver;
12815 long long cdt;
12816 long siz;
12817 int maxfilelen = (strlen (files[file_idx].path)
12818 + dirs[dir_idx].length
12819 + MAX_VMS_VERSION_LEN + 1);
12820 char *filebuf = XALLOCAVEC (char, maxfilelen);
12822 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12823 snprintf (filebuf, maxfilelen, "%s;%d",
12824 files[file_idx].path + dirs[dir_idx].length, ver);
12826 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12828 /* Include directory index. */
12829 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12830 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12831 dir_idx + idx_offset, NULL);
12832 else
12833 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12835 /* Modification time. */
12836 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12837 &cdt, 0, 0, 0) == 0)
12838 ? cdt : 0, NULL);
12840 /* File length in bytes. */
12841 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12842 0, &siz, 0, 0) == 0)
12843 ? siz : 0, NULL);
12844 #else
12845 output_line_string (str_form,
12846 files[file_idx].path + dirs[dir_idx].length,
12847 "File Entry", (unsigned) i + 1);
12849 /* Include directory index. */
12850 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12851 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12852 dir_idx + idx_offset, NULL);
12853 else
12854 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12856 if (dwarf_version >= 5)
12857 continue;
12859 /* Modification time. */
12860 dw2_asm_output_data_uleb128 (0, NULL);
12862 /* File length in bytes. */
12863 dw2_asm_output_data_uleb128 (0, NULL);
12864 #endif /* VMS_DEBUGGING_INFO */
12867 if (dwarf_version < 5)
12868 dw2_asm_output_data (1, 0, "End file name table");
12872 /* Output one line number table into the .debug_line section. */
12874 static void
12875 output_one_line_info_table (dw_line_info_table *table)
12877 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12878 unsigned int current_line = 1;
12879 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12880 dw_line_info_entry *ent, *prev_addr;
12881 size_t i;
12882 unsigned int view;
12884 view = 0;
12886 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12888 switch (ent->opcode)
12890 case LI_set_address:
12891 /* ??? Unfortunately, we have little choice here currently, and
12892 must always use the most general form. GCC does not know the
12893 address delta itself, so we can't use DW_LNS_advance_pc. Many
12894 ports do have length attributes which will give an upper bound
12895 on the address range. We could perhaps use length attributes
12896 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12897 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12899 view = 0;
12901 /* This can handle any delta. This takes
12902 4+DWARF2_ADDR_SIZE bytes. */
12903 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12904 debug_variable_location_views
12905 ? ", reset view to 0" : "");
12906 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12907 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12908 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12910 prev_addr = ent;
12911 break;
12913 case LI_adv_address:
12915 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12916 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12917 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12919 view++;
12921 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12922 dw2_asm_output_delta (2, line_label, prev_label,
12923 "from %s to %s", prev_label, line_label);
12925 prev_addr = ent;
12926 break;
12929 case LI_set_line:
12930 if (ent->val == current_line)
12932 /* We still need to start a new row, so output a copy insn. */
12933 dw2_asm_output_data (1, DW_LNS_copy,
12934 "copy line %u", current_line);
12936 else
12938 int line_offset = ent->val - current_line;
12939 int line_delta = line_offset - DWARF_LINE_BASE;
12941 current_line = ent->val;
12942 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12944 /* This can handle deltas from -10 to 234, using the current
12945 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12946 This takes 1 byte. */
12947 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12948 "line %u", current_line);
12950 else
12952 /* This can handle any delta. This takes at least 4 bytes,
12953 depending on the value being encoded. */
12954 dw2_asm_output_data (1, DW_LNS_advance_line,
12955 "advance to line %u", current_line);
12956 dw2_asm_output_data_sleb128 (line_offset, NULL);
12957 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12960 break;
12962 case LI_set_file:
12963 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12964 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12965 break;
12967 case LI_set_column:
12968 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12969 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12970 break;
12972 case LI_negate_stmt:
12973 current_is_stmt = !current_is_stmt;
12974 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12975 "is_stmt %d", current_is_stmt);
12976 break;
12978 case LI_set_prologue_end:
12979 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12980 "set prologue end");
12981 break;
12983 case LI_set_epilogue_begin:
12984 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12985 "set epilogue begin");
12986 break;
12988 case LI_set_discriminator:
12989 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12990 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12991 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12992 dw2_asm_output_data_uleb128 (ent->val, NULL);
12993 break;
12997 /* Emit debug info for the address of the end of the table. */
12998 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12999 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13000 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13001 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13003 dw2_asm_output_data (1, 0, "end sequence");
13004 dw2_asm_output_data_uleb128 (1, NULL);
13005 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13008 static unsigned int output_line_info_generation;
13010 /* Output the source line number correspondence information. This
13011 information goes into the .debug_line section. */
13013 static void
13014 output_line_info (bool prologue_only)
13016 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13017 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13018 bool saw_one = false;
13019 int opc;
13021 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13022 output_line_info_generation);
13023 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13024 output_line_info_generation);
13025 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13026 output_line_info_generation);
13027 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13028 output_line_info_generation++);
13030 if (!XCOFF_DEBUGGING_INFO)
13032 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13033 dw2_asm_output_data (4, 0xffffffff,
13034 "Initial length escape value indicating 64-bit DWARF extension");
13035 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13036 "Length of Source Line Info");
13039 ASM_OUTPUT_LABEL (asm_out_file, l1);
13041 output_dwarf_version ();
13042 if (dwarf_version >= 5)
13044 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13045 dw2_asm_output_data (1, 0, "Segment Size");
13047 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13048 ASM_OUTPUT_LABEL (asm_out_file, p1);
13050 /* Define the architecture-dependent minimum instruction length (in bytes).
13051 In this implementation of DWARF, this field is used for information
13052 purposes only. Since GCC generates assembly language, we have no
13053 a priori knowledge of how many instruction bytes are generated for each
13054 source line, and therefore can use only the DW_LNE_set_address and
13055 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13056 this as '1', which is "correct enough" for all architectures,
13057 and don't let the target override. */
13058 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13060 if (dwarf_version >= 4)
13061 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13062 "Maximum Operations Per Instruction");
13063 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13064 "Default is_stmt_start flag");
13065 dw2_asm_output_data (1, DWARF_LINE_BASE,
13066 "Line Base Value (Special Opcodes)");
13067 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13068 "Line Range Value (Special Opcodes)");
13069 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13070 "Special Opcode Base");
13072 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13074 int n_op_args;
13075 switch (opc)
13077 case DW_LNS_advance_pc:
13078 case DW_LNS_advance_line:
13079 case DW_LNS_set_file:
13080 case DW_LNS_set_column:
13081 case DW_LNS_fixed_advance_pc:
13082 case DW_LNS_set_isa:
13083 n_op_args = 1;
13084 break;
13085 default:
13086 n_op_args = 0;
13087 break;
13090 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13091 opc, n_op_args);
13094 /* Write out the information about the files we use. */
13095 output_file_names ();
13096 ASM_OUTPUT_LABEL (asm_out_file, p2);
13097 if (prologue_only)
13099 /* Output the marker for the end of the line number info. */
13100 ASM_OUTPUT_LABEL (asm_out_file, l2);
13101 return;
13104 if (separate_line_info)
13106 dw_line_info_table *table;
13107 size_t i;
13109 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13110 if (table->in_use)
13112 output_one_line_info_table (table);
13113 saw_one = true;
13116 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13118 output_one_line_info_table (cold_text_section_line_info);
13119 saw_one = true;
13122 /* ??? Some Darwin linkers crash on a .debug_line section with no
13123 sequences. Further, merely a DW_LNE_end_sequence entry is not
13124 sufficient -- the address column must also be initialized.
13125 Make sure to output at least one set_address/end_sequence pair,
13126 choosing .text since that section is always present. */
13127 if (text_section_line_info->in_use || !saw_one)
13128 output_one_line_info_table (text_section_line_info);
13130 /* Output the marker for the end of the line number info. */
13131 ASM_OUTPUT_LABEL (asm_out_file, l2);
13134 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13136 static inline bool
13137 need_endianity_attribute_p (bool reverse)
13139 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13142 /* Given a pointer to a tree node for some base type, return a pointer to
13143 a DIE that describes the given type. REVERSE is true if the type is
13144 to be interpreted in the reverse storage order wrt the target order.
13146 This routine must only be called for GCC type nodes that correspond to
13147 Dwarf base (fundamental) types. */
13149 dw_die_ref
13150 base_type_die (tree type, bool reverse)
13152 dw_die_ref base_type_result;
13153 enum dwarf_type encoding;
13154 bool fpt_used = false;
13155 struct fixed_point_type_info fpt_info;
13156 tree type_bias = NULL_TREE;
13158 /* If this is a subtype that should not be emitted as a subrange type,
13159 use the base type. See subrange_type_for_debug_p. */
13160 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13161 type = TREE_TYPE (type);
13163 switch (TREE_CODE (type))
13165 case INTEGER_TYPE:
13166 if ((dwarf_version >= 4 || !dwarf_strict)
13167 && TYPE_NAME (type)
13168 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13169 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13170 && DECL_NAME (TYPE_NAME (type)))
13172 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13173 if (strcmp (name, "char16_t") == 0
13174 || strcmp (name, "char32_t") == 0)
13176 encoding = DW_ATE_UTF;
13177 break;
13180 if ((dwarf_version >= 3 || !dwarf_strict)
13181 && lang_hooks.types.get_fixed_point_type_info)
13183 memset (&fpt_info, 0, sizeof (fpt_info));
13184 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13186 fpt_used = true;
13187 encoding = ((TYPE_UNSIGNED (type))
13188 ? DW_ATE_unsigned_fixed
13189 : DW_ATE_signed_fixed);
13190 break;
13193 if (TYPE_STRING_FLAG (type))
13195 if (TYPE_UNSIGNED (type))
13196 encoding = DW_ATE_unsigned_char;
13197 else
13198 encoding = DW_ATE_signed_char;
13200 else if (TYPE_UNSIGNED (type))
13201 encoding = DW_ATE_unsigned;
13202 else
13203 encoding = DW_ATE_signed;
13205 if (!dwarf_strict
13206 && lang_hooks.types.get_type_bias)
13207 type_bias = lang_hooks.types.get_type_bias (type);
13208 break;
13210 case REAL_TYPE:
13211 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13213 if (dwarf_version >= 3 || !dwarf_strict)
13214 encoding = DW_ATE_decimal_float;
13215 else
13216 encoding = DW_ATE_lo_user;
13218 else
13219 encoding = DW_ATE_float;
13220 break;
13222 case FIXED_POINT_TYPE:
13223 if (!(dwarf_version >= 3 || !dwarf_strict))
13224 encoding = DW_ATE_lo_user;
13225 else if (TYPE_UNSIGNED (type))
13226 encoding = DW_ATE_unsigned_fixed;
13227 else
13228 encoding = DW_ATE_signed_fixed;
13229 break;
13231 /* Dwarf2 doesn't know anything about complex ints, so use
13232 a user defined type for it. */
13233 case COMPLEX_TYPE:
13234 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13235 encoding = DW_ATE_complex_float;
13236 else
13237 encoding = DW_ATE_lo_user;
13238 break;
13240 case BOOLEAN_TYPE:
13241 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13242 encoding = DW_ATE_boolean;
13243 break;
13245 default:
13246 /* No other TREE_CODEs are Dwarf fundamental types. */
13247 gcc_unreachable ();
13250 base_type_result = new_die_raw (DW_TAG_base_type);
13252 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13253 int_size_in_bytes (type));
13254 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13256 if (need_endianity_attribute_p (reverse))
13257 add_AT_unsigned (base_type_result, DW_AT_endianity,
13258 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13260 add_alignment_attribute (base_type_result, type);
13262 if (fpt_used)
13264 switch (fpt_info.scale_factor_kind)
13266 case fixed_point_scale_factor_binary:
13267 add_AT_int (base_type_result, DW_AT_binary_scale,
13268 fpt_info.scale_factor.binary);
13269 break;
13271 case fixed_point_scale_factor_decimal:
13272 add_AT_int (base_type_result, DW_AT_decimal_scale,
13273 fpt_info.scale_factor.decimal);
13274 break;
13276 case fixed_point_scale_factor_arbitrary:
13277 /* Arbitrary scale factors cannot be described in standard DWARF. */
13278 if (!dwarf_strict)
13280 /* Describe the scale factor as a rational constant. */
13281 const dw_die_ref scale_factor
13282 = new_die (DW_TAG_constant, comp_unit_die (), type);
13284 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13285 fpt_info.scale_factor.arbitrary.numerator,
13286 dw_scalar_form_constant, NULL);
13287 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13288 fpt_info.scale_factor.arbitrary.denominator,
13289 dw_scalar_form_constant, NULL);
13291 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13293 break;
13295 default:
13296 gcc_unreachable ();
13300 if (type_bias)
13301 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13302 dw_scalar_form_constant
13303 | dw_scalar_form_exprloc
13304 | dw_scalar_form_reference,
13305 NULL);
13307 return base_type_result;
13310 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13311 named 'auto' in its type: return true for it, false otherwise. */
13313 static inline bool
13314 is_cxx_auto (tree type)
13316 if (is_cxx ())
13318 tree name = TYPE_IDENTIFIER (type);
13319 if (name == get_identifier ("auto")
13320 || name == get_identifier ("decltype(auto)"))
13321 return true;
13323 return false;
13326 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13327 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13329 static inline int
13330 is_base_type (tree type)
13332 switch (TREE_CODE (type))
13334 case INTEGER_TYPE:
13335 case REAL_TYPE:
13336 case FIXED_POINT_TYPE:
13337 case COMPLEX_TYPE:
13338 case BOOLEAN_TYPE:
13339 return 1;
13341 case VOID_TYPE:
13342 case OPAQUE_TYPE:
13343 case ARRAY_TYPE:
13344 case RECORD_TYPE:
13345 case UNION_TYPE:
13346 case QUAL_UNION_TYPE:
13347 case ENUMERAL_TYPE:
13348 case FUNCTION_TYPE:
13349 case METHOD_TYPE:
13350 case POINTER_TYPE:
13351 case REFERENCE_TYPE:
13352 case NULLPTR_TYPE:
13353 case OFFSET_TYPE:
13354 case LANG_TYPE:
13355 case VECTOR_TYPE:
13356 return 0;
13358 default:
13359 if (is_cxx_auto (type))
13360 return 0;
13361 gcc_unreachable ();
13364 return 0;
13367 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13368 node, return the size in bits for the type if it is a constant, or else
13369 return the alignment for the type if the type's size is not constant, or
13370 else return BITS_PER_WORD if the type actually turns out to be an
13371 ERROR_MARK node. */
13373 static inline unsigned HOST_WIDE_INT
13374 simple_type_size_in_bits (const_tree type)
13376 if (TREE_CODE (type) == ERROR_MARK)
13377 return BITS_PER_WORD;
13378 else if (TYPE_SIZE (type) == NULL_TREE)
13379 return 0;
13380 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13381 return tree_to_uhwi (TYPE_SIZE (type));
13382 else
13383 return TYPE_ALIGN (type);
13386 /* Similarly, but return an offset_int instead of UHWI. */
13388 static inline offset_int
13389 offset_int_type_size_in_bits (const_tree type)
13391 if (TREE_CODE (type) == ERROR_MARK)
13392 return BITS_PER_WORD;
13393 else if (TYPE_SIZE (type) == NULL_TREE)
13394 return 0;
13395 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13396 return wi::to_offset (TYPE_SIZE (type));
13397 else
13398 return TYPE_ALIGN (type);
13401 /* Given a pointer to a tree node for a subrange type, return a pointer
13402 to a DIE that describes the given type. */
13404 static dw_die_ref
13405 subrange_type_die (tree type, tree low, tree high, tree bias,
13406 dw_die_ref context_die)
13408 dw_die_ref subrange_die;
13409 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13411 if (context_die == NULL)
13412 context_die = comp_unit_die ();
13414 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13416 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13418 /* The size of the subrange type and its base type do not match,
13419 so we need to generate a size attribute for the subrange type. */
13420 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13423 add_alignment_attribute (subrange_die, type);
13425 if (low)
13426 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13427 if (high)
13428 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13429 if (bias && !dwarf_strict)
13430 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13431 dw_scalar_form_constant
13432 | dw_scalar_form_exprloc
13433 | dw_scalar_form_reference,
13434 NULL);
13436 return subrange_die;
13439 /* Returns the (const and/or volatile) cv_qualifiers associated with
13440 the decl node. This will normally be augmented with the
13441 cv_qualifiers of the underlying type in add_type_attribute. */
13443 static int
13444 decl_quals (const_tree decl)
13446 return ((TREE_READONLY (decl)
13447 /* The C++ front-end correctly marks reference-typed
13448 variables as readonly, but from a language (and debug
13449 info) standpoint they are not const-qualified. */
13450 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13451 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13452 | (TREE_THIS_VOLATILE (decl)
13453 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13456 /* Determine the TYPE whose qualifiers match the largest strict subset
13457 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13458 qualifiers outside QUAL_MASK. */
13460 static int
13461 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13463 tree t;
13464 int best_rank = 0, best_qual = 0, max_rank;
13466 type_quals &= qual_mask;
13467 max_rank = popcount_hwi (type_quals) - 1;
13469 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13470 t = TYPE_NEXT_VARIANT (t))
13472 int q = TYPE_QUALS (t) & qual_mask;
13474 if ((q & type_quals) == q && q != type_quals
13475 && check_base_type (t, type))
13477 int rank = popcount_hwi (q);
13479 if (rank > best_rank)
13481 best_rank = rank;
13482 best_qual = q;
13487 return best_qual;
13490 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13491 static const dwarf_qual_info_t dwarf_qual_info[] =
13493 { TYPE_QUAL_CONST, DW_TAG_const_type },
13494 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13495 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13496 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13498 static const unsigned int dwarf_qual_info_size
13499 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13501 /* If DIE is a qualified DIE of some base DIE with the same parent,
13502 return the base DIE, otherwise return NULL. Set MASK to the
13503 qualifiers added compared to the returned DIE. */
13505 static dw_die_ref
13506 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13508 unsigned int i;
13509 for (i = 0; i < dwarf_qual_info_size; i++)
13510 if (die->die_tag == dwarf_qual_info[i].t)
13511 break;
13512 if (i == dwarf_qual_info_size)
13513 return NULL;
13514 if (vec_safe_length (die->die_attr) != 1)
13515 return NULL;
13516 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13517 if (type == NULL || type->die_parent != die->die_parent)
13518 return NULL;
13519 *mask |= dwarf_qual_info[i].q;
13520 if (depth)
13522 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13523 if (ret)
13524 return ret;
13526 return type;
13529 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13530 entry that chains the modifiers specified by CV_QUALS in front of the
13531 given type. REVERSE is true if the type is to be interpreted in the
13532 reverse storage order wrt the target order. */
13534 static dw_die_ref
13535 modified_type_die (tree type, int cv_quals, bool reverse,
13536 dw_die_ref context_die)
13538 enum tree_code code = TREE_CODE (type);
13539 dw_die_ref mod_type_die;
13540 dw_die_ref sub_die = NULL;
13541 tree item_type = NULL;
13542 tree qualified_type;
13543 tree name, low, high;
13544 dw_die_ref mod_scope;
13545 /* Only these cv-qualifiers are currently handled. */
13546 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13547 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13548 ENCODE_QUAL_ADDR_SPACE(~0U));
13549 const bool reverse_base_type
13550 = need_endianity_attribute_p (reverse) && is_base_type (type);
13552 if (code == ERROR_MARK)
13553 return NULL;
13555 if (lang_hooks.types.get_debug_type)
13557 tree debug_type = lang_hooks.types.get_debug_type (type);
13559 if (debug_type != NULL_TREE && debug_type != type)
13560 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13563 cv_quals &= cv_qual_mask;
13565 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13566 tag modifier (and not an attribute) old consumers won't be able
13567 to handle it. */
13568 if (dwarf_version < 3)
13569 cv_quals &= ~TYPE_QUAL_RESTRICT;
13571 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13572 if (dwarf_version < 5)
13573 cv_quals &= ~TYPE_QUAL_ATOMIC;
13575 /* See if we already have the appropriately qualified variant of
13576 this type. */
13577 qualified_type = get_qualified_type (type, cv_quals);
13579 if (qualified_type == sizetype)
13581 /* Try not to expose the internal sizetype type's name. */
13582 if (TYPE_NAME (qualified_type)
13583 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13585 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13587 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13588 && (TYPE_PRECISION (t)
13589 == TYPE_PRECISION (qualified_type))
13590 && (TYPE_UNSIGNED (t)
13591 == TYPE_UNSIGNED (qualified_type)));
13592 qualified_type = t;
13594 else if (qualified_type == sizetype
13595 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13596 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13597 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13598 qualified_type = size_type_node;
13599 if (type == sizetype)
13600 type = qualified_type;
13603 /* If we do, then we can just use its DIE, if it exists. */
13604 if (qualified_type)
13606 mod_type_die = lookup_type_die (qualified_type);
13608 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13609 dealt with specially: the DIE with the attribute, if it exists, is
13610 placed immediately after the regular DIE for the same base type. */
13611 if (mod_type_die
13612 && (!reverse_base_type
13613 || ((mod_type_die = mod_type_die->die_sib) != NULL
13614 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13615 return mod_type_die;
13618 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13620 /* Handle C typedef types. */
13621 if (name
13622 && TREE_CODE (name) == TYPE_DECL
13623 && DECL_ORIGINAL_TYPE (name)
13624 && !DECL_ARTIFICIAL (name))
13626 tree dtype = TREE_TYPE (name);
13628 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13629 if (qualified_type == dtype && !reverse_base_type)
13631 tree origin = decl_ultimate_origin (name);
13633 /* Typedef variants that have an abstract origin don't get their own
13634 type DIE (see gen_typedef_die), so fall back on the ultimate
13635 abstract origin instead. */
13636 if (origin != NULL && origin != name)
13637 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13638 context_die);
13640 /* For a named type, use the typedef. */
13641 gen_type_die (qualified_type, context_die);
13642 return lookup_type_die (qualified_type);
13644 else
13646 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13647 dquals &= cv_qual_mask;
13648 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13649 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13650 /* cv-unqualified version of named type. Just use
13651 the unnamed type to which it refers. */
13652 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13653 reverse, context_die);
13654 /* Else cv-qualified version of named type; fall through. */
13658 mod_scope = scope_die_for (type, context_die);
13660 if (cv_quals)
13662 int sub_quals = 0, first_quals = 0;
13663 unsigned i;
13664 dw_die_ref first = NULL, last = NULL;
13666 /* Determine a lesser qualified type that most closely matches
13667 this one. Then generate DW_TAG_* entries for the remaining
13668 qualifiers. */
13669 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13670 cv_qual_mask);
13671 if (sub_quals && use_debug_types)
13673 bool needed = false;
13674 /* If emitting type units, make sure the order of qualifiers
13675 is canonical. Thus, start from unqualified type if
13676 an earlier qualifier is missing in sub_quals, but some later
13677 one is present there. */
13678 for (i = 0; i < dwarf_qual_info_size; i++)
13679 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13680 needed = true;
13681 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13683 sub_quals = 0;
13684 break;
13687 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13688 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13690 /* As not all intermediate qualified DIEs have corresponding
13691 tree types, ensure that qualified DIEs in the same scope
13692 as their DW_AT_type are emitted after their DW_AT_type,
13693 only with other qualified DIEs for the same type possibly
13694 in between them. Determine the range of such qualified
13695 DIEs now (first being the base type, last being corresponding
13696 last qualified DIE for it). */
13697 unsigned int count = 0;
13698 first = qualified_die_p (mod_type_die, &first_quals,
13699 dwarf_qual_info_size);
13700 if (first == NULL)
13701 first = mod_type_die;
13702 gcc_assert ((first_quals & ~sub_quals) == 0);
13703 for (count = 0, last = first;
13704 count < (1U << dwarf_qual_info_size);
13705 count++, last = last->die_sib)
13707 int quals = 0;
13708 if (last == mod_scope->die_child)
13709 break;
13710 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13711 != first)
13712 break;
13716 for (i = 0; i < dwarf_qual_info_size; i++)
13717 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13719 dw_die_ref d;
13720 if (first && first != last)
13722 for (d = first->die_sib; ; d = d->die_sib)
13724 int quals = 0;
13725 qualified_die_p (d, &quals, dwarf_qual_info_size);
13726 if (quals == (first_quals | dwarf_qual_info[i].q))
13727 break;
13728 if (d == last)
13730 d = NULL;
13731 break;
13734 if (d)
13736 mod_type_die = d;
13737 continue;
13740 if (first)
13742 d = new_die_raw (dwarf_qual_info[i].t);
13743 add_child_die_after (mod_scope, d, last);
13744 last = d;
13746 else
13747 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13748 if (mod_type_die)
13749 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13750 mod_type_die = d;
13751 first_quals |= dwarf_qual_info[i].q;
13754 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13756 dwarf_tag tag = DW_TAG_pointer_type;
13757 if (code == REFERENCE_TYPE)
13759 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13760 tag = DW_TAG_rvalue_reference_type;
13761 else
13762 tag = DW_TAG_reference_type;
13764 mod_type_die = new_die (tag, mod_scope, type);
13766 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13767 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13768 add_alignment_attribute (mod_type_die, type);
13769 item_type = TREE_TYPE (type);
13771 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13772 if (!ADDR_SPACE_GENERIC_P (as))
13774 int action = targetm.addr_space.debug (as);
13775 if (action >= 0)
13777 /* Positive values indicate an address_class. */
13778 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13780 else
13782 /* Negative values indicate an (inverted) segment base reg. */
13783 dw_loc_descr_ref d
13784 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13785 add_AT_loc (mod_type_die, DW_AT_segment, d);
13789 else if (code == INTEGER_TYPE
13790 && TREE_TYPE (type) != NULL_TREE
13791 && subrange_type_for_debug_p (type, &low, &high))
13793 tree bias = NULL_TREE;
13794 if (lang_hooks.types.get_type_bias)
13795 bias = lang_hooks.types.get_type_bias (type);
13796 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13797 item_type = TREE_TYPE (type);
13799 else if (is_base_type (type))
13801 mod_type_die = base_type_die (type, reverse);
13803 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13804 if (reverse_base_type)
13806 dw_die_ref after_die
13807 = modified_type_die (type, cv_quals, false, context_die);
13808 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13810 else
13811 add_child_die (comp_unit_die (), mod_type_die);
13813 add_pubtype (type, mod_type_die);
13815 else
13817 gen_type_die (type, context_die);
13819 /* We have to get the type_main_variant here (and pass that to the
13820 `lookup_type_die' routine) because the ..._TYPE node we have
13821 might simply be a *copy* of some original type node (where the
13822 copy was created to help us keep track of typedef names) and
13823 that copy might have a different TYPE_UID from the original
13824 ..._TYPE node. */
13825 if (TREE_CODE (type) == FUNCTION_TYPE
13826 || TREE_CODE (type) == METHOD_TYPE)
13828 /* For function/method types, can't just use type_main_variant here,
13829 because that can have different ref-qualifiers for C++,
13830 but try to canonicalize. */
13831 tree main = TYPE_MAIN_VARIANT (type);
13832 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13833 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13834 && check_base_type (t, main)
13835 && check_lang_type (t, type))
13836 return lookup_type_die (t);
13837 return lookup_type_die (type);
13839 else if (TREE_CODE (type) != VECTOR_TYPE
13840 && TREE_CODE (type) != ARRAY_TYPE)
13841 return lookup_type_die (type_main_variant (type));
13842 else
13843 /* Vectors have the debugging information in the type,
13844 not the main variant. */
13845 return lookup_type_die (type);
13848 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13849 don't output a DW_TAG_typedef, since there isn't one in the
13850 user's program; just attach a DW_AT_name to the type.
13851 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13852 if the base type already has the same name. */
13853 if (name
13854 && ((TREE_CODE (name) != TYPE_DECL
13855 && (qualified_type == TYPE_MAIN_VARIANT (type)
13856 || (cv_quals == TYPE_UNQUALIFIED)))
13857 || (TREE_CODE (name) == TYPE_DECL
13858 && TREE_TYPE (name) == qualified_type
13859 && DECL_NAME (name))))
13861 if (TREE_CODE (name) == TYPE_DECL)
13862 /* Could just call add_name_and_src_coords_attributes here,
13863 but since this is a builtin type it doesn't have any
13864 useful source coordinates anyway. */
13865 name = DECL_NAME (name);
13866 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13868 /* This probably indicates a bug. */
13869 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13871 name = TYPE_IDENTIFIER (type);
13872 add_name_attribute (mod_type_die,
13873 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13876 if (qualified_type && !reverse_base_type)
13877 equate_type_number_to_die (qualified_type, mod_type_die);
13879 if (item_type)
13880 /* We must do this after the equate_type_number_to_die call, in case
13881 this is a recursive type. This ensures that the modified_type_die
13882 recursion will terminate even if the type is recursive. Recursive
13883 types are possible in Ada. */
13884 sub_die = modified_type_die (item_type,
13885 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13886 reverse,
13887 context_die);
13889 if (sub_die != NULL)
13890 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13892 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13893 if (TYPE_ARTIFICIAL (type))
13894 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13896 return mod_type_die;
13899 /* Generate DIEs for the generic parameters of T.
13900 T must be either a generic type or a generic function.
13901 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13903 static void
13904 gen_generic_params_dies (tree t)
13906 tree parms, args;
13907 int parms_num, i;
13908 dw_die_ref die = NULL;
13909 int non_default;
13911 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13912 return;
13914 if (TYPE_P (t))
13915 die = lookup_type_die (t);
13916 else if (DECL_P (t))
13917 die = lookup_decl_die (t);
13919 gcc_assert (die);
13921 parms = lang_hooks.get_innermost_generic_parms (t);
13922 if (!parms)
13923 /* T has no generic parameter. It means T is neither a generic type
13924 or function. End of story. */
13925 return;
13927 parms_num = TREE_VEC_LENGTH (parms);
13928 args = lang_hooks.get_innermost_generic_args (t);
13929 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13930 non_default = int_cst_value (TREE_CHAIN (args));
13931 else
13932 non_default = TREE_VEC_LENGTH (args);
13933 for (i = 0; i < parms_num; i++)
13935 tree parm, arg, arg_pack_elems;
13936 dw_die_ref parm_die;
13938 parm = TREE_VEC_ELT (parms, i);
13939 arg = TREE_VEC_ELT (args, i);
13940 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13941 gcc_assert (parm && TREE_VALUE (parm) && arg);
13943 if (parm && TREE_VALUE (parm) && arg)
13945 /* If PARM represents a template parameter pack,
13946 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13947 by DW_TAG_template_*_parameter DIEs for the argument
13948 pack elements of ARG. Note that ARG would then be
13949 an argument pack. */
13950 if (arg_pack_elems)
13951 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13952 arg_pack_elems,
13953 die);
13954 else
13955 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13956 true /* emit name */, die);
13957 if (i >= non_default)
13958 add_AT_flag (parm_die, DW_AT_default_value, 1);
13963 /* Create and return a DIE for PARM which should be
13964 the representation of a generic type parameter.
13965 For instance, in the C++ front end, PARM would be a template parameter.
13966 ARG is the argument to PARM.
13967 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13968 name of the PARM.
13969 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13970 as a child node. */
13972 static dw_die_ref
13973 generic_parameter_die (tree parm, tree arg,
13974 bool emit_name_p,
13975 dw_die_ref parent_die)
13977 dw_die_ref tmpl_die = NULL;
13978 const char *name = NULL;
13980 /* C++20 accepts class literals as template parameters, and var
13981 decls with initializers represent them. The VAR_DECLs would be
13982 rejected, but we can take the DECL_INITIAL constructor and
13983 attempt to expand it. */
13984 if (arg && VAR_P (arg))
13985 arg = DECL_INITIAL (arg);
13987 if (!parm || !DECL_NAME (parm) || !arg)
13988 return NULL;
13990 /* We support non-type generic parameters and arguments,
13991 type generic parameters and arguments, as well as
13992 generic generic parameters (a.k.a. template template parameters in C++)
13993 and arguments. */
13994 if (TREE_CODE (parm) == PARM_DECL)
13995 /* PARM is a nontype generic parameter */
13996 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13997 else if (TREE_CODE (parm) == TYPE_DECL)
13998 /* PARM is a type generic parameter. */
13999 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14000 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14001 /* PARM is a generic generic parameter.
14002 Its DIE is a GNU extension. It shall have a
14003 DW_AT_name attribute to represent the name of the template template
14004 parameter, and a DW_AT_GNU_template_name attribute to represent the
14005 name of the template template argument. */
14006 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14007 parent_die, parm);
14008 else
14009 gcc_unreachable ();
14011 if (tmpl_die)
14013 tree tmpl_type;
14015 /* If PARM is a generic parameter pack, it means we are
14016 emitting debug info for a template argument pack element.
14017 In other terms, ARG is a template argument pack element.
14018 In that case, we don't emit any DW_AT_name attribute for
14019 the die. */
14020 if (emit_name_p)
14022 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14023 gcc_assert (name);
14024 add_AT_string (tmpl_die, DW_AT_name, name);
14027 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14029 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14030 TMPL_DIE should have a child DW_AT_type attribute that is set
14031 to the type of the argument to PARM, which is ARG.
14032 If PARM is a type generic parameter, TMPL_DIE should have a
14033 child DW_AT_type that is set to ARG. */
14034 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14035 add_type_attribute (tmpl_die, tmpl_type,
14036 (TREE_THIS_VOLATILE (tmpl_type)
14037 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14038 false, parent_die);
14040 else
14042 /* So TMPL_DIE is a DIE representing a
14043 a generic generic template parameter, a.k.a template template
14044 parameter in C++ and arg is a template. */
14046 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14047 to the name of the argument. */
14048 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14049 if (name)
14050 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14053 if (TREE_CODE (parm) == PARM_DECL)
14054 /* So PARM is a non-type generic parameter.
14055 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14056 attribute of TMPL_DIE which value represents the value
14057 of ARG.
14058 We must be careful here:
14059 The value of ARG might reference some function decls.
14060 We might currently be emitting debug info for a generic
14061 type and types are emitted before function decls, we don't
14062 know if the function decls referenced by ARG will actually be
14063 emitted after cgraph computations.
14064 So must defer the generation of the DW_AT_const_value to
14065 after cgraph is ready. */
14066 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14069 return tmpl_die;
14072 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14073 PARM_PACK must be a template parameter pack. The returned DIE
14074 will be child DIE of PARENT_DIE. */
14076 static dw_die_ref
14077 template_parameter_pack_die (tree parm_pack,
14078 tree parm_pack_args,
14079 dw_die_ref parent_die)
14081 dw_die_ref die;
14082 int j;
14084 gcc_assert (parent_die && parm_pack);
14086 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14087 add_name_and_src_coords_attributes (die, parm_pack);
14088 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14089 generic_parameter_die (parm_pack,
14090 TREE_VEC_ELT (parm_pack_args, j),
14091 false /* Don't emit DW_AT_name */,
14092 die);
14093 return die;
14096 /* Return the DBX register number described by a given RTL node. */
14098 static unsigned int
14099 dbx_reg_number (const_rtx rtl)
14101 unsigned regno = REGNO (rtl);
14103 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14105 #ifdef LEAF_REG_REMAP
14106 if (crtl->uses_only_leaf_regs)
14108 int leaf_reg = LEAF_REG_REMAP (regno);
14109 if (leaf_reg != -1)
14110 regno = (unsigned) leaf_reg;
14112 #endif
14114 regno = DBX_REGISTER_NUMBER (regno);
14115 gcc_assert (regno != INVALID_REGNUM);
14116 return regno;
14119 /* Optionally add a DW_OP_piece term to a location description expression.
14120 DW_OP_piece is only added if the location description expression already
14121 doesn't end with DW_OP_piece. */
14123 static void
14124 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14126 dw_loc_descr_ref loc;
14128 if (*list_head != NULL)
14130 /* Find the end of the chain. */
14131 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14134 if (loc->dw_loc_opc != DW_OP_piece)
14135 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14139 /* Return a location descriptor that designates a machine register or
14140 zero if there is none. */
14142 static dw_loc_descr_ref
14143 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14145 rtx regs;
14147 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14148 return 0;
14150 /* We only use "frame base" when we're sure we're talking about the
14151 post-prologue local stack frame. We do this by *not* running
14152 register elimination until this point, and recognizing the special
14153 argument pointer and soft frame pointer rtx's.
14154 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14155 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14156 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14158 dw_loc_descr_ref result = NULL;
14160 if (dwarf_version >= 4 || !dwarf_strict)
14162 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14163 initialized);
14164 if (result)
14165 add_loc_descr (&result,
14166 new_loc_descr (DW_OP_stack_value, 0, 0));
14168 return result;
14171 regs = targetm.dwarf_register_span (rtl);
14173 if (REG_NREGS (rtl) > 1 || regs)
14174 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14175 else
14177 unsigned int dbx_regnum = dbx_reg_number (rtl);
14178 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14179 return 0;
14180 return one_reg_loc_descriptor (dbx_regnum, initialized);
14184 /* Return a location descriptor that designates a machine register for
14185 a given hard register number. */
14187 static dw_loc_descr_ref
14188 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14190 dw_loc_descr_ref reg_loc_descr;
14192 if (regno <= 31)
14193 reg_loc_descr
14194 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14195 else
14196 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14198 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14199 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14201 return reg_loc_descr;
14204 /* Given an RTL of a register, return a location descriptor that
14205 designates a value that spans more than one register. */
14207 static dw_loc_descr_ref
14208 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14209 enum var_init_status initialized)
14211 int size, i;
14212 dw_loc_descr_ref loc_result = NULL;
14214 /* Simple, contiguous registers. */
14215 if (regs == NULL_RTX)
14217 unsigned reg = REGNO (rtl);
14218 int nregs;
14220 #ifdef LEAF_REG_REMAP
14221 if (crtl->uses_only_leaf_regs)
14223 int leaf_reg = LEAF_REG_REMAP (reg);
14224 if (leaf_reg != -1)
14225 reg = (unsigned) leaf_reg;
14227 #endif
14229 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
14230 nregs = REG_NREGS (rtl);
14232 /* At present we only track constant-sized pieces. */
14233 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14234 return NULL;
14235 size /= nregs;
14237 loc_result = NULL;
14238 while (nregs--)
14240 dw_loc_descr_ref t;
14242 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
14243 VAR_INIT_STATUS_INITIALIZED);
14244 add_loc_descr (&loc_result, t);
14245 add_loc_descr_op_piece (&loc_result, size);
14246 ++reg;
14248 return loc_result;
14251 /* Now onto stupid register sets in non contiguous locations. */
14253 gcc_assert (GET_CODE (regs) == PARALLEL);
14255 /* At present we only track constant-sized pieces. */
14256 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14257 return NULL;
14258 loc_result = NULL;
14260 for (i = 0; i < XVECLEN (regs, 0); ++i)
14262 dw_loc_descr_ref t;
14264 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
14265 VAR_INIT_STATUS_INITIALIZED);
14266 add_loc_descr (&loc_result, t);
14267 add_loc_descr_op_piece (&loc_result, size);
14270 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14271 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14272 return loc_result;
14275 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14277 /* Return a location descriptor that designates a constant i,
14278 as a compound operation from constant (i >> shift), constant shift
14279 and DW_OP_shl. */
14281 static dw_loc_descr_ref
14282 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14284 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14285 add_loc_descr (&ret, int_loc_descriptor (shift));
14286 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14287 return ret;
14290 /* Return a location descriptor that designates constant POLY_I. */
14292 static dw_loc_descr_ref
14293 int_loc_descriptor (poly_int64 poly_i)
14295 enum dwarf_location_atom op;
14297 HOST_WIDE_INT i;
14298 if (!poly_i.is_constant (&i))
14300 /* Create location descriptions for the non-constant part and
14301 add any constant offset at the end. */
14302 dw_loc_descr_ref ret = NULL;
14303 HOST_WIDE_INT constant = poly_i.coeffs[0];
14304 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14306 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14307 if (coeff != 0)
14309 dw_loc_descr_ref start = ret;
14310 unsigned int factor;
14311 int bias;
14312 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14313 (j, &factor, &bias);
14315 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14316 add COEFF * (REGNO / FACTOR) now and subtract
14317 COEFF * BIAS from the final constant part. */
14318 constant -= coeff * bias;
14319 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14320 if (coeff % factor == 0)
14321 coeff /= factor;
14322 else
14324 int amount = exact_log2 (factor);
14325 gcc_assert (amount >= 0);
14326 add_loc_descr (&ret, int_loc_descriptor (amount));
14327 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14329 if (coeff != 1)
14331 add_loc_descr (&ret, int_loc_descriptor (coeff));
14332 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14334 if (start)
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14338 loc_descr_plus_const (&ret, constant);
14339 return ret;
14342 /* Pick the smallest representation of a constant, rather than just
14343 defaulting to the LEB encoding. */
14344 if (i >= 0)
14346 int clz = clz_hwi (i);
14347 int ctz = ctz_hwi (i);
14348 if (i <= 31)
14349 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14350 else if (i <= 0xff)
14351 op = DW_OP_const1u;
14352 else if (i <= 0xffff)
14353 op = DW_OP_const2u;
14354 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14355 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14356 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14357 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14358 while DW_OP_const4u is 5 bytes. */
14359 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14360 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14361 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14362 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14363 while DW_OP_const4u is 5 bytes. */
14364 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14366 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14367 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14368 <= 4)
14370 /* As i >= 2**31, the double cast above will yield a negative number.
14371 Since wrapping is defined in DWARF expressions we can output big
14372 positive integers as small negative ones, regardless of the size
14373 of host wide ints.
14375 Here, since the evaluator will handle 32-bit values and since i >=
14376 2**31, we know it's going to be interpreted as a negative literal:
14377 store it this way if we can do better than 5 bytes this way. */
14378 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14380 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14381 op = DW_OP_const4u;
14383 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14384 least 6 bytes: see if we can do better before falling back to it. */
14385 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14386 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14387 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14388 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14389 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14390 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14391 >= HOST_BITS_PER_WIDE_INT)
14392 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14393 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14394 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14395 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14396 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14397 && size_of_uleb128 (i) > 6)
14398 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14399 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14400 else
14401 op = DW_OP_constu;
14403 else
14405 if (i >= -0x80)
14406 op = DW_OP_const1s;
14407 else if (i >= -0x8000)
14408 op = DW_OP_const2s;
14409 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14411 if (size_of_int_loc_descriptor (i) < 5)
14413 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14414 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14415 return ret;
14417 op = DW_OP_const4s;
14419 else
14421 if (size_of_int_loc_descriptor (i)
14422 < (unsigned long) 1 + size_of_sleb128 (i))
14424 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14425 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14426 return ret;
14428 op = DW_OP_consts;
14432 return new_loc_descr (op, i, 0);
14435 /* Likewise, for unsigned constants. */
14437 static dw_loc_descr_ref
14438 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14440 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14441 const unsigned HOST_WIDE_INT max_uint
14442 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14444 /* If possible, use the clever signed constants handling. */
14445 if (i <= max_int)
14446 return int_loc_descriptor ((HOST_WIDE_INT) i);
14448 /* Here, we are left with positive numbers that cannot be represented as
14449 HOST_WIDE_INT, i.e.:
14450 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14452 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14453 whereas may be better to output a negative integer: thanks to integer
14454 wrapping, we know that:
14455 x = x - 2 ** DWARF2_ADDR_SIZE
14456 = x - 2 * (max (HOST_WIDE_INT) + 1)
14457 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14458 small negative integers. Let's try that in cases it will clearly improve
14459 the encoding: there is no gain turning DW_OP_const4u into
14460 DW_OP_const4s. */
14461 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14462 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14463 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14465 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14467 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14468 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14469 const HOST_WIDE_INT second_shift
14470 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14472 /* So we finally have:
14473 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14474 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14475 return int_loc_descriptor (second_shift);
14478 /* Last chance: fallback to a simple constant operation. */
14479 return new_loc_descr
14480 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14481 ? DW_OP_const4u
14482 : DW_OP_const8u,
14483 i, 0);
14486 /* Generate and return a location description that computes the unsigned
14487 comparison of the two stack top entries (a OP b where b is the top-most
14488 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14489 LE_EXPR, GT_EXPR or GE_EXPR. */
14491 static dw_loc_descr_ref
14492 uint_comparison_loc_list (enum tree_code kind)
14494 enum dwarf_location_atom op, flip_op;
14495 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14497 switch (kind)
14499 case LT_EXPR:
14500 op = DW_OP_lt;
14501 break;
14502 case LE_EXPR:
14503 op = DW_OP_le;
14504 break;
14505 case GT_EXPR:
14506 op = DW_OP_gt;
14507 break;
14508 case GE_EXPR:
14509 op = DW_OP_ge;
14510 break;
14511 default:
14512 gcc_unreachable ();
14515 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14516 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14518 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14519 possible to perform unsigned comparisons: we just have to distinguish
14520 three cases:
14522 1. when a and b have the same sign (as signed integers); then we should
14523 return: a OP(signed) b;
14525 2. when a is a negative signed integer while b is a positive one, then a
14526 is a greater unsigned integer than b; likewise when a and b's roles
14527 are flipped.
14529 So first, compare the sign of the two operands. */
14530 ret = new_loc_descr (DW_OP_over, 0, 0);
14531 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14532 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14533 /* If they have different signs (i.e. they have different sign bits), then
14534 the stack top value has now the sign bit set and thus it's smaller than
14535 zero. */
14536 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14537 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14538 add_loc_descr (&ret, bra_node);
14540 /* We are in case 1. At this point, we know both operands have the same
14541 sign, to it's safe to use the built-in signed comparison. */
14542 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14543 add_loc_descr (&ret, jmp_node);
14545 /* We are in case 2. Here, we know both operands do not have the same sign,
14546 so we have to flip the signed comparison. */
14547 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14548 tmp = new_loc_descr (flip_op, 0, 0);
14549 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14550 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14551 add_loc_descr (&ret, tmp);
14553 /* This dummy operation is necessary to make the two branches join. */
14554 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14555 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14556 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14557 add_loc_descr (&ret, tmp);
14559 return ret;
14562 /* Likewise, but takes the location description lists (might be destructive on
14563 them). Return NULL if either is NULL or if concatenation fails. */
14565 static dw_loc_list_ref
14566 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14567 enum tree_code kind)
14569 if (left == NULL || right == NULL)
14570 return NULL;
14572 add_loc_list (&left, right);
14573 if (left == NULL)
14574 return NULL;
14576 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14577 return left;
14580 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14581 without actually allocating it. */
14583 static unsigned long
14584 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14586 return size_of_int_loc_descriptor (i >> shift)
14587 + size_of_int_loc_descriptor (shift)
14588 + 1;
14591 /* Return size_of_locs (int_loc_descriptor (i)) without
14592 actually allocating it. */
14594 static unsigned long
14595 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14597 unsigned long s;
14599 if (i >= 0)
14601 int clz, ctz;
14602 if (i <= 31)
14603 return 1;
14604 else if (i <= 0xff)
14605 return 2;
14606 else if (i <= 0xffff)
14607 return 3;
14608 clz = clz_hwi (i);
14609 ctz = ctz_hwi (i);
14610 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14611 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14612 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14613 - clz - 5);
14614 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14615 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14616 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14617 - clz - 8);
14618 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14619 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14620 <= 4)
14621 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14622 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14623 return 5;
14624 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14625 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14626 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14627 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14628 - clz - 8);
14629 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14630 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14631 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14632 - clz - 16);
14633 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14634 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14635 && s > 6)
14636 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14637 - clz - 32);
14638 else
14639 return 1 + s;
14641 else
14643 if (i >= -0x80)
14644 return 2;
14645 else if (i >= -0x8000)
14646 return 3;
14647 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14649 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14651 s = size_of_int_loc_descriptor (-i) + 1;
14652 if (s < 5)
14653 return s;
14655 return 5;
14657 else
14659 unsigned long r = 1 + size_of_sleb128 (i);
14660 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14662 s = size_of_int_loc_descriptor (-i) + 1;
14663 if (s < r)
14664 return s;
14666 return r;
14671 /* Return loc description representing "address" of integer value.
14672 This can appear only as toplevel expression. */
14674 static dw_loc_descr_ref
14675 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14677 int litsize;
14678 dw_loc_descr_ref loc_result = NULL;
14680 if (!(dwarf_version >= 4 || !dwarf_strict))
14681 return NULL;
14683 litsize = size_of_int_loc_descriptor (i);
14684 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14685 is more compact. For DW_OP_stack_value we need:
14686 litsize + 1 (DW_OP_stack_value)
14687 and for DW_OP_implicit_value:
14688 1 (DW_OP_implicit_value) + 1 (length) + size. */
14689 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14691 loc_result = int_loc_descriptor (i);
14692 add_loc_descr (&loc_result,
14693 new_loc_descr (DW_OP_stack_value, 0, 0));
14694 return loc_result;
14697 loc_result = new_loc_descr (DW_OP_implicit_value,
14698 size, 0);
14699 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14700 loc_result->dw_loc_oprnd2.v.val_int = i;
14701 return loc_result;
14704 /* Return a location descriptor that designates a base+offset location. */
14706 static dw_loc_descr_ref
14707 based_loc_descr (rtx reg, poly_int64 offset,
14708 enum var_init_status initialized)
14710 unsigned int regno;
14711 dw_loc_descr_ref result;
14712 dw_fde_ref fde = cfun->fde;
14714 /* We only use "frame base" when we're sure we're talking about the
14715 post-prologue local stack frame. We do this by *not* running
14716 register elimination until this point, and recognizing the special
14717 argument pointer and soft frame pointer rtx's. */
14718 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14720 rtx elim = (ira_use_lra_p
14721 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14722 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14724 if (elim != reg)
14726 /* Allow hard frame pointer here even if frame pointer
14727 isn't used since hard frame pointer is encoded with
14728 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14729 not hard frame pointer directly. */
14730 elim = strip_offset_and_add (elim, &offset);
14731 gcc_assert (elim == hard_frame_pointer_rtx
14732 || elim == stack_pointer_rtx);
14734 /* If drap register is used to align stack, use frame
14735 pointer + offset to access stack variables. If stack
14736 is aligned without drap, use stack pointer + offset to
14737 access stack variables. */
14738 if (crtl->stack_realign_tried
14739 && reg == frame_pointer_rtx)
14741 int base_reg
14742 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14743 ? HARD_FRAME_POINTER_REGNUM
14744 : REGNO (elim));
14745 return new_reg_loc_descr (base_reg, offset);
14748 gcc_assert (frame_pointer_fb_offset_valid);
14749 offset += frame_pointer_fb_offset;
14750 HOST_WIDE_INT const_offset;
14751 if (offset.is_constant (&const_offset))
14752 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14753 else
14755 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14756 loc_descr_plus_const (&ret, offset);
14757 return ret;
14762 regno = REGNO (reg);
14763 #ifdef LEAF_REG_REMAP
14764 if (crtl->uses_only_leaf_regs)
14766 int leaf_reg = LEAF_REG_REMAP (regno);
14767 if (leaf_reg != -1)
14768 regno = (unsigned) leaf_reg;
14770 #endif
14771 regno = DWARF_FRAME_REGNUM (regno);
14773 HOST_WIDE_INT const_offset;
14774 if (!optimize && fde
14775 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14776 && offset.is_constant (&const_offset))
14778 /* Use cfa+offset to represent the location of arguments passed
14779 on the stack when drap is used to align stack.
14780 Only do this when not optimizing, for optimized code var-tracking
14781 is supposed to track where the arguments live and the register
14782 used as vdrap or drap in some spot might be used for something
14783 else in other part of the routine. */
14784 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14787 result = new_reg_loc_descr (regno, offset);
14789 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14790 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14792 return result;
14795 /* Return true if this RTL expression describes a base+offset calculation. */
14797 static inline int
14798 is_based_loc (const_rtx rtl)
14800 return (GET_CODE (rtl) == PLUS
14801 && ((REG_P (XEXP (rtl, 0))
14802 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14803 && CONST_INT_P (XEXP (rtl, 1)))));
14806 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14807 failed. */
14809 static dw_loc_descr_ref
14810 tls_mem_loc_descriptor (rtx mem)
14812 tree base;
14813 dw_loc_descr_ref loc_result;
14815 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14816 return NULL;
14818 base = get_base_address (MEM_EXPR (mem));
14819 if (base == NULL
14820 || !VAR_P (base)
14821 || !DECL_THREAD_LOCAL_P (base))
14822 return NULL;
14824 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14825 if (loc_result == NULL)
14826 return NULL;
14828 if (maybe_ne (MEM_OFFSET (mem), 0))
14829 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14831 return loc_result;
14834 /* Output debug info about reason why we failed to expand expression as dwarf
14835 expression. */
14837 static void
14838 expansion_failed (tree expr, rtx rtl, char const *reason)
14840 if (dump_file && (dump_flags & TDF_DETAILS))
14842 fprintf (dump_file, "Failed to expand as dwarf: ");
14843 if (expr)
14844 print_generic_expr (dump_file, expr, dump_flags);
14845 if (rtl)
14847 fprintf (dump_file, "\n");
14848 print_rtl (dump_file, rtl);
14850 fprintf (dump_file, "\nReason: %s\n", reason);
14854 /* Helper function for const_ok_for_output. */
14856 static bool
14857 const_ok_for_output_1 (rtx rtl)
14859 if (targetm.const_not_ok_for_debug_p (rtl))
14861 if (GET_CODE (rtl) != UNSPEC)
14863 expansion_failed (NULL_TREE, rtl,
14864 "Expression rejected for debug by the backend.\n");
14865 return false;
14868 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14869 the target hook doesn't explicitly allow it in debug info, assume
14870 we can't express it in the debug info. */
14871 /* Don't complain about TLS UNSPECs, those are just too hard to
14872 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14873 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14874 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14875 if (flag_checking
14876 && (XVECLEN (rtl, 0) == 0
14877 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14878 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14879 inform (current_function_decl
14880 ? DECL_SOURCE_LOCATION (current_function_decl)
14881 : UNKNOWN_LOCATION,
14882 #if NUM_UNSPEC_VALUES > 0
14883 "non-delegitimized UNSPEC %s (%d) found in variable location",
14884 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14885 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14886 #else
14887 "non-delegitimized UNSPEC %d found in variable location",
14888 #endif
14889 XINT (rtl, 1));
14890 expansion_failed (NULL_TREE, rtl,
14891 "UNSPEC hasn't been delegitimized.\n");
14892 return false;
14895 if (CONST_POLY_INT_P (rtl))
14896 return false;
14898 /* FIXME: Refer to PR60655. It is possible for simplification
14899 of rtl expressions in var tracking to produce such expressions.
14900 We should really identify / validate expressions
14901 enclosed in CONST that can be handled by assemblers on various
14902 targets and only handle legitimate cases here. */
14903 switch (GET_CODE (rtl))
14905 case SYMBOL_REF:
14906 break;
14907 case NOT:
14908 case NEG:
14909 return false;
14910 case PLUS:
14912 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14913 operands. */
14914 subrtx_var_iterator::array_type array;
14915 bool first = false;
14916 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14917 if (SYMBOL_REF_P (*iter)
14918 || LABEL_P (*iter)
14919 || GET_CODE (*iter) == UNSPEC)
14921 first = true;
14922 break;
14924 if (!first)
14925 return true;
14926 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14927 if (SYMBOL_REF_P (*iter)
14928 || LABEL_P (*iter)
14929 || GET_CODE (*iter) == UNSPEC)
14930 return false;
14931 return true;
14933 case MINUS:
14935 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14936 appear in the second operand of MINUS. */
14937 subrtx_var_iterator::array_type array;
14938 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14939 if (SYMBOL_REF_P (*iter)
14940 || LABEL_P (*iter)
14941 || GET_CODE (*iter) == UNSPEC)
14942 return false;
14943 return true;
14945 default:
14946 return true;
14949 if (CONSTANT_POOL_ADDRESS_P (rtl))
14951 bool marked;
14952 get_pool_constant_mark (rtl, &marked);
14953 /* If all references to this pool constant were optimized away,
14954 it was not output and thus we can't represent it. */
14955 if (!marked)
14957 expansion_failed (NULL_TREE, rtl,
14958 "Constant was removed from constant pool.\n");
14959 return false;
14963 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14964 return false;
14966 /* Avoid references to external symbols in debug info, on several targets
14967 the linker might even refuse to link when linking a shared library,
14968 and in many other cases the relocations for .debug_info/.debug_loc are
14969 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14970 to be defined within the same shared library or executable are fine. */
14971 if (SYMBOL_REF_EXTERNAL_P (rtl))
14973 tree decl = SYMBOL_REF_DECL (rtl);
14975 if (decl == NULL || !targetm.binds_local_p (decl))
14977 expansion_failed (NULL_TREE, rtl,
14978 "Symbol not defined in current TU.\n");
14979 return false;
14983 return true;
14986 /* Return true if constant RTL can be emitted in DW_OP_addr or
14987 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14988 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14990 static bool
14991 const_ok_for_output (rtx rtl)
14993 if (GET_CODE (rtl) == SYMBOL_REF)
14994 return const_ok_for_output_1 (rtl);
14996 if (GET_CODE (rtl) == CONST)
14998 subrtx_var_iterator::array_type array;
14999 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15000 if (!const_ok_for_output_1 (*iter))
15001 return false;
15002 return true;
15005 return true;
15008 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15009 if possible, NULL otherwise. */
15011 static dw_die_ref
15012 base_type_for_mode (machine_mode mode, bool unsignedp)
15014 dw_die_ref type_die;
15015 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15017 if (type == NULL)
15018 return NULL;
15019 switch (TREE_CODE (type))
15021 case INTEGER_TYPE:
15022 case REAL_TYPE:
15023 break;
15024 default:
15025 return NULL;
15027 type_die = lookup_type_die (type);
15028 if (!type_die)
15029 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15030 comp_unit_die ());
15031 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15032 return NULL;
15033 return type_die;
15036 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15037 type matching MODE, or, if MODE is narrower than or as wide as
15038 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15039 possible. */
15041 static dw_loc_descr_ref
15042 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15044 machine_mode outer_mode = mode;
15045 dw_die_ref type_die;
15046 dw_loc_descr_ref cvt;
15048 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15050 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15051 return op;
15053 type_die = base_type_for_mode (outer_mode, 1);
15054 if (type_die == NULL)
15055 return NULL;
15056 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15057 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15058 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15059 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15060 add_loc_descr (&op, cvt);
15061 return op;
15064 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15066 static dw_loc_descr_ref
15067 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15068 dw_loc_descr_ref op1)
15070 dw_loc_descr_ref ret = op0;
15071 add_loc_descr (&ret, op1);
15072 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15073 if (STORE_FLAG_VALUE != 1)
15075 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15076 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15078 return ret;
15081 /* Subroutine of scompare_loc_descriptor for the case in which we're
15082 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15083 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15085 static dw_loc_descr_ref
15086 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15087 scalar_int_mode op_mode,
15088 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15090 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15091 dw_loc_descr_ref cvt;
15093 if (type_die == NULL)
15094 return NULL;
15095 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15096 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15097 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15098 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15099 add_loc_descr (&op0, cvt);
15100 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15101 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15102 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15103 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15104 add_loc_descr (&op1, cvt);
15105 return compare_loc_descriptor (op, op0, op1);
15108 /* Subroutine of scompare_loc_descriptor for the case in which we're
15109 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15110 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15112 static dw_loc_descr_ref
15113 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15114 scalar_int_mode op_mode,
15115 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15117 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15118 /* For eq/ne, if the operands are known to be zero-extended,
15119 there is no need to do the fancy shifting up. */
15120 if (op == DW_OP_eq || op == DW_OP_ne)
15122 dw_loc_descr_ref last0, last1;
15123 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15125 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15127 /* deref_size zero extends, and for constants we can check
15128 whether they are zero extended or not. */
15129 if (((last0->dw_loc_opc == DW_OP_deref_size
15130 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15131 || (CONST_INT_P (XEXP (rtl, 0))
15132 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15133 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15134 && ((last1->dw_loc_opc == DW_OP_deref_size
15135 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15136 || (CONST_INT_P (XEXP (rtl, 1))
15137 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15138 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15139 return compare_loc_descriptor (op, op0, op1);
15141 /* EQ/NE comparison against constant in narrower type than
15142 DWARF2_ADDR_SIZE can be performed either as
15143 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15144 DW_OP_{eq,ne}
15146 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15147 DW_OP_{eq,ne}. Pick whatever is shorter. */
15148 if (CONST_INT_P (XEXP (rtl, 1))
15149 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15150 && (size_of_int_loc_descriptor (shift) + 1
15151 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15152 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15153 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15154 & GET_MODE_MASK (op_mode))))
15156 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15157 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15158 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15159 & GET_MODE_MASK (op_mode));
15160 return compare_loc_descriptor (op, op0, op1);
15163 add_loc_descr (&op0, int_loc_descriptor (shift));
15164 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15165 if (CONST_INT_P (XEXP (rtl, 1)))
15166 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15167 else
15169 add_loc_descr (&op1, int_loc_descriptor (shift));
15170 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15172 return compare_loc_descriptor (op, op0, op1);
15175 /* Return location descriptor for signed comparison OP RTL. */
15177 static dw_loc_descr_ref
15178 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15179 machine_mode mem_mode)
15181 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15182 dw_loc_descr_ref op0, op1;
15184 if (op_mode == VOIDmode)
15185 op_mode = GET_MODE (XEXP (rtl, 1));
15186 if (op_mode == VOIDmode)
15187 return NULL;
15189 scalar_int_mode int_op_mode;
15190 if (dwarf_strict
15191 && dwarf_version < 5
15192 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15193 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15194 return NULL;
15196 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15197 VAR_INIT_STATUS_INITIALIZED);
15198 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15199 VAR_INIT_STATUS_INITIALIZED);
15201 if (op0 == NULL || op1 == NULL)
15202 return NULL;
15204 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15206 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15207 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15209 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15210 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15212 return compare_loc_descriptor (op, op0, op1);
15215 /* Return location descriptor for unsigned comparison OP RTL. */
15217 static dw_loc_descr_ref
15218 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15219 machine_mode mem_mode)
15221 dw_loc_descr_ref op0, op1;
15223 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15224 if (test_op_mode == VOIDmode)
15225 test_op_mode = GET_MODE (XEXP (rtl, 1));
15227 scalar_int_mode op_mode;
15228 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15229 return NULL;
15231 if (dwarf_strict
15232 && dwarf_version < 5
15233 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15234 return NULL;
15236 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15237 VAR_INIT_STATUS_INITIALIZED);
15238 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15239 VAR_INIT_STATUS_INITIALIZED);
15241 if (op0 == NULL || op1 == NULL)
15242 return NULL;
15244 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15246 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15247 dw_loc_descr_ref last0, last1;
15248 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15250 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15252 if (CONST_INT_P (XEXP (rtl, 0)))
15253 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15254 /* deref_size zero extends, so no need to mask it again. */
15255 else if (last0->dw_loc_opc != DW_OP_deref_size
15256 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15258 add_loc_descr (&op0, int_loc_descriptor (mask));
15259 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15261 if (CONST_INT_P (XEXP (rtl, 1)))
15262 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15263 /* deref_size zero extends, so no need to mask it again. */
15264 else if (last1->dw_loc_opc != DW_OP_deref_size
15265 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15267 add_loc_descr (&op1, int_loc_descriptor (mask));
15268 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15271 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15273 HOST_WIDE_INT bias = 1;
15274 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15275 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15276 if (CONST_INT_P (XEXP (rtl, 1)))
15277 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15278 + INTVAL (XEXP (rtl, 1)));
15279 else
15280 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15281 bias, 0));
15283 return compare_loc_descriptor (op, op0, op1);
15286 /* Return location descriptor for {U,S}{MIN,MAX}. */
15288 static dw_loc_descr_ref
15289 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15290 machine_mode mem_mode)
15292 enum dwarf_location_atom op;
15293 dw_loc_descr_ref op0, op1, ret;
15294 dw_loc_descr_ref bra_node, drop_node;
15296 scalar_int_mode int_mode;
15297 if (dwarf_strict
15298 && dwarf_version < 5
15299 && (!is_a <scalar_int_mode> (mode, &int_mode)
15300 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15301 return NULL;
15303 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15304 VAR_INIT_STATUS_INITIALIZED);
15305 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15306 VAR_INIT_STATUS_INITIALIZED);
15308 if (op0 == NULL || op1 == NULL)
15309 return NULL;
15311 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15312 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15313 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15314 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15316 /* Checked by the caller. */
15317 int_mode = as_a <scalar_int_mode> (mode);
15318 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15320 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15321 add_loc_descr (&op0, int_loc_descriptor (mask));
15322 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15323 add_loc_descr (&op1, int_loc_descriptor (mask));
15324 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15326 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15328 HOST_WIDE_INT bias = 1;
15329 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15330 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15331 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15334 else if (is_a <scalar_int_mode> (mode, &int_mode)
15335 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15337 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15338 add_loc_descr (&op0, int_loc_descriptor (shift));
15339 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15340 add_loc_descr (&op1, int_loc_descriptor (shift));
15341 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15343 else if (is_a <scalar_int_mode> (mode, &int_mode)
15344 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15346 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15347 dw_loc_descr_ref cvt;
15348 if (type_die == NULL)
15349 return NULL;
15350 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15351 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15352 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15353 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15354 add_loc_descr (&op0, cvt);
15355 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15356 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15357 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15358 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15359 add_loc_descr (&op1, cvt);
15362 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15363 op = DW_OP_lt;
15364 else
15365 op = DW_OP_gt;
15366 ret = op0;
15367 add_loc_descr (&ret, op1);
15368 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15369 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15370 add_loc_descr (&ret, bra_node);
15371 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15372 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15373 add_loc_descr (&ret, drop_node);
15374 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15375 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15376 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15377 && is_a <scalar_int_mode> (mode, &int_mode)
15378 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15379 ret = convert_descriptor_to_mode (int_mode, ret);
15380 return ret;
15383 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15384 but after converting arguments to type_die, afterwards
15385 convert back to unsigned. */
15387 static dw_loc_descr_ref
15388 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15389 scalar_int_mode mode, machine_mode mem_mode)
15391 dw_loc_descr_ref cvt, op0, op1;
15393 if (type_die == NULL)
15394 return NULL;
15395 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15396 VAR_INIT_STATUS_INITIALIZED);
15397 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15398 VAR_INIT_STATUS_INITIALIZED);
15399 if (op0 == NULL || op1 == NULL)
15400 return NULL;
15401 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15402 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15403 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15404 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15405 add_loc_descr (&op0, cvt);
15406 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15407 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15408 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15409 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15410 add_loc_descr (&op1, cvt);
15411 add_loc_descr (&op0, op1);
15412 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15413 return convert_descriptor_to_mode (mode, op0);
15416 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15417 const0 is DW_OP_lit0 or corresponding typed constant,
15418 const1 is DW_OP_lit1 or corresponding typed constant
15419 and constMSB is constant with just the MSB bit set
15420 for the mode):
15421 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15422 L1: const0 DW_OP_swap
15423 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15424 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15425 L3: DW_OP_drop
15426 L4: DW_OP_nop
15428 CTZ is similar:
15429 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15430 L1: const0 DW_OP_swap
15431 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15432 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15433 L3: DW_OP_drop
15434 L4: DW_OP_nop
15436 FFS is similar:
15437 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15438 L1: const1 DW_OP_swap
15439 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15440 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15441 L3: DW_OP_drop
15442 L4: DW_OP_nop */
15444 static dw_loc_descr_ref
15445 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15446 machine_mode mem_mode)
15448 dw_loc_descr_ref op0, ret, tmp;
15449 HOST_WIDE_INT valv;
15450 dw_loc_descr_ref l1jump, l1label;
15451 dw_loc_descr_ref l2jump, l2label;
15452 dw_loc_descr_ref l3jump, l3label;
15453 dw_loc_descr_ref l4jump, l4label;
15454 rtx msb;
15456 if (GET_MODE (XEXP (rtl, 0)) != mode)
15457 return NULL;
15459 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15460 VAR_INIT_STATUS_INITIALIZED);
15461 if (op0 == NULL)
15462 return NULL;
15463 ret = op0;
15464 if (GET_CODE (rtl) == CLZ)
15466 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15467 valv = GET_MODE_BITSIZE (mode);
15469 else if (GET_CODE (rtl) == FFS)
15470 valv = 0;
15471 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15472 valv = GET_MODE_BITSIZE (mode);
15473 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15474 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15475 add_loc_descr (&ret, l1jump);
15476 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15477 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15478 VAR_INIT_STATUS_INITIALIZED);
15479 if (tmp == NULL)
15480 return NULL;
15481 add_loc_descr (&ret, tmp);
15482 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15483 add_loc_descr (&ret, l4jump);
15484 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15485 ? const1_rtx : const0_rtx,
15486 mode, mem_mode,
15487 VAR_INIT_STATUS_INITIALIZED);
15488 if (l1label == NULL)
15489 return NULL;
15490 add_loc_descr (&ret, l1label);
15491 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15492 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15493 add_loc_descr (&ret, l2label);
15494 if (GET_CODE (rtl) != CLZ)
15495 msb = const1_rtx;
15496 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15497 msb = GEN_INT (HOST_WIDE_INT_1U
15498 << (GET_MODE_BITSIZE (mode) - 1));
15499 else
15500 msb = immed_wide_int_const
15501 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15502 GET_MODE_PRECISION (mode)), mode);
15503 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15504 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15505 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15506 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15507 else
15508 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15509 VAR_INIT_STATUS_INITIALIZED);
15510 if (tmp == NULL)
15511 return NULL;
15512 add_loc_descr (&ret, tmp);
15513 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15514 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15515 add_loc_descr (&ret, l3jump);
15516 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15517 VAR_INIT_STATUS_INITIALIZED);
15518 if (tmp == NULL)
15519 return NULL;
15520 add_loc_descr (&ret, tmp);
15521 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15522 ? DW_OP_shl : DW_OP_shr, 0, 0));
15523 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15524 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15525 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15526 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15527 add_loc_descr (&ret, l2jump);
15528 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15529 add_loc_descr (&ret, l3label);
15530 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15531 add_loc_descr (&ret, l4label);
15532 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15533 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15534 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15535 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15536 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15537 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15538 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15539 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15540 return ret;
15543 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15544 const1 is DW_OP_lit1 or corresponding typed constant):
15545 const0 DW_OP_swap
15546 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15547 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15548 L2: DW_OP_drop
15550 PARITY is similar:
15551 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15552 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15553 L2: DW_OP_drop */
15555 static dw_loc_descr_ref
15556 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15557 machine_mode mem_mode)
15559 dw_loc_descr_ref op0, ret, tmp;
15560 dw_loc_descr_ref l1jump, l1label;
15561 dw_loc_descr_ref l2jump, l2label;
15563 if (GET_MODE (XEXP (rtl, 0)) != mode)
15564 return NULL;
15566 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15567 VAR_INIT_STATUS_INITIALIZED);
15568 if (op0 == NULL)
15569 return NULL;
15570 ret = op0;
15571 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15572 VAR_INIT_STATUS_INITIALIZED);
15573 if (tmp == NULL)
15574 return NULL;
15575 add_loc_descr (&ret, tmp);
15576 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15577 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15578 add_loc_descr (&ret, l1label);
15579 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15580 add_loc_descr (&ret, l2jump);
15581 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15582 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15583 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15584 VAR_INIT_STATUS_INITIALIZED);
15585 if (tmp == NULL)
15586 return NULL;
15587 add_loc_descr (&ret, tmp);
15588 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15589 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15590 ? DW_OP_plus : DW_OP_xor, 0, 0));
15591 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15592 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15593 VAR_INIT_STATUS_INITIALIZED);
15594 add_loc_descr (&ret, tmp);
15595 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15596 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15597 add_loc_descr (&ret, l1jump);
15598 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15599 add_loc_descr (&ret, l2label);
15600 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15601 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15602 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15603 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15604 return ret;
15607 /* BSWAP (constS is initial shift count, either 56 or 24):
15608 constS const0
15609 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15610 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15611 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15612 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15613 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15615 static dw_loc_descr_ref
15616 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15617 machine_mode mem_mode)
15619 dw_loc_descr_ref op0, ret, tmp;
15620 dw_loc_descr_ref l1jump, l1label;
15621 dw_loc_descr_ref l2jump, l2label;
15623 if (BITS_PER_UNIT != 8
15624 || (GET_MODE_BITSIZE (mode) != 32
15625 && GET_MODE_BITSIZE (mode) != 64))
15626 return NULL;
15628 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15629 VAR_INIT_STATUS_INITIALIZED);
15630 if (op0 == NULL)
15631 return NULL;
15633 ret = op0;
15634 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15635 mode, mem_mode,
15636 VAR_INIT_STATUS_INITIALIZED);
15637 if (tmp == NULL)
15638 return NULL;
15639 add_loc_descr (&ret, tmp);
15640 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15641 VAR_INIT_STATUS_INITIALIZED);
15642 if (tmp == NULL)
15643 return NULL;
15644 add_loc_descr (&ret, tmp);
15645 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15646 add_loc_descr (&ret, l1label);
15647 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15648 mode, mem_mode,
15649 VAR_INIT_STATUS_INITIALIZED);
15650 add_loc_descr (&ret, tmp);
15651 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15652 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15653 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15654 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15655 VAR_INIT_STATUS_INITIALIZED);
15656 if (tmp == NULL)
15657 return NULL;
15658 add_loc_descr (&ret, tmp);
15659 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15660 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15661 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15662 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15663 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15664 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15665 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15666 VAR_INIT_STATUS_INITIALIZED);
15667 add_loc_descr (&ret, tmp);
15668 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15669 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15670 add_loc_descr (&ret, l2jump);
15671 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15672 VAR_INIT_STATUS_INITIALIZED);
15673 add_loc_descr (&ret, tmp);
15674 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15675 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15676 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15677 add_loc_descr (&ret, l1jump);
15678 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15679 add_loc_descr (&ret, l2label);
15680 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15681 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15682 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15683 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15684 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15685 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15686 return ret;
15689 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15690 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15691 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15692 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15694 ROTATERT is similar:
15695 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15696 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15697 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15699 static dw_loc_descr_ref
15700 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15701 machine_mode mem_mode)
15703 rtx rtlop1 = XEXP (rtl, 1);
15704 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15705 int i;
15707 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15708 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15709 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15710 VAR_INIT_STATUS_INITIALIZED);
15711 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15712 VAR_INIT_STATUS_INITIALIZED);
15713 if (op0 == NULL || op1 == NULL)
15714 return NULL;
15715 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15716 for (i = 0; i < 2; i++)
15718 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15719 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15720 mode, mem_mode,
15721 VAR_INIT_STATUS_INITIALIZED);
15722 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15723 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15724 ? DW_OP_const4u
15725 : HOST_BITS_PER_WIDE_INT == 64
15726 ? DW_OP_const8u : DW_OP_constu,
15727 GET_MODE_MASK (mode), 0);
15728 else
15729 mask[i] = NULL;
15730 if (mask[i] == NULL)
15731 return NULL;
15732 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15734 ret = op0;
15735 add_loc_descr (&ret, op1);
15736 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15737 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15738 if (GET_CODE (rtl) == ROTATERT)
15740 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15741 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15742 GET_MODE_BITSIZE (mode), 0));
15744 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15745 if (mask[0] != NULL)
15746 add_loc_descr (&ret, mask[0]);
15747 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15748 if (mask[1] != NULL)
15750 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15751 add_loc_descr (&ret, mask[1]);
15752 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15754 if (GET_CODE (rtl) == ROTATE)
15756 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15757 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15758 GET_MODE_BITSIZE (mode), 0));
15760 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15761 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15762 return ret;
15765 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15766 for DEBUG_PARAMETER_REF RTL. */
15768 static dw_loc_descr_ref
15769 parameter_ref_descriptor (rtx rtl)
15771 dw_loc_descr_ref ret;
15772 dw_die_ref ref;
15774 if (dwarf_strict)
15775 return NULL;
15776 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15777 /* With LTO during LTRANS we get the late DIE that refers to the early
15778 DIE, thus we add another indirection here. This seems to confuse
15779 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15780 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15781 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15782 if (ref)
15784 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15785 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15786 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15788 else
15790 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15791 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15793 return ret;
15796 /* The following routine converts the RTL for a variable or parameter
15797 (resident in memory) into an equivalent Dwarf representation of a
15798 mechanism for getting the address of that same variable onto the top of a
15799 hypothetical "address evaluation" stack.
15801 When creating memory location descriptors, we are effectively transforming
15802 the RTL for a memory-resident object into its Dwarf postfix expression
15803 equivalent. This routine recursively descends an RTL tree, turning
15804 it into Dwarf postfix code as it goes.
15806 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15808 MEM_MODE is the mode of the memory reference, needed to handle some
15809 autoincrement addressing modes.
15811 Return 0 if we can't represent the location. */
15813 dw_loc_descr_ref
15814 mem_loc_descriptor (rtx rtl, machine_mode mode,
15815 machine_mode mem_mode,
15816 enum var_init_status initialized)
15818 dw_loc_descr_ref mem_loc_result = NULL;
15819 enum dwarf_location_atom op;
15820 dw_loc_descr_ref op0, op1;
15821 rtx inner = NULL_RTX;
15822 poly_int64 offset;
15824 if (mode == VOIDmode)
15825 mode = GET_MODE (rtl);
15827 /* Note that for a dynamically sized array, the location we will generate a
15828 description of here will be the lowest numbered location which is
15829 actually within the array. That's *not* necessarily the same as the
15830 zeroth element of the array. */
15832 rtl = targetm.delegitimize_address (rtl);
15834 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15835 return NULL;
15837 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15838 switch (GET_CODE (rtl))
15840 case POST_INC:
15841 case POST_DEC:
15842 case POST_MODIFY:
15843 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15845 case SUBREG:
15846 /* The case of a subreg may arise when we have a local (register)
15847 variable or a formal (register) parameter which doesn't quite fill
15848 up an entire register. For now, just assume that it is
15849 legitimate to make the Dwarf info refer to the whole register which
15850 contains the given subreg. */
15851 if (!subreg_lowpart_p (rtl))
15852 break;
15853 inner = SUBREG_REG (rtl);
15854 /* FALLTHRU */
15855 case TRUNCATE:
15856 if (inner == NULL_RTX)
15857 inner = XEXP (rtl, 0);
15858 if (is_a <scalar_int_mode> (mode, &int_mode)
15859 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15860 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15861 #ifdef POINTERS_EXTEND_UNSIGNED
15862 || (int_mode == Pmode && mem_mode != VOIDmode)
15863 #endif
15865 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15867 mem_loc_result = mem_loc_descriptor (inner,
15868 inner_mode,
15869 mem_mode, initialized);
15870 break;
15872 if (dwarf_strict && dwarf_version < 5)
15873 break;
15874 if (is_a <scalar_int_mode> (mode, &int_mode)
15875 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15876 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15877 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15879 dw_die_ref type_die;
15880 dw_loc_descr_ref cvt;
15882 mem_loc_result = mem_loc_descriptor (inner,
15883 GET_MODE (inner),
15884 mem_mode, initialized);
15885 if (mem_loc_result == NULL)
15886 break;
15887 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15888 if (type_die == NULL)
15890 mem_loc_result = NULL;
15891 break;
15893 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15894 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15895 else
15896 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15897 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15898 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15899 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15900 add_loc_descr (&mem_loc_result, cvt);
15901 if (is_a <scalar_int_mode> (mode, &int_mode)
15902 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15904 /* Convert it to untyped afterwards. */
15905 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15906 add_loc_descr (&mem_loc_result, cvt);
15909 break;
15911 case REG:
15912 if (!is_a <scalar_int_mode> (mode, &int_mode)
15913 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15914 && rtl != arg_pointer_rtx
15915 && rtl != frame_pointer_rtx
15916 #ifdef POINTERS_EXTEND_UNSIGNED
15917 && (int_mode != Pmode || mem_mode == VOIDmode)
15918 #endif
15921 dw_die_ref type_die;
15922 unsigned int dbx_regnum;
15924 if (dwarf_strict && dwarf_version < 5)
15925 break;
15926 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15927 break;
15928 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15929 if (type_die == NULL)
15930 break;
15932 dbx_regnum = dbx_reg_number (rtl);
15933 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15934 break;
15935 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15936 dbx_regnum, 0);
15937 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15938 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15939 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15940 break;
15942 /* Whenever a register number forms a part of the description of the
15943 method for calculating the (dynamic) address of a memory resident
15944 object, DWARF rules require the register number be referred to as
15945 a "base register". This distinction is not based in any way upon
15946 what category of register the hardware believes the given register
15947 belongs to. This is strictly DWARF terminology we're dealing with
15948 here. Note that in cases where the location of a memory-resident
15949 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15950 OP_CONST (0)) the actual DWARF location descriptor that we generate
15951 may just be OP_BASEREG (basereg). This may look deceptively like
15952 the object in question was allocated to a register (rather than in
15953 memory) so DWARF consumers need to be aware of the subtle
15954 distinction between OP_REG and OP_BASEREG. */
15955 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15956 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15957 else if (stack_realign_drap
15958 && crtl->drap_reg
15959 && crtl->args.internal_arg_pointer == rtl
15960 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15962 /* If RTL is internal_arg_pointer, which has been optimized
15963 out, use DRAP instead. */
15964 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15965 VAR_INIT_STATUS_INITIALIZED);
15967 break;
15969 case SIGN_EXTEND:
15970 case ZERO_EXTEND:
15971 if (!is_a <scalar_int_mode> (mode, &int_mode)
15972 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15973 break;
15974 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15975 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15976 if (op0 == 0)
15977 break;
15978 else if (GET_CODE (rtl) == ZERO_EXTEND
15979 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15980 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15981 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15982 to expand zero extend as two shifts instead of
15983 masking. */
15984 && GET_MODE_SIZE (inner_mode) <= 4)
15986 mem_loc_result = op0;
15987 add_loc_descr (&mem_loc_result,
15988 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15989 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15991 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15993 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15994 shift *= BITS_PER_UNIT;
15995 if (GET_CODE (rtl) == SIGN_EXTEND)
15996 op = DW_OP_shra;
15997 else
15998 op = DW_OP_shr;
15999 mem_loc_result = op0;
16000 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16001 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16002 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16003 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16005 else if (!dwarf_strict || dwarf_version >= 5)
16007 dw_die_ref type_die1, type_die2;
16008 dw_loc_descr_ref cvt;
16010 type_die1 = base_type_for_mode (inner_mode,
16011 GET_CODE (rtl) == ZERO_EXTEND);
16012 if (type_die1 == NULL)
16013 break;
16014 type_die2 = base_type_for_mode (int_mode, 1);
16015 if (type_die2 == NULL)
16016 break;
16017 mem_loc_result = op0;
16018 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16019 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16020 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16021 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16022 add_loc_descr (&mem_loc_result, cvt);
16023 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16024 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16025 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16026 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16027 add_loc_descr (&mem_loc_result, cvt);
16029 break;
16031 case MEM:
16033 rtx new_rtl = avoid_constant_pool_reference (rtl);
16034 if (new_rtl != rtl)
16036 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16037 initialized);
16038 if (mem_loc_result != NULL)
16039 return mem_loc_result;
16042 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16043 get_address_mode (rtl), mode,
16044 VAR_INIT_STATUS_INITIALIZED);
16045 if (mem_loc_result == NULL)
16046 mem_loc_result = tls_mem_loc_descriptor (rtl);
16047 if (mem_loc_result != NULL)
16049 if (!is_a <scalar_int_mode> (mode, &int_mode)
16050 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16052 dw_die_ref type_die;
16053 dw_loc_descr_ref deref;
16054 HOST_WIDE_INT size;
16056 if (dwarf_strict && dwarf_version < 5)
16057 return NULL;
16058 if (!GET_MODE_SIZE (mode).is_constant (&size))
16059 return NULL;
16060 type_die
16061 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16062 if (type_die == NULL)
16063 return NULL;
16064 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16065 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16066 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16067 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16068 add_loc_descr (&mem_loc_result, deref);
16070 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16071 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16072 else
16073 add_loc_descr (&mem_loc_result,
16074 new_loc_descr (DW_OP_deref_size,
16075 GET_MODE_SIZE (int_mode), 0));
16077 break;
16079 case LO_SUM:
16080 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16082 case LABEL_REF:
16083 /* Some ports can transform a symbol ref into a label ref, because
16084 the symbol ref is too far away and has to be dumped into a constant
16085 pool. */
16086 case CONST:
16087 case SYMBOL_REF:
16088 case UNSPEC:
16089 if (!is_a <scalar_int_mode> (mode, &int_mode)
16090 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16091 #ifdef POINTERS_EXTEND_UNSIGNED
16092 && (int_mode != Pmode || mem_mode == VOIDmode)
16093 #endif
16095 break;
16097 if (GET_CODE (rtl) == UNSPEC)
16099 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16100 can't express it in the debug info. This can happen e.g. with some
16101 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16102 approves. */
16103 bool not_ok = false;
16104 subrtx_var_iterator::array_type array;
16105 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16106 if (*iter != rtl && !CONSTANT_P (*iter))
16108 not_ok = true;
16109 break;
16112 if (not_ok)
16113 break;
16115 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16116 if (!const_ok_for_output_1 (*iter))
16118 not_ok = true;
16119 break;
16122 if (not_ok)
16123 break;
16125 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16126 goto symref;
16129 if (GET_CODE (rtl) == SYMBOL_REF
16130 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16132 dw_loc_descr_ref temp;
16134 /* If this is not defined, we have no way to emit the data. */
16135 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16136 break;
16138 temp = new_addr_loc_descr (rtl, dtprel_true);
16140 /* We check for DWARF 5 here because gdb did not implement
16141 DW_OP_form_tls_address until after 7.12. */
16142 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16143 ? DW_OP_form_tls_address
16144 : DW_OP_GNU_push_tls_address),
16145 0, 0);
16146 add_loc_descr (&mem_loc_result, temp);
16148 break;
16151 if (!const_ok_for_output (rtl))
16153 if (GET_CODE (rtl) == CONST)
16154 switch (GET_CODE (XEXP (rtl, 0)))
16156 case NOT:
16157 op = DW_OP_not;
16158 goto try_const_unop;
16159 case NEG:
16160 op = DW_OP_neg;
16161 goto try_const_unop;
16162 try_const_unop:
16163 rtx arg;
16164 arg = XEXP (XEXP (rtl, 0), 0);
16165 if (!CONSTANT_P (arg))
16166 arg = gen_rtx_CONST (int_mode, arg);
16167 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16168 initialized);
16169 if (op0)
16171 mem_loc_result = op0;
16172 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16174 break;
16175 default:
16176 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16177 mem_mode, initialized);
16178 break;
16180 break;
16183 symref:
16184 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16185 vec_safe_push (used_rtx_array, rtl);
16186 break;
16188 case CONCAT:
16189 case CONCATN:
16190 case VAR_LOCATION:
16191 case DEBUG_IMPLICIT_PTR:
16192 expansion_failed (NULL_TREE, rtl,
16193 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16194 return 0;
16196 case ENTRY_VALUE:
16197 if (dwarf_strict && dwarf_version < 5)
16198 return NULL;
16199 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16201 if (!is_a <scalar_int_mode> (mode, &int_mode)
16202 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16203 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16204 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16205 else
16207 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
16208 if (dbx_regnum == IGNORED_DWARF_REGNUM)
16209 return NULL;
16210 op0 = one_reg_loc_descriptor (dbx_regnum,
16211 VAR_INIT_STATUS_INITIALIZED);
16214 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16215 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16217 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16218 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16219 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16220 return NULL;
16222 else
16223 gcc_unreachable ();
16224 if (op0 == NULL)
16225 return NULL;
16226 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16227 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16228 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16229 break;
16231 case DEBUG_PARAMETER_REF:
16232 mem_loc_result = parameter_ref_descriptor (rtl);
16233 break;
16235 case PRE_MODIFY:
16236 /* Extract the PLUS expression nested inside and fall into
16237 PLUS code below. */
16238 rtl = XEXP (rtl, 1);
16239 goto plus;
16241 case PRE_INC:
16242 case PRE_DEC:
16243 /* Turn these into a PLUS expression and fall into the PLUS code
16244 below. */
16245 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16246 gen_int_mode (GET_CODE (rtl) == PRE_INC
16247 ? GET_MODE_UNIT_SIZE (mem_mode)
16248 : -GET_MODE_UNIT_SIZE (mem_mode),
16249 mode));
16251 /* fall through */
16253 case PLUS:
16254 plus:
16255 if (is_based_loc (rtl)
16256 && is_a <scalar_int_mode> (mode, &int_mode)
16257 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16258 || XEXP (rtl, 0) == arg_pointer_rtx
16259 || XEXP (rtl, 0) == frame_pointer_rtx))
16260 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16261 INTVAL (XEXP (rtl, 1)),
16262 VAR_INIT_STATUS_INITIALIZED);
16263 else
16265 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16266 VAR_INIT_STATUS_INITIALIZED);
16267 if (mem_loc_result == 0)
16268 break;
16270 if (CONST_INT_P (XEXP (rtl, 1))
16271 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16272 <= DWARF2_ADDR_SIZE))
16273 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16274 else
16276 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16277 VAR_INIT_STATUS_INITIALIZED);
16278 if (op1 == 0)
16279 return NULL;
16280 add_loc_descr (&mem_loc_result, op1);
16281 add_loc_descr (&mem_loc_result,
16282 new_loc_descr (DW_OP_plus, 0, 0));
16285 break;
16287 /* If a pseudo-reg is optimized away, it is possible for it to
16288 be replaced with a MEM containing a multiply or shift. */
16289 case MINUS:
16290 op = DW_OP_minus;
16291 goto do_binop;
16293 case MULT:
16294 op = DW_OP_mul;
16295 goto do_binop;
16297 case DIV:
16298 if ((!dwarf_strict || dwarf_version >= 5)
16299 && is_a <scalar_int_mode> (mode, &int_mode)
16300 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16302 mem_loc_result = typed_binop (DW_OP_div, rtl,
16303 base_type_for_mode (mode, 0),
16304 int_mode, mem_mode);
16305 break;
16307 op = DW_OP_div;
16308 goto do_binop;
16310 case UMOD:
16311 op = DW_OP_mod;
16312 goto do_binop;
16314 case ASHIFT:
16315 op = DW_OP_shl;
16316 goto do_shift;
16318 case ASHIFTRT:
16319 op = DW_OP_shra;
16320 goto do_shift;
16322 case LSHIFTRT:
16323 op = DW_OP_shr;
16324 goto do_shift;
16326 do_shift:
16327 if (!is_a <scalar_int_mode> (mode, &int_mode))
16328 break;
16329 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16330 VAR_INIT_STATUS_INITIALIZED);
16332 rtx rtlop1 = XEXP (rtl, 1);
16333 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16334 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16335 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16336 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16337 VAR_INIT_STATUS_INITIALIZED);
16340 if (op0 == 0 || op1 == 0)
16341 break;
16343 mem_loc_result = op0;
16344 add_loc_descr (&mem_loc_result, op1);
16345 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16346 break;
16348 case AND:
16349 op = DW_OP_and;
16350 goto do_binop;
16352 case IOR:
16353 op = DW_OP_or;
16354 goto do_binop;
16356 case XOR:
16357 op = DW_OP_xor;
16358 goto do_binop;
16360 do_binop:
16361 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16362 VAR_INIT_STATUS_INITIALIZED);
16363 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16364 VAR_INIT_STATUS_INITIALIZED);
16366 if (op0 == 0 || op1 == 0)
16367 break;
16369 mem_loc_result = op0;
16370 add_loc_descr (&mem_loc_result, op1);
16371 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16372 break;
16374 case MOD:
16375 if ((!dwarf_strict || dwarf_version >= 5)
16376 && is_a <scalar_int_mode> (mode, &int_mode)
16377 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16379 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16380 base_type_for_mode (mode, 0),
16381 int_mode, mem_mode);
16382 break;
16385 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16386 VAR_INIT_STATUS_INITIALIZED);
16387 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16388 VAR_INIT_STATUS_INITIALIZED);
16390 if (op0 == 0 || op1 == 0)
16391 break;
16393 mem_loc_result = op0;
16394 add_loc_descr (&mem_loc_result, op1);
16395 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16396 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16397 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16398 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16399 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16400 break;
16402 case UDIV:
16403 if ((!dwarf_strict || dwarf_version >= 5)
16404 && is_a <scalar_int_mode> (mode, &int_mode))
16406 /* We can use a signed divide if the sign bit is not set. */
16407 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16409 op = DW_OP_div;
16410 goto do_binop;
16413 mem_loc_result = typed_binop (DW_OP_div, rtl,
16414 base_type_for_mode (int_mode, 1),
16415 int_mode, mem_mode);
16417 break;
16419 case NOT:
16420 op = DW_OP_not;
16421 goto do_unop;
16423 case ABS:
16424 op = DW_OP_abs;
16425 goto do_unop;
16427 case NEG:
16428 op = DW_OP_neg;
16429 goto do_unop;
16431 do_unop:
16432 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16433 VAR_INIT_STATUS_INITIALIZED);
16435 if (op0 == 0)
16436 break;
16438 mem_loc_result = op0;
16439 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16440 break;
16442 case CONST_INT:
16443 if (!is_a <scalar_int_mode> (mode, &int_mode)
16444 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16445 #ifdef POINTERS_EXTEND_UNSIGNED
16446 || (int_mode == Pmode
16447 && mem_mode != VOIDmode
16448 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16449 #endif
16452 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16453 break;
16455 if ((!dwarf_strict || dwarf_version >= 5)
16456 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16457 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16459 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16460 scalar_int_mode amode;
16461 if (type_die == NULL)
16462 return NULL;
16463 if (INTVAL (rtl) >= 0
16464 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16465 .exists (&amode))
16466 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16467 /* const DW_OP_convert <XXX> vs.
16468 DW_OP_const_type <XXX, 1, const>. */
16469 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16470 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16472 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16473 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16474 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16475 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16476 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16477 add_loc_descr (&mem_loc_result, op0);
16478 return mem_loc_result;
16480 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16481 INTVAL (rtl));
16482 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16483 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16484 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16485 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16486 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16487 else
16489 mem_loc_result->dw_loc_oprnd2.val_class
16490 = dw_val_class_const_double;
16491 mem_loc_result->dw_loc_oprnd2.v.val_double
16492 = double_int::from_shwi (INTVAL (rtl));
16495 break;
16497 case CONST_DOUBLE:
16498 if (!dwarf_strict || dwarf_version >= 5)
16500 dw_die_ref type_die;
16502 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16503 CONST_DOUBLE rtx could represent either a large integer
16504 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16505 the value is always a floating point constant.
16507 When it is an integer, a CONST_DOUBLE is used whenever
16508 the constant requires 2 HWIs to be adequately represented.
16509 We output CONST_DOUBLEs as blocks. */
16510 if (mode == VOIDmode
16511 || (GET_MODE (rtl) == VOIDmode
16512 && maybe_ne (GET_MODE_BITSIZE (mode),
16513 HOST_BITS_PER_DOUBLE_INT)))
16514 break;
16515 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16516 if (type_die == NULL)
16517 return NULL;
16518 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16519 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16520 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16521 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16522 #if TARGET_SUPPORTS_WIDE_INT == 0
16523 if (!SCALAR_FLOAT_MODE_P (mode))
16525 mem_loc_result->dw_loc_oprnd2.val_class
16526 = dw_val_class_const_double;
16527 mem_loc_result->dw_loc_oprnd2.v.val_double
16528 = rtx_to_double_int (rtl);
16530 else
16531 #endif
16533 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16534 unsigned int length = GET_MODE_SIZE (float_mode);
16535 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16536 unsigned int elt_size = insert_float (rtl, array);
16538 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16539 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16540 = length / elt_size;
16541 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16542 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16545 break;
16547 case CONST_WIDE_INT:
16548 if (!dwarf_strict || dwarf_version >= 5)
16550 dw_die_ref type_die;
16552 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16553 if (type_die == NULL)
16554 return NULL;
16555 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16556 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16557 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16558 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16559 mem_loc_result->dw_loc_oprnd2.val_class
16560 = dw_val_class_wide_int;
16561 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16562 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16564 break;
16566 case CONST_POLY_INT:
16567 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16568 break;
16570 case EQ:
16571 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16572 break;
16574 case GE:
16575 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16576 break;
16578 case GT:
16579 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16580 break;
16582 case LE:
16583 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16584 break;
16586 case LT:
16587 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16588 break;
16590 case NE:
16591 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16592 break;
16594 case GEU:
16595 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16596 break;
16598 case GTU:
16599 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16600 break;
16602 case LEU:
16603 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16604 break;
16606 case LTU:
16607 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16608 break;
16610 case UMIN:
16611 case UMAX:
16612 if (!SCALAR_INT_MODE_P (mode))
16613 break;
16614 /* FALLTHRU */
16615 case SMIN:
16616 case SMAX:
16617 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16618 break;
16620 case ZERO_EXTRACT:
16621 case SIGN_EXTRACT:
16622 if (CONST_INT_P (XEXP (rtl, 1))
16623 && CONST_INT_P (XEXP (rtl, 2))
16624 && is_a <scalar_int_mode> (mode, &int_mode)
16625 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16626 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16627 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16628 && ((unsigned) INTVAL (XEXP (rtl, 1))
16629 + (unsigned) INTVAL (XEXP (rtl, 2))
16630 <= GET_MODE_BITSIZE (int_mode)))
16632 int shift, size;
16633 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16634 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16635 if (op0 == 0)
16636 break;
16637 if (GET_CODE (rtl) == SIGN_EXTRACT)
16638 op = DW_OP_shra;
16639 else
16640 op = DW_OP_shr;
16641 mem_loc_result = op0;
16642 size = INTVAL (XEXP (rtl, 1));
16643 shift = INTVAL (XEXP (rtl, 2));
16644 if (BITS_BIG_ENDIAN)
16645 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16646 if (shift + size != (int) DWARF2_ADDR_SIZE)
16648 add_loc_descr (&mem_loc_result,
16649 int_loc_descriptor (DWARF2_ADDR_SIZE
16650 - shift - size));
16651 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16653 if (size != (int) DWARF2_ADDR_SIZE)
16655 add_loc_descr (&mem_loc_result,
16656 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16657 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16660 break;
16662 case IF_THEN_ELSE:
16664 dw_loc_descr_ref op2, bra_node, drop_node;
16665 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16666 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16667 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16668 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16669 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16670 VAR_INIT_STATUS_INITIALIZED);
16671 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16672 VAR_INIT_STATUS_INITIALIZED);
16673 if (op0 == NULL || op1 == NULL || op2 == NULL)
16674 break;
16676 mem_loc_result = op1;
16677 add_loc_descr (&mem_loc_result, op2);
16678 add_loc_descr (&mem_loc_result, op0);
16679 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16680 add_loc_descr (&mem_loc_result, bra_node);
16681 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16682 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16683 add_loc_descr (&mem_loc_result, drop_node);
16684 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16685 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16687 break;
16689 case FLOAT_EXTEND:
16690 case FLOAT_TRUNCATE:
16691 case FLOAT:
16692 case UNSIGNED_FLOAT:
16693 case FIX:
16694 case UNSIGNED_FIX:
16695 if (!dwarf_strict || dwarf_version >= 5)
16697 dw_die_ref type_die;
16698 dw_loc_descr_ref cvt;
16700 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16701 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16702 if (op0 == NULL)
16703 break;
16704 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16705 && (GET_CODE (rtl) == FLOAT
16706 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16708 type_die = base_type_for_mode (int_mode,
16709 GET_CODE (rtl) == UNSIGNED_FLOAT);
16710 if (type_die == NULL)
16711 break;
16712 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16713 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16714 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16715 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16716 add_loc_descr (&op0, cvt);
16718 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16719 if (type_die == NULL)
16720 break;
16721 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16722 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16723 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16724 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16725 add_loc_descr (&op0, cvt);
16726 if (is_a <scalar_int_mode> (mode, &int_mode)
16727 && (GET_CODE (rtl) == FIX
16728 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16730 op0 = convert_descriptor_to_mode (int_mode, op0);
16731 if (op0 == NULL)
16732 break;
16734 mem_loc_result = op0;
16736 break;
16738 case CLZ:
16739 case CTZ:
16740 case FFS:
16741 if (is_a <scalar_int_mode> (mode, &int_mode))
16742 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16743 break;
16745 case POPCOUNT:
16746 case PARITY:
16747 if (is_a <scalar_int_mode> (mode, &int_mode))
16748 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16749 break;
16751 case BSWAP:
16752 if (is_a <scalar_int_mode> (mode, &int_mode))
16753 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16754 break;
16756 case ROTATE:
16757 case ROTATERT:
16758 if (is_a <scalar_int_mode> (mode, &int_mode))
16759 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16760 break;
16762 case COMPARE:
16763 /* In theory, we could implement the above. */
16764 /* DWARF cannot represent the unsigned compare operations
16765 natively. */
16766 case SS_MULT:
16767 case US_MULT:
16768 case SS_DIV:
16769 case US_DIV:
16770 case SS_PLUS:
16771 case US_PLUS:
16772 case SS_MINUS:
16773 case US_MINUS:
16774 case SS_NEG:
16775 case US_NEG:
16776 case SS_ABS:
16777 case SS_ASHIFT:
16778 case US_ASHIFT:
16779 case SS_TRUNCATE:
16780 case US_TRUNCATE:
16781 case UNORDERED:
16782 case ORDERED:
16783 case UNEQ:
16784 case UNGE:
16785 case UNGT:
16786 case UNLE:
16787 case UNLT:
16788 case LTGT:
16789 case FRACT_CONVERT:
16790 case UNSIGNED_FRACT_CONVERT:
16791 case SAT_FRACT:
16792 case UNSIGNED_SAT_FRACT:
16793 case SQRT:
16794 case ASM_OPERANDS:
16795 case VEC_MERGE:
16796 case VEC_SELECT:
16797 case VEC_CONCAT:
16798 case VEC_DUPLICATE:
16799 case VEC_SERIES:
16800 case HIGH:
16801 case FMA:
16802 case STRICT_LOW_PART:
16803 case CONST_VECTOR:
16804 case CONST_FIXED:
16805 case CLRSB:
16806 case CLOBBER:
16807 break;
16809 case CONST_STRING:
16810 resolve_one_addr (&rtl);
16811 goto symref;
16813 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16814 the expression. An UNSPEC rtx represents a raw DWARF operation,
16815 new_loc_descr is called for it to build the operation directly.
16816 Otherwise mem_loc_descriptor is called recursively. */
16817 case PARALLEL:
16819 int index = 0;
16820 dw_loc_descr_ref exp_result = NULL;
16822 for (; index < XVECLEN (rtl, 0); index++)
16824 rtx elem = XVECEXP (rtl, 0, index);
16825 if (GET_CODE (elem) == UNSPEC)
16827 /* Each DWARF operation UNSPEC contain two operands, if
16828 one operand is not used for the operation, const0_rtx is
16829 passed. */
16830 gcc_assert (XVECLEN (elem, 0) == 2);
16832 HOST_WIDE_INT dw_op = XINT (elem, 1);
16833 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16834 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16835 exp_result
16836 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16837 oprnd2);
16839 else
16840 exp_result
16841 = mem_loc_descriptor (elem, mode, mem_mode,
16842 VAR_INIT_STATUS_INITIALIZED);
16844 if (!mem_loc_result)
16845 mem_loc_result = exp_result;
16846 else
16847 add_loc_descr (&mem_loc_result, exp_result);
16850 break;
16853 default:
16854 if (flag_checking)
16856 print_rtl (stderr, rtl);
16857 gcc_unreachable ();
16859 break;
16862 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16863 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16865 return mem_loc_result;
16868 /* Return a descriptor that describes the concatenation of two locations.
16869 This is typically a complex variable. */
16871 static dw_loc_descr_ref
16872 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16874 /* At present we only track constant-sized pieces. */
16875 unsigned int size0, size1;
16876 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16877 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16878 return 0;
16880 dw_loc_descr_ref cc_loc_result = NULL;
16881 dw_loc_descr_ref x0_ref
16882 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16883 dw_loc_descr_ref x1_ref
16884 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16886 if (x0_ref == 0 || x1_ref == 0)
16887 return 0;
16889 cc_loc_result = x0_ref;
16890 add_loc_descr_op_piece (&cc_loc_result, size0);
16892 add_loc_descr (&cc_loc_result, x1_ref);
16893 add_loc_descr_op_piece (&cc_loc_result, size1);
16895 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16896 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16898 return cc_loc_result;
16901 /* Return a descriptor that describes the concatenation of N
16902 locations. */
16904 static dw_loc_descr_ref
16905 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16907 unsigned int i;
16908 dw_loc_descr_ref cc_loc_result = NULL;
16909 unsigned int n = XVECLEN (concatn, 0);
16910 unsigned int size;
16912 for (i = 0; i < n; ++i)
16914 dw_loc_descr_ref ref;
16915 rtx x = XVECEXP (concatn, 0, i);
16917 /* At present we only track constant-sized pieces. */
16918 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16919 return NULL;
16921 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16922 if (ref == NULL)
16923 return NULL;
16925 add_loc_descr (&cc_loc_result, ref);
16926 add_loc_descr_op_piece (&cc_loc_result, size);
16929 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16930 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16932 return cc_loc_result;
16935 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16936 for DEBUG_IMPLICIT_PTR RTL. */
16938 static dw_loc_descr_ref
16939 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16941 dw_loc_descr_ref ret;
16942 dw_die_ref ref;
16944 if (dwarf_strict && dwarf_version < 5)
16945 return NULL;
16946 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16947 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16948 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16949 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16950 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16951 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16952 if (ref)
16954 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16955 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16956 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16958 else
16960 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16961 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16963 return ret;
16966 /* Output a proper Dwarf location descriptor for a variable or parameter
16967 which is either allocated in a register or in a memory location. For a
16968 register, we just generate an OP_REG and the register number. For a
16969 memory location we provide a Dwarf postfix expression describing how to
16970 generate the (dynamic) address of the object onto the address stack.
16972 MODE is mode of the decl if this loc_descriptor is going to be used in
16973 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16974 allowed, VOIDmode otherwise.
16976 If we don't know how to describe it, return 0. */
16978 static dw_loc_descr_ref
16979 loc_descriptor (rtx rtl, machine_mode mode,
16980 enum var_init_status initialized)
16982 dw_loc_descr_ref loc_result = NULL;
16983 scalar_int_mode int_mode;
16985 switch (GET_CODE (rtl))
16987 case SUBREG:
16988 /* The case of a subreg may arise when we have a local (register)
16989 variable or a formal (register) parameter which doesn't quite fill
16990 up an entire register. For now, just assume that it is
16991 legitimate to make the Dwarf info refer to the whole register which
16992 contains the given subreg. */
16993 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16994 loc_result = loc_descriptor (SUBREG_REG (rtl),
16995 GET_MODE (SUBREG_REG (rtl)), initialized);
16996 else
16997 goto do_default;
16998 break;
17000 case REG:
17001 loc_result = reg_loc_descriptor (rtl, initialized);
17002 break;
17004 case MEM:
17005 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17006 GET_MODE (rtl), initialized);
17007 if (loc_result == NULL)
17008 loc_result = tls_mem_loc_descriptor (rtl);
17009 if (loc_result == NULL)
17011 rtx new_rtl = avoid_constant_pool_reference (rtl);
17012 if (new_rtl != rtl)
17013 loc_result = loc_descriptor (new_rtl, mode, initialized);
17015 break;
17017 case CONCAT:
17018 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17019 initialized);
17020 break;
17022 case CONCATN:
17023 loc_result = concatn_loc_descriptor (rtl, initialized);
17024 break;
17026 case VAR_LOCATION:
17027 /* Single part. */
17028 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17030 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17031 if (GET_CODE (loc) == EXPR_LIST)
17032 loc = XEXP (loc, 0);
17033 loc_result = loc_descriptor (loc, mode, initialized);
17034 break;
17037 rtl = XEXP (rtl, 1);
17038 /* FALLTHRU */
17040 case PARALLEL:
17042 rtvec par_elems = XVEC (rtl, 0);
17043 int num_elem = GET_NUM_ELEM (par_elems);
17044 machine_mode mode;
17045 int i, size;
17047 /* Create the first one, so we have something to add to. */
17048 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17049 VOIDmode, initialized);
17050 if (loc_result == NULL)
17051 return NULL;
17052 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17053 /* At present we only track constant-sized pieces. */
17054 if (!GET_MODE_SIZE (mode).is_constant (&size))
17055 return NULL;
17056 add_loc_descr_op_piece (&loc_result, size);
17057 for (i = 1; i < num_elem; i++)
17059 dw_loc_descr_ref temp;
17061 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17062 VOIDmode, initialized);
17063 if (temp == NULL)
17064 return NULL;
17065 add_loc_descr (&loc_result, temp);
17066 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17067 /* At present we only track constant-sized pieces. */
17068 if (!GET_MODE_SIZE (mode).is_constant (&size))
17069 return NULL;
17070 add_loc_descr_op_piece (&loc_result, size);
17073 break;
17075 case CONST_INT:
17076 if (mode != VOIDmode && mode != BLKmode)
17078 int_mode = as_a <scalar_int_mode> (mode);
17079 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17080 INTVAL (rtl));
17082 break;
17084 case CONST_DOUBLE:
17085 if (mode == VOIDmode)
17086 mode = GET_MODE (rtl);
17088 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17090 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17092 /* Note that a CONST_DOUBLE rtx could represent either an integer
17093 or a floating-point constant. A CONST_DOUBLE is used whenever
17094 the constant requires more than one word in order to be
17095 adequately represented. We output CONST_DOUBLEs as blocks. */
17096 scalar_mode smode = as_a <scalar_mode> (mode);
17097 loc_result = new_loc_descr (DW_OP_implicit_value,
17098 GET_MODE_SIZE (smode), 0);
17099 #if TARGET_SUPPORTS_WIDE_INT == 0
17100 if (!SCALAR_FLOAT_MODE_P (smode))
17102 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17103 loc_result->dw_loc_oprnd2.v.val_double
17104 = rtx_to_double_int (rtl);
17106 else
17107 #endif
17109 unsigned int length = GET_MODE_SIZE (smode);
17110 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17111 unsigned int elt_size = insert_float (rtl, array);
17113 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17114 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17115 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17116 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17119 break;
17121 case CONST_WIDE_INT:
17122 if (mode == VOIDmode)
17123 mode = GET_MODE (rtl);
17125 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17127 int_mode = as_a <scalar_int_mode> (mode);
17128 loc_result = new_loc_descr (DW_OP_implicit_value,
17129 GET_MODE_SIZE (int_mode), 0);
17130 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17131 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17132 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17134 break;
17136 case CONST_VECTOR:
17137 if (mode == VOIDmode)
17138 mode = GET_MODE (rtl);
17140 if (mode != VOIDmode
17141 /* The combination of a length and byte elt_size doesn't extend
17142 naturally to boolean vectors, where several elements are packed
17143 into the same byte. */
17144 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17145 && (dwarf_version >= 4 || !dwarf_strict))
17147 unsigned int length;
17148 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17149 return NULL;
17151 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17152 unsigned char *array
17153 = ggc_vec_alloc<unsigned char> (length * elt_size);
17154 unsigned int i;
17155 unsigned char *p;
17156 machine_mode imode = GET_MODE_INNER (mode);
17158 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17159 switch (GET_MODE_CLASS (mode))
17161 case MODE_VECTOR_INT:
17162 for (i = 0, p = array; i < length; i++, p += elt_size)
17164 rtx elt = CONST_VECTOR_ELT (rtl, i);
17165 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17167 break;
17169 case MODE_VECTOR_FLOAT:
17170 for (i = 0, p = array; i < length; i++, p += elt_size)
17172 rtx elt = CONST_VECTOR_ELT (rtl, i);
17173 insert_float (elt, p);
17175 break;
17177 default:
17178 gcc_unreachable ();
17181 loc_result = new_loc_descr (DW_OP_implicit_value,
17182 length * elt_size, 0);
17183 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17184 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17185 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17186 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17188 break;
17190 case CONST:
17191 if (mode == VOIDmode
17192 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17193 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17194 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17196 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17197 break;
17199 /* FALLTHROUGH */
17200 case SYMBOL_REF:
17201 if (!const_ok_for_output (rtl))
17202 break;
17203 /* FALLTHROUGH */
17204 case LABEL_REF:
17205 if (is_a <scalar_int_mode> (mode, &int_mode)
17206 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17207 && (dwarf_version >= 4 || !dwarf_strict))
17209 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17210 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17211 vec_safe_push (used_rtx_array, rtl);
17213 break;
17215 case DEBUG_IMPLICIT_PTR:
17216 loc_result = implicit_ptr_descriptor (rtl, 0);
17217 break;
17219 case PLUS:
17220 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17221 && CONST_INT_P (XEXP (rtl, 1)))
17223 loc_result
17224 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17225 break;
17227 /* FALLTHRU */
17228 do_default:
17229 default:
17230 if ((is_a <scalar_int_mode> (mode, &int_mode)
17231 && GET_MODE (rtl) == int_mode
17232 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17233 && dwarf_version >= 4)
17234 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17236 /* Value expression. */
17237 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17238 if (loc_result)
17239 add_loc_descr (&loc_result,
17240 new_loc_descr (DW_OP_stack_value, 0, 0));
17242 break;
17245 return loc_result;
17248 /* We need to figure out what section we should use as the base for the
17249 address ranges where a given location is valid.
17250 1. If this particular DECL has a section associated with it, use that.
17251 2. If this function has a section associated with it, use that.
17252 3. Otherwise, use the text section.
17253 XXX: If you split a variable across multiple sections, we won't notice. */
17255 static const char *
17256 secname_for_decl (const_tree decl)
17258 const char *secname;
17260 if (VAR_OR_FUNCTION_DECL_P (decl)
17261 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17262 && DECL_SECTION_NAME (decl))
17263 secname = DECL_SECTION_NAME (decl);
17264 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17266 if (in_cold_section_p)
17268 section *sec = current_function_section ();
17269 if (sec->common.flags & SECTION_NAMED)
17270 return sec->named.name;
17272 secname = DECL_SECTION_NAME (current_function_decl);
17274 else if (cfun && in_cold_section_p)
17275 secname = crtl->subsections.cold_section_label;
17276 else
17277 secname = text_section_label;
17279 return secname;
17282 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17284 static bool
17285 decl_by_reference_p (tree decl)
17287 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17288 || VAR_P (decl))
17289 && DECL_BY_REFERENCE (decl));
17292 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17293 for VARLOC. */
17295 static dw_loc_descr_ref
17296 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17297 enum var_init_status initialized)
17299 int have_address = 0;
17300 dw_loc_descr_ref descr;
17301 machine_mode mode;
17303 if (want_address != 2)
17305 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17306 /* Single part. */
17307 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17309 varloc = PAT_VAR_LOCATION_LOC (varloc);
17310 if (GET_CODE (varloc) == EXPR_LIST)
17311 varloc = XEXP (varloc, 0);
17312 mode = GET_MODE (varloc);
17313 if (MEM_P (varloc))
17315 rtx addr = XEXP (varloc, 0);
17316 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17317 mode, initialized);
17318 if (descr)
17319 have_address = 1;
17320 else
17322 rtx x = avoid_constant_pool_reference (varloc);
17323 if (x != varloc)
17324 descr = mem_loc_descriptor (x, mode, VOIDmode,
17325 initialized);
17328 else
17329 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17331 else
17332 return 0;
17334 else
17336 if (GET_CODE (varloc) == VAR_LOCATION)
17337 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17338 else
17339 mode = DECL_MODE (loc);
17340 descr = loc_descriptor (varloc, mode, initialized);
17341 have_address = 1;
17344 if (!descr)
17345 return 0;
17347 if (want_address == 2 && !have_address
17348 && (dwarf_version >= 4 || !dwarf_strict))
17350 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17352 expansion_failed (loc, NULL_RTX,
17353 "DWARF address size mismatch");
17354 return 0;
17356 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17357 have_address = 1;
17359 /* Show if we can't fill the request for an address. */
17360 if (want_address && !have_address)
17362 expansion_failed (loc, NULL_RTX,
17363 "Want address and only have value");
17364 return 0;
17367 /* If we've got an address and don't want one, dereference. */
17368 if (!want_address && have_address)
17370 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17371 enum dwarf_location_atom op;
17373 if (size > DWARF2_ADDR_SIZE || size == -1)
17375 expansion_failed (loc, NULL_RTX,
17376 "DWARF address size mismatch");
17377 return 0;
17379 else if (size == DWARF2_ADDR_SIZE)
17380 op = DW_OP_deref;
17381 else
17382 op = DW_OP_deref_size;
17384 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17387 return descr;
17390 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17391 if it is not possible. */
17393 static dw_loc_descr_ref
17394 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17396 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17397 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17398 else if (dwarf_version >= 3 || !dwarf_strict)
17399 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17400 else
17401 return NULL;
17404 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17405 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17407 static dw_loc_descr_ref
17408 dw_sra_loc_expr (tree decl, rtx loc)
17410 rtx p;
17411 unsigned HOST_WIDE_INT padsize = 0;
17412 dw_loc_descr_ref descr, *descr_tail;
17413 unsigned HOST_WIDE_INT decl_size;
17414 rtx varloc;
17415 enum var_init_status initialized;
17417 if (DECL_SIZE (decl) == NULL
17418 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17419 return NULL;
17421 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17422 descr = NULL;
17423 descr_tail = &descr;
17425 for (p = loc; p; p = XEXP (p, 1))
17427 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17428 rtx loc_note = *decl_piece_varloc_ptr (p);
17429 dw_loc_descr_ref cur_descr;
17430 dw_loc_descr_ref *tail, last = NULL;
17431 unsigned HOST_WIDE_INT opsize = 0;
17433 if (loc_note == NULL_RTX
17434 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17436 padsize += bitsize;
17437 continue;
17439 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17440 varloc = NOTE_VAR_LOCATION (loc_note);
17441 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17442 if (cur_descr == NULL)
17444 padsize += bitsize;
17445 continue;
17448 /* Check that cur_descr either doesn't use
17449 DW_OP_*piece operations, or their sum is equal
17450 to bitsize. Otherwise we can't embed it. */
17451 for (tail = &cur_descr; *tail != NULL;
17452 tail = &(*tail)->dw_loc_next)
17453 if ((*tail)->dw_loc_opc == DW_OP_piece)
17455 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17456 * BITS_PER_UNIT;
17457 last = *tail;
17459 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17461 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17462 last = *tail;
17465 if (last != NULL && opsize != bitsize)
17467 padsize += bitsize;
17468 /* Discard the current piece of the descriptor and release any
17469 addr_table entries it uses. */
17470 remove_loc_list_addr_table_entries (cur_descr);
17471 continue;
17474 /* If there is a hole, add DW_OP_*piece after empty DWARF
17475 expression, which means that those bits are optimized out. */
17476 if (padsize)
17478 if (padsize > decl_size)
17480 remove_loc_list_addr_table_entries (cur_descr);
17481 goto discard_descr;
17483 decl_size -= padsize;
17484 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17485 if (*descr_tail == NULL)
17487 remove_loc_list_addr_table_entries (cur_descr);
17488 goto discard_descr;
17490 descr_tail = &(*descr_tail)->dw_loc_next;
17491 padsize = 0;
17493 *descr_tail = cur_descr;
17494 descr_tail = tail;
17495 if (bitsize > decl_size)
17496 goto discard_descr;
17497 decl_size -= bitsize;
17498 if (last == NULL)
17500 HOST_WIDE_INT offset = 0;
17501 if (GET_CODE (varloc) == VAR_LOCATION
17502 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17504 varloc = PAT_VAR_LOCATION_LOC (varloc);
17505 if (GET_CODE (varloc) == EXPR_LIST)
17506 varloc = XEXP (varloc, 0);
17510 if (GET_CODE (varloc) == CONST
17511 || GET_CODE (varloc) == SIGN_EXTEND
17512 || GET_CODE (varloc) == ZERO_EXTEND)
17513 varloc = XEXP (varloc, 0);
17514 else if (GET_CODE (varloc) == SUBREG)
17515 varloc = SUBREG_REG (varloc);
17516 else
17517 break;
17519 while (1);
17520 /* DW_OP_bit_size offset should be zero for register
17521 or implicit location descriptions and empty location
17522 descriptions, but for memory addresses needs big endian
17523 adjustment. */
17524 if (MEM_P (varloc))
17526 unsigned HOST_WIDE_INT memsize;
17527 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17528 goto discard_descr;
17529 memsize *= BITS_PER_UNIT;
17530 if (memsize != bitsize)
17532 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17533 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17534 goto discard_descr;
17535 if (memsize < bitsize)
17536 goto discard_descr;
17537 if (BITS_BIG_ENDIAN)
17538 offset = memsize - bitsize;
17542 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17543 if (*descr_tail == NULL)
17544 goto discard_descr;
17545 descr_tail = &(*descr_tail)->dw_loc_next;
17549 /* If there were any non-empty expressions, add padding till the end of
17550 the decl. */
17551 if (descr != NULL && decl_size != 0)
17553 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17554 if (*descr_tail == NULL)
17555 goto discard_descr;
17557 return descr;
17559 discard_descr:
17560 /* Discard the descriptor and release any addr_table entries it uses. */
17561 remove_loc_list_addr_table_entries (descr);
17562 return NULL;
17565 /* Return the dwarf representation of the location list LOC_LIST of
17566 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17567 function. */
17569 static dw_loc_list_ref
17570 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17572 const char *endname, *secname;
17573 var_loc_view endview;
17574 rtx varloc;
17575 enum var_init_status initialized;
17576 struct var_loc_node *node;
17577 dw_loc_descr_ref descr;
17578 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17579 dw_loc_list_ref list = NULL;
17580 dw_loc_list_ref *listp = &list;
17582 /* Now that we know what section we are using for a base,
17583 actually construct the list of locations.
17584 The first location information is what is passed to the
17585 function that creates the location list, and the remaining
17586 locations just get added on to that list.
17587 Note that we only know the start address for a location
17588 (IE location changes), so to build the range, we use
17589 the range [current location start, next location start].
17590 This means we have to special case the last node, and generate
17591 a range of [last location start, end of function label]. */
17593 if (cfun && crtl->has_bb_partition)
17595 bool save_in_cold_section_p = in_cold_section_p;
17596 in_cold_section_p = first_function_block_is_cold;
17597 if (loc_list->last_before_switch == NULL)
17598 in_cold_section_p = !in_cold_section_p;
17599 secname = secname_for_decl (decl);
17600 in_cold_section_p = save_in_cold_section_p;
17602 else
17603 secname = secname_for_decl (decl);
17605 for (node = loc_list->first; node; node = node->next)
17607 bool range_across_switch = false;
17608 if (GET_CODE (node->loc) == EXPR_LIST
17609 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17611 if (GET_CODE (node->loc) == EXPR_LIST)
17613 descr = NULL;
17614 /* This requires DW_OP_{,bit_}piece, which is not usable
17615 inside DWARF expressions. */
17616 if (want_address == 2)
17617 descr = dw_sra_loc_expr (decl, node->loc);
17619 else
17621 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17622 varloc = NOTE_VAR_LOCATION (node->loc);
17623 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17625 if (descr)
17627 /* If section switch happens in between node->label
17628 and node->next->label (or end of function) and
17629 we can't emit it as a single entry list,
17630 emit two ranges, first one ending at the end
17631 of first partition and second one starting at the
17632 beginning of second partition. */
17633 if (node == loc_list->last_before_switch
17634 && (node != loc_list->first || loc_list->first->next
17635 /* If we are to emit a view number, we will emit
17636 a loclist rather than a single location
17637 expression for the entire function (see
17638 loc_list_has_views), so we have to split the
17639 range that straddles across partitions. */
17640 || !ZERO_VIEW_P (node->view))
17641 && current_function_decl)
17643 endname = cfun->fde->dw_fde_end;
17644 endview = 0;
17645 range_across_switch = true;
17647 /* The variable has a location between NODE->LABEL and
17648 NODE->NEXT->LABEL. */
17649 else if (node->next)
17650 endname = node->next->label, endview = node->next->view;
17651 /* If the variable has a location at the last label
17652 it keeps its location until the end of function. */
17653 else if (!current_function_decl)
17654 endname = text_end_label, endview = 0;
17655 else
17657 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17658 current_function_funcdef_no);
17659 endname = ggc_strdup (label_id);
17660 endview = 0;
17663 *listp = new_loc_list (descr, node->label, node->view,
17664 endname, endview, secname);
17665 if (TREE_CODE (decl) == PARM_DECL
17666 && node == loc_list->first
17667 && NOTE_P (node->loc)
17668 && strcmp (node->label, endname) == 0)
17669 (*listp)->force = true;
17670 listp = &(*listp)->dw_loc_next;
17674 if (cfun
17675 && crtl->has_bb_partition
17676 && node == loc_list->last_before_switch)
17678 bool save_in_cold_section_p = in_cold_section_p;
17679 in_cold_section_p = !first_function_block_is_cold;
17680 secname = secname_for_decl (decl);
17681 in_cold_section_p = save_in_cold_section_p;
17684 if (range_across_switch)
17686 if (GET_CODE (node->loc) == EXPR_LIST)
17687 descr = dw_sra_loc_expr (decl, node->loc);
17688 else
17690 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17691 varloc = NOTE_VAR_LOCATION (node->loc);
17692 descr = dw_loc_list_1 (decl, varloc, want_address,
17693 initialized);
17695 gcc_assert (descr);
17696 /* The variable has a location between NODE->LABEL and
17697 NODE->NEXT->LABEL. */
17698 if (node->next)
17699 endname = node->next->label, endview = node->next->view;
17700 else
17701 endname = cfun->fde->dw_fde_second_end, endview = 0;
17702 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17703 endname, endview, secname);
17704 listp = &(*listp)->dw_loc_next;
17708 /* Try to avoid the overhead of a location list emitting a location
17709 expression instead, but only if we didn't have more than one
17710 location entry in the first place. If some entries were not
17711 representable, we don't want to pretend a single entry that was
17712 applies to the entire scope in which the variable is
17713 available. */
17714 if (list && loc_list->first->next)
17715 gen_llsym (list);
17716 else
17717 maybe_gen_llsym (list);
17719 return list;
17722 /* Return if the loc_list has only single element and thus can be represented
17723 as location description. */
17725 static bool
17726 single_element_loc_list_p (dw_loc_list_ref list)
17728 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17729 return !list->ll_symbol;
17732 /* Duplicate a single element of location list. */
17734 static inline dw_loc_descr_ref
17735 copy_loc_descr (dw_loc_descr_ref ref)
17737 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17738 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17739 return copy;
17742 /* To each location in list LIST append loc descr REF. */
17744 static void
17745 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17747 dw_loc_descr_ref copy;
17748 add_loc_descr (&list->expr, ref);
17749 list = list->dw_loc_next;
17750 while (list)
17752 copy = copy_loc_descr (ref);
17753 add_loc_descr (&list->expr, copy);
17754 while (copy->dw_loc_next)
17755 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17756 list = list->dw_loc_next;
17760 /* To each location in list LIST prepend loc descr REF. */
17762 static void
17763 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17765 dw_loc_descr_ref copy;
17766 dw_loc_descr_ref ref_end = list->expr;
17767 add_loc_descr (&ref, list->expr);
17768 list->expr = ref;
17769 list = list->dw_loc_next;
17770 while (list)
17772 dw_loc_descr_ref end = list->expr;
17773 list->expr = copy = copy_loc_descr (ref);
17774 while (copy->dw_loc_next != ref_end)
17775 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17776 copy->dw_loc_next = end;
17777 list = list->dw_loc_next;
17781 /* Given two lists RET and LIST
17782 produce location list that is result of adding expression in LIST
17783 to expression in RET on each position in program.
17784 Might be destructive on both RET and LIST.
17786 TODO: We handle only simple cases of RET or LIST having at most one
17787 element. General case would involve sorting the lists in program order
17788 and merging them that will need some additional work.
17789 Adding that will improve quality of debug info especially for SRA-ed
17790 structures. */
17792 static void
17793 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17795 if (!list)
17796 return;
17797 if (!*ret)
17799 *ret = list;
17800 return;
17802 if (!list->dw_loc_next)
17804 add_loc_descr_to_each (*ret, list->expr);
17805 return;
17807 if (!(*ret)->dw_loc_next)
17809 prepend_loc_descr_to_each (list, (*ret)->expr);
17810 *ret = list;
17811 return;
17813 expansion_failed (NULL_TREE, NULL_RTX,
17814 "Don't know how to merge two non-trivial"
17815 " location lists.\n");
17816 *ret = NULL;
17817 return;
17820 /* LOC is constant expression. Try a luck, look it up in constant
17821 pool and return its loc_descr of its address. */
17823 static dw_loc_descr_ref
17824 cst_pool_loc_descr (tree loc)
17826 /* Get an RTL for this, if something has been emitted. */
17827 rtx rtl = lookup_constant_def (loc);
17829 if (!rtl || !MEM_P (rtl))
17831 gcc_assert (!rtl);
17832 return 0;
17834 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17836 /* TODO: We might get more coverage if we was actually delaying expansion
17837 of all expressions till end of compilation when constant pools are fully
17838 populated. */
17839 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17841 expansion_failed (loc, NULL_RTX,
17842 "CST value in contant pool but not marked.");
17843 return 0;
17845 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17846 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17849 /* Return dw_loc_list representing address of addr_expr LOC
17850 by looking for inner INDIRECT_REF expression and turning
17851 it into simple arithmetics.
17853 See loc_list_from_tree for the meaning of CONTEXT. */
17855 static dw_loc_list_ref
17856 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17857 loc_descr_context *context)
17859 tree obj, offset;
17860 poly_int64 bitsize, bitpos, bytepos;
17861 machine_mode mode;
17862 int unsignedp, reversep, volatilep = 0;
17863 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17865 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17866 &bitsize, &bitpos, &offset, &mode,
17867 &unsignedp, &reversep, &volatilep);
17868 STRIP_NOPS (obj);
17869 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17871 expansion_failed (loc, NULL_RTX, "bitfield access");
17872 return 0;
17874 if (!INDIRECT_REF_P (obj))
17876 expansion_failed (obj,
17877 NULL_RTX, "no indirect ref in inner refrence");
17878 return 0;
17880 if (!offset && known_eq (bitpos, 0))
17881 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17882 context);
17883 else if (toplev
17884 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17885 && (dwarf_version >= 4 || !dwarf_strict))
17887 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17888 if (!list_ret)
17889 return 0;
17890 if (offset)
17892 /* Variable offset. */
17893 list_ret1 = loc_list_from_tree (offset, 0, context);
17894 if (list_ret1 == 0)
17895 return 0;
17896 add_loc_list (&list_ret, list_ret1);
17897 if (!list_ret)
17898 return 0;
17899 add_loc_descr_to_each (list_ret,
17900 new_loc_descr (DW_OP_plus, 0, 0));
17902 HOST_WIDE_INT value;
17903 if (bytepos.is_constant (&value) && value > 0)
17904 add_loc_descr_to_each (list_ret,
17905 new_loc_descr (DW_OP_plus_uconst, value, 0));
17906 else if (maybe_ne (bytepos, 0))
17907 loc_list_plus_const (list_ret, bytepos);
17908 add_loc_descr_to_each (list_ret,
17909 new_loc_descr (DW_OP_stack_value, 0, 0));
17911 return list_ret;
17914 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17915 all operations from LOC are nops, move to the last one. Insert in NOPS all
17916 operations that are skipped. */
17918 static void
17919 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17920 hash_set<dw_loc_descr_ref> &nops)
17922 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17924 nops.add (loc);
17925 loc = loc->dw_loc_next;
17929 /* Helper for loc_descr_without_nops: free the location description operation
17930 P. */
17932 bool
17933 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17935 ggc_free (loc);
17936 return true;
17939 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17940 finishes LOC. */
17942 static void
17943 loc_descr_without_nops (dw_loc_descr_ref &loc)
17945 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17946 return;
17948 /* Set of all DW_OP_nop operations we remove. */
17949 hash_set<dw_loc_descr_ref> nops;
17951 /* First, strip all prefix NOP operations in order to keep the head of the
17952 operations list. */
17953 loc_descr_to_next_no_nop (loc, nops);
17955 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17957 /* For control flow operations: strip "prefix" nops in destination
17958 labels. */
17959 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17960 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17961 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17962 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17964 /* Do the same for the operations that follow, then move to the next
17965 iteration. */
17966 if (cur->dw_loc_next != NULL)
17967 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17968 cur = cur->dw_loc_next;
17971 nops.traverse<void *, free_loc_descr> (NULL);
17975 struct dwarf_procedure_info;
17977 /* Helper structure for location descriptions generation. */
17978 struct loc_descr_context
17980 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17981 NULL_TREE if DW_OP_push_object_address in invalid for this location
17982 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17983 tree context_type;
17984 /* The ..._DECL node that should be translated as a
17985 DW_OP_push_object_address operation. */
17986 tree base_decl;
17987 /* Information about the DWARF procedure we are currently generating. NULL if
17988 we are not generating a DWARF procedure. */
17989 struct dwarf_procedure_info *dpi;
17990 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17991 by consumer. Used for DW_TAG_generic_subrange attributes. */
17992 bool placeholder_arg;
17993 /* True if PLACEHOLDER_EXPR has been seen. */
17994 bool placeholder_seen;
17995 /* True if strict preservation of signedness has been requested. */
17996 bool strict_signedness;
17999 /* DWARF procedures generation
18001 DWARF expressions (aka. location descriptions) are used to encode variable
18002 things such as sizes or offsets. Such computations can have redundant parts
18003 that can be factorized in order to reduce the size of the output debug
18004 information. This is the whole point of DWARF procedures.
18006 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
18007 already factorized into functions ("size functions") in order to handle very
18008 big and complex types. Such functions are quite simple: they have integral
18009 arguments, they return an integral result and their body contains only a
18010 return statement with arithmetic expressions. This is the only kind of
18011 function we are interested in translating into DWARF procedures, here.
18013 DWARF expressions and DWARF procedure are executed using a stack, so we have
18014 to define some calling convention for them to interact. Let's say that:
18016 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18017 all arguments in reverse order (right-to-left) so that when the DWARF
18018 procedure execution starts, the first argument is the top of the stack.
18020 - Then, when returning, the DWARF procedure must have consumed all arguments
18021 on the stack, must have pushed the result and touched nothing else.
18023 - Each integral argument and the result are integral types can be hold in a
18024 single stack slot.
18026 - We call "frame offset" the number of stack slots that are "under DWARF
18027 procedure control": it includes the arguments slots, the temporaries and
18028 the result slot. Thus, it is equal to the number of arguments when the
18029 procedure execution starts and must be equal to one (the result) when it
18030 returns. */
18032 /* Helper structure used when generating operations for a DWARF procedure. */
18033 struct dwarf_procedure_info
18035 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18036 currently translated. */
18037 tree fndecl;
18038 /* The number of arguments FNDECL takes. */
18039 unsigned args_count;
18042 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18043 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18044 equate it to this DIE. */
18046 static dw_die_ref
18047 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18048 dw_die_ref parent_die)
18050 dw_die_ref dwarf_proc_die;
18052 if ((dwarf_version < 3 && dwarf_strict)
18053 || location == NULL)
18054 return NULL;
18056 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18057 if (fndecl)
18058 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18059 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18060 return dwarf_proc_die;
18063 /* Return whether TYPE is a supported type as a DWARF procedure argument
18064 type or return type (we handle only scalar types and pointer types that
18065 aren't wider than the DWARF expression evaluation stack). */
18067 static bool
18068 is_handled_procedure_type (tree type)
18070 return ((INTEGRAL_TYPE_P (type)
18071 || TREE_CODE (type) == OFFSET_TYPE
18072 || TREE_CODE (type) == POINTER_TYPE)
18073 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18076 /* Helper for resolve_args_picking: do the same but stop when coming across
18077 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18078 offset *before* evaluating the corresponding operation. */
18080 static bool
18081 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18082 struct dwarf_procedure_info *dpi,
18083 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18085 /* The "frame_offset" identifier is already used to name a macro... */
18086 unsigned frame_offset_ = initial_frame_offset;
18087 dw_loc_descr_ref l;
18089 for (l = loc; l != NULL;)
18091 bool existed;
18092 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18094 /* If we already met this node, there is nothing to compute anymore. */
18095 if (existed)
18097 /* Make sure that the stack size is consistent wherever the execution
18098 flow comes from. */
18099 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18100 break;
18102 l_frame_offset = frame_offset_;
18104 /* If needed, relocate the picking offset with respect to the frame
18105 offset. */
18106 if (l->frame_offset_rel)
18108 unsigned HOST_WIDE_INT off;
18109 switch (l->dw_loc_opc)
18111 case DW_OP_pick:
18112 off = l->dw_loc_oprnd1.v.val_unsigned;
18113 break;
18114 case DW_OP_dup:
18115 off = 0;
18116 break;
18117 case DW_OP_over:
18118 off = 1;
18119 break;
18120 default:
18121 gcc_unreachable ();
18123 /* frame_offset_ is the size of the current stack frame, including
18124 incoming arguments. Besides, the arguments are pushed
18125 right-to-left. Thus, in order to access the Nth argument from
18126 this operation node, the picking has to skip temporaries *plus*
18127 one stack slot per argument (0 for the first one, 1 for the second
18128 one, etc.).
18130 The targetted argument number (N) is already set as the operand,
18131 and the number of temporaries can be computed with:
18132 frame_offsets_ - dpi->args_count */
18133 off += frame_offset_ - dpi->args_count;
18135 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18136 if (off > 255)
18137 return false;
18139 if (off == 0)
18141 l->dw_loc_opc = DW_OP_dup;
18142 l->dw_loc_oprnd1.v.val_unsigned = 0;
18144 else if (off == 1)
18146 l->dw_loc_opc = DW_OP_over;
18147 l->dw_loc_oprnd1.v.val_unsigned = 0;
18149 else
18151 l->dw_loc_opc = DW_OP_pick;
18152 l->dw_loc_oprnd1.v.val_unsigned = off;
18156 /* Update frame_offset according to the effect the current operation has
18157 on the stack. */
18158 switch (l->dw_loc_opc)
18160 case DW_OP_deref:
18161 case DW_OP_swap:
18162 case DW_OP_rot:
18163 case DW_OP_abs:
18164 case DW_OP_neg:
18165 case DW_OP_not:
18166 case DW_OP_plus_uconst:
18167 case DW_OP_skip:
18168 case DW_OP_reg0:
18169 case DW_OP_reg1:
18170 case DW_OP_reg2:
18171 case DW_OP_reg3:
18172 case DW_OP_reg4:
18173 case DW_OP_reg5:
18174 case DW_OP_reg6:
18175 case DW_OP_reg7:
18176 case DW_OP_reg8:
18177 case DW_OP_reg9:
18178 case DW_OP_reg10:
18179 case DW_OP_reg11:
18180 case DW_OP_reg12:
18181 case DW_OP_reg13:
18182 case DW_OP_reg14:
18183 case DW_OP_reg15:
18184 case DW_OP_reg16:
18185 case DW_OP_reg17:
18186 case DW_OP_reg18:
18187 case DW_OP_reg19:
18188 case DW_OP_reg20:
18189 case DW_OP_reg21:
18190 case DW_OP_reg22:
18191 case DW_OP_reg23:
18192 case DW_OP_reg24:
18193 case DW_OP_reg25:
18194 case DW_OP_reg26:
18195 case DW_OP_reg27:
18196 case DW_OP_reg28:
18197 case DW_OP_reg29:
18198 case DW_OP_reg30:
18199 case DW_OP_reg31:
18200 case DW_OP_bregx:
18201 case DW_OP_piece:
18202 case DW_OP_deref_size:
18203 case DW_OP_nop:
18204 case DW_OP_bit_piece:
18205 case DW_OP_implicit_value:
18206 case DW_OP_stack_value:
18207 case DW_OP_deref_type:
18208 case DW_OP_convert:
18209 case DW_OP_reinterpret:
18210 case DW_OP_GNU_deref_type:
18211 case DW_OP_GNU_convert:
18212 case DW_OP_GNU_reinterpret:
18213 break;
18215 case DW_OP_addr:
18216 case DW_OP_const1u:
18217 case DW_OP_const1s:
18218 case DW_OP_const2u:
18219 case DW_OP_const2s:
18220 case DW_OP_const4u:
18221 case DW_OP_const4s:
18222 case DW_OP_const8u:
18223 case DW_OP_const8s:
18224 case DW_OP_constu:
18225 case DW_OP_consts:
18226 case DW_OP_dup:
18227 case DW_OP_over:
18228 case DW_OP_pick:
18229 case DW_OP_lit0:
18230 case DW_OP_lit1:
18231 case DW_OP_lit2:
18232 case DW_OP_lit3:
18233 case DW_OP_lit4:
18234 case DW_OP_lit5:
18235 case DW_OP_lit6:
18236 case DW_OP_lit7:
18237 case DW_OP_lit8:
18238 case DW_OP_lit9:
18239 case DW_OP_lit10:
18240 case DW_OP_lit11:
18241 case DW_OP_lit12:
18242 case DW_OP_lit13:
18243 case DW_OP_lit14:
18244 case DW_OP_lit15:
18245 case DW_OP_lit16:
18246 case DW_OP_lit17:
18247 case DW_OP_lit18:
18248 case DW_OP_lit19:
18249 case DW_OP_lit20:
18250 case DW_OP_lit21:
18251 case DW_OP_lit22:
18252 case DW_OP_lit23:
18253 case DW_OP_lit24:
18254 case DW_OP_lit25:
18255 case DW_OP_lit26:
18256 case DW_OP_lit27:
18257 case DW_OP_lit28:
18258 case DW_OP_lit29:
18259 case DW_OP_lit30:
18260 case DW_OP_lit31:
18261 case DW_OP_breg0:
18262 case DW_OP_breg1:
18263 case DW_OP_breg2:
18264 case DW_OP_breg3:
18265 case DW_OP_breg4:
18266 case DW_OP_breg5:
18267 case DW_OP_breg6:
18268 case DW_OP_breg7:
18269 case DW_OP_breg8:
18270 case DW_OP_breg9:
18271 case DW_OP_breg10:
18272 case DW_OP_breg11:
18273 case DW_OP_breg12:
18274 case DW_OP_breg13:
18275 case DW_OP_breg14:
18276 case DW_OP_breg15:
18277 case DW_OP_breg16:
18278 case DW_OP_breg17:
18279 case DW_OP_breg18:
18280 case DW_OP_breg19:
18281 case DW_OP_breg20:
18282 case DW_OP_breg21:
18283 case DW_OP_breg22:
18284 case DW_OP_breg23:
18285 case DW_OP_breg24:
18286 case DW_OP_breg25:
18287 case DW_OP_breg26:
18288 case DW_OP_breg27:
18289 case DW_OP_breg28:
18290 case DW_OP_breg29:
18291 case DW_OP_breg30:
18292 case DW_OP_breg31:
18293 case DW_OP_fbreg:
18294 case DW_OP_push_object_address:
18295 case DW_OP_call_frame_cfa:
18296 case DW_OP_GNU_variable_value:
18297 case DW_OP_GNU_addr_index:
18298 case DW_OP_GNU_const_index:
18299 ++frame_offset_;
18300 break;
18302 case DW_OP_drop:
18303 case DW_OP_xderef:
18304 case DW_OP_and:
18305 case DW_OP_div:
18306 case DW_OP_minus:
18307 case DW_OP_mod:
18308 case DW_OP_mul:
18309 case DW_OP_or:
18310 case DW_OP_plus:
18311 case DW_OP_shl:
18312 case DW_OP_shr:
18313 case DW_OP_shra:
18314 case DW_OP_xor:
18315 case DW_OP_bra:
18316 case DW_OP_eq:
18317 case DW_OP_ge:
18318 case DW_OP_gt:
18319 case DW_OP_le:
18320 case DW_OP_lt:
18321 case DW_OP_ne:
18322 case DW_OP_regx:
18323 case DW_OP_xderef_size:
18324 --frame_offset_;
18325 break;
18327 case DW_OP_call2:
18328 case DW_OP_call4:
18329 case DW_OP_call_ref:
18331 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18332 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18334 if (stack_usage == NULL)
18335 return false;
18336 frame_offset_ += *stack_usage;
18337 break;
18340 case DW_OP_implicit_pointer:
18341 case DW_OP_entry_value:
18342 case DW_OP_const_type:
18343 case DW_OP_regval_type:
18344 case DW_OP_form_tls_address:
18345 case DW_OP_GNU_push_tls_address:
18346 case DW_OP_GNU_uninit:
18347 case DW_OP_GNU_encoded_addr:
18348 case DW_OP_GNU_implicit_pointer:
18349 case DW_OP_GNU_entry_value:
18350 case DW_OP_GNU_const_type:
18351 case DW_OP_GNU_regval_type:
18352 case DW_OP_GNU_parameter_ref:
18353 /* loc_list_from_tree will probably not output these operations for
18354 size functions, so assume they will not appear here. */
18355 /* Fall through... */
18357 default:
18358 gcc_unreachable ();
18361 /* Now, follow the control flow (except subroutine calls). */
18362 switch (l->dw_loc_opc)
18364 case DW_OP_bra:
18365 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18366 frame_offsets))
18367 return false;
18368 /* Fall through. */
18370 case DW_OP_skip:
18371 l = l->dw_loc_oprnd1.v.val_loc;
18372 break;
18374 case DW_OP_stack_value:
18375 return true;
18377 default:
18378 l = l->dw_loc_next;
18379 break;
18383 return true;
18386 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18387 operations) in order to resolve the operand of DW_OP_pick operations that
18388 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18389 offset *before* LOC is executed. Return if all relocations were
18390 successful. */
18392 static bool
18393 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18394 struct dwarf_procedure_info *dpi)
18396 /* Associate to all visited operations the frame offset *before* evaluating
18397 this operation. */
18398 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18400 return
18401 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18404 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18405 Return NULL if it is not possible. */
18407 static dw_die_ref
18408 function_to_dwarf_procedure (tree fndecl)
18410 struct dwarf_procedure_info dpi;
18411 struct loc_descr_context ctx = {
18412 NULL_TREE, /* context_type */
18413 NULL_TREE, /* base_decl */
18414 &dpi, /* dpi */
18415 false, /* placeholder_arg */
18416 false, /* placeholder_seen */
18417 true /* strict_signedness */
18419 dw_die_ref dwarf_proc_die;
18420 tree tree_body = DECL_SAVED_TREE (fndecl);
18421 dw_loc_descr_ref loc_body, epilogue;
18423 tree cursor;
18424 unsigned i;
18426 /* Do not generate multiple DWARF procedures for the same function
18427 declaration. */
18428 dwarf_proc_die = lookup_decl_die (fndecl);
18429 if (dwarf_proc_die != NULL)
18430 return dwarf_proc_die;
18432 /* DWARF procedures are available starting with the DWARFv3 standard. */
18433 if (dwarf_version < 3 && dwarf_strict)
18434 return NULL;
18436 /* We handle only functions for which we still have a body, that return a
18437 supported type and that takes arguments with supported types. Note that
18438 there is no point translating functions that return nothing. */
18439 if (tree_body == NULL_TREE
18440 || DECL_RESULT (fndecl) == NULL_TREE
18441 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18442 return NULL;
18444 for (cursor = DECL_ARGUMENTS (fndecl);
18445 cursor != NULL_TREE;
18446 cursor = TREE_CHAIN (cursor))
18447 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18448 return NULL;
18450 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18451 if (TREE_CODE (tree_body) != RETURN_EXPR)
18452 return NULL;
18453 tree_body = TREE_OPERAND (tree_body, 0);
18454 if (TREE_CODE (tree_body) != MODIFY_EXPR
18455 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18456 return NULL;
18457 tree_body = TREE_OPERAND (tree_body, 1);
18459 /* Try to translate the body expression itself. Note that this will probably
18460 cause an infinite recursion if its call graph has a cycle. This is very
18461 unlikely for size functions, however, so don't bother with such things at
18462 the moment. */
18463 dpi.fndecl = fndecl;
18464 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18465 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18466 if (!loc_body)
18467 return NULL;
18469 /* After evaluating all operands in "loc_body", we should still have on the
18470 stack all arguments plus the desired function result (top of the stack).
18471 Generate code in order to keep only the result in our stack frame. */
18472 epilogue = NULL;
18473 for (i = 0; i < dpi.args_count; ++i)
18475 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18476 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18477 op_couple->dw_loc_next->dw_loc_next = epilogue;
18478 epilogue = op_couple;
18480 add_loc_descr (&loc_body, epilogue);
18481 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18482 return NULL;
18484 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18485 because they are considered useful. Now there is an epilogue, they are
18486 not anymore, so give it another try. */
18487 loc_descr_without_nops (loc_body);
18489 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18490 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18491 though, given that size functions do not come from source, so they should
18492 not have a dedicated DW_TAG_subprogram DIE. */
18493 dwarf_proc_die
18494 = new_dwarf_proc_die (loc_body, fndecl,
18495 get_context_die (DECL_CONTEXT (fndecl)));
18497 /* The called DWARF procedure consumes one stack slot per argument and
18498 returns one stack slot. */
18499 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18501 return dwarf_proc_die;
18504 /* Helper function for loc_list_from_tree. Perform OP binary op,
18505 but after converting arguments to type_die, afterwards convert
18506 back to unsigned. */
18508 static dw_loc_list_ref
18509 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18510 dw_die_ref type_die, scalar_int_mode mode,
18511 struct loc_descr_context *context)
18513 dw_loc_list_ref op0, op1;
18514 dw_loc_descr_ref cvt, binop;
18516 if (type_die == NULL)
18517 return NULL;
18519 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18520 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18521 if (op0 == NULL || op1 == NULL)
18522 return NULL;
18524 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18525 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18526 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18527 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18528 add_loc_descr_to_each (op0, cvt);
18530 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18531 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18532 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18533 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18534 add_loc_descr_to_each (op1, cvt);
18536 add_loc_list (&op0, op1);
18537 if (op0 == NULL)
18538 return NULL;
18540 binop = new_loc_descr (op, 0, 0);
18541 convert_descriptor_to_mode (mode, binop);
18542 add_loc_descr_to_each (op0, binop);
18544 return op0;
18547 /* Generate Dwarf location list representing LOC.
18548 If WANT_ADDRESS is false, expression computing LOC will be computed
18549 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18550 if WANT_ADDRESS is 2, expression computing address useable in location
18551 will be returned (i.e. DW_OP_reg can be used
18552 to refer to register values).
18554 CONTEXT provides information to customize the location descriptions
18555 generation. Its context_type field specifies what type is implicitly
18556 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18557 will not be generated.
18559 Its DPI field determines whether we are generating a DWARF expression for a
18560 DWARF procedure, so PARM_DECL references are processed specifically.
18562 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18563 and dpi fields were null. */
18565 static dw_loc_list_ref
18566 loc_list_from_tree_1 (tree loc, int want_address,
18567 struct loc_descr_context *context)
18569 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18570 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18571 int have_address = 0;
18572 enum dwarf_location_atom op;
18574 /* ??? Most of the time we do not take proper care for sign/zero
18575 extending the values properly. Hopefully this won't be a real
18576 problem... */
18578 if (context != NULL
18579 && context->base_decl == loc
18580 && want_address == 0)
18582 if (dwarf_version >= 3 || !dwarf_strict)
18583 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18584 NULL, 0, NULL, 0, NULL);
18585 else
18586 return NULL;
18589 switch (TREE_CODE (loc))
18591 case ERROR_MARK:
18592 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18593 return 0;
18595 case PLACEHOLDER_EXPR:
18596 /* This case involves extracting fields from an object to determine the
18597 position of other fields. It is supposed to appear only as the first
18598 operand of COMPONENT_REF nodes and to reference precisely the type
18599 that the context allows or its enclosing type. */
18600 if (context != NULL
18601 && (TREE_TYPE (loc) == context->context_type
18602 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18603 && want_address >= 1)
18605 if (dwarf_version >= 3 || !dwarf_strict)
18607 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18608 have_address = 1;
18609 break;
18611 else
18612 return NULL;
18614 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18615 the single argument passed by consumer. */
18616 else if (context != NULL
18617 && context->placeholder_arg
18618 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18619 && want_address == 0)
18621 ret = new_loc_descr (DW_OP_pick, 0, 0);
18622 ret->frame_offset_rel = 1;
18623 context->placeholder_seen = true;
18624 break;
18626 else
18627 expansion_failed (loc, NULL_RTX,
18628 "PLACEHOLDER_EXPR for an unexpected type");
18629 break;
18631 case CALL_EXPR:
18633 tree callee = get_callee_fndecl (loc);
18634 dw_die_ref dwarf_proc;
18636 if (callee
18637 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18638 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18640 /* DWARF procedures are used for size functions, which are built
18641 when size expressions contain conditional constructs, so we
18642 request strict preservation of signedness for comparisons. */
18643 bool old_strict_signedness;
18644 if (context)
18646 old_strict_signedness = context->strict_signedness;
18647 context->strict_signedness = true;
18650 /* Evaluate arguments right-to-left so that the first argument
18651 will be the top-most one on the stack. */
18652 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18654 tree arg = CALL_EXPR_ARG (loc, i);
18655 ret1 = loc_descriptor_from_tree (arg, 0, context);
18656 if (!ret1)
18658 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18659 return NULL;
18661 add_loc_descr (&ret, ret1);
18664 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18665 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18666 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18667 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18668 add_loc_descr (&ret, ret1);
18669 if (context)
18670 context->strict_signedness = old_strict_signedness;
18672 else
18673 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18674 break;
18677 case PREINCREMENT_EXPR:
18678 case PREDECREMENT_EXPR:
18679 case POSTINCREMENT_EXPR:
18680 case POSTDECREMENT_EXPR:
18681 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18682 /* There are no opcodes for these operations. */
18683 return 0;
18685 case ADDR_EXPR:
18686 /* If we already want an address, see if there is INDIRECT_REF inside
18687 e.g. for &this->field. */
18688 if (want_address)
18690 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18691 (loc, want_address == 2, context);
18692 if (list_ret)
18693 have_address = 1;
18694 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18695 && (ret = cst_pool_loc_descr (loc)))
18696 have_address = 1;
18698 /* Otherwise, process the argument and look for the address. */
18699 if (!list_ret && !ret)
18700 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18701 else
18703 if (want_address)
18704 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18705 return NULL;
18707 break;
18709 case VAR_DECL:
18710 if (DECL_THREAD_LOCAL_P (loc))
18712 rtx rtl;
18713 enum dwarf_location_atom tls_op;
18714 enum dtprel_bool dtprel = dtprel_false;
18716 if (targetm.have_tls)
18718 /* If this is not defined, we have no way to emit the
18719 data. */
18720 if (!targetm.asm_out.output_dwarf_dtprel)
18721 return 0;
18723 /* The way DW_OP_GNU_push_tls_address is specified, we
18724 can only look up addresses of objects in the current
18725 module. We used DW_OP_addr as first op, but that's
18726 wrong, because DW_OP_addr is relocated by the debug
18727 info consumer, while DW_OP_GNU_push_tls_address
18728 operand shouldn't be. */
18729 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18730 return 0;
18731 dtprel = dtprel_true;
18732 /* We check for DWARF 5 here because gdb did not implement
18733 DW_OP_form_tls_address until after 7.12. */
18734 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18735 : DW_OP_GNU_push_tls_address);
18737 else
18739 if (!targetm.emutls.debug_form_tls_address
18740 || !(dwarf_version >= 3 || !dwarf_strict))
18741 return 0;
18742 /* We stuffed the control variable into the DECL_VALUE_EXPR
18743 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18744 no longer appear in gimple code. We used the control
18745 variable in specific so that we could pick it up here. */
18746 loc = DECL_VALUE_EXPR (loc);
18747 tls_op = DW_OP_form_tls_address;
18750 rtl = rtl_for_decl_location (loc);
18751 if (rtl == NULL_RTX)
18752 return 0;
18754 if (!MEM_P (rtl))
18755 return 0;
18756 rtl = XEXP (rtl, 0);
18757 if (! CONSTANT_P (rtl))
18758 return 0;
18760 ret = new_addr_loc_descr (rtl, dtprel);
18761 ret1 = new_loc_descr (tls_op, 0, 0);
18762 add_loc_descr (&ret, ret1);
18764 have_address = 1;
18765 break;
18767 /* FALLTHRU */
18769 case PARM_DECL:
18770 if (context != NULL && context->dpi != NULL
18771 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18773 /* We are generating code for a DWARF procedure and we want to access
18774 one of its arguments: find the appropriate argument offset and let
18775 the resolve_args_picking pass compute the offset that complies
18776 with the stack frame size. */
18777 unsigned i = 0;
18778 tree cursor;
18780 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18781 cursor != NULL_TREE && cursor != loc;
18782 cursor = TREE_CHAIN (cursor), ++i)
18784 /* If we are translating a DWARF procedure, all referenced parameters
18785 must belong to the current function. */
18786 gcc_assert (cursor != NULL_TREE);
18788 ret = new_loc_descr (DW_OP_pick, i, 0);
18789 ret->frame_offset_rel = 1;
18790 break;
18792 /* FALLTHRU */
18794 case RESULT_DECL:
18795 if (DECL_HAS_VALUE_EXPR_P (loc))
18797 tree value_expr = DECL_VALUE_EXPR (loc);
18799 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18800 to wait until they get an RTX in order to reference them. */
18801 if (early_dwarf
18802 && TREE_CODE (value_expr) == COMPONENT_REF
18803 && VAR_P (TREE_OPERAND (value_expr, 0))
18804 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18806 else
18807 return loc_list_from_tree_1 (value_expr, want_address, context);
18810 /* FALLTHRU */
18812 case FUNCTION_DECL:
18814 rtx rtl;
18815 var_loc_list *loc_list = lookup_decl_loc (loc);
18817 if (loc_list && loc_list->first)
18819 list_ret = dw_loc_list (loc_list, loc, want_address);
18820 have_address = want_address != 0;
18821 break;
18823 rtl = rtl_for_decl_location (loc);
18824 if (rtl == NULL_RTX)
18826 if (TREE_CODE (loc) != FUNCTION_DECL
18827 && early_dwarf
18828 && want_address != 1
18829 && ! DECL_IGNORED_P (loc)
18830 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18831 || POINTER_TYPE_P (TREE_TYPE (loc)))
18832 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18833 <= DWARF2_ADDR_SIZE))
18835 dw_die_ref ref = lookup_decl_die (loc);
18836 if (ref)
18838 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18839 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18840 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18841 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18843 else if (current_function_decl
18844 && DECL_CONTEXT (loc) == current_function_decl)
18846 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18847 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18848 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18850 break;
18852 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18853 return 0;
18855 else if (CONST_INT_P (rtl))
18857 HOST_WIDE_INT val = INTVAL (rtl);
18858 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18859 val &= GET_MODE_MASK (DECL_MODE (loc));
18860 ret = int_loc_descriptor (val);
18862 else if (GET_CODE (rtl) == CONST_STRING)
18864 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18865 return 0;
18867 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18868 ret = new_addr_loc_descr (rtl, dtprel_false);
18869 else
18871 machine_mode mode, mem_mode;
18873 /* Certain constructs can only be represented at top-level. */
18874 if (want_address == 2)
18876 ret = loc_descriptor (rtl, VOIDmode,
18877 VAR_INIT_STATUS_INITIALIZED);
18878 have_address = 1;
18880 else
18882 mode = GET_MODE (rtl);
18883 mem_mode = VOIDmode;
18884 if (MEM_P (rtl))
18886 mem_mode = mode;
18887 mode = get_address_mode (rtl);
18888 rtl = XEXP (rtl, 0);
18889 have_address = 1;
18891 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18892 VAR_INIT_STATUS_INITIALIZED);
18894 if (!ret)
18895 expansion_failed (loc, rtl,
18896 "failed to produce loc descriptor for rtl");
18899 break;
18901 case MEM_REF:
18902 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18904 have_address = 1;
18905 goto do_plus;
18907 /* Fallthru. */
18908 case INDIRECT_REF:
18909 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18910 have_address = 1;
18911 break;
18913 case TARGET_MEM_REF:
18914 case SSA_NAME:
18915 case DEBUG_EXPR_DECL:
18916 return NULL;
18918 case COMPOUND_EXPR:
18919 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18920 context);
18922 CASE_CONVERT:
18923 case VIEW_CONVERT_EXPR:
18924 case SAVE_EXPR:
18925 case MODIFY_EXPR:
18926 case NON_LVALUE_EXPR:
18927 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18928 context);
18930 case COMPONENT_REF:
18931 case BIT_FIELD_REF:
18932 case ARRAY_REF:
18933 case ARRAY_RANGE_REF:
18934 case REALPART_EXPR:
18935 case IMAGPART_EXPR:
18937 tree obj, offset;
18938 poly_int64 bitsize, bitpos, bytepos;
18939 machine_mode mode;
18940 int unsignedp, reversep, volatilep = 0;
18942 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18943 &unsignedp, &reversep, &volatilep);
18945 gcc_assert (obj != loc);
18947 list_ret = loc_list_from_tree_1 (obj,
18948 want_address == 2
18949 && known_eq (bitpos, 0)
18950 && !offset ? 2 : 1,
18951 context);
18952 /* TODO: We can extract value of the small expression via shifting even
18953 for nonzero bitpos. */
18954 if (list_ret == 0)
18955 return 0;
18956 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18957 || !multiple_p (bitsize, BITS_PER_UNIT))
18959 expansion_failed (loc, NULL_RTX,
18960 "bitfield access");
18961 return 0;
18964 if (offset != NULL_TREE)
18966 /* Variable offset. */
18967 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18968 if (list_ret1 == 0)
18969 return 0;
18970 add_loc_list (&list_ret, list_ret1);
18971 if (!list_ret)
18972 return 0;
18973 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18976 HOST_WIDE_INT value;
18977 if (bytepos.is_constant (&value) && value > 0)
18978 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18979 value, 0));
18980 else if (maybe_ne (bytepos, 0))
18981 loc_list_plus_const (list_ret, bytepos);
18983 have_address = 1;
18984 break;
18987 case INTEGER_CST:
18988 if ((want_address || !tree_fits_shwi_p (loc))
18989 && (ret = cst_pool_loc_descr (loc)))
18990 have_address = 1;
18991 else if (want_address == 2
18992 && tree_fits_shwi_p (loc)
18993 && (ret = address_of_int_loc_descriptor
18994 (int_size_in_bytes (TREE_TYPE (loc)),
18995 tree_to_shwi (loc))))
18996 have_address = 1;
18997 else if (tree_fits_shwi_p (loc))
18998 ret = int_loc_descriptor (tree_to_shwi (loc));
18999 else if (tree_fits_uhwi_p (loc))
19000 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19001 else
19003 expansion_failed (loc, NULL_RTX,
19004 "Integer operand is not host integer");
19005 return 0;
19007 break;
19009 case POLY_INT_CST:
19011 if (want_address)
19013 expansion_failed (loc, NULL_RTX,
19014 "constant address with a runtime component");
19015 return 0;
19017 poly_int64 value;
19018 if (!poly_int_tree_p (loc, &value))
19020 expansion_failed (loc, NULL_RTX, "constant too big");
19021 return 0;
19023 ret = int_loc_descriptor (value);
19025 break;
19027 case CONSTRUCTOR:
19028 case REAL_CST:
19029 case STRING_CST:
19030 case COMPLEX_CST:
19031 if ((ret = cst_pool_loc_descr (loc)))
19032 have_address = 1;
19033 else if (TREE_CODE (loc) == CONSTRUCTOR)
19035 tree type = TREE_TYPE (loc);
19036 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19037 unsigned HOST_WIDE_INT offset = 0;
19038 unsigned HOST_WIDE_INT cnt;
19039 constructor_elt *ce;
19041 if (TREE_CODE (type) == RECORD_TYPE)
19043 /* This is very limited, but it's enough to output
19044 pointers to member functions, as long as the
19045 referenced function is defined in the current
19046 translation unit. */
19047 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19049 tree val = ce->value;
19051 tree field = ce->index;
19053 if (val)
19054 STRIP_NOPS (val);
19056 if (!field || DECL_BIT_FIELD (field))
19058 expansion_failed (loc, NULL_RTX,
19059 "bitfield in record type constructor");
19060 size = offset = (unsigned HOST_WIDE_INT)-1;
19061 ret = NULL;
19062 break;
19065 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19066 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19067 gcc_assert (pos + fieldsize <= size);
19068 if (pos < offset)
19070 expansion_failed (loc, NULL_RTX,
19071 "out-of-order fields in record constructor");
19072 size = offset = (unsigned HOST_WIDE_INT)-1;
19073 ret = NULL;
19074 break;
19076 if (pos > offset)
19078 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19079 add_loc_descr (&ret, ret1);
19080 offset = pos;
19082 if (val && fieldsize != 0)
19084 ret1 = loc_descriptor_from_tree (val, want_address, context);
19085 if (!ret1)
19087 expansion_failed (loc, NULL_RTX,
19088 "unsupported expression in field");
19089 size = offset = (unsigned HOST_WIDE_INT)-1;
19090 ret = NULL;
19091 break;
19093 add_loc_descr (&ret, ret1);
19095 if (fieldsize)
19097 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19098 add_loc_descr (&ret, ret1);
19099 offset = pos + fieldsize;
19103 if (offset != size)
19105 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19106 add_loc_descr (&ret, ret1);
19107 offset = size;
19110 have_address = !!want_address;
19112 else
19113 expansion_failed (loc, NULL_RTX,
19114 "constructor of non-record type");
19116 else
19117 /* We can construct small constants here using int_loc_descriptor. */
19118 expansion_failed (loc, NULL_RTX,
19119 "constructor or constant not in constant pool");
19120 break;
19122 case TRUTH_AND_EXPR:
19123 case TRUTH_ANDIF_EXPR:
19124 case BIT_AND_EXPR:
19125 op = DW_OP_and;
19126 goto do_binop;
19128 case TRUTH_XOR_EXPR:
19129 case BIT_XOR_EXPR:
19130 op = DW_OP_xor;
19131 goto do_binop;
19133 case TRUTH_OR_EXPR:
19134 case TRUTH_ORIF_EXPR:
19135 case BIT_IOR_EXPR:
19136 op = DW_OP_or;
19137 goto do_binop;
19139 case EXACT_DIV_EXPR:
19140 case FLOOR_DIV_EXPR:
19141 case TRUNC_DIV_EXPR:
19142 /* Turn a divide by a power of 2 into a shift when possible. */
19143 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19144 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19146 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19147 if (log2 > 0)
19149 list_ret
19150 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19151 if (list_ret == 0)
19152 return 0;
19154 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19155 add_loc_descr_to_each (list_ret,
19156 new_loc_descr (DW_OP_shr, 0, 0));
19157 break;
19161 /* fall through */
19163 case CEIL_DIV_EXPR:
19164 case ROUND_DIV_EXPR:
19165 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19167 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19168 scalar_int_mode int_mode;
19170 if ((dwarf_strict && dwarf_version < 5)
19171 || !is_a <scalar_int_mode> (mode, &int_mode))
19172 return 0;
19174 /* We can use a signed divide if the sign bit is not set. */
19175 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19177 op = DW_OP_div;
19178 goto do_binop;
19181 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19182 base_type_for_mode (int_mode, 1),
19183 int_mode, context);
19184 break;
19186 op = DW_OP_div;
19187 goto do_binop;
19189 case MINUS_EXPR:
19190 op = DW_OP_minus;
19191 goto do_binop;
19193 case FLOOR_MOD_EXPR:
19194 case CEIL_MOD_EXPR:
19195 case ROUND_MOD_EXPR:
19196 case TRUNC_MOD_EXPR:
19197 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19199 op = DW_OP_mod;
19200 goto do_binop;
19202 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19203 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19204 if (list_ret == 0 || list_ret1 == 0)
19205 return 0;
19207 add_loc_list (&list_ret, list_ret1);
19208 if (list_ret == 0)
19209 return 0;
19210 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19211 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19212 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19213 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19214 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19215 break;
19217 case MULT_EXPR:
19218 op = DW_OP_mul;
19219 goto do_binop;
19221 case LSHIFT_EXPR:
19222 op = DW_OP_shl;
19223 goto do_binop;
19225 case RSHIFT_EXPR:
19226 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19227 goto do_binop;
19229 case POINTER_PLUS_EXPR:
19230 case PLUS_EXPR:
19231 do_plus:
19232 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19234 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19235 smarter to encode their opposite. The DW_OP_plus_uconst operation
19236 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19237 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19238 bytes, Y being the size of the operation that pushes the opposite
19239 of the addend. So let's choose the smallest representation. */
19240 const tree tree_addend = TREE_OPERAND (loc, 1);
19241 offset_int wi_addend;
19242 HOST_WIDE_INT shwi_addend;
19243 dw_loc_descr_ref loc_naddend;
19245 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19246 if (list_ret == 0)
19247 return 0;
19249 /* Try to get the literal to push. It is the opposite of the addend,
19250 so as we rely on wrapping during DWARF evaluation, first decode
19251 the literal as a "DWARF-sized" signed number. */
19252 wi_addend = wi::to_offset (tree_addend);
19253 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19254 shwi_addend = wi_addend.to_shwi ();
19255 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19256 ? int_loc_descriptor (-shwi_addend)
19257 : NULL;
19259 if (loc_naddend != NULL
19260 && ((unsigned) size_of_uleb128 (shwi_addend)
19261 > size_of_loc_descr (loc_naddend)))
19263 add_loc_descr_to_each (list_ret, loc_naddend);
19264 add_loc_descr_to_each (list_ret,
19265 new_loc_descr (DW_OP_minus, 0, 0));
19267 else
19269 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19271 loc_naddend = loc_cur;
19272 loc_cur = loc_cur->dw_loc_next;
19273 ggc_free (loc_naddend);
19275 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19277 break;
19280 op = DW_OP_plus;
19281 goto do_binop;
19283 case LE_EXPR:
19284 op = DW_OP_le;
19285 goto do_comp_binop;
19287 case GE_EXPR:
19288 op = DW_OP_ge;
19289 goto do_comp_binop;
19291 case LT_EXPR:
19292 op = DW_OP_lt;
19293 goto do_comp_binop;
19295 case GT_EXPR:
19296 op = DW_OP_gt;
19297 goto do_comp_binop;
19299 do_comp_binop:
19300 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19302 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19303 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19304 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19305 TREE_CODE (loc));
19306 break;
19308 else
19309 goto do_binop;
19311 case EQ_EXPR:
19312 op = DW_OP_eq;
19313 goto do_binop;
19315 case NE_EXPR:
19316 op = DW_OP_ne;
19317 goto do_binop;
19319 do_binop:
19320 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19321 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19322 if (list_ret == 0 || list_ret1 == 0)
19323 return 0;
19325 add_loc_list (&list_ret, list_ret1);
19326 if (list_ret == 0)
19327 return 0;
19328 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19329 break;
19331 case TRUTH_NOT_EXPR:
19332 case BIT_NOT_EXPR:
19333 op = DW_OP_not;
19334 goto do_unop;
19336 case ABS_EXPR:
19337 op = DW_OP_abs;
19338 goto do_unop;
19340 case NEGATE_EXPR:
19341 op = DW_OP_neg;
19342 goto do_unop;
19344 do_unop:
19345 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19346 if (list_ret == 0)
19347 return 0;
19349 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19350 break;
19352 case MIN_EXPR:
19353 case MAX_EXPR:
19355 const enum tree_code code =
19356 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19358 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19359 build2 (code, integer_type_node,
19360 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19361 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19364 /* fall through */
19366 case COND_EXPR:
19368 dw_loc_descr_ref lhs
19369 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19370 dw_loc_list_ref rhs
19371 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19372 dw_loc_descr_ref bra_node, jump_node, tmp;
19374 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19375 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19376 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19377 list_ret
19378 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19379 0, context);
19380 else
19381 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19382 if (list_ret == 0 || lhs == 0 || rhs == 0)
19383 return 0;
19385 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19386 add_loc_descr_to_each (list_ret, bra_node);
19388 add_loc_list (&list_ret, rhs);
19389 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19390 add_loc_descr_to_each (list_ret, jump_node);
19392 add_loc_descr_to_each (list_ret, lhs);
19393 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19394 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19396 /* ??? Need a node to point the skip at. Use a nop. */
19397 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19398 add_loc_descr_to_each (list_ret, tmp);
19399 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19400 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19402 break;
19404 case FIX_TRUNC_EXPR:
19405 return 0;
19407 case COMPOUND_LITERAL_EXPR:
19408 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19409 0, context);
19411 default:
19412 /* Leave front-end specific codes as simply unknown. This comes
19413 up, for instance, with the C STMT_EXPR. */
19414 if ((unsigned int) TREE_CODE (loc)
19415 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19417 expansion_failed (loc, NULL_RTX,
19418 "language specific tree node");
19419 return 0;
19422 /* Otherwise this is a generic code; we should just lists all of
19423 these explicitly. We forgot one. */
19424 if (flag_checking)
19425 gcc_unreachable ();
19427 /* In a release build, we want to degrade gracefully: better to
19428 generate incomplete debugging information than to crash. */
19429 return NULL;
19432 if (!ret && !list_ret)
19433 return 0;
19435 if (want_address == 2 && !have_address
19436 && (dwarf_version >= 4 || !dwarf_strict))
19438 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19440 expansion_failed (loc, NULL_RTX,
19441 "DWARF address size mismatch");
19442 return 0;
19444 if (ret)
19445 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19446 else
19447 add_loc_descr_to_each (list_ret,
19448 new_loc_descr (DW_OP_stack_value, 0, 0));
19449 have_address = 1;
19451 /* Show if we can't fill the request for an address. */
19452 if (want_address && !have_address)
19454 expansion_failed (loc, NULL_RTX,
19455 "Want address and only have value");
19456 return 0;
19459 gcc_assert (!ret || !list_ret);
19461 /* If we've got an address and don't want one, dereference. */
19462 if (!want_address && have_address)
19464 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19465 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19466 scalar_int_mode int_mode;
19467 dw_die_ref type_die;
19468 dw_loc_descr_ref deref;
19470 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19471 if (size > DWARF2_ADDR_SIZE || size == -1)
19473 expansion_failed (loc, NULL_RTX,
19474 "DWARF address size mismatch");
19475 return 0;
19478 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19479 else if (size == DWARF2_ADDR_SIZE)
19480 deref = new_loc_descr (DW_OP_deref, size, 0);
19482 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19483 extend the value, which is really OK for unsigned types only. */
19484 else if (!(context && context->strict_signedness)
19485 || TYPE_UNSIGNED (TREE_TYPE (loc))
19486 || (dwarf_strict && dwarf_version < 5)
19487 || !is_a <scalar_int_mode> (mode, &int_mode)
19488 || !(type_die = base_type_for_mode (mode, false)))
19489 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19491 /* Use DW_OP_deref_type for signed integral types if possible, but
19492 convert back to the generic type to avoid type mismatches later. */
19493 else
19495 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19496 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19497 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19498 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19499 add_loc_descr (&deref,
19500 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19503 if (ret)
19504 add_loc_descr (&ret, deref);
19505 else
19506 add_loc_descr_to_each (list_ret, deref);
19509 if (ret)
19510 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19512 return list_ret;
19515 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19516 expressions. */
19518 static dw_loc_list_ref
19519 loc_list_from_tree (tree loc, int want_address,
19520 struct loc_descr_context *context)
19522 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19524 for (dw_loc_list_ref loc_cur = result;
19525 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19526 loc_descr_without_nops (loc_cur->expr);
19527 return result;
19530 /* Same as above but return only single location expression. */
19531 static dw_loc_descr_ref
19532 loc_descriptor_from_tree (tree loc, int want_address,
19533 struct loc_descr_context *context)
19535 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19536 if (!ret)
19537 return NULL;
19538 if (ret->dw_loc_next)
19540 expansion_failed (loc, NULL_RTX,
19541 "Location list where only loc descriptor needed");
19542 return NULL;
19544 return ret->expr;
19547 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19548 pointer to the declared type for the relevant field variable, or return
19549 `integer_type_node' if the given node turns out to be an
19550 ERROR_MARK node. */
19552 static inline tree
19553 field_type (const_tree decl)
19555 tree type;
19557 if (TREE_CODE (decl) == ERROR_MARK)
19558 return integer_type_node;
19560 type = DECL_BIT_FIELD_TYPE (decl);
19561 if (type == NULL_TREE)
19562 type = TREE_TYPE (decl);
19564 return type;
19567 /* Given a pointer to a tree node, return the alignment in bits for
19568 it, or else return BITS_PER_WORD if the node actually turns out to
19569 be an ERROR_MARK node. */
19571 static inline unsigned
19572 simple_type_align_in_bits (const_tree type)
19574 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19577 static inline unsigned
19578 simple_decl_align_in_bits (const_tree decl)
19580 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19583 /* Return the result of rounding T up to ALIGN. */
19585 static inline offset_int
19586 round_up_to_align (const offset_int &t, unsigned int align)
19588 return wi::udiv_trunc (t + align - 1, align) * align;
19591 /* Helper structure for RECORD_TYPE processing. */
19592 struct vlr_context
19594 /* Root RECORD_TYPE. It is needed to generate data member location
19595 descriptions in variable-length records (VLR), but also to cope with
19596 variants, which are composed of nested structures multiplexed with
19597 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19598 function processing a FIELD_DECL, it is required to be non null. */
19599 tree struct_type;
19601 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19602 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19603 this variant part as part of the root record (in storage units). For
19604 regular records, it must be NULL_TREE. */
19605 tree variant_part_offset;
19608 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19609 addressed byte of the "containing object" for the given FIELD_DECL. If
19610 possible, return a native constant through CST_OFFSET (in which case NULL is
19611 returned); otherwise return a DWARF expression that computes the offset.
19613 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19614 that offset is, either because the argument turns out to be a pointer to an
19615 ERROR_MARK node, or because the offset expression is too complex for us.
19617 CTX is required: see the comment for VLR_CONTEXT. */
19619 static dw_loc_descr_ref
19620 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19621 HOST_WIDE_INT *cst_offset)
19623 tree tree_result;
19624 dw_loc_list_ref loc_result;
19626 *cst_offset = 0;
19628 if (TREE_CODE (decl) == ERROR_MARK)
19629 return NULL;
19630 else
19631 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19633 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19634 case. */
19635 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19636 return NULL;
19638 /* We used to handle only constant offsets in all cases. Now, we handle
19639 properly dynamic byte offsets only when PCC bitfield type doesn't
19640 matter. */
19641 if (PCC_BITFIELD_TYPE_MATTERS
19642 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19644 offset_int object_offset_in_bits;
19645 offset_int object_offset_in_bytes;
19646 offset_int bitpos_int;
19647 tree type;
19648 tree field_size_tree;
19649 offset_int deepest_bitpos;
19650 offset_int field_size_in_bits;
19651 unsigned int type_align_in_bits;
19652 unsigned int decl_align_in_bits;
19653 offset_int type_size_in_bits;
19655 bitpos_int = wi::to_offset (bit_position (decl));
19656 type = field_type (decl);
19657 type_size_in_bits = offset_int_type_size_in_bits (type);
19658 type_align_in_bits = simple_type_align_in_bits (type);
19660 field_size_tree = DECL_SIZE (decl);
19662 /* The size could be unspecified if there was an error, or for
19663 a flexible array member. */
19664 if (!field_size_tree)
19665 field_size_tree = bitsize_zero_node;
19667 /* If the size of the field is not constant, use the type size. */
19668 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19669 field_size_in_bits = wi::to_offset (field_size_tree);
19670 else
19671 field_size_in_bits = type_size_in_bits;
19673 decl_align_in_bits = simple_decl_align_in_bits (decl);
19675 /* The GCC front-end doesn't make any attempt to keep track of the
19676 starting bit offset (relative to the start of the containing
19677 structure type) of the hypothetical "containing object" for a
19678 bit-field. Thus, when computing the byte offset value for the
19679 start of the "containing object" of a bit-field, we must deduce
19680 this information on our own. This can be rather tricky to do in
19681 some cases. For example, handling the following structure type
19682 definition when compiling for an i386/i486 target (which only
19683 aligns long long's to 32-bit boundaries) can be very tricky:
19685 struct S { int field1; long long field2:31; };
19687 Fortunately, there is a simple rule-of-thumb which can be used
19688 in such cases. When compiling for an i386/i486, GCC will
19689 allocate 8 bytes for the structure shown above. It decides to
19690 do this based upon one simple rule for bit-field allocation.
19691 GCC allocates each "containing object" for each bit-field at
19692 the first (i.e. lowest addressed) legitimate alignment boundary
19693 (based upon the required minimum alignment for the declared
19694 type of the field) which it can possibly use, subject to the
19695 condition that there is still enough available space remaining
19696 in the containing object (when allocated at the selected point)
19697 to fully accommodate all of the bits of the bit-field itself.
19699 This simple rule makes it obvious why GCC allocates 8 bytes for
19700 each object of the structure type shown above. When looking
19701 for a place to allocate the "containing object" for `field2',
19702 the compiler simply tries to allocate a 64-bit "containing
19703 object" at each successive 32-bit boundary (starting at zero)
19704 until it finds a place to allocate that 64- bit field such that
19705 at least 31 contiguous (and previously unallocated) bits remain
19706 within that selected 64 bit field. (As it turns out, for the
19707 example above, the compiler finds it is OK to allocate the
19708 "containing object" 64-bit field at bit-offset zero within the
19709 structure type.)
19711 Here we attempt to work backwards from the limited set of facts
19712 we're given, and we try to deduce from those facts, where GCC
19713 must have believed that the containing object started (within
19714 the structure type). The value we deduce is then used (by the
19715 callers of this routine) to generate DW_AT_location and
19716 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19717 the case of DW_AT_location, regular fields as well). */
19719 /* Figure out the bit-distance from the start of the structure to
19720 the "deepest" bit of the bit-field. */
19721 deepest_bitpos = bitpos_int + field_size_in_bits;
19723 /* This is the tricky part. Use some fancy footwork to deduce
19724 where the lowest addressed bit of the containing object must
19725 be. */
19726 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19728 /* Round up to type_align by default. This works best for
19729 bitfields. */
19730 object_offset_in_bits
19731 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19733 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19735 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19737 /* Round up to decl_align instead. */
19738 object_offset_in_bits
19739 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19742 object_offset_in_bytes
19743 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19744 if (ctx->variant_part_offset == NULL_TREE)
19746 *cst_offset = object_offset_in_bytes.to_shwi ();
19747 return NULL;
19749 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19751 else
19752 tree_result = byte_position (decl);
19754 if (ctx->variant_part_offset != NULL_TREE)
19755 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19756 ctx->variant_part_offset, tree_result);
19758 /* If the byte offset is a constant, it's simplier to handle a native
19759 constant rather than a DWARF expression. */
19760 if (TREE_CODE (tree_result) == INTEGER_CST)
19762 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19763 return NULL;
19766 struct loc_descr_context loc_ctx = {
19767 ctx->struct_type, /* context_type */
19768 NULL_TREE, /* base_decl */
19769 NULL, /* dpi */
19770 false, /* placeholder_arg */
19771 false, /* placeholder_seen */
19772 false /* strict_signedness */
19774 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19776 /* We want a DWARF expression: abort if we only have a location list with
19777 multiple elements. */
19778 if (!loc_result || !single_element_loc_list_p (loc_result))
19779 return NULL;
19780 else
19781 return loc_result->expr;
19784 /* The following routines define various Dwarf attributes and any data
19785 associated with them. */
19787 /* Add a location description attribute value to a DIE.
19789 This emits location attributes suitable for whole variables and
19790 whole parameters. Note that the location attributes for struct fields are
19791 generated by the routine `data_member_location_attribute' below. */
19793 static inline void
19794 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19795 dw_loc_list_ref descr)
19797 bool check_no_locviews = true;
19798 if (descr == 0)
19799 return;
19800 if (single_element_loc_list_p (descr))
19801 add_AT_loc (die, attr_kind, descr->expr);
19802 else
19804 add_AT_loc_list (die, attr_kind, descr);
19805 gcc_assert (descr->ll_symbol);
19806 if (attr_kind == DW_AT_location && descr->vl_symbol
19807 && dwarf2out_locviews_in_attribute ())
19809 add_AT_view_list (die, DW_AT_GNU_locviews);
19810 check_no_locviews = false;
19814 if (check_no_locviews)
19815 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19818 /* Add DW_AT_accessibility attribute to DIE if needed. */
19820 static void
19821 add_accessibility_attribute (dw_die_ref die, tree decl)
19823 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19824 children, otherwise the default is DW_ACCESS_public. In DWARF2
19825 the default has always been DW_ACCESS_public. */
19826 if (TREE_PROTECTED (decl))
19827 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19828 else if (TREE_PRIVATE (decl))
19830 if (dwarf_version == 2
19831 || die->die_parent == NULL
19832 || die->die_parent->die_tag != DW_TAG_class_type)
19833 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19835 else if (dwarf_version > 2
19836 && die->die_parent
19837 && die->die_parent->die_tag == DW_TAG_class_type)
19838 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19841 /* Attach the specialized form of location attribute used for data members of
19842 struct and union types. In the special case of a FIELD_DECL node which
19843 represents a bit-field, the "offset" part of this special location
19844 descriptor must indicate the distance in bytes from the lowest-addressed
19845 byte of the containing struct or union type to the lowest-addressed byte of
19846 the "containing object" for the bit-field. (See the `field_byte_offset'
19847 function above).
19849 For any given bit-field, the "containing object" is a hypothetical object
19850 (of some integral or enum type) within which the given bit-field lives. The
19851 type of this hypothetical "containing object" is always the same as the
19852 declared type of the individual bit-field itself (for GCC anyway... the
19853 DWARF spec doesn't actually mandate this). Note that it is the size (in
19854 bytes) of the hypothetical "containing object" which will be given in the
19855 DW_AT_byte_size attribute for this bit-field. (See the
19856 `byte_size_attribute' function below.) It is also used when calculating the
19857 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19858 function below.)
19860 CTX is required: see the comment for VLR_CONTEXT. */
19862 static void
19863 add_data_member_location_attribute (dw_die_ref die,
19864 tree decl,
19865 struct vlr_context *ctx)
19867 HOST_WIDE_INT offset;
19868 dw_loc_descr_ref loc_descr = 0;
19870 if (TREE_CODE (decl) == TREE_BINFO)
19872 /* We're working on the TAG_inheritance for a base class. */
19873 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19875 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19876 aren't at a fixed offset from all (sub)objects of the same
19877 type. We need to extract the appropriate offset from our
19878 vtable. The following dwarf expression means
19880 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19882 This is specific to the V3 ABI, of course. */
19884 dw_loc_descr_ref tmp;
19886 /* Make a copy of the object address. */
19887 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19888 add_loc_descr (&loc_descr, tmp);
19890 /* Extract the vtable address. */
19891 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19892 add_loc_descr (&loc_descr, tmp);
19894 /* Calculate the address of the offset. */
19895 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19896 gcc_assert (offset < 0);
19898 tmp = int_loc_descriptor (-offset);
19899 add_loc_descr (&loc_descr, tmp);
19900 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19901 add_loc_descr (&loc_descr, tmp);
19903 /* Extract the offset. */
19904 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19905 add_loc_descr (&loc_descr, tmp);
19907 /* Add it to the object address. */
19908 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19909 add_loc_descr (&loc_descr, tmp);
19911 else
19912 offset = tree_to_shwi (BINFO_OFFSET (decl));
19914 else
19916 loc_descr = field_byte_offset (decl, ctx, &offset);
19918 if (!loc_descr)
19921 /* If loc_descr is available, then we know the offset is dynamic. */
19922 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
19924 loc_descr = NULL;
19925 offset = 0;
19928 /* Data member location evaluation starts with the base address on the
19929 stack. Compute the field offset and add it to this base address. */
19930 else
19931 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19934 if (!loc_descr)
19936 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19937 e.g. GDB only added support to it in November 2016. For DWARF5
19938 we need newer debug info consumers anyway. We might change this
19939 to dwarf_version >= 4 once most consumers catched up. */
19940 if (dwarf_version >= 5
19941 && TREE_CODE (decl) == FIELD_DECL
19942 && DECL_BIT_FIELD_TYPE (decl)
19943 && (ctx->variant_part_offset == NULL_TREE
19944 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19946 tree off = bit_position (decl);
19947 if (ctx->variant_part_offset)
19948 off = bit_from_pos (ctx->variant_part_offset, off);
19949 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19951 remove_AT (die, DW_AT_byte_size);
19952 remove_AT (die, DW_AT_bit_offset);
19953 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19954 return;
19957 if (dwarf_version > 2)
19959 /* Don't need to output a location expression, just the constant. */
19960 if (offset < 0)
19961 add_AT_int (die, DW_AT_data_member_location, offset);
19962 else
19963 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19964 return;
19966 else
19968 enum dwarf_location_atom op;
19970 /* The DWARF2 standard says that we should assume that the structure
19971 address is already on the stack, so we can specify a structure
19972 field address by using DW_OP_plus_uconst. */
19973 op = DW_OP_plus_uconst;
19974 loc_descr = new_loc_descr (op, offset, 0);
19978 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19981 /* Writes integer values to dw_vec_const array. */
19983 static void
19984 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19986 while (size != 0)
19988 *dest++ = val & 0xff;
19989 val >>= 8;
19990 --size;
19994 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19996 static HOST_WIDE_INT
19997 extract_int (const unsigned char *src, unsigned int size)
19999 HOST_WIDE_INT val = 0;
20001 src += size;
20002 while (size != 0)
20004 val <<= 8;
20005 val |= *--src & 0xff;
20006 --size;
20008 return val;
20011 /* Writes wide_int values to dw_vec_const array. */
20013 static void
20014 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20016 int i;
20018 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20020 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20021 return;
20024 /* We'd have to extend this code to support odd sizes. */
20025 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20027 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20029 if (WORDS_BIG_ENDIAN)
20030 for (i = n - 1; i >= 0; i--)
20032 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20033 dest += sizeof (HOST_WIDE_INT);
20035 else
20036 for (i = 0; i < n; i++)
20038 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20039 dest += sizeof (HOST_WIDE_INT);
20043 /* Writes floating point values to dw_vec_const array. */
20045 static unsigned
20046 insert_float (const_rtx rtl, unsigned char *array)
20048 long val[4];
20049 int i;
20050 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20052 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20054 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20055 if (GET_MODE_SIZE (mode) < 4)
20057 gcc_assert (GET_MODE_SIZE (mode) == 2);
20058 insert_int (val[0], 2, array);
20059 return 2;
20062 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20064 insert_int (val[i], 4, array);
20065 array += 4;
20067 return 4;
20070 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20071 does not have a "location" either in memory or in a register. These
20072 things can arise in GNU C when a constant is passed as an actual parameter
20073 to an inlined function. They can also arise in C++ where declared
20074 constants do not necessarily get memory "homes". */
20076 static bool
20077 add_const_value_attribute (dw_die_ref die, rtx rtl)
20079 switch (GET_CODE (rtl))
20081 case CONST_INT:
20083 HOST_WIDE_INT val = INTVAL (rtl);
20085 if (val < 0)
20086 add_AT_int (die, DW_AT_const_value, val);
20087 else
20088 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20090 return true;
20092 case CONST_WIDE_INT:
20094 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
20095 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
20096 (unsigned int) CONST_WIDE_INT_NUNITS (rtl)
20097 * HOST_BITS_PER_WIDE_INT);
20098 wide_int w = wide_int::from (w1, prec, UNSIGNED);
20099 add_AT_wide (die, DW_AT_const_value, w);
20101 return true;
20103 case CONST_DOUBLE:
20104 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20105 floating-point constant. A CONST_DOUBLE is used whenever the
20106 constant requires more than one word in order to be adequately
20107 represented. */
20108 if (TARGET_SUPPORTS_WIDE_INT == 0
20109 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20110 add_AT_double (die, DW_AT_const_value,
20111 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20112 else
20114 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20115 unsigned int length = GET_MODE_SIZE (mode);
20116 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20117 unsigned int elt_size = insert_float (rtl, array);
20119 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20120 array);
20122 return true;
20124 case CONST_VECTOR:
20126 unsigned int length;
20127 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20128 return false;
20130 machine_mode mode = GET_MODE (rtl);
20131 /* The combination of a length and byte elt_size doesn't extend
20132 naturally to boolean vectors, where several elements are packed
20133 into the same byte. */
20134 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20135 return false;
20137 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20138 unsigned char *array
20139 = ggc_vec_alloc<unsigned char> (length * elt_size);
20140 unsigned int i;
20141 unsigned char *p;
20142 machine_mode imode = GET_MODE_INNER (mode);
20144 switch (GET_MODE_CLASS (mode))
20146 case MODE_VECTOR_INT:
20147 for (i = 0, p = array; i < length; i++, p += elt_size)
20149 rtx elt = CONST_VECTOR_ELT (rtl, i);
20150 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20152 break;
20154 case MODE_VECTOR_FLOAT:
20155 for (i = 0, p = array; i < length; i++, p += elt_size)
20157 rtx elt = CONST_VECTOR_ELT (rtl, i);
20158 insert_float (elt, p);
20160 break;
20162 default:
20163 gcc_unreachable ();
20166 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20168 return true;
20170 case CONST_STRING:
20171 if (dwarf_version >= 4 || !dwarf_strict)
20173 dw_loc_descr_ref loc_result;
20174 resolve_one_addr (&rtl);
20175 rtl_addr:
20176 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20177 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20178 add_AT_loc (die, DW_AT_location, loc_result);
20179 vec_safe_push (used_rtx_array, rtl);
20180 return true;
20182 return false;
20184 case CONST:
20185 if (CONSTANT_P (XEXP (rtl, 0)))
20186 return add_const_value_attribute (die, XEXP (rtl, 0));
20187 /* FALLTHROUGH */
20188 case SYMBOL_REF:
20189 if (!const_ok_for_output (rtl))
20190 return false;
20191 /* FALLTHROUGH */
20192 case LABEL_REF:
20193 if (dwarf_version >= 4 || !dwarf_strict)
20194 goto rtl_addr;
20195 return false;
20197 case PLUS:
20198 /* In cases where an inlined instance of an inline function is passed
20199 the address of an `auto' variable (which is local to the caller) we
20200 can get a situation where the DECL_RTL of the artificial local
20201 variable (for the inlining) which acts as a stand-in for the
20202 corresponding formal parameter (of the inline function) will look
20203 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20204 exactly a compile-time constant expression, but it isn't the address
20205 of the (artificial) local variable either. Rather, it represents the
20206 *value* which the artificial local variable always has during its
20207 lifetime. We currently have no way to represent such quasi-constant
20208 values in Dwarf, so for now we just punt and generate nothing. */
20209 return false;
20211 case HIGH:
20212 case CONST_FIXED:
20213 case MINUS:
20214 case SIGN_EXTEND:
20215 case ZERO_EXTEND:
20216 case CONST_POLY_INT:
20217 return false;
20219 case MEM:
20220 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20221 && MEM_READONLY_P (rtl)
20222 && GET_MODE (rtl) == BLKmode)
20224 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20225 return true;
20227 return false;
20229 default:
20230 /* No other kinds of rtx should be possible here. */
20231 gcc_unreachable ();
20233 return false;
20236 /* Determine whether the evaluation of EXPR references any variables
20237 or functions which aren't otherwise used (and therefore may not be
20238 output). */
20239 static tree
20240 reference_to_unused (tree * tp, int * walk_subtrees,
20241 void * data ATTRIBUTE_UNUSED)
20243 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20244 *walk_subtrees = 0;
20246 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20247 && ! TREE_ASM_WRITTEN (*tp))
20248 return *tp;
20249 /* ??? The C++ FE emits debug information for using decls, so
20250 putting gcc_unreachable here falls over. See PR31899. For now
20251 be conservative. */
20252 else if (!symtab->global_info_ready && VAR_P (*tp))
20253 return *tp;
20254 else if (VAR_P (*tp))
20256 varpool_node *node = varpool_node::get (*tp);
20257 if (!node || !node->definition)
20258 return *tp;
20260 else if (TREE_CODE (*tp) == FUNCTION_DECL
20261 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20263 /* The call graph machinery must have finished analyzing,
20264 optimizing and gimplifying the CU by now.
20265 So if *TP has no call graph node associated
20266 to it, it means *TP will not be emitted. */
20267 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20268 return *tp;
20270 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20271 return *tp;
20273 return NULL_TREE;
20276 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20277 for use in a later add_const_value_attribute call. */
20279 static rtx
20280 rtl_for_decl_init (tree init, tree type)
20282 rtx rtl = NULL_RTX;
20284 STRIP_NOPS (init);
20286 /* If a variable is initialized with a string constant without embedded
20287 zeros, build CONST_STRING. */
20288 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20290 tree enttype = TREE_TYPE (type);
20291 tree domain = TYPE_DOMAIN (type);
20292 scalar_int_mode mode;
20294 if (is_int_mode (TYPE_MODE (enttype), &mode)
20295 && GET_MODE_SIZE (mode) == 1
20296 && domain
20297 && TYPE_MAX_VALUE (domain)
20298 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20299 && integer_zerop (TYPE_MIN_VALUE (domain))
20300 && compare_tree_int (TYPE_MAX_VALUE (domain),
20301 TREE_STRING_LENGTH (init) - 1) == 0
20302 && ((size_t) TREE_STRING_LENGTH (init)
20303 == strlen (TREE_STRING_POINTER (init)) + 1))
20305 rtl = gen_rtx_CONST_STRING (VOIDmode,
20306 ggc_strdup (TREE_STRING_POINTER (init)));
20307 rtl = gen_rtx_MEM (BLKmode, rtl);
20308 MEM_READONLY_P (rtl) = 1;
20311 /* Other aggregates, and complex values, could be represented using
20312 CONCAT: FIXME! */
20313 else if (AGGREGATE_TYPE_P (type)
20314 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20315 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20316 || TREE_CODE (type) == COMPLEX_TYPE)
20318 /* Vectors only work if their mode is supported by the target.
20319 FIXME: generic vectors ought to work too. */
20320 else if (TREE_CODE (type) == VECTOR_TYPE
20321 && !VECTOR_MODE_P (TYPE_MODE (type)))
20323 /* If the initializer is something that we know will expand into an
20324 immediate RTL constant, expand it now. We must be careful not to
20325 reference variables which won't be output. */
20326 else if (initializer_constant_valid_p (init, type)
20327 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20329 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20330 possible. */
20331 if (TREE_CODE (type) == VECTOR_TYPE)
20332 switch (TREE_CODE (init))
20334 case VECTOR_CST:
20335 break;
20336 case CONSTRUCTOR:
20337 if (TREE_CONSTANT (init))
20339 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20340 bool constant_p = true;
20341 tree value;
20342 unsigned HOST_WIDE_INT ix;
20344 /* Even when ctor is constant, it might contain non-*_CST
20345 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20346 belong into VECTOR_CST nodes. */
20347 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20348 if (!CONSTANT_CLASS_P (value))
20350 constant_p = false;
20351 break;
20354 if (constant_p)
20356 init = build_vector_from_ctor (type, elts);
20357 break;
20360 /* FALLTHRU */
20362 default:
20363 return NULL;
20366 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20368 /* If expand_expr returns a MEM, it wasn't immediate. */
20369 gcc_assert (!rtl || !MEM_P (rtl));
20372 return rtl;
20375 /* Generate RTL for the variable DECL to represent its location. */
20377 static rtx
20378 rtl_for_decl_location (tree decl)
20380 rtx rtl;
20382 /* Here we have to decide where we are going to say the parameter "lives"
20383 (as far as the debugger is concerned). We only have a couple of
20384 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20386 DECL_RTL normally indicates where the parameter lives during most of the
20387 activation of the function. If optimization is enabled however, this
20388 could be either NULL or else a pseudo-reg. Both of those cases indicate
20389 that the parameter doesn't really live anywhere (as far as the code
20390 generation parts of GCC are concerned) during most of the function's
20391 activation. That will happen (for example) if the parameter is never
20392 referenced within the function.
20394 We could just generate a location descriptor here for all non-NULL
20395 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20396 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20397 where DECL_RTL is NULL or is a pseudo-reg.
20399 Note however that we can only get away with using DECL_INCOMING_RTL as
20400 a backup substitute for DECL_RTL in certain limited cases. In cases
20401 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20402 we can be sure that the parameter was passed using the same type as it is
20403 declared to have within the function, and that its DECL_INCOMING_RTL
20404 points us to a place where a value of that type is passed.
20406 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20407 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20408 because in these cases DECL_INCOMING_RTL points us to a value of some
20409 type which is *different* from the type of the parameter itself. Thus,
20410 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20411 such cases, the debugger would end up (for example) trying to fetch a
20412 `float' from a place which actually contains the first part of a
20413 `double'. That would lead to really incorrect and confusing
20414 output at debug-time.
20416 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20417 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20418 are a couple of exceptions however. On little-endian machines we can
20419 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20420 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20421 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20422 when (on a little-endian machine) a non-prototyped function has a
20423 parameter declared to be of type `short' or `char'. In such cases,
20424 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20425 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20426 passed `int' value. If the debugger then uses that address to fetch
20427 a `short' or a `char' (on a little-endian machine) the result will be
20428 the correct data, so we allow for such exceptional cases below.
20430 Note that our goal here is to describe the place where the given formal
20431 parameter lives during most of the function's activation (i.e. between the
20432 end of the prologue and the start of the epilogue). We'll do that as best
20433 as we can. Note however that if the given formal parameter is modified
20434 sometime during the execution of the function, then a stack backtrace (at
20435 debug-time) will show the function as having been called with the *new*
20436 value rather than the value which was originally passed in. This happens
20437 rarely enough that it is not a major problem, but it *is* a problem, and
20438 I'd like to fix it.
20440 A future version of dwarf2out.c may generate two additional attributes for
20441 any given DW_TAG_formal_parameter DIE which will describe the "passed
20442 type" and the "passed location" for the given formal parameter in addition
20443 to the attributes we now generate to indicate the "declared type" and the
20444 "active location" for each parameter. This additional set of attributes
20445 could be used by debuggers for stack backtraces. Separately, note that
20446 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20447 This happens (for example) for inlined-instances of inline function formal
20448 parameters which are never referenced. This really shouldn't be
20449 happening. All PARM_DECL nodes should get valid non-NULL
20450 DECL_INCOMING_RTL values. FIXME. */
20452 /* Use DECL_RTL as the "location" unless we find something better. */
20453 rtl = DECL_RTL_IF_SET (decl);
20455 /* When generating abstract instances, ignore everything except
20456 constants, symbols living in memory, and symbols living in
20457 fixed registers. */
20458 if (! reload_completed)
20460 if (rtl
20461 && (CONSTANT_P (rtl)
20462 || (MEM_P (rtl)
20463 && CONSTANT_P (XEXP (rtl, 0)))
20464 || (REG_P (rtl)
20465 && VAR_P (decl)
20466 && TREE_STATIC (decl))))
20468 rtl = targetm.delegitimize_address (rtl);
20469 return rtl;
20471 rtl = NULL_RTX;
20473 else if (TREE_CODE (decl) == PARM_DECL)
20475 if (rtl == NULL_RTX
20476 || is_pseudo_reg (rtl)
20477 || (MEM_P (rtl)
20478 && is_pseudo_reg (XEXP (rtl, 0))
20479 && DECL_INCOMING_RTL (decl)
20480 && MEM_P (DECL_INCOMING_RTL (decl))
20481 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20483 tree declared_type = TREE_TYPE (decl);
20484 tree passed_type = DECL_ARG_TYPE (decl);
20485 machine_mode dmode = TYPE_MODE (declared_type);
20486 machine_mode pmode = TYPE_MODE (passed_type);
20488 /* This decl represents a formal parameter which was optimized out.
20489 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20490 all cases where (rtl == NULL_RTX) just below. */
20491 if (dmode == pmode)
20492 rtl = DECL_INCOMING_RTL (decl);
20493 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20494 && SCALAR_INT_MODE_P (dmode)
20495 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20496 && DECL_INCOMING_RTL (decl))
20498 rtx inc = DECL_INCOMING_RTL (decl);
20499 if (REG_P (inc))
20500 rtl = inc;
20501 else if (MEM_P (inc))
20503 if (BYTES_BIG_ENDIAN)
20504 rtl = adjust_address_nv (inc, dmode,
20505 GET_MODE_SIZE (pmode)
20506 - GET_MODE_SIZE (dmode));
20507 else
20508 rtl = inc;
20513 /* If the parm was passed in registers, but lives on the stack, then
20514 make a big endian correction if the mode of the type of the
20515 parameter is not the same as the mode of the rtl. */
20516 /* ??? This is the same series of checks that are made in dbxout.c before
20517 we reach the big endian correction code there. It isn't clear if all
20518 of these checks are necessary here, but keeping them all is the safe
20519 thing to do. */
20520 else if (MEM_P (rtl)
20521 && XEXP (rtl, 0) != const0_rtx
20522 && ! CONSTANT_P (XEXP (rtl, 0))
20523 /* Not passed in memory. */
20524 && !MEM_P (DECL_INCOMING_RTL (decl))
20525 /* Not passed by invisible reference. */
20526 && (!REG_P (XEXP (rtl, 0))
20527 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20528 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20529 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20530 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20531 #endif
20533 /* Big endian correction check. */
20534 && BYTES_BIG_ENDIAN
20535 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20536 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20537 UNITS_PER_WORD))
20539 machine_mode addr_mode = get_address_mode (rtl);
20540 poly_int64 offset = (UNITS_PER_WORD
20541 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20543 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20544 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20547 else if (VAR_P (decl)
20548 && rtl
20549 && MEM_P (rtl)
20550 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20552 machine_mode addr_mode = get_address_mode (rtl);
20553 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20554 GET_MODE (rtl));
20556 /* If a variable is declared "register" yet is smaller than
20557 a register, then if we store the variable to memory, it
20558 looks like we're storing a register-sized value, when in
20559 fact we are not. We need to adjust the offset of the
20560 storage location to reflect the actual value's bytes,
20561 else gdb will not be able to display it. */
20562 if (maybe_ne (offset, 0))
20563 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20564 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20567 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20568 and will have been substituted directly into all expressions that use it.
20569 C does not have such a concept, but C++ and other languages do. */
20570 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20571 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20573 if (rtl)
20574 rtl = targetm.delegitimize_address (rtl);
20576 /* If we don't look past the constant pool, we risk emitting a
20577 reference to a constant pool entry that isn't referenced from
20578 code, and thus is not emitted. */
20579 if (rtl)
20580 rtl = avoid_constant_pool_reference (rtl);
20582 /* Try harder to get a rtl. If this symbol ends up not being emitted
20583 in the current CU, resolve_addr will remove the expression referencing
20584 it. */
20585 if (rtl == NULL_RTX
20586 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20587 && VAR_P (decl)
20588 && !DECL_EXTERNAL (decl)
20589 && TREE_STATIC (decl)
20590 && DECL_NAME (decl)
20591 && !DECL_HARD_REGISTER (decl)
20592 && DECL_MODE (decl) != VOIDmode)
20594 rtl = make_decl_rtl_for_debug (decl);
20595 if (!MEM_P (rtl)
20596 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20597 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20598 rtl = NULL_RTX;
20601 return rtl;
20604 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20605 returned. If so, the decl for the COMMON block is returned, and the
20606 value is the offset into the common block for the symbol. */
20608 static tree
20609 fortran_common (tree decl, HOST_WIDE_INT *value)
20611 tree val_expr, cvar;
20612 machine_mode mode;
20613 poly_int64 bitsize, bitpos;
20614 tree offset;
20615 HOST_WIDE_INT cbitpos;
20616 int unsignedp, reversep, volatilep = 0;
20618 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20619 it does not have a value (the offset into the common area), or if it
20620 is thread local (as opposed to global) then it isn't common, and shouldn't
20621 be handled as such. */
20622 if (!VAR_P (decl)
20623 || !TREE_STATIC (decl)
20624 || !DECL_HAS_VALUE_EXPR_P (decl)
20625 || !is_fortran ())
20626 return NULL_TREE;
20628 val_expr = DECL_VALUE_EXPR (decl);
20629 if (TREE_CODE (val_expr) != COMPONENT_REF)
20630 return NULL_TREE;
20632 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20633 &unsignedp, &reversep, &volatilep);
20635 if (cvar == NULL_TREE
20636 || !VAR_P (cvar)
20637 || DECL_ARTIFICIAL (cvar)
20638 || !TREE_PUBLIC (cvar)
20639 /* We don't expect to have to cope with variable offsets,
20640 since at present all static data must have a constant size. */
20641 || !bitpos.is_constant (&cbitpos))
20642 return NULL_TREE;
20644 *value = 0;
20645 if (offset != NULL)
20647 if (!tree_fits_shwi_p (offset))
20648 return NULL_TREE;
20649 *value = tree_to_shwi (offset);
20651 if (cbitpos != 0)
20652 *value += cbitpos / BITS_PER_UNIT;
20654 return cvar;
20657 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20658 data attribute for a variable or a parameter. We generate the
20659 DW_AT_const_value attribute only in those cases where the given variable
20660 or parameter does not have a true "location" either in memory or in a
20661 register. This can happen (for example) when a constant is passed as an
20662 actual argument in a call to an inline function. (It's possible that
20663 these things can crop up in other ways also.) Note that one type of
20664 constant value which can be passed into an inlined function is a constant
20665 pointer. This can happen for example if an actual argument in an inlined
20666 function call evaluates to a compile-time constant address.
20668 CACHE_P is true if it is worth caching the location list for DECL,
20669 so that future calls can reuse it rather than regenerate it from scratch.
20670 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20671 since we will need to refer to them each time the function is inlined. */
20673 static bool
20674 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20676 rtx rtl;
20677 dw_loc_list_ref list;
20678 var_loc_list *loc_list;
20679 cached_dw_loc_list *cache;
20681 if (early_dwarf)
20682 return false;
20684 if (TREE_CODE (decl) == ERROR_MARK)
20685 return false;
20687 if (get_AT (die, DW_AT_location)
20688 || get_AT (die, DW_AT_const_value))
20689 return true;
20691 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20692 || TREE_CODE (decl) == RESULT_DECL);
20694 /* Try to get some constant RTL for this decl, and use that as the value of
20695 the location. */
20697 rtl = rtl_for_decl_location (decl);
20698 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20699 && add_const_value_attribute (die, rtl))
20700 return true;
20702 /* See if we have single element location list that is equivalent to
20703 a constant value. That way we are better to use add_const_value_attribute
20704 rather than expanding constant value equivalent. */
20705 loc_list = lookup_decl_loc (decl);
20706 if (loc_list
20707 && loc_list->first
20708 && loc_list->first->next == NULL
20709 && NOTE_P (loc_list->first->loc)
20710 && NOTE_VAR_LOCATION (loc_list->first->loc)
20711 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20713 struct var_loc_node *node;
20715 node = loc_list->first;
20716 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20717 if (GET_CODE (rtl) == EXPR_LIST)
20718 rtl = XEXP (rtl, 0);
20719 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20720 && add_const_value_attribute (die, rtl))
20721 return true;
20723 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20724 list several times. See if we've already cached the contents. */
20725 list = NULL;
20726 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20727 cache_p = false;
20728 if (cache_p)
20730 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20731 if (cache)
20732 list = cache->loc_list;
20734 if (list == NULL)
20736 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20737 NULL);
20738 /* It is usually worth caching this result if the decl is from
20739 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20740 if (cache_p && list && list->dw_loc_next)
20742 cached_dw_loc_list **slot
20743 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20744 DECL_UID (decl),
20745 INSERT);
20746 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20747 cache->decl_id = DECL_UID (decl);
20748 cache->loc_list = list;
20749 *slot = cache;
20752 if (list)
20754 add_AT_location_description (die, DW_AT_location, list);
20755 return true;
20757 /* None of that worked, so it must not really have a location;
20758 try adding a constant value attribute from the DECL_INITIAL. */
20759 return tree_add_const_value_attribute_for_decl (die, decl);
20762 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20763 attribute is the const value T. */
20765 static bool
20766 tree_add_const_value_attribute (dw_die_ref die, tree t)
20768 tree init;
20769 tree type = TREE_TYPE (t);
20770 rtx rtl;
20772 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20773 return false;
20775 init = t;
20776 gcc_assert (!DECL_P (init));
20778 if (TREE_CODE (init) == INTEGER_CST)
20780 if (tree_fits_uhwi_p (init))
20782 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20783 return true;
20785 if (tree_fits_shwi_p (init))
20787 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20788 return true;
20791 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20792 symbols. */
20793 rtl = rtl_for_decl_init (init, type);
20794 if (rtl && !early_dwarf)
20795 return add_const_value_attribute (die, rtl);
20796 /* If the host and target are sane, try harder. */
20797 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20798 && initializer_constant_valid_p (init, type))
20800 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20801 if (size > 0 && (int) size == size)
20803 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20805 if (native_encode_initializer (init, array, size) == size)
20807 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20808 return true;
20810 ggc_free (array);
20813 return false;
20816 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20817 attribute is the const value of T, where T is an integral constant
20818 variable with static storage duration
20819 (so it can't be a PARM_DECL or a RESULT_DECL). */
20821 static bool
20822 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20825 if (!decl
20826 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20827 || (VAR_P (decl) && !TREE_STATIC (decl)))
20828 return false;
20830 if (TREE_READONLY (decl)
20831 && ! TREE_THIS_VOLATILE (decl)
20832 && DECL_INITIAL (decl))
20833 /* OK */;
20834 else
20835 return false;
20837 /* Don't add DW_AT_const_value if abstract origin already has one. */
20838 if (get_AT (var_die, DW_AT_const_value))
20839 return false;
20841 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20844 /* Convert the CFI instructions for the current function into a
20845 location list. This is used for DW_AT_frame_base when we targeting
20846 a dwarf2 consumer that does not support the dwarf3
20847 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20848 expressions. */
20850 static dw_loc_list_ref
20851 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20853 int ix;
20854 dw_fde_ref fde;
20855 dw_loc_list_ref list, *list_tail;
20856 dw_cfi_ref cfi;
20857 dw_cfa_location last_cfa, next_cfa;
20858 const char *start_label, *last_label, *section;
20859 dw_cfa_location remember;
20861 fde = cfun->fde;
20862 gcc_assert (fde != NULL);
20864 section = secname_for_decl (current_function_decl);
20865 list_tail = &list;
20866 list = NULL;
20868 memset (&next_cfa, 0, sizeof (next_cfa));
20869 next_cfa.reg = INVALID_REGNUM;
20870 remember = next_cfa;
20872 start_label = fde->dw_fde_begin;
20874 /* ??? Bald assumption that the CIE opcode list does not contain
20875 advance opcodes. */
20876 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20877 lookup_cfa_1 (cfi, &next_cfa, &remember);
20879 last_cfa = next_cfa;
20880 last_label = start_label;
20882 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20884 /* If the first partition contained no CFI adjustments, the
20885 CIE opcodes apply to the whole first partition. */
20886 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20887 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20888 list_tail =&(*list_tail)->dw_loc_next;
20889 start_label = last_label = fde->dw_fde_second_begin;
20892 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20894 switch (cfi->dw_cfi_opc)
20896 case DW_CFA_set_loc:
20897 case DW_CFA_advance_loc1:
20898 case DW_CFA_advance_loc2:
20899 case DW_CFA_advance_loc4:
20900 if (!cfa_equal_p (&last_cfa, &next_cfa))
20902 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20903 start_label, 0, last_label, 0, section);
20905 list_tail = &(*list_tail)->dw_loc_next;
20906 last_cfa = next_cfa;
20907 start_label = last_label;
20909 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20910 break;
20912 case DW_CFA_advance_loc:
20913 /* The encoding is complex enough that we should never emit this. */
20914 gcc_unreachable ();
20916 default:
20917 lookup_cfa_1 (cfi, &next_cfa, &remember);
20918 break;
20920 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20922 if (!cfa_equal_p (&last_cfa, &next_cfa))
20924 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20925 start_label, 0, last_label, 0, section);
20927 list_tail = &(*list_tail)->dw_loc_next;
20928 last_cfa = next_cfa;
20929 start_label = last_label;
20931 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20932 start_label, 0, fde->dw_fde_end, 0, section);
20933 list_tail = &(*list_tail)->dw_loc_next;
20934 start_label = last_label = fde->dw_fde_second_begin;
20938 if (!cfa_equal_p (&last_cfa, &next_cfa))
20940 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20941 start_label, 0, last_label, 0, section);
20942 list_tail = &(*list_tail)->dw_loc_next;
20943 start_label = last_label;
20946 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20947 start_label, 0,
20948 fde->dw_fde_second_begin
20949 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20950 section);
20952 maybe_gen_llsym (list);
20954 return list;
20957 /* Compute a displacement from the "steady-state frame pointer" to the
20958 frame base (often the same as the CFA), and store it in
20959 frame_pointer_fb_offset. OFFSET is added to the displacement
20960 before the latter is negated. */
20962 static void
20963 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20965 rtx reg, elim;
20967 #ifdef FRAME_POINTER_CFA_OFFSET
20968 reg = frame_pointer_rtx;
20969 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20970 #else
20971 reg = arg_pointer_rtx;
20972 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20973 #endif
20975 elim = (ira_use_lra_p
20976 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20977 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20978 elim = strip_offset_and_add (elim, &offset);
20980 frame_pointer_fb_offset = -offset;
20982 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20983 in which to eliminate. This is because it's stack pointer isn't
20984 directly accessible as a register within the ISA. To work around
20985 this, assume that while we cannot provide a proper value for
20986 frame_pointer_fb_offset, we won't need one either. We can use
20987 hard frame pointer in debug info even if frame pointer isn't used
20988 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20989 which uses the DW_AT_frame_base attribute, not hard frame pointer
20990 directly. */
20991 frame_pointer_fb_offset_valid
20992 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20995 /* Generate a DW_AT_name attribute given some string value to be included as
20996 the value of the attribute. */
20998 void
20999 add_name_attribute (dw_die_ref die, const char *name_string)
21001 if (name_string != NULL && *name_string != 0)
21003 if (demangle_name_func)
21004 name_string = (*demangle_name_func) (name_string);
21006 add_AT_string (die, DW_AT_name, name_string);
21010 /* Generate a DW_AT_name attribute given some string value representing a
21011 file or filepath to be included as value of the attribute. */
21012 static void
21013 add_filename_attribute (dw_die_ref die, const char *name_string)
21015 if (name_string != NULL && *name_string != 0)
21016 add_filepath_AT_string (die, DW_AT_name, name_string);
21019 /* Generate a DW_AT_description attribute given some string value to be included
21020 as the value of the attribute. */
21022 static void
21023 add_desc_attribute (dw_die_ref die, const char *name_string)
21025 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21026 return;
21028 if (name_string == NULL || *name_string == 0)
21029 return;
21031 if (demangle_name_func)
21032 name_string = (*demangle_name_func) (name_string);
21034 add_AT_string (die, DW_AT_description, name_string);
21037 /* Generate a DW_AT_description attribute given some decl to be included
21038 as the value of the attribute. */
21040 static void
21041 add_desc_attribute (dw_die_ref die, tree decl)
21043 tree decl_name;
21045 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21046 return;
21048 if (decl == NULL_TREE || !DECL_P (decl))
21049 return;
21050 decl_name = DECL_NAME (decl);
21052 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21054 const char *name = dwarf2_name (decl, 0);
21055 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21057 else
21059 char *desc = print_generic_expr_to_str (decl);
21060 add_desc_attribute (die, desc);
21061 free (desc);
21065 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21066 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21067 of TYPE accordingly.
21069 ??? This is a temporary measure until after we're able to generate
21070 regular DWARF for the complex Ada type system. */
21072 static void
21073 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21074 dw_die_ref context_die)
21076 tree dtype;
21077 dw_die_ref dtype_die;
21079 if (!lang_hooks.types.descriptive_type)
21080 return;
21082 dtype = lang_hooks.types.descriptive_type (type);
21083 if (!dtype)
21084 return;
21086 dtype_die = lookup_type_die (dtype);
21087 if (!dtype_die)
21089 gen_type_die (dtype, context_die);
21090 dtype_die = lookup_type_die (dtype);
21091 gcc_assert (dtype_die);
21094 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21097 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21099 static const char *
21100 comp_dir_string (void)
21102 const char *wd;
21103 char *wd_plus_sep = NULL;
21104 static const char *cached_wd = NULL;
21106 if (cached_wd != NULL)
21107 return cached_wd;
21109 wd = get_src_pwd ();
21110 if (wd == NULL)
21111 return NULL;
21113 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21115 size_t wdlen = strlen (wd);
21116 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21117 strcpy (wd_plus_sep, wd);
21118 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21119 wd_plus_sep [wdlen + 1] = 0;
21120 wd = wd_plus_sep;
21123 cached_wd = remap_debug_filename (wd);
21125 /* remap_debug_filename can just pass through wd or return a new gc string.
21126 These two types can't be both stored in a GTY(())-tagged string, but since
21127 the cached value lives forever just copy it if needed. */
21128 if (cached_wd != wd)
21130 cached_wd = xstrdup (cached_wd);
21131 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21132 free (wd_plus_sep);
21135 return cached_wd;
21138 /* Generate a DW_AT_comp_dir attribute for DIE. */
21140 static void
21141 add_comp_dir_attribute (dw_die_ref die)
21143 const char * wd = comp_dir_string ();
21144 if (wd != NULL)
21145 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21148 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21149 pointer computation, ...), output a representation for that bound according
21150 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21151 loc_list_from_tree for the meaning of CONTEXT. */
21153 static void
21154 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21155 int forms, struct loc_descr_context *context)
21157 dw_die_ref context_die, decl_die = NULL;
21158 dw_loc_list_ref list;
21159 bool strip_conversions = true;
21160 bool placeholder_seen = false;
21162 while (strip_conversions)
21163 switch (TREE_CODE (value))
21165 case ERROR_MARK:
21166 case SAVE_EXPR:
21167 return;
21169 CASE_CONVERT:
21170 case VIEW_CONVERT_EXPR:
21171 value = TREE_OPERAND (value, 0);
21172 break;
21174 default:
21175 strip_conversions = false;
21176 break;
21179 /* If possible and permitted, output the attribute as a constant. */
21180 if ((forms & dw_scalar_form_constant) != 0
21181 && TREE_CODE (value) == INTEGER_CST)
21183 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21185 /* If HOST_WIDE_INT is big enough then represent the bound as
21186 a constant value. We need to choose a form based on
21187 whether the type is signed or unsigned. We cannot just
21188 call add_AT_unsigned if the value itself is positive
21189 (add_AT_unsigned might add the unsigned value encoded as
21190 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21191 bounds type and then sign extend any unsigned values found
21192 for signed types. This is needed only for
21193 DW_AT_{lower,upper}_bound, since for most other attributes,
21194 consumers will treat DW_FORM_data[1248] as unsigned values,
21195 regardless of the underlying type. */
21196 if (prec <= HOST_BITS_PER_WIDE_INT
21197 || tree_fits_uhwi_p (value))
21199 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21200 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21201 else
21202 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21204 else if (dwarf_version >= 5
21205 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21206 /* Otherwise represent the bound as an unsigned value with
21207 the precision of its type. The precision and signedness
21208 of the type will be necessary to re-interpret it
21209 unambiguously. */
21210 add_AT_wide (die, attr, wi::to_wide (value));
21211 else
21213 rtx v = immed_wide_int_const (wi::to_wide (value),
21214 TYPE_MODE (TREE_TYPE (value)));
21215 dw_loc_descr_ref loc
21216 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21217 VAR_INIT_STATUS_INITIALIZED);
21218 if (loc)
21219 add_AT_loc (die, attr, loc);
21221 return;
21224 /* Otherwise, if it's possible and permitted too, output a reference to
21225 another DIE. */
21226 if ((forms & dw_scalar_form_reference) != 0)
21228 tree decl = NULL_TREE;
21230 /* Some type attributes reference an outer type. For instance, the upper
21231 bound of an array may reference an embedding record (this happens in
21232 Ada). */
21233 if (TREE_CODE (value) == COMPONENT_REF
21234 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21235 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21236 decl = TREE_OPERAND (value, 1);
21238 else if (VAR_P (value)
21239 || TREE_CODE (value) == PARM_DECL
21240 || TREE_CODE (value) == RESULT_DECL)
21241 decl = value;
21243 if (decl != NULL_TREE)
21245 decl_die = lookup_decl_die (decl);
21247 /* ??? Can this happen, or should the variable have been bound
21248 first? Probably it can, since I imagine that we try to create
21249 the types of parameters in the order in which they exist in
21250 the list, and won't have created a forward reference to a
21251 later parameter. */
21252 if (decl_die != NULL)
21254 if (get_AT (decl_die, DW_AT_location)
21255 || get_AT (decl_die, DW_AT_data_member_location)
21256 || get_AT (decl_die, DW_AT_data_bit_offset)
21257 || get_AT (decl_die, DW_AT_const_value))
21259 add_AT_die_ref (die, attr, decl_die);
21260 return;
21266 /* Last chance: try to create a stack operation procedure to evaluate the
21267 value. Do nothing if even that is not possible or permitted. */
21268 if ((forms & dw_scalar_form_exprloc) == 0)
21269 return;
21271 list = loc_list_from_tree (value, 2, context);
21272 if (context && context->placeholder_arg)
21274 placeholder_seen = context->placeholder_seen;
21275 context->placeholder_seen = false;
21277 if (list == NULL || single_element_loc_list_p (list))
21279 /* If this attribute is not a reference nor constant, it is
21280 a DWARF expression rather than location description. For that
21281 loc_list_from_tree (value, 0, &context) is needed. */
21282 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21283 if (list2 && single_element_loc_list_p (list2))
21285 if (placeholder_seen)
21287 struct dwarf_procedure_info dpi;
21288 dpi.fndecl = NULL_TREE;
21289 dpi.args_count = 1;
21290 if (!resolve_args_picking (list2->expr, 1, &dpi))
21291 return;
21293 add_AT_loc (die, attr, list2->expr);
21294 return;
21298 /* If that failed to give a single element location list, fall back to
21299 outputting this as a reference... still if permitted. */
21300 if (list == NULL
21301 || (forms & dw_scalar_form_reference) == 0
21302 || placeholder_seen)
21303 return;
21305 if (!decl_die)
21307 if (current_function_decl == 0)
21308 context_die = comp_unit_die ();
21309 else
21310 context_die = lookup_decl_die (current_function_decl);
21312 decl_die = new_die (DW_TAG_variable, context_die, value);
21313 add_AT_flag (decl_die, DW_AT_artificial, 1);
21314 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21315 context_die);
21318 add_AT_location_description (decl_die, DW_AT_location, list);
21319 add_AT_die_ref (die, attr, decl_die);
21322 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21323 default. */
21325 static int
21326 lower_bound_default (void)
21328 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21330 case DW_LANG_C:
21331 case DW_LANG_C89:
21332 case DW_LANG_C99:
21333 case DW_LANG_C11:
21334 case DW_LANG_C_plus_plus:
21335 case DW_LANG_C_plus_plus_11:
21336 case DW_LANG_C_plus_plus_14:
21337 case DW_LANG_ObjC:
21338 case DW_LANG_ObjC_plus_plus:
21339 return 0;
21340 case DW_LANG_Fortran77:
21341 case DW_LANG_Fortran90:
21342 case DW_LANG_Fortran95:
21343 case DW_LANG_Fortran03:
21344 case DW_LANG_Fortran08:
21345 return 1;
21346 case DW_LANG_UPC:
21347 case DW_LANG_D:
21348 case DW_LANG_Python:
21349 return dwarf_version >= 4 ? 0 : -1;
21350 case DW_LANG_Ada95:
21351 case DW_LANG_Ada83:
21352 case DW_LANG_Cobol74:
21353 case DW_LANG_Cobol85:
21354 case DW_LANG_Modula2:
21355 case DW_LANG_PLI:
21356 return dwarf_version >= 4 ? 1 : -1;
21357 default:
21358 return -1;
21362 /* Given a tree node describing an array bound (either lower or upper) output
21363 a representation for that bound. */
21365 static void
21366 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21367 tree bound, struct loc_descr_context *context)
21369 int dflt;
21371 while (1)
21372 switch (TREE_CODE (bound))
21374 /* Strip all conversions. */
21375 CASE_CONVERT:
21376 case VIEW_CONVERT_EXPR:
21377 bound = TREE_OPERAND (bound, 0);
21378 break;
21380 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21381 are even omitted when they are the default. */
21382 case INTEGER_CST:
21383 /* If the value for this bound is the default one, we can even omit the
21384 attribute. */
21385 if (bound_attr == DW_AT_lower_bound
21386 && tree_fits_shwi_p (bound)
21387 && (dflt = lower_bound_default ()) != -1
21388 && tree_to_shwi (bound) == dflt)
21389 return;
21391 /* FALLTHRU */
21393 default:
21394 /* Let GNAT encodings do the magic for self-referential bounds. */
21395 if (is_ada ()
21396 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21397 && contains_placeholder_p (bound))
21398 return;
21400 add_scalar_info (subrange_die, bound_attr, bound,
21401 dw_scalar_form_constant
21402 | dw_scalar_form_exprloc
21403 | dw_scalar_form_reference,
21404 context);
21405 return;
21409 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21410 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21412 This function reuses previously set type and bound information if
21413 available. */
21415 static void
21416 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21418 dw_die_ref child = type_die->die_child;
21419 struct array_descr_info info;
21420 int dimension_number;
21422 if (lang_hooks.types.get_array_descr_info)
21424 memset (&info, 0, sizeof (info));
21425 if (lang_hooks.types.get_array_descr_info (type, &info))
21426 /* Fortran sometimes emits array types with no dimension. */
21427 gcc_assert (info.ndimensions >= 0
21428 && info.ndimensions
21429 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21431 else
21432 info.ndimensions = 0;
21434 for (dimension_number = 0;
21435 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21436 type = TREE_TYPE (type), dimension_number++)
21438 tree domain = TYPE_DOMAIN (type);
21440 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21441 break;
21443 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21444 and (in GNU C only) variable bounds. Handle all three forms
21445 here. */
21447 /* Find and reuse a previously generated DW_TAG_subrange_type if
21448 available.
21450 For multi-dimensional arrays, as we iterate through the
21451 various dimensions in the enclosing for loop above, we also
21452 iterate through the DIE children and pick at each
21453 DW_TAG_subrange_type previously generated (if available).
21454 Each child DW_TAG_subrange_type DIE describes the range of
21455 the current dimension. At this point we should have as many
21456 DW_TAG_subrange_type's as we have dimensions in the
21457 array. */
21458 dw_die_ref subrange_die = NULL;
21459 if (child)
21460 while (1)
21462 child = child->die_sib;
21463 if (child->die_tag == DW_TAG_subrange_type)
21464 subrange_die = child;
21465 if (child == type_die->die_child)
21467 /* If we wrapped around, stop looking next time. */
21468 child = NULL;
21469 break;
21471 if (child->die_tag == DW_TAG_subrange_type)
21472 break;
21474 if (!subrange_die)
21475 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21477 if (domain)
21479 /* We have an array type with specified bounds. */
21480 tree lower = TYPE_MIN_VALUE (domain);
21481 tree upper = TYPE_MAX_VALUE (domain);
21482 tree index_type = TREE_TYPE (domain);
21484 if (dimension_number <= info.ndimensions - 1)
21486 lower = info.dimen[dimension_number].lower_bound;
21487 upper = info.dimen[dimension_number].upper_bound;
21488 index_type = info.dimen[dimension_number].bounds_type;
21491 /* Define the index type. */
21492 if (index_type && !get_AT (subrange_die, DW_AT_type))
21493 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21494 false, type_die);
21496 /* ??? If upper is NULL, the array has unspecified length,
21497 but it does have a lower bound. This happens with Fortran
21498 dimension arr(N:*)
21499 Since the debugger is definitely going to need to know N
21500 to produce useful results, go ahead and output the lower
21501 bound solo, and hope the debugger can cope. */
21503 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21504 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21506 if (!get_AT (subrange_die, DW_AT_upper_bound)
21507 && !get_AT (subrange_die, DW_AT_count))
21509 if (upper)
21510 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21511 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21512 /* Zero-length array. */
21513 add_bound_info (subrange_die, DW_AT_count,
21514 build_int_cst (TREE_TYPE (lower), 0), NULL);
21518 /* Otherwise we have an array type with an unspecified length. The
21519 DWARF-2 spec does not say how to handle this; let's just leave out the
21520 bounds. */
21524 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21526 static void
21527 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21529 dw_die_ref decl_die;
21530 HOST_WIDE_INT size;
21532 switch (TREE_CODE (tree_node))
21534 case ERROR_MARK:
21535 size = 0;
21536 break;
21537 case ENUMERAL_TYPE:
21538 case RECORD_TYPE:
21539 case UNION_TYPE:
21540 case QUAL_UNION_TYPE:
21541 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21542 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21544 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21545 return;
21547 size = int_size_in_bytes (tree_node);
21548 break;
21549 case FIELD_DECL:
21550 /* For a data member of a struct or union, the DW_AT_byte_size is
21551 generally given as the number of bytes normally allocated for an
21552 object of the *declared* type of the member itself. This is true
21553 even for bit-fields. */
21554 size = int_size_in_bytes (field_type (tree_node));
21555 break;
21556 default:
21557 gcc_unreachable ();
21560 /* Note that `size' might be -1 when we get to this point. If it is, that
21561 indicates that the byte size of the entity in question is variable. */
21562 if (size >= 0)
21563 add_AT_unsigned (die, DW_AT_byte_size, size);
21565 /* Support for dynamically-sized objects was introduced in DWARF3. */
21566 else if (TYPE_P (tree_node)
21567 && (dwarf_version >= 3 || !dwarf_strict)
21568 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21570 struct loc_descr_context ctx = {
21571 const_cast<tree> (tree_node), /* context_type */
21572 NULL_TREE, /* base_decl */
21573 NULL, /* dpi */
21574 false, /* placeholder_arg */
21575 false, /* placeholder_seen */
21576 false /* strict_signedness */
21579 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21580 add_scalar_info (die, DW_AT_byte_size, tree_size,
21581 dw_scalar_form_constant
21582 | dw_scalar_form_exprloc
21583 | dw_scalar_form_reference,
21584 &ctx);
21588 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21589 alignment. */
21591 static void
21592 add_alignment_attribute (dw_die_ref die, tree tree_node)
21594 if (dwarf_version < 5 && dwarf_strict)
21595 return;
21597 unsigned align;
21599 if (DECL_P (tree_node))
21601 if (!DECL_USER_ALIGN (tree_node))
21602 return;
21604 align = DECL_ALIGN_UNIT (tree_node);
21606 else if (TYPE_P (tree_node))
21608 if (!TYPE_USER_ALIGN (tree_node))
21609 return;
21611 align = TYPE_ALIGN_UNIT (tree_node);
21613 else
21614 gcc_unreachable ();
21616 add_AT_unsigned (die, DW_AT_alignment, align);
21619 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21620 which specifies the distance in bits from the highest order bit of the
21621 "containing object" for the bit-field to the highest order bit of the
21622 bit-field itself.
21624 For any given bit-field, the "containing object" is a hypothetical object
21625 (of some integral or enum type) within which the given bit-field lives. The
21626 type of this hypothetical "containing object" is always the same as the
21627 declared type of the individual bit-field itself. The determination of the
21628 exact location of the "containing object" for a bit-field is rather
21629 complicated. It's handled by the `field_byte_offset' function (above).
21631 Note that it is the size (in bytes) of the hypothetical "containing object"
21632 which will be given in the DW_AT_byte_size attribute for this bit-field.
21633 (See `byte_size_attribute' above). */
21635 static inline void
21636 add_bit_offset_attribute (dw_die_ref die, tree decl)
21638 HOST_WIDE_INT object_offset_in_bytes;
21639 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21640 HOST_WIDE_INT bitpos_int;
21641 HOST_WIDE_INT highest_order_object_bit_offset;
21642 HOST_WIDE_INT highest_order_field_bit_offset;
21643 HOST_WIDE_INT bit_offset;
21645 /* The containing object is within the DECL_CONTEXT. */
21646 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21648 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21650 /* Must be a field and a bit field. */
21651 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21653 /* We can't yet handle bit-fields whose offsets are variable, so if we
21654 encounter such things, just return without generating any attribute
21655 whatsoever. Likewise for variable or too large size. */
21656 if (! tree_fits_shwi_p (bit_position (decl))
21657 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21658 return;
21660 bitpos_int = int_bit_position (decl);
21662 /* Note that the bit offset is always the distance (in bits) from the
21663 highest-order bit of the "containing object" to the highest-order bit of
21664 the bit-field itself. Since the "high-order end" of any object or field
21665 is different on big-endian and little-endian machines, the computation
21666 below must take account of these differences. */
21667 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21668 highest_order_field_bit_offset = bitpos_int;
21670 if (! BYTES_BIG_ENDIAN)
21672 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21673 highest_order_object_bit_offset +=
21674 simple_type_size_in_bits (original_type);
21677 bit_offset
21678 = (! BYTES_BIG_ENDIAN
21679 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21680 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21682 if (bit_offset < 0)
21683 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21684 else
21685 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21688 /* For a FIELD_DECL node which represents a bit field, output an attribute
21689 which specifies the length in bits of the given field. */
21691 static inline void
21692 add_bit_size_attribute (dw_die_ref die, tree decl)
21694 /* Must be a field and a bit field. */
21695 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21696 && DECL_BIT_FIELD_TYPE (decl));
21698 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21699 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21702 /* If the compiled language is ANSI C, then add a 'prototyped'
21703 attribute, if arg types are given for the parameters of a function. */
21705 static inline void
21706 add_prototyped_attribute (dw_die_ref die, tree func_type)
21708 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21710 case DW_LANG_C:
21711 case DW_LANG_C89:
21712 case DW_LANG_C99:
21713 case DW_LANG_C11:
21714 case DW_LANG_ObjC:
21715 if (prototype_p (func_type))
21716 add_AT_flag (die, DW_AT_prototyped, 1);
21717 break;
21718 default:
21719 break;
21723 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21724 by looking in the type declaration, the object declaration equate table or
21725 the block mapping. */
21727 static inline void
21728 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21730 dw_die_ref origin_die = NULL;
21732 /* For late LTO debug output we want to refer directly to the abstract
21733 DIE in the early debug rather to the possibly existing concrete
21734 instance and avoid creating that just for this purpose. */
21735 sym_off_pair *desc;
21736 if (in_lto_p
21737 && external_die_map
21738 && (desc = external_die_map->get (origin)))
21740 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21741 desc->sym, desc->off);
21742 return;
21745 if (DECL_P (origin))
21746 origin_die = lookup_decl_die (origin);
21747 else if (TYPE_P (origin))
21748 origin_die = lookup_type_die (origin);
21749 else if (TREE_CODE (origin) == BLOCK)
21750 origin_die = lookup_block_die (origin);
21752 /* XXX: Functions that are never lowered don't always have correct block
21753 trees (in the case of java, they simply have no block tree, in some other
21754 languages). For these functions, there is nothing we can really do to
21755 output correct debug info for inlined functions in all cases. Rather
21756 than die, we'll just produce deficient debug info now, in that we will
21757 have variables without a proper abstract origin. In the future, when all
21758 functions are lowered, we should re-add a gcc_assert (origin_die)
21759 here. */
21761 if (origin_die)
21763 dw_attr_node *a;
21764 /* Like above, if we already created a concrete instance DIE
21765 do not use that for the abstract origin but the early DIE
21766 if present. */
21767 if (in_lto_p
21768 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21769 origin_die = AT_ref (a);
21770 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21774 /* We do not currently support the pure_virtual attribute. */
21776 static inline void
21777 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21779 if (DECL_VINDEX (func_decl))
21781 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21783 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21784 add_AT_loc (die, DW_AT_vtable_elem_location,
21785 new_loc_descr (DW_OP_constu,
21786 tree_to_shwi (DECL_VINDEX (func_decl)),
21787 0));
21789 /* GNU extension: Record what type this method came from originally. */
21790 if (debug_info_level > DINFO_LEVEL_TERSE
21791 && DECL_CONTEXT (func_decl))
21792 add_AT_die_ref (die, DW_AT_containing_type,
21793 lookup_type_die (DECL_CONTEXT (func_decl)));
21797 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21798 given decl. This used to be a vendor extension until after DWARF 4
21799 standardized it. */
21801 static void
21802 add_linkage_attr (dw_die_ref die, tree decl)
21804 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21806 /* Mimic what assemble_name_raw does with a leading '*'. */
21807 if (name[0] == '*')
21808 name = &name[1];
21810 if (dwarf_version >= 4)
21811 add_AT_string (die, DW_AT_linkage_name, name);
21812 else
21813 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21816 /* Add source coordinate attributes for the given decl. */
21818 static void
21819 add_src_coords_attributes (dw_die_ref die, tree decl)
21821 expanded_location s;
21823 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21824 return;
21825 s = expand_location (DECL_SOURCE_LOCATION (decl));
21826 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21827 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21828 if (debug_column_info && s.column)
21829 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21832 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21834 static void
21835 add_linkage_name_raw (dw_die_ref die, tree decl)
21837 /* Defer until we have an assembler name set. */
21838 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21840 limbo_die_node *asm_name;
21842 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21843 asm_name->die = die;
21844 asm_name->created_for = decl;
21845 asm_name->next = deferred_asm_name;
21846 deferred_asm_name = asm_name;
21848 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21849 add_linkage_attr (die, decl);
21852 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21854 static void
21855 add_linkage_name (dw_die_ref die, tree decl)
21857 if (debug_info_level > DINFO_LEVEL_NONE
21858 && VAR_OR_FUNCTION_DECL_P (decl)
21859 && TREE_PUBLIC (decl)
21860 && !(VAR_P (decl) && DECL_REGISTER (decl))
21861 && die->die_tag != DW_TAG_member)
21862 add_linkage_name_raw (die, decl);
21865 /* Add a DW_AT_name attribute and source coordinate attribute for the
21866 given decl, but only if it actually has a name. */
21868 static void
21869 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21870 bool no_linkage_name)
21872 tree decl_name;
21874 decl_name = DECL_NAME (decl);
21875 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21877 const char *name = dwarf2_name (decl, 0);
21878 if (name)
21879 add_name_attribute (die, name);
21880 else
21881 add_desc_attribute (die, decl);
21883 if (! DECL_ARTIFICIAL (decl))
21884 add_src_coords_attributes (die, decl);
21886 if (!no_linkage_name)
21887 add_linkage_name (die, decl);
21889 else
21890 add_desc_attribute (die, decl);
21892 #ifdef VMS_DEBUGGING_INFO
21893 /* Get the function's name, as described by its RTL. This may be different
21894 from the DECL_NAME name used in the source file. */
21895 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21897 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21898 XEXP (DECL_RTL (decl), 0), false);
21899 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21901 #endif /* VMS_DEBUGGING_INFO */
21904 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21906 static void
21907 add_discr_value (dw_die_ref die, dw_discr_value *value)
21909 dw_attr_node attr;
21911 attr.dw_attr = DW_AT_discr_value;
21912 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21913 attr.dw_attr_val.val_entry = NULL;
21914 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21915 if (value->pos)
21916 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21917 else
21918 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21919 add_dwarf_attr (die, &attr);
21922 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21924 static void
21925 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21927 dw_attr_node attr;
21929 attr.dw_attr = DW_AT_discr_list;
21930 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21931 attr.dw_attr_val.val_entry = NULL;
21932 attr.dw_attr_val.v.val_discr_list = discr_list;
21933 add_dwarf_attr (die, &attr);
21936 static inline dw_discr_list_ref
21937 AT_discr_list (dw_attr_node *attr)
21939 return attr->dw_attr_val.v.val_discr_list;
21942 #ifdef VMS_DEBUGGING_INFO
21943 /* Output the debug main pointer die for VMS */
21945 void
21946 dwarf2out_vms_debug_main_pointer (void)
21948 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21949 dw_die_ref die;
21951 /* Allocate the VMS debug main subprogram die. */
21952 die = new_die_raw (DW_TAG_subprogram);
21953 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21954 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21955 current_function_funcdef_no);
21956 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21958 /* Make it the first child of comp_unit_die (). */
21959 die->die_parent = comp_unit_die ();
21960 if (comp_unit_die ()->die_child)
21962 die->die_sib = comp_unit_die ()->die_child->die_sib;
21963 comp_unit_die ()->die_child->die_sib = die;
21965 else
21967 die->die_sib = die;
21968 comp_unit_die ()->die_child = die;
21971 #endif /* VMS_DEBUGGING_INFO */
21973 /* walk_tree helper function for uses_local_type, below. */
21975 static tree
21976 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21978 if (!TYPE_P (*tp))
21979 *walk_subtrees = 0;
21980 else
21982 tree name = TYPE_NAME (*tp);
21983 if (name && DECL_P (name) && decl_function_context (name))
21984 return *tp;
21986 return NULL_TREE;
21989 /* If TYPE involves a function-local type (including a local typedef to a
21990 non-local type), returns that type; otherwise returns NULL_TREE. */
21992 static tree
21993 uses_local_type (tree type)
21995 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21996 return used;
21999 /* Return the DIE for the scope that immediately contains this type.
22000 Non-named types that do not involve a function-local type get global
22001 scope. Named types nested in namespaces or other types get their
22002 containing scope. All other types (i.e. function-local named types) get
22003 the current active scope. */
22005 static dw_die_ref
22006 scope_die_for (tree t, dw_die_ref context_die)
22008 dw_die_ref scope_die = NULL;
22009 tree containing_scope;
22011 /* Non-types always go in the current scope. */
22012 gcc_assert (TYPE_P (t));
22014 /* Use the scope of the typedef, rather than the scope of the type
22015 it refers to. */
22016 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22017 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22018 else
22019 containing_scope = TYPE_CONTEXT (t);
22021 /* Use the containing namespace if there is one. */
22022 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22024 if (context_die == lookup_decl_die (containing_scope))
22025 /* OK */;
22026 else if (debug_info_level > DINFO_LEVEL_TERSE)
22027 context_die = get_context_die (containing_scope);
22028 else
22029 containing_scope = NULL_TREE;
22032 /* Ignore function type "scopes" from the C frontend. They mean that
22033 a tagged type is local to a parmlist of a function declarator, but
22034 that isn't useful to DWARF. */
22035 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22036 containing_scope = NULL_TREE;
22038 if (SCOPE_FILE_SCOPE_P (containing_scope))
22040 /* If T uses a local type keep it local as well, to avoid references
22041 to function-local DIEs from outside the function. */
22042 if (current_function_decl && uses_local_type (t))
22043 scope_die = context_die;
22044 else
22045 scope_die = comp_unit_die ();
22047 else if (TYPE_P (containing_scope))
22049 /* For types, we can just look up the appropriate DIE. */
22050 if (debug_info_level > DINFO_LEVEL_TERSE)
22051 scope_die = get_context_die (containing_scope);
22052 else
22054 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22055 if (scope_die == NULL)
22056 scope_die = comp_unit_die ();
22059 else
22060 scope_die = context_die;
22062 return scope_die;
22065 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22067 static inline int
22068 local_scope_p (dw_die_ref context_die)
22070 for (; context_die; context_die = context_die->die_parent)
22071 if (context_die->die_tag == DW_TAG_inlined_subroutine
22072 || context_die->die_tag == DW_TAG_subprogram)
22073 return 1;
22075 return 0;
22078 /* Returns nonzero if CONTEXT_DIE is a class. */
22080 static inline int
22081 class_scope_p (dw_die_ref context_die)
22083 return (context_die
22084 && (context_die->die_tag == DW_TAG_structure_type
22085 || context_die->die_tag == DW_TAG_class_type
22086 || context_die->die_tag == DW_TAG_interface_type
22087 || context_die->die_tag == DW_TAG_union_type));
22090 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22091 whether or not to treat a DIE in this context as a declaration. */
22093 static inline int
22094 class_or_namespace_scope_p (dw_die_ref context_die)
22096 return (class_scope_p (context_die)
22097 || (context_die && context_die->die_tag == DW_TAG_namespace));
22100 /* Many forms of DIEs require a "type description" attribute. This
22101 routine locates the proper "type descriptor" die for the type given
22102 by 'type' plus any additional qualifiers given by 'cv_quals', and
22103 adds a DW_AT_type attribute below the given die. */
22105 static void
22106 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22107 bool reverse, dw_die_ref context_die)
22109 enum tree_code code = TREE_CODE (type);
22110 dw_die_ref type_die = NULL;
22112 if (debug_info_level <= DINFO_LEVEL_TERSE)
22113 return;
22115 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22116 or fixed-point type, use the inner type. This is because we have no
22117 support for unnamed types in base_type_die. This can happen if this is
22118 an Ada subrange type. Correct solution is emit a subrange type die. */
22119 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22120 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22121 type = TREE_TYPE (type), code = TREE_CODE (type);
22123 if (code == ERROR_MARK
22124 /* Handle a special case. For functions whose return type is void, we
22125 generate *no* type attribute. (Note that no object may have type
22126 `void', so this only applies to function return types). */
22127 || code == VOID_TYPE)
22128 return;
22130 type_die = modified_type_die (type,
22131 cv_quals | TYPE_QUALS (type),
22132 reverse,
22133 context_die);
22135 if (type_die != NULL)
22136 add_AT_die_ref (object_die, DW_AT_type, type_die);
22139 /* Given an object die, add the calling convention attribute for the
22140 function call type. */
22141 static void
22142 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22144 enum dwarf_calling_convention value = DW_CC_normal;
22146 value = ((enum dwarf_calling_convention)
22147 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22149 if (is_fortran ()
22150 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22152 /* DWARF 2 doesn't provide a way to identify a program's source-level
22153 entry point. DW_AT_calling_convention attributes are only meant
22154 to describe functions' calling conventions. However, lacking a
22155 better way to signal the Fortran main program, we used this for
22156 a long time, following existing custom. Now, DWARF 4 has
22157 DW_AT_main_subprogram, which we add below, but some tools still
22158 rely on the old way, which we thus keep. */
22159 value = DW_CC_program;
22161 if (dwarf_version >= 4 || !dwarf_strict)
22162 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22165 /* Only add the attribute if the backend requests it, and
22166 is not DW_CC_normal. */
22167 if (value && (value != DW_CC_normal))
22168 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22171 /* Given a tree pointer to a struct, class, union, or enum type node, return
22172 a pointer to the (string) tag name for the given type, or zero if the type
22173 was declared without a tag. */
22175 static const char *
22176 type_tag (const_tree type)
22178 const char *name = 0;
22180 if (TYPE_NAME (type) != 0)
22182 tree t = 0;
22184 /* Find the IDENTIFIER_NODE for the type name. */
22185 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22186 && !TYPE_NAMELESS (type))
22187 t = TYPE_NAME (type);
22189 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22190 a TYPE_DECL node, regardless of whether or not a `typedef' was
22191 involved. */
22192 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22193 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22195 /* We want to be extra verbose. Don't call dwarf_name if
22196 DECL_NAME isn't set. The default hook for decl_printable_name
22197 doesn't like that, and in this context it's correct to return
22198 0, instead of "<anonymous>" or the like. */
22199 if (DECL_NAME (TYPE_NAME (type))
22200 && !DECL_NAMELESS (TYPE_NAME (type)))
22201 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22204 /* Now get the name as a string, or invent one. */
22205 if (!name && t != 0)
22206 name = IDENTIFIER_POINTER (t);
22209 return (name == 0 || *name == '\0') ? 0 : name;
22212 /* Return the type associated with a data member, make a special check
22213 for bit field types. */
22215 static inline tree
22216 member_declared_type (const_tree member)
22218 return (DECL_BIT_FIELD_TYPE (member)
22219 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22222 /* Get the decl's label, as described by its RTL. This may be different
22223 from the DECL_NAME name used in the source file. */
22225 #if 0
22226 static const char *
22227 decl_start_label (tree decl)
22229 rtx x;
22230 const char *fnname;
22232 x = DECL_RTL (decl);
22233 gcc_assert (MEM_P (x));
22235 x = XEXP (x, 0);
22236 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22238 fnname = XSTR (x, 0);
22239 return fnname;
22241 #endif
22243 /* For variable-length arrays that have been previously generated, but
22244 may be incomplete due to missing subscript info, fill the subscript
22245 info. Return TRUE if this is one of those cases. */
22247 static bool
22248 fill_variable_array_bounds (tree type)
22250 if (TREE_ASM_WRITTEN (type)
22251 && TREE_CODE (type) == ARRAY_TYPE
22252 && variably_modified_type_p (type, NULL))
22254 dw_die_ref array_die = lookup_type_die (type);
22255 if (!array_die)
22256 return false;
22257 add_subscript_info (array_die, type, !is_ada ());
22258 return true;
22260 return false;
22263 /* These routines generate the internal representation of the DIE's for
22264 the compilation unit. Debugging information is collected by walking
22265 the declaration trees passed in from dwarf2out_decl(). */
22267 static void
22268 gen_array_type_die (tree type, dw_die_ref context_die)
22270 dw_die_ref array_die;
22272 /* GNU compilers represent multidimensional array types as sequences of one
22273 dimensional array types whose element types are themselves array types.
22274 We sometimes squish that down to a single array_type DIE with multiple
22275 subscripts in the Dwarf debugging info. The draft Dwarf specification
22276 say that we are allowed to do this kind of compression in C, because
22277 there is no difference between an array of arrays and a multidimensional
22278 array. We don't do this for Ada to remain as close as possible to the
22279 actual representation, which is especially important against the language
22280 flexibilty wrt arrays of variable size. */
22282 bool collapse_nested_arrays = !is_ada ();
22284 if (fill_variable_array_bounds (type))
22285 return;
22287 dw_die_ref scope_die = scope_die_for (type, context_die);
22288 tree element_type;
22290 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22291 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22292 if (TREE_CODE (type) == ARRAY_TYPE
22293 && TYPE_STRING_FLAG (type)
22294 && is_fortran ()
22295 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22297 HOST_WIDE_INT size;
22299 array_die = new_die (DW_TAG_string_type, scope_die, type);
22300 add_name_attribute (array_die, type_tag (type));
22301 equate_type_number_to_die (type, array_die);
22302 size = int_size_in_bytes (type);
22303 if (size >= 0)
22304 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22305 /* ??? We can't annotate types late, but for LTO we may not
22306 generate a location early either (gfortran.dg/save_6.f90). */
22307 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22308 && TYPE_DOMAIN (type) != NULL_TREE
22309 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22311 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22312 tree rszdecl = szdecl;
22314 size = int_size_in_bytes (TREE_TYPE (szdecl));
22315 if (!DECL_P (szdecl))
22317 if (TREE_CODE (szdecl) == INDIRECT_REF
22318 && DECL_P (TREE_OPERAND (szdecl, 0)))
22320 rszdecl = TREE_OPERAND (szdecl, 0);
22321 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22322 != DWARF2_ADDR_SIZE)
22323 size = 0;
22325 else
22326 size = 0;
22328 if (size > 0)
22330 dw_loc_list_ref loc
22331 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22332 NULL);
22333 if (loc)
22335 add_AT_location_description (array_die, DW_AT_string_length,
22336 loc);
22337 if (size != DWARF2_ADDR_SIZE)
22338 add_AT_unsigned (array_die, dwarf_version >= 5
22339 ? DW_AT_string_length_byte_size
22340 : DW_AT_byte_size, size);
22344 return;
22347 array_die = new_die (DW_TAG_array_type, scope_die, type);
22348 add_name_attribute (array_die, type_tag (type));
22349 equate_type_number_to_die (type, array_die);
22351 if (TREE_CODE (type) == VECTOR_TYPE)
22352 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22354 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22355 if (is_fortran ()
22356 && TREE_CODE (type) == ARRAY_TYPE
22357 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22358 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22359 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22361 #if 0
22362 /* We default the array ordering. Debuggers will probably do the right
22363 things even if DW_AT_ordering is not present. It's not even an issue
22364 until we start to get into multidimensional arrays anyway. If a debugger
22365 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22366 then we'll have to put the DW_AT_ordering attribute back in. (But if
22367 and when we find out that we need to put these in, we will only do so
22368 for multidimensional arrays. */
22369 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22370 #endif
22372 if (TREE_CODE (type) == VECTOR_TYPE)
22374 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22375 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22376 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22377 add_bound_info (subrange_die, DW_AT_upper_bound,
22378 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22380 else
22381 add_subscript_info (array_die, type, collapse_nested_arrays);
22383 /* Add representation of the type of the elements of this array type and
22384 emit the corresponding DIE if we haven't done it already. */
22385 element_type = TREE_TYPE (type);
22386 if (collapse_nested_arrays)
22387 while (TREE_CODE (element_type) == ARRAY_TYPE)
22389 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22390 break;
22391 element_type = TREE_TYPE (element_type);
22394 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22395 TREE_CODE (type) == ARRAY_TYPE
22396 && TYPE_REVERSE_STORAGE_ORDER (type),
22397 context_die);
22399 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22400 if (TYPE_ARTIFICIAL (type))
22401 add_AT_flag (array_die, DW_AT_artificial, 1);
22403 if (get_AT (array_die, DW_AT_name))
22404 add_pubtype (type, array_die);
22406 add_alignment_attribute (array_die, type);
22409 /* This routine generates DIE for array with hidden descriptor, details
22410 are filled into *info by a langhook. */
22412 static void
22413 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22414 dw_die_ref context_die)
22416 const dw_die_ref scope_die = scope_die_for (type, context_die);
22417 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22418 struct loc_descr_context context = {
22419 type, /* context_type */
22420 info->base_decl, /* base_decl */
22421 NULL, /* dpi */
22422 false, /* placeholder_arg */
22423 false, /* placeholder_seen */
22424 false /* strict_signedness */
22426 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22427 int dim;
22429 add_name_attribute (array_die, type_tag (type));
22430 equate_type_number_to_die (type, array_die);
22432 if (info->ndimensions > 1)
22433 switch (info->ordering)
22435 case array_descr_ordering_row_major:
22436 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22437 break;
22438 case array_descr_ordering_column_major:
22439 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22440 break;
22441 default:
22442 break;
22445 if (dwarf_version >= 3 || !dwarf_strict)
22447 if (info->data_location)
22448 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22449 dw_scalar_form_exprloc, &context);
22450 if (info->associated)
22451 add_scalar_info (array_die, DW_AT_associated, info->associated,
22452 dw_scalar_form_constant
22453 | dw_scalar_form_exprloc
22454 | dw_scalar_form_reference, &context);
22455 if (info->allocated)
22456 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22457 dw_scalar_form_constant
22458 | dw_scalar_form_exprloc
22459 | dw_scalar_form_reference, &context);
22460 if (info->stride)
22462 const enum dwarf_attribute attr
22463 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22464 const int forms
22465 = (info->stride_in_bits)
22466 ? dw_scalar_form_constant
22467 : (dw_scalar_form_constant
22468 | dw_scalar_form_exprloc
22469 | dw_scalar_form_reference);
22471 add_scalar_info (array_die, attr, info->stride, forms, &context);
22474 if (dwarf_version >= 5)
22476 if (info->rank)
22478 add_scalar_info (array_die, DW_AT_rank, info->rank,
22479 dw_scalar_form_constant
22480 | dw_scalar_form_exprloc, &context);
22481 subrange_tag = DW_TAG_generic_subrange;
22482 context.placeholder_arg = true;
22486 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22488 for (dim = 0; dim < info->ndimensions; dim++)
22490 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22492 if (info->dimen[dim].bounds_type)
22493 add_type_attribute (subrange_die,
22494 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22495 false, context_die);
22496 if (info->dimen[dim].lower_bound)
22497 add_bound_info (subrange_die, DW_AT_lower_bound,
22498 info->dimen[dim].lower_bound, &context);
22499 if (info->dimen[dim].upper_bound)
22500 add_bound_info (subrange_die, DW_AT_upper_bound,
22501 info->dimen[dim].upper_bound, &context);
22502 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22503 add_scalar_info (subrange_die, DW_AT_byte_stride,
22504 info->dimen[dim].stride,
22505 dw_scalar_form_constant
22506 | dw_scalar_form_exprloc
22507 | dw_scalar_form_reference,
22508 &context);
22511 gen_type_die (info->element_type, context_die);
22512 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22513 TREE_CODE (type) == ARRAY_TYPE
22514 && TYPE_REVERSE_STORAGE_ORDER (type),
22515 context_die);
22517 if (get_AT (array_die, DW_AT_name))
22518 add_pubtype (type, array_die);
22520 add_alignment_attribute (array_die, type);
22523 #if 0
22524 static void
22525 gen_entry_point_die (tree decl, dw_die_ref context_die)
22527 tree origin = decl_ultimate_origin (decl);
22528 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22530 if (origin != NULL)
22531 add_abstract_origin_attribute (decl_die, origin);
22532 else
22534 add_name_and_src_coords_attributes (decl_die, decl);
22535 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22536 TYPE_UNQUALIFIED, false, context_die);
22539 if (DECL_ABSTRACT_P (decl))
22540 equate_decl_number_to_die (decl, decl_die);
22541 else
22542 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22544 #endif
22546 /* Walk through the list of incomplete types again, trying once more to
22547 emit full debugging info for them. */
22549 static void
22550 retry_incomplete_types (void)
22552 set_early_dwarf s;
22553 int i;
22555 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22556 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22557 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22558 vec_safe_truncate (incomplete_types, 0);
22561 /* Determine what tag to use for a record type. */
22563 static enum dwarf_tag
22564 record_type_tag (tree type)
22566 if (! lang_hooks.types.classify_record)
22567 return DW_TAG_structure_type;
22569 switch (lang_hooks.types.classify_record (type))
22571 case RECORD_IS_STRUCT:
22572 return DW_TAG_structure_type;
22574 case RECORD_IS_CLASS:
22575 return DW_TAG_class_type;
22577 case RECORD_IS_INTERFACE:
22578 if (dwarf_version >= 3 || !dwarf_strict)
22579 return DW_TAG_interface_type;
22580 return DW_TAG_structure_type;
22582 default:
22583 gcc_unreachable ();
22587 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22588 include all of the information about the enumeration values also. Each
22589 enumerated type name/value is listed as a child of the enumerated type
22590 DIE. */
22592 static dw_die_ref
22593 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22595 dw_die_ref type_die = lookup_type_die (type);
22596 dw_die_ref orig_type_die = type_die;
22598 if (type_die == NULL)
22600 type_die = new_die (DW_TAG_enumeration_type,
22601 scope_die_for (type, context_die), type);
22602 equate_type_number_to_die (type, type_die);
22603 add_name_attribute (type_die, type_tag (type));
22604 if ((dwarf_version >= 4 || !dwarf_strict)
22605 && ENUM_IS_SCOPED (type))
22606 add_AT_flag (type_die, DW_AT_enum_class, 1);
22607 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22608 add_AT_flag (type_die, DW_AT_declaration, 1);
22609 if (!dwarf_strict)
22610 add_AT_unsigned (type_die, DW_AT_encoding,
22611 TYPE_UNSIGNED (type)
22612 ? DW_ATE_unsigned
22613 : DW_ATE_signed);
22615 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22616 return type_die;
22617 else
22618 remove_AT (type_die, DW_AT_declaration);
22620 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22621 given enum type is incomplete, do not generate the DW_AT_byte_size
22622 attribute or the DW_AT_element_list attribute. */
22623 if (TYPE_SIZE (type))
22625 tree link;
22627 if (!ENUM_IS_OPAQUE (type))
22628 TREE_ASM_WRITTEN (type) = 1;
22629 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22630 add_byte_size_attribute (type_die, type);
22631 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22632 add_alignment_attribute (type_die, type);
22633 if ((dwarf_version >= 3 || !dwarf_strict)
22634 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22636 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22637 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22638 context_die);
22640 if (TYPE_STUB_DECL (type) != NULL_TREE)
22642 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22643 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22644 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22645 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22648 /* If the first reference to this type was as the return type of an
22649 inline function, then it may not have a parent. Fix this now. */
22650 if (type_die->die_parent == NULL)
22651 add_child_die (scope_die_for (type, context_die), type_die);
22653 for (link = TYPE_VALUES (type);
22654 link != NULL; link = TREE_CHAIN (link))
22656 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22657 tree value = TREE_VALUE (link);
22659 if (DECL_P (value))
22660 equate_decl_number_to_die (value, enum_die);
22662 gcc_assert (!ENUM_IS_OPAQUE (type));
22663 add_name_attribute (enum_die,
22664 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22666 if (TREE_CODE (value) == CONST_DECL)
22667 value = DECL_INITIAL (value);
22669 if (simple_type_size_in_bits (TREE_TYPE (value))
22670 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22672 /* For constant forms created by add_AT_unsigned DWARF
22673 consumers (GDB, elfutils, etc.) always zero extend
22674 the value. Only when the actual value is negative
22675 do we need to use add_AT_int to generate a constant
22676 form that can represent negative values. */
22677 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22678 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22679 add_AT_unsigned (enum_die, DW_AT_const_value,
22680 (unsigned HOST_WIDE_INT) val);
22681 else
22682 add_AT_int (enum_die, DW_AT_const_value, val);
22684 else
22685 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22686 that here. TODO: This should be re-worked to use correct
22687 signed/unsigned double tags for all cases. */
22688 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22691 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22692 if (TYPE_ARTIFICIAL (type)
22693 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22694 add_AT_flag (type_die, DW_AT_artificial, 1);
22696 else
22697 add_AT_flag (type_die, DW_AT_declaration, 1);
22699 add_pubtype (type, type_die);
22701 return type_die;
22704 /* Generate a DIE to represent either a real live formal parameter decl or to
22705 represent just the type of some formal parameter position in some function
22706 type.
22708 Note that this routine is a bit unusual because its argument may be a
22709 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22710 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22711 node. If it's the former then this function is being called to output a
22712 DIE to represent a formal parameter object (or some inlining thereof). If
22713 it's the latter, then this function is only being called to output a
22714 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22715 argument type of some subprogram type.
22716 If EMIT_NAME_P is true, name and source coordinate attributes
22717 are emitted. */
22719 static dw_die_ref
22720 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22721 dw_die_ref context_die)
22723 tree node_or_origin = node ? node : origin;
22724 tree ultimate_origin;
22725 dw_die_ref parm_die = NULL;
22727 if (DECL_P (node_or_origin))
22729 parm_die = lookup_decl_die (node);
22731 /* If the contexts differ, we may not be talking about the same
22732 thing.
22733 ??? When in LTO the DIE parent is the "abstract" copy and the
22734 context_die is the specification "copy". */
22735 if (parm_die
22736 && parm_die->die_parent != context_die
22737 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22738 || parm_die->die_parent->die_parent != context_die)
22739 && !in_lto_p)
22741 gcc_assert (!DECL_ABSTRACT_P (node));
22742 /* This can happen when creating a concrete instance, in
22743 which case we need to create a new DIE that will get
22744 annotated with DW_AT_abstract_origin. */
22745 parm_die = NULL;
22748 if (parm_die && parm_die->die_parent == NULL)
22750 /* Check that parm_die already has the right attributes that
22751 we would have added below. If any attributes are
22752 missing, fall through to add them. */
22753 if (! DECL_ABSTRACT_P (node_or_origin)
22754 && !get_AT (parm_die, DW_AT_location)
22755 && !get_AT (parm_die, DW_AT_const_value))
22756 /* We are missing location info, and are about to add it. */
22758 else
22760 add_child_die (context_die, parm_die);
22761 return parm_die;
22766 /* If we have a previously generated DIE, use it, unless this is an
22767 concrete instance (origin != NULL), in which case we need a new
22768 DIE with a corresponding DW_AT_abstract_origin. */
22769 bool reusing_die;
22770 if (parm_die && origin == NULL)
22771 reusing_die = true;
22772 else
22774 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22775 reusing_die = false;
22778 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22780 case tcc_declaration:
22781 ultimate_origin = decl_ultimate_origin (node_or_origin);
22782 if (node || ultimate_origin)
22783 origin = ultimate_origin;
22785 if (reusing_die)
22786 goto add_location;
22788 if (origin != NULL)
22789 add_abstract_origin_attribute (parm_die, origin);
22790 else if (emit_name_p)
22791 add_name_and_src_coords_attributes (parm_die, node);
22792 if (origin == NULL
22793 || (! DECL_ABSTRACT_P (node_or_origin)
22794 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22795 decl_function_context
22796 (node_or_origin))))
22798 tree type = TREE_TYPE (node_or_origin);
22799 if (decl_by_reference_p (node_or_origin))
22800 add_type_attribute (parm_die, TREE_TYPE (type),
22801 TYPE_UNQUALIFIED,
22802 false, context_die);
22803 else
22804 add_type_attribute (parm_die, type,
22805 decl_quals (node_or_origin),
22806 false, context_die);
22808 if (origin == NULL && DECL_ARTIFICIAL (node))
22809 add_AT_flag (parm_die, DW_AT_artificial, 1);
22810 add_location:
22811 if (node && node != origin)
22812 equate_decl_number_to_die (node, parm_die);
22813 if (! DECL_ABSTRACT_P (node_or_origin))
22814 add_location_or_const_value_attribute (parm_die, node_or_origin,
22815 node == NULL);
22817 break;
22819 case tcc_type:
22820 /* We were called with some kind of a ..._TYPE node. */
22821 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22822 context_die);
22823 break;
22825 default:
22826 gcc_unreachable ();
22829 return parm_die;
22832 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22833 children DW_TAG_formal_parameter DIEs representing the arguments of the
22834 parameter pack.
22836 PARM_PACK must be a function parameter pack.
22837 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22838 must point to the subsequent arguments of the function PACK_ARG belongs to.
22839 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22840 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22841 following the last one for which a DIE was generated. */
22843 static dw_die_ref
22844 gen_formal_parameter_pack_die (tree parm_pack,
22845 tree pack_arg,
22846 dw_die_ref subr_die,
22847 tree *next_arg)
22849 tree arg;
22850 dw_die_ref parm_pack_die;
22852 gcc_assert (parm_pack
22853 && lang_hooks.function_parameter_pack_p (parm_pack)
22854 && subr_die);
22856 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22857 add_src_coords_attributes (parm_pack_die, parm_pack);
22859 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22861 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22862 parm_pack))
22863 break;
22864 gen_formal_parameter_die (arg, NULL,
22865 false /* Don't emit name attribute. */,
22866 parm_pack_die);
22868 if (next_arg)
22869 *next_arg = arg;
22870 return parm_pack_die;
22873 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22874 at the end of an (ANSI prototyped) formal parameters list. */
22876 static void
22877 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22879 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22882 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22883 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22884 parameters as specified in some function type specification (except for
22885 those which appear as part of a function *definition*). */
22887 static void
22888 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22890 tree link;
22891 tree formal_type = NULL;
22892 tree first_parm_type;
22893 tree arg;
22895 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22897 arg = DECL_ARGUMENTS (function_or_method_type);
22898 function_or_method_type = TREE_TYPE (function_or_method_type);
22900 else
22901 arg = NULL_TREE;
22903 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22905 /* Make our first pass over the list of formal parameter types and output a
22906 DW_TAG_formal_parameter DIE for each one. */
22907 for (link = first_parm_type; link; )
22909 dw_die_ref parm_die;
22911 formal_type = TREE_VALUE (link);
22912 if (formal_type == void_type_node)
22913 break;
22915 /* Output a (nameless) DIE to represent the formal parameter itself. */
22916 parm_die = gen_formal_parameter_die (formal_type, NULL,
22917 true /* Emit name attribute. */,
22918 context_die);
22919 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22920 && link == first_parm_type)
22922 add_AT_flag (parm_die, DW_AT_artificial, 1);
22923 if (dwarf_version >= 3 || !dwarf_strict)
22924 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22926 else if (arg && DECL_ARTIFICIAL (arg))
22927 add_AT_flag (parm_die, DW_AT_artificial, 1);
22929 link = TREE_CHAIN (link);
22930 if (arg)
22931 arg = DECL_CHAIN (arg);
22934 /* If this function type has an ellipsis, add a
22935 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22936 if (formal_type != void_type_node)
22937 gen_unspecified_parameters_die (function_or_method_type, context_die);
22939 /* Make our second (and final) pass over the list of formal parameter types
22940 and output DIEs to represent those types (as necessary). */
22941 for (link = TYPE_ARG_TYPES (function_or_method_type);
22942 link && TREE_VALUE (link);
22943 link = TREE_CHAIN (link))
22944 gen_type_die (TREE_VALUE (link), context_die);
22947 /* We want to generate the DIE for TYPE so that we can generate the
22948 die for MEMBER, which has been defined; we will need to refer back
22949 to the member declaration nested within TYPE. If we're trying to
22950 generate minimal debug info for TYPE, processing TYPE won't do the
22951 trick; we need to attach the member declaration by hand. */
22953 static void
22954 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22956 gen_type_die (type, context_die);
22958 /* If we're trying to avoid duplicate debug info, we may not have
22959 emitted the member decl for this function. Emit it now. */
22960 if (TYPE_STUB_DECL (type)
22961 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22962 && ! lookup_decl_die (member))
22964 dw_die_ref type_die;
22965 gcc_assert (!decl_ultimate_origin (member));
22967 type_die = lookup_type_die_strip_naming_typedef (type);
22968 if (TREE_CODE (member) == FUNCTION_DECL)
22969 gen_subprogram_die (member, type_die);
22970 else if (TREE_CODE (member) == FIELD_DECL)
22972 /* Ignore the nameless fields that are used to skip bits but handle
22973 C++ anonymous unions and structs. */
22974 if (DECL_NAME (member) != NULL_TREE
22975 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22976 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22978 struct vlr_context vlr_ctx = {
22979 DECL_CONTEXT (member), /* struct_type */
22980 NULL_TREE /* variant_part_offset */
22982 gen_type_die (member_declared_type (member), type_die);
22983 gen_field_die (member, &vlr_ctx, type_die);
22986 else
22987 gen_variable_die (member, NULL_TREE, type_die);
22991 /* Forward declare these functions, because they are mutually recursive
22992 with their set_block_* pairing functions. */
22993 static void set_decl_origin_self (tree);
22995 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22996 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22997 that it points to the node itself, thus indicating that the node is its
22998 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22999 the given node is NULL, recursively descend the decl/block tree which
23000 it is the root of, and for each other ..._DECL or BLOCK node contained
23001 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23002 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23003 values to point to themselves. */
23005 static void
23006 set_block_origin_self (tree stmt)
23008 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23010 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23013 tree local_decl;
23015 for (local_decl = BLOCK_VARS (stmt);
23016 local_decl != NULL_TREE;
23017 local_decl = DECL_CHAIN (local_decl))
23018 /* Do not recurse on nested functions since the inlining status
23019 of parent and child can be different as per the DWARF spec. */
23020 if (TREE_CODE (local_decl) != FUNCTION_DECL
23021 && !DECL_EXTERNAL (local_decl))
23022 set_decl_origin_self (local_decl);
23026 tree subblock;
23028 for (subblock = BLOCK_SUBBLOCKS (stmt);
23029 subblock != NULL_TREE;
23030 subblock = BLOCK_CHAIN (subblock))
23031 set_block_origin_self (subblock); /* Recurse. */
23036 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23037 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23038 node to so that it points to the node itself, thus indicating that the
23039 node represents its own (abstract) origin. Additionally, if the
23040 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23041 the decl/block tree of which the given node is the root of, and for
23042 each other ..._DECL or BLOCK node contained therein whose
23043 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23044 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23045 point to themselves. */
23047 static void
23048 set_decl_origin_self (tree decl)
23050 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23052 DECL_ABSTRACT_ORIGIN (decl) = decl;
23053 if (TREE_CODE (decl) == FUNCTION_DECL)
23055 tree arg;
23057 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23058 DECL_ABSTRACT_ORIGIN (arg) = arg;
23059 if (DECL_INITIAL (decl) != NULL_TREE
23060 && DECL_INITIAL (decl) != error_mark_node)
23061 set_block_origin_self (DECL_INITIAL (decl));
23066 /* Mark the early DIE for DECL as the abstract instance. */
23068 static void
23069 dwarf2out_abstract_function (tree decl)
23071 dw_die_ref old_die;
23073 /* Make sure we have the actual abstract inline, not a clone. */
23074 decl = DECL_ORIGIN (decl);
23076 if (DECL_IGNORED_P (decl))
23077 return;
23079 /* In LTO we're all set. We already created abstract instances
23080 early and we want to avoid creating a concrete instance of that
23081 if we don't output it. */
23082 if (in_lto_p)
23083 return;
23085 old_die = lookup_decl_die (decl);
23086 gcc_assert (old_die != NULL);
23087 if (get_AT (old_die, DW_AT_inline))
23088 /* We've already generated the abstract instance. */
23089 return;
23091 /* Go ahead and put DW_AT_inline on the DIE. */
23092 if (DECL_DECLARED_INLINE_P (decl))
23094 if (cgraph_function_possibly_inlined_p (decl))
23095 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23096 else
23097 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23099 else
23101 if (cgraph_function_possibly_inlined_p (decl))
23102 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23103 else
23104 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23107 if (DECL_DECLARED_INLINE_P (decl)
23108 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23109 add_AT_flag (old_die, DW_AT_artificial, 1);
23111 set_decl_origin_self (decl);
23114 /* Helper function of premark_used_types() which gets called through
23115 htab_traverse.
23117 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23118 marked as unused by prune_unused_types. */
23120 bool
23121 premark_used_types_helper (tree const &type, void *)
23123 dw_die_ref die;
23125 die = lookup_type_die (type);
23126 if (die != NULL)
23127 die->die_perennial_p = 1;
23128 return true;
23131 /* Helper function of premark_types_used_by_global_vars which gets called
23132 through htab_traverse.
23134 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23135 marked as unused by prune_unused_types. The DIE of the type is marked
23136 only if the global variable using the type will actually be emitted. */
23139 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23140 void *)
23142 struct types_used_by_vars_entry *entry;
23143 dw_die_ref die;
23145 entry = (struct types_used_by_vars_entry *) *slot;
23146 gcc_assert (entry->type != NULL
23147 && entry->var_decl != NULL);
23148 die = lookup_type_die (entry->type);
23149 if (die)
23151 /* Ask cgraph if the global variable really is to be emitted.
23152 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23153 varpool_node *node = varpool_node::get (entry->var_decl);
23154 if (node && node->definition)
23156 die->die_perennial_p = 1;
23157 /* Keep the parent DIEs as well. */
23158 while ((die = die->die_parent) && die->die_perennial_p == 0)
23159 die->die_perennial_p = 1;
23162 return 1;
23165 /* Mark all members of used_types_hash as perennial. */
23167 static void
23168 premark_used_types (struct function *fun)
23170 if (fun && fun->used_types_hash)
23171 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23174 /* Mark all members of types_used_by_vars_entry as perennial. */
23176 static void
23177 premark_types_used_by_global_vars (void)
23179 if (types_used_by_vars_hash)
23180 types_used_by_vars_hash
23181 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23184 /* Mark all variables used by the symtab as perennial. */
23186 static void
23187 premark_used_variables (void)
23189 /* Mark DIEs in the symtab as used. */
23190 varpool_node *var;
23191 FOR_EACH_VARIABLE (var)
23193 dw_die_ref die = lookup_decl_die (var->decl);
23194 if (die)
23195 die->die_perennial_p = 1;
23199 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23200 for CA_LOC call arg loc node. */
23202 static dw_die_ref
23203 gen_call_site_die (tree decl, dw_die_ref subr_die,
23204 struct call_arg_loc_node *ca_loc)
23206 dw_die_ref stmt_die = NULL, die;
23207 tree block = ca_loc->block;
23209 while (block
23210 && block != DECL_INITIAL (decl)
23211 && TREE_CODE (block) == BLOCK)
23213 stmt_die = lookup_block_die (block);
23214 if (stmt_die)
23215 break;
23216 block = BLOCK_SUPERCONTEXT (block);
23218 if (stmt_die == NULL)
23219 stmt_die = subr_die;
23220 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23221 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23222 if (ca_loc->tail_call_p)
23223 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23224 if (ca_loc->symbol_ref)
23226 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23227 if (tdie)
23228 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23229 else
23230 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23231 false);
23233 return die;
23236 /* Generate a DIE to represent a declared function (either file-scope or
23237 block-local). */
23239 static void
23240 gen_subprogram_die (tree decl, dw_die_ref context_die)
23242 tree origin = decl_ultimate_origin (decl);
23243 dw_die_ref subr_die;
23244 dw_die_ref old_die = lookup_decl_die (decl);
23245 bool old_die_had_no_children = false;
23247 /* This function gets called multiple times for different stages of
23248 the debug process. For example, for func() in this code:
23250 namespace S
23252 void func() { ... }
23255 ...we get called 4 times. Twice in early debug and twice in
23256 late debug:
23258 Early debug
23259 -----------
23261 1. Once while generating func() within the namespace. This is
23262 the declaration. The declaration bit below is set, as the
23263 context is the namespace.
23265 A new DIE will be generated with DW_AT_declaration set.
23267 2. Once for func() itself. This is the specification. The
23268 declaration bit below is clear as the context is the CU.
23270 We will use the cached DIE from (1) to create a new DIE with
23271 DW_AT_specification pointing to the declaration in (1).
23273 Late debug via rest_of_handle_final()
23274 -------------------------------------
23276 3. Once generating func() within the namespace. This is also the
23277 declaration, as in (1), but this time we will early exit below
23278 as we have a cached DIE and a declaration needs no additional
23279 annotations (no locations), as the source declaration line
23280 info is enough.
23282 4. Once for func() itself. As in (2), this is the specification,
23283 but this time we will re-use the cached DIE, and just annotate
23284 it with the location information that should now be available.
23286 For something without namespaces, but with abstract instances, we
23287 are also called a multiple times:
23289 class Base
23291 public:
23292 Base (); // constructor declaration (1)
23295 Base::Base () { } // constructor specification (2)
23297 Early debug
23298 -----------
23300 1. Once for the Base() constructor by virtue of it being a
23301 member of the Base class. This is done via
23302 rest_of_type_compilation.
23304 This is a declaration, so a new DIE will be created with
23305 DW_AT_declaration.
23307 2. Once for the Base() constructor definition, but this time
23308 while generating the abstract instance of the base
23309 constructor (__base_ctor) which is being generated via early
23310 debug of reachable functions.
23312 Even though we have a cached version of the declaration (1),
23313 we will create a DW_AT_specification of the declaration DIE
23314 in (1).
23316 3. Once for the __base_ctor itself, but this time, we generate
23317 an DW_AT_abstract_origin version of the DW_AT_specification in
23318 (2).
23320 Late debug via rest_of_handle_final
23321 -----------------------------------
23323 4. One final time for the __base_ctor (which will have a cached
23324 DIE with DW_AT_abstract_origin created in (3). This time,
23325 we will just annotate the location information now
23326 available.
23328 int declaration = (current_function_decl != decl
23329 || (!DECL_INITIAL (decl) && !origin)
23330 || class_or_namespace_scope_p (context_die));
23332 /* A declaration that has been previously dumped needs no
23333 additional information. */
23334 if (old_die && declaration)
23335 return;
23337 if (in_lto_p && old_die && old_die->die_child == NULL)
23338 old_die_had_no_children = true;
23340 /* Now that the C++ front end lazily declares artificial member fns, we
23341 might need to retrofit the declaration into its class. */
23342 if (!declaration && !origin && !old_die
23343 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23344 && !class_or_namespace_scope_p (context_die)
23345 && debug_info_level > DINFO_LEVEL_TERSE)
23346 old_die = force_decl_die (decl);
23348 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23349 if (origin != NULL)
23351 gcc_assert (!declaration || local_scope_p (context_die));
23353 /* Fixup die_parent for the abstract instance of a nested
23354 inline function. */
23355 if (old_die && old_die->die_parent == NULL)
23356 add_child_die (context_die, old_die);
23358 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23360 /* If we have a DW_AT_abstract_origin we have a working
23361 cached version. */
23362 subr_die = old_die;
23364 else
23366 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23367 add_abstract_origin_attribute (subr_die, origin);
23368 /* This is where the actual code for a cloned function is.
23369 Let's emit linkage name attribute for it. This helps
23370 debuggers to e.g, set breakpoints into
23371 constructors/destructors when the user asks "break
23372 K::K". */
23373 add_linkage_name (subr_die, decl);
23376 /* A cached copy, possibly from early dwarf generation. Reuse as
23377 much as possible. */
23378 else if (old_die)
23380 if (!get_AT_flag (old_die, DW_AT_declaration)
23381 /* We can have a normal definition following an inline one in the
23382 case of redefinition of GNU C extern inlines.
23383 It seems reasonable to use AT_specification in this case. */
23384 && !get_AT (old_die, DW_AT_inline))
23386 /* Detect and ignore this case, where we are trying to output
23387 something we have already output. */
23388 if (get_AT (old_die, DW_AT_low_pc)
23389 || get_AT (old_die, DW_AT_ranges))
23390 return;
23392 /* If we have no location information, this must be a
23393 partially generated DIE from early dwarf generation.
23394 Fall through and generate it. */
23397 /* If the definition comes from the same place as the declaration,
23398 maybe use the old DIE. We always want the DIE for this function
23399 that has the *_pc attributes to be under comp_unit_die so the
23400 debugger can find it. We also need to do this for abstract
23401 instances of inlines, since the spec requires the out-of-line copy
23402 to have the same parent. For local class methods, this doesn't
23403 apply; we just use the old DIE. */
23404 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23405 struct dwarf_file_data * file_index = lookup_filename (s.file);
23406 if (((is_unit_die (old_die->die_parent)
23407 /* This condition fixes the inconsistency/ICE with the
23408 following Fortran test (or some derivative thereof) while
23409 building libgfortran:
23411 module some_m
23412 contains
23413 logical function funky (FLAG)
23414 funky = .true.
23415 end function
23416 end module
23418 || (old_die->die_parent
23419 && old_die->die_parent->die_tag == DW_TAG_module)
23420 || local_scope_p (old_die->die_parent)
23421 || context_die == NULL)
23422 && (DECL_ARTIFICIAL (decl)
23423 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23424 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23425 == (unsigned) s.line)
23426 && (!debug_column_info
23427 || s.column == 0
23428 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23429 == (unsigned) s.column)))))
23430 /* With LTO if there's an abstract instance for
23431 the old DIE, this is a concrete instance and
23432 thus re-use the DIE. */
23433 || get_AT (old_die, DW_AT_abstract_origin))
23435 subr_die = old_die;
23437 /* Clear out the declaration attribute, but leave the
23438 parameters so they can be augmented with location
23439 information later. Unless this was a declaration, in
23440 which case, wipe out the nameless parameters and recreate
23441 them further down. */
23442 if (remove_AT (subr_die, DW_AT_declaration))
23445 remove_AT (subr_die, DW_AT_object_pointer);
23446 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23449 /* Make a specification pointing to the previously built
23450 declaration. */
23451 else
23453 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23454 add_AT_specification (subr_die, old_die);
23455 add_pubname (decl, subr_die);
23456 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23457 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23458 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23459 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23460 if (debug_column_info
23461 && s.column
23462 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23463 != (unsigned) s.column))
23464 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23466 /* If the prototype had an 'auto' or 'decltype(auto)' in
23467 the return type, emit the real type on the definition die. */
23468 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23470 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23471 while (die
23472 && (die->die_tag == DW_TAG_reference_type
23473 || die->die_tag == DW_TAG_rvalue_reference_type
23474 || die->die_tag == DW_TAG_pointer_type
23475 || die->die_tag == DW_TAG_const_type
23476 || die->die_tag == DW_TAG_volatile_type
23477 || die->die_tag == DW_TAG_restrict_type
23478 || die->die_tag == DW_TAG_array_type
23479 || die->die_tag == DW_TAG_ptr_to_member_type
23480 || die->die_tag == DW_TAG_subroutine_type))
23481 die = get_AT_ref (die, DW_AT_type);
23482 if (die == auto_die || die == decltype_auto_die)
23483 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23484 TYPE_UNQUALIFIED, false, context_die);
23487 /* When we process the method declaration, we haven't seen
23488 the out-of-class defaulted definition yet, so we have to
23489 recheck now. */
23490 if ((dwarf_version >= 5 || ! dwarf_strict)
23491 && !get_AT (subr_die, DW_AT_defaulted))
23493 int defaulted
23494 = lang_hooks.decls.decl_dwarf_attribute (decl,
23495 DW_AT_defaulted);
23496 if (defaulted != -1)
23498 /* Other values must have been handled before. */
23499 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23500 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23505 /* Create a fresh DIE for anything else. */
23506 else
23508 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23510 if (TREE_PUBLIC (decl))
23511 add_AT_flag (subr_die, DW_AT_external, 1);
23513 add_name_and_src_coords_attributes (subr_die, decl);
23514 add_pubname (decl, subr_die);
23515 if (debug_info_level > DINFO_LEVEL_TERSE)
23517 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23518 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23519 TYPE_UNQUALIFIED, false, context_die);
23522 add_pure_or_virtual_attribute (subr_die, decl);
23523 if (DECL_ARTIFICIAL (decl))
23524 add_AT_flag (subr_die, DW_AT_artificial, 1);
23526 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23527 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23529 add_alignment_attribute (subr_die, decl);
23531 add_accessibility_attribute (subr_die, decl);
23534 /* Unless we have an existing non-declaration DIE, equate the new
23535 DIE. */
23536 if (!old_die || is_declaration_die (old_die))
23537 equate_decl_number_to_die (decl, subr_die);
23539 if (declaration)
23541 if (!old_die || !get_AT (old_die, DW_AT_inline))
23543 add_AT_flag (subr_die, DW_AT_declaration, 1);
23545 /* If this is an explicit function declaration then generate
23546 a DW_AT_explicit attribute. */
23547 if ((dwarf_version >= 3 || !dwarf_strict)
23548 && lang_hooks.decls.decl_dwarf_attribute (decl,
23549 DW_AT_explicit) == 1)
23550 add_AT_flag (subr_die, DW_AT_explicit, 1);
23552 /* If this is a C++11 deleted special function member then generate
23553 a DW_AT_deleted attribute. */
23554 if ((dwarf_version >= 5 || !dwarf_strict)
23555 && lang_hooks.decls.decl_dwarf_attribute (decl,
23556 DW_AT_deleted) == 1)
23557 add_AT_flag (subr_die, DW_AT_deleted, 1);
23559 /* If this is a C++11 defaulted special function member then
23560 generate a DW_AT_defaulted attribute. */
23561 if (dwarf_version >= 5 || !dwarf_strict)
23563 int defaulted
23564 = lang_hooks.decls.decl_dwarf_attribute (decl,
23565 DW_AT_defaulted);
23566 if (defaulted != -1)
23567 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23570 /* If this is a C++11 non-static member function with & ref-qualifier
23571 then generate a DW_AT_reference attribute. */
23572 if ((dwarf_version >= 5 || !dwarf_strict)
23573 && lang_hooks.decls.decl_dwarf_attribute (decl,
23574 DW_AT_reference) == 1)
23575 add_AT_flag (subr_die, DW_AT_reference, 1);
23577 /* If this is a C++11 non-static member function with &&
23578 ref-qualifier then generate a DW_AT_reference attribute. */
23579 if ((dwarf_version >= 5 || !dwarf_strict)
23580 && lang_hooks.decls.decl_dwarf_attribute (decl,
23581 DW_AT_rvalue_reference)
23582 == 1)
23583 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23586 /* For non DECL_EXTERNALs, if range information is available, fill
23587 the DIE with it. */
23588 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23590 HOST_WIDE_INT cfa_fb_offset;
23592 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23594 if (!crtl->has_bb_partition)
23596 dw_fde_ref fde = fun->fde;
23597 if (fde->dw_fde_begin)
23599 /* We have already generated the labels. */
23600 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23601 fde->dw_fde_end, false);
23603 else
23605 /* Create start/end labels and add the range. */
23606 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23607 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23608 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23609 current_function_funcdef_no);
23610 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23611 current_function_funcdef_no);
23612 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23613 false);
23616 #if VMS_DEBUGGING_INFO
23617 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23618 Section 2.3 Prologue and Epilogue Attributes:
23619 When a breakpoint is set on entry to a function, it is generally
23620 desirable for execution to be suspended, not on the very first
23621 instruction of the function, but rather at a point after the
23622 function's frame has been set up, after any language defined local
23623 declaration processing has been completed, and before execution of
23624 the first statement of the function begins. Debuggers generally
23625 cannot properly determine where this point is. Similarly for a
23626 breakpoint set on exit from a function. The prologue and epilogue
23627 attributes allow a compiler to communicate the location(s) to use. */
23630 if (fde->dw_fde_vms_end_prologue)
23631 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23632 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23634 if (fde->dw_fde_vms_begin_epilogue)
23635 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23636 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23638 #endif
23641 else
23643 /* Generate pubnames entries for the split function code ranges. */
23644 dw_fde_ref fde = fun->fde;
23646 if (fde->dw_fde_second_begin)
23648 if (dwarf_version >= 3 || !dwarf_strict)
23650 /* We should use ranges for non-contiguous code section
23651 addresses. Use the actual code range for the initial
23652 section, since the HOT/COLD labels might precede an
23653 alignment offset. */
23654 bool range_list_added = false;
23655 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23656 fde->dw_fde_end, &range_list_added,
23657 false);
23658 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23659 fde->dw_fde_second_end,
23660 &range_list_added, false);
23661 if (range_list_added)
23662 add_ranges (NULL);
23664 else
23666 /* There is no real support in DW2 for this .. so we make
23667 a work-around. First, emit the pub name for the segment
23668 containing the function label. Then make and emit a
23669 simplified subprogram DIE for the second segment with the
23670 name pre-fixed by __hot/cold_sect_of_. We use the same
23671 linkage name for the second die so that gdb will find both
23672 sections when given "b foo". */
23673 const char *name = NULL;
23674 tree decl_name = DECL_NAME (decl);
23675 dw_die_ref seg_die;
23677 /* Do the 'primary' section. */
23678 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23679 fde->dw_fde_end, false);
23681 /* Build a minimal DIE for the secondary section. */
23682 seg_die = new_die (DW_TAG_subprogram,
23683 subr_die->die_parent, decl);
23685 if (TREE_PUBLIC (decl))
23686 add_AT_flag (seg_die, DW_AT_external, 1);
23688 if (decl_name != NULL
23689 && IDENTIFIER_POINTER (decl_name) != NULL)
23691 name = dwarf2_name (decl, 1);
23692 if (! DECL_ARTIFICIAL (decl))
23693 add_src_coords_attributes (seg_die, decl);
23695 add_linkage_name (seg_die, decl);
23697 gcc_assert (name != NULL);
23698 add_pure_or_virtual_attribute (seg_die, decl);
23699 if (DECL_ARTIFICIAL (decl))
23700 add_AT_flag (seg_die, DW_AT_artificial, 1);
23702 name = concat ("__second_sect_of_", name, NULL);
23703 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23704 fde->dw_fde_second_end, false);
23705 add_name_attribute (seg_die, name);
23706 if (want_pubnames ())
23707 add_pubname_string (name, seg_die);
23710 else
23711 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23712 false);
23715 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23717 /* We define the "frame base" as the function's CFA. This is more
23718 convenient for several reasons: (1) It's stable across the prologue
23719 and epilogue, which makes it better than just a frame pointer,
23720 (2) With dwarf3, there exists a one-byte encoding that allows us
23721 to reference the .debug_frame data by proxy, but failing that,
23722 (3) We can at least reuse the code inspection and interpretation
23723 code that determines the CFA position at various points in the
23724 function. */
23725 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23727 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23728 add_AT_loc (subr_die, DW_AT_frame_base, op);
23730 else
23732 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23733 if (list->dw_loc_next)
23734 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23735 else
23736 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23739 /* Compute a displacement from the "steady-state frame pointer" to
23740 the CFA. The former is what all stack slots and argument slots
23741 will reference in the rtl; the latter is what we've told the
23742 debugger about. We'll need to adjust all frame_base references
23743 by this displacement. */
23744 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23746 if (fun->static_chain_decl)
23748 /* DWARF requires here a location expression that computes the
23749 address of the enclosing subprogram's frame base. The machinery
23750 in tree-nested.c is supposed to store this specific address in the
23751 last field of the FRAME record. */
23752 const tree frame_type
23753 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23754 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23756 tree fb_expr
23757 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23758 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23759 fb_expr, fb_decl, NULL_TREE);
23761 add_AT_location_description (subr_die, DW_AT_static_link,
23762 loc_list_from_tree (fb_expr, 0, NULL));
23765 resolve_variable_values ();
23768 /* Generate child dies for template parameters. */
23769 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23770 gen_generic_params_dies (decl);
23772 /* Now output descriptions of the arguments for this function. This gets
23773 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23774 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23775 `...' at the end of the formal parameter list. In order to find out if
23776 there was a trailing ellipsis or not, we must instead look at the type
23777 associated with the FUNCTION_DECL. This will be a node of type
23778 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23779 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23780 an ellipsis at the end. */
23782 /* In the case where we are describing a mere function declaration, all we
23783 need to do here (and all we *can* do here) is to describe the *types* of
23784 its formal parameters. */
23785 if (debug_info_level <= DINFO_LEVEL_TERSE)
23787 else if (declaration)
23788 gen_formal_types_die (decl, subr_die);
23789 else
23791 /* Generate DIEs to represent all known formal parameters. */
23792 tree parm = DECL_ARGUMENTS (decl);
23793 tree generic_decl = early_dwarf
23794 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23795 tree generic_decl_parm = generic_decl
23796 ? DECL_ARGUMENTS (generic_decl)
23797 : NULL;
23799 /* Now we want to walk the list of parameters of the function and
23800 emit their relevant DIEs.
23802 We consider the case of DECL being an instance of a generic function
23803 as well as it being a normal function.
23805 If DECL is an instance of a generic function we walk the
23806 parameters of the generic function declaration _and_ the parameters of
23807 DECL itself. This is useful because we want to emit specific DIEs for
23808 function parameter packs and those are declared as part of the
23809 generic function declaration. In that particular case,
23810 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23811 That DIE has children DIEs representing the set of arguments
23812 of the pack. Note that the set of pack arguments can be empty.
23813 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23814 children DIE.
23816 Otherwise, we just consider the parameters of DECL. */
23817 while (generic_decl_parm || parm)
23819 if (generic_decl_parm
23820 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23821 gen_formal_parameter_pack_die (generic_decl_parm,
23822 parm, subr_die,
23823 &parm);
23824 else if (parm)
23826 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23828 if (early_dwarf
23829 && parm == DECL_ARGUMENTS (decl)
23830 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23831 && parm_die
23832 && (dwarf_version >= 3 || !dwarf_strict))
23833 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23835 parm = DECL_CHAIN (parm);
23838 if (generic_decl_parm)
23839 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23842 /* Decide whether we need an unspecified_parameters DIE at the end.
23843 There are 2 more cases to do this for: 1) the ansi ... declaration -
23844 this is detectable when the end of the arg list is not a
23845 void_type_node 2) an unprototyped function declaration (not a
23846 definition). This just means that we have no info about the
23847 parameters at all. */
23848 if (early_dwarf)
23850 if (prototype_p (TREE_TYPE (decl)))
23852 /* This is the prototyped case, check for.... */
23853 if (stdarg_p (TREE_TYPE (decl)))
23854 gen_unspecified_parameters_die (decl, subr_die);
23856 else if (DECL_INITIAL (decl) == NULL_TREE)
23857 gen_unspecified_parameters_die (decl, subr_die);
23859 else if ((subr_die != old_die || old_die_had_no_children)
23860 && prototype_p (TREE_TYPE (decl))
23861 && stdarg_p (TREE_TYPE (decl)))
23862 gen_unspecified_parameters_die (decl, subr_die);
23865 if (subr_die != old_die)
23866 /* Add the calling convention attribute if requested. */
23867 add_calling_convention_attribute (subr_die, decl);
23869 /* Output Dwarf info for all of the stuff within the body of the function
23870 (if it has one - it may be just a declaration).
23872 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23873 a function. This BLOCK actually represents the outermost binding contour
23874 for the function, i.e. the contour in which the function's formal
23875 parameters and labels get declared. Curiously, it appears that the front
23876 end doesn't actually put the PARM_DECL nodes for the current function onto
23877 the BLOCK_VARS list for this outer scope, but are strung off of the
23878 DECL_ARGUMENTS list for the function instead.
23880 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23881 the LABEL_DECL nodes for the function however, and we output DWARF info
23882 for those in decls_for_scope. Just within the `outer_scope' there will be
23883 a BLOCK node representing the function's outermost pair of curly braces,
23884 and any blocks used for the base and member initializers of a C++
23885 constructor function. */
23886 tree outer_scope = DECL_INITIAL (decl);
23887 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23889 int call_site_note_count = 0;
23890 int tail_call_site_note_count = 0;
23892 /* Emit a DW_TAG_variable DIE for a named return value. */
23893 if (DECL_NAME (DECL_RESULT (decl)))
23894 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23896 /* The first time through decls_for_scope we will generate the
23897 DIEs for the locals. The second time, we fill in the
23898 location info. */
23899 decls_for_scope (outer_scope, subr_die);
23901 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23903 struct call_arg_loc_node *ca_loc;
23904 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23906 dw_die_ref die = NULL;
23907 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23908 rtx arg, next_arg;
23909 tree arg_decl = NULL_TREE;
23911 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23912 ? XEXP (ca_loc->call_arg_loc_note, 0)
23913 : NULL_RTX);
23914 arg; arg = next_arg)
23916 dw_loc_descr_ref reg, val;
23917 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23918 dw_die_ref cdie, tdie = NULL;
23920 next_arg = XEXP (arg, 1);
23921 if (REG_P (XEXP (XEXP (arg, 0), 0))
23922 && next_arg
23923 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23924 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23925 && REGNO (XEXP (XEXP (arg, 0), 0))
23926 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23927 next_arg = XEXP (next_arg, 1);
23928 if (mode == VOIDmode)
23930 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23931 if (mode == VOIDmode)
23932 mode = GET_MODE (XEXP (arg, 0));
23934 if (mode == VOIDmode || mode == BLKmode)
23935 continue;
23936 /* Get dynamic information about call target only if we
23937 have no static information: we cannot generate both
23938 DW_AT_call_origin and DW_AT_call_target
23939 attributes. */
23940 if (ca_loc->symbol_ref == NULL_RTX)
23942 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23944 tloc = XEXP (XEXP (arg, 0), 1);
23945 continue;
23947 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23948 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23950 tlocc = XEXP (XEXP (arg, 0), 1);
23951 continue;
23954 reg = NULL;
23955 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23956 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23957 VAR_INIT_STATUS_INITIALIZED);
23958 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23960 rtx mem = XEXP (XEXP (arg, 0), 0);
23961 reg = mem_loc_descriptor (XEXP (mem, 0),
23962 get_address_mode (mem),
23963 GET_MODE (mem),
23964 VAR_INIT_STATUS_INITIALIZED);
23966 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23967 == DEBUG_PARAMETER_REF)
23969 tree tdecl
23970 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23971 tdie = lookup_decl_die (tdecl);
23972 if (tdie == NULL)
23973 continue;
23974 arg_decl = tdecl;
23976 else
23977 continue;
23978 if (reg == NULL
23979 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23980 != DEBUG_PARAMETER_REF)
23981 continue;
23982 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23983 VOIDmode,
23984 VAR_INIT_STATUS_INITIALIZED);
23985 if (val == NULL)
23986 continue;
23987 if (die == NULL)
23988 die = gen_call_site_die (decl, subr_die, ca_loc);
23989 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23990 NULL_TREE);
23991 add_desc_attribute (cdie, arg_decl);
23992 if (reg != NULL)
23993 add_AT_loc (cdie, DW_AT_location, reg);
23994 else if (tdie != NULL)
23995 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23996 tdie);
23997 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23998 if (next_arg != XEXP (arg, 1))
24000 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24001 if (mode == VOIDmode)
24002 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24003 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24004 0), 1),
24005 mode, VOIDmode,
24006 VAR_INIT_STATUS_INITIALIZED);
24007 if (val != NULL)
24008 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24009 val);
24012 if (die == NULL
24013 && (ca_loc->symbol_ref || tloc))
24014 die = gen_call_site_die (decl, subr_die, ca_loc);
24015 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24017 dw_loc_descr_ref tval = NULL;
24019 if (tloc != NULL_RTX)
24020 tval = mem_loc_descriptor (tloc,
24021 GET_MODE (tloc) == VOIDmode
24022 ? Pmode : GET_MODE (tloc),
24023 VOIDmode,
24024 VAR_INIT_STATUS_INITIALIZED);
24025 if (tval)
24026 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24027 else if (tlocc != NULL_RTX)
24029 tval = mem_loc_descriptor (tlocc,
24030 GET_MODE (tlocc) == VOIDmode
24031 ? Pmode : GET_MODE (tlocc),
24032 VOIDmode,
24033 VAR_INIT_STATUS_INITIALIZED);
24034 if (tval)
24035 add_AT_loc (die,
24036 dwarf_AT (DW_AT_call_target_clobbered),
24037 tval);
24040 if (die != NULL)
24042 call_site_note_count++;
24043 if (ca_loc->tail_call_p)
24044 tail_call_site_note_count++;
24048 call_arg_locations = NULL;
24049 call_arg_loc_last = NULL;
24050 if (tail_call_site_count >= 0
24051 && tail_call_site_count == tail_call_site_note_count
24052 && (!dwarf_strict || dwarf_version >= 5))
24054 if (call_site_count >= 0
24055 && call_site_count == call_site_note_count)
24056 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24057 else
24058 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24060 call_site_count = -1;
24061 tail_call_site_count = -1;
24064 /* Mark used types after we have created DIEs for the functions scopes. */
24065 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24068 /* Returns a hash value for X (which really is a die_struct). */
24070 hashval_t
24071 block_die_hasher::hash (die_struct *d)
24073 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24076 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24077 as decl_id and die_parent of die_struct Y. */
24079 bool
24080 block_die_hasher::equal (die_struct *x, die_struct *y)
24082 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24085 /* Hold information about markers for inlined entry points. */
24086 struct GTY ((for_user)) inline_entry_data
24088 /* The block that's the inlined_function_outer_scope for an inlined
24089 function. */
24090 tree block;
24092 /* The label at the inlined entry point. */
24093 const char *label_pfx;
24094 unsigned int label_num;
24096 /* The view number to be used as the inlined entry point. */
24097 var_loc_view view;
24100 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24102 typedef tree compare_type;
24103 static inline hashval_t hash (const inline_entry_data *);
24104 static inline bool equal (const inline_entry_data *, const_tree);
24107 /* Hash table routines for inline_entry_data. */
24109 inline hashval_t
24110 inline_entry_data_hasher::hash (const inline_entry_data *data)
24112 return htab_hash_pointer (data->block);
24115 inline bool
24116 inline_entry_data_hasher::equal (const inline_entry_data *data,
24117 const_tree block)
24119 return data->block == block;
24122 /* Inlined entry points pending DIE creation in this compilation unit. */
24124 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24127 /* Return TRUE if DECL, which may have been previously generated as
24128 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24129 true if decl (or its origin) is either an extern declaration or a
24130 class/namespace scoped declaration.
24132 The declare_in_namespace support causes us to get two DIEs for one
24133 variable, both of which are declarations. We want to avoid
24134 considering one to be a specification, so we must test for
24135 DECLARATION and DW_AT_declaration. */
24136 static inline bool
24137 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24139 return (old_die && TREE_STATIC (decl) && !declaration
24140 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24143 /* Return true if DECL is a local static. */
24145 static inline bool
24146 local_function_static (tree decl)
24148 gcc_assert (VAR_P (decl));
24149 return TREE_STATIC (decl)
24150 && DECL_CONTEXT (decl)
24151 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24154 /* Return true iff DECL overrides (presumably completes) the type of
24155 OLD_DIE within CONTEXT_DIE. */
24157 static bool
24158 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24159 dw_die_ref context_die)
24161 tree type = TREE_TYPE (decl);
24162 int cv_quals;
24164 if (decl_by_reference_p (decl))
24166 type = TREE_TYPE (type);
24167 cv_quals = TYPE_UNQUALIFIED;
24169 else
24170 cv_quals = decl_quals (decl);
24172 dw_die_ref type_die = modified_type_die (type,
24173 cv_quals | TYPE_QUALS (type),
24174 false,
24175 context_die);
24177 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24179 return type_die != old_type_die;
24182 /* Generate a DIE to represent a declared data object.
24183 Either DECL or ORIGIN must be non-null. */
24185 static void
24186 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24188 HOST_WIDE_INT off = 0;
24189 tree com_decl;
24190 tree decl_or_origin = decl ? decl : origin;
24191 tree ultimate_origin;
24192 dw_die_ref var_die;
24193 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24194 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24195 || class_or_namespace_scope_p (context_die));
24196 bool specialization_p = false;
24197 bool no_linkage_name = false;
24199 /* While C++ inline static data members have definitions inside of the
24200 class, force the first DIE to be a declaration, then let gen_member_die
24201 reparent it to the class context and call gen_variable_die again
24202 to create the outside of the class DIE for the definition. */
24203 if (!declaration
24204 && old_die == NULL
24205 && decl
24206 && DECL_CONTEXT (decl)
24207 && TYPE_P (DECL_CONTEXT (decl))
24208 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24210 declaration = true;
24211 if (dwarf_version < 5)
24212 no_linkage_name = true;
24215 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24216 if (decl || ultimate_origin)
24217 origin = ultimate_origin;
24218 com_decl = fortran_common (decl_or_origin, &off);
24220 /* Symbol in common gets emitted as a child of the common block, in the form
24221 of a data member. */
24222 if (com_decl)
24224 dw_die_ref com_die;
24225 dw_loc_list_ref loc = NULL;
24226 die_node com_die_arg;
24228 var_die = lookup_decl_die (decl_or_origin);
24229 if (var_die)
24231 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24233 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24234 if (loc)
24236 if (off)
24238 /* Optimize the common case. */
24239 if (single_element_loc_list_p (loc)
24240 && loc->expr->dw_loc_opc == DW_OP_addr
24241 && loc->expr->dw_loc_next == NULL
24242 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24243 == SYMBOL_REF)
24245 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24246 loc->expr->dw_loc_oprnd1.v.val_addr
24247 = plus_constant (GET_MODE (x), x , off);
24249 else
24250 loc_list_plus_const (loc, off);
24252 add_AT_location_description (var_die, DW_AT_location, loc);
24253 remove_AT (var_die, DW_AT_declaration);
24256 return;
24259 if (common_block_die_table == NULL)
24260 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24262 com_die_arg.decl_id = DECL_UID (com_decl);
24263 com_die_arg.die_parent = context_die;
24264 com_die = common_block_die_table->find (&com_die_arg);
24265 if (! early_dwarf)
24266 loc = loc_list_from_tree (com_decl, 2, NULL);
24267 if (com_die == NULL)
24269 const char *cnam
24270 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24271 die_node **slot;
24273 com_die = new_die (DW_TAG_common_block, context_die, decl);
24274 add_name_and_src_coords_attributes (com_die, com_decl);
24275 if (loc)
24277 add_AT_location_description (com_die, DW_AT_location, loc);
24278 /* Avoid sharing the same loc descriptor between
24279 DW_TAG_common_block and DW_TAG_variable. */
24280 loc = loc_list_from_tree (com_decl, 2, NULL);
24282 else if (DECL_EXTERNAL (decl_or_origin))
24283 add_AT_flag (com_die, DW_AT_declaration, 1);
24284 if (want_pubnames ())
24285 add_pubname_string (cnam, com_die); /* ??? needed? */
24286 com_die->decl_id = DECL_UID (com_decl);
24287 slot = common_block_die_table->find_slot (com_die, INSERT);
24288 *slot = com_die;
24290 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24292 add_AT_location_description (com_die, DW_AT_location, loc);
24293 loc = loc_list_from_tree (com_decl, 2, NULL);
24294 remove_AT (com_die, DW_AT_declaration);
24296 var_die = new_die (DW_TAG_variable, com_die, decl);
24297 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24298 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24299 decl_quals (decl_or_origin), false,
24300 context_die);
24301 add_alignment_attribute (var_die, decl);
24302 add_AT_flag (var_die, DW_AT_external, 1);
24303 if (loc)
24305 if (off)
24307 /* Optimize the common case. */
24308 if (single_element_loc_list_p (loc)
24309 && loc->expr->dw_loc_opc == DW_OP_addr
24310 && loc->expr->dw_loc_next == NULL
24311 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24313 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24314 loc->expr->dw_loc_oprnd1.v.val_addr
24315 = plus_constant (GET_MODE (x), x, off);
24317 else
24318 loc_list_plus_const (loc, off);
24320 add_AT_location_description (var_die, DW_AT_location, loc);
24322 else if (DECL_EXTERNAL (decl_or_origin))
24323 add_AT_flag (var_die, DW_AT_declaration, 1);
24324 if (decl)
24325 equate_decl_number_to_die (decl, var_die);
24326 return;
24329 if (old_die)
24331 if (declaration)
24333 /* A declaration that has been previously dumped, needs no
24334 further annotations, since it doesn't need location on
24335 the second pass. */
24336 return;
24338 else if (decl_will_get_specification_p (old_die, decl, declaration)
24339 && !get_AT (old_die, DW_AT_specification))
24341 /* Fall-thru so we can make a new variable die along with a
24342 DW_AT_specification. */
24344 else if (origin && old_die->die_parent != context_die)
24346 /* If we will be creating an inlined instance, we need a
24347 new DIE that will get annotated with
24348 DW_AT_abstract_origin. */
24349 gcc_assert (!DECL_ABSTRACT_P (decl));
24351 else
24353 /* If a DIE was dumped early, it still needs location info.
24354 Skip to where we fill the location bits. */
24355 var_die = old_die;
24357 /* ??? In LTRANS we cannot annotate early created variably
24358 modified type DIEs without copying them and adjusting all
24359 references to them. Thus we dumped them again. Also add a
24360 reference to them but beware of -g0 compile and -g link
24361 in which case the reference will be already present. */
24362 tree type = TREE_TYPE (decl_or_origin);
24363 if (in_lto_p
24364 && ! get_AT (var_die, DW_AT_type)
24365 && variably_modified_type_p
24366 (type, decl_function_context (decl_or_origin)))
24368 if (decl_by_reference_p (decl_or_origin))
24369 add_type_attribute (var_die, TREE_TYPE (type),
24370 TYPE_UNQUALIFIED, false, context_die);
24371 else
24372 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24373 false, context_die);
24376 goto gen_variable_die_location;
24380 /* For static data members, the declaration in the class is supposed
24381 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24382 also in DWARF2; the specification should still be DW_TAG_variable
24383 referencing the DW_TAG_member DIE. */
24384 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24385 var_die = new_die (DW_TAG_member, context_die, decl);
24386 else
24387 var_die = new_die (DW_TAG_variable, context_die, decl);
24389 if (origin != NULL)
24390 add_abstract_origin_attribute (var_die, origin);
24392 /* Loop unrolling can create multiple blocks that refer to the same
24393 static variable, so we must test for the DW_AT_declaration flag.
24395 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24396 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24397 sharing them.
24399 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24400 else if (decl_will_get_specification_p (old_die, decl, declaration))
24402 /* This is a definition of a C++ class level static. */
24403 add_AT_specification (var_die, old_die);
24404 specialization_p = true;
24405 if (DECL_NAME (decl))
24407 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24408 struct dwarf_file_data * file_index = lookup_filename (s.file);
24410 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24411 add_AT_file (var_die, DW_AT_decl_file, file_index);
24413 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24414 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24416 if (debug_column_info
24417 && s.column
24418 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24419 != (unsigned) s.column))
24420 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24422 if (old_die->die_tag == DW_TAG_member)
24423 add_linkage_name (var_die, decl);
24426 else
24427 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24429 if ((origin == NULL && !specialization_p)
24430 || (origin != NULL
24431 && !DECL_ABSTRACT_P (decl_or_origin)
24432 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24433 decl_function_context
24434 (decl_or_origin)))
24435 || (old_die && specialization_p
24436 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24438 tree type = TREE_TYPE (decl_or_origin);
24440 if (decl_by_reference_p (decl_or_origin))
24441 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24442 context_die);
24443 else
24444 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24445 context_die);
24448 if (origin == NULL && !specialization_p)
24450 if (TREE_PUBLIC (decl))
24451 add_AT_flag (var_die, DW_AT_external, 1);
24453 if (DECL_ARTIFICIAL (decl))
24454 add_AT_flag (var_die, DW_AT_artificial, 1);
24456 add_alignment_attribute (var_die, decl);
24458 add_accessibility_attribute (var_die, decl);
24461 if (declaration)
24462 add_AT_flag (var_die, DW_AT_declaration, 1);
24464 if (decl && (DECL_ABSTRACT_P (decl)
24465 || !old_die || is_declaration_die (old_die)))
24466 equate_decl_number_to_die (decl, var_die);
24468 gen_variable_die_location:
24469 if (! declaration
24470 && (! DECL_ABSTRACT_P (decl_or_origin)
24471 /* Local static vars are shared between all clones/inlines,
24472 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24473 already set. */
24474 || (VAR_P (decl_or_origin)
24475 && TREE_STATIC (decl_or_origin)
24476 && DECL_RTL_SET_P (decl_or_origin))))
24478 if (early_dwarf)
24479 add_pubname (decl_or_origin, var_die);
24480 else
24481 add_location_or_const_value_attribute (var_die, decl_or_origin,
24482 decl == NULL);
24484 else
24485 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24487 if ((dwarf_version >= 4 || !dwarf_strict)
24488 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24489 DW_AT_const_expr) == 1
24490 && !get_AT (var_die, DW_AT_const_expr)
24491 && !specialization_p)
24492 add_AT_flag (var_die, DW_AT_const_expr, 1);
24494 if (!dwarf_strict)
24496 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24497 DW_AT_inline);
24498 if (inl != -1
24499 && !get_AT (var_die, DW_AT_inline)
24500 && !specialization_p)
24501 add_AT_unsigned (var_die, DW_AT_inline, inl);
24505 /* Generate a DIE to represent a named constant. */
24507 static void
24508 gen_const_die (tree decl, dw_die_ref context_die)
24510 dw_die_ref const_die;
24511 tree type = TREE_TYPE (decl);
24513 const_die = lookup_decl_die (decl);
24514 if (const_die)
24515 return;
24517 const_die = new_die (DW_TAG_constant, context_die, decl);
24518 equate_decl_number_to_die (decl, const_die);
24519 add_name_and_src_coords_attributes (const_die, decl);
24520 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24521 if (TREE_PUBLIC (decl))
24522 add_AT_flag (const_die, DW_AT_external, 1);
24523 if (DECL_ARTIFICIAL (decl))
24524 add_AT_flag (const_die, DW_AT_artificial, 1);
24525 tree_add_const_value_attribute_for_decl (const_die, decl);
24528 /* Generate a DIE to represent a label identifier. */
24530 static void
24531 gen_label_die (tree decl, dw_die_ref context_die)
24533 tree origin = decl_ultimate_origin (decl);
24534 dw_die_ref lbl_die = lookup_decl_die (decl);
24535 rtx insn;
24536 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24538 if (!lbl_die)
24540 lbl_die = new_die (DW_TAG_label, context_die, decl);
24541 equate_decl_number_to_die (decl, lbl_die);
24543 if (origin != NULL)
24544 add_abstract_origin_attribute (lbl_die, origin);
24545 else
24546 add_name_and_src_coords_attributes (lbl_die, decl);
24549 if (DECL_ABSTRACT_P (decl))
24550 equate_decl_number_to_die (decl, lbl_die);
24551 else if (! early_dwarf)
24553 insn = DECL_RTL_IF_SET (decl);
24555 /* Deleted labels are programmer specified labels which have been
24556 eliminated because of various optimizations. We still emit them
24557 here so that it is possible to put breakpoints on them. */
24558 if (insn
24559 && (LABEL_P (insn)
24560 || ((NOTE_P (insn)
24561 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24563 /* When optimization is enabled (via -O) some parts of the compiler
24564 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24565 represent source-level labels which were explicitly declared by
24566 the user. This really shouldn't be happening though, so catch
24567 it if it ever does happen. */
24568 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24570 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24571 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24573 else if (insn
24574 && NOTE_P (insn)
24575 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24576 && CODE_LABEL_NUMBER (insn) != -1)
24578 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24579 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24584 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24585 attributes to the DIE for a block STMT, to describe where the inlined
24586 function was called from. This is similar to add_src_coords_attributes. */
24588 static inline void
24589 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24591 /* We can end up with BUILTINS_LOCATION here. */
24592 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24593 return;
24595 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24597 if (dwarf_version >= 3 || !dwarf_strict)
24599 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24600 add_AT_unsigned (die, DW_AT_call_line, s.line);
24601 if (debug_column_info && s.column)
24602 add_AT_unsigned (die, DW_AT_call_column, s.column);
24607 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24608 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24610 static inline void
24611 add_high_low_attributes (tree stmt, dw_die_ref die)
24613 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24615 if (inline_entry_data **iedp
24616 = !inline_entry_data_table ? NULL
24617 : inline_entry_data_table->find_slot_with_hash (stmt,
24618 htab_hash_pointer (stmt),
24619 NO_INSERT))
24621 inline_entry_data *ied = *iedp;
24622 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24623 gcc_assert (debug_inline_points);
24624 gcc_assert (inlined_function_outer_scope_p (stmt));
24626 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24627 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24629 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24630 && !dwarf_strict)
24632 if (!output_asm_line_debug_info ())
24633 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24634 else
24636 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24637 /* FIXME: this will resolve to a small number. Could we
24638 possibly emit smaller data? Ideally we'd emit a
24639 uleb128, but that would make the size of DIEs
24640 impossible for the compiler to compute, since it's
24641 the assembler that computes the value of the view
24642 label in this case. Ideally, we'd have a single form
24643 encompassing both the address and the view, and
24644 indirecting them through a table might make things
24645 easier, but even that would be more wasteful,
24646 space-wise, than what we have now. */
24647 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24651 inline_entry_data_table->clear_slot (iedp);
24654 if (BLOCK_FRAGMENT_CHAIN (stmt)
24655 && (dwarf_version >= 3 || !dwarf_strict))
24657 tree chain, superblock = NULL_TREE;
24658 dw_die_ref pdie;
24659 dw_attr_node *attr = NULL;
24661 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24663 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24664 BLOCK_NUMBER (stmt));
24665 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24668 /* Optimize duplicate .debug_ranges lists or even tails of
24669 lists. If this BLOCK has same ranges as its supercontext,
24670 lookup DW_AT_ranges attribute in the supercontext (and
24671 recursively so), verify that the ranges_table contains the
24672 right values and use it instead of adding a new .debug_range. */
24673 for (chain = stmt, pdie = die;
24674 BLOCK_SAME_RANGE (chain);
24675 chain = BLOCK_SUPERCONTEXT (chain))
24677 dw_attr_node *new_attr;
24679 pdie = pdie->die_parent;
24680 if (pdie == NULL)
24681 break;
24682 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24683 break;
24684 new_attr = get_AT (pdie, DW_AT_ranges);
24685 if (new_attr == NULL
24686 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24687 break;
24688 attr = new_attr;
24689 superblock = BLOCK_SUPERCONTEXT (chain);
24691 if (attr != NULL
24692 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24693 == (int)BLOCK_NUMBER (superblock))
24694 && BLOCK_FRAGMENT_CHAIN (superblock))
24696 unsigned long off = attr->dw_attr_val.v.val_offset;
24697 unsigned long supercnt = 0, thiscnt = 0;
24698 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24699 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24701 ++supercnt;
24702 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24703 == (int)BLOCK_NUMBER (chain));
24705 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24706 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24707 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24708 ++thiscnt;
24709 gcc_assert (supercnt >= thiscnt);
24710 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24711 false);
24712 note_rnglist_head (off + supercnt - thiscnt);
24713 return;
24716 unsigned int offset = add_ranges (stmt, true);
24717 add_AT_range_list (die, DW_AT_ranges, offset, false);
24718 note_rnglist_head (offset);
24720 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24721 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24724 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24725 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24726 chain = BLOCK_FRAGMENT_CHAIN (chain);
24728 while (chain);
24729 add_ranges (NULL);
24731 else
24733 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24734 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24735 BLOCK_NUMBER (stmt));
24736 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24737 BLOCK_NUMBER (stmt));
24738 add_AT_low_high_pc (die, label, label_high, false);
24742 /* Generate a DIE for a lexical block. */
24744 static void
24745 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24747 dw_die_ref old_die = lookup_block_die (stmt);
24748 dw_die_ref stmt_die = NULL;
24749 if (!old_die)
24751 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24752 equate_block_to_die (stmt, stmt_die);
24755 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24757 /* If this is an inlined or conrecte instance, create a new lexical
24758 die for anything below to attach DW_AT_abstract_origin to. */
24759 if (old_die)
24760 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24762 tree origin = block_ultimate_origin (stmt);
24763 if (origin != NULL_TREE && (origin != stmt || old_die))
24764 add_abstract_origin_attribute (stmt_die, origin);
24766 old_die = NULL;
24769 if (old_die)
24770 stmt_die = old_die;
24772 /* A non abstract block whose blocks have already been reordered
24773 should have the instruction range for this block. If so, set the
24774 high/low attributes. */
24775 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24777 gcc_assert (stmt_die);
24778 add_high_low_attributes (stmt, stmt_die);
24781 decls_for_scope (stmt, stmt_die);
24784 /* Generate a DIE for an inlined subprogram. */
24786 static void
24787 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24789 tree decl = block_ultimate_origin (stmt);
24791 /* Make sure any inlined functions are known to be inlineable. */
24792 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24793 || cgraph_function_possibly_inlined_p (decl));
24795 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24797 if (call_arg_locations || debug_inline_points)
24798 equate_block_to_die (stmt, subr_die);
24799 add_abstract_origin_attribute (subr_die, decl);
24800 if (TREE_ASM_WRITTEN (stmt))
24801 add_high_low_attributes (stmt, subr_die);
24802 add_call_src_coords_attributes (stmt, subr_die);
24804 /* The inliner creates an extra BLOCK for the parameter setup,
24805 we want to merge that with the actual outermost BLOCK of the
24806 inlined function to avoid duplicate locals in consumers.
24807 Do that by doing the recursion to subblocks on the single subblock
24808 of STMT. */
24809 bool unwrap_one = false;
24810 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24812 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24813 if (origin
24814 && TREE_CODE (origin) == BLOCK
24815 && BLOCK_SUPERCONTEXT (origin) == decl)
24816 unwrap_one = true;
24818 decls_for_scope (stmt, subr_die, !unwrap_one);
24819 if (unwrap_one)
24820 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24823 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24824 the comment for VLR_CONTEXT. */
24826 static void
24827 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24829 dw_die_ref decl_die;
24831 if (TREE_TYPE (decl) == error_mark_node)
24832 return;
24834 decl_die = new_die (DW_TAG_member, context_die, decl);
24835 add_name_and_src_coords_attributes (decl_die, decl);
24836 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24837 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24838 context_die);
24840 if (DECL_BIT_FIELD_TYPE (decl))
24842 add_byte_size_attribute (decl_die, decl);
24843 add_bit_size_attribute (decl_die, decl);
24844 add_bit_offset_attribute (decl_die, decl);
24847 add_alignment_attribute (decl_die, decl);
24849 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24850 add_data_member_location_attribute (decl_die, decl, ctx);
24852 if (DECL_ARTIFICIAL (decl))
24853 add_AT_flag (decl_die, DW_AT_artificial, 1);
24855 add_accessibility_attribute (decl_die, decl);
24857 /* Equate decl number to die, so that we can look up this decl later on. */
24858 equate_decl_number_to_die (decl, decl_die);
24861 /* Generate a DIE for a pointer to a member type. TYPE can be an
24862 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24863 pointer to member function. */
24865 static void
24866 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24868 if (lookup_type_die (type))
24869 return;
24871 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24872 scope_die_for (type, context_die), type);
24874 equate_type_number_to_die (type, ptr_die);
24875 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24876 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24877 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24878 context_die);
24879 add_alignment_attribute (ptr_die, type);
24881 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24882 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24884 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24885 add_AT_loc (ptr_die, DW_AT_use_location, op);
24889 static char *producer_string;
24891 /* Given a C and/or C++ language/version string return the "highest".
24892 C++ is assumed to be "higher" than C in this case. Used for merging
24893 LTO translation unit languages. */
24894 static const char *
24895 highest_c_language (const char *lang1, const char *lang2)
24897 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24898 return "GNU C++23";
24899 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24900 return "GNU C++20";
24901 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24902 return "GNU C++17";
24903 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24904 return "GNU C++14";
24905 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24906 return "GNU C++11";
24907 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24908 return "GNU C++98";
24910 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24911 return "GNU C2X";
24912 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24913 return "GNU C17";
24914 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24915 return "GNU C11";
24916 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24917 return "GNU C99";
24918 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24919 return "GNU C89";
24921 gcc_unreachable ();
24925 /* Generate the DIE for the compilation unit. */
24927 static dw_die_ref
24928 gen_compile_unit_die (const char *filename)
24930 dw_die_ref die;
24931 const char *language_string = lang_hooks.name;
24932 int language;
24934 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24936 if (filename)
24938 add_filename_attribute (die, filename);
24939 /* Don't add cwd for <built-in>. */
24940 if (filename[0] != '<')
24941 add_comp_dir_attribute (die);
24944 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24946 /* If our producer is LTO try to figure out a common language to use
24947 from the global list of translation units. */
24948 if (strcmp (language_string, "GNU GIMPLE") == 0)
24950 unsigned i;
24951 tree t;
24952 const char *common_lang = NULL;
24954 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24956 if (!TRANSLATION_UNIT_LANGUAGE (t))
24957 continue;
24958 if (!common_lang)
24959 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24960 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24962 else if (startswith (common_lang, "GNU C")
24963 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
24964 /* Mixing C and C++ is ok, use C++ in that case. */
24965 common_lang = highest_c_language (common_lang,
24966 TRANSLATION_UNIT_LANGUAGE (t));
24967 else
24969 /* Fall back to C. */
24970 common_lang = NULL;
24971 break;
24975 if (common_lang)
24976 language_string = common_lang;
24979 language = DW_LANG_C;
24980 if (startswith (language_string, "GNU C")
24981 && ISDIGIT (language_string[5]))
24983 language = DW_LANG_C89;
24984 if (dwarf_version >= 3 || !dwarf_strict)
24986 if (strcmp (language_string, "GNU C89") != 0)
24987 language = DW_LANG_C99;
24989 if (dwarf_version >= 5 /* || !dwarf_strict */)
24990 if (strcmp (language_string, "GNU C11") == 0
24991 || strcmp (language_string, "GNU C17") == 0
24992 || strcmp (language_string, "GNU C2X") == 0)
24993 language = DW_LANG_C11;
24996 else if (startswith (language_string, "GNU C++"))
24998 language = DW_LANG_C_plus_plus;
24999 if (dwarf_version >= 5 /* || !dwarf_strict */)
25001 if (strcmp (language_string, "GNU C++11") == 0)
25002 language = DW_LANG_C_plus_plus_11;
25003 else if (strcmp (language_string, "GNU C++14") == 0)
25004 language = DW_LANG_C_plus_plus_14;
25005 else if (strcmp (language_string, "GNU C++17") == 0
25006 || strcmp (language_string, "GNU C++20") == 0
25007 || strcmp (language_string, "GNU C++23") == 0)
25008 /* For now. */
25009 language = DW_LANG_C_plus_plus_14;
25012 else if (strcmp (language_string, "GNU F77") == 0)
25013 language = DW_LANG_Fortran77;
25014 else if (dwarf_version >= 3 || !dwarf_strict)
25016 if (strcmp (language_string, "GNU Ada") == 0)
25017 language = DW_LANG_Ada95;
25018 else if (startswith (language_string, "GNU Fortran"))
25020 language = DW_LANG_Fortran95;
25021 if (dwarf_version >= 5 /* || !dwarf_strict */)
25023 if (strcmp (language_string, "GNU Fortran2003") == 0)
25024 language = DW_LANG_Fortran03;
25025 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25026 language = DW_LANG_Fortran08;
25029 else if (strcmp (language_string, "GNU Objective-C") == 0)
25030 language = DW_LANG_ObjC;
25031 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25032 language = DW_LANG_ObjC_plus_plus;
25033 else if (strcmp (language_string, "GNU D") == 0)
25034 language = DW_LANG_D;
25035 else if (dwarf_version >= 5 || !dwarf_strict)
25037 if (strcmp (language_string, "GNU Go") == 0)
25038 language = DW_LANG_Go;
25041 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25042 else if (startswith (language_string, "GNU Fortran"))
25043 language = DW_LANG_Fortran90;
25044 /* Likewise for Ada. */
25045 else if (strcmp (language_string, "GNU Ada") == 0)
25046 language = DW_LANG_Ada83;
25048 add_AT_unsigned (die, DW_AT_language, language);
25050 switch (language)
25052 case DW_LANG_Fortran77:
25053 case DW_LANG_Fortran90:
25054 case DW_LANG_Fortran95:
25055 case DW_LANG_Fortran03:
25056 case DW_LANG_Fortran08:
25057 /* Fortran has case insensitive identifiers and the front-end
25058 lowercases everything. */
25059 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25060 break;
25061 default:
25062 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25063 break;
25065 return die;
25068 /* Generate the DIE for a base class. */
25070 static void
25071 gen_inheritance_die (tree binfo, tree access, tree type,
25072 dw_die_ref context_die)
25074 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25075 struct vlr_context ctx = { type, NULL };
25077 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25078 context_die);
25079 add_data_member_location_attribute (die, binfo, &ctx);
25081 if (BINFO_VIRTUAL_P (binfo))
25082 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25084 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25085 children, otherwise the default is DW_ACCESS_public. In DWARF2
25086 the default has always been DW_ACCESS_private. */
25087 if (access == access_public_node)
25089 if (dwarf_version == 2
25090 || context_die->die_tag == DW_TAG_class_type)
25091 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25093 else if (access == access_protected_node)
25094 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25095 else if (dwarf_version > 2
25096 && context_die->die_tag != DW_TAG_class_type)
25097 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25100 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25101 structure. */
25103 static bool
25104 is_variant_part (tree decl)
25106 return (TREE_CODE (decl) == FIELD_DECL
25107 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25110 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25111 return the FIELD_DECL. Return NULL_TREE otherwise. */
25113 static tree
25114 analyze_discr_in_predicate (tree operand, tree struct_type)
25116 while (CONVERT_EXPR_P (operand))
25117 operand = TREE_OPERAND (operand, 0);
25119 /* Match field access to members of struct_type only. */
25120 if (TREE_CODE (operand) == COMPONENT_REF
25121 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25122 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25123 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25124 return TREE_OPERAND (operand, 1);
25125 else
25126 return NULL_TREE;
25129 /* Check that SRC is a constant integer that can be represented as a native
25130 integer constant (either signed or unsigned). If so, store it into DEST and
25131 return true. Return false otherwise. */
25133 static bool
25134 get_discr_value (tree src, dw_discr_value *dest)
25136 tree discr_type = TREE_TYPE (src);
25138 if (lang_hooks.types.get_debug_type)
25140 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25141 if (debug_type != NULL)
25142 discr_type = debug_type;
25145 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25146 return false;
25148 /* Signedness can vary between the original type and the debug type. This
25149 can happen for character types in Ada for instance: the character type
25150 used for code generation can be signed, to be compatible with the C one,
25151 but from a debugger point of view, it must be unsigned. */
25152 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25153 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25155 if (is_orig_unsigned != is_debug_unsigned)
25156 src = fold_convert (discr_type, src);
25158 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25159 return false;
25161 dest->pos = is_debug_unsigned;
25162 if (is_debug_unsigned)
25163 dest->v.uval = tree_to_uhwi (src);
25164 else
25165 dest->v.sval = tree_to_shwi (src);
25167 return true;
25170 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25171 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25172 store NULL_TREE in DISCR_DECL. Otherwise:
25174 - store the discriminant field in STRUCT_TYPE that controls the variant
25175 part to *DISCR_DECL
25177 - put in *DISCR_LISTS_P an array where for each variant, the item
25178 represents the corresponding matching list of discriminant values.
25180 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25181 the above array.
25183 Note that when the array is allocated (i.e. when the analysis is
25184 successful), it is up to the caller to free the array. */
25186 static void
25187 analyze_variants_discr (tree variant_part_decl,
25188 tree struct_type,
25189 tree *discr_decl,
25190 dw_discr_list_ref **discr_lists_p,
25191 unsigned *discr_lists_length)
25193 tree variant_part_type = TREE_TYPE (variant_part_decl);
25194 tree variant;
25195 dw_discr_list_ref *discr_lists;
25196 unsigned i;
25198 /* Compute how many variants there are in this variant part. */
25199 *discr_lists_length = 0;
25200 for (variant = TYPE_FIELDS (variant_part_type);
25201 variant != NULL_TREE;
25202 variant = DECL_CHAIN (variant))
25203 ++*discr_lists_length;
25205 *discr_decl = NULL_TREE;
25206 *discr_lists_p
25207 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25208 sizeof (**discr_lists_p));
25209 discr_lists = *discr_lists_p;
25211 /* And then analyze all variants to extract discriminant information for all
25212 of them. This analysis is conservative: as soon as we detect something we
25213 do not support, abort everything and pretend we found nothing. */
25214 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25215 variant != NULL_TREE;
25216 variant = DECL_CHAIN (variant), ++i)
25218 tree match_expr = DECL_QUALIFIER (variant);
25220 /* Now, try to analyze the predicate and deduce a discriminant for
25221 it. */
25222 if (match_expr == boolean_true_node)
25223 /* Typically happens for the default variant: it matches all cases that
25224 previous variants rejected. Don't output any matching value for
25225 this one. */
25226 continue;
25228 /* The following loop tries to iterate over each discriminant
25229 possibility: single values or ranges. */
25230 while (match_expr != NULL_TREE)
25232 tree next_round_match_expr;
25233 tree candidate_discr = NULL_TREE;
25234 dw_discr_list_ref new_node = NULL;
25236 /* Possibilities are matched one after the other by nested
25237 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25238 continue with the rest at next iteration. */
25239 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25241 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25242 match_expr = TREE_OPERAND (match_expr, 1);
25244 else
25245 next_round_match_expr = NULL_TREE;
25247 if (match_expr == boolean_false_node)
25248 /* This sub-expression matches nothing: just wait for the next
25249 one. */
25252 else if (TREE_CODE (match_expr) == EQ_EXPR)
25254 /* We are matching: <discr_field> == <integer_cst>
25255 This sub-expression matches a single value. */
25256 tree integer_cst = TREE_OPERAND (match_expr, 1);
25258 candidate_discr
25259 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25260 struct_type);
25262 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25263 if (!get_discr_value (integer_cst,
25264 &new_node->dw_discr_lower_bound))
25265 goto abort;
25266 new_node->dw_discr_range = false;
25269 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25271 /* We are matching:
25272 <discr_field> > <integer_cst>
25273 && <discr_field> < <integer_cst>.
25274 This sub-expression matches the range of values between the
25275 two matched integer constants. Note that comparisons can be
25276 inclusive or exclusive. */
25277 tree candidate_discr_1, candidate_discr_2;
25278 tree lower_cst, upper_cst;
25279 bool lower_cst_included, upper_cst_included;
25280 tree lower_op = TREE_OPERAND (match_expr, 0);
25281 tree upper_op = TREE_OPERAND (match_expr, 1);
25283 /* When the comparison is exclusive, the integer constant is not
25284 the discriminant range bound we are looking for: we will have
25285 to increment or decrement it. */
25286 if (TREE_CODE (lower_op) == GE_EXPR)
25287 lower_cst_included = true;
25288 else if (TREE_CODE (lower_op) == GT_EXPR)
25289 lower_cst_included = false;
25290 else
25291 goto abort;
25293 if (TREE_CODE (upper_op) == LE_EXPR)
25294 upper_cst_included = true;
25295 else if (TREE_CODE (upper_op) == LT_EXPR)
25296 upper_cst_included = false;
25297 else
25298 goto abort;
25300 /* Extract the discriminant from the first operand and check it
25301 is consistant with the same analysis in the second
25302 operand. */
25303 candidate_discr_1
25304 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25305 struct_type);
25306 candidate_discr_2
25307 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25308 struct_type);
25309 if (candidate_discr_1 == candidate_discr_2)
25310 candidate_discr = candidate_discr_1;
25311 else
25312 goto abort;
25314 /* Extract bounds from both. */
25315 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25316 lower_cst = TREE_OPERAND (lower_op, 1);
25317 upper_cst = TREE_OPERAND (upper_op, 1);
25319 if (!lower_cst_included)
25320 lower_cst
25321 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25322 build_int_cst (TREE_TYPE (lower_cst), 1));
25323 if (!upper_cst_included)
25324 upper_cst
25325 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25326 build_int_cst (TREE_TYPE (upper_cst), 1));
25328 if (!get_discr_value (lower_cst,
25329 &new_node->dw_discr_lower_bound)
25330 || !get_discr_value (upper_cst,
25331 &new_node->dw_discr_upper_bound))
25332 goto abort;
25334 new_node->dw_discr_range = true;
25337 else if ((candidate_discr
25338 = analyze_discr_in_predicate (match_expr, struct_type))
25339 && (TREE_TYPE (candidate_discr) == boolean_type_node
25340 || TREE_TYPE (TREE_TYPE (candidate_discr))
25341 == boolean_type_node))
25343 /* We are matching: <discr_field> for a boolean discriminant.
25344 This sub-expression matches boolean_true_node. */
25345 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25346 if (!get_discr_value (boolean_true_node,
25347 &new_node->dw_discr_lower_bound))
25348 goto abort;
25349 new_node->dw_discr_range = false;
25352 else
25353 /* Unsupported sub-expression: we cannot determine the set of
25354 matching discriminant values. Abort everything. */
25355 goto abort;
25357 /* If the discriminant info is not consistant with what we saw so
25358 far, consider the analysis failed and abort everything. */
25359 if (candidate_discr == NULL_TREE
25360 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25361 goto abort;
25362 else
25363 *discr_decl = candidate_discr;
25365 if (new_node != NULL)
25367 new_node->dw_discr_next = discr_lists[i];
25368 discr_lists[i] = new_node;
25370 match_expr = next_round_match_expr;
25374 /* If we reach this point, we could match everything we were interested
25375 in. */
25376 return;
25378 abort:
25379 /* Clean all data structure and return no result. */
25380 free (*discr_lists_p);
25381 *discr_lists_p = NULL;
25382 *discr_decl = NULL_TREE;
25385 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25386 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25387 under CONTEXT_DIE.
25389 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25390 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25391 this type, which are record types, represent the available variants and each
25392 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25393 values are inferred from these attributes.
25395 In trees, the offsets for the fields inside these sub-records are relative
25396 to the variant part itself, whereas the corresponding DIEs should have
25397 offset attributes that are relative to the embedding record base address.
25398 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25399 must be an expression that computes the offset of the variant part to
25400 describe in DWARF. */
25402 static void
25403 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25404 dw_die_ref context_die)
25406 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25407 tree variant_part_offset = vlr_ctx->variant_part_offset;
25409 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25410 NULL_TREE if there is no such field. */
25411 tree discr_decl = NULL_TREE;
25412 dw_discr_list_ref *discr_lists;
25413 unsigned discr_lists_length = 0;
25414 unsigned i;
25416 dw_die_ref dwarf_proc_die = NULL;
25417 dw_die_ref variant_part_die
25418 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25420 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25422 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25423 &discr_decl, &discr_lists, &discr_lists_length);
25425 if (discr_decl != NULL_TREE)
25427 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25429 if (discr_die)
25430 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25431 else
25432 /* We have no DIE for the discriminant, so just discard all
25433 discrimimant information in the output. */
25434 discr_decl = NULL_TREE;
25437 /* If the offset for this variant part is more complex than a constant,
25438 create a DWARF procedure for it so that we will not have to generate
25439 DWARF expressions for it for each member. */
25440 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25441 && (dwarf_version >= 3 || !dwarf_strict))
25443 struct loc_descr_context ctx = {
25444 vlr_ctx->struct_type, /* context_type */
25445 NULL_TREE, /* base_decl */
25446 NULL, /* dpi */
25447 false, /* placeholder_arg */
25448 false, /* placeholder_seen */
25449 false /* strict_signedness */
25451 const tree dwarf_proc_fndecl
25452 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25453 build_function_type (TREE_TYPE (variant_part_offset),
25454 NULL_TREE));
25455 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25456 const dw_loc_descr_ref dwarf_proc_body
25457 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25459 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25460 dwarf_proc_fndecl, context_die);
25461 if (dwarf_proc_die != NULL)
25462 variant_part_offset = dwarf_proc_call;
25465 /* Output DIEs for all variants. */
25466 i = 0;
25467 for (tree variant = TYPE_FIELDS (variant_part_type);
25468 variant != NULL_TREE;
25469 variant = DECL_CHAIN (variant), ++i)
25471 tree variant_type = TREE_TYPE (variant);
25472 dw_die_ref variant_die;
25474 /* All variants (i.e. members of a variant part) are supposed to be
25475 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25476 under these records. */
25477 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25479 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25480 equate_decl_number_to_die (variant, variant_die);
25482 /* Output discriminant values this variant matches, if any. */
25483 if (discr_decl == NULL || discr_lists[i] == NULL)
25484 /* In the case we have discriminant information at all, this is
25485 probably the default variant: as the standard says, don't
25486 output any discriminant value/list attribute. */
25488 else if (discr_lists[i]->dw_discr_next == NULL
25489 && !discr_lists[i]->dw_discr_range)
25490 /* If there is only one accepted value, don't bother outputting a
25491 list. */
25492 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25493 else
25494 add_discr_list (variant_die, discr_lists[i]);
25496 for (tree member = TYPE_FIELDS (variant_type);
25497 member != NULL_TREE;
25498 member = DECL_CHAIN (member))
25500 struct vlr_context vlr_sub_ctx = {
25501 vlr_ctx->struct_type, /* struct_type */
25502 NULL /* variant_part_offset */
25504 if (is_variant_part (member))
25506 /* All offsets for fields inside variant parts are relative to
25507 the top-level embedding RECORD_TYPE's base address. On the
25508 other hand, offsets in GCC's types are relative to the
25509 nested-most variant part. So we have to sum offsets each time
25510 we recurse. */
25512 vlr_sub_ctx.variant_part_offset
25513 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25514 variant_part_offset, byte_position (member));
25515 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25517 else
25519 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25520 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25525 free (discr_lists);
25528 /* Generate a DIE for a class member. */
25530 static void
25531 gen_member_die (tree type, dw_die_ref context_die)
25533 tree member;
25534 tree binfo = TYPE_BINFO (type);
25536 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25538 /* If this is not an incomplete type, output descriptions of each of its
25539 members. Note that as we output the DIEs necessary to represent the
25540 members of this record or union type, we will also be trying to output
25541 DIEs to represent the *types* of those members. However the `type'
25542 function (above) will specifically avoid generating type DIEs for member
25543 types *within* the list of member DIEs for this (containing) type except
25544 for those types (of members) which are explicitly marked as also being
25545 members of this (containing) type themselves. The g++ front- end can
25546 force any given type to be treated as a member of some other (containing)
25547 type by setting the TYPE_CONTEXT of the given (member) type to point to
25548 the TREE node representing the appropriate (containing) type. */
25550 /* First output info about the base classes. */
25551 if (binfo && early_dwarf)
25553 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25554 int i;
25555 tree base;
25557 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25558 gen_inheritance_die (base,
25559 (accesses ? (*accesses)[i] : access_public_node),
25560 type,
25561 context_die);
25564 /* Now output info about the members. */
25565 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25567 /* Ignore clones. */
25568 if (DECL_ABSTRACT_ORIGIN (member))
25569 continue;
25571 struct vlr_context vlr_ctx = { type, NULL_TREE };
25572 bool static_inline_p
25573 = (VAR_P (member)
25574 && TREE_STATIC (member)
25575 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25576 != -1));
25578 /* If we thought we were generating minimal debug info for TYPE
25579 and then changed our minds, some of the member declarations
25580 may have already been defined. Don't define them again, but
25581 do put them in the right order. */
25583 if (dw_die_ref child = lookup_decl_die (member))
25585 /* Handle inline static data members, which only have in-class
25586 declarations. */
25587 bool splice = true;
25589 dw_die_ref ref = NULL;
25590 if (child->die_tag == DW_TAG_variable
25591 && child->die_parent == comp_unit_die ())
25593 ref = get_AT_ref (child, DW_AT_specification);
25595 /* For C++17 inline static data members followed by redundant
25596 out of class redeclaration, we might get here with
25597 child being the DIE created for the out of class
25598 redeclaration and with its DW_AT_specification being
25599 the DIE created for in-class definition. We want to
25600 reparent the latter, and don't want to create another
25601 DIE with DW_AT_specification in that case, because
25602 we already have one. */
25603 if (ref
25604 && static_inline_p
25605 && ref->die_tag == DW_TAG_variable
25606 && ref->die_parent == comp_unit_die ()
25607 && get_AT (ref, DW_AT_specification) == NULL)
25609 child = ref;
25610 ref = NULL;
25611 static_inline_p = false;
25614 if (!ref)
25616 reparent_child (child, context_die);
25617 if (dwarf_version < 5)
25618 child->die_tag = DW_TAG_member;
25619 splice = false;
25622 else if (child->die_tag == DW_TAG_enumerator)
25623 /* Enumerators remain under their enumeration even if
25624 their names are introduced in the enclosing scope. */
25625 splice = false;
25627 if (splice)
25628 splice_child_die (context_die, child);
25631 /* Do not generate DWARF for variant parts if we are generating the
25632 corresponding GNAT encodings: DIEs generated for the two schemes
25633 would conflict in our mappings. */
25634 else if (is_variant_part (member)
25635 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25637 vlr_ctx.variant_part_offset = byte_position (member);
25638 gen_variant_part (member, &vlr_ctx, context_die);
25640 else
25642 vlr_ctx.variant_part_offset = NULL_TREE;
25643 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25646 /* For C++ inline static data members emit immediately a DW_TAG_variable
25647 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25648 DW_AT_specification. */
25649 if (static_inline_p)
25651 int old_extern = DECL_EXTERNAL (member);
25652 DECL_EXTERNAL (member) = 0;
25653 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25654 DECL_EXTERNAL (member) = old_extern;
25659 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25660 is set, we pretend that the type was never defined, so we only get the
25661 member DIEs needed by later specification DIEs. */
25663 static void
25664 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25665 enum debug_info_usage usage)
25667 if (TREE_ASM_WRITTEN (type))
25669 /* Fill in the bound of variable-length fields in late dwarf if
25670 still incomplete. */
25671 if (!early_dwarf && variably_modified_type_p (type, NULL))
25672 for (tree member = TYPE_FIELDS (type);
25673 member;
25674 member = DECL_CHAIN (member))
25675 fill_variable_array_bounds (TREE_TYPE (member));
25676 return;
25679 dw_die_ref type_die = lookup_type_die (type);
25680 dw_die_ref scope_die = 0;
25681 int nested = 0;
25682 int complete = (TYPE_SIZE (type)
25683 && (! TYPE_STUB_DECL (type)
25684 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25685 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25686 complete = complete && should_emit_struct_debug (type, usage);
25688 if (type_die && ! complete)
25689 return;
25691 if (TYPE_CONTEXT (type) != NULL_TREE
25692 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25693 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25694 nested = 1;
25696 scope_die = scope_die_for (type, context_die);
25698 /* Generate child dies for template parameters. */
25699 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25700 schedule_generic_params_dies_gen (type);
25702 if (! type_die || (nested && is_cu_die (scope_die)))
25703 /* First occurrence of type or toplevel definition of nested class. */
25705 dw_die_ref old_die = type_die;
25707 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25708 ? record_type_tag (type) : DW_TAG_union_type,
25709 scope_die, type);
25710 equate_type_number_to_die (type, type_die);
25711 if (old_die)
25712 add_AT_specification (type_die, old_die);
25713 else
25714 add_name_attribute (type_die, type_tag (type));
25716 else
25717 remove_AT (type_die, DW_AT_declaration);
25719 /* If this type has been completed, then give it a byte_size attribute and
25720 then give a list of members. */
25721 if (complete && !ns_decl)
25723 /* Prevent infinite recursion in cases where the type of some member of
25724 this type is expressed in terms of this type itself. */
25725 TREE_ASM_WRITTEN (type) = 1;
25726 add_byte_size_attribute (type_die, type);
25727 add_alignment_attribute (type_die, type);
25728 if (TYPE_STUB_DECL (type) != NULL_TREE)
25730 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25731 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25734 /* If the first reference to this type was as the return type of an
25735 inline function, then it may not have a parent. Fix this now. */
25736 if (type_die->die_parent == NULL)
25737 add_child_die (scope_die, type_die);
25739 gen_member_die (type, type_die);
25741 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25742 if (TYPE_ARTIFICIAL (type))
25743 add_AT_flag (type_die, DW_AT_artificial, 1);
25745 /* GNU extension: Record what type our vtable lives in. */
25746 if (TYPE_VFIELD (type))
25748 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25750 gen_type_die (vtype, context_die);
25751 add_AT_die_ref (type_die, DW_AT_containing_type,
25752 lookup_type_die (vtype));
25755 else
25757 add_AT_flag (type_die, DW_AT_declaration, 1);
25759 /* We don't need to do this for function-local types. */
25760 if (TYPE_STUB_DECL (type)
25761 && ! decl_function_context (TYPE_STUB_DECL (type)))
25762 vec_safe_push (incomplete_types, type);
25765 if (get_AT (type_die, DW_AT_name))
25766 add_pubtype (type, type_die);
25769 /* Generate a DIE for a subroutine _type_. */
25771 static void
25772 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25774 tree return_type = TREE_TYPE (type);
25775 dw_die_ref subr_die
25776 = new_die (DW_TAG_subroutine_type,
25777 scope_die_for (type, context_die), type);
25779 equate_type_number_to_die (type, subr_die);
25780 add_prototyped_attribute (subr_die, type);
25781 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25782 context_die);
25783 add_alignment_attribute (subr_die, type);
25784 gen_formal_types_die (type, subr_die);
25786 if (get_AT (subr_die, DW_AT_name))
25787 add_pubtype (type, subr_die);
25788 if ((dwarf_version >= 5 || !dwarf_strict)
25789 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25790 add_AT_flag (subr_die, DW_AT_reference, 1);
25791 if ((dwarf_version >= 5 || !dwarf_strict)
25792 && lang_hooks.types.type_dwarf_attribute (type,
25793 DW_AT_rvalue_reference) != -1)
25794 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25797 /* Generate a DIE for a type definition. */
25799 static void
25800 gen_typedef_die (tree decl, dw_die_ref context_die)
25802 dw_die_ref type_die;
25803 tree type;
25805 if (TREE_ASM_WRITTEN (decl))
25807 if (DECL_ORIGINAL_TYPE (decl))
25808 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25809 return;
25812 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25813 checks in process_scope_var and modified_type_die), this should be called
25814 only for original types. */
25815 gcc_assert (decl_ultimate_origin (decl) == NULL
25816 || decl_ultimate_origin (decl) == decl);
25818 TREE_ASM_WRITTEN (decl) = 1;
25819 type_die = new_die (DW_TAG_typedef, context_die, decl);
25821 add_name_and_src_coords_attributes (type_die, decl);
25822 if (DECL_ORIGINAL_TYPE (decl))
25824 type = DECL_ORIGINAL_TYPE (decl);
25825 if (type == error_mark_node)
25826 return;
25828 gcc_assert (type != TREE_TYPE (decl));
25829 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25831 else
25833 type = TREE_TYPE (decl);
25834 if (type == error_mark_node)
25835 return;
25837 if (is_naming_typedef_decl (TYPE_NAME (type)))
25839 /* Here, we are in the case of decl being a typedef naming
25840 an anonymous type, e.g:
25841 typedef struct {...} foo;
25842 In that case TREE_TYPE (decl) is not a typedef variant
25843 type and TYPE_NAME of the anonymous type is set to the
25844 TYPE_DECL of the typedef. This construct is emitted by
25845 the C++ FE.
25847 TYPE is the anonymous struct named by the typedef
25848 DECL. As we need the DW_AT_type attribute of the
25849 DW_TAG_typedef to point to the DIE of TYPE, let's
25850 generate that DIE right away. add_type_attribute
25851 called below will then pick (via lookup_type_die) that
25852 anonymous struct DIE. */
25853 if (!TREE_ASM_WRITTEN (type))
25854 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25856 /* This is a GNU Extension. We are adding a
25857 DW_AT_linkage_name attribute to the DIE of the
25858 anonymous struct TYPE. The value of that attribute
25859 is the name of the typedef decl naming the anonymous
25860 struct. This greatly eases the work of consumers of
25861 this debug info. */
25862 add_linkage_name_raw (lookup_type_die (type), decl);
25866 add_type_attribute (type_die, type, decl_quals (decl), false,
25867 context_die);
25869 if (is_naming_typedef_decl (decl))
25870 /* We want that all subsequent calls to lookup_type_die with
25871 TYPE in argument yield the DW_TAG_typedef we have just
25872 created. */
25873 equate_type_number_to_die (type, type_die);
25875 add_alignment_attribute (type_die, TREE_TYPE (decl));
25877 add_accessibility_attribute (type_die, decl);
25879 if (DECL_ABSTRACT_P (decl))
25880 equate_decl_number_to_die (decl, type_die);
25882 if (get_AT (type_die, DW_AT_name))
25883 add_pubtype (decl, type_die);
25886 /* Generate a DIE for a struct, class, enum or union type. */
25888 static void
25889 gen_tagged_type_die (tree type,
25890 dw_die_ref context_die,
25891 enum debug_info_usage usage)
25893 if (type == NULL_TREE
25894 || !is_tagged_type (type))
25895 return;
25897 if (TREE_ASM_WRITTEN (type))
25899 /* If this is a nested type whose containing class hasn't been written
25900 out yet, writing it out will cover this one, too. This does not apply
25901 to instantiations of member class templates; they need to be added to
25902 the containing class as they are generated. FIXME: This hurts the
25903 idea of combining type decls from multiple TUs, since we can't predict
25904 what set of template instantiations we'll get. */
25905 else if (TYPE_CONTEXT (type)
25906 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25907 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25909 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25911 if (TREE_ASM_WRITTEN (type))
25912 return;
25914 /* If that failed, attach ourselves to the stub. */
25915 context_die = lookup_type_die (TYPE_CONTEXT (type));
25917 else if (TYPE_CONTEXT (type) != NULL_TREE
25918 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25920 /* If this type is local to a function that hasn't been written
25921 out yet, use a NULL context for now; it will be fixed up in
25922 decls_for_scope. */
25923 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25924 /* A declaration DIE doesn't count; nested types need to go in the
25925 specification. */
25926 if (context_die && is_declaration_die (context_die))
25927 context_die = NULL;
25929 else
25930 context_die = declare_in_namespace (type, context_die);
25932 if (TREE_CODE (type) == ENUMERAL_TYPE)
25934 /* This might have been written out by the call to
25935 declare_in_namespace. */
25936 if (!TREE_ASM_WRITTEN (type))
25937 gen_enumeration_type_die (type, context_die);
25939 else
25940 gen_struct_or_union_type_die (type, context_die, usage);
25942 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25943 it up if it is ever completed. gen_*_type_die will set it for us
25944 when appropriate. */
25947 /* Generate a type description DIE. */
25949 static void
25950 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25951 enum debug_info_usage usage)
25953 struct array_descr_info info;
25955 if (type == NULL_TREE || type == error_mark_node)
25956 return;
25958 if (flag_checking && type)
25959 verify_type (type);
25961 if (TYPE_NAME (type) != NULL_TREE
25962 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25963 && is_redundant_typedef (TYPE_NAME (type))
25964 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25965 /* The DECL of this type is a typedef we don't want to emit debug
25966 info for but we want debug info for its underlying typedef.
25967 This can happen for e.g, the injected-class-name of a C++
25968 type. */
25969 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25971 /* If TYPE is a typedef type variant, let's generate debug info
25972 for the parent typedef which TYPE is a type of. */
25973 if (typedef_variant_p (type))
25975 if (TREE_ASM_WRITTEN (type))
25976 return;
25978 tree name = TYPE_NAME (type);
25979 tree origin = decl_ultimate_origin (name);
25980 if (origin != NULL && origin != name)
25982 gen_decl_die (origin, NULL, NULL, context_die);
25983 return;
25986 /* Prevent broken recursion; we can't hand off to the same type. */
25987 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25989 /* Give typedefs the right scope. */
25990 context_die = scope_die_for (type, context_die);
25992 TREE_ASM_WRITTEN (type) = 1;
25994 gen_decl_die (name, NULL, NULL, context_die);
25995 return;
25998 /* If type is an anonymous tagged type named by a typedef, let's
25999 generate debug info for the typedef. */
26000 if (is_naming_typedef_decl (TYPE_NAME (type)))
26002 /* Give typedefs the right scope. */
26003 context_die = scope_die_for (type, context_die);
26005 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26006 return;
26009 if (lang_hooks.types.get_debug_type)
26011 tree debug_type = lang_hooks.types.get_debug_type (type);
26013 if (debug_type != NULL_TREE && debug_type != type)
26015 gen_type_die_with_usage (debug_type, context_die, usage);
26016 return;
26020 /* We are going to output a DIE to represent the unqualified version
26021 of this type (i.e. without any const or volatile qualifiers) so
26022 get the main variant (i.e. the unqualified version) of this type
26023 now. (Vectors and arrays are special because the debugging info is in the
26024 cloned type itself. Similarly function/method types can contain extra
26025 ref-qualification). */
26026 if (TREE_CODE (type) == FUNCTION_TYPE
26027 || TREE_CODE (type) == METHOD_TYPE)
26029 /* For function/method types, can't use type_main_variant here,
26030 because that can have different ref-qualifiers for C++,
26031 but try to canonicalize. */
26032 tree main = TYPE_MAIN_VARIANT (type);
26033 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26034 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26035 && check_base_type (t, main)
26036 && check_lang_type (t, type))
26038 type = t;
26039 break;
26042 else if (TREE_CODE (type) != VECTOR_TYPE
26043 && TREE_CODE (type) != ARRAY_TYPE)
26044 type = type_main_variant (type);
26046 /* If this is an array type with hidden descriptor, handle it first. */
26047 if (!TREE_ASM_WRITTEN (type)
26048 && lang_hooks.types.get_array_descr_info)
26050 memset (&info, 0, sizeof (info));
26051 if (lang_hooks.types.get_array_descr_info (type, &info))
26053 /* Fortran sometimes emits array types with no dimension. */
26054 gcc_assert (info.ndimensions >= 0
26055 && (info.ndimensions
26056 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26057 gen_descr_array_type_die (type, &info, context_die);
26058 TREE_ASM_WRITTEN (type) = 1;
26059 return;
26063 if (TREE_ASM_WRITTEN (type))
26065 /* Variable-length types may be incomplete even if
26066 TREE_ASM_WRITTEN. For such types, fall through to
26067 gen_array_type_die() and possibly fill in
26068 DW_AT_{upper,lower}_bound attributes. */
26069 if ((TREE_CODE (type) != ARRAY_TYPE
26070 && TREE_CODE (type) != RECORD_TYPE
26071 && TREE_CODE (type) != UNION_TYPE
26072 && TREE_CODE (type) != QUAL_UNION_TYPE)
26073 || !variably_modified_type_p (type, NULL))
26074 return;
26077 switch (TREE_CODE (type))
26079 case ERROR_MARK:
26080 break;
26082 case POINTER_TYPE:
26083 case REFERENCE_TYPE:
26084 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26085 ensures that the gen_type_die recursion will terminate even if the
26086 type is recursive. Recursive types are possible in Ada. */
26087 /* ??? We could perhaps do this for all types before the switch
26088 statement. */
26089 TREE_ASM_WRITTEN (type) = 1;
26091 /* For these types, all that is required is that we output a DIE (or a
26092 set of DIEs) to represent the "basis" type. */
26093 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26094 DINFO_USAGE_IND_USE);
26095 break;
26097 case OFFSET_TYPE:
26098 /* This code is used for C++ pointer-to-data-member types.
26099 Output a description of the relevant class type. */
26100 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26101 DINFO_USAGE_IND_USE);
26103 /* Output a description of the type of the object pointed to. */
26104 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26105 DINFO_USAGE_IND_USE);
26107 /* Now output a DIE to represent this pointer-to-data-member type
26108 itself. */
26109 gen_ptr_to_mbr_type_die (type, context_die);
26110 break;
26112 case FUNCTION_TYPE:
26113 /* Force out return type (in case it wasn't forced out already). */
26114 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26115 DINFO_USAGE_DIR_USE);
26116 gen_subroutine_type_die (type, context_die);
26117 break;
26119 case METHOD_TYPE:
26120 /* Force out return type (in case it wasn't forced out already). */
26121 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26122 DINFO_USAGE_DIR_USE);
26123 gen_subroutine_type_die (type, context_die);
26124 break;
26126 case ARRAY_TYPE:
26127 case VECTOR_TYPE:
26128 gen_array_type_die (type, context_die);
26129 break;
26131 case ENUMERAL_TYPE:
26132 case RECORD_TYPE:
26133 case UNION_TYPE:
26134 case QUAL_UNION_TYPE:
26135 gen_tagged_type_die (type, context_die, usage);
26136 return;
26138 case VOID_TYPE:
26139 case OPAQUE_TYPE:
26140 case INTEGER_TYPE:
26141 case REAL_TYPE:
26142 case FIXED_POINT_TYPE:
26143 case COMPLEX_TYPE:
26144 case BOOLEAN_TYPE:
26145 /* No DIEs needed for fundamental types. */
26146 break;
26148 case NULLPTR_TYPE:
26149 case LANG_TYPE:
26150 /* Just use DW_TAG_unspecified_type. */
26152 dw_die_ref type_die = lookup_type_die (type);
26153 if (type_die == NULL)
26155 tree name = TYPE_IDENTIFIER (type);
26156 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26157 type);
26158 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26159 equate_type_number_to_die (type, type_die);
26162 break;
26164 default:
26165 if (is_cxx_auto (type))
26167 tree name = TYPE_IDENTIFIER (type);
26168 dw_die_ref *die = (name == get_identifier ("auto")
26169 ? &auto_die : &decltype_auto_die);
26170 if (!*die)
26172 *die = new_die (DW_TAG_unspecified_type,
26173 comp_unit_die (), NULL_TREE);
26174 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26176 equate_type_number_to_die (type, *die);
26177 break;
26179 gcc_unreachable ();
26182 TREE_ASM_WRITTEN (type) = 1;
26185 static void
26186 gen_type_die (tree type, dw_die_ref context_die)
26188 if (type != error_mark_node)
26190 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26191 if (flag_checking)
26193 dw_die_ref die = lookup_type_die (type);
26194 if (die)
26195 check_die (die);
26200 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26201 things which are local to the given block. */
26203 static void
26204 gen_block_die (tree stmt, dw_die_ref context_die)
26206 int must_output_die = 0;
26207 bool inlined_func;
26209 /* Ignore blocks that are NULL. */
26210 if (stmt == NULL_TREE)
26211 return;
26213 inlined_func = inlined_function_outer_scope_p (stmt);
26215 /* If the block is one fragment of a non-contiguous block, do not
26216 process the variables, since they will have been done by the
26217 origin block. Do process subblocks. */
26218 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26220 tree sub;
26222 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26223 gen_block_die (sub, context_die);
26225 return;
26228 /* Determine if we need to output any Dwarf DIEs at all to represent this
26229 block. */
26230 if (inlined_func)
26231 /* The outer scopes for inlinings *must* always be represented. We
26232 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26233 must_output_die = 1;
26234 else if (lookup_block_die (stmt))
26235 /* If we already have a DIE then it was filled early. Meanwhile
26236 we might have pruned all BLOCK_VARS as optimized out but we
26237 still want to generate high/low PC attributes so output it. */
26238 must_output_die = 1;
26239 else if (TREE_USED (stmt)
26240 || TREE_ASM_WRITTEN (stmt))
26242 /* Determine if this block directly contains any "significant"
26243 local declarations which we will need to output DIEs for. */
26244 if (debug_info_level > DINFO_LEVEL_TERSE)
26246 /* We are not in terse mode so any local declaration that
26247 is not ignored for debug purposes counts as being a
26248 "significant" one. */
26249 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26250 must_output_die = 1;
26251 else
26252 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26253 if (!DECL_IGNORED_P (var))
26255 must_output_die = 1;
26256 break;
26259 else if (!dwarf2out_ignore_block (stmt))
26260 must_output_die = 1;
26263 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26264 DIE for any block which contains no significant local declarations at
26265 all. Rather, in such cases we just call `decls_for_scope' so that any
26266 needed Dwarf info for any sub-blocks will get properly generated. Note
26267 that in terse mode, our definition of what constitutes a "significant"
26268 local declaration gets restricted to include only inlined function
26269 instances and local (nested) function definitions. */
26270 if (must_output_die)
26272 if (inlined_func)
26273 gen_inlined_subroutine_die (stmt, context_die);
26274 else
26275 gen_lexical_block_die (stmt, context_die);
26277 else
26278 decls_for_scope (stmt, context_die);
26281 /* Process variable DECL (or variable with origin ORIGIN) within
26282 block STMT and add it to CONTEXT_DIE. */
26283 static void
26284 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26286 dw_die_ref die;
26287 tree decl_or_origin = decl ? decl : origin;
26289 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26290 die = lookup_decl_die (decl_or_origin);
26291 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26293 if (TYPE_DECL_IS_STUB (decl_or_origin))
26294 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26295 else
26296 die = lookup_decl_die (decl_or_origin);
26297 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26298 if (! die && ! early_dwarf)
26299 return;
26301 else
26302 die = NULL;
26304 /* Avoid creating DIEs for local typedefs and concrete static variables that
26305 will only be pruned later. */
26306 if ((origin || decl_ultimate_origin (decl))
26307 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26308 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26310 origin = decl_ultimate_origin (decl_or_origin);
26311 if (decl && VAR_P (decl) && die != NULL)
26313 die = lookup_decl_die (origin);
26314 if (die != NULL)
26315 equate_decl_number_to_die (decl, die);
26317 return;
26320 if (die != NULL && die->die_parent == NULL)
26321 add_child_die (context_die, die);
26322 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26324 if (early_dwarf)
26325 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26326 stmt, context_die);
26328 else
26330 if (decl && DECL_P (decl))
26332 die = lookup_decl_die (decl);
26334 /* Early created DIEs do not have a parent as the decls refer
26335 to the function as DECL_CONTEXT rather than the BLOCK. */
26336 if (die && die->die_parent == NULL)
26338 gcc_assert (in_lto_p);
26339 add_child_die (context_die, die);
26343 gen_decl_die (decl, origin, NULL, context_die);
26347 /* Generate all of the decls declared within a given scope and (recursively)
26348 all of its sub-blocks. */
26350 static void
26351 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26353 tree decl;
26354 unsigned int i;
26355 tree subblocks;
26357 /* Ignore NULL blocks. */
26358 if (stmt == NULL_TREE)
26359 return;
26361 /* Output the DIEs to represent all of the data objects and typedefs
26362 declared directly within this block but not within any nested
26363 sub-blocks. Also, nested function and tag DIEs have been
26364 generated with a parent of NULL; fix that up now. We don't
26365 have to do this if we're at -g1. */
26366 if (debug_info_level > DINFO_LEVEL_TERSE)
26368 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26369 process_scope_var (stmt, decl, NULL_TREE, context_die);
26370 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26371 origin - avoid doing this twice as we have no good way to see
26372 if we've done it once already. */
26373 if (! early_dwarf)
26374 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26376 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26377 if (decl == current_function_decl)
26378 /* Ignore declarations of the current function, while they
26379 are declarations, gen_subprogram_die would treat them
26380 as definitions again, because they are equal to
26381 current_function_decl and endlessly recurse. */;
26382 else if (TREE_CODE (decl) == FUNCTION_DECL)
26383 process_scope_var (stmt, decl, NULL_TREE, context_die);
26384 else
26385 process_scope_var (stmt, NULL_TREE, decl, context_die);
26389 /* Even if we're at -g1, we need to process the subblocks in order to get
26390 inlined call information. */
26392 /* Output the DIEs to represent all sub-blocks (and the items declared
26393 therein) of this block. */
26394 if (recurse)
26395 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26396 subblocks != NULL;
26397 subblocks = BLOCK_CHAIN (subblocks))
26398 gen_block_die (subblocks, context_die);
26401 /* Is this a typedef we can avoid emitting? */
26403 static bool
26404 is_redundant_typedef (const_tree decl)
26406 if (TYPE_DECL_IS_STUB (decl))
26407 return true;
26409 if (DECL_ARTIFICIAL (decl)
26410 && DECL_CONTEXT (decl)
26411 && is_tagged_type (DECL_CONTEXT (decl))
26412 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26413 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26414 /* Also ignore the artificial member typedef for the class name. */
26415 return true;
26417 return false;
26420 /* Return TRUE if TYPE is a typedef that names a type for linkage
26421 purposes. This kind of typedefs is produced by the C++ FE for
26422 constructs like:
26424 typedef struct {...} foo;
26426 In that case, there is no typedef variant type produced for foo.
26427 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26428 struct type. */
26430 static bool
26431 is_naming_typedef_decl (const_tree decl)
26433 if (decl == NULL_TREE
26434 || TREE_CODE (decl) != TYPE_DECL
26435 || DECL_NAMELESS (decl)
26436 || !is_tagged_type (TREE_TYPE (decl))
26437 || DECL_IS_UNDECLARED_BUILTIN (decl)
26438 || is_redundant_typedef (decl)
26439 /* It looks like Ada produces TYPE_DECLs that are very similar
26440 to C++ naming typedefs but that have different
26441 semantics. Let's be specific to c++ for now. */
26442 || !is_cxx (decl))
26443 return FALSE;
26445 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26446 && TYPE_NAME (TREE_TYPE (decl)) == decl
26447 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26448 != TYPE_NAME (TREE_TYPE (decl))));
26451 /* Looks up the DIE for a context. */
26453 static inline dw_die_ref
26454 lookup_context_die (tree context)
26456 if (context)
26458 /* Find die that represents this context. */
26459 if (TYPE_P (context))
26461 context = TYPE_MAIN_VARIANT (context);
26462 dw_die_ref ctx = lookup_type_die (context);
26463 if (!ctx)
26464 return NULL;
26465 return strip_naming_typedef (context, ctx);
26467 else
26468 return lookup_decl_die (context);
26470 return comp_unit_die ();
26473 /* Returns the DIE for a context. */
26475 static inline dw_die_ref
26476 get_context_die (tree context)
26478 if (context)
26480 /* Find die that represents this context. */
26481 if (TYPE_P (context))
26483 context = TYPE_MAIN_VARIANT (context);
26484 return strip_naming_typedef (context, force_type_die (context));
26486 else
26487 return force_decl_die (context);
26489 return comp_unit_die ();
26492 /* Returns the DIE for decl. A DIE will always be returned. */
26494 static dw_die_ref
26495 force_decl_die (tree decl)
26497 dw_die_ref decl_die;
26498 unsigned saved_external_flag;
26499 tree save_fn = NULL_TREE;
26500 decl_die = lookup_decl_die (decl);
26501 if (!decl_die)
26503 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26505 decl_die = lookup_decl_die (decl);
26506 if (decl_die)
26507 return decl_die;
26509 switch (TREE_CODE (decl))
26511 case FUNCTION_DECL:
26512 /* Clear current_function_decl, so that gen_subprogram_die thinks
26513 that this is a declaration. At this point, we just want to force
26514 declaration die. */
26515 save_fn = current_function_decl;
26516 current_function_decl = NULL_TREE;
26517 gen_subprogram_die (decl, context_die);
26518 current_function_decl = save_fn;
26519 break;
26521 case VAR_DECL:
26522 /* Set external flag to force declaration die. Restore it after
26523 gen_decl_die() call. */
26524 saved_external_flag = DECL_EXTERNAL (decl);
26525 DECL_EXTERNAL (decl) = 1;
26526 gen_decl_die (decl, NULL, NULL, context_die);
26527 DECL_EXTERNAL (decl) = saved_external_flag;
26528 break;
26530 case NAMESPACE_DECL:
26531 if (dwarf_version >= 3 || !dwarf_strict)
26532 dwarf2out_decl (decl);
26533 else
26534 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26535 decl_die = comp_unit_die ();
26536 break;
26538 case CONST_DECL:
26539 /* Enumerators shouldn't need force_decl_die. */
26540 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26541 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26542 gen_decl_die (decl, NULL, NULL, context_die);
26543 break;
26545 case TRANSLATION_UNIT_DECL:
26546 decl_die = comp_unit_die ();
26547 break;
26549 default:
26550 gcc_unreachable ();
26553 /* We should be able to find the DIE now. */
26554 if (!decl_die)
26555 decl_die = lookup_decl_die (decl);
26556 gcc_assert (decl_die);
26559 return decl_die;
26562 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26563 always returned. */
26565 static dw_die_ref
26566 force_type_die (tree type)
26568 dw_die_ref type_die;
26570 type_die = lookup_type_die (type);
26571 if (!type_die)
26573 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26575 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26576 false, context_die);
26577 gcc_assert (type_die);
26579 return type_die;
26582 /* Force out any required namespaces to be able to output DECL,
26583 and return the new context_die for it, if it's changed. */
26585 static dw_die_ref
26586 setup_namespace_context (tree thing, dw_die_ref context_die)
26588 tree context = (DECL_P (thing)
26589 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26590 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26591 /* Force out the namespace. */
26592 context_die = force_decl_die (context);
26594 return context_die;
26597 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26598 type) within its namespace, if appropriate.
26600 For compatibility with older debuggers, namespace DIEs only contain
26601 declarations; all definitions are emitted at CU scope, with
26602 DW_AT_specification pointing to the declaration (like with class
26603 members). */
26605 static dw_die_ref
26606 declare_in_namespace (tree thing, dw_die_ref context_die)
26608 dw_die_ref ns_context;
26610 if (debug_info_level <= DINFO_LEVEL_TERSE)
26611 return context_die;
26613 /* External declarations in the local scope only need to be emitted
26614 once, not once in the namespace and once in the scope.
26616 This avoids declaring the `extern' below in the
26617 namespace DIE as well as in the innermost scope:
26619 namespace S
26621 int i=5;
26622 int foo()
26624 int i=8;
26625 extern int i;
26626 return i;
26630 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26631 return context_die;
26633 /* If this decl is from an inlined function, then don't try to emit it in its
26634 namespace, as we will get confused. It would have already been emitted
26635 when the abstract instance of the inline function was emitted anyways. */
26636 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26637 return context_die;
26639 ns_context = setup_namespace_context (thing, context_die);
26641 if (ns_context != context_die)
26643 if (is_fortran () || is_dlang ())
26644 return ns_context;
26645 if (DECL_P (thing))
26646 gen_decl_die (thing, NULL, NULL, ns_context);
26647 else
26648 gen_type_die (thing, ns_context);
26650 return context_die;
26653 /* Generate a DIE for a namespace or namespace alias. */
26655 static void
26656 gen_namespace_die (tree decl, dw_die_ref context_die)
26658 dw_die_ref namespace_die;
26660 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26661 they are an alias of. */
26662 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26664 /* Output a real namespace or module. */
26665 context_die = setup_namespace_context (decl, comp_unit_die ());
26666 namespace_die = new_die (is_fortran () || is_dlang ()
26667 ? DW_TAG_module : DW_TAG_namespace,
26668 context_die, decl);
26669 /* For Fortran modules defined in different CU don't add src coords. */
26670 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26672 const char *name = dwarf2_name (decl, 0);
26673 if (name)
26674 add_name_attribute (namespace_die, name);
26676 else
26677 add_name_and_src_coords_attributes (namespace_die, decl);
26678 if (DECL_EXTERNAL (decl))
26679 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26680 equate_decl_number_to_die (decl, namespace_die);
26682 else
26684 /* Output a namespace alias. */
26686 /* Force out the namespace we are an alias of, if necessary. */
26687 dw_die_ref origin_die
26688 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26690 if (DECL_FILE_SCOPE_P (decl)
26691 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26692 context_die = setup_namespace_context (decl, comp_unit_die ());
26693 /* Now create the namespace alias DIE. */
26694 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26695 add_name_and_src_coords_attributes (namespace_die, decl);
26696 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26697 equate_decl_number_to_die (decl, namespace_die);
26699 if ((dwarf_version >= 5 || !dwarf_strict)
26700 && lang_hooks.decls.decl_dwarf_attribute (decl,
26701 DW_AT_export_symbols) == 1)
26702 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26704 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26705 if (want_pubnames ())
26706 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26709 /* Generate Dwarf debug information for a decl described by DECL.
26710 The return value is currently only meaningful for PARM_DECLs,
26711 for all other decls it returns NULL.
26713 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26714 It can be NULL otherwise. */
26716 static dw_die_ref
26717 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26718 dw_die_ref context_die)
26720 tree decl_or_origin = decl ? decl : origin;
26721 tree class_origin = NULL, ultimate_origin;
26723 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26724 return NULL;
26726 switch (TREE_CODE (decl_or_origin))
26728 case ERROR_MARK:
26729 break;
26731 case CONST_DECL:
26732 if (!is_fortran () && !is_ada () && !is_dlang ())
26734 /* The individual enumerators of an enum type get output when we output
26735 the Dwarf representation of the relevant enum type itself. */
26736 break;
26739 /* Emit its type. */
26740 gen_type_die (TREE_TYPE (decl), context_die);
26742 /* And its containing namespace. */
26743 context_die = declare_in_namespace (decl, context_die);
26745 gen_const_die (decl, context_die);
26746 break;
26748 case FUNCTION_DECL:
26749 #if 0
26750 /* FIXME */
26751 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26752 on local redeclarations of global functions. That seems broken. */
26753 if (current_function_decl != decl)
26754 /* This is only a declaration. */;
26755 #endif
26757 /* We should have abstract copies already and should not generate
26758 stray type DIEs in late LTO dumping. */
26759 if (! early_dwarf)
26762 /* If we're emitting a clone, emit info for the abstract instance. */
26763 else if (origin || DECL_ORIGIN (decl) != decl)
26764 dwarf2out_abstract_function (origin
26765 ? DECL_ORIGIN (origin)
26766 : DECL_ABSTRACT_ORIGIN (decl));
26768 /* If we're emitting a possibly inlined function emit it as
26769 abstract instance. */
26770 else if (cgraph_function_possibly_inlined_p (decl)
26771 && ! DECL_ABSTRACT_P (decl)
26772 && ! class_or_namespace_scope_p (context_die)
26773 /* dwarf2out_abstract_function won't emit a die if this is just
26774 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26775 that case, because that works only if we have a die. */
26776 && DECL_INITIAL (decl) != NULL_TREE)
26777 dwarf2out_abstract_function (decl);
26779 /* Otherwise we're emitting the primary DIE for this decl. */
26780 else if (debug_info_level > DINFO_LEVEL_TERSE)
26782 /* Before we describe the FUNCTION_DECL itself, make sure that we
26783 have its containing type. */
26784 if (!origin)
26785 origin = decl_class_context (decl);
26786 if (origin != NULL_TREE)
26787 gen_type_die (origin, context_die);
26789 /* And its return type. */
26790 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26792 /* And its virtual context. */
26793 if (DECL_VINDEX (decl) != NULL_TREE)
26794 gen_type_die (DECL_CONTEXT (decl), context_die);
26796 /* Make sure we have a member DIE for decl. */
26797 if (origin != NULL_TREE)
26798 gen_type_die_for_member (origin, decl, context_die);
26800 /* And its containing namespace. */
26801 context_die = declare_in_namespace (decl, context_die);
26804 /* Now output a DIE to represent the function itself. */
26805 if (decl)
26806 gen_subprogram_die (decl, context_die);
26807 break;
26809 case TYPE_DECL:
26810 /* If we are in terse mode, don't generate any DIEs to represent any
26811 actual typedefs. */
26812 if (debug_info_level <= DINFO_LEVEL_TERSE)
26813 break;
26815 /* In the special case of a TYPE_DECL node representing the declaration
26816 of some type tag, if the given TYPE_DECL is marked as having been
26817 instantiated from some other (original) TYPE_DECL node (e.g. one which
26818 was generated within the original definition of an inline function) we
26819 used to generate a special (abbreviated) DW_TAG_structure_type,
26820 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26821 should be actually referencing those DIEs, as variable DIEs with that
26822 type would be emitted already in the abstract origin, so it was always
26823 removed during unused type prunning. Don't add anything in this
26824 case. */
26825 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26826 break;
26828 if (is_redundant_typedef (decl))
26829 gen_type_die (TREE_TYPE (decl), context_die);
26830 else
26831 /* Output a DIE to represent the typedef itself. */
26832 gen_typedef_die (decl, context_die);
26833 break;
26835 case LABEL_DECL:
26836 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26837 gen_label_die (decl, context_die);
26838 break;
26840 case VAR_DECL:
26841 case RESULT_DECL:
26842 /* If we are in terse mode, don't generate any DIEs to represent any
26843 variable declarations or definitions unless it is external. */
26844 if (debug_info_level < DINFO_LEVEL_TERSE
26845 || (debug_info_level == DINFO_LEVEL_TERSE
26846 && !TREE_PUBLIC (decl_or_origin)))
26847 break;
26849 if (debug_info_level > DINFO_LEVEL_TERSE)
26851 /* Avoid generating stray type DIEs during late dwarf dumping.
26852 All types have been dumped early. */
26853 if (early_dwarf
26854 /* ??? But in LTRANS we cannot annotate early created variably
26855 modified type DIEs without copying them and adjusting all
26856 references to them. Dump them again as happens for inlining
26857 which copies both the decl and the types. */
26858 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26859 in VLA bound information for example. */
26860 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26861 current_function_decl)))
26863 /* Output any DIEs that are needed to specify the type of this data
26864 object. */
26865 if (decl_by_reference_p (decl_or_origin))
26866 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26867 else
26868 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26871 if (early_dwarf)
26873 /* And its containing type. */
26874 class_origin = decl_class_context (decl_or_origin);
26875 if (class_origin != NULL_TREE)
26876 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26878 /* And its containing namespace. */
26879 context_die = declare_in_namespace (decl_or_origin, context_die);
26883 /* Now output the DIE to represent the data object itself. This gets
26884 complicated because of the possibility that the VAR_DECL really
26885 represents an inlined instance of a formal parameter for an inline
26886 function. */
26887 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26888 if (ultimate_origin != NULL_TREE
26889 && TREE_CODE (ultimate_origin) == PARM_DECL)
26890 gen_formal_parameter_die (decl, origin,
26891 true /* Emit name attribute. */,
26892 context_die);
26893 else
26894 gen_variable_die (decl, origin, context_die);
26895 break;
26897 case FIELD_DECL:
26898 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26899 /* Ignore the nameless fields that are used to skip bits but handle C++
26900 anonymous unions and structs. */
26901 if (DECL_NAME (decl) != NULL_TREE
26902 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26903 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26905 gen_type_die (member_declared_type (decl), context_die);
26906 gen_field_die (decl, ctx, context_die);
26908 break;
26910 case PARM_DECL:
26911 /* Avoid generating stray type DIEs during late dwarf dumping.
26912 All types have been dumped early. */
26913 if (early_dwarf
26914 /* ??? But in LTRANS we cannot annotate early created variably
26915 modified type DIEs without copying them and adjusting all
26916 references to them. Dump them again as happens for inlining
26917 which copies both the decl and the types. */
26918 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26919 in VLA bound information for example. */
26920 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26921 current_function_decl)))
26923 if (DECL_BY_REFERENCE (decl_or_origin))
26924 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26925 else
26926 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26928 return gen_formal_parameter_die (decl, origin,
26929 true /* Emit name attribute. */,
26930 context_die);
26932 case NAMESPACE_DECL:
26933 if (dwarf_version >= 3 || !dwarf_strict)
26934 gen_namespace_die (decl, context_die);
26935 break;
26937 case IMPORTED_DECL:
26938 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26939 DECL_CONTEXT (decl), context_die);
26940 break;
26942 case NAMELIST_DECL:
26943 gen_namelist_decl (DECL_NAME (decl), context_die,
26944 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26945 break;
26947 default:
26948 /* Probably some frontend-internal decl. Assume we don't care. */
26949 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26950 break;
26953 return NULL;
26956 /* Output initial debug information for global DECL. Called at the
26957 end of the parsing process.
26959 This is the initial debug generation process. As such, the DIEs
26960 generated may be incomplete. A later debug generation pass
26961 (dwarf2out_late_global_decl) will augment the information generated
26962 in this pass (e.g., with complete location info). */
26964 static void
26965 dwarf2out_early_global_decl (tree decl)
26967 set_early_dwarf s;
26969 /* gen_decl_die() will set DECL_ABSTRACT because
26970 cgraph_function_possibly_inlined_p() returns true. This is in
26971 turn will cause DW_AT_inline attributes to be set.
26973 This happens because at early dwarf generation, there is no
26974 cgraph information, causing cgraph_function_possibly_inlined_p()
26975 to return true. Trick cgraph_function_possibly_inlined_p()
26976 while we generate dwarf early. */
26977 bool save = symtab->global_info_ready;
26978 symtab->global_info_ready = true;
26980 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26981 other DECLs and they can point to template types or other things
26982 that dwarf2out can't handle when done via dwarf2out_decl. */
26983 if (TREE_CODE (decl) != TYPE_DECL
26984 && TREE_CODE (decl) != PARM_DECL)
26986 if (TREE_CODE (decl) == FUNCTION_DECL)
26988 tree save_fndecl = current_function_decl;
26990 /* For nested functions, make sure we have DIEs for the parents first
26991 so that all nested DIEs are generated at the proper scope in the
26992 first shot. */
26993 tree context = decl_function_context (decl);
26994 if (context != NULL)
26996 dw_die_ref context_die = lookup_decl_die (context);
26997 current_function_decl = context;
26999 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27000 enough so that it lands in its own context. This avoids type
27001 pruning issues later on. */
27002 if (context_die == NULL || is_declaration_die (context_die))
27003 dwarf2out_early_global_decl (context);
27006 /* Emit an abstract origin of a function first. This happens
27007 with C++ constructor clones for example and makes
27008 dwarf2out_abstract_function happy which requires the early
27009 DIE of the abstract instance to be present. */
27010 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27011 dw_die_ref origin_die;
27012 if (origin != NULL
27013 /* Do not emit the DIE multiple times but make sure to
27014 process it fully here in case we just saw a declaration. */
27015 && ((origin_die = lookup_decl_die (origin)) == NULL
27016 || is_declaration_die (origin_die)))
27018 current_function_decl = origin;
27019 dwarf2out_decl (origin);
27022 /* Emit the DIE for decl but avoid doing that multiple times. */
27023 dw_die_ref old_die;
27024 if ((old_die = lookup_decl_die (decl)) == NULL
27025 || is_declaration_die (old_die))
27027 current_function_decl = decl;
27028 dwarf2out_decl (decl);
27031 current_function_decl = save_fndecl;
27033 else
27034 dwarf2out_decl (decl);
27036 symtab->global_info_ready = save;
27039 /* Return whether EXPR is an expression with the following pattern:
27040 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27042 static bool
27043 is_trivial_indirect_ref (tree expr)
27045 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27046 return false;
27048 tree nop = TREE_OPERAND (expr, 0);
27049 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27050 return false;
27052 tree int_cst = TREE_OPERAND (nop, 0);
27053 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27056 /* Output debug information for global decl DECL. Called from
27057 toplev.c after compilation proper has finished. */
27059 static void
27060 dwarf2out_late_global_decl (tree decl)
27062 /* Fill-in any location information we were unable to determine
27063 on the first pass. */
27064 if (VAR_P (decl))
27066 dw_die_ref die = lookup_decl_die (decl);
27068 /* We may have to generate full debug late for LTO in case debug
27069 was not enabled at compile-time or the target doesn't support
27070 the LTO early debug scheme. */
27071 if (! die && in_lto_p)
27072 dwarf2out_decl (decl);
27073 else if (die)
27075 /* We get called via the symtab code invoking late_global_decl
27076 for symbols that are optimized out.
27078 Do not add locations for those, except if they have a
27079 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27080 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27081 INDIRECT_REF expression, as this could generate relocations to
27082 text symbols in LTO object files, which is invalid. */
27083 varpool_node *node = varpool_node::get (decl);
27084 if ((! node || ! node->definition)
27085 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27086 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27087 tree_add_const_value_attribute_for_decl (die, decl);
27088 else
27089 add_location_or_const_value_attribute (die, decl, false);
27094 /* Output debug information for type decl DECL. Called from toplev.c
27095 and from language front ends (to record built-in types). */
27096 static void
27097 dwarf2out_type_decl (tree decl, int local)
27099 if (!local)
27101 set_early_dwarf s;
27102 dwarf2out_decl (decl);
27106 /* Output debug information for imported module or decl DECL.
27107 NAME is non-NULL name in the lexical block if the decl has been renamed.
27108 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27109 that DECL belongs to.
27110 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27111 static void
27112 dwarf2out_imported_module_or_decl_1 (tree decl,
27113 tree name,
27114 tree lexical_block,
27115 dw_die_ref lexical_block_die)
27117 expanded_location xloc;
27118 dw_die_ref imported_die = NULL;
27119 dw_die_ref at_import_die;
27121 if (TREE_CODE (decl) == IMPORTED_DECL)
27123 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27124 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27125 gcc_assert (decl);
27127 else
27128 xloc = expand_location (input_location);
27130 if (TREE_CODE (decl) == TYPE_DECL)
27132 at_import_die = force_type_die (TREE_TYPE (decl));
27133 /* For namespace N { typedef void T; } using N::T; base_type_die
27134 returns NULL, but DW_TAG_imported_declaration requires
27135 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27136 if (!at_import_die)
27138 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27139 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27140 at_import_die = lookup_type_die (TREE_TYPE (decl));
27141 gcc_assert (at_import_die);
27144 else
27146 at_import_die = lookup_decl_die (decl);
27147 if (!at_import_die)
27149 /* If we're trying to avoid duplicate debug info, we may not have
27150 emitted the member decl for this field. Emit it now. */
27151 if (TREE_CODE (decl) == FIELD_DECL)
27153 tree type = DECL_CONTEXT (decl);
27155 if (TYPE_CONTEXT (type)
27156 && TYPE_P (TYPE_CONTEXT (type))
27157 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27158 DINFO_USAGE_DIR_USE))
27159 return;
27160 gen_type_die_for_member (type, decl,
27161 get_context_die (TYPE_CONTEXT (type)));
27163 if (TREE_CODE (decl) == CONST_DECL)
27165 /* Individual enumerators of an enum type do not get output here
27166 (see gen_decl_die), so we cannot call force_decl_die. */
27167 if (!is_fortran () && !is_ada () && !is_dlang ())
27168 return;
27170 if (TREE_CODE (decl) == NAMELIST_DECL)
27171 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27172 get_context_die (DECL_CONTEXT (decl)),
27173 NULL_TREE);
27174 else
27175 at_import_die = force_decl_die (decl);
27179 if (TREE_CODE (decl) == NAMESPACE_DECL)
27181 if (dwarf_version >= 3 || !dwarf_strict)
27182 imported_die = new_die (DW_TAG_imported_module,
27183 lexical_block_die,
27184 lexical_block);
27185 else
27186 return;
27188 else
27189 imported_die = new_die (DW_TAG_imported_declaration,
27190 lexical_block_die,
27191 lexical_block);
27193 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27194 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27195 if (debug_column_info && xloc.column)
27196 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27197 if (name)
27198 add_AT_string (imported_die, DW_AT_name,
27199 IDENTIFIER_POINTER (name));
27200 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27203 /* Output debug information for imported module or decl DECL.
27204 NAME is non-NULL name in context if the decl has been renamed.
27205 CHILD is true if decl is one of the renamed decls as part of
27206 importing whole module.
27207 IMPLICIT is set if this hook is called for an implicit import
27208 such as inline namespace. */
27210 static void
27211 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27212 bool child, bool implicit)
27214 /* dw_die_ref at_import_die; */
27215 dw_die_ref scope_die;
27217 if (debug_info_level <= DINFO_LEVEL_TERSE)
27218 return;
27220 gcc_assert (decl);
27222 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27223 should be enough, for DWARF4 and older even if we emit as extension
27224 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27225 for the benefit of consumers unaware of DW_AT_export_symbols. */
27226 if (implicit
27227 && dwarf_version >= 5
27228 && lang_hooks.decls.decl_dwarf_attribute (decl,
27229 DW_AT_export_symbols) == 1)
27230 return;
27232 set_early_dwarf s;
27234 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27235 We need decl DIE for reference and scope die. First, get DIE for the decl
27236 itself. */
27238 /* Get the scope die for decl context. Use comp_unit_die for global module
27239 or decl. If die is not found for non globals, force new die. */
27240 if (context
27241 && TYPE_P (context)
27242 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27243 return;
27245 scope_die = get_context_die (context);
27247 if (child)
27249 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27250 there is nothing we can do, here. */
27251 if (dwarf_version < 3 && dwarf_strict)
27252 return;
27254 gcc_assert (scope_die->die_child);
27255 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27256 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27257 scope_die = scope_die->die_child;
27260 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27261 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27264 /* Output debug information for namelists. */
27266 static dw_die_ref
27267 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27269 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27270 tree value;
27271 unsigned i;
27273 if (debug_info_level <= DINFO_LEVEL_TERSE)
27274 return NULL;
27276 gcc_assert (scope_die != NULL);
27277 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27278 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27280 /* If there are no item_decls, we have a nondefining namelist, e.g.
27281 with USE association; hence, set DW_AT_declaration. */
27282 if (item_decls == NULL_TREE)
27284 add_AT_flag (nml_die, DW_AT_declaration, 1);
27285 return nml_die;
27288 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27290 nml_item_ref_die = lookup_decl_die (value);
27291 if (!nml_item_ref_die)
27292 nml_item_ref_die = force_decl_die (value);
27294 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27295 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27297 return nml_die;
27301 /* Write the debugging output for DECL and return the DIE. */
27303 static void
27304 dwarf2out_decl (tree decl)
27306 dw_die_ref context_die = comp_unit_die ();
27308 switch (TREE_CODE (decl))
27310 case ERROR_MARK:
27311 return;
27313 case FUNCTION_DECL:
27314 /* If we're a nested function, initially use a parent of NULL; if we're
27315 a plain function, this will be fixed up in decls_for_scope. If
27316 we're a method, it will be ignored, since we already have a DIE.
27317 Avoid doing this late though since clones of class methods may
27318 otherwise end up in limbo and create type DIEs late. */
27319 if (early_dwarf
27320 && decl_function_context (decl)
27321 /* But if we're in terse mode, we don't care about scope. */
27322 && debug_info_level > DINFO_LEVEL_TERSE)
27323 context_die = NULL;
27324 break;
27326 case VAR_DECL:
27327 /* For local statics lookup proper context die. */
27328 if (local_function_static (decl))
27329 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27331 /* If we are in terse mode, don't generate any DIEs to represent any
27332 variable declarations or definitions unless it is external. */
27333 if (debug_info_level < DINFO_LEVEL_TERSE
27334 || (debug_info_level == DINFO_LEVEL_TERSE
27335 && !TREE_PUBLIC (decl)))
27336 return;
27337 break;
27339 case CONST_DECL:
27340 if (debug_info_level <= DINFO_LEVEL_TERSE)
27341 return;
27342 if (!is_fortran () && !is_ada () && !is_dlang ())
27343 return;
27344 if (TREE_STATIC (decl) && decl_function_context (decl))
27345 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27346 break;
27348 case NAMESPACE_DECL:
27349 case IMPORTED_DECL:
27350 if (debug_info_level <= DINFO_LEVEL_TERSE)
27351 return;
27352 if (lookup_decl_die (decl) != NULL)
27353 return;
27354 break;
27356 case TYPE_DECL:
27357 /* Don't emit stubs for types unless they are needed by other DIEs. */
27358 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27359 return;
27361 /* Don't bother trying to generate any DIEs to represent any of the
27362 normal built-in types for the language we are compiling. */
27363 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27364 return;
27366 /* If we are in terse mode, don't generate any DIEs for types. */
27367 if (debug_info_level <= DINFO_LEVEL_TERSE)
27368 return;
27370 /* If we're a function-scope tag, initially use a parent of NULL;
27371 this will be fixed up in decls_for_scope. */
27372 if (decl_function_context (decl))
27373 context_die = NULL;
27375 break;
27377 case NAMELIST_DECL:
27378 break;
27380 default:
27381 return;
27384 gen_decl_die (decl, NULL, NULL, context_die);
27386 if (flag_checking)
27388 dw_die_ref die = lookup_decl_die (decl);
27389 if (die)
27390 check_die (die);
27394 /* Write the debugging output for DECL. */
27396 static void
27397 dwarf2out_function_decl (tree decl)
27399 dwarf2out_decl (decl);
27400 call_arg_locations = NULL;
27401 call_arg_loc_last = NULL;
27402 call_site_count = -1;
27403 tail_call_site_count = -1;
27404 decl_loc_table->empty ();
27405 cached_dw_loc_list_table->empty ();
27408 /* Output a marker (i.e. a label) for the beginning of the generated code for
27409 a lexical block. */
27411 static void
27412 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27413 unsigned int blocknum)
27415 switch_to_section (current_function_section ());
27416 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27419 /* Output a marker (i.e. a label) for the end of the generated code for a
27420 lexical block. */
27422 static void
27423 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27425 switch_to_section (current_function_section ());
27426 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27429 /* Returns nonzero if it is appropriate not to emit any debugging
27430 information for BLOCK, because it doesn't contain any instructions.
27432 Don't allow this for blocks with nested functions or local classes
27433 as we would end up with orphans, and in the presence of scheduling
27434 we may end up calling them anyway. */
27436 static bool
27437 dwarf2out_ignore_block (const_tree block)
27439 tree decl;
27440 unsigned int i;
27442 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27443 if (TREE_CODE (decl) == FUNCTION_DECL
27444 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27445 return 0;
27446 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27448 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27449 if (TREE_CODE (decl) == FUNCTION_DECL
27450 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27451 return 0;
27454 return 1;
27457 /* Hash table routines for file_hash. */
27459 bool
27460 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27462 return filename_cmp (p1->key, p2) == 0;
27465 hashval_t
27466 dwarf_file_hasher::hash (dwarf_file_data *p)
27468 return htab_hash_string (p->key);
27471 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27472 dwarf2out.c) and return its "index". The index of each (known) filename is
27473 just a unique number which is associated with only that one filename. We
27474 need such numbers for the sake of generating labels (in the .debug_sfnames
27475 section) and references to those files numbers (in the .debug_srcinfo
27476 and .debug_macinfo sections). If the filename given as an argument is not
27477 found in our current list, add it to the list and assign it the next
27478 available unique index number. */
27480 static struct dwarf_file_data *
27481 lookup_filename (const char *file_name)
27483 struct dwarf_file_data * created;
27485 if (!file_name)
27486 return NULL;
27488 if (!file_name[0])
27489 file_name = "<stdin>";
27491 dwarf_file_data **slot
27492 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27493 INSERT);
27494 if (*slot)
27495 return *slot;
27497 created = ggc_alloc<dwarf_file_data> ();
27498 created->key = file_name;
27499 created->filename = remap_debug_filename (file_name);
27500 created->emitted_number = 0;
27501 *slot = created;
27502 return created;
27505 /* If the assembler will construct the file table, then translate the compiler
27506 internal file table number into the assembler file table number, and emit
27507 a .file directive if we haven't already emitted one yet. The file table
27508 numbers are different because we prune debug info for unused variables and
27509 types, which may include filenames. */
27511 static int
27512 maybe_emit_file (struct dwarf_file_data * fd)
27514 if (! fd->emitted_number)
27516 if (last_emitted_file)
27517 fd->emitted_number = last_emitted_file->emitted_number + 1;
27518 else
27519 fd->emitted_number = 1;
27520 last_emitted_file = fd;
27522 if (output_asm_line_debug_info ())
27524 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27525 output_quoted_string (asm_out_file, fd->filename);
27526 fputc ('\n', asm_out_file);
27530 return fd->emitted_number;
27533 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27534 That generation should happen after function debug info has been
27535 generated. The value of the attribute is the constant value of ARG. */
27537 static void
27538 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27540 die_arg_entry entry;
27542 if (!die || !arg)
27543 return;
27545 gcc_assert (early_dwarf);
27547 if (!tmpl_value_parm_die_table)
27548 vec_alloc (tmpl_value_parm_die_table, 32);
27550 entry.die = die;
27551 entry.arg = arg;
27552 vec_safe_push (tmpl_value_parm_die_table, entry);
27555 /* Return TRUE if T is an instance of generic type, FALSE
27556 otherwise. */
27558 static bool
27559 generic_type_p (tree t)
27561 if (t == NULL_TREE || !TYPE_P (t))
27562 return false;
27563 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27566 /* Schedule the generation of the generic parameter dies for the
27567 instance of generic type T. The proper generation itself is later
27568 done by gen_scheduled_generic_parms_dies. */
27570 static void
27571 schedule_generic_params_dies_gen (tree t)
27573 if (!generic_type_p (t))
27574 return;
27576 gcc_assert (early_dwarf);
27578 if (!generic_type_instances)
27579 vec_alloc (generic_type_instances, 256);
27581 vec_safe_push (generic_type_instances, t);
27584 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27585 by append_entry_to_tmpl_value_parm_die_table. This function must
27586 be called after function DIEs have been generated. */
27588 static void
27589 gen_remaining_tmpl_value_param_die_attribute (void)
27591 if (tmpl_value_parm_die_table)
27593 unsigned i, j;
27594 die_arg_entry *e;
27596 /* We do this in two phases - first get the cases we can
27597 handle during early-finish, preserving those we cannot
27598 (containing symbolic constants where we don't yet know
27599 whether we are going to output the referenced symbols).
27600 For those we try again at late-finish. */
27601 j = 0;
27602 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27604 if (!e->die->removed
27605 && !tree_add_const_value_attribute (e->die, e->arg))
27607 dw_loc_descr_ref loc = NULL;
27608 if (! early_dwarf
27609 && (dwarf_version >= 5 || !dwarf_strict))
27610 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27611 if (loc)
27612 add_AT_loc (e->die, DW_AT_location, loc);
27613 else
27614 (*tmpl_value_parm_die_table)[j++] = *e;
27617 tmpl_value_parm_die_table->truncate (j);
27621 /* Generate generic parameters DIEs for instances of generic types
27622 that have been previously scheduled by
27623 schedule_generic_params_dies_gen. This function must be called
27624 after all the types of the CU have been laid out. */
27626 static void
27627 gen_scheduled_generic_parms_dies (void)
27629 unsigned i;
27630 tree t;
27632 if (!generic_type_instances)
27633 return;
27635 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27636 if (COMPLETE_TYPE_P (t))
27637 gen_generic_params_dies (t);
27639 generic_type_instances = NULL;
27643 /* Replace DW_AT_name for the decl with name. */
27645 static void
27646 dwarf2out_set_name (tree decl, tree name)
27648 dw_die_ref die;
27649 dw_attr_node *attr;
27650 const char *dname;
27652 die = TYPE_SYMTAB_DIE (decl);
27653 if (!die)
27654 return;
27656 dname = dwarf2_name (name, 0);
27657 if (!dname)
27658 return;
27660 attr = get_AT (die, DW_AT_name);
27661 if (attr)
27663 struct indirect_string_node *node;
27665 node = find_AT_string (dname);
27666 /* replace the string. */
27667 attr->dw_attr_val.v.val_str = node;
27670 else
27671 add_name_attribute (die, dname);
27674 /* True if before or during processing of the first function being emitted. */
27675 static bool in_first_function_p = true;
27676 /* True if loc_note during dwarf2out_var_location call might still be
27677 before first real instruction at address equal to .Ltext0. */
27678 static bool maybe_at_text_label_p = true;
27679 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27680 static unsigned int first_loclabel_num_not_at_text_label;
27682 /* Look ahead for a real insn. */
27684 static rtx_insn *
27685 dwarf2out_next_real_insn (rtx_insn *loc_note)
27687 rtx_insn *next_real = NEXT_INSN (loc_note);
27689 while (next_real)
27690 if (INSN_P (next_real))
27691 break;
27692 else
27693 next_real = NEXT_INSN (next_real);
27695 return next_real;
27698 /* Called by the final INSN scan whenever we see a var location. We
27699 use it to drop labels in the right places, and throw the location in
27700 our lookup table. */
27702 static void
27703 dwarf2out_var_location (rtx_insn *loc_note)
27705 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27706 struct var_loc_node *newloc;
27707 rtx_insn *next_real;
27708 rtx_insn *call_insn = NULL;
27709 static const char *last_label;
27710 static const char *last_postcall_label;
27711 static bool last_in_cold_section_p;
27712 static rtx_insn *expected_next_loc_note;
27713 tree decl;
27714 bool var_loc_p;
27715 var_loc_view view = 0;
27717 if (!NOTE_P (loc_note))
27719 if (CALL_P (loc_note))
27721 maybe_reset_location_view (loc_note, cur_line_info_table);
27722 call_site_count++;
27723 if (SIBLING_CALL_P (loc_note))
27724 tail_call_site_count++;
27725 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27727 call_insn = loc_note;
27728 loc_note = NULL;
27729 var_loc_p = false;
27731 next_real = dwarf2out_next_real_insn (call_insn);
27732 cached_next_real_insn = NULL;
27733 goto create_label;
27735 if (optimize == 0 && !flag_var_tracking)
27737 /* When the var-tracking pass is not running, there is no note
27738 for indirect calls whose target is compile-time known. In this
27739 case, process such calls specifically so that we generate call
27740 sites for them anyway. */
27741 rtx x = PATTERN (loc_note);
27742 if (GET_CODE (x) == PARALLEL)
27743 x = XVECEXP (x, 0, 0);
27744 if (GET_CODE (x) == SET)
27745 x = SET_SRC (x);
27746 if (GET_CODE (x) == CALL)
27747 x = XEXP (x, 0);
27748 if (!MEM_P (x)
27749 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27750 || !SYMBOL_REF_DECL (XEXP (x, 0))
27751 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27752 != FUNCTION_DECL))
27754 call_insn = loc_note;
27755 loc_note = NULL;
27756 var_loc_p = false;
27758 next_real = dwarf2out_next_real_insn (call_insn);
27759 cached_next_real_insn = NULL;
27760 goto create_label;
27764 else if (!debug_variable_location_views)
27765 gcc_unreachable ();
27766 else
27767 maybe_reset_location_view (loc_note, cur_line_info_table);
27769 return;
27772 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27773 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27774 return;
27776 /* Optimize processing a large consecutive sequence of location
27777 notes so we don't spend too much time in next_real_insn. If the
27778 next insn is another location note, remember the next_real_insn
27779 calculation for next time. */
27780 next_real = cached_next_real_insn;
27781 if (next_real)
27783 if (expected_next_loc_note != loc_note)
27784 next_real = NULL;
27787 if (! next_real)
27788 next_real = dwarf2out_next_real_insn (loc_note);
27790 if (next_real)
27792 rtx_insn *next_note = NEXT_INSN (loc_note);
27793 while (next_note != next_real)
27795 if (! next_note->deleted ()
27796 && NOTE_P (next_note)
27797 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27798 break;
27799 next_note = NEXT_INSN (next_note);
27802 if (next_note == next_real)
27803 cached_next_real_insn = NULL;
27804 else
27806 expected_next_loc_note = next_note;
27807 cached_next_real_insn = next_real;
27810 else
27811 cached_next_real_insn = NULL;
27813 /* If there are no instructions which would be affected by this note,
27814 don't do anything. */
27815 if (var_loc_p
27816 && next_real == NULL_RTX
27817 && !NOTE_DURING_CALL_P (loc_note))
27818 return;
27820 create_label:
27822 if (next_real == NULL_RTX)
27823 next_real = get_last_insn ();
27825 /* If there were any real insns between note we processed last time
27826 and this note (or if it is the first note), clear
27827 last_{,postcall_}label so that they are not reused this time. */
27828 if (last_var_location_insn == NULL_RTX
27829 || last_var_location_insn != next_real
27830 || last_in_cold_section_p != in_cold_section_p)
27832 last_label = NULL;
27833 last_postcall_label = NULL;
27836 if (var_loc_p)
27838 const char *label
27839 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27840 view = cur_line_info_table->view;
27841 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27842 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27843 if (newloc == NULL)
27844 return;
27846 else
27848 decl = NULL_TREE;
27849 newloc = NULL;
27852 /* If there were no real insns between note we processed last time
27853 and this note, use the label we emitted last time. Otherwise
27854 create a new label and emit it. */
27855 if (last_label == NULL)
27857 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27858 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27859 loclabel_num++;
27860 last_label = ggc_strdup (loclabel);
27861 /* See if loclabel might be equal to .Ltext0. If yes,
27862 bump first_loclabel_num_not_at_text_label. */
27863 if (!have_multiple_function_sections
27864 && in_first_function_p
27865 && maybe_at_text_label_p)
27867 static rtx_insn *last_start;
27868 rtx_insn *insn;
27869 for (insn = loc_note; insn; insn = previous_insn (insn))
27870 if (insn == last_start)
27871 break;
27872 else if (!NONDEBUG_INSN_P (insn))
27873 continue;
27874 else
27876 rtx body = PATTERN (insn);
27877 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27878 continue;
27879 /* Inline asm could occupy zero bytes. */
27880 else if (GET_CODE (body) == ASM_INPUT
27881 || asm_noperands (body) >= 0)
27882 continue;
27883 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27884 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27885 continue;
27886 #endif
27887 else
27889 /* Assume insn has non-zero length. */
27890 maybe_at_text_label_p = false;
27891 break;
27894 if (maybe_at_text_label_p)
27896 last_start = loc_note;
27897 first_loclabel_num_not_at_text_label = loclabel_num;
27902 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27903 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27905 if (!var_loc_p)
27907 struct call_arg_loc_node *ca_loc
27908 = ggc_cleared_alloc<call_arg_loc_node> ();
27909 rtx_insn *prev = call_insn;
27911 ca_loc->call_arg_loc_note
27912 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27913 ca_loc->next = NULL;
27914 ca_loc->label = last_label;
27915 gcc_assert (prev
27916 && (CALL_P (prev)
27917 || (NONJUMP_INSN_P (prev)
27918 && GET_CODE (PATTERN (prev)) == SEQUENCE
27919 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27920 if (!CALL_P (prev))
27921 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27922 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27924 /* Look for a SYMBOL_REF in the "prev" instruction. */
27925 rtx x = get_call_rtx_from (prev);
27926 if (x)
27928 /* Try to get the call symbol, if any. */
27929 if (MEM_P (XEXP (x, 0)))
27930 x = XEXP (x, 0);
27931 /* First, look for a memory access to a symbol_ref. */
27932 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27933 && SYMBOL_REF_DECL (XEXP (x, 0))
27934 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27935 ca_loc->symbol_ref = XEXP (x, 0);
27936 /* Otherwise, look at a compile-time known user-level function
27937 declaration. */
27938 else if (MEM_P (x)
27939 && MEM_EXPR (x)
27940 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27941 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27944 ca_loc->block = insn_scope (prev);
27945 if (call_arg_locations)
27946 call_arg_loc_last->next = ca_loc;
27947 else
27948 call_arg_locations = ca_loc;
27949 call_arg_loc_last = ca_loc;
27951 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27953 newloc->label = last_label;
27954 newloc->view = view;
27956 else
27958 if (!last_postcall_label)
27960 sprintf (loclabel, "%s-1", last_label);
27961 last_postcall_label = ggc_strdup (loclabel);
27963 newloc->label = last_postcall_label;
27964 /* ??? This view is at last_label, not last_label-1, but we
27965 could only assume view at last_label-1 is zero if we could
27966 assume calls always have length greater than one. This is
27967 probably true in general, though there might be a rare
27968 exception to this rule, e.g. if a call insn is optimized out
27969 by target magic. Then, even the -1 in the label will be
27970 wrong, which might invalidate the range. Anyway, using view,
27971 though technically possibly incorrect, will work as far as
27972 ranges go: since L-1 is in the middle of the call insn,
27973 (L-1).0 and (L-1).V shouldn't make any difference, and having
27974 the loclist entry refer to the .loc entry might be useful, so
27975 leave it like this. */
27976 newloc->view = view;
27979 if (var_loc_p && flag_debug_asm)
27981 const char *name, *sep, *patstr;
27982 if (decl && DECL_NAME (decl))
27983 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27984 else
27985 name = "";
27986 if (NOTE_VAR_LOCATION_LOC (loc_note))
27988 sep = " => ";
27989 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27991 else
27993 sep = " ";
27994 patstr = "RESET";
27996 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27997 name, sep, patstr);
28000 last_var_location_insn = next_real;
28001 last_in_cold_section_p = in_cold_section_p;
28004 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28005 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28006 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28007 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28008 BLOCK_FRAGMENT_ORIGIN links. */
28009 static bool
28010 block_within_block_p (tree block, tree outer, bool bothways)
28012 if (block == outer)
28013 return true;
28015 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28016 for (tree context = BLOCK_SUPERCONTEXT (block);
28017 context != outer;
28018 context = BLOCK_SUPERCONTEXT (context))
28019 if (!context || TREE_CODE (context) != BLOCK)
28020 return false;
28022 if (!bothways)
28023 return true;
28025 /* Now check that each block is actually referenced by its
28026 parent. */
28027 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28028 context = BLOCK_SUPERCONTEXT (context))
28030 if (BLOCK_FRAGMENT_ORIGIN (context))
28032 gcc_assert (!BLOCK_SUBBLOCKS (context));
28033 context = BLOCK_FRAGMENT_ORIGIN (context);
28035 for (tree sub = BLOCK_SUBBLOCKS (context);
28036 sub != block;
28037 sub = BLOCK_CHAIN (sub))
28038 if (!sub)
28039 return false;
28040 if (context == outer)
28041 return true;
28042 else
28043 block = context;
28047 /* Called during final while assembling the marker of the entry point
28048 for an inlined function. */
28050 static void
28051 dwarf2out_inline_entry (tree block)
28053 gcc_assert (debug_inline_points);
28055 /* If we can't represent it, don't bother. */
28056 if (!(dwarf_version >= 3 || !dwarf_strict))
28057 return;
28059 gcc_assert (DECL_P (block_ultimate_origin (block)));
28061 /* Sanity check the block tree. This would catch a case in which
28062 BLOCK got removed from the tree reachable from the outermost
28063 lexical block, but got retained in markers. It would still link
28064 back to its parents, but some ancestor would be missing a link
28065 down the path to the sub BLOCK. If the block got removed, its
28066 BLOCK_NUMBER will not be a usable value. */
28067 if (flag_checking)
28068 gcc_assert (block_within_block_p (block,
28069 DECL_INITIAL (current_function_decl),
28070 true));
28072 gcc_assert (inlined_function_outer_scope_p (block));
28073 gcc_assert (!lookup_block_die (block));
28075 if (BLOCK_FRAGMENT_ORIGIN (block))
28076 block = BLOCK_FRAGMENT_ORIGIN (block);
28077 /* Can the entry point ever not be at the beginning of an
28078 unfragmented lexical block? */
28079 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28080 || (cur_line_info_table
28081 && !ZERO_VIEW_P (cur_line_info_table->view))))
28082 return;
28084 if (!inline_entry_data_table)
28085 inline_entry_data_table
28086 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28089 inline_entry_data **iedp
28090 = inline_entry_data_table->find_slot_with_hash (block,
28091 htab_hash_pointer (block),
28092 INSERT);
28093 if (*iedp)
28094 /* ??? Ideally, we'd record all entry points for the same inlined
28095 function (some may have been duplicated by e.g. unrolling), but
28096 we have no way to represent that ATM. */
28097 return;
28099 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28100 ied->block = block;
28101 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28102 ied->label_num = BLOCK_NUMBER (block);
28103 if (cur_line_info_table)
28104 ied->view = cur_line_info_table->view;
28106 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28107 BLOCK_NUMBER (block));
28110 /* Called from finalize_size_functions for size functions so that their body
28111 can be encoded in the debug info to describe the layout of variable-length
28112 structures. */
28114 static void
28115 dwarf2out_size_function (tree decl)
28117 set_early_dwarf s;
28118 function_to_dwarf_procedure (decl);
28121 /* Note in one location list that text section has changed. */
28124 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28126 var_loc_list *list = *slot;
28127 if (list->first)
28128 list->last_before_switch
28129 = list->last->next ? list->last->next : list->last;
28130 return 1;
28133 /* Note in all location lists that text section has changed. */
28135 static void
28136 var_location_switch_text_section (void)
28138 if (decl_loc_table == NULL)
28139 return;
28141 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28144 /* Create a new line number table. */
28146 static dw_line_info_table *
28147 new_line_info_table (void)
28149 dw_line_info_table *table;
28151 table = ggc_cleared_alloc<dw_line_info_table> ();
28152 table->file_num = 1;
28153 table->line_num = 1;
28154 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28155 FORCE_RESET_NEXT_VIEW (table->view);
28156 table->symviews_since_reset = 0;
28158 return table;
28161 /* Lookup the "current" table into which we emit line info, so
28162 that we don't have to do it for every source line. */
28164 static void
28165 set_cur_line_info_table (section *sec)
28167 dw_line_info_table *table;
28169 if (sec == text_section)
28170 table = text_section_line_info;
28171 else if (sec == cold_text_section)
28173 table = cold_text_section_line_info;
28174 if (!table)
28176 cold_text_section_line_info = table = new_line_info_table ();
28177 table->end_label = cold_end_label;
28180 else
28182 const char *end_label;
28184 if (crtl->has_bb_partition)
28186 if (in_cold_section_p)
28187 end_label = crtl->subsections.cold_section_end_label;
28188 else
28189 end_label = crtl->subsections.hot_section_end_label;
28191 else
28193 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28194 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28195 current_function_funcdef_no);
28196 end_label = ggc_strdup (label);
28199 table = new_line_info_table ();
28200 table->end_label = end_label;
28202 vec_safe_push (separate_line_info, table);
28205 if (output_asm_line_debug_info ())
28206 table->is_stmt = (cur_line_info_table
28207 ? cur_line_info_table->is_stmt
28208 : DWARF_LINE_DEFAULT_IS_STMT_START);
28209 cur_line_info_table = table;
28213 /* We need to reset the locations at the beginning of each
28214 function. We can't do this in the end_function hook, because the
28215 declarations that use the locations won't have been output when
28216 that hook is called. Also compute have_multiple_function_sections here. */
28218 static void
28219 dwarf2out_begin_function (tree fun)
28221 section *sec = function_section (fun);
28223 if (sec != text_section)
28224 have_multiple_function_sections = true;
28226 if (crtl->has_bb_partition && !cold_text_section)
28228 gcc_assert (current_function_decl == fun);
28229 cold_text_section = unlikely_text_section ();
28230 switch_to_section (cold_text_section);
28231 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28232 switch_to_section (sec);
28235 call_site_count = 0;
28236 tail_call_site_count = 0;
28238 set_cur_line_info_table (sec);
28239 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28242 /* Helper function of dwarf2out_end_function, called only after emitting
28243 the very first function into assembly. Check if some .debug_loc range
28244 might end with a .LVL* label that could be equal to .Ltext0.
28245 In that case we must force using absolute addresses in .debug_loc ranges,
28246 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28247 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28248 list terminator.
28249 Set have_multiple_function_sections to true in that case and
28250 terminate htab traversal. */
28253 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28255 var_loc_list *entry = *slot;
28256 struct var_loc_node *node;
28258 node = entry->first;
28259 if (node && node->next && node->next->label)
28261 unsigned int i;
28262 const char *label = node->next->label;
28263 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28265 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28267 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28268 if (strcmp (label, loclabel) == 0)
28270 have_multiple_function_sections = true;
28271 return 0;
28275 return 1;
28278 /* Hook called after emitting a function into assembly.
28279 This does something only for the very first function emitted. */
28281 static void
28282 dwarf2out_end_function (unsigned int)
28284 if (in_first_function_p
28285 && !have_multiple_function_sections
28286 && first_loclabel_num_not_at_text_label
28287 && decl_loc_table)
28288 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28289 in_first_function_p = false;
28290 maybe_at_text_label_p = false;
28293 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28294 front-ends register a translation unit even before dwarf2out_init is
28295 called. */
28296 static tree main_translation_unit = NULL_TREE;
28298 /* Hook called by front-ends after they built their main translation unit.
28299 Associate comp_unit_die to UNIT. */
28301 static void
28302 dwarf2out_register_main_translation_unit (tree unit)
28304 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28305 && main_translation_unit == NULL_TREE);
28306 main_translation_unit = unit;
28307 /* If dwarf2out_init has not been called yet, it will perform the association
28308 itself looking at main_translation_unit. */
28309 if (decl_die_table != NULL)
28310 equate_decl_number_to_die (unit, comp_unit_die ());
28313 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28315 static void
28316 push_dw_line_info_entry (dw_line_info_table *table,
28317 enum dw_line_info_opcode opcode, unsigned int val)
28319 dw_line_info_entry e;
28320 e.opcode = opcode;
28321 e.val = val;
28322 vec_safe_push (table->entries, e);
28325 /* Output a label to mark the beginning of a source code line entry
28326 and record information relating to this source line, in
28327 'line_info_table' for later output of the .debug_line section. */
28328 /* ??? The discriminator parameter ought to be unsigned. */
28330 static void
28331 dwarf2out_source_line (unsigned int line, unsigned int column,
28332 const char *filename,
28333 int discriminator, bool is_stmt)
28335 unsigned int file_num;
28336 dw_line_info_table *table;
28337 static var_loc_view lvugid;
28339 /* 'line_info_table' information gathering is not needed when the debug
28340 info level is set to the lowest value. Also, the current DWARF-based
28341 debug formats do not use this info. */
28342 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28343 return;
28345 table = cur_line_info_table;
28347 if (line == 0)
28349 if (debug_variable_location_views
28350 && output_asm_line_debug_info ()
28351 && table && !RESETTING_VIEW_P (table->view))
28353 /* If we're using the assembler to compute view numbers, we
28354 can't issue a .loc directive for line zero, so we can't
28355 get a view number at this point. We might attempt to
28356 compute it from the previous view, or equate it to a
28357 subsequent view (though it might not be there!), but
28358 since we're omitting the line number entry, we might as
28359 well omit the view number as well. That means pretending
28360 it's a view number zero, which might very well turn out
28361 to be correct. ??? Extend the assembler so that the
28362 compiler could emit e.g. ".locview .LVU#", to output a
28363 view without changing line number information. We'd then
28364 have to count it in symviews_since_reset; when it's omitted,
28365 it doesn't count. */
28366 if (!zero_view_p)
28367 zero_view_p = BITMAP_GGC_ALLOC ();
28368 bitmap_set_bit (zero_view_p, table->view);
28369 if (flag_debug_asm)
28371 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28372 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28373 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28374 ASM_COMMENT_START);
28375 assemble_name (asm_out_file, label);
28376 putc ('\n', asm_out_file);
28378 table->view = ++lvugid;
28380 return;
28383 /* The discriminator column was added in dwarf4. Simplify the below
28384 by simply removing it if we're not supposed to output it. */
28385 if (dwarf_version < 4 && dwarf_strict)
28386 discriminator = 0;
28388 if (!debug_column_info)
28389 column = 0;
28391 file_num = maybe_emit_file (lookup_filename (filename));
28393 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28394 the debugger has used the second (possibly duplicate) line number
28395 at the beginning of the function to mark the end of the prologue.
28396 We could eliminate any other duplicates within the function. For
28397 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28398 that second line number entry. */
28399 /* Recall that this end-of-prologue indication is *not* the same thing
28400 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28401 to which the hook corresponds, follows the last insn that was
28402 emitted by gen_prologue. What we need is to precede the first insn
28403 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28404 insn that corresponds to something the user wrote. These may be
28405 very different locations once scheduling is enabled. */
28407 if (0 && file_num == table->file_num
28408 && line == table->line_num
28409 && column == table->column_num
28410 && discriminator == table->discrim_num
28411 && is_stmt == table->is_stmt)
28412 return;
28414 switch_to_section (current_function_section ());
28416 /* If requested, emit something human-readable. */
28417 if (flag_debug_asm)
28419 if (debug_column_info)
28420 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28421 filename, line, column);
28422 else
28423 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28424 filename, line);
28427 if (output_asm_line_debug_info ())
28429 /* Emit the .loc directive understood by GNU as. */
28430 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28431 file_num, line, is_stmt, discriminator */
28432 fputs ("\t.loc ", asm_out_file);
28433 fprint_ul (asm_out_file, file_num);
28434 putc (' ', asm_out_file);
28435 fprint_ul (asm_out_file, line);
28436 putc (' ', asm_out_file);
28437 fprint_ul (asm_out_file, column);
28439 if (is_stmt != table->is_stmt)
28441 #if HAVE_GAS_LOC_STMT
28442 fputs (" is_stmt ", asm_out_file);
28443 putc (is_stmt ? '1' : '0', asm_out_file);
28444 #endif
28446 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28448 gcc_assert (discriminator > 0);
28449 fputs (" discriminator ", asm_out_file);
28450 fprint_ul (asm_out_file, (unsigned long) discriminator);
28452 if (debug_variable_location_views)
28454 if (!RESETTING_VIEW_P (table->view))
28456 table->symviews_since_reset++;
28457 if (table->symviews_since_reset > symview_upper_bound)
28458 symview_upper_bound = table->symviews_since_reset;
28459 /* When we're using the assembler to compute view
28460 numbers, we output symbolic labels after "view" in
28461 .loc directives, and the assembler will set them for
28462 us, so that we can refer to the view numbers in
28463 location lists. The only exceptions are when we know
28464 a view will be zero: "-0" is a forced reset, used
28465 e.g. in the beginning of functions, whereas "0" tells
28466 the assembler to check that there was a PC change
28467 since the previous view, in a way that implicitly
28468 resets the next view. */
28469 fputs (" view ", asm_out_file);
28470 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28471 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28472 assemble_name (asm_out_file, label);
28473 table->view = ++lvugid;
28475 else
28477 table->symviews_since_reset = 0;
28478 if (FORCE_RESETTING_VIEW_P (table->view))
28479 fputs (" view -0", asm_out_file);
28480 else
28481 fputs (" view 0", asm_out_file);
28482 /* Mark the present view as a zero view. Earlier debug
28483 binds may have already added its id to loclists to be
28484 emitted later, so we can't reuse the id for something
28485 else. However, it's good to know whether a view is
28486 known to be zero, because then we may be able to
28487 optimize out locviews that are all zeros, so take
28488 note of it in zero_view_p. */
28489 if (!zero_view_p)
28490 zero_view_p = BITMAP_GGC_ALLOC ();
28491 bitmap_set_bit (zero_view_p, lvugid);
28492 table->view = ++lvugid;
28495 putc ('\n', asm_out_file);
28497 else
28499 unsigned int label_num = ++line_info_label_num;
28501 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28503 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28504 push_dw_line_info_entry (table, LI_adv_address, label_num);
28505 else
28506 push_dw_line_info_entry (table, LI_set_address, label_num);
28507 if (debug_variable_location_views)
28509 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28510 if (resetting)
28511 table->view = 0;
28513 if (flag_debug_asm)
28514 fprintf (asm_out_file, "\t%s view %s%d\n",
28515 ASM_COMMENT_START,
28516 resetting ? "-" : "",
28517 table->view);
28519 table->view++;
28521 if (file_num != table->file_num)
28522 push_dw_line_info_entry (table, LI_set_file, file_num);
28523 if (discriminator != table->discrim_num)
28524 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28525 if (is_stmt != table->is_stmt)
28526 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28527 push_dw_line_info_entry (table, LI_set_line, line);
28528 if (debug_column_info)
28529 push_dw_line_info_entry (table, LI_set_column, column);
28532 table->file_num = file_num;
28533 table->line_num = line;
28534 table->column_num = column;
28535 table->discrim_num = discriminator;
28536 table->is_stmt = is_stmt;
28537 table->in_use = true;
28540 /* Record a source file location for a DECL_IGNORED_P function. */
28542 static void
28543 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28544 const char *filename)
28546 dw_fde_ref fde = cfun->fde;
28548 fde->ignored_debug = false;
28549 set_cur_line_info_table (function_section (fde->decl));
28551 dwarf2out_source_line (line, column, filename, 0, true);
28554 /* Record the beginning of a new source file. */
28556 static void
28557 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28559 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28561 macinfo_entry e;
28562 e.code = DW_MACINFO_start_file;
28563 e.lineno = lineno;
28564 e.info = ggc_strdup (filename);
28565 vec_safe_push (macinfo_table, e);
28569 /* Record the end of a source file. */
28571 static void
28572 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28574 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28576 macinfo_entry e;
28577 e.code = DW_MACINFO_end_file;
28578 e.lineno = lineno;
28579 e.info = NULL;
28580 vec_safe_push (macinfo_table, e);
28584 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28585 the tail part of the directive line, i.e. the part which is past the
28586 initial whitespace, #, whitespace, directive-name, whitespace part. */
28588 static void
28589 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28590 const char *buffer ATTRIBUTE_UNUSED)
28592 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28594 macinfo_entry e;
28595 /* Insert a dummy first entry to be able to optimize the whole
28596 predefined macro block using DW_MACRO_import. */
28597 if (macinfo_table->is_empty () && lineno <= 1)
28599 e.code = 0;
28600 e.lineno = 0;
28601 e.info = NULL;
28602 vec_safe_push (macinfo_table, e);
28604 e.code = DW_MACINFO_define;
28605 e.lineno = lineno;
28606 e.info = ggc_strdup (buffer);
28607 vec_safe_push (macinfo_table, e);
28611 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28612 the tail part of the directive line, i.e. the part which is past the
28613 initial whitespace, #, whitespace, directive-name, whitespace part. */
28615 static void
28616 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28617 const char *buffer ATTRIBUTE_UNUSED)
28619 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28621 macinfo_entry e;
28622 /* Insert a dummy first entry to be able to optimize the whole
28623 predefined macro block using DW_MACRO_import. */
28624 if (macinfo_table->is_empty () && lineno <= 1)
28626 e.code = 0;
28627 e.lineno = 0;
28628 e.info = NULL;
28629 vec_safe_push (macinfo_table, e);
28631 e.code = DW_MACINFO_undef;
28632 e.lineno = lineno;
28633 e.info = ggc_strdup (buffer);
28634 vec_safe_push (macinfo_table, e);
28638 /* Helpers to manipulate hash table of CUs. */
28640 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28642 static inline hashval_t hash (const macinfo_entry *);
28643 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28646 inline hashval_t
28647 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28649 return htab_hash_string (entry->info);
28652 inline bool
28653 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28654 const macinfo_entry *entry2)
28656 return !strcmp (entry1->info, entry2->info);
28659 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28661 /* Output a single .debug_macinfo entry. */
28663 static void
28664 output_macinfo_op (macinfo_entry *ref)
28666 int file_num;
28667 size_t len;
28668 struct indirect_string_node *node;
28669 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28670 struct dwarf_file_data *fd;
28672 switch (ref->code)
28674 case DW_MACINFO_start_file:
28675 fd = lookup_filename (ref->info);
28676 file_num = maybe_emit_file (fd);
28677 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28678 dw2_asm_output_data_uleb128 (ref->lineno,
28679 "Included from line number %lu",
28680 (unsigned long) ref->lineno);
28681 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28682 break;
28683 case DW_MACINFO_end_file:
28684 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28685 break;
28686 case DW_MACINFO_define:
28687 case DW_MACINFO_undef:
28688 len = strlen (ref->info) + 1;
28689 if ((!dwarf_strict || dwarf_version >= 5)
28690 && len > (size_t) dwarf_offset_size
28691 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28692 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28694 if (dwarf_split_debug_info && dwarf_version >= 5)
28695 ref->code = ref->code == DW_MACINFO_define
28696 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28697 else
28698 ref->code = ref->code == DW_MACINFO_define
28699 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28700 output_macinfo_op (ref);
28701 return;
28703 dw2_asm_output_data (1, ref->code,
28704 ref->code == DW_MACINFO_define
28705 ? "Define macro" : "Undefine macro");
28706 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28707 (unsigned long) ref->lineno);
28708 dw2_asm_output_nstring (ref->info, -1, "The macro");
28709 break;
28710 case DW_MACRO_define_strp:
28711 dw2_asm_output_data (1, ref->code, "Define macro strp");
28712 goto do_DW_MACRO_define_strpx;
28713 case DW_MACRO_undef_strp:
28714 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28715 goto do_DW_MACRO_define_strpx;
28716 case DW_MACRO_define_strx:
28717 dw2_asm_output_data (1, ref->code, "Define macro strx");
28718 goto do_DW_MACRO_define_strpx;
28719 case DW_MACRO_undef_strx:
28720 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28721 /* FALLTHRU */
28722 do_DW_MACRO_define_strpx:
28723 /* NB: dwarf2out_finish performs:
28724 1. save_macinfo_strings
28725 2. hash table traverse of index_string
28726 3. output_macinfo -> output_macinfo_op
28727 4. output_indirect_strings
28728 -> hash table traverse of output_index_string
28730 When output_macinfo_op is called, all index strings have been
28731 added to hash table by save_macinfo_strings and we can't pass
28732 INSERT to find_slot_with_hash which may expand hash table, even
28733 if no insertion is needed, and change hash table traverse order
28734 between index_string and output_index_string. */
28735 node = find_AT_string (ref->info, NO_INSERT);
28736 gcc_assert (node
28737 && (node->form == DW_FORM_strp
28738 || node->form == dwarf_FORM (DW_FORM_strx)));
28739 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28740 (unsigned long) ref->lineno);
28741 if (node->form == DW_FORM_strp)
28742 dw2_asm_output_offset (dwarf_offset_size, node->label,
28743 debug_str_section, "The macro: \"%s\"",
28744 ref->info);
28745 else
28746 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28747 ref->info);
28748 break;
28749 case DW_MACRO_import:
28750 dw2_asm_output_data (1, ref->code, "Import");
28751 ASM_GENERATE_INTERNAL_LABEL (label,
28752 DEBUG_MACRO_SECTION_LABEL,
28753 ref->lineno + macinfo_label_base);
28754 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28755 break;
28756 default:
28757 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28758 ASM_COMMENT_START, (unsigned long) ref->code);
28759 break;
28763 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28764 other compilation unit .debug_macinfo sections. IDX is the first
28765 index of a define/undef, return the number of ops that should be
28766 emitted in a comdat .debug_macinfo section and emit
28767 a DW_MACRO_import entry referencing it.
28768 If the define/undef entry should be emitted normally, return 0. */
28770 static unsigned
28771 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28772 macinfo_hash_type **macinfo_htab)
28774 macinfo_entry *first, *second, *cur, *inc;
28775 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28776 unsigned char checksum[16];
28777 struct md5_ctx ctx;
28778 char *grp_name, *tail;
28779 const char *base;
28780 unsigned int i, count, encoded_filename_len, linebuf_len;
28781 macinfo_entry **slot;
28783 first = &(*macinfo_table)[idx];
28784 second = &(*macinfo_table)[idx + 1];
28786 /* Optimize only if there are at least two consecutive define/undef ops,
28787 and either all of them are before first DW_MACINFO_start_file
28788 with lineno {0,1} (i.e. predefined macro block), or all of them are
28789 in some included header file. */
28790 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28791 return 0;
28792 if (vec_safe_is_empty (files))
28794 if (first->lineno > 1 || second->lineno > 1)
28795 return 0;
28797 else if (first->lineno == 0)
28798 return 0;
28800 /* Find the last define/undef entry that can be grouped together
28801 with first and at the same time compute md5 checksum of their
28802 codes, linenumbers and strings. */
28803 md5_init_ctx (&ctx);
28804 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28805 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28806 break;
28807 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28808 break;
28809 else
28811 unsigned char code = cur->code;
28812 md5_process_bytes (&code, 1, &ctx);
28813 checksum_uleb128 (cur->lineno, &ctx);
28814 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28816 md5_finish_ctx (&ctx, checksum);
28817 count = i - idx;
28819 /* From the containing include filename (if any) pick up just
28820 usable characters from its basename. */
28821 if (vec_safe_is_empty (files))
28822 base = "";
28823 else
28824 base = lbasename (files->last ().info);
28825 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28826 if (ISIDNUM (base[i]) || base[i] == '.')
28827 encoded_filename_len++;
28828 /* Count . at the end. */
28829 if (encoded_filename_len)
28830 encoded_filename_len++;
28832 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28833 linebuf_len = strlen (linebuf);
28835 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28836 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28837 + 16 * 2 + 1);
28838 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28839 tail = grp_name + 4;
28840 if (encoded_filename_len)
28842 for (i = 0; base[i]; i++)
28843 if (ISIDNUM (base[i]) || base[i] == '.')
28844 *tail++ = base[i];
28845 *tail++ = '.';
28847 memcpy (tail, linebuf, linebuf_len);
28848 tail += linebuf_len;
28849 *tail++ = '.';
28850 for (i = 0; i < 16; i++)
28851 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28853 /* Construct a macinfo_entry for DW_MACRO_import
28854 in the empty vector entry before the first define/undef. */
28855 inc = &(*macinfo_table)[idx - 1];
28856 inc->code = DW_MACRO_import;
28857 inc->lineno = 0;
28858 inc->info = ggc_strdup (grp_name);
28859 if (!*macinfo_htab)
28860 *macinfo_htab = new macinfo_hash_type (10);
28861 /* Avoid emitting duplicates. */
28862 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28863 if (*slot != NULL)
28865 inc->code = 0;
28866 inc->info = NULL;
28867 /* If such an entry has been used before, just emit
28868 a DW_MACRO_import op. */
28869 inc = *slot;
28870 output_macinfo_op (inc);
28871 /* And clear all macinfo_entry in the range to avoid emitting them
28872 in the second pass. */
28873 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28875 cur->code = 0;
28876 cur->info = NULL;
28879 else
28881 *slot = inc;
28882 inc->lineno = (*macinfo_htab)->elements ();
28883 output_macinfo_op (inc);
28885 return count;
28888 /* Save any strings needed by the macinfo table in the debug str
28889 table. All strings must be collected into the table by the time
28890 index_string is called. */
28892 static void
28893 save_macinfo_strings (void)
28895 unsigned len;
28896 unsigned i;
28897 macinfo_entry *ref;
28899 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28901 switch (ref->code)
28903 /* Match the logic in output_macinfo_op to decide on
28904 indirect strings. */
28905 case DW_MACINFO_define:
28906 case DW_MACINFO_undef:
28907 len = strlen (ref->info) + 1;
28908 if ((!dwarf_strict || dwarf_version >= 5)
28909 && len > (unsigned) dwarf_offset_size
28910 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28911 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28912 set_indirect_string (find_AT_string (ref->info));
28913 break;
28914 case DW_MACINFO_start_file:
28915 /* -gsplit-dwarf -g3 will also output filename as indirect
28916 string. */
28917 if (!dwarf_split_debug_info)
28918 break;
28919 /* Fall through. */
28920 case DW_MACRO_define_strp:
28921 case DW_MACRO_undef_strp:
28922 case DW_MACRO_define_strx:
28923 case DW_MACRO_undef_strx:
28924 set_indirect_string (find_AT_string (ref->info));
28925 break;
28926 default:
28927 break;
28932 /* Output macinfo section(s). */
28934 static void
28935 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28937 unsigned i;
28938 unsigned long length = vec_safe_length (macinfo_table);
28939 macinfo_entry *ref;
28940 vec<macinfo_entry, va_gc> *files = NULL;
28941 macinfo_hash_type *macinfo_htab = NULL;
28942 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28944 if (! length)
28945 return;
28947 /* output_macinfo* uses these interchangeably. */
28948 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28949 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28950 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28951 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28953 /* AIX Assembler inserts the length, so adjust the reference to match the
28954 offset expected by debuggers. */
28955 strcpy (dl_section_ref, debug_line_label);
28956 if (XCOFF_DEBUGGING_INFO)
28957 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28959 /* For .debug_macro emit the section header. */
28960 if (!dwarf_strict || dwarf_version >= 5)
28962 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28963 "DWARF macro version number");
28964 if (dwarf_offset_size == 8)
28965 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28966 else
28967 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28968 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
28969 debug_line_section, NULL);
28972 /* In the first loop, it emits the primary .debug_macinfo section
28973 and after each emitted op the macinfo_entry is cleared.
28974 If a longer range of define/undef ops can be optimized using
28975 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28976 the vector before the first define/undef in the range and the
28977 whole range of define/undef ops is not emitted and kept. */
28978 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28980 switch (ref->code)
28982 case DW_MACINFO_start_file:
28983 vec_safe_push (files, *ref);
28984 break;
28985 case DW_MACINFO_end_file:
28986 if (!vec_safe_is_empty (files))
28987 files->pop ();
28988 break;
28989 case DW_MACINFO_define:
28990 case DW_MACINFO_undef:
28991 if ((!dwarf_strict || dwarf_version >= 5)
28992 && HAVE_COMDAT_GROUP
28993 && vec_safe_length (files) != 1
28994 && i > 0
28995 && i + 1 < length
28996 && (*macinfo_table)[i - 1].code == 0)
28998 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28999 if (count)
29001 i += count - 1;
29002 continue;
29005 break;
29006 case 0:
29007 /* A dummy entry may be inserted at the beginning to be able
29008 to optimize the whole block of predefined macros. */
29009 if (i == 0)
29010 continue;
29011 default:
29012 break;
29014 output_macinfo_op (ref);
29015 ref->info = NULL;
29016 ref->code = 0;
29019 if (!macinfo_htab)
29020 return;
29022 /* Save the number of transparent includes so we can adjust the
29023 label number for the fat LTO object DWARF. */
29024 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29026 delete macinfo_htab;
29027 macinfo_htab = NULL;
29029 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29030 terminate the current chain and switch to a new comdat .debug_macinfo
29031 section and emit the define/undef entries within it. */
29032 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29033 switch (ref->code)
29035 case 0:
29036 continue;
29037 case DW_MACRO_import:
29039 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29040 tree comdat_key = get_identifier (ref->info);
29041 /* Terminate the previous .debug_macinfo section. */
29042 dw2_asm_output_data (1, 0, "End compilation unit");
29043 targetm.asm_out.named_section (debug_macinfo_section_name,
29044 SECTION_DEBUG
29045 | SECTION_LINKONCE
29046 | (early_lto_debug
29047 ? SECTION_EXCLUDE : 0),
29048 comdat_key);
29049 ASM_GENERATE_INTERNAL_LABEL (label,
29050 DEBUG_MACRO_SECTION_LABEL,
29051 ref->lineno + macinfo_label_base);
29052 ASM_OUTPUT_LABEL (asm_out_file, label);
29053 ref->code = 0;
29054 ref->info = NULL;
29055 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29056 "DWARF macro version number");
29057 if (dwarf_offset_size == 8)
29058 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29059 else
29060 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29062 break;
29063 case DW_MACINFO_define:
29064 case DW_MACINFO_undef:
29065 output_macinfo_op (ref);
29066 ref->code = 0;
29067 ref->info = NULL;
29068 break;
29069 default:
29070 gcc_unreachable ();
29073 macinfo_label_base += macinfo_label_base_adj;
29076 /* As init_sections_and_labels may get called multiple times, have a
29077 generation count for labels. */
29078 static unsigned init_sections_and_labels_generation;
29080 /* Initialize the various sections and labels for dwarf output and prefix
29081 them with PREFIX if non-NULL. Returns the generation (zero based
29082 number of times function was called). */
29084 static unsigned
29085 init_sections_and_labels (bool early_lto_debug)
29087 if (early_lto_debug)
29089 if (!dwarf_split_debug_info)
29091 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29092 SECTION_DEBUG | SECTION_EXCLUDE,
29093 NULL);
29094 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29095 SECTION_DEBUG | SECTION_EXCLUDE,
29096 NULL);
29097 debug_macinfo_section_name
29098 = ((dwarf_strict && dwarf_version < 5)
29099 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29100 debug_macinfo_section = get_section (debug_macinfo_section_name,
29101 SECTION_DEBUG
29102 | SECTION_EXCLUDE, NULL);
29104 else
29106 /* ??? Which of the following do we need early? */
29107 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29108 SECTION_DEBUG | SECTION_EXCLUDE,
29109 NULL);
29110 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29111 SECTION_DEBUG | SECTION_EXCLUDE,
29112 NULL);
29113 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29114 SECTION_DEBUG
29115 | SECTION_EXCLUDE, NULL);
29116 debug_skeleton_abbrev_section
29117 = get_section (DEBUG_LTO_ABBREV_SECTION,
29118 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29119 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29120 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29121 init_sections_and_labels_generation);
29123 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29124 stay in the main .o, but the skeleton_line goes into the split
29125 off dwo. */
29126 debug_skeleton_line_section
29127 = get_section (DEBUG_LTO_LINE_SECTION,
29128 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29129 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29130 DEBUG_SKELETON_LINE_SECTION_LABEL,
29131 init_sections_and_labels_generation);
29132 debug_str_offsets_section
29133 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29134 SECTION_DEBUG | SECTION_EXCLUDE,
29135 NULL);
29136 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29137 DEBUG_SKELETON_INFO_SECTION_LABEL,
29138 init_sections_and_labels_generation);
29139 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29140 DEBUG_STR_DWO_SECTION_FLAGS,
29141 NULL);
29142 debug_macinfo_section_name
29143 = ((dwarf_strict && dwarf_version < 5)
29144 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29145 debug_macinfo_section = get_section (debug_macinfo_section_name,
29146 SECTION_DEBUG | SECTION_EXCLUDE,
29147 NULL);
29149 /* For macro info and the file table we have to refer to a
29150 debug_line section. */
29151 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29152 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29153 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29154 DEBUG_LINE_SECTION_LABEL,
29155 init_sections_and_labels_generation);
29157 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29158 DEBUG_STR_SECTION_FLAGS
29159 | SECTION_EXCLUDE, NULL);
29160 if (!dwarf_split_debug_info)
29161 debug_line_str_section
29162 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29163 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29165 else
29167 if (!dwarf_split_debug_info)
29169 debug_info_section = get_section (DEBUG_INFO_SECTION,
29170 SECTION_DEBUG, NULL);
29171 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29172 SECTION_DEBUG, NULL);
29173 debug_loc_section = get_section (dwarf_version >= 5
29174 ? DEBUG_LOCLISTS_SECTION
29175 : DEBUG_LOC_SECTION,
29176 SECTION_DEBUG, NULL);
29177 debug_macinfo_section_name
29178 = ((dwarf_strict && dwarf_version < 5)
29179 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29180 debug_macinfo_section = get_section (debug_macinfo_section_name,
29181 SECTION_DEBUG, NULL);
29183 else
29185 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29186 SECTION_DEBUG | SECTION_EXCLUDE,
29187 NULL);
29188 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29189 SECTION_DEBUG | SECTION_EXCLUDE,
29190 NULL);
29191 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29192 SECTION_DEBUG, NULL);
29193 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29194 SECTION_DEBUG, NULL);
29195 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29196 SECTION_DEBUG, NULL);
29197 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29198 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29199 init_sections_and_labels_generation);
29201 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29202 stay in the main .o, but the skeleton_line goes into the
29203 split off dwo. */
29204 debug_skeleton_line_section
29205 = get_section (DEBUG_DWO_LINE_SECTION,
29206 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29207 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29208 DEBUG_SKELETON_LINE_SECTION_LABEL,
29209 init_sections_and_labels_generation);
29210 debug_str_offsets_section
29211 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29212 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29213 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29214 DEBUG_SKELETON_INFO_SECTION_LABEL,
29215 init_sections_and_labels_generation);
29216 debug_loc_section = get_section (dwarf_version >= 5
29217 ? DEBUG_DWO_LOCLISTS_SECTION
29218 : DEBUG_DWO_LOC_SECTION,
29219 SECTION_DEBUG | SECTION_EXCLUDE,
29220 NULL);
29221 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29222 DEBUG_STR_DWO_SECTION_FLAGS,
29223 NULL);
29224 debug_macinfo_section_name
29225 = ((dwarf_strict && dwarf_version < 5)
29226 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29227 debug_macinfo_section = get_section (debug_macinfo_section_name,
29228 SECTION_DEBUG | SECTION_EXCLUDE,
29229 NULL);
29230 if (dwarf_version >= 5)
29231 debug_ranges_dwo_section
29232 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29233 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29235 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29236 SECTION_DEBUG, NULL);
29237 debug_line_section = get_section (DEBUG_LINE_SECTION,
29238 SECTION_DEBUG, NULL);
29239 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29240 SECTION_DEBUG, NULL);
29241 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29242 SECTION_DEBUG, NULL);
29243 debug_str_section = get_section (DEBUG_STR_SECTION,
29244 DEBUG_STR_SECTION_FLAGS, NULL);
29245 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29246 || asm_outputs_debug_line_str ())
29247 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29248 DEBUG_STR_SECTION_FLAGS, NULL);
29250 debug_ranges_section = get_section (dwarf_version >= 5
29251 ? DEBUG_RNGLISTS_SECTION
29252 : DEBUG_RANGES_SECTION,
29253 SECTION_DEBUG, NULL);
29254 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29255 SECTION_DEBUG, NULL);
29258 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29259 DEBUG_ABBREV_SECTION_LABEL,
29260 init_sections_and_labels_generation);
29261 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29262 DEBUG_INFO_SECTION_LABEL,
29263 init_sections_and_labels_generation);
29264 info_section_emitted = false;
29265 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29266 DEBUG_LINE_SECTION_LABEL,
29267 init_sections_and_labels_generation);
29268 /* There are up to 6 unique ranges labels per generation.
29269 See also output_rnglists. */
29270 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29271 DEBUG_RANGES_SECTION_LABEL,
29272 init_sections_and_labels_generation * 6);
29273 if (dwarf_version >= 5 && dwarf_split_debug_info)
29274 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29275 DEBUG_RANGES_SECTION_LABEL,
29276 1 + init_sections_and_labels_generation * 6);
29277 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29278 DEBUG_ADDR_SECTION_LABEL,
29279 init_sections_and_labels_generation);
29280 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29281 (dwarf_strict && dwarf_version < 5)
29282 ? DEBUG_MACINFO_SECTION_LABEL
29283 : DEBUG_MACRO_SECTION_LABEL,
29284 init_sections_and_labels_generation);
29285 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29286 init_sections_and_labels_generation);
29288 ++init_sections_and_labels_generation;
29289 return init_sections_and_labels_generation - 1;
29292 /* Set up for Dwarf output at the start of compilation. */
29294 static void
29295 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29297 /* Allocate the file_table. */
29298 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29300 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29301 /* Allocate the decl_die_table. */
29302 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29304 /* Allocate the decl_loc_table. */
29305 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29307 /* Allocate the cached_dw_loc_list_table. */
29308 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29310 /* Allocate the initial hunk of the abbrev_die_table. */
29311 vec_alloc (abbrev_die_table, 256);
29312 /* Zero-th entry is allocated, but unused. */
29313 abbrev_die_table->quick_push (NULL);
29315 /* Allocate the dwarf_proc_stack_usage_map. */
29316 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29318 /* Allocate the pubtypes and pubnames vectors. */
29319 vec_alloc (pubname_table, 32);
29320 vec_alloc (pubtype_table, 32);
29322 vec_alloc (incomplete_types, 64);
29324 vec_alloc (used_rtx_array, 32);
29326 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29327 vec_alloc (macinfo_table, 64);
29328 #endif
29330 /* If front-ends already registered a main translation unit but we were not
29331 ready to perform the association, do this now. */
29332 if (main_translation_unit != NULL_TREE)
29333 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29336 /* Called before compile () starts outputtting functions, variables
29337 and toplevel asms into assembly. */
29339 static void
29340 dwarf2out_assembly_start (void)
29342 if (text_section_line_info)
29343 return;
29345 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29346 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29347 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29348 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29349 COLD_TEXT_SECTION_LABEL, 0);
29350 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29352 switch_to_section (text_section);
29353 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29354 #endif
29356 /* Make sure the line number table for .text always exists. */
29357 text_section_line_info = new_line_info_table ();
29358 text_section_line_info->end_label = text_end_label;
29360 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29361 cur_line_info_table = text_section_line_info;
29362 #endif
29364 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29365 && dwarf2out_do_cfi_asm ()
29366 && !dwarf2out_do_eh_frame ())
29367 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29369 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29370 if (output_asm_line_debug_info () && dwarf_version >= 5)
29372 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29373 tell it the comp_dir and main file name for the zero entry
29374 line table. */
29375 const char *comp_dir, *filename0;
29377 comp_dir = comp_dir_string ();
29378 if (comp_dir == NULL)
29379 comp_dir = "";
29381 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29382 if (filename0 == NULL)
29383 filename0 = "";
29385 fprintf (asm_out_file, "\t.file 0 ");
29386 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29387 fputc (' ', asm_out_file);
29388 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29389 fputc ('\n', asm_out_file);
29391 else
29392 #endif
29393 /* Work around for PR101575: output a dummy .file directive. */
29394 if (!last_emitted_file && dwarf_debuginfo_p ()
29395 && debug_info_level >= DINFO_LEVEL_TERSE)
29397 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29399 if (filename0 == NULL)
29400 filename0 = "<dummy>";
29401 maybe_emit_file (lookup_filename (filename0));
29405 /* A helper function for dwarf2out_finish called through
29406 htab_traverse. Assign a string its index. All strings must be
29407 collected into the table by the time index_string is called,
29408 because the indexing code relies on htab_traverse to traverse nodes
29409 in the same order for each run. */
29412 index_string (indirect_string_node **h, unsigned int *index)
29414 indirect_string_node *node = *h;
29416 find_string_form (node);
29417 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29419 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29420 node->index = *index;
29421 *index += 1;
29423 return 1;
29426 /* A helper function for output_indirect_strings called through
29427 htab_traverse. Output the offset to a string and update the
29428 current offset. */
29431 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29433 indirect_string_node *node = *h;
29435 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29437 /* Assert that this node has been assigned an index. */
29438 gcc_assert (node->index != NO_INDEX_ASSIGNED
29439 && node->index != NOT_INDEXED);
29440 dw2_asm_output_data (dwarf_offset_size, *offset,
29441 "indexed string 0x%x: %s", node->index, node->str);
29442 *offset += strlen (node->str) + 1;
29444 return 1;
29447 /* A helper function for dwarf2out_finish called through
29448 htab_traverse. Output the indexed string. */
29451 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29453 struct indirect_string_node *node = *h;
29455 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29457 /* Assert that the strings are output in the same order as their
29458 indexes were assigned. */
29459 gcc_assert (*cur_idx == node->index);
29460 assemble_string (node->str, strlen (node->str) + 1);
29461 *cur_idx += 1;
29463 return 1;
29466 /* A helper function for output_indirect_strings. Counts the number
29467 of index strings offsets. Must match the logic of the functions
29468 output_index_string[_offsets] above. */
29470 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29472 struct indirect_string_node *node = *h;
29474 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29475 *last_idx += 1;
29476 return 1;
29479 /* A helper function for dwarf2out_finish called through
29480 htab_traverse. Emit one queued .debug_str string. */
29483 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29485 struct indirect_string_node *node = *h;
29487 node->form = find_string_form (node);
29488 if (node->form == form && node->refcount > 0)
29490 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29491 assemble_string (node->str, strlen (node->str) + 1);
29494 return 1;
29497 /* Output the indexed string table. */
29499 static void
29500 output_indirect_strings (void)
29502 switch_to_section (debug_str_section);
29503 if (!dwarf_split_debug_info)
29504 debug_str_hash->traverse<enum dwarf_form,
29505 output_indirect_string> (DW_FORM_strp);
29506 else
29508 unsigned int offset = 0;
29509 unsigned int cur_idx = 0;
29511 if (skeleton_debug_str_hash)
29512 skeleton_debug_str_hash->traverse<enum dwarf_form,
29513 output_indirect_string> (DW_FORM_strp);
29515 switch_to_section (debug_str_offsets_section);
29516 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29517 header. Note that we don't need to generate a label to the
29518 actual index table following the header here, because this is
29519 for the split dwarf case only. In an .dwo file there is only
29520 one string offsets table (and one debug info section). But
29521 if we would start using string offset tables for the main (or
29522 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29523 pointing to the actual index after the header. Split dwarf
29524 units will never have a string offsets base attribute. When
29525 a split unit is moved into a .dwp file the string offsets can
29526 be found through the .debug_cu_index section table. */
29527 if (dwarf_version >= 5)
29529 unsigned int last_idx = 0;
29530 unsigned long str_offsets_length;
29532 debug_str_hash->traverse_noresize
29533 <unsigned int *, count_index_strings> (&last_idx);
29534 str_offsets_length = last_idx * dwarf_offset_size + 4;
29535 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29536 dw2_asm_output_data (4, 0xffffffff,
29537 "Escape value for 64-bit DWARF extension");
29538 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29539 "Length of string offsets unit");
29540 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29541 dw2_asm_output_data (2, 0, "Header zero padding");
29543 debug_str_hash->traverse_noresize
29544 <unsigned int *, output_index_string_offset> (&offset);
29545 switch_to_section (debug_str_dwo_section);
29546 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29547 (&cur_idx);
29551 /* Callback for htab_traverse to assign an index to an entry in the
29552 table, and to write that entry to the .debug_addr section. */
29555 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29557 addr_table_entry *entry = *slot;
29559 if (entry->refcount == 0)
29561 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29562 || entry->index == NOT_INDEXED);
29563 return 1;
29566 gcc_assert (entry->index == *cur_index);
29567 (*cur_index)++;
29569 switch (entry->kind)
29571 case ate_kind_rtx:
29572 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29573 "0x%x", entry->index);
29574 break;
29575 case ate_kind_rtx_dtprel:
29576 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29577 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29578 DWARF2_ADDR_SIZE,
29579 entry->addr.rtl);
29580 fputc ('\n', asm_out_file);
29581 break;
29582 case ate_kind_label:
29583 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29584 "0x%x", entry->index);
29585 break;
29586 default:
29587 gcc_unreachable ();
29589 return 1;
29592 /* A helper function for dwarf2out_finish. Counts the number
29593 of indexed addresses. Must match the logic of the functions
29594 output_addr_table_entry above. */
29596 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29598 addr_table_entry *entry = *slot;
29600 if (entry->refcount > 0)
29601 *last_idx += 1;
29602 return 1;
29605 /* Produce the .debug_addr section. */
29607 static void
29608 output_addr_table (void)
29610 unsigned int index = 0;
29611 if (addr_index_table == NULL || addr_index_table->size () == 0)
29612 return;
29614 switch_to_section (debug_addr_section);
29615 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29616 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29617 before DWARF5, didn't have a header for .debug_addr units.
29618 DWARF5 specifies a small header when address tables are used. */
29619 if (dwarf_version >= 5)
29621 unsigned int last_idx = 0;
29622 unsigned long addrs_length;
29624 addr_index_table->traverse_noresize
29625 <unsigned int *, count_index_addrs> (&last_idx);
29626 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29628 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29629 dw2_asm_output_data (4, 0xffffffff,
29630 "Escape value for 64-bit DWARF extension");
29631 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29632 "Length of Address Unit");
29633 dw2_asm_output_data (2, 5, "DWARF addr version");
29634 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29635 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29637 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29639 addr_index_table
29640 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29643 #if ENABLE_ASSERT_CHECKING
29644 /* Verify that all marks are clear. */
29646 static void
29647 verify_marks_clear (dw_die_ref die)
29649 dw_die_ref c;
29651 gcc_assert (! die->die_mark);
29652 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29654 #endif /* ENABLE_ASSERT_CHECKING */
29656 /* Clear the marks for a die and its children.
29657 Be cool if the mark isn't set. */
29659 static void
29660 prune_unmark_dies (dw_die_ref die)
29662 dw_die_ref c;
29664 if (die->die_mark)
29665 die->die_mark = 0;
29666 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29669 /* Given LOC that is referenced by a DIE we're marking as used, find all
29670 referenced DWARF procedures it references and mark them as used. */
29672 static void
29673 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29675 for (; loc != NULL; loc = loc->dw_loc_next)
29676 switch (loc->dw_loc_opc)
29678 case DW_OP_implicit_pointer:
29679 case DW_OP_convert:
29680 case DW_OP_reinterpret:
29681 case DW_OP_GNU_implicit_pointer:
29682 case DW_OP_GNU_convert:
29683 case DW_OP_GNU_reinterpret:
29684 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29685 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29686 break;
29687 case DW_OP_GNU_variable_value:
29688 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29690 dw_die_ref ref
29691 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29692 if (ref == NULL)
29693 break;
29694 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29695 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29696 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29698 /* FALLTHRU */
29699 case DW_OP_call2:
29700 case DW_OP_call4:
29701 case DW_OP_call_ref:
29702 case DW_OP_const_type:
29703 case DW_OP_GNU_const_type:
29704 case DW_OP_GNU_parameter_ref:
29705 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29706 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29707 break;
29708 case DW_OP_regval_type:
29709 case DW_OP_deref_type:
29710 case DW_OP_GNU_regval_type:
29711 case DW_OP_GNU_deref_type:
29712 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29713 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29714 break;
29715 case DW_OP_entry_value:
29716 case DW_OP_GNU_entry_value:
29717 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29718 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29719 break;
29720 default:
29721 break;
29725 /* Given DIE that we're marking as used, find any other dies
29726 it references as attributes and mark them as used. */
29728 static void
29729 prune_unused_types_walk_attribs (dw_die_ref die)
29731 dw_attr_node *a;
29732 unsigned ix;
29734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29736 switch (AT_class (a))
29738 /* Make sure DWARF procedures referenced by location descriptions will
29739 get emitted. */
29740 case dw_val_class_loc:
29741 prune_unused_types_walk_loc_descr (AT_loc (a));
29742 break;
29743 case dw_val_class_loc_list:
29744 for (dw_loc_list_ref list = AT_loc_list (a);
29745 list != NULL;
29746 list = list->dw_loc_next)
29747 prune_unused_types_walk_loc_descr (list->expr);
29748 break;
29750 case dw_val_class_view_list:
29751 /* This points to a loc_list in another attribute, so it's
29752 already covered. */
29753 break;
29755 case dw_val_class_die_ref:
29756 /* A reference to another DIE.
29757 Make sure that it will get emitted.
29758 If it was broken out into a comdat group, don't follow it. */
29759 if (! AT_ref (a)->comdat_type_p
29760 || a->dw_attr == DW_AT_specification)
29761 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29762 break;
29764 case dw_val_class_str:
29765 /* Set the string's refcount to 0 so that prune_unused_types_mark
29766 accounts properly for it. */
29767 a->dw_attr_val.v.val_str->refcount = 0;
29768 break;
29770 default:
29771 break;
29776 /* Mark the generic parameters and arguments children DIEs of DIE. */
29778 static void
29779 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29781 dw_die_ref c;
29783 if (die == NULL || die->die_child == NULL)
29784 return;
29785 c = die->die_child;
29788 if (is_template_parameter (c))
29789 prune_unused_types_mark (c, 1);
29790 c = c->die_sib;
29791 } while (c && c != die->die_child);
29794 /* Mark DIE as being used. If DOKIDS is true, then walk down
29795 to DIE's children. */
29797 static void
29798 prune_unused_types_mark (dw_die_ref die, int dokids)
29800 dw_die_ref c;
29802 if (die->die_mark == 0)
29804 /* We haven't done this node yet. Mark it as used. */
29805 die->die_mark = 1;
29806 /* If this is the DIE of a generic type instantiation,
29807 mark the children DIEs that describe its generic parms and
29808 args. */
29809 prune_unused_types_mark_generic_parms_dies (die);
29811 /* We also have to mark its parents as used.
29812 (But we don't want to mark our parent's kids due to this,
29813 unless it is a class.) */
29814 if (die->die_parent)
29815 prune_unused_types_mark (die->die_parent,
29816 class_scope_p (die->die_parent));
29818 /* Mark any referenced nodes. */
29819 prune_unused_types_walk_attribs (die);
29821 /* If this node is a specification,
29822 also mark the definition, if it exists. */
29823 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29824 prune_unused_types_mark (die->die_definition, 1);
29827 if (dokids && die->die_mark != 2)
29829 /* We need to walk the children, but haven't done so yet.
29830 Remember that we've walked the kids. */
29831 die->die_mark = 2;
29833 /* If this is an array type, we need to make sure our
29834 kids get marked, even if they're types. If we're
29835 breaking out types into comdat sections, do this
29836 for all type definitions. */
29837 if (die->die_tag == DW_TAG_array_type
29838 || (use_debug_types
29839 && is_type_die (die) && ! is_declaration_die (die)))
29840 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29841 else
29842 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29846 /* For local classes, look if any static member functions were emitted
29847 and if so, mark them. */
29849 static void
29850 prune_unused_types_walk_local_classes (dw_die_ref die)
29852 dw_die_ref c;
29854 if (die->die_mark == 2)
29855 return;
29857 switch (die->die_tag)
29859 case DW_TAG_structure_type:
29860 case DW_TAG_union_type:
29861 case DW_TAG_class_type:
29862 case DW_TAG_interface_type:
29863 break;
29865 case DW_TAG_subprogram:
29866 if (!get_AT_flag (die, DW_AT_declaration)
29867 || die->die_definition != NULL)
29868 prune_unused_types_mark (die, 1);
29869 return;
29871 default:
29872 return;
29875 /* Mark children. */
29876 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29879 /* Walk the tree DIE and mark types that we actually use. */
29881 static void
29882 prune_unused_types_walk (dw_die_ref die)
29884 dw_die_ref c;
29886 /* Don't do anything if this node is already marked and
29887 children have been marked as well. */
29888 if (die->die_mark == 2)
29889 return;
29891 switch (die->die_tag)
29893 case DW_TAG_structure_type:
29894 case DW_TAG_union_type:
29895 case DW_TAG_class_type:
29896 case DW_TAG_interface_type:
29897 if (die->die_perennial_p)
29898 break;
29900 for (c = die->die_parent; c; c = c->die_parent)
29901 if (c->die_tag == DW_TAG_subprogram)
29902 break;
29904 /* Finding used static member functions inside of classes
29905 is needed just for local classes, because for other classes
29906 static member function DIEs with DW_AT_specification
29907 are emitted outside of the DW_TAG_*_type. If we ever change
29908 it, we'd need to call this even for non-local classes. */
29909 if (c)
29910 prune_unused_types_walk_local_classes (die);
29912 /* It's a type node --- don't mark it. */
29913 return;
29915 case DW_TAG_const_type:
29916 case DW_TAG_packed_type:
29917 case DW_TAG_pointer_type:
29918 case DW_TAG_reference_type:
29919 case DW_TAG_rvalue_reference_type:
29920 case DW_TAG_volatile_type:
29921 case DW_TAG_typedef:
29922 case DW_TAG_array_type:
29923 case DW_TAG_friend:
29924 case DW_TAG_enumeration_type:
29925 case DW_TAG_subroutine_type:
29926 case DW_TAG_string_type:
29927 case DW_TAG_set_type:
29928 case DW_TAG_subrange_type:
29929 case DW_TAG_ptr_to_member_type:
29930 case DW_TAG_file_type:
29931 /* Type nodes are useful only when other DIEs reference them --- don't
29932 mark them. */
29933 /* FALLTHROUGH */
29935 case DW_TAG_dwarf_procedure:
29936 /* Likewise for DWARF procedures. */
29938 if (die->die_perennial_p)
29939 break;
29941 return;
29943 case DW_TAG_variable:
29944 if (flag_debug_only_used_symbols)
29946 if (die->die_perennial_p)
29947 break;
29949 /* For static data members, the declaration in the class is supposed
29950 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29951 DWARF5. DW_TAG_member will be marked, so mark even such
29952 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29953 attribute. */
29954 if (dwarf_version >= 5
29955 && class_scope_p (die->die_parent)
29956 && get_AT (die, DW_AT_const_value))
29957 break;
29959 /* premark_used_variables marks external variables --- don't mark
29960 them here. But function-local externals are always considered
29961 used. */
29962 if (get_AT (die, DW_AT_external))
29964 for (c = die->die_parent; c; c = c->die_parent)
29965 if (c->die_tag == DW_TAG_subprogram)
29966 break;
29967 if (!c)
29968 return;
29971 /* FALLTHROUGH */
29973 default:
29974 /* Mark everything else. */
29975 break;
29978 if (die->die_mark == 0)
29980 die->die_mark = 1;
29982 /* Now, mark any dies referenced from here. */
29983 prune_unused_types_walk_attribs (die);
29986 die->die_mark = 2;
29988 /* Mark children. */
29989 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29992 /* Increment the string counts on strings referred to from DIE's
29993 attributes. */
29995 static void
29996 prune_unused_types_update_strings (dw_die_ref die)
29998 dw_attr_node *a;
29999 unsigned ix;
30001 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30002 if (AT_class (a) == dw_val_class_str)
30004 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30005 s->refcount++;
30006 /* Avoid unnecessarily putting strings that are used less than
30007 twice in the hash table. */
30008 if (s->form != DW_FORM_line_strp
30009 && (s->refcount
30010 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30012 indirect_string_node **slot
30013 = debug_str_hash->find_slot_with_hash (s->str,
30014 htab_hash_string (s->str),
30015 INSERT);
30016 gcc_assert (*slot == NULL);
30017 *slot = s;
30022 /* Mark DIE and its children as removed. */
30024 static void
30025 mark_removed (dw_die_ref die)
30027 dw_die_ref c;
30028 die->removed = true;
30029 FOR_EACH_CHILD (die, c, mark_removed (c));
30032 /* Remove from the tree DIE any dies that aren't marked. */
30034 static void
30035 prune_unused_types_prune (dw_die_ref die)
30037 dw_die_ref c;
30039 gcc_assert (die->die_mark);
30040 prune_unused_types_update_strings (die);
30042 if (! die->die_child)
30043 return;
30045 c = die->die_child;
30046 do {
30047 dw_die_ref prev = c, next;
30048 for (c = c->die_sib; ! c->die_mark; c = next)
30049 if (c == die->die_child)
30051 /* No marked children between 'prev' and the end of the list. */
30052 if (prev == c)
30053 /* No marked children at all. */
30054 die->die_child = NULL;
30055 else
30057 prev->die_sib = c->die_sib;
30058 die->die_child = prev;
30060 c->die_sib = NULL;
30061 mark_removed (c);
30062 return;
30064 else
30066 next = c->die_sib;
30067 c->die_sib = NULL;
30068 mark_removed (c);
30071 if (c != prev->die_sib)
30072 prev->die_sib = c;
30073 prune_unused_types_prune (c);
30074 } while (c != die->die_child);
30077 /* Remove dies representing declarations that we never use. */
30079 static void
30080 prune_unused_types (void)
30082 unsigned int i;
30083 limbo_die_node *node;
30084 comdat_type_node *ctnode;
30085 pubname_entry *pub;
30086 dw_die_ref base_type;
30088 #if ENABLE_ASSERT_CHECKING
30089 /* All the marks should already be clear. */
30090 verify_marks_clear (comp_unit_die ());
30091 for (node = limbo_die_list; node; node = node->next)
30092 verify_marks_clear (node->die);
30093 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30094 verify_marks_clear (ctnode->root_die);
30095 #endif /* ENABLE_ASSERT_CHECKING */
30097 /* Mark types that are used in global variables. */
30098 premark_types_used_by_global_vars ();
30100 /* Mark variables used in the symtab. */
30101 if (flag_debug_only_used_symbols)
30102 premark_used_variables ();
30104 /* Set the mark on nodes that are actually used. */
30105 prune_unused_types_walk (comp_unit_die ());
30106 for (node = limbo_die_list; node; node = node->next)
30107 prune_unused_types_walk (node->die);
30108 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30110 prune_unused_types_walk (ctnode->root_die);
30111 prune_unused_types_mark (ctnode->type_die, 1);
30114 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30115 are unusual in that they are pubnames that are the children of pubtypes.
30116 They should only be marked via their parent DW_TAG_enumeration_type die,
30117 not as roots in themselves. */
30118 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30119 if (pub->die->die_tag != DW_TAG_enumerator)
30120 prune_unused_types_mark (pub->die, 1);
30121 for (i = 0; base_types.iterate (i, &base_type); i++)
30122 prune_unused_types_mark (base_type, 1);
30124 /* Also set the mark on nodes that could be referenced by
30125 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30126 by DW_TAG_inlined_subroutine origins. */
30127 cgraph_node *cnode;
30128 FOR_EACH_FUNCTION (cnode)
30129 if (cnode->referred_to_p (false))
30131 dw_die_ref die = lookup_decl_die (cnode->decl);
30132 if (die == NULL || die->die_mark)
30133 continue;
30134 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30135 if (e->caller != cnode)
30137 prune_unused_types_mark (die, 1);
30138 break;
30142 if (debug_str_hash)
30143 debug_str_hash->empty ();
30144 if (skeleton_debug_str_hash)
30145 skeleton_debug_str_hash->empty ();
30146 prune_unused_types_prune (comp_unit_die ());
30147 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30149 node = *pnode;
30150 if (!node->die->die_mark)
30151 *pnode = node->next;
30152 else
30154 prune_unused_types_prune (node->die);
30155 pnode = &node->next;
30158 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30159 prune_unused_types_prune (ctnode->root_die);
30161 /* Leave the marks clear. */
30162 prune_unmark_dies (comp_unit_die ());
30163 for (node = limbo_die_list; node; node = node->next)
30164 prune_unmark_dies (node->die);
30165 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30166 prune_unmark_dies (ctnode->root_die);
30169 /* Helpers to manipulate hash table of comdat type units. */
30171 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30173 static inline hashval_t hash (const comdat_type_node *);
30174 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30177 inline hashval_t
30178 comdat_type_hasher::hash (const comdat_type_node *type_node)
30180 hashval_t h;
30181 memcpy (&h, type_node->signature, sizeof (h));
30182 return h;
30185 inline bool
30186 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30187 const comdat_type_node *type_node_2)
30189 return (! memcmp (type_node_1->signature, type_node_2->signature,
30190 DWARF_TYPE_SIGNATURE_SIZE));
30193 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30194 to the location it would have been added, should we know its
30195 DECL_ASSEMBLER_NAME when we added other attributes. This will
30196 probably improve compactness of debug info, removing equivalent
30197 abbrevs, and hide any differences caused by deferring the
30198 computation of the assembler name, triggered by e.g. PCH. */
30200 static inline void
30201 move_linkage_attr (dw_die_ref die)
30203 unsigned ix = vec_safe_length (die->die_attr);
30204 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30206 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30207 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30209 while (--ix > 0)
30211 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30213 if (prev->dw_attr == DW_AT_decl_line
30214 || prev->dw_attr == DW_AT_decl_column
30215 || prev->dw_attr == DW_AT_name)
30216 break;
30219 if (ix != vec_safe_length (die->die_attr) - 1)
30221 die->die_attr->pop ();
30222 die->die_attr->quick_insert (ix, linkage);
30226 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30227 referenced from typed stack ops and count how often they are used. */
30229 static void
30230 mark_base_types (dw_loc_descr_ref loc)
30232 dw_die_ref base_type = NULL;
30234 for (; loc; loc = loc->dw_loc_next)
30236 switch (loc->dw_loc_opc)
30238 case DW_OP_regval_type:
30239 case DW_OP_deref_type:
30240 case DW_OP_GNU_regval_type:
30241 case DW_OP_GNU_deref_type:
30242 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30243 break;
30244 case DW_OP_convert:
30245 case DW_OP_reinterpret:
30246 case DW_OP_GNU_convert:
30247 case DW_OP_GNU_reinterpret:
30248 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30249 continue;
30250 /* FALLTHRU */
30251 case DW_OP_const_type:
30252 case DW_OP_GNU_const_type:
30253 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30254 break;
30255 case DW_OP_entry_value:
30256 case DW_OP_GNU_entry_value:
30257 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30258 continue;
30259 default:
30260 continue;
30262 gcc_assert (base_type->die_parent == comp_unit_die ());
30263 if (base_type->die_mark)
30264 base_type->die_mark++;
30265 else
30267 base_types.safe_push (base_type);
30268 base_type->die_mark = 1;
30273 /* Comparison function for sorting marked base types. */
30275 static int
30276 base_type_cmp (const void *x, const void *y)
30278 dw_die_ref dx = *(const dw_die_ref *) x;
30279 dw_die_ref dy = *(const dw_die_ref *) y;
30280 unsigned int byte_size1, byte_size2;
30281 unsigned int encoding1, encoding2;
30282 unsigned int align1, align2;
30283 if (dx->die_mark > dy->die_mark)
30284 return -1;
30285 if (dx->die_mark < dy->die_mark)
30286 return 1;
30287 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30288 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30289 if (byte_size1 < byte_size2)
30290 return 1;
30291 if (byte_size1 > byte_size2)
30292 return -1;
30293 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30294 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30295 if (encoding1 < encoding2)
30296 return 1;
30297 if (encoding1 > encoding2)
30298 return -1;
30299 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30300 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30301 if (align1 < align2)
30302 return 1;
30303 if (align1 > align2)
30304 return -1;
30305 return 0;
30308 /* Move base types marked by mark_base_types as early as possible
30309 in the CU, sorted by decreasing usage count both to make the
30310 uleb128 references as small as possible and to make sure they
30311 will have die_offset already computed by calc_die_sizes when
30312 sizes of typed stack loc ops is computed. */
30314 static void
30315 move_marked_base_types (void)
30317 unsigned int i;
30318 dw_die_ref base_type, die, c;
30320 if (base_types.is_empty ())
30321 return;
30323 /* Sort by decreasing usage count, they will be added again in that
30324 order later on. */
30325 base_types.qsort (base_type_cmp);
30326 die = comp_unit_die ();
30327 c = die->die_child;
30330 dw_die_ref prev = c;
30331 c = c->die_sib;
30332 while (c->die_mark)
30334 remove_child_with_prev (c, prev);
30335 /* As base types got marked, there must be at least
30336 one node other than DW_TAG_base_type. */
30337 gcc_assert (die->die_child != NULL);
30338 c = prev->die_sib;
30341 while (c != die->die_child);
30342 gcc_assert (die->die_child);
30343 c = die->die_child;
30344 for (i = 0; base_types.iterate (i, &base_type); i++)
30346 base_type->die_mark = 0;
30347 base_type->die_sib = c->die_sib;
30348 c->die_sib = base_type;
30349 c = base_type;
30353 /* Helper function for resolve_addr, attempt to resolve
30354 one CONST_STRING, return true if successful. Similarly verify that
30355 SYMBOL_REFs refer to variables emitted in the current CU. */
30357 static bool
30358 resolve_one_addr (rtx *addr)
30360 rtx rtl = *addr;
30362 if (GET_CODE (rtl) == CONST_STRING)
30364 size_t len = strlen (XSTR (rtl, 0)) + 1;
30365 tree t = build_string (len, XSTR (rtl, 0));
30366 tree tlen = size_int (len - 1);
30367 TREE_TYPE (t)
30368 = build_array_type (char_type_node, build_index_type (tlen));
30369 rtl = lookup_constant_def (t);
30370 if (!rtl || !MEM_P (rtl))
30371 return false;
30372 rtl = XEXP (rtl, 0);
30373 if (GET_CODE (rtl) == SYMBOL_REF
30374 && SYMBOL_REF_DECL (rtl)
30375 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30376 return false;
30377 vec_safe_push (used_rtx_array, rtl);
30378 *addr = rtl;
30379 return true;
30382 if (GET_CODE (rtl) == SYMBOL_REF
30383 && SYMBOL_REF_DECL (rtl))
30385 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30387 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30388 return false;
30390 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30391 return false;
30394 if (GET_CODE (rtl) == CONST)
30396 subrtx_ptr_iterator::array_type array;
30397 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30398 if (!resolve_one_addr (*iter))
30399 return false;
30402 return true;
30405 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30406 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30407 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30409 static rtx
30410 string_cst_pool_decl (tree t)
30412 rtx rtl = output_constant_def (t, 1);
30413 unsigned char *array;
30414 dw_loc_descr_ref l;
30415 tree decl;
30416 size_t len;
30417 dw_die_ref ref;
30419 if (!rtl || !MEM_P (rtl))
30420 return NULL_RTX;
30421 rtl = XEXP (rtl, 0);
30422 if (GET_CODE (rtl) != SYMBOL_REF
30423 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30424 return NULL_RTX;
30426 decl = SYMBOL_REF_DECL (rtl);
30427 if (!lookup_decl_die (decl))
30429 len = TREE_STRING_LENGTH (t);
30430 vec_safe_push (used_rtx_array, rtl);
30431 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30432 array = ggc_vec_alloc<unsigned char> (len);
30433 memcpy (array, TREE_STRING_POINTER (t), len);
30434 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30435 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30436 l->dw_loc_oprnd2.v.val_vec.length = len;
30437 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30438 l->dw_loc_oprnd2.v.val_vec.array = array;
30439 add_AT_loc (ref, DW_AT_location, l);
30440 equate_decl_number_to_die (decl, ref);
30442 return rtl;
30445 /* Helper function of resolve_addr_in_expr. LOC is
30446 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30447 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30448 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30449 with DW_OP_implicit_pointer if possible
30450 and return true, if unsuccessful, return false. */
30452 static bool
30453 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30455 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30456 HOST_WIDE_INT offset = 0;
30457 dw_die_ref ref = NULL;
30458 tree decl;
30460 if (GET_CODE (rtl) == CONST
30461 && GET_CODE (XEXP (rtl, 0)) == PLUS
30462 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30464 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30465 rtl = XEXP (XEXP (rtl, 0), 0);
30467 if (GET_CODE (rtl) == CONST_STRING)
30469 size_t len = strlen (XSTR (rtl, 0)) + 1;
30470 tree t = build_string (len, XSTR (rtl, 0));
30471 tree tlen = size_int (len - 1);
30473 TREE_TYPE (t)
30474 = build_array_type (char_type_node, build_index_type (tlen));
30475 rtl = string_cst_pool_decl (t);
30476 if (!rtl)
30477 return false;
30479 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30481 decl = SYMBOL_REF_DECL (rtl);
30482 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30484 ref = lookup_decl_die (decl);
30485 if (ref && (get_AT (ref, DW_AT_location)
30486 || get_AT (ref, DW_AT_const_value)))
30488 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30489 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30490 loc->dw_loc_oprnd1.val_entry = NULL;
30491 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30492 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30493 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30494 loc->dw_loc_oprnd2.v.val_int = offset;
30495 return true;
30499 return false;
30502 /* Helper function for resolve_addr, handle one location
30503 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30504 the location list couldn't be resolved. */
30506 static bool
30507 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30509 dw_loc_descr_ref keep = NULL;
30510 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30511 switch (loc->dw_loc_opc)
30513 case DW_OP_addr:
30514 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30516 if ((prev == NULL
30517 || prev->dw_loc_opc == DW_OP_piece
30518 || prev->dw_loc_opc == DW_OP_bit_piece)
30519 && loc->dw_loc_next
30520 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30521 && (!dwarf_strict || dwarf_version >= 5)
30522 && optimize_one_addr_into_implicit_ptr (loc))
30523 break;
30524 return false;
30526 break;
30527 case DW_OP_GNU_addr_index:
30528 case DW_OP_addrx:
30529 case DW_OP_GNU_const_index:
30530 case DW_OP_constx:
30531 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30532 || loc->dw_loc_opc == DW_OP_addrx)
30533 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30534 || loc->dw_loc_opc == DW_OP_constx)
30535 && loc->dtprel))
30537 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30538 if (!resolve_one_addr (&rtl))
30539 return false;
30540 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30541 loc->dw_loc_oprnd1.val_entry
30542 = add_addr_table_entry (rtl, ate_kind_rtx);
30544 break;
30545 case DW_OP_const4u:
30546 case DW_OP_const8u:
30547 if (loc->dtprel
30548 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30549 return false;
30550 break;
30551 case DW_OP_plus_uconst:
30552 if (size_of_loc_descr (loc)
30553 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30555 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30557 dw_loc_descr_ref repl
30558 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30559 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30560 add_loc_descr (&repl, loc->dw_loc_next);
30561 *loc = *repl;
30563 break;
30564 case DW_OP_implicit_value:
30565 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30566 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30567 return false;
30568 break;
30569 case DW_OP_implicit_pointer:
30570 case DW_OP_GNU_implicit_pointer:
30571 case DW_OP_GNU_parameter_ref:
30572 case DW_OP_GNU_variable_value:
30573 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30575 dw_die_ref ref
30576 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30577 if (ref == NULL)
30578 return false;
30579 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30580 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30581 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30583 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30585 if (prev == NULL
30586 && loc->dw_loc_next == NULL
30587 && AT_class (a) == dw_val_class_loc)
30588 switch (a->dw_attr)
30590 /* Following attributes allow both exprloc and reference,
30591 so if the whole expression is DW_OP_GNU_variable_value
30592 alone we could transform it into reference. */
30593 case DW_AT_byte_size:
30594 case DW_AT_bit_size:
30595 case DW_AT_lower_bound:
30596 case DW_AT_upper_bound:
30597 case DW_AT_bit_stride:
30598 case DW_AT_count:
30599 case DW_AT_allocated:
30600 case DW_AT_associated:
30601 case DW_AT_byte_stride:
30602 a->dw_attr_val.val_class = dw_val_class_die_ref;
30603 a->dw_attr_val.val_entry = NULL;
30604 a->dw_attr_val.v.val_die_ref.die
30605 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30606 a->dw_attr_val.v.val_die_ref.external = 0;
30607 return true;
30608 default:
30609 break;
30611 if (dwarf_strict)
30612 return false;
30614 break;
30615 case DW_OP_const_type:
30616 case DW_OP_regval_type:
30617 case DW_OP_deref_type:
30618 case DW_OP_convert:
30619 case DW_OP_reinterpret:
30620 case DW_OP_GNU_const_type:
30621 case DW_OP_GNU_regval_type:
30622 case DW_OP_GNU_deref_type:
30623 case DW_OP_GNU_convert:
30624 case DW_OP_GNU_reinterpret:
30625 while (loc->dw_loc_next
30626 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30627 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30629 dw_die_ref base1, base2;
30630 unsigned enc1, enc2, size1, size2;
30631 if (loc->dw_loc_opc == DW_OP_regval_type
30632 || loc->dw_loc_opc == DW_OP_deref_type
30633 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30634 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30635 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30636 else if (loc->dw_loc_oprnd1.val_class
30637 == dw_val_class_unsigned_const)
30638 break;
30639 else
30640 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30641 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30642 == dw_val_class_unsigned_const)
30643 break;
30644 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30645 gcc_assert (base1->die_tag == DW_TAG_base_type
30646 && base2->die_tag == DW_TAG_base_type);
30647 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30648 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30649 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30650 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30651 if (size1 == size2
30652 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30653 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30654 && loc != keep)
30655 || enc1 == enc2))
30657 /* Optimize away next DW_OP_convert after
30658 adjusting LOC's base type die reference. */
30659 if (loc->dw_loc_opc == DW_OP_regval_type
30660 || loc->dw_loc_opc == DW_OP_deref_type
30661 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30662 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30663 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30664 else
30665 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30666 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30667 continue;
30669 /* Don't change integer DW_OP_convert after e.g. floating
30670 point typed stack entry. */
30671 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30672 keep = loc->dw_loc_next;
30673 break;
30675 break;
30676 default:
30677 break;
30679 return true;
30682 /* Helper function of resolve_addr. DIE had DW_AT_location of
30683 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30684 and DW_OP_addr couldn't be resolved. resolve_addr has already
30685 removed the DW_AT_location attribute. This function attempts to
30686 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30687 to it or DW_AT_const_value attribute, if possible. */
30689 static void
30690 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30692 if (!VAR_P (decl)
30693 || lookup_decl_die (decl) != die
30694 || DECL_EXTERNAL (decl)
30695 || !TREE_STATIC (decl)
30696 || DECL_INITIAL (decl) == NULL_TREE
30697 || DECL_P (DECL_INITIAL (decl))
30698 || get_AT (die, DW_AT_const_value))
30699 return;
30701 tree init = DECL_INITIAL (decl);
30702 HOST_WIDE_INT offset = 0;
30703 /* For variables that have been optimized away and thus
30704 don't have a memory location, see if we can emit
30705 DW_AT_const_value instead. */
30706 if (tree_add_const_value_attribute (die, init))
30707 return;
30708 if (dwarf_strict && dwarf_version < 5)
30709 return;
30710 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30711 and ADDR_EXPR refers to a decl that has DW_AT_location or
30712 DW_AT_const_value (but isn't addressable, otherwise
30713 resolving the original DW_OP_addr wouldn't fail), see if
30714 we can add DW_OP_implicit_pointer. */
30715 STRIP_NOPS (init);
30716 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30717 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30719 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30720 init = TREE_OPERAND (init, 0);
30721 STRIP_NOPS (init);
30723 if (TREE_CODE (init) != ADDR_EXPR)
30724 return;
30725 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30726 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30727 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30728 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30729 && TREE_OPERAND (init, 0) != decl))
30731 dw_die_ref ref;
30732 dw_loc_descr_ref l;
30734 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30736 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30737 if (!rtl)
30738 return;
30739 decl = SYMBOL_REF_DECL (rtl);
30741 else
30742 decl = TREE_OPERAND (init, 0);
30743 ref = lookup_decl_die (decl);
30744 if (ref == NULL
30745 || (!get_AT (ref, DW_AT_location)
30746 && !get_AT (ref, DW_AT_const_value)))
30747 return;
30748 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30749 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30750 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30751 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30752 add_AT_loc (die, DW_AT_location, l);
30756 /* Return NULL if l is a DWARF expression, or first op that is not
30757 valid DWARF expression. */
30759 static dw_loc_descr_ref
30760 non_dwarf_expression (dw_loc_descr_ref l)
30762 while (l)
30764 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30765 return l;
30766 switch (l->dw_loc_opc)
30768 case DW_OP_regx:
30769 case DW_OP_implicit_value:
30770 case DW_OP_stack_value:
30771 case DW_OP_implicit_pointer:
30772 case DW_OP_GNU_implicit_pointer:
30773 case DW_OP_GNU_parameter_ref:
30774 case DW_OP_piece:
30775 case DW_OP_bit_piece:
30776 return l;
30777 default:
30778 break;
30780 l = l->dw_loc_next;
30782 return NULL;
30785 /* Return adjusted copy of EXPR:
30786 If it is empty DWARF expression, return it.
30787 If it is valid non-empty DWARF expression,
30788 return copy of EXPR with DW_OP_deref appended to it.
30789 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30790 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30791 If it is DWARF expression followed by DW_OP_stack_value, return
30792 copy of the DWARF expression without anything appended.
30793 Otherwise, return NULL. */
30795 static dw_loc_descr_ref
30796 copy_deref_exprloc (dw_loc_descr_ref expr)
30798 dw_loc_descr_ref tail = NULL;
30800 if (expr == NULL)
30801 return NULL;
30803 dw_loc_descr_ref l = non_dwarf_expression (expr);
30804 if (l && l->dw_loc_next)
30805 return NULL;
30807 if (l)
30809 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30810 tail = new_loc_descr ((enum dwarf_location_atom)
30811 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30812 0, 0);
30813 else
30814 switch (l->dw_loc_opc)
30816 case DW_OP_regx:
30817 tail = new_loc_descr (DW_OP_bregx,
30818 l->dw_loc_oprnd1.v.val_unsigned, 0);
30819 break;
30820 case DW_OP_stack_value:
30821 break;
30822 default:
30823 return NULL;
30826 else
30827 tail = new_loc_descr (DW_OP_deref, 0, 0);
30829 dw_loc_descr_ref ret = NULL, *p = &ret;
30830 while (expr != l)
30832 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30833 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30834 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30835 p = &(*p)->dw_loc_next;
30836 expr = expr->dw_loc_next;
30838 *p = tail;
30839 return ret;
30842 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30843 reference to a variable or argument, adjust it if needed and return:
30844 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30845 attribute if present should be removed
30846 0 keep the attribute perhaps with minor modifications, no need to rescan
30847 1 if the attribute has been successfully adjusted. */
30849 static int
30850 optimize_string_length (dw_attr_node *a)
30852 dw_loc_descr_ref l = AT_loc (a), lv;
30853 dw_die_ref die;
30854 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30856 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30857 die = lookup_decl_die (decl);
30858 if (die)
30860 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30861 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30862 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30864 else
30865 return -1;
30867 else
30868 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30870 /* DWARF5 allows reference class, so we can then reference the DIE.
30871 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30872 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30874 a->dw_attr_val.val_class = dw_val_class_die_ref;
30875 a->dw_attr_val.val_entry = NULL;
30876 a->dw_attr_val.v.val_die_ref.die = die;
30877 a->dw_attr_val.v.val_die_ref.external = 0;
30878 return 0;
30881 dw_attr_node *av = get_AT (die, DW_AT_location);
30882 dw_loc_list_ref d;
30883 bool non_dwarf_expr = false;
30885 if (av == NULL)
30886 return dwarf_strict ? -1 : 0;
30887 switch (AT_class (av))
30889 case dw_val_class_loc_list:
30890 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30891 if (d->expr && non_dwarf_expression (d->expr))
30892 non_dwarf_expr = true;
30893 break;
30894 case dw_val_class_view_list:
30895 gcc_unreachable ();
30896 case dw_val_class_loc:
30897 lv = AT_loc (av);
30898 if (lv == NULL)
30899 return dwarf_strict ? -1 : 0;
30900 if (non_dwarf_expression (lv))
30901 non_dwarf_expr = true;
30902 break;
30903 default:
30904 return dwarf_strict ? -1 : 0;
30907 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30908 into DW_OP_call4 or DW_OP_GNU_variable_value into
30909 DW_OP_call4 DW_OP_deref, do so. */
30910 if (!non_dwarf_expr
30911 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30913 l->dw_loc_opc = DW_OP_call4;
30914 if (l->dw_loc_next)
30915 l->dw_loc_next = NULL;
30916 else
30917 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30918 return 0;
30921 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30922 copy over the DW_AT_location attribute from die to a. */
30923 if (l->dw_loc_next != NULL)
30925 a->dw_attr_val = av->dw_attr_val;
30926 return 1;
30929 dw_loc_list_ref list, *p;
30930 switch (AT_class (av))
30932 case dw_val_class_loc_list:
30933 p = &list;
30934 list = NULL;
30935 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30937 lv = copy_deref_exprloc (d->expr);
30938 if (lv)
30940 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30941 p = &(*p)->dw_loc_next;
30943 else if (!dwarf_strict && d->expr)
30944 return 0;
30946 if (list == NULL)
30947 return dwarf_strict ? -1 : 0;
30948 a->dw_attr_val.val_class = dw_val_class_loc_list;
30949 gen_llsym (list);
30950 *AT_loc_list_ptr (a) = list;
30951 return 1;
30952 case dw_val_class_loc:
30953 lv = copy_deref_exprloc (AT_loc (av));
30954 if (lv == NULL)
30955 return dwarf_strict ? -1 : 0;
30956 a->dw_attr_val.v.val_loc = lv;
30957 return 1;
30958 default:
30959 gcc_unreachable ();
30963 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30964 an address in .rodata section if the string literal is emitted there,
30965 or remove the containing location list or replace DW_AT_const_value
30966 with DW_AT_location and empty location expression, if it isn't found
30967 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30968 to something that has been emitted in the current CU. */
30970 static void
30971 resolve_addr (dw_die_ref die)
30973 dw_die_ref c;
30974 dw_attr_node *a;
30975 dw_loc_list_ref *curr, *start, loc;
30976 unsigned ix;
30977 bool remove_AT_byte_size = false;
30979 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30980 switch (AT_class (a))
30982 case dw_val_class_loc_list:
30983 start = curr = AT_loc_list_ptr (a);
30984 loc = *curr;
30985 gcc_assert (loc);
30986 /* The same list can be referenced more than once. See if we have
30987 already recorded the result from a previous pass. */
30988 if (loc->replaced)
30989 *curr = loc->dw_loc_next;
30990 else if (!loc->resolved_addr)
30992 /* As things stand, we do not expect or allow one die to
30993 reference a suffix of another die's location list chain.
30994 References must be identical or completely separate.
30995 There is therefore no need to cache the result of this
30996 pass on any list other than the first; doing so
30997 would lead to unnecessary writes. */
30998 while (*curr)
31000 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31001 if (!resolve_addr_in_expr (a, (*curr)->expr))
31003 dw_loc_list_ref next = (*curr)->dw_loc_next;
31004 dw_loc_descr_ref l = (*curr)->expr;
31006 if (next && (*curr)->ll_symbol)
31008 gcc_assert (!next->ll_symbol);
31009 next->ll_symbol = (*curr)->ll_symbol;
31010 next->vl_symbol = (*curr)->vl_symbol;
31012 if (dwarf_split_debug_info)
31013 remove_loc_list_addr_table_entries (l);
31014 *curr = next;
31016 else
31018 mark_base_types ((*curr)->expr);
31019 curr = &(*curr)->dw_loc_next;
31022 if (loc == *start)
31023 loc->resolved_addr = 1;
31024 else
31026 loc->replaced = 1;
31027 loc->dw_loc_next = *start;
31030 if (!*start)
31032 remove_AT (die, a->dw_attr);
31033 ix--;
31035 break;
31036 case dw_val_class_view_list:
31038 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31039 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31040 dw_val_node *llnode
31041 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31042 /* If we no longer have a loclist, or it no longer needs
31043 views, drop this attribute. */
31044 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31046 remove_AT (die, a->dw_attr);
31047 ix--;
31049 break;
31051 case dw_val_class_loc:
31053 dw_loc_descr_ref l = AT_loc (a);
31054 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31055 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31056 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31057 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31058 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31059 with DW_FORM_ref referencing the same DIE as
31060 DW_OP_GNU_variable_value used to reference. */
31061 if (a->dw_attr == DW_AT_string_length
31062 && l
31063 && l->dw_loc_opc == DW_OP_GNU_variable_value
31064 && (l->dw_loc_next == NULL
31065 || (l->dw_loc_next->dw_loc_next == NULL
31066 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31068 switch (optimize_string_length (a))
31070 case -1:
31071 remove_AT (die, a->dw_attr);
31072 ix--;
31073 /* If we drop DW_AT_string_length, we need to drop also
31074 DW_AT_{string_length_,}byte_size. */
31075 remove_AT_byte_size = true;
31076 continue;
31077 default:
31078 break;
31079 case 1:
31080 /* Even if we keep the optimized DW_AT_string_length,
31081 it might have changed AT_class, so process it again. */
31082 ix--;
31083 continue;
31086 /* For -gdwarf-2 don't attempt to optimize
31087 DW_AT_data_member_location containing
31088 DW_OP_plus_uconst - older consumers might
31089 rely on it being that op instead of a more complex,
31090 but shorter, location description. */
31091 if ((dwarf_version > 2
31092 || a->dw_attr != DW_AT_data_member_location
31093 || l == NULL
31094 || l->dw_loc_opc != DW_OP_plus_uconst
31095 || l->dw_loc_next != NULL)
31096 && !resolve_addr_in_expr (a, l))
31098 if (dwarf_split_debug_info)
31099 remove_loc_list_addr_table_entries (l);
31100 if (l != NULL
31101 && l->dw_loc_next == NULL
31102 && l->dw_loc_opc == DW_OP_addr
31103 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31104 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31105 && a->dw_attr == DW_AT_location)
31107 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31108 remove_AT (die, a->dw_attr);
31109 ix--;
31110 optimize_location_into_implicit_ptr (die, decl);
31111 break;
31113 if (a->dw_attr == DW_AT_string_length)
31114 /* If we drop DW_AT_string_length, we need to drop also
31115 DW_AT_{string_length_,}byte_size. */
31116 remove_AT_byte_size = true;
31117 remove_AT (die, a->dw_attr);
31118 ix--;
31120 else
31121 mark_base_types (l);
31123 break;
31124 case dw_val_class_addr:
31125 if (a->dw_attr == DW_AT_const_value
31126 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31128 if (AT_index (a) != NOT_INDEXED)
31129 remove_addr_table_entry (a->dw_attr_val.val_entry);
31130 remove_AT (die, a->dw_attr);
31131 ix--;
31133 if ((die->die_tag == DW_TAG_call_site
31134 && a->dw_attr == DW_AT_call_origin)
31135 || (die->die_tag == DW_TAG_GNU_call_site
31136 && a->dw_attr == DW_AT_abstract_origin))
31138 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31139 dw_die_ref tdie = lookup_decl_die (tdecl);
31140 dw_die_ref cdie;
31141 if (tdie == NULL
31142 && DECL_EXTERNAL (tdecl)
31143 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31144 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31146 dw_die_ref pdie = cdie;
31147 /* Make sure we don't add these DIEs into type units.
31148 We could emit skeleton DIEs for context (namespaces,
31149 outer structs/classes) and a skeleton DIE for the
31150 innermost context with DW_AT_signature pointing to the
31151 type unit. See PR78835. */
31152 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31153 pdie = pdie->die_parent;
31154 if (pdie == NULL)
31156 /* Creating a full DIE for tdecl is overly expensive and
31157 at this point even wrong when in the LTO phase
31158 as it can end up generating new type DIEs we didn't
31159 output and thus optimize_external_refs will crash. */
31160 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31161 add_AT_flag (tdie, DW_AT_external, 1);
31162 add_AT_flag (tdie, DW_AT_declaration, 1);
31163 add_linkage_attr (tdie, tdecl);
31164 add_name_and_src_coords_attributes (tdie, tdecl, true);
31165 equate_decl_number_to_die (tdecl, tdie);
31168 if (tdie)
31170 a->dw_attr_val.val_class = dw_val_class_die_ref;
31171 a->dw_attr_val.v.val_die_ref.die = tdie;
31172 a->dw_attr_val.v.val_die_ref.external = 0;
31174 else
31176 if (AT_index (a) != NOT_INDEXED)
31177 remove_addr_table_entry (a->dw_attr_val.val_entry);
31178 remove_AT (die, a->dw_attr);
31179 ix--;
31182 break;
31183 default:
31184 break;
31187 if (remove_AT_byte_size)
31188 remove_AT (die, dwarf_version >= 5
31189 ? DW_AT_string_length_byte_size
31190 : DW_AT_byte_size);
31192 FOR_EACH_CHILD (die, c, resolve_addr (c));
31195 /* Helper routines for optimize_location_lists.
31196 This pass tries to share identical local lists in .debug_loc
31197 section. */
31199 /* Iteratively hash operands of LOC opcode into HSTATE. */
31201 static void
31202 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31204 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31205 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31207 switch (loc->dw_loc_opc)
31209 case DW_OP_const4u:
31210 case DW_OP_const8u:
31211 if (loc->dtprel)
31212 goto hash_addr;
31213 /* FALLTHRU */
31214 case DW_OP_const1u:
31215 case DW_OP_const1s:
31216 case DW_OP_const2u:
31217 case DW_OP_const2s:
31218 case DW_OP_const4s:
31219 case DW_OP_const8s:
31220 case DW_OP_constu:
31221 case DW_OP_consts:
31222 case DW_OP_pick:
31223 case DW_OP_plus_uconst:
31224 case DW_OP_breg0:
31225 case DW_OP_breg1:
31226 case DW_OP_breg2:
31227 case DW_OP_breg3:
31228 case DW_OP_breg4:
31229 case DW_OP_breg5:
31230 case DW_OP_breg6:
31231 case DW_OP_breg7:
31232 case DW_OP_breg8:
31233 case DW_OP_breg9:
31234 case DW_OP_breg10:
31235 case DW_OP_breg11:
31236 case DW_OP_breg12:
31237 case DW_OP_breg13:
31238 case DW_OP_breg14:
31239 case DW_OP_breg15:
31240 case DW_OP_breg16:
31241 case DW_OP_breg17:
31242 case DW_OP_breg18:
31243 case DW_OP_breg19:
31244 case DW_OP_breg20:
31245 case DW_OP_breg21:
31246 case DW_OP_breg22:
31247 case DW_OP_breg23:
31248 case DW_OP_breg24:
31249 case DW_OP_breg25:
31250 case DW_OP_breg26:
31251 case DW_OP_breg27:
31252 case DW_OP_breg28:
31253 case DW_OP_breg29:
31254 case DW_OP_breg30:
31255 case DW_OP_breg31:
31256 case DW_OP_regx:
31257 case DW_OP_fbreg:
31258 case DW_OP_piece:
31259 case DW_OP_deref_size:
31260 case DW_OP_xderef_size:
31261 hstate.add_object (val1->v.val_int);
31262 break;
31263 case DW_OP_skip:
31264 case DW_OP_bra:
31266 int offset;
31268 gcc_assert (val1->val_class == dw_val_class_loc);
31269 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31270 hstate.add_object (offset);
31272 break;
31273 case DW_OP_implicit_value:
31274 hstate.add_object (val1->v.val_unsigned);
31275 switch (val2->val_class)
31277 case dw_val_class_const:
31278 hstate.add_object (val2->v.val_int);
31279 break;
31280 case dw_val_class_vec:
31282 unsigned int elt_size = val2->v.val_vec.elt_size;
31283 unsigned int len = val2->v.val_vec.length;
31285 hstate.add_int (elt_size);
31286 hstate.add_int (len);
31287 hstate.add (val2->v.val_vec.array, len * elt_size);
31289 break;
31290 case dw_val_class_const_double:
31291 hstate.add_object (val2->v.val_double.low);
31292 hstate.add_object (val2->v.val_double.high);
31293 break;
31294 case dw_val_class_wide_int:
31295 hstate.add (val2->v.val_wide->get_val (),
31296 get_full_len (*val2->v.val_wide)
31297 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31298 break;
31299 case dw_val_class_addr:
31300 inchash::add_rtx (val2->v.val_addr, hstate);
31301 break;
31302 default:
31303 gcc_unreachable ();
31305 break;
31306 case DW_OP_bregx:
31307 case DW_OP_bit_piece:
31308 hstate.add_object (val1->v.val_int);
31309 hstate.add_object (val2->v.val_int);
31310 break;
31311 case DW_OP_addr:
31312 hash_addr:
31313 if (loc->dtprel)
31315 unsigned char dtprel = 0xd1;
31316 hstate.add_object (dtprel);
31318 inchash::add_rtx (val1->v.val_addr, hstate);
31319 break;
31320 case DW_OP_GNU_addr_index:
31321 case DW_OP_addrx:
31322 case DW_OP_GNU_const_index:
31323 case DW_OP_constx:
31325 if (loc->dtprel)
31327 unsigned char dtprel = 0xd1;
31328 hstate.add_object (dtprel);
31330 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31332 break;
31333 case DW_OP_implicit_pointer:
31334 case DW_OP_GNU_implicit_pointer:
31335 hstate.add_int (val2->v.val_int);
31336 break;
31337 case DW_OP_entry_value:
31338 case DW_OP_GNU_entry_value:
31339 hstate.add_object (val1->v.val_loc);
31340 break;
31341 case DW_OP_regval_type:
31342 case DW_OP_deref_type:
31343 case DW_OP_GNU_regval_type:
31344 case DW_OP_GNU_deref_type:
31346 unsigned int byte_size
31347 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31348 unsigned int encoding
31349 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31350 hstate.add_object (val1->v.val_int);
31351 hstate.add_object (byte_size);
31352 hstate.add_object (encoding);
31354 break;
31355 case DW_OP_convert:
31356 case DW_OP_reinterpret:
31357 case DW_OP_GNU_convert:
31358 case DW_OP_GNU_reinterpret:
31359 if (val1->val_class == dw_val_class_unsigned_const)
31361 hstate.add_object (val1->v.val_unsigned);
31362 break;
31364 /* FALLTHRU */
31365 case DW_OP_const_type:
31366 case DW_OP_GNU_const_type:
31368 unsigned int byte_size
31369 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31370 unsigned int encoding
31371 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31372 hstate.add_object (byte_size);
31373 hstate.add_object (encoding);
31374 if (loc->dw_loc_opc != DW_OP_const_type
31375 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31376 break;
31377 hstate.add_object (val2->val_class);
31378 switch (val2->val_class)
31380 case dw_val_class_const:
31381 hstate.add_object (val2->v.val_int);
31382 break;
31383 case dw_val_class_vec:
31385 unsigned int elt_size = val2->v.val_vec.elt_size;
31386 unsigned int len = val2->v.val_vec.length;
31388 hstate.add_object (elt_size);
31389 hstate.add_object (len);
31390 hstate.add (val2->v.val_vec.array, len * elt_size);
31392 break;
31393 case dw_val_class_const_double:
31394 hstate.add_object (val2->v.val_double.low);
31395 hstate.add_object (val2->v.val_double.high);
31396 break;
31397 case dw_val_class_wide_int:
31398 hstate.add (val2->v.val_wide->get_val (),
31399 get_full_len (*val2->v.val_wide)
31400 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31401 break;
31402 default:
31403 gcc_unreachable ();
31406 break;
31408 default:
31409 /* Other codes have no operands. */
31410 break;
31414 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31416 static inline void
31417 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31419 dw_loc_descr_ref l;
31420 bool sizes_computed = false;
31421 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31422 size_of_locs (loc);
31424 for (l = loc; l != NULL; l = l->dw_loc_next)
31426 enum dwarf_location_atom opc = l->dw_loc_opc;
31427 hstate.add_object (opc);
31428 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31430 size_of_locs (loc);
31431 sizes_computed = true;
31433 hash_loc_operands (l, hstate);
31437 /* Compute hash of the whole location list LIST_HEAD. */
31439 static inline void
31440 hash_loc_list (dw_loc_list_ref list_head)
31442 dw_loc_list_ref curr = list_head;
31443 inchash::hash hstate;
31445 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31447 hstate.add (curr->begin, strlen (curr->begin) + 1);
31448 hstate.add (curr->end, strlen (curr->end) + 1);
31449 hstate.add_object (curr->vbegin);
31450 hstate.add_object (curr->vend);
31451 if (curr->section)
31452 hstate.add (curr->section, strlen (curr->section) + 1);
31453 hash_locs (curr->expr, hstate);
31455 list_head->hash = hstate.end ();
31458 /* Return true if X and Y opcodes have the same operands. */
31460 static inline bool
31461 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31463 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31464 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31465 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31466 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31468 switch (x->dw_loc_opc)
31470 case DW_OP_const4u:
31471 case DW_OP_const8u:
31472 if (x->dtprel)
31473 goto hash_addr;
31474 /* FALLTHRU */
31475 case DW_OP_const1u:
31476 case DW_OP_const1s:
31477 case DW_OP_const2u:
31478 case DW_OP_const2s:
31479 case DW_OP_const4s:
31480 case DW_OP_const8s:
31481 case DW_OP_constu:
31482 case DW_OP_consts:
31483 case DW_OP_pick:
31484 case DW_OP_plus_uconst:
31485 case DW_OP_breg0:
31486 case DW_OP_breg1:
31487 case DW_OP_breg2:
31488 case DW_OP_breg3:
31489 case DW_OP_breg4:
31490 case DW_OP_breg5:
31491 case DW_OP_breg6:
31492 case DW_OP_breg7:
31493 case DW_OP_breg8:
31494 case DW_OP_breg9:
31495 case DW_OP_breg10:
31496 case DW_OP_breg11:
31497 case DW_OP_breg12:
31498 case DW_OP_breg13:
31499 case DW_OP_breg14:
31500 case DW_OP_breg15:
31501 case DW_OP_breg16:
31502 case DW_OP_breg17:
31503 case DW_OP_breg18:
31504 case DW_OP_breg19:
31505 case DW_OP_breg20:
31506 case DW_OP_breg21:
31507 case DW_OP_breg22:
31508 case DW_OP_breg23:
31509 case DW_OP_breg24:
31510 case DW_OP_breg25:
31511 case DW_OP_breg26:
31512 case DW_OP_breg27:
31513 case DW_OP_breg28:
31514 case DW_OP_breg29:
31515 case DW_OP_breg30:
31516 case DW_OP_breg31:
31517 case DW_OP_regx:
31518 case DW_OP_fbreg:
31519 case DW_OP_piece:
31520 case DW_OP_deref_size:
31521 case DW_OP_xderef_size:
31522 return valx1->v.val_int == valy1->v.val_int;
31523 case DW_OP_skip:
31524 case DW_OP_bra:
31525 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31526 can cause irrelevant differences in dw_loc_addr. */
31527 gcc_assert (valx1->val_class == dw_val_class_loc
31528 && valy1->val_class == dw_val_class_loc
31529 && (dwarf_split_debug_info
31530 || x->dw_loc_addr == y->dw_loc_addr));
31531 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31532 case DW_OP_implicit_value:
31533 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31534 || valx2->val_class != valy2->val_class)
31535 return false;
31536 switch (valx2->val_class)
31538 case dw_val_class_const:
31539 return valx2->v.val_int == valy2->v.val_int;
31540 case dw_val_class_vec:
31541 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31542 && valx2->v.val_vec.length == valy2->v.val_vec.length
31543 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31544 valx2->v.val_vec.elt_size
31545 * valx2->v.val_vec.length) == 0;
31546 case dw_val_class_const_double:
31547 return valx2->v.val_double.low == valy2->v.val_double.low
31548 && valx2->v.val_double.high == valy2->v.val_double.high;
31549 case dw_val_class_wide_int:
31550 return *valx2->v.val_wide == *valy2->v.val_wide;
31551 case dw_val_class_addr:
31552 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31553 default:
31554 gcc_unreachable ();
31556 case DW_OP_bregx:
31557 case DW_OP_bit_piece:
31558 return valx1->v.val_int == valy1->v.val_int
31559 && valx2->v.val_int == valy2->v.val_int;
31560 case DW_OP_addr:
31561 hash_addr:
31562 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31563 case DW_OP_GNU_addr_index:
31564 case DW_OP_addrx:
31565 case DW_OP_GNU_const_index:
31566 case DW_OP_constx:
31568 rtx ax1 = valx1->val_entry->addr.rtl;
31569 rtx ay1 = valy1->val_entry->addr.rtl;
31570 return rtx_equal_p (ax1, ay1);
31572 case DW_OP_implicit_pointer:
31573 case DW_OP_GNU_implicit_pointer:
31574 return valx1->val_class == dw_val_class_die_ref
31575 && valx1->val_class == valy1->val_class
31576 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31577 && valx2->v.val_int == valy2->v.val_int;
31578 case DW_OP_entry_value:
31579 case DW_OP_GNU_entry_value:
31580 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31581 case DW_OP_const_type:
31582 case DW_OP_GNU_const_type:
31583 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31584 || valx2->val_class != valy2->val_class)
31585 return false;
31586 switch (valx2->val_class)
31588 case dw_val_class_const:
31589 return valx2->v.val_int == valy2->v.val_int;
31590 case dw_val_class_vec:
31591 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31592 && valx2->v.val_vec.length == valy2->v.val_vec.length
31593 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31594 valx2->v.val_vec.elt_size
31595 * valx2->v.val_vec.length) == 0;
31596 case dw_val_class_const_double:
31597 return valx2->v.val_double.low == valy2->v.val_double.low
31598 && valx2->v.val_double.high == valy2->v.val_double.high;
31599 case dw_val_class_wide_int:
31600 return *valx2->v.val_wide == *valy2->v.val_wide;
31601 default:
31602 gcc_unreachable ();
31604 case DW_OP_regval_type:
31605 case DW_OP_deref_type:
31606 case DW_OP_GNU_regval_type:
31607 case DW_OP_GNU_deref_type:
31608 return valx1->v.val_int == valy1->v.val_int
31609 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31610 case DW_OP_convert:
31611 case DW_OP_reinterpret:
31612 case DW_OP_GNU_convert:
31613 case DW_OP_GNU_reinterpret:
31614 if (valx1->val_class != valy1->val_class)
31615 return false;
31616 if (valx1->val_class == dw_val_class_unsigned_const)
31617 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31618 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31619 case DW_OP_GNU_parameter_ref:
31620 return valx1->val_class == dw_val_class_die_ref
31621 && valx1->val_class == valy1->val_class
31622 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31623 default:
31624 /* Other codes have no operands. */
31625 return true;
31629 /* Return true if DWARF location expressions X and Y are the same. */
31631 static inline bool
31632 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31634 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31635 if (x->dw_loc_opc != y->dw_loc_opc
31636 || x->dtprel != y->dtprel
31637 || !compare_loc_operands (x, y))
31638 break;
31639 return x == NULL && y == NULL;
31642 /* Hashtable helpers. */
31644 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31646 static inline hashval_t hash (const dw_loc_list_struct *);
31647 static inline bool equal (const dw_loc_list_struct *,
31648 const dw_loc_list_struct *);
31651 /* Return precomputed hash of location list X. */
31653 inline hashval_t
31654 loc_list_hasher::hash (const dw_loc_list_struct *x)
31656 return x->hash;
31659 /* Return true if location lists A and B are the same. */
31661 inline bool
31662 loc_list_hasher::equal (const dw_loc_list_struct *a,
31663 const dw_loc_list_struct *b)
31665 if (a == b)
31666 return 1;
31667 if (a->hash != b->hash)
31668 return 0;
31669 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31670 if (strcmp (a->begin, b->begin) != 0
31671 || strcmp (a->end, b->end) != 0
31672 || (a->section == NULL) != (b->section == NULL)
31673 || (a->section && strcmp (a->section, b->section) != 0)
31674 || a->vbegin != b->vbegin || a->vend != b->vend
31675 || !compare_locs (a->expr, b->expr))
31676 break;
31677 return a == NULL && b == NULL;
31680 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31683 /* Recursively optimize location lists referenced from DIE
31684 children and share them whenever possible. */
31686 static void
31687 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31689 dw_die_ref c;
31690 dw_attr_node *a;
31691 unsigned ix;
31692 dw_loc_list_struct **slot;
31693 bool drop_locviews = false;
31694 bool has_locviews = false;
31696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31697 if (AT_class (a) == dw_val_class_loc_list)
31699 dw_loc_list_ref list = AT_loc_list (a);
31700 /* TODO: perform some optimizations here, before hashing
31701 it and storing into the hash table. */
31702 hash_loc_list (list);
31703 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31704 if (*slot == NULL)
31706 *slot = list;
31707 if (loc_list_has_views (list))
31708 gcc_assert (list->vl_symbol);
31709 else if (list->vl_symbol)
31711 drop_locviews = true;
31712 list->vl_symbol = NULL;
31715 else
31717 if (list->vl_symbol && !(*slot)->vl_symbol)
31718 drop_locviews = true;
31719 a->dw_attr_val.v.val_loc_list = *slot;
31722 else if (AT_class (a) == dw_val_class_view_list)
31724 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31725 has_locviews = true;
31729 if (drop_locviews && has_locviews)
31730 remove_AT (die, DW_AT_GNU_locviews);
31732 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31736 /* Recursively assign each location list a unique index into the debug_addr
31737 section. */
31739 static void
31740 index_location_lists (dw_die_ref die)
31742 dw_die_ref c;
31743 dw_attr_node *a;
31744 unsigned ix;
31746 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31747 if (AT_class (a) == dw_val_class_loc_list)
31749 dw_loc_list_ref list = AT_loc_list (a);
31750 dw_loc_list_ref curr;
31751 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31753 /* Don't index an entry that has already been indexed
31754 or won't be output. Make sure skip_loc_list_entry doesn't
31755 call size_of_locs, because that might cause circular dependency,
31756 index_location_lists requiring address table indexes to be
31757 computed, but adding new indexes through add_addr_table_entry
31758 and address table index computation requiring no new additions
31759 to the hash table. In the rare case of DWARF[234] >= 64KB
31760 location expression, we'll just waste unused address table entry
31761 for it. */
31762 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31763 continue;
31765 curr->begin_entry
31766 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31767 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31768 curr->end_entry
31769 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31773 FOR_EACH_CHILD (die, c, index_location_lists (c));
31776 /* Optimize location lists referenced from DIE
31777 children and share them whenever possible. */
31779 static void
31780 optimize_location_lists (dw_die_ref die)
31782 loc_list_hash_type htab (500);
31783 optimize_location_lists_1 (die, &htab);
31786 /* Traverse the limbo die list, and add parent/child links. The only
31787 dies without parents that should be here are concrete instances of
31788 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31789 For concrete instances, we can get the parent die from the abstract
31790 instance. */
31792 static void
31793 flush_limbo_die_list (void)
31795 limbo_die_node *node;
31797 /* get_context_die calls force_decl_die, which can put new DIEs on the
31798 limbo list in LTO mode when nested functions are put in a different
31799 partition than that of their parent function. */
31800 while ((node = limbo_die_list))
31802 dw_die_ref die = node->die;
31803 limbo_die_list = node->next;
31805 if (die->die_parent == NULL)
31807 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31809 if (origin && origin->die_parent)
31810 add_child_die (origin->die_parent, die);
31811 else if (is_cu_die (die))
31813 else if (seen_error ())
31814 /* It's OK to be confused by errors in the input. */
31815 add_child_die (comp_unit_die (), die);
31816 else
31818 /* In certain situations, the lexical block containing a
31819 nested function can be optimized away, which results
31820 in the nested function die being orphaned. Likewise
31821 with the return type of that nested function. Force
31822 this to be a child of the containing function.
31824 It may happen that even the containing function got fully
31825 inlined and optimized out. In that case we are lost and
31826 assign the empty child. This should not be big issue as
31827 the function is likely unreachable too. */
31828 gcc_assert (node->created_for);
31830 if (DECL_P (node->created_for))
31831 origin = get_context_die (DECL_CONTEXT (node->created_for));
31832 else if (TYPE_P (node->created_for))
31833 origin = scope_die_for (node->created_for, comp_unit_die ());
31834 else
31835 origin = comp_unit_die ();
31837 add_child_die (origin, die);
31843 /* Reset DIEs so we can output them again. */
31845 static void
31846 reset_dies (dw_die_ref die)
31848 dw_die_ref c;
31850 /* Remove stuff we re-generate. */
31851 die->die_mark = 0;
31852 die->die_offset = 0;
31853 die->die_abbrev = 0;
31854 remove_AT (die, DW_AT_sibling);
31856 FOR_EACH_CHILD (die, c, reset_dies (c));
31859 /* reset_indirect_string removed the references coming from DW_AT_name
31860 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31861 .debug_line_str strings again. */
31863 static void
31864 adjust_name_comp_dir (dw_die_ref die)
31866 for (int i = 0; i < 2; i++)
31868 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31869 dw_attr_node *a = get_AT (die, attr_kind);
31870 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31871 continue;
31873 if (!debug_line_str_hash)
31874 debug_line_str_hash
31875 = hash_table<indirect_string_hasher>::create_ggc (10);
31877 struct indirect_string_node *node
31878 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31879 debug_line_str_hash);
31880 set_indirect_string (node);
31881 node->form = DW_FORM_line_strp;
31882 a->dw_attr_val.v.val_str = node;
31886 /* Output stuff that dwarf requires at the end of every file,
31887 and generate the DWARF-2 debugging info. */
31889 static void
31890 dwarf2out_finish (const char *filename)
31892 comdat_type_node *ctnode;
31893 dw_die_ref main_comp_unit_die;
31894 unsigned char checksum[16];
31895 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31897 /* Skip emitting DWARF if not required. */
31898 if (!dwarf_debuginfo_p ())
31899 return;
31901 /* Flush out any latecomers to the limbo party. */
31902 flush_limbo_die_list ();
31904 if (inline_entry_data_table)
31905 gcc_assert (inline_entry_data_table->is_empty ());
31907 if (flag_checking)
31909 verify_die (comp_unit_die ());
31910 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31911 verify_die (node->die);
31914 /* We shouldn't have any symbols with delayed asm names for
31915 DIEs generated after early finish. */
31916 gcc_assert (deferred_asm_name == NULL);
31918 gen_remaining_tmpl_value_param_die_attribute ();
31920 if (flag_generate_lto || flag_generate_offload)
31922 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31924 /* Prune stuff so that dwarf2out_finish runs successfully
31925 for the fat part of the object. */
31926 reset_dies (comp_unit_die ());
31927 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31928 reset_dies (node->die);
31930 hash_table<comdat_type_hasher> comdat_type_table (100);
31931 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31933 comdat_type_node **slot
31934 = comdat_type_table.find_slot (ctnode, INSERT);
31936 /* Don't reset types twice. */
31937 if (*slot != HTAB_EMPTY_ENTRY)
31938 continue;
31940 /* Remove the pointer to the line table. */
31941 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31943 if (debug_info_level >= DINFO_LEVEL_TERSE)
31944 reset_dies (ctnode->root_die);
31946 *slot = ctnode;
31949 /* Reset die CU symbol so we don't output it twice. */
31950 comp_unit_die ()->die_id.die_symbol = NULL;
31952 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31953 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31954 if (have_macinfo)
31955 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31957 /* Remove indirect string decisions. */
31958 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31959 if (debug_line_str_hash)
31961 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31962 debug_line_str_hash = NULL;
31963 if (asm_outputs_debug_line_str ())
31965 adjust_name_comp_dir (comp_unit_die ());
31966 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31967 adjust_name_comp_dir (node->die);
31972 #if ENABLE_ASSERT_CHECKING
31974 dw_die_ref die = comp_unit_die (), c;
31975 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31977 #endif
31978 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31979 resolve_addr (ctnode->root_die);
31980 resolve_addr (comp_unit_die ());
31981 move_marked_base_types ();
31983 if (dump_file)
31985 fprintf (dump_file, "DWARF for %s\n", filename);
31986 print_die (comp_unit_die (), dump_file);
31989 /* Initialize sections and labels used for actual assembler output. */
31990 unsigned generation = init_sections_and_labels (false);
31992 /* Traverse the DIE's and add sibling attributes to those DIE's that
31993 have children. */
31994 add_sibling_attributes (comp_unit_die ());
31995 limbo_die_node *node;
31996 for (node = cu_die_list; node; node = node->next)
31997 add_sibling_attributes (node->die);
31998 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31999 add_sibling_attributes (ctnode->root_die);
32001 /* When splitting DWARF info, we put some attributes in the
32002 skeleton compile_unit DIE that remains in the .o, while
32003 most attributes go in the DWO compile_unit_die. */
32004 if (dwarf_split_debug_info)
32006 limbo_die_node *cu;
32007 main_comp_unit_die = gen_compile_unit_die (NULL);
32008 if (dwarf_version >= 5)
32009 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32010 cu = limbo_die_list;
32011 gcc_assert (cu->die == main_comp_unit_die);
32012 limbo_die_list = limbo_die_list->next;
32013 cu->next = cu_die_list;
32014 cu_die_list = cu;
32016 else
32017 main_comp_unit_die = comp_unit_die ();
32019 /* Output a terminator label for the .text section. */
32020 switch_to_section (text_section);
32021 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32022 if (cold_text_section)
32024 switch_to_section (cold_text_section);
32025 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32028 /* We can only use the low/high_pc attributes if all of the code was
32029 in .text. */
32030 if ((!have_multiple_function_sections
32031 && vec_safe_length (switch_text_ranges) < 2)
32032 || (dwarf_version < 3 && dwarf_strict))
32034 const char *end_label = text_end_label;
32035 if (vec_safe_length (switch_text_ranges) == 1)
32036 end_label = (*switch_text_ranges)[0];
32037 /* Don't add if the CU has no associated code. */
32038 if (switch_text_ranges)
32039 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32040 end_label, true);
32042 else
32044 unsigned fde_idx;
32045 dw_fde_ref fde;
32046 bool range_list_added = false;
32047 if (switch_text_ranges)
32049 const char *prev_loc = text_section_label;
32050 const char *loc;
32051 unsigned idx;
32053 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32054 if (prev_loc)
32056 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32057 loc, &range_list_added, true);
32058 prev_loc = NULL;
32060 else
32061 prev_loc = loc;
32063 if (prev_loc)
32064 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32065 text_end_label, &range_list_added, true);
32068 if (switch_cold_ranges)
32070 const char *prev_loc = cold_text_section_label;
32071 const char *loc;
32072 unsigned idx;
32074 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32075 if (prev_loc)
32077 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32078 loc, &range_list_added, true);
32079 prev_loc = NULL;
32081 else
32082 prev_loc = loc;
32084 if (prev_loc)
32085 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32086 cold_end_label, &range_list_added, true);
32089 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32091 if (fde->ignored_debug)
32092 continue;
32093 if (!fde->in_std_section)
32094 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32095 fde->dw_fde_end, &range_list_added,
32096 true);
32097 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32098 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32099 fde->dw_fde_second_end, &range_list_added,
32100 true);
32103 if (range_list_added)
32105 /* We need to give .debug_loc and .debug_ranges an appropriate
32106 "base address". Use zero so that these addresses become
32107 absolute. Historically, we've emitted the unexpected
32108 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32109 Emit both to give time for other tools to adapt. */
32110 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32111 if (! dwarf_strict && dwarf_version < 4)
32112 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32114 add_ranges (NULL);
32115 have_multiple_function_sections = true;
32119 /* AIX Assembler inserts the length, so adjust the reference to match the
32120 offset expected by debuggers. */
32121 strcpy (dl_section_ref, debug_line_section_label);
32122 if (XCOFF_DEBUGGING_INFO)
32123 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32125 if (debug_info_level >= DINFO_LEVEL_TERSE)
32126 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32127 dl_section_ref);
32129 if (have_macinfo)
32130 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32131 macinfo_section_label);
32133 if (dwarf_split_debug_info)
32135 if (have_location_lists)
32137 /* Since we generate the loclists in the split DWARF .dwo
32138 file itself, we don't need to generate a loclists_base
32139 attribute for the split compile unit DIE. That attribute
32140 (and using relocatable sec_offset FORMs) isn't allowed
32141 for a split compile unit. Only if the .debug_loclists
32142 section was in the main file, would we need to generate a
32143 loclists_base attribute here (for the full or skeleton
32144 unit DIE). */
32146 /* optimize_location_lists calculates the size of the lists,
32147 so index them first, and assign indices to the entries.
32148 Although optimize_location_lists will remove entries from
32149 the table, it only does so for duplicates, and therefore
32150 only reduces ref_counts to 1. */
32151 index_location_lists (comp_unit_die ());
32154 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32155 index_rnglists ();
32157 if (addr_index_table != NULL)
32159 unsigned int index = 0;
32160 addr_index_table
32161 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32162 (&index);
32166 loc_list_idx = 0;
32167 if (have_location_lists)
32169 optimize_location_lists (comp_unit_die ());
32170 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32171 if (dwarf_version >= 5 && dwarf_split_debug_info)
32172 assign_location_list_indexes (comp_unit_die ());
32175 save_macinfo_strings ();
32177 if (dwarf_split_debug_info)
32179 unsigned int index = 0;
32181 /* Add attributes common to skeleton compile_units and
32182 type_units. Because these attributes include strings, it
32183 must be done before freezing the string table. Top-level
32184 skeleton die attrs are added when the skeleton type unit is
32185 created, so ensure it is created by this point. */
32186 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32187 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32190 /* Output all of the compilation units. We put the main one last so that
32191 the offsets are available to output_pubnames. */
32192 for (node = cu_die_list; node; node = node->next)
32193 output_comp_unit (node->die, 0, NULL);
32195 hash_table<comdat_type_hasher> comdat_type_table (100);
32196 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32198 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32200 /* Don't output duplicate types. */
32201 if (*slot != HTAB_EMPTY_ENTRY)
32202 continue;
32204 /* Add a pointer to the line table for the main compilation unit
32205 so that the debugger can make sense of DW_AT_decl_file
32206 attributes. */
32207 if (debug_info_level >= DINFO_LEVEL_TERSE)
32208 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32209 (!dwarf_split_debug_info
32210 ? dl_section_ref
32211 : debug_skeleton_line_section_label));
32213 output_comdat_type_unit (ctnode, false);
32214 *slot = ctnode;
32217 if (dwarf_split_debug_info)
32219 int mark;
32220 struct md5_ctx ctx;
32222 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32223 md5_init_ctx (&ctx);
32224 mark = 0;
32225 die_checksum (comp_unit_die (), &ctx, &mark);
32226 unmark_all_dies (comp_unit_die ());
32227 md5_finish_ctx (&ctx, checksum);
32229 if (dwarf_version < 5)
32231 /* Use the first 8 bytes of the checksum as the dwo_id,
32232 and add it to both comp-unit DIEs. */
32233 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32234 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32237 /* Add the base offset of the ranges table to the skeleton
32238 comp-unit DIE. */
32239 if (!vec_safe_is_empty (ranges_table))
32241 if (dwarf_version < 5)
32242 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32243 ranges_section_label);
32246 output_addr_table ();
32249 /* Output the main compilation unit if non-empty or if .debug_macinfo
32250 or .debug_macro will be emitted. */
32251 output_comp_unit (comp_unit_die (), have_macinfo,
32252 dwarf_split_debug_info ? checksum : NULL);
32254 if (dwarf_split_debug_info && info_section_emitted)
32255 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32257 /* Output the abbreviation table. */
32258 if (vec_safe_length (abbrev_die_table) != 1)
32260 switch_to_section (debug_abbrev_section);
32261 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32262 output_abbrev_section ();
32265 /* Output location list section if necessary. */
32266 if (have_location_lists)
32268 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32269 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32270 /* Output the location lists info. */
32271 switch_to_section (debug_loc_section);
32272 if (dwarf_version >= 5)
32274 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32275 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32276 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32277 dw2_asm_output_data (4, 0xffffffff,
32278 "Initial length escape value indicating "
32279 "64-bit DWARF extension");
32280 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32281 "Length of Location Lists");
32282 ASM_OUTPUT_LABEL (asm_out_file, l1);
32283 output_dwarf_version ();
32284 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32285 dw2_asm_output_data (1, 0, "Segment Size");
32286 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32287 "Offset Entry Count");
32289 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32290 if (dwarf_version >= 5 && dwarf_split_debug_info)
32292 unsigned int save_loc_list_idx = loc_list_idx;
32293 loc_list_idx = 0;
32294 output_loclists_offsets (comp_unit_die ());
32295 gcc_assert (save_loc_list_idx == loc_list_idx);
32297 output_location_lists (comp_unit_die ());
32298 if (dwarf_version >= 5)
32299 ASM_OUTPUT_LABEL (asm_out_file, l2);
32302 output_pubtables ();
32304 /* Output the address range information if a CU (.debug_info section)
32305 was emitted. We output an empty table even if we had no functions
32306 to put in it. This because the consumer has no way to tell the
32307 difference between an empty table that we omitted and failure to
32308 generate a table that would have contained data. */
32309 if (info_section_emitted)
32311 switch_to_section (debug_aranges_section);
32312 output_aranges ();
32315 /* Output ranges section if necessary. */
32316 if (!vec_safe_is_empty (ranges_table))
32318 if (dwarf_version >= 5)
32320 if (dwarf_split_debug_info)
32322 /* We don't know right now whether there are any
32323 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32324 Depending on into which of those two belongs the first
32325 ranges_table entry, emit that section first and that
32326 output_rnglists call will return true if the other kind of
32327 ranges needs to be emitted as well. */
32328 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32329 if (output_rnglists (generation, dwo))
32330 output_rnglists (generation, !dwo);
32332 else
32333 output_rnglists (generation, false);
32335 else
32336 output_ranges ();
32339 /* Have to end the macro section. */
32340 if (have_macinfo)
32342 switch_to_section (debug_macinfo_section);
32343 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32344 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32345 : debug_skeleton_line_section_label, false);
32346 dw2_asm_output_data (1, 0, "End compilation unit");
32349 /* Output the source line correspondence table. We must do this
32350 even if there is no line information. Otherwise, on an empty
32351 translation unit, we will generate a present, but empty,
32352 .debug_info section. IRIX 6.5 `nm' will then complain when
32353 examining the file. This is done late so that any filenames
32354 used by the debug_info section are marked as 'used'. */
32355 switch_to_section (debug_line_section);
32356 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32357 if (! output_asm_line_debug_info ())
32358 output_line_info (false);
32360 if (dwarf_split_debug_info && info_section_emitted)
32362 switch_to_section (debug_skeleton_line_section);
32363 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32364 output_line_info (true);
32367 /* If we emitted any indirect strings, output the string table too. */
32368 if (debug_str_hash || skeleton_debug_str_hash)
32369 output_indirect_strings ();
32370 if (debug_line_str_hash)
32372 switch_to_section (debug_line_str_section);
32373 const enum dwarf_form form = DW_FORM_line_strp;
32374 debug_line_str_hash->traverse<enum dwarf_form,
32375 output_indirect_string> (form);
32378 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32379 symview_upper_bound = 0;
32380 if (zero_view_p)
32381 bitmap_clear (zero_view_p);
32384 /* Returns a hash value for X (which really is a variable_value_struct). */
32386 inline hashval_t
32387 variable_value_hasher::hash (variable_value_struct *x)
32389 return (hashval_t) x->decl_id;
32392 /* Return nonzero if decl_id of variable_value_struct X is the same as
32393 UID of decl Y. */
32395 inline bool
32396 variable_value_hasher::equal (variable_value_struct *x, tree y)
32398 return x->decl_id == DECL_UID (y);
32401 /* Helper function for resolve_variable_value, handle
32402 DW_OP_GNU_variable_value in one location expression.
32403 Return true if exprloc has been changed into loclist. */
32405 static bool
32406 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32408 dw_loc_descr_ref next;
32409 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32411 next = loc->dw_loc_next;
32412 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32413 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32414 continue;
32416 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32417 if (DECL_CONTEXT (decl) != current_function_decl)
32418 continue;
32420 dw_die_ref ref = lookup_decl_die (decl);
32421 if (ref)
32423 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32424 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32425 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32426 continue;
32428 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32429 if (l == NULL)
32430 continue;
32431 if (l->dw_loc_next)
32433 if (AT_class (a) != dw_val_class_loc)
32434 continue;
32435 switch (a->dw_attr)
32437 /* Following attributes allow both exprloc and loclist
32438 classes, so we can change them into a loclist. */
32439 case DW_AT_location:
32440 case DW_AT_string_length:
32441 case DW_AT_return_addr:
32442 case DW_AT_data_member_location:
32443 case DW_AT_frame_base:
32444 case DW_AT_segment:
32445 case DW_AT_static_link:
32446 case DW_AT_use_location:
32447 case DW_AT_vtable_elem_location:
32448 if (prev)
32450 prev->dw_loc_next = NULL;
32451 prepend_loc_descr_to_each (l, AT_loc (a));
32453 if (next)
32454 add_loc_descr_to_each (l, next);
32455 a->dw_attr_val.val_class = dw_val_class_loc_list;
32456 a->dw_attr_val.val_entry = NULL;
32457 a->dw_attr_val.v.val_loc_list = l;
32458 have_location_lists = true;
32459 return true;
32460 /* Following attributes allow both exprloc and reference,
32461 so if the whole expression is DW_OP_GNU_variable_value alone
32462 we could transform it into reference. */
32463 case DW_AT_byte_size:
32464 case DW_AT_bit_size:
32465 case DW_AT_lower_bound:
32466 case DW_AT_upper_bound:
32467 case DW_AT_bit_stride:
32468 case DW_AT_count:
32469 case DW_AT_allocated:
32470 case DW_AT_associated:
32471 case DW_AT_byte_stride:
32472 if (prev == NULL && next == NULL)
32473 break;
32474 /* FALLTHRU */
32475 default:
32476 if (dwarf_strict)
32477 continue;
32478 break;
32480 /* Create DW_TAG_variable that we can refer to. */
32481 gen_decl_die (decl, NULL_TREE, NULL,
32482 lookup_decl_die (current_function_decl));
32483 ref = lookup_decl_die (decl);
32484 if (ref)
32486 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32487 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32488 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32490 continue;
32492 if (prev)
32494 prev->dw_loc_next = l->expr;
32495 add_loc_descr (&prev->dw_loc_next, next);
32496 free_loc_descr (loc, NULL);
32497 next = prev->dw_loc_next;
32499 else
32501 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32502 add_loc_descr (&loc, next);
32503 next = loc;
32505 loc = prev;
32507 return false;
32510 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32512 static void
32513 resolve_variable_value (dw_die_ref die)
32515 dw_attr_node *a;
32516 dw_loc_list_ref loc;
32517 unsigned ix;
32519 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32520 switch (AT_class (a))
32522 case dw_val_class_loc:
32523 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32524 break;
32525 /* FALLTHRU */
32526 case dw_val_class_loc_list:
32527 loc = AT_loc_list (a);
32528 gcc_assert (loc);
32529 for (; loc; loc = loc->dw_loc_next)
32530 resolve_variable_value_in_expr (a, loc->expr);
32531 break;
32532 default:
32533 break;
32537 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32538 temporaries in the current function. */
32540 static void
32541 resolve_variable_values (void)
32543 if (!variable_value_hash || !current_function_decl)
32544 return;
32546 struct variable_value_struct *node
32547 = variable_value_hash->find_with_hash (current_function_decl,
32548 DECL_UID (current_function_decl));
32550 if (node == NULL)
32551 return;
32553 unsigned int i;
32554 dw_die_ref die;
32555 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32556 resolve_variable_value (die);
32559 /* Helper function for note_variable_value, handle one location
32560 expression. */
32562 static void
32563 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32565 for (; loc; loc = loc->dw_loc_next)
32566 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32567 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32569 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32570 dw_die_ref ref = lookup_decl_die (decl);
32571 if (! ref && (flag_generate_lto || flag_generate_offload))
32573 /* ??? This is somewhat a hack because we do not create DIEs
32574 for variables not in BLOCK trees early but when generating
32575 early LTO output we need the dw_val_class_decl_ref to be
32576 fully resolved. For fat LTO objects we'd also like to
32577 undo this after LTO dwarf output. */
32578 gcc_assert (DECL_CONTEXT (decl));
32579 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32580 gcc_assert (ctx != NULL);
32581 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32582 ref = lookup_decl_die (decl);
32583 gcc_assert (ref != NULL);
32585 if (ref)
32587 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32588 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32589 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32590 continue;
32592 if (VAR_P (decl)
32593 && DECL_CONTEXT (decl)
32594 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32595 && lookup_decl_die (DECL_CONTEXT (decl)))
32597 if (!variable_value_hash)
32598 variable_value_hash
32599 = hash_table<variable_value_hasher>::create_ggc (10);
32601 tree fndecl = DECL_CONTEXT (decl);
32602 struct variable_value_struct *node;
32603 struct variable_value_struct **slot
32604 = variable_value_hash->find_slot_with_hash (fndecl,
32605 DECL_UID (fndecl),
32606 INSERT);
32607 if (*slot == NULL)
32609 node = ggc_cleared_alloc<variable_value_struct> ();
32610 node->decl_id = DECL_UID (fndecl);
32611 *slot = node;
32613 else
32614 node = *slot;
32616 vec_safe_push (node->dies, die);
32621 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32622 with dw_val_class_decl_ref operand. */
32624 static void
32625 note_variable_value (dw_die_ref die)
32627 dw_die_ref c;
32628 dw_attr_node *a;
32629 dw_loc_list_ref loc;
32630 unsigned ix;
32632 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32633 switch (AT_class (a))
32635 case dw_val_class_loc_list:
32636 loc = AT_loc_list (a);
32637 gcc_assert (loc);
32638 if (!loc->noted_variable_value)
32640 loc->noted_variable_value = 1;
32641 for (; loc; loc = loc->dw_loc_next)
32642 note_variable_value_in_expr (die, loc->expr);
32644 break;
32645 case dw_val_class_loc:
32646 note_variable_value_in_expr (die, AT_loc (a));
32647 break;
32648 default:
32649 break;
32652 /* Mark children. */
32653 FOR_EACH_CHILD (die, c, note_variable_value (c));
32656 /* Process DWARF dies for CTF generation. */
32658 static void
32659 ctf_debug_do_cu (dw_die_ref die)
32661 dw_die_ref c;
32663 if (!ctf_do_die (die))
32664 return;
32666 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32669 /* Perform any cleanups needed after the early debug generation pass
32670 has run. */
32672 static void
32673 dwarf2out_early_finish (const char *filename)
32675 set_early_dwarf s;
32676 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32678 /* PCH might result in DW_AT_producer string being restored from the
32679 header compilation, so always fill it with empty string initially
32680 and overwrite only here. */
32681 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32683 if (dwarf_record_gcc_switches)
32684 producer_string = gen_producer_string (lang_hooks.name,
32685 save_decoded_options,
32686 save_decoded_options_count);
32687 else
32688 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32690 producer->dw_attr_val.v.val_str->refcount--;
32691 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32693 /* Add the name for the main input file now. We delayed this from
32694 dwarf2out_init to avoid complications with PCH. */
32695 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32696 add_comp_dir_attribute (comp_unit_die ());
32698 /* With LTO early dwarf was really finished at compile-time, so make
32699 sure to adjust the phase after annotating the LTRANS CU DIE. */
32700 if (in_lto_p)
32702 early_dwarf_finished = true;
32703 if (dump_file)
32705 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32706 print_die (comp_unit_die (), dump_file);
32708 return;
32711 /* Walk through the list of incomplete types again, trying once more to
32712 emit full debugging info for them. */
32713 retry_incomplete_types ();
32715 gen_scheduled_generic_parms_dies ();
32716 gen_remaining_tmpl_value_param_die_attribute ();
32718 /* The point here is to flush out the limbo list so that it is empty
32719 and we don't need to stream it for LTO. */
32720 flush_limbo_die_list ();
32722 /* Add DW_AT_linkage_name for all deferred DIEs. */
32723 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32725 tree decl = node->created_for;
32726 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32727 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32728 ended up in deferred_asm_name before we knew it was
32729 constant and never written to disk. */
32730 && DECL_ASSEMBLER_NAME (decl))
32732 add_linkage_attr (node->die, decl);
32733 move_linkage_attr (node->die);
32736 deferred_asm_name = NULL;
32738 if (flag_eliminate_unused_debug_types)
32739 prune_unused_types ();
32741 /* Generate separate COMDAT sections for type DIEs. */
32742 if (use_debug_types)
32744 break_out_comdat_types (comp_unit_die ());
32746 /* Each new type_unit DIE was added to the limbo die list when created.
32747 Since these have all been added to comdat_type_list, clear the
32748 limbo die list. */
32749 limbo_die_list = NULL;
32751 /* For each new comdat type unit, copy declarations for incomplete
32752 types to make the new unit self-contained (i.e., no direct
32753 references to the main compile unit). */
32754 for (comdat_type_node *ctnode = comdat_type_list;
32755 ctnode != NULL; ctnode = ctnode->next)
32756 copy_decls_for_unworthy_types (ctnode->root_die);
32757 copy_decls_for_unworthy_types (comp_unit_die ());
32759 /* In the process of copying declarations from one unit to another,
32760 we may have left some declarations behind that are no longer
32761 referenced. Prune them. */
32762 prune_unused_types ();
32765 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32766 with dw_val_class_decl_ref operand. */
32767 note_variable_value (comp_unit_die ());
32768 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32769 note_variable_value (node->die);
32770 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
32771 ctnode = ctnode->next)
32772 note_variable_value (ctnode->root_die);
32773 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32774 note_variable_value (node->die);
32776 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32777 both the main_cu and all skeleton TUs. Making this call unconditional
32778 would end up either adding a second copy of the AT_pubnames attribute, or
32779 requiring a special case in add_top_level_skeleton_die_attrs. */
32780 if (!dwarf_split_debug_info)
32781 add_AT_pubnames (comp_unit_die ());
32783 /* The early debug phase is now finished. */
32784 early_dwarf_finished = true;
32785 if (dump_file)
32787 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32788 print_die (comp_unit_die (), dump_file);
32791 /* Generate CTF/BTF debug info. */
32792 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32793 || btf_debuginfo_p ()) && lang_GNU_C ())
32795 ctf_debug_init ();
32796 ctf_debug_do_cu (comp_unit_die ());
32797 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32798 ctf_debug_do_cu (node->die);
32799 /* Post process the debug data in the CTF container if necessary. */
32800 ctf_debug_init_postprocess (btf_debuginfo_p ());
32801 /* Emit CTF/BTF debug info. */
32802 ctf_debug_finalize (filename, btf_debuginfo_p ());
32805 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32806 if ((!flag_generate_lto && !flag_generate_offload)
32807 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32808 copy_lto_debug_sections operation of the simple object support in
32809 libiberty is not implemented for them yet. */
32810 || TARGET_PECOFF || TARGET_COFF)
32811 return;
32813 /* Now as we are going to output for LTO initialize sections and labels
32814 to the LTO variants. We don't need a random-seed postfix as other
32815 LTO sections as linking the LTO debug sections into one in a partial
32816 link is fine. */
32817 init_sections_and_labels (true);
32819 /* The output below is modeled after dwarf2out_finish with all
32820 location related output removed and some LTO specific changes.
32821 Some refactoring might make both smaller and easier to match up. */
32823 /* Traverse the DIE's and add sibling attributes to those DIE's
32824 that have children. */
32825 add_sibling_attributes (comp_unit_die ());
32826 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32827 add_sibling_attributes (node->die);
32828 for (comdat_type_node *ctnode = comdat_type_list;
32829 ctnode != NULL; ctnode = ctnode->next)
32830 add_sibling_attributes (ctnode->root_die);
32832 /* AIX Assembler inserts the length, so adjust the reference to match the
32833 offset expected by debuggers. */
32834 strcpy (dl_section_ref, debug_line_section_label);
32835 if (XCOFF_DEBUGGING_INFO)
32836 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32838 if (debug_info_level >= DINFO_LEVEL_TERSE)
32839 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32841 if (have_macinfo)
32842 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32843 macinfo_section_label);
32845 save_macinfo_strings ();
32847 if (dwarf_split_debug_info)
32849 unsigned int index = 0;
32850 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32853 /* Output all of the compilation units. We put the main one last so that
32854 the offsets are available to output_pubnames. */
32855 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32856 output_comp_unit (node->die, 0, NULL);
32858 hash_table<comdat_type_hasher> comdat_type_table (100);
32859 for (comdat_type_node *ctnode = comdat_type_list;
32860 ctnode != NULL; ctnode = ctnode->next)
32862 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32864 /* Don't output duplicate types. */
32865 if (*slot != HTAB_EMPTY_ENTRY)
32866 continue;
32868 /* Add a pointer to the line table for the main compilation unit
32869 so that the debugger can make sense of DW_AT_decl_file
32870 attributes. */
32871 if (debug_info_level >= DINFO_LEVEL_TERSE)
32872 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32873 (!dwarf_split_debug_info
32874 ? debug_line_section_label
32875 : debug_skeleton_line_section_label));
32877 output_comdat_type_unit (ctnode, true);
32878 *slot = ctnode;
32881 /* Stick a unique symbol to the main debuginfo section. */
32882 compute_comp_unit_symbol (comp_unit_die ());
32884 /* Output the main compilation unit. We always need it if only for
32885 the CU symbol. */
32886 output_comp_unit (comp_unit_die (), true, NULL);
32888 /* Output the abbreviation table. */
32889 if (vec_safe_length (abbrev_die_table) != 1)
32891 switch_to_section (debug_abbrev_section);
32892 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32893 output_abbrev_section ();
32896 /* Have to end the macro section. */
32897 if (have_macinfo)
32899 /* We have to save macinfo state if we need to output it again
32900 for the FAT part of the object. */
32901 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32902 if (flag_fat_lto_objects)
32903 macinfo_table = macinfo_table->copy ();
32905 switch_to_section (debug_macinfo_section);
32906 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32907 output_macinfo (debug_line_section_label, true);
32908 dw2_asm_output_data (1, 0, "End compilation unit");
32910 if (flag_fat_lto_objects)
32912 vec_free (macinfo_table);
32913 macinfo_table = saved_macinfo_table;
32917 /* Emit a skeleton debug_line section. */
32918 switch_to_section (debug_line_section);
32919 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32920 output_line_info (true);
32922 /* If we emitted any indirect strings, output the string table too. */
32923 if (debug_str_hash || skeleton_debug_str_hash)
32924 output_indirect_strings ();
32925 if (debug_line_str_hash)
32927 switch_to_section (debug_line_str_section);
32928 const enum dwarf_form form = DW_FORM_line_strp;
32929 debug_line_str_hash->traverse<enum dwarf_form,
32930 output_indirect_string> (form);
32933 /* Switch back to the text section. */
32934 switch_to_section (text_section);
32937 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32938 within the same process. For use by toplev::finalize. */
32940 void
32941 dwarf2out_c_finalize (void)
32943 last_var_location_insn = NULL;
32944 cached_next_real_insn = NULL;
32945 used_rtx_array = NULL;
32946 incomplete_types = NULL;
32947 debug_info_section = NULL;
32948 debug_skeleton_info_section = NULL;
32949 debug_abbrev_section = NULL;
32950 debug_skeleton_abbrev_section = NULL;
32951 debug_aranges_section = NULL;
32952 debug_addr_section = NULL;
32953 debug_macinfo_section = NULL;
32954 debug_line_section = NULL;
32955 debug_skeleton_line_section = NULL;
32956 debug_loc_section = NULL;
32957 debug_pubnames_section = NULL;
32958 debug_pubtypes_section = NULL;
32959 debug_str_section = NULL;
32960 debug_line_str_section = NULL;
32961 debug_str_dwo_section = NULL;
32962 debug_str_offsets_section = NULL;
32963 debug_ranges_section = NULL;
32964 debug_ranges_dwo_section = NULL;
32965 debug_frame_section = NULL;
32966 fde_vec = NULL;
32967 debug_str_hash = NULL;
32968 debug_line_str_hash = NULL;
32969 skeleton_debug_str_hash = NULL;
32970 dw2_string_counter = 0;
32971 have_multiple_function_sections = false;
32972 in_text_section_p = false;
32973 cold_text_section = NULL;
32974 last_text_label = NULL;
32975 last_cold_label = NULL;
32976 switch_text_ranges = NULL;
32977 switch_cold_ranges = NULL;
32978 current_unit_personality = NULL;
32980 early_dwarf = false;
32981 early_dwarf_finished = false;
32983 next_die_offset = 0;
32984 single_comp_unit_die = NULL;
32985 comdat_type_list = NULL;
32986 limbo_die_list = NULL;
32987 file_table = NULL;
32988 decl_die_table = NULL;
32989 common_block_die_table = NULL;
32990 decl_loc_table = NULL;
32991 call_arg_locations = NULL;
32992 call_arg_loc_last = NULL;
32993 call_site_count = -1;
32994 tail_call_site_count = -1;
32995 cached_dw_loc_list_table = NULL;
32996 abbrev_die_table = NULL;
32997 delete dwarf_proc_stack_usage_map;
32998 dwarf_proc_stack_usage_map = NULL;
32999 line_info_label_num = 0;
33000 cur_line_info_table = NULL;
33001 text_section_line_info = NULL;
33002 cold_text_section_line_info = NULL;
33003 separate_line_info = NULL;
33004 info_section_emitted = false;
33005 pubname_table = NULL;
33006 pubtype_table = NULL;
33007 macinfo_table = NULL;
33008 ranges_table = NULL;
33009 ranges_by_label = NULL;
33010 rnglist_idx = 0;
33011 have_location_lists = false;
33012 loclabel_num = 0;
33013 poc_label_num = 0;
33014 last_emitted_file = NULL;
33015 label_num = 0;
33016 tmpl_value_parm_die_table = NULL;
33017 generic_type_instances = NULL;
33018 frame_pointer_fb_offset = 0;
33019 frame_pointer_fb_offset_valid = false;
33020 base_types.release ();
33021 XDELETEVEC (producer_string);
33022 producer_string = NULL;
33023 output_line_info_generation = 0;
33024 init_sections_and_labels_generation = 0;
33027 #include "gt-dwarf2out.h"