compiler: only build thunk struct type when it is needed
[official-gcc.git] / gcc / dwarf2out.cc
blobe81044b8c482e87a5536206fd120edc15c754836
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2022 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2ctf.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tree-pretty-print.h"
87 #include "print-rtl.h"
88 #include "debug.h"
89 #include "common/common-target.h"
90 #include "langhooks.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
96 #include "rtl-iter.h"
97 #include "stringpool.h"
98 #include "attribs.h"
99 #include "file-prefix-map.h" /* remap_debug_filename() */
101 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
102 int, bool);
103 static rtx_insn *last_var_location_insn;
104 static rtx_insn *cached_next_real_insn;
105 static void dwarf2out_decl (tree);
106 static bool is_redundant_typedef (const_tree);
108 #ifndef XCOFF_DEBUGGING_INFO
109 #define XCOFF_DEBUGGING_INFO 0
110 #endif
112 #ifndef HAVE_XCOFF_DWARF_EXTRAS
113 #define HAVE_XCOFF_DWARF_EXTRAS 0
114 #endif
116 #ifdef VMS_DEBUGGING_INFO
117 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119 /* Define this macro to be a nonzero value if the directory specifications
120 which are output in the debug info should end with a separator. */
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
122 /* Define this macro to evaluate to a nonzero value if GCC should refrain
123 from generating indirect strings in DWARF2 debug information, for instance
124 if your target is stuck with an old version of GDB that is unable to
125 process them properly or uses VMS Debug. */
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
127 #else
128 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
130 #endif
132 /* ??? Poison these here until it can be done generically. They've been
133 totally replaced in this file; make sure it stays that way. */
134 #undef DWARF2_UNWIND_INFO
135 #undef DWARF2_FRAME_INFO
136 #if (GCC_VERSION >= 3000)
137 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
138 #endif
140 /* The size of the target's pointer type. */
141 #ifndef PTR_SIZE
142 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
143 #endif
145 /* Array of RTXes referenced by the debugging information, which therefore
146 must be kept around forever. */
147 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149 /* A pointer to the base of a list of incomplete types which might be
150 completed at some later time. incomplete_types_list needs to be a
151 vec<tree, va_gc> *because we want to tell the garbage collector about
152 it. */
153 static GTY(()) vec<tree, va_gc> *incomplete_types;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static unsigned macinfo_label_base = 1;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_ranges_dwo_section;
176 static GTY(()) section *debug_frame_section;
178 /* Maximum size (in bytes) of an artificially generated label. */
179 #define MAX_ARTIFICIAL_LABEL_BYTES 40
181 /* According to the (draft) DWARF 3 specification, the initial length
182 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
183 bytes are 0xffffffff, followed by the length stored in the next 8
184 bytes.
186 However, the SGI/MIPS ABI uses an initial length which is equal to
187 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189 #ifndef DWARF_INITIAL_LENGTH_SIZE
190 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
191 #endif
193 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
194 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
195 #endif
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
225 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227 typedef const char *compare_type;
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238 main object file, rather than the dwo, similar to the force_direct
239 parameter elsewhere but with additional complications:
241 1) The string is needed in both the main object file and the dwo.
242 That is, the comp_dir and dwo_name will appear in both places.
244 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
245 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247 3) GCC chooses the form to use late, depending on the size and
248 reference count.
250 Rather than forcing the all debug string handling functions and
251 callers to deal with these complications, simply use a separate,
252 special-cased string table for any attribute that should go in the
253 main object file. This limits the complexity to just the places
254 that need it. */
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258 static GTY(()) int dw2_string_counter;
260 /* True if the compilation unit places functions in more than one section. */
261 static GTY(()) bool have_multiple_function_sections = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* True if currently in text section. */
267 static GTY(()) bool in_text_section_p = false;
269 /* Last debug-on location in corresponding section. */
270 static GTY(()) const char *last_text_label;
271 static GTY(()) const char *last_cold_label;
273 /* Mark debug-on/off locations per section.
274 NULL means the section is not used at all. */
275 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
276 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278 /* The DIE for C++14 'auto' in a function return type. */
279 static GTY(()) dw_die_ref auto_die;
281 /* The DIE for C++14 'decltype(auto)' in a function return type. */
282 static GTY(()) dw_die_ref decltype_auto_die;
284 /* Forward declarations for functions defined in this file. */
286 static void output_call_frame_info (int);
288 /* Personality decl of current unit. Used only when assembler does not support
289 personality CFI. */
290 static GTY(()) rtx current_unit_personality;
292 /* Whether an eh_frame section is required. */
293 static GTY(()) bool do_eh_frame = false;
295 /* .debug_rnglists next index. */
296 static unsigned int rnglist_idx;
298 /* Data and reference forms for relocatable data. */
299 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
300 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302 #ifndef DEBUG_FRAME_SECTION
303 #define DEBUG_FRAME_SECTION ".debug_frame"
304 #endif
306 #ifndef FUNC_BEGIN_LABEL
307 #define FUNC_BEGIN_LABEL "LFB"
308 #endif
310 #ifndef FUNC_SECOND_SECT_LABEL
311 #define FUNC_SECOND_SECT_LABEL "LFSB"
312 #endif
314 #ifndef FUNC_END_LABEL
315 #define FUNC_END_LABEL "LFE"
316 #endif
318 #ifndef PROLOGUE_END_LABEL
319 #define PROLOGUE_END_LABEL "LPE"
320 #endif
322 #ifndef EPILOGUE_BEGIN_LABEL
323 #define EPILOGUE_BEGIN_LABEL "LEB"
324 #endif
326 #ifndef FRAME_BEGIN_LABEL
327 #define FRAME_BEGIN_LABEL "Lframe"
328 #endif
329 #define CIE_AFTER_SIZE_LABEL "LSCIE"
330 #define CIE_END_LABEL "LECIE"
331 #define FDE_LABEL "LSFDE"
332 #define FDE_AFTER_SIZE_LABEL "LASFDE"
333 #define FDE_END_LABEL "LEFDE"
334 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
335 #define LINE_NUMBER_END_LABEL "LELT"
336 #define LN_PROLOG_AS_LABEL "LASLTP"
337 #define LN_PROLOG_END_LABEL "LELTP"
338 #define DIE_LABEL_PREFIX "DW"
340 /* Match the base name of a file to the base name of a compilation unit. */
342 static int
343 matches_main_base (const char *path)
345 /* Cache the last query. */
346 static const char *last_path = NULL;
347 static int last_match = 0;
348 if (path != last_path)
350 const char *base;
351 int length = base_of_path (path, &base);
352 last_path = path;
353 last_match = (length == main_input_baselength
354 && memcmp (base, main_input_basename, length) == 0);
356 return last_match;
359 #ifdef DEBUG_DEBUG_STRUCT
361 static int
362 dump_struct_debug (tree type, enum debug_info_usage usage,
363 enum debug_struct_file criterion, int generic,
364 int matches, int result)
366 /* Find the type name. */
367 tree type_decl = TYPE_STUB_DECL (type);
368 tree t = type_decl;
369 const char *name = 0;
370 if (TREE_CODE (t) == TYPE_DECL)
371 t = DECL_NAME (t);
372 if (t)
373 name = IDENTIFIER_POINTER (t);
375 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
376 criterion,
377 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
378 matches ? "bas" : "hdr",
379 generic ? "gen" : "ord",
380 usage == DINFO_USAGE_DFN ? ";" :
381 usage == DINFO_USAGE_DIR_USE ? "." : "*",
382 result,
383 (void*) type_decl, name);
384 return result;
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 dump_struct_debug (type, usage, criterion, generic, matches, result)
389 #else
391 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
392 (result)
394 #endif
396 /* Get the number of HOST_WIDE_INTs needed to represent the precision
397 of the number. */
399 static unsigned int
400 get_full_len (const wide_int &op)
402 int prec = wi::get_precision (op);
403 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
404 / HOST_BITS_PER_WIDE_INT);
407 static bool
408 should_emit_struct_debug (tree type, enum debug_info_usage usage)
410 if (debug_info_level <= DINFO_LEVEL_TERSE)
411 return false;
413 enum debug_struct_file criterion;
414 tree type_decl;
415 bool generic = lang_hooks.types.generic_p (type);
417 if (generic)
418 criterion = debug_struct_generic[usage];
419 else
420 criterion = debug_struct_ordinary[usage];
422 if (criterion == DINFO_STRUCT_FILE_NONE)
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
424 if (criterion == DINFO_STRUCT_FILE_ANY)
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
427 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
429 if (type_decl != NULL)
431 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
432 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
434 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
435 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
438 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
441 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
442 switch to the data section instead, and write out a synthetic start label
443 for collect2 the first time around. */
445 static void
446 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
448 if (eh_frame_section == 0)
450 int flags;
452 if (EH_TABLES_CAN_BE_READ_ONLY)
454 int fde_encoding;
455 int per_encoding;
456 int lsda_encoding;
458 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
459 /*global=*/0);
460 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
461 /*global=*/1);
462 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
463 /*global=*/0);
464 flags = ((! flag_pic
465 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
466 && (fde_encoding & 0x70) != DW_EH_PE_aligned
467 && (per_encoding & 0x70) != DW_EH_PE_absptr
468 && (per_encoding & 0x70) != DW_EH_PE_aligned
469 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
470 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
471 ? 0 : SECTION_WRITE);
473 else
474 flags = SECTION_WRITE;
476 #ifdef EH_FRAME_SECTION_NAME
477 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
478 #else
479 eh_frame_section = ((flags == SECTION_WRITE)
480 ? data_section : readonly_data_section);
481 #endif /* EH_FRAME_SECTION_NAME */
484 switch_to_section (eh_frame_section);
486 #ifdef EH_FRAME_THROUGH_COLLECT2
487 /* We have no special eh_frame section. Emit special labels to guide
488 collect2. */
489 if (!back)
491 tree label = get_file_function_name ("F");
492 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
493 targetm.asm_out.globalize_label (asm_out_file,
494 IDENTIFIER_POINTER (label));
495 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
497 #endif
500 /* Switch [BACK] to the eh or debug frame table section, depending on
501 FOR_EH. */
503 static void
504 switch_to_frame_table_section (int for_eh, bool back)
506 if (for_eh)
507 switch_to_eh_frame_section (back);
508 else
510 if (!debug_frame_section)
511 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
512 SECTION_DEBUG, NULL);
513 switch_to_section (debug_frame_section);
517 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
519 enum dw_cfi_oprnd_type
520 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
522 switch (cfi)
524 case DW_CFA_nop:
525 case DW_CFA_GNU_window_save:
526 case DW_CFA_remember_state:
527 case DW_CFA_restore_state:
528 return dw_cfi_oprnd_unused;
530 case DW_CFA_set_loc:
531 case DW_CFA_advance_loc1:
532 case DW_CFA_advance_loc2:
533 case DW_CFA_advance_loc4:
534 case DW_CFA_MIPS_advance_loc8:
535 return dw_cfi_oprnd_addr;
537 case DW_CFA_offset:
538 case DW_CFA_offset_extended:
539 case DW_CFA_def_cfa:
540 case DW_CFA_offset_extended_sf:
541 case DW_CFA_def_cfa_sf:
542 case DW_CFA_restore:
543 case DW_CFA_restore_extended:
544 case DW_CFA_undefined:
545 case DW_CFA_same_value:
546 case DW_CFA_def_cfa_register:
547 case DW_CFA_register:
548 case DW_CFA_expression:
549 case DW_CFA_val_expression:
550 return dw_cfi_oprnd_reg_num;
552 case DW_CFA_def_cfa_offset:
553 case DW_CFA_GNU_args_size:
554 case DW_CFA_def_cfa_offset_sf:
555 return dw_cfi_oprnd_offset;
557 case DW_CFA_def_cfa_expression:
558 return dw_cfi_oprnd_loc;
560 default:
561 gcc_unreachable ();
565 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
567 enum dw_cfi_oprnd_type
568 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
570 switch (cfi)
572 case DW_CFA_def_cfa:
573 case DW_CFA_def_cfa_sf:
574 case DW_CFA_offset:
575 case DW_CFA_offset_extended_sf:
576 case DW_CFA_offset_extended:
577 return dw_cfi_oprnd_offset;
579 case DW_CFA_register:
580 return dw_cfi_oprnd_reg_num;
582 case DW_CFA_expression:
583 case DW_CFA_val_expression:
584 return dw_cfi_oprnd_loc;
586 case DW_CFA_def_cfa_expression:
587 return dw_cfi_oprnd_cfa_loc;
589 default:
590 return dw_cfi_oprnd_unused;
594 /* Output one FDE. */
596 static void
597 output_fde (dw_fde_ref fde, bool for_eh, bool second,
598 char *section_start_label, int fde_encoding, char *augmentation,
599 bool any_lsda_needed, int lsda_encoding)
601 const char *begin, *end;
602 static unsigned int j;
603 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
605 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
606 /* empty */ 0);
607 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
608 for_eh + j);
609 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
610 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
611 if (!XCOFF_DEBUGGING_INFO || for_eh)
613 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
614 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
615 " indicating 64-bit DWARF extension");
616 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
617 "FDE Length");
619 ASM_OUTPUT_LABEL (asm_out_file, l1);
621 if (for_eh)
622 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
623 else
624 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
625 debug_frame_section, "FDE CIE offset");
627 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
628 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
630 if (for_eh)
632 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
633 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
634 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
635 "FDE initial location");
636 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
637 end, begin, "FDE address range");
639 else
641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645 if (augmentation[0])
647 if (any_lsda_needed)
649 int size = size_of_encoded_value (lsda_encoding);
651 if (lsda_encoding == DW_EH_PE_aligned)
653 int offset = ( 4 /* Length */
654 + 4 /* CIE offset */
655 + 2 * size_of_encoded_value (fde_encoding)
656 + 1 /* Augmentation size */ );
657 int pad = -offset & (PTR_SIZE - 1);
659 size += pad;
660 gcc_assert (size_of_uleb128 (size) == 1);
663 dw2_asm_output_data_uleb128 (size, "Augmentation size");
665 if (fde->uses_eh_lsda)
667 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
668 fde->funcdef_number);
669 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
670 gen_rtx_SYMBOL_REF (Pmode, l1),
671 false,
672 "Language Specific Data Area");
674 else
676 if (lsda_encoding == DW_EH_PE_aligned)
677 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
678 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
679 "Language Specific Data Area (none)");
682 else
683 dw2_asm_output_data_uleb128 (0, "Augmentation size");
686 /* Loop through the Call Frame Instructions associated with this FDE. */
687 fde->dw_fde_current_label = begin;
689 size_t from, until, i;
691 from = 0;
692 until = vec_safe_length (fde->dw_fde_cfi);
694 if (fde->dw_fde_second_begin == NULL)
696 else if (!second)
697 until = fde->dw_fde_switch_cfi_index;
698 else
699 from = fde->dw_fde_switch_cfi_index;
701 for (i = from; i < until; i++)
702 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705 /* If we are to emit a ref/link from function bodies to their frame tables,
706 do it now. This is typically performed to make sure that tables
707 associated with functions are dragged with them and not discarded in
708 garbage collecting links. We need to do this on a per function basis to
709 cope with -ffunction-sections. */
711 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
712 /* Switch to the function section, emit the ref to the tables, and
713 switch *back* into the table section. */
714 switch_to_section (function_section (fde->decl));
715 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
716 switch_to_frame_table_section (for_eh, true);
717 #endif
719 /* Pad the FDE out to an address sized boundary. */
720 ASM_OUTPUT_ALIGN (asm_out_file,
721 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
722 ASM_OUTPUT_LABEL (asm_out_file, l2);
724 j += 2;
727 /* Return true if frame description entry FDE is needed for EH. */
729 static bool
730 fde_needed_for_eh_p (dw_fde_ref fde)
732 if (flag_asynchronous_unwind_tables)
733 return true;
735 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
736 return true;
738 if (fde->uses_eh_lsda)
739 return true;
741 /* If exceptions are enabled, we have collected nothrow info. */
742 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
743 return false;
745 return true;
748 /* Output the call frame information used to record information
749 that relates to calculating the frame pointer, and records the
750 location of saved registers. */
752 static void
753 output_call_frame_info (int for_eh)
755 unsigned int i;
756 dw_fde_ref fde;
757 dw_cfi_ref cfi;
758 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
759 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
760 bool any_lsda_needed = false;
761 char augmentation[6];
762 int augmentation_size;
763 int fde_encoding = DW_EH_PE_absptr;
764 int per_encoding = DW_EH_PE_absptr;
765 int lsda_encoding = DW_EH_PE_absptr;
766 int return_reg;
767 rtx personality = NULL;
768 int dw_cie_version;
770 /* Don't emit a CIE if there won't be any FDEs. */
771 if (!fde_vec)
772 return;
774 /* Nothing to do if the assembler's doing it all. */
775 if (dwarf2out_do_cfi_asm ())
776 return;
778 /* If we don't have any functions we'll want to unwind out of, don't emit
779 any EH unwind information. If we make FDEs linkonce, we may have to
780 emit an empty label for an FDE that wouldn't otherwise be emitted. We
781 want to avoid having an FDE kept around when the function it refers to
782 is discarded. Example where this matters: a primary function template
783 in C++ requires EH information, an explicit specialization doesn't. */
784 if (for_eh)
786 bool any_eh_needed = false;
788 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790 if (fde->uses_eh_lsda)
791 any_eh_needed = any_lsda_needed = true;
792 else if (fde_needed_for_eh_p (fde))
793 any_eh_needed = true;
794 else if (TARGET_USES_WEAK_UNWIND_INFO)
795 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
798 if (!any_eh_needed)
799 return;
802 /* We're going to be generating comments, so turn on app. */
803 if (flag_debug_asm)
804 app_enable ();
806 /* Switch to the proper frame section, first time. */
807 switch_to_frame_table_section (for_eh, false);
809 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
810 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812 /* Output the CIE. */
813 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
814 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
815 if (!XCOFF_DEBUGGING_INFO || for_eh)
817 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
818 dw2_asm_output_data (4, 0xffffffff,
819 "Initial length escape value indicating 64-bit DWARF extension");
820 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
821 "Length of Common Information Entry");
823 ASM_OUTPUT_LABEL (asm_out_file, l1);
825 /* Now that the CIE pointer is PC-relative for EH,
826 use 0 to identify the CIE. */
827 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
828 (for_eh ? 0 : DWARF_CIE_ID),
829 "CIE Identifier Tag");
831 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
832 use CIE version 1, unless that would produce incorrect results
833 due to overflowing the return register column. */
834 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
835 dw_cie_version = 1;
836 if (return_reg >= 256 || dwarf_version > 2)
837 dw_cie_version = 3;
838 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
840 augmentation[0] = 0;
841 augmentation_size = 0;
843 personality = current_unit_personality;
844 if (for_eh)
846 char *p;
848 /* Augmentation:
849 z Indicates that a uleb128 is present to size the
850 augmentation section.
851 L Indicates the encoding (and thus presence) of
852 an LSDA pointer in the FDE augmentation.
853 R Indicates a non-default pointer encoding for
854 FDE code pointers.
855 P Indicates the presence of an encoding + language
856 personality routine in the CIE augmentation. */
858 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
859 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
860 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
862 p = augmentation + 1;
863 if (personality)
865 *p++ = 'P';
866 augmentation_size += 1 + size_of_encoded_value (per_encoding);
867 assemble_external_libcall (personality);
869 if (any_lsda_needed)
871 *p++ = 'L';
872 augmentation_size += 1;
874 if (fde_encoding != DW_EH_PE_absptr)
876 *p++ = 'R';
877 augmentation_size += 1;
879 if (p > augmentation + 1)
881 augmentation[0] = 'z';
882 *p = '\0';
885 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
886 if (personality && per_encoding == DW_EH_PE_aligned)
888 int offset = ( 4 /* Length */
889 + 4 /* CIE Id */
890 + 1 /* CIE version */
891 + strlen (augmentation) + 1 /* Augmentation */
892 + size_of_uleb128 (1) /* Code alignment */
893 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
894 + 1 /* RA column */
895 + 1 /* Augmentation size */
896 + 1 /* Personality encoding */ );
897 int pad = -offset & (PTR_SIZE - 1);
899 augmentation_size += pad;
901 /* Augmentations should be small, so there's scarce need to
902 iterate for a solution. Die if we exceed one uleb128 byte. */
903 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
907 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
908 if (dw_cie_version >= 4)
910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
911 dw2_asm_output_data (1, 0, "CIE Segment Size");
913 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
914 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
915 "CIE Data Alignment Factor");
917 if (dw_cie_version == 1)
918 dw2_asm_output_data (1, return_reg, "CIE RA Column");
919 else
920 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
922 if (augmentation[0])
924 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
925 if (personality)
927 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
928 eh_data_format_name (per_encoding));
929 dw2_asm_output_encoded_addr_rtx (per_encoding,
930 personality,
931 true, NULL);
934 if (any_lsda_needed)
935 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
936 eh_data_format_name (lsda_encoding));
938 if (fde_encoding != DW_EH_PE_absptr)
939 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
940 eh_data_format_name (fde_encoding));
943 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
944 output_cfi (cfi, NULL, for_eh);
946 /* Pad the CIE out to an address sized boundary. */
947 ASM_OUTPUT_ALIGN (asm_out_file,
948 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
949 ASM_OUTPUT_LABEL (asm_out_file, l2);
951 /* Loop through all of the FDE's. */
952 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
954 unsigned int k;
956 /* Don't emit EH unwind info for leaf functions that don't need it. */
957 if (for_eh && !fde_needed_for_eh_p (fde))
958 continue;
960 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
961 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
962 augmentation, any_lsda_needed, lsda_encoding);
965 if (for_eh && targetm.terminate_dw2_eh_frame_info)
966 dw2_asm_output_data (4, 0, "End of Table");
968 /* Turn off app to make assembly quicker. */
969 if (flag_debug_asm)
970 app_disable ();
973 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
975 static void
976 dwarf2out_do_cfi_startproc (bool second)
978 int enc;
979 rtx ref;
981 fprintf (asm_out_file, "\t.cfi_startproc\n");
983 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
985 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
986 eh unwinders. */
987 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
988 return;
990 rtx personality = get_personality_function (current_function_decl);
992 if (personality)
994 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
995 ref = personality;
997 /* ??? The GAS support isn't entirely consistent. We have to
998 handle indirect support ourselves, but PC-relative is done
999 in the assembler. Further, the assembler can't handle any
1000 of the weirder relocation types. */
1001 if (enc & DW_EH_PE_indirect)
1003 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1004 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1005 else
1006 ref = dw2_force_const_mem (ref, true);
1009 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1010 output_addr_const (asm_out_file, ref);
1011 fputc ('\n', asm_out_file);
1014 if (crtl->uses_eh_lsda)
1016 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1018 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1019 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1020 current_function_funcdef_no);
1021 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1022 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1024 if (enc & DW_EH_PE_indirect)
1026 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1027 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1028 else
1029 ref = dw2_force_const_mem (ref, true);
1032 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1033 output_addr_const (asm_out_file, ref);
1034 fputc ('\n', asm_out_file);
1038 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1039 this allocation may be done before pass_final. */
1041 dw_fde_ref
1042 dwarf2out_alloc_current_fde (void)
1044 dw_fde_ref fde;
1046 fde = ggc_cleared_alloc<dw_fde_node> ();
1047 fde->decl = current_function_decl;
1048 fde->funcdef_number = current_function_funcdef_no;
1049 fde->fde_index = vec_safe_length (fde_vec);
1050 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1051 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1052 fde->nothrow = crtl->nothrow;
1053 fde->drap_reg = INVALID_REGNUM;
1054 fde->vdrap_reg = INVALID_REGNUM;
1056 /* Record the FDE associated with this function. */
1057 cfun->fde = fde;
1058 vec_safe_push (fde_vec, fde);
1060 return fde;
1063 /* Output a marker (i.e. a label) for the beginning of a function, before
1064 the prologue. */
1066 void
1067 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 unsigned int column ATTRIBUTE_UNUSED,
1069 const char *file ATTRIBUTE_UNUSED)
1071 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 char * dup_label;
1073 dw_fde_ref fde;
1074 section *fnsec;
1075 bool do_frame;
1077 current_function_func_begin_label = NULL;
1079 do_frame = dwarf2out_do_frame ();
1081 /* ??? current_function_func_begin_label is also used by except.cc for
1082 call-site information. We must emit this label if it might be used. */
1083 if (!do_frame
1084 && (!flag_exceptions
1085 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1086 return;
1088 fnsec = function_section (current_function_decl);
1089 switch_to_section (fnsec);
1090 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1091 current_function_funcdef_no);
1092 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1093 current_function_funcdef_no);
1094 dup_label = xstrdup (label);
1095 current_function_func_begin_label = dup_label;
1097 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1098 if (!do_frame)
1099 return;
1101 /* Unlike the debug version, the EH version of frame unwind info is a per-
1102 function setting so we need to record whether we need it for the unit. */
1103 do_eh_frame |= dwarf2out_do_eh_frame ();
1105 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1106 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1107 would include pass_dwarf2_frame. If we've not created the FDE yet,
1108 do so now. */
1109 fde = cfun->fde;
1110 if (fde == NULL)
1111 fde = dwarf2out_alloc_current_fde ();
1113 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1114 fde->dw_fde_begin = dup_label;
1115 fde->dw_fde_current_label = dup_label;
1116 fde->in_std_section = (fnsec == text_section
1117 || (cold_text_section && fnsec == cold_text_section));
1118 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1119 in_text_section_p = fnsec == text_section;
1121 /* We only want to output line number information for the genuine dwarf2
1122 prologue case, not the eh frame case. */
1123 #ifdef DWARF2_DEBUGGING_INFO
1124 if (file)
1125 dwarf2out_source_line (line, column, file, 0, true);
1126 #endif
1128 if (dwarf2out_do_cfi_asm ())
1129 dwarf2out_do_cfi_startproc (false);
1130 else
1132 rtx personality = get_personality_function (current_function_decl);
1133 if (!current_unit_personality)
1134 current_unit_personality = personality;
1136 /* We cannot keep a current personality per function as without CFI
1137 asm, at the point where we emit the CFI data, there is no current
1138 function anymore. */
1139 if (personality && current_unit_personality != personality)
1140 sorry ("multiple EH personalities are supported only with assemblers "
1141 "supporting %<.cfi_personality%> directive");
1145 /* Output a marker (i.e. a label) for the end of the generated code
1146 for a function prologue. This gets called *after* the prologue code has
1147 been generated. */
1149 void
1150 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1151 const char *file ATTRIBUTE_UNUSED)
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1160 current_function_funcdef_no);
1161 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the beginning of the generated code
1165 for a function epilogue. This gets called *before* the prologue code has
1166 been generated. */
1168 void
1169 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde = cfun->fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 if (fde->dw_fde_vms_begin_epilogue)
1176 return;
1178 /* Output a label to mark the endpoint of the code generated for this
1179 function. */
1180 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1181 current_function_funcdef_no);
1182 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1183 current_function_funcdef_no);
1184 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1187 /* Mark the ranges of non-debug subsections in the std text sections. */
1189 static void
1190 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1192 bool std_section;
1193 const char *begin_label, *end_label;
1194 const char **last_end_label;
1195 vec<const char *, va_gc> **switch_ranges;
1197 if (second)
1199 std_section = fde->second_in_std_section;
1200 begin_label = fde->dw_fde_second_begin;
1201 end_label = fde->dw_fde_second_end;
1203 else
1205 std_section = fde->in_std_section;
1206 begin_label = fde->dw_fde_begin;
1207 end_label = fde->dw_fde_end;
1210 if (!std_section)
1211 return;
1213 if (in_text_section_p)
1215 last_end_label = &last_text_label;
1216 switch_ranges = &switch_text_ranges;
1218 else
1220 last_end_label = &last_cold_label;
1221 switch_ranges = &switch_cold_ranges;
1224 if (fde->ignored_debug)
1226 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1227 vec_safe_push (*switch_ranges, *last_end_label);
1229 else
1231 *last_end_label = end_label;
1233 if (!*switch_ranges)
1234 vec_alloc (*switch_ranges, 16);
1235 else if (vec_safe_length (*switch_ranges) & 1)
1236 vec_safe_push (*switch_ranges, begin_label);
1240 /* Output a marker (i.e. a label) for the absolute end of the generated code
1241 for a function definition. This gets called *after* the epilogue code has
1242 been generated. */
1244 void
1245 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1246 const char *file ATTRIBUTE_UNUSED)
1248 dw_fde_ref fde;
1249 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1251 last_var_location_insn = NULL;
1252 cached_next_real_insn = NULL;
1254 if (dwarf2out_do_cfi_asm ())
1255 fprintf (asm_out_file, "\t.cfi_endproc\n");
1257 /* Output a label to mark the endpoint of the code generated for this
1258 function. */
1259 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1260 current_function_funcdef_no);
1261 ASM_OUTPUT_LABEL (asm_out_file, label);
1262 fde = cfun->fde;
1263 gcc_assert (fde != NULL);
1264 if (fde->dw_fde_second_begin == NULL)
1265 fde->dw_fde_end = xstrdup (label);
1267 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1270 void
1271 dwarf2out_frame_finish (void)
1273 /* Output call frame information. */
1274 if (targetm.debug_unwind_info () == UI_DWARF2)
1275 output_call_frame_info (0);
1277 /* Output another copy for the unwinder. */
1278 if (do_eh_frame)
1279 output_call_frame_info (1);
1282 static void var_location_switch_text_section (void);
1283 static void set_cur_line_info_table (section *);
1285 void
1286 dwarf2out_switch_text_section (void)
1288 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1289 section *sect;
1290 dw_fde_ref fde = cfun->fde;
1292 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1294 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1295 current_function_funcdef_no);
1297 fde->dw_fde_second_begin = ggc_strdup (label);
1298 if (!in_cold_section_p)
1300 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1301 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1303 else
1305 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1306 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1308 have_multiple_function_sections = true;
1310 if (dwarf2out_do_cfi_asm ())
1311 fprintf (asm_out_file, "\t.cfi_endproc\n");
1313 mark_ignored_debug_section (fde, false);
1315 /* Now do the real section switch. */
1316 sect = current_function_section ();
1317 switch_to_section (sect);
1319 fde->second_in_std_section
1320 = (sect == text_section
1321 || (cold_text_section && sect == cold_text_section));
1322 in_text_section_p = sect == text_section;
1324 if (dwarf2out_do_cfi_asm ())
1325 dwarf2out_do_cfi_startproc (true);
1327 var_location_switch_text_section ();
1329 if (cold_text_section != NULL)
1330 set_cur_line_info_table (sect);
1333 /* And now, the subset of the debugging information support code necessary
1334 for emitting location expressions. */
1336 /* Describe an entry into the .debug_addr section. */
1338 enum ate_kind {
1339 ate_kind_rtx,
1340 ate_kind_rtx_dtprel,
1341 ate_kind_label
1344 struct GTY((for_user)) addr_table_entry {
1345 enum ate_kind kind;
1346 unsigned int refcount;
1347 unsigned int index;
1348 union addr_table_entry_struct_union
1350 rtx GTY ((tag ("0"))) rtl;
1351 char * GTY ((tag ("1"))) label;
1353 GTY ((desc ("%1.kind"))) addr;
1356 typedef unsigned int var_loc_view;
1358 /* Location lists are ranges + location descriptions for that range,
1359 so you can track variables that are in different places over
1360 their entire life. */
1361 typedef struct GTY(()) dw_loc_list_struct {
1362 dw_loc_list_ref dw_loc_next;
1363 const char *begin; /* Label and addr_entry for start of range */
1364 addr_table_entry *begin_entry;
1365 const char *end; /* Label for end of range */
1366 addr_table_entry *end_entry;
1367 char *ll_symbol; /* Label for beginning of location list.
1368 Only on head of list. */
1369 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1370 const char *section; /* Section this loclist is relative to */
1371 dw_loc_descr_ref expr;
1372 var_loc_view vbegin, vend;
1373 hashval_t hash;
1374 /* True if all addresses in this and subsequent lists are known to be
1375 resolved. */
1376 bool resolved_addr;
1377 /* True if this list has been replaced by dw_loc_next. */
1378 bool replaced;
1379 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1380 section. */
1381 unsigned char emitted : 1;
1382 /* True if hash field is index rather than hash value. */
1383 unsigned char num_assigned : 1;
1384 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1385 unsigned char offset_emitted : 1;
1386 /* True if note_variable_value_in_expr has been called on it. */
1387 unsigned char noted_variable_value : 1;
1388 /* True if the range should be emitted even if begin and end
1389 are the same. */
1390 bool force;
1391 } dw_loc_list_node;
1393 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1394 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1396 /* Convert a DWARF stack opcode into its string name. */
1398 static const char *
1399 dwarf_stack_op_name (unsigned int op)
1401 const char *name = get_DW_OP_name (op);
1403 if (name != NULL)
1404 return name;
1406 return "OP_<unknown>";
1409 /* Return TRUE iff we're to output location view lists as a separate
1410 attribute next to the location lists, as an extension compatible
1411 with DWARF 2 and above. */
1413 static inline bool
1414 dwarf2out_locviews_in_attribute ()
1416 return debug_variable_location_views == 1;
1419 /* Return TRUE iff we're to output location view lists as part of the
1420 location lists, as proposed for standardization after DWARF 5. */
1422 static inline bool
1423 dwarf2out_locviews_in_loclist ()
1425 #ifndef DW_LLE_view_pair
1426 return false;
1427 #else
1428 return debug_variable_location_views == -1;
1429 #endif
1432 /* Return a pointer to a newly allocated location description. Location
1433 descriptions are simple expression terms that can be strung
1434 together to form more complicated location (address) descriptions. */
1436 static inline dw_loc_descr_ref
1437 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1438 unsigned HOST_WIDE_INT oprnd2)
1440 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1442 descr->dw_loc_opc = op;
1443 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1444 descr->dw_loc_oprnd1.val_entry = NULL;
1445 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1446 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1447 descr->dw_loc_oprnd2.val_entry = NULL;
1448 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1450 return descr;
1453 /* Add a location description term to a location description expression. */
1455 static inline void
1456 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1458 dw_loc_descr_ref *d;
1460 /* Find the end of the chain. */
1461 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1464 *d = descr;
1467 /* Compare two location operands for exact equality. */
1469 static bool
1470 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1472 if (a->val_class != b->val_class)
1473 return false;
1474 switch (a->val_class)
1476 case dw_val_class_none:
1477 return true;
1478 case dw_val_class_addr:
1479 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1481 case dw_val_class_offset:
1482 case dw_val_class_unsigned_const:
1483 case dw_val_class_const:
1484 case dw_val_class_unsigned_const_implicit:
1485 case dw_val_class_const_implicit:
1486 case dw_val_class_range_list:
1487 /* These are all HOST_WIDE_INT, signed or unsigned. */
1488 return a->v.val_unsigned == b->v.val_unsigned;
1490 case dw_val_class_loc:
1491 return a->v.val_loc == b->v.val_loc;
1492 case dw_val_class_loc_list:
1493 return a->v.val_loc_list == b->v.val_loc_list;
1494 case dw_val_class_view_list:
1495 return a->v.val_view_list == b->v.val_view_list;
1496 case dw_val_class_die_ref:
1497 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1498 case dw_val_class_fde_ref:
1499 return a->v.val_fde_index == b->v.val_fde_index;
1500 case dw_val_class_symview:
1501 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1502 case dw_val_class_lbl_id:
1503 case dw_val_class_lineptr:
1504 case dw_val_class_macptr:
1505 case dw_val_class_loclistsptr:
1506 case dw_val_class_high_pc:
1507 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1508 case dw_val_class_str:
1509 return a->v.val_str == b->v.val_str;
1510 case dw_val_class_flag:
1511 return a->v.val_flag == b->v.val_flag;
1512 case dw_val_class_file:
1513 case dw_val_class_file_implicit:
1514 return a->v.val_file == b->v.val_file;
1515 case dw_val_class_decl_ref:
1516 return a->v.val_decl_ref == b->v.val_decl_ref;
1518 case dw_val_class_const_double:
1519 return (a->v.val_double.high == b->v.val_double.high
1520 && a->v.val_double.low == b->v.val_double.low);
1522 case dw_val_class_wide_int:
1523 return *a->v.val_wide == *b->v.val_wide;
1525 case dw_val_class_vec:
1527 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1528 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1530 return (a_len == b_len
1531 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1534 case dw_val_class_data8:
1535 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1537 case dw_val_class_vms_delta:
1538 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1539 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1541 case dw_val_class_discr_value:
1542 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1543 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1544 case dw_val_class_discr_list:
1545 /* It makes no sense comparing two discriminant value lists. */
1546 return false;
1548 gcc_unreachable ();
1551 /* Compare two location atoms for exact equality. */
1553 static bool
1554 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1556 if (a->dw_loc_opc != b->dw_loc_opc)
1557 return false;
1559 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1560 address size, but since we always allocate cleared storage it
1561 should be zero for other types of locations. */
1562 if (a->dtprel != b->dtprel)
1563 return false;
1565 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1566 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1569 /* Compare two complete location expressions for exact equality. */
1571 bool
1572 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1574 while (1)
1576 if (a == b)
1577 return true;
1578 if (a == NULL || b == NULL)
1579 return false;
1580 if (!loc_descr_equal_p_1 (a, b))
1581 return false;
1583 a = a->dw_loc_next;
1584 b = b->dw_loc_next;
1589 /* Add a constant POLY_OFFSET to a location expression. */
1591 static void
1592 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1594 dw_loc_descr_ref loc;
1595 HOST_WIDE_INT *p;
1597 gcc_assert (*list_head != NULL);
1599 if (known_eq (poly_offset, 0))
1600 return;
1602 /* Find the end of the chain. */
1603 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1606 HOST_WIDE_INT offset;
1607 if (!poly_offset.is_constant (&offset))
1609 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1610 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1611 return;
1614 p = NULL;
1615 if (loc->dw_loc_opc == DW_OP_fbreg
1616 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1617 p = &loc->dw_loc_oprnd1.v.val_int;
1618 else if (loc->dw_loc_opc == DW_OP_bregx)
1619 p = &loc->dw_loc_oprnd2.v.val_int;
1621 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1622 offset. Don't optimize if an signed integer overflow would happen. */
1623 if (p != NULL
1624 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1625 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1626 *p += offset;
1628 else if (offset > 0)
1629 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1631 else
1633 loc->dw_loc_next
1634 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1635 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1639 /* Return a pointer to a newly allocated location description for
1640 REG and OFFSET. */
1642 static inline dw_loc_descr_ref
1643 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1645 HOST_WIDE_INT const_offset;
1646 if (offset.is_constant (&const_offset))
1648 if (reg <= 31)
1649 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1650 const_offset, 0);
1651 else
1652 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1654 else
1656 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1657 loc_descr_plus_const (&ret, offset);
1658 return ret;
1662 /* Add a constant OFFSET to a location list. */
1664 static void
1665 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1667 dw_loc_list_ref d;
1668 for (d = list_head; d != NULL; d = d->dw_loc_next)
1669 loc_descr_plus_const (&d->expr, offset);
1672 #define DWARF_REF_SIZE \
1673 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1675 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1676 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1677 DW_FORM_data16 with 128 bits. */
1678 #define DWARF_LARGEST_DATA_FORM_BITS \
1679 (dwarf_version >= 5 ? 128 : 64)
1681 /* Utility inline function for construction of ops that were GNU extension
1682 before DWARF 5. */
1683 static inline enum dwarf_location_atom
1684 dwarf_OP (enum dwarf_location_atom op)
1686 switch (op)
1688 case DW_OP_implicit_pointer:
1689 if (dwarf_version < 5)
1690 return DW_OP_GNU_implicit_pointer;
1691 break;
1693 case DW_OP_entry_value:
1694 if (dwarf_version < 5)
1695 return DW_OP_GNU_entry_value;
1696 break;
1698 case DW_OP_const_type:
1699 if (dwarf_version < 5)
1700 return DW_OP_GNU_const_type;
1701 break;
1703 case DW_OP_regval_type:
1704 if (dwarf_version < 5)
1705 return DW_OP_GNU_regval_type;
1706 break;
1708 case DW_OP_deref_type:
1709 if (dwarf_version < 5)
1710 return DW_OP_GNU_deref_type;
1711 break;
1713 case DW_OP_convert:
1714 if (dwarf_version < 5)
1715 return DW_OP_GNU_convert;
1716 break;
1718 case DW_OP_reinterpret:
1719 if (dwarf_version < 5)
1720 return DW_OP_GNU_reinterpret;
1721 break;
1723 case DW_OP_addrx:
1724 if (dwarf_version < 5)
1725 return DW_OP_GNU_addr_index;
1726 break;
1728 case DW_OP_constx:
1729 if (dwarf_version < 5)
1730 return DW_OP_GNU_const_index;
1731 break;
1733 default:
1734 break;
1736 return op;
1739 /* Similarly for attributes. */
1740 static inline enum dwarf_attribute
1741 dwarf_AT (enum dwarf_attribute at)
1743 switch (at)
1745 case DW_AT_call_return_pc:
1746 if (dwarf_version < 5)
1747 return DW_AT_low_pc;
1748 break;
1750 case DW_AT_call_tail_call:
1751 if (dwarf_version < 5)
1752 return DW_AT_GNU_tail_call;
1753 break;
1755 case DW_AT_call_origin:
1756 if (dwarf_version < 5)
1757 return DW_AT_abstract_origin;
1758 break;
1760 case DW_AT_call_target:
1761 if (dwarf_version < 5)
1762 return DW_AT_GNU_call_site_target;
1763 break;
1765 case DW_AT_call_target_clobbered:
1766 if (dwarf_version < 5)
1767 return DW_AT_GNU_call_site_target_clobbered;
1768 break;
1770 case DW_AT_call_parameter:
1771 if (dwarf_version < 5)
1772 return DW_AT_abstract_origin;
1773 break;
1775 case DW_AT_call_value:
1776 if (dwarf_version < 5)
1777 return DW_AT_GNU_call_site_value;
1778 break;
1780 case DW_AT_call_data_value:
1781 if (dwarf_version < 5)
1782 return DW_AT_GNU_call_site_data_value;
1783 break;
1785 case DW_AT_call_all_calls:
1786 if (dwarf_version < 5)
1787 return DW_AT_GNU_all_call_sites;
1788 break;
1790 case DW_AT_call_all_tail_calls:
1791 if (dwarf_version < 5)
1792 return DW_AT_GNU_all_tail_call_sites;
1793 break;
1795 case DW_AT_dwo_name:
1796 if (dwarf_version < 5)
1797 return DW_AT_GNU_dwo_name;
1798 break;
1800 case DW_AT_addr_base:
1801 if (dwarf_version < 5)
1802 return DW_AT_GNU_addr_base;
1803 break;
1805 default:
1806 break;
1808 return at;
1811 /* And similarly for tags. */
1812 static inline enum dwarf_tag
1813 dwarf_TAG (enum dwarf_tag tag)
1815 switch (tag)
1817 case DW_TAG_call_site:
1818 if (dwarf_version < 5)
1819 return DW_TAG_GNU_call_site;
1820 break;
1822 case DW_TAG_call_site_parameter:
1823 if (dwarf_version < 5)
1824 return DW_TAG_GNU_call_site_parameter;
1825 break;
1827 default:
1828 break;
1830 return tag;
1833 /* And similarly for forms. */
1834 static inline enum dwarf_form
1835 dwarf_FORM (enum dwarf_form form)
1837 switch (form)
1839 case DW_FORM_addrx:
1840 if (dwarf_version < 5)
1841 return DW_FORM_GNU_addr_index;
1842 break;
1844 case DW_FORM_strx:
1845 if (dwarf_version < 5)
1846 return DW_FORM_GNU_str_index;
1847 break;
1849 default:
1850 break;
1852 return form;
1855 static unsigned long int get_base_type_offset (dw_die_ref);
1857 /* Return the size of a location descriptor. */
1859 static unsigned long
1860 size_of_loc_descr (dw_loc_descr_ref loc)
1862 unsigned long size = 1;
1864 switch (loc->dw_loc_opc)
1866 case DW_OP_addr:
1867 size += DWARF2_ADDR_SIZE;
1868 break;
1869 case DW_OP_GNU_addr_index:
1870 case DW_OP_addrx:
1871 case DW_OP_GNU_const_index:
1872 case DW_OP_constx:
1873 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1874 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1875 break;
1876 case DW_OP_const1u:
1877 case DW_OP_const1s:
1878 size += 1;
1879 break;
1880 case DW_OP_const2u:
1881 case DW_OP_const2s:
1882 size += 2;
1883 break;
1884 case DW_OP_const4u:
1885 case DW_OP_const4s:
1886 size += 4;
1887 break;
1888 case DW_OP_const8u:
1889 case DW_OP_const8s:
1890 size += 8;
1891 break;
1892 case DW_OP_constu:
1893 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1894 break;
1895 case DW_OP_consts:
1896 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1897 break;
1898 case DW_OP_pick:
1899 size += 1;
1900 break;
1901 case DW_OP_plus_uconst:
1902 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1903 break;
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 size += 2;
1907 break;
1908 case DW_OP_breg0:
1909 case DW_OP_breg1:
1910 case DW_OP_breg2:
1911 case DW_OP_breg3:
1912 case DW_OP_breg4:
1913 case DW_OP_breg5:
1914 case DW_OP_breg6:
1915 case DW_OP_breg7:
1916 case DW_OP_breg8:
1917 case DW_OP_breg9:
1918 case DW_OP_breg10:
1919 case DW_OP_breg11:
1920 case DW_OP_breg12:
1921 case DW_OP_breg13:
1922 case DW_OP_breg14:
1923 case DW_OP_breg15:
1924 case DW_OP_breg16:
1925 case DW_OP_breg17:
1926 case DW_OP_breg18:
1927 case DW_OP_breg19:
1928 case DW_OP_breg20:
1929 case DW_OP_breg21:
1930 case DW_OP_breg22:
1931 case DW_OP_breg23:
1932 case DW_OP_breg24:
1933 case DW_OP_breg25:
1934 case DW_OP_breg26:
1935 case DW_OP_breg27:
1936 case DW_OP_breg28:
1937 case DW_OP_breg29:
1938 case DW_OP_breg30:
1939 case DW_OP_breg31:
1940 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1941 break;
1942 case DW_OP_regx:
1943 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1944 break;
1945 case DW_OP_fbreg:
1946 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1947 break;
1948 case DW_OP_bregx:
1949 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1950 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1951 break;
1952 case DW_OP_piece:
1953 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1954 break;
1955 case DW_OP_bit_piece:
1956 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1957 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1958 break;
1959 case DW_OP_deref_size:
1960 case DW_OP_xderef_size:
1961 size += 1;
1962 break;
1963 case DW_OP_call2:
1964 size += 2;
1965 break;
1966 case DW_OP_call4:
1967 size += 4;
1968 break;
1969 case DW_OP_call_ref:
1970 case DW_OP_GNU_variable_value:
1971 size += DWARF_REF_SIZE;
1972 break;
1973 case DW_OP_implicit_value:
1974 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1975 + loc->dw_loc_oprnd1.v.val_unsigned;
1976 break;
1977 case DW_OP_implicit_pointer:
1978 case DW_OP_GNU_implicit_pointer:
1979 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1980 break;
1981 case DW_OP_entry_value:
1982 case DW_OP_GNU_entry_value:
1984 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1985 size += size_of_uleb128 (op_size) + op_size;
1986 break;
1988 case DW_OP_const_type:
1989 case DW_OP_GNU_const_type:
1991 unsigned long o
1992 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1993 size += size_of_uleb128 (o) + 1;
1994 switch (loc->dw_loc_oprnd2.val_class)
1996 case dw_val_class_vec:
1997 size += loc->dw_loc_oprnd2.v.val_vec.length
1998 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1999 break;
2000 case dw_val_class_const:
2001 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2002 break;
2003 case dw_val_class_const_double:
2004 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2005 break;
2006 case dw_val_class_wide_int:
2007 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2008 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2009 break;
2010 default:
2011 gcc_unreachable ();
2013 break;
2015 case DW_OP_regval_type:
2016 case DW_OP_GNU_regval_type:
2018 unsigned long o
2019 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2020 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2021 + size_of_uleb128 (o);
2023 break;
2024 case DW_OP_deref_type:
2025 case DW_OP_GNU_deref_type:
2027 unsigned long o
2028 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2029 size += 1 + size_of_uleb128 (o);
2031 break;
2032 case DW_OP_convert:
2033 case DW_OP_reinterpret:
2034 case DW_OP_GNU_convert:
2035 case DW_OP_GNU_reinterpret:
2036 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2037 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2038 else
2040 unsigned long o
2041 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2042 size += size_of_uleb128 (o);
2044 break;
2045 case DW_OP_GNU_parameter_ref:
2046 size += 4;
2047 break;
2048 default:
2049 break;
2052 return size;
2055 /* Return the size of a series of location descriptors. */
2057 unsigned long
2058 size_of_locs (dw_loc_descr_ref loc)
2060 dw_loc_descr_ref l;
2061 unsigned long size;
2063 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2064 field, to avoid writing to a PCH file. */
2065 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2067 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2068 break;
2069 size += size_of_loc_descr (l);
2071 if (! l)
2072 return size;
2074 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2076 l->dw_loc_addr = size;
2077 size += size_of_loc_descr (l);
2080 return size;
2083 /* Return the size of the value in a DW_AT_discr_value attribute. */
2085 static int
2086 size_of_discr_value (dw_discr_value *discr_value)
2088 if (discr_value->pos)
2089 return size_of_uleb128 (discr_value->v.uval);
2090 else
2091 return size_of_sleb128 (discr_value->v.sval);
2094 /* Return the size of the value in a DW_AT_discr_list attribute. */
2096 static int
2097 size_of_discr_list (dw_discr_list_ref discr_list)
2099 int size = 0;
2101 for (dw_discr_list_ref list = discr_list;
2102 list != NULL;
2103 list = list->dw_discr_next)
2105 /* One byte for the discriminant value descriptor, and then one or two
2106 LEB128 numbers, depending on whether it's a single case label or a
2107 range label. */
2108 size += 1;
2109 size += size_of_discr_value (&list->dw_discr_lower_bound);
2110 if (list->dw_discr_range != 0)
2111 size += size_of_discr_value (&list->dw_discr_upper_bound);
2113 return size;
2116 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2117 static void get_ref_die_offset_label (char *, dw_die_ref);
2118 static unsigned long int get_ref_die_offset (dw_die_ref);
2120 /* Output location description stack opcode's operands (if any).
2121 The for_eh_or_skip parameter controls whether register numbers are
2122 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2123 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2124 info). This should be suppressed for the cases that have not been converted
2125 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2127 static void
2128 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2130 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2131 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2133 switch (loc->dw_loc_opc)
2135 #ifdef DWARF2_DEBUGGING_INFO
2136 case DW_OP_const2u:
2137 case DW_OP_const2s:
2138 dw2_asm_output_data (2, val1->v.val_int, NULL);
2139 break;
2140 case DW_OP_const4u:
2141 if (loc->dtprel)
2143 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2144 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2145 val1->v.val_addr);
2146 fputc ('\n', asm_out_file);
2147 break;
2149 /* FALLTHRU */
2150 case DW_OP_const4s:
2151 dw2_asm_output_data (4, val1->v.val_int, NULL);
2152 break;
2153 case DW_OP_const8u:
2154 if (loc->dtprel)
2156 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2157 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2158 val1->v.val_addr);
2159 fputc ('\n', asm_out_file);
2160 break;
2162 /* FALLTHRU */
2163 case DW_OP_const8s:
2164 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2165 dw2_asm_output_data (8, val1->v.val_int, NULL);
2166 break;
2167 case DW_OP_skip:
2168 case DW_OP_bra:
2170 int offset;
2172 gcc_assert (val1->val_class == dw_val_class_loc);
2173 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2175 dw2_asm_output_data (2, offset, NULL);
2177 break;
2178 case DW_OP_implicit_value:
2179 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2180 switch (val2->val_class)
2182 case dw_val_class_const:
2183 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2184 break;
2185 case dw_val_class_vec:
2187 unsigned int elt_size = val2->v.val_vec.elt_size;
2188 unsigned int len = val2->v.val_vec.length;
2189 unsigned int i;
2190 unsigned char *p;
2192 if (elt_size > sizeof (HOST_WIDE_INT))
2194 elt_size /= 2;
2195 len *= 2;
2197 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2198 i < len;
2199 i++, p += elt_size)
2200 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2201 "fp or vector constant word %u", i);
2203 break;
2204 case dw_val_class_const_double:
2206 unsigned HOST_WIDE_INT first, second;
2208 if (WORDS_BIG_ENDIAN)
2210 first = val2->v.val_double.high;
2211 second = val2->v.val_double.low;
2213 else
2215 first = val2->v.val_double.low;
2216 second = val2->v.val_double.high;
2218 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2219 first, NULL);
2220 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2221 second, NULL);
2223 break;
2224 case dw_val_class_wide_int:
2226 int i;
2227 int len = get_full_len (*val2->v.val_wide);
2228 if (WORDS_BIG_ENDIAN)
2229 for (i = len - 1; i >= 0; --i)
2230 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2231 val2->v.val_wide->elt (i), NULL);
2232 else
2233 for (i = 0; i < len; ++i)
2234 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2235 val2->v.val_wide->elt (i), NULL);
2237 break;
2238 case dw_val_class_addr:
2239 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2240 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2241 break;
2242 default:
2243 gcc_unreachable ();
2245 break;
2246 #else
2247 case DW_OP_const2u:
2248 case DW_OP_const2s:
2249 case DW_OP_const4u:
2250 case DW_OP_const4s:
2251 case DW_OP_const8u:
2252 case DW_OP_const8s:
2253 case DW_OP_skip:
2254 case DW_OP_bra:
2255 case DW_OP_implicit_value:
2256 /* We currently don't make any attempt to make sure these are
2257 aligned properly like we do for the main unwind info, so
2258 don't support emitting things larger than a byte if we're
2259 only doing unwinding. */
2260 gcc_unreachable ();
2261 #endif
2262 case DW_OP_const1u:
2263 case DW_OP_const1s:
2264 dw2_asm_output_data (1, val1->v.val_int, NULL);
2265 break;
2266 case DW_OP_constu:
2267 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2268 break;
2269 case DW_OP_consts:
2270 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2271 break;
2272 case DW_OP_pick:
2273 dw2_asm_output_data (1, val1->v.val_int, NULL);
2274 break;
2275 case DW_OP_plus_uconst:
2276 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2277 break;
2278 case DW_OP_breg0:
2279 case DW_OP_breg1:
2280 case DW_OP_breg2:
2281 case DW_OP_breg3:
2282 case DW_OP_breg4:
2283 case DW_OP_breg5:
2284 case DW_OP_breg6:
2285 case DW_OP_breg7:
2286 case DW_OP_breg8:
2287 case DW_OP_breg9:
2288 case DW_OP_breg10:
2289 case DW_OP_breg11:
2290 case DW_OP_breg12:
2291 case DW_OP_breg13:
2292 case DW_OP_breg14:
2293 case DW_OP_breg15:
2294 case DW_OP_breg16:
2295 case DW_OP_breg17:
2296 case DW_OP_breg18:
2297 case DW_OP_breg19:
2298 case DW_OP_breg20:
2299 case DW_OP_breg21:
2300 case DW_OP_breg22:
2301 case DW_OP_breg23:
2302 case DW_OP_breg24:
2303 case DW_OP_breg25:
2304 case DW_OP_breg26:
2305 case DW_OP_breg27:
2306 case DW_OP_breg28:
2307 case DW_OP_breg29:
2308 case DW_OP_breg30:
2309 case DW_OP_breg31:
2310 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2311 break;
2312 case DW_OP_regx:
2314 unsigned r = val1->v.val_unsigned;
2315 if (for_eh_or_skip >= 0)
2316 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 dw2_asm_output_data_uleb128 (r, NULL);
2321 break;
2322 case DW_OP_fbreg:
2323 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2324 break;
2325 case DW_OP_bregx:
2327 unsigned r = val1->v.val_unsigned;
2328 if (for_eh_or_skip >= 0)
2329 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2330 gcc_assert (size_of_uleb128 (r)
2331 == size_of_uleb128 (val1->v.val_unsigned));
2332 dw2_asm_output_data_uleb128 (r, NULL);
2333 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2335 break;
2336 case DW_OP_piece:
2337 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2338 break;
2339 case DW_OP_bit_piece:
2340 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2341 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2342 break;
2343 case DW_OP_deref_size:
2344 case DW_OP_xderef_size:
2345 dw2_asm_output_data (1, val1->v.val_int, NULL);
2346 break;
2348 case DW_OP_addr:
2349 if (loc->dtprel)
2351 if (targetm.asm_out.output_dwarf_dtprel)
2353 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2354 DWARF2_ADDR_SIZE,
2355 val1->v.val_addr);
2356 fputc ('\n', asm_out_file);
2358 else
2359 gcc_unreachable ();
2361 else
2363 #ifdef DWARF2_DEBUGGING_INFO
2364 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2365 #else
2366 gcc_unreachable ();
2367 #endif
2369 break;
2371 case DW_OP_GNU_addr_index:
2372 case DW_OP_addrx:
2373 case DW_OP_GNU_const_index:
2374 case DW_OP_constx:
2375 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2376 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2377 "(index into .debug_addr)");
2378 break;
2380 case DW_OP_call2:
2381 case DW_OP_call4:
2383 unsigned long die_offset
2384 = get_ref_die_offset (val1->v.val_die_ref.die);
2385 /* Make sure the offset has been computed and that we can encode it as
2386 an operand. */
2387 gcc_assert (die_offset > 0
2388 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2389 ? 0xffff
2390 : 0xffffffff));
2391 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2392 die_offset, NULL);
2394 break;
2396 case DW_OP_call_ref:
2397 case DW_OP_GNU_variable_value:
2399 char label[MAX_ARTIFICIAL_LABEL_BYTES
2400 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2401 gcc_assert (val1->val_class == dw_val_class_die_ref);
2402 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2403 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2405 break;
2407 case DW_OP_implicit_pointer:
2408 case DW_OP_GNU_implicit_pointer:
2410 char label[MAX_ARTIFICIAL_LABEL_BYTES
2411 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2412 gcc_assert (val1->val_class == dw_val_class_die_ref);
2413 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2414 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2415 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2417 break;
2419 case DW_OP_entry_value:
2420 case DW_OP_GNU_entry_value:
2421 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2422 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2423 break;
2425 case DW_OP_const_type:
2426 case DW_OP_GNU_const_type:
2428 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2429 gcc_assert (o);
2430 dw2_asm_output_data_uleb128 (o, NULL);
2431 switch (val2->val_class)
2433 case dw_val_class_const:
2434 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2435 dw2_asm_output_data (1, l, NULL);
2436 dw2_asm_output_data (l, val2->v.val_int, NULL);
2437 break;
2438 case dw_val_class_vec:
2440 unsigned int elt_size = val2->v.val_vec.elt_size;
2441 unsigned int len = val2->v.val_vec.length;
2442 unsigned int i;
2443 unsigned char *p;
2445 l = len * elt_size;
2446 dw2_asm_output_data (1, l, NULL);
2447 if (elt_size > sizeof (HOST_WIDE_INT))
2449 elt_size /= 2;
2450 len *= 2;
2452 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2453 i < len;
2454 i++, p += elt_size)
2455 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2456 "fp or vector constant word %u", i);
2458 break;
2459 case dw_val_class_const_double:
2461 unsigned HOST_WIDE_INT first, second;
2462 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2464 dw2_asm_output_data (1, 2 * l, NULL);
2465 if (WORDS_BIG_ENDIAN)
2467 first = val2->v.val_double.high;
2468 second = val2->v.val_double.low;
2470 else
2472 first = val2->v.val_double.low;
2473 second = val2->v.val_double.high;
2475 dw2_asm_output_data (l, first, NULL);
2476 dw2_asm_output_data (l, second, NULL);
2478 break;
2479 case dw_val_class_wide_int:
2481 int i;
2482 int len = get_full_len (*val2->v.val_wide);
2483 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2485 dw2_asm_output_data (1, len * l, NULL);
2486 if (WORDS_BIG_ENDIAN)
2487 for (i = len - 1; i >= 0; --i)
2488 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2489 else
2490 for (i = 0; i < len; ++i)
2491 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2493 break;
2494 default:
2495 gcc_unreachable ();
2498 break;
2499 case DW_OP_regval_type:
2500 case DW_OP_GNU_regval_type:
2502 unsigned r = val1->v.val_unsigned;
2503 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2504 gcc_assert (o);
2505 if (for_eh_or_skip >= 0)
2507 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2508 gcc_assert (size_of_uleb128 (r)
2509 == size_of_uleb128 (val1->v.val_unsigned));
2511 dw2_asm_output_data_uleb128 (r, NULL);
2512 dw2_asm_output_data_uleb128 (o, NULL);
2514 break;
2515 case DW_OP_deref_type:
2516 case DW_OP_GNU_deref_type:
2518 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2519 gcc_assert (o);
2520 dw2_asm_output_data (1, val1->v.val_int, NULL);
2521 dw2_asm_output_data_uleb128 (o, NULL);
2523 break;
2524 case DW_OP_convert:
2525 case DW_OP_reinterpret:
2526 case DW_OP_GNU_convert:
2527 case DW_OP_GNU_reinterpret:
2528 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2529 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2530 else
2532 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2533 gcc_assert (o);
2534 dw2_asm_output_data_uleb128 (o, NULL);
2536 break;
2538 case DW_OP_GNU_parameter_ref:
2540 unsigned long o;
2541 gcc_assert (val1->val_class == dw_val_class_die_ref);
2542 o = get_ref_die_offset (val1->v.val_die_ref.die);
2543 dw2_asm_output_data (4, o, NULL);
2545 break;
2547 default:
2548 /* Other codes have no operands. */
2549 break;
2553 /* Output a sequence of location operations.
2554 The for_eh_or_skip parameter controls whether register numbers are
2555 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2556 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2557 info). This should be suppressed for the cases that have not been converted
2558 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2560 void
2561 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2563 for (; loc != NULL; loc = loc->dw_loc_next)
2565 enum dwarf_location_atom opc = loc->dw_loc_opc;
2566 /* Output the opcode. */
2567 if (for_eh_or_skip >= 0
2568 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2570 unsigned r = (opc - DW_OP_breg0);
2571 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2572 gcc_assert (r <= 31);
2573 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2575 else if (for_eh_or_skip >= 0
2576 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2578 unsigned r = (opc - DW_OP_reg0);
2579 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2580 gcc_assert (r <= 31);
2581 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2584 dw2_asm_output_data (1, opc,
2585 "%s", dwarf_stack_op_name (opc));
2587 /* Output the operand(s) (if any). */
2588 output_loc_operands (loc, for_eh_or_skip);
2592 /* Output location description stack opcode's operands (if any).
2593 The output is single bytes on a line, suitable for .cfi_escape. */
2595 static void
2596 output_loc_operands_raw (dw_loc_descr_ref loc)
2598 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2599 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2601 switch (loc->dw_loc_opc)
2603 case DW_OP_addr:
2604 case DW_OP_GNU_addr_index:
2605 case DW_OP_addrx:
2606 case DW_OP_GNU_const_index:
2607 case DW_OP_constx:
2608 case DW_OP_implicit_value:
2609 /* We cannot output addresses in .cfi_escape, only bytes. */
2610 gcc_unreachable ();
2612 case DW_OP_const1u:
2613 case DW_OP_const1s:
2614 case DW_OP_pick:
2615 case DW_OP_deref_size:
2616 case DW_OP_xderef_size:
2617 fputc (',', asm_out_file);
2618 dw2_asm_output_data_raw (1, val1->v.val_int);
2619 break;
2621 case DW_OP_const2u:
2622 case DW_OP_const2s:
2623 fputc (',', asm_out_file);
2624 dw2_asm_output_data_raw (2, val1->v.val_int);
2625 break;
2627 case DW_OP_const4u:
2628 case DW_OP_const4s:
2629 fputc (',', asm_out_file);
2630 dw2_asm_output_data_raw (4, val1->v.val_int);
2631 break;
2633 case DW_OP_const8u:
2634 case DW_OP_const8s:
2635 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2636 fputc (',', asm_out_file);
2637 dw2_asm_output_data_raw (8, val1->v.val_int);
2638 break;
2640 case DW_OP_skip:
2641 case DW_OP_bra:
2643 int offset;
2645 gcc_assert (val1->val_class == dw_val_class_loc);
2646 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_raw (2, offset);
2651 break;
2653 case DW_OP_regx:
2655 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2656 gcc_assert (size_of_uleb128 (r)
2657 == size_of_uleb128 (val1->v.val_unsigned));
2658 fputc (',', asm_out_file);
2659 dw2_asm_output_data_uleb128_raw (r);
2661 break;
2663 case DW_OP_constu:
2664 case DW_OP_plus_uconst:
2665 case DW_OP_piece:
2666 fputc (',', asm_out_file);
2667 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2668 break;
2670 case DW_OP_bit_piece:
2671 fputc (',', asm_out_file);
2672 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2673 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2674 break;
2676 case DW_OP_consts:
2677 case DW_OP_breg0:
2678 case DW_OP_breg1:
2679 case DW_OP_breg2:
2680 case DW_OP_breg3:
2681 case DW_OP_breg4:
2682 case DW_OP_breg5:
2683 case DW_OP_breg6:
2684 case DW_OP_breg7:
2685 case DW_OP_breg8:
2686 case DW_OP_breg9:
2687 case DW_OP_breg10:
2688 case DW_OP_breg11:
2689 case DW_OP_breg12:
2690 case DW_OP_breg13:
2691 case DW_OP_breg14:
2692 case DW_OP_breg15:
2693 case DW_OP_breg16:
2694 case DW_OP_breg17:
2695 case DW_OP_breg18:
2696 case DW_OP_breg19:
2697 case DW_OP_breg20:
2698 case DW_OP_breg21:
2699 case DW_OP_breg22:
2700 case DW_OP_breg23:
2701 case DW_OP_breg24:
2702 case DW_OP_breg25:
2703 case DW_OP_breg26:
2704 case DW_OP_breg27:
2705 case DW_OP_breg28:
2706 case DW_OP_breg29:
2707 case DW_OP_breg30:
2708 case DW_OP_breg31:
2709 case DW_OP_fbreg:
2710 fputc (',', asm_out_file);
2711 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2712 break;
2714 case DW_OP_bregx:
2716 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2717 gcc_assert (size_of_uleb128 (r)
2718 == size_of_uleb128 (val1->v.val_unsigned));
2719 fputc (',', asm_out_file);
2720 dw2_asm_output_data_uleb128_raw (r);
2721 fputc (',', asm_out_file);
2722 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2724 break;
2726 case DW_OP_implicit_pointer:
2727 case DW_OP_entry_value:
2728 case DW_OP_const_type:
2729 case DW_OP_regval_type:
2730 case DW_OP_deref_type:
2731 case DW_OP_convert:
2732 case DW_OP_reinterpret:
2733 case DW_OP_GNU_implicit_pointer:
2734 case DW_OP_GNU_entry_value:
2735 case DW_OP_GNU_const_type:
2736 case DW_OP_GNU_regval_type:
2737 case DW_OP_GNU_deref_type:
2738 case DW_OP_GNU_convert:
2739 case DW_OP_GNU_reinterpret:
2740 case DW_OP_GNU_parameter_ref:
2741 gcc_unreachable ();
2742 break;
2744 default:
2745 /* Other codes have no operands. */
2746 break;
2750 void
2751 output_loc_sequence_raw (dw_loc_descr_ref loc)
2753 while (1)
2755 enum dwarf_location_atom opc = loc->dw_loc_opc;
2756 /* Output the opcode. */
2757 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2759 unsigned r = (opc - DW_OP_breg0);
2760 r = DWARF2_FRAME_REG_OUT (r, 1);
2761 gcc_assert (r <= 31);
2762 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2764 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2766 unsigned r = (opc - DW_OP_reg0);
2767 r = DWARF2_FRAME_REG_OUT (r, 1);
2768 gcc_assert (r <= 31);
2769 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2771 /* Output the opcode. */
2772 fprintf (asm_out_file, "%#x", opc);
2773 output_loc_operands_raw (loc);
2775 if (!loc->dw_loc_next)
2776 break;
2777 loc = loc->dw_loc_next;
2779 fputc (',', asm_out_file);
2783 static void
2784 build_breg_loc (struct dw_loc_descr_node **head, unsigned int regno)
2786 if (regno <= 31)
2787 add_loc_descr (head, new_loc_descr ((enum dwarf_location_atom)
2788 (DW_OP_breg0 + regno), 0, 0));
2789 else
2790 add_loc_descr (head, new_loc_descr (DW_OP_bregx, regno, 0));
2793 /* Build a dwarf location for a cfa_reg spanning multiple
2794 consecutive registers. */
2796 struct dw_loc_descr_node *
2797 build_span_loc (struct cfa_reg reg)
2799 struct dw_loc_descr_node *head = NULL;
2801 gcc_assert (reg.span_width > 0);
2802 gcc_assert (reg.span > 1);
2804 /* Start from the highest number register as it goes in the upper bits. */
2805 unsigned int regno = reg.reg + reg.span - 1;
2806 build_breg_loc (&head, regno);
2808 /* Deal with the remaining registers in the span. */
2809 for (int i = reg.span - 2; i >= 0; i--)
2811 add_loc_descr (&head, int_loc_descriptor (reg.span_width * 8));
2812 add_loc_descr (&head, new_loc_descr (DW_OP_shl, 0, 0));
2813 regno--;
2814 build_breg_loc (&head, regno);
2815 add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
2817 return head;
2820 /* This function builds a dwarf location descriptor sequence from a
2821 dw_cfa_location, adding the given OFFSET to the result of the
2822 expression. */
2824 struct dw_loc_descr_node *
2825 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2827 struct dw_loc_descr_node *head, *tmp;
2829 offset += cfa->offset;
2831 if (cfa->reg.span > 1)
2833 head = build_span_loc (cfa->reg);
2835 if (maybe_ne (offset, 0))
2836 loc_descr_plus_const (&head, offset);
2838 else if (cfa->indirect)
2840 head = new_reg_loc_descr (cfa->reg.reg, cfa->base_offset);
2841 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2842 head->dw_loc_oprnd1.val_entry = NULL;
2843 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2844 add_loc_descr (&head, tmp);
2845 loc_descr_plus_const (&head, offset);
2847 else
2848 head = new_reg_loc_descr (cfa->reg.reg, offset);
2850 return head;
2853 /* This function builds a dwarf location descriptor sequence for
2854 the address at OFFSET from the CFA when stack is aligned to
2855 ALIGNMENT byte. */
2857 struct dw_loc_descr_node *
2858 build_cfa_aligned_loc (dw_cfa_location *cfa,
2859 poly_int64 offset, HOST_WIDE_INT alignment)
2861 struct dw_loc_descr_node *head;
2862 unsigned int dwarf_fp
2863 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2865 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2866 if (cfa->reg.reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2868 head = new_reg_loc_descr (dwarf_fp, 0);
2869 add_loc_descr (&head, int_loc_descriptor (alignment));
2870 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2871 loc_descr_plus_const (&head, offset);
2873 else
2874 head = new_reg_loc_descr (dwarf_fp, offset);
2875 return head;
2878 /* And now, the support for symbolic debugging information. */
2880 /* .debug_str support. */
2882 static void dwarf2out_init (const char *);
2883 static void dwarf2out_finish (const char *);
2884 static void dwarf2out_early_finish (const char *);
2885 static void dwarf2out_assembly_start (void);
2886 static void dwarf2out_define (unsigned int, const char *);
2887 static void dwarf2out_undef (unsigned int, const char *);
2888 static void dwarf2out_start_source_file (unsigned, const char *);
2889 static void dwarf2out_end_source_file (unsigned);
2890 static void dwarf2out_function_decl (tree);
2891 static void dwarf2out_begin_block (unsigned, unsigned);
2892 static void dwarf2out_end_block (unsigned, unsigned);
2893 static bool dwarf2out_ignore_block (const_tree);
2894 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2895 static void dwarf2out_early_global_decl (tree);
2896 static void dwarf2out_late_global_decl (tree);
2897 static void dwarf2out_type_decl (tree, int);
2898 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2899 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2900 dw_die_ref);
2901 static void dwarf2out_abstract_function (tree);
2902 static void dwarf2out_var_location (rtx_insn *);
2903 static void dwarf2out_inline_entry (tree);
2904 static void dwarf2out_size_function (tree);
2905 static void dwarf2out_begin_function (tree);
2906 static void dwarf2out_end_function (unsigned int);
2907 static void dwarf2out_register_main_translation_unit (tree unit);
2908 static void dwarf2out_set_name (tree, tree);
2909 static void dwarf2out_register_external_die (tree decl, const char *sym,
2910 unsigned HOST_WIDE_INT off);
2911 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2912 unsigned HOST_WIDE_INT *off);
2914 /* The debug hooks structure. */
2916 const struct gcc_debug_hooks dwarf2_debug_hooks =
2918 dwarf2out_init,
2919 dwarf2out_finish,
2920 dwarf2out_early_finish,
2921 dwarf2out_assembly_start,
2922 dwarf2out_define,
2923 dwarf2out_undef,
2924 dwarf2out_start_source_file,
2925 dwarf2out_end_source_file,
2926 dwarf2out_begin_block,
2927 dwarf2out_end_block,
2928 dwarf2out_ignore_block,
2929 dwarf2out_source_line,
2930 dwarf2out_set_ignored_loc,
2931 dwarf2out_begin_prologue,
2932 #if VMS_DEBUGGING_INFO
2933 dwarf2out_vms_end_prologue,
2934 dwarf2out_vms_begin_epilogue,
2935 #else
2936 debug_nothing_int_charstar,
2937 debug_nothing_int_charstar,
2938 #endif
2939 dwarf2out_end_epilogue,
2940 dwarf2out_begin_function,
2941 dwarf2out_end_function, /* end_function */
2942 dwarf2out_register_main_translation_unit,
2943 dwarf2out_function_decl, /* function_decl */
2944 dwarf2out_early_global_decl,
2945 dwarf2out_late_global_decl,
2946 dwarf2out_type_decl, /* type_decl */
2947 dwarf2out_imported_module_or_decl,
2948 dwarf2out_die_ref_for_decl,
2949 dwarf2out_register_external_die,
2950 debug_nothing_tree, /* deferred_inline_function */
2951 /* The DWARF 2 backend tries to reduce debugging bloat by not
2952 emitting the abstract description of inline functions until
2953 something tries to reference them. */
2954 dwarf2out_abstract_function, /* outlining_inline_function */
2955 debug_nothing_rtx_code_label, /* label */
2956 debug_nothing_int, /* handle_pch */
2957 dwarf2out_var_location,
2958 dwarf2out_inline_entry, /* inline_entry */
2959 dwarf2out_size_function, /* size_function */
2960 dwarf2out_switch_text_section,
2961 dwarf2out_set_name,
2962 1, /* start_end_main_source_file */
2963 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2966 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2968 dwarf2out_init,
2969 debug_nothing_charstar,
2970 debug_nothing_charstar,
2971 dwarf2out_assembly_start,
2972 debug_nothing_int_charstar,
2973 debug_nothing_int_charstar,
2974 debug_nothing_int_charstar,
2975 debug_nothing_int,
2976 debug_nothing_int_int, /* begin_block */
2977 debug_nothing_int_int, /* end_block */
2978 debug_true_const_tree, /* ignore_block */
2979 dwarf2out_source_line, /* source_line */
2980 debug_nothing_int_int_charstar, /* set_ignored_loc */
2981 debug_nothing_int_int_charstar, /* begin_prologue */
2982 debug_nothing_int_charstar, /* end_prologue */
2983 debug_nothing_int_charstar, /* begin_epilogue */
2984 debug_nothing_int_charstar, /* end_epilogue */
2985 debug_nothing_tree, /* begin_function */
2986 debug_nothing_int, /* end_function */
2987 debug_nothing_tree, /* register_main_translation_unit */
2988 debug_nothing_tree, /* function_decl */
2989 debug_nothing_tree, /* early_global_decl */
2990 debug_nothing_tree, /* late_global_decl */
2991 debug_nothing_tree_int, /* type_decl */
2992 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2993 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2994 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2995 debug_nothing_tree, /* deferred_inline_function */
2996 debug_nothing_tree, /* outlining_inline_function */
2997 debug_nothing_rtx_code_label, /* label */
2998 debug_nothing_int, /* handle_pch */
2999 debug_nothing_rtx_insn, /* var_location */
3000 debug_nothing_tree, /* inline_entry */
3001 debug_nothing_tree, /* size_function */
3002 debug_nothing_void, /* switch_text_section */
3003 debug_nothing_tree_tree, /* set_name */
3004 0, /* start_end_main_source_file */
3005 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
3008 /* NOTE: In the comments in this file, many references are made to
3009 "Debugging Information Entries". This term is abbreviated as `DIE'
3010 throughout the remainder of this file. */
3012 /* An internal representation of the DWARF output is built, and then
3013 walked to generate the DWARF debugging info. The walk of the internal
3014 representation is done after the entire program has been compiled.
3015 The types below are used to describe the internal representation. */
3017 /* Whether to put type DIEs into their own section .debug_types instead
3018 of making them part of the .debug_info section. Only supported for
3019 Dwarf V4 or higher and the user didn't disable them through
3020 -fno-debug-types-section. It is more efficient to put them in a
3021 separate comdat sections since the linker will then be able to
3022 remove duplicates. But not all tools support .debug_types sections
3023 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
3024 it is DW_UT_type unit type in .debug_info section. For late LTO
3025 debug there should be almost no types emitted so avoid enabling
3026 -fdebug-types-section there. */
3028 #define use_debug_types (dwarf_version >= 4 \
3029 && flag_debug_types_section \
3030 && !in_lto_p)
3032 /* Various DIE's use offsets relative to the beginning of the
3033 .debug_info section to refer to each other. */
3035 typedef long int dw_offset;
3037 struct comdat_type_node;
3039 /* The entries in the line_info table more-or-less mirror the opcodes
3040 that are used in the real dwarf line table. Arrays of these entries
3041 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3042 supported. */
3044 enum dw_line_info_opcode {
3045 /* Emit DW_LNE_set_address; the operand is the label index. */
3046 LI_set_address,
3048 /* Emit a row to the matrix with the given line. This may be done
3049 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3050 special opcodes. */
3051 LI_set_line,
3053 /* Emit a DW_LNS_set_file. */
3054 LI_set_file,
3056 /* Emit a DW_LNS_set_column. */
3057 LI_set_column,
3059 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3060 LI_negate_stmt,
3062 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3063 LI_set_prologue_end,
3064 LI_set_epilogue_begin,
3066 /* Emit a DW_LNE_set_discriminator. */
3067 LI_set_discriminator,
3069 /* Output a Fixed Advance PC; the target PC is the label index; the
3070 base PC is the previous LI_adv_address or LI_set_address entry.
3071 We only use this when emitting debug views without assembler
3072 support, at explicit user request. Ideally, we should only use
3073 it when the offset might be zero but we can't tell: it's the only
3074 way to maybe change the PC without resetting the view number. */
3075 LI_adv_address
3078 typedef struct GTY(()) dw_line_info_struct {
3079 enum dw_line_info_opcode opcode;
3080 unsigned int val;
3081 } dw_line_info_entry;
3084 struct GTY(()) dw_line_info_table {
3085 /* The label that marks the end of this section. */
3086 const char *end_label;
3088 /* The values for the last row of the matrix, as collected in the table.
3089 These are used to minimize the changes to the next row. */
3090 unsigned int file_num;
3091 unsigned int line_num;
3092 unsigned int column_num;
3093 int discrim_num;
3094 bool is_stmt;
3095 bool in_use;
3097 /* This denotes the NEXT view number.
3099 If it is 0, it is known that the NEXT view will be the first view
3100 at the given PC.
3102 If it is -1, we're forcing the view number to be reset, e.g. at a
3103 function entry.
3105 The meaning of other nonzero values depends on whether we're
3106 computing views internally or leaving it for the assembler to do
3107 so. If we're emitting them internally, view denotes the view
3108 number since the last known advance of PC. If we're leaving it
3109 for the assembler, it denotes the LVU label number that we're
3110 going to ask the assembler to assign. */
3111 var_loc_view view;
3113 /* This counts the number of symbolic views emitted in this table
3114 since the latest view reset. Its max value, over all tables,
3115 sets symview_upper_bound. */
3116 var_loc_view symviews_since_reset;
3118 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3119 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3120 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3121 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3123 vec<dw_line_info_entry, va_gc> *entries;
3126 /* This is an upper bound for view numbers that the assembler may
3127 assign to symbolic views output in this translation. It is used to
3128 decide how big a field to use to represent view numbers in
3129 symview-classed attributes. */
3131 static var_loc_view symview_upper_bound;
3133 /* If we're keep track of location views and their reset points, and
3134 INSN is a reset point (i.e., it necessarily advances the PC), mark
3135 the next view in TABLE as reset. */
3137 static void
3138 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3140 if (!debug_internal_reset_location_views)
3141 return;
3143 /* Maybe turn (part of?) this test into a default target hook. */
3144 int reset = 0;
3146 if (targetm.reset_location_view)
3147 reset = targetm.reset_location_view (insn);
3149 if (reset)
3151 else if (JUMP_TABLE_DATA_P (insn))
3152 reset = 1;
3153 else if (GET_CODE (insn) == USE
3154 || GET_CODE (insn) == CLOBBER
3155 || GET_CODE (insn) == ASM_INPUT
3156 || asm_noperands (insn) >= 0)
3158 else if (get_attr_min_length (insn) > 0)
3159 reset = 1;
3161 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3162 RESET_NEXT_VIEW (table->view);
3165 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3166 The children of each node form a circular list linked by
3167 die_sib. die_child points to the node *before* the "first" child node. */
3169 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3170 union die_symbol_or_type_node
3172 const char * GTY ((tag ("0"))) die_symbol;
3173 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3175 GTY ((desc ("%0.comdat_type_p"))) die_id;
3176 vec<dw_attr_node, va_gc> *die_attr;
3177 dw_die_ref die_parent;
3178 dw_die_ref die_child;
3179 dw_die_ref die_sib;
3180 dw_die_ref die_definition; /* ref from a specification to its definition */
3181 dw_offset die_offset;
3182 unsigned long die_abbrev;
3183 int die_mark;
3184 unsigned int decl_id;
3185 enum dwarf_tag die_tag;
3186 /* Die is used and must not be pruned as unused. */
3187 BOOL_BITFIELD die_perennial_p : 1;
3188 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3189 /* For an external ref to die_symbol if die_offset contains an extra
3190 offset to that symbol. */
3191 BOOL_BITFIELD with_offset : 1;
3192 /* Whether this DIE was removed from the DIE tree, for example via
3193 prune_unused_types. We don't consider those present from the
3194 DIE lookup routines. */
3195 BOOL_BITFIELD removed : 1;
3196 /* Lots of spare bits. */
3198 die_node;
3200 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3201 static bool early_dwarf;
3202 static bool early_dwarf_finished;
3203 class set_early_dwarf {
3204 public:
3205 bool saved;
3206 set_early_dwarf () : saved(early_dwarf)
3208 gcc_assert (! early_dwarf_finished);
3209 early_dwarf = true;
3211 ~set_early_dwarf () { early_dwarf = saved; }
3214 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3215 #define FOR_EACH_CHILD(die, c, expr) do { \
3216 c = die->die_child; \
3217 if (c) do { \
3218 c = c->die_sib; \
3219 expr; \
3220 } while (c != die->die_child); \
3221 } while (0)
3223 /* The pubname structure */
3225 typedef struct GTY(()) pubname_struct {
3226 dw_die_ref die;
3227 const char *name;
3229 pubname_entry;
3232 struct GTY(()) dw_ranges {
3233 const char *label;
3234 /* If this is positive, it's a block number, otherwise it's a
3235 bitwise-negated index into dw_ranges_by_label. */
3236 int num;
3237 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3238 into .debug_rnglists section rather than .debug_rnglists.dwo
3239 for -gsplit-dwarf and DWARF >= 5. */
3240 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3241 /* Index for the range list for DW_FORM_rnglistx. */
3242 unsigned int idx : 31;
3243 /* True if this range might be possibly in a different section
3244 from previous entry. */
3245 unsigned int maybe_new_sec : 1;
3246 addr_table_entry *begin_entry;
3247 addr_table_entry *end_entry;
3250 /* A structure to hold a macinfo entry. */
3252 typedef struct GTY(()) macinfo_struct {
3253 unsigned char code;
3254 unsigned HOST_WIDE_INT lineno;
3255 const char *info;
3257 macinfo_entry;
3260 struct GTY(()) dw_ranges_by_label {
3261 const char *begin;
3262 const char *end;
3265 /* The comdat type node structure. */
3266 struct GTY(()) comdat_type_node
3268 dw_die_ref root_die;
3269 dw_die_ref type_die;
3270 dw_die_ref skeleton_die;
3271 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3272 comdat_type_node *next;
3275 /* A list of DIEs for which we can't determine ancestry (parent_die
3276 field) just yet. Later in dwarf2out_finish we will fill in the
3277 missing bits. */
3278 typedef struct GTY(()) limbo_die_struct {
3279 dw_die_ref die;
3280 /* The tree for which this DIE was created. We use this to
3281 determine ancestry later. */
3282 tree created_for;
3283 struct limbo_die_struct *next;
3285 limbo_die_node;
3287 typedef struct skeleton_chain_struct
3289 dw_die_ref old_die;
3290 dw_die_ref new_die;
3291 struct skeleton_chain_struct *parent;
3293 skeleton_chain_node;
3295 /* Define a macro which returns nonzero for a TYPE_DECL which was
3296 implicitly generated for a type.
3298 Note that, unlike the C front-end (which generates a NULL named
3299 TYPE_DECL node for each complete tagged type, each array type,
3300 and each function type node created) the C++ front-end generates
3301 a _named_ TYPE_DECL node for each tagged type node created.
3302 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3303 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3304 front-end, but for each type, tagged or not. */
3306 #define TYPE_DECL_IS_STUB(decl) \
3307 (DECL_NAME (decl) == NULL_TREE \
3308 || (DECL_ARTIFICIAL (decl) \
3309 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3310 /* This is necessary for stub decls that \
3311 appear in nested inline functions. */ \
3312 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3313 && (decl_ultimate_origin (decl) \
3314 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3316 /* Information concerning the compilation unit's programming
3317 language, and compiler version. */
3319 /* Fixed size portion of the DWARF compilation unit header. */
3320 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3321 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3322 + (dwarf_version >= 5 ? 4 : 3))
3324 /* Fixed size portion of the DWARF comdat type unit header. */
3325 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3326 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3327 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3329 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3330 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3331 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3333 /* Fixed size portion of public names info. */
3334 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3336 /* Fixed size portion of the address range info. */
3337 #define DWARF_ARANGES_HEADER_SIZE \
3338 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3339 DWARF2_ADDR_SIZE * 2) \
3340 - DWARF_INITIAL_LENGTH_SIZE)
3342 /* Size of padding portion in the address range info. It must be
3343 aligned to twice the pointer size. */
3344 #define DWARF_ARANGES_PAD_SIZE \
3345 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3346 DWARF2_ADDR_SIZE * 2) \
3347 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3349 /* Use assembler line directives if available. */
3350 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3351 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3352 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3353 #else
3354 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3355 #endif
3356 #endif
3358 /* Use assembler views in line directives if available. */
3359 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3360 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3361 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3362 #else
3363 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3364 #endif
3365 #endif
3367 /* Return true if GCC configure detected assembler support for .loc. */
3369 bool
3370 dwarf2out_default_as_loc_support (void)
3372 return DWARF2_ASM_LINE_DEBUG_INFO;
3373 #if (GCC_VERSION >= 3000)
3374 # undef DWARF2_ASM_LINE_DEBUG_INFO
3375 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3376 #endif
3379 /* Return true if GCC configure detected assembler support for views
3380 in .loc directives. */
3382 bool
3383 dwarf2out_default_as_locview_support (void)
3385 return DWARF2_ASM_VIEW_DEBUG_INFO;
3386 #if (GCC_VERSION >= 3000)
3387 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3388 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3389 #endif
3392 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3393 view computation, and it refers to a view identifier for which we
3394 will not emit a label because it is known to map to a view number
3395 zero. We won't allocate the bitmap if we're not using assembler
3396 support for location views, but we have to make the variable
3397 visible for GGC and for code that will be optimized out for lack of
3398 support but that's still parsed and compiled. We could abstract it
3399 out with macros, but it's not worth it. */
3400 static GTY(()) bitmap zero_view_p;
3402 /* Evaluate to TRUE iff N is known to identify the first location view
3403 at its PC. When not using assembler location view computation,
3404 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3405 and views label numbers recorded in it are the ones known to be
3406 zero. */
3407 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3408 || (N) == (var_loc_view)-1 \
3409 || (zero_view_p \
3410 && bitmap_bit_p (zero_view_p, (N))))
3412 /* Return true iff we're to emit .loc directives for the assembler to
3413 generate line number sections.
3415 When we're not emitting views, all we need from the assembler is
3416 support for .loc directives.
3418 If we are emitting views, we can only use the assembler's .loc
3419 support if it also supports views.
3421 When the compiler is emitting the line number programs and
3422 computing view numbers itself, it resets view numbers at known PC
3423 changes and counts from that, and then it emits view numbers as
3424 literal constants in locviewlists. There are cases in which the
3425 compiler is not sure about PC changes, e.g. when extra alignment is
3426 requested for a label. In these cases, the compiler may not reset
3427 the view counter, and the potential PC advance in the line number
3428 program will use an opcode that does not reset the view counter
3429 even if the PC actually changes, so that compiler and debug info
3430 consumer can keep view numbers in sync.
3432 When the compiler defers view computation to the assembler, it
3433 emits symbolic view numbers in locviewlists, with the exception of
3434 views known to be zero (forced resets, or reset after
3435 compiler-visible PC changes): instead of emitting symbols for
3436 these, we emit literal zero and assert the assembler agrees with
3437 the compiler's assessment. We could use symbolic views everywhere,
3438 instead of special-casing zero views, but then we'd be unable to
3439 optimize out locviewlists that contain only zeros. */
3441 static bool
3442 output_asm_line_debug_info (void)
3444 return (dwarf2out_as_loc_support
3445 && (dwarf2out_as_locview_support
3446 || !debug_variable_location_views));
3449 static bool asm_outputs_debug_line_str (void);
3451 /* Minimum line offset in a special line info. opcode.
3452 This value was chosen to give a reasonable range of values. */
3453 #define DWARF_LINE_BASE -10
3455 /* First special line opcode - leave room for the standard opcodes. */
3456 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3458 /* Range of line offsets in a special line info. opcode. */
3459 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3461 /* Flag that indicates the initial value of the is_stmt_start flag.
3462 In the present implementation, we do not mark any lines as
3463 the beginning of a source statement, because that information
3464 is not made available by the GCC front-end. */
3465 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3467 /* Maximum number of operations per instruction bundle. */
3468 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3469 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3470 #endif
3472 /* This location is used by calc_die_sizes() to keep track
3473 the offset of each DIE within the .debug_info section. */
3474 static unsigned long next_die_offset;
3476 /* Record the root of the DIE's built for the current compilation unit. */
3477 static GTY(()) dw_die_ref single_comp_unit_die;
3479 /* A list of type DIEs that have been separated into comdat sections. */
3480 static GTY(()) comdat_type_node *comdat_type_list;
3482 /* A list of CU DIEs that have been separated. */
3483 static GTY(()) limbo_die_node *cu_die_list;
3485 /* A list of DIEs with a NULL parent waiting to be relocated. */
3486 static GTY(()) limbo_die_node *limbo_die_list;
3488 /* A list of DIEs for which we may have to generate
3489 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3490 static GTY(()) limbo_die_node *deferred_asm_name;
3492 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3494 typedef const char *compare_type;
3496 static hashval_t hash (dwarf_file_data *);
3497 static bool equal (dwarf_file_data *, const char *);
3500 /* Filenames referenced by this compilation unit. */
3501 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3503 struct decl_die_hasher : ggc_ptr_hash<die_node>
3505 typedef tree compare_type;
3507 static hashval_t hash (die_node *);
3508 static bool equal (die_node *, tree);
3510 /* A hash table of references to DIE's that describe declarations.
3511 The key is a DECL_UID() which is a unique number identifying each decl. */
3512 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3514 struct GTY ((for_user)) variable_value_struct {
3515 unsigned int decl_id;
3516 vec<dw_die_ref, va_gc> *dies;
3519 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3521 typedef tree compare_type;
3523 static hashval_t hash (variable_value_struct *);
3524 static bool equal (variable_value_struct *, tree);
3526 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3527 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3528 DECL_CONTEXT of the referenced VAR_DECLs. */
3529 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3531 struct block_die_hasher : ggc_ptr_hash<die_struct>
3533 static hashval_t hash (die_struct *);
3534 static bool equal (die_struct *, die_struct *);
3537 /* A hash table of references to DIE's that describe COMMON blocks.
3538 The key is DECL_UID() ^ die_parent. */
3539 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3541 typedef struct GTY(()) die_arg_entry_struct {
3542 dw_die_ref die;
3543 tree arg;
3544 } die_arg_entry;
3547 /* Node of the variable location list. */
3548 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3549 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3550 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3551 in mode of the EXPR_LIST node and first EXPR_LIST operand
3552 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3553 location or NULL for padding. For larger bitsizes,
3554 mode is 0 and first operand is a CONCAT with bitsize
3555 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3556 NULL as second operand. */
3557 rtx GTY (()) loc;
3558 const char * GTY (()) label;
3559 struct var_loc_node * GTY (()) next;
3560 var_loc_view view;
3563 /* Variable location list. */
3564 struct GTY ((for_user)) var_loc_list_def {
3565 struct var_loc_node * GTY (()) first;
3567 /* Pointer to the last but one or last element of the
3568 chained list. If the list is empty, both first and
3569 last are NULL, if the list contains just one node
3570 or the last node certainly is not redundant, it points
3571 to the last node, otherwise points to the last but one.
3572 Do not mark it for GC because it is marked through the chain. */
3573 struct var_loc_node * GTY ((skip ("%h"))) last;
3575 /* Pointer to the last element before section switch,
3576 if NULL, either sections weren't switched or first
3577 is after section switch. */
3578 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3580 /* DECL_UID of the variable decl. */
3581 unsigned int decl_id;
3583 typedef struct var_loc_list_def var_loc_list;
3585 /* Call argument location list. */
3586 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3587 rtx GTY (()) call_arg_loc_note;
3588 const char * GTY (()) label;
3589 tree GTY (()) block;
3590 bool tail_call_p;
3591 rtx GTY (()) symbol_ref;
3592 struct call_arg_loc_node * GTY (()) next;
3596 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3598 typedef const_tree compare_type;
3600 static hashval_t hash (var_loc_list *);
3601 static bool equal (var_loc_list *, const_tree);
3604 /* Table of decl location linked lists. */
3605 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3607 /* Head and tail of call_arg_loc chain. */
3608 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3609 static struct call_arg_loc_node *call_arg_loc_last;
3611 /* Number of call sites in the current function. */
3612 static int call_site_count = -1;
3613 /* Number of tail call sites in the current function. */
3614 static int tail_call_site_count = -1;
3616 /* A cached location list. */
3617 struct GTY ((for_user)) cached_dw_loc_list_def {
3618 /* The DECL_UID of the decl that this entry describes. */
3619 unsigned int decl_id;
3621 /* The cached location list. */
3622 dw_loc_list_ref loc_list;
3624 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3626 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3629 typedef const_tree compare_type;
3631 static hashval_t hash (cached_dw_loc_list *);
3632 static bool equal (cached_dw_loc_list *, const_tree);
3635 /* Table of cached location lists. */
3636 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3638 /* A vector of references to DIE's that are uniquely identified by their tag,
3639 presence/absence of children DIE's, and list of attribute/value pairs. */
3640 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3642 /* A hash map to remember the stack usage for DWARF procedures. The value
3643 stored is the stack size difference between before the DWARF procedure
3644 invokation and after it returned. In other words, for a DWARF procedure
3645 that consumes N stack slots and that pushes M ones, this stores M - N. */
3646 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3648 /* A global counter for generating labels for line number data. */
3649 static unsigned int line_info_label_num;
3651 /* The current table to which we should emit line number information
3652 for the current function. This will be set up at the beginning of
3653 assembly for the function. */
3654 static GTY(()) dw_line_info_table *cur_line_info_table;
3656 /* The two default tables of line number info. */
3657 static GTY(()) dw_line_info_table *text_section_line_info;
3658 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3660 /* The set of all non-default tables of line number info. */
3661 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3663 /* A flag to tell pubnames/types export if there is an info section to
3664 refer to. */
3665 static bool info_section_emitted;
3667 /* A pointer to the base of a table that contains a list of publicly
3668 accessible names. */
3669 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3671 /* A pointer to the base of a table that contains a list of publicly
3672 accessible types. */
3673 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3675 /* A pointer to the base of a table that contains a list of macro
3676 defines/undefines (and file start/end markers). */
3677 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3679 /* True if .debug_macinfo or .debug_macros section is going to be
3680 emitted. */
3681 #define have_macinfo \
3682 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3683 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3684 && !macinfo_table->is_empty ())
3686 /* Vector of dies for which we should generate .debug_ranges info. */
3687 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3689 /* Vector of pairs of labels referenced in ranges_table. */
3690 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3692 /* Whether we have location lists that need outputting */
3693 static GTY(()) bool have_location_lists;
3695 /* Unique label counter. */
3696 static GTY(()) unsigned int loclabel_num;
3698 /* Unique label counter for point-of-call tables. */
3699 static GTY(()) unsigned int poc_label_num;
3701 /* The last file entry emitted by maybe_emit_file(). */
3702 static GTY(()) struct dwarf_file_data * last_emitted_file;
3704 /* Number of internal labels generated by gen_internal_sym(). */
3705 static GTY(()) int label_num;
3707 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3709 /* Instances of generic types for which we need to generate debug
3710 info that describe their generic parameters and arguments. That
3711 generation needs to happen once all types are properly laid out so
3712 we do it at the end of compilation. */
3713 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3715 /* Offset from the "steady-state frame pointer" to the frame base,
3716 within the current function. */
3717 static poly_int64 frame_pointer_fb_offset;
3718 static bool frame_pointer_fb_offset_valid;
3720 static vec<dw_die_ref> base_types;
3722 /* Flags to represent a set of attribute classes for attributes that represent
3723 a scalar value (bounds, pointers, ...). */
3724 enum dw_scalar_form
3726 dw_scalar_form_constant = 0x01,
3727 dw_scalar_form_exprloc = 0x02,
3728 dw_scalar_form_reference = 0x04
3731 /* Forward declarations for functions defined in this file. */
3733 static int is_pseudo_reg (const_rtx);
3734 static tree type_main_variant (tree);
3735 static int is_tagged_type (const_tree);
3736 static const char *dwarf_tag_name (unsigned);
3737 static const char *dwarf_attr_name (unsigned);
3738 static const char *dwarf_form_name (unsigned);
3739 static tree decl_ultimate_origin (const_tree);
3740 static tree decl_class_context (tree);
3741 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3742 static inline unsigned int AT_index (dw_attr_node *);
3743 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3744 static inline unsigned AT_flag (dw_attr_node *);
3745 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3746 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3747 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3748 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3749 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3750 unsigned int, unsigned char *);
3751 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3752 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3753 static inline const char *AT_string (dw_attr_node *);
3754 static enum dwarf_form AT_string_form (dw_attr_node *);
3755 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3756 static void add_AT_specification (dw_die_ref, dw_die_ref);
3757 static inline dw_die_ref AT_ref (dw_attr_node *);
3758 static inline int AT_ref_external (dw_attr_node *);
3759 static inline void set_AT_ref_external (dw_attr_node *, int);
3760 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3761 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3762 dw_loc_list_ref);
3763 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3764 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3765 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3766 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3767 static void remove_addr_table_entry (addr_table_entry *);
3768 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3769 static inline rtx AT_addr (dw_attr_node *);
3770 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3771 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3772 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3773 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3774 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3775 unsigned long, bool);
3776 static inline const char *AT_lbl (dw_attr_node *);
3777 static const char *get_AT_low_pc (dw_die_ref);
3778 static bool is_c (void);
3779 static bool is_cxx (void);
3780 static bool is_cxx (const_tree);
3781 static bool is_fortran (void);
3782 static bool is_ada (void);
3783 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3784 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3785 static void add_child_die (dw_die_ref, dw_die_ref);
3786 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3787 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3788 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3789 static void equate_type_number_to_die (tree, dw_die_ref);
3790 static var_loc_list *lookup_decl_loc (const_tree);
3791 static void equate_decl_number_to_die (tree, dw_die_ref);
3792 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3793 static void print_spaces (FILE *);
3794 static void print_die (dw_die_ref, FILE *);
3795 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3796 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3797 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3798 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3799 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3800 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3801 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3802 struct md5_ctx *, int *);
3803 struct checksum_attributes;
3804 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3805 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3806 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3807 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3808 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3809 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3810 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3811 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3812 static int is_type_die (dw_die_ref);
3813 static inline bool is_template_instantiation (dw_die_ref);
3814 static int is_declaration_die (dw_die_ref);
3815 static int should_move_die_to_comdat (dw_die_ref);
3816 static dw_die_ref clone_as_declaration (dw_die_ref);
3817 static dw_die_ref clone_die (dw_die_ref);
3818 static dw_die_ref clone_tree (dw_die_ref);
3819 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3820 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3821 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3822 static dw_die_ref generate_skeleton (dw_die_ref);
3823 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3824 dw_die_ref,
3825 dw_die_ref);
3826 static void break_out_comdat_types (dw_die_ref);
3827 static void copy_decls_for_unworthy_types (dw_die_ref);
3829 static void add_sibling_attributes (dw_die_ref);
3830 static void output_location_lists (dw_die_ref);
3831 static int constant_size (unsigned HOST_WIDE_INT);
3832 static unsigned long size_of_die (dw_die_ref);
3833 static void calc_die_sizes (dw_die_ref);
3834 static void calc_base_type_die_sizes (void);
3835 static void mark_dies (dw_die_ref);
3836 static void unmark_dies (dw_die_ref);
3837 static void unmark_all_dies (dw_die_ref);
3838 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3839 static unsigned long size_of_aranges (void);
3840 static enum dwarf_form value_format (dw_attr_node *);
3841 static void output_value_format (dw_attr_node *);
3842 static void output_abbrev_section (void);
3843 static void output_die_abbrevs (unsigned long, dw_die_ref);
3844 static void output_die (dw_die_ref);
3845 static void output_compilation_unit_header (enum dwarf_unit_type);
3846 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3847 static void output_comdat_type_unit (comdat_type_node *, bool);
3848 static const char *dwarf2_name (tree, int);
3849 static void add_pubname (tree, dw_die_ref);
3850 static void add_enumerator_pubname (const char *, dw_die_ref);
3851 static void add_pubname_string (const char *, dw_die_ref);
3852 static void add_pubtype (tree, dw_die_ref);
3853 static void output_pubnames (vec<pubname_entry, va_gc> *);
3854 static void output_aranges (void);
3855 static unsigned int add_ranges (const_tree, bool = false);
3856 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3857 bool *, bool);
3858 static void output_ranges (void);
3859 static dw_line_info_table *new_line_info_table (void);
3860 static void output_line_info (bool);
3861 static void output_file_names (void);
3862 static int is_base_type (tree);
3863 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3864 static int decl_quals (const_tree);
3865 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3866 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3867 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3868 static unsigned int debugger_reg_number (const_rtx);
3869 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3870 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3871 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3872 enum var_init_status);
3873 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3874 enum var_init_status);
3875 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3876 enum var_init_status);
3877 static int is_based_loc (const_rtx);
3878 static bool resolve_one_addr (rtx *);
3879 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3880 enum var_init_status);
3881 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3882 enum var_init_status);
3883 struct loc_descr_context;
3884 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3885 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3886 static dw_loc_list_ref loc_list_from_tree (tree, int,
3887 struct loc_descr_context *);
3888 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3889 struct loc_descr_context *);
3890 static tree field_type (const_tree);
3891 static unsigned int simple_type_align_in_bits (const_tree);
3892 static unsigned int simple_decl_align_in_bits (const_tree);
3893 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3894 struct vlr_context;
3895 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3896 HOST_WIDE_INT *);
3897 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3898 dw_loc_list_ref);
3899 static void add_data_member_location_attribute (dw_die_ref, tree,
3900 struct vlr_context *);
3901 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3902 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3903 static void insert_wide_int (const wide_int &, unsigned char *, int);
3904 static unsigned insert_float (const_rtx, unsigned char *);
3905 static rtx rtl_for_decl_location (tree);
3906 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3907 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3908 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3909 static void add_desc_attribute (dw_die_ref, tree);
3910 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3911 static void add_comp_dir_attribute (dw_die_ref);
3912 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3913 struct loc_descr_context *);
3914 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3915 struct loc_descr_context *);
3916 static void add_subscript_info (dw_die_ref, tree, bool);
3917 static void add_byte_size_attribute (dw_die_ref, tree);
3918 static void add_alignment_attribute (dw_die_ref, tree);
3919 static void add_bit_offset_attribute (dw_die_ref, tree);
3920 static void add_bit_size_attribute (dw_die_ref, tree);
3921 static void add_prototyped_attribute (dw_die_ref, tree);
3922 static void add_abstract_origin_attribute (dw_die_ref, tree);
3923 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3924 static void add_src_coords_attributes (dw_die_ref, tree);
3925 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3926 static void add_discr_value (dw_die_ref, dw_discr_value *);
3927 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3928 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3929 static dw_die_ref scope_die_for (tree, dw_die_ref);
3930 static inline int local_scope_p (dw_die_ref);
3931 static inline int class_scope_p (dw_die_ref);
3932 static inline int class_or_namespace_scope_p (dw_die_ref);
3933 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3934 static void add_calling_convention_attribute (dw_die_ref, tree);
3935 static const char *type_tag (const_tree);
3936 static tree member_declared_type (const_tree);
3937 #if 0
3938 static const char *decl_start_label (tree);
3939 #endif
3940 static void gen_array_type_die (tree, dw_die_ref);
3941 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3942 #if 0
3943 static void gen_entry_point_die (tree, dw_die_ref);
3944 #endif
3945 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3946 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3947 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3948 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3949 static void gen_formal_types_die (tree, dw_die_ref);
3950 static void gen_subprogram_die (tree, dw_die_ref);
3951 static void gen_variable_die (tree, tree, dw_die_ref);
3952 static void gen_const_die (tree, dw_die_ref);
3953 static void gen_label_die (tree, dw_die_ref);
3954 static void gen_lexical_block_die (tree, dw_die_ref);
3955 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3956 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3957 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3958 static dw_die_ref gen_compile_unit_die (const char *);
3959 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3960 static void gen_member_die (tree, dw_die_ref);
3961 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3962 enum debug_info_usage);
3963 static void gen_subroutine_type_die (tree, dw_die_ref);
3964 static void gen_typedef_die (tree, dw_die_ref);
3965 static void gen_type_die (tree, dw_die_ref);
3966 static void gen_block_die (tree, dw_die_ref);
3967 static void decls_for_scope (tree, dw_die_ref, bool = true);
3968 static bool is_naming_typedef_decl (const_tree);
3969 static inline dw_die_ref get_context_die (tree);
3970 static void gen_namespace_die (tree, dw_die_ref);
3971 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3972 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3973 static dw_die_ref force_decl_die (tree);
3974 static dw_die_ref force_type_die (tree);
3975 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3976 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3977 static struct dwarf_file_data * lookup_filename (const char *);
3978 static void retry_incomplete_types (void);
3979 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3980 static void gen_generic_params_dies (tree);
3981 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3982 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3983 static void splice_child_die (dw_die_ref, dw_die_ref);
3984 static int file_info_cmp (const void *, const void *);
3985 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3986 const char *, var_loc_view, const char *);
3987 static void output_loc_list (dw_loc_list_ref);
3988 static char *gen_internal_sym (const char *);
3989 static bool want_pubnames (void);
3991 static void prune_unmark_dies (dw_die_ref);
3992 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3993 static void prune_unused_types_mark (dw_die_ref, int);
3994 static void prune_unused_types_walk (dw_die_ref);
3995 static void prune_unused_types_walk_attribs (dw_die_ref);
3996 static void prune_unused_types_prune (dw_die_ref);
3997 static void prune_unused_types (void);
3998 static int maybe_emit_file (struct dwarf_file_data *fd);
3999 static inline const char *AT_vms_delta1 (dw_attr_node *);
4000 static inline const char *AT_vms_delta2 (dw_attr_node *);
4001 #if VMS_DEBUGGING_INFO
4002 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4003 const char *, const char *);
4004 #endif
4005 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4006 static void gen_remaining_tmpl_value_param_die_attribute (void);
4007 static bool generic_type_p (tree);
4008 static void schedule_generic_params_dies_gen (tree t);
4009 static void gen_scheduled_generic_parms_dies (void);
4010 static void resolve_variable_values (void);
4012 static const char *comp_dir_string (void);
4014 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
4016 /* enum for tracking thread-local variables whose address is really an offset
4017 relative to the TLS pointer, which will need link-time relocation, but will
4018 not need relocation by the DWARF consumer. */
4020 enum dtprel_bool
4022 dtprel_false = 0,
4023 dtprel_true = 1
4026 /* Return the operator to use for an address of a variable. For dtprel_true, we
4027 use DW_OP_const*. For regular variables, which need both link-time
4028 relocation and consumer-level relocation (e.g., to account for shared objects
4029 loaded at a random address), we use DW_OP_addr*. */
4031 static inline enum dwarf_location_atom
4032 dw_addr_op (enum dtprel_bool dtprel)
4034 if (dtprel == dtprel_true)
4035 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
4036 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
4037 else
4038 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
4041 /* Return a pointer to a newly allocated address location description. If
4042 dwarf_split_debug_info is true, then record the address with the appropriate
4043 relocation. */
4044 static inline dw_loc_descr_ref
4045 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4047 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4049 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4050 ref->dw_loc_oprnd1.v.val_addr = addr;
4051 ref->dtprel = dtprel;
4052 if (dwarf_split_debug_info)
4053 ref->dw_loc_oprnd1.val_entry
4054 = add_addr_table_entry (addr,
4055 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4056 else
4057 ref->dw_loc_oprnd1.val_entry = NULL;
4059 return ref;
4062 /* Section names used to hold DWARF debugging information. */
4064 #ifndef DEBUG_INFO_SECTION
4065 #define DEBUG_INFO_SECTION ".debug_info"
4066 #endif
4067 #ifndef DEBUG_DWO_INFO_SECTION
4068 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4069 #endif
4070 #ifndef DEBUG_LTO_INFO_SECTION
4071 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4072 #endif
4073 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4074 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4075 #endif
4076 #ifndef DEBUG_ABBREV_SECTION
4077 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4078 #endif
4079 #ifndef DEBUG_LTO_ABBREV_SECTION
4080 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4081 #endif
4082 #ifndef DEBUG_DWO_ABBREV_SECTION
4083 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4084 #endif
4085 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4086 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4087 #endif
4088 #ifndef DEBUG_ARANGES_SECTION
4089 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4090 #endif
4091 #ifndef DEBUG_ADDR_SECTION
4092 #define DEBUG_ADDR_SECTION ".debug_addr"
4093 #endif
4094 #ifndef DEBUG_MACINFO_SECTION
4095 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4096 #endif
4097 #ifndef DEBUG_LTO_MACINFO_SECTION
4098 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4099 #endif
4100 #ifndef DEBUG_DWO_MACINFO_SECTION
4101 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4102 #endif
4103 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4104 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4105 #endif
4106 #ifndef DEBUG_MACRO_SECTION
4107 #define DEBUG_MACRO_SECTION ".debug_macro"
4108 #endif
4109 #ifndef DEBUG_LTO_MACRO_SECTION
4110 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4111 #endif
4112 #ifndef DEBUG_DWO_MACRO_SECTION
4113 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4114 #endif
4115 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4116 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4117 #endif
4118 #ifndef DEBUG_LINE_SECTION
4119 #define DEBUG_LINE_SECTION ".debug_line"
4120 #endif
4121 #ifndef DEBUG_LTO_LINE_SECTION
4122 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4123 #endif
4124 #ifndef DEBUG_DWO_LINE_SECTION
4125 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4126 #endif
4127 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4128 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4129 #endif
4130 #ifndef DEBUG_LOC_SECTION
4131 #define DEBUG_LOC_SECTION ".debug_loc"
4132 #endif
4133 #ifndef DEBUG_DWO_LOC_SECTION
4134 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4135 #endif
4136 #ifndef DEBUG_LOCLISTS_SECTION
4137 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4138 #endif
4139 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4140 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4141 #endif
4142 #ifndef DEBUG_PUBNAMES_SECTION
4143 #define DEBUG_PUBNAMES_SECTION \
4144 ((debug_generate_pub_sections == 2) \
4145 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4146 #endif
4147 #ifndef DEBUG_PUBTYPES_SECTION
4148 #define DEBUG_PUBTYPES_SECTION \
4149 ((debug_generate_pub_sections == 2) \
4150 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4151 #endif
4152 #ifndef DEBUG_STR_OFFSETS_SECTION
4153 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4154 #endif
4155 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4156 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4157 #endif
4158 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4159 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4160 #endif
4161 #ifndef DEBUG_STR_SECTION
4162 #define DEBUG_STR_SECTION ".debug_str"
4163 #endif
4164 #ifndef DEBUG_LTO_STR_SECTION
4165 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4166 #endif
4167 #ifndef DEBUG_STR_DWO_SECTION
4168 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4169 #endif
4170 #ifndef DEBUG_LTO_STR_DWO_SECTION
4171 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4172 #endif
4173 #ifndef DEBUG_RANGES_SECTION
4174 #define DEBUG_RANGES_SECTION ".debug_ranges"
4175 #endif
4176 #ifndef DEBUG_RNGLISTS_SECTION
4177 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4178 #endif
4179 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4180 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4181 #endif
4182 #ifndef DEBUG_LINE_STR_SECTION
4183 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4184 #endif
4185 #ifndef DEBUG_LTO_LINE_STR_SECTION
4186 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4187 #endif
4189 /* Section flags for .debug_str section. */
4190 #define DEBUG_STR_SECTION_FLAGS \
4191 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4192 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4193 : SECTION_DEBUG)
4195 /* Section flags for .debug_str.dwo section. */
4196 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4198 /* Attribute used to refer to the macro section. */
4199 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4200 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4202 /* Labels we insert at beginning sections we can reference instead of
4203 the section names themselves. */
4205 #ifndef TEXT_SECTION_LABEL
4206 #define TEXT_SECTION_LABEL "Ltext"
4207 #endif
4208 #ifndef COLD_TEXT_SECTION_LABEL
4209 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4210 #endif
4211 #ifndef DEBUG_LINE_SECTION_LABEL
4212 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4213 #endif
4214 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4215 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4216 #endif
4217 #ifndef DEBUG_INFO_SECTION_LABEL
4218 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4219 #endif
4220 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4221 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4222 #endif
4223 #ifndef DEBUG_ABBREV_SECTION_LABEL
4224 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4225 #endif
4226 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4227 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4228 #endif
4229 #ifndef DEBUG_ADDR_SECTION_LABEL
4230 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4231 #endif
4232 #ifndef DEBUG_LOC_SECTION_LABEL
4233 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4234 #endif
4235 #ifndef DEBUG_RANGES_SECTION_LABEL
4236 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4237 #endif
4238 #ifndef DEBUG_MACINFO_SECTION_LABEL
4239 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4240 #endif
4241 #ifndef DEBUG_MACRO_SECTION_LABEL
4242 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4243 #endif
4244 #define SKELETON_COMP_DIE_ABBREV 1
4245 #define SKELETON_TYPE_DIE_ABBREV 2
4247 /* Definitions of defaults for formats and names of various special
4248 (artificial) labels which may be generated within this file (when the -g
4249 options is used and DWARF2_DEBUGGING_INFO is in effect.
4250 If necessary, these may be overridden from within the tm.h file, but
4251 typically, overriding these defaults is unnecessary. */
4253 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4254 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4255 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4256 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4257 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4258 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4259 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4260 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4261 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4262 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4263 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4264 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4265 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4266 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4267 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4269 #ifndef TEXT_END_LABEL
4270 #define TEXT_END_LABEL "Letext"
4271 #endif
4272 #ifndef COLD_END_LABEL
4273 #define COLD_END_LABEL "Letext_cold"
4274 #endif
4275 #ifndef BLOCK_BEGIN_LABEL
4276 #define BLOCK_BEGIN_LABEL "LBB"
4277 #endif
4278 #ifndef BLOCK_INLINE_ENTRY_LABEL
4279 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4280 #endif
4281 #ifndef BLOCK_END_LABEL
4282 #define BLOCK_END_LABEL "LBE"
4283 #endif
4284 #ifndef LINE_CODE_LABEL
4285 #define LINE_CODE_LABEL "LM"
4286 #endif
4289 /* Return the root of the DIE's built for the current compilation unit. */
4290 static dw_die_ref
4291 comp_unit_die (void)
4293 if (!single_comp_unit_die)
4294 single_comp_unit_die = gen_compile_unit_die (NULL);
4295 return single_comp_unit_die;
4298 /* We allow a language front-end to designate a function that is to be
4299 called to "demangle" any name before it is put into a DIE. */
4301 static const char *(*demangle_name_func) (const char *);
4303 void
4304 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4306 demangle_name_func = func;
4309 /* Test if rtl node points to a pseudo register. */
4311 static inline int
4312 is_pseudo_reg (const_rtx rtl)
4314 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4315 || (GET_CODE (rtl) == SUBREG
4316 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4319 /* Return a reference to a type, with its const and volatile qualifiers
4320 removed. */
4322 static inline tree
4323 type_main_variant (tree type)
4325 type = TYPE_MAIN_VARIANT (type);
4327 /* ??? There really should be only one main variant among any group of
4328 variants of a given type (and all of the MAIN_VARIANT values for all
4329 members of the group should point to that one type) but sometimes the C
4330 front-end messes this up for array types, so we work around that bug
4331 here. */
4332 if (TREE_CODE (type) == ARRAY_TYPE)
4333 while (type != TYPE_MAIN_VARIANT (type))
4334 type = TYPE_MAIN_VARIANT (type);
4336 return type;
4339 /* Return nonzero if the given type node represents a tagged type. */
4341 static inline int
4342 is_tagged_type (const_tree type)
4344 enum tree_code code = TREE_CODE (type);
4346 return (code == RECORD_TYPE || code == UNION_TYPE
4347 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4350 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4352 static void
4353 get_ref_die_offset_label (char *label, dw_die_ref ref)
4355 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4358 /* Return die_offset of a DIE reference to a base type. */
4360 static unsigned long int
4361 get_base_type_offset (dw_die_ref ref)
4363 if (ref->die_offset)
4364 return ref->die_offset;
4365 if (comp_unit_die ()->die_abbrev)
4367 calc_base_type_die_sizes ();
4368 gcc_assert (ref->die_offset);
4370 return ref->die_offset;
4373 /* Return die_offset of a DIE reference other than base type. */
4375 static unsigned long int
4376 get_ref_die_offset (dw_die_ref ref)
4378 gcc_assert (ref->die_offset);
4379 return ref->die_offset;
4382 /* Convert a DIE tag into its string name. */
4384 static const char *
4385 dwarf_tag_name (unsigned int tag)
4387 const char *name = get_DW_TAG_name (tag);
4389 if (name != NULL)
4390 return name;
4392 return "DW_TAG_<unknown>";
4395 /* Convert a DWARF attribute code into its string name. */
4397 static const char *
4398 dwarf_attr_name (unsigned int attr)
4400 const char *name;
4402 switch (attr)
4404 #if VMS_DEBUGGING_INFO
4405 case DW_AT_HP_prologue:
4406 return "DW_AT_HP_prologue";
4407 #else
4408 case DW_AT_MIPS_loop_unroll_factor:
4409 return "DW_AT_MIPS_loop_unroll_factor";
4410 #endif
4412 #if VMS_DEBUGGING_INFO
4413 case DW_AT_HP_epilogue:
4414 return "DW_AT_HP_epilogue";
4415 #else
4416 case DW_AT_MIPS_stride:
4417 return "DW_AT_MIPS_stride";
4418 #endif
4421 name = get_DW_AT_name (attr);
4423 if (name != NULL)
4424 return name;
4426 return "DW_AT_<unknown>";
4429 /* Convert a DWARF value form code into its string name. */
4431 static const char *
4432 dwarf_form_name (unsigned int form)
4434 const char *name = get_DW_FORM_name (form);
4436 if (name != NULL)
4437 return name;
4439 return "DW_FORM_<unknown>";
4442 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4443 instance of an inlined instance of a decl which is local to an inline
4444 function, so we have to trace all of the way back through the origin chain
4445 to find out what sort of node actually served as the original seed for the
4446 given block. */
4448 static tree
4449 decl_ultimate_origin (const_tree decl)
4451 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4452 return NULL_TREE;
4454 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4455 we're trying to output the abstract instance of this function. */
4456 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4457 return NULL_TREE;
4459 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4460 most distant ancestor, this should never happen. */
4461 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4463 return DECL_ABSTRACT_ORIGIN (decl);
4466 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4467 of a virtual function may refer to a base class, so we check the 'this'
4468 parameter. */
4470 static tree
4471 decl_class_context (tree decl)
4473 tree context = NULL_TREE;
4475 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4476 context = DECL_CONTEXT (decl);
4477 else
4478 context = TYPE_MAIN_VARIANT
4479 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4481 if (context && !TYPE_P (context))
4482 context = NULL_TREE;
4484 return context;
4487 /* Add an attribute/value pair to a DIE. */
4489 static inline void
4490 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4492 /* Maybe this should be an assert? */
4493 if (die == NULL)
4494 return;
4496 if (flag_checking)
4498 /* Check we do not add duplicate attrs. Can't use get_AT here
4499 because that recurses to the specification/abstract origin DIE. */
4500 dw_attr_node *a;
4501 unsigned ix;
4502 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4503 gcc_assert (a->dw_attr != attr->dw_attr);
4506 vec_safe_reserve (die->die_attr, 1);
4507 vec_safe_push (die->die_attr, *attr);
4510 enum dw_val_class
4511 AT_class (dw_attr_node *a)
4513 return a->dw_attr_val.val_class;
4516 /* Return the index for any attribute that will be referenced with a
4517 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4518 indices are stored in dw_attr_val.v.val_str for reference counting
4519 pruning. */
4521 static inline unsigned int
4522 AT_index (dw_attr_node *a)
4524 if (AT_class (a) == dw_val_class_str)
4525 return a->dw_attr_val.v.val_str->index;
4526 else if (a->dw_attr_val.val_entry != NULL)
4527 return a->dw_attr_val.val_entry->index;
4528 return NOT_INDEXED;
4531 /* Add a flag value attribute to a DIE. */
4533 static inline void
4534 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_flag;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_flag = flag;
4542 add_dwarf_attr (die, &attr);
4545 static inline unsigned
4546 AT_flag (dw_attr_node *a)
4548 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4549 return a->dw_attr_val.v.val_flag;
4552 /* Add a signed integer attribute value to a DIE. */
4554 static inline void
4555 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4557 dw_attr_node attr;
4559 attr.dw_attr = attr_kind;
4560 attr.dw_attr_val.val_class = dw_val_class_const;
4561 attr.dw_attr_val.val_entry = NULL;
4562 attr.dw_attr_val.v.val_int = int_val;
4563 add_dwarf_attr (die, &attr);
4566 HOST_WIDE_INT
4567 AT_int (dw_attr_node *a)
4569 gcc_assert (a && (AT_class (a) == dw_val_class_const
4570 || AT_class (a) == dw_val_class_const_implicit));
4571 return a->dw_attr_val.v.val_int;
4574 /* Add an unsigned integer attribute value to a DIE. */
4576 static inline void
4577 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4578 unsigned HOST_WIDE_INT unsigned_val)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4586 add_dwarf_attr (die, &attr);
4589 unsigned HOST_WIDE_INT
4590 AT_unsigned (dw_attr_node *a)
4592 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4593 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4594 return a->dw_attr_val.v.val_unsigned;
4597 /* Add an unsigned wide integer attribute value to a DIE. */
4599 static inline void
4600 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4601 const wide_int& w)
4603 dw_attr_node attr;
4605 attr.dw_attr = attr_kind;
4606 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4607 attr.dw_attr_val.val_entry = NULL;
4608 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4609 *attr.dw_attr_val.v.val_wide = w;
4610 add_dwarf_attr (die, &attr);
4613 /* Add an unsigned double integer attribute value to a DIE. */
4615 static inline void
4616 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4617 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4619 dw_attr_node attr;
4621 attr.dw_attr = attr_kind;
4622 attr.dw_attr_val.val_class = dw_val_class_const_double;
4623 attr.dw_attr_val.val_entry = NULL;
4624 attr.dw_attr_val.v.val_double.high = high;
4625 attr.dw_attr_val.v.val_double.low = low;
4626 add_dwarf_attr (die, &attr);
4629 /* Add a floating point attribute value to a DIE and return it. */
4631 static inline void
4632 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4633 unsigned int length, unsigned int elt_size, unsigned char *array)
4635 dw_attr_node attr;
4637 attr.dw_attr = attr_kind;
4638 attr.dw_attr_val.val_class = dw_val_class_vec;
4639 attr.dw_attr_val.val_entry = NULL;
4640 attr.dw_attr_val.v.val_vec.length = length;
4641 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4642 attr.dw_attr_val.v.val_vec.array = array;
4643 add_dwarf_attr (die, &attr);
4646 /* Add an 8-byte data attribute value to a DIE. */
4648 static inline void
4649 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4650 unsigned char data8[8])
4652 dw_attr_node attr;
4654 attr.dw_attr = attr_kind;
4655 attr.dw_attr_val.val_class = dw_val_class_data8;
4656 attr.dw_attr_val.val_entry = NULL;
4657 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4658 add_dwarf_attr (die, &attr);
4661 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4662 dwarf_split_debug_info, address attributes in dies destined for the
4663 final executable have force_direct set to avoid using indexed
4664 references. */
4666 static inline void
4667 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4668 bool force_direct)
4670 dw_attr_node attr;
4671 char * lbl_id;
4673 lbl_id = xstrdup (lbl_low);
4674 attr.dw_attr = DW_AT_low_pc;
4675 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4676 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4677 if (dwarf_split_debug_info && !force_direct)
4678 attr.dw_attr_val.val_entry
4679 = add_addr_table_entry (lbl_id, ate_kind_label);
4680 else
4681 attr.dw_attr_val.val_entry = NULL;
4682 add_dwarf_attr (die, &attr);
4684 attr.dw_attr = DW_AT_high_pc;
4685 if (dwarf_version < 4)
4686 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4687 else
4688 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4689 lbl_id = xstrdup (lbl_high);
4690 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4691 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4692 && dwarf_split_debug_info && !force_direct)
4693 attr.dw_attr_val.val_entry
4694 = add_addr_table_entry (lbl_id, ate_kind_label);
4695 else
4696 attr.dw_attr_val.val_entry = NULL;
4697 add_dwarf_attr (die, &attr);
4700 /* Hash and equality functions for debug_str_hash. */
4702 hashval_t
4703 indirect_string_hasher::hash (indirect_string_node *x)
4705 return htab_hash_string (x->str);
4708 bool
4709 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4711 return strcmp (x1->str, x2) == 0;
4714 /* Add STR to the given string hash table. */
4716 static struct indirect_string_node *
4717 find_AT_string_in_table (const char *str,
4718 hash_table<indirect_string_hasher> *table,
4719 enum insert_option insert = INSERT)
4721 struct indirect_string_node *node;
4723 indirect_string_node **slot
4724 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4725 if (*slot == NULL)
4727 node = ggc_cleared_alloc<indirect_string_node> ();
4728 node->str = ggc_strdup (str);
4729 *slot = node;
4731 else
4732 node = *slot;
4734 node->refcount++;
4735 return node;
4738 /* Add STR to the indirect string hash table. */
4740 static struct indirect_string_node *
4741 find_AT_string (const char *str, enum insert_option insert = INSERT)
4743 if (! debug_str_hash)
4744 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4746 return find_AT_string_in_table (str, debug_str_hash, insert);
4749 /* Add a string attribute value to a DIE. */
4751 static inline void
4752 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4754 dw_attr_node attr;
4755 struct indirect_string_node *node;
4757 node = find_AT_string (str);
4759 attr.dw_attr = attr_kind;
4760 attr.dw_attr_val.val_class = dw_val_class_str;
4761 attr.dw_attr_val.val_entry = NULL;
4762 attr.dw_attr_val.v.val_str = node;
4763 add_dwarf_attr (die, &attr);
4766 static inline const char *
4767 AT_string (dw_attr_node *a)
4769 gcc_assert (a && AT_class (a) == dw_val_class_str);
4770 return a->dw_attr_val.v.val_str->str;
4773 /* Call this function directly to bypass AT_string_form's logic to put
4774 the string inline in the die. */
4776 static void
4777 set_indirect_string (struct indirect_string_node *node)
4779 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4780 /* Already indirect is a no op. */
4781 if (node->form == DW_FORM_strp
4782 || node->form == DW_FORM_line_strp
4783 || node->form == dwarf_FORM (DW_FORM_strx))
4785 gcc_assert (node->label);
4786 return;
4788 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4789 ++dw2_string_counter;
4790 node->label = xstrdup (label);
4792 if (!dwarf_split_debug_info)
4794 node->form = DW_FORM_strp;
4795 node->index = NOT_INDEXED;
4797 else
4799 node->form = dwarf_FORM (DW_FORM_strx);
4800 node->index = NO_INDEX_ASSIGNED;
4804 /* A helper function for dwarf2out_finish, called to reset indirect
4805 string decisions done for early LTO dwarf output before fat object
4806 dwarf output. */
4809 reset_indirect_string (indirect_string_node **h, void *)
4811 struct indirect_string_node *node = *h;
4812 if (node->form == DW_FORM_strp
4813 || node->form == DW_FORM_line_strp
4814 || node->form == dwarf_FORM (DW_FORM_strx))
4816 free (node->label);
4817 node->label = NULL;
4818 node->form = (dwarf_form) 0;
4819 node->index = 0;
4821 return 1;
4824 /* Add a string representing a file or filepath attribute value to a DIE. */
4826 static inline void
4827 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4828 const char *str)
4830 if (! asm_outputs_debug_line_str ())
4831 add_AT_string (die, attr_kind, str);
4832 else
4834 dw_attr_node attr;
4835 struct indirect_string_node *node;
4837 if (!debug_line_str_hash)
4838 debug_line_str_hash
4839 = hash_table<indirect_string_hasher>::create_ggc (10);
4841 node = find_AT_string_in_table (str, debug_line_str_hash);
4842 set_indirect_string (node);
4843 node->form = DW_FORM_line_strp;
4845 attr.dw_attr = attr_kind;
4846 attr.dw_attr_val.val_class = dw_val_class_str;
4847 attr.dw_attr_val.val_entry = NULL;
4848 attr.dw_attr_val.v.val_str = node;
4849 add_dwarf_attr (die, &attr);
4853 /* Find out whether a string should be output inline in DIE
4854 or out-of-line in .debug_str section. */
4856 static enum dwarf_form
4857 find_string_form (struct indirect_string_node *node)
4859 unsigned int len;
4861 if (node->form)
4862 return node->form;
4864 len = strlen (node->str) + 1;
4866 /* If the string is shorter or equal to the size of the reference, it is
4867 always better to put it inline. */
4868 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4869 return node->form = DW_FORM_string;
4871 /* If we cannot expect the linker to merge strings in .debug_str
4872 section, only put it into .debug_str if it is worth even in this
4873 single module. */
4874 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4875 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4876 && (len - dwarf_offset_size) * node->refcount <= len))
4877 return node->form = DW_FORM_string;
4879 set_indirect_string (node);
4881 return node->form;
4884 /* Find out whether the string referenced from the attribute should be
4885 output inline in DIE or out-of-line in .debug_str section. */
4887 static enum dwarf_form
4888 AT_string_form (dw_attr_node *a)
4890 gcc_assert (a && AT_class (a) == dw_val_class_str);
4891 return find_string_form (a->dw_attr_val.v.val_str);
4894 /* Add a DIE reference attribute value to a DIE. */
4896 static inline void
4897 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4899 dw_attr_node attr;
4900 gcc_checking_assert (targ_die != NULL);
4902 /* With LTO we can end up trying to reference something we didn't create
4903 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4904 if (targ_die == NULL)
4905 return;
4907 attr.dw_attr = attr_kind;
4908 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4909 attr.dw_attr_val.val_entry = NULL;
4910 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4911 attr.dw_attr_val.v.val_die_ref.external = 0;
4912 add_dwarf_attr (die, &attr);
4915 /* Change DIE reference REF to point to NEW_DIE instead. */
4917 static inline void
4918 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4920 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4921 ref->dw_attr_val.v.val_die_ref.die = new_die;
4922 ref->dw_attr_val.v.val_die_ref.external = 0;
4925 /* Add an AT_specification attribute to a DIE, and also make the back
4926 pointer from the specification to the definition. */
4928 static inline void
4929 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4931 add_AT_die_ref (die, DW_AT_specification, targ_die);
4932 gcc_assert (!targ_die->die_definition);
4933 targ_die->die_definition = die;
4936 static inline dw_die_ref
4937 AT_ref (dw_attr_node *a)
4939 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4940 return a->dw_attr_val.v.val_die_ref.die;
4943 static inline int
4944 AT_ref_external (dw_attr_node *a)
4946 if (a && AT_class (a) == dw_val_class_die_ref)
4947 return a->dw_attr_val.v.val_die_ref.external;
4949 return 0;
4952 static inline void
4953 set_AT_ref_external (dw_attr_node *a, int i)
4955 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4956 a->dw_attr_val.v.val_die_ref.external = i;
4959 /* Add a location description attribute value to a DIE. */
4961 static inline void
4962 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4964 dw_attr_node attr;
4966 attr.dw_attr = attr_kind;
4967 attr.dw_attr_val.val_class = dw_val_class_loc;
4968 attr.dw_attr_val.val_entry = NULL;
4969 attr.dw_attr_val.v.val_loc = loc;
4970 add_dwarf_attr (die, &attr);
4973 dw_loc_descr_ref
4974 AT_loc (dw_attr_node *a)
4976 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4977 return a->dw_attr_val.v.val_loc;
4980 static inline void
4981 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4983 dw_attr_node attr;
4985 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4986 return;
4988 attr.dw_attr = attr_kind;
4989 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4990 attr.dw_attr_val.val_entry = NULL;
4991 attr.dw_attr_val.v.val_loc_list = loc_list;
4992 add_dwarf_attr (die, &attr);
4993 have_location_lists = true;
4996 static inline dw_loc_list_ref
4997 AT_loc_list (dw_attr_node *a)
4999 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5000 return a->dw_attr_val.v.val_loc_list;
5003 /* Add a view list attribute to DIE. It must have a DW_AT_location
5004 attribute, because the view list complements the location list. */
5006 static inline void
5007 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
5009 dw_attr_node attr;
5011 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
5012 return;
5014 attr.dw_attr = attr_kind;
5015 attr.dw_attr_val.val_class = dw_val_class_view_list;
5016 attr.dw_attr_val.val_entry = NULL;
5017 attr.dw_attr_val.v.val_view_list = die;
5018 add_dwarf_attr (die, &attr);
5019 gcc_checking_assert (get_AT (die, DW_AT_location));
5020 gcc_assert (have_location_lists);
5023 /* Return a pointer to the location list referenced by the attribute.
5024 If the named attribute is a view list, look up the corresponding
5025 DW_AT_location attribute and return its location list. */
5027 static inline dw_loc_list_ref *
5028 AT_loc_list_ptr (dw_attr_node *a)
5030 gcc_assert (a);
5031 switch (AT_class (a))
5033 case dw_val_class_loc_list:
5034 return &a->dw_attr_val.v.val_loc_list;
5035 case dw_val_class_view_list:
5037 dw_attr_node *l;
5038 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5039 if (!l)
5040 return NULL;
5041 gcc_checking_assert (l + 1 == a);
5042 return AT_loc_list_ptr (l);
5044 default:
5045 gcc_unreachable ();
5049 /* Return the location attribute value associated with a view list
5050 attribute value. */
5052 static inline dw_val_node *
5053 view_list_to_loc_list_val_node (dw_val_node *val)
5055 gcc_assert (val->val_class == dw_val_class_view_list);
5056 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5057 if (!loc)
5058 return NULL;
5059 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5060 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5061 return &loc->dw_attr_val;
5064 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5066 static hashval_t hash (addr_table_entry *);
5067 static bool equal (addr_table_entry *, addr_table_entry *);
5070 /* Table of entries into the .debug_addr section. */
5072 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5074 /* Hash an address_table_entry. */
5076 hashval_t
5077 addr_hasher::hash (addr_table_entry *a)
5079 inchash::hash hstate;
5080 switch (a->kind)
5082 case ate_kind_rtx:
5083 hstate.add_int (0);
5084 break;
5085 case ate_kind_rtx_dtprel:
5086 hstate.add_int (1);
5087 break;
5088 case ate_kind_label:
5089 return htab_hash_string (a->addr.label);
5090 default:
5091 gcc_unreachable ();
5093 inchash::add_rtx (a->addr.rtl, hstate);
5094 return hstate.end ();
5097 /* Determine equality for two address_table_entries. */
5099 bool
5100 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5102 if (a1->kind != a2->kind)
5103 return 0;
5104 switch (a1->kind)
5106 case ate_kind_rtx:
5107 case ate_kind_rtx_dtprel:
5108 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5109 case ate_kind_label:
5110 return strcmp (a1->addr.label, a2->addr.label) == 0;
5111 default:
5112 gcc_unreachable ();
5116 /* Initialize an addr_table_entry. */
5118 void
5119 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5121 e->kind = kind;
5122 switch (kind)
5124 case ate_kind_rtx:
5125 case ate_kind_rtx_dtprel:
5126 e->addr.rtl = (rtx) addr;
5127 break;
5128 case ate_kind_label:
5129 e->addr.label = (char *) addr;
5130 break;
5132 e->refcount = 0;
5133 e->index = NO_INDEX_ASSIGNED;
5136 /* Add attr to the address table entry to the table. Defer setting an
5137 index until output time. */
5139 static addr_table_entry *
5140 add_addr_table_entry (void *addr, enum ate_kind kind)
5142 addr_table_entry *node;
5143 addr_table_entry finder;
5145 gcc_assert (dwarf_split_debug_info);
5146 if (! addr_index_table)
5147 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5148 init_addr_table_entry (&finder, kind, addr);
5149 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5151 if (*slot == HTAB_EMPTY_ENTRY)
5153 node = ggc_cleared_alloc<addr_table_entry> ();
5154 init_addr_table_entry (node, kind, addr);
5155 *slot = node;
5157 else
5158 node = *slot;
5160 node->refcount++;
5161 return node;
5164 /* Remove an entry from the addr table by decrementing its refcount.
5165 Strictly, decrementing the refcount would be enough, but the
5166 assertion that the entry is actually in the table has found
5167 bugs. */
5169 static void
5170 remove_addr_table_entry (addr_table_entry *entry)
5172 gcc_assert (dwarf_split_debug_info && addr_index_table);
5173 /* After an index is assigned, the table is frozen. */
5174 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5175 entry->refcount--;
5178 /* Given a location list, remove all addresses it refers to from the
5179 address_table. */
5181 static void
5182 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5184 for (; descr; descr = descr->dw_loc_next)
5185 if (descr->dw_loc_oprnd1.val_entry != NULL)
5187 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5188 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5192 /* A helper function for dwarf2out_finish called through
5193 htab_traverse. Assign an addr_table_entry its index. All entries
5194 must be collected into the table when this function is called,
5195 because the indexing code relies on htab_traverse to traverse nodes
5196 in the same order for each run. */
5199 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5201 addr_table_entry *node = *h;
5203 /* Don't index unreferenced nodes. */
5204 if (node->refcount == 0)
5205 return 1;
5207 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5208 node->index = *index;
5209 *index += 1;
5211 return 1;
5214 /* Return the tag of a given DIE. */
5216 enum dwarf_tag
5217 dw_get_die_tag (dw_die_ref die)
5219 return die->die_tag;
5222 /* Return a reference to the children list of a given DIE. */
5224 dw_die_ref
5225 dw_get_die_child (dw_die_ref die)
5227 return die->die_child;
5230 /* Return a reference to the sibling of a given DIE. */
5232 dw_die_ref
5233 dw_get_die_sib (dw_die_ref die)
5235 return die->die_sib;
5238 /* Add an address constant attribute value to a DIE. When using
5239 dwarf_split_debug_info, address attributes in dies destined for the
5240 final executable should be direct references--setting the parameter
5241 force_direct ensures this behavior. */
5243 static inline void
5244 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5245 bool force_direct)
5247 dw_attr_node attr;
5249 attr.dw_attr = attr_kind;
5250 attr.dw_attr_val.val_class = dw_val_class_addr;
5251 attr.dw_attr_val.v.val_addr = addr;
5252 if (dwarf_split_debug_info && !force_direct)
5253 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5254 else
5255 attr.dw_attr_val.val_entry = NULL;
5256 add_dwarf_attr (die, &attr);
5259 /* Get the RTX from to an address DIE attribute. */
5261 static inline rtx
5262 AT_addr (dw_attr_node *a)
5264 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5265 return a->dw_attr_val.v.val_addr;
5268 /* Add a file attribute value to a DIE. */
5270 static inline void
5271 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5272 struct dwarf_file_data *fd)
5274 dw_attr_node attr;
5276 attr.dw_attr = attr_kind;
5277 attr.dw_attr_val.val_class = dw_val_class_file;
5278 attr.dw_attr_val.val_entry = NULL;
5279 attr.dw_attr_val.v.val_file = fd;
5280 add_dwarf_attr (die, &attr);
5283 /* Get the dwarf_file_data from a file DIE attribute. */
5285 static inline struct dwarf_file_data *
5286 AT_file (dw_attr_node *a)
5288 gcc_assert (a && (AT_class (a) == dw_val_class_file
5289 || AT_class (a) == dw_val_class_file_implicit));
5290 return a->dw_attr_val.v.val_file;
5293 #if VMS_DEBUGGING_INFO
5294 /* Add a vms delta attribute value to a DIE. */
5296 static inline void
5297 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5298 const char *lbl1, const char *lbl2)
5300 dw_attr_node attr;
5302 attr.dw_attr = attr_kind;
5303 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5304 attr.dw_attr_val.val_entry = NULL;
5305 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5306 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5307 add_dwarf_attr (die, &attr);
5309 #endif
5311 /* Add a symbolic view identifier attribute value to a DIE. */
5313 static inline void
5314 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5315 const char *view_label)
5317 dw_attr_node attr;
5319 attr.dw_attr = attr_kind;
5320 attr.dw_attr_val.val_class = dw_val_class_symview;
5321 attr.dw_attr_val.val_entry = NULL;
5322 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5323 add_dwarf_attr (die, &attr);
5326 /* Add a label identifier attribute value to a DIE. */
5328 static inline void
5329 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5330 const char *lbl_id)
5332 dw_attr_node attr;
5334 attr.dw_attr = attr_kind;
5335 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5336 attr.dw_attr_val.val_entry = NULL;
5337 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5338 if (dwarf_split_debug_info)
5339 attr.dw_attr_val.val_entry
5340 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5341 ate_kind_label);
5342 add_dwarf_attr (die, &attr);
5345 /* Add a section offset attribute value to a DIE, an offset into the
5346 debug_line section. */
5348 static inline void
5349 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5350 const char *label)
5352 dw_attr_node attr;
5354 attr.dw_attr = attr_kind;
5355 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5356 attr.dw_attr_val.val_entry = NULL;
5357 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5358 add_dwarf_attr (die, &attr);
5361 /* Add a section offset attribute value to a DIE, an offset into the
5362 debug_macinfo section. */
5364 static inline void
5365 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5366 const char *label)
5368 dw_attr_node attr;
5370 attr.dw_attr = attr_kind;
5371 attr.dw_attr_val.val_class = dw_val_class_macptr;
5372 attr.dw_attr_val.val_entry = NULL;
5373 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5374 add_dwarf_attr (die, &attr);
5377 /* Add a range_list attribute value to a DIE. When using
5378 dwarf_split_debug_info, address attributes in dies destined for the
5379 final executable should be direct references--setting the parameter
5380 force_direct ensures this behavior. */
5382 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5383 #define RELOCATED_OFFSET (NULL)
5385 static void
5386 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5387 long unsigned int offset, bool force_direct)
5389 dw_attr_node attr;
5391 attr.dw_attr = attr_kind;
5392 attr.dw_attr_val.val_class = dw_val_class_range_list;
5393 /* For the range_list attribute, use val_entry to store whether the
5394 offset should follow split-debug-info or normal semantics. This
5395 value is read in output_range_list_offset. */
5396 if (dwarf_split_debug_info && !force_direct)
5397 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5398 else
5399 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5400 attr.dw_attr_val.v.val_offset = offset;
5401 add_dwarf_attr (die, &attr);
5404 /* Return the start label of a delta attribute. */
5406 static inline const char *
5407 AT_vms_delta1 (dw_attr_node *a)
5409 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5410 return a->dw_attr_val.v.val_vms_delta.lbl1;
5413 /* Return the end label of a delta attribute. */
5415 static inline const char *
5416 AT_vms_delta2 (dw_attr_node *a)
5418 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5419 return a->dw_attr_val.v.val_vms_delta.lbl2;
5422 static inline const char *
5423 AT_lbl (dw_attr_node *a)
5425 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5426 || AT_class (a) == dw_val_class_lineptr
5427 || AT_class (a) == dw_val_class_macptr
5428 || AT_class (a) == dw_val_class_loclistsptr
5429 || AT_class (a) == dw_val_class_high_pc));
5430 return a->dw_attr_val.v.val_lbl_id;
5433 /* Get the attribute of type attr_kind. */
5435 dw_attr_node *
5436 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5438 dw_attr_node *a;
5439 unsigned ix;
5440 dw_die_ref spec = NULL;
5442 if (! die)
5443 return NULL;
5445 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5446 if (a->dw_attr == attr_kind)
5447 return a;
5448 else if (a->dw_attr == DW_AT_specification
5449 || a->dw_attr == DW_AT_abstract_origin)
5450 spec = AT_ref (a);
5452 if (spec)
5453 return get_AT (spec, attr_kind);
5455 return NULL;
5458 /* Returns the parent of the declaration of DIE. */
5460 static dw_die_ref
5461 get_die_parent (dw_die_ref die)
5463 dw_die_ref t;
5465 if (!die)
5466 return NULL;
5468 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5469 || (t = get_AT_ref (die, DW_AT_specification)))
5470 die = t;
5472 return die->die_parent;
5475 /* Return the "low pc" attribute value, typically associated with a subprogram
5476 DIE. Return null if the "low pc" attribute is either not present, or if it
5477 cannot be represented as an assembler label identifier. */
5479 static inline const char *
5480 get_AT_low_pc (dw_die_ref die)
5482 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5484 return a ? AT_lbl (a) : NULL;
5487 /* Return the value of the string attribute designated by ATTR_KIND, or
5488 NULL if it is not present. */
5490 const char *
5491 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5493 dw_attr_node *a = get_AT (die, attr_kind);
5495 return a ? AT_string (a) : NULL;
5498 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5499 if it is not present. */
5502 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5504 dw_attr_node *a = get_AT (die, attr_kind);
5506 return a ? AT_flag (a) : 0;
5509 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5510 if it is not present. */
5512 unsigned
5513 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5515 dw_attr_node *a = get_AT (die, attr_kind);
5517 return a ? AT_unsigned (a) : 0;
5520 dw_die_ref
5521 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5523 dw_attr_node *a = get_AT (die, attr_kind);
5525 return a ? AT_ref (a) : NULL;
5528 struct dwarf_file_data *
5529 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5531 dw_attr_node *a = get_AT (die, attr_kind);
5533 return a ? AT_file (a) : NULL;
5536 /* Return TRUE if the language is C. */
5538 static inline bool
5539 is_c (void)
5541 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5543 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5544 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5549 /* Return TRUE if the language is C++. */
5551 static inline bool
5552 is_cxx (void)
5554 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5556 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5557 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5560 /* Return TRUE if DECL was created by the C++ frontend. */
5562 static bool
5563 is_cxx (const_tree decl)
5565 if (in_lto_p)
5567 const_tree context = get_ultimate_context (decl);
5568 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5569 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5571 return is_cxx ();
5574 /* Return TRUE if the language is Fortran. */
5576 static inline bool
5577 is_fortran (void)
5579 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5581 return (lang == DW_LANG_Fortran77
5582 || lang == DW_LANG_Fortran90
5583 || lang == DW_LANG_Fortran95
5584 || lang == DW_LANG_Fortran03
5585 || lang == DW_LANG_Fortran08);
5588 static inline bool
5589 is_fortran (const_tree decl)
5591 if (in_lto_p)
5593 const_tree context = get_ultimate_context (decl);
5594 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5595 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5596 "GNU Fortran", 11) == 0
5597 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5598 "GNU F77") == 0);
5600 return is_fortran ();
5603 /* Return TRUE if the language is Ada. */
5605 static inline bool
5606 is_ada (void)
5608 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5610 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5613 /* Return TRUE if the language is D. */
5615 static inline bool
5616 is_dlang (void)
5618 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5620 return lang == DW_LANG_D;
5623 /* Remove the specified attribute if present. Return TRUE if removal
5624 was successful. */
5626 static bool
5627 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5629 dw_attr_node *a;
5630 unsigned ix;
5632 if (! die)
5633 return false;
5635 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5636 if (a->dw_attr == attr_kind)
5638 if (AT_class (a) == dw_val_class_str)
5639 if (a->dw_attr_val.v.val_str->refcount)
5640 a->dw_attr_val.v.val_str->refcount--;
5642 /* vec::ordered_remove should help reduce the number of abbrevs
5643 that are needed. */
5644 die->die_attr->ordered_remove (ix);
5645 return true;
5647 return false;
5650 /* Remove CHILD from its parent. PREV must have the property that
5651 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5653 static void
5654 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5656 gcc_assert (child->die_parent == prev->die_parent);
5657 gcc_assert (prev->die_sib == child);
5658 if (prev == child)
5660 gcc_assert (child->die_parent->die_child == child);
5661 prev = NULL;
5663 else
5664 prev->die_sib = child->die_sib;
5665 if (child->die_parent->die_child == child)
5666 child->die_parent->die_child = prev;
5667 child->die_sib = NULL;
5670 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5671 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5673 static void
5674 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5676 dw_die_ref parent = old_child->die_parent;
5678 gcc_assert (parent == prev->die_parent);
5679 gcc_assert (prev->die_sib == old_child);
5681 new_child->die_parent = parent;
5682 if (prev == old_child)
5684 gcc_assert (parent->die_child == old_child);
5685 new_child->die_sib = new_child;
5687 else
5689 prev->die_sib = new_child;
5690 new_child->die_sib = old_child->die_sib;
5692 if (old_child->die_parent->die_child == old_child)
5693 old_child->die_parent->die_child = new_child;
5694 old_child->die_sib = NULL;
5697 /* Move all children from OLD_PARENT to NEW_PARENT. */
5699 static void
5700 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5702 dw_die_ref c;
5703 new_parent->die_child = old_parent->die_child;
5704 old_parent->die_child = NULL;
5705 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5708 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5709 matches TAG. */
5711 static void
5712 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5714 dw_die_ref c;
5716 c = die->die_child;
5717 if (c) do {
5718 dw_die_ref prev = c;
5719 c = c->die_sib;
5720 while (c->die_tag == tag)
5722 remove_child_with_prev (c, prev);
5723 c->die_parent = NULL;
5724 /* Might have removed every child. */
5725 if (die->die_child == NULL)
5726 return;
5727 c = prev->die_sib;
5729 } while (c != die->die_child);
5732 /* Add a CHILD_DIE as the last child of DIE. */
5734 static void
5735 add_child_die (dw_die_ref die, dw_die_ref child_die)
5737 /* FIXME this should probably be an assert. */
5738 if (! die || ! child_die)
5739 return;
5740 gcc_assert (die != child_die);
5742 child_die->die_parent = die;
5743 if (die->die_child)
5745 child_die->die_sib = die->die_child->die_sib;
5746 die->die_child->die_sib = child_die;
5748 else
5749 child_die->die_sib = child_die;
5750 die->die_child = child_die;
5753 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5755 static void
5756 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5757 dw_die_ref after_die)
5759 gcc_assert (die
5760 && child_die
5761 && after_die
5762 && die->die_child
5763 && die != child_die);
5765 child_die->die_parent = die;
5766 child_die->die_sib = after_die->die_sib;
5767 after_die->die_sib = child_die;
5768 if (die->die_child == after_die)
5769 die->die_child = child_die;
5772 /* Unassociate CHILD from its parent, and make its parent be
5773 NEW_PARENT. */
5775 static void
5776 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5778 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5779 if (p->die_sib == child)
5781 remove_child_with_prev (child, p);
5782 break;
5784 add_child_die (new_parent, child);
5787 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5788 is the specification, to the end of PARENT's list of children.
5789 This is done by removing and re-adding it. */
5791 static void
5792 splice_child_die (dw_die_ref parent, dw_die_ref child)
5794 /* We want the declaration DIE from inside the class, not the
5795 specification DIE at toplevel. */
5796 if (child->die_parent != parent)
5798 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5800 if (tmp)
5801 child = tmp;
5804 gcc_assert (child->die_parent == parent
5805 || (child->die_parent
5806 == get_AT_ref (parent, DW_AT_specification)));
5808 reparent_child (child, parent);
5811 /* Create and return a new die with TAG_VALUE as tag. */
5813 dw_die_ref
5814 new_die_raw (enum dwarf_tag tag_value)
5816 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5817 die->die_tag = tag_value;
5818 return die;
5821 /* Create and return a new die with a parent of PARENT_DIE. If
5822 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5823 associated tree T must be supplied to determine parenthood
5824 later. */
5826 static inline dw_die_ref
5827 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5829 dw_die_ref die = new_die_raw (tag_value);
5831 if (parent_die != NULL)
5832 add_child_die (parent_die, die);
5833 else
5835 limbo_die_node *limbo_node;
5837 /* No DIEs created after early dwarf should end up in limbo,
5838 because the limbo list should not persist past LTO
5839 streaming. */
5840 if (tag_value != DW_TAG_compile_unit
5841 /* These are allowed because they're generated while
5842 breaking out COMDAT units late. */
5843 && tag_value != DW_TAG_type_unit
5844 && tag_value != DW_TAG_skeleton_unit
5845 && !early_dwarf
5846 /* Allow nested functions to live in limbo because they will
5847 only temporarily live there, as decls_for_scope will fix
5848 them up. */
5849 && (TREE_CODE (t) != FUNCTION_DECL
5850 || !decl_function_context (t))
5851 /* Same as nested functions above but for types. Types that
5852 are local to a function will be fixed in
5853 decls_for_scope. */
5854 && (!RECORD_OR_UNION_TYPE_P (t)
5855 || !TYPE_CONTEXT (t)
5856 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5857 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5858 especially in the ltrans stage, but once we implement LTO
5859 dwarf streaming, we should remove this exception. */
5860 && !in_lto_p)
5862 fprintf (stderr, "symbol ended up in limbo too late:");
5863 debug_generic_stmt (t);
5864 gcc_unreachable ();
5867 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5868 limbo_node->die = die;
5869 limbo_node->created_for = t;
5870 limbo_node->next = limbo_die_list;
5871 limbo_die_list = limbo_node;
5874 return die;
5877 /* Return the DIE associated with the given type specifier. */
5879 dw_die_ref
5880 lookup_type_die (tree type)
5882 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5883 if (die && die->removed)
5885 TYPE_SYMTAB_DIE (type) = NULL;
5886 return NULL;
5888 return die;
5891 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5892 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5893 anonymous type instead the one of the naming typedef. */
5895 static inline dw_die_ref
5896 strip_naming_typedef (tree type, dw_die_ref type_die)
5898 if (type
5899 && TREE_CODE (type) == RECORD_TYPE
5900 && type_die
5901 && type_die->die_tag == DW_TAG_typedef
5902 && is_naming_typedef_decl (TYPE_NAME (type)))
5903 type_die = get_AT_ref (type_die, DW_AT_type);
5904 return type_die;
5907 /* Like lookup_type_die, but if type is an anonymous type named by a
5908 typedef[1], return the DIE of the anonymous type instead the one of
5909 the naming typedef. This is because in gen_typedef_die, we did
5910 equate the anonymous struct named by the typedef with the DIE of
5911 the naming typedef. So by default, lookup_type_die on an anonymous
5912 struct yields the DIE of the naming typedef.
5914 [1]: Read the comment of is_naming_typedef_decl to learn about what
5915 a naming typedef is. */
5917 static inline dw_die_ref
5918 lookup_type_die_strip_naming_typedef (tree type)
5920 dw_die_ref die = lookup_type_die (type);
5921 return strip_naming_typedef (type, die);
5924 /* Equate a DIE to a given type specifier. */
5926 static inline void
5927 equate_type_number_to_die (tree type, dw_die_ref type_die)
5929 TYPE_SYMTAB_DIE (type) = type_die;
5932 static dw_die_ref maybe_create_die_with_external_ref (tree);
5933 struct GTY(()) sym_off_pair
5935 const char * GTY((skip)) sym;
5936 unsigned HOST_WIDE_INT off;
5938 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5940 /* Returns a hash value for X (which really is a die_struct). */
5942 inline hashval_t
5943 decl_die_hasher::hash (die_node *x)
5945 return (hashval_t) x->decl_id;
5948 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5950 inline bool
5951 decl_die_hasher::equal (die_node *x, tree y)
5953 return (x->decl_id == DECL_UID (y));
5956 /* Return the DIE associated with a given declaration. */
5958 dw_die_ref
5959 lookup_decl_die (tree decl)
5961 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5962 NO_INSERT);
5963 if (!die)
5965 if (in_lto_p)
5966 return maybe_create_die_with_external_ref (decl);
5967 return NULL;
5969 if ((*die)->removed)
5971 decl_die_table->clear_slot (die);
5972 return NULL;
5974 return *die;
5978 /* Return the DIE associated with BLOCK. */
5980 static inline dw_die_ref
5981 lookup_block_die (tree block)
5983 dw_die_ref die = BLOCK_DIE (block);
5984 if (!die && in_lto_p)
5985 return maybe_create_die_with_external_ref (block);
5986 return die;
5989 /* Associate DIE with BLOCK. */
5991 static inline void
5992 equate_block_to_die (tree block, dw_die_ref die)
5994 BLOCK_DIE (block) = die;
5996 #undef BLOCK_DIE
5999 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
6000 style reference. Return true if we found one refering to a DIE for
6001 DECL, otherwise return false. */
6003 static bool
6004 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
6005 unsigned HOST_WIDE_INT *off)
6007 dw_die_ref die;
6009 if (in_lto_p)
6011 /* During WPA stage and incremental linking we use a hash-map
6012 to store the decl <-> label + offset map. */
6013 if (!external_die_map)
6014 return false;
6015 sym_off_pair *desc = external_die_map->get (decl);
6016 if (!desc)
6017 return false;
6018 *sym = desc->sym;
6019 *off = desc->off;
6020 return true;
6023 if (TREE_CODE (decl) == BLOCK)
6024 die = lookup_block_die (decl);
6025 else
6026 die = lookup_decl_die (decl);
6027 if (!die)
6028 return false;
6030 /* Similar to get_ref_die_offset_label, but using the "correct"
6031 label. */
6032 *off = die->die_offset;
6033 while (die->die_parent)
6034 die = die->die_parent;
6035 /* For the containing CU DIE we compute a die_symbol in
6036 compute_comp_unit_symbol. */
6037 gcc_assert (die->die_tag == DW_TAG_compile_unit
6038 && die->die_id.die_symbol != NULL);
6039 *sym = die->die_id.die_symbol;
6040 return true;
6043 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6045 static void
6046 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6047 const char *symbol, HOST_WIDE_INT offset)
6049 /* Create a fake DIE that contains the reference. Don't use
6050 new_die because we don't want to end up in the limbo list. */
6051 /* ??? We probably want to share these, thus put a ref to the DIE
6052 we create here to the external_die_map entry. */
6053 dw_die_ref ref = new_die_raw (die->die_tag);
6054 ref->die_id.die_symbol = symbol;
6055 ref->die_offset = offset;
6056 ref->with_offset = 1;
6057 add_AT_die_ref (die, attr_kind, ref);
6060 /* Create a DIE for DECL if required and add a reference to a DIE
6061 at SYMBOL + OFFSET which contains attributes dumped early. */
6063 static void
6064 dwarf2out_register_external_die (tree decl, const char *sym,
6065 unsigned HOST_WIDE_INT off)
6067 if (debug_info_level == DINFO_LEVEL_NONE)
6068 return;
6070 if (!external_die_map)
6071 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6072 gcc_checking_assert (!external_die_map->get (decl));
6073 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6074 external_die_map->put (decl, p);
6077 /* If we have a registered external DIE for DECL return a new DIE for
6078 the concrete instance with an appropriate abstract origin. */
6080 static dw_die_ref
6081 maybe_create_die_with_external_ref (tree decl)
6083 if (!external_die_map)
6084 return NULL;
6085 sym_off_pair *desc = external_die_map->get (decl);
6086 if (!desc)
6087 return NULL;
6089 const char *sym = desc->sym;
6090 unsigned HOST_WIDE_INT off = desc->off;
6091 external_die_map->remove (decl);
6093 in_lto_p = false;
6094 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6095 ? lookup_block_die (decl) : lookup_decl_die (decl));
6096 gcc_assert (!die);
6097 in_lto_p = true;
6099 tree ctx;
6100 dw_die_ref parent = NULL;
6101 /* Need to lookup a DIE for the decls context - the containing
6102 function or translation unit. */
6103 if (TREE_CODE (decl) == BLOCK)
6105 ctx = BLOCK_SUPERCONTEXT (decl);
6106 /* ??? We do not output DIEs for all scopes thus skip as
6107 many DIEs as needed. */
6108 while (TREE_CODE (ctx) == BLOCK
6109 && !lookup_block_die (ctx))
6110 ctx = BLOCK_SUPERCONTEXT (ctx);
6112 else
6113 ctx = DECL_CONTEXT (decl);
6114 /* Peel types in the context stack. */
6115 while (ctx && TYPE_P (ctx))
6116 ctx = TYPE_CONTEXT (ctx);
6117 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6118 if (debug_info_level <= DINFO_LEVEL_TERSE)
6119 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6120 ctx = DECL_CONTEXT (ctx);
6121 if (ctx)
6123 if (TREE_CODE (ctx) == BLOCK)
6124 parent = lookup_block_die (ctx);
6125 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6126 /* Keep the 1:1 association during WPA. */
6127 && !flag_wpa
6128 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6129 /* Otherwise all late annotations go to the main CU which
6130 imports the original CUs. */
6131 parent = comp_unit_die ();
6132 else if (TREE_CODE (ctx) == FUNCTION_DECL
6133 && TREE_CODE (decl) != FUNCTION_DECL
6134 && TREE_CODE (decl) != PARM_DECL
6135 && TREE_CODE (decl) != RESULT_DECL
6136 && TREE_CODE (decl) != BLOCK)
6137 /* Leave function local entities parent determination to when
6138 we process scope vars. */
6140 else
6141 parent = lookup_decl_die (ctx);
6143 else
6144 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6145 Handle this case gracefully by globalizing stuff. */
6146 parent = comp_unit_die ();
6147 /* Create a DIE "stub". */
6148 switch (TREE_CODE (decl))
6150 case TRANSLATION_UNIT_DECL:
6152 die = comp_unit_die ();
6153 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6154 to create a DIE for the original CUs. */
6155 return die;
6157 case NAMESPACE_DECL:
6158 if (is_fortran (decl))
6159 die = new_die (DW_TAG_module, parent, decl);
6160 else
6161 die = new_die (DW_TAG_namespace, parent, decl);
6162 break;
6163 case FUNCTION_DECL:
6164 die = new_die (DW_TAG_subprogram, parent, decl);
6165 break;
6166 case VAR_DECL:
6167 die = new_die (DW_TAG_variable, parent, decl);
6168 break;
6169 case RESULT_DECL:
6170 die = new_die (DW_TAG_variable, parent, decl);
6171 break;
6172 case PARM_DECL:
6173 die = new_die (DW_TAG_formal_parameter, parent, decl);
6174 break;
6175 case CONST_DECL:
6176 die = new_die (DW_TAG_constant, parent, decl);
6177 break;
6178 case LABEL_DECL:
6179 die = new_die (DW_TAG_label, parent, decl);
6180 break;
6181 case BLOCK:
6182 die = new_die (DW_TAG_lexical_block, parent, decl);
6183 break;
6184 default:
6185 gcc_unreachable ();
6187 if (TREE_CODE (decl) == BLOCK)
6188 equate_block_to_die (decl, die);
6189 else
6190 equate_decl_number_to_die (decl, die);
6192 add_desc_attribute (die, decl);
6194 /* Add a reference to the DIE providing early debug at $sym + off. */
6195 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6197 return die;
6200 /* Returns a hash value for X (which really is a var_loc_list). */
6202 inline hashval_t
6203 decl_loc_hasher::hash (var_loc_list *x)
6205 return (hashval_t) x->decl_id;
6208 /* Return nonzero if decl_id of var_loc_list X is the same as
6209 UID of decl *Y. */
6211 inline bool
6212 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6214 return (x->decl_id == DECL_UID (y));
6217 /* Return the var_loc list associated with a given declaration. */
6219 static inline var_loc_list *
6220 lookup_decl_loc (const_tree decl)
6222 if (!decl_loc_table)
6223 return NULL;
6224 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6227 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6229 inline hashval_t
6230 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6232 return (hashval_t) x->decl_id;
6235 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6236 UID of decl *Y. */
6238 inline bool
6239 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6241 return (x->decl_id == DECL_UID (y));
6244 /* Equate a DIE to a particular declaration. */
6246 static void
6247 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6249 unsigned int decl_id = DECL_UID (decl);
6251 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6252 decl_die->decl_id = decl_id;
6255 /* Return how many bits covers PIECE EXPR_LIST. */
6257 static HOST_WIDE_INT
6258 decl_piece_bitsize (rtx piece)
6260 int ret = (int) GET_MODE (piece);
6261 if (ret)
6262 return ret;
6263 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6264 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6265 return INTVAL (XEXP (XEXP (piece, 0), 0));
6268 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6270 static rtx *
6271 decl_piece_varloc_ptr (rtx piece)
6273 if ((int) GET_MODE (piece))
6274 return &XEXP (piece, 0);
6275 else
6276 return &XEXP (XEXP (piece, 0), 1);
6279 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6280 Next is the chain of following piece nodes. */
6282 static rtx_expr_list *
6283 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6285 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6286 return alloc_EXPR_LIST (bitsize, loc_note, next);
6287 else
6288 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6289 GEN_INT (bitsize),
6290 loc_note), next);
6293 /* Return rtx that should be stored into loc field for
6294 LOC_NOTE and BITPOS/BITSIZE. */
6296 static rtx
6297 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6298 HOST_WIDE_INT bitsize)
6300 if (bitsize != -1)
6302 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6303 if (bitpos != 0)
6304 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6306 return loc_note;
6309 /* This function either modifies location piece list *DEST in
6310 place (if SRC and INNER is NULL), or copies location piece list
6311 *SRC to *DEST while modifying it. Location BITPOS is modified
6312 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6313 not copied and if needed some padding around it is added.
6314 When modifying in place, DEST should point to EXPR_LIST where
6315 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6316 to the start of the whole list and INNER points to the EXPR_LIST
6317 where earlier pieces cover PIECE_BITPOS bits. */
6319 static void
6320 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6321 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6322 HOST_WIDE_INT bitsize, rtx loc_note)
6324 HOST_WIDE_INT diff;
6325 bool copy = inner != NULL;
6327 if (copy)
6329 /* First copy all nodes preceding the current bitpos. */
6330 while (src != inner)
6332 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6333 decl_piece_bitsize (*src), NULL_RTX);
6334 dest = &XEXP (*dest, 1);
6335 src = &XEXP (*src, 1);
6338 /* Add padding if needed. */
6339 if (bitpos != piece_bitpos)
6341 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6342 copy ? NULL_RTX : *dest);
6343 dest = &XEXP (*dest, 1);
6345 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6347 gcc_assert (!copy);
6348 /* A piece with correct bitpos and bitsize already exist,
6349 just update the location for it and return. */
6350 *decl_piece_varloc_ptr (*dest) = loc_note;
6351 return;
6353 /* Add the piece that changed. */
6354 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6355 dest = &XEXP (*dest, 1);
6356 /* Skip over pieces that overlap it. */
6357 diff = bitpos - piece_bitpos + bitsize;
6358 if (!copy)
6359 src = dest;
6360 while (diff > 0 && *src)
6362 rtx piece = *src;
6363 diff -= decl_piece_bitsize (piece);
6364 if (copy)
6365 src = &XEXP (piece, 1);
6366 else
6368 *src = XEXP (piece, 1);
6369 free_EXPR_LIST_node (piece);
6372 /* Add padding if needed. */
6373 if (diff < 0 && *src)
6375 if (!copy)
6376 dest = src;
6377 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6378 dest = &XEXP (*dest, 1);
6380 if (!copy)
6381 return;
6382 /* Finally copy all nodes following it. */
6383 while (*src)
6385 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6386 decl_piece_bitsize (*src), NULL_RTX);
6387 dest = &XEXP (*dest, 1);
6388 src = &XEXP (*src, 1);
6392 /* Add a variable location node to the linked list for DECL. */
6394 static struct var_loc_node *
6395 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6397 unsigned int decl_id;
6398 var_loc_list *temp;
6399 struct var_loc_node *loc = NULL;
6400 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6402 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6404 tree realdecl = DECL_DEBUG_EXPR (decl);
6405 if (handled_component_p (realdecl)
6406 || (TREE_CODE (realdecl) == MEM_REF
6407 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6409 bool reverse;
6410 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6411 &bitsize, &reverse);
6412 if (!innerdecl
6413 || !DECL_P (innerdecl)
6414 || DECL_IGNORED_P (innerdecl)
6415 || TREE_STATIC (innerdecl)
6416 || bitsize == 0
6417 || bitpos + bitsize > 256)
6418 return NULL;
6419 decl = innerdecl;
6423 decl_id = DECL_UID (decl);
6424 var_loc_list **slot
6425 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6426 if (*slot == NULL)
6428 temp = ggc_cleared_alloc<var_loc_list> ();
6429 temp->decl_id = decl_id;
6430 *slot = temp;
6432 else
6433 temp = *slot;
6435 /* For PARM_DECLs try to keep around the original incoming value,
6436 even if that means we'll emit a zero-range .debug_loc entry. */
6437 if (temp->last
6438 && temp->first == temp->last
6439 && TREE_CODE (decl) == PARM_DECL
6440 && NOTE_P (temp->first->loc)
6441 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6442 && DECL_INCOMING_RTL (decl)
6443 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6444 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6445 == GET_CODE (DECL_INCOMING_RTL (decl))
6446 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6447 && (bitsize != -1
6448 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6449 NOTE_VAR_LOCATION_LOC (loc_note))
6450 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6451 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6453 loc = ggc_cleared_alloc<var_loc_node> ();
6454 temp->first->next = loc;
6455 temp->last = loc;
6456 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6458 else if (temp->last)
6460 struct var_loc_node *last = temp->last, *unused = NULL;
6461 rtx *piece_loc = NULL, last_loc_note;
6462 HOST_WIDE_INT piece_bitpos = 0;
6463 if (last->next)
6465 last = last->next;
6466 gcc_assert (last->next == NULL);
6468 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6470 piece_loc = &last->loc;
6473 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6474 if (piece_bitpos + cur_bitsize > bitpos)
6475 break;
6476 piece_bitpos += cur_bitsize;
6477 piece_loc = &XEXP (*piece_loc, 1);
6479 while (*piece_loc);
6481 /* TEMP->LAST here is either pointer to the last but one or
6482 last element in the chained list, LAST is pointer to the
6483 last element. */
6484 if (label && strcmp (last->label, label) == 0 && last->view == view)
6486 /* For SRA optimized variables if there weren't any real
6487 insns since last note, just modify the last node. */
6488 if (piece_loc != NULL)
6490 adjust_piece_list (piece_loc, NULL, NULL,
6491 bitpos, piece_bitpos, bitsize, loc_note);
6492 return NULL;
6494 /* If the last note doesn't cover any instructions, remove it. */
6495 if (temp->last != last)
6497 temp->last->next = NULL;
6498 unused = last;
6499 last = temp->last;
6500 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6502 else
6504 gcc_assert (temp->first == temp->last
6505 || (temp->first->next == temp->last
6506 && TREE_CODE (decl) == PARM_DECL));
6507 memset (temp->last, '\0', sizeof (*temp->last));
6508 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6509 return temp->last;
6512 if (bitsize == -1 && NOTE_P (last->loc))
6513 last_loc_note = last->loc;
6514 else if (piece_loc != NULL
6515 && *piece_loc != NULL_RTX
6516 && piece_bitpos == bitpos
6517 && decl_piece_bitsize (*piece_loc) == bitsize)
6518 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6519 else
6520 last_loc_note = NULL_RTX;
6521 /* If the current location is the same as the end of the list,
6522 and either both or neither of the locations is uninitialized,
6523 we have nothing to do. */
6524 if (last_loc_note == NULL_RTX
6525 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6526 NOTE_VAR_LOCATION_LOC (loc_note)))
6527 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6528 != NOTE_VAR_LOCATION_STATUS (loc_note))
6529 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6530 == VAR_INIT_STATUS_UNINITIALIZED)
6531 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6532 == VAR_INIT_STATUS_UNINITIALIZED))))
6534 /* Add LOC to the end of list and update LAST. If the last
6535 element of the list has been removed above, reuse its
6536 memory for the new node, otherwise allocate a new one. */
6537 if (unused)
6539 loc = unused;
6540 memset (loc, '\0', sizeof (*loc));
6542 else
6543 loc = ggc_cleared_alloc<var_loc_node> ();
6544 if (bitsize == -1 || piece_loc == NULL)
6545 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6546 else
6547 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6548 bitpos, piece_bitpos, bitsize, loc_note);
6549 last->next = loc;
6550 /* Ensure TEMP->LAST will point either to the new last but one
6551 element of the chain, or to the last element in it. */
6552 if (last != temp->last)
6553 temp->last = last;
6555 else if (unused)
6556 ggc_free (unused);
6558 else
6560 loc = ggc_cleared_alloc<var_loc_node> ();
6561 temp->first = loc;
6562 temp->last = loc;
6563 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6565 return loc;
6568 /* Keep track of the number of spaces used to indent the
6569 output of the debugging routines that print the structure of
6570 the DIE internal representation. */
6571 static int print_indent;
6573 /* Indent the line the number of spaces given by print_indent. */
6575 static inline void
6576 print_spaces (FILE *outfile)
6578 fprintf (outfile, "%*s", print_indent, "");
6581 /* Print a type signature in hex. */
6583 static inline void
6584 print_signature (FILE *outfile, char *sig)
6586 int i;
6588 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6589 fprintf (outfile, "%02x", sig[i] & 0xff);
6592 static inline void
6593 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6595 if (discr_value->pos)
6596 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6597 else
6598 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6601 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6603 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6604 RECURSE, output location descriptor operations. */
6606 static void
6607 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6609 switch (val->val_class)
6611 case dw_val_class_addr:
6612 fprintf (outfile, "address");
6613 break;
6614 case dw_val_class_offset:
6615 fprintf (outfile, "offset");
6616 break;
6617 case dw_val_class_loc:
6618 fprintf (outfile, "location descriptor");
6619 if (val->v.val_loc == NULL)
6620 fprintf (outfile, " -> <null>\n");
6621 else if (recurse)
6623 fprintf (outfile, ":\n");
6624 print_indent += 4;
6625 print_loc_descr (val->v.val_loc, outfile);
6626 print_indent -= 4;
6628 else
6630 if (flag_dump_noaddr || flag_dump_unnumbered)
6631 fprintf (outfile, " #\n");
6632 else
6633 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6635 break;
6636 case dw_val_class_loc_list:
6637 fprintf (outfile, "location list -> label:%s",
6638 val->v.val_loc_list->ll_symbol);
6639 break;
6640 case dw_val_class_view_list:
6641 val = view_list_to_loc_list_val_node (val);
6642 fprintf (outfile, "location list with views -> labels:%s and %s",
6643 val->v.val_loc_list->ll_symbol,
6644 val->v.val_loc_list->vl_symbol);
6645 break;
6646 case dw_val_class_range_list:
6647 fprintf (outfile, "range list");
6648 break;
6649 case dw_val_class_const:
6650 case dw_val_class_const_implicit:
6651 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6652 break;
6653 case dw_val_class_unsigned_const:
6654 case dw_val_class_unsigned_const_implicit:
6655 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6656 break;
6657 case dw_val_class_const_double:
6658 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6659 HOST_WIDE_INT_PRINT_UNSIGNED")",
6660 val->v.val_double.high,
6661 val->v.val_double.low);
6662 break;
6663 case dw_val_class_wide_int:
6665 int i = val->v.val_wide->get_len ();
6666 fprintf (outfile, "constant (");
6667 gcc_assert (i > 0);
6668 if (val->v.val_wide->elt (i - 1) == 0)
6669 fprintf (outfile, "0x");
6670 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6671 val->v.val_wide->elt (--i));
6672 while (--i >= 0)
6673 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6674 val->v.val_wide->elt (i));
6675 fprintf (outfile, ")");
6676 break;
6678 case dw_val_class_vec:
6679 fprintf (outfile, "floating-point or vector constant");
6680 break;
6681 case dw_val_class_flag:
6682 fprintf (outfile, "%u", val->v.val_flag);
6683 break;
6684 case dw_val_class_die_ref:
6685 if (val->v.val_die_ref.die != NULL)
6687 dw_die_ref die = val->v.val_die_ref.die;
6689 if (die->comdat_type_p)
6691 fprintf (outfile, "die -> signature: ");
6692 print_signature (outfile,
6693 die->die_id.die_type_node->signature);
6695 else if (die->die_id.die_symbol)
6697 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6698 if (die->with_offset)
6699 fprintf (outfile, " + %ld", die->die_offset);
6701 else
6702 fprintf (outfile, "die -> %ld", die->die_offset);
6703 if (flag_dump_noaddr || flag_dump_unnumbered)
6704 fprintf (outfile, " #");
6705 else
6706 fprintf (outfile, " (%p)", (void *) die);
6708 else
6709 fprintf (outfile, "die -> <null>");
6710 break;
6711 case dw_val_class_vms_delta:
6712 fprintf (outfile, "delta: @slotcount(%s-%s)",
6713 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6714 break;
6715 case dw_val_class_symview:
6716 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6717 break;
6718 case dw_val_class_lbl_id:
6719 case dw_val_class_lineptr:
6720 case dw_val_class_macptr:
6721 case dw_val_class_loclistsptr:
6722 case dw_val_class_high_pc:
6723 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6724 break;
6725 case dw_val_class_str:
6726 if (val->v.val_str->str != NULL)
6727 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6728 else
6729 fprintf (outfile, "<null>");
6730 break;
6731 case dw_val_class_file:
6732 case dw_val_class_file_implicit:
6733 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6734 val->v.val_file->emitted_number);
6735 break;
6736 case dw_val_class_data8:
6738 int i;
6740 for (i = 0; i < 8; i++)
6741 fprintf (outfile, "%02x", val->v.val_data8[i]);
6742 break;
6744 case dw_val_class_discr_value:
6745 print_discr_value (outfile, &val->v.val_discr_value);
6746 break;
6747 case dw_val_class_discr_list:
6748 for (dw_discr_list_ref node = val->v.val_discr_list;
6749 node != NULL;
6750 node = node->dw_discr_next)
6752 if (node->dw_discr_range)
6754 fprintf (outfile, " .. ");
6755 print_discr_value (outfile, &node->dw_discr_lower_bound);
6756 print_discr_value (outfile, &node->dw_discr_upper_bound);
6758 else
6759 print_discr_value (outfile, &node->dw_discr_lower_bound);
6761 if (node->dw_discr_next != NULL)
6762 fprintf (outfile, " | ");
6764 default:
6765 break;
6769 /* Likewise, for a DIE attribute. */
6771 static void
6772 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6774 print_dw_val (&a->dw_attr_val, recurse, outfile);
6778 /* Print the list of operands in the LOC location description to OUTFILE. This
6779 routine is a debugging aid only. */
6781 static void
6782 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6784 dw_loc_descr_ref l = loc;
6786 if (loc == NULL)
6788 print_spaces (outfile);
6789 fprintf (outfile, "<null>\n");
6790 return;
6793 for (l = loc; l != NULL; l = l->dw_loc_next)
6795 print_spaces (outfile);
6796 if (flag_dump_noaddr || flag_dump_unnumbered)
6797 fprintf (outfile, "#");
6798 else
6799 fprintf (outfile, "(%p)", (void *) l);
6800 fprintf (outfile, " %s",
6801 dwarf_stack_op_name (l->dw_loc_opc));
6802 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6804 fprintf (outfile, " ");
6805 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6807 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6809 fprintf (outfile, ", ");
6810 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6812 fprintf (outfile, "\n");
6816 /* Print the information associated with a given DIE, and its children.
6817 This routine is a debugging aid only. */
6819 static void
6820 print_die (dw_die_ref die, FILE *outfile)
6822 dw_attr_node *a;
6823 dw_die_ref c;
6824 unsigned ix;
6826 print_spaces (outfile);
6827 fprintf (outfile, "DIE %4ld: %s ",
6828 die->die_offset, dwarf_tag_name (die->die_tag));
6829 if (flag_dump_noaddr || flag_dump_unnumbered)
6830 fprintf (outfile, "#\n");
6831 else
6832 fprintf (outfile, "(%p)\n", (void*) die);
6833 print_spaces (outfile);
6834 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6835 fprintf (outfile, " offset: %ld", die->die_offset);
6836 fprintf (outfile, " mark: %d\n", die->die_mark);
6838 if (die->comdat_type_p)
6840 print_spaces (outfile);
6841 fprintf (outfile, " signature: ");
6842 print_signature (outfile, die->die_id.die_type_node->signature);
6843 fprintf (outfile, "\n");
6846 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6848 print_spaces (outfile);
6849 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6851 print_attribute (a, true, outfile);
6852 fprintf (outfile, "\n");
6855 if (die->die_child != NULL)
6857 print_indent += 4;
6858 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6859 print_indent -= 4;
6861 if (print_indent == 0)
6862 fprintf (outfile, "\n");
6865 /* Print the list of operations in the LOC location description. */
6867 DEBUG_FUNCTION void
6868 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6870 print_loc_descr (loc, stderr);
6873 /* Print the information collected for a given DIE. */
6875 DEBUG_FUNCTION void
6876 debug_dwarf_die (dw_die_ref die)
6878 print_die (die, stderr);
6881 DEBUG_FUNCTION void
6882 debug (die_struct &ref)
6884 print_die (&ref, stderr);
6887 DEBUG_FUNCTION void
6888 debug (die_struct *ptr)
6890 if (ptr)
6891 debug (*ptr);
6892 else
6893 fprintf (stderr, "<nil>\n");
6897 /* Print all DWARF information collected for the compilation unit.
6898 This routine is a debugging aid only. */
6900 DEBUG_FUNCTION void
6901 debug_dwarf (void)
6903 print_indent = 0;
6904 print_die (comp_unit_die (), stderr);
6907 /* Verify the DIE tree structure. */
6909 DEBUG_FUNCTION void
6910 verify_die (dw_die_ref die)
6912 gcc_assert (!die->die_mark);
6913 if (die->die_parent == NULL
6914 && die->die_sib == NULL)
6915 return;
6916 /* Verify the die_sib list is cyclic. */
6917 dw_die_ref x = die;
6920 x->die_mark = 1;
6921 x = x->die_sib;
6923 while (x && !x->die_mark);
6924 gcc_assert (x == die);
6925 x = die;
6928 /* Verify all dies have the same parent. */
6929 gcc_assert (x->die_parent == die->die_parent);
6930 if (x->die_child)
6932 /* Verify the child has the proper parent and recurse. */
6933 gcc_assert (x->die_child->die_parent == x);
6934 verify_die (x->die_child);
6936 x->die_mark = 0;
6937 x = x->die_sib;
6939 while (x && x->die_mark);
6942 /* Sanity checks on DIEs. */
6944 static void
6945 check_die (dw_die_ref die)
6947 unsigned ix;
6948 dw_attr_node *a;
6949 bool inline_found = false;
6950 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6951 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6952 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6954 switch (a->dw_attr)
6956 case DW_AT_inline:
6957 if (a->dw_attr_val.v.val_unsigned)
6958 inline_found = true;
6959 break;
6960 case DW_AT_location:
6961 ++n_location;
6962 break;
6963 case DW_AT_low_pc:
6964 ++n_low_pc;
6965 break;
6966 case DW_AT_high_pc:
6967 ++n_high_pc;
6968 break;
6969 case DW_AT_artificial:
6970 ++n_artificial;
6971 break;
6972 case DW_AT_decl_column:
6973 ++n_decl_column;
6974 break;
6975 case DW_AT_decl_line:
6976 ++n_decl_line;
6977 break;
6978 case DW_AT_decl_file:
6979 ++n_decl_file;
6980 break;
6981 default:
6982 break;
6985 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6986 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6988 fprintf (stderr, "Duplicate attributes in DIE:\n");
6989 debug_dwarf_die (die);
6990 gcc_unreachable ();
6992 if (inline_found)
6994 /* A debugging information entry that is a member of an abstract
6995 instance tree [that has DW_AT_inline] should not contain any
6996 attributes which describe aspects of the subroutine which vary
6997 between distinct inlined expansions or distinct out-of-line
6998 expansions. */
6999 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7000 gcc_assert (a->dw_attr != DW_AT_low_pc
7001 && a->dw_attr != DW_AT_high_pc
7002 && a->dw_attr != DW_AT_location
7003 && a->dw_attr != DW_AT_frame_base
7004 && a->dw_attr != DW_AT_call_all_calls
7005 && a->dw_attr != DW_AT_GNU_all_call_sites);
7009 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7010 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7011 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7013 /* Calculate the checksum of a location expression. */
7015 static inline void
7016 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7018 int tem;
7019 inchash::hash hstate;
7020 hashval_t hash;
7022 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7023 CHECKSUM (tem);
7024 hash_loc_operands (loc, hstate);
7025 hash = hstate.end();
7026 CHECKSUM (hash);
7029 /* Calculate the checksum of an attribute. */
7031 static void
7032 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
7034 dw_loc_descr_ref loc;
7035 rtx r;
7037 CHECKSUM (at->dw_attr);
7039 /* We don't care that this was compiled with a different compiler
7040 snapshot; if the output is the same, that's what matters. */
7041 if (at->dw_attr == DW_AT_producer)
7042 return;
7044 switch (AT_class (at))
7046 case dw_val_class_const:
7047 case dw_val_class_const_implicit:
7048 CHECKSUM (at->dw_attr_val.v.val_int);
7049 break;
7050 case dw_val_class_unsigned_const:
7051 case dw_val_class_unsigned_const_implicit:
7052 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7053 break;
7054 case dw_val_class_const_double:
7055 CHECKSUM (at->dw_attr_val.v.val_double);
7056 break;
7057 case dw_val_class_wide_int:
7058 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7059 get_full_len (*at->dw_attr_val.v.val_wide)
7060 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7061 break;
7062 case dw_val_class_vec:
7063 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7064 (at->dw_attr_val.v.val_vec.length
7065 * at->dw_attr_val.v.val_vec.elt_size));
7066 break;
7067 case dw_val_class_flag:
7068 CHECKSUM (at->dw_attr_val.v.val_flag);
7069 break;
7070 case dw_val_class_str:
7071 CHECKSUM_STRING (AT_string (at));
7072 break;
7074 case dw_val_class_addr:
7075 r = AT_addr (at);
7076 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7077 CHECKSUM_STRING (XSTR (r, 0));
7078 break;
7080 case dw_val_class_offset:
7081 CHECKSUM (at->dw_attr_val.v.val_offset);
7082 break;
7084 case dw_val_class_loc:
7085 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7086 loc_checksum (loc, ctx);
7087 break;
7089 case dw_val_class_die_ref:
7090 die_checksum (AT_ref (at), ctx, mark);
7091 break;
7093 case dw_val_class_fde_ref:
7094 case dw_val_class_vms_delta:
7095 case dw_val_class_symview:
7096 case dw_val_class_lbl_id:
7097 case dw_val_class_lineptr:
7098 case dw_val_class_macptr:
7099 case dw_val_class_loclistsptr:
7100 case dw_val_class_high_pc:
7101 break;
7103 case dw_val_class_file:
7104 case dw_val_class_file_implicit:
7105 CHECKSUM_STRING (AT_file (at)->filename);
7106 break;
7108 case dw_val_class_data8:
7109 CHECKSUM (at->dw_attr_val.v.val_data8);
7110 break;
7112 default:
7113 break;
7117 /* Calculate the checksum of a DIE. */
7119 static void
7120 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7122 dw_die_ref c;
7123 dw_attr_node *a;
7124 unsigned ix;
7126 /* To avoid infinite recursion. */
7127 if (die->die_mark)
7129 CHECKSUM (die->die_mark);
7130 return;
7132 die->die_mark = ++(*mark);
7134 CHECKSUM (die->die_tag);
7136 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7137 attr_checksum (a, ctx, mark);
7139 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7142 #undef CHECKSUM
7143 #undef CHECKSUM_BLOCK
7144 #undef CHECKSUM_STRING
7146 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7147 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7148 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7149 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7150 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7151 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7152 #define CHECKSUM_ATTR(FOO) \
7153 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7155 /* Calculate the checksum of a number in signed LEB128 format. */
7157 static void
7158 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7160 unsigned char byte;
7161 bool more;
7163 while (1)
7165 byte = (value & 0x7f);
7166 value >>= 7;
7167 more = !((value == 0 && (byte & 0x40) == 0)
7168 || (value == -1 && (byte & 0x40) != 0));
7169 if (more)
7170 byte |= 0x80;
7171 CHECKSUM (byte);
7172 if (!more)
7173 break;
7177 /* Calculate the checksum of a number in unsigned LEB128 format. */
7179 static void
7180 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7182 while (1)
7184 unsigned char byte = (value & 0x7f);
7185 value >>= 7;
7186 if (value != 0)
7187 /* More bytes to follow. */
7188 byte |= 0x80;
7189 CHECKSUM (byte);
7190 if (value == 0)
7191 break;
7195 /* Checksum the context of the DIE. This adds the names of any
7196 surrounding namespaces or structures to the checksum. */
7198 static void
7199 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7201 const char *name;
7202 dw_die_ref spec;
7203 int tag = die->die_tag;
7205 if (tag != DW_TAG_namespace
7206 && tag != DW_TAG_structure_type
7207 && tag != DW_TAG_class_type)
7208 return;
7210 name = get_AT_string (die, DW_AT_name);
7212 spec = get_AT_ref (die, DW_AT_specification);
7213 if (spec != NULL)
7214 die = spec;
7216 if (die->die_parent != NULL)
7217 checksum_die_context (die->die_parent, ctx);
7219 CHECKSUM_ULEB128 ('C');
7220 CHECKSUM_ULEB128 (tag);
7221 if (name != NULL)
7222 CHECKSUM_STRING (name);
7225 /* Calculate the checksum of a location expression. */
7227 static inline void
7228 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7230 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7231 were emitted as a DW_FORM_sdata instead of a location expression. */
7232 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7234 CHECKSUM_ULEB128 (DW_FORM_sdata);
7235 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7236 return;
7239 /* Otherwise, just checksum the raw location expression. */
7240 while (loc != NULL)
7242 inchash::hash hstate;
7243 hashval_t hash;
7245 CHECKSUM_ULEB128 (loc->dtprel);
7246 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7247 hash_loc_operands (loc, hstate);
7248 hash = hstate.end ();
7249 CHECKSUM (hash);
7250 loc = loc->dw_loc_next;
7254 /* Calculate the checksum of an attribute. */
7256 static void
7257 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7258 struct md5_ctx *ctx, int *mark)
7260 dw_loc_descr_ref loc;
7261 rtx r;
7263 if (AT_class (at) == dw_val_class_die_ref)
7265 dw_die_ref target_die = AT_ref (at);
7267 /* For pointer and reference types, we checksum only the (qualified)
7268 name of the target type (if there is a name). For friend entries,
7269 we checksum only the (qualified) name of the target type or function.
7270 This allows the checksum to remain the same whether the target type
7271 is complete or not. */
7272 if ((at->dw_attr == DW_AT_type
7273 && (tag == DW_TAG_pointer_type
7274 || tag == DW_TAG_reference_type
7275 || tag == DW_TAG_rvalue_reference_type
7276 || tag == DW_TAG_ptr_to_member_type))
7277 || (at->dw_attr == DW_AT_friend
7278 && tag == DW_TAG_friend))
7280 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7282 if (name_attr != NULL)
7284 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7286 if (decl == NULL)
7287 decl = target_die;
7288 CHECKSUM_ULEB128 ('N');
7289 CHECKSUM_ULEB128 (at->dw_attr);
7290 if (decl->die_parent != NULL)
7291 checksum_die_context (decl->die_parent, ctx);
7292 CHECKSUM_ULEB128 ('E');
7293 CHECKSUM_STRING (AT_string (name_attr));
7294 return;
7298 /* For all other references to another DIE, we check to see if the
7299 target DIE has already been visited. If it has, we emit a
7300 backward reference; if not, we descend recursively. */
7301 if (target_die->die_mark > 0)
7303 CHECKSUM_ULEB128 ('R');
7304 CHECKSUM_ULEB128 (at->dw_attr);
7305 CHECKSUM_ULEB128 (target_die->die_mark);
7307 else
7309 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7311 if (decl == NULL)
7312 decl = target_die;
7313 target_die->die_mark = ++(*mark);
7314 CHECKSUM_ULEB128 ('T');
7315 CHECKSUM_ULEB128 (at->dw_attr);
7316 if (decl->die_parent != NULL)
7317 checksum_die_context (decl->die_parent, ctx);
7318 die_checksum_ordered (target_die, ctx, mark);
7320 return;
7323 CHECKSUM_ULEB128 ('A');
7324 CHECKSUM_ULEB128 (at->dw_attr);
7326 switch (AT_class (at))
7328 case dw_val_class_const:
7329 case dw_val_class_const_implicit:
7330 CHECKSUM_ULEB128 (DW_FORM_sdata);
7331 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7332 break;
7334 case dw_val_class_unsigned_const:
7335 case dw_val_class_unsigned_const_implicit:
7336 CHECKSUM_ULEB128 (DW_FORM_sdata);
7337 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7338 break;
7340 case dw_val_class_const_double:
7341 CHECKSUM_ULEB128 (DW_FORM_block);
7342 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7343 CHECKSUM (at->dw_attr_val.v.val_double);
7344 break;
7346 case dw_val_class_wide_int:
7347 CHECKSUM_ULEB128 (DW_FORM_block);
7348 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7349 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7350 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7351 get_full_len (*at->dw_attr_val.v.val_wide)
7352 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7353 break;
7355 case dw_val_class_vec:
7356 CHECKSUM_ULEB128 (DW_FORM_block);
7357 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7358 * at->dw_attr_val.v.val_vec.elt_size);
7359 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7360 (at->dw_attr_val.v.val_vec.length
7361 * at->dw_attr_val.v.val_vec.elt_size));
7362 break;
7364 case dw_val_class_flag:
7365 CHECKSUM_ULEB128 (DW_FORM_flag);
7366 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7367 break;
7369 case dw_val_class_str:
7370 CHECKSUM_ULEB128 (DW_FORM_string);
7371 CHECKSUM_STRING (AT_string (at));
7372 break;
7374 case dw_val_class_addr:
7375 r = AT_addr (at);
7376 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7377 CHECKSUM_ULEB128 (DW_FORM_string);
7378 CHECKSUM_STRING (XSTR (r, 0));
7379 break;
7381 case dw_val_class_offset:
7382 CHECKSUM_ULEB128 (DW_FORM_sdata);
7383 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7384 break;
7386 case dw_val_class_loc:
7387 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7388 loc_checksum_ordered (loc, ctx);
7389 break;
7391 case dw_val_class_fde_ref:
7392 case dw_val_class_symview:
7393 case dw_val_class_lbl_id:
7394 case dw_val_class_lineptr:
7395 case dw_val_class_macptr:
7396 case dw_val_class_loclistsptr:
7397 case dw_val_class_high_pc:
7398 break;
7400 case dw_val_class_file:
7401 case dw_val_class_file_implicit:
7402 CHECKSUM_ULEB128 (DW_FORM_string);
7403 CHECKSUM_STRING (AT_file (at)->filename);
7404 break;
7406 case dw_val_class_data8:
7407 CHECKSUM (at->dw_attr_val.v.val_data8);
7408 break;
7410 default:
7411 break;
7415 struct checksum_attributes
7417 dw_attr_node *at_name;
7418 dw_attr_node *at_type;
7419 dw_attr_node *at_friend;
7420 dw_attr_node *at_accessibility;
7421 dw_attr_node *at_address_class;
7422 dw_attr_node *at_alignment;
7423 dw_attr_node *at_allocated;
7424 dw_attr_node *at_artificial;
7425 dw_attr_node *at_associated;
7426 dw_attr_node *at_binary_scale;
7427 dw_attr_node *at_bit_offset;
7428 dw_attr_node *at_bit_size;
7429 dw_attr_node *at_bit_stride;
7430 dw_attr_node *at_byte_size;
7431 dw_attr_node *at_byte_stride;
7432 dw_attr_node *at_const_value;
7433 dw_attr_node *at_containing_type;
7434 dw_attr_node *at_count;
7435 dw_attr_node *at_data_location;
7436 dw_attr_node *at_data_member_location;
7437 dw_attr_node *at_decimal_scale;
7438 dw_attr_node *at_decimal_sign;
7439 dw_attr_node *at_default_value;
7440 dw_attr_node *at_digit_count;
7441 dw_attr_node *at_discr;
7442 dw_attr_node *at_discr_list;
7443 dw_attr_node *at_discr_value;
7444 dw_attr_node *at_encoding;
7445 dw_attr_node *at_endianity;
7446 dw_attr_node *at_explicit;
7447 dw_attr_node *at_is_optional;
7448 dw_attr_node *at_location;
7449 dw_attr_node *at_lower_bound;
7450 dw_attr_node *at_mutable;
7451 dw_attr_node *at_ordering;
7452 dw_attr_node *at_picture_string;
7453 dw_attr_node *at_prototyped;
7454 dw_attr_node *at_small;
7455 dw_attr_node *at_segment;
7456 dw_attr_node *at_string_length;
7457 dw_attr_node *at_string_length_bit_size;
7458 dw_attr_node *at_string_length_byte_size;
7459 dw_attr_node *at_threads_scaled;
7460 dw_attr_node *at_upper_bound;
7461 dw_attr_node *at_use_location;
7462 dw_attr_node *at_use_UTF8;
7463 dw_attr_node *at_variable_parameter;
7464 dw_attr_node *at_virtuality;
7465 dw_attr_node *at_visibility;
7466 dw_attr_node *at_vtable_elem_location;
7469 /* Collect the attributes that we will want to use for the checksum. */
7471 static void
7472 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7474 dw_attr_node *a;
7475 unsigned ix;
7477 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7479 switch (a->dw_attr)
7481 case DW_AT_name:
7482 attrs->at_name = a;
7483 break;
7484 case DW_AT_type:
7485 attrs->at_type = a;
7486 break;
7487 case DW_AT_friend:
7488 attrs->at_friend = a;
7489 break;
7490 case DW_AT_accessibility:
7491 attrs->at_accessibility = a;
7492 break;
7493 case DW_AT_address_class:
7494 attrs->at_address_class = a;
7495 break;
7496 case DW_AT_alignment:
7497 attrs->at_alignment = a;
7498 break;
7499 case DW_AT_allocated:
7500 attrs->at_allocated = a;
7501 break;
7502 case DW_AT_artificial:
7503 attrs->at_artificial = a;
7504 break;
7505 case DW_AT_associated:
7506 attrs->at_associated = a;
7507 break;
7508 case DW_AT_binary_scale:
7509 attrs->at_binary_scale = a;
7510 break;
7511 case DW_AT_bit_offset:
7512 attrs->at_bit_offset = a;
7513 break;
7514 case DW_AT_bit_size:
7515 attrs->at_bit_size = a;
7516 break;
7517 case DW_AT_bit_stride:
7518 attrs->at_bit_stride = a;
7519 break;
7520 case DW_AT_byte_size:
7521 attrs->at_byte_size = a;
7522 break;
7523 case DW_AT_byte_stride:
7524 attrs->at_byte_stride = a;
7525 break;
7526 case DW_AT_const_value:
7527 attrs->at_const_value = a;
7528 break;
7529 case DW_AT_containing_type:
7530 attrs->at_containing_type = a;
7531 break;
7532 case DW_AT_count:
7533 attrs->at_count = a;
7534 break;
7535 case DW_AT_data_location:
7536 attrs->at_data_location = a;
7537 break;
7538 case DW_AT_data_member_location:
7539 attrs->at_data_member_location = a;
7540 break;
7541 case DW_AT_decimal_scale:
7542 attrs->at_decimal_scale = a;
7543 break;
7544 case DW_AT_decimal_sign:
7545 attrs->at_decimal_sign = a;
7546 break;
7547 case DW_AT_default_value:
7548 attrs->at_default_value = a;
7549 break;
7550 case DW_AT_digit_count:
7551 attrs->at_digit_count = a;
7552 break;
7553 case DW_AT_discr:
7554 attrs->at_discr = a;
7555 break;
7556 case DW_AT_discr_list:
7557 attrs->at_discr_list = a;
7558 break;
7559 case DW_AT_discr_value:
7560 attrs->at_discr_value = a;
7561 break;
7562 case DW_AT_encoding:
7563 attrs->at_encoding = a;
7564 break;
7565 case DW_AT_endianity:
7566 attrs->at_endianity = a;
7567 break;
7568 case DW_AT_explicit:
7569 attrs->at_explicit = a;
7570 break;
7571 case DW_AT_is_optional:
7572 attrs->at_is_optional = a;
7573 break;
7574 case DW_AT_location:
7575 attrs->at_location = a;
7576 break;
7577 case DW_AT_lower_bound:
7578 attrs->at_lower_bound = a;
7579 break;
7580 case DW_AT_mutable:
7581 attrs->at_mutable = a;
7582 break;
7583 case DW_AT_ordering:
7584 attrs->at_ordering = a;
7585 break;
7586 case DW_AT_picture_string:
7587 attrs->at_picture_string = a;
7588 break;
7589 case DW_AT_prototyped:
7590 attrs->at_prototyped = a;
7591 break;
7592 case DW_AT_small:
7593 attrs->at_small = a;
7594 break;
7595 case DW_AT_segment:
7596 attrs->at_segment = a;
7597 break;
7598 case DW_AT_string_length:
7599 attrs->at_string_length = a;
7600 break;
7601 case DW_AT_string_length_bit_size:
7602 attrs->at_string_length_bit_size = a;
7603 break;
7604 case DW_AT_string_length_byte_size:
7605 attrs->at_string_length_byte_size = a;
7606 break;
7607 case DW_AT_threads_scaled:
7608 attrs->at_threads_scaled = a;
7609 break;
7610 case DW_AT_upper_bound:
7611 attrs->at_upper_bound = a;
7612 break;
7613 case DW_AT_use_location:
7614 attrs->at_use_location = a;
7615 break;
7616 case DW_AT_use_UTF8:
7617 attrs->at_use_UTF8 = a;
7618 break;
7619 case DW_AT_variable_parameter:
7620 attrs->at_variable_parameter = a;
7621 break;
7622 case DW_AT_virtuality:
7623 attrs->at_virtuality = a;
7624 break;
7625 case DW_AT_visibility:
7626 attrs->at_visibility = a;
7627 break;
7628 case DW_AT_vtable_elem_location:
7629 attrs->at_vtable_elem_location = a;
7630 break;
7631 default:
7632 break;
7637 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7639 static void
7640 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7642 dw_die_ref c;
7643 dw_die_ref decl;
7644 struct checksum_attributes attrs;
7646 CHECKSUM_ULEB128 ('D');
7647 CHECKSUM_ULEB128 (die->die_tag);
7649 memset (&attrs, 0, sizeof (attrs));
7651 decl = get_AT_ref (die, DW_AT_specification);
7652 if (decl != NULL)
7653 collect_checksum_attributes (&attrs, decl);
7654 collect_checksum_attributes (&attrs, die);
7656 CHECKSUM_ATTR (attrs.at_name);
7657 CHECKSUM_ATTR (attrs.at_accessibility);
7658 CHECKSUM_ATTR (attrs.at_address_class);
7659 CHECKSUM_ATTR (attrs.at_allocated);
7660 CHECKSUM_ATTR (attrs.at_artificial);
7661 CHECKSUM_ATTR (attrs.at_associated);
7662 CHECKSUM_ATTR (attrs.at_binary_scale);
7663 CHECKSUM_ATTR (attrs.at_bit_offset);
7664 CHECKSUM_ATTR (attrs.at_bit_size);
7665 CHECKSUM_ATTR (attrs.at_bit_stride);
7666 CHECKSUM_ATTR (attrs.at_byte_size);
7667 CHECKSUM_ATTR (attrs.at_byte_stride);
7668 CHECKSUM_ATTR (attrs.at_const_value);
7669 CHECKSUM_ATTR (attrs.at_containing_type);
7670 CHECKSUM_ATTR (attrs.at_count);
7671 CHECKSUM_ATTR (attrs.at_data_location);
7672 CHECKSUM_ATTR (attrs.at_data_member_location);
7673 CHECKSUM_ATTR (attrs.at_decimal_scale);
7674 CHECKSUM_ATTR (attrs.at_decimal_sign);
7675 CHECKSUM_ATTR (attrs.at_default_value);
7676 CHECKSUM_ATTR (attrs.at_digit_count);
7677 CHECKSUM_ATTR (attrs.at_discr);
7678 CHECKSUM_ATTR (attrs.at_discr_list);
7679 CHECKSUM_ATTR (attrs.at_discr_value);
7680 CHECKSUM_ATTR (attrs.at_encoding);
7681 CHECKSUM_ATTR (attrs.at_endianity);
7682 CHECKSUM_ATTR (attrs.at_explicit);
7683 CHECKSUM_ATTR (attrs.at_is_optional);
7684 CHECKSUM_ATTR (attrs.at_location);
7685 CHECKSUM_ATTR (attrs.at_lower_bound);
7686 CHECKSUM_ATTR (attrs.at_mutable);
7687 CHECKSUM_ATTR (attrs.at_ordering);
7688 CHECKSUM_ATTR (attrs.at_picture_string);
7689 CHECKSUM_ATTR (attrs.at_prototyped);
7690 CHECKSUM_ATTR (attrs.at_small);
7691 CHECKSUM_ATTR (attrs.at_segment);
7692 CHECKSUM_ATTR (attrs.at_string_length);
7693 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7694 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7695 CHECKSUM_ATTR (attrs.at_threads_scaled);
7696 CHECKSUM_ATTR (attrs.at_upper_bound);
7697 CHECKSUM_ATTR (attrs.at_use_location);
7698 CHECKSUM_ATTR (attrs.at_use_UTF8);
7699 CHECKSUM_ATTR (attrs.at_variable_parameter);
7700 CHECKSUM_ATTR (attrs.at_virtuality);
7701 CHECKSUM_ATTR (attrs.at_visibility);
7702 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7703 CHECKSUM_ATTR (attrs.at_type);
7704 CHECKSUM_ATTR (attrs.at_friend);
7705 CHECKSUM_ATTR (attrs.at_alignment);
7707 /* Checksum the child DIEs. */
7708 c = die->die_child;
7709 if (c) do {
7710 dw_attr_node *name_attr;
7712 c = c->die_sib;
7713 name_attr = get_AT (c, DW_AT_name);
7714 if (is_template_instantiation (c))
7716 /* Ignore instantiations of member type and function templates. */
7718 else if (name_attr != NULL
7719 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7721 /* Use a shallow checksum for named nested types and member
7722 functions. */
7723 CHECKSUM_ULEB128 ('S');
7724 CHECKSUM_ULEB128 (c->die_tag);
7725 CHECKSUM_STRING (AT_string (name_attr));
7727 else
7729 /* Use a deep checksum for other children. */
7730 /* Mark this DIE so it gets processed when unmarking. */
7731 if (c->die_mark == 0)
7732 c->die_mark = -1;
7733 die_checksum_ordered (c, ctx, mark);
7735 } while (c != die->die_child);
7737 CHECKSUM_ULEB128 (0);
7740 /* Add a type name and tag to a hash. */
7741 static void
7742 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7744 CHECKSUM_ULEB128 (tag);
7745 CHECKSUM_STRING (name);
7748 #undef CHECKSUM
7749 #undef CHECKSUM_STRING
7750 #undef CHECKSUM_ATTR
7751 #undef CHECKSUM_LEB128
7752 #undef CHECKSUM_ULEB128
7754 /* Generate the type signature for DIE. This is computed by generating an
7755 MD5 checksum over the DIE's tag, its relevant attributes, and its
7756 children. Attributes that are references to other DIEs are processed
7757 by recursion, using the MARK field to prevent infinite recursion.
7758 If the DIE is nested inside a namespace or another type, we also
7759 need to include that context in the signature. The lower 64 bits
7760 of the resulting MD5 checksum comprise the signature. */
7762 static void
7763 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7765 int mark;
7766 const char *name;
7767 unsigned char checksum[16];
7768 struct md5_ctx ctx;
7769 dw_die_ref decl;
7770 dw_die_ref parent;
7772 name = get_AT_string (die, DW_AT_name);
7773 decl = get_AT_ref (die, DW_AT_specification);
7774 parent = get_die_parent (die);
7776 /* First, compute a signature for just the type name (and its surrounding
7777 context, if any. This is stored in the type unit DIE for link-time
7778 ODR (one-definition rule) checking. */
7780 if (is_cxx () && name != NULL)
7782 md5_init_ctx (&ctx);
7784 /* Checksum the names of surrounding namespaces and structures. */
7785 if (parent != NULL)
7786 checksum_die_context (parent, &ctx);
7788 /* Checksum the current DIE. */
7789 die_odr_checksum (die->die_tag, name, &ctx);
7790 md5_finish_ctx (&ctx, checksum);
7792 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7795 /* Next, compute the complete type signature. */
7797 md5_init_ctx (&ctx);
7798 mark = 1;
7799 die->die_mark = mark;
7801 /* Checksum the names of surrounding namespaces and structures. */
7802 if (parent != NULL)
7803 checksum_die_context (parent, &ctx);
7805 /* Checksum the DIE and its children. */
7806 die_checksum_ordered (die, &ctx, &mark);
7807 unmark_all_dies (die);
7808 md5_finish_ctx (&ctx, checksum);
7810 /* Store the signature in the type node and link the type DIE and the
7811 type node together. */
7812 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7813 DWARF_TYPE_SIGNATURE_SIZE);
7814 die->comdat_type_p = true;
7815 die->die_id.die_type_node = type_node;
7816 type_node->type_die = die;
7818 /* If the DIE is a specification, link its declaration to the type node
7819 as well. */
7820 if (decl != NULL)
7822 decl->comdat_type_p = true;
7823 decl->die_id.die_type_node = type_node;
7827 /* Do the location expressions look same? */
7828 static inline int
7829 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7831 return loc1->dw_loc_opc == loc2->dw_loc_opc
7832 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7833 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7836 /* Do the values look the same? */
7837 static int
7838 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7840 dw_loc_descr_ref loc1, loc2;
7841 rtx r1, r2;
7843 if (v1->val_class != v2->val_class)
7844 return 0;
7846 switch (v1->val_class)
7848 case dw_val_class_const:
7849 case dw_val_class_const_implicit:
7850 return v1->v.val_int == v2->v.val_int;
7851 case dw_val_class_unsigned_const:
7852 case dw_val_class_unsigned_const_implicit:
7853 return v1->v.val_unsigned == v2->v.val_unsigned;
7854 case dw_val_class_const_double:
7855 return v1->v.val_double.high == v2->v.val_double.high
7856 && v1->v.val_double.low == v2->v.val_double.low;
7857 case dw_val_class_wide_int:
7858 return *v1->v.val_wide == *v2->v.val_wide;
7859 case dw_val_class_vec:
7860 if (v1->v.val_vec.length != v2->v.val_vec.length
7861 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7862 return 0;
7863 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7864 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7865 return 0;
7866 return 1;
7867 case dw_val_class_flag:
7868 return v1->v.val_flag == v2->v.val_flag;
7869 case dw_val_class_str:
7870 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7872 case dw_val_class_addr:
7873 r1 = v1->v.val_addr;
7874 r2 = v2->v.val_addr;
7875 if (GET_CODE (r1) != GET_CODE (r2))
7876 return 0;
7877 return !rtx_equal_p (r1, r2);
7879 case dw_val_class_offset:
7880 return v1->v.val_offset == v2->v.val_offset;
7882 case dw_val_class_loc:
7883 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7884 loc1 && loc2;
7885 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7886 if (!same_loc_p (loc1, loc2, mark))
7887 return 0;
7888 return !loc1 && !loc2;
7890 case dw_val_class_die_ref:
7891 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7893 case dw_val_class_symview:
7894 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7896 case dw_val_class_fde_ref:
7897 case dw_val_class_vms_delta:
7898 case dw_val_class_lbl_id:
7899 case dw_val_class_lineptr:
7900 case dw_val_class_macptr:
7901 case dw_val_class_loclistsptr:
7902 case dw_val_class_high_pc:
7903 return 1;
7905 case dw_val_class_file:
7906 case dw_val_class_file_implicit:
7907 return v1->v.val_file == v2->v.val_file;
7909 case dw_val_class_data8:
7910 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7912 default:
7913 return 1;
7917 /* Do the attributes look the same? */
7919 static int
7920 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7922 if (at1->dw_attr != at2->dw_attr)
7923 return 0;
7925 /* We don't care that this was compiled with a different compiler
7926 snapshot; if the output is the same, that's what matters. */
7927 if (at1->dw_attr == DW_AT_producer)
7928 return 1;
7930 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7933 /* Do the dies look the same? */
7935 static int
7936 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7938 dw_die_ref c1, c2;
7939 dw_attr_node *a1;
7940 unsigned ix;
7942 /* To avoid infinite recursion. */
7943 if (die1->die_mark)
7944 return die1->die_mark == die2->die_mark;
7945 die1->die_mark = die2->die_mark = ++(*mark);
7947 if (die1->die_tag != die2->die_tag)
7948 return 0;
7950 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7951 return 0;
7953 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7954 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7955 return 0;
7957 c1 = die1->die_child;
7958 c2 = die2->die_child;
7959 if (! c1)
7961 if (c2)
7962 return 0;
7964 else
7965 for (;;)
7967 if (!same_die_p (c1, c2, mark))
7968 return 0;
7969 c1 = c1->die_sib;
7970 c2 = c2->die_sib;
7971 if (c1 == die1->die_child)
7973 if (c2 == die2->die_child)
7974 break;
7975 else
7976 return 0;
7980 return 1;
7983 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7984 children, and set die_symbol. */
7986 static void
7987 compute_comp_unit_symbol (dw_die_ref unit_die)
7989 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7990 const char *base = die_name ? lbasename (die_name) : "anonymous";
7991 char *name = XALLOCAVEC (char, strlen (base) + 64);
7992 char *p;
7993 int i, mark;
7994 unsigned char checksum[16];
7995 struct md5_ctx ctx;
7997 /* Compute the checksum of the DIE, then append part of it as hex digits to
7998 the name filename of the unit. */
8000 md5_init_ctx (&ctx);
8001 mark = 0;
8002 die_checksum (unit_die, &ctx, &mark);
8003 unmark_all_dies (unit_die);
8004 md5_finish_ctx (&ctx, checksum);
8006 /* When we this for comp_unit_die () we have a DW_AT_name that might
8007 not start with a letter but with anything valid for filenames and
8008 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
8009 character is not a letter. */
8010 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
8011 clean_symbol_name (name);
8013 p = name + strlen (name);
8014 for (i = 0; i < 4; i++)
8016 sprintf (p, "%.2x", checksum[i]);
8017 p += 2;
8020 unit_die->die_id.die_symbol = xstrdup (name);
8023 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
8025 static int
8026 is_type_die (dw_die_ref die)
8028 switch (die->die_tag)
8030 case DW_TAG_array_type:
8031 case DW_TAG_class_type:
8032 case DW_TAG_interface_type:
8033 case DW_TAG_enumeration_type:
8034 case DW_TAG_pointer_type:
8035 case DW_TAG_reference_type:
8036 case DW_TAG_rvalue_reference_type:
8037 case DW_TAG_string_type:
8038 case DW_TAG_structure_type:
8039 case DW_TAG_subroutine_type:
8040 case DW_TAG_union_type:
8041 case DW_TAG_ptr_to_member_type:
8042 case DW_TAG_set_type:
8043 case DW_TAG_subrange_type:
8044 case DW_TAG_base_type:
8045 case DW_TAG_const_type:
8046 case DW_TAG_file_type:
8047 case DW_TAG_packed_type:
8048 case DW_TAG_volatile_type:
8049 case DW_TAG_typedef:
8050 return 1;
8051 default:
8052 return 0;
8056 /* Returns true iff C is a compile-unit DIE. */
8058 static inline bool
8059 is_cu_die (dw_die_ref c)
8061 return c && (c->die_tag == DW_TAG_compile_unit
8062 || c->die_tag == DW_TAG_skeleton_unit);
8065 /* Returns true iff C is a unit DIE of some sort. */
8067 static inline bool
8068 is_unit_die (dw_die_ref c)
8070 return c && (c->die_tag == DW_TAG_compile_unit
8071 || c->die_tag == DW_TAG_partial_unit
8072 || c->die_tag == DW_TAG_type_unit
8073 || c->die_tag == DW_TAG_skeleton_unit);
8076 /* Returns true iff C is a namespace DIE. */
8078 static inline bool
8079 is_namespace_die (dw_die_ref c)
8081 return c && c->die_tag == DW_TAG_namespace;
8084 /* Return non-zero if this DIE is a template parameter. */
8086 static inline bool
8087 is_template_parameter (dw_die_ref die)
8089 switch (die->die_tag)
8091 case DW_TAG_template_type_param:
8092 case DW_TAG_template_value_param:
8093 case DW_TAG_GNU_template_template_param:
8094 case DW_TAG_GNU_template_parameter_pack:
8095 return true;
8096 default:
8097 return false;
8101 /* Return non-zero if this DIE represents a template instantiation. */
8103 static inline bool
8104 is_template_instantiation (dw_die_ref die)
8106 dw_die_ref c;
8108 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8109 return false;
8110 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8111 return false;
8114 static char *
8115 gen_internal_sym (const char *prefix)
8117 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8119 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8120 return xstrdup (buf);
8123 /* Return non-zero if this DIE is a declaration. */
8125 static int
8126 is_declaration_die (dw_die_ref die)
8128 dw_attr_node *a;
8129 unsigned ix;
8131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8132 if (a->dw_attr == DW_AT_declaration)
8133 return 1;
8135 return 0;
8138 /* Return non-zero if this DIE is nested inside a subprogram. */
8140 static int
8141 is_nested_in_subprogram (dw_die_ref die)
8143 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8145 if (decl == NULL)
8146 decl = die;
8147 return local_scope_p (decl);
8150 /* Return non-zero if this DIE contains a defining declaration of a
8151 subprogram. */
8153 static int
8154 contains_subprogram_definition (dw_die_ref die)
8156 dw_die_ref c;
8158 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8159 return 1;
8160 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8161 return 0;
8164 /* Return non-zero if this is a type DIE that should be moved to a
8165 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8166 unit type. */
8168 static int
8169 should_move_die_to_comdat (dw_die_ref die)
8171 switch (die->die_tag)
8173 case DW_TAG_class_type:
8174 case DW_TAG_structure_type:
8175 case DW_TAG_enumeration_type:
8176 case DW_TAG_union_type:
8177 /* Don't move declarations, inlined instances, types nested in a
8178 subprogram, or types that contain subprogram definitions. */
8179 if (is_declaration_die (die)
8180 || get_AT (die, DW_AT_abstract_origin)
8181 || is_nested_in_subprogram (die)
8182 || contains_subprogram_definition (die))
8183 return 0;
8184 return 1;
8185 case DW_TAG_array_type:
8186 case DW_TAG_interface_type:
8187 case DW_TAG_pointer_type:
8188 case DW_TAG_reference_type:
8189 case DW_TAG_rvalue_reference_type:
8190 case DW_TAG_string_type:
8191 case DW_TAG_subroutine_type:
8192 case DW_TAG_ptr_to_member_type:
8193 case DW_TAG_set_type:
8194 case DW_TAG_subrange_type:
8195 case DW_TAG_base_type:
8196 case DW_TAG_const_type:
8197 case DW_TAG_file_type:
8198 case DW_TAG_packed_type:
8199 case DW_TAG_volatile_type:
8200 case DW_TAG_typedef:
8201 default:
8202 return 0;
8206 /* Make a clone of DIE. */
8208 static dw_die_ref
8209 clone_die (dw_die_ref die)
8211 dw_die_ref clone = new_die_raw (die->die_tag);
8212 dw_attr_node *a;
8213 unsigned ix;
8215 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8216 add_dwarf_attr (clone, a);
8218 return clone;
8221 /* Make a clone of the tree rooted at DIE. */
8223 static dw_die_ref
8224 clone_tree (dw_die_ref die)
8226 dw_die_ref c;
8227 dw_die_ref clone = clone_die (die);
8229 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8231 return clone;
8234 /* Make a clone of DIE as a declaration. */
8236 static dw_die_ref
8237 clone_as_declaration (dw_die_ref die)
8239 dw_die_ref clone;
8240 dw_die_ref decl;
8241 dw_attr_node *a;
8242 unsigned ix;
8244 /* If the DIE is already a declaration, just clone it. */
8245 if (is_declaration_die (die))
8246 return clone_die (die);
8248 /* If the DIE is a specification, just clone its declaration DIE. */
8249 decl = get_AT_ref (die, DW_AT_specification);
8250 if (decl != NULL)
8252 clone = clone_die (decl);
8253 if (die->comdat_type_p)
8254 add_AT_die_ref (clone, DW_AT_signature, die);
8255 return clone;
8258 clone = new_die_raw (die->die_tag);
8260 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8262 /* We don't want to copy over all attributes.
8263 For example we don't want DW_AT_byte_size because otherwise we will no
8264 longer have a declaration and GDB will treat it as a definition. */
8266 switch (a->dw_attr)
8268 case DW_AT_abstract_origin:
8269 case DW_AT_artificial:
8270 case DW_AT_containing_type:
8271 case DW_AT_external:
8272 case DW_AT_name:
8273 case DW_AT_type:
8274 case DW_AT_virtuality:
8275 case DW_AT_linkage_name:
8276 case DW_AT_MIPS_linkage_name:
8277 add_dwarf_attr (clone, a);
8278 break;
8279 case DW_AT_byte_size:
8280 case DW_AT_alignment:
8281 default:
8282 break;
8286 if (die->comdat_type_p)
8287 add_AT_die_ref (clone, DW_AT_signature, die);
8289 add_AT_flag (clone, DW_AT_declaration, 1);
8290 return clone;
8294 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8296 struct decl_table_entry
8298 dw_die_ref orig;
8299 dw_die_ref copy;
8302 /* Helpers to manipulate hash table of copied declarations. */
8304 /* Hashtable helpers. */
8306 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8308 typedef die_struct *compare_type;
8309 static inline hashval_t hash (const decl_table_entry *);
8310 static inline bool equal (const decl_table_entry *, const die_struct *);
8313 inline hashval_t
8314 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8316 return htab_hash_pointer (entry->orig);
8319 inline bool
8320 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8321 const die_struct *entry2)
8323 return entry1->orig == entry2;
8326 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8328 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8329 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8330 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8331 to check if the ancestor has already been copied into UNIT. */
8333 static dw_die_ref
8334 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8335 decl_hash_type *decl_table)
8337 dw_die_ref parent = die->die_parent;
8338 dw_die_ref new_parent = unit;
8339 dw_die_ref copy;
8340 decl_table_entry **slot = NULL;
8341 struct decl_table_entry *entry = NULL;
8343 /* If DIE refers to a stub unfold that so we get the appropriate
8344 DIE registered as orig in decl_table. */
8345 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8346 die = c;
8348 if (decl_table)
8350 /* Check if the entry has already been copied to UNIT. */
8351 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8352 INSERT);
8353 if (*slot != HTAB_EMPTY_ENTRY)
8355 entry = *slot;
8356 return entry->copy;
8359 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8360 entry = XCNEW (struct decl_table_entry);
8361 entry->orig = die;
8362 entry->copy = NULL;
8363 *slot = entry;
8366 if (parent != NULL)
8368 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8369 if (spec != NULL)
8370 parent = spec;
8371 if (!is_unit_die (parent))
8372 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8375 copy = clone_as_declaration (die);
8376 add_child_die (new_parent, copy);
8378 if (decl_table)
8380 /* Record the pointer to the copy. */
8381 entry->copy = copy;
8384 return copy;
8386 /* Copy the declaration context to the new type unit DIE. This includes
8387 any surrounding namespace or type declarations. If the DIE has an
8388 AT_specification attribute, it also includes attributes and children
8389 attached to the specification, and returns a pointer to the original
8390 parent of the declaration DIE. Returns NULL otherwise. */
8392 static dw_die_ref
8393 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8395 dw_die_ref decl;
8396 dw_die_ref new_decl;
8397 dw_die_ref orig_parent = NULL;
8399 decl = get_AT_ref (die, DW_AT_specification);
8400 if (decl == NULL)
8401 decl = die;
8402 else
8404 unsigned ix;
8405 dw_die_ref c;
8406 dw_attr_node *a;
8408 /* The original DIE will be changed to a declaration, and must
8409 be moved to be a child of the original declaration DIE. */
8410 orig_parent = decl->die_parent;
8412 /* Copy the type node pointer from the new DIE to the original
8413 declaration DIE so we can forward references later. */
8414 decl->comdat_type_p = true;
8415 decl->die_id.die_type_node = die->die_id.die_type_node;
8417 remove_AT (die, DW_AT_specification);
8419 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8421 if (a->dw_attr != DW_AT_name
8422 && a->dw_attr != DW_AT_declaration
8423 && a->dw_attr != DW_AT_external)
8424 add_dwarf_attr (die, a);
8427 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8430 if (decl->die_parent != NULL
8431 && !is_unit_die (decl->die_parent))
8433 new_decl = copy_ancestor_tree (unit, decl, NULL);
8434 if (new_decl != NULL)
8436 remove_AT (new_decl, DW_AT_signature);
8437 add_AT_specification (die, new_decl);
8441 return orig_parent;
8444 /* Generate the skeleton ancestor tree for the given NODE, then clone
8445 the DIE and add the clone into the tree. */
8447 static void
8448 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8450 if (node->new_die != NULL)
8451 return;
8453 node->new_die = clone_as_declaration (node->old_die);
8455 if (node->parent != NULL)
8457 generate_skeleton_ancestor_tree (node->parent);
8458 add_child_die (node->parent->new_die, node->new_die);
8462 /* Generate a skeleton tree of DIEs containing any declarations that are
8463 found in the original tree. We traverse the tree looking for declaration
8464 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8466 static void
8467 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8469 skeleton_chain_node node;
8470 dw_die_ref c;
8471 dw_die_ref first;
8472 dw_die_ref prev = NULL;
8473 dw_die_ref next = NULL;
8475 node.parent = parent;
8477 first = c = parent->old_die->die_child;
8478 if (c)
8479 next = c->die_sib;
8480 if (c) do {
8481 if (prev == NULL || prev->die_sib == c)
8482 prev = c;
8483 c = next;
8484 next = (c == first ? NULL : c->die_sib);
8485 node.old_die = c;
8486 node.new_die = NULL;
8487 if (is_declaration_die (c))
8489 if (is_template_instantiation (c))
8491 /* Instantiated templates do not need to be cloned into the
8492 type unit. Just move the DIE and its children back to
8493 the skeleton tree (in the main CU). */
8494 remove_child_with_prev (c, prev);
8495 add_child_die (parent->new_die, c);
8496 c = prev;
8498 else if (c->comdat_type_p)
8500 /* This is the skeleton of earlier break_out_comdat_types
8501 type. Clone the existing DIE, but keep the children
8502 under the original (which is in the main CU). */
8503 dw_die_ref clone = clone_die (c);
8505 replace_child (c, clone, prev);
8506 generate_skeleton_ancestor_tree (parent);
8507 add_child_die (parent->new_die, c);
8508 c = clone;
8509 continue;
8511 else
8513 /* Clone the existing DIE, move the original to the skeleton
8514 tree (which is in the main CU), and put the clone, with
8515 all the original's children, where the original came from
8516 (which is about to be moved to the type unit). */
8517 dw_die_ref clone = clone_die (c);
8518 move_all_children (c, clone);
8520 /* If the original has a DW_AT_object_pointer attribute,
8521 it would now point to a child DIE just moved to the
8522 cloned tree, so we need to remove that attribute from
8523 the original. */
8524 remove_AT (c, DW_AT_object_pointer);
8526 replace_child (c, clone, prev);
8527 generate_skeleton_ancestor_tree (parent);
8528 add_child_die (parent->new_die, c);
8529 node.old_die = clone;
8530 node.new_die = c;
8531 c = clone;
8534 generate_skeleton_bottom_up (&node);
8535 } while (next != NULL);
8538 /* Wrapper function for generate_skeleton_bottom_up. */
8540 static dw_die_ref
8541 generate_skeleton (dw_die_ref die)
8543 skeleton_chain_node node;
8545 node.old_die = die;
8546 node.new_die = NULL;
8547 node.parent = NULL;
8549 /* If this type definition is nested inside another type,
8550 and is not an instantiation of a template, always leave
8551 at least a declaration in its place. */
8552 if (die->die_parent != NULL
8553 && is_type_die (die->die_parent)
8554 && !is_template_instantiation (die))
8555 node.new_die = clone_as_declaration (die);
8557 generate_skeleton_bottom_up (&node);
8558 return node.new_die;
8561 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8562 declaration. The original DIE is moved to a new compile unit so that
8563 existing references to it follow it to the new location. If any of the
8564 original DIE's descendants is a declaration, we need to replace the
8565 original DIE with a skeleton tree and move the declarations back into the
8566 skeleton tree. */
8568 static dw_die_ref
8569 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8570 dw_die_ref prev)
8572 dw_die_ref skeleton, orig_parent;
8574 /* Copy the declaration context to the type unit DIE. If the returned
8575 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8576 that DIE. */
8577 orig_parent = copy_declaration_context (unit, child);
8579 skeleton = generate_skeleton (child);
8580 if (skeleton == NULL)
8581 remove_child_with_prev (child, prev);
8582 else
8584 skeleton->comdat_type_p = true;
8585 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8587 /* If the original DIE was a specification, we need to put
8588 the skeleton under the parent DIE of the declaration.
8589 This leaves the original declaration in the tree, but
8590 it will be pruned later since there are no longer any
8591 references to it. */
8592 if (orig_parent != NULL)
8594 remove_child_with_prev (child, prev);
8595 add_child_die (orig_parent, skeleton);
8597 else
8598 replace_child (child, skeleton, prev);
8601 return skeleton;
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 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8610 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8611 DWARF procedure references in the DW_AT_location attribute. */
8613 static dw_die_ref
8614 copy_dwarf_procedure (dw_die_ref die,
8615 comdat_type_node *type_node,
8616 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8618 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8620 /* DWARF procedures are not supposed to have children... */
8621 gcc_assert (die->die_child == NULL);
8623 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8624 gcc_assert (vec_safe_length (die->die_attr) == 1
8625 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8627 /* Do not copy more than once DWARF procedures. */
8628 bool existed;
8629 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8630 if (existed)
8631 return die_copy;
8633 die_copy = clone_die (die);
8634 add_child_die (type_node->root_die, die_copy);
8635 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8636 return die_copy;
8639 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8640 procedures in DIE's attributes. */
8642 static void
8643 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8644 comdat_type_node *type_node,
8645 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8647 dw_attr_node *a;
8648 unsigned i;
8650 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8652 dw_loc_descr_ref loc;
8654 if (a->dw_attr_val.val_class != dw_val_class_loc)
8655 continue;
8657 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8659 switch (loc->dw_loc_opc)
8661 case DW_OP_call2:
8662 case DW_OP_call4:
8663 case DW_OP_call_ref:
8664 gcc_assert (loc->dw_loc_oprnd1.val_class
8665 == dw_val_class_die_ref);
8666 loc->dw_loc_oprnd1.v.val_die_ref.die
8667 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8668 type_node,
8669 copied_dwarf_procs);
8671 default:
8672 break;
8678 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8679 rewrite references to point to the copies.
8681 References are looked for in DIE's attributes and recursively in all its
8682 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8683 mapping from old DWARF procedures to their copy. It is used not to copy
8684 twice the same DWARF procedure under TYPE_NODE. */
8686 static void
8687 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8688 comdat_type_node *type_node,
8689 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8691 dw_die_ref c;
8693 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8694 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8695 type_node,
8696 copied_dwarf_procs));
8699 /* Traverse the DIE and set up additional .debug_types or .debug_info
8700 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8701 section. */
8703 static void
8704 break_out_comdat_types (dw_die_ref die)
8706 dw_die_ref c;
8707 dw_die_ref first;
8708 dw_die_ref prev = NULL;
8709 dw_die_ref next = NULL;
8710 dw_die_ref unit = NULL;
8712 first = c = die->die_child;
8713 if (c)
8714 next = c->die_sib;
8715 if (c) do {
8716 if (prev == NULL || prev->die_sib == c)
8717 prev = c;
8718 c = next;
8719 next = (c == first ? NULL : c->die_sib);
8720 if (should_move_die_to_comdat (c))
8722 dw_die_ref replacement;
8723 comdat_type_node *type_node;
8725 /* Break out nested types into their own type units. */
8726 break_out_comdat_types (c);
8728 /* Create a new type unit DIE as the root for the new tree. */
8729 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8730 add_AT_unsigned (unit, DW_AT_language,
8731 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8733 /* Add the new unit's type DIE into the comdat type list. */
8734 type_node = ggc_cleared_alloc<comdat_type_node> ();
8735 type_node->root_die = unit;
8736 type_node->next = comdat_type_list;
8737 comdat_type_list = type_node;
8739 /* Generate the type signature. */
8740 generate_type_signature (c, type_node);
8742 /* Copy the declaration context, attributes, and children of the
8743 declaration into the new type unit DIE, then remove this DIE
8744 from the main CU (or replace it with a skeleton if necessary). */
8745 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8746 type_node->skeleton_die = replacement;
8748 /* Add the DIE to the new compunit. */
8749 add_child_die (unit, c);
8751 /* Types can reference DWARF procedures for type size or data location
8752 expressions. Calls in DWARF expressions cannot target procedures
8753 that are not in the same section. So we must copy DWARF procedures
8754 along with this type and then rewrite references to them. */
8755 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8756 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8758 if (replacement != NULL)
8759 c = replacement;
8761 else if (c->die_tag == DW_TAG_namespace
8762 || c->die_tag == DW_TAG_class_type
8763 || c->die_tag == DW_TAG_structure_type
8764 || c->die_tag == DW_TAG_union_type)
8766 /* Look for nested types that can be broken out. */
8767 break_out_comdat_types (c);
8769 } while (next != NULL);
8772 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8773 Enter all the cloned children into the hash table decl_table. */
8775 static dw_die_ref
8776 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8778 dw_die_ref c;
8779 dw_die_ref clone;
8780 struct decl_table_entry *entry;
8781 decl_table_entry **slot;
8783 if (die->die_tag == DW_TAG_subprogram)
8784 clone = clone_as_declaration (die);
8785 else
8786 clone = clone_die (die);
8788 slot = decl_table->find_slot_with_hash (die,
8789 htab_hash_pointer (die), INSERT);
8791 /* Assert that DIE isn't in the hash table yet. If it would be there
8792 before, the ancestors would be necessarily there as well, therefore
8793 clone_tree_partial wouldn't be called. */
8794 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8796 entry = XCNEW (struct decl_table_entry);
8797 entry->orig = die;
8798 entry->copy = clone;
8799 *slot = entry;
8801 if (die->die_tag != DW_TAG_subprogram)
8802 FOR_EACH_CHILD (die, c,
8803 add_child_die (clone, clone_tree_partial (c, decl_table)));
8805 return clone;
8808 /* Walk the DIE and its children, looking for references to incomplete
8809 or trivial types that are unmarked (i.e., that are not in the current
8810 type_unit). */
8812 static void
8813 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8815 dw_die_ref c;
8816 dw_attr_node *a;
8817 unsigned ix;
8819 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8821 if (AT_class (a) == dw_val_class_die_ref)
8823 dw_die_ref targ = AT_ref (a);
8824 decl_table_entry **slot;
8825 struct decl_table_entry *entry;
8827 if (targ->die_mark != 0 || targ->comdat_type_p)
8828 continue;
8830 slot = decl_table->find_slot_with_hash (targ,
8831 htab_hash_pointer (targ),
8832 INSERT);
8834 if (*slot != HTAB_EMPTY_ENTRY)
8836 /* TARG has already been copied, so we just need to
8837 modify the reference to point to the copy. */
8838 entry = *slot;
8839 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8841 else
8843 dw_die_ref parent = unit;
8844 dw_die_ref copy = clone_die (targ);
8846 /* Record in DECL_TABLE that TARG has been copied.
8847 Need to do this now, before the recursive call,
8848 because DECL_TABLE may be expanded and SLOT
8849 would no longer be a valid pointer. */
8850 entry = XCNEW (struct decl_table_entry);
8851 entry->orig = targ;
8852 entry->copy = copy;
8853 *slot = entry;
8855 /* If TARG is not a declaration DIE, we need to copy its
8856 children. */
8857 if (!is_declaration_die (targ))
8859 FOR_EACH_CHILD (
8860 targ, c,
8861 add_child_die (copy,
8862 clone_tree_partial (c, decl_table)));
8865 /* Make sure the cloned tree is marked as part of the
8866 type unit. */
8867 mark_dies (copy);
8869 /* If TARG has surrounding context, copy its ancestor tree
8870 into the new type unit. */
8871 if (targ->die_parent != NULL
8872 && !is_unit_die (targ->die_parent))
8873 parent = copy_ancestor_tree (unit, targ->die_parent,
8874 decl_table);
8876 add_child_die (parent, copy);
8877 a->dw_attr_val.v.val_die_ref.die = copy;
8879 /* Make sure the newly-copied DIE is walked. If it was
8880 installed in a previously-added context, it won't
8881 get visited otherwise. */
8882 if (parent != unit)
8884 /* Find the highest point of the newly-added tree,
8885 mark each node along the way, and walk from there. */
8886 parent->die_mark = 1;
8887 while (parent->die_parent
8888 && parent->die_parent->die_mark == 0)
8890 parent = parent->die_parent;
8891 parent->die_mark = 1;
8893 copy_decls_walk (unit, parent, decl_table);
8899 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8902 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8903 and record them in DECL_TABLE. */
8905 static void
8906 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8908 dw_die_ref c;
8910 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8912 dw_die_ref targ = AT_ref (a);
8913 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8914 decl_table_entry **slot
8915 = decl_table->find_slot_with_hash (targ,
8916 htab_hash_pointer (targ),
8917 INSERT);
8918 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8919 /* Record in DECL_TABLE that TARG has been already copied
8920 by remove_child_or_replace_with_skeleton. */
8921 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8922 entry->orig = targ;
8923 entry->copy = die;
8924 *slot = entry;
8926 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8929 /* Copy declarations for "unworthy" types into the new comdat section.
8930 Incomplete types, modified types, and certain other types aren't broken
8931 out into comdat sections of their own, so they don't have a signature,
8932 and we need to copy the declaration into the same section so that we
8933 don't have an external reference. */
8935 static void
8936 copy_decls_for_unworthy_types (dw_die_ref unit)
8938 mark_dies (unit);
8939 decl_hash_type decl_table (10);
8940 collect_skeleton_dies (unit, &decl_table);
8941 copy_decls_walk (unit, unit, &decl_table);
8942 unmark_dies (unit);
8945 /* Traverse the DIE and add a sibling attribute if it may have the
8946 effect of speeding up access to siblings. To save some space,
8947 avoid generating sibling attributes for DIE's without children. */
8949 static void
8950 add_sibling_attributes (dw_die_ref die)
8952 dw_die_ref c;
8954 if (! die->die_child)
8955 return;
8957 if (die->die_parent && die != die->die_parent->die_child)
8958 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8960 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8963 /* Output all location lists for the DIE and its children. */
8965 static void
8966 output_location_lists (dw_die_ref die)
8968 dw_die_ref c;
8969 dw_attr_node *a;
8970 unsigned ix;
8972 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8973 if (AT_class (a) == dw_val_class_loc_list)
8974 output_loc_list (AT_loc_list (a));
8976 FOR_EACH_CHILD (die, c, output_location_lists (c));
8979 /* During assign_location_list_indexes and output_loclists_offset the
8980 current index, after it the number of assigned indexes (i.e. how
8981 large the .debug_loclists* offset table should be). */
8982 static unsigned int loc_list_idx;
8984 /* Output all location list offsets for the DIE and its children. */
8986 static void
8987 output_loclists_offsets (dw_die_ref die)
8989 dw_die_ref c;
8990 dw_attr_node *a;
8991 unsigned ix;
8993 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8994 if (AT_class (a) == dw_val_class_loc_list)
8996 dw_loc_list_ref l = AT_loc_list (a);
8997 if (l->offset_emitted)
8998 continue;
8999 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
9000 loc_section_label, NULL);
9001 gcc_assert (l->hash == loc_list_idx);
9002 loc_list_idx++;
9003 l->offset_emitted = true;
9006 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
9009 /* Recursively set indexes of location lists. */
9011 static void
9012 assign_location_list_indexes (dw_die_ref die)
9014 dw_die_ref c;
9015 dw_attr_node *a;
9016 unsigned ix;
9018 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9019 if (AT_class (a) == dw_val_class_loc_list)
9021 dw_loc_list_ref list = AT_loc_list (a);
9022 if (!list->num_assigned)
9024 list->num_assigned = true;
9025 list->hash = loc_list_idx++;
9029 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
9032 /* We want to limit the number of external references, because they are
9033 larger than local references: a relocation takes multiple words, and
9034 even a sig8 reference is always eight bytes, whereas a local reference
9035 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
9036 So if we encounter multiple external references to the same type DIE, we
9037 make a local typedef stub for it and redirect all references there.
9039 This is the element of the hash table for keeping track of these
9040 references. */
9042 struct external_ref
9044 dw_die_ref type;
9045 dw_die_ref stub;
9046 unsigned n_refs;
9049 /* Hashtable helpers. */
9051 struct external_ref_hasher : free_ptr_hash <external_ref>
9053 static inline hashval_t hash (const external_ref *);
9054 static inline bool equal (const external_ref *, const external_ref *);
9057 inline hashval_t
9058 external_ref_hasher::hash (const external_ref *r)
9060 dw_die_ref die = r->type;
9061 hashval_t h = 0;
9063 /* We can't use the address of the DIE for hashing, because
9064 that will make the order of the stub DIEs non-deterministic. */
9065 if (! die->comdat_type_p)
9066 /* We have a symbol; use it to compute a hash. */
9067 h = htab_hash_string (die->die_id.die_symbol);
9068 else
9070 /* We have a type signature; use a subset of the bits as the hash.
9071 The 8-byte signature is at least as large as hashval_t. */
9072 comdat_type_node *type_node = die->die_id.die_type_node;
9073 memcpy (&h, type_node->signature, sizeof (h));
9075 return h;
9078 inline bool
9079 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9081 return r1->type == r2->type;
9084 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9086 /* Return a pointer to the external_ref for references to DIE. */
9088 static struct external_ref *
9089 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9091 struct external_ref ref, *ref_p;
9092 external_ref **slot;
9094 ref.type = die;
9095 slot = map->find_slot (&ref, INSERT);
9096 if (*slot != HTAB_EMPTY_ENTRY)
9097 return *slot;
9099 ref_p = XCNEW (struct external_ref);
9100 ref_p->type = die;
9101 *slot = ref_p;
9102 return ref_p;
9105 /* Subroutine of optimize_external_refs, below.
9107 If we see a type skeleton, record it as our stub. If we see external
9108 references, remember how many we've seen. */
9110 static void
9111 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9113 dw_die_ref c;
9114 dw_attr_node *a;
9115 unsigned ix;
9116 struct external_ref *ref_p;
9118 if (is_type_die (die)
9119 && (c = get_AT_ref (die, DW_AT_signature)))
9121 /* This is a local skeleton; use it for local references. */
9122 ref_p = lookup_external_ref (map, c);
9123 ref_p->stub = die;
9126 /* Scan the DIE references, and remember any that refer to DIEs from
9127 other CUs (i.e. those which are not marked). */
9128 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9129 if (AT_class (a) == dw_val_class_die_ref
9130 && (c = AT_ref (a))->die_mark == 0
9131 && is_type_die (c))
9133 ref_p = lookup_external_ref (map, c);
9134 ref_p->n_refs++;
9137 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9140 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9141 points to an external_ref, DATA is the CU we're processing. If we don't
9142 already have a local stub, and we have multiple refs, build a stub. */
9145 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9147 struct external_ref *ref_p = *slot;
9149 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9151 /* We have multiple references to this type, so build a small stub.
9152 Both of these forms are a bit dodgy from the perspective of the
9153 DWARF standard, since technically they should have names. */
9154 dw_die_ref cu = data;
9155 dw_die_ref type = ref_p->type;
9156 dw_die_ref stub = NULL;
9158 if (type->comdat_type_p)
9160 /* If we refer to this type via sig8, use AT_signature. */
9161 stub = new_die (type->die_tag, cu, NULL_TREE);
9162 add_AT_die_ref (stub, DW_AT_signature, type);
9164 else
9166 /* Otherwise, use a typedef with no name. */
9167 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9168 add_AT_die_ref (stub, DW_AT_type, type);
9171 stub->die_mark++;
9172 ref_p->stub = stub;
9174 return 1;
9177 /* DIE is a unit; look through all the DIE references to see if there are
9178 any external references to types, and if so, create local stubs for
9179 them which will be applied in build_abbrev_table. This is useful because
9180 references to local DIEs are smaller. */
9182 static external_ref_hash_type *
9183 optimize_external_refs (dw_die_ref die)
9185 external_ref_hash_type *map = new external_ref_hash_type (10);
9186 optimize_external_refs_1 (die, map);
9187 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9188 return map;
9191 /* The following 3 variables are temporaries that are computed only during the
9192 build_abbrev_table call and used and released during the following
9193 optimize_abbrev_table call. */
9195 /* First abbrev_id that can be optimized based on usage. */
9196 static unsigned int abbrev_opt_start;
9198 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9199 abbrev_id smaller than this, because they must be already sized
9200 during build_abbrev_table). */
9201 static unsigned int abbrev_opt_base_type_end;
9203 /* Vector of usage counts during build_abbrev_table. Indexed by
9204 abbrev_id - abbrev_opt_start. */
9205 static vec<unsigned int> abbrev_usage_count;
9207 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9208 static vec<dw_die_ref> sorted_abbrev_dies;
9210 /* The format of each DIE (and its attribute value pairs) is encoded in an
9211 abbreviation table. This routine builds the abbreviation table and assigns
9212 a unique abbreviation id for each abbreviation entry. The children of each
9213 die are visited recursively. */
9215 static void
9216 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9218 unsigned int abbrev_id = 0;
9219 dw_die_ref c;
9220 dw_attr_node *a;
9221 unsigned ix;
9222 dw_die_ref abbrev;
9224 /* Scan the DIE references, and replace any that refer to
9225 DIEs from other CUs (i.e. those which are not marked) with
9226 the local stubs we built in optimize_external_refs. */
9227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9228 if (AT_class (a) == dw_val_class_die_ref
9229 && (c = AT_ref (a))->die_mark == 0)
9231 struct external_ref *ref_p;
9232 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9234 if (is_type_die (c)
9235 && (ref_p = lookup_external_ref (extern_map, c))
9236 && ref_p->stub && ref_p->stub != die)
9238 gcc_assert (a->dw_attr != DW_AT_signature);
9239 change_AT_die_ref (a, ref_p->stub);
9241 else
9242 /* We aren't changing this reference, so mark it external. */
9243 set_AT_ref_external (a, 1);
9246 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9248 dw_attr_node *die_a, *abbrev_a;
9249 unsigned ix;
9250 bool ok = true;
9252 if (abbrev_id == 0)
9253 continue;
9254 if (abbrev->die_tag != die->die_tag)
9255 continue;
9256 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9257 continue;
9259 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9260 continue;
9262 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9264 abbrev_a = &(*abbrev->die_attr)[ix];
9265 if ((abbrev_a->dw_attr != die_a->dw_attr)
9266 || (value_format (abbrev_a) != value_format (die_a)))
9268 ok = false;
9269 break;
9272 if (ok)
9273 break;
9276 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9278 vec_safe_push (abbrev_die_table, die);
9279 if (abbrev_opt_start)
9280 abbrev_usage_count.safe_push (0);
9282 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9284 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9285 sorted_abbrev_dies.safe_push (die);
9288 die->die_abbrev = abbrev_id;
9289 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9292 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9293 by die_abbrev's usage count, from the most commonly used
9294 abbreviation to the least. */
9296 static int
9297 die_abbrev_cmp (const void *p1, const void *p2)
9299 dw_die_ref die1 = *(const dw_die_ref *) p1;
9300 dw_die_ref die2 = *(const dw_die_ref *) p2;
9302 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9303 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9305 if (die1->die_abbrev >= abbrev_opt_base_type_end
9306 && die2->die_abbrev >= abbrev_opt_base_type_end)
9308 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9309 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9310 return -1;
9311 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9312 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9313 return 1;
9316 /* Stabilize the sort. */
9317 if (die1->die_abbrev < die2->die_abbrev)
9318 return -1;
9319 if (die1->die_abbrev > die2->die_abbrev)
9320 return 1;
9322 return 0;
9325 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9326 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9327 into dw_val_class_const_implicit or
9328 dw_val_class_unsigned_const_implicit. */
9330 static void
9331 optimize_implicit_const (unsigned int first_id, unsigned int end,
9332 vec<bool> &implicit_consts)
9334 /* It never makes sense if there is just one DIE using the abbreviation. */
9335 if (end < first_id + 2)
9336 return;
9338 dw_attr_node *a;
9339 unsigned ix, i;
9340 dw_die_ref die = sorted_abbrev_dies[first_id];
9341 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9342 if (implicit_consts[ix])
9344 enum dw_val_class new_class = dw_val_class_none;
9345 switch (AT_class (a))
9347 case dw_val_class_unsigned_const:
9348 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9349 continue;
9351 /* The .debug_abbrev section will grow by
9352 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9353 in all the DIEs using that abbreviation. */
9354 if (constant_size (AT_unsigned (a)) * (end - first_id)
9355 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9356 continue;
9358 new_class = dw_val_class_unsigned_const_implicit;
9359 break;
9361 case dw_val_class_const:
9362 new_class = dw_val_class_const_implicit;
9363 break;
9365 case dw_val_class_file:
9366 new_class = dw_val_class_file_implicit;
9367 break;
9369 default:
9370 continue;
9372 for (i = first_id; i < end; i++)
9373 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9374 = new_class;
9378 /* Attempt to optimize abbreviation table from abbrev_opt_start
9379 abbreviation above. */
9381 static void
9382 optimize_abbrev_table (void)
9384 if (abbrev_opt_start
9385 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9386 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9388 auto_vec<bool, 32> implicit_consts;
9389 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9391 unsigned int abbrev_id = abbrev_opt_start - 1;
9392 unsigned int first_id = ~0U;
9393 unsigned int last_abbrev_id = 0;
9394 unsigned int i;
9395 dw_die_ref die;
9396 if (abbrev_opt_base_type_end > abbrev_opt_start)
9397 abbrev_id = abbrev_opt_base_type_end - 1;
9398 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9399 most commonly used abbreviations come first. */
9400 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9402 dw_attr_node *a;
9403 unsigned ix;
9405 /* If calc_base_type_die_sizes has been called, the CU and
9406 base types after it can't be optimized, because we've already
9407 calculated their DIE offsets. We've sorted them first. */
9408 if (die->die_abbrev < abbrev_opt_base_type_end)
9409 continue;
9410 if (die->die_abbrev != last_abbrev_id)
9412 last_abbrev_id = die->die_abbrev;
9413 if (dwarf_version >= 5 && first_id != ~0U)
9414 optimize_implicit_const (first_id, i, implicit_consts);
9415 abbrev_id++;
9416 (*abbrev_die_table)[abbrev_id] = die;
9417 if (dwarf_version >= 5)
9419 first_id = i;
9420 implicit_consts.truncate (0);
9422 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9423 switch (AT_class (a))
9425 case dw_val_class_const:
9426 case dw_val_class_unsigned_const:
9427 case dw_val_class_file:
9428 implicit_consts.safe_push (true);
9429 break;
9430 default:
9431 implicit_consts.safe_push (false);
9432 break;
9436 else if (dwarf_version >= 5)
9438 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9439 if (!implicit_consts[ix])
9440 continue;
9441 else
9443 dw_attr_node *other_a
9444 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9445 if (!dw_val_equal_p (&a->dw_attr_val,
9446 &other_a->dw_attr_val))
9447 implicit_consts[ix] = false;
9450 die->die_abbrev = abbrev_id;
9452 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9453 if (dwarf_version >= 5 && first_id != ~0U)
9454 optimize_implicit_const (first_id, i, implicit_consts);
9457 abbrev_opt_start = 0;
9458 abbrev_opt_base_type_end = 0;
9459 abbrev_usage_count.release ();
9460 sorted_abbrev_dies.release ();
9463 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9465 static int
9466 constant_size (unsigned HOST_WIDE_INT value)
9468 int log;
9470 if (value == 0)
9471 log = 0;
9472 else
9473 log = floor_log2 (value);
9475 log = log / 8;
9476 log = 1 << (floor_log2 (log) + 1);
9478 return log;
9481 /* Return the size of a DIE as it is represented in the
9482 .debug_info section. */
9484 static unsigned long
9485 size_of_die (dw_die_ref die)
9487 unsigned long size = 0;
9488 dw_attr_node *a;
9489 unsigned ix;
9490 enum dwarf_form form;
9492 size += size_of_uleb128 (die->die_abbrev);
9493 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9495 switch (AT_class (a))
9497 case dw_val_class_addr:
9498 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9500 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9501 size += size_of_uleb128 (AT_index (a));
9503 else
9504 size += DWARF2_ADDR_SIZE;
9505 break;
9506 case dw_val_class_offset:
9507 size += dwarf_offset_size;
9508 break;
9509 case dw_val_class_loc:
9511 unsigned long lsize = size_of_locs (AT_loc (a));
9513 /* Block length. */
9514 if (dwarf_version >= 4)
9515 size += size_of_uleb128 (lsize);
9516 else
9517 size += constant_size (lsize);
9518 size += lsize;
9520 break;
9521 case dw_val_class_loc_list:
9522 if (dwarf_split_debug_info && dwarf_version >= 5)
9524 gcc_assert (AT_loc_list (a)->num_assigned);
9525 size += size_of_uleb128 (AT_loc_list (a)->hash);
9527 else
9528 size += dwarf_offset_size;
9529 break;
9530 case dw_val_class_view_list:
9531 size += dwarf_offset_size;
9532 break;
9533 case dw_val_class_range_list:
9534 if (value_format (a) == DW_FORM_rnglistx)
9536 gcc_assert (rnglist_idx);
9537 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9538 size += size_of_uleb128 (r->idx);
9540 else
9541 size += dwarf_offset_size;
9542 break;
9543 case dw_val_class_const:
9544 size += size_of_sleb128 (AT_int (a));
9545 break;
9546 case dw_val_class_unsigned_const:
9548 int csize = constant_size (AT_unsigned (a));
9549 if (dwarf_version == 3
9550 && a->dw_attr == DW_AT_data_member_location
9551 && csize >= 4)
9552 size += size_of_uleb128 (AT_unsigned (a));
9553 else
9554 size += csize;
9556 break;
9557 case dw_val_class_symview:
9558 if (symview_upper_bound <= 0xff)
9559 size += 1;
9560 else if (symview_upper_bound <= 0xffff)
9561 size += 2;
9562 else if (symview_upper_bound <= 0xffffffff)
9563 size += 4;
9564 else
9565 size += 8;
9566 break;
9567 case dw_val_class_const_implicit:
9568 case dw_val_class_unsigned_const_implicit:
9569 case dw_val_class_file_implicit:
9570 /* These occupy no size in the DIE, just an extra sleb128 in
9571 .debug_abbrev. */
9572 break;
9573 case dw_val_class_const_double:
9574 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9575 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9576 size++; /* block */
9577 break;
9578 case dw_val_class_wide_int:
9579 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9580 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9581 if (get_full_len (*a->dw_attr_val.v.val_wide)
9582 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9583 size++; /* block */
9584 break;
9585 case dw_val_class_vec:
9586 size += constant_size (a->dw_attr_val.v.val_vec.length
9587 * a->dw_attr_val.v.val_vec.elt_size)
9588 + a->dw_attr_val.v.val_vec.length
9589 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9590 break;
9591 case dw_val_class_flag:
9592 if (dwarf_version >= 4)
9593 /* Currently all add_AT_flag calls pass in 1 as last argument,
9594 so DW_FORM_flag_present can be used. If that ever changes,
9595 we'll need to use DW_FORM_flag and have some optimization
9596 in build_abbrev_table that will change those to
9597 DW_FORM_flag_present if it is set to 1 in all DIEs using
9598 the same abbrev entry. */
9599 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9600 else
9601 size += 1;
9602 break;
9603 case dw_val_class_die_ref:
9604 if (AT_ref_external (a))
9606 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9607 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9608 is sized by target address length, whereas in DWARF3
9609 it's always sized as an offset. */
9610 if (AT_ref (a)->comdat_type_p)
9611 size += DWARF_TYPE_SIGNATURE_SIZE;
9612 else if (dwarf_version == 2)
9613 size += DWARF2_ADDR_SIZE;
9614 else
9615 size += dwarf_offset_size;
9617 else
9618 size += dwarf_offset_size;
9619 break;
9620 case dw_val_class_fde_ref:
9621 size += dwarf_offset_size;
9622 break;
9623 case dw_val_class_lbl_id:
9624 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9626 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9627 size += size_of_uleb128 (AT_index (a));
9629 else
9630 size += DWARF2_ADDR_SIZE;
9631 break;
9632 case dw_val_class_lineptr:
9633 case dw_val_class_macptr:
9634 case dw_val_class_loclistsptr:
9635 size += dwarf_offset_size;
9636 break;
9637 case dw_val_class_str:
9638 form = AT_string_form (a);
9639 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9640 size += dwarf_offset_size;
9641 else if (form == dwarf_FORM (DW_FORM_strx))
9642 size += size_of_uleb128 (AT_index (a));
9643 else
9644 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9645 break;
9646 case dw_val_class_file:
9647 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9648 break;
9649 case dw_val_class_data8:
9650 size += 8;
9651 break;
9652 case dw_val_class_vms_delta:
9653 size += dwarf_offset_size;
9654 break;
9655 case dw_val_class_high_pc:
9656 size += DWARF2_ADDR_SIZE;
9657 break;
9658 case dw_val_class_discr_value:
9659 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9660 break;
9661 case dw_val_class_discr_list:
9663 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9665 /* This is a block, so we have the block length and then its
9666 data. */
9667 size += constant_size (block_size) + block_size;
9669 break;
9670 default:
9671 gcc_unreachable ();
9675 return size;
9678 /* Size the debugging information associated with a given DIE. Visits the
9679 DIE's children recursively. Updates the global variable next_die_offset, on
9680 each time through. Uses the current value of next_die_offset to update the
9681 die_offset field in each DIE. */
9683 static void
9684 calc_die_sizes (dw_die_ref die)
9686 dw_die_ref c;
9688 gcc_assert (die->die_offset == 0
9689 || (unsigned long int) die->die_offset == next_die_offset);
9690 die->die_offset = next_die_offset;
9691 next_die_offset += size_of_die (die);
9693 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9695 if (die->die_child != NULL)
9696 /* Count the null byte used to terminate sibling lists. */
9697 next_die_offset += 1;
9700 /* Size just the base type children at the start of the CU.
9701 This is needed because build_abbrev needs to size locs
9702 and sizing of type based stack ops needs to know die_offset
9703 values for the base types. */
9705 static void
9706 calc_base_type_die_sizes (void)
9708 unsigned long die_offset = (dwarf_split_debug_info
9709 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9710 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9711 unsigned int i;
9712 dw_die_ref base_type;
9713 #if ENABLE_ASSERT_CHECKING
9714 dw_die_ref prev = comp_unit_die ()->die_child;
9715 #endif
9717 die_offset += size_of_die (comp_unit_die ());
9718 for (i = 0; base_types.iterate (i, &base_type); i++)
9720 #if ENABLE_ASSERT_CHECKING
9721 gcc_assert (base_type->die_offset == 0
9722 && prev->die_sib == base_type
9723 && base_type->die_child == NULL
9724 && base_type->die_abbrev);
9725 prev = base_type;
9726 #endif
9727 if (abbrev_opt_start
9728 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9729 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9730 base_type->die_offset = die_offset;
9731 die_offset += size_of_die (base_type);
9735 /* Set the marks for a die and its children. We do this so
9736 that we know whether or not a reference needs to use FORM_ref_addr; only
9737 DIEs in the same CU will be marked. We used to clear out the offset
9738 and use that as the flag, but ran into ordering problems. */
9740 static void
9741 mark_dies (dw_die_ref die)
9743 dw_die_ref c;
9745 gcc_assert (!die->die_mark);
9747 die->die_mark = 1;
9748 FOR_EACH_CHILD (die, c, mark_dies (c));
9751 /* Clear the marks for a die and its children. */
9753 static void
9754 unmark_dies (dw_die_ref die)
9756 dw_die_ref c;
9758 if (! use_debug_types)
9759 gcc_assert (die->die_mark);
9761 die->die_mark = 0;
9762 FOR_EACH_CHILD (die, c, unmark_dies (c));
9765 /* Clear the marks for a die, its children and referred dies. */
9767 static void
9768 unmark_all_dies (dw_die_ref die)
9770 dw_die_ref c;
9771 dw_attr_node *a;
9772 unsigned ix;
9774 if (!die->die_mark)
9775 return;
9776 die->die_mark = 0;
9778 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9780 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9781 if (AT_class (a) == dw_val_class_die_ref)
9782 unmark_all_dies (AT_ref (a));
9785 /* Calculate if the entry should appear in the final output file. It may be
9786 from a pruned a type. */
9788 static bool
9789 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9791 /* By limiting gnu pubnames to definitions only, gold can generate a
9792 gdb index without entries for declarations, which don't include
9793 enough information to be useful. */
9794 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9795 return false;
9797 if (table == pubname_table)
9799 /* Enumerator names are part of the pubname table, but the
9800 parent DW_TAG_enumeration_type die may have been pruned.
9801 Don't output them if that is the case. */
9802 if (p->die->die_tag == DW_TAG_enumerator &&
9803 (p->die->die_parent == NULL
9804 || !p->die->die_parent->die_perennial_p))
9805 return false;
9807 /* Everything else in the pubname table is included. */
9808 return true;
9811 /* The pubtypes table shouldn't include types that have been
9812 pruned. */
9813 return (p->die->die_offset != 0
9814 || !flag_eliminate_unused_debug_types);
9817 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9818 generated for the compilation unit. */
9820 static unsigned long
9821 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9823 unsigned long size;
9824 unsigned i;
9825 pubname_entry *p;
9826 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9828 size = DWARF_PUBNAMES_HEADER_SIZE;
9829 FOR_EACH_VEC_ELT (*names, i, p)
9830 if (include_pubname_in_output (names, p))
9831 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9833 size += dwarf_offset_size;
9834 return size;
9837 /* Return the size of the information in the .debug_aranges section. */
9839 static unsigned long
9840 size_of_aranges (void)
9842 unsigned long size;
9844 size = DWARF_ARANGES_HEADER_SIZE;
9846 /* Count the address/length pair for this compilation unit. */
9847 if (switch_text_ranges)
9848 size += 2 * DWARF2_ADDR_SIZE
9849 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9850 if (switch_cold_ranges)
9851 size += 2 * DWARF2_ADDR_SIZE
9852 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9853 if (have_multiple_function_sections)
9855 unsigned fde_idx;
9856 dw_fde_ref fde;
9858 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9860 if (fde->ignored_debug)
9861 continue;
9862 if (!fde->in_std_section)
9863 size += 2 * DWARF2_ADDR_SIZE;
9864 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9865 size += 2 * DWARF2_ADDR_SIZE;
9869 /* Count the two zero words used to terminated the address range table. */
9870 size += 2 * DWARF2_ADDR_SIZE;
9871 return size;
9874 /* Select the encoding of an attribute value. */
9876 static enum dwarf_form
9877 value_format (dw_attr_node *a)
9879 switch (AT_class (a))
9881 case dw_val_class_addr:
9882 /* Only very few attributes allow DW_FORM_addr. */
9883 switch (a->dw_attr)
9885 case DW_AT_low_pc:
9886 case DW_AT_high_pc:
9887 case DW_AT_entry_pc:
9888 case DW_AT_trampoline:
9889 return (AT_index (a) == NOT_INDEXED
9890 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9891 default:
9892 break;
9894 switch (DWARF2_ADDR_SIZE)
9896 case 1:
9897 return DW_FORM_data1;
9898 case 2:
9899 return DW_FORM_data2;
9900 case 4:
9901 return DW_FORM_data4;
9902 case 8:
9903 return DW_FORM_data8;
9904 default:
9905 gcc_unreachable ();
9907 case dw_val_class_loc_list:
9908 if (dwarf_split_debug_info
9909 && dwarf_version >= 5
9910 && AT_loc_list (a)->num_assigned)
9911 return DW_FORM_loclistx;
9912 /* FALLTHRU */
9913 case dw_val_class_view_list:
9914 case dw_val_class_range_list:
9915 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9916 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9917 care about sizes of .debug* sections in shared libraries and
9918 executables and don't take into account relocations that affect just
9919 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9920 table in the .debug_rnglists section. */
9921 if (dwarf_split_debug_info
9922 && dwarf_version >= 5
9923 && AT_class (a) == dw_val_class_range_list
9924 && rnglist_idx
9925 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9926 return DW_FORM_rnglistx;
9927 if (dwarf_version >= 4)
9928 return DW_FORM_sec_offset;
9929 /* FALLTHRU */
9930 case dw_val_class_vms_delta:
9931 case dw_val_class_offset:
9932 switch (dwarf_offset_size)
9934 case 4:
9935 return DW_FORM_data4;
9936 case 8:
9937 return DW_FORM_data8;
9938 default:
9939 gcc_unreachable ();
9941 case dw_val_class_loc:
9942 if (dwarf_version >= 4)
9943 return DW_FORM_exprloc;
9944 switch (constant_size (size_of_locs (AT_loc (a))))
9946 case 1:
9947 return DW_FORM_block1;
9948 case 2:
9949 return DW_FORM_block2;
9950 case 4:
9951 return DW_FORM_block4;
9952 default:
9953 gcc_unreachable ();
9955 case dw_val_class_const:
9956 return DW_FORM_sdata;
9957 case dw_val_class_unsigned_const:
9958 switch (constant_size (AT_unsigned (a)))
9960 case 1:
9961 return DW_FORM_data1;
9962 case 2:
9963 return DW_FORM_data2;
9964 case 4:
9965 /* In DWARF3 DW_AT_data_member_location with
9966 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9967 constant, so we need to use DW_FORM_udata if we need
9968 a large constant. */
9969 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9970 return DW_FORM_udata;
9971 return DW_FORM_data4;
9972 case 8:
9973 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9974 return DW_FORM_udata;
9975 return DW_FORM_data8;
9976 default:
9977 gcc_unreachable ();
9979 case dw_val_class_const_implicit:
9980 case dw_val_class_unsigned_const_implicit:
9981 case dw_val_class_file_implicit:
9982 return DW_FORM_implicit_const;
9983 case dw_val_class_const_double:
9984 switch (HOST_BITS_PER_WIDE_INT)
9986 case 8:
9987 return DW_FORM_data2;
9988 case 16:
9989 return DW_FORM_data4;
9990 case 32:
9991 return DW_FORM_data8;
9992 case 64:
9993 if (dwarf_version >= 5)
9994 return DW_FORM_data16;
9995 /* FALLTHRU */
9996 default:
9997 return DW_FORM_block1;
9999 case dw_val_class_wide_int:
10000 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
10002 case 8:
10003 return DW_FORM_data1;
10004 case 16:
10005 return DW_FORM_data2;
10006 case 32:
10007 return DW_FORM_data4;
10008 case 64:
10009 return DW_FORM_data8;
10010 case 128:
10011 if (dwarf_version >= 5)
10012 return DW_FORM_data16;
10013 /* FALLTHRU */
10014 default:
10015 return DW_FORM_block1;
10017 case dw_val_class_symview:
10018 /* ??? We might use uleb128, but then we'd have to compute
10019 .debug_info offsets in the assembler. */
10020 if (symview_upper_bound <= 0xff)
10021 return DW_FORM_data1;
10022 else if (symview_upper_bound <= 0xffff)
10023 return DW_FORM_data2;
10024 else if (symview_upper_bound <= 0xffffffff)
10025 return DW_FORM_data4;
10026 else
10027 return DW_FORM_data8;
10028 case dw_val_class_vec:
10029 switch (constant_size (a->dw_attr_val.v.val_vec.length
10030 * a->dw_attr_val.v.val_vec.elt_size))
10032 case 1:
10033 return DW_FORM_block1;
10034 case 2:
10035 return DW_FORM_block2;
10036 case 4:
10037 return DW_FORM_block4;
10038 default:
10039 gcc_unreachable ();
10041 case dw_val_class_flag:
10042 if (dwarf_version >= 4)
10044 /* Currently all add_AT_flag calls pass in 1 as last argument,
10045 so DW_FORM_flag_present can be used. If that ever changes,
10046 we'll need to use DW_FORM_flag and have some optimization
10047 in build_abbrev_table that will change those to
10048 DW_FORM_flag_present if it is set to 1 in all DIEs using
10049 the same abbrev entry. */
10050 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10051 return DW_FORM_flag_present;
10053 return DW_FORM_flag;
10054 case dw_val_class_die_ref:
10055 if (AT_ref_external (a))
10057 if (AT_ref (a)->comdat_type_p)
10058 return DW_FORM_ref_sig8;
10059 else
10060 return DW_FORM_ref_addr;
10062 else
10063 return DW_FORM_ref;
10064 case dw_val_class_fde_ref:
10065 return DW_FORM_data;
10066 case dw_val_class_lbl_id:
10067 return (AT_index (a) == NOT_INDEXED
10068 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10069 case dw_val_class_lineptr:
10070 case dw_val_class_macptr:
10071 case dw_val_class_loclistsptr:
10072 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10073 case dw_val_class_str:
10074 return AT_string_form (a);
10075 case dw_val_class_file:
10076 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10078 case 1:
10079 return DW_FORM_data1;
10080 case 2:
10081 return DW_FORM_data2;
10082 case 4:
10083 return DW_FORM_data4;
10084 default:
10085 gcc_unreachable ();
10088 case dw_val_class_data8:
10089 return DW_FORM_data8;
10091 case dw_val_class_high_pc:
10092 switch (DWARF2_ADDR_SIZE)
10094 case 1:
10095 return DW_FORM_data1;
10096 case 2:
10097 return DW_FORM_data2;
10098 case 4:
10099 return DW_FORM_data4;
10100 case 8:
10101 return DW_FORM_data8;
10102 default:
10103 gcc_unreachable ();
10106 case dw_val_class_discr_value:
10107 return (a->dw_attr_val.v.val_discr_value.pos
10108 ? DW_FORM_udata
10109 : DW_FORM_sdata);
10110 case dw_val_class_discr_list:
10111 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10113 case 1:
10114 return DW_FORM_block1;
10115 case 2:
10116 return DW_FORM_block2;
10117 case 4:
10118 return DW_FORM_block4;
10119 default:
10120 gcc_unreachable ();
10123 default:
10124 gcc_unreachable ();
10128 /* Output the encoding of an attribute value. */
10130 static void
10131 output_value_format (dw_attr_node *a)
10133 enum dwarf_form form = value_format (a);
10135 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10138 /* Given a die and id, produce the appropriate abbreviations. */
10140 static void
10141 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10143 unsigned ix;
10144 dw_attr_node *a_attr;
10146 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10147 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10148 dwarf_tag_name (abbrev->die_tag));
10150 if (abbrev->die_child != NULL)
10151 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10152 else
10153 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10155 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10157 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10158 dwarf_attr_name (a_attr->dw_attr));
10159 output_value_format (a_attr);
10160 if (value_format (a_attr) == DW_FORM_implicit_const)
10162 if (AT_class (a_attr) == dw_val_class_file_implicit)
10164 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10165 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10166 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10168 else
10169 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10173 dw2_asm_output_data (1, 0, NULL);
10174 dw2_asm_output_data (1, 0, NULL);
10178 /* Output the .debug_abbrev section which defines the DIE abbreviation
10179 table. */
10181 static void
10182 output_abbrev_section (void)
10184 unsigned int abbrev_id;
10185 dw_die_ref abbrev;
10187 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10188 if (abbrev_id != 0)
10189 output_die_abbrevs (abbrev_id, abbrev);
10191 /* Terminate the table. */
10192 dw2_asm_output_data (1, 0, NULL);
10195 /* Return a new location list, given the begin and end range, and the
10196 expression. */
10198 static inline dw_loc_list_ref
10199 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10200 const char *end, var_loc_view vend,
10201 const char *section)
10203 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10205 retlist->begin = begin;
10206 retlist->begin_entry = NULL;
10207 retlist->end = end;
10208 retlist->end_entry = NULL;
10209 retlist->expr = expr;
10210 retlist->section = section;
10211 retlist->vbegin = vbegin;
10212 retlist->vend = vend;
10214 return retlist;
10217 /* Return true iff there's any nonzero view number in the loc list.
10219 ??? When views are not enabled, we'll often extend a single range
10220 to the entire function, so that we emit a single location
10221 expression rather than a location list. With views, even with a
10222 single range, we'll output a list if start or end have a nonzero
10223 view. If we change this, we may want to stop splitting a single
10224 range in dw_loc_list just because of a nonzero view, even if it
10225 straddles across hot/cold partitions. */
10227 static bool
10228 loc_list_has_views (dw_loc_list_ref list)
10230 if (!debug_variable_location_views)
10231 return false;
10233 for (dw_loc_list_ref loc = list;
10234 loc != NULL; loc = loc->dw_loc_next)
10235 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10236 return true;
10238 return false;
10241 /* Generate a new internal symbol for this location list node, if it
10242 hasn't got one yet. */
10244 static inline void
10245 gen_llsym (dw_loc_list_ref list)
10247 gcc_assert (!list->ll_symbol);
10248 list->ll_symbol = gen_internal_sym ("LLST");
10250 if (!loc_list_has_views (list))
10251 return;
10253 if (dwarf2out_locviews_in_attribute ())
10255 /* Use the same label_num for the view list. */
10256 label_num--;
10257 list->vl_symbol = gen_internal_sym ("LVUS");
10259 else
10260 list->vl_symbol = list->ll_symbol;
10263 /* Generate a symbol for the list, but only if we really want to emit
10264 it as a list. */
10266 static inline void
10267 maybe_gen_llsym (dw_loc_list_ref list)
10269 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10270 return;
10272 gen_llsym (list);
10275 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10276 NULL, don't consider size of the location expression. If we're not
10277 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10278 representation in *SIZEP. */
10280 static bool
10281 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10283 /* Don't output an entry that starts and ends at the same address. */
10284 if (strcmp (curr->begin, curr->end) == 0
10285 && curr->vbegin == curr->vend && !curr->force)
10286 return true;
10288 if (!sizep)
10289 return false;
10291 unsigned long size = size_of_locs (curr->expr);
10293 /* If the expression is too large, drop it on the floor. We could
10294 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10295 in the expression, but >= 64KB expressions for a single value
10296 in a single range are unlikely very useful. */
10297 if (dwarf_version < 5 && size > 0xffff)
10298 return true;
10300 *sizep = size;
10302 return false;
10305 /* Output a view pair loclist entry for CURR, if it requires one. */
10307 static void
10308 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10310 if (!dwarf2out_locviews_in_loclist ())
10311 return;
10313 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10314 return;
10316 #ifdef DW_LLE_view_pair
10317 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10319 if (dwarf2out_as_locview_support)
10321 if (ZERO_VIEW_P (curr->vbegin))
10322 dw2_asm_output_data_uleb128 (0, "Location view begin");
10323 else
10325 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10326 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10327 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10330 if (ZERO_VIEW_P (curr->vend))
10331 dw2_asm_output_data_uleb128 (0, "Location view end");
10332 else
10334 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10335 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10336 dw2_asm_output_symname_uleb128 (label, "Location view end");
10339 else
10341 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10342 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10344 #endif /* DW_LLE_view_pair */
10346 return;
10349 /* Output the location list given to us. */
10351 static void
10352 output_loc_list (dw_loc_list_ref list_head)
10354 int vcount = 0, lcount = 0;
10356 if (list_head->emitted)
10357 return;
10358 list_head->emitted = true;
10360 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10362 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10364 for (dw_loc_list_ref curr = list_head; curr != NULL;
10365 curr = curr->dw_loc_next)
10367 unsigned long size;
10369 if (skip_loc_list_entry (curr, &size))
10370 continue;
10372 vcount++;
10374 /* ?? dwarf_split_debug_info? */
10375 if (dwarf2out_as_locview_support)
10377 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10379 if (!ZERO_VIEW_P (curr->vbegin))
10381 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10382 dw2_asm_output_symname_uleb128 (label,
10383 "View list begin (%s)",
10384 list_head->vl_symbol);
10386 else
10387 dw2_asm_output_data_uleb128 (0,
10388 "View list begin (%s)",
10389 list_head->vl_symbol);
10391 if (!ZERO_VIEW_P (curr->vend))
10393 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10394 dw2_asm_output_symname_uleb128 (label,
10395 "View list end (%s)",
10396 list_head->vl_symbol);
10398 else
10399 dw2_asm_output_data_uleb128 (0,
10400 "View list end (%s)",
10401 list_head->vl_symbol);
10403 else
10405 dw2_asm_output_data_uleb128 (curr->vbegin,
10406 "View list begin (%s)",
10407 list_head->vl_symbol);
10408 dw2_asm_output_data_uleb128 (curr->vend,
10409 "View list end (%s)",
10410 list_head->vl_symbol);
10415 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10417 const char *last_section = NULL;
10418 const char *base_label = NULL;
10420 /* Walk the location list, and output each range + expression. */
10421 for (dw_loc_list_ref curr = list_head; curr != NULL;
10422 curr = curr->dw_loc_next)
10424 unsigned long size;
10426 /* Skip this entry? If we skip it here, we must skip it in the
10427 view list above as well. */
10428 if (skip_loc_list_entry (curr, &size))
10429 continue;
10431 lcount++;
10433 if (dwarf_version >= 5)
10435 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10437 dwarf2out_maybe_output_loclist_view_pair (curr);
10438 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10439 uleb128 index into .debug_addr and uleb128 length. */
10440 dw2_asm_output_data (1, DW_LLE_startx_length,
10441 "DW_LLE_startx_length (%s)",
10442 list_head->ll_symbol);
10443 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10444 "Location list range start index "
10445 "(%s)", curr->begin);
10446 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10447 "Location list length (%s)",
10448 list_head->ll_symbol);
10450 else if (dwarf_split_debug_info)
10452 dwarf2out_maybe_output_loclist_view_pair (curr);
10453 /* For -gsplit-dwarf without usable .uleb128 support, emit
10454 DW_LLE_startx_endx, which has two uleb128 indexes into
10455 .debug_addr. */
10456 dw2_asm_output_data (1, DW_LLE_startx_endx,
10457 "DW_LLE_startx_endx (%s)",
10458 list_head->ll_symbol);
10459 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10460 "Location list range start index "
10461 "(%s)", curr->begin);
10462 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10463 "Location list range end index "
10464 "(%s)", curr->end);
10466 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10468 dwarf2out_maybe_output_loclist_view_pair (curr);
10469 /* If all code is in .text section, the base address is
10470 already provided by the CU attributes. Use
10471 DW_LLE_offset_pair where both addresses are uleb128 encoded
10472 offsets against that base. */
10473 dw2_asm_output_data (1, DW_LLE_offset_pair,
10474 "DW_LLE_offset_pair (%s)",
10475 list_head->ll_symbol);
10476 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10477 "Location list begin address (%s)",
10478 list_head->ll_symbol);
10479 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10480 "Location list end address (%s)",
10481 list_head->ll_symbol);
10483 else if (HAVE_AS_LEB128)
10485 /* Otherwise, find out how many consecutive entries could share
10486 the same base entry. If just one, emit DW_LLE_start_length,
10487 otherwise emit DW_LLE_base_address for the base address
10488 followed by a series of DW_LLE_offset_pair. */
10489 if (last_section == NULL || curr->section != last_section)
10491 dw_loc_list_ref curr2;
10492 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10493 curr2 = curr2->dw_loc_next)
10495 if (strcmp (curr2->begin, curr2->end) == 0
10496 && !curr2->force)
10497 continue;
10498 break;
10500 if (curr2 == NULL || curr->section != curr2->section)
10501 last_section = NULL;
10502 else
10504 last_section = curr->section;
10505 base_label = curr->begin;
10506 dw2_asm_output_data (1, DW_LLE_base_address,
10507 "DW_LLE_base_address (%s)",
10508 list_head->ll_symbol);
10509 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10510 "Base address (%s)",
10511 list_head->ll_symbol);
10514 /* Only one entry with the same base address. Use
10515 DW_LLE_start_length with absolute address and uleb128
10516 length. */
10517 if (last_section == NULL)
10519 dwarf2out_maybe_output_loclist_view_pair (curr);
10520 dw2_asm_output_data (1, DW_LLE_start_length,
10521 "DW_LLE_start_length (%s)",
10522 list_head->ll_symbol);
10523 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10524 "Location list begin address (%s)",
10525 list_head->ll_symbol);
10526 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10527 "Location list length "
10528 "(%s)", list_head->ll_symbol);
10530 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10531 DW_LLE_base_address. */
10532 else
10534 dwarf2out_maybe_output_loclist_view_pair (curr);
10535 dw2_asm_output_data (1, DW_LLE_offset_pair,
10536 "DW_LLE_offset_pair (%s)",
10537 list_head->ll_symbol);
10538 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10539 "Location list begin address "
10540 "(%s)", list_head->ll_symbol);
10541 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10542 "Location list end address "
10543 "(%s)", list_head->ll_symbol);
10546 /* The assembler does not support .uleb128 directive. Emit
10547 DW_LLE_start_end with a pair of absolute addresses. */
10548 else
10550 dwarf2out_maybe_output_loclist_view_pair (curr);
10551 dw2_asm_output_data (1, DW_LLE_start_end,
10552 "DW_LLE_start_end (%s)",
10553 list_head->ll_symbol);
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 else if (dwarf_split_debug_info)
10564 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10565 and 4 byte length. */
10566 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10567 "Location list start/length entry (%s)",
10568 list_head->ll_symbol);
10569 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10570 "Location list range start index (%s)",
10571 curr->begin);
10572 /* The length field is 4 bytes. If we ever need to support
10573 an 8-byte length, we can add a new DW_LLE code or fall back
10574 to DW_LLE_GNU_start_end_entry. */
10575 dw2_asm_output_delta (4, curr->end, curr->begin,
10576 "Location list range length (%s)",
10577 list_head->ll_symbol);
10579 else if (!have_multiple_function_sections)
10581 /* Pair of relative addresses against start of text section. */
10582 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10583 "Location list begin address (%s)",
10584 list_head->ll_symbol);
10585 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10586 "Location list end address (%s)",
10587 list_head->ll_symbol);
10589 else
10591 /* Pair of absolute addresses. */
10592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10593 "Location list begin address (%s)",
10594 list_head->ll_symbol);
10595 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10596 "Location list end address (%s)",
10597 list_head->ll_symbol);
10600 /* Output the block length for this list of location operations. */
10601 if (dwarf_version >= 5)
10602 dw2_asm_output_data_uleb128 (size, "Location expression size");
10603 else
10605 gcc_assert (size <= 0xffff);
10606 dw2_asm_output_data (2, size, "Location expression size");
10609 output_loc_sequence (curr->expr, -1);
10612 /* And finally list termination. */
10613 if (dwarf_version >= 5)
10614 dw2_asm_output_data (1, DW_LLE_end_of_list,
10615 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10616 else if (dwarf_split_debug_info)
10617 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10618 "Location list terminator (%s)",
10619 list_head->ll_symbol);
10620 else
10622 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10623 "Location list terminator begin (%s)",
10624 list_head->ll_symbol);
10625 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10626 "Location list terminator end (%s)",
10627 list_head->ll_symbol);
10630 gcc_assert (!list_head->vl_symbol
10631 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10634 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10635 section. Emit a relocated reference if val_entry is NULL, otherwise,
10636 emit an indirect reference. */
10638 static void
10639 output_range_list_offset (dw_attr_node *a)
10641 const char *name = dwarf_attr_name (a->dw_attr);
10643 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10645 if (dwarf_version >= 5)
10647 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10648 dw2_asm_output_offset (dwarf_offset_size, r->label,
10649 debug_ranges_section, "%s", name);
10651 else
10653 char *p = strchr (ranges_section_label, '\0');
10654 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10655 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10656 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10657 debug_ranges_section, "%s", name);
10658 *p = '\0';
10661 else if (dwarf_version >= 5)
10663 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10664 gcc_assert (rnglist_idx);
10665 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10667 else
10668 dw2_asm_output_data (dwarf_offset_size,
10669 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10670 "%s (offset from %s)", name, ranges_section_label);
10673 /* Output the offset into the debug_loc section. */
10675 static void
10676 output_loc_list_offset (dw_attr_node *a)
10678 char *sym = AT_loc_list (a)->ll_symbol;
10680 gcc_assert (sym);
10681 if (!dwarf_split_debug_info)
10682 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10683 "%s", dwarf_attr_name (a->dw_attr));
10684 else if (dwarf_version >= 5)
10686 gcc_assert (AT_loc_list (a)->num_assigned);
10687 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10688 dwarf_attr_name (a->dw_attr),
10689 sym);
10691 else
10692 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10693 "%s", dwarf_attr_name (a->dw_attr));
10696 /* Output the offset into the debug_loc section. */
10698 static void
10699 output_view_list_offset (dw_attr_node *a)
10701 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10703 gcc_assert (sym);
10704 if (dwarf_split_debug_info)
10705 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10706 "%s", dwarf_attr_name (a->dw_attr));
10707 else
10708 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10709 "%s", dwarf_attr_name (a->dw_attr));
10712 /* Output an attribute's index or value appropriately. */
10714 static void
10715 output_attr_index_or_value (dw_attr_node *a)
10717 const char *name = dwarf_attr_name (a->dw_attr);
10719 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10721 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10722 return;
10724 switch (AT_class (a))
10726 case dw_val_class_addr:
10727 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10728 break;
10729 case dw_val_class_high_pc:
10730 case dw_val_class_lbl_id:
10731 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10732 break;
10733 default:
10734 gcc_unreachable ();
10738 /* Output a type signature. */
10740 static inline void
10741 output_signature (const char *sig, const char *name)
10743 int i;
10745 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10746 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10749 /* Output a discriminant value. */
10751 static inline void
10752 output_discr_value (dw_discr_value *discr_value, const char *name)
10754 if (discr_value->pos)
10755 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10756 else
10757 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10760 /* Output the DIE and its attributes. Called recursively to generate
10761 the definitions of each child DIE. */
10763 static void
10764 output_die (dw_die_ref die)
10766 dw_attr_node *a;
10767 dw_die_ref c;
10768 unsigned long size;
10769 unsigned ix;
10771 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10772 (unsigned long)die->die_offset,
10773 dwarf_tag_name (die->die_tag));
10775 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10777 const char *name = dwarf_attr_name (a->dw_attr);
10779 switch (AT_class (a))
10781 case dw_val_class_addr:
10782 output_attr_index_or_value (a);
10783 break;
10785 case dw_val_class_offset:
10786 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10787 "%s", name);
10788 break;
10790 case dw_val_class_range_list:
10791 output_range_list_offset (a);
10792 break;
10794 case dw_val_class_loc:
10795 size = size_of_locs (AT_loc (a));
10797 /* Output the block length for this list of location operations. */
10798 if (dwarf_version >= 4)
10799 dw2_asm_output_data_uleb128 (size, "%s", name);
10800 else
10801 dw2_asm_output_data (constant_size (size), size, "%s", name);
10803 output_loc_sequence (AT_loc (a), -1);
10804 break;
10806 case dw_val_class_const:
10807 /* ??? It would be slightly more efficient to use a scheme like is
10808 used for unsigned constants below, but gdb 4.x does not sign
10809 extend. Gdb 5.x does sign extend. */
10810 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10811 break;
10813 case dw_val_class_unsigned_const:
10815 int csize = constant_size (AT_unsigned (a));
10816 if (dwarf_version == 3
10817 && a->dw_attr == DW_AT_data_member_location
10818 && csize >= 4)
10819 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10820 else
10821 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10823 break;
10825 case dw_val_class_symview:
10827 int vsize;
10828 if (symview_upper_bound <= 0xff)
10829 vsize = 1;
10830 else if (symview_upper_bound <= 0xffff)
10831 vsize = 2;
10832 else if (symview_upper_bound <= 0xffffffff)
10833 vsize = 4;
10834 else
10835 vsize = 8;
10836 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10837 "%s", name);
10839 break;
10841 case dw_val_class_const_implicit:
10842 if (flag_debug_asm)
10843 fprintf (asm_out_file, "\t\t\t%s %s ("
10844 HOST_WIDE_INT_PRINT_DEC ")\n",
10845 ASM_COMMENT_START, name, AT_int (a));
10846 break;
10848 case dw_val_class_unsigned_const_implicit:
10849 if (flag_debug_asm)
10850 fprintf (asm_out_file, "\t\t\t%s %s ("
10851 HOST_WIDE_INT_PRINT_HEX ")\n",
10852 ASM_COMMENT_START, name, AT_unsigned (a));
10853 break;
10855 case dw_val_class_const_double:
10857 unsigned HOST_WIDE_INT first, second;
10859 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10860 dw2_asm_output_data (1,
10861 HOST_BITS_PER_DOUBLE_INT
10862 / HOST_BITS_PER_CHAR,
10863 NULL);
10865 if (WORDS_BIG_ENDIAN)
10867 first = a->dw_attr_val.v.val_double.high;
10868 second = a->dw_attr_val.v.val_double.low;
10870 else
10872 first = a->dw_attr_val.v.val_double.low;
10873 second = a->dw_attr_val.v.val_double.high;
10876 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10877 first, "%s", name);
10878 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10879 second, NULL);
10881 break;
10883 case dw_val_class_wide_int:
10885 int i;
10886 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10887 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10888 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10889 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10890 * l, NULL);
10892 if (WORDS_BIG_ENDIAN)
10893 for (i = len - 1; i >= 0; --i)
10895 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10896 "%s", name);
10897 name = "";
10899 else
10900 for (i = 0; i < len; ++i)
10902 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10903 "%s", name);
10904 name = "";
10907 break;
10909 case dw_val_class_vec:
10911 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10912 unsigned int len = a->dw_attr_val.v.val_vec.length;
10913 unsigned int i;
10914 unsigned char *p;
10916 dw2_asm_output_data (constant_size (len * elt_size),
10917 len * elt_size, "%s", name);
10918 if (elt_size > sizeof (HOST_WIDE_INT))
10920 elt_size /= 2;
10921 len *= 2;
10923 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10924 i < len;
10925 i++, p += elt_size)
10926 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10927 "fp or vector constant word %u", i);
10928 break;
10931 case dw_val_class_flag:
10932 if (dwarf_version >= 4)
10934 /* Currently all add_AT_flag calls pass in 1 as last argument,
10935 so DW_FORM_flag_present can be used. If that ever changes,
10936 we'll need to use DW_FORM_flag and have some optimization
10937 in build_abbrev_table that will change those to
10938 DW_FORM_flag_present if it is set to 1 in all DIEs using
10939 the same abbrev entry. */
10940 gcc_assert (AT_flag (a) == 1);
10941 if (flag_debug_asm)
10942 fprintf (asm_out_file, "\t\t\t%s %s\n",
10943 ASM_COMMENT_START, name);
10944 break;
10946 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10947 break;
10949 case dw_val_class_loc_list:
10950 output_loc_list_offset (a);
10951 break;
10953 case dw_val_class_view_list:
10954 output_view_list_offset (a);
10955 break;
10957 case dw_val_class_die_ref:
10958 if (AT_ref_external (a))
10960 if (AT_ref (a)->comdat_type_p)
10962 comdat_type_node *type_node
10963 = AT_ref (a)->die_id.die_type_node;
10965 gcc_assert (type_node);
10966 output_signature (type_node->signature, name);
10968 else
10970 const char *sym = AT_ref (a)->die_id.die_symbol;
10971 int size;
10973 gcc_assert (sym);
10974 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10975 length, whereas in DWARF3 it's always sized as an
10976 offset. */
10977 if (dwarf_version == 2)
10978 size = DWARF2_ADDR_SIZE;
10979 else
10980 size = dwarf_offset_size;
10981 /* ??? We cannot unconditionally output die_offset if
10982 non-zero - others might create references to those
10983 DIEs via symbols.
10984 And we do not clear its DIE offset after outputting it
10985 (and the label refers to the actual DIEs, not the
10986 DWARF CU unit header which is when using label + offset
10987 would be the correct thing to do).
10988 ??? This is the reason for the with_offset flag. */
10989 if (AT_ref (a)->with_offset)
10990 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10991 debug_info_section, "%s", name);
10992 else
10993 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10994 name);
10997 else
10999 gcc_assert (AT_ref (a)->die_offset);
11000 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
11001 "%s", name);
11003 break;
11005 case dw_val_class_fde_ref:
11007 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11009 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11010 a->dw_attr_val.v.val_fde_index * 2);
11011 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
11012 "%s", name);
11014 break;
11016 case dw_val_class_vms_delta:
11017 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
11018 dw2_asm_output_vms_delta (dwarf_offset_size,
11019 AT_vms_delta2 (a), AT_vms_delta1 (a),
11020 "%s", name);
11021 #else
11022 dw2_asm_output_delta (dwarf_offset_size,
11023 AT_vms_delta2 (a), AT_vms_delta1 (a),
11024 "%s", name);
11025 #endif
11026 break;
11028 case dw_val_class_lbl_id:
11029 output_attr_index_or_value (a);
11030 break;
11032 case dw_val_class_lineptr:
11033 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11034 debug_line_section, "%s", name);
11035 break;
11037 case dw_val_class_macptr:
11038 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11039 debug_macinfo_section, "%s", name);
11040 break;
11042 case dw_val_class_loclistsptr:
11043 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11044 debug_loc_section, "%s", name);
11045 break;
11047 case dw_val_class_str:
11048 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11049 dw2_asm_output_offset (dwarf_offset_size,
11050 a->dw_attr_val.v.val_str->label,
11051 debug_str_section,
11052 "%s: \"%s\"", name, AT_string (a));
11053 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11054 dw2_asm_output_offset (dwarf_offset_size,
11055 a->dw_attr_val.v.val_str->label,
11056 debug_line_str_section,
11057 "%s: \"%s\"", name, AT_string (a));
11058 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11059 dw2_asm_output_data_uleb128 (AT_index (a),
11060 "%s: \"%s\"", name, AT_string (a));
11061 else
11062 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11063 break;
11065 case dw_val_class_file:
11067 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11069 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11070 a->dw_attr_val.v.val_file->filename);
11071 break;
11074 case dw_val_class_file_implicit:
11075 if (flag_debug_asm)
11076 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11077 ASM_COMMENT_START, name,
11078 maybe_emit_file (a->dw_attr_val.v.val_file),
11079 a->dw_attr_val.v.val_file->filename);
11080 break;
11082 case dw_val_class_data8:
11084 int i;
11086 for (i = 0; i < 8; i++)
11087 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11088 i == 0 ? "%s" : NULL, name);
11089 break;
11092 case dw_val_class_high_pc:
11093 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11094 get_AT_low_pc (die), "DW_AT_high_pc");
11095 break;
11097 case dw_val_class_discr_value:
11098 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11099 break;
11101 case dw_val_class_discr_list:
11103 dw_discr_list_ref list = AT_discr_list (a);
11104 const int size = size_of_discr_list (list);
11106 /* This is a block, so output its length first. */
11107 dw2_asm_output_data (constant_size (size), size,
11108 "%s: block size", name);
11110 for (; list != NULL; list = list->dw_discr_next)
11112 /* One byte for the discriminant value descriptor, and then as
11113 many LEB128 numbers as required. */
11114 if (list->dw_discr_range)
11115 dw2_asm_output_data (1, DW_DSC_range,
11116 "%s: DW_DSC_range", name);
11117 else
11118 dw2_asm_output_data (1, DW_DSC_label,
11119 "%s: DW_DSC_label", name);
11121 output_discr_value (&list->dw_discr_lower_bound, name);
11122 if (list->dw_discr_range)
11123 output_discr_value (&list->dw_discr_upper_bound, name);
11125 break;
11128 default:
11129 gcc_unreachable ();
11133 FOR_EACH_CHILD (die, c, output_die (c));
11135 /* Add null byte to terminate sibling list. */
11136 if (die->die_child != NULL)
11137 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11138 (unsigned long) die->die_offset);
11141 /* Output the dwarf version number. */
11143 static void
11144 output_dwarf_version ()
11146 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11147 views in loclist. That will change eventually. */
11148 if (dwarf_version == 6)
11150 static bool once;
11151 if (!once)
11153 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11154 "incompatibilities");
11155 once = true;
11157 dw2_asm_output_data (2, 5, "DWARF version number");
11159 else
11160 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11163 /* Output the compilation unit that appears at the beginning of the
11164 .debug_info section, and precedes the DIE descriptions. */
11166 static void
11167 output_compilation_unit_header (enum dwarf_unit_type ut)
11169 if (!XCOFF_DEBUGGING_INFO)
11171 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11172 dw2_asm_output_data (4, 0xffffffff,
11173 "Initial length escape value indicating 64-bit DWARF extension");
11174 dw2_asm_output_data (dwarf_offset_size,
11175 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11176 "Length of Compilation Unit Info");
11179 output_dwarf_version ();
11180 if (dwarf_version >= 5)
11182 const char *name;
11183 switch (ut)
11185 case DW_UT_compile: name = "DW_UT_compile"; break;
11186 case DW_UT_type: name = "DW_UT_type"; break;
11187 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11188 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11189 default: gcc_unreachable ();
11191 dw2_asm_output_data (1, ut, "%s", name);
11192 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11194 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11195 debug_abbrev_section,
11196 "Offset Into Abbrev. Section");
11197 if (dwarf_version < 5)
11198 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11201 /* Output the compilation unit DIE and its children. */
11203 static void
11204 output_comp_unit (dw_die_ref die, int output_if_empty,
11205 const unsigned char *dwo_id)
11207 const char *secname, *oldsym;
11208 char *tmp;
11210 /* Unless we are outputting main CU, we may throw away empty ones. */
11211 if (!output_if_empty && die->die_child == NULL)
11212 return;
11214 /* Even if there are no children of this DIE, we must output the information
11215 about the compilation unit. Otherwise, on an empty translation unit, we
11216 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11217 will then complain when examining the file. First mark all the DIEs in
11218 this CU so we know which get local refs. */
11219 mark_dies (die);
11221 external_ref_hash_type *extern_map = optimize_external_refs (die);
11223 /* For now, optimize only the main CU, in order to optimize the rest
11224 we'd need to see all of them earlier. Leave the rest for post-linking
11225 tools like DWZ. */
11226 if (die == comp_unit_die ())
11227 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11229 build_abbrev_table (die, extern_map);
11231 optimize_abbrev_table ();
11233 delete extern_map;
11235 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11236 next_die_offset = (dwo_id
11237 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11238 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11239 calc_die_sizes (die);
11241 oldsym = die->die_id.die_symbol;
11242 if (oldsym && die->comdat_type_p)
11244 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11246 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11247 secname = tmp;
11248 die->die_id.die_symbol = NULL;
11249 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11251 else
11253 switch_to_section (debug_info_section);
11254 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11255 info_section_emitted = true;
11258 /* For LTO cross unit DIE refs we want a symbol on the start of the
11259 debuginfo section, not on the CU DIE. */
11260 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11262 /* ??? No way to get visibility assembled without a decl. */
11263 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11264 get_identifier (oldsym), char_type_node);
11265 TREE_PUBLIC (decl) = true;
11266 TREE_STATIC (decl) = true;
11267 DECL_ARTIFICIAL (decl) = true;
11268 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11269 DECL_VISIBILITY_SPECIFIED (decl) = true;
11270 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11271 #ifdef ASM_WEAKEN_LABEL
11272 /* We prefer a .weak because that handles duplicates from duplicate
11273 archive members in a graceful way. */
11274 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11275 #else
11276 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11277 #endif
11278 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11281 /* Output debugging information. */
11282 output_compilation_unit_header (dwo_id
11283 ? DW_UT_split_compile : DW_UT_compile);
11284 if (dwarf_version >= 5)
11286 if (dwo_id != NULL)
11287 for (int i = 0; i < 8; i++)
11288 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11290 output_die (die);
11292 /* Leave the marks on the main CU, so we can check them in
11293 output_pubnames. */
11294 if (oldsym)
11296 unmark_dies (die);
11297 die->die_id.die_symbol = oldsym;
11301 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11302 and .debug_pubtypes. This is configured per-target, but can be
11303 overridden by the -gpubnames or -gno-pubnames options. */
11305 static inline bool
11306 want_pubnames (void)
11308 if (debug_info_level <= DINFO_LEVEL_TERSE
11309 /* Names and types go to the early debug part only. */
11310 || in_lto_p)
11311 return false;
11312 if (debug_generate_pub_sections != -1)
11313 return debug_generate_pub_sections;
11314 return targetm.want_debug_pub_sections;
11317 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11319 static void
11320 add_AT_pubnames (dw_die_ref die)
11322 if (want_pubnames ())
11323 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11326 /* Add a string attribute value to a skeleton DIE. */
11328 static inline void
11329 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11330 const char *str)
11332 dw_attr_node attr;
11333 struct indirect_string_node *node;
11335 if (! skeleton_debug_str_hash)
11336 skeleton_debug_str_hash
11337 = hash_table<indirect_string_hasher>::create_ggc (10);
11339 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11340 find_string_form (node);
11341 if (node->form == dwarf_FORM (DW_FORM_strx))
11342 node->form = DW_FORM_strp;
11344 attr.dw_attr = attr_kind;
11345 attr.dw_attr_val.val_class = dw_val_class_str;
11346 attr.dw_attr_val.val_entry = NULL;
11347 attr.dw_attr_val.v.val_str = node;
11348 add_dwarf_attr (die, &attr);
11351 /* Helper function to generate top-level dies for skeleton debug_info and
11352 debug_types. */
11354 static void
11355 add_top_level_skeleton_die_attrs (dw_die_ref die)
11357 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11358 const char *comp_dir = comp_dir_string ();
11360 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11361 if (comp_dir != NULL)
11362 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11363 add_AT_pubnames (die);
11364 if (addr_index_table != NULL && addr_index_table->size () > 0)
11365 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11368 /* Output skeleton debug sections that point to the dwo file. */
11370 static void
11371 output_skeleton_debug_sections (dw_die_ref comp_unit,
11372 const unsigned char *dwo_id)
11374 /* These attributes will be found in the full debug_info section. */
11375 remove_AT (comp_unit, DW_AT_producer);
11376 remove_AT (comp_unit, DW_AT_language);
11378 switch_to_section (debug_skeleton_info_section);
11379 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11381 /* Produce the skeleton compilation-unit header. This one differs enough from
11382 a normal CU header that it's better not to call output_compilation_unit
11383 header. */
11384 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11385 dw2_asm_output_data (4, 0xffffffff,
11386 "Initial length escape value indicating 64-bit "
11387 "DWARF extension");
11389 dw2_asm_output_data (dwarf_offset_size,
11390 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11391 - DWARF_INITIAL_LENGTH_SIZE
11392 + size_of_die (comp_unit),
11393 "Length of Compilation Unit Info");
11394 output_dwarf_version ();
11395 if (dwarf_version >= 5)
11397 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11398 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11400 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11401 debug_skeleton_abbrev_section,
11402 "Offset Into Abbrev. Section");
11403 if (dwarf_version < 5)
11404 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11405 else
11406 for (int i = 0; i < 8; i++)
11407 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11409 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11410 output_die (comp_unit);
11412 /* Build the skeleton debug_abbrev section. */
11413 switch_to_section (debug_skeleton_abbrev_section);
11414 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11416 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11418 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11421 /* Output a comdat type unit DIE and its children. */
11423 static void
11424 output_comdat_type_unit (comdat_type_node *node,
11425 bool early_lto_debug ATTRIBUTE_UNUSED)
11427 const char *secname;
11428 char *tmp;
11429 int i;
11430 #if defined (OBJECT_FORMAT_ELF)
11431 tree comdat_key;
11432 #endif
11434 /* First mark all the DIEs in this CU so we know which get local refs. */
11435 mark_dies (node->root_die);
11437 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11439 build_abbrev_table (node->root_die, extern_map);
11441 delete extern_map;
11442 extern_map = NULL;
11444 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11445 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11446 calc_die_sizes (node->root_die);
11448 #if defined (OBJECT_FORMAT_ELF)
11449 if (dwarf_version >= 5)
11451 if (!dwarf_split_debug_info)
11452 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11453 else
11454 secname = (early_lto_debug
11455 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11457 else if (!dwarf_split_debug_info)
11458 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11459 else
11460 secname = (early_lto_debug
11461 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11463 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11464 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11465 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11466 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11467 comdat_key = get_identifier (tmp);
11468 targetm.asm_out.named_section (secname,
11469 SECTION_DEBUG | SECTION_LINKONCE,
11470 comdat_key);
11471 #else
11472 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11473 sprintf (tmp, (dwarf_version >= 5
11474 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11475 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11476 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11477 secname = tmp;
11478 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11479 #endif
11481 /* Output debugging information. */
11482 output_compilation_unit_header (dwarf_split_debug_info
11483 ? DW_UT_split_type : DW_UT_type);
11484 output_signature (node->signature, "Type Signature");
11485 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11486 "Offset to Type DIE");
11487 output_die (node->root_die);
11489 unmark_dies (node->root_die);
11492 /* Return the DWARF2/3 pubname associated with a decl. */
11494 static const char *
11495 dwarf2_name (tree decl, int scope)
11497 if (DECL_NAMELESS (decl))
11498 return NULL;
11499 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11502 /* Add a new entry to .debug_pubnames if appropriate. */
11504 static void
11505 add_pubname_string (const char *str, dw_die_ref die)
11507 pubname_entry e;
11509 e.die = die;
11510 e.name = xstrdup (str);
11511 vec_safe_push (pubname_table, e);
11514 static void
11515 add_pubname (tree decl, dw_die_ref die)
11517 if (!want_pubnames ())
11518 return;
11520 /* Don't add items to the table when we expect that the consumer will have
11521 just read the enclosing die. For example, if the consumer is looking at a
11522 class_member, it will either be inside the class already, or will have just
11523 looked up the class to find the member. Either way, searching the class is
11524 faster than searching the index. */
11525 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11526 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11528 const char *name = dwarf2_name (decl, 1);
11530 if (name)
11531 add_pubname_string (name, die);
11535 /* Add an enumerator to the pubnames section. */
11537 static void
11538 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11540 pubname_entry e;
11542 gcc_assert (scope_name);
11543 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11544 e.die = die;
11545 vec_safe_push (pubname_table, e);
11548 /* Add a new entry to .debug_pubtypes if appropriate. */
11550 static void
11551 add_pubtype (tree decl, dw_die_ref die)
11553 pubname_entry e;
11555 if (!want_pubnames ())
11556 return;
11558 if ((TREE_PUBLIC (decl)
11559 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11560 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11562 tree scope = NULL;
11563 const char *scope_name = "";
11564 const char *sep = is_cxx () ? "::" : ".";
11565 const char *name;
11567 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11568 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11570 scope_name = lang_hooks.dwarf_name (scope, 1);
11571 if (scope_name != NULL && scope_name[0] != '\0')
11572 scope_name = concat (scope_name, sep, NULL);
11573 else
11574 scope_name = "";
11577 if (TYPE_P (decl))
11578 name = type_tag (decl);
11579 else
11580 name = lang_hooks.dwarf_name (decl, 1);
11582 /* If we don't have a name for the type, there's no point in adding
11583 it to the table. */
11584 if (name != NULL && name[0] != '\0')
11586 e.die = die;
11587 e.name = concat (scope_name, name, NULL);
11588 vec_safe_push (pubtype_table, e);
11591 /* Although it might be more consistent to add the pubinfo for the
11592 enumerators as their dies are created, they should only be added if the
11593 enum type meets the criteria above. So rather than re-check the parent
11594 enum type whenever an enumerator die is created, just output them all
11595 here. This isn't protected by the name conditional because anonymous
11596 enums don't have names. */
11597 if (die->die_tag == DW_TAG_enumeration_type)
11599 dw_die_ref c;
11601 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11606 /* Output a single entry in the pubnames table. */
11608 static void
11609 output_pubname (dw_offset die_offset, pubname_entry *entry)
11611 dw_die_ref die = entry->die;
11612 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11614 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11616 if (debug_generate_pub_sections == 2)
11618 /* This logic follows gdb's method for determining the value of the flag
11619 byte. */
11620 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11621 switch (die->die_tag)
11623 case DW_TAG_typedef:
11624 case DW_TAG_base_type:
11625 case DW_TAG_subrange_type:
11626 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11627 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11628 break;
11629 case DW_TAG_enumerator:
11630 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11631 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11632 if (!is_cxx ())
11633 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11634 break;
11635 case DW_TAG_subprogram:
11636 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11637 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11638 if (!is_ada ())
11639 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11640 break;
11641 case DW_TAG_constant:
11642 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11643 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11644 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11645 break;
11646 case DW_TAG_variable:
11647 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11648 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11649 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11650 break;
11651 case DW_TAG_namespace:
11652 case DW_TAG_imported_declaration:
11653 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11654 break;
11655 case DW_TAG_class_type:
11656 case DW_TAG_interface_type:
11657 case DW_TAG_structure_type:
11658 case DW_TAG_union_type:
11659 case DW_TAG_enumeration_type:
11660 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11661 if (!is_cxx ())
11662 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11663 break;
11664 default:
11665 /* An unusual tag. Leave the flag-byte empty. */
11666 break;
11668 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11669 "GDB-index flags");
11672 dw2_asm_output_nstring (entry->name, -1, "external name");
11676 /* Output the public names table used to speed up access to externally
11677 visible names; or the public types table used to find type definitions. */
11679 static void
11680 output_pubnames (vec<pubname_entry, va_gc> *names)
11682 unsigned i;
11683 unsigned long pubnames_length = size_of_pubnames (names);
11684 pubname_entry *pub;
11686 if (!XCOFF_DEBUGGING_INFO)
11688 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11689 dw2_asm_output_data (4, 0xffffffff,
11690 "Initial length escape value indicating 64-bit DWARF extension");
11691 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11692 "Pub Info Length");
11695 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11696 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11698 if (dwarf_split_debug_info)
11699 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11700 debug_skeleton_info_section,
11701 "Offset of Compilation Unit Info");
11702 else
11703 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11704 debug_info_section,
11705 "Offset of Compilation Unit Info");
11706 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11707 "Compilation Unit Length");
11709 FOR_EACH_VEC_ELT (*names, i, pub)
11711 if (include_pubname_in_output (names, pub))
11713 dw_offset die_offset = pub->die->die_offset;
11715 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11716 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11717 gcc_assert (pub->die->die_mark);
11719 /* If we're putting types in their own .debug_types sections,
11720 the .debug_pubtypes table will still point to the compile
11721 unit (not the type unit), so we want to use the offset of
11722 the skeleton DIE (if there is one). */
11723 if (pub->die->comdat_type_p && names == pubtype_table)
11725 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11727 if (type_node != NULL)
11728 die_offset = (type_node->skeleton_die != NULL
11729 ? type_node->skeleton_die->die_offset
11730 : comp_unit_die ()->die_offset);
11733 output_pubname (die_offset, pub);
11737 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11740 /* Output public names and types tables if necessary. */
11742 static void
11743 output_pubtables (void)
11745 if (!want_pubnames () || !info_section_emitted)
11746 return;
11748 switch_to_section (debug_pubnames_section);
11749 output_pubnames (pubname_table);
11750 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11751 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11752 simply won't look for the section. */
11753 switch_to_section (debug_pubtypes_section);
11754 output_pubnames (pubtype_table);
11758 /* Output the information that goes into the .debug_aranges table.
11759 Namely, define the beginning and ending address range of the
11760 text section generated for this compilation unit. */
11762 static void
11763 output_aranges (void)
11765 unsigned i;
11766 unsigned long aranges_length = size_of_aranges ();
11768 if (!XCOFF_DEBUGGING_INFO)
11770 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11771 dw2_asm_output_data (4, 0xffffffff,
11772 "Initial length escape value indicating 64-bit DWARF extension");
11773 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11774 "Length of Address Ranges Info");
11777 /* Version number for aranges is still 2, even up to DWARF5. */
11778 dw2_asm_output_data (2, 2, "DWARF aranges version");
11779 if (dwarf_split_debug_info)
11780 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11781 debug_skeleton_info_section,
11782 "Offset of Compilation Unit Info");
11783 else
11784 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11785 debug_info_section,
11786 "Offset of Compilation Unit Info");
11787 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11788 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11790 /* We need to align to twice the pointer size here. */
11791 if (DWARF_ARANGES_PAD_SIZE)
11793 /* Pad using a 2 byte words so that padding is correct for any
11794 pointer size. */
11795 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11796 2 * DWARF2_ADDR_SIZE);
11797 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11798 dw2_asm_output_data (2, 0, NULL);
11801 /* It is necessary not to output these entries if the sections were
11802 not used; if the sections were not used, the length will be 0 and
11803 the address may end up as 0 if the section is discarded by ld
11804 --gc-sections, leaving an invalid (0, 0) entry that can be
11805 confused with the terminator. */
11806 if (switch_text_ranges)
11808 const char *prev_loc = text_section_label;
11809 const char *loc;
11810 unsigned idx;
11812 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11813 if (prev_loc)
11815 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11816 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11817 prev_loc = NULL;
11819 else
11820 prev_loc = loc;
11822 if (prev_loc)
11824 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11825 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11826 prev_loc, "Length");
11830 if (switch_cold_ranges)
11832 const char *prev_loc = cold_text_section_label;
11833 const char *loc;
11834 unsigned idx;
11836 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11837 if (prev_loc)
11839 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11840 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11841 prev_loc = NULL;
11843 else
11844 prev_loc = loc;
11846 if (prev_loc)
11848 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11849 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11850 prev_loc, "Length");
11854 if (have_multiple_function_sections)
11856 unsigned fde_idx;
11857 dw_fde_ref fde;
11859 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11861 if (fde->ignored_debug)
11862 continue;
11863 if (!fde->in_std_section)
11865 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11866 "Address");
11867 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11868 fde->dw_fde_begin, "Length");
11870 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11872 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11873 "Address");
11874 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11875 fde->dw_fde_second_begin, "Length");
11880 /* Output the terminator words. */
11881 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11882 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11885 /* Add a new entry to .debug_ranges. Return its index into
11886 ranges_table vector. */
11888 static unsigned int
11889 add_ranges_num (int num, bool maybe_new_sec)
11891 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11892 vec_safe_push (ranges_table, r);
11893 return vec_safe_length (ranges_table) - 1;
11896 /* Add a new entry to .debug_ranges corresponding to a block, or a
11897 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11898 this entry might be in a different section from previous range. */
11900 static unsigned int
11901 add_ranges (const_tree block, bool maybe_new_sec)
11903 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11906 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11907 chain, or middle entry of a chain that will be directly referred to. */
11909 static void
11910 note_rnglist_head (unsigned int offset)
11912 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11913 return;
11914 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11917 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11918 When using dwarf_split_debug_info, address attributes in dies destined
11919 for the final executable should be direct references--setting the
11920 parameter force_direct ensures this behavior. */
11922 static void
11923 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11924 bool *added, bool force_direct)
11926 unsigned int in_use = vec_safe_length (ranges_by_label);
11927 unsigned int offset;
11928 dw_ranges_by_label rbl = { begin, end };
11929 vec_safe_push (ranges_by_label, rbl);
11930 offset = add_ranges_num (-(int)in_use - 1, true);
11931 if (!*added)
11933 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11934 *added = true;
11935 note_rnglist_head (offset);
11936 if (dwarf_split_debug_info && force_direct)
11937 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11941 /* Emit .debug_ranges section. */
11943 static void
11944 output_ranges (void)
11946 unsigned i;
11947 static const char *const start_fmt = "Offset %#x";
11948 const char *fmt = start_fmt;
11949 dw_ranges *r;
11951 switch_to_section (debug_ranges_section);
11952 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11953 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11955 int block_num = r->num;
11957 if (block_num > 0)
11959 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11960 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11962 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11963 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11965 /* If all code is in the text section, then the compilation
11966 unit base address defaults to DW_AT_low_pc, which is the
11967 base of the text section. */
11968 if (!have_multiple_function_sections)
11970 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11971 text_section_label,
11972 fmt, i * 2 * DWARF2_ADDR_SIZE);
11973 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11974 text_section_label, NULL);
11977 /* Otherwise, the compilation unit base address is zero,
11978 which allows us to use absolute addresses, and not worry
11979 about whether the target supports cross-section
11980 arithmetic. */
11981 else
11983 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11984 fmt, i * 2 * DWARF2_ADDR_SIZE);
11985 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11988 fmt = NULL;
11991 /* Negative block_num stands for an index into ranges_by_label. */
11992 else if (block_num < 0)
11994 int lab_idx = - block_num - 1;
11996 if (!have_multiple_function_sections)
11998 gcc_unreachable ();
11999 #if 0
12000 /* If we ever use add_ranges_by_labels () for a single
12001 function section, all we have to do is to take out
12002 the #if 0 above. */
12003 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12004 (*ranges_by_label)[lab_idx].begin,
12005 text_section_label,
12006 fmt, i * 2 * DWARF2_ADDR_SIZE);
12007 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12008 (*ranges_by_label)[lab_idx].end,
12009 text_section_label, NULL);
12010 #endif
12012 else
12014 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12015 (*ranges_by_label)[lab_idx].begin,
12016 fmt, i * 2 * DWARF2_ADDR_SIZE);
12017 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12018 (*ranges_by_label)[lab_idx].end,
12019 NULL);
12022 else
12024 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12025 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12026 fmt = start_fmt;
12031 /* Non-zero if .debug_line_str should be used for .debug_line section
12032 strings or strings that are likely shareable with those. */
12033 #define DWARF5_USE_DEBUG_LINE_STR \
12034 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
12035 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
12036 /* FIXME: there is no .debug_line_str.dwo section, \
12037 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
12038 && !dwarf_split_debug_info)
12041 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12042 DWARF5 .debug_line tables using .debug_line_str or we generate
12043 it ourselves, except for split-dwarf which doesn't have a
12044 .debug_line_str. */
12045 static bool
12046 asm_outputs_debug_line_str (void)
12048 if (dwarf_version >= 5
12049 && ! output_asm_line_debug_info ()
12050 && DWARF5_USE_DEBUG_LINE_STR)
12051 return true;
12052 else
12054 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12055 return !dwarf_split_debug_info && dwarf_version >= 5;
12056 #else
12057 return false;
12058 #endif
12062 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12063 I is index of the following range. */
12065 static bool
12066 use_distinct_base_address_for_range (unsigned int i)
12068 if (i >= vec_safe_length (ranges_table))
12069 return false;
12071 dw_ranges *r2 = &(*ranges_table)[i];
12072 /* Use DW_RLE_base_address{,x} if there is a next range in the
12073 range list and is guaranteed to be in the same section. */
12074 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12077 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12078 section when needed. */
12080 static void
12081 index_rnglists (void)
12083 unsigned i;
12084 dw_ranges *r;
12085 bool base = false;
12087 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12089 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12090 r->idx = rnglist_idx++;
12092 int block_num = r->num;
12093 if ((HAVE_AS_LEB128 || block_num < 0)
12094 && !have_multiple_function_sections)
12095 continue;
12096 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12097 base = false;
12098 if (block_num > 0)
12100 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12101 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12103 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12104 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12106 if (HAVE_AS_LEB128)
12108 if (!base && use_distinct_base_address_for_range (i + 1))
12110 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12111 ate_kind_label);
12112 base = true;
12114 if (base)
12115 /* If we have a base, no need for further
12116 begin_entry/end_entry, as DW_RLE_offset_pair will be
12117 used. */
12118 continue;
12119 r->begin_entry
12120 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12121 /* No need for end_entry, DW_RLE_start{,x}_length will use
12122 length as opposed to a pair of addresses. */
12124 else
12126 r->begin_entry
12127 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12128 r->end_entry
12129 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12133 /* Negative block_num stands for an index into ranges_by_label. */
12134 else if (block_num < 0)
12136 int lab_idx = - block_num - 1;
12137 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12138 const char *elabel = (*ranges_by_label)[lab_idx].end;
12140 r->begin_entry
12141 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12142 if (!HAVE_AS_LEB128)
12143 r->end_entry
12144 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12149 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12151 static bool
12152 output_rnglists (unsigned generation, bool dwo)
12154 unsigned i;
12155 dw_ranges *r;
12156 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12157 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12158 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12160 if (dwo)
12161 switch_to_section (debug_ranges_dwo_section);
12162 else
12164 switch_to_section (debug_ranges_section);
12165 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12167 /* There are up to 4 unique ranges labels per generation.
12168 See also init_sections_and_labels. */
12169 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12170 2 + 2 * dwo + generation * 6);
12171 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12172 3 + 2 * dwo + generation * 6);
12173 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12174 dw2_asm_output_data (4, 0xffffffff,
12175 "Initial length escape value indicating "
12176 "64-bit DWARF extension");
12177 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12178 "Length of Range Lists");
12179 ASM_OUTPUT_LABEL (asm_out_file, l1);
12180 output_dwarf_version ();
12181 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12182 dw2_asm_output_data (1, 0, "Segment Size");
12183 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12184 about relocation sizes and primarily care about the size of .debug*
12185 sections in linked shared libraries and executables, then
12186 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12187 into it are usually larger than just DW_FORM_sec_offset offsets
12188 into the .debug_rnglists section. */
12189 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12190 "Offset Entry Count");
12191 if (dwo)
12193 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12194 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12195 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12196 dw2_asm_output_delta (dwarf_offset_size, r->label,
12197 ranges_base_label, NULL);
12200 const char *lab = "";
12201 const char *base = NULL;
12202 bool skipping = false;
12203 bool ret = false;
12204 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12206 int block_num = r->num;
12208 if (r->label)
12210 if (dwarf_split_debug_info
12211 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12213 ret = true;
12214 skipping = true;
12215 continue;
12217 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12218 lab = r->label;
12220 if (skipping)
12222 if (block_num == 0)
12223 skipping = false;
12224 continue;
12226 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12227 base = NULL;
12228 if (block_num > 0)
12230 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12231 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12233 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12234 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12236 if (HAVE_AS_LEB128)
12238 /* If all code is in the text section, then the compilation
12239 unit base address defaults to DW_AT_low_pc, which is the
12240 base of the text section. */
12241 if (!have_multiple_function_sections)
12243 dw2_asm_output_data (1, DW_RLE_offset_pair,
12244 "DW_RLE_offset_pair (%s)", lab);
12245 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12246 "Range begin address (%s)", lab);
12247 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12248 "Range end address (%s)", lab);
12249 continue;
12251 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12253 if (dwarf_split_debug_info)
12255 dw2_asm_output_data (1, DW_RLE_base_addressx,
12256 "DW_RLE_base_addressx (%s)", lab);
12257 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12258 "Base address index (%s)",
12259 blabel);
12261 else
12263 dw2_asm_output_data (1, DW_RLE_base_address,
12264 "DW_RLE_base_address (%s)", lab);
12265 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12266 "Base address (%s)", lab);
12268 strcpy (basebuf, blabel);
12269 base = basebuf;
12271 if (base)
12273 dw2_asm_output_data (1, DW_RLE_offset_pair,
12274 "DW_RLE_offset_pair (%s)", lab);
12275 dw2_asm_output_delta_uleb128 (blabel, base,
12276 "Range begin address (%s)", lab);
12277 dw2_asm_output_delta_uleb128 (elabel, base,
12278 "Range end address (%s)", lab);
12279 continue;
12281 if (dwarf_split_debug_info)
12283 dw2_asm_output_data (1, DW_RLE_startx_length,
12284 "DW_RLE_startx_length (%s)", lab);
12285 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12286 "Range begin address index "
12287 "(%s)", blabel);
12289 else
12291 dw2_asm_output_data (1, DW_RLE_start_length,
12292 "DW_RLE_start_length (%s)", lab);
12293 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12294 "Range begin address (%s)", lab);
12296 dw2_asm_output_delta_uleb128 (elabel, blabel,
12297 "Range length (%s)", lab);
12299 else if (dwarf_split_debug_info)
12301 dw2_asm_output_data (1, DW_RLE_startx_endx,
12302 "DW_RLE_startx_endx (%s)", lab);
12303 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12304 "Range begin address index "
12305 "(%s)", blabel);
12306 dw2_asm_output_data_uleb128 (r->end_entry->index,
12307 "Range end address index "
12308 "(%s)", elabel);
12310 else
12312 dw2_asm_output_data (1, DW_RLE_start_end,
12313 "DW_RLE_start_end (%s)", lab);
12314 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12315 "Range begin address (%s)", lab);
12316 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12317 "Range end address (%s)", lab);
12321 /* Negative block_num stands for an index into ranges_by_label. */
12322 else if (block_num < 0)
12324 int lab_idx = - block_num - 1;
12325 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12326 const char *elabel = (*ranges_by_label)[lab_idx].end;
12328 if (!have_multiple_function_sections)
12329 gcc_unreachable ();
12330 if (HAVE_AS_LEB128)
12332 if (dwarf_split_debug_info)
12334 dw2_asm_output_data (1, DW_RLE_startx_length,
12335 "DW_RLE_startx_length (%s)", lab);
12336 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12337 "Range begin address index "
12338 "(%s)", blabel);
12340 else
12342 dw2_asm_output_data (1, DW_RLE_start_length,
12343 "DW_RLE_start_length (%s)", lab);
12344 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12345 "Range begin address (%s)", lab);
12347 dw2_asm_output_delta_uleb128 (elabel, blabel,
12348 "Range length (%s)", lab);
12350 else if (dwarf_split_debug_info)
12352 dw2_asm_output_data (1, DW_RLE_startx_endx,
12353 "DW_RLE_startx_endx (%s)", lab);
12354 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12355 "Range begin address index "
12356 "(%s)", blabel);
12357 dw2_asm_output_data_uleb128 (r->end_entry->index,
12358 "Range end address index "
12359 "(%s)", elabel);
12361 else
12363 dw2_asm_output_data (1, DW_RLE_start_end,
12364 "DW_RLE_start_end (%s)", lab);
12365 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12366 "Range begin address (%s)", lab);
12367 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12368 "Range end address (%s)", lab);
12371 else
12372 dw2_asm_output_data (1, DW_RLE_end_of_list,
12373 "DW_RLE_end_of_list (%s)", lab);
12375 ASM_OUTPUT_LABEL (asm_out_file, l2);
12376 return ret;
12379 /* Data structure containing information about input files. */
12380 struct file_info
12382 const char *path; /* Complete file name. */
12383 const char *fname; /* File name part. */
12384 int length; /* Length of entire string. */
12385 struct dwarf_file_data * file_idx; /* Index in input file table. */
12386 int dir_idx; /* Index in directory table. */
12389 /* Data structure containing information about directories with source
12390 files. */
12391 struct dir_info
12393 const char *path; /* Path including directory name. */
12394 int length; /* Path length. */
12395 int prefix; /* Index of directory entry which is a prefix. */
12396 int count; /* Number of files in this directory. */
12397 int dir_idx; /* Index of directory used as base. */
12400 /* Callback function for file_info comparison. We sort by looking at
12401 the directories in the path. */
12403 static int
12404 file_info_cmp (const void *p1, const void *p2)
12406 const struct file_info *const s1 = (const struct file_info *) p1;
12407 const struct file_info *const s2 = (const struct file_info *) p2;
12408 const unsigned char *cp1;
12409 const unsigned char *cp2;
12411 /* Take care of file names without directories. We need to make sure that
12412 we return consistent values to qsort since some will get confused if
12413 we return the same value when identical operands are passed in opposite
12414 orders. So if neither has a directory, return 0 and otherwise return
12415 1 or -1 depending on which one has the directory. We want the one with
12416 the directory to sort after the one without, so all no directory files
12417 are at the start (normally only the compilation unit file). */
12418 if ((s1->path == s1->fname || s2->path == s2->fname))
12419 return (s2->path == s2->fname) - (s1->path == s1->fname);
12421 cp1 = (const unsigned char *) s1->path;
12422 cp2 = (const unsigned char *) s2->path;
12424 while (1)
12426 ++cp1;
12427 ++cp2;
12428 /* Reached the end of the first path? If so, handle like above,
12429 but now we want longer directory prefixes before shorter ones. */
12430 if ((cp1 == (const unsigned char *) s1->fname)
12431 || (cp2 == (const unsigned char *) s2->fname))
12432 return ((cp1 == (const unsigned char *) s1->fname)
12433 - (cp2 == (const unsigned char *) s2->fname));
12435 /* Character of current path component the same? */
12436 else if (*cp1 != *cp2)
12437 return *cp1 - *cp2;
12441 struct file_name_acquire_data
12443 struct file_info *files;
12444 int used_files;
12445 int max_files;
12448 /* Traversal function for the hash table. */
12451 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12453 struct dwarf_file_data *d = *slot;
12454 struct file_info *fi;
12455 const char *f;
12457 gcc_assert (fnad->max_files >= d->emitted_number);
12459 if (! d->emitted_number)
12460 return 1;
12462 gcc_assert (fnad->max_files != fnad->used_files);
12464 fi = fnad->files + fnad->used_files++;
12466 f = d->filename;
12468 /* Skip all leading "./". */
12469 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12470 f += 2;
12472 /* Create a new array entry. */
12473 fi->path = f;
12474 fi->length = strlen (f);
12475 fi->file_idx = d;
12477 /* Search for the file name part. */
12478 f = strrchr (f, DIR_SEPARATOR);
12479 #if defined (DIR_SEPARATOR_2)
12481 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12483 if (g != NULL)
12485 if (f == NULL || f < g)
12486 f = g;
12489 #endif
12491 fi->fname = f == NULL ? fi->path : f + 1;
12492 return 1;
12495 /* Helper function for output_file_names. Emit a FORM encoded
12496 string STR, with assembly comment start ENTRY_KIND and
12497 index IDX */
12499 static void
12500 output_line_string (enum dwarf_form form, const char *str,
12501 const char *entry_kind, unsigned int idx)
12503 switch (form)
12505 case DW_FORM_string:
12506 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12507 break;
12508 case DW_FORM_line_strp:
12509 if (!debug_line_str_hash)
12510 debug_line_str_hash
12511 = hash_table<indirect_string_hasher>::create_ggc (10);
12513 struct indirect_string_node *node;
12514 node = find_AT_string_in_table (str, debug_line_str_hash);
12515 set_indirect_string (node);
12516 node->form = form;
12517 dw2_asm_output_offset (dwarf_offset_size, node->label,
12518 debug_line_str_section, "%s: %#x: \"%s\"",
12519 entry_kind, 0, node->str);
12520 break;
12521 default:
12522 gcc_unreachable ();
12526 /* Output the directory table and the file name table. We try to minimize
12527 the total amount of memory needed. A heuristic is used to avoid large
12528 slowdowns with many input files. */
12530 static void
12531 output_file_names (void)
12533 struct file_name_acquire_data fnad;
12534 int numfiles;
12535 struct file_info *files;
12536 struct dir_info *dirs;
12537 int *saved;
12538 int *savehere;
12539 int *backmap;
12540 int ndirs;
12541 int idx_offset;
12542 int i;
12544 if (!last_emitted_file)
12546 if (dwarf_version >= 5)
12548 const char *comp_dir = comp_dir_string ();
12549 if (comp_dir == NULL)
12550 comp_dir = "";
12551 dw2_asm_output_data (1, 1, "Directory entry format count");
12552 enum dwarf_form str_form = DW_FORM_string;
12553 if (DWARF5_USE_DEBUG_LINE_STR)
12554 str_form = DW_FORM_line_strp;
12555 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12556 dw2_asm_output_data_uleb128 (str_form, "%s",
12557 get_DW_FORM_name (str_form));
12558 dw2_asm_output_data_uleb128 (1, "Directories count");
12559 if (str_form == DW_FORM_string)
12560 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12561 else
12562 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12563 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12564 if (filename0 == NULL)
12565 filename0 = "";
12566 #ifdef VMS_DEBUGGING_INFO
12567 dw2_asm_output_data (1, 4, "File name entry format count");
12568 #else
12569 dw2_asm_output_data (1, 2, "File name entry format count");
12570 #endif
12571 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12572 dw2_asm_output_data_uleb128 (str_form, "%s",
12573 get_DW_FORM_name (str_form));
12574 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12575 "DW_LNCT_directory_index");
12576 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12577 get_DW_FORM_name (DW_FORM_data1));
12578 #ifdef VMS_DEBUGGING_INFO
12579 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12580 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12581 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12582 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12583 #endif
12584 dw2_asm_output_data_uleb128 (1, "File names count");
12586 output_line_string (str_form, filename0, "File Entry", 0);
12587 dw2_asm_output_data (1, 0, NULL);
12588 #ifdef VMS_DEBUGGING_INFO
12589 dw2_asm_output_data_uleb128 (0, NULL);
12590 dw2_asm_output_data_uleb128 (0, NULL);
12591 #endif
12593 else
12595 dw2_asm_output_data (1, 0, "End directory table");
12596 dw2_asm_output_data (1, 0, "End file name table");
12598 return;
12601 numfiles = last_emitted_file->emitted_number;
12603 /* Allocate the various arrays we need. */
12604 files = XALLOCAVEC (struct file_info, numfiles);
12605 dirs = XALLOCAVEC (struct dir_info, numfiles);
12607 fnad.files = files;
12608 fnad.used_files = 0;
12609 fnad.max_files = numfiles;
12610 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12611 gcc_assert (fnad.used_files == fnad.max_files);
12613 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12615 /* Find all the different directories used. */
12616 dirs[0].path = files[0].path;
12617 dirs[0].length = files[0].fname - files[0].path;
12618 dirs[0].prefix = -1;
12619 dirs[0].count = 1;
12620 dirs[0].dir_idx = 0;
12621 files[0].dir_idx = 0;
12622 ndirs = 1;
12624 for (i = 1; i < numfiles; i++)
12625 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12626 && memcmp (dirs[ndirs - 1].path, files[i].path,
12627 dirs[ndirs - 1].length) == 0)
12629 /* Same directory as last entry. */
12630 files[i].dir_idx = ndirs - 1;
12631 ++dirs[ndirs - 1].count;
12633 else
12635 int j;
12637 /* This is a new directory. */
12638 dirs[ndirs].path = files[i].path;
12639 dirs[ndirs].length = files[i].fname - files[i].path;
12640 dirs[ndirs].count = 1;
12641 dirs[ndirs].dir_idx = ndirs;
12642 files[i].dir_idx = ndirs;
12644 /* Search for a prefix. */
12645 dirs[ndirs].prefix = -1;
12646 for (j = 0; j < ndirs; j++)
12647 if (dirs[j].length < dirs[ndirs].length
12648 && dirs[j].length > 1
12649 && (dirs[ndirs].prefix == -1
12650 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12651 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12652 dirs[ndirs].prefix = j;
12654 ++ndirs;
12657 /* Now to the actual work. We have to find a subset of the directories which
12658 allow expressing the file name using references to the directory table
12659 with the least amount of characters. We do not do an exhaustive search
12660 where we would have to check out every combination of every single
12661 possible prefix. Instead we use a heuristic which provides nearly optimal
12662 results in most cases and never is much off. */
12663 saved = XALLOCAVEC (int, ndirs);
12664 savehere = XALLOCAVEC (int, ndirs);
12666 memset (saved, '\0', ndirs * sizeof (saved[0]));
12667 for (i = 0; i < ndirs; i++)
12669 int j;
12670 int total;
12672 /* We can always save some space for the current directory. But this
12673 does not mean it will be enough to justify adding the directory. */
12674 savehere[i] = dirs[i].length;
12675 total = (savehere[i] - saved[i]) * dirs[i].count;
12677 for (j = i + 1; j < ndirs; j++)
12679 savehere[j] = 0;
12680 if (saved[j] < dirs[i].length)
12682 /* Determine whether the dirs[i] path is a prefix of the
12683 dirs[j] path. */
12684 int k;
12686 k = dirs[j].prefix;
12687 while (k != -1 && k != (int) i)
12688 k = dirs[k].prefix;
12690 if (k == (int) i)
12692 /* Yes it is. We can possibly save some memory by
12693 writing the filenames in dirs[j] relative to
12694 dirs[i]. */
12695 savehere[j] = dirs[i].length;
12696 total += (savehere[j] - saved[j]) * dirs[j].count;
12701 /* Check whether we can save enough to justify adding the dirs[i]
12702 directory. */
12703 if (total > dirs[i].length + 1)
12705 /* It's worthwhile adding. */
12706 for (j = i; j < ndirs; j++)
12707 if (savehere[j] > 0)
12709 /* Remember how much we saved for this directory so far. */
12710 saved[j] = savehere[j];
12712 /* Remember the prefix directory. */
12713 dirs[j].dir_idx = i;
12718 /* Emit the directory name table. */
12719 idx_offset = dirs[0].length > 0 ? 1 : 0;
12720 enum dwarf_form str_form = DW_FORM_string;
12721 enum dwarf_form idx_form = DW_FORM_udata;
12722 if (dwarf_version >= 5)
12724 const char *comp_dir = comp_dir_string ();
12725 if (comp_dir == NULL)
12726 comp_dir = "";
12727 dw2_asm_output_data (1, 1, "Directory entry format count");
12728 if (DWARF5_USE_DEBUG_LINE_STR)
12729 str_form = DW_FORM_line_strp;
12730 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12731 dw2_asm_output_data_uleb128 (str_form, "%s",
12732 get_DW_FORM_name (str_form));
12733 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12734 if (str_form == DW_FORM_string)
12736 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12737 for (i = 1 - idx_offset; i < ndirs; i++)
12738 dw2_asm_output_nstring (dirs[i].path,
12739 dirs[i].length
12740 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12741 "Directory Entry: %#x", i + idx_offset);
12743 else
12745 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12746 for (i = 1 - idx_offset; i < ndirs; i++)
12748 const char *str
12749 = ggc_alloc_string (dirs[i].path,
12750 dirs[i].length
12751 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12752 output_line_string (str_form, str, "Directory Entry",
12753 (unsigned) i + idx_offset);
12757 else
12759 for (i = 1 - idx_offset; i < ndirs; i++)
12760 dw2_asm_output_nstring (dirs[i].path,
12761 dirs[i].length
12762 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12763 "Directory Entry: %#x", i + idx_offset);
12765 dw2_asm_output_data (1, 0, "End directory table");
12768 /* We have to emit them in the order of emitted_number since that's
12769 used in the debug info generation. To do this efficiently we
12770 generate a back-mapping of the indices first. */
12771 backmap = XALLOCAVEC (int, numfiles);
12772 for (i = 0; i < numfiles; i++)
12773 backmap[files[i].file_idx->emitted_number - 1] = i;
12775 if (dwarf_version >= 5)
12777 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12778 if (filename0 == NULL)
12779 filename0 = "";
12780 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12781 DW_FORM_data2. Choose one based on the number of directories
12782 and how much space would they occupy in each encoding.
12783 If we have at most 256 directories, all indexes fit into
12784 a single byte, so DW_FORM_data1 is most compact (if there
12785 are at most 128 directories, DW_FORM_udata would be as
12786 compact as that, but not shorter and slower to decode). */
12787 if (ndirs + idx_offset <= 256)
12788 idx_form = DW_FORM_data1;
12789 /* If there are more than 65536 directories, we have to use
12790 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12791 Otherwise, compute what space would occupy if all the indexes
12792 used DW_FORM_udata - sum - and compare that to how large would
12793 be DW_FORM_data2 encoding, and pick the more efficient one. */
12794 else if (ndirs + idx_offset <= 65536)
12796 unsigned HOST_WIDE_INT sum = 1;
12797 for (i = 0; i < numfiles; i++)
12799 int file_idx = backmap[i];
12800 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12801 sum += size_of_uleb128 (dir_idx);
12803 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12804 idx_form = DW_FORM_data2;
12806 #ifdef VMS_DEBUGGING_INFO
12807 dw2_asm_output_data (1, 4, "File name entry format count");
12808 #else
12809 dw2_asm_output_data (1, 2, "File name entry format count");
12810 #endif
12811 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12812 dw2_asm_output_data_uleb128 (str_form, "%s",
12813 get_DW_FORM_name (str_form));
12814 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12815 "DW_LNCT_directory_index");
12816 dw2_asm_output_data_uleb128 (idx_form, "%s",
12817 get_DW_FORM_name (idx_form));
12818 #ifdef VMS_DEBUGGING_INFO
12819 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12820 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12821 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12822 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12823 #endif
12824 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12826 output_line_string (str_form, filename0, "File Entry", 0);
12828 /* Include directory index. */
12829 if (idx_form != DW_FORM_udata)
12830 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12831 0, NULL);
12832 else
12833 dw2_asm_output_data_uleb128 (0, NULL);
12835 #ifdef VMS_DEBUGGING_INFO
12836 dw2_asm_output_data_uleb128 (0, NULL);
12837 dw2_asm_output_data_uleb128 (0, NULL);
12838 #endif
12841 /* Now write all the file names. */
12842 for (i = 0; i < numfiles; i++)
12844 int file_idx = backmap[i];
12845 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12847 #ifdef VMS_DEBUGGING_INFO
12848 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12850 /* Setting these fields can lead to debugger miscomparisons,
12851 but VMS Debug requires them to be set correctly. */
12853 int ver;
12854 long long cdt;
12855 long siz;
12856 int maxfilelen = (strlen (files[file_idx].path)
12857 + dirs[dir_idx].length
12858 + MAX_VMS_VERSION_LEN + 1);
12859 char *filebuf = XALLOCAVEC (char, maxfilelen);
12861 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12862 snprintf (filebuf, maxfilelen, "%s;%d",
12863 files[file_idx].path + dirs[dir_idx].length, ver);
12865 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12867 /* Include directory index. */
12868 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12869 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12870 dir_idx + idx_offset, NULL);
12871 else
12872 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12874 /* Modification time. */
12875 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12876 &cdt, 0, 0, 0) == 0)
12877 ? cdt : 0, NULL);
12879 /* File length in bytes. */
12880 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12881 0, &siz, 0, 0) == 0)
12882 ? siz : 0, NULL);
12883 #else
12884 output_line_string (str_form,
12885 files[file_idx].path + dirs[dir_idx].length,
12886 "File Entry", (unsigned) i + 1);
12888 /* Include directory index. */
12889 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12890 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12891 dir_idx + idx_offset, NULL);
12892 else
12893 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12895 if (dwarf_version >= 5)
12896 continue;
12898 /* Modification time. */
12899 dw2_asm_output_data_uleb128 (0, NULL);
12901 /* File length in bytes. */
12902 dw2_asm_output_data_uleb128 (0, NULL);
12903 #endif /* VMS_DEBUGGING_INFO */
12906 if (dwarf_version < 5)
12907 dw2_asm_output_data (1, 0, "End file name table");
12911 /* Output one line number table into the .debug_line section. */
12913 static void
12914 output_one_line_info_table (dw_line_info_table *table)
12916 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12917 unsigned int current_line = 1;
12918 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12919 dw_line_info_entry *ent, *prev_addr = NULL;
12920 size_t i;
12921 unsigned int view;
12923 view = 0;
12925 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12927 switch (ent->opcode)
12929 case LI_set_address:
12930 /* ??? Unfortunately, we have little choice here currently, and
12931 must always use the most general form. GCC does not know the
12932 address delta itself, so we can't use DW_LNS_advance_pc. Many
12933 ports do have length attributes which will give an upper bound
12934 on the address range. We could perhaps use length attributes
12935 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12936 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12938 view = 0;
12940 /* This can handle any delta. This takes
12941 4+DWARF2_ADDR_SIZE bytes. */
12942 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12943 debug_variable_location_views
12944 ? ", reset view to 0" : "");
12945 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12946 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12947 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12949 prev_addr = ent;
12950 break;
12952 case LI_adv_address:
12954 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12955 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12956 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12958 view++;
12960 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12961 dw2_asm_output_delta (2, line_label, prev_label,
12962 "from %s to %s", prev_label, line_label);
12964 prev_addr = ent;
12965 break;
12968 case LI_set_line:
12969 if (ent->val == current_line)
12971 /* We still need to start a new row, so output a copy insn. */
12972 dw2_asm_output_data (1, DW_LNS_copy,
12973 "copy line %u", current_line);
12975 else
12977 int line_offset = ent->val - current_line;
12978 int line_delta = line_offset - DWARF_LINE_BASE;
12980 current_line = ent->val;
12981 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12983 /* This can handle deltas from -10 to 234, using the current
12984 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12985 This takes 1 byte. */
12986 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12987 "line %u", current_line);
12989 else
12991 /* This can handle any delta. This takes at least 4 bytes,
12992 depending on the value being encoded. */
12993 dw2_asm_output_data (1, DW_LNS_advance_line,
12994 "advance to line %u", current_line);
12995 dw2_asm_output_data_sleb128 (line_offset, NULL);
12996 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12999 break;
13001 case LI_set_file:
13002 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
13003 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13004 break;
13006 case LI_set_column:
13007 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
13008 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
13009 break;
13011 case LI_negate_stmt:
13012 current_is_stmt = !current_is_stmt;
13013 dw2_asm_output_data (1, DW_LNS_negate_stmt,
13014 "is_stmt %d", current_is_stmt);
13015 break;
13017 case LI_set_prologue_end:
13018 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
13019 "set prologue end");
13020 break;
13022 case LI_set_epilogue_begin:
13023 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
13024 "set epilogue begin");
13025 break;
13027 case LI_set_discriminator:
13028 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
13029 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
13030 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
13031 dw2_asm_output_data_uleb128 (ent->val, NULL);
13032 break;
13036 /* Emit debug info for the address of the end of the table. */
13037 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
13038 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
13039 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13040 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13042 dw2_asm_output_data (1, 0, "end sequence");
13043 dw2_asm_output_data_uleb128 (1, NULL);
13044 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13047 static unsigned int output_line_info_generation;
13049 /* Output the source line number correspondence information. This
13050 information goes into the .debug_line section. */
13052 static void
13053 output_line_info (bool prologue_only)
13055 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13056 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13057 bool saw_one = false;
13058 int opc;
13060 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13061 output_line_info_generation);
13062 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13063 output_line_info_generation);
13064 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13065 output_line_info_generation);
13066 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13067 output_line_info_generation++);
13069 if (!XCOFF_DEBUGGING_INFO)
13071 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13072 dw2_asm_output_data (4, 0xffffffff,
13073 "Initial length escape value indicating 64-bit DWARF extension");
13074 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13075 "Length of Source Line Info");
13078 ASM_OUTPUT_LABEL (asm_out_file, l1);
13080 output_dwarf_version ();
13081 if (dwarf_version >= 5)
13083 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13084 dw2_asm_output_data (1, 0, "Segment Size");
13086 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13087 ASM_OUTPUT_LABEL (asm_out_file, p1);
13089 /* Define the architecture-dependent minimum instruction length (in bytes).
13090 In this implementation of DWARF, this field is used for information
13091 purposes only. Since GCC generates assembly language, we have no
13092 a priori knowledge of how many instruction bytes are generated for each
13093 source line, and therefore can use only the DW_LNE_set_address and
13094 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13095 this as '1', which is "correct enough" for all architectures,
13096 and don't let the target override. */
13097 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13099 if (dwarf_version >= 4)
13100 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13101 "Maximum Operations Per Instruction");
13102 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13103 "Default is_stmt_start flag");
13104 dw2_asm_output_data (1, DWARF_LINE_BASE,
13105 "Line Base Value (Special Opcodes)");
13106 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13107 "Line Range Value (Special Opcodes)");
13108 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13109 "Special Opcode Base");
13111 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13113 int n_op_args;
13114 switch (opc)
13116 case DW_LNS_advance_pc:
13117 case DW_LNS_advance_line:
13118 case DW_LNS_set_file:
13119 case DW_LNS_set_column:
13120 case DW_LNS_fixed_advance_pc:
13121 case DW_LNS_set_isa:
13122 n_op_args = 1;
13123 break;
13124 default:
13125 n_op_args = 0;
13126 break;
13129 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13130 opc, n_op_args);
13133 /* Write out the information about the files we use. */
13134 output_file_names ();
13135 ASM_OUTPUT_LABEL (asm_out_file, p2);
13136 if (prologue_only)
13138 /* Output the marker for the end of the line number info. */
13139 ASM_OUTPUT_LABEL (asm_out_file, l2);
13140 return;
13143 if (separate_line_info)
13145 dw_line_info_table *table;
13146 size_t i;
13148 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13149 if (table->in_use)
13151 output_one_line_info_table (table);
13152 saw_one = true;
13155 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13157 output_one_line_info_table (cold_text_section_line_info);
13158 saw_one = true;
13161 /* ??? Some Darwin linkers crash on a .debug_line section with no
13162 sequences. Further, merely a DW_LNE_end_sequence entry is not
13163 sufficient -- the address column must also be initialized.
13164 Make sure to output at least one set_address/end_sequence pair,
13165 choosing .text since that section is always present. */
13166 if (text_section_line_info->in_use || !saw_one)
13167 output_one_line_info_table (text_section_line_info);
13169 /* Output the marker for the end of the line number info. */
13170 ASM_OUTPUT_LABEL (asm_out_file, l2);
13173 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13175 static inline bool
13176 need_endianity_attribute_p (bool reverse)
13178 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13181 /* Given a pointer to a tree node for some base type, return a pointer to
13182 a DIE that describes the given type. REVERSE is true if the type is
13183 to be interpreted in the reverse storage order wrt the target order.
13185 This routine must only be called for GCC type nodes that correspond to
13186 Dwarf base (fundamental) types. */
13188 dw_die_ref
13189 base_type_die (tree type, bool reverse)
13191 dw_die_ref base_type_result;
13192 enum dwarf_type encoding;
13193 bool fpt_used = false;
13194 struct fixed_point_type_info fpt_info;
13195 tree type_bias = NULL_TREE;
13197 /* If this is a subtype that should not be emitted as a subrange type,
13198 use the base type. See subrange_type_for_debug_p. */
13199 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13200 type = TREE_TYPE (type);
13202 switch (TREE_CODE (type))
13204 case INTEGER_TYPE:
13205 if ((dwarf_version >= 4 || !dwarf_strict)
13206 && TYPE_NAME (type)
13207 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13208 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13209 && DECL_NAME (TYPE_NAME (type)))
13211 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13212 if (strcmp (name, "char16_t") == 0
13213 || strcmp (name, "char8_t") == 0
13214 || strcmp (name, "char32_t") == 0)
13216 encoding = DW_ATE_UTF;
13217 break;
13220 if ((dwarf_version >= 3 || !dwarf_strict)
13221 && lang_hooks.types.get_fixed_point_type_info)
13223 memset (&fpt_info, 0, sizeof (fpt_info));
13224 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13226 fpt_used = true;
13227 encoding = ((TYPE_UNSIGNED (type))
13228 ? DW_ATE_unsigned_fixed
13229 : DW_ATE_signed_fixed);
13230 break;
13233 if (TYPE_STRING_FLAG (type))
13235 if (TYPE_UNSIGNED (type))
13236 encoding = DW_ATE_unsigned_char;
13237 else
13238 encoding = DW_ATE_signed_char;
13240 else if (TYPE_UNSIGNED (type))
13241 encoding = DW_ATE_unsigned;
13242 else
13243 encoding = DW_ATE_signed;
13245 if (!dwarf_strict
13246 && lang_hooks.types.get_type_bias)
13247 type_bias = lang_hooks.types.get_type_bias (type);
13248 break;
13250 case REAL_TYPE:
13251 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13253 if (dwarf_version >= 3 || !dwarf_strict)
13254 encoding = DW_ATE_decimal_float;
13255 else
13256 encoding = DW_ATE_lo_user;
13258 else
13259 encoding = DW_ATE_float;
13260 break;
13262 case FIXED_POINT_TYPE:
13263 if (!(dwarf_version >= 3 || !dwarf_strict))
13264 encoding = DW_ATE_lo_user;
13265 else if (TYPE_UNSIGNED (type))
13266 encoding = DW_ATE_unsigned_fixed;
13267 else
13268 encoding = DW_ATE_signed_fixed;
13269 break;
13271 /* Dwarf2 doesn't know anything about complex ints, so use
13272 a user defined type for it. */
13273 case COMPLEX_TYPE:
13274 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13275 encoding = DW_ATE_complex_float;
13276 else
13277 encoding = DW_ATE_lo_user;
13278 break;
13280 case BOOLEAN_TYPE:
13281 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13282 encoding = DW_ATE_boolean;
13283 break;
13285 default:
13286 /* No other TREE_CODEs are Dwarf fundamental types. */
13287 gcc_unreachable ();
13290 base_type_result = new_die_raw (DW_TAG_base_type);
13292 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13293 int_size_in_bytes (type));
13294 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13296 if (need_endianity_attribute_p (reverse))
13297 add_AT_unsigned (base_type_result, DW_AT_endianity,
13298 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13300 add_alignment_attribute (base_type_result, type);
13302 if (fpt_used)
13304 switch (fpt_info.scale_factor_kind)
13306 case fixed_point_scale_factor_binary:
13307 add_AT_int (base_type_result, DW_AT_binary_scale,
13308 fpt_info.scale_factor.binary);
13309 break;
13311 case fixed_point_scale_factor_decimal:
13312 add_AT_int (base_type_result, DW_AT_decimal_scale,
13313 fpt_info.scale_factor.decimal);
13314 break;
13316 case fixed_point_scale_factor_arbitrary:
13317 /* Arbitrary scale factors cannot be described in standard DWARF. */
13318 if (!dwarf_strict)
13320 /* Describe the scale factor as a rational constant. */
13321 const dw_die_ref scale_factor
13322 = new_die (DW_TAG_constant, comp_unit_die (), type);
13324 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13325 fpt_info.scale_factor.arbitrary.numerator,
13326 dw_scalar_form_constant, NULL);
13327 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13328 fpt_info.scale_factor.arbitrary.denominator,
13329 dw_scalar_form_constant, NULL);
13331 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13333 break;
13335 default:
13336 gcc_unreachable ();
13340 if (type_bias)
13341 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13342 dw_scalar_form_constant
13343 | dw_scalar_form_exprloc
13344 | dw_scalar_form_reference,
13345 NULL);
13347 return base_type_result;
13350 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13351 named 'auto' in its type: return true for it, false otherwise. */
13353 static inline bool
13354 is_cxx_auto (tree type)
13356 if (is_cxx ())
13358 tree name = TYPE_IDENTIFIER (type);
13359 if (name == get_identifier ("auto")
13360 || name == get_identifier ("decltype(auto)"))
13361 return true;
13363 return false;
13366 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13367 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13369 static inline int
13370 is_base_type (tree type)
13372 switch (TREE_CODE (type))
13374 case INTEGER_TYPE:
13375 case REAL_TYPE:
13376 case FIXED_POINT_TYPE:
13377 case COMPLEX_TYPE:
13378 case BOOLEAN_TYPE:
13379 return 1;
13381 case VOID_TYPE:
13382 case OPAQUE_TYPE:
13383 case ARRAY_TYPE:
13384 case RECORD_TYPE:
13385 case UNION_TYPE:
13386 case QUAL_UNION_TYPE:
13387 case ENUMERAL_TYPE:
13388 case FUNCTION_TYPE:
13389 case METHOD_TYPE:
13390 case POINTER_TYPE:
13391 case REFERENCE_TYPE:
13392 case NULLPTR_TYPE:
13393 case OFFSET_TYPE:
13394 case LANG_TYPE:
13395 case VECTOR_TYPE:
13396 return 0;
13398 default:
13399 if (is_cxx_auto (type))
13400 return 0;
13401 gcc_unreachable ();
13405 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13406 node, return the size in bits for the type if it is a constant, or else
13407 return the alignment for the type if the type's size is not constant, or
13408 else return BITS_PER_WORD if the type actually turns out to be an
13409 ERROR_MARK node. */
13411 static inline unsigned HOST_WIDE_INT
13412 simple_type_size_in_bits (const_tree type)
13414 if (TREE_CODE (type) == ERROR_MARK)
13415 return BITS_PER_WORD;
13416 else if (TYPE_SIZE (type) == NULL_TREE)
13417 return 0;
13418 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13419 return tree_to_uhwi (TYPE_SIZE (type));
13420 else
13421 return TYPE_ALIGN (type);
13424 /* Similarly, but return an offset_int instead of UHWI. */
13426 static inline offset_int
13427 offset_int_type_size_in_bits (const_tree type)
13429 if (TREE_CODE (type) == ERROR_MARK)
13430 return BITS_PER_WORD;
13431 else if (TYPE_SIZE (type) == NULL_TREE)
13432 return 0;
13433 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13434 return wi::to_offset (TYPE_SIZE (type));
13435 else
13436 return TYPE_ALIGN (type);
13439 /* Given a pointer to a tree node for a subrange type, return a pointer
13440 to a DIE that describes the given type. */
13442 static dw_die_ref
13443 subrange_type_die (tree type, tree low, tree high, tree bias,
13444 dw_die_ref context_die)
13446 dw_die_ref subrange_die;
13447 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13449 if (context_die == NULL)
13450 context_die = comp_unit_die ();
13452 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13454 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13456 /* The size of the subrange type and its base type do not match,
13457 so we need to generate a size attribute for the subrange type. */
13458 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13461 add_alignment_attribute (subrange_die, type);
13463 if (low)
13464 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13465 if (high)
13466 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13467 if (bias && !dwarf_strict)
13468 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13469 dw_scalar_form_constant
13470 | dw_scalar_form_exprloc
13471 | dw_scalar_form_reference,
13472 NULL);
13474 return subrange_die;
13477 /* Returns the (const and/or volatile) cv_qualifiers associated with
13478 the decl node. This will normally be augmented with the
13479 cv_qualifiers of the underlying type in add_type_attribute. */
13481 static int
13482 decl_quals (const_tree decl)
13484 return ((TREE_READONLY (decl)
13485 /* The C++ front-end correctly marks reference-typed
13486 variables as readonly, but from a language (and debug
13487 info) standpoint they are not const-qualified. */
13488 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13489 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13490 | (TREE_THIS_VOLATILE (decl)
13491 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13494 /* Determine the TYPE whose qualifiers match the largest strict subset
13495 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13496 qualifiers outside QUAL_MASK. */
13498 static int
13499 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13501 tree t;
13502 int best_rank = 0, best_qual = 0, max_rank;
13504 type_quals &= qual_mask;
13505 max_rank = popcount_hwi (type_quals) - 1;
13507 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13508 t = TYPE_NEXT_VARIANT (t))
13510 int q = TYPE_QUALS (t) & qual_mask;
13512 if ((q & type_quals) == q && q != type_quals
13513 && check_base_type (t, type))
13515 int rank = popcount_hwi (q);
13517 if (rank > best_rank)
13519 best_rank = rank;
13520 best_qual = q;
13525 return best_qual;
13528 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13529 static const dwarf_qual_info_t dwarf_qual_info[] =
13531 { TYPE_QUAL_CONST, DW_TAG_const_type },
13532 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13533 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13534 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13536 static const unsigned int dwarf_qual_info_size = ARRAY_SIZE (dwarf_qual_info);
13538 /* If DIE is a qualified DIE of some base DIE with the same parent,
13539 return the base DIE, otherwise return NULL. Set MASK to the
13540 qualifiers added compared to the returned DIE. */
13542 static dw_die_ref
13543 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13545 unsigned int i;
13546 for (i = 0; i < dwarf_qual_info_size; i++)
13547 if (die->die_tag == dwarf_qual_info[i].t)
13548 break;
13549 if (i == dwarf_qual_info_size)
13550 return NULL;
13551 if (vec_safe_length (die->die_attr) != 1)
13552 return NULL;
13553 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13554 if (type == NULL || type->die_parent != die->die_parent)
13555 return NULL;
13556 *mask |= dwarf_qual_info[i].q;
13557 if (depth)
13559 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13560 if (ret)
13561 return ret;
13563 return type;
13566 /* If TYPE is long double or complex long double that
13567 should be emitted as artificial typedef to _Float128 or
13568 complex _Float128, return the type it should be emitted as.
13569 This is done in case the target already supports 16-byte
13570 composite floating point type (ibm_extended_format). */
13572 static tree
13573 long_double_as_float128 (tree type)
13575 if (type != long_double_type_node
13576 && type != complex_long_double_type_node)
13577 return NULL_TREE;
13579 machine_mode mode, fmode;
13580 if (TREE_CODE (type) == COMPLEX_TYPE)
13581 mode = TYPE_MODE (TREE_TYPE (type));
13582 else
13583 mode = TYPE_MODE (type);
13584 if (known_eq (GET_MODE_SIZE (mode), 16) && !MODE_COMPOSITE_P (mode))
13585 FOR_EACH_MODE_IN_CLASS (fmode, MODE_FLOAT)
13586 if (known_eq (GET_MODE_SIZE (fmode), 16)
13587 && MODE_COMPOSITE_P (fmode))
13589 if (type == long_double_type_node)
13591 if (float128_type_node
13592 && (TYPE_MODE (float128_type_node)
13593 == TYPE_MODE (type)))
13594 return float128_type_node;
13595 return NULL_TREE;
13597 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
13598 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
13599 && (TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i))
13600 == TYPE_MODE (type)))
13601 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
13604 return NULL_TREE;
13607 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13608 entry that chains the modifiers specified by CV_QUALS in front of the
13609 given type. REVERSE is true if the type is to be interpreted in the
13610 reverse storage order wrt the target order. */
13612 static dw_die_ref
13613 modified_type_die (tree type, int cv_quals, bool reverse,
13614 dw_die_ref context_die)
13616 enum tree_code code = TREE_CODE (type);
13617 dw_die_ref mod_type_die;
13618 dw_die_ref sub_die = NULL;
13619 tree item_type = NULL;
13620 tree qualified_type;
13621 tree name, low, high;
13622 dw_die_ref mod_scope;
13623 struct array_descr_info info;
13624 /* Only these cv-qualifiers are currently handled. */
13625 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13626 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13627 ENCODE_QUAL_ADDR_SPACE(~0U));
13628 const bool reverse_base_type
13629 = need_endianity_attribute_p (reverse) && is_base_type (type);
13631 if (code == ERROR_MARK)
13632 return NULL;
13634 if (lang_hooks.types.get_debug_type)
13636 tree debug_type = lang_hooks.types.get_debug_type (type);
13638 if (debug_type != NULL_TREE && debug_type != type)
13639 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13642 cv_quals &= cv_qual_mask;
13644 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13645 tag modifier (and not an attribute) old consumers won't be able
13646 to handle it. */
13647 if (dwarf_version < 3)
13648 cv_quals &= ~TYPE_QUAL_RESTRICT;
13650 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13651 if (dwarf_version < 5)
13652 cv_quals &= ~TYPE_QUAL_ATOMIC;
13654 /* See if we already have the appropriately qualified variant of
13655 this type. */
13656 qualified_type = get_qualified_type (type, cv_quals);
13658 if (qualified_type == sizetype)
13660 /* Try not to expose the internal sizetype type's name. */
13661 if (TYPE_NAME (qualified_type)
13662 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13664 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13666 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13667 && (TYPE_PRECISION (t)
13668 == TYPE_PRECISION (qualified_type))
13669 && (TYPE_UNSIGNED (t)
13670 == TYPE_UNSIGNED (qualified_type)));
13671 qualified_type = t;
13673 else if (qualified_type == sizetype
13674 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13675 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13676 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13677 qualified_type = size_type_node;
13678 if (type == sizetype)
13679 type = qualified_type;
13682 /* If we do, then we can just use its DIE, if it exists. */
13683 if (qualified_type)
13685 mod_type_die = lookup_type_die (qualified_type);
13687 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13688 dealt with specially: the DIE with the attribute, if it exists, is
13689 placed immediately after the regular DIE for the same base type. */
13690 if (mod_type_die
13691 && (!reverse_base_type
13692 || ((mod_type_die = mod_type_die->die_sib) != NULL
13693 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13694 return mod_type_die;
13697 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13699 /* Handle C typedef types. */
13700 if (name
13701 && TREE_CODE (name) == TYPE_DECL
13702 && DECL_ORIGINAL_TYPE (name)
13703 && !DECL_ARTIFICIAL (name))
13705 tree dtype = TREE_TYPE (name);
13707 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13708 if (qualified_type == dtype && !reverse_base_type)
13710 tree origin = decl_ultimate_origin (name);
13712 /* Typedef variants that have an abstract origin don't get their own
13713 type DIE (see gen_typedef_die), so fall back on the ultimate
13714 abstract origin instead. */
13715 if (origin != NULL && origin != name)
13716 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13717 context_die);
13719 /* For a named type, use the typedef. */
13720 gen_type_die (qualified_type, context_die);
13721 return lookup_type_die (qualified_type);
13723 else
13725 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13726 dquals &= cv_qual_mask;
13727 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13728 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13729 /* cv-unqualified version of named type. Just use
13730 the unnamed type to which it refers. */
13731 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13732 reverse, context_die);
13733 /* Else cv-qualified version of named type; fall through. */
13737 mod_scope = scope_die_for (type, context_die);
13739 if (cv_quals)
13741 int sub_quals = 0, first_quals = 0;
13742 unsigned i;
13743 dw_die_ref first = NULL, last = NULL;
13745 /* Determine a lesser qualified type that most closely matches
13746 this one. Then generate DW_TAG_* entries for the remaining
13747 qualifiers. */
13748 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13749 cv_qual_mask);
13750 if (sub_quals && use_debug_types)
13752 bool needed = false;
13753 /* If emitting type units, make sure the order of qualifiers
13754 is canonical. Thus, start from unqualified type if
13755 an earlier qualifier is missing in sub_quals, but some later
13756 one is present there. */
13757 for (i = 0; i < dwarf_qual_info_size; i++)
13758 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13759 needed = true;
13760 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13762 sub_quals = 0;
13763 break;
13766 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13767 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13769 /* As not all intermediate qualified DIEs have corresponding
13770 tree types, ensure that qualified DIEs in the same scope
13771 as their DW_AT_type are emitted after their DW_AT_type,
13772 only with other qualified DIEs for the same type possibly
13773 in between them. Determine the range of such qualified
13774 DIEs now (first being the base type, last being corresponding
13775 last qualified DIE for it). */
13776 unsigned int count = 0;
13777 first = qualified_die_p (mod_type_die, &first_quals,
13778 dwarf_qual_info_size);
13779 if (first == NULL)
13780 first = mod_type_die;
13781 gcc_assert ((first_quals & ~sub_quals) == 0);
13782 for (count = 0, last = first;
13783 count < (1U << dwarf_qual_info_size);
13784 count++, last = last->die_sib)
13786 int quals = 0;
13787 if (last == mod_scope->die_child)
13788 break;
13789 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13790 != first)
13791 break;
13795 for (i = 0; i < dwarf_qual_info_size; i++)
13796 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13798 dw_die_ref d;
13799 if (first && first != last)
13801 for (d = first->die_sib; ; d = d->die_sib)
13803 int quals = 0;
13804 qualified_die_p (d, &quals, dwarf_qual_info_size);
13805 if (quals == (first_quals | dwarf_qual_info[i].q))
13806 break;
13807 if (d == last)
13809 d = NULL;
13810 break;
13813 if (d)
13815 mod_type_die = d;
13816 continue;
13819 if (first)
13821 d = new_die_raw (dwarf_qual_info[i].t);
13822 add_child_die_after (mod_scope, d, last);
13823 last = d;
13825 else
13826 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13827 if (mod_type_die)
13828 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13829 mod_type_die = d;
13830 first_quals |= dwarf_qual_info[i].q;
13833 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13835 dwarf_tag tag = DW_TAG_pointer_type;
13836 if (code == REFERENCE_TYPE)
13838 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13839 tag = DW_TAG_rvalue_reference_type;
13840 else
13841 tag = DW_TAG_reference_type;
13843 mod_type_die = new_die (tag, mod_scope, type);
13845 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13846 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13847 add_alignment_attribute (mod_type_die, type);
13848 item_type = TREE_TYPE (type);
13850 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13851 if (!ADDR_SPACE_GENERIC_P (as))
13853 int action = targetm.addr_space.debug (as);
13854 if (action >= 0)
13856 /* Positive values indicate an address_class. */
13857 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13859 else
13861 /* Negative values indicate an (inverted) segment base reg. */
13862 dw_loc_descr_ref d
13863 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13864 add_AT_loc (mod_type_die, DW_AT_segment, d);
13868 else if (code == ARRAY_TYPE
13869 || (lang_hooks.types.get_array_descr_info
13870 && lang_hooks.types.get_array_descr_info (type, &info)))
13872 gen_type_die (type, context_die);
13873 return lookup_type_die (type);
13875 else if (code == INTEGER_TYPE
13876 && TREE_TYPE (type) != NULL_TREE
13877 && subrange_type_for_debug_p (type, &low, &high))
13879 tree bias = NULL_TREE;
13880 if (lang_hooks.types.get_type_bias)
13881 bias = lang_hooks.types.get_type_bias (type);
13882 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13883 item_type = TREE_TYPE (type);
13885 else if (is_base_type (type))
13887 /* If a target supports long double as different floating point
13888 modes with the same 16-byte size, use normal DW_TAG_base_type
13889 only for the composite (ibm_extended_real_format) type and
13890 for the other for the time being emit instead a "_Float128"
13891 or "complex _Float128" DW_TAG_base_type and a "long double"
13892 or "complex long double" typedef to it. */
13893 if (tree other_type = long_double_as_float128 (type))
13895 dw_die_ref other_die;
13896 if (TYPE_NAME (other_type))
13897 other_die
13898 = modified_type_die (other_type, TYPE_UNQUALIFIED, reverse,
13899 context_die);
13900 else
13902 other_die = base_type_die (type, reverse);
13903 add_child_die (comp_unit_die (), other_die);
13904 add_name_attribute (other_die,
13905 TREE_CODE (type) == COMPLEX_TYPE
13906 ? "complex _Float128" : "_Float128");
13908 mod_type_die = new_die_raw (DW_TAG_typedef);
13909 add_AT_die_ref (mod_type_die, DW_AT_type, other_die);
13911 else
13912 mod_type_die = base_type_die (type, reverse);
13914 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13915 if (reverse_base_type)
13917 dw_die_ref after_die
13918 = modified_type_die (type, cv_quals, false, context_die);
13919 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13921 else
13922 add_child_die (comp_unit_die (), mod_type_die);
13924 add_pubtype (type, mod_type_die);
13926 else
13928 gen_type_die (type, context_die);
13930 /* We have to get the type_main_variant here (and pass that to the
13931 `lookup_type_die' routine) because the ..._TYPE node we have
13932 might simply be a *copy* of some original type node (where the
13933 copy was created to help us keep track of typedef names) and
13934 that copy might have a different TYPE_UID from the original
13935 ..._TYPE node. */
13936 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13938 /* For function/method types, can't just use type_main_variant here,
13939 because that can have different ref-qualifiers for C++,
13940 but try to canonicalize. */
13941 tree main = TYPE_MAIN_VARIANT (type);
13942 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13943 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13944 && check_base_type (t, main)
13945 && check_lang_type (t, type))
13946 return lookup_type_die (t);
13947 return lookup_type_die (type);
13949 /* Vectors have the debugging information in the type,
13950 not the main variant. */
13951 else if (code == VECTOR_TYPE)
13952 return lookup_type_die (type);
13953 else
13954 return lookup_type_die (type_main_variant (type));
13957 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13958 don't output a DW_TAG_typedef, since there isn't one in the
13959 user's program; just attach a DW_AT_name to the type.
13960 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13961 if the base type already has the same name. */
13962 if (name
13963 && ((TREE_CODE (name) != TYPE_DECL
13964 && (qualified_type == TYPE_MAIN_VARIANT (type)
13965 || (cv_quals == TYPE_UNQUALIFIED)))
13966 || (TREE_CODE (name) == TYPE_DECL
13967 && TREE_TYPE (name) == qualified_type
13968 && DECL_NAME (name))))
13970 if (TREE_CODE (name) == TYPE_DECL)
13971 /* Could just call add_name_and_src_coords_attributes here,
13972 but since this is a builtin type it doesn't have any
13973 useful source coordinates anyway. */
13974 name = DECL_NAME (name);
13975 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13977 /* This probably indicates a bug. */
13978 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13980 name = TYPE_IDENTIFIER (type);
13981 add_name_attribute (mod_type_die,
13982 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13985 if (qualified_type && !reverse_base_type)
13986 equate_type_number_to_die (qualified_type, mod_type_die);
13988 if (item_type)
13989 /* We must do this after the equate_type_number_to_die call, in case
13990 this is a recursive type. This ensures that the modified_type_die
13991 recursion will terminate even if the type is recursive. Recursive
13992 types are possible in Ada. */
13993 sub_die = modified_type_die (item_type,
13994 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13995 reverse,
13996 context_die);
13998 if (sub_die != NULL)
13999 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
14001 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
14002 if (TYPE_ARTIFICIAL (type))
14003 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
14005 return mod_type_die;
14008 /* Generate DIEs for the generic parameters of T.
14009 T must be either a generic type or a generic function.
14010 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
14012 static void
14013 gen_generic_params_dies (tree t)
14015 tree parms, args;
14016 int parms_num, i;
14017 dw_die_ref die = NULL;
14018 int non_default;
14020 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
14021 return;
14023 if (TYPE_P (t))
14024 die = lookup_type_die (t);
14025 else if (DECL_P (t))
14026 die = lookup_decl_die (t);
14028 gcc_assert (die);
14030 parms = lang_hooks.get_innermost_generic_parms (t);
14031 if (!parms)
14032 /* T has no generic parameter. It means T is neither a generic type
14033 or function. End of story. */
14034 return;
14036 parms_num = TREE_VEC_LENGTH (parms);
14037 args = lang_hooks.get_innermost_generic_args (t);
14038 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
14039 non_default = int_cst_value (TREE_CHAIN (args));
14040 else
14041 non_default = TREE_VEC_LENGTH (args);
14042 for (i = 0; i < parms_num; i++)
14044 tree parm, arg, arg_pack_elems;
14045 dw_die_ref parm_die;
14047 parm = TREE_VEC_ELT (parms, i);
14048 arg = TREE_VEC_ELT (args, i);
14049 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
14050 gcc_assert (parm && TREE_VALUE (parm) && arg);
14052 if (parm && TREE_VALUE (parm) && arg)
14054 /* If PARM represents a template parameter pack,
14055 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
14056 by DW_TAG_template_*_parameter DIEs for the argument
14057 pack elements of ARG. Note that ARG would then be
14058 an argument pack. */
14059 if (arg_pack_elems)
14060 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
14061 arg_pack_elems,
14062 die);
14063 else
14064 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
14065 true /* emit name */, die);
14066 if (i >= non_default)
14067 add_AT_flag (parm_die, DW_AT_default_value, 1);
14072 /* Create and return a DIE for PARM which should be
14073 the representation of a generic type parameter.
14074 For instance, in the C++ front end, PARM would be a template parameter.
14075 ARG is the argument to PARM.
14076 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
14077 name of the PARM.
14078 PARENT_DIE is the parent DIE which the new created DIE should be added to,
14079 as a child node. */
14081 static dw_die_ref
14082 generic_parameter_die (tree parm, tree arg,
14083 bool emit_name_p,
14084 dw_die_ref parent_die)
14086 dw_die_ref tmpl_die = NULL;
14087 const char *name = NULL;
14089 /* C++20 accepts class literals as template parameters, and var
14090 decls with initializers represent them. The VAR_DECLs would be
14091 rejected, but we can take the DECL_INITIAL constructor and
14092 attempt to expand it. */
14093 if (arg && VAR_P (arg))
14094 arg = DECL_INITIAL (arg);
14096 if (!parm || !DECL_NAME (parm) || !arg)
14097 return NULL;
14099 /* We support non-type generic parameters and arguments,
14100 type generic parameters and arguments, as well as
14101 generic generic parameters (a.k.a. template template parameters in C++)
14102 and arguments. */
14103 if (TREE_CODE (parm) == PARM_DECL)
14104 /* PARM is a nontype generic parameter */
14105 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14106 else if (TREE_CODE (parm) == TYPE_DECL)
14107 /* PARM is a type generic parameter. */
14108 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14109 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14110 /* PARM is a generic generic parameter.
14111 Its DIE is a GNU extension. It shall have a
14112 DW_AT_name attribute to represent the name of the template template
14113 parameter, and a DW_AT_GNU_template_name attribute to represent the
14114 name of the template template argument. */
14115 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14116 parent_die, parm);
14117 else
14118 gcc_unreachable ();
14120 if (tmpl_die)
14122 tree tmpl_type;
14124 /* If PARM is a generic parameter pack, it means we are
14125 emitting debug info for a template argument pack element.
14126 In other terms, ARG is a template argument pack element.
14127 In that case, we don't emit any DW_AT_name attribute for
14128 the die. */
14129 if (emit_name_p)
14131 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14132 gcc_assert (name);
14133 add_AT_string (tmpl_die, DW_AT_name, name);
14136 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14138 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14139 TMPL_DIE should have a child DW_AT_type attribute that is set
14140 to the type of the argument to PARM, which is ARG.
14141 If PARM is a type generic parameter, TMPL_DIE should have a
14142 child DW_AT_type that is set to ARG. */
14143 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14144 add_type_attribute (tmpl_die, tmpl_type,
14145 (TREE_THIS_VOLATILE (tmpl_type)
14146 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14147 false, parent_die);
14149 else
14151 /* So TMPL_DIE is a DIE representing a
14152 a generic generic template parameter, a.k.a template template
14153 parameter in C++ and arg is a template. */
14155 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14156 to the name of the argument. */
14157 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14158 if (name)
14159 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14162 if (TREE_CODE (parm) == PARM_DECL)
14163 /* So PARM is a non-type generic parameter.
14164 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14165 attribute of TMPL_DIE which value represents the value
14166 of ARG.
14167 We must be careful here:
14168 The value of ARG might reference some function decls.
14169 We might currently be emitting debug info for a generic
14170 type and types are emitted before function decls, we don't
14171 know if the function decls referenced by ARG will actually be
14172 emitted after cgraph computations.
14173 So must defer the generation of the DW_AT_const_value to
14174 after cgraph is ready. */
14175 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14178 return tmpl_die;
14181 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14182 PARM_PACK must be a template parameter pack. The returned DIE
14183 will be child DIE of PARENT_DIE. */
14185 static dw_die_ref
14186 template_parameter_pack_die (tree parm_pack,
14187 tree parm_pack_args,
14188 dw_die_ref parent_die)
14190 dw_die_ref die;
14191 int j;
14193 gcc_assert (parent_die && parm_pack);
14195 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14196 add_name_and_src_coords_attributes (die, parm_pack);
14197 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14198 generic_parameter_die (parm_pack,
14199 TREE_VEC_ELT (parm_pack_args, j),
14200 false /* Don't emit DW_AT_name */,
14201 die);
14202 return die;
14205 /* Return the debugger register number described by a given RTL node. */
14207 static unsigned int
14208 debugger_reg_number (const_rtx rtl)
14210 unsigned regno = REGNO (rtl);
14212 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14214 #ifdef LEAF_REG_REMAP
14215 if (crtl->uses_only_leaf_regs)
14217 int leaf_reg = LEAF_REG_REMAP (regno);
14218 if (leaf_reg != -1)
14219 regno = (unsigned) leaf_reg;
14221 #endif
14223 regno = DEBUGGER_REGNO (regno);
14224 gcc_assert (regno != INVALID_REGNUM);
14225 return regno;
14228 /* Optionally add a DW_OP_piece term to a location description expression.
14229 DW_OP_piece is only added if the location description expression already
14230 doesn't end with DW_OP_piece. */
14232 static void
14233 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14235 dw_loc_descr_ref loc;
14237 if (*list_head != NULL)
14239 /* Find the end of the chain. */
14240 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14243 if (loc->dw_loc_opc != DW_OP_piece)
14244 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14248 /* Return a location descriptor that designates a machine register or
14249 zero if there is none. */
14251 static dw_loc_descr_ref
14252 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14254 rtx regs;
14256 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14257 return 0;
14259 /* We only use "frame base" when we're sure we're talking about the
14260 post-prologue local stack frame. We do this by *not* running
14261 register elimination until this point, and recognizing the special
14262 argument pointer and soft frame pointer rtx's.
14263 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14264 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14265 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14267 dw_loc_descr_ref result = NULL;
14269 if (dwarf_version >= 4 || !dwarf_strict)
14271 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14272 initialized);
14273 if (result)
14274 add_loc_descr (&result,
14275 new_loc_descr (DW_OP_stack_value, 0, 0));
14277 return result;
14280 regs = targetm.dwarf_register_span (rtl);
14282 if (REG_NREGS (rtl) > 1 || regs)
14283 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14284 else
14286 unsigned int debugger_regnum = debugger_reg_number (rtl);
14287 if (debugger_regnum == IGNORED_DWARF_REGNUM)
14288 return 0;
14289 return one_reg_loc_descriptor (debugger_regnum, initialized);
14293 /* Return a location descriptor that designates a machine register for
14294 a given hard register number. */
14296 static dw_loc_descr_ref
14297 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14299 dw_loc_descr_ref reg_loc_descr;
14301 if (regno <= 31)
14302 reg_loc_descr
14303 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14304 else
14305 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14307 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14308 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14310 return reg_loc_descr;
14313 /* Given an RTL of a register, return a location descriptor that
14314 designates a value that spans more than one register. */
14316 static dw_loc_descr_ref
14317 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14318 enum var_init_status initialized)
14320 int size, i;
14321 dw_loc_descr_ref loc_result = NULL;
14323 /* Simple, contiguous registers. */
14324 if (regs == NULL_RTX)
14326 unsigned reg = REGNO (rtl);
14327 int nregs;
14329 #ifdef LEAF_REG_REMAP
14330 if (crtl->uses_only_leaf_regs)
14332 int leaf_reg = LEAF_REG_REMAP (reg);
14333 if (leaf_reg != -1)
14334 reg = (unsigned) leaf_reg;
14336 #endif
14338 gcc_assert ((unsigned) DEBUGGER_REGNO (reg) == debugger_reg_number (rtl));
14339 nregs = REG_NREGS (rtl);
14341 /* At present we only track constant-sized pieces. */
14342 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14343 return NULL;
14344 size /= nregs;
14346 loc_result = NULL;
14347 while (nregs--)
14349 dw_loc_descr_ref t;
14351 t = one_reg_loc_descriptor (DEBUGGER_REGNO (reg),
14352 VAR_INIT_STATUS_INITIALIZED);
14353 add_loc_descr (&loc_result, t);
14354 add_loc_descr_op_piece (&loc_result, size);
14355 ++reg;
14357 return loc_result;
14360 /* Now onto stupid register sets in non contiguous locations. */
14362 gcc_assert (GET_CODE (regs) == PARALLEL);
14364 /* At present we only track constant-sized pieces. */
14365 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14366 return NULL;
14367 loc_result = NULL;
14369 for (i = 0; i < XVECLEN (regs, 0); ++i)
14371 dw_loc_descr_ref t;
14373 t = one_reg_loc_descriptor (debugger_reg_number (XVECEXP (regs, 0, i)),
14374 VAR_INIT_STATUS_INITIALIZED);
14375 add_loc_descr (&loc_result, t);
14376 add_loc_descr_op_piece (&loc_result, size);
14379 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14380 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14381 return loc_result;
14384 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14386 /* Return a location descriptor that designates a constant i,
14387 as a compound operation from constant (i >> shift), constant shift
14388 and DW_OP_shl. */
14390 static dw_loc_descr_ref
14391 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14393 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14394 add_loc_descr (&ret, int_loc_descriptor (shift));
14395 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14396 return ret;
14399 /* Return a location descriptor that designates constant POLY_I. */
14401 static dw_loc_descr_ref
14402 int_loc_descriptor (poly_int64 poly_i)
14404 enum dwarf_location_atom op;
14406 HOST_WIDE_INT i;
14407 if (!poly_i.is_constant (&i))
14409 /* Create location descriptions for the non-constant part and
14410 add any constant offset at the end. */
14411 dw_loc_descr_ref ret = NULL;
14412 HOST_WIDE_INT constant = poly_i.coeffs[0];
14413 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14415 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14416 if (coeff != 0)
14418 dw_loc_descr_ref start = ret;
14419 unsigned int factor;
14420 int bias;
14421 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14422 (j, &factor, &bias);
14424 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14425 add COEFF * (REGNO / FACTOR) now and subtract
14426 COEFF * BIAS from the final constant part. */
14427 constant -= coeff * bias;
14428 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14429 if (coeff % factor == 0)
14430 coeff /= factor;
14431 else
14433 int amount = exact_log2 (factor);
14434 gcc_assert (amount >= 0);
14435 add_loc_descr (&ret, int_loc_descriptor (amount));
14436 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14438 if (coeff != 1)
14440 add_loc_descr (&ret, int_loc_descriptor (coeff));
14441 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14443 if (start)
14444 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14447 loc_descr_plus_const (&ret, constant);
14448 return ret;
14451 /* Pick the smallest representation of a constant, rather than just
14452 defaulting to the LEB encoding. */
14453 if (i >= 0)
14455 int clz = clz_hwi (i);
14456 int ctz = ctz_hwi (i);
14457 if (i <= 31)
14458 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14459 else if (i <= 0xff)
14460 op = DW_OP_const1u;
14461 else if (i <= 0xffff)
14462 op = DW_OP_const2u;
14463 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14464 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14465 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14466 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14467 while DW_OP_const4u is 5 bytes. */
14468 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14469 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14470 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14471 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14472 while DW_OP_const4u is 5 bytes. */
14473 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14475 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14476 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14477 <= 4)
14479 /* As i >= 2**31, the double cast above will yield a negative number.
14480 Since wrapping is defined in DWARF expressions we can output big
14481 positive integers as small negative ones, regardless of the size
14482 of host wide ints.
14484 Here, since the evaluator will handle 32-bit values and since i >=
14485 2**31, we know it's going to be interpreted as a negative literal:
14486 store it this way if we can do better than 5 bytes this way. */
14487 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14489 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14490 op = DW_OP_const4u;
14492 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14493 least 6 bytes: see if we can do better before falling back to it. */
14494 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14495 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14496 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14497 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14498 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14499 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14500 >= HOST_BITS_PER_WIDE_INT)
14501 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14502 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14503 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14504 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14505 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14506 && size_of_uleb128 (i) > 6)
14507 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14508 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14509 else
14510 op = DW_OP_constu;
14512 else
14514 if (i >= -0x80)
14515 op = DW_OP_const1s;
14516 else if (i >= -0x8000)
14517 op = DW_OP_const2s;
14518 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14520 if (size_of_int_loc_descriptor (i) < 5)
14522 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14523 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14524 return ret;
14526 op = DW_OP_const4s;
14528 else
14530 if (size_of_int_loc_descriptor (i)
14531 < (unsigned long) 1 + size_of_sleb128 (i))
14533 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14534 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14535 return ret;
14537 op = DW_OP_consts;
14541 return new_loc_descr (op, i, 0);
14544 /* Likewise, for unsigned constants. */
14546 static dw_loc_descr_ref
14547 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14549 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14550 const unsigned HOST_WIDE_INT max_uint
14551 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14553 /* If possible, use the clever signed constants handling. */
14554 if (i <= max_int)
14555 return int_loc_descriptor ((HOST_WIDE_INT) i);
14557 /* Here, we are left with positive numbers that cannot be represented as
14558 HOST_WIDE_INT, i.e.:
14559 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14561 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14562 whereas may be better to output a negative integer: thanks to integer
14563 wrapping, we know that:
14564 x = x - 2 ** DWARF2_ADDR_SIZE
14565 = x - 2 * (max (HOST_WIDE_INT) + 1)
14566 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14567 small negative integers. Let's try that in cases it will clearly improve
14568 the encoding: there is no gain turning DW_OP_const4u into
14569 DW_OP_const4s. */
14570 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14571 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14572 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14574 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14576 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14577 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14578 const HOST_WIDE_INT second_shift
14579 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14581 /* So we finally have:
14582 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14583 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14584 return int_loc_descriptor (second_shift);
14587 /* Last chance: fallback to a simple constant operation. */
14588 return new_loc_descr
14589 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14590 ? DW_OP_const4u
14591 : DW_OP_const8u,
14592 i, 0);
14595 /* Generate and return a location description that computes the unsigned
14596 comparison of the two stack top entries (a OP b where b is the top-most
14597 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14598 LE_EXPR, GT_EXPR or GE_EXPR. */
14600 static dw_loc_descr_ref
14601 uint_comparison_loc_list (enum tree_code kind)
14603 enum dwarf_location_atom op, flip_op;
14604 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14606 switch (kind)
14608 case LT_EXPR:
14609 op = DW_OP_lt;
14610 break;
14611 case LE_EXPR:
14612 op = DW_OP_le;
14613 break;
14614 case GT_EXPR:
14615 op = DW_OP_gt;
14616 break;
14617 case GE_EXPR:
14618 op = DW_OP_ge;
14619 break;
14620 default:
14621 gcc_unreachable ();
14624 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14625 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14627 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14628 possible to perform unsigned comparisons: we just have to distinguish
14629 three cases:
14631 1. when a and b have the same sign (as signed integers); then we should
14632 return: a OP(signed) b;
14634 2. when a is a negative signed integer while b is a positive one, then a
14635 is a greater unsigned integer than b; likewise when a and b's roles
14636 are flipped.
14638 So first, compare the sign of the two operands. */
14639 ret = new_loc_descr (DW_OP_over, 0, 0);
14640 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14641 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14642 /* If they have different signs (i.e. they have different sign bits), then
14643 the stack top value has now the sign bit set and thus it's smaller than
14644 zero. */
14645 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14646 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14647 add_loc_descr (&ret, bra_node);
14649 /* We are in case 1. At this point, we know both operands have the same
14650 sign, to it's safe to use the built-in signed comparison. */
14651 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14652 add_loc_descr (&ret, jmp_node);
14654 /* We are in case 2. Here, we know both operands do not have the same sign,
14655 so we have to flip the signed comparison. */
14656 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14657 tmp = new_loc_descr (flip_op, 0, 0);
14658 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14659 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14660 add_loc_descr (&ret, tmp);
14662 /* This dummy operation is necessary to make the two branches join. */
14663 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14664 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14665 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14666 add_loc_descr (&ret, tmp);
14668 return ret;
14671 /* Likewise, but takes the location description lists (might be destructive on
14672 them). Return NULL if either is NULL or if concatenation fails. */
14674 static dw_loc_list_ref
14675 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14676 enum tree_code kind)
14678 if (left == NULL || right == NULL)
14679 return NULL;
14681 add_loc_list (&left, right);
14682 if (left == NULL)
14683 return NULL;
14685 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14686 return left;
14689 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14690 without actually allocating it. */
14692 static unsigned long
14693 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14695 return size_of_int_loc_descriptor (i >> shift)
14696 + size_of_int_loc_descriptor (shift)
14697 + 1;
14700 /* Return size_of_locs (int_loc_descriptor (i)) without
14701 actually allocating it. */
14703 static unsigned long
14704 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14706 unsigned long s;
14708 if (i >= 0)
14710 int clz, ctz;
14711 if (i <= 31)
14712 return 1;
14713 else if (i <= 0xff)
14714 return 2;
14715 else if (i <= 0xffff)
14716 return 3;
14717 clz = clz_hwi (i);
14718 ctz = ctz_hwi (i);
14719 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14720 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14721 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14722 - clz - 5);
14723 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14724 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14725 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14726 - clz - 8);
14727 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14728 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14729 <= 4)
14730 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14731 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14732 return 5;
14733 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14734 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14735 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14736 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14737 - clz - 8);
14738 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14739 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14740 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14741 - clz - 16);
14742 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14743 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14744 && s > 6)
14745 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14746 - clz - 32);
14747 else
14748 return 1 + s;
14750 else
14752 if (i >= -0x80)
14753 return 2;
14754 else if (i >= -0x8000)
14755 return 3;
14756 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14758 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14760 s = size_of_int_loc_descriptor (-i) + 1;
14761 if (s < 5)
14762 return s;
14764 return 5;
14766 else
14768 unsigned long r = 1 + size_of_sleb128 (i);
14769 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14771 s = size_of_int_loc_descriptor (-i) + 1;
14772 if (s < r)
14773 return s;
14775 return r;
14780 /* Return loc description representing "address" of integer value.
14781 This can appear only as toplevel expression. */
14783 static dw_loc_descr_ref
14784 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14786 int litsize;
14787 dw_loc_descr_ref loc_result = NULL;
14789 if (!(dwarf_version >= 4 || !dwarf_strict))
14790 return NULL;
14792 litsize = size_of_int_loc_descriptor (i);
14793 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14794 is more compact. For DW_OP_stack_value we need:
14795 litsize + 1 (DW_OP_stack_value)
14796 and for DW_OP_implicit_value:
14797 1 (DW_OP_implicit_value) + 1 (length) + size. */
14798 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14800 loc_result = int_loc_descriptor (i);
14801 add_loc_descr (&loc_result,
14802 new_loc_descr (DW_OP_stack_value, 0, 0));
14803 return loc_result;
14806 loc_result = new_loc_descr (DW_OP_implicit_value,
14807 size, 0);
14808 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14809 loc_result->dw_loc_oprnd2.v.val_int = i;
14810 return loc_result;
14813 /* Return a location descriptor that designates a base+offset location. */
14815 static dw_loc_descr_ref
14816 based_loc_descr (rtx reg, poly_int64 offset,
14817 enum var_init_status initialized)
14819 unsigned int regno;
14820 dw_loc_descr_ref result;
14821 dw_fde_ref fde = cfun->fde;
14823 /* We only use "frame base" when we're sure we're talking about the
14824 post-prologue local stack frame. We do this by *not* running
14825 register elimination until this point, and recognizing the special
14826 argument pointer and soft frame pointer rtx's. */
14827 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14829 rtx elim = (ira_use_lra_p
14830 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14831 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14833 if (elim != reg)
14835 /* Allow hard frame pointer here even if frame pointer
14836 isn't used since hard frame pointer is encoded with
14837 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14838 not hard frame pointer directly. */
14839 elim = strip_offset_and_add (elim, &offset);
14840 gcc_assert (elim == hard_frame_pointer_rtx
14841 || elim == stack_pointer_rtx);
14843 /* If drap register is used to align stack, use frame
14844 pointer + offset to access stack variables. If stack
14845 is aligned without drap, use stack pointer + offset to
14846 access stack variables. */
14847 if (crtl->stack_realign_tried
14848 && reg == frame_pointer_rtx)
14850 int base_reg
14851 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14852 ? HARD_FRAME_POINTER_REGNUM
14853 : REGNO (elim));
14854 return new_reg_loc_descr (base_reg, offset);
14857 gcc_assert (frame_pointer_fb_offset_valid);
14858 offset += frame_pointer_fb_offset;
14859 HOST_WIDE_INT const_offset;
14860 if (offset.is_constant (&const_offset))
14861 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14862 else
14864 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14865 loc_descr_plus_const (&ret, offset);
14866 return ret;
14871 regno = REGNO (reg);
14872 #ifdef LEAF_REG_REMAP
14873 if (crtl->uses_only_leaf_regs)
14875 int leaf_reg = LEAF_REG_REMAP (regno);
14876 if (leaf_reg != -1)
14877 regno = (unsigned) leaf_reg;
14879 #endif
14880 regno = DWARF_FRAME_REGNUM (regno);
14882 HOST_WIDE_INT const_offset;
14883 if (!optimize && fde
14884 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14885 && offset.is_constant (&const_offset))
14887 /* Use cfa+offset to represent the location of arguments passed
14888 on the stack when drap is used to align stack.
14889 Only do this when not optimizing, for optimized code var-tracking
14890 is supposed to track where the arguments live and the register
14891 used as vdrap or drap in some spot might be used for something
14892 else in other part of the routine. */
14893 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14896 result = new_reg_loc_descr (regno, offset);
14898 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14899 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14901 return result;
14904 /* Return true if this RTL expression describes a base+offset calculation. */
14906 static inline int
14907 is_based_loc (const_rtx rtl)
14909 return (GET_CODE (rtl) == PLUS
14910 && ((REG_P (XEXP (rtl, 0))
14911 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14912 && CONST_INT_P (XEXP (rtl, 1)))));
14915 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14916 failed. */
14918 static dw_loc_descr_ref
14919 tls_mem_loc_descriptor (rtx mem)
14921 tree base;
14922 dw_loc_descr_ref loc_result;
14924 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14925 return NULL;
14927 base = get_base_address (MEM_EXPR (mem));
14928 if (base == NULL
14929 || !VAR_P (base)
14930 || !DECL_THREAD_LOCAL_P (base))
14931 return NULL;
14933 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14934 if (loc_result == NULL)
14935 return NULL;
14937 if (maybe_ne (MEM_OFFSET (mem), 0))
14938 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14940 return loc_result;
14943 /* Output debug info about reason why we failed to expand expression as dwarf
14944 expression. */
14946 static void
14947 expansion_failed (tree expr, rtx rtl, char const *reason)
14949 if (dump_file && (dump_flags & TDF_DETAILS))
14951 fprintf (dump_file, "Failed to expand as dwarf: ");
14952 if (expr)
14953 print_generic_expr (dump_file, expr, dump_flags);
14954 if (rtl)
14956 fprintf (dump_file, "\n");
14957 print_rtl (dump_file, rtl);
14959 fprintf (dump_file, "\nReason: %s\n", reason);
14963 /* Helper function for const_ok_for_output. */
14965 static bool
14966 const_ok_for_output_1 (rtx rtl)
14968 if (targetm.const_not_ok_for_debug_p (rtl))
14970 if (GET_CODE (rtl) != UNSPEC)
14972 expansion_failed (NULL_TREE, rtl,
14973 "Expression rejected for debug by the backend.\n");
14974 return false;
14977 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14978 the target hook doesn't explicitly allow it in debug info, assume
14979 we can't express it in the debug info. */
14980 /* Don't complain about TLS UNSPECs, those are just too hard to
14981 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14982 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14983 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14984 if (flag_checking
14985 && (XVECLEN (rtl, 0) == 0
14986 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14987 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14988 inform (current_function_decl
14989 ? DECL_SOURCE_LOCATION (current_function_decl)
14990 : UNKNOWN_LOCATION,
14991 #if NUM_UNSPEC_VALUES > 0
14992 "non-delegitimized UNSPEC %s (%d) found in variable location",
14993 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14994 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14995 #else
14996 "non-delegitimized UNSPEC %d found in variable location",
14997 #endif
14998 XINT (rtl, 1));
14999 expansion_failed (NULL_TREE, rtl,
15000 "UNSPEC hasn't been delegitimized.\n");
15001 return false;
15004 if (CONST_POLY_INT_P (rtl))
15005 return false;
15007 /* FIXME: Refer to PR60655. It is possible for simplification
15008 of rtl expressions in var tracking to produce such expressions.
15009 We should really identify / validate expressions
15010 enclosed in CONST that can be handled by assemblers on various
15011 targets and only handle legitimate cases here. */
15012 switch (GET_CODE (rtl))
15014 case SYMBOL_REF:
15015 break;
15016 case NOT:
15017 case NEG:
15018 return false;
15019 case PLUS:
15021 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
15022 operands. */
15023 subrtx_var_iterator::array_type array;
15024 bool first = false;
15025 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15026 if (SYMBOL_REF_P (*iter)
15027 || LABEL_P (*iter)
15028 || GET_CODE (*iter) == UNSPEC)
15030 first = true;
15031 break;
15033 if (!first)
15034 return true;
15035 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15036 if (SYMBOL_REF_P (*iter)
15037 || LABEL_P (*iter)
15038 || GET_CODE (*iter) == UNSPEC)
15039 return false;
15040 return true;
15042 case MINUS:
15044 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
15045 appear in the second operand of MINUS. */
15046 subrtx_var_iterator::array_type array;
15047 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
15048 if (SYMBOL_REF_P (*iter)
15049 || LABEL_P (*iter)
15050 || GET_CODE (*iter) == UNSPEC)
15051 return false;
15052 return true;
15054 default:
15055 return true;
15058 if (CONSTANT_POOL_ADDRESS_P (rtl))
15060 bool marked;
15061 get_pool_constant_mark (rtl, &marked);
15062 /* If all references to this pool constant were optimized away,
15063 it was not output and thus we can't represent it. */
15064 if (!marked)
15066 expansion_failed (NULL_TREE, rtl,
15067 "Constant was removed from constant pool.\n");
15068 return false;
15072 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15073 return false;
15075 /* Avoid references to external symbols in debug info, on several targets
15076 the linker might even refuse to link when linking a shared library,
15077 and in many other cases the relocations for .debug_info/.debug_loc are
15078 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
15079 to be defined within the same shared library or executable are fine. */
15080 if (SYMBOL_REF_EXTERNAL_P (rtl))
15082 tree decl = SYMBOL_REF_DECL (rtl);
15084 if (decl == NULL || !targetm.binds_local_p (decl))
15086 expansion_failed (NULL_TREE, rtl,
15087 "Symbol not defined in current TU.\n");
15088 return false;
15092 return true;
15095 /* Return true if constant RTL can be emitted in DW_OP_addr or
15096 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
15097 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
15099 static bool
15100 const_ok_for_output (rtx rtl)
15102 if (GET_CODE (rtl) == SYMBOL_REF)
15103 return const_ok_for_output_1 (rtl);
15105 if (GET_CODE (rtl) == CONST)
15107 subrtx_var_iterator::array_type array;
15108 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15109 if (!const_ok_for_output_1 (*iter))
15110 return false;
15111 return true;
15114 return true;
15117 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15118 if possible, NULL otherwise. */
15120 static dw_die_ref
15121 base_type_for_mode (machine_mode mode, bool unsignedp)
15123 dw_die_ref type_die;
15124 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15126 if (type == NULL)
15127 return NULL;
15128 switch (TREE_CODE (type))
15130 case INTEGER_TYPE:
15131 case REAL_TYPE:
15132 break;
15133 default:
15134 return NULL;
15136 type_die = lookup_type_die (type);
15137 if (!type_die)
15138 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15139 comp_unit_die ());
15140 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15141 return NULL;
15142 return type_die;
15145 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15146 type matching MODE, or, if MODE is narrower than or as wide as
15147 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15148 possible. */
15150 static dw_loc_descr_ref
15151 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15153 machine_mode outer_mode = mode;
15154 dw_die_ref type_die;
15155 dw_loc_descr_ref cvt;
15157 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15159 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15160 return op;
15162 type_die = base_type_for_mode (outer_mode, 1);
15163 if (type_die == NULL)
15164 return NULL;
15165 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15166 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15167 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15168 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15169 add_loc_descr (&op, cvt);
15170 return op;
15173 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15175 static dw_loc_descr_ref
15176 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15177 dw_loc_descr_ref op1)
15179 dw_loc_descr_ref ret = op0;
15180 add_loc_descr (&ret, op1);
15181 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15182 if (STORE_FLAG_VALUE != 1)
15184 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15185 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15187 return ret;
15190 /* Subroutine of scompare_loc_descriptor for the case in which we're
15191 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15192 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15194 static dw_loc_descr_ref
15195 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15196 scalar_int_mode op_mode,
15197 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15199 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15200 dw_loc_descr_ref cvt;
15202 if (type_die == NULL)
15203 return NULL;
15204 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15205 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15206 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15207 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15208 add_loc_descr (&op0, cvt);
15209 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15210 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15211 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15212 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15213 add_loc_descr (&op1, cvt);
15214 return compare_loc_descriptor (op, op0, op1);
15217 /* Subroutine of scompare_loc_descriptor for the case in which we're
15218 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15219 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15221 static dw_loc_descr_ref
15222 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15223 scalar_int_mode op_mode,
15224 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15226 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15227 /* For eq/ne, if the operands are known to be zero-extended,
15228 there is no need to do the fancy shifting up. */
15229 if (op == DW_OP_eq || op == DW_OP_ne)
15231 dw_loc_descr_ref last0, last1;
15232 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15234 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15236 /* deref_size zero extends, and for constants we can check
15237 whether they are zero extended or not. */
15238 if (((last0->dw_loc_opc == DW_OP_deref_size
15239 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15240 || (CONST_INT_P (XEXP (rtl, 0))
15241 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15242 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15243 && ((last1->dw_loc_opc == DW_OP_deref_size
15244 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15245 || (CONST_INT_P (XEXP (rtl, 1))
15246 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15247 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15248 return compare_loc_descriptor (op, op0, op1);
15250 /* EQ/NE comparison against constant in narrower type than
15251 DWARF2_ADDR_SIZE can be performed either as
15252 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15253 DW_OP_{eq,ne}
15255 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15256 DW_OP_{eq,ne}. Pick whatever is shorter. */
15257 if (CONST_INT_P (XEXP (rtl, 1))
15258 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15259 && (size_of_int_loc_descriptor (shift) + 1
15260 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15261 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15262 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15263 & GET_MODE_MASK (op_mode))))
15265 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15266 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15267 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15268 & GET_MODE_MASK (op_mode));
15269 return compare_loc_descriptor (op, op0, op1);
15272 add_loc_descr (&op0, int_loc_descriptor (shift));
15273 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15274 if (CONST_INT_P (XEXP (rtl, 1)))
15275 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15276 else
15278 add_loc_descr (&op1, int_loc_descriptor (shift));
15279 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15281 return compare_loc_descriptor (op, op0, op1);
15284 /* Return location descriptor for signed comparison OP RTL. */
15286 static dw_loc_descr_ref
15287 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15288 machine_mode mem_mode)
15290 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15291 dw_loc_descr_ref op0, op1;
15293 if (op_mode == VOIDmode)
15294 op_mode = GET_MODE (XEXP (rtl, 1));
15295 if (op_mode == VOIDmode)
15296 return NULL;
15298 scalar_int_mode int_op_mode;
15299 if (dwarf_strict
15300 && dwarf_version < 5
15301 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15302 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15303 return NULL;
15305 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15306 VAR_INIT_STATUS_INITIALIZED);
15307 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15308 VAR_INIT_STATUS_INITIALIZED);
15310 if (op0 == NULL || op1 == NULL)
15311 return NULL;
15313 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15315 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15316 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15318 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15319 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15321 return compare_loc_descriptor (op, op0, op1);
15324 /* Return location descriptor for unsigned comparison OP RTL. */
15326 static dw_loc_descr_ref
15327 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15328 machine_mode mem_mode)
15330 dw_loc_descr_ref op0, op1;
15332 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15333 if (test_op_mode == VOIDmode)
15334 test_op_mode = GET_MODE (XEXP (rtl, 1));
15336 scalar_int_mode op_mode;
15337 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15338 return NULL;
15340 if (dwarf_strict
15341 && dwarf_version < 5
15342 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15343 return NULL;
15345 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15346 VAR_INIT_STATUS_INITIALIZED);
15347 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15348 VAR_INIT_STATUS_INITIALIZED);
15350 if (op0 == NULL || op1 == NULL)
15351 return NULL;
15353 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15355 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15356 dw_loc_descr_ref last0, last1;
15357 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15359 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15361 if (CONST_INT_P (XEXP (rtl, 0)))
15362 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15363 /* deref_size zero extends, so no need to mask it again. */
15364 else if (last0->dw_loc_opc != DW_OP_deref_size
15365 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15367 add_loc_descr (&op0, int_loc_descriptor (mask));
15368 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15370 if (CONST_INT_P (XEXP (rtl, 1)))
15371 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15372 /* deref_size zero extends, so no need to mask it again. */
15373 else if (last1->dw_loc_opc != DW_OP_deref_size
15374 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15376 add_loc_descr (&op1, int_loc_descriptor (mask));
15377 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15380 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15382 HOST_WIDE_INT bias = 1;
15383 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15384 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15385 if (CONST_INT_P (XEXP (rtl, 1)))
15386 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15387 + INTVAL (XEXP (rtl, 1)));
15388 else
15389 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15390 bias, 0));
15392 return compare_loc_descriptor (op, op0, op1);
15395 /* Return location descriptor for {U,S}{MIN,MAX}. */
15397 static dw_loc_descr_ref
15398 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15399 machine_mode mem_mode)
15401 enum dwarf_location_atom op;
15402 dw_loc_descr_ref op0, op1, ret;
15403 dw_loc_descr_ref bra_node, drop_node;
15405 scalar_int_mode int_mode;
15406 if (dwarf_strict
15407 && dwarf_version < 5
15408 && (!is_a <scalar_int_mode> (mode, &int_mode)
15409 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15410 return NULL;
15412 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15413 VAR_INIT_STATUS_INITIALIZED);
15414 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15415 VAR_INIT_STATUS_INITIALIZED);
15417 if (op0 == NULL || op1 == NULL)
15418 return NULL;
15420 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15421 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15422 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15423 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15425 /* Checked by the caller. */
15426 int_mode = as_a <scalar_int_mode> (mode);
15427 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15429 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15430 add_loc_descr (&op0, int_loc_descriptor (mask));
15431 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15432 add_loc_descr (&op1, int_loc_descriptor (mask));
15433 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15435 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15437 HOST_WIDE_INT bias = 1;
15438 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15439 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15440 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15443 else if (is_a <scalar_int_mode> (mode, &int_mode)
15444 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15446 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15447 add_loc_descr (&op0, int_loc_descriptor (shift));
15448 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15449 add_loc_descr (&op1, int_loc_descriptor (shift));
15450 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15452 else if (is_a <scalar_int_mode> (mode, &int_mode)
15453 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15455 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15456 dw_loc_descr_ref cvt;
15457 if (type_die == NULL)
15458 return NULL;
15459 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15460 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15461 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15462 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15463 add_loc_descr (&op0, cvt);
15464 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15465 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15466 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15467 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15468 add_loc_descr (&op1, cvt);
15471 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15472 op = DW_OP_lt;
15473 else
15474 op = DW_OP_gt;
15475 ret = op0;
15476 add_loc_descr (&ret, op1);
15477 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15478 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15479 add_loc_descr (&ret, bra_node);
15480 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15481 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15482 add_loc_descr (&ret, drop_node);
15483 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15484 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15485 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15486 && is_a <scalar_int_mode> (mode, &int_mode)
15487 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15488 ret = convert_descriptor_to_mode (int_mode, ret);
15489 return ret;
15492 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15493 but after converting arguments to type_die, afterwards
15494 convert back to unsigned. */
15496 static dw_loc_descr_ref
15497 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15498 scalar_int_mode mode, machine_mode mem_mode)
15500 dw_loc_descr_ref cvt, op0, op1;
15502 if (type_die == NULL)
15503 return NULL;
15504 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15505 VAR_INIT_STATUS_INITIALIZED);
15506 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15507 VAR_INIT_STATUS_INITIALIZED);
15508 if (op0 == NULL || op1 == NULL)
15509 return NULL;
15510 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15511 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15512 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15513 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15514 add_loc_descr (&op0, cvt);
15515 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15516 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15517 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15518 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15519 add_loc_descr (&op1, cvt);
15520 add_loc_descr (&op0, op1);
15521 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15522 return convert_descriptor_to_mode (mode, op0);
15525 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15526 const0 is DW_OP_lit0 or corresponding typed constant,
15527 const1 is DW_OP_lit1 or corresponding typed constant
15528 and constMSB is constant with just the MSB bit set
15529 for the mode):
15530 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15531 L1: const0 DW_OP_swap
15532 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15533 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15534 L3: DW_OP_drop
15535 L4: DW_OP_nop
15537 CTZ is similar:
15538 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15539 L1: const0 DW_OP_swap
15540 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15541 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15542 L3: DW_OP_drop
15543 L4: DW_OP_nop
15545 FFS is similar:
15546 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15547 L1: const1 DW_OP_swap
15548 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15549 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15550 L3: DW_OP_drop
15551 L4: DW_OP_nop */
15553 static dw_loc_descr_ref
15554 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15555 machine_mode mem_mode)
15557 dw_loc_descr_ref op0, ret, tmp;
15558 HOST_WIDE_INT valv;
15559 dw_loc_descr_ref l1jump, l1label;
15560 dw_loc_descr_ref l2jump, l2label;
15561 dw_loc_descr_ref l3jump, l3label;
15562 dw_loc_descr_ref l4jump, l4label;
15563 rtx msb;
15565 if (GET_MODE (XEXP (rtl, 0)) != mode)
15566 return NULL;
15568 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15569 VAR_INIT_STATUS_INITIALIZED);
15570 if (op0 == NULL)
15571 return NULL;
15572 ret = op0;
15573 if (GET_CODE (rtl) == CLZ)
15575 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15576 valv = GET_MODE_BITSIZE (mode);
15578 else if (GET_CODE (rtl) == FFS)
15579 valv = 0;
15580 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15581 valv = GET_MODE_BITSIZE (mode);
15582 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15583 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15584 add_loc_descr (&ret, l1jump);
15585 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15586 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15587 VAR_INIT_STATUS_INITIALIZED);
15588 if (tmp == NULL)
15589 return NULL;
15590 add_loc_descr (&ret, tmp);
15591 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15592 add_loc_descr (&ret, l4jump);
15593 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15594 ? const1_rtx : const0_rtx,
15595 mode, mem_mode,
15596 VAR_INIT_STATUS_INITIALIZED);
15597 if (l1label == NULL)
15598 return NULL;
15599 add_loc_descr (&ret, l1label);
15600 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15601 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15602 add_loc_descr (&ret, l2label);
15603 if (GET_CODE (rtl) != CLZ)
15604 msb = const1_rtx;
15605 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15606 msb = GEN_INT (HOST_WIDE_INT_1U
15607 << (GET_MODE_BITSIZE (mode) - 1));
15608 else
15609 msb = immed_wide_int_const
15610 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15611 GET_MODE_PRECISION (mode)), mode);
15612 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15613 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15614 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15615 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15616 else
15617 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15618 VAR_INIT_STATUS_INITIALIZED);
15619 if (tmp == NULL)
15620 return NULL;
15621 add_loc_descr (&ret, tmp);
15622 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15623 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15624 add_loc_descr (&ret, l3jump);
15625 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15626 VAR_INIT_STATUS_INITIALIZED);
15627 if (tmp == NULL)
15628 return NULL;
15629 add_loc_descr (&ret, tmp);
15630 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15631 ? DW_OP_shl : DW_OP_shr, 0, 0));
15632 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15633 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15634 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15635 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15636 add_loc_descr (&ret, l2jump);
15637 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15638 add_loc_descr (&ret, l3label);
15639 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15640 add_loc_descr (&ret, l4label);
15641 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15642 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15643 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15644 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15645 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15646 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15647 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15648 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15649 return ret;
15652 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15653 const1 is DW_OP_lit1 or corresponding typed constant):
15654 const0 DW_OP_swap
15655 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15656 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15657 L2: DW_OP_drop
15659 PARITY is similar:
15660 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15661 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15662 L2: DW_OP_drop */
15664 static dw_loc_descr_ref
15665 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15666 machine_mode mem_mode)
15668 dw_loc_descr_ref op0, ret, tmp;
15669 dw_loc_descr_ref l1jump, l1label;
15670 dw_loc_descr_ref l2jump, l2label;
15672 if (GET_MODE (XEXP (rtl, 0)) != mode)
15673 return NULL;
15675 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15676 VAR_INIT_STATUS_INITIALIZED);
15677 if (op0 == NULL)
15678 return NULL;
15679 ret = op0;
15680 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15681 VAR_INIT_STATUS_INITIALIZED);
15682 if (tmp == NULL)
15683 return NULL;
15684 add_loc_descr (&ret, tmp);
15685 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15686 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15687 add_loc_descr (&ret, l1label);
15688 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15689 add_loc_descr (&ret, l2jump);
15690 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15691 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15692 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15693 VAR_INIT_STATUS_INITIALIZED);
15694 if (tmp == NULL)
15695 return NULL;
15696 add_loc_descr (&ret, tmp);
15697 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15698 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15699 ? DW_OP_plus : DW_OP_xor, 0, 0));
15700 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15701 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15702 VAR_INIT_STATUS_INITIALIZED);
15703 add_loc_descr (&ret, tmp);
15704 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15705 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15706 add_loc_descr (&ret, l1jump);
15707 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15708 add_loc_descr (&ret, l2label);
15709 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15710 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15711 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15712 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15713 return ret;
15716 /* BSWAP (constS is initial shift count, either 56 or 24):
15717 constS const0
15718 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15719 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15720 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15721 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15722 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15724 static dw_loc_descr_ref
15725 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15726 machine_mode mem_mode)
15728 dw_loc_descr_ref op0, ret, tmp;
15729 dw_loc_descr_ref l1jump, l1label;
15730 dw_loc_descr_ref l2jump, l2label;
15732 if (BITS_PER_UNIT != 8
15733 || (GET_MODE_BITSIZE (mode) != 32
15734 && GET_MODE_BITSIZE (mode) != 64))
15735 return NULL;
15737 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15738 VAR_INIT_STATUS_INITIALIZED);
15739 if (op0 == NULL)
15740 return NULL;
15742 ret = op0;
15743 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15744 mode, mem_mode,
15745 VAR_INIT_STATUS_INITIALIZED);
15746 if (tmp == NULL)
15747 return NULL;
15748 add_loc_descr (&ret, tmp);
15749 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15750 VAR_INIT_STATUS_INITIALIZED);
15751 if (tmp == NULL)
15752 return NULL;
15753 add_loc_descr (&ret, tmp);
15754 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15755 add_loc_descr (&ret, l1label);
15756 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15757 mode, mem_mode,
15758 VAR_INIT_STATUS_INITIALIZED);
15759 add_loc_descr (&ret, tmp);
15760 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15761 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15762 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15763 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15764 VAR_INIT_STATUS_INITIALIZED);
15765 if (tmp == NULL)
15766 return NULL;
15767 add_loc_descr (&ret, tmp);
15768 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15769 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15770 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15771 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15772 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15773 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15774 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15775 VAR_INIT_STATUS_INITIALIZED);
15776 add_loc_descr (&ret, tmp);
15777 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15778 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15779 add_loc_descr (&ret, l2jump);
15780 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15781 VAR_INIT_STATUS_INITIALIZED);
15782 add_loc_descr (&ret, tmp);
15783 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15784 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15785 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15786 add_loc_descr (&ret, l1jump);
15787 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15788 add_loc_descr (&ret, l2label);
15789 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15790 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15791 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15792 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15793 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15794 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15795 return ret;
15798 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15799 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15800 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15801 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15803 ROTATERT is similar:
15804 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15805 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15806 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15808 static dw_loc_descr_ref
15809 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15810 machine_mode mem_mode)
15812 rtx rtlop1 = XEXP (rtl, 1);
15813 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15814 int i;
15816 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15817 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15818 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15819 VAR_INIT_STATUS_INITIALIZED);
15820 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15821 VAR_INIT_STATUS_INITIALIZED);
15822 if (op0 == NULL || op1 == NULL)
15823 return NULL;
15824 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15825 for (i = 0; i < 2; i++)
15827 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15828 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15829 mode, mem_mode,
15830 VAR_INIT_STATUS_INITIALIZED);
15831 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15832 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15833 ? DW_OP_const4u
15834 : HOST_BITS_PER_WIDE_INT == 64
15835 ? DW_OP_const8u : DW_OP_constu,
15836 GET_MODE_MASK (mode), 0);
15837 else
15838 mask[i] = NULL;
15839 if (mask[i] == NULL)
15840 return NULL;
15841 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15843 ret = op0;
15844 add_loc_descr (&ret, op1);
15845 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15846 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15847 if (GET_CODE (rtl) == ROTATERT)
15849 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15850 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15851 GET_MODE_BITSIZE (mode), 0));
15853 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15854 if (mask[0] != NULL)
15855 add_loc_descr (&ret, mask[0]);
15856 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15857 if (mask[1] != NULL)
15859 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15860 add_loc_descr (&ret, mask[1]);
15861 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15863 if (GET_CODE (rtl) == ROTATE)
15865 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15866 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15867 GET_MODE_BITSIZE (mode), 0));
15869 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15870 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15871 return ret;
15874 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15875 for DEBUG_PARAMETER_REF RTL. */
15877 static dw_loc_descr_ref
15878 parameter_ref_descriptor (rtx rtl)
15880 dw_loc_descr_ref ret;
15881 dw_die_ref ref;
15883 if (dwarf_strict)
15884 return NULL;
15885 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15886 /* With LTO during LTRANS we get the late DIE that refers to the early
15887 DIE, thus we add another indirection here. This seems to confuse
15888 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15889 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15890 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15891 if (ref)
15893 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15894 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15895 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15897 else
15899 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15900 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15902 return ret;
15905 /* The following routine converts the RTL for a variable or parameter
15906 (resident in memory) into an equivalent Dwarf representation of a
15907 mechanism for getting the address of that same variable onto the top of a
15908 hypothetical "address evaluation" stack.
15910 When creating memory location descriptors, we are effectively transforming
15911 the RTL for a memory-resident object into its Dwarf postfix expression
15912 equivalent. This routine recursively descends an RTL tree, turning
15913 it into Dwarf postfix code as it goes.
15915 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15917 MEM_MODE is the mode of the memory reference, needed to handle some
15918 autoincrement addressing modes.
15920 Return 0 if we can't represent the location. */
15922 dw_loc_descr_ref
15923 mem_loc_descriptor (rtx rtl, machine_mode mode,
15924 machine_mode mem_mode,
15925 enum var_init_status initialized)
15927 dw_loc_descr_ref mem_loc_result = NULL;
15928 enum dwarf_location_atom op;
15929 dw_loc_descr_ref op0, op1;
15930 rtx inner = NULL_RTX;
15931 poly_int64 offset;
15933 if (mode == VOIDmode)
15934 mode = GET_MODE (rtl);
15936 /* Note that for a dynamically sized array, the location we will generate a
15937 description of here will be the lowest numbered location which is
15938 actually within the array. That's *not* necessarily the same as the
15939 zeroth element of the array. */
15941 rtl = targetm.delegitimize_address (rtl);
15943 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15944 return NULL;
15946 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15947 switch (GET_CODE (rtl))
15949 case POST_INC:
15950 case POST_DEC:
15951 case POST_MODIFY:
15952 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15954 case SUBREG:
15955 /* The case of a subreg may arise when we have a local (register)
15956 variable or a formal (register) parameter which doesn't quite fill
15957 up an entire register. For now, just assume that it is
15958 legitimate to make the Dwarf info refer to the whole register which
15959 contains the given subreg. */
15960 if (!subreg_lowpart_p (rtl))
15961 break;
15962 inner = SUBREG_REG (rtl);
15963 /* FALLTHRU */
15964 case TRUNCATE:
15965 if (inner == NULL_RTX)
15966 inner = XEXP (rtl, 0);
15967 if (is_a <scalar_int_mode> (mode, &int_mode)
15968 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15969 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15970 #ifdef POINTERS_EXTEND_UNSIGNED
15971 || (int_mode == Pmode && mem_mode != VOIDmode)
15972 #endif
15974 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15976 mem_loc_result = mem_loc_descriptor (inner,
15977 inner_mode,
15978 mem_mode, initialized);
15979 break;
15981 if (dwarf_strict && dwarf_version < 5)
15982 break;
15983 if (is_a <scalar_int_mode> (mode, &int_mode)
15984 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15985 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15986 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15988 dw_die_ref type_die;
15989 dw_loc_descr_ref cvt;
15991 mem_loc_result = mem_loc_descriptor (inner,
15992 GET_MODE (inner),
15993 mem_mode, initialized);
15994 if (mem_loc_result == NULL)
15995 break;
15996 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15997 if (type_die == NULL)
15999 mem_loc_result = NULL;
16000 break;
16002 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
16003 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16004 else
16005 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
16006 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16007 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16008 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16009 add_loc_descr (&mem_loc_result, cvt);
16010 if (is_a <scalar_int_mode> (mode, &int_mode)
16011 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16013 /* Convert it to untyped afterwards. */
16014 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16015 add_loc_descr (&mem_loc_result, cvt);
16018 break;
16020 case REG:
16021 if (!is_a <scalar_int_mode> (mode, &int_mode)
16022 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16023 && rtl != arg_pointer_rtx
16024 && rtl != frame_pointer_rtx
16025 #ifdef POINTERS_EXTEND_UNSIGNED
16026 && (int_mode != Pmode || mem_mode == VOIDmode)
16027 #endif
16030 dw_die_ref type_die;
16031 unsigned int debugger_regnum;
16033 if (dwarf_strict && dwarf_version < 5)
16034 break;
16035 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
16036 break;
16037 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16038 if (type_die == NULL)
16039 break;
16041 debugger_regnum = debugger_reg_number (rtl);
16042 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16043 break;
16044 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
16045 debugger_regnum, 0);
16046 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16047 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16048 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
16049 break;
16051 /* Whenever a register number forms a part of the description of the
16052 method for calculating the (dynamic) address of a memory resident
16053 object, DWARF rules require the register number be referred to as
16054 a "base register". This distinction is not based in any way upon
16055 what category of register the hardware believes the given register
16056 belongs to. This is strictly DWARF terminology we're dealing with
16057 here. Note that in cases where the location of a memory-resident
16058 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
16059 OP_CONST (0)) the actual DWARF location descriptor that we generate
16060 may just be OP_BASEREG (basereg). This may look deceptively like
16061 the object in question was allocated to a register (rather than in
16062 memory) so DWARF consumers need to be aware of the subtle
16063 distinction between OP_REG and OP_BASEREG. */
16064 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
16065 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
16066 else if (stack_realign_drap
16067 && crtl->drap_reg
16068 && crtl->args.internal_arg_pointer == rtl
16069 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
16071 /* If RTL is internal_arg_pointer, which has been optimized
16072 out, use DRAP instead. */
16073 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
16074 VAR_INIT_STATUS_INITIALIZED);
16076 break;
16078 case SIGN_EXTEND:
16079 case ZERO_EXTEND:
16080 if (!is_a <scalar_int_mode> (mode, &int_mode)
16081 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
16082 break;
16083 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16084 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16085 if (op0 == 0)
16086 break;
16087 else if (GET_CODE (rtl) == ZERO_EXTEND
16088 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16089 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
16090 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
16091 to expand zero extend as two shifts instead of
16092 masking. */
16093 && GET_MODE_SIZE (inner_mode) <= 4)
16095 mem_loc_result = op0;
16096 add_loc_descr (&mem_loc_result,
16097 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
16098 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
16100 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
16102 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
16103 shift *= BITS_PER_UNIT;
16104 if (GET_CODE (rtl) == SIGN_EXTEND)
16105 op = DW_OP_shra;
16106 else
16107 op = DW_OP_shr;
16108 mem_loc_result = op0;
16109 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16110 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16111 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16112 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16114 else if (!dwarf_strict || dwarf_version >= 5)
16116 dw_die_ref type_die1, type_die2;
16117 dw_loc_descr_ref cvt;
16119 type_die1 = base_type_for_mode (inner_mode,
16120 GET_CODE (rtl) == ZERO_EXTEND);
16121 if (type_die1 == NULL)
16122 break;
16123 type_die2 = base_type_for_mode (int_mode, 1);
16124 if (type_die2 == NULL)
16125 break;
16126 mem_loc_result = op0;
16127 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16128 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16129 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16130 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16131 add_loc_descr (&mem_loc_result, cvt);
16132 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16133 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16134 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16135 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16136 add_loc_descr (&mem_loc_result, cvt);
16138 break;
16140 case MEM:
16142 rtx new_rtl = avoid_constant_pool_reference (rtl);
16143 if (new_rtl != rtl)
16145 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16146 initialized);
16147 if (mem_loc_result != NULL)
16148 return mem_loc_result;
16151 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16152 get_address_mode (rtl), mode,
16153 VAR_INIT_STATUS_INITIALIZED);
16154 if (mem_loc_result == NULL)
16155 mem_loc_result = tls_mem_loc_descriptor (rtl);
16156 if (mem_loc_result != NULL)
16158 if (!is_a <scalar_int_mode> (mode, &int_mode)
16159 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16161 dw_die_ref type_die;
16162 dw_loc_descr_ref deref;
16163 HOST_WIDE_INT size;
16165 if (dwarf_strict && dwarf_version < 5)
16166 return NULL;
16167 if (!GET_MODE_SIZE (mode).is_constant (&size))
16168 return NULL;
16169 type_die
16170 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16171 if (type_die == NULL)
16172 return NULL;
16173 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16174 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16175 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16176 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16177 add_loc_descr (&mem_loc_result, deref);
16179 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16180 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16181 else
16182 add_loc_descr (&mem_loc_result,
16183 new_loc_descr (DW_OP_deref_size,
16184 GET_MODE_SIZE (int_mode), 0));
16186 break;
16188 case LO_SUM:
16189 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16191 case LABEL_REF:
16192 /* Some ports can transform a symbol ref into a label ref, because
16193 the symbol ref is too far away and has to be dumped into a constant
16194 pool. */
16195 case CONST:
16196 case SYMBOL_REF:
16197 case UNSPEC:
16198 if (!is_a <scalar_int_mode> (mode, &int_mode)
16199 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16200 #ifdef POINTERS_EXTEND_UNSIGNED
16201 && (int_mode != Pmode || mem_mode == VOIDmode)
16202 #endif
16204 break;
16206 if (GET_CODE (rtl) == UNSPEC)
16208 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16209 can't express it in the debug info. This can happen e.g. with some
16210 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16211 approves. */
16212 bool not_ok = false;
16213 subrtx_var_iterator::array_type array;
16214 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16215 if (*iter != rtl && !CONSTANT_P (*iter))
16217 not_ok = true;
16218 break;
16221 if (not_ok)
16222 break;
16224 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16225 if (!const_ok_for_output_1 (*iter))
16227 not_ok = true;
16228 break;
16231 if (not_ok)
16232 break;
16234 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16235 goto symref;
16238 if (GET_CODE (rtl) == SYMBOL_REF
16239 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16241 dw_loc_descr_ref temp;
16243 /* If this is not defined, we have no way to emit the data. */
16244 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16245 break;
16247 temp = new_addr_loc_descr (rtl, dtprel_true);
16249 /* We check for DWARF 5 here because gdb did not implement
16250 DW_OP_form_tls_address until after 7.12. */
16251 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16252 ? DW_OP_form_tls_address
16253 : DW_OP_GNU_push_tls_address),
16254 0, 0);
16255 add_loc_descr (&mem_loc_result, temp);
16257 break;
16260 if (!const_ok_for_output (rtl))
16262 if (GET_CODE (rtl) == CONST)
16263 switch (GET_CODE (XEXP (rtl, 0)))
16265 case NOT:
16266 op = DW_OP_not;
16267 goto try_const_unop;
16268 case NEG:
16269 op = DW_OP_neg;
16270 goto try_const_unop;
16271 try_const_unop:
16272 rtx arg;
16273 arg = XEXP (XEXP (rtl, 0), 0);
16274 if (!CONSTANT_P (arg))
16275 arg = gen_rtx_CONST (int_mode, arg);
16276 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16277 initialized);
16278 if (op0)
16280 mem_loc_result = op0;
16281 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16283 break;
16284 default:
16285 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16286 mem_mode, initialized);
16287 break;
16289 break;
16292 symref:
16293 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16294 vec_safe_push (used_rtx_array, rtl);
16295 break;
16297 case CONCAT:
16298 case CONCATN:
16299 case VAR_LOCATION:
16300 case DEBUG_IMPLICIT_PTR:
16301 expansion_failed (NULL_TREE, rtl,
16302 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16303 return 0;
16305 case ENTRY_VALUE:
16306 if (dwarf_strict && dwarf_version < 5)
16307 return NULL;
16308 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16310 if (!is_a <scalar_int_mode> (mode, &int_mode)
16311 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16312 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16313 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16314 else
16316 unsigned int debugger_regnum = debugger_reg_number (ENTRY_VALUE_EXP (rtl));
16317 if (debugger_regnum == IGNORED_DWARF_REGNUM)
16318 return NULL;
16319 op0 = one_reg_loc_descriptor (debugger_regnum,
16320 VAR_INIT_STATUS_INITIALIZED);
16323 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16324 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16326 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16327 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16328 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16329 return NULL;
16331 else
16332 gcc_unreachable ();
16333 if (op0 == NULL)
16334 return NULL;
16335 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16336 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16337 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16338 break;
16340 case DEBUG_PARAMETER_REF:
16341 mem_loc_result = parameter_ref_descriptor (rtl);
16342 break;
16344 case PRE_MODIFY:
16345 /* Extract the PLUS expression nested inside and fall into
16346 PLUS code below. */
16347 rtl = XEXP (rtl, 1);
16348 goto plus;
16350 case PRE_INC:
16351 case PRE_DEC:
16352 /* Turn these into a PLUS expression and fall into the PLUS code
16353 below. */
16354 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16355 gen_int_mode (GET_CODE (rtl) == PRE_INC
16356 ? GET_MODE_UNIT_SIZE (mem_mode)
16357 : -GET_MODE_UNIT_SIZE (mem_mode),
16358 mode));
16360 /* fall through */
16362 case PLUS:
16363 plus:
16364 if (is_based_loc (rtl)
16365 && is_a <scalar_int_mode> (mode, &int_mode)
16366 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16367 || XEXP (rtl, 0) == arg_pointer_rtx
16368 || XEXP (rtl, 0) == frame_pointer_rtx))
16369 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16370 INTVAL (XEXP (rtl, 1)),
16371 VAR_INIT_STATUS_INITIALIZED);
16372 else
16374 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16375 VAR_INIT_STATUS_INITIALIZED);
16376 if (mem_loc_result == 0)
16377 break;
16379 if (CONST_INT_P (XEXP (rtl, 1))
16380 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16381 <= DWARF2_ADDR_SIZE))
16382 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16383 else
16385 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16386 VAR_INIT_STATUS_INITIALIZED);
16387 if (op1 == 0)
16388 return NULL;
16389 add_loc_descr (&mem_loc_result, op1);
16390 add_loc_descr (&mem_loc_result,
16391 new_loc_descr (DW_OP_plus, 0, 0));
16394 break;
16396 /* If a pseudo-reg is optimized away, it is possible for it to
16397 be replaced with a MEM containing a multiply or shift. */
16398 case MINUS:
16399 op = DW_OP_minus;
16400 goto do_binop;
16402 case MULT:
16403 op = DW_OP_mul;
16404 goto do_binop;
16406 case DIV:
16407 if ((!dwarf_strict || dwarf_version >= 5)
16408 && is_a <scalar_int_mode> (mode, &int_mode)
16409 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16411 mem_loc_result = typed_binop (DW_OP_div, rtl,
16412 base_type_for_mode (mode, 0),
16413 int_mode, mem_mode);
16414 break;
16416 op = DW_OP_div;
16417 goto do_binop;
16419 case UMOD:
16420 op = DW_OP_mod;
16421 goto do_binop;
16423 case ASHIFT:
16424 op = DW_OP_shl;
16425 goto do_shift;
16427 case ASHIFTRT:
16428 op = DW_OP_shra;
16429 goto do_shift;
16431 case LSHIFTRT:
16432 op = DW_OP_shr;
16433 goto do_shift;
16435 do_shift:
16436 if (!is_a <scalar_int_mode> (mode, &int_mode))
16437 break;
16438 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16439 VAR_INIT_STATUS_INITIALIZED);
16441 rtx rtlop1 = XEXP (rtl, 1);
16442 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16443 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16444 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16445 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16446 VAR_INIT_STATUS_INITIALIZED);
16449 if (op0 == 0 || op1 == 0)
16450 break;
16452 mem_loc_result = op0;
16453 add_loc_descr (&mem_loc_result, op1);
16454 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16455 break;
16457 case AND:
16458 op = DW_OP_and;
16459 goto do_binop;
16461 case IOR:
16462 op = DW_OP_or;
16463 goto do_binop;
16465 case XOR:
16466 op = DW_OP_xor;
16467 goto do_binop;
16469 do_binop:
16470 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16471 VAR_INIT_STATUS_INITIALIZED);
16472 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16474 if (op0 == 0)
16475 break;
16476 mem_loc_result = op0;
16477 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16478 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16479 break;
16481 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16482 VAR_INIT_STATUS_INITIALIZED);
16484 if (op0 == 0 || op1 == 0)
16485 break;
16487 mem_loc_result = op0;
16488 add_loc_descr (&mem_loc_result, op1);
16489 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16490 break;
16492 case MOD:
16493 if ((!dwarf_strict || dwarf_version >= 5)
16494 && is_a <scalar_int_mode> (mode, &int_mode)
16495 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16497 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16498 base_type_for_mode (mode, 0),
16499 int_mode, mem_mode);
16500 break;
16503 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16504 VAR_INIT_STATUS_INITIALIZED);
16505 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16506 VAR_INIT_STATUS_INITIALIZED);
16508 if (op0 == 0 || op1 == 0)
16509 break;
16511 mem_loc_result = op0;
16512 add_loc_descr (&mem_loc_result, op1);
16513 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16514 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16515 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16516 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16517 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16518 break;
16520 case UDIV:
16521 if ((!dwarf_strict || dwarf_version >= 5)
16522 && is_a <scalar_int_mode> (mode, &int_mode))
16524 /* We can use a signed divide if the sign bit is not set. */
16525 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16527 op = DW_OP_div;
16528 goto do_binop;
16531 mem_loc_result = typed_binop (DW_OP_div, rtl,
16532 base_type_for_mode (int_mode, 1),
16533 int_mode, mem_mode);
16535 break;
16537 case NOT:
16538 op = DW_OP_not;
16539 goto do_unop;
16541 case ABS:
16542 op = DW_OP_abs;
16543 goto do_unop;
16545 case NEG:
16546 op = DW_OP_neg;
16547 goto do_unop;
16549 do_unop:
16550 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16551 VAR_INIT_STATUS_INITIALIZED);
16553 if (op0 == 0)
16554 break;
16556 mem_loc_result = op0;
16557 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16558 break;
16560 case CONST_INT:
16561 if (!is_a <scalar_int_mode> (mode, &int_mode)
16562 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16563 #ifdef POINTERS_EXTEND_UNSIGNED
16564 || (int_mode == Pmode
16565 && mem_mode != VOIDmode
16566 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16567 #endif
16570 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16571 break;
16573 if ((!dwarf_strict || dwarf_version >= 5)
16574 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16575 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16577 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16578 scalar_int_mode amode;
16579 if (type_die == NULL)
16580 return NULL;
16581 if (INTVAL (rtl) >= 0
16582 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16583 .exists (&amode))
16584 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16585 /* const DW_OP_convert <XXX> vs.
16586 DW_OP_const_type <XXX, 1, const>. */
16587 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16588 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16590 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16591 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16592 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16593 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16594 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16595 add_loc_descr (&mem_loc_result, op0);
16596 return mem_loc_result;
16598 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16599 INTVAL (rtl));
16600 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16601 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16602 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16603 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16604 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16605 else
16607 mem_loc_result->dw_loc_oprnd2.val_class
16608 = dw_val_class_const_double;
16609 mem_loc_result->dw_loc_oprnd2.v.val_double
16610 = double_int::from_shwi (INTVAL (rtl));
16613 break;
16615 case CONST_DOUBLE:
16616 if (!dwarf_strict || dwarf_version >= 5)
16618 dw_die_ref type_die;
16620 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16621 CONST_DOUBLE rtx could represent either a large integer
16622 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16623 the value is always a floating point constant.
16625 When it is an integer, a CONST_DOUBLE is used whenever
16626 the constant requires 2 HWIs to be adequately represented.
16627 We output CONST_DOUBLEs as blocks. */
16628 if (mode == VOIDmode
16629 || (GET_MODE (rtl) == VOIDmode
16630 && maybe_ne (GET_MODE_BITSIZE (mode),
16631 HOST_BITS_PER_DOUBLE_INT)))
16632 break;
16633 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16634 if (type_die == NULL)
16635 return NULL;
16636 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16637 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16638 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16639 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16640 #if TARGET_SUPPORTS_WIDE_INT == 0
16641 if (!SCALAR_FLOAT_MODE_P (mode))
16643 mem_loc_result->dw_loc_oprnd2.val_class
16644 = dw_val_class_const_double;
16645 mem_loc_result->dw_loc_oprnd2.v.val_double
16646 = rtx_to_double_int (rtl);
16648 else
16649 #endif
16651 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16652 unsigned int length = GET_MODE_SIZE (float_mode);
16653 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16654 unsigned int elt_size = insert_float (rtl, array);
16656 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16657 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16658 = length / elt_size;
16659 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16660 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16663 break;
16665 case CONST_WIDE_INT:
16666 if (!dwarf_strict || dwarf_version >= 5)
16668 dw_die_ref type_die;
16670 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16671 if (type_die == NULL)
16672 return NULL;
16673 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16674 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16675 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16676 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16677 mem_loc_result->dw_loc_oprnd2.val_class
16678 = dw_val_class_wide_int;
16679 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16680 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16682 break;
16684 case CONST_POLY_INT:
16685 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16686 break;
16688 case EQ:
16689 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16690 break;
16692 case GE:
16693 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16694 break;
16696 case GT:
16697 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16698 break;
16700 case LE:
16701 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16702 break;
16704 case LT:
16705 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16706 break;
16708 case NE:
16709 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16710 break;
16712 case GEU:
16713 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16714 break;
16716 case GTU:
16717 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16718 break;
16720 case LEU:
16721 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16722 break;
16724 case LTU:
16725 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16726 break;
16728 case UMIN:
16729 case UMAX:
16730 if (!SCALAR_INT_MODE_P (mode))
16731 break;
16732 /* FALLTHRU */
16733 case SMIN:
16734 case SMAX:
16735 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16736 break;
16738 case ZERO_EXTRACT:
16739 case SIGN_EXTRACT:
16740 if (CONST_INT_P (XEXP (rtl, 1))
16741 && CONST_INT_P (XEXP (rtl, 2))
16742 && is_a <scalar_int_mode> (mode, &int_mode)
16743 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16744 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16745 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16746 && ((unsigned) INTVAL (XEXP (rtl, 1))
16747 + (unsigned) INTVAL (XEXP (rtl, 2))
16748 <= GET_MODE_BITSIZE (int_mode)))
16750 int shift, size;
16751 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16752 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16753 if (op0 == 0)
16754 break;
16755 if (GET_CODE (rtl) == SIGN_EXTRACT)
16756 op = DW_OP_shra;
16757 else
16758 op = DW_OP_shr;
16759 mem_loc_result = op0;
16760 size = INTVAL (XEXP (rtl, 1));
16761 shift = INTVAL (XEXP (rtl, 2));
16762 if (BITS_BIG_ENDIAN)
16763 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16764 if (shift + size != (int) DWARF2_ADDR_SIZE)
16766 add_loc_descr (&mem_loc_result,
16767 int_loc_descriptor (DWARF2_ADDR_SIZE
16768 - shift - size));
16769 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16771 if (size != (int) DWARF2_ADDR_SIZE)
16773 add_loc_descr (&mem_loc_result,
16774 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16775 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16778 break;
16780 case IF_THEN_ELSE:
16782 dw_loc_descr_ref op2, bra_node, drop_node;
16783 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16784 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16785 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16786 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16787 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16788 VAR_INIT_STATUS_INITIALIZED);
16789 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16790 VAR_INIT_STATUS_INITIALIZED);
16791 if (op0 == NULL || op1 == NULL || op2 == NULL)
16792 break;
16794 mem_loc_result = op1;
16795 add_loc_descr (&mem_loc_result, op2);
16796 add_loc_descr (&mem_loc_result, op0);
16797 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16798 add_loc_descr (&mem_loc_result, bra_node);
16799 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16800 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16801 add_loc_descr (&mem_loc_result, drop_node);
16802 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16803 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16805 break;
16807 case FLOAT_EXTEND:
16808 case FLOAT_TRUNCATE:
16809 case FLOAT:
16810 case UNSIGNED_FLOAT:
16811 case FIX:
16812 case UNSIGNED_FIX:
16813 if (!dwarf_strict || dwarf_version >= 5)
16815 dw_die_ref type_die;
16816 dw_loc_descr_ref cvt;
16818 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16819 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16820 if (op0 == NULL)
16821 break;
16822 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16823 && (GET_CODE (rtl) == FLOAT
16824 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16826 type_die = base_type_for_mode (int_mode,
16827 GET_CODE (rtl) == UNSIGNED_FLOAT);
16828 if (type_die == NULL)
16829 break;
16830 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16831 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16832 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16833 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16834 add_loc_descr (&op0, cvt);
16836 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16837 if (type_die == NULL)
16838 break;
16839 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16840 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16841 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16842 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16843 add_loc_descr (&op0, cvt);
16844 if (is_a <scalar_int_mode> (mode, &int_mode)
16845 && (GET_CODE (rtl) == FIX
16846 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16848 op0 = convert_descriptor_to_mode (int_mode, op0);
16849 if (op0 == NULL)
16850 break;
16852 mem_loc_result = op0;
16854 break;
16856 case CLZ:
16857 case CTZ:
16858 case FFS:
16859 if (is_a <scalar_int_mode> (mode, &int_mode))
16860 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16861 break;
16863 case POPCOUNT:
16864 case PARITY:
16865 if (is_a <scalar_int_mode> (mode, &int_mode))
16866 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16867 break;
16869 case BSWAP:
16870 if (is_a <scalar_int_mode> (mode, &int_mode))
16871 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16872 break;
16874 case ROTATE:
16875 case ROTATERT:
16876 if (is_a <scalar_int_mode> (mode, &int_mode))
16877 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16878 break;
16880 case COMPARE:
16881 /* In theory, we could implement the above. */
16882 /* DWARF cannot represent the unsigned compare operations
16883 natively. */
16884 case SS_MULT:
16885 case US_MULT:
16886 case SS_DIV:
16887 case US_DIV:
16888 case SS_PLUS:
16889 case US_PLUS:
16890 case SS_MINUS:
16891 case US_MINUS:
16892 case SS_NEG:
16893 case US_NEG:
16894 case SS_ABS:
16895 case SS_ASHIFT:
16896 case US_ASHIFT:
16897 case SS_TRUNCATE:
16898 case US_TRUNCATE:
16899 case UNORDERED:
16900 case ORDERED:
16901 case UNEQ:
16902 case UNGE:
16903 case UNGT:
16904 case UNLE:
16905 case UNLT:
16906 case LTGT:
16907 case FRACT_CONVERT:
16908 case UNSIGNED_FRACT_CONVERT:
16909 case SAT_FRACT:
16910 case UNSIGNED_SAT_FRACT:
16911 case SQRT:
16912 case ASM_OPERANDS:
16913 case VEC_MERGE:
16914 case VEC_SELECT:
16915 case VEC_CONCAT:
16916 case VEC_DUPLICATE:
16917 case VEC_SERIES:
16918 case HIGH:
16919 case FMA:
16920 case STRICT_LOW_PART:
16921 case CONST_VECTOR:
16922 case CONST_FIXED:
16923 case CLRSB:
16924 case CLOBBER:
16925 case SMUL_HIGHPART:
16926 case UMUL_HIGHPART:
16927 break;
16929 case CONST_STRING:
16930 resolve_one_addr (&rtl);
16931 goto symref;
16933 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16934 the expression. An UNSPEC rtx represents a raw DWARF operation,
16935 new_loc_descr is called for it to build the operation directly.
16936 Otherwise mem_loc_descriptor is called recursively. */
16937 case PARALLEL:
16939 int index = 0;
16940 dw_loc_descr_ref exp_result = NULL;
16942 for (; index < XVECLEN (rtl, 0); index++)
16944 rtx elem = XVECEXP (rtl, 0, index);
16945 if (GET_CODE (elem) == UNSPEC)
16947 /* Each DWARF operation UNSPEC contain two operands, if
16948 one operand is not used for the operation, const0_rtx is
16949 passed. */
16950 gcc_assert (XVECLEN (elem, 0) == 2);
16952 HOST_WIDE_INT dw_op = XINT (elem, 1);
16953 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16954 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16955 exp_result
16956 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16957 oprnd2);
16959 else
16960 exp_result
16961 = mem_loc_descriptor (elem, mode, mem_mode,
16962 VAR_INIT_STATUS_INITIALIZED);
16964 if (!mem_loc_result)
16965 mem_loc_result = exp_result;
16966 else
16967 add_loc_descr (&mem_loc_result, exp_result);
16970 break;
16973 default:
16974 if (flag_checking)
16976 print_rtl (stderr, rtl);
16977 gcc_unreachable ();
16979 break;
16982 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16983 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16985 return mem_loc_result;
16988 /* Return a descriptor that describes the concatenation of two locations.
16989 This is typically a complex variable. */
16991 static dw_loc_descr_ref
16992 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16994 /* At present we only track constant-sized pieces. */
16995 unsigned int size0, size1;
16996 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16997 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16998 return 0;
17000 dw_loc_descr_ref cc_loc_result = NULL;
17001 dw_loc_descr_ref x0_ref
17002 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17003 dw_loc_descr_ref x1_ref
17004 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17006 if (x0_ref == 0 || x1_ref == 0)
17007 return 0;
17009 cc_loc_result = x0_ref;
17010 add_loc_descr_op_piece (&cc_loc_result, size0);
17012 add_loc_descr (&cc_loc_result, x1_ref);
17013 add_loc_descr_op_piece (&cc_loc_result, size1);
17015 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
17016 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17018 return cc_loc_result;
17021 /* Return a descriptor that describes the concatenation of N
17022 locations. */
17024 static dw_loc_descr_ref
17025 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
17027 unsigned int i;
17028 dw_loc_descr_ref cc_loc_result = NULL;
17029 unsigned int n = XVECLEN (concatn, 0);
17030 unsigned int size;
17032 for (i = 0; i < n; ++i)
17034 dw_loc_descr_ref ref;
17035 rtx x = XVECEXP (concatn, 0, i);
17037 /* At present we only track constant-sized pieces. */
17038 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
17039 return NULL;
17041 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
17042 if (ref == NULL)
17043 return NULL;
17045 add_loc_descr (&cc_loc_result, ref);
17046 add_loc_descr_op_piece (&cc_loc_result, size);
17049 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
17050 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
17052 return cc_loc_result;
17055 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
17056 for DEBUG_IMPLICIT_PTR RTL. */
17058 static dw_loc_descr_ref
17059 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
17061 dw_loc_descr_ref ret;
17062 dw_die_ref ref;
17064 if (dwarf_strict && dwarf_version < 5)
17065 return NULL;
17066 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
17067 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
17068 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
17069 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
17070 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
17071 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
17072 if (ref)
17074 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17075 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17076 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17078 else
17080 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17081 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
17083 return ret;
17086 /* Output a proper Dwarf location descriptor for a variable or parameter
17087 which is either allocated in a register or in a memory location. For a
17088 register, we just generate an OP_REG and the register number. For a
17089 memory location we provide a Dwarf postfix expression describing how to
17090 generate the (dynamic) address of the object onto the address stack.
17092 MODE is mode of the decl if this loc_descriptor is going to be used in
17093 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
17094 allowed, VOIDmode otherwise.
17096 If we don't know how to describe it, return 0. */
17098 static dw_loc_descr_ref
17099 loc_descriptor (rtx rtl, machine_mode mode,
17100 enum var_init_status initialized)
17102 dw_loc_descr_ref loc_result = NULL;
17103 scalar_int_mode int_mode;
17105 switch (GET_CODE (rtl))
17107 case SUBREG:
17108 /* The case of a subreg may arise when we have a local (register)
17109 variable or a formal (register) parameter which doesn't quite fill
17110 up an entire register. For now, just assume that it is
17111 legitimate to make the Dwarf info refer to the whole register which
17112 contains the given subreg. */
17113 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17114 loc_result = loc_descriptor (SUBREG_REG (rtl),
17115 GET_MODE (SUBREG_REG (rtl)), initialized);
17116 else
17117 goto do_default;
17118 break;
17120 case REG:
17121 loc_result = reg_loc_descriptor (rtl, initialized);
17122 break;
17124 case MEM:
17125 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17126 GET_MODE (rtl), initialized);
17127 if (loc_result == NULL)
17128 loc_result = tls_mem_loc_descriptor (rtl);
17129 if (loc_result == NULL)
17131 rtx new_rtl = avoid_constant_pool_reference (rtl);
17132 if (new_rtl != rtl)
17133 loc_result = loc_descriptor (new_rtl, mode, initialized);
17135 break;
17137 case CONCAT:
17138 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17139 initialized);
17140 break;
17142 case CONCATN:
17143 loc_result = concatn_loc_descriptor (rtl, initialized);
17144 break;
17146 case VAR_LOCATION:
17147 /* Single part. */
17148 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17150 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17151 if (GET_CODE (loc) == EXPR_LIST)
17152 loc = XEXP (loc, 0);
17153 loc_result = loc_descriptor (loc, mode, initialized);
17154 break;
17157 rtl = XEXP (rtl, 1);
17158 /* FALLTHRU */
17160 case PARALLEL:
17162 rtvec par_elems = XVEC (rtl, 0);
17163 int num_elem = GET_NUM_ELEM (par_elems);
17164 machine_mode mode;
17165 int i, size;
17167 /* Create the first one, so we have something to add to. */
17168 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17169 VOIDmode, initialized);
17170 if (loc_result == NULL)
17171 return NULL;
17172 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17173 /* At present we only track constant-sized pieces. */
17174 if (!GET_MODE_SIZE (mode).is_constant (&size))
17175 return NULL;
17176 add_loc_descr_op_piece (&loc_result, size);
17177 for (i = 1; i < num_elem; i++)
17179 dw_loc_descr_ref temp;
17181 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17182 VOIDmode, initialized);
17183 if (temp == NULL)
17184 return NULL;
17185 add_loc_descr (&loc_result, temp);
17186 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17187 /* At present we only track constant-sized pieces. */
17188 if (!GET_MODE_SIZE (mode).is_constant (&size))
17189 return NULL;
17190 add_loc_descr_op_piece (&loc_result, size);
17193 break;
17195 case CONST_INT:
17196 if (mode != VOIDmode && mode != BLKmode)
17198 int_mode = as_a <scalar_int_mode> (mode);
17199 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17200 INTVAL (rtl));
17202 break;
17204 case CONST_DOUBLE:
17205 if (mode == VOIDmode)
17206 mode = GET_MODE (rtl);
17208 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17210 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17212 /* Note that a CONST_DOUBLE rtx could represent either an integer
17213 or a floating-point constant. A CONST_DOUBLE is used whenever
17214 the constant requires more than one word in order to be
17215 adequately represented. We output CONST_DOUBLEs as blocks. */
17216 scalar_mode smode = as_a <scalar_mode> (mode);
17217 loc_result = new_loc_descr (DW_OP_implicit_value,
17218 GET_MODE_SIZE (smode), 0);
17219 #if TARGET_SUPPORTS_WIDE_INT == 0
17220 if (!SCALAR_FLOAT_MODE_P (smode))
17222 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17223 loc_result->dw_loc_oprnd2.v.val_double
17224 = rtx_to_double_int (rtl);
17226 else
17227 #endif
17229 unsigned int length = GET_MODE_SIZE (smode);
17230 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17231 unsigned int elt_size = insert_float (rtl, array);
17233 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17234 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17235 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17236 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17239 break;
17241 case CONST_WIDE_INT:
17242 if (mode == VOIDmode)
17243 mode = GET_MODE (rtl);
17245 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17247 int_mode = as_a <scalar_int_mode> (mode);
17248 loc_result = new_loc_descr (DW_OP_implicit_value,
17249 GET_MODE_SIZE (int_mode), 0);
17250 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17251 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17252 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17254 break;
17256 case CONST_VECTOR:
17257 if (mode == VOIDmode)
17258 mode = GET_MODE (rtl);
17260 if (mode != VOIDmode
17261 /* The combination of a length and byte elt_size doesn't extend
17262 naturally to boolean vectors, where several elements are packed
17263 into the same byte. */
17264 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17265 && (dwarf_version >= 4 || !dwarf_strict))
17267 unsigned int length;
17268 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17269 return NULL;
17271 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17272 unsigned char *array
17273 = ggc_vec_alloc<unsigned char> (length * elt_size);
17274 unsigned int i;
17275 unsigned char *p;
17276 machine_mode imode = GET_MODE_INNER (mode);
17278 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17279 switch (GET_MODE_CLASS (mode))
17281 case MODE_VECTOR_INT:
17282 for (i = 0, p = array; i < length; i++, p += elt_size)
17284 rtx elt = CONST_VECTOR_ELT (rtl, i);
17285 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17287 break;
17289 case MODE_VECTOR_FLOAT:
17290 for (i = 0, p = array; i < length; i++, p += elt_size)
17292 rtx elt = CONST_VECTOR_ELT (rtl, i);
17293 insert_float (elt, p);
17295 break;
17297 default:
17298 gcc_unreachable ();
17301 loc_result = new_loc_descr (DW_OP_implicit_value,
17302 length * elt_size, 0);
17303 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17304 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17305 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17306 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17308 break;
17310 case CONST:
17311 if (mode == VOIDmode
17312 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17313 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17314 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17316 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17317 break;
17319 /* FALLTHROUGH */
17320 case SYMBOL_REF:
17321 if (!const_ok_for_output (rtl))
17322 break;
17323 /* FALLTHROUGH */
17324 case LABEL_REF:
17325 if (is_a <scalar_int_mode> (mode, &int_mode)
17326 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17327 && (dwarf_version >= 4 || !dwarf_strict))
17329 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17330 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17331 vec_safe_push (used_rtx_array, rtl);
17333 break;
17335 case DEBUG_IMPLICIT_PTR:
17336 loc_result = implicit_ptr_descriptor (rtl, 0);
17337 break;
17339 case PLUS:
17340 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17341 && CONST_INT_P (XEXP (rtl, 1)))
17343 loc_result
17344 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17345 break;
17347 /* FALLTHRU */
17348 do_default:
17349 default:
17350 if ((is_a <scalar_int_mode> (mode, &int_mode)
17351 && GET_MODE (rtl) == int_mode
17352 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17353 && dwarf_version >= 4)
17354 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17356 /* Value expression. */
17357 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17358 if (loc_result)
17359 add_loc_descr (&loc_result,
17360 new_loc_descr (DW_OP_stack_value, 0, 0));
17362 break;
17365 return loc_result;
17368 /* We need to figure out what section we should use as the base for the
17369 address ranges where a given location is valid.
17370 1. If this particular DECL has a section associated with it, use that.
17371 2. If this function has a section associated with it, use that.
17372 3. Otherwise, use the text section.
17373 XXX: If you split a variable across multiple sections, we won't notice. */
17375 static const char *
17376 secname_for_decl (const_tree decl)
17378 const char *secname;
17380 if (VAR_OR_FUNCTION_DECL_P (decl)
17381 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17382 && DECL_SECTION_NAME (decl))
17383 secname = DECL_SECTION_NAME (decl);
17384 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17386 if (in_cold_section_p)
17388 section *sec = current_function_section ();
17389 if (sec->common.flags & SECTION_NAMED)
17390 return sec->named.name;
17392 secname = DECL_SECTION_NAME (current_function_decl);
17394 else if (cfun && in_cold_section_p)
17395 secname = crtl->subsections.cold_section_label;
17396 else
17397 secname = text_section_label;
17399 return secname;
17402 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17404 static bool
17405 decl_by_reference_p (tree decl)
17407 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17408 || VAR_P (decl))
17409 && DECL_BY_REFERENCE (decl));
17412 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17413 for VARLOC. */
17415 static dw_loc_descr_ref
17416 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17417 enum var_init_status initialized)
17419 int have_address = 0;
17420 dw_loc_descr_ref descr;
17421 machine_mode mode;
17423 if (want_address != 2)
17425 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17426 /* Single part. */
17427 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17429 varloc = PAT_VAR_LOCATION_LOC (varloc);
17430 if (GET_CODE (varloc) == EXPR_LIST)
17431 varloc = XEXP (varloc, 0);
17432 mode = GET_MODE (varloc);
17433 if (MEM_P (varloc))
17435 rtx addr = XEXP (varloc, 0);
17436 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17437 mode, initialized);
17438 if (descr)
17439 have_address = 1;
17440 else
17442 rtx x = avoid_constant_pool_reference (varloc);
17443 if (x != varloc)
17444 descr = mem_loc_descriptor (x, mode, VOIDmode,
17445 initialized);
17448 else
17449 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17451 else
17452 return 0;
17454 else
17456 if (GET_CODE (varloc) == VAR_LOCATION)
17457 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17458 else
17459 mode = DECL_MODE (loc);
17460 descr = loc_descriptor (varloc, mode, initialized);
17461 have_address = 1;
17464 if (!descr)
17465 return 0;
17467 if (want_address == 2 && !have_address
17468 && (dwarf_version >= 4 || !dwarf_strict))
17470 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17472 expansion_failed (loc, NULL_RTX,
17473 "DWARF address size mismatch");
17474 return 0;
17476 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17477 have_address = 1;
17479 /* Show if we can't fill the request for an address. */
17480 if (want_address && !have_address)
17482 expansion_failed (loc, NULL_RTX,
17483 "Want address and only have value");
17484 return 0;
17487 /* If we've got an address and don't want one, dereference. */
17488 if (!want_address && have_address)
17490 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17491 enum dwarf_location_atom op;
17493 if (size > DWARF2_ADDR_SIZE || size == -1)
17495 expansion_failed (loc, NULL_RTX,
17496 "DWARF address size mismatch");
17497 return 0;
17499 else if (size == DWARF2_ADDR_SIZE)
17500 op = DW_OP_deref;
17501 else
17502 op = DW_OP_deref_size;
17504 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17507 return descr;
17510 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17511 if it is not possible. */
17513 static dw_loc_descr_ref
17514 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17516 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17517 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17518 else if (dwarf_version >= 3 || !dwarf_strict)
17519 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17520 else
17521 return NULL;
17524 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17525 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17527 static dw_loc_descr_ref
17528 dw_sra_loc_expr (tree decl, rtx loc)
17530 rtx p;
17531 unsigned HOST_WIDE_INT padsize = 0;
17532 dw_loc_descr_ref descr, *descr_tail;
17533 unsigned HOST_WIDE_INT decl_size;
17534 rtx varloc;
17535 enum var_init_status initialized;
17537 if (DECL_SIZE (decl) == NULL
17538 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17539 return NULL;
17541 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17542 descr = NULL;
17543 descr_tail = &descr;
17545 for (p = loc; p; p = XEXP (p, 1))
17547 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17548 rtx loc_note = *decl_piece_varloc_ptr (p);
17549 dw_loc_descr_ref cur_descr;
17550 dw_loc_descr_ref *tail, last = NULL;
17551 unsigned HOST_WIDE_INT opsize = 0;
17553 if (loc_note == NULL_RTX
17554 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17556 padsize += bitsize;
17557 continue;
17559 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17560 varloc = NOTE_VAR_LOCATION (loc_note);
17561 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17562 if (cur_descr == NULL)
17564 padsize += bitsize;
17565 continue;
17568 /* Check that cur_descr either doesn't use
17569 DW_OP_*piece operations, or their sum is equal
17570 to bitsize. Otherwise we can't embed it. */
17571 for (tail = &cur_descr; *tail != NULL;
17572 tail = &(*tail)->dw_loc_next)
17573 if ((*tail)->dw_loc_opc == DW_OP_piece)
17575 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17576 * BITS_PER_UNIT;
17577 last = *tail;
17579 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17581 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17582 last = *tail;
17585 if (last != NULL && opsize != bitsize)
17587 padsize += bitsize;
17588 /* Discard the current piece of the descriptor and release any
17589 addr_table entries it uses. */
17590 remove_loc_list_addr_table_entries (cur_descr);
17591 continue;
17594 /* If there is a hole, add DW_OP_*piece after empty DWARF
17595 expression, which means that those bits are optimized out. */
17596 if (padsize)
17598 if (padsize > decl_size)
17600 remove_loc_list_addr_table_entries (cur_descr);
17601 goto discard_descr;
17603 decl_size -= padsize;
17604 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17605 if (*descr_tail == NULL)
17607 remove_loc_list_addr_table_entries (cur_descr);
17608 goto discard_descr;
17610 descr_tail = &(*descr_tail)->dw_loc_next;
17611 padsize = 0;
17613 *descr_tail = cur_descr;
17614 descr_tail = tail;
17615 if (bitsize > decl_size)
17616 goto discard_descr;
17617 decl_size -= bitsize;
17618 if (last == NULL)
17620 HOST_WIDE_INT offset = 0;
17621 if (GET_CODE (varloc) == VAR_LOCATION
17622 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17624 varloc = PAT_VAR_LOCATION_LOC (varloc);
17625 if (GET_CODE (varloc) == EXPR_LIST)
17626 varloc = XEXP (varloc, 0);
17630 if (GET_CODE (varloc) == CONST
17631 || GET_CODE (varloc) == SIGN_EXTEND
17632 || GET_CODE (varloc) == ZERO_EXTEND)
17633 varloc = XEXP (varloc, 0);
17634 else if (GET_CODE (varloc) == SUBREG)
17635 varloc = SUBREG_REG (varloc);
17636 else
17637 break;
17639 while (1);
17640 /* DW_OP_bit_size offset should be zero for register
17641 or implicit location descriptions and empty location
17642 descriptions, but for memory addresses needs big endian
17643 adjustment. */
17644 if (MEM_P (varloc))
17646 unsigned HOST_WIDE_INT memsize;
17647 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17648 goto discard_descr;
17649 memsize *= BITS_PER_UNIT;
17650 if (memsize != bitsize)
17652 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17653 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17654 goto discard_descr;
17655 if (memsize < bitsize)
17656 goto discard_descr;
17657 if (BITS_BIG_ENDIAN)
17658 offset = memsize - bitsize;
17662 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17663 if (*descr_tail == NULL)
17664 goto discard_descr;
17665 descr_tail = &(*descr_tail)->dw_loc_next;
17669 /* If there were any non-empty expressions, add padding till the end of
17670 the decl. */
17671 if (descr != NULL && decl_size != 0)
17673 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17674 if (*descr_tail == NULL)
17675 goto discard_descr;
17677 return descr;
17679 discard_descr:
17680 /* Discard the descriptor and release any addr_table entries it uses. */
17681 remove_loc_list_addr_table_entries (descr);
17682 return NULL;
17685 /* Return the dwarf representation of the location list LOC_LIST of
17686 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17687 function. */
17689 static dw_loc_list_ref
17690 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17692 const char *endname, *secname;
17693 var_loc_view endview;
17694 rtx varloc;
17695 enum var_init_status initialized;
17696 struct var_loc_node *node;
17697 dw_loc_descr_ref descr;
17698 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17699 dw_loc_list_ref list = NULL;
17700 dw_loc_list_ref *listp = &list;
17702 /* Now that we know what section we are using for a base,
17703 actually construct the list of locations.
17704 The first location information is what is passed to the
17705 function that creates the location list, and the remaining
17706 locations just get added on to that list.
17707 Note that we only know the start address for a location
17708 (IE location changes), so to build the range, we use
17709 the range [current location start, next location start].
17710 This means we have to special case the last node, and generate
17711 a range of [last location start, end of function label]. */
17713 if (cfun && crtl->has_bb_partition)
17715 bool save_in_cold_section_p = in_cold_section_p;
17716 in_cold_section_p = first_function_block_is_cold;
17717 if (loc_list->last_before_switch == NULL)
17718 in_cold_section_p = !in_cold_section_p;
17719 secname = secname_for_decl (decl);
17720 in_cold_section_p = save_in_cold_section_p;
17722 else
17723 secname = secname_for_decl (decl);
17725 for (node = loc_list->first; node; node = node->next)
17727 bool range_across_switch = false;
17728 if (GET_CODE (node->loc) == EXPR_LIST
17729 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17731 if (GET_CODE (node->loc) == EXPR_LIST)
17733 descr = NULL;
17734 /* This requires DW_OP_{,bit_}piece, which is not usable
17735 inside DWARF expressions. */
17736 if (want_address == 2)
17737 descr = dw_sra_loc_expr (decl, node->loc);
17739 else
17741 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17742 varloc = NOTE_VAR_LOCATION (node->loc);
17743 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17745 if (descr)
17747 /* If section switch happens in between node->label
17748 and node->next->label (or end of function) and
17749 we can't emit it as a single entry list,
17750 emit two ranges, first one ending at the end
17751 of first partition and second one starting at the
17752 beginning of second partition. */
17753 if (node == loc_list->last_before_switch
17754 && (node != loc_list->first || loc_list->first->next
17755 /* If we are to emit a view number, we will emit
17756 a loclist rather than a single location
17757 expression for the entire function (see
17758 loc_list_has_views), so we have to split the
17759 range that straddles across partitions. */
17760 || !ZERO_VIEW_P (node->view))
17761 && current_function_decl)
17763 endname = cfun->fde->dw_fde_end;
17764 endview = 0;
17765 range_across_switch = true;
17767 /* The variable has a location between NODE->LABEL and
17768 NODE->NEXT->LABEL. */
17769 else if (node->next)
17770 endname = node->next->label, endview = node->next->view;
17771 /* If the variable has a location at the last label
17772 it keeps its location until the end of function. */
17773 else if (!current_function_decl)
17774 endname = text_end_label, endview = 0;
17775 else
17777 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17778 current_function_funcdef_no);
17779 endname = ggc_strdup (label_id);
17780 endview = 0;
17783 *listp = new_loc_list (descr, node->label, node->view,
17784 endname, endview, secname);
17785 if (TREE_CODE (decl) == PARM_DECL
17786 && node == loc_list->first
17787 && NOTE_P (node->loc)
17788 && strcmp (node->label, endname) == 0)
17789 (*listp)->force = true;
17790 listp = &(*listp)->dw_loc_next;
17794 if (cfun
17795 && crtl->has_bb_partition
17796 && node == loc_list->last_before_switch)
17798 bool save_in_cold_section_p = in_cold_section_p;
17799 in_cold_section_p = !first_function_block_is_cold;
17800 secname = secname_for_decl (decl);
17801 in_cold_section_p = save_in_cold_section_p;
17804 if (range_across_switch)
17806 if (GET_CODE (node->loc) == EXPR_LIST)
17807 descr = dw_sra_loc_expr (decl, node->loc);
17808 else
17810 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17811 varloc = NOTE_VAR_LOCATION (node->loc);
17812 descr = dw_loc_list_1 (decl, varloc, want_address,
17813 initialized);
17815 gcc_assert (descr);
17816 /* The variable has a location between NODE->LABEL and
17817 NODE->NEXT->LABEL. */
17818 if (node->next)
17819 endname = node->next->label, endview = node->next->view;
17820 else
17821 endname = cfun->fde->dw_fde_second_end, endview = 0;
17822 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17823 endname, endview, secname);
17824 listp = &(*listp)->dw_loc_next;
17828 /* Try to avoid the overhead of a location list emitting a location
17829 expression instead, but only if we didn't have more than one
17830 location entry in the first place. If some entries were not
17831 representable, we don't want to pretend a single entry that was
17832 applies to the entire scope in which the variable is
17833 available. */
17834 if (list && loc_list->first->next)
17835 gen_llsym (list);
17836 else
17837 maybe_gen_llsym (list);
17839 return list;
17842 /* Return if the loc_list has only single element and thus can be represented
17843 as location description. */
17845 static bool
17846 single_element_loc_list_p (dw_loc_list_ref list)
17848 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17849 return !list->ll_symbol;
17852 /* Duplicate a single element of location list. */
17854 static inline dw_loc_descr_ref
17855 copy_loc_descr (dw_loc_descr_ref ref)
17857 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17858 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17859 return copy;
17862 /* To each location in list LIST append loc descr REF. */
17864 static void
17865 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17867 dw_loc_descr_ref copy;
17868 add_loc_descr (&list->expr, ref);
17869 list = list->dw_loc_next;
17870 while (list)
17872 copy = copy_loc_descr (ref);
17873 add_loc_descr (&list->expr, copy);
17874 while (copy->dw_loc_next)
17875 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17876 list = list->dw_loc_next;
17880 /* To each location in list LIST prepend loc descr REF. */
17882 static void
17883 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17885 dw_loc_descr_ref copy;
17886 dw_loc_descr_ref ref_end = list->expr;
17887 add_loc_descr (&ref, list->expr);
17888 list->expr = ref;
17889 list = list->dw_loc_next;
17890 while (list)
17892 dw_loc_descr_ref end = list->expr;
17893 list->expr = copy = copy_loc_descr (ref);
17894 while (copy->dw_loc_next != ref_end)
17895 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17896 copy->dw_loc_next = end;
17897 list = list->dw_loc_next;
17901 /* Given two lists RET and LIST
17902 produce location list that is result of adding expression in LIST
17903 to expression in RET on each position in program.
17904 Might be destructive on both RET and LIST.
17906 TODO: We handle only simple cases of RET or LIST having at most one
17907 element. General case would involve sorting the lists in program order
17908 and merging them that will need some additional work.
17909 Adding that will improve quality of debug info especially for SRA-ed
17910 structures. */
17912 static void
17913 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17915 if (!list)
17916 return;
17917 if (!*ret)
17919 *ret = list;
17920 return;
17922 if (!list->dw_loc_next)
17924 add_loc_descr_to_each (*ret, list->expr);
17925 return;
17927 if (!(*ret)->dw_loc_next)
17929 prepend_loc_descr_to_each (list, (*ret)->expr);
17930 *ret = list;
17931 return;
17933 expansion_failed (NULL_TREE, NULL_RTX,
17934 "Don't know how to merge two non-trivial"
17935 " location lists.\n");
17936 *ret = NULL;
17937 return;
17940 /* LOC is constant expression. Try a luck, look it up in constant
17941 pool and return its loc_descr of its address. */
17943 static dw_loc_descr_ref
17944 cst_pool_loc_descr (tree loc)
17946 /* Get an RTL for this, if something has been emitted. */
17947 rtx rtl = lookup_constant_def (loc);
17949 if (!rtl || !MEM_P (rtl))
17951 gcc_assert (!rtl);
17952 return 0;
17954 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17956 /* TODO: We might get more coverage if we was actually delaying expansion
17957 of all expressions till end of compilation when constant pools are fully
17958 populated. */
17959 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17961 expansion_failed (loc, NULL_RTX,
17962 "CST value in contant pool but not marked.");
17963 return 0;
17965 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17966 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17969 /* Return dw_loc_list representing address of addr_expr LOC
17970 by looking for inner INDIRECT_REF expression and turning
17971 it into simple arithmetics.
17973 See loc_list_from_tree for the meaning of CONTEXT. */
17975 static dw_loc_list_ref
17976 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17977 loc_descr_context *context)
17979 tree obj, offset;
17980 poly_int64 bitsize, bitpos, bytepos;
17981 machine_mode mode;
17982 int unsignedp, reversep, volatilep = 0;
17983 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17985 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17986 &bitsize, &bitpos, &offset, &mode,
17987 &unsignedp, &reversep, &volatilep);
17988 STRIP_NOPS (obj);
17989 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17991 expansion_failed (loc, NULL_RTX, "bitfield access");
17992 return 0;
17994 if (!INDIRECT_REF_P (obj))
17996 expansion_failed (obj,
17997 NULL_RTX, "no indirect ref in inner refrence");
17998 return 0;
18000 if (!offset && known_eq (bitpos, 0))
18001 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
18002 context);
18003 else if (toplev
18004 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
18005 && (dwarf_version >= 4 || !dwarf_strict))
18007 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
18008 if (!list_ret)
18009 return 0;
18010 if (offset)
18012 /* Variable offset. */
18013 list_ret1 = loc_list_from_tree (offset, 0, context);
18014 if (list_ret1 == 0)
18015 return 0;
18016 add_loc_list (&list_ret, list_ret1);
18017 if (!list_ret)
18018 return 0;
18019 add_loc_descr_to_each (list_ret,
18020 new_loc_descr (DW_OP_plus, 0, 0));
18022 HOST_WIDE_INT value;
18023 if (bytepos.is_constant (&value) && value > 0)
18024 add_loc_descr_to_each (list_ret,
18025 new_loc_descr (DW_OP_plus_uconst, value, 0));
18026 else if (maybe_ne (bytepos, 0))
18027 loc_list_plus_const (list_ret, bytepos);
18028 add_loc_descr_to_each (list_ret,
18029 new_loc_descr (DW_OP_stack_value, 0, 0));
18031 return list_ret;
18034 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
18035 all operations from LOC are nops, move to the last one. Insert in NOPS all
18036 operations that are skipped. */
18038 static void
18039 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
18040 hash_set<dw_loc_descr_ref> &nops)
18042 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
18044 nops.add (loc);
18045 loc = loc->dw_loc_next;
18049 /* Helper for loc_descr_without_nops: free the location description operation
18050 P. */
18052 bool
18053 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
18055 ggc_free (loc);
18056 return true;
18059 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
18060 finishes LOC. */
18062 static void
18063 loc_descr_without_nops (dw_loc_descr_ref &loc)
18065 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
18066 return;
18068 /* Set of all DW_OP_nop operations we remove. */
18069 hash_set<dw_loc_descr_ref> nops;
18071 /* First, strip all prefix NOP operations in order to keep the head of the
18072 operations list. */
18073 loc_descr_to_next_no_nop (loc, nops);
18075 for (dw_loc_descr_ref cur = loc; cur != NULL;)
18077 /* For control flow operations: strip "prefix" nops in destination
18078 labels. */
18079 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
18080 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
18081 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
18082 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
18084 /* Do the same for the operations that follow, then move to the next
18085 iteration. */
18086 if (cur->dw_loc_next != NULL)
18087 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
18088 cur = cur->dw_loc_next;
18091 nops.traverse<void *, free_loc_descr> (NULL);
18095 struct dwarf_procedure_info;
18097 /* Helper structure for location descriptions generation. */
18098 struct loc_descr_context
18100 /* The type that is implicitly referenced by DW_OP_push_object_address, or
18101 NULL_TREE if DW_OP_push_object_address in invalid for this location
18102 description. This is used when processing PLACEHOLDER_EXPR nodes. */
18103 tree context_type;
18104 /* The ..._DECL node that should be translated as a
18105 DW_OP_push_object_address operation. */
18106 tree base_decl;
18107 /* Information about the DWARF procedure we are currently generating. NULL if
18108 we are not generating a DWARF procedure. */
18109 struct dwarf_procedure_info *dpi;
18110 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18111 by consumer. Used for DW_TAG_generic_subrange attributes. */
18112 bool placeholder_arg;
18113 /* True if PLACEHOLDER_EXPR has been seen. */
18114 bool placeholder_seen;
18115 /* True if strict preservation of signedness has been requested. */
18116 bool strict_signedness;
18119 /* DWARF procedures generation
18121 DWARF expressions (aka. location descriptions) are used to encode variable
18122 things such as sizes or offsets. Such computations can have redundant parts
18123 that can be factorized in order to reduce the size of the output debug
18124 information. This is the whole point of DWARF procedures.
18126 Thanks to stor-layout.cc, size and offset expressions in GENERIC trees are
18127 already factorized into functions ("size functions") in order to handle very
18128 big and complex types. Such functions are quite simple: they have integral
18129 arguments, they return an integral result and their body contains only a
18130 return statement with arithmetic expressions. This is the only kind of
18131 function we are interested in translating into DWARF procedures, here.
18133 DWARF expressions and DWARF procedure are executed using a stack, so we have
18134 to define some calling convention for them to interact. Let's say that:
18136 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18137 all arguments in reverse order (right-to-left) so that when the DWARF
18138 procedure execution starts, the first argument is the top of the stack.
18140 - Then, when returning, the DWARF procedure must have consumed all arguments
18141 on the stack, must have pushed the result and touched nothing else.
18143 - Each integral argument and the result are integral types can be hold in a
18144 single stack slot.
18146 - We call "frame offset" the number of stack slots that are "under DWARF
18147 procedure control": it includes the arguments slots, the temporaries and
18148 the result slot. Thus, it is equal to the number of arguments when the
18149 procedure execution starts and must be equal to one (the result) when it
18150 returns. */
18152 /* Helper structure used when generating operations for a DWARF procedure. */
18153 struct dwarf_procedure_info
18155 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18156 currently translated. */
18157 tree fndecl;
18158 /* The number of arguments FNDECL takes. */
18159 unsigned args_count;
18162 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18163 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18164 equate it to this DIE. */
18166 static dw_die_ref
18167 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18168 dw_die_ref parent_die)
18170 dw_die_ref dwarf_proc_die;
18172 if ((dwarf_version < 3 && dwarf_strict)
18173 || location == NULL)
18174 return NULL;
18176 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18177 if (fndecl)
18178 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18179 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18180 return dwarf_proc_die;
18183 /* Return whether TYPE is a supported type as a DWARF procedure argument
18184 type or return type (we handle only scalar types and pointer types that
18185 aren't wider than the DWARF expression evaluation stack). */
18187 static bool
18188 is_handled_procedure_type (tree type)
18190 return ((INTEGRAL_TYPE_P (type)
18191 || TREE_CODE (type) == OFFSET_TYPE
18192 || TREE_CODE (type) == POINTER_TYPE)
18193 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18196 /* Helper for resolve_args_picking: do the same but stop when coming across
18197 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18198 offset *before* evaluating the corresponding operation. */
18200 static bool
18201 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18202 struct dwarf_procedure_info *dpi,
18203 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18205 /* The "frame_offset" identifier is already used to name a macro... */
18206 unsigned frame_offset_ = initial_frame_offset;
18207 dw_loc_descr_ref l;
18209 for (l = loc; l != NULL;)
18211 bool existed;
18212 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18214 /* If we already met this node, there is nothing to compute anymore. */
18215 if (existed)
18217 /* Make sure that the stack size is consistent wherever the execution
18218 flow comes from. */
18219 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18220 break;
18222 l_frame_offset = frame_offset_;
18224 /* If needed, relocate the picking offset with respect to the frame
18225 offset. */
18226 if (l->frame_offset_rel)
18228 unsigned HOST_WIDE_INT off;
18229 switch (l->dw_loc_opc)
18231 case DW_OP_pick:
18232 off = l->dw_loc_oprnd1.v.val_unsigned;
18233 break;
18234 case DW_OP_dup:
18235 off = 0;
18236 break;
18237 case DW_OP_over:
18238 off = 1;
18239 break;
18240 default:
18241 gcc_unreachable ();
18243 /* frame_offset_ is the size of the current stack frame, including
18244 incoming arguments. Besides, the arguments are pushed
18245 right-to-left. Thus, in order to access the Nth argument from
18246 this operation node, the picking has to skip temporaries *plus*
18247 one stack slot per argument (0 for the first one, 1 for the second
18248 one, etc.).
18250 The targetted argument number (N) is already set as the operand,
18251 and the number of temporaries can be computed with:
18252 frame_offsets_ - dpi->args_count */
18253 off += frame_offset_ - dpi->args_count;
18255 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18256 if (off > 255)
18257 return false;
18259 if (off == 0)
18261 l->dw_loc_opc = DW_OP_dup;
18262 l->dw_loc_oprnd1.v.val_unsigned = 0;
18264 else if (off == 1)
18266 l->dw_loc_opc = DW_OP_over;
18267 l->dw_loc_oprnd1.v.val_unsigned = 0;
18269 else
18271 l->dw_loc_opc = DW_OP_pick;
18272 l->dw_loc_oprnd1.v.val_unsigned = off;
18276 /* Update frame_offset according to the effect the current operation has
18277 on the stack. */
18278 switch (l->dw_loc_opc)
18280 case DW_OP_deref:
18281 case DW_OP_swap:
18282 case DW_OP_rot:
18283 case DW_OP_abs:
18284 case DW_OP_neg:
18285 case DW_OP_not:
18286 case DW_OP_plus_uconst:
18287 case DW_OP_skip:
18288 case DW_OP_reg0:
18289 case DW_OP_reg1:
18290 case DW_OP_reg2:
18291 case DW_OP_reg3:
18292 case DW_OP_reg4:
18293 case DW_OP_reg5:
18294 case DW_OP_reg6:
18295 case DW_OP_reg7:
18296 case DW_OP_reg8:
18297 case DW_OP_reg9:
18298 case DW_OP_reg10:
18299 case DW_OP_reg11:
18300 case DW_OP_reg12:
18301 case DW_OP_reg13:
18302 case DW_OP_reg14:
18303 case DW_OP_reg15:
18304 case DW_OP_reg16:
18305 case DW_OP_reg17:
18306 case DW_OP_reg18:
18307 case DW_OP_reg19:
18308 case DW_OP_reg20:
18309 case DW_OP_reg21:
18310 case DW_OP_reg22:
18311 case DW_OP_reg23:
18312 case DW_OP_reg24:
18313 case DW_OP_reg25:
18314 case DW_OP_reg26:
18315 case DW_OP_reg27:
18316 case DW_OP_reg28:
18317 case DW_OP_reg29:
18318 case DW_OP_reg30:
18319 case DW_OP_reg31:
18320 case DW_OP_bregx:
18321 case DW_OP_piece:
18322 case DW_OP_deref_size:
18323 case DW_OP_nop:
18324 case DW_OP_bit_piece:
18325 case DW_OP_implicit_value:
18326 case DW_OP_stack_value:
18327 case DW_OP_deref_type:
18328 case DW_OP_convert:
18329 case DW_OP_reinterpret:
18330 case DW_OP_GNU_deref_type:
18331 case DW_OP_GNU_convert:
18332 case DW_OP_GNU_reinterpret:
18333 break;
18335 case DW_OP_addr:
18336 case DW_OP_const1u:
18337 case DW_OP_const1s:
18338 case DW_OP_const2u:
18339 case DW_OP_const2s:
18340 case DW_OP_const4u:
18341 case DW_OP_const4s:
18342 case DW_OP_const8u:
18343 case DW_OP_const8s:
18344 case DW_OP_constu:
18345 case DW_OP_consts:
18346 case DW_OP_dup:
18347 case DW_OP_over:
18348 case DW_OP_pick:
18349 case DW_OP_lit0:
18350 case DW_OP_lit1:
18351 case DW_OP_lit2:
18352 case DW_OP_lit3:
18353 case DW_OP_lit4:
18354 case DW_OP_lit5:
18355 case DW_OP_lit6:
18356 case DW_OP_lit7:
18357 case DW_OP_lit8:
18358 case DW_OP_lit9:
18359 case DW_OP_lit10:
18360 case DW_OP_lit11:
18361 case DW_OP_lit12:
18362 case DW_OP_lit13:
18363 case DW_OP_lit14:
18364 case DW_OP_lit15:
18365 case DW_OP_lit16:
18366 case DW_OP_lit17:
18367 case DW_OP_lit18:
18368 case DW_OP_lit19:
18369 case DW_OP_lit20:
18370 case DW_OP_lit21:
18371 case DW_OP_lit22:
18372 case DW_OP_lit23:
18373 case DW_OP_lit24:
18374 case DW_OP_lit25:
18375 case DW_OP_lit26:
18376 case DW_OP_lit27:
18377 case DW_OP_lit28:
18378 case DW_OP_lit29:
18379 case DW_OP_lit30:
18380 case DW_OP_lit31:
18381 case DW_OP_breg0:
18382 case DW_OP_breg1:
18383 case DW_OP_breg2:
18384 case DW_OP_breg3:
18385 case DW_OP_breg4:
18386 case DW_OP_breg5:
18387 case DW_OP_breg6:
18388 case DW_OP_breg7:
18389 case DW_OP_breg8:
18390 case DW_OP_breg9:
18391 case DW_OP_breg10:
18392 case DW_OP_breg11:
18393 case DW_OP_breg12:
18394 case DW_OP_breg13:
18395 case DW_OP_breg14:
18396 case DW_OP_breg15:
18397 case DW_OP_breg16:
18398 case DW_OP_breg17:
18399 case DW_OP_breg18:
18400 case DW_OP_breg19:
18401 case DW_OP_breg20:
18402 case DW_OP_breg21:
18403 case DW_OP_breg22:
18404 case DW_OP_breg23:
18405 case DW_OP_breg24:
18406 case DW_OP_breg25:
18407 case DW_OP_breg26:
18408 case DW_OP_breg27:
18409 case DW_OP_breg28:
18410 case DW_OP_breg29:
18411 case DW_OP_breg30:
18412 case DW_OP_breg31:
18413 case DW_OP_fbreg:
18414 case DW_OP_push_object_address:
18415 case DW_OP_call_frame_cfa:
18416 case DW_OP_GNU_variable_value:
18417 case DW_OP_GNU_addr_index:
18418 case DW_OP_GNU_const_index:
18419 ++frame_offset_;
18420 break;
18422 case DW_OP_drop:
18423 case DW_OP_xderef:
18424 case DW_OP_and:
18425 case DW_OP_div:
18426 case DW_OP_minus:
18427 case DW_OP_mod:
18428 case DW_OP_mul:
18429 case DW_OP_or:
18430 case DW_OP_plus:
18431 case DW_OP_shl:
18432 case DW_OP_shr:
18433 case DW_OP_shra:
18434 case DW_OP_xor:
18435 case DW_OP_bra:
18436 case DW_OP_eq:
18437 case DW_OP_ge:
18438 case DW_OP_gt:
18439 case DW_OP_le:
18440 case DW_OP_lt:
18441 case DW_OP_ne:
18442 case DW_OP_regx:
18443 case DW_OP_xderef_size:
18444 --frame_offset_;
18445 break;
18447 case DW_OP_call2:
18448 case DW_OP_call4:
18449 case DW_OP_call_ref:
18451 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18452 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18454 if (stack_usage == NULL)
18455 return false;
18456 frame_offset_ += *stack_usage;
18457 break;
18460 case DW_OP_implicit_pointer:
18461 case DW_OP_entry_value:
18462 case DW_OP_const_type:
18463 case DW_OP_regval_type:
18464 case DW_OP_form_tls_address:
18465 case DW_OP_GNU_push_tls_address:
18466 case DW_OP_GNU_uninit:
18467 case DW_OP_GNU_encoded_addr:
18468 case DW_OP_GNU_implicit_pointer:
18469 case DW_OP_GNU_entry_value:
18470 case DW_OP_GNU_const_type:
18471 case DW_OP_GNU_regval_type:
18472 case DW_OP_GNU_parameter_ref:
18473 /* loc_list_from_tree will probably not output these operations for
18474 size functions, so assume they will not appear here. */
18475 /* Fall through... */
18477 default:
18478 gcc_unreachable ();
18481 /* Now, follow the control flow (except subroutine calls). */
18482 switch (l->dw_loc_opc)
18484 case DW_OP_bra:
18485 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18486 frame_offsets))
18487 return false;
18488 /* Fall through. */
18490 case DW_OP_skip:
18491 l = l->dw_loc_oprnd1.v.val_loc;
18492 break;
18494 case DW_OP_stack_value:
18495 return true;
18497 default:
18498 l = l->dw_loc_next;
18499 break;
18503 return true;
18506 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18507 operations) in order to resolve the operand of DW_OP_pick operations that
18508 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18509 offset *before* LOC is executed. Return if all relocations were
18510 successful. */
18512 static bool
18513 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18514 struct dwarf_procedure_info *dpi)
18516 /* Associate to all visited operations the frame offset *before* evaluating
18517 this operation. */
18518 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18520 return
18521 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18524 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18525 Return NULL if it is not possible. */
18527 static dw_die_ref
18528 function_to_dwarf_procedure (tree fndecl)
18530 struct dwarf_procedure_info dpi;
18531 struct loc_descr_context ctx = {
18532 NULL_TREE, /* context_type */
18533 NULL_TREE, /* base_decl */
18534 &dpi, /* dpi */
18535 false, /* placeholder_arg */
18536 false, /* placeholder_seen */
18537 true /* strict_signedness */
18539 dw_die_ref dwarf_proc_die;
18540 tree tree_body = DECL_SAVED_TREE (fndecl);
18541 dw_loc_descr_ref loc_body, epilogue;
18543 tree cursor;
18544 unsigned i;
18546 /* Do not generate multiple DWARF procedures for the same function
18547 declaration. */
18548 dwarf_proc_die = lookup_decl_die (fndecl);
18549 if (dwarf_proc_die != NULL)
18550 return dwarf_proc_die;
18552 /* DWARF procedures are available starting with the DWARFv3 standard. */
18553 if (dwarf_version < 3 && dwarf_strict)
18554 return NULL;
18556 /* We handle only functions for which we still have a body, that return a
18557 supported type and that takes arguments with supported types. Note that
18558 there is no point translating functions that return nothing. */
18559 if (tree_body == NULL_TREE
18560 || DECL_RESULT (fndecl) == NULL_TREE
18561 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18562 return NULL;
18564 for (cursor = DECL_ARGUMENTS (fndecl);
18565 cursor != NULL_TREE;
18566 cursor = TREE_CHAIN (cursor))
18567 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18568 return NULL;
18570 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18571 if (TREE_CODE (tree_body) != RETURN_EXPR)
18572 return NULL;
18573 tree_body = TREE_OPERAND (tree_body, 0);
18574 if (TREE_CODE (tree_body) != MODIFY_EXPR
18575 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18576 return NULL;
18577 tree_body = TREE_OPERAND (tree_body, 1);
18579 /* Try to translate the body expression itself. Note that this will probably
18580 cause an infinite recursion if its call graph has a cycle. This is very
18581 unlikely for size functions, however, so don't bother with such things at
18582 the moment. */
18583 dpi.fndecl = fndecl;
18584 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18585 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18586 if (!loc_body)
18587 return NULL;
18589 /* After evaluating all operands in "loc_body", we should still have on the
18590 stack all arguments plus the desired function result (top of the stack).
18591 Generate code in order to keep only the result in our stack frame. */
18592 epilogue = NULL;
18593 for (i = 0; i < dpi.args_count; ++i)
18595 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18596 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18597 op_couple->dw_loc_next->dw_loc_next = epilogue;
18598 epilogue = op_couple;
18600 add_loc_descr (&loc_body, epilogue);
18601 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18602 return NULL;
18604 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18605 because they are considered useful. Now there is an epilogue, they are
18606 not anymore, so give it another try. */
18607 loc_descr_without_nops (loc_body);
18609 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18610 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18611 though, given that size functions do not come from source, so they should
18612 not have a dedicated DW_TAG_subprogram DIE. */
18613 dwarf_proc_die
18614 = new_dwarf_proc_die (loc_body, fndecl,
18615 get_context_die (DECL_CONTEXT (fndecl)));
18617 /* The called DWARF procedure consumes one stack slot per argument and
18618 returns one stack slot. */
18619 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18621 return dwarf_proc_die;
18624 /* Helper function for loc_list_from_tree. Perform OP binary op,
18625 but after converting arguments to type_die, afterwards convert
18626 back to unsigned. */
18628 static dw_loc_list_ref
18629 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18630 dw_die_ref type_die, scalar_int_mode mode,
18631 struct loc_descr_context *context)
18633 dw_loc_list_ref op0, op1;
18634 dw_loc_descr_ref cvt, binop;
18636 if (type_die == NULL)
18637 return NULL;
18639 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18640 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18641 if (op0 == NULL || op1 == NULL)
18642 return NULL;
18644 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18645 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18646 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18647 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18648 add_loc_descr_to_each (op0, cvt);
18650 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18651 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18652 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18653 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18654 add_loc_descr_to_each (op1, cvt);
18656 add_loc_list (&op0, op1);
18657 if (op0 == NULL)
18658 return NULL;
18660 binop = new_loc_descr (op, 0, 0);
18661 convert_descriptor_to_mode (mode, binop);
18662 add_loc_descr_to_each (op0, binop);
18664 return op0;
18667 /* Generate Dwarf location list representing LOC.
18668 If WANT_ADDRESS is false, expression computing LOC will be computed
18669 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18670 if WANT_ADDRESS is 2, expression computing address useable in location
18671 will be returned (i.e. DW_OP_reg can be used
18672 to refer to register values).
18674 CONTEXT provides information to customize the location descriptions
18675 generation. Its context_type field specifies what type is implicitly
18676 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18677 will not be generated.
18679 Its DPI field determines whether we are generating a DWARF expression for a
18680 DWARF procedure, so PARM_DECL references are processed specifically.
18682 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18683 and dpi fields were null. */
18685 static dw_loc_list_ref
18686 loc_list_from_tree_1 (tree loc, int want_address,
18687 struct loc_descr_context *context)
18689 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18690 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18691 int have_address = 0;
18692 enum dwarf_location_atom op;
18694 /* ??? Most of the time we do not take proper care for sign/zero
18695 extending the values properly. Hopefully this won't be a real
18696 problem... */
18698 if (context != NULL
18699 && context->base_decl == loc
18700 && want_address == 0)
18702 if (dwarf_version >= 3 || !dwarf_strict)
18703 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18704 NULL, 0, NULL, 0, NULL);
18705 else
18706 return NULL;
18709 switch (TREE_CODE (loc))
18711 case ERROR_MARK:
18712 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18713 return 0;
18715 case PLACEHOLDER_EXPR:
18716 /* This case involves extracting fields from an object to determine the
18717 position of other fields. It is supposed to appear only as the first
18718 operand of COMPONENT_REF nodes and to reference precisely the type
18719 that the context allows or its enclosing type. */
18720 if (context != NULL
18721 && (TREE_TYPE (loc) == context->context_type
18722 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18723 && want_address >= 1)
18725 if (dwarf_version >= 3 || !dwarf_strict)
18727 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18728 have_address = 1;
18729 break;
18731 else
18732 return NULL;
18734 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18735 the single argument passed by consumer. */
18736 else if (context != NULL
18737 && context->placeholder_arg
18738 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18739 && want_address == 0)
18741 ret = new_loc_descr (DW_OP_pick, 0, 0);
18742 ret->frame_offset_rel = 1;
18743 context->placeholder_seen = true;
18744 break;
18746 else
18747 expansion_failed (loc, NULL_RTX,
18748 "PLACEHOLDER_EXPR for an unexpected type");
18749 break;
18751 case CALL_EXPR:
18753 tree callee = get_callee_fndecl (loc);
18754 dw_die_ref dwarf_proc;
18756 if (callee
18757 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18758 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18760 /* DWARF procedures are used for size functions, which are built
18761 when size expressions contain conditional constructs, so we
18762 request strict preservation of signedness for comparisons. */
18763 bool old_strict_signedness;
18764 if (context)
18766 old_strict_signedness = context->strict_signedness;
18767 context->strict_signedness = true;
18770 /* Evaluate arguments right-to-left so that the first argument
18771 will be the top-most one on the stack. */
18772 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18774 tree arg = CALL_EXPR_ARG (loc, i);
18775 ret1 = loc_descriptor_from_tree (arg, 0, context);
18776 if (!ret1)
18778 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18779 return NULL;
18781 add_loc_descr (&ret, ret1);
18784 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18785 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18786 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18787 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18788 add_loc_descr (&ret, ret1);
18789 if (context)
18790 context->strict_signedness = old_strict_signedness;
18792 else
18793 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18794 break;
18797 case PREINCREMENT_EXPR:
18798 case PREDECREMENT_EXPR:
18799 case POSTINCREMENT_EXPR:
18800 case POSTDECREMENT_EXPR:
18801 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18802 /* There are no opcodes for these operations. */
18803 return 0;
18805 case ADDR_EXPR:
18806 /* If we already want an address, see if there is INDIRECT_REF inside
18807 e.g. for &this->field. */
18808 if (want_address)
18810 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18811 (loc, want_address == 2, context);
18812 if (list_ret)
18813 have_address = 1;
18814 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18815 && (ret = cst_pool_loc_descr (loc)))
18816 have_address = 1;
18818 /* Otherwise, process the argument and look for the address. */
18819 if (!list_ret && !ret)
18820 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18821 else
18823 if (want_address)
18824 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18825 return NULL;
18827 break;
18829 case VAR_DECL:
18830 if (DECL_THREAD_LOCAL_P (loc))
18832 rtx rtl;
18833 enum dwarf_location_atom tls_op;
18834 enum dtprel_bool dtprel = dtprel_false;
18836 if (targetm.have_tls)
18838 /* If this is not defined, we have no way to emit the
18839 data. */
18840 if (!targetm.asm_out.output_dwarf_dtprel)
18841 return 0;
18843 /* The way DW_OP_GNU_push_tls_address is specified, we
18844 can only look up addresses of objects in the current
18845 module. We used DW_OP_addr as first op, but that's
18846 wrong, because DW_OP_addr is relocated by the debug
18847 info consumer, while DW_OP_GNU_push_tls_address
18848 operand shouldn't be. */
18849 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18850 return 0;
18851 dtprel = dtprel_true;
18852 /* We check for DWARF 5 here because gdb did not implement
18853 DW_OP_form_tls_address until after 7.12. */
18854 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18855 : DW_OP_GNU_push_tls_address);
18857 else
18859 if (!targetm.emutls.debug_form_tls_address
18860 || !(dwarf_version >= 3 || !dwarf_strict))
18861 return 0;
18862 /* We stuffed the control variable into the DECL_VALUE_EXPR
18863 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18864 no longer appear in gimple code. We used the control
18865 variable in specific so that we could pick it up here. */
18866 loc = DECL_VALUE_EXPR (loc);
18867 tls_op = DW_OP_form_tls_address;
18870 rtl = rtl_for_decl_location (loc);
18871 if (rtl == NULL_RTX)
18872 return 0;
18874 if (!MEM_P (rtl))
18875 return 0;
18876 rtl = XEXP (rtl, 0);
18877 if (! CONSTANT_P (rtl))
18878 return 0;
18880 ret = new_addr_loc_descr (rtl, dtprel);
18881 ret1 = new_loc_descr (tls_op, 0, 0);
18882 add_loc_descr (&ret, ret1);
18884 have_address = 1;
18885 break;
18887 /* FALLTHRU */
18889 case PARM_DECL:
18890 if (context != NULL && context->dpi != NULL
18891 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18893 /* We are generating code for a DWARF procedure and we want to access
18894 one of its arguments: find the appropriate argument offset and let
18895 the resolve_args_picking pass compute the offset that complies
18896 with the stack frame size. */
18897 unsigned i = 0;
18898 tree cursor;
18900 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18901 cursor != NULL_TREE && cursor != loc;
18902 cursor = TREE_CHAIN (cursor), ++i)
18904 /* If we are translating a DWARF procedure, all referenced parameters
18905 must belong to the current function. */
18906 gcc_assert (cursor != NULL_TREE);
18908 ret = new_loc_descr (DW_OP_pick, i, 0);
18909 ret->frame_offset_rel = 1;
18910 break;
18912 /* FALLTHRU */
18914 case RESULT_DECL:
18915 if (DECL_HAS_VALUE_EXPR_P (loc))
18917 tree value_expr = DECL_VALUE_EXPR (loc);
18919 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18920 to wait until they get an RTX in order to reference them. */
18921 if (early_dwarf
18922 && TREE_CODE (value_expr) == COMPONENT_REF
18923 && VAR_P (TREE_OPERAND (value_expr, 0))
18924 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18926 else
18927 return loc_list_from_tree_1 (value_expr, want_address, context);
18930 /* FALLTHRU */
18932 case FUNCTION_DECL:
18934 rtx rtl;
18935 var_loc_list *loc_list = lookup_decl_loc (loc);
18937 if (loc_list && loc_list->first)
18939 list_ret = dw_loc_list (loc_list, loc, want_address);
18940 have_address = want_address != 0;
18941 break;
18943 rtl = rtl_for_decl_location (loc);
18944 if (rtl == NULL_RTX)
18946 if (TREE_CODE (loc) != FUNCTION_DECL
18947 && early_dwarf
18948 && want_address != 1
18949 && ! DECL_IGNORED_P (loc)
18950 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18951 || POINTER_TYPE_P (TREE_TYPE (loc)))
18952 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18953 <= DWARF2_ADDR_SIZE))
18955 dw_die_ref ref = lookup_decl_die (loc);
18956 if (ref)
18958 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18959 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18960 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18961 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18963 else if (current_function_decl
18964 && DECL_CONTEXT (loc) == current_function_decl)
18966 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18967 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18968 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18970 break;
18972 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18973 return 0;
18975 else if (CONST_INT_P (rtl))
18977 HOST_WIDE_INT val = INTVAL (rtl);
18978 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18979 val &= GET_MODE_MASK (DECL_MODE (loc));
18980 ret = int_loc_descriptor (val);
18982 else if (GET_CODE (rtl) == CONST_STRING)
18984 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18985 return 0;
18987 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18988 ret = new_addr_loc_descr (rtl, dtprel_false);
18989 else
18991 machine_mode mode, mem_mode;
18993 /* Certain constructs can only be represented at top-level. */
18994 if (want_address == 2)
18996 ret = loc_descriptor (rtl, VOIDmode,
18997 VAR_INIT_STATUS_INITIALIZED);
18998 have_address = 1;
19000 else
19002 mode = GET_MODE (rtl);
19003 mem_mode = VOIDmode;
19004 if (MEM_P (rtl))
19006 mem_mode = mode;
19007 mode = get_address_mode (rtl);
19008 rtl = XEXP (rtl, 0);
19009 have_address = 1;
19011 ret = mem_loc_descriptor (rtl, mode, mem_mode,
19012 VAR_INIT_STATUS_INITIALIZED);
19014 if (!ret)
19015 expansion_failed (loc, rtl,
19016 "failed to produce loc descriptor for rtl");
19019 break;
19021 case MEM_REF:
19022 if (!integer_zerop (TREE_OPERAND (loc, 1)))
19024 have_address = 1;
19025 goto do_plus;
19027 /* Fallthru. */
19028 case INDIRECT_REF:
19029 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19030 have_address = 1;
19031 break;
19033 case TARGET_MEM_REF:
19034 case SSA_NAME:
19035 case DEBUG_EXPR_DECL:
19036 return NULL;
19038 case COMPOUND_EXPR:
19039 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
19040 context);
19042 CASE_CONVERT:
19043 case VIEW_CONVERT_EXPR:
19044 case SAVE_EXPR:
19045 case MODIFY_EXPR:
19046 case NON_LVALUE_EXPR:
19047 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
19048 context);
19050 case COMPONENT_REF:
19051 case BIT_FIELD_REF:
19052 case ARRAY_REF:
19053 case ARRAY_RANGE_REF:
19054 case REALPART_EXPR:
19055 case IMAGPART_EXPR:
19057 tree obj, offset;
19058 poly_int64 bitsize, bitpos, bytepos;
19059 machine_mode mode;
19060 int unsignedp, reversep, volatilep = 0;
19062 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
19063 &unsignedp, &reversep, &volatilep);
19065 gcc_assert (obj != loc);
19067 list_ret = loc_list_from_tree_1 (obj,
19068 want_address == 2
19069 && known_eq (bitpos, 0)
19070 && !offset ? 2 : 1,
19071 context);
19072 /* TODO: We can extract value of the small expression via shifting even
19073 for nonzero bitpos. */
19074 if (list_ret == 0)
19075 return 0;
19076 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
19077 || !multiple_p (bitsize, BITS_PER_UNIT))
19079 expansion_failed (loc, NULL_RTX,
19080 "bitfield access");
19081 return 0;
19084 if (offset != NULL_TREE)
19086 /* Variable offset. */
19087 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
19088 if (list_ret1 == 0)
19089 return 0;
19090 add_loc_list (&list_ret, list_ret1);
19091 if (!list_ret)
19092 return 0;
19093 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
19096 HOST_WIDE_INT value;
19097 if (bytepos.is_constant (&value) && value > 0)
19098 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
19099 value, 0));
19100 else if (maybe_ne (bytepos, 0))
19101 loc_list_plus_const (list_ret, bytepos);
19103 have_address = 1;
19104 break;
19107 case INTEGER_CST:
19108 if ((want_address || !tree_fits_shwi_p (loc))
19109 && (ret = cst_pool_loc_descr (loc)))
19110 have_address = 1;
19111 else if (want_address == 2
19112 && tree_fits_shwi_p (loc)
19113 && (ret = address_of_int_loc_descriptor
19114 (int_size_in_bytes (TREE_TYPE (loc)),
19115 tree_to_shwi (loc))))
19116 have_address = 1;
19117 else if (tree_fits_shwi_p (loc))
19118 ret = int_loc_descriptor (tree_to_shwi (loc));
19119 else if (tree_fits_uhwi_p (loc))
19120 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19121 else
19123 expansion_failed (loc, NULL_RTX,
19124 "Integer operand is not host integer");
19125 return 0;
19127 break;
19129 case POLY_INT_CST:
19131 if (want_address)
19133 expansion_failed (loc, NULL_RTX,
19134 "constant address with a runtime component");
19135 return 0;
19137 poly_int64 value;
19138 if (!poly_int_tree_p (loc, &value))
19140 expansion_failed (loc, NULL_RTX, "constant too big");
19141 return 0;
19143 ret = int_loc_descriptor (value);
19145 break;
19147 case CONSTRUCTOR:
19148 case REAL_CST:
19149 case STRING_CST:
19150 case COMPLEX_CST:
19151 if ((ret = cst_pool_loc_descr (loc)))
19152 have_address = 1;
19153 else if (TREE_CODE (loc) == CONSTRUCTOR)
19155 tree type = TREE_TYPE (loc);
19156 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19157 unsigned HOST_WIDE_INT offset = 0;
19158 unsigned HOST_WIDE_INT cnt;
19159 constructor_elt *ce;
19161 if (TREE_CODE (type) == RECORD_TYPE)
19163 /* This is very limited, but it's enough to output
19164 pointers to member functions, as long as the
19165 referenced function is defined in the current
19166 translation unit. */
19167 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19169 tree val = ce->value;
19171 tree field = ce->index;
19173 if (val)
19174 STRIP_NOPS (val);
19176 if (!field || DECL_BIT_FIELD (field))
19178 expansion_failed (loc, NULL_RTX,
19179 "bitfield in record type constructor");
19180 size = offset = (unsigned HOST_WIDE_INT)-1;
19181 ret = NULL;
19182 break;
19185 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19186 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19187 gcc_assert (pos + fieldsize <= size);
19188 if (pos < offset)
19190 expansion_failed (loc, NULL_RTX,
19191 "out-of-order fields in record constructor");
19192 size = offset = (unsigned HOST_WIDE_INT)-1;
19193 ret = NULL;
19194 break;
19196 if (pos > offset)
19198 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19199 add_loc_descr (&ret, ret1);
19200 offset = pos;
19202 if (val && fieldsize != 0)
19204 ret1 = loc_descriptor_from_tree (val, want_address, context);
19205 if (!ret1)
19207 expansion_failed (loc, NULL_RTX,
19208 "unsupported expression in field");
19209 size = offset = (unsigned HOST_WIDE_INT)-1;
19210 ret = NULL;
19211 break;
19213 add_loc_descr (&ret, ret1);
19215 if (fieldsize)
19217 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19218 add_loc_descr (&ret, ret1);
19219 offset = pos + fieldsize;
19223 if (offset != size)
19225 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19226 add_loc_descr (&ret, ret1);
19227 offset = size;
19230 have_address = !!want_address;
19232 else
19233 expansion_failed (loc, NULL_RTX,
19234 "constructor of non-record type");
19236 else
19237 /* We can construct small constants here using int_loc_descriptor. */
19238 expansion_failed (loc, NULL_RTX,
19239 "constructor or constant not in constant pool");
19240 break;
19242 case TRUTH_AND_EXPR:
19243 case TRUTH_ANDIF_EXPR:
19244 case BIT_AND_EXPR:
19245 op = DW_OP_and;
19246 goto do_binop;
19248 case TRUTH_XOR_EXPR:
19249 case BIT_XOR_EXPR:
19250 op = DW_OP_xor;
19251 goto do_binop;
19253 case TRUTH_OR_EXPR:
19254 case TRUTH_ORIF_EXPR:
19255 case BIT_IOR_EXPR:
19256 op = DW_OP_or;
19257 goto do_binop;
19259 case EXACT_DIV_EXPR:
19260 case FLOOR_DIV_EXPR:
19261 case TRUNC_DIV_EXPR:
19262 /* Turn a divide by a power of 2 into a shift when possible. */
19263 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19264 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19266 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19267 if (log2 > 0)
19269 list_ret
19270 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19271 if (list_ret == 0)
19272 return 0;
19274 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19275 add_loc_descr_to_each (list_ret,
19276 new_loc_descr (DW_OP_shr, 0, 0));
19277 break;
19281 /* fall through */
19283 case CEIL_DIV_EXPR:
19284 case ROUND_DIV_EXPR:
19285 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19287 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19288 scalar_int_mode int_mode;
19290 if ((dwarf_strict && dwarf_version < 5)
19291 || !is_a <scalar_int_mode> (mode, &int_mode))
19292 return 0;
19294 /* We can use a signed divide if the sign bit is not set. */
19295 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19297 op = DW_OP_div;
19298 goto do_binop;
19301 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19302 base_type_for_mode (int_mode, 1),
19303 int_mode, context);
19304 break;
19306 op = DW_OP_div;
19307 goto do_binop;
19309 case MINUS_EXPR:
19310 op = DW_OP_minus;
19311 goto do_binop;
19313 case FLOOR_MOD_EXPR:
19314 case CEIL_MOD_EXPR:
19315 case ROUND_MOD_EXPR:
19316 case TRUNC_MOD_EXPR:
19317 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19319 op = DW_OP_mod;
19320 goto do_binop;
19322 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19323 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19324 if (list_ret == 0 || list_ret1 == 0)
19325 return 0;
19327 add_loc_list (&list_ret, list_ret1);
19328 if (list_ret == 0)
19329 return 0;
19330 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19331 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19332 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19333 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19334 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19335 break;
19337 case MULT_EXPR:
19338 op = DW_OP_mul;
19339 goto do_binop;
19341 case LSHIFT_EXPR:
19342 op = DW_OP_shl;
19343 goto do_binop;
19345 case RSHIFT_EXPR:
19346 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19347 goto do_binop;
19349 case POINTER_PLUS_EXPR:
19350 case PLUS_EXPR:
19351 do_plus:
19352 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19354 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19355 smarter to encode their opposite. The DW_OP_plus_uconst operation
19356 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19357 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19358 bytes, Y being the size of the operation that pushes the opposite
19359 of the addend. So let's choose the smallest representation. */
19360 const tree tree_addend = TREE_OPERAND (loc, 1);
19361 offset_int wi_addend;
19362 HOST_WIDE_INT shwi_addend;
19363 dw_loc_descr_ref loc_naddend;
19365 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19366 if (list_ret == 0)
19367 return 0;
19369 /* Try to get the literal to push. It is the opposite of the addend,
19370 so as we rely on wrapping during DWARF evaluation, first decode
19371 the literal as a "DWARF-sized" signed number. */
19372 wi_addend = wi::to_offset (tree_addend);
19373 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19374 shwi_addend = wi_addend.to_shwi ();
19375 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19376 ? int_loc_descriptor (-shwi_addend)
19377 : NULL;
19379 if (loc_naddend != NULL
19380 && ((unsigned) size_of_uleb128 (shwi_addend)
19381 > size_of_loc_descr (loc_naddend)))
19383 add_loc_descr_to_each (list_ret, loc_naddend);
19384 add_loc_descr_to_each (list_ret,
19385 new_loc_descr (DW_OP_minus, 0, 0));
19387 else
19389 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19391 loc_naddend = loc_cur;
19392 loc_cur = loc_cur->dw_loc_next;
19393 ggc_free (loc_naddend);
19395 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19397 break;
19400 op = DW_OP_plus;
19401 goto do_binop;
19403 case LE_EXPR:
19404 op = DW_OP_le;
19405 goto do_comp_binop;
19407 case GE_EXPR:
19408 op = DW_OP_ge;
19409 goto do_comp_binop;
19411 case LT_EXPR:
19412 op = DW_OP_lt;
19413 goto do_comp_binop;
19415 case GT_EXPR:
19416 op = DW_OP_gt;
19417 goto do_comp_binop;
19419 do_comp_binop:
19420 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19422 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19423 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19424 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19425 TREE_CODE (loc));
19426 break;
19428 else
19429 goto do_binop;
19431 case EQ_EXPR:
19432 op = DW_OP_eq;
19433 goto do_binop;
19435 case NE_EXPR:
19436 op = DW_OP_ne;
19437 goto do_binop;
19439 do_binop:
19440 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19441 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19442 if (list_ret == 0 || list_ret1 == 0)
19443 return 0;
19445 add_loc_list (&list_ret, list_ret1);
19446 if (list_ret == 0)
19447 return 0;
19448 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19449 break;
19451 case TRUTH_NOT_EXPR:
19452 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19453 if (list_ret == 0)
19454 return 0;
19456 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_lit0, 0, 0));
19457 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_eq, 0, 0));
19458 break;
19460 case BIT_NOT_EXPR:
19461 op = DW_OP_not;
19462 goto do_unop;
19464 case ABS_EXPR:
19465 op = DW_OP_abs;
19466 goto do_unop;
19468 case NEGATE_EXPR:
19469 op = DW_OP_neg;
19470 goto do_unop;
19472 do_unop:
19473 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19474 if (list_ret == 0)
19475 return 0;
19477 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19478 break;
19480 case MIN_EXPR:
19481 case MAX_EXPR:
19483 const enum tree_code code =
19484 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19486 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19487 build2 (code, integer_type_node,
19488 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19489 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19492 /* fall through */
19494 case COND_EXPR:
19496 dw_loc_descr_ref lhs
19497 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19498 dw_loc_list_ref rhs
19499 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19500 dw_loc_descr_ref bra_node, jump_node, tmp;
19502 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19503 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19504 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19505 list_ret
19506 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19507 0, context);
19508 /* Likewise, swap the operands for a logically negated condition. */
19509 else if (TREE_CODE (TREE_OPERAND (loc, 0)) == TRUTH_NOT_EXPR)
19511 lhs = loc_descriptor_from_tree (TREE_OPERAND (loc, 2), 0, context);
19512 rhs = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19513 list_ret
19514 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19515 0, context);
19517 else
19518 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19519 if (list_ret == 0 || lhs == 0 || rhs == 0)
19520 return 0;
19522 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19523 add_loc_descr_to_each (list_ret, bra_node);
19525 add_loc_list (&list_ret, rhs);
19526 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19527 add_loc_descr_to_each (list_ret, jump_node);
19529 add_loc_descr_to_each (list_ret, lhs);
19530 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19531 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19533 /* ??? Need a node to point the skip at. Use a nop. */
19534 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19535 add_loc_descr_to_each (list_ret, tmp);
19536 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19537 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19539 break;
19541 case FIX_TRUNC_EXPR:
19542 return 0;
19544 case COMPOUND_LITERAL_EXPR:
19545 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19546 0, context);
19548 default:
19549 /* Leave front-end specific codes as simply unknown. This comes
19550 up, for instance, with the C STMT_EXPR. */
19551 if ((unsigned int) TREE_CODE (loc)
19552 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19554 expansion_failed (loc, NULL_RTX,
19555 "language specific tree node");
19556 return 0;
19559 /* Otherwise this is a generic code; we should just lists all of
19560 these explicitly. We forgot one. */
19561 if (flag_checking)
19562 gcc_unreachable ();
19564 /* In a release build, we want to degrade gracefully: better to
19565 generate incomplete debugging information than to crash. */
19566 return NULL;
19569 if (!ret && !list_ret)
19570 return 0;
19572 if (want_address == 2 && !have_address
19573 && (dwarf_version >= 4 || !dwarf_strict))
19575 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19577 expansion_failed (loc, NULL_RTX,
19578 "DWARF address size mismatch");
19579 return 0;
19581 if (ret)
19582 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19583 else
19584 add_loc_descr_to_each (list_ret,
19585 new_loc_descr (DW_OP_stack_value, 0, 0));
19586 have_address = 1;
19588 /* Show if we can't fill the request for an address. */
19589 if (want_address && !have_address)
19591 expansion_failed (loc, NULL_RTX,
19592 "Want address and only have value");
19593 return 0;
19596 gcc_assert (!ret || !list_ret);
19598 /* If we've got an address and don't want one, dereference. */
19599 if (!want_address && have_address)
19601 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19602 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19603 scalar_int_mode int_mode;
19604 dw_die_ref type_die;
19605 dw_loc_descr_ref deref;
19607 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19608 if (size > DWARF2_ADDR_SIZE || size == -1)
19610 expansion_failed (loc, NULL_RTX,
19611 "DWARF address size mismatch");
19612 return 0;
19615 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19616 else if (size == DWARF2_ADDR_SIZE)
19617 deref = new_loc_descr (DW_OP_deref, size, 0);
19619 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19620 extend the value, which is really OK for unsigned types only. */
19621 else if (!(context && context->strict_signedness)
19622 || TYPE_UNSIGNED (TREE_TYPE (loc))
19623 || (dwarf_strict && dwarf_version < 5)
19624 || !is_a <scalar_int_mode> (mode, &int_mode)
19625 || !(type_die = base_type_for_mode (mode, false)))
19626 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19628 /* Use DW_OP_deref_type for signed integral types if possible, but
19629 convert back to the generic type to avoid type mismatches later. */
19630 else
19632 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19633 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19634 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19635 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19636 add_loc_descr (&deref,
19637 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19640 if (ret)
19641 add_loc_descr (&ret, deref);
19642 else
19643 add_loc_descr_to_each (list_ret, deref);
19646 if (ret)
19647 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19649 return list_ret;
19652 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19653 expressions. */
19655 static dw_loc_list_ref
19656 loc_list_from_tree (tree loc, int want_address,
19657 struct loc_descr_context *context)
19659 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19661 for (dw_loc_list_ref loc_cur = result;
19662 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19663 loc_descr_without_nops (loc_cur->expr);
19664 return result;
19667 /* Same as above but return only single location expression. */
19668 static dw_loc_descr_ref
19669 loc_descriptor_from_tree (tree loc, int want_address,
19670 struct loc_descr_context *context)
19672 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19673 if (!ret)
19674 return NULL;
19675 if (ret->dw_loc_next)
19677 expansion_failed (loc, NULL_RTX,
19678 "Location list where only loc descriptor needed");
19679 return NULL;
19681 return ret->expr;
19684 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19685 pointer to the declared type for the relevant field variable, or return
19686 `integer_type_node' if the given node turns out to be an
19687 ERROR_MARK node. */
19689 static inline tree
19690 field_type (const_tree decl)
19692 tree type;
19694 if (TREE_CODE (decl) == ERROR_MARK)
19695 return integer_type_node;
19697 type = DECL_BIT_FIELD_TYPE (decl);
19698 if (type == NULL_TREE)
19699 type = TREE_TYPE (decl);
19701 return type;
19704 /* Given a pointer to a tree node, return the alignment in bits for
19705 it, or else return BITS_PER_WORD if the node actually turns out to
19706 be an ERROR_MARK node. */
19708 static inline unsigned
19709 simple_type_align_in_bits (const_tree type)
19711 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19714 static inline unsigned
19715 simple_decl_align_in_bits (const_tree decl)
19717 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19720 /* Return the result of rounding T up to ALIGN. */
19722 static inline offset_int
19723 round_up_to_align (const offset_int &t, unsigned int align)
19725 return wi::udiv_trunc (t + align - 1, align) * align;
19728 /* Helper structure for RECORD_TYPE processing. */
19729 struct vlr_context
19731 /* Root RECORD_TYPE. It is needed to generate data member location
19732 descriptions in variable-length records (VLR), but also to cope with
19733 variants, which are composed of nested structures multiplexed with
19734 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19735 function processing a FIELD_DECL, it is required to be non null. */
19736 tree struct_type;
19738 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19739 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19740 this variant part as part of the root record (in storage units). For
19741 regular records, it must be NULL_TREE. */
19742 tree variant_part_offset;
19745 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19746 addressed byte of the "containing object" for the given FIELD_DECL. If
19747 possible, return a native constant through CST_OFFSET (in which case NULL is
19748 returned); otherwise return a DWARF expression that computes the offset.
19750 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19751 that offset is, either because the argument turns out to be a pointer to an
19752 ERROR_MARK node, or because the offset expression is too complex for us.
19754 CTX is required: see the comment for VLR_CONTEXT. */
19756 static dw_loc_descr_ref
19757 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19758 HOST_WIDE_INT *cst_offset)
19760 tree tree_result;
19761 dw_loc_list_ref loc_result;
19763 *cst_offset = 0;
19765 if (TREE_CODE (decl) == ERROR_MARK)
19766 return NULL;
19767 else
19768 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19770 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19771 case. */
19772 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19773 return NULL;
19775 /* We used to handle only constant offsets in all cases. Now, we handle
19776 properly dynamic byte offsets only when PCC bitfield type doesn't
19777 matter. */
19778 if (PCC_BITFIELD_TYPE_MATTERS
19779 && DECL_BIT_FIELD_TYPE (decl)
19780 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19782 offset_int object_offset_in_bits;
19783 offset_int object_offset_in_bytes;
19784 offset_int bitpos_int;
19785 tree type;
19786 tree field_size_tree;
19787 offset_int deepest_bitpos;
19788 offset_int field_size_in_bits;
19789 unsigned int type_align_in_bits;
19790 unsigned int decl_align_in_bits;
19791 offset_int type_size_in_bits;
19793 bitpos_int = wi::to_offset (bit_position (decl));
19794 type = field_type (decl);
19795 type_size_in_bits = offset_int_type_size_in_bits (type);
19796 type_align_in_bits = simple_type_align_in_bits (type);
19798 field_size_tree = DECL_SIZE (decl);
19800 /* The size could be unspecified if there was an error, or for
19801 a flexible array member. */
19802 if (!field_size_tree)
19803 field_size_tree = bitsize_zero_node;
19805 /* If the size of the field is not constant, use the type size. */
19806 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19807 field_size_in_bits = wi::to_offset (field_size_tree);
19808 else
19809 field_size_in_bits = type_size_in_bits;
19811 decl_align_in_bits = simple_decl_align_in_bits (decl);
19813 /* The GCC front-end doesn't make any attempt to keep track of the
19814 starting bit offset (relative to the start of the containing
19815 structure type) of the hypothetical "containing object" for a
19816 bit-field. Thus, when computing the byte offset value for the
19817 start of the "containing object" of a bit-field, we must deduce
19818 this information on our own. This can be rather tricky to do in
19819 some cases. For example, handling the following structure type
19820 definition when compiling for an i386/i486 target (which only
19821 aligns long long's to 32-bit boundaries) can be very tricky:
19823 struct S { int field1; long long field2:31; };
19825 Fortunately, there is a simple rule-of-thumb which can be used
19826 in such cases. When compiling for an i386/i486, GCC will
19827 allocate 8 bytes for the structure shown above. It decides to
19828 do this based upon one simple rule for bit-field allocation.
19829 GCC allocates each "containing object" for each bit-field at
19830 the first (i.e. lowest addressed) legitimate alignment boundary
19831 (based upon the required minimum alignment for the declared
19832 type of the field) which it can possibly use, subject to the
19833 condition that there is still enough available space remaining
19834 in the containing object (when allocated at the selected point)
19835 to fully accommodate all of the bits of the bit-field itself.
19837 This simple rule makes it obvious why GCC allocates 8 bytes for
19838 each object of the structure type shown above. When looking
19839 for a place to allocate the "containing object" for `field2',
19840 the compiler simply tries to allocate a 64-bit "containing
19841 object" at each successive 32-bit boundary (starting at zero)
19842 until it finds a place to allocate that 64- bit field such that
19843 at least 31 contiguous (and previously unallocated) bits remain
19844 within that selected 64 bit field. (As it turns out, for the
19845 example above, the compiler finds it is OK to allocate the
19846 "containing object" 64-bit field at bit-offset zero within the
19847 structure type.)
19849 Here we attempt to work backwards from the limited set of facts
19850 we're given, and we try to deduce from those facts, where GCC
19851 must have believed that the containing object started (within
19852 the structure type). The value we deduce is then used (by the
19853 callers of this routine) to generate DW_AT_location and
19854 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19855 the case of DW_AT_location, regular fields as well). */
19857 /* Figure out the bit-distance from the start of the structure to
19858 the "deepest" bit of the bit-field. */
19859 deepest_bitpos = bitpos_int + field_size_in_bits;
19861 /* This is the tricky part. Use some fancy footwork to deduce
19862 where the lowest addressed bit of the containing object must
19863 be. */
19864 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19866 /* Round up to type_align by default. This works best for
19867 bitfields. */
19868 object_offset_in_bits
19869 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19871 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19873 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19875 /* Round up to decl_align instead. */
19876 object_offset_in_bits
19877 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19880 object_offset_in_bytes
19881 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19882 if (ctx->variant_part_offset == NULL_TREE)
19884 *cst_offset = object_offset_in_bytes.to_shwi ();
19885 return NULL;
19887 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19889 else
19890 tree_result = byte_position (decl);
19892 if (ctx->variant_part_offset != NULL_TREE)
19893 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19894 ctx->variant_part_offset, tree_result);
19896 /* If the byte offset is a constant, it's simplier to handle a native
19897 constant rather than a DWARF expression. */
19898 if (TREE_CODE (tree_result) == INTEGER_CST)
19900 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19901 return NULL;
19904 struct loc_descr_context loc_ctx = {
19905 ctx->struct_type, /* context_type */
19906 NULL_TREE, /* base_decl */
19907 NULL, /* dpi */
19908 false, /* placeholder_arg */
19909 false, /* placeholder_seen */
19910 false /* strict_signedness */
19912 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19914 /* We want a DWARF expression: abort if we only have a location list with
19915 multiple elements. */
19916 if (!loc_result || !single_element_loc_list_p (loc_result))
19917 return NULL;
19918 else
19919 return loc_result->expr;
19922 /* The following routines define various Dwarf attributes and any data
19923 associated with them. */
19925 /* Add a location description attribute value to a DIE.
19927 This emits location attributes suitable for whole variables and
19928 whole parameters. Note that the location attributes for struct fields are
19929 generated by the routine `data_member_location_attribute' below. */
19931 static inline void
19932 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19933 dw_loc_list_ref descr)
19935 bool check_no_locviews = true;
19936 if (descr == 0)
19937 return;
19938 if (single_element_loc_list_p (descr))
19939 add_AT_loc (die, attr_kind, descr->expr);
19940 else
19942 add_AT_loc_list (die, attr_kind, descr);
19943 gcc_assert (descr->ll_symbol);
19944 if (attr_kind == DW_AT_location && descr->vl_symbol
19945 && dwarf2out_locviews_in_attribute ())
19947 add_AT_view_list (die, DW_AT_GNU_locviews);
19948 check_no_locviews = false;
19952 if (check_no_locviews)
19953 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19956 /* Add DW_AT_accessibility attribute to DIE if needed. */
19958 static void
19959 add_accessibility_attribute (dw_die_ref die, tree decl)
19961 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19962 children, otherwise the default is DW_ACCESS_public. In DWARF2
19963 the default has always been DW_ACCESS_public. */
19964 if (TREE_PROTECTED (decl))
19965 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19966 else if (TREE_PRIVATE (decl))
19968 if (dwarf_version == 2
19969 || die->die_parent == NULL
19970 || die->die_parent->die_tag != DW_TAG_class_type)
19971 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19973 else if (dwarf_version > 2
19974 && die->die_parent
19975 && die->die_parent->die_tag == DW_TAG_class_type)
19976 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19979 /* Attach the specialized form of location attribute used for data members of
19980 struct and union types. In the special case of a FIELD_DECL node which
19981 represents a bit-field, the "offset" part of this special location
19982 descriptor must indicate the distance in bytes from the lowest-addressed
19983 byte of the containing struct or union type to the lowest-addressed byte of
19984 the "containing object" for the bit-field. (See the `field_byte_offset'
19985 function above).
19987 For any given bit-field, the "containing object" is a hypothetical object
19988 (of some integral or enum type) within which the given bit-field lives. The
19989 type of this hypothetical "containing object" is always the same as the
19990 declared type of the individual bit-field itself (for GCC anyway... the
19991 DWARF spec doesn't actually mandate this). Note that it is the size (in
19992 bytes) of the hypothetical "containing object" which will be given in the
19993 DW_AT_byte_size attribute for this bit-field. (See the
19994 `byte_size_attribute' function below.) It is also used when calculating the
19995 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19996 function below.)
19998 CTX is required: see the comment for VLR_CONTEXT. */
20000 static void
20001 add_data_member_location_attribute (dw_die_ref die,
20002 tree decl,
20003 struct vlr_context *ctx)
20005 HOST_WIDE_INT offset;
20006 dw_loc_descr_ref loc_descr = 0;
20008 if (TREE_CODE (decl) == TREE_BINFO)
20010 /* We're working on the TAG_inheritance for a base class. */
20011 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
20013 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
20014 aren't at a fixed offset from all (sub)objects of the same
20015 type. We need to extract the appropriate offset from our
20016 vtable. The following dwarf expression means
20018 BaseAddr = ObAddr + *((*ObAddr) - Offset)
20020 This is specific to the V3 ABI, of course. */
20022 dw_loc_descr_ref tmp;
20024 /* Make a copy of the object address. */
20025 tmp = new_loc_descr (DW_OP_dup, 0, 0);
20026 add_loc_descr (&loc_descr, tmp);
20028 /* Extract the vtable address. */
20029 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20030 add_loc_descr (&loc_descr, tmp);
20032 /* Calculate the address of the offset. */
20033 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
20034 gcc_assert (offset < 0);
20036 tmp = int_loc_descriptor (-offset);
20037 add_loc_descr (&loc_descr, tmp);
20038 tmp = new_loc_descr (DW_OP_minus, 0, 0);
20039 add_loc_descr (&loc_descr, tmp);
20041 /* Extract the offset. */
20042 tmp = new_loc_descr (DW_OP_deref, 0, 0);
20043 add_loc_descr (&loc_descr, tmp);
20045 /* Add it to the object address. */
20046 tmp = new_loc_descr (DW_OP_plus, 0, 0);
20047 add_loc_descr (&loc_descr, tmp);
20049 else
20050 offset = tree_to_shwi (BINFO_OFFSET (decl));
20052 else
20054 loc_descr = field_byte_offset (decl, ctx, &offset);
20056 if (!loc_descr)
20059 /* If loc_descr is available, then we know the offset is dynamic. */
20060 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
20062 loc_descr = NULL;
20063 offset = 0;
20066 /* Data member location evaluation starts with the base address on the
20067 stack. Compute the field offset and add it to this base address. */
20068 else
20069 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
20072 if (!loc_descr)
20074 /* While DW_AT_data_bit_offset has been added already in DWARF4,
20075 e.g. GDB only added support to it in November 2016. For DWARF5
20076 we need newer debug info consumers anyway. We might change this
20077 to dwarf_version >= 4 once most consumers catched up. */
20078 if (dwarf_version >= 5
20079 && TREE_CODE (decl) == FIELD_DECL
20080 && DECL_BIT_FIELD_TYPE (decl)
20081 && (ctx->variant_part_offset == NULL_TREE
20082 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
20084 tree off = bit_position (decl);
20085 if (ctx->variant_part_offset)
20086 off = bit_from_pos (ctx->variant_part_offset, off);
20087 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
20089 remove_AT (die, DW_AT_byte_size);
20090 remove_AT (die, DW_AT_bit_offset);
20091 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
20092 return;
20095 if (dwarf_version > 2)
20097 /* Don't need to output a location expression, just the constant. */
20098 if (offset < 0)
20099 add_AT_int (die, DW_AT_data_member_location, offset);
20100 else
20101 add_AT_unsigned (die, DW_AT_data_member_location, offset);
20102 return;
20104 else
20106 enum dwarf_location_atom op;
20108 /* The DWARF2 standard says that we should assume that the structure
20109 address is already on the stack, so we can specify a structure
20110 field address by using DW_OP_plus_uconst. */
20111 op = DW_OP_plus_uconst;
20112 loc_descr = new_loc_descr (op, offset, 0);
20116 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
20119 /* Writes integer values to dw_vec_const array. */
20121 static void
20122 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20124 while (size != 0)
20126 *dest++ = val & 0xff;
20127 val >>= 8;
20128 --size;
20132 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20134 static HOST_WIDE_INT
20135 extract_int (const unsigned char *src, unsigned int size)
20137 HOST_WIDE_INT val = 0;
20139 src += size;
20140 while (size != 0)
20142 val <<= 8;
20143 val |= *--src & 0xff;
20144 --size;
20146 return val;
20149 /* Writes wide_int values to dw_vec_const array. */
20151 static void
20152 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20154 int i;
20156 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20158 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20159 return;
20162 /* We'd have to extend this code to support odd sizes. */
20163 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20165 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20167 if (WORDS_BIG_ENDIAN)
20168 for (i = n - 1; i >= 0; i--)
20170 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20171 dest += sizeof (HOST_WIDE_INT);
20173 else
20174 for (i = 0; i < n; i++)
20176 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20177 dest += sizeof (HOST_WIDE_INT);
20181 /* Writes floating point values to dw_vec_const array. */
20183 static unsigned
20184 insert_float (const_rtx rtl, unsigned char *array)
20186 long val[4];
20187 int i;
20188 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20190 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20192 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20193 if (GET_MODE_SIZE (mode) < 4)
20195 gcc_assert (GET_MODE_SIZE (mode) == 2);
20196 insert_int (val[0], 2, array);
20197 return 2;
20200 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20202 insert_int (val[i], 4, array);
20203 array += 4;
20205 return 4;
20208 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20209 does not have a "location" either in memory or in a register. These
20210 things can arise in GNU C when a constant is passed as an actual parameter
20211 to an inlined function. They can also arise in C++ where declared
20212 constants do not necessarily get memory "homes". */
20214 static bool
20215 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20217 scalar_mode int_mode;
20219 switch (GET_CODE (rtl))
20221 case CONST_INT:
20223 HOST_WIDE_INT val = INTVAL (rtl);
20225 if (val < 0)
20226 add_AT_int (die, DW_AT_const_value, val);
20227 else
20228 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20230 return true;
20232 case CONST_WIDE_INT:
20233 if (is_int_mode (mode, &int_mode)
20234 && (GET_MODE_PRECISION (int_mode)
20235 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20237 wide_int w = rtx_mode_t (rtl, int_mode);
20238 add_AT_wide (die, DW_AT_const_value, w);
20239 return true;
20241 return false;
20243 case CONST_DOUBLE:
20244 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20245 floating-point constant. A CONST_DOUBLE is used whenever the
20246 constant requires more than one word in order to be adequately
20247 represented. */
20248 if (TARGET_SUPPORTS_WIDE_INT == 0
20249 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20250 add_AT_double (die, DW_AT_const_value,
20251 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20252 else
20254 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20255 unsigned int length = GET_MODE_SIZE (mode);
20256 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20257 unsigned int elt_size = insert_float (rtl, array);
20259 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20260 array);
20262 return true;
20264 case CONST_VECTOR:
20266 unsigned int length;
20267 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20268 return false;
20270 machine_mode mode = GET_MODE (rtl);
20271 /* The combination of a length and byte elt_size doesn't extend
20272 naturally to boolean vectors, where several elements are packed
20273 into the same byte. */
20274 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20275 return false;
20277 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20278 unsigned char *array
20279 = ggc_vec_alloc<unsigned char> (length * elt_size);
20280 unsigned int i;
20281 unsigned char *p;
20282 machine_mode imode = GET_MODE_INNER (mode);
20284 switch (GET_MODE_CLASS (mode))
20286 case MODE_VECTOR_INT:
20287 for (i = 0, p = array; i < length; i++, p += elt_size)
20289 rtx elt = CONST_VECTOR_ELT (rtl, i);
20290 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20292 break;
20294 case MODE_VECTOR_FLOAT:
20295 for (i = 0, p = array; i < length; i++, p += elt_size)
20297 rtx elt = CONST_VECTOR_ELT (rtl, i);
20298 insert_float (elt, p);
20300 break;
20302 default:
20303 gcc_unreachable ();
20306 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20308 return true;
20310 case CONST_STRING:
20311 if (dwarf_version >= 4 || !dwarf_strict)
20313 dw_loc_descr_ref loc_result;
20314 resolve_one_addr (&rtl);
20315 rtl_addr:
20316 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20317 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20318 add_AT_loc (die, DW_AT_location, loc_result);
20319 vec_safe_push (used_rtx_array, rtl);
20320 return true;
20322 return false;
20324 case CONST:
20325 if (CONSTANT_P (XEXP (rtl, 0)))
20326 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20327 /* FALLTHROUGH */
20328 case SYMBOL_REF:
20329 if (!const_ok_for_output (rtl))
20330 return false;
20331 /* FALLTHROUGH */
20332 case LABEL_REF:
20333 if (dwarf_version >= 4 || !dwarf_strict)
20334 goto rtl_addr;
20335 return false;
20337 case PLUS:
20338 /* In cases where an inlined instance of an inline function is passed
20339 the address of an `auto' variable (which is local to the caller) we
20340 can get a situation where the DECL_RTL of the artificial local
20341 variable (for the inlining) which acts as a stand-in for the
20342 corresponding formal parameter (of the inline function) will look
20343 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20344 exactly a compile-time constant expression, but it isn't the address
20345 of the (artificial) local variable either. Rather, it represents the
20346 *value* which the artificial local variable always has during its
20347 lifetime. We currently have no way to represent such quasi-constant
20348 values in Dwarf, so for now we just punt and generate nothing. */
20349 return false;
20351 case HIGH:
20352 case CONST_FIXED:
20353 case MINUS:
20354 case SIGN_EXTEND:
20355 case ZERO_EXTEND:
20356 case CONST_POLY_INT:
20357 return false;
20359 case MEM:
20360 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20361 && MEM_READONLY_P (rtl)
20362 && GET_MODE (rtl) == BLKmode)
20364 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20365 return true;
20367 return false;
20369 default:
20370 /* No other kinds of rtx should be possible here. */
20371 gcc_unreachable ();
20375 /* Determine whether the evaluation of EXPR references any variables
20376 or functions which aren't otherwise used (and therefore may not be
20377 output). */
20378 static tree
20379 reference_to_unused (tree * tp, int * walk_subtrees,
20380 void * data ATTRIBUTE_UNUSED)
20382 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20383 *walk_subtrees = 0;
20385 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20386 && ! TREE_ASM_WRITTEN (*tp))
20387 return *tp;
20388 /* ??? The C++ FE emits debug information for using decls, so
20389 putting gcc_unreachable here falls over. See PR31899. For now
20390 be conservative. */
20391 else if (!symtab->global_info_ready && VAR_P (*tp))
20392 return *tp;
20393 else if (VAR_P (*tp))
20395 varpool_node *node = varpool_node::get (*tp);
20396 if (!node || !node->definition)
20397 return *tp;
20399 else if (TREE_CODE (*tp) == FUNCTION_DECL
20400 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20402 /* The call graph machinery must have finished analyzing,
20403 optimizing and gimplifying the CU by now.
20404 So if *TP has no call graph node associated
20405 to it, it means *TP will not be emitted. */
20406 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20407 return *tp;
20409 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20410 return *tp;
20412 return NULL_TREE;
20415 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20416 for use in a later add_const_value_attribute call. */
20418 static rtx
20419 rtl_for_decl_init (tree init, tree type)
20421 rtx rtl = NULL_RTX;
20423 STRIP_NOPS (init);
20425 /* If a variable is initialized with a string constant without embedded
20426 zeros, build CONST_STRING. */
20427 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20429 tree enttype = TREE_TYPE (type);
20430 tree domain = TYPE_DOMAIN (type);
20431 scalar_int_mode mode;
20433 if (is_int_mode (TYPE_MODE (enttype), &mode)
20434 && GET_MODE_SIZE (mode) == 1
20435 && domain
20436 && TYPE_MAX_VALUE (domain)
20437 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20438 && integer_zerop (TYPE_MIN_VALUE (domain))
20439 && compare_tree_int (TYPE_MAX_VALUE (domain),
20440 TREE_STRING_LENGTH (init) - 1) == 0
20441 && ((size_t) TREE_STRING_LENGTH (init)
20442 == strlen (TREE_STRING_POINTER (init)) + 1))
20444 rtl = gen_rtx_CONST_STRING (VOIDmode,
20445 ggc_strdup (TREE_STRING_POINTER (init)));
20446 rtl = gen_rtx_MEM (BLKmode, rtl);
20447 MEM_READONLY_P (rtl) = 1;
20450 /* Other aggregates, and complex values, could be represented using
20451 CONCAT: FIXME!
20452 If this changes, please adjust tree_add_const_value_attribute
20453 so that for early_dwarf it will for such initializers mangle referenced
20454 decls. */
20455 else if (AGGREGATE_TYPE_P (type)
20456 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20457 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20458 || TREE_CODE (type) == COMPLEX_TYPE)
20460 /* Vectors only work if their mode is supported by the target.
20461 FIXME: generic vectors ought to work too. */
20462 else if (TREE_CODE (type) == VECTOR_TYPE
20463 && !VECTOR_MODE_P (TYPE_MODE (type)))
20465 /* If the initializer is something that we know will expand into an
20466 immediate RTL constant, expand it now. We must be careful not to
20467 reference variables which won't be output. */
20468 else if (initializer_constant_valid_p (init, type)
20469 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20471 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20472 possible. */
20473 if (TREE_CODE (type) == VECTOR_TYPE)
20474 switch (TREE_CODE (init))
20476 case VECTOR_CST:
20477 break;
20478 case CONSTRUCTOR:
20479 if (TREE_CONSTANT (init))
20481 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20482 bool constant_p = true;
20483 tree value;
20484 unsigned HOST_WIDE_INT ix;
20486 /* Even when ctor is constant, it might contain non-*_CST
20487 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20488 belong into VECTOR_CST nodes. */
20489 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20490 if (!CONSTANT_CLASS_P (value))
20492 constant_p = false;
20493 break;
20496 if (constant_p)
20498 init = build_vector_from_ctor (type, elts);
20499 break;
20502 /* FALLTHRU */
20504 default:
20505 return NULL;
20508 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20510 /* If expand_expr returns a MEM, it wasn't immediate. */
20511 gcc_assert (!rtl || !MEM_P (rtl));
20514 return rtl;
20517 /* Generate RTL for the variable DECL to represent its location. */
20519 static rtx
20520 rtl_for_decl_location (tree decl)
20522 rtx rtl;
20524 /* Here we have to decide where we are going to say the parameter "lives"
20525 (as far as the debugger is concerned). We only have a couple of
20526 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20528 DECL_RTL normally indicates where the parameter lives during most of the
20529 activation of the function. If optimization is enabled however, this
20530 could be either NULL or else a pseudo-reg. Both of those cases indicate
20531 that the parameter doesn't really live anywhere (as far as the code
20532 generation parts of GCC are concerned) during most of the function's
20533 activation. That will happen (for example) if the parameter is never
20534 referenced within the function.
20536 We could just generate a location descriptor here for all non-NULL
20537 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20538 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20539 where DECL_RTL is NULL or is a pseudo-reg.
20541 Note however that we can only get away with using DECL_INCOMING_RTL as
20542 a backup substitute for DECL_RTL in certain limited cases. In cases
20543 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20544 we can be sure that the parameter was passed using the same type as it is
20545 declared to have within the function, and that its DECL_INCOMING_RTL
20546 points us to a place where a value of that type is passed.
20548 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20549 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20550 because in these cases DECL_INCOMING_RTL points us to a value of some
20551 type which is *different* from the type of the parameter itself. Thus,
20552 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20553 such cases, the debugger would end up (for example) trying to fetch a
20554 `float' from a place which actually contains the first part of a
20555 `double'. That would lead to really incorrect and confusing
20556 output at debug-time.
20558 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20559 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20560 are a couple of exceptions however. On little-endian machines we can
20561 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20562 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20563 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20564 when (on a little-endian machine) a non-prototyped function has a
20565 parameter declared to be of type `short' or `char'. In such cases,
20566 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20567 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20568 passed `int' value. If the debugger then uses that address to fetch
20569 a `short' or a `char' (on a little-endian machine) the result will be
20570 the correct data, so we allow for such exceptional cases below.
20572 Note that our goal here is to describe the place where the given formal
20573 parameter lives during most of the function's activation (i.e. between the
20574 end of the prologue and the start of the epilogue). We'll do that as best
20575 as we can. Note however that if the given formal parameter is modified
20576 sometime during the execution of the function, then a stack backtrace (at
20577 debug-time) will show the function as having been called with the *new*
20578 value rather than the value which was originally passed in. This happens
20579 rarely enough that it is not a major problem, but it *is* a problem, and
20580 I'd like to fix it.
20582 A future version of dwarf2out.cc may generate two additional attributes for
20583 any given DW_TAG_formal_parameter DIE which will describe the "passed
20584 type" and the "passed location" for the given formal parameter in addition
20585 to the attributes we now generate to indicate the "declared type" and the
20586 "active location" for each parameter. This additional set of attributes
20587 could be used by debuggers for stack backtraces. Separately, note that
20588 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20589 This happens (for example) for inlined-instances of inline function formal
20590 parameters which are never referenced. This really shouldn't be
20591 happening. All PARM_DECL nodes should get valid non-NULL
20592 DECL_INCOMING_RTL values. FIXME. */
20594 /* Use DECL_RTL as the "location" unless we find something better. */
20595 rtl = DECL_RTL_IF_SET (decl);
20597 /* When generating abstract instances, ignore everything except
20598 constants, symbols living in memory, and symbols living in
20599 fixed registers. */
20600 if (! reload_completed)
20602 if (rtl
20603 && (CONSTANT_P (rtl)
20604 || (MEM_P (rtl)
20605 && CONSTANT_P (XEXP (rtl, 0)))
20606 || (REG_P (rtl)
20607 && VAR_P (decl)
20608 && TREE_STATIC (decl))))
20610 rtl = targetm.delegitimize_address (rtl);
20611 return rtl;
20613 rtl = NULL_RTX;
20615 else if (TREE_CODE (decl) == PARM_DECL)
20617 if (rtl == NULL_RTX
20618 || is_pseudo_reg (rtl)
20619 || (MEM_P (rtl)
20620 && is_pseudo_reg (XEXP (rtl, 0))
20621 && DECL_INCOMING_RTL (decl)
20622 && MEM_P (DECL_INCOMING_RTL (decl))
20623 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20625 tree declared_type = TREE_TYPE (decl);
20626 tree passed_type = DECL_ARG_TYPE (decl);
20627 machine_mode dmode = TYPE_MODE (declared_type);
20628 machine_mode pmode = TYPE_MODE (passed_type);
20630 /* This decl represents a formal parameter which was optimized out.
20631 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20632 all cases where (rtl == NULL_RTX) just below. */
20633 if (dmode == pmode)
20634 rtl = DECL_INCOMING_RTL (decl);
20635 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20636 && SCALAR_INT_MODE_P (dmode)
20637 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20638 && DECL_INCOMING_RTL (decl))
20640 rtx inc = DECL_INCOMING_RTL (decl);
20641 if (REG_P (inc))
20642 rtl = inc;
20643 else if (MEM_P (inc))
20645 if (BYTES_BIG_ENDIAN)
20646 rtl = adjust_address_nv (inc, dmode,
20647 GET_MODE_SIZE (pmode)
20648 - GET_MODE_SIZE (dmode));
20649 else
20650 rtl = inc;
20655 /* If the parm was passed in registers, but lives on the stack, then
20656 make a big endian correction if the mode of the type of the
20657 parameter is not the same as the mode of the rtl. */
20658 /* ??? This is the same series of checks that are made in dbxout.cc before
20659 we reach the big endian correction code there. It isn't clear if all
20660 of these checks are necessary here, but keeping them all is the safe
20661 thing to do. */
20662 else if (MEM_P (rtl)
20663 && XEXP (rtl, 0) != const0_rtx
20664 && ! CONSTANT_P (XEXP (rtl, 0))
20665 /* Not passed in memory. */
20666 && !MEM_P (DECL_INCOMING_RTL (decl))
20667 /* Not passed by invisible reference. */
20668 && (!REG_P (XEXP (rtl, 0))
20669 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20670 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20671 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20672 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20673 #endif
20675 /* Big endian correction check. */
20676 && BYTES_BIG_ENDIAN
20677 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20678 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20679 UNITS_PER_WORD))
20681 machine_mode addr_mode = get_address_mode (rtl);
20682 poly_int64 offset = (UNITS_PER_WORD
20683 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20685 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20686 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20689 else if (VAR_P (decl)
20690 && rtl
20691 && MEM_P (rtl)
20692 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20694 machine_mode addr_mode = get_address_mode (rtl);
20695 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20696 GET_MODE (rtl));
20698 /* If a variable is declared "register" yet is smaller than
20699 a register, then if we store the variable to memory, it
20700 looks like we're storing a register-sized value, when in
20701 fact we are not. We need to adjust the offset of the
20702 storage location to reflect the actual value's bytes,
20703 else gdb will not be able to display it. */
20704 if (maybe_ne (offset, 0))
20705 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20706 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20709 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20710 and will have been substituted directly into all expressions that use it.
20711 C does not have such a concept, but C++ and other languages do. */
20712 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20713 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20715 if (rtl)
20716 rtl = targetm.delegitimize_address (rtl);
20718 /* If we don't look past the constant pool, we risk emitting a
20719 reference to a constant pool entry that isn't referenced from
20720 code, and thus is not emitted. */
20721 if (rtl)
20722 rtl = avoid_constant_pool_reference (rtl);
20724 /* Try harder to get a rtl. If this symbol ends up not being emitted
20725 in the current CU, resolve_addr will remove the expression referencing
20726 it. */
20727 if (rtl == NULL_RTX
20728 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20729 && VAR_P (decl)
20730 && !DECL_EXTERNAL (decl)
20731 && TREE_STATIC (decl)
20732 && DECL_NAME (decl)
20733 && !DECL_HARD_REGISTER (decl)
20734 && DECL_MODE (decl) != VOIDmode)
20736 rtl = make_decl_rtl_for_debug (decl);
20737 if (!MEM_P (rtl)
20738 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20739 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20740 rtl = NULL_RTX;
20743 return rtl;
20746 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20747 returned. If so, the decl for the COMMON block is returned, and the
20748 value is the offset into the common block for the symbol. */
20750 static tree
20751 fortran_common (tree decl, HOST_WIDE_INT *value)
20753 tree val_expr, cvar;
20754 machine_mode mode;
20755 poly_int64 bitsize, bitpos;
20756 tree offset;
20757 HOST_WIDE_INT cbitpos;
20758 int unsignedp, reversep, volatilep = 0;
20760 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20761 it does not have a value (the offset into the common area), or if it
20762 is thread local (as opposed to global) then it isn't common, and shouldn't
20763 be handled as such. */
20764 if (!VAR_P (decl)
20765 || !TREE_STATIC (decl)
20766 || !DECL_HAS_VALUE_EXPR_P (decl)
20767 || !is_fortran ())
20768 return NULL_TREE;
20770 val_expr = DECL_VALUE_EXPR (decl);
20771 if (TREE_CODE (val_expr) != COMPONENT_REF)
20772 return NULL_TREE;
20774 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20775 &unsignedp, &reversep, &volatilep);
20777 if (cvar == NULL_TREE
20778 || !VAR_P (cvar)
20779 || DECL_ARTIFICIAL (cvar)
20780 || !TREE_PUBLIC (cvar)
20781 /* We don't expect to have to cope with variable offsets,
20782 since at present all static data must have a constant size. */
20783 || !bitpos.is_constant (&cbitpos))
20784 return NULL_TREE;
20786 *value = 0;
20787 if (offset != NULL)
20789 if (!tree_fits_shwi_p (offset))
20790 return NULL_TREE;
20791 *value = tree_to_shwi (offset);
20793 if (cbitpos != 0)
20794 *value += cbitpos / BITS_PER_UNIT;
20796 return cvar;
20799 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20800 data attribute for a variable or a parameter. We generate the
20801 DW_AT_const_value attribute only in those cases where the given variable
20802 or parameter does not have a true "location" either in memory or in a
20803 register. This can happen (for example) when a constant is passed as an
20804 actual argument in a call to an inline function. (It's possible that
20805 these things can crop up in other ways also.) Note that one type of
20806 constant value which can be passed into an inlined function is a constant
20807 pointer. This can happen for example if an actual argument in an inlined
20808 function call evaluates to a compile-time constant address.
20810 CACHE_P is true if it is worth caching the location list for DECL,
20811 so that future calls can reuse it rather than regenerate it from scratch.
20812 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20813 since we will need to refer to them each time the function is inlined. */
20815 static bool
20816 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20818 rtx rtl;
20819 dw_loc_list_ref list;
20820 var_loc_list *loc_list;
20821 cached_dw_loc_list *cache;
20823 if (early_dwarf)
20824 return false;
20826 if (TREE_CODE (decl) == ERROR_MARK)
20827 return false;
20829 if (get_AT (die, DW_AT_location)
20830 || get_AT (die, DW_AT_const_value))
20831 return true;
20833 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20834 || TREE_CODE (decl) == RESULT_DECL);
20836 /* Try to get some constant RTL for this decl, and use that as the value of
20837 the location. */
20839 rtl = rtl_for_decl_location (decl);
20840 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20841 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20842 return true;
20844 /* See if we have single element location list that is equivalent to
20845 a constant value. That way we are better to use add_const_value_attribute
20846 rather than expanding constant value equivalent. */
20847 loc_list = lookup_decl_loc (decl);
20848 if (loc_list
20849 && loc_list->first
20850 && loc_list->first->next == NULL
20851 && NOTE_P (loc_list->first->loc)
20852 && NOTE_VAR_LOCATION (loc_list->first->loc)
20853 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20855 struct var_loc_node *node;
20857 node = loc_list->first;
20858 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20859 if (GET_CODE (rtl) == EXPR_LIST)
20860 rtl = XEXP (rtl, 0);
20861 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20862 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20863 return true;
20865 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20866 list several times. See if we've already cached the contents. */
20867 list = NULL;
20868 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20869 cache_p = false;
20870 if (cache_p)
20872 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20873 if (cache)
20874 list = cache->loc_list;
20876 if (list == NULL)
20878 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20879 NULL);
20880 /* It is usually worth caching this result if the decl is from
20881 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20882 if (cache_p && list && list->dw_loc_next)
20884 cached_dw_loc_list **slot
20885 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20886 DECL_UID (decl),
20887 INSERT);
20888 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20889 cache->decl_id = DECL_UID (decl);
20890 cache->loc_list = list;
20891 *slot = cache;
20894 if (list)
20896 add_AT_location_description (die, DW_AT_location, list);
20897 return true;
20899 /* None of that worked, so it must not really have a location;
20900 try adding a constant value attribute from the DECL_INITIAL. */
20901 return tree_add_const_value_attribute_for_decl (die, decl);
20904 /* Mangle referenced decls. */
20905 static tree
20906 mangle_referenced_decls (tree *tp, int *walk_subtrees, void *)
20908 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20909 *walk_subtrees = 0;
20911 if (VAR_OR_FUNCTION_DECL_P (*tp))
20912 assign_assembler_name_if_needed (*tp);
20914 return NULL_TREE;
20917 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20918 attribute is the const value T. */
20920 static bool
20921 tree_add_const_value_attribute (dw_die_ref die, tree t)
20923 tree init;
20924 tree type = TREE_TYPE (t);
20926 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20927 return false;
20929 init = t;
20930 gcc_assert (!DECL_P (init));
20932 if (TREE_CODE (init) == INTEGER_CST)
20934 if (tree_fits_uhwi_p (init))
20936 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20937 return true;
20939 if (tree_fits_shwi_p (init))
20941 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20942 return true;
20945 if (!early_dwarf)
20947 rtx rtl = rtl_for_decl_init (init, type);
20948 if (rtl)
20949 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
20951 else
20953 /* For early_dwarf force mangling of all referenced symbols. */
20954 tree initializer = init;
20955 STRIP_NOPS (initializer);
20956 /* rtl_for_decl_init punts on other aggregates, and complex values. */
20957 if (AGGREGATE_TYPE_P (type)
20958 || (TREE_CODE (initializer) == VIEW_CONVERT_EXPR
20959 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (initializer, 0))))
20960 || TREE_CODE (type) == COMPLEX_TYPE)
20962 else if (initializer_constant_valid_p (initializer, type))
20963 walk_tree (&initializer, mangle_referenced_decls, NULL, NULL);
20965 /* If the host and target are sane, try harder. */
20966 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20967 && initializer_constant_valid_p (init, type))
20969 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20970 if (size > 0 && (int) size == size)
20972 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20974 if (native_encode_initializer (init, array, size) == size)
20976 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20977 return true;
20979 ggc_free (array);
20982 return false;
20985 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20986 attribute is the const value of T, where T is an integral constant
20987 variable with static storage duration
20988 (so it can't be a PARM_DECL or a RESULT_DECL). */
20990 static bool
20991 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20994 if (!decl
20995 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20996 || (VAR_P (decl) && !TREE_STATIC (decl)))
20997 return false;
20999 if (TREE_READONLY (decl)
21000 && ! TREE_THIS_VOLATILE (decl)
21001 && DECL_INITIAL (decl))
21002 /* OK */;
21003 else
21004 return false;
21006 /* Don't add DW_AT_const_value if abstract origin already has one. */
21007 if (get_AT (var_die, DW_AT_const_value))
21008 return false;
21010 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
21013 /* Convert the CFI instructions for the current function into a
21014 location list. This is used for DW_AT_frame_base when we targeting
21015 a dwarf2 consumer that does not support the dwarf3
21016 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
21017 expressions. */
21019 static dw_loc_list_ref
21020 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
21022 int ix;
21023 dw_fde_ref fde;
21024 dw_loc_list_ref list, *list_tail;
21025 dw_cfi_ref cfi;
21026 dw_cfa_location last_cfa, next_cfa;
21027 const char *start_label, *last_label, *section;
21028 dw_cfa_location remember;
21030 fde = cfun->fde;
21031 gcc_assert (fde != NULL);
21033 section = secname_for_decl (current_function_decl);
21034 list_tail = &list;
21035 list = NULL;
21037 memset (&next_cfa, 0, sizeof (next_cfa));
21038 next_cfa.reg.set_by_dwreg (INVALID_REGNUM);
21039 remember = next_cfa;
21041 start_label = fde->dw_fde_begin;
21043 /* ??? Bald assumption that the CIE opcode list does not contain
21044 advance opcodes. */
21045 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
21046 lookup_cfa_1 (cfi, &next_cfa, &remember);
21048 last_cfa = next_cfa;
21049 last_label = start_label;
21051 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
21053 /* If the first partition contained no CFI adjustments, the
21054 CIE opcodes apply to the whole first partition. */
21055 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21056 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
21057 list_tail =&(*list_tail)->dw_loc_next;
21058 start_label = last_label = fde->dw_fde_second_begin;
21061 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
21063 switch (cfi->dw_cfi_opc)
21065 case DW_CFA_set_loc:
21066 case DW_CFA_advance_loc1:
21067 case DW_CFA_advance_loc2:
21068 case DW_CFA_advance_loc4:
21069 if (!cfa_equal_p (&last_cfa, &next_cfa))
21071 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21072 start_label, 0, last_label, 0, section);
21074 list_tail = &(*list_tail)->dw_loc_next;
21075 last_cfa = next_cfa;
21076 start_label = last_label;
21078 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
21079 break;
21081 case DW_CFA_advance_loc:
21082 /* The encoding is complex enough that we should never emit this. */
21083 gcc_unreachable ();
21085 default:
21086 lookup_cfa_1 (cfi, &next_cfa, &remember);
21087 break;
21089 if (ix + 1 == fde->dw_fde_switch_cfi_index)
21091 if (!cfa_equal_p (&last_cfa, &next_cfa))
21093 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21094 start_label, 0, last_label, 0, section);
21096 list_tail = &(*list_tail)->dw_loc_next;
21097 last_cfa = next_cfa;
21098 start_label = last_label;
21100 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21101 start_label, 0, fde->dw_fde_end, 0, section);
21102 list_tail = &(*list_tail)->dw_loc_next;
21103 start_label = last_label = fde->dw_fde_second_begin;
21107 if (!cfa_equal_p (&last_cfa, &next_cfa))
21109 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
21110 start_label, 0, last_label, 0, section);
21111 list_tail = &(*list_tail)->dw_loc_next;
21112 start_label = last_label;
21115 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
21116 start_label, 0,
21117 fde->dw_fde_second_begin
21118 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
21119 section);
21121 maybe_gen_llsym (list);
21123 return list;
21126 /* Compute a displacement from the "steady-state frame pointer" to the
21127 frame base (often the same as the CFA), and store it in
21128 frame_pointer_fb_offset. OFFSET is added to the displacement
21129 before the latter is negated. */
21131 static void
21132 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
21134 rtx reg, elim;
21136 #ifdef FRAME_POINTER_CFA_OFFSET
21137 reg = frame_pointer_rtx;
21138 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
21139 #else
21140 reg = arg_pointer_rtx;
21141 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
21142 #endif
21144 elim = (ira_use_lra_p
21145 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
21146 : eliminate_regs (reg, VOIDmode, NULL_RTX));
21147 elim = strip_offset_and_add (elim, &offset);
21149 frame_pointer_fb_offset = -offset;
21151 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
21152 in which to eliminate. This is because it's stack pointer isn't
21153 directly accessible as a register within the ISA. To work around
21154 this, assume that while we cannot provide a proper value for
21155 frame_pointer_fb_offset, we won't need one either. We can use
21156 hard frame pointer in debug info even if frame pointer isn't used
21157 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21158 which uses the DW_AT_frame_base attribute, not hard frame pointer
21159 directly. */
21160 frame_pointer_fb_offset_valid
21161 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21164 /* Generate a DW_AT_name attribute given some string value to be included as
21165 the value of the attribute. */
21167 void
21168 add_name_attribute (dw_die_ref die, const char *name_string)
21170 if (name_string != NULL && *name_string != 0)
21172 if (demangle_name_func)
21173 name_string = (*demangle_name_func) (name_string);
21175 add_AT_string (die, DW_AT_name, name_string);
21179 /* Generate a DW_AT_name attribute given some string value representing a
21180 file or filepath to be included as value of the attribute. */
21181 static void
21182 add_filename_attribute (dw_die_ref die, const char *name_string)
21184 if (name_string != NULL && *name_string != 0)
21185 add_filepath_AT_string (die, DW_AT_name, name_string);
21188 /* Generate a DW_AT_description attribute given some string value to be included
21189 as the value of the attribute. */
21191 static void
21192 add_desc_attribute (dw_die_ref die, const char *name_string)
21194 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21195 return;
21197 if (name_string == NULL || *name_string == 0)
21198 return;
21200 if (demangle_name_func)
21201 name_string = (*demangle_name_func) (name_string);
21203 add_AT_string (die, DW_AT_description, name_string);
21206 /* Generate a DW_AT_description attribute given some decl to be included
21207 as the value of the attribute. */
21209 static void
21210 add_desc_attribute (dw_die_ref die, tree decl)
21212 tree decl_name;
21214 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21215 return;
21217 if (decl == NULL_TREE || !DECL_P (decl))
21218 return;
21219 decl_name = DECL_NAME (decl);
21221 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21223 const char *name = dwarf2_name (decl, 0);
21224 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21226 else
21228 char *desc = print_generic_expr_to_str (decl);
21229 add_desc_attribute (die, desc);
21230 free (desc);
21234 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21235 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21236 of TYPE accordingly.
21238 ??? This is a temporary measure until after we're able to generate
21239 regular DWARF for the complex Ada type system. */
21241 static void
21242 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21243 dw_die_ref context_die)
21245 tree dtype;
21246 dw_die_ref dtype_die;
21248 if (!lang_hooks.types.descriptive_type)
21249 return;
21251 dtype = lang_hooks.types.descriptive_type (type);
21252 if (!dtype)
21253 return;
21255 dtype_die = lookup_type_die (dtype);
21256 if (!dtype_die)
21258 gen_type_die (dtype, context_die);
21259 dtype_die = lookup_type_die (dtype);
21260 gcc_assert (dtype_die);
21263 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21266 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21268 static const char *
21269 comp_dir_string (void)
21271 const char *wd;
21272 char *wd_plus_sep = NULL;
21273 static const char *cached_wd = NULL;
21275 if (cached_wd != NULL)
21276 return cached_wd;
21278 wd = get_src_pwd ();
21279 if (wd == NULL)
21280 return NULL;
21282 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21284 size_t wdlen = strlen (wd);
21285 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21286 strcpy (wd_plus_sep, wd);
21287 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21288 wd_plus_sep [wdlen + 1] = 0;
21289 wd = wd_plus_sep;
21292 cached_wd = remap_debug_filename (wd);
21294 /* remap_debug_filename can just pass through wd or return a new gc string.
21295 These two types can't be both stored in a GTY(())-tagged string, but since
21296 the cached value lives forever just copy it if needed. */
21297 if (cached_wd != wd)
21299 cached_wd = xstrdup (cached_wd);
21300 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21301 free (wd_plus_sep);
21304 return cached_wd;
21307 /* Generate a DW_AT_comp_dir attribute for DIE. */
21309 static void
21310 add_comp_dir_attribute (dw_die_ref die)
21312 const char * wd = comp_dir_string ();
21313 if (wd != NULL)
21314 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21317 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21318 pointer computation, ...), output a representation for that bound according
21319 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21320 loc_list_from_tree for the meaning of CONTEXT. */
21322 static void
21323 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21324 int forms, struct loc_descr_context *context)
21326 dw_die_ref context_die, decl_die = NULL;
21327 dw_loc_list_ref list;
21328 bool strip_conversions = true;
21329 bool placeholder_seen = false;
21331 while (strip_conversions)
21332 switch (TREE_CODE (value))
21334 case ERROR_MARK:
21335 case SAVE_EXPR:
21336 return;
21338 CASE_CONVERT:
21339 case VIEW_CONVERT_EXPR:
21340 value = TREE_OPERAND (value, 0);
21341 break;
21343 default:
21344 strip_conversions = false;
21345 break;
21348 /* If possible and permitted, output the attribute as a constant. */
21349 if ((forms & dw_scalar_form_constant) != 0
21350 && TREE_CODE (value) == INTEGER_CST)
21352 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21354 /* If HOST_WIDE_INT is big enough then represent the bound as
21355 a constant value. We need to choose a form based on
21356 whether the type is signed or unsigned. We cannot just
21357 call add_AT_unsigned if the value itself is positive
21358 (add_AT_unsigned might add the unsigned value encoded as
21359 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21360 bounds type and then sign extend any unsigned values found
21361 for signed types. This is needed only for
21362 DW_AT_{lower,upper}_bound, since for most other attributes,
21363 consumers will treat DW_FORM_data[1248] as unsigned values,
21364 regardless of the underlying type. */
21365 if (prec <= HOST_BITS_PER_WIDE_INT
21366 || tree_fits_uhwi_p (value))
21368 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21369 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21370 else
21371 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21373 else if (dwarf_version >= 5
21374 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21375 /* Otherwise represent the bound as an unsigned value with
21376 the precision of its type. The precision and signedness
21377 of the type will be necessary to re-interpret it
21378 unambiguously. */
21379 add_AT_wide (die, attr, wi::to_wide (value));
21380 else
21382 rtx v = immed_wide_int_const (wi::to_wide (value),
21383 TYPE_MODE (TREE_TYPE (value)));
21384 dw_loc_descr_ref loc
21385 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21386 VAR_INIT_STATUS_INITIALIZED);
21387 if (loc)
21388 add_AT_loc (die, attr, loc);
21390 return;
21393 /* Otherwise, if it's possible and permitted too, output a reference to
21394 another DIE. */
21395 if ((forms & dw_scalar_form_reference) != 0)
21397 tree decl = NULL_TREE;
21399 /* Some type attributes reference an outer type. For instance, the upper
21400 bound of an array may reference an embedding record (this happens in
21401 Ada). */
21402 if (TREE_CODE (value) == COMPONENT_REF
21403 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21404 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21405 decl = TREE_OPERAND (value, 1);
21407 else if (VAR_P (value)
21408 || TREE_CODE (value) == PARM_DECL
21409 || TREE_CODE (value) == RESULT_DECL)
21410 decl = value;
21412 if (decl != NULL_TREE)
21414 decl_die = lookup_decl_die (decl);
21416 /* ??? Can this happen, or should the variable have been bound
21417 first? Probably it can, since I imagine that we try to create
21418 the types of parameters in the order in which they exist in
21419 the list, and won't have created a forward reference to a
21420 later parameter. */
21421 if (decl_die != NULL)
21423 if (get_AT (decl_die, DW_AT_location)
21424 || get_AT (decl_die, DW_AT_data_member_location)
21425 || get_AT (decl_die, DW_AT_data_bit_offset)
21426 || get_AT (decl_die, DW_AT_const_value))
21428 add_AT_die_ref (die, attr, decl_die);
21429 return;
21435 /* Last chance: try to create a stack operation procedure to evaluate the
21436 value. Do nothing if even that is not possible or permitted. */
21437 if ((forms & dw_scalar_form_exprloc) == 0)
21438 return;
21440 list = loc_list_from_tree (value, 2, context);
21441 if (context && context->placeholder_arg)
21443 placeholder_seen = context->placeholder_seen;
21444 context->placeholder_seen = false;
21446 if (list == NULL || single_element_loc_list_p (list))
21448 /* If this attribute is not a reference nor constant, it is
21449 a DWARF expression rather than location description. For that
21450 loc_list_from_tree (value, 0, &context) is needed. */
21451 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21452 if (list2 && single_element_loc_list_p (list2))
21454 if (placeholder_seen)
21456 struct dwarf_procedure_info dpi;
21457 dpi.fndecl = NULL_TREE;
21458 dpi.args_count = 1;
21459 if (!resolve_args_picking (list2->expr, 1, &dpi))
21460 return;
21462 add_AT_loc (die, attr, list2->expr);
21463 return;
21467 /* If that failed to give a single element location list, fall back to
21468 outputting this as a reference... still if permitted. */
21469 if (list == NULL
21470 || (forms & dw_scalar_form_reference) == 0
21471 || placeholder_seen)
21472 return;
21474 if (!decl_die)
21476 if (current_function_decl == 0)
21477 context_die = comp_unit_die ();
21478 else
21479 context_die = lookup_decl_die (current_function_decl);
21481 decl_die = new_die (DW_TAG_variable, context_die, value);
21482 add_AT_flag (decl_die, DW_AT_artificial, 1);
21483 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21484 context_die);
21487 add_AT_location_description (decl_die, DW_AT_location, list);
21488 add_AT_die_ref (die, attr, decl_die);
21491 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21492 default. */
21494 static int
21495 lower_bound_default (void)
21497 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21499 case DW_LANG_C:
21500 case DW_LANG_C89:
21501 case DW_LANG_C99:
21502 case DW_LANG_C11:
21503 case DW_LANG_C_plus_plus:
21504 case DW_LANG_C_plus_plus_11:
21505 case DW_LANG_C_plus_plus_14:
21506 case DW_LANG_ObjC:
21507 case DW_LANG_ObjC_plus_plus:
21508 return 0;
21509 case DW_LANG_Fortran77:
21510 case DW_LANG_Fortran90:
21511 case DW_LANG_Fortran95:
21512 case DW_LANG_Fortran03:
21513 case DW_LANG_Fortran08:
21514 return 1;
21515 case DW_LANG_UPC:
21516 case DW_LANG_D:
21517 case DW_LANG_Python:
21518 return dwarf_version >= 4 ? 0 : -1;
21519 case DW_LANG_Ada95:
21520 case DW_LANG_Ada83:
21521 case DW_LANG_Cobol74:
21522 case DW_LANG_Cobol85:
21523 case DW_LANG_Modula2:
21524 case DW_LANG_PLI:
21525 return dwarf_version >= 4 ? 1 : -1;
21526 default:
21527 return -1;
21531 /* Given a tree node describing an array bound (either lower or upper) output
21532 a representation for that bound. */
21534 static void
21535 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21536 tree bound, struct loc_descr_context *context)
21538 int dflt;
21540 while (1)
21541 switch (TREE_CODE (bound))
21543 /* Strip all conversions. */
21544 CASE_CONVERT:
21545 case VIEW_CONVERT_EXPR:
21546 bound = TREE_OPERAND (bound, 0);
21547 break;
21549 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21550 are even omitted when they are the default. */
21551 case INTEGER_CST:
21552 /* If the value for this bound is the default one, we can even omit the
21553 attribute. */
21554 if (bound_attr == DW_AT_lower_bound
21555 && tree_fits_shwi_p (bound)
21556 && (dflt = lower_bound_default ()) != -1
21557 && tree_to_shwi (bound) == dflt)
21558 return;
21560 /* FALLTHRU */
21562 default:
21563 /* Let GNAT encodings do the magic for self-referential bounds. */
21564 if (is_ada ()
21565 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21566 && contains_placeholder_p (bound))
21567 return;
21569 add_scalar_info (subrange_die, bound_attr, bound,
21570 dw_scalar_form_constant
21571 | dw_scalar_form_exprloc
21572 | dw_scalar_form_reference,
21573 context);
21574 return;
21578 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21579 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21581 This function reuses previously set type and bound information if
21582 available. */
21584 static void
21585 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21587 dw_die_ref child = type_die->die_child;
21588 struct array_descr_info info;
21589 int dimension_number;
21591 if (lang_hooks.types.get_array_descr_info)
21593 memset (&info, 0, sizeof (info));
21594 if (lang_hooks.types.get_array_descr_info (type, &info))
21595 /* Fortran sometimes emits array types with no dimension. */
21596 gcc_assert (info.ndimensions >= 0
21597 && info.ndimensions
21598 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21600 else
21601 info.ndimensions = 0;
21603 for (dimension_number = 0;
21604 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21605 type = TREE_TYPE (type), dimension_number++)
21607 tree domain = TYPE_DOMAIN (type);
21609 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21610 break;
21612 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21613 and (in GNU C only) variable bounds. Handle all three forms
21614 here. */
21616 /* Find and reuse a previously generated DW_TAG_subrange_type if
21617 available.
21619 For multi-dimensional arrays, as we iterate through the
21620 various dimensions in the enclosing for loop above, we also
21621 iterate through the DIE children and pick at each
21622 DW_TAG_subrange_type previously generated (if available).
21623 Each child DW_TAG_subrange_type DIE describes the range of
21624 the current dimension. At this point we should have as many
21625 DW_TAG_subrange_type's as we have dimensions in the
21626 array. */
21627 dw_die_ref subrange_die = NULL;
21628 if (child)
21629 while (1)
21631 child = child->die_sib;
21632 if (child->die_tag == DW_TAG_subrange_type)
21633 subrange_die = child;
21634 if (child == type_die->die_child)
21636 /* If we wrapped around, stop looking next time. */
21637 child = NULL;
21638 break;
21640 if (child->die_tag == DW_TAG_subrange_type)
21641 break;
21643 if (!subrange_die)
21644 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21646 if (domain)
21648 /* We have an array type with specified bounds. */
21649 tree lower = TYPE_MIN_VALUE (domain);
21650 tree upper = TYPE_MAX_VALUE (domain);
21651 tree index_type = TREE_TYPE (domain);
21653 if (dimension_number <= info.ndimensions - 1)
21655 lower = info.dimen[dimension_number].lower_bound;
21656 upper = info.dimen[dimension_number].upper_bound;
21657 index_type = info.dimen[dimension_number].bounds_type;
21660 /* Define the index type. */
21661 if (index_type && !get_AT (subrange_die, DW_AT_type))
21662 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21663 false, type_die);
21665 /* ??? If upper is NULL, the array has unspecified length,
21666 but it does have a lower bound. This happens with Fortran
21667 dimension arr(N:*)
21668 Since the debugger is definitely going to need to know N
21669 to produce useful results, go ahead and output the lower
21670 bound solo, and hope the debugger can cope. */
21672 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21673 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21675 if (!get_AT (subrange_die, DW_AT_upper_bound)
21676 && !get_AT (subrange_die, DW_AT_count))
21678 if (upper)
21679 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21680 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21681 /* Zero-length array. */
21682 add_bound_info (subrange_die, DW_AT_count,
21683 build_int_cst (TREE_TYPE (lower), 0), NULL);
21687 /* Otherwise we have an array type with an unspecified length. The
21688 DWARF-2 spec does not say how to handle this; let's just leave out the
21689 bounds. */
21693 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21695 static void
21696 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21698 dw_die_ref decl_die;
21699 HOST_WIDE_INT size;
21701 switch (TREE_CODE (tree_node))
21703 case ERROR_MARK:
21704 size = 0;
21705 break;
21706 case ENUMERAL_TYPE:
21707 case RECORD_TYPE:
21708 case UNION_TYPE:
21709 case QUAL_UNION_TYPE:
21710 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21711 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21713 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21714 return;
21716 size = int_size_in_bytes (tree_node);
21717 break;
21718 case FIELD_DECL:
21719 /* For a data member of a struct or union, the DW_AT_byte_size is
21720 generally given as the number of bytes normally allocated for an
21721 object of the *declared* type of the member itself. This is true
21722 even for bit-fields. */
21723 size = int_size_in_bytes (field_type (tree_node));
21724 break;
21725 default:
21726 gcc_unreachable ();
21729 /* Note that `size' might be -1 when we get to this point. If it is, that
21730 indicates that the byte size of the entity in question is variable. */
21731 if (size >= 0)
21732 add_AT_unsigned (die, DW_AT_byte_size, size);
21734 /* Support for dynamically-sized objects was introduced in DWARF3. */
21735 else if (TYPE_P (tree_node)
21736 && (dwarf_version >= 3 || !dwarf_strict)
21737 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21739 struct loc_descr_context ctx = {
21740 const_cast<tree> (tree_node), /* context_type */
21741 NULL_TREE, /* base_decl */
21742 NULL, /* dpi */
21743 false, /* placeholder_arg */
21744 false, /* placeholder_seen */
21745 false /* strict_signedness */
21748 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21749 add_scalar_info (die, DW_AT_byte_size, tree_size,
21750 dw_scalar_form_constant
21751 | dw_scalar_form_exprloc
21752 | dw_scalar_form_reference,
21753 &ctx);
21757 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21758 alignment. */
21760 static void
21761 add_alignment_attribute (dw_die_ref die, tree tree_node)
21763 if (dwarf_version < 5 && dwarf_strict)
21764 return;
21766 unsigned align;
21768 if (DECL_P (tree_node))
21770 if (!DECL_USER_ALIGN (tree_node))
21771 return;
21773 align = DECL_ALIGN_UNIT (tree_node);
21775 else if (TYPE_P (tree_node))
21777 if (!TYPE_USER_ALIGN (tree_node))
21778 return;
21780 align = TYPE_ALIGN_UNIT (tree_node);
21782 else
21783 gcc_unreachable ();
21785 add_AT_unsigned (die, DW_AT_alignment, align);
21788 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21789 which specifies the distance in bits from the highest order bit of the
21790 "containing object" for the bit-field to the highest order bit of the
21791 bit-field itself.
21793 For any given bit-field, the "containing object" is a hypothetical object
21794 (of some integral or enum type) within which the given bit-field lives. The
21795 type of this hypothetical "containing object" is always the same as the
21796 declared type of the individual bit-field itself. The determination of the
21797 exact location of the "containing object" for a bit-field is rather
21798 complicated. It's handled by the `field_byte_offset' function (above).
21800 Note that it is the size (in bytes) of the hypothetical "containing object"
21801 which will be given in the DW_AT_byte_size attribute for this bit-field.
21802 (See `byte_size_attribute' above). */
21804 static inline void
21805 add_bit_offset_attribute (dw_die_ref die, tree decl)
21807 HOST_WIDE_INT object_offset_in_bytes;
21808 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21809 HOST_WIDE_INT bitpos_int;
21810 HOST_WIDE_INT highest_order_object_bit_offset;
21811 HOST_WIDE_INT highest_order_field_bit_offset;
21812 HOST_WIDE_INT bit_offset;
21814 /* The containing object is within the DECL_CONTEXT. */
21815 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21817 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21819 /* Must be a field and a bit field. */
21820 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21822 /* We can't yet handle bit-fields whose offsets are variable, so if we
21823 encounter such things, just return without generating any attribute
21824 whatsoever. Likewise for variable or too large size. */
21825 if (! tree_fits_shwi_p (bit_position (decl))
21826 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21827 return;
21829 bitpos_int = int_bit_position (decl);
21831 /* Note that the bit offset is always the distance (in bits) from the
21832 highest-order bit of the "containing object" to the highest-order bit of
21833 the bit-field itself. Since the "high-order end" of any object or field
21834 is different on big-endian and little-endian machines, the computation
21835 below must take account of these differences. */
21836 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21837 highest_order_field_bit_offset = bitpos_int;
21839 if (! BYTES_BIG_ENDIAN)
21841 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21842 highest_order_object_bit_offset +=
21843 simple_type_size_in_bits (original_type);
21846 bit_offset
21847 = (! BYTES_BIG_ENDIAN
21848 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21849 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21851 if (bit_offset < 0)
21852 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21853 else
21854 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21857 /* For a FIELD_DECL node which represents a bit field, output an attribute
21858 which specifies the length in bits of the given field. */
21860 static inline void
21861 add_bit_size_attribute (dw_die_ref die, tree decl)
21863 /* Must be a field and a bit field. */
21864 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21865 && DECL_BIT_FIELD_TYPE (decl));
21867 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21868 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21871 /* If the compiled language is ANSI C, then add a 'prototyped'
21872 attribute, if arg types are given for the parameters of a function. */
21874 static inline void
21875 add_prototyped_attribute (dw_die_ref die, tree func_type)
21877 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21879 case DW_LANG_C:
21880 case DW_LANG_C89:
21881 case DW_LANG_C99:
21882 case DW_LANG_C11:
21883 case DW_LANG_ObjC:
21884 if (prototype_p (func_type))
21885 add_AT_flag (die, DW_AT_prototyped, 1);
21886 break;
21887 default:
21888 break;
21892 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21893 by looking in the type declaration, the object declaration equate table or
21894 the block mapping. */
21896 static inline void
21897 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21899 dw_die_ref origin_die = NULL;
21901 /* For late LTO debug output we want to refer directly to the abstract
21902 DIE in the early debug rather to the possibly existing concrete
21903 instance and avoid creating that just for this purpose. */
21904 sym_off_pair *desc;
21905 if (in_lto_p
21906 && external_die_map
21907 && (desc = external_die_map->get (origin)))
21909 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21910 desc->sym, desc->off);
21911 return;
21914 if (DECL_P (origin))
21915 origin_die = lookup_decl_die (origin);
21916 else if (TYPE_P (origin))
21917 origin_die = lookup_type_die (origin);
21918 else if (TREE_CODE (origin) == BLOCK)
21919 origin_die = lookup_block_die (origin);
21921 /* XXX: Functions that are never lowered don't always have correct block
21922 trees (in the case of java, they simply have no block tree, in some other
21923 languages). For these functions, there is nothing we can really do to
21924 output correct debug info for inlined functions in all cases. Rather
21925 than die, we'll just produce deficient debug info now, in that we will
21926 have variables without a proper abstract origin. In the future, when all
21927 functions are lowered, we should re-add a gcc_assert (origin_die)
21928 here. */
21930 if (origin_die)
21932 dw_attr_node *a;
21933 /* Like above, if we already created a concrete instance DIE
21934 do not use that for the abstract origin but the early DIE
21935 if present. */
21936 if (in_lto_p
21937 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21938 origin_die = AT_ref (a);
21939 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21943 /* We do not currently support the pure_virtual attribute. */
21945 static inline void
21946 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21948 if (DECL_VINDEX (func_decl))
21950 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21952 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21953 add_AT_loc (die, DW_AT_vtable_elem_location,
21954 new_loc_descr (DW_OP_constu,
21955 tree_to_shwi (DECL_VINDEX (func_decl)),
21956 0));
21958 /* GNU extension: Record what type this method came from originally. */
21959 if (debug_info_level > DINFO_LEVEL_TERSE
21960 && DECL_CONTEXT (func_decl))
21961 add_AT_die_ref (die, DW_AT_containing_type,
21962 lookup_type_die (DECL_CONTEXT (func_decl)));
21966 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21967 given decl. This used to be a vendor extension until after DWARF 4
21968 standardized it. */
21970 static void
21971 add_linkage_attr (dw_die_ref die, tree decl)
21973 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21975 /* Mimic what assemble_name_raw does with a leading '*'. */
21976 if (name[0] == '*')
21977 name = &name[1];
21979 if (dwarf_version >= 4)
21980 add_AT_string (die, DW_AT_linkage_name, name);
21981 else
21982 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21985 /* Add source coordinate attributes for the given decl. */
21987 static void
21988 add_src_coords_attributes (dw_die_ref die, tree decl)
21990 expanded_location s;
21992 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21993 return;
21994 s = expand_location (DECL_SOURCE_LOCATION (decl));
21995 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21996 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21997 if (debug_column_info && s.column)
21998 add_AT_unsigned (die, DW_AT_decl_column, s.column);
22001 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
22003 static void
22004 add_linkage_name_raw (dw_die_ref die, tree decl)
22006 /* Defer until we have an assembler name set. */
22007 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
22009 limbo_die_node *asm_name;
22011 asm_name = ggc_cleared_alloc<limbo_die_node> ();
22012 asm_name->die = die;
22013 asm_name->created_for = decl;
22014 asm_name->next = deferred_asm_name;
22015 deferred_asm_name = asm_name;
22017 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22018 add_linkage_attr (die, decl);
22021 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
22023 static void
22024 add_linkage_name (dw_die_ref die, tree decl)
22026 if (debug_info_level > DINFO_LEVEL_NONE
22027 && VAR_OR_FUNCTION_DECL_P (decl)
22028 && TREE_PUBLIC (decl)
22029 && !(VAR_P (decl) && DECL_REGISTER (decl))
22030 && die->die_tag != DW_TAG_member)
22031 add_linkage_name_raw (die, decl);
22034 /* Add a DW_AT_name attribute and source coordinate attribute for the
22035 given decl, but only if it actually has a name. */
22037 static void
22038 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
22039 bool no_linkage_name)
22041 tree decl_name;
22043 decl_name = DECL_NAME (decl);
22044 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
22046 const char *name = dwarf2_name (decl, 0);
22047 if (name)
22048 add_name_attribute (die, name);
22049 else
22050 add_desc_attribute (die, decl);
22052 if (! DECL_ARTIFICIAL (decl))
22053 add_src_coords_attributes (die, decl);
22055 if (!no_linkage_name)
22056 add_linkage_name (die, decl);
22058 else
22059 add_desc_attribute (die, decl);
22061 #ifdef VMS_DEBUGGING_INFO
22062 /* Get the function's name, as described by its RTL. This may be different
22063 from the DECL_NAME name used in the source file. */
22064 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
22066 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
22067 XEXP (DECL_RTL (decl), 0), false);
22068 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
22070 #endif /* VMS_DEBUGGING_INFO */
22073 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
22075 static void
22076 add_discr_value (dw_die_ref die, dw_discr_value *value)
22078 dw_attr_node attr;
22080 attr.dw_attr = DW_AT_discr_value;
22081 attr.dw_attr_val.val_class = dw_val_class_discr_value;
22082 attr.dw_attr_val.val_entry = NULL;
22083 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
22084 if (value->pos)
22085 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
22086 else
22087 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
22088 add_dwarf_attr (die, &attr);
22091 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
22093 static void
22094 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
22096 dw_attr_node attr;
22098 attr.dw_attr = DW_AT_discr_list;
22099 attr.dw_attr_val.val_class = dw_val_class_discr_list;
22100 attr.dw_attr_val.val_entry = NULL;
22101 attr.dw_attr_val.v.val_discr_list = discr_list;
22102 add_dwarf_attr (die, &attr);
22105 static inline dw_discr_list_ref
22106 AT_discr_list (dw_attr_node *attr)
22108 return attr->dw_attr_val.v.val_discr_list;
22111 #ifdef VMS_DEBUGGING_INFO
22112 /* Output the debug main pointer die for VMS */
22114 void
22115 dwarf2out_vms_debug_main_pointer (void)
22117 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22118 dw_die_ref die;
22120 /* Allocate the VMS debug main subprogram die. */
22121 die = new_die_raw (DW_TAG_subprogram);
22122 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
22123 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
22124 current_function_funcdef_no);
22125 add_AT_lbl_id (die, DW_AT_entry_pc, label);
22127 /* Make it the first child of comp_unit_die (). */
22128 die->die_parent = comp_unit_die ();
22129 if (comp_unit_die ()->die_child)
22131 die->die_sib = comp_unit_die ()->die_child->die_sib;
22132 comp_unit_die ()->die_child->die_sib = die;
22134 else
22136 die->die_sib = die;
22137 comp_unit_die ()->die_child = die;
22140 #endif /* VMS_DEBUGGING_INFO */
22142 /* walk_tree helper function for uses_local_type, below. */
22144 static tree
22145 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
22147 if (!TYPE_P (*tp))
22148 *walk_subtrees = 0;
22149 else
22151 tree name = TYPE_NAME (*tp);
22152 if (name && DECL_P (name) && decl_function_context (name))
22153 return *tp;
22155 return NULL_TREE;
22158 /* If TYPE involves a function-local type (including a local typedef to a
22159 non-local type), returns that type; otherwise returns NULL_TREE. */
22161 static tree
22162 uses_local_type (tree type)
22164 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22165 return used;
22168 /* Return the DIE for the scope that immediately contains this type.
22169 Non-named types that do not involve a function-local type get global
22170 scope. Named types nested in namespaces or other types get their
22171 containing scope. All other types (i.e. function-local named types) get
22172 the current active scope. */
22174 static dw_die_ref
22175 scope_die_for (tree t, dw_die_ref context_die)
22177 dw_die_ref scope_die = NULL;
22178 tree containing_scope;
22180 /* Non-types always go in the current scope. */
22181 gcc_assert (TYPE_P (t));
22183 /* Use the scope of the typedef, rather than the scope of the type
22184 it refers to. */
22185 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22186 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22187 else
22188 containing_scope = TYPE_CONTEXT (t);
22190 /* Use the containing namespace if there is one. */
22191 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22193 if (context_die == lookup_decl_die (containing_scope))
22194 /* OK */;
22195 else if (debug_info_level > DINFO_LEVEL_TERSE)
22196 context_die = get_context_die (containing_scope);
22197 else
22198 containing_scope = NULL_TREE;
22201 /* Ignore function type "scopes" from the C frontend. They mean that
22202 a tagged type is local to a parmlist of a function declarator, but
22203 that isn't useful to DWARF. */
22204 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22205 containing_scope = NULL_TREE;
22207 if (SCOPE_FILE_SCOPE_P (containing_scope))
22209 /* If T uses a local type keep it local as well, to avoid references
22210 to function-local DIEs from outside the function. */
22211 if (current_function_decl && uses_local_type (t))
22212 scope_die = context_die;
22213 else
22214 scope_die = comp_unit_die ();
22216 else if (TYPE_P (containing_scope))
22218 /* For types, we can just look up the appropriate DIE. */
22219 if (debug_info_level > DINFO_LEVEL_TERSE)
22220 scope_die = get_context_die (containing_scope);
22221 else
22223 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22224 if (scope_die == NULL)
22225 scope_die = comp_unit_die ();
22228 else
22229 scope_die = context_die;
22231 return scope_die;
22234 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22236 static inline int
22237 local_scope_p (dw_die_ref context_die)
22239 for (; context_die; context_die = context_die->die_parent)
22240 if (context_die->die_tag == DW_TAG_inlined_subroutine
22241 || context_die->die_tag == DW_TAG_subprogram)
22242 return 1;
22244 return 0;
22247 /* Returns nonzero if CONTEXT_DIE is a class. */
22249 static inline int
22250 class_scope_p (dw_die_ref context_die)
22252 return (context_die
22253 && (context_die->die_tag == DW_TAG_structure_type
22254 || context_die->die_tag == DW_TAG_class_type
22255 || context_die->die_tag == DW_TAG_interface_type
22256 || context_die->die_tag == DW_TAG_union_type));
22259 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22260 whether or not to treat a DIE in this context as a declaration. */
22262 static inline int
22263 class_or_namespace_scope_p (dw_die_ref context_die)
22265 return (class_scope_p (context_die)
22266 || (context_die && context_die->die_tag == DW_TAG_namespace));
22269 /* Many forms of DIEs require a "type description" attribute. This
22270 routine locates the proper "type descriptor" die for the type given
22271 by 'type' plus any additional qualifiers given by 'cv_quals', and
22272 adds a DW_AT_type attribute below the given die. */
22274 static void
22275 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22276 bool reverse, dw_die_ref context_die)
22278 enum tree_code code = TREE_CODE (type);
22279 dw_die_ref type_die = NULL;
22281 if (debug_info_level <= DINFO_LEVEL_TERSE)
22282 return;
22284 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22285 or fixed-point type, use the inner type. This is because we have no
22286 support for unnamed types in base_type_die. This can happen if this is
22287 an Ada subrange type. Correct solution is emit a subrange type die. */
22288 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22289 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22290 type = TREE_TYPE (type), code = TREE_CODE (type);
22292 if (code == ERROR_MARK
22293 /* Handle a special case. For functions whose return type is void, we
22294 generate *no* type attribute. (Note that no object may have type
22295 `void', so this only applies to function return types). */
22296 || code == VOID_TYPE)
22297 return;
22299 type_die = modified_type_die (type,
22300 cv_quals | TYPE_QUALS (type),
22301 reverse,
22302 context_die);
22304 if (type_die != NULL)
22305 add_AT_die_ref (object_die, DW_AT_type, type_die);
22308 /* Given an object die, add the calling convention attribute for the
22309 function call type. */
22310 static void
22311 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22313 enum dwarf_calling_convention value = DW_CC_normal;
22315 value = ((enum dwarf_calling_convention)
22316 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22318 if (is_fortran ()
22319 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22321 /* DWARF 2 doesn't provide a way to identify a program's source-level
22322 entry point. DW_AT_calling_convention attributes are only meant
22323 to describe functions' calling conventions. However, lacking a
22324 better way to signal the Fortran main program, we used this for
22325 a long time, following existing custom. Now, DWARF 4 has
22326 DW_AT_main_subprogram, which we add below, but some tools still
22327 rely on the old way, which we thus keep. */
22328 value = DW_CC_program;
22330 if (dwarf_version >= 4 || !dwarf_strict)
22331 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22334 /* Only add the attribute if the backend requests it, and
22335 is not DW_CC_normal. */
22336 if (value && (value != DW_CC_normal))
22337 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22340 /* Given a tree pointer to a struct, class, union, or enum type node, return
22341 a pointer to the (string) tag name for the given type, or zero if the type
22342 was declared without a tag. */
22344 static const char *
22345 type_tag (const_tree type)
22347 const char *name = 0;
22349 if (TYPE_NAME (type) != 0)
22351 tree t = 0;
22353 /* Find the IDENTIFIER_NODE for the type name. */
22354 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22355 && !TYPE_NAMELESS (type))
22356 t = TYPE_NAME (type);
22358 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22359 a TYPE_DECL node, regardless of whether or not a `typedef' was
22360 involved. */
22361 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22362 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22364 /* We want to be extra verbose. Don't call dwarf_name if
22365 DECL_NAME isn't set. The default hook for decl_printable_name
22366 doesn't like that, and in this context it's correct to return
22367 0, instead of "<anonymous>" or the like. */
22368 if (DECL_NAME (TYPE_NAME (type))
22369 && !DECL_NAMELESS (TYPE_NAME (type)))
22370 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22373 /* Now get the name as a string, or invent one. */
22374 if (!name && t != 0)
22375 name = IDENTIFIER_POINTER (t);
22378 return (name == 0 || *name == '\0') ? 0 : name;
22381 /* Return the type associated with a data member, make a special check
22382 for bit field types. */
22384 static inline tree
22385 member_declared_type (const_tree member)
22387 return (DECL_BIT_FIELD_TYPE (member)
22388 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22391 /* Get the decl's label, as described by its RTL. This may be different
22392 from the DECL_NAME name used in the source file. */
22394 #if 0
22395 static const char *
22396 decl_start_label (tree decl)
22398 rtx x;
22399 const char *fnname;
22401 x = DECL_RTL (decl);
22402 gcc_assert (MEM_P (x));
22404 x = XEXP (x, 0);
22405 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22407 fnname = XSTR (x, 0);
22408 return fnname;
22410 #endif
22412 /* For variable-length arrays that have been previously generated, but
22413 may be incomplete due to missing subscript info, fill the subscript
22414 info. Return TRUE if this is one of those cases. */
22416 static bool
22417 fill_variable_array_bounds (tree type)
22419 if (TREE_ASM_WRITTEN (type)
22420 && TREE_CODE (type) == ARRAY_TYPE
22421 && variably_modified_type_p (type, NULL))
22423 dw_die_ref array_die = lookup_type_die (type);
22424 if (!array_die)
22425 return false;
22426 add_subscript_info (array_die, type, !is_ada ());
22427 return true;
22429 return false;
22432 /* These routines generate the internal representation of the DIE's for
22433 the compilation unit. Debugging information is collected by walking
22434 the declaration trees passed in from dwarf2out_decl(). */
22436 static void
22437 gen_array_type_die (tree type, dw_die_ref context_die)
22439 dw_die_ref array_die;
22441 /* GNU compilers represent multidimensional array types as sequences of one
22442 dimensional array types whose element types are themselves array types.
22443 We sometimes squish that down to a single array_type DIE with multiple
22444 subscripts in the Dwarf debugging info. The draft Dwarf specification
22445 say that we are allowed to do this kind of compression in C, because
22446 there is no difference between an array of arrays and a multidimensional
22447 array. We don't do this for Ada to remain as close as possible to the
22448 actual representation, which is especially important against the language
22449 flexibilty wrt arrays of variable size. */
22451 bool collapse_nested_arrays = !is_ada ();
22453 if (fill_variable_array_bounds (type))
22454 return;
22456 dw_die_ref scope_die = scope_die_for (type, context_die);
22457 tree element_type;
22459 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22460 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22461 if (TREE_CODE (type) == ARRAY_TYPE
22462 && TYPE_STRING_FLAG (type)
22463 && is_fortran ()
22464 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22466 HOST_WIDE_INT size;
22468 array_die = new_die (DW_TAG_string_type, scope_die, type);
22469 add_name_attribute (array_die, type_tag (type));
22470 equate_type_number_to_die (type, array_die);
22471 size = int_size_in_bytes (type);
22472 if (size >= 0)
22473 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22474 /* ??? We can't annotate types late, but for LTO we may not
22475 generate a location early either (gfortran.dg/save_6.f90). */
22476 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22477 && TYPE_DOMAIN (type) != NULL_TREE
22478 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22480 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22481 tree rszdecl = szdecl;
22483 size = int_size_in_bytes (TREE_TYPE (szdecl));
22484 if (!DECL_P (szdecl))
22486 if (TREE_CODE (szdecl) == INDIRECT_REF
22487 && DECL_P (TREE_OPERAND (szdecl, 0)))
22489 rszdecl = TREE_OPERAND (szdecl, 0);
22490 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22491 != DWARF2_ADDR_SIZE)
22492 size = 0;
22494 else
22495 size = 0;
22497 if (size > 0)
22499 dw_loc_list_ref loc
22500 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22501 NULL);
22502 if (loc)
22504 add_AT_location_description (array_die, DW_AT_string_length,
22505 loc);
22506 if (size != DWARF2_ADDR_SIZE)
22507 add_AT_unsigned (array_die, dwarf_version >= 5
22508 ? DW_AT_string_length_byte_size
22509 : DW_AT_byte_size, size);
22513 return;
22516 array_die = new_die (DW_TAG_array_type, scope_die, type);
22517 add_name_attribute (array_die, type_tag (type));
22518 equate_type_number_to_die (type, array_die);
22520 if (TREE_CODE (type) == VECTOR_TYPE)
22521 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22523 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22524 if (is_fortran ()
22525 && TREE_CODE (type) == ARRAY_TYPE
22526 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22527 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22528 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22530 #if 0
22531 /* We default the array ordering. Debuggers will probably do the right
22532 things even if DW_AT_ordering is not present. It's not even an issue
22533 until we start to get into multidimensional arrays anyway. If a debugger
22534 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22535 then we'll have to put the DW_AT_ordering attribute back in. (But if
22536 and when we find out that we need to put these in, we will only do so
22537 for multidimensional arrays. */
22538 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22539 #endif
22541 if (TREE_CODE (type) == VECTOR_TYPE)
22543 /* For VECTOR_TYPEs we use an array DIE with appropriate bounds. */
22544 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22545 int lb = lower_bound_default ();
22546 if (lb == -1)
22547 lb = 0;
22548 add_bound_info (subrange_die, DW_AT_lower_bound, size_int (lb), NULL);
22549 add_bound_info (subrange_die, DW_AT_upper_bound,
22550 size_int (lb + TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22552 else
22553 add_subscript_info (array_die, type, collapse_nested_arrays);
22555 /* Add representation of the type of the elements of this array type and
22556 emit the corresponding DIE if we haven't done it already. */
22557 element_type = TREE_TYPE (type);
22558 if (collapse_nested_arrays)
22559 while (TREE_CODE (element_type) == ARRAY_TYPE)
22561 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22562 break;
22563 element_type = TREE_TYPE (element_type);
22566 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22567 TREE_CODE (type) == ARRAY_TYPE
22568 && TYPE_REVERSE_STORAGE_ORDER (type),
22569 context_die);
22571 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22572 if (TYPE_ARTIFICIAL (type))
22573 add_AT_flag (array_die, DW_AT_artificial, 1);
22575 if (get_AT (array_die, DW_AT_name))
22576 add_pubtype (type, array_die);
22578 add_alignment_attribute (array_die, type);
22581 /* This routine generates DIE for array with hidden descriptor, details
22582 are filled into *info by a langhook. */
22584 static void
22585 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22586 dw_die_ref context_die)
22588 const dw_die_ref scope_die = scope_die_for (type, context_die);
22589 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22590 struct loc_descr_context context = {
22591 type, /* context_type */
22592 info->base_decl, /* base_decl */
22593 NULL, /* dpi */
22594 false, /* placeholder_arg */
22595 false, /* placeholder_seen */
22596 false /* strict_signedness */
22598 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22599 int dim;
22601 add_name_attribute (array_die, type_tag (type));
22602 equate_type_number_to_die (type, array_die);
22604 if (info->ndimensions > 1)
22605 switch (info->ordering)
22607 case array_descr_ordering_row_major:
22608 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22609 break;
22610 case array_descr_ordering_column_major:
22611 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22612 break;
22613 default:
22614 break;
22617 if (dwarf_version >= 3 || !dwarf_strict)
22619 if (info->data_location)
22620 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22621 dw_scalar_form_exprloc, &context);
22622 if (info->associated)
22623 add_scalar_info (array_die, DW_AT_associated, info->associated,
22624 dw_scalar_form_constant
22625 | dw_scalar_form_exprloc
22626 | dw_scalar_form_reference, &context);
22627 if (info->allocated)
22628 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22629 dw_scalar_form_constant
22630 | dw_scalar_form_exprloc
22631 | dw_scalar_form_reference, &context);
22632 if (info->stride)
22634 const enum dwarf_attribute attr
22635 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22636 const int forms
22637 = (info->stride_in_bits)
22638 ? dw_scalar_form_constant
22639 : (dw_scalar_form_constant
22640 | dw_scalar_form_exprloc
22641 | dw_scalar_form_reference);
22643 add_scalar_info (array_die, attr, info->stride, forms, &context);
22646 if (dwarf_version >= 5)
22648 if (info->rank)
22650 add_scalar_info (array_die, DW_AT_rank, info->rank,
22651 dw_scalar_form_constant
22652 | dw_scalar_form_exprloc, &context);
22653 subrange_tag = DW_TAG_generic_subrange;
22654 context.placeholder_arg = true;
22658 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22660 for (dim = 0; dim < info->ndimensions; dim++)
22662 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22664 if (info->dimen[dim].bounds_type)
22665 add_type_attribute (subrange_die,
22666 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22667 false, context_die);
22668 if (info->dimen[dim].lower_bound)
22669 add_bound_info (subrange_die, DW_AT_lower_bound,
22670 info->dimen[dim].lower_bound, &context);
22671 if (info->dimen[dim].upper_bound)
22672 add_bound_info (subrange_die, DW_AT_upper_bound,
22673 info->dimen[dim].upper_bound, &context);
22674 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22675 add_scalar_info (subrange_die, DW_AT_byte_stride,
22676 info->dimen[dim].stride,
22677 dw_scalar_form_constant
22678 | dw_scalar_form_exprloc
22679 | dw_scalar_form_reference,
22680 &context);
22683 gen_type_die (info->element_type, context_die);
22684 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22685 TREE_CODE (type) == ARRAY_TYPE
22686 && TYPE_REVERSE_STORAGE_ORDER (type),
22687 context_die);
22689 if (get_AT (array_die, DW_AT_name))
22690 add_pubtype (type, array_die);
22692 add_alignment_attribute (array_die, type);
22695 #if 0
22696 static void
22697 gen_entry_point_die (tree decl, dw_die_ref context_die)
22699 tree origin = decl_ultimate_origin (decl);
22700 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22702 if (origin != NULL)
22703 add_abstract_origin_attribute (decl_die, origin);
22704 else
22706 add_name_and_src_coords_attributes (decl_die, decl);
22707 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22708 TYPE_UNQUALIFIED, false, context_die);
22711 if (DECL_ABSTRACT_P (decl))
22712 equate_decl_number_to_die (decl, decl_die);
22713 else
22714 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22716 #endif
22718 /* Walk through the list of incomplete types again, trying once more to
22719 emit full debugging info for them. */
22721 static void
22722 retry_incomplete_types (void)
22724 set_early_dwarf s;
22725 int i;
22727 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22728 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22729 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22730 vec_safe_truncate (incomplete_types, 0);
22733 /* Determine what tag to use for a record type. */
22735 static enum dwarf_tag
22736 record_type_tag (tree type)
22738 if (! lang_hooks.types.classify_record)
22739 return DW_TAG_structure_type;
22741 switch (lang_hooks.types.classify_record (type))
22743 case RECORD_IS_STRUCT:
22744 return DW_TAG_structure_type;
22746 case RECORD_IS_CLASS:
22747 return DW_TAG_class_type;
22749 case RECORD_IS_INTERFACE:
22750 if (dwarf_version >= 3 || !dwarf_strict)
22751 return DW_TAG_interface_type;
22752 return DW_TAG_structure_type;
22754 default:
22755 gcc_unreachable ();
22759 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22760 include all of the information about the enumeration values also. Each
22761 enumerated type name/value is listed as a child of the enumerated type
22762 DIE. */
22764 static dw_die_ref
22765 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22767 dw_die_ref type_die = lookup_type_die (type);
22768 dw_die_ref orig_type_die = type_die;
22770 if (type_die == NULL)
22772 type_die = new_die (DW_TAG_enumeration_type,
22773 scope_die_for (type, context_die), type);
22774 equate_type_number_to_die (type, type_die);
22775 add_name_attribute (type_die, type_tag (type));
22776 if ((dwarf_version >= 4 || !dwarf_strict)
22777 && ENUM_IS_SCOPED (type))
22778 add_AT_flag (type_die, DW_AT_enum_class, 1);
22779 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22780 add_AT_flag (type_die, DW_AT_declaration, 1);
22781 if (!dwarf_strict)
22782 add_AT_unsigned (type_die, DW_AT_encoding,
22783 TYPE_UNSIGNED (type)
22784 ? DW_ATE_unsigned
22785 : DW_ATE_signed);
22787 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22788 return type_die;
22789 else
22790 remove_AT (type_die, DW_AT_declaration);
22792 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22793 given enum type is incomplete, do not generate the DW_AT_byte_size
22794 attribute or the DW_AT_element_list attribute. */
22795 if (TYPE_SIZE (type))
22797 tree link;
22799 if (!ENUM_IS_OPAQUE (type))
22800 TREE_ASM_WRITTEN (type) = 1;
22801 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22802 add_byte_size_attribute (type_die, type);
22803 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22804 add_alignment_attribute (type_die, type);
22805 if ((dwarf_version >= 3 || !dwarf_strict)
22806 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22808 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22809 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22810 context_die);
22812 if (TYPE_STUB_DECL (type) != NULL_TREE)
22814 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22815 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22816 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22817 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22820 /* If the first reference to this type was as the return type of an
22821 inline function, then it may not have a parent. Fix this now. */
22822 if (type_die->die_parent == NULL)
22823 add_child_die (scope_die_for (type, context_die), type_die);
22825 for (link = TYPE_VALUES (type);
22826 link != NULL; link = TREE_CHAIN (link))
22828 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22829 tree value = TREE_VALUE (link);
22831 if (DECL_P (value))
22832 equate_decl_number_to_die (value, enum_die);
22834 gcc_assert (!ENUM_IS_OPAQUE (type));
22835 add_name_attribute (enum_die,
22836 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22838 if (TREE_CODE (value) == CONST_DECL)
22839 value = DECL_INITIAL (value);
22841 if (simple_type_size_in_bits (TREE_TYPE (value))
22842 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22844 /* For constant forms created by add_AT_unsigned DWARF
22845 consumers (GDB, elfutils, etc.) always zero extend
22846 the value. Only when the actual value is negative
22847 do we need to use add_AT_int to generate a constant
22848 form that can represent negative values. */
22849 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22850 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22851 add_AT_unsigned (enum_die, DW_AT_const_value,
22852 (unsigned HOST_WIDE_INT) val);
22853 else
22854 add_AT_int (enum_die, DW_AT_const_value, val);
22856 else
22857 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22858 that here. TODO: This should be re-worked to use correct
22859 signed/unsigned double tags for all cases. */
22860 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22863 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22864 if (TYPE_ARTIFICIAL (type)
22865 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22866 add_AT_flag (type_die, DW_AT_artificial, 1);
22868 else
22869 add_AT_flag (type_die, DW_AT_declaration, 1);
22871 add_pubtype (type, type_die);
22873 return type_die;
22876 /* Generate a DIE to represent either a real live formal parameter decl or to
22877 represent just the type of some formal parameter position in some function
22878 type.
22880 Note that this routine is a bit unusual because its argument may be a
22881 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22882 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22883 node. If it's the former then this function is being called to output a
22884 DIE to represent a formal parameter object (or some inlining thereof). If
22885 it's the latter, then this function is only being called to output a
22886 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22887 argument type of some subprogram type.
22888 If EMIT_NAME_P is true, name and source coordinate attributes
22889 are emitted. */
22891 static dw_die_ref
22892 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22893 dw_die_ref context_die)
22895 tree node_or_origin = node ? node : origin;
22896 tree ultimate_origin;
22897 dw_die_ref parm_die = NULL;
22899 if (DECL_P (node_or_origin))
22901 parm_die = lookup_decl_die (node);
22903 /* If the contexts differ, we may not be talking about the same
22904 thing.
22905 ??? When in LTO the DIE parent is the "abstract" copy and the
22906 context_die is the specification "copy". */
22907 if (parm_die
22908 && parm_die->die_parent != context_die
22909 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22910 || parm_die->die_parent->die_parent != context_die)
22911 && !in_lto_p)
22913 gcc_assert (!DECL_ABSTRACT_P (node));
22914 /* This can happen when creating a concrete instance, in
22915 which case we need to create a new DIE that will get
22916 annotated with DW_AT_abstract_origin. */
22917 parm_die = NULL;
22920 if (parm_die && parm_die->die_parent == NULL)
22922 /* Check that parm_die already has the right attributes that
22923 we would have added below. If any attributes are
22924 missing, fall through to add them. */
22925 if (! DECL_ABSTRACT_P (node_or_origin)
22926 && !get_AT (parm_die, DW_AT_location)
22927 && !get_AT (parm_die, DW_AT_const_value))
22928 /* We are missing location info, and are about to add it. */
22930 else
22932 add_child_die (context_die, parm_die);
22933 return parm_die;
22938 /* If we have a previously generated DIE, use it, unless this is an
22939 concrete instance (origin != NULL), in which case we need a new
22940 DIE with a corresponding DW_AT_abstract_origin. */
22941 bool reusing_die;
22942 if (parm_die && origin == NULL)
22943 reusing_die = true;
22944 else
22946 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22947 reusing_die = false;
22950 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22952 case tcc_declaration:
22953 ultimate_origin = decl_ultimate_origin (node_or_origin);
22954 if (node || ultimate_origin)
22955 origin = ultimate_origin;
22957 if (reusing_die)
22958 goto add_location;
22960 if (origin != NULL)
22961 add_abstract_origin_attribute (parm_die, origin);
22962 else if (emit_name_p)
22963 add_name_and_src_coords_attributes (parm_die, node);
22964 if (origin == NULL
22965 || (! DECL_ABSTRACT_P (node_or_origin)
22966 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22967 decl_function_context
22968 (node_or_origin))))
22970 tree type = TREE_TYPE (node_or_origin);
22971 if (decl_by_reference_p (node_or_origin))
22972 add_type_attribute (parm_die, TREE_TYPE (type),
22973 TYPE_UNQUALIFIED,
22974 false, context_die);
22975 else
22976 add_type_attribute (parm_die, type,
22977 decl_quals (node_or_origin),
22978 false, context_die);
22980 if (origin == NULL && DECL_ARTIFICIAL (node))
22981 add_AT_flag (parm_die, DW_AT_artificial, 1);
22982 add_location:
22983 if (node && node != origin)
22984 equate_decl_number_to_die (node, parm_die);
22985 if (! DECL_ABSTRACT_P (node_or_origin))
22986 add_location_or_const_value_attribute (parm_die, node_or_origin,
22987 node == NULL);
22989 break;
22991 case tcc_type:
22992 /* We were called with some kind of a ..._TYPE node. */
22993 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22994 context_die);
22995 break;
22997 default:
22998 gcc_unreachable ();
23001 return parm_die;
23004 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
23005 children DW_TAG_formal_parameter DIEs representing the arguments of the
23006 parameter pack.
23008 PARM_PACK must be a function parameter pack.
23009 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
23010 must point to the subsequent arguments of the function PACK_ARG belongs to.
23011 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
23012 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
23013 following the last one for which a DIE was generated. */
23015 static dw_die_ref
23016 gen_formal_parameter_pack_die (tree parm_pack,
23017 tree pack_arg,
23018 dw_die_ref subr_die,
23019 tree *next_arg)
23021 tree arg;
23022 dw_die_ref parm_pack_die;
23024 gcc_assert (parm_pack
23025 && lang_hooks.function_parameter_pack_p (parm_pack)
23026 && subr_die);
23028 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
23029 add_src_coords_attributes (parm_pack_die, parm_pack);
23031 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
23033 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
23034 parm_pack))
23035 break;
23036 gen_formal_parameter_die (arg, NULL,
23037 false /* Don't emit name attribute. */,
23038 parm_pack_die);
23040 if (next_arg)
23041 *next_arg = arg;
23042 return parm_pack_die;
23045 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
23046 at the end of an (ANSI prototyped) formal parameters list. */
23048 static void
23049 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
23051 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
23054 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
23055 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
23056 parameters as specified in some function type specification (except for
23057 those which appear as part of a function *definition*). */
23059 static void
23060 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
23062 tree link;
23063 tree formal_type = NULL;
23064 tree first_parm_type;
23065 tree arg;
23067 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
23069 arg = DECL_ARGUMENTS (function_or_method_type);
23070 function_or_method_type = TREE_TYPE (function_or_method_type);
23072 else
23073 arg = NULL_TREE;
23075 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
23077 /* Make our first pass over the list of formal parameter types and output a
23078 DW_TAG_formal_parameter DIE for each one. */
23079 for (link = first_parm_type; link; )
23081 dw_die_ref parm_die;
23083 formal_type = TREE_VALUE (link);
23084 if (formal_type == void_type_node)
23085 break;
23087 /* Output a (nameless) DIE to represent the formal parameter itself. */
23088 parm_die = gen_formal_parameter_die (formal_type, NULL,
23089 true /* Emit name attribute. */,
23090 context_die);
23091 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
23092 && link == first_parm_type)
23094 add_AT_flag (parm_die, DW_AT_artificial, 1);
23095 if (dwarf_version >= 3 || !dwarf_strict)
23096 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
23098 else if (arg && DECL_ARTIFICIAL (arg))
23099 add_AT_flag (parm_die, DW_AT_artificial, 1);
23101 link = TREE_CHAIN (link);
23102 if (arg)
23103 arg = DECL_CHAIN (arg);
23106 /* If this function type has an ellipsis, add a
23107 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
23108 if (formal_type != void_type_node)
23109 gen_unspecified_parameters_die (function_or_method_type, context_die);
23111 /* Make our second (and final) pass over the list of formal parameter types
23112 and output DIEs to represent those types (as necessary). */
23113 for (link = TYPE_ARG_TYPES (function_or_method_type);
23114 link && TREE_VALUE (link);
23115 link = TREE_CHAIN (link))
23116 gen_type_die (TREE_VALUE (link), context_die);
23119 /* We want to generate the DIE for TYPE so that we can generate the
23120 die for MEMBER, which has been defined; we will need to refer back
23121 to the member declaration nested within TYPE. If we're trying to
23122 generate minimal debug info for TYPE, processing TYPE won't do the
23123 trick; we need to attach the member declaration by hand. */
23125 static void
23126 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
23128 gen_type_die (type, context_die);
23130 /* If we're trying to avoid duplicate debug info, we may not have
23131 emitted the member decl for this function. Emit it now. */
23132 if (TYPE_STUB_DECL (type)
23133 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
23134 && ! lookup_decl_die (member))
23136 dw_die_ref type_die;
23137 gcc_assert (!decl_ultimate_origin (member));
23139 type_die = lookup_type_die_strip_naming_typedef (type);
23140 if (TREE_CODE (member) == FUNCTION_DECL)
23141 gen_subprogram_die (member, type_die);
23142 else if (TREE_CODE (member) == FIELD_DECL)
23144 /* Ignore the nameless fields that are used to skip bits but handle
23145 C++ anonymous unions and structs. */
23146 if (DECL_NAME (member) != NULL_TREE
23147 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
23148 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
23150 struct vlr_context vlr_ctx = {
23151 DECL_CONTEXT (member), /* struct_type */
23152 NULL_TREE /* variant_part_offset */
23154 gen_type_die (member_declared_type (member), type_die);
23155 gen_field_die (member, &vlr_ctx, type_die);
23158 else
23159 gen_variable_die (member, NULL_TREE, type_die);
23163 /* Forward declare these functions, because they are mutually recursive
23164 with their set_block_* pairing functions. */
23165 static void set_decl_origin_self (tree);
23167 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23168 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23169 that it points to the node itself, thus indicating that the node is its
23170 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23171 the given node is NULL, recursively descend the decl/block tree which
23172 it is the root of, and for each other ..._DECL or BLOCK node contained
23173 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23174 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23175 values to point to themselves. */
23177 static void
23178 set_block_origin_self (tree stmt)
23180 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23182 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23185 tree local_decl;
23187 for (local_decl = BLOCK_VARS (stmt);
23188 local_decl != NULL_TREE;
23189 local_decl = DECL_CHAIN (local_decl))
23190 /* Do not recurse on nested functions since the inlining status
23191 of parent and child can be different as per the DWARF spec. */
23192 if (TREE_CODE (local_decl) != FUNCTION_DECL
23193 && !DECL_EXTERNAL (local_decl))
23194 set_decl_origin_self (local_decl);
23198 tree subblock;
23200 for (subblock = BLOCK_SUBBLOCKS (stmt);
23201 subblock != NULL_TREE;
23202 subblock = BLOCK_CHAIN (subblock))
23203 set_block_origin_self (subblock); /* Recurse. */
23208 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23209 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23210 node to so that it points to the node itself, thus indicating that the
23211 node represents its own (abstract) origin. Additionally, if the
23212 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23213 the decl/block tree of which the given node is the root of, and for
23214 each other ..._DECL or BLOCK node contained therein whose
23215 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23216 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23217 point to themselves. */
23219 static void
23220 set_decl_origin_self (tree decl)
23222 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23224 DECL_ABSTRACT_ORIGIN (decl) = decl;
23225 if (TREE_CODE (decl) == FUNCTION_DECL)
23227 tree arg;
23229 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23230 DECL_ABSTRACT_ORIGIN (arg) = arg;
23231 if (DECL_INITIAL (decl) != NULL_TREE
23232 && DECL_INITIAL (decl) != error_mark_node)
23233 set_block_origin_self (DECL_INITIAL (decl));
23238 /* Mark the early DIE for DECL as the abstract instance. */
23240 static void
23241 dwarf2out_abstract_function (tree decl)
23243 dw_die_ref old_die;
23245 /* Make sure we have the actual abstract inline, not a clone. */
23246 decl = DECL_ORIGIN (decl);
23248 if (DECL_IGNORED_P (decl))
23249 return;
23251 /* In LTO we're all set. We already created abstract instances
23252 early and we want to avoid creating a concrete instance of that
23253 if we don't output it. */
23254 if (in_lto_p)
23255 return;
23257 old_die = lookup_decl_die (decl);
23258 gcc_assert (old_die != NULL);
23259 if (get_AT (old_die, DW_AT_inline))
23260 /* We've already generated the abstract instance. */
23261 return;
23263 /* Go ahead and put DW_AT_inline on the DIE. */
23264 if (DECL_DECLARED_INLINE_P (decl))
23266 if (cgraph_function_possibly_inlined_p (decl))
23267 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23268 else
23269 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23271 else
23273 if (cgraph_function_possibly_inlined_p (decl))
23274 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23275 else
23276 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23279 if (DECL_DECLARED_INLINE_P (decl)
23280 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23281 add_AT_flag (old_die, DW_AT_artificial, 1);
23283 set_decl_origin_self (decl);
23286 /* Helper function of premark_used_types() which gets called through
23287 htab_traverse.
23289 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23290 marked as unused by prune_unused_types. */
23292 bool
23293 premark_used_types_helper (tree const &type, void *)
23295 dw_die_ref die;
23297 die = lookup_type_die (type);
23298 if (die != NULL)
23299 die->die_perennial_p = 1;
23300 return true;
23303 /* Helper function of premark_types_used_by_global_vars which gets called
23304 through htab_traverse.
23306 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23307 marked as unused by prune_unused_types. The DIE of the type is marked
23308 only if the global variable using the type will actually be emitted. */
23311 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23312 void *)
23314 struct types_used_by_vars_entry *entry;
23315 dw_die_ref die;
23317 entry = (struct types_used_by_vars_entry *) *slot;
23318 gcc_assert (entry->type != NULL
23319 && entry->var_decl != NULL);
23320 die = lookup_type_die (entry->type);
23321 if (die)
23323 /* Ask cgraph if the global variable really is to be emitted.
23324 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23325 varpool_node *node = varpool_node::get (entry->var_decl);
23326 if (node && node->definition)
23328 die->die_perennial_p = 1;
23329 /* Keep the parent DIEs as well. */
23330 while ((die = die->die_parent) && die->die_perennial_p == 0)
23331 die->die_perennial_p = 1;
23334 return 1;
23337 /* Mark all members of used_types_hash as perennial. */
23339 static void
23340 premark_used_types (struct function *fun)
23342 if (fun && fun->used_types_hash)
23343 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23346 /* Mark all members of types_used_by_vars_entry as perennial. */
23348 static void
23349 premark_types_used_by_global_vars (void)
23351 if (types_used_by_vars_hash)
23352 types_used_by_vars_hash
23353 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23356 /* Mark all variables used by the symtab as perennial. */
23358 static void
23359 premark_used_variables (void)
23361 /* Mark DIEs in the symtab as used. */
23362 varpool_node *var;
23363 FOR_EACH_VARIABLE (var)
23365 dw_die_ref die = lookup_decl_die (var->decl);
23366 if (die)
23367 die->die_perennial_p = 1;
23371 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23372 for CA_LOC call arg loc node. */
23374 static dw_die_ref
23375 gen_call_site_die (tree decl, dw_die_ref subr_die,
23376 struct call_arg_loc_node *ca_loc)
23378 dw_die_ref stmt_die = NULL, die;
23379 tree block = ca_loc->block;
23381 while (block
23382 && block != DECL_INITIAL (decl)
23383 && TREE_CODE (block) == BLOCK)
23385 stmt_die = lookup_block_die (block);
23386 if (stmt_die)
23387 break;
23388 block = BLOCK_SUPERCONTEXT (block);
23390 if (stmt_die == NULL)
23391 stmt_die = subr_die;
23392 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23393 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23394 if (ca_loc->tail_call_p)
23395 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23396 if (ca_loc->symbol_ref)
23398 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23399 if (tdie)
23400 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23401 else
23402 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23403 false);
23405 return die;
23408 /* Generate a DIE to represent a declared function (either file-scope or
23409 block-local). */
23411 static void
23412 gen_subprogram_die (tree decl, dw_die_ref context_die)
23414 tree origin = decl_ultimate_origin (decl);
23415 dw_die_ref subr_die;
23416 dw_die_ref old_die = lookup_decl_die (decl);
23417 bool old_die_had_no_children = false;
23419 /* This function gets called multiple times for different stages of
23420 the debug process. For example, for func() in this code:
23422 namespace S
23424 void func() { ... }
23427 ...we get called 4 times. Twice in early debug and twice in
23428 late debug:
23430 Early debug
23431 -----------
23433 1. Once while generating func() within the namespace. This is
23434 the declaration. The declaration bit below is set, as the
23435 context is the namespace.
23437 A new DIE will be generated with DW_AT_declaration set.
23439 2. Once for func() itself. This is the specification. The
23440 declaration bit below is clear as the context is the CU.
23442 We will use the cached DIE from (1) to create a new DIE with
23443 DW_AT_specification pointing to the declaration in (1).
23445 Late debug via rest_of_handle_final()
23446 -------------------------------------
23448 3. Once generating func() within the namespace. This is also the
23449 declaration, as in (1), but this time we will early exit below
23450 as we have a cached DIE and a declaration needs no additional
23451 annotations (no locations), as the source declaration line
23452 info is enough.
23454 4. Once for func() itself. As in (2), this is the specification,
23455 but this time we will re-use the cached DIE, and just annotate
23456 it with the location information that should now be available.
23458 For something without namespaces, but with abstract instances, we
23459 are also called a multiple times:
23461 class Base
23463 public:
23464 Base (); // constructor declaration (1)
23467 Base::Base () { } // constructor specification (2)
23469 Early debug
23470 -----------
23472 1. Once for the Base() constructor by virtue of it being a
23473 member of the Base class. This is done via
23474 rest_of_type_compilation.
23476 This is a declaration, so a new DIE will be created with
23477 DW_AT_declaration.
23479 2. Once for the Base() constructor definition, but this time
23480 while generating the abstract instance of the base
23481 constructor (__base_ctor) which is being generated via early
23482 debug of reachable functions.
23484 Even though we have a cached version of the declaration (1),
23485 we will create a DW_AT_specification of the declaration DIE
23486 in (1).
23488 3. Once for the __base_ctor itself, but this time, we generate
23489 an DW_AT_abstract_origin version of the DW_AT_specification in
23490 (2).
23492 Late debug via rest_of_handle_final
23493 -----------------------------------
23495 4. One final time for the __base_ctor (which will have a cached
23496 DIE with DW_AT_abstract_origin created in (3). This time,
23497 we will just annotate the location information now
23498 available.
23500 int declaration = (current_function_decl != decl
23501 || (!DECL_INITIAL (decl) && !origin)
23502 || class_or_namespace_scope_p (context_die));
23504 /* A declaration that has been previously dumped needs no
23505 additional information. */
23506 if (old_die && declaration)
23507 return;
23509 if (in_lto_p && old_die && old_die->die_child == NULL)
23510 old_die_had_no_children = true;
23512 /* Now that the C++ front end lazily declares artificial member fns, we
23513 might need to retrofit the declaration into its class. */
23514 if (!declaration && !origin && !old_die
23515 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23516 && !class_or_namespace_scope_p (context_die)
23517 && debug_info_level > DINFO_LEVEL_TERSE)
23518 old_die = force_decl_die (decl);
23520 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23521 if (origin != NULL)
23523 gcc_assert (!declaration || local_scope_p (context_die));
23525 /* Fixup die_parent for the abstract instance of a nested
23526 inline function. */
23527 if (old_die && old_die->die_parent == NULL)
23528 add_child_die (context_die, old_die);
23530 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23532 /* If we have a DW_AT_abstract_origin we have a working
23533 cached version. */
23534 subr_die = old_die;
23536 else
23538 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23539 add_abstract_origin_attribute (subr_die, origin);
23540 /* This is where the actual code for a cloned function is.
23541 Let's emit linkage name attribute for it. This helps
23542 debuggers to e.g, set breakpoints into
23543 constructors/destructors when the user asks "break
23544 K::K". */
23545 add_linkage_name (subr_die, decl);
23548 /* A cached copy, possibly from early dwarf generation. Reuse as
23549 much as possible. */
23550 else if (old_die)
23552 if (!get_AT_flag (old_die, DW_AT_declaration)
23553 /* We can have a normal definition following an inline one in the
23554 case of redefinition of GNU C extern inlines.
23555 It seems reasonable to use AT_specification in this case. */
23556 && !get_AT (old_die, DW_AT_inline))
23558 /* Detect and ignore this case, where we are trying to output
23559 something we have already output. */
23560 if (get_AT (old_die, DW_AT_low_pc)
23561 || get_AT (old_die, DW_AT_ranges))
23562 return;
23564 /* If we have no location information, this must be a
23565 partially generated DIE from early dwarf generation.
23566 Fall through and generate it. */
23569 /* If the definition comes from the same place as the declaration,
23570 maybe use the old DIE. We always want the DIE for this function
23571 that has the *_pc attributes to be under comp_unit_die so the
23572 debugger can find it. We also need to do this for abstract
23573 instances of inlines, since the spec requires the out-of-line copy
23574 to have the same parent. For local class methods, this doesn't
23575 apply; we just use the old DIE. */
23576 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23577 struct dwarf_file_data * file_index = lookup_filename (s.file);
23578 if (((is_unit_die (old_die->die_parent)
23579 /* This condition fixes the inconsistency/ICE with the
23580 following Fortran test (or some derivative thereof) while
23581 building libgfortran:
23583 module some_m
23584 contains
23585 logical function funky (FLAG)
23586 funky = .true.
23587 end function
23588 end module
23590 || (old_die->die_parent
23591 && old_die->die_parent->die_tag == DW_TAG_module)
23592 || local_scope_p (old_die->die_parent)
23593 || context_die == NULL)
23594 && (DECL_ARTIFICIAL (decl)
23595 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23596 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23597 == (unsigned) s.line)
23598 && (!debug_column_info
23599 || s.column == 0
23600 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23601 == (unsigned) s.column)))))
23602 /* With LTO if there's an abstract instance for
23603 the old DIE, this is a concrete instance and
23604 thus re-use the DIE. */
23605 || get_AT (old_die, DW_AT_abstract_origin))
23607 subr_die = old_die;
23609 /* Clear out the declaration attribute, but leave the
23610 parameters so they can be augmented with location
23611 information later. Unless this was a declaration, in
23612 which case, wipe out the nameless parameters and recreate
23613 them further down. */
23614 if (remove_AT (subr_die, DW_AT_declaration))
23617 remove_AT (subr_die, DW_AT_object_pointer);
23618 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23621 /* Make a specification pointing to the previously built
23622 declaration. */
23623 else
23625 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23626 add_AT_specification (subr_die, old_die);
23627 add_pubname (decl, subr_die);
23628 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23629 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23630 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23631 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23632 if (debug_column_info
23633 && s.column
23634 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23635 != (unsigned) s.column))
23636 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23638 /* If the prototype had an 'auto' or 'decltype(auto)' in
23639 the return type, emit the real type on the definition die. */
23640 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23642 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23643 while (die
23644 && (die->die_tag == DW_TAG_reference_type
23645 || die->die_tag == DW_TAG_rvalue_reference_type
23646 || die->die_tag == DW_TAG_pointer_type
23647 || die->die_tag == DW_TAG_const_type
23648 || die->die_tag == DW_TAG_volatile_type
23649 || die->die_tag == DW_TAG_restrict_type
23650 || die->die_tag == DW_TAG_array_type
23651 || die->die_tag == DW_TAG_ptr_to_member_type
23652 || die->die_tag == DW_TAG_subroutine_type))
23653 die = get_AT_ref (die, DW_AT_type);
23654 if (die == auto_die || die == decltype_auto_die)
23655 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23656 TYPE_UNQUALIFIED, false, context_die);
23659 /* When we process the method declaration, we haven't seen
23660 the out-of-class defaulted definition yet, so we have to
23661 recheck now. */
23662 if ((dwarf_version >= 5 || ! dwarf_strict)
23663 && !get_AT (subr_die, DW_AT_defaulted))
23665 int defaulted
23666 = lang_hooks.decls.decl_dwarf_attribute (decl,
23667 DW_AT_defaulted);
23668 if (defaulted != -1)
23670 /* Other values must have been handled before. */
23671 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23672 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23677 /* Create a fresh DIE for anything else. */
23678 else
23680 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23682 if (TREE_PUBLIC (decl))
23683 add_AT_flag (subr_die, DW_AT_external, 1);
23685 add_name_and_src_coords_attributes (subr_die, decl);
23686 add_pubname (decl, subr_die);
23687 if (debug_info_level > DINFO_LEVEL_TERSE)
23689 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23690 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23691 TYPE_UNQUALIFIED, false, context_die);
23694 add_pure_or_virtual_attribute (subr_die, decl);
23695 if (DECL_ARTIFICIAL (decl))
23696 add_AT_flag (subr_die, DW_AT_artificial, 1);
23698 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23699 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23701 add_alignment_attribute (subr_die, decl);
23703 add_accessibility_attribute (subr_die, decl);
23706 /* Unless we have an existing non-declaration DIE, equate the new
23707 DIE. */
23708 if (!old_die || is_declaration_die (old_die))
23709 equate_decl_number_to_die (decl, subr_die);
23711 if (declaration)
23713 if (!old_die || !get_AT (old_die, DW_AT_inline))
23715 add_AT_flag (subr_die, DW_AT_declaration, 1);
23717 /* If this is an explicit function declaration then generate
23718 a DW_AT_explicit attribute. */
23719 if ((dwarf_version >= 3 || !dwarf_strict)
23720 && lang_hooks.decls.decl_dwarf_attribute (decl,
23721 DW_AT_explicit) == 1)
23722 add_AT_flag (subr_die, DW_AT_explicit, 1);
23724 /* If this is a C++11 deleted special function member then generate
23725 a DW_AT_deleted attribute. */
23726 if ((dwarf_version >= 5 || !dwarf_strict)
23727 && lang_hooks.decls.decl_dwarf_attribute (decl,
23728 DW_AT_deleted) == 1)
23729 add_AT_flag (subr_die, DW_AT_deleted, 1);
23731 /* If this is a C++11 defaulted special function member then
23732 generate a DW_AT_defaulted attribute. */
23733 if (dwarf_version >= 5 || !dwarf_strict)
23735 int defaulted
23736 = lang_hooks.decls.decl_dwarf_attribute (decl,
23737 DW_AT_defaulted);
23738 if (defaulted != -1)
23739 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23742 /* If this is a C++11 non-static member function with & ref-qualifier
23743 then generate a DW_AT_reference attribute. */
23744 if ((dwarf_version >= 5 || !dwarf_strict)
23745 && lang_hooks.decls.decl_dwarf_attribute (decl,
23746 DW_AT_reference) == 1)
23747 add_AT_flag (subr_die, DW_AT_reference, 1);
23749 /* If this is a C++11 non-static member function with &&
23750 ref-qualifier then generate a DW_AT_reference attribute. */
23751 if ((dwarf_version >= 5 || !dwarf_strict)
23752 && lang_hooks.decls.decl_dwarf_attribute (decl,
23753 DW_AT_rvalue_reference)
23754 == 1)
23755 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23758 /* For non DECL_EXTERNALs, if range information is available, fill
23759 the DIE with it. */
23760 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23762 HOST_WIDE_INT cfa_fb_offset;
23764 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23766 if (!crtl->has_bb_partition)
23768 dw_fde_ref fde = fun->fde;
23769 if (fde->dw_fde_begin)
23771 /* We have already generated the labels. */
23772 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23773 fde->dw_fde_end, false);
23775 else
23777 /* Create start/end labels and add the range. */
23778 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23779 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23780 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23781 current_function_funcdef_no);
23782 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23783 current_function_funcdef_no);
23784 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23785 false);
23788 #if VMS_DEBUGGING_INFO
23789 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23790 Section 2.3 Prologue and Epilogue Attributes:
23791 When a breakpoint is set on entry to a function, it is generally
23792 desirable for execution to be suspended, not on the very first
23793 instruction of the function, but rather at a point after the
23794 function's frame has been set up, after any language defined local
23795 declaration processing has been completed, and before execution of
23796 the first statement of the function begins. Debuggers generally
23797 cannot properly determine where this point is. Similarly for a
23798 breakpoint set on exit from a function. The prologue and epilogue
23799 attributes allow a compiler to communicate the location(s) to use. */
23802 if (fde->dw_fde_vms_end_prologue)
23803 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23804 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23806 if (fde->dw_fde_vms_begin_epilogue)
23807 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23808 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23810 #endif
23813 else
23815 /* Generate pubnames entries for the split function code ranges. */
23816 dw_fde_ref fde = fun->fde;
23818 if (fde->dw_fde_second_begin)
23820 if (dwarf_version >= 3 || !dwarf_strict)
23822 /* We should use ranges for non-contiguous code section
23823 addresses. Use the actual code range for the initial
23824 section, since the HOT/COLD labels might precede an
23825 alignment offset. */
23826 bool range_list_added = false;
23827 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23828 fde->dw_fde_end, &range_list_added,
23829 false);
23830 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23831 fde->dw_fde_second_end,
23832 &range_list_added, false);
23833 if (range_list_added)
23834 add_ranges (NULL);
23836 else
23838 /* There is no real support in DW2 for this .. so we make
23839 a work-around. First, emit the pub name for the segment
23840 containing the function label. Then make and emit a
23841 simplified subprogram DIE for the second segment with the
23842 name pre-fixed by __hot/cold_sect_of_. We use the same
23843 linkage name for the second die so that gdb will find both
23844 sections when given "b foo". */
23845 const char *name = NULL;
23846 tree decl_name = DECL_NAME (decl);
23847 dw_die_ref seg_die;
23849 /* Do the 'primary' section. */
23850 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23851 fde->dw_fde_end, false);
23853 /* Build a minimal DIE for the secondary section. */
23854 seg_die = new_die (DW_TAG_subprogram,
23855 subr_die->die_parent, decl);
23857 if (TREE_PUBLIC (decl))
23858 add_AT_flag (seg_die, DW_AT_external, 1);
23860 if (decl_name != NULL
23861 && IDENTIFIER_POINTER (decl_name) != NULL)
23863 name = dwarf2_name (decl, 1);
23864 if (! DECL_ARTIFICIAL (decl))
23865 add_src_coords_attributes (seg_die, decl);
23867 add_linkage_name (seg_die, decl);
23869 gcc_assert (name != NULL);
23870 add_pure_or_virtual_attribute (seg_die, decl);
23871 if (DECL_ARTIFICIAL (decl))
23872 add_AT_flag (seg_die, DW_AT_artificial, 1);
23874 name = concat ("__second_sect_of_", name, NULL);
23875 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23876 fde->dw_fde_second_end, false);
23877 add_name_attribute (seg_die, name);
23878 if (want_pubnames ())
23879 add_pubname_string (name, seg_die);
23882 else
23883 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23884 false);
23887 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23889 /* We define the "frame base" as the function's CFA. This is more
23890 convenient for several reasons: (1) It's stable across the prologue
23891 and epilogue, which makes it better than just a frame pointer,
23892 (2) With dwarf3, there exists a one-byte encoding that allows us
23893 to reference the .debug_frame data by proxy, but failing that,
23894 (3) We can at least reuse the code inspection and interpretation
23895 code that determines the CFA position at various points in the
23896 function. */
23897 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23899 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23900 add_AT_loc (subr_die, DW_AT_frame_base, op);
23902 else
23904 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23905 if (list->dw_loc_next)
23906 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23907 else
23908 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23911 /* Compute a displacement from the "steady-state frame pointer" to
23912 the CFA. The former is what all stack slots and argument slots
23913 will reference in the rtl; the latter is what we've told the
23914 debugger about. We'll need to adjust all frame_base references
23915 by this displacement. */
23916 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23918 if (fun->static_chain_decl)
23920 /* DWARF requires here a location expression that computes the
23921 address of the enclosing subprogram's frame base. The machinery
23922 in tree-nested.cc is supposed to store this specific address in the
23923 last field of the FRAME record. */
23924 const tree frame_type
23925 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23926 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23928 tree fb_expr
23929 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23930 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23931 fb_expr, fb_decl, NULL_TREE);
23933 add_AT_location_description (subr_die, DW_AT_static_link,
23934 loc_list_from_tree (fb_expr, 0, NULL));
23937 resolve_variable_values ();
23940 /* Generate child dies for template parameters. */
23941 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23942 gen_generic_params_dies (decl);
23944 /* Now output descriptions of the arguments for this function. This gets
23945 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23946 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23947 `...' at the end of the formal parameter list. In order to find out if
23948 there was a trailing ellipsis or not, we must instead look at the type
23949 associated with the FUNCTION_DECL. This will be a node of type
23950 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23951 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23952 an ellipsis at the end. */
23954 /* In the case where we are describing a mere function declaration, all we
23955 need to do here (and all we *can* do here) is to describe the *types* of
23956 its formal parameters. */
23957 if (debug_info_level <= DINFO_LEVEL_TERSE)
23959 else if (declaration)
23960 gen_formal_types_die (decl, subr_die);
23961 else
23963 /* Generate DIEs to represent all known formal parameters. */
23964 tree parm = DECL_ARGUMENTS (decl);
23965 tree generic_decl = early_dwarf
23966 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23967 tree generic_decl_parm = generic_decl
23968 ? DECL_ARGUMENTS (generic_decl)
23969 : NULL;
23971 /* Now we want to walk the list of parameters of the function and
23972 emit their relevant DIEs.
23974 We consider the case of DECL being an instance of a generic function
23975 as well as it being a normal function.
23977 If DECL is an instance of a generic function we walk the
23978 parameters of the generic function declaration _and_ the parameters of
23979 DECL itself. This is useful because we want to emit specific DIEs for
23980 function parameter packs and those are declared as part of the
23981 generic function declaration. In that particular case,
23982 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23983 That DIE has children DIEs representing the set of arguments
23984 of the pack. Note that the set of pack arguments can be empty.
23985 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23986 children DIE.
23988 Otherwise, we just consider the parameters of DECL. */
23989 while (generic_decl_parm || parm)
23991 if (generic_decl_parm
23992 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23993 gen_formal_parameter_pack_die (generic_decl_parm,
23994 parm, subr_die,
23995 &parm);
23996 else if (parm)
23998 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
24000 if (early_dwarf
24001 && parm == DECL_ARGUMENTS (decl)
24002 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
24003 && parm_die
24004 && (dwarf_version >= 3 || !dwarf_strict))
24005 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
24007 parm = DECL_CHAIN (parm);
24010 if (generic_decl_parm)
24011 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
24014 /* Decide whether we need an unspecified_parameters DIE at the end.
24015 There are 2 more cases to do this for: 1) the ansi ... declaration -
24016 this is detectable when the end of the arg list is not a
24017 void_type_node 2) an unprototyped function declaration (not a
24018 definition). This just means that we have no info about the
24019 parameters at all. */
24020 if (early_dwarf)
24022 if (prototype_p (TREE_TYPE (decl)))
24024 /* This is the prototyped case, check for.... */
24025 if (stdarg_p (TREE_TYPE (decl)))
24026 gen_unspecified_parameters_die (decl, subr_die);
24028 else if (DECL_INITIAL (decl) == NULL_TREE)
24029 gen_unspecified_parameters_die (decl, subr_die);
24031 else if ((subr_die != old_die || old_die_had_no_children)
24032 && prototype_p (TREE_TYPE (decl))
24033 && stdarg_p (TREE_TYPE (decl)))
24034 gen_unspecified_parameters_die (decl, subr_die);
24037 if (subr_die != old_die)
24038 /* Add the calling convention attribute if requested. */
24039 add_calling_convention_attribute (subr_die, decl);
24041 /* Output Dwarf info for all of the stuff within the body of the function
24042 (if it has one - it may be just a declaration).
24044 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
24045 a function. This BLOCK actually represents the outermost binding contour
24046 for the function, i.e. the contour in which the function's formal
24047 parameters and labels get declared. Curiously, it appears that the front
24048 end doesn't actually put the PARM_DECL nodes for the current function onto
24049 the BLOCK_VARS list for this outer scope, but are strung off of the
24050 DECL_ARGUMENTS list for the function instead.
24052 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
24053 the LABEL_DECL nodes for the function however, and we output DWARF info
24054 for those in decls_for_scope. Just within the `outer_scope' there will be
24055 a BLOCK node representing the function's outermost pair of curly braces,
24056 and any blocks used for the base and member initializers of a C++
24057 constructor function. */
24058 tree outer_scope = DECL_INITIAL (decl);
24059 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
24061 int call_site_note_count = 0;
24062 int tail_call_site_note_count = 0;
24064 /* Emit a DW_TAG_variable DIE for a named return value. */
24065 if (DECL_NAME (DECL_RESULT (decl)))
24066 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
24068 /* The first time through decls_for_scope we will generate the
24069 DIEs for the locals. The second time, we fill in the
24070 location info. */
24071 decls_for_scope (outer_scope, subr_die);
24073 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
24075 struct call_arg_loc_node *ca_loc;
24076 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
24078 dw_die_ref die = NULL;
24079 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
24080 rtx arg, next_arg;
24081 tree arg_decl = NULL_TREE;
24083 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
24084 ? XEXP (ca_loc->call_arg_loc_note, 0)
24085 : NULL_RTX);
24086 arg; arg = next_arg)
24088 dw_loc_descr_ref reg, val;
24089 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
24090 dw_die_ref cdie, tdie = NULL;
24092 next_arg = XEXP (arg, 1);
24093 if (REG_P (XEXP (XEXP (arg, 0), 0))
24094 && next_arg
24095 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
24096 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
24097 && REGNO (XEXP (XEXP (arg, 0), 0))
24098 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
24099 next_arg = XEXP (next_arg, 1);
24100 if (mode == VOIDmode)
24102 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
24103 if (mode == VOIDmode)
24104 mode = GET_MODE (XEXP (arg, 0));
24106 if (mode == VOIDmode || mode == BLKmode)
24107 continue;
24108 /* Get dynamic information about call target only if we
24109 have no static information: we cannot generate both
24110 DW_AT_call_origin and DW_AT_call_target
24111 attributes. */
24112 if (ca_loc->symbol_ref == NULL_RTX)
24114 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
24116 tloc = XEXP (XEXP (arg, 0), 1);
24117 continue;
24119 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
24120 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
24122 tlocc = XEXP (XEXP (arg, 0), 1);
24123 continue;
24126 reg = NULL;
24127 if (REG_P (XEXP (XEXP (arg, 0), 0)))
24128 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
24129 VAR_INIT_STATUS_INITIALIZED);
24130 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
24132 rtx mem = XEXP (XEXP (arg, 0), 0);
24133 reg = mem_loc_descriptor (XEXP (mem, 0),
24134 get_address_mode (mem),
24135 GET_MODE (mem),
24136 VAR_INIT_STATUS_INITIALIZED);
24138 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
24139 == DEBUG_PARAMETER_REF)
24141 tree tdecl
24142 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
24143 tdie = lookup_decl_die (tdecl);
24144 if (tdie == NULL)
24145 continue;
24146 arg_decl = tdecl;
24148 else
24149 continue;
24150 if (reg == NULL
24151 && GET_CODE (XEXP (XEXP (arg, 0), 0))
24152 != DEBUG_PARAMETER_REF)
24153 continue;
24154 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
24155 VOIDmode,
24156 VAR_INIT_STATUS_INITIALIZED);
24157 if (val == NULL)
24158 continue;
24159 if (die == NULL)
24160 die = gen_call_site_die (decl, subr_die, ca_loc);
24161 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24162 NULL_TREE);
24163 add_desc_attribute (cdie, arg_decl);
24164 if (reg != NULL)
24165 add_AT_loc (cdie, DW_AT_location, reg);
24166 else if (tdie != NULL)
24167 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24168 tdie);
24169 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24170 if (next_arg != XEXP (arg, 1))
24172 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24173 if (mode == VOIDmode)
24174 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24175 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24176 0), 1),
24177 mode, VOIDmode,
24178 VAR_INIT_STATUS_INITIALIZED);
24179 if (val != NULL)
24180 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24181 val);
24184 if (die == NULL
24185 && (ca_loc->symbol_ref || tloc))
24186 die = gen_call_site_die (decl, subr_die, ca_loc);
24187 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24189 dw_loc_descr_ref tval = NULL;
24191 if (tloc != NULL_RTX)
24192 tval = mem_loc_descriptor (tloc,
24193 GET_MODE (tloc) == VOIDmode
24194 ? Pmode : GET_MODE (tloc),
24195 VOIDmode,
24196 VAR_INIT_STATUS_INITIALIZED);
24197 if (tval)
24198 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24199 else if (tlocc != NULL_RTX)
24201 tval = mem_loc_descriptor (tlocc,
24202 GET_MODE (tlocc) == VOIDmode
24203 ? Pmode : GET_MODE (tlocc),
24204 VOIDmode,
24205 VAR_INIT_STATUS_INITIALIZED);
24206 if (tval)
24207 add_AT_loc (die,
24208 dwarf_AT (DW_AT_call_target_clobbered),
24209 tval);
24212 if (die != NULL)
24214 call_site_note_count++;
24215 if (ca_loc->tail_call_p)
24216 tail_call_site_note_count++;
24220 call_arg_locations = NULL;
24221 call_arg_loc_last = NULL;
24222 if (tail_call_site_count >= 0
24223 && tail_call_site_count == tail_call_site_note_count
24224 && (!dwarf_strict || dwarf_version >= 5))
24226 if (call_site_count >= 0
24227 && call_site_count == call_site_note_count)
24228 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24229 else
24230 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24232 call_site_count = -1;
24233 tail_call_site_count = -1;
24236 /* Mark used types after we have created DIEs for the functions scopes. */
24237 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24240 /* Returns a hash value for X (which really is a die_struct). */
24242 hashval_t
24243 block_die_hasher::hash (die_struct *d)
24245 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24248 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24249 as decl_id and die_parent of die_struct Y. */
24251 bool
24252 block_die_hasher::equal (die_struct *x, die_struct *y)
24254 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24257 /* Hold information about markers for inlined entry points. */
24258 struct GTY ((for_user)) inline_entry_data
24260 /* The block that's the inlined_function_outer_scope for an inlined
24261 function. */
24262 tree block;
24264 /* The label at the inlined entry point. */
24265 const char *label_pfx;
24266 unsigned int label_num;
24268 /* The view number to be used as the inlined entry point. */
24269 var_loc_view view;
24272 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24274 typedef tree compare_type;
24275 static inline hashval_t hash (const inline_entry_data *);
24276 static inline bool equal (const inline_entry_data *, const_tree);
24279 /* Hash table routines for inline_entry_data. */
24281 inline hashval_t
24282 inline_entry_data_hasher::hash (const inline_entry_data *data)
24284 return htab_hash_pointer (data->block);
24287 inline bool
24288 inline_entry_data_hasher::equal (const inline_entry_data *data,
24289 const_tree block)
24291 return data->block == block;
24294 /* Inlined entry points pending DIE creation in this compilation unit. */
24296 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24299 /* Return TRUE if DECL, which may have been previously generated as
24300 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24301 true if decl (or its origin) is either an extern declaration or a
24302 class/namespace scoped declaration.
24304 The declare_in_namespace support causes us to get two DIEs for one
24305 variable, both of which are declarations. We want to avoid
24306 considering one to be a specification, so we must test for
24307 DECLARATION and DW_AT_declaration. */
24308 static inline bool
24309 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24311 return (old_die && TREE_STATIC (decl) && !declaration
24312 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24315 /* Return true if DECL is a local static. */
24317 static inline bool
24318 local_function_static (tree decl)
24320 gcc_assert (VAR_P (decl));
24321 return TREE_STATIC (decl)
24322 && DECL_CONTEXT (decl)
24323 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24326 /* Return true iff DECL overrides (presumably completes) the type of
24327 OLD_DIE within CONTEXT_DIE. */
24329 static bool
24330 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24331 dw_die_ref context_die)
24333 tree type = TREE_TYPE (decl);
24334 int cv_quals;
24336 if (decl_by_reference_p (decl))
24338 type = TREE_TYPE (type);
24339 cv_quals = TYPE_UNQUALIFIED;
24341 else
24342 cv_quals = decl_quals (decl);
24344 dw_die_ref type_die = modified_type_die (type,
24345 cv_quals | TYPE_QUALS (type),
24346 false,
24347 context_die);
24349 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24351 return type_die != old_type_die;
24354 /* Generate a DIE to represent a declared data object.
24355 Either DECL or ORIGIN must be non-null. */
24357 static void
24358 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24360 HOST_WIDE_INT off = 0;
24361 tree com_decl;
24362 tree decl_or_origin = decl ? decl : origin;
24363 tree ultimate_origin;
24364 dw_die_ref var_die;
24365 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24366 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24367 || class_or_namespace_scope_p (context_die));
24368 bool specialization_p = false;
24369 bool no_linkage_name = false;
24371 /* While C++ inline static data members have definitions inside of the
24372 class, force the first DIE to be a declaration, then let gen_member_die
24373 reparent it to the class context and call gen_variable_die again
24374 to create the outside of the class DIE for the definition. */
24375 if (!declaration
24376 && old_die == NULL
24377 && decl
24378 && DECL_CONTEXT (decl)
24379 && TYPE_P (DECL_CONTEXT (decl))
24380 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24382 declaration = true;
24383 if (dwarf_version < 5)
24384 no_linkage_name = true;
24387 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24388 if (decl || ultimate_origin)
24389 origin = ultimate_origin;
24390 com_decl = fortran_common (decl_or_origin, &off);
24392 /* Symbol in common gets emitted as a child of the common block, in the form
24393 of a data member. */
24394 if (com_decl)
24396 dw_die_ref com_die;
24397 dw_loc_list_ref loc = NULL;
24398 die_node com_die_arg;
24400 var_die = lookup_decl_die (decl_or_origin);
24401 if (var_die)
24403 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24405 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24406 if (loc)
24408 if (off)
24410 /* Optimize the common case. */
24411 if (single_element_loc_list_p (loc)
24412 && loc->expr->dw_loc_opc == DW_OP_addr
24413 && loc->expr->dw_loc_next == NULL
24414 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24415 == SYMBOL_REF)
24417 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24418 loc->expr->dw_loc_oprnd1.v.val_addr
24419 = plus_constant (GET_MODE (x), x , off);
24421 else
24422 loc_list_plus_const (loc, off);
24424 add_AT_location_description (var_die, DW_AT_location, loc);
24425 remove_AT (var_die, DW_AT_declaration);
24428 return;
24431 if (common_block_die_table == NULL)
24432 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24434 com_die_arg.decl_id = DECL_UID (com_decl);
24435 com_die_arg.die_parent = context_die;
24436 com_die = common_block_die_table->find (&com_die_arg);
24437 if (! early_dwarf)
24438 loc = loc_list_from_tree (com_decl, 2, NULL);
24439 if (com_die == NULL)
24441 const char *cnam
24442 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24443 die_node **slot;
24445 com_die = new_die (DW_TAG_common_block, context_die, decl);
24446 add_name_and_src_coords_attributes (com_die, com_decl);
24447 if (loc)
24449 add_AT_location_description (com_die, DW_AT_location, loc);
24450 /* Avoid sharing the same loc descriptor between
24451 DW_TAG_common_block and DW_TAG_variable. */
24452 loc = loc_list_from_tree (com_decl, 2, NULL);
24454 else if (DECL_EXTERNAL (decl_or_origin))
24455 add_AT_flag (com_die, DW_AT_declaration, 1);
24456 if (want_pubnames ())
24457 add_pubname_string (cnam, com_die); /* ??? needed? */
24458 com_die->decl_id = DECL_UID (com_decl);
24459 slot = common_block_die_table->find_slot (com_die, INSERT);
24460 *slot = com_die;
24462 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24464 add_AT_location_description (com_die, DW_AT_location, loc);
24465 loc = loc_list_from_tree (com_decl, 2, NULL);
24466 remove_AT (com_die, DW_AT_declaration);
24468 var_die = new_die (DW_TAG_variable, com_die, decl);
24469 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24470 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24471 decl_quals (decl_or_origin), false,
24472 context_die);
24473 add_alignment_attribute (var_die, decl);
24474 add_AT_flag (var_die, DW_AT_external, 1);
24475 if (loc)
24477 if (off)
24479 /* Optimize the common case. */
24480 if (single_element_loc_list_p (loc)
24481 && loc->expr->dw_loc_opc == DW_OP_addr
24482 && loc->expr->dw_loc_next == NULL
24483 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24485 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24486 loc->expr->dw_loc_oprnd1.v.val_addr
24487 = plus_constant (GET_MODE (x), x, off);
24489 else
24490 loc_list_plus_const (loc, off);
24492 add_AT_location_description (var_die, DW_AT_location, loc);
24494 else if (DECL_EXTERNAL (decl_or_origin))
24495 add_AT_flag (var_die, DW_AT_declaration, 1);
24496 if (decl)
24497 equate_decl_number_to_die (decl, var_die);
24498 return;
24501 if (old_die)
24503 if (declaration)
24505 /* A declaration that has been previously dumped, needs no
24506 further annotations, since it doesn't need location on
24507 the second pass. */
24508 return;
24510 else if (decl_will_get_specification_p (old_die, decl, declaration)
24511 && !get_AT (old_die, DW_AT_specification))
24513 /* Fall-thru so we can make a new variable die along with a
24514 DW_AT_specification. */
24516 else if (origin && old_die->die_parent != context_die)
24518 /* If we will be creating an inlined instance, we need a
24519 new DIE that will get annotated with
24520 DW_AT_abstract_origin. */
24521 gcc_assert (!DECL_ABSTRACT_P (decl));
24523 else
24525 /* If a DIE was dumped early, it still needs location info.
24526 Skip to where we fill the location bits. */
24527 var_die = old_die;
24529 /* ??? In LTRANS we cannot annotate early created variably
24530 modified type DIEs without copying them and adjusting all
24531 references to them. Thus we dumped them again. Also add a
24532 reference to them but beware of -g0 compile and -g link
24533 in which case the reference will be already present. */
24534 tree type = TREE_TYPE (decl_or_origin);
24535 if (in_lto_p
24536 && ! get_AT (var_die, DW_AT_type)
24537 && variably_modified_type_p
24538 (type, decl_function_context (decl_or_origin)))
24540 if (decl_by_reference_p (decl_or_origin))
24541 add_type_attribute (var_die, TREE_TYPE (type),
24542 TYPE_UNQUALIFIED, false, context_die);
24543 else
24544 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24545 false, context_die);
24548 goto gen_variable_die_location;
24552 /* For static data members, the declaration in the class is supposed
24553 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24554 also in DWARF2; the specification should still be DW_TAG_variable
24555 referencing the DW_TAG_member DIE. */
24556 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24557 var_die = new_die (DW_TAG_member, context_die, decl);
24558 else
24559 var_die = new_die (DW_TAG_variable, context_die, decl);
24561 if (origin != NULL)
24562 add_abstract_origin_attribute (var_die, origin);
24564 /* Loop unrolling can create multiple blocks that refer to the same
24565 static variable, so we must test for the DW_AT_declaration flag.
24567 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24568 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24569 sharing them.
24571 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24572 else if (decl_will_get_specification_p (old_die, decl, declaration))
24574 /* This is a definition of a C++ class level static. */
24575 add_AT_specification (var_die, old_die);
24576 specialization_p = true;
24577 if (DECL_NAME (decl))
24579 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24580 struct dwarf_file_data * file_index = lookup_filename (s.file);
24582 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24583 add_AT_file (var_die, DW_AT_decl_file, file_index);
24585 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24586 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24588 if (debug_column_info
24589 && s.column
24590 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24591 != (unsigned) s.column))
24592 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24594 if (old_die->die_tag == DW_TAG_member)
24595 add_linkage_name (var_die, decl);
24598 else
24599 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24601 if ((origin == NULL && !specialization_p)
24602 || (origin != NULL
24603 && !DECL_ABSTRACT_P (decl_or_origin)
24604 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24605 decl_function_context
24606 (decl_or_origin)))
24607 || (old_die && specialization_p
24608 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24610 tree type = TREE_TYPE (decl_or_origin);
24612 if (decl_by_reference_p (decl_or_origin))
24613 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24614 context_die);
24615 else
24616 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24617 context_die);
24620 if (origin == NULL && !specialization_p)
24622 if (TREE_PUBLIC (decl))
24623 add_AT_flag (var_die, DW_AT_external, 1);
24625 if (DECL_ARTIFICIAL (decl))
24626 add_AT_flag (var_die, DW_AT_artificial, 1);
24628 add_alignment_attribute (var_die, decl);
24630 add_accessibility_attribute (var_die, decl);
24633 if (declaration)
24634 add_AT_flag (var_die, DW_AT_declaration, 1);
24636 if (decl && (DECL_ABSTRACT_P (decl)
24637 || !old_die || is_declaration_die (old_die)))
24638 equate_decl_number_to_die (decl, var_die);
24640 gen_variable_die_location:
24641 if (! declaration
24642 && (! DECL_ABSTRACT_P (decl_or_origin)
24643 /* Local static vars are shared between all clones/inlines,
24644 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24645 already set. */
24646 || (VAR_P (decl_or_origin)
24647 && TREE_STATIC (decl_or_origin)
24648 && DECL_RTL_SET_P (decl_or_origin))))
24650 if (early_dwarf)
24652 add_pubname (decl_or_origin, var_die);
24653 /* For global register variables, emit DW_AT_location if possible
24654 already during early_dwarf, as late_global_decl won't be usually
24655 called. */
24656 if (DECL_HARD_REGISTER (decl_or_origin)
24657 && TREE_STATIC (decl_or_origin)
24658 && !decl_by_reference_p (decl_or_origin)
24659 && !get_AT (var_die, DW_AT_location)
24660 && !get_AT (var_die, DW_AT_const_value)
24661 && DECL_RTL_SET_P (decl_or_origin)
24662 && REG_P (DECL_RTL (decl_or_origin)))
24664 dw_loc_descr_ref descr
24665 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24666 VAR_INIT_STATUS_INITIALIZED);
24667 if (descr)
24668 add_AT_loc (var_die, DW_AT_location, descr);
24671 else
24672 add_location_or_const_value_attribute (var_die, decl_or_origin,
24673 decl == NULL);
24675 else
24676 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24678 if ((dwarf_version >= 4 || !dwarf_strict)
24679 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24680 DW_AT_const_expr) == 1
24681 && !get_AT (var_die, DW_AT_const_expr)
24682 && !specialization_p)
24683 add_AT_flag (var_die, DW_AT_const_expr, 1);
24685 if (!dwarf_strict)
24687 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24688 DW_AT_inline);
24689 if (inl != -1
24690 && !get_AT (var_die, DW_AT_inline)
24691 && !specialization_p)
24692 add_AT_unsigned (var_die, DW_AT_inline, inl);
24696 /* Generate a DIE to represent a named constant. */
24698 static void
24699 gen_const_die (tree decl, dw_die_ref context_die)
24701 dw_die_ref const_die;
24702 tree type = TREE_TYPE (decl);
24704 const_die = lookup_decl_die (decl);
24705 if (const_die)
24706 return;
24708 const_die = new_die (DW_TAG_constant, context_die, decl);
24709 equate_decl_number_to_die (decl, const_die);
24710 add_name_and_src_coords_attributes (const_die, decl);
24711 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24712 if (TREE_PUBLIC (decl))
24713 add_AT_flag (const_die, DW_AT_external, 1);
24714 if (DECL_ARTIFICIAL (decl))
24715 add_AT_flag (const_die, DW_AT_artificial, 1);
24716 tree_add_const_value_attribute_for_decl (const_die, decl);
24719 /* Generate a DIE to represent a label identifier. */
24721 static void
24722 gen_label_die (tree decl, dw_die_ref context_die)
24724 tree origin = decl_ultimate_origin (decl);
24725 dw_die_ref lbl_die = lookup_decl_die (decl);
24726 rtx insn;
24727 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24729 if (!lbl_die)
24731 lbl_die = new_die (DW_TAG_label, context_die, decl);
24732 equate_decl_number_to_die (decl, lbl_die);
24734 if (origin != NULL)
24735 add_abstract_origin_attribute (lbl_die, origin);
24736 else
24737 add_name_and_src_coords_attributes (lbl_die, decl);
24740 if (DECL_ABSTRACT_P (decl))
24741 equate_decl_number_to_die (decl, lbl_die);
24742 else if (! early_dwarf)
24744 insn = DECL_RTL_IF_SET (decl);
24746 /* Deleted labels are programmer specified labels which have been
24747 eliminated because of various optimizations. We still emit them
24748 here so that it is possible to put breakpoints on them. */
24749 if (insn
24750 && (LABEL_P (insn)
24751 || ((NOTE_P (insn)
24752 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24754 /* When optimization is enabled (via -O) some parts of the compiler
24755 (e.g. jump.cc and cse.cc) may try to delete CODE_LABEL insns which
24756 represent source-level labels which were explicitly declared by
24757 the user. This really shouldn't be happening though, so catch
24758 it if it ever does happen. */
24759 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24761 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24762 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24764 else if (insn
24765 && NOTE_P (insn)
24766 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24767 && CODE_LABEL_NUMBER (insn) != -1)
24769 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24770 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24775 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24776 attributes to the DIE for a block STMT, to describe where the inlined
24777 function was called from. This is similar to add_src_coords_attributes. */
24779 static inline void
24780 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24782 /* We can end up with BUILTINS_LOCATION here. */
24783 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24784 return;
24786 location_t locus = BLOCK_SOURCE_LOCATION (stmt);
24787 expanded_location s = expand_location (locus);
24789 if (dwarf_version >= 3 || !dwarf_strict)
24791 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24792 add_AT_unsigned (die, DW_AT_call_line, s.line);
24793 if (debug_column_info && s.column)
24794 add_AT_unsigned (die, DW_AT_call_column, s.column);
24795 unsigned discr = get_discriminator_from_loc (locus);
24796 if (discr != 0)
24797 add_AT_unsigned (die, DW_AT_GNU_discriminator, discr);
24802 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24803 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24805 static inline void
24806 add_high_low_attributes (tree stmt, dw_die_ref die)
24808 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24810 if (inline_entry_data **iedp
24811 = !inline_entry_data_table ? NULL
24812 : inline_entry_data_table->find_slot_with_hash (stmt,
24813 htab_hash_pointer (stmt),
24814 NO_INSERT))
24816 inline_entry_data *ied = *iedp;
24817 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24818 gcc_assert (debug_inline_points);
24819 gcc_assert (inlined_function_outer_scope_p (stmt));
24821 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24822 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24824 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24825 && !dwarf_strict)
24827 if (!output_asm_line_debug_info ())
24828 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24829 else
24831 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24832 /* FIXME: this will resolve to a small number. Could we
24833 possibly emit smaller data? Ideally we'd emit a
24834 uleb128, but that would make the size of DIEs
24835 impossible for the compiler to compute, since it's
24836 the assembler that computes the value of the view
24837 label in this case. Ideally, we'd have a single form
24838 encompassing both the address and the view, and
24839 indirecting them through a table might make things
24840 easier, but even that would be more wasteful,
24841 space-wise, than what we have now. */
24842 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24846 inline_entry_data_table->clear_slot (iedp);
24849 if (BLOCK_FRAGMENT_CHAIN (stmt)
24850 && (dwarf_version >= 3 || !dwarf_strict))
24852 tree chain, superblock = NULL_TREE;
24853 dw_die_ref pdie;
24854 dw_attr_node *attr = NULL;
24856 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24858 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24859 BLOCK_NUMBER (stmt));
24860 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24863 /* Optimize duplicate .debug_ranges lists or even tails of
24864 lists. If this BLOCK has same ranges as its supercontext,
24865 lookup DW_AT_ranges attribute in the supercontext (and
24866 recursively so), verify that the ranges_table contains the
24867 right values and use it instead of adding a new .debug_range. */
24868 for (chain = stmt, pdie = die;
24869 BLOCK_SAME_RANGE (chain);
24870 chain = BLOCK_SUPERCONTEXT (chain))
24872 dw_attr_node *new_attr;
24874 pdie = pdie->die_parent;
24875 if (pdie == NULL)
24876 break;
24877 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24878 break;
24879 new_attr = get_AT (pdie, DW_AT_ranges);
24880 if (new_attr == NULL
24881 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24882 break;
24883 attr = new_attr;
24884 superblock = BLOCK_SUPERCONTEXT (chain);
24886 if (attr != NULL
24887 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24888 == (int)BLOCK_NUMBER (superblock))
24889 && BLOCK_FRAGMENT_CHAIN (superblock))
24891 unsigned long off = attr->dw_attr_val.v.val_offset;
24892 unsigned long supercnt = 0, thiscnt = 0;
24893 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24894 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24896 ++supercnt;
24897 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24898 == (int)BLOCK_NUMBER (chain));
24900 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24901 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24902 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24903 ++thiscnt;
24904 gcc_assert (supercnt >= thiscnt);
24905 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24906 false);
24907 note_rnglist_head (off + supercnt - thiscnt);
24908 return;
24911 unsigned int offset = add_ranges (stmt, true);
24912 add_AT_range_list (die, DW_AT_ranges, offset, false);
24913 note_rnglist_head (offset);
24915 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24916 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24919 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24920 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24921 chain = BLOCK_FRAGMENT_CHAIN (chain);
24923 while (chain);
24924 add_ranges (NULL);
24926 else
24928 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24929 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24930 BLOCK_NUMBER (stmt));
24931 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24932 BLOCK_NUMBER (stmt));
24933 add_AT_low_high_pc (die, label, label_high, false);
24937 /* Generate a DIE for a lexical block. */
24939 static void
24940 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24942 dw_die_ref old_die = lookup_block_die (stmt);
24943 dw_die_ref stmt_die = NULL;
24944 if (!old_die)
24946 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24947 equate_block_to_die (stmt, stmt_die);
24950 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24952 /* If this is an inlined or conrecte instance, create a new lexical
24953 die for anything below to attach DW_AT_abstract_origin to. */
24954 if (old_die)
24955 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24957 tree origin = block_ultimate_origin (stmt);
24958 if (origin != NULL_TREE && (origin != stmt || old_die))
24959 add_abstract_origin_attribute (stmt_die, origin);
24961 old_die = NULL;
24964 if (old_die)
24965 stmt_die = old_die;
24967 /* A non abstract block whose blocks have already been reordered
24968 should have the instruction range for this block. If so, set the
24969 high/low attributes. */
24970 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24972 gcc_assert (stmt_die);
24973 add_high_low_attributes (stmt, stmt_die);
24976 decls_for_scope (stmt, stmt_die);
24979 /* Generate a DIE for an inlined subprogram. */
24981 static void
24982 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24984 tree decl = block_ultimate_origin (stmt);
24986 /* Make sure any inlined functions are known to be inlineable. */
24987 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24988 || cgraph_function_possibly_inlined_p (decl));
24990 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24992 if (call_arg_locations || debug_inline_points)
24993 equate_block_to_die (stmt, subr_die);
24994 add_abstract_origin_attribute (subr_die, decl);
24995 if (TREE_ASM_WRITTEN (stmt))
24996 add_high_low_attributes (stmt, subr_die);
24997 add_call_src_coords_attributes (stmt, subr_die);
24999 /* The inliner creates an extra BLOCK for the parameter setup,
25000 we want to merge that with the actual outermost BLOCK of the
25001 inlined function to avoid duplicate locals in consumers.
25002 Do that by doing the recursion to subblocks on the single subblock
25003 of STMT. */
25004 bool unwrap_one = false;
25005 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
25007 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
25008 if (origin
25009 && TREE_CODE (origin) == BLOCK
25010 && BLOCK_SUPERCONTEXT (origin) == decl)
25011 unwrap_one = true;
25013 decls_for_scope (stmt, subr_die, !unwrap_one);
25014 if (unwrap_one)
25015 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
25018 /* Generate a DIE for a field in a record, or structure. CTX is required: see
25019 the comment for VLR_CONTEXT. */
25021 static void
25022 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
25024 dw_die_ref decl_die;
25026 if (TREE_TYPE (decl) == error_mark_node)
25027 return;
25029 decl_die = new_die (DW_TAG_member, context_die, decl);
25030 add_name_and_src_coords_attributes (decl_die, decl);
25031 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
25032 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
25033 context_die);
25035 if (DECL_BIT_FIELD_TYPE (decl))
25037 add_byte_size_attribute (decl_die, decl);
25038 add_bit_size_attribute (decl_die, decl);
25039 add_bit_offset_attribute (decl_die, decl);
25042 add_alignment_attribute (decl_die, decl);
25044 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
25045 add_data_member_location_attribute (decl_die, decl, ctx);
25047 if (DECL_ARTIFICIAL (decl))
25048 add_AT_flag (decl_die, DW_AT_artificial, 1);
25050 add_accessibility_attribute (decl_die, decl);
25052 /* Equate decl number to die, so that we can look up this decl later on. */
25053 equate_decl_number_to_die (decl, decl_die);
25056 /* Generate a DIE for a pointer to a member type. TYPE can be an
25057 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
25058 pointer to member function. */
25060 static void
25061 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
25063 if (lookup_type_die (type))
25064 return;
25066 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
25067 scope_die_for (type, context_die), type);
25069 equate_type_number_to_die (type, ptr_die);
25070 add_AT_die_ref (ptr_die, DW_AT_containing_type,
25071 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
25072 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
25073 context_die);
25074 add_alignment_attribute (ptr_die, type);
25076 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
25077 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
25079 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
25080 add_AT_loc (ptr_die, DW_AT_use_location, op);
25084 static char *producer_string;
25086 /* Given a C and/or C++ language/version string return the "highest".
25087 C++ is assumed to be "higher" than C in this case. Used for merging
25088 LTO translation unit languages. */
25089 static const char *
25090 highest_c_language (const char *lang1, const char *lang2)
25092 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
25093 return "GNU C++23";
25094 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
25095 return "GNU C++20";
25096 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
25097 return "GNU C++17";
25098 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
25099 return "GNU C++14";
25100 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
25101 return "GNU C++11";
25102 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
25103 return "GNU C++98";
25105 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
25106 return "GNU C2X";
25107 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
25108 return "GNU C17";
25109 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
25110 return "GNU C11";
25111 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
25112 return "GNU C99";
25113 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
25114 return "GNU C89";
25116 gcc_unreachable ();
25120 /* Generate the DIE for the compilation unit. */
25122 static dw_die_ref
25123 gen_compile_unit_die (const char *filename)
25125 dw_die_ref die;
25126 const char *language_string = lang_hooks.name;
25127 int language;
25129 die = new_die (DW_TAG_compile_unit, NULL, NULL);
25131 if (filename)
25133 add_filename_attribute (die, filename);
25134 /* Don't add cwd for <built-in>. */
25135 if (filename[0] != '<')
25136 add_comp_dir_attribute (die);
25139 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
25141 /* If our producer is LTO try to figure out a common language to use
25142 from the global list of translation units. */
25143 if (strcmp (language_string, "GNU GIMPLE") == 0)
25145 unsigned i;
25146 tree t;
25147 const char *common_lang = NULL;
25149 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
25151 if (!TRANSLATION_UNIT_LANGUAGE (t))
25152 continue;
25153 if (!common_lang)
25154 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
25155 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
25157 else if (startswith (common_lang, "GNU C")
25158 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
25159 /* Mixing C and C++ is ok, use C++ in that case. */
25160 common_lang = highest_c_language (common_lang,
25161 TRANSLATION_UNIT_LANGUAGE (t));
25162 else
25164 /* Fall back to C. */
25165 common_lang = NULL;
25166 break;
25170 if (common_lang)
25171 language_string = common_lang;
25174 language = DW_LANG_C;
25175 if (startswith (language_string, "GNU C")
25176 && ISDIGIT (language_string[5]))
25178 language = DW_LANG_C89;
25179 if (dwarf_version >= 3 || !dwarf_strict)
25181 if (strcmp (language_string, "GNU C89") != 0)
25182 language = DW_LANG_C99;
25184 if (dwarf_version >= 5 /* || !dwarf_strict */)
25185 if (strcmp (language_string, "GNU C11") == 0
25186 || strcmp (language_string, "GNU C17") == 0
25187 || strcmp (language_string, "GNU C2X") == 0)
25188 language = DW_LANG_C11;
25191 else if (startswith (language_string, "GNU C++"))
25193 language = DW_LANG_C_plus_plus;
25194 if (dwarf_version >= 5 /* || !dwarf_strict */)
25196 if (strcmp (language_string, "GNU C++11") == 0)
25197 language = DW_LANG_C_plus_plus_11;
25198 else if (strcmp (language_string, "GNU C++14") == 0)
25199 language = DW_LANG_C_plus_plus_14;
25200 else if (strcmp (language_string, "GNU C++17") == 0
25201 || strcmp (language_string, "GNU C++20") == 0
25202 || strcmp (language_string, "GNU C++23") == 0)
25203 /* For now. */
25204 language = DW_LANG_C_plus_plus_14;
25207 else if (strcmp (language_string, "GNU F77") == 0)
25208 language = DW_LANG_Fortran77;
25209 else if (dwarf_version >= 3 || !dwarf_strict)
25211 if (strcmp (language_string, "GNU Ada") == 0)
25212 language = DW_LANG_Ada95;
25213 else if (startswith (language_string, "GNU Fortran"))
25215 language = DW_LANG_Fortran95;
25216 if (dwarf_version >= 5 /* || !dwarf_strict */)
25218 if (strcmp (language_string, "GNU Fortran2003") == 0)
25219 language = DW_LANG_Fortran03;
25220 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25221 language = DW_LANG_Fortran08;
25224 else if (strcmp (language_string, "GNU Objective-C") == 0)
25225 language = DW_LANG_ObjC;
25226 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25227 language = DW_LANG_ObjC_plus_plus;
25228 else if (strcmp (language_string, "GNU D") == 0)
25229 language = DW_LANG_D;
25230 else if (dwarf_version >= 5 || !dwarf_strict)
25232 if (strcmp (language_string, "GNU Go") == 0)
25233 language = DW_LANG_Go;
25236 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25237 else if (startswith (language_string, "GNU Fortran"))
25238 language = DW_LANG_Fortran90;
25239 /* Likewise for Ada. */
25240 else if (strcmp (language_string, "GNU Ada") == 0)
25241 language = DW_LANG_Ada83;
25243 add_AT_unsigned (die, DW_AT_language, language);
25245 switch (language)
25247 case DW_LANG_Fortran77:
25248 case DW_LANG_Fortran90:
25249 case DW_LANG_Fortran95:
25250 case DW_LANG_Fortran03:
25251 case DW_LANG_Fortran08:
25252 /* Fortran has case insensitive identifiers and the front-end
25253 lowercases everything. */
25254 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25255 break;
25256 default:
25257 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25258 break;
25260 return die;
25263 /* Generate the DIE for a base class. */
25265 static void
25266 gen_inheritance_die (tree binfo, tree access, tree type,
25267 dw_die_ref context_die)
25269 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25270 struct vlr_context ctx = { type, NULL };
25272 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25273 context_die);
25274 add_data_member_location_attribute (die, binfo, &ctx);
25276 if (BINFO_VIRTUAL_P (binfo))
25277 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25279 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25280 children, otherwise the default is DW_ACCESS_public. In DWARF2
25281 the default has always been DW_ACCESS_private. */
25282 if (access == access_public_node)
25284 if (dwarf_version == 2
25285 || context_die->die_tag == DW_TAG_class_type)
25286 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25288 else if (access == access_protected_node)
25289 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25290 else if (dwarf_version > 2
25291 && context_die->die_tag != DW_TAG_class_type)
25292 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25295 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25296 structure. */
25298 static bool
25299 is_variant_part (tree decl)
25301 return (TREE_CODE (decl) == FIELD_DECL
25302 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25305 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25306 return the FIELD_DECL. Return NULL_TREE otherwise. */
25308 static tree
25309 analyze_discr_in_predicate (tree operand, tree struct_type)
25311 while (CONVERT_EXPR_P (operand))
25312 operand = TREE_OPERAND (operand, 0);
25314 /* Match field access to members of struct_type only. */
25315 if (TREE_CODE (operand) == COMPONENT_REF
25316 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25317 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25318 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25319 return TREE_OPERAND (operand, 1);
25320 else
25321 return NULL_TREE;
25324 /* Check that SRC is a constant integer that can be represented as a native
25325 integer constant (either signed or unsigned). If so, store it into DEST and
25326 return true. Return false otherwise. */
25328 static bool
25329 get_discr_value (tree src, dw_discr_value *dest)
25331 tree discr_type = TREE_TYPE (src);
25333 if (lang_hooks.types.get_debug_type)
25335 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25336 if (debug_type != NULL)
25337 discr_type = debug_type;
25340 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25341 return false;
25343 /* Signedness can vary between the original type and the debug type. This
25344 can happen for character types in Ada for instance: the character type
25345 used for code generation can be signed, to be compatible with the C one,
25346 but from a debugger point of view, it must be unsigned. */
25347 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25348 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25350 if (is_orig_unsigned != is_debug_unsigned)
25351 src = fold_convert (discr_type, src);
25353 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25354 return false;
25356 dest->pos = is_debug_unsigned;
25357 if (is_debug_unsigned)
25358 dest->v.uval = tree_to_uhwi (src);
25359 else
25360 dest->v.sval = tree_to_shwi (src);
25362 return true;
25365 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25366 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25367 store NULL_TREE in DISCR_DECL. Otherwise:
25369 - store the discriminant field in STRUCT_TYPE that controls the variant
25370 part to *DISCR_DECL
25372 - put in *DISCR_LISTS_P an array where for each variant, the item
25373 represents the corresponding matching list of discriminant values.
25375 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25376 the above array.
25378 Note that when the array is allocated (i.e. when the analysis is
25379 successful), it is up to the caller to free the array. */
25381 static void
25382 analyze_variants_discr (tree variant_part_decl,
25383 tree struct_type,
25384 tree *discr_decl,
25385 dw_discr_list_ref **discr_lists_p,
25386 unsigned *discr_lists_length)
25388 tree variant_part_type = TREE_TYPE (variant_part_decl);
25389 tree variant;
25390 dw_discr_list_ref *discr_lists;
25391 unsigned i;
25393 /* Compute how many variants there are in this variant part. */
25394 *discr_lists_length = 0;
25395 for (variant = TYPE_FIELDS (variant_part_type);
25396 variant != NULL_TREE;
25397 variant = DECL_CHAIN (variant))
25398 ++*discr_lists_length;
25400 *discr_decl = NULL_TREE;
25401 *discr_lists_p
25402 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25403 sizeof (**discr_lists_p));
25404 discr_lists = *discr_lists_p;
25406 /* And then analyze all variants to extract discriminant information for all
25407 of them. This analysis is conservative: as soon as we detect something we
25408 do not support, abort everything and pretend we found nothing. */
25409 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25410 variant != NULL_TREE;
25411 variant = DECL_CHAIN (variant), ++i)
25413 tree match_expr = DECL_QUALIFIER (variant);
25415 /* Now, try to analyze the predicate and deduce a discriminant for
25416 it. */
25417 if (match_expr == boolean_true_node)
25418 /* Typically happens for the default variant: it matches all cases that
25419 previous variants rejected. Don't output any matching value for
25420 this one. */
25421 continue;
25423 /* The following loop tries to iterate over each discriminant
25424 possibility: single values or ranges. */
25425 while (match_expr != NULL_TREE)
25427 tree next_round_match_expr;
25428 tree candidate_discr = NULL_TREE;
25429 dw_discr_list_ref new_node = NULL;
25431 /* Possibilities are matched one after the other by nested
25432 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25433 continue with the rest at next iteration. */
25434 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25436 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25437 match_expr = TREE_OPERAND (match_expr, 1);
25439 else
25440 next_round_match_expr = NULL_TREE;
25442 if (match_expr == boolean_false_node)
25443 /* This sub-expression matches nothing: just wait for the next
25444 one. */
25447 else if (TREE_CODE (match_expr) == EQ_EXPR)
25449 /* We are matching: <discr_field> == <integer_cst>
25450 This sub-expression matches a single value. */
25451 tree integer_cst = TREE_OPERAND (match_expr, 1);
25453 candidate_discr
25454 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25455 struct_type);
25457 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25458 if (!get_discr_value (integer_cst,
25459 &new_node->dw_discr_lower_bound))
25460 goto abort;
25461 new_node->dw_discr_range = false;
25464 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25466 /* We are matching:
25467 <discr_field> > <integer_cst>
25468 && <discr_field> < <integer_cst>.
25469 This sub-expression matches the range of values between the
25470 two matched integer constants. Note that comparisons can be
25471 inclusive or exclusive. */
25472 tree candidate_discr_1, candidate_discr_2;
25473 tree lower_cst, upper_cst;
25474 bool lower_cst_included, upper_cst_included;
25475 tree lower_op = TREE_OPERAND (match_expr, 0);
25476 tree upper_op = TREE_OPERAND (match_expr, 1);
25478 /* When the comparison is exclusive, the integer constant is not
25479 the discriminant range bound we are looking for: we will have
25480 to increment or decrement it. */
25481 if (TREE_CODE (lower_op) == GE_EXPR)
25482 lower_cst_included = true;
25483 else if (TREE_CODE (lower_op) == GT_EXPR)
25484 lower_cst_included = false;
25485 else
25486 goto abort;
25488 if (TREE_CODE (upper_op) == LE_EXPR)
25489 upper_cst_included = true;
25490 else if (TREE_CODE (upper_op) == LT_EXPR)
25491 upper_cst_included = false;
25492 else
25493 goto abort;
25495 /* Extract the discriminant from the first operand and check it
25496 is consistant with the same analysis in the second
25497 operand. */
25498 candidate_discr_1
25499 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25500 struct_type);
25501 candidate_discr_2
25502 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25503 struct_type);
25504 if (candidate_discr_1 == candidate_discr_2)
25505 candidate_discr = candidate_discr_1;
25506 else
25507 goto abort;
25509 /* Extract bounds from both. */
25510 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25511 lower_cst = TREE_OPERAND (lower_op, 1);
25512 upper_cst = TREE_OPERAND (upper_op, 1);
25514 if (!lower_cst_included)
25515 lower_cst
25516 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25517 build_int_cst (TREE_TYPE (lower_cst), 1));
25518 if (!upper_cst_included)
25519 upper_cst
25520 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25521 build_int_cst (TREE_TYPE (upper_cst), 1));
25523 if (!get_discr_value (lower_cst,
25524 &new_node->dw_discr_lower_bound)
25525 || !get_discr_value (upper_cst,
25526 &new_node->dw_discr_upper_bound))
25527 goto abort;
25529 new_node->dw_discr_range = true;
25532 else if ((candidate_discr
25533 = analyze_discr_in_predicate (match_expr, struct_type))
25534 && (TREE_TYPE (candidate_discr) == boolean_type_node
25535 || TREE_TYPE (TREE_TYPE (candidate_discr))
25536 == boolean_type_node))
25538 /* We are matching: <discr_field> for a boolean discriminant.
25539 This sub-expression matches boolean_true_node. */
25540 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25541 if (!get_discr_value (boolean_true_node,
25542 &new_node->dw_discr_lower_bound))
25543 goto abort;
25544 new_node->dw_discr_range = false;
25547 else
25548 /* Unsupported sub-expression: we cannot determine the set of
25549 matching discriminant values. Abort everything. */
25550 goto abort;
25552 /* If the discriminant info is not consistant with what we saw so
25553 far, consider the analysis failed and abort everything. */
25554 if (candidate_discr == NULL_TREE
25555 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25556 goto abort;
25557 else
25558 *discr_decl = candidate_discr;
25560 if (new_node != NULL)
25562 new_node->dw_discr_next = discr_lists[i];
25563 discr_lists[i] = new_node;
25565 match_expr = next_round_match_expr;
25569 /* If we reach this point, we could match everything we were interested
25570 in. */
25571 return;
25573 abort:
25574 /* Clean all data structure and return no result. */
25575 free (*discr_lists_p);
25576 *discr_lists_p = NULL;
25577 *discr_decl = NULL_TREE;
25580 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25581 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25582 under CONTEXT_DIE.
25584 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25585 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25586 this type, which are record types, represent the available variants and each
25587 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25588 values are inferred from these attributes.
25590 In trees, the offsets for the fields inside these sub-records are relative
25591 to the variant part itself, whereas the corresponding DIEs should have
25592 offset attributes that are relative to the embedding record base address.
25593 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25594 must be an expression that computes the offset of the variant part to
25595 describe in DWARF. */
25597 static void
25598 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25599 dw_die_ref context_die)
25601 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25602 tree variant_part_offset = vlr_ctx->variant_part_offset;
25604 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25605 NULL_TREE if there is no such field. */
25606 tree discr_decl = NULL_TREE;
25607 dw_discr_list_ref *discr_lists;
25608 unsigned discr_lists_length = 0;
25609 unsigned i;
25611 dw_die_ref dwarf_proc_die = NULL;
25612 dw_die_ref variant_part_die
25613 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25615 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25617 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25618 &discr_decl, &discr_lists, &discr_lists_length);
25620 if (discr_decl != NULL_TREE)
25622 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25624 if (discr_die)
25625 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25626 else
25627 /* We have no DIE for the discriminant, so just discard all
25628 discrimimant information in the output. */
25629 discr_decl = NULL_TREE;
25632 /* If the offset for this variant part is more complex than a constant,
25633 create a DWARF procedure for it so that we will not have to generate
25634 DWARF expressions for it for each member. */
25635 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25636 && (dwarf_version >= 3 || !dwarf_strict))
25638 struct loc_descr_context ctx = {
25639 vlr_ctx->struct_type, /* context_type */
25640 NULL_TREE, /* base_decl */
25641 NULL, /* dpi */
25642 false, /* placeholder_arg */
25643 false, /* placeholder_seen */
25644 false /* strict_signedness */
25646 const tree dwarf_proc_fndecl
25647 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25648 build_function_type (TREE_TYPE (variant_part_offset),
25649 NULL_TREE));
25650 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25651 const dw_loc_descr_ref dwarf_proc_body
25652 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25654 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25655 dwarf_proc_fndecl, context_die);
25656 if (dwarf_proc_die != NULL)
25657 variant_part_offset = dwarf_proc_call;
25660 /* Output DIEs for all variants. */
25661 i = 0;
25662 for (tree variant = TYPE_FIELDS (variant_part_type);
25663 variant != NULL_TREE;
25664 variant = DECL_CHAIN (variant), ++i)
25666 tree variant_type = TREE_TYPE (variant);
25667 dw_die_ref variant_die;
25669 /* All variants (i.e. members of a variant part) are supposed to be
25670 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25671 under these records. */
25672 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25674 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25675 equate_decl_number_to_die (variant, variant_die);
25677 /* Output discriminant values this variant matches, if any. */
25678 if (discr_decl == NULL || discr_lists[i] == NULL)
25679 /* In the case we have discriminant information at all, this is
25680 probably the default variant: as the standard says, don't
25681 output any discriminant value/list attribute. */
25683 else if (discr_lists[i]->dw_discr_next == NULL
25684 && !discr_lists[i]->dw_discr_range)
25685 /* If there is only one accepted value, don't bother outputting a
25686 list. */
25687 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25688 else
25689 add_discr_list (variant_die, discr_lists[i]);
25691 for (tree member = TYPE_FIELDS (variant_type);
25692 member != NULL_TREE;
25693 member = DECL_CHAIN (member))
25695 struct vlr_context vlr_sub_ctx = {
25696 vlr_ctx->struct_type, /* struct_type */
25697 NULL /* variant_part_offset */
25699 if (is_variant_part (member))
25701 /* All offsets for fields inside variant parts are relative to
25702 the top-level embedding RECORD_TYPE's base address. On the
25703 other hand, offsets in GCC's types are relative to the
25704 nested-most variant part. So we have to sum offsets each time
25705 we recurse. */
25707 vlr_sub_ctx.variant_part_offset
25708 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25709 variant_part_offset, byte_position (member));
25710 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25712 else
25714 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25715 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25720 free (discr_lists);
25723 /* Generate a DIE for a class member. */
25725 static void
25726 gen_member_die (tree type, dw_die_ref context_die)
25728 tree member;
25729 tree binfo = TYPE_BINFO (type);
25731 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25733 /* If this is not an incomplete type, output descriptions of each of its
25734 members. Note that as we output the DIEs necessary to represent the
25735 members of this record or union type, we will also be trying to output
25736 DIEs to represent the *types* of those members. However the `type'
25737 function (above) will specifically avoid generating type DIEs for member
25738 types *within* the list of member DIEs for this (containing) type except
25739 for those types (of members) which are explicitly marked as also being
25740 members of this (containing) type themselves. The g++ front- end can
25741 force any given type to be treated as a member of some other (containing)
25742 type by setting the TYPE_CONTEXT of the given (member) type to point to
25743 the TREE node representing the appropriate (containing) type. */
25745 /* First output info about the base classes. */
25746 if (binfo && early_dwarf)
25748 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25749 int i;
25750 tree base;
25752 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25753 gen_inheritance_die (base,
25754 (accesses ? (*accesses)[i] : access_public_node),
25755 type,
25756 context_die);
25759 /* Now output info about the members. */
25760 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25762 /* Ignore clones. */
25763 if (DECL_ABSTRACT_ORIGIN (member))
25764 continue;
25766 struct vlr_context vlr_ctx = { type, NULL_TREE };
25767 bool static_inline_p
25768 = (VAR_P (member)
25769 && TREE_STATIC (member)
25770 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25771 != -1));
25773 /* If we thought we were generating minimal debug info for TYPE
25774 and then changed our minds, some of the member declarations
25775 may have already been defined. Don't define them again, but
25776 do put them in the right order. */
25778 if (dw_die_ref child = lookup_decl_die (member))
25780 /* Handle inline static data members, which only have in-class
25781 declarations. */
25782 bool splice = true;
25784 dw_die_ref ref = NULL;
25785 if (child->die_tag == DW_TAG_variable
25786 && child->die_parent == comp_unit_die ())
25788 ref = get_AT_ref (child, DW_AT_specification);
25790 /* For C++17 inline static data members followed by redundant
25791 out of class redeclaration, we might get here with
25792 child being the DIE created for the out of class
25793 redeclaration and with its DW_AT_specification being
25794 the DIE created for in-class definition. We want to
25795 reparent the latter, and don't want to create another
25796 DIE with DW_AT_specification in that case, because
25797 we already have one. */
25798 if (ref
25799 && static_inline_p
25800 && ref->die_tag == DW_TAG_variable
25801 && ref->die_parent == comp_unit_die ()
25802 && get_AT (ref, DW_AT_specification) == NULL)
25804 child = ref;
25805 ref = NULL;
25806 static_inline_p = false;
25809 if (!ref)
25811 reparent_child (child, context_die);
25812 if (dwarf_version < 5)
25813 child->die_tag = DW_TAG_member;
25814 splice = false;
25817 else if (child->die_tag == DW_TAG_enumerator)
25818 /* Enumerators remain under their enumeration even if
25819 their names are introduced in the enclosing scope. */
25820 splice = false;
25822 if (splice)
25823 splice_child_die (context_die, child);
25826 /* Do not generate DWARF for variant parts if we are generating the
25827 corresponding GNAT encodings: DIEs generated for the two schemes
25828 would conflict in our mappings. */
25829 else if (is_variant_part (member)
25830 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25832 vlr_ctx.variant_part_offset = byte_position (member);
25833 gen_variant_part (member, &vlr_ctx, context_die);
25835 else
25837 vlr_ctx.variant_part_offset = NULL_TREE;
25838 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25841 /* For C++ inline static data members emit immediately a DW_TAG_variable
25842 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25843 DW_AT_specification. */
25844 if (static_inline_p)
25846 int old_extern = DECL_EXTERNAL (member);
25847 DECL_EXTERNAL (member) = 0;
25848 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25849 DECL_EXTERNAL (member) = old_extern;
25854 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25855 is set, we pretend that the type was never defined, so we only get the
25856 member DIEs needed by later specification DIEs. */
25858 static void
25859 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25860 enum debug_info_usage usage)
25862 if (TREE_ASM_WRITTEN (type))
25864 /* Fill in the bound of variable-length fields in late dwarf if
25865 still incomplete. */
25866 if (!early_dwarf && variably_modified_type_p (type, NULL))
25867 for (tree member = TYPE_FIELDS (type);
25868 member;
25869 member = DECL_CHAIN (member))
25870 fill_variable_array_bounds (TREE_TYPE (member));
25871 return;
25874 dw_die_ref type_die = lookup_type_die (type);
25875 dw_die_ref scope_die = 0;
25876 int nested = 0;
25877 int complete = (TYPE_SIZE (type)
25878 && (! TYPE_STUB_DECL (type)
25879 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25880 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25881 complete = complete && should_emit_struct_debug (type, usage);
25883 if (type_die && ! complete)
25884 return;
25886 if (TYPE_CONTEXT (type) != NULL_TREE
25887 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25888 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25889 nested = 1;
25891 scope_die = scope_die_for (type, context_die);
25893 /* Generate child dies for template parameters. */
25894 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25895 schedule_generic_params_dies_gen (type);
25897 if (! type_die || (nested && is_cu_die (scope_die)))
25898 /* First occurrence of type or toplevel definition of nested class. */
25900 dw_die_ref old_die = type_die;
25902 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25903 ? record_type_tag (type) : DW_TAG_union_type,
25904 scope_die, type);
25905 equate_type_number_to_die (type, type_die);
25906 if (old_die)
25907 add_AT_specification (type_die, old_die);
25908 else
25909 add_name_attribute (type_die, type_tag (type));
25911 else
25912 remove_AT (type_die, DW_AT_declaration);
25914 /* If this type has been completed, then give it a byte_size attribute and
25915 then give a list of members. */
25916 if (complete && !ns_decl)
25918 /* Prevent infinite recursion in cases where the type of some member of
25919 this type is expressed in terms of this type itself. */
25920 TREE_ASM_WRITTEN (type) = 1;
25921 add_byte_size_attribute (type_die, type);
25922 add_alignment_attribute (type_die, type);
25923 if (TYPE_STUB_DECL (type) != NULL_TREE)
25925 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25926 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25929 /* If the first reference to this type was as the return type of an
25930 inline function, then it may not have a parent. Fix this now. */
25931 if (type_die->die_parent == NULL)
25932 add_child_die (scope_die, type_die);
25934 gen_member_die (type, type_die);
25936 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25937 if (TYPE_ARTIFICIAL (type))
25938 add_AT_flag (type_die, DW_AT_artificial, 1);
25940 /* GNU extension: Record what type our vtable lives in. */
25941 if (TYPE_VFIELD (type))
25943 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25945 gen_type_die (vtype, context_die);
25946 add_AT_die_ref (type_die, DW_AT_containing_type,
25947 lookup_type_die (vtype));
25950 else
25952 add_AT_flag (type_die, DW_AT_declaration, 1);
25954 /* We don't need to do this for function-local types. */
25955 if (TYPE_STUB_DECL (type)
25956 && ! decl_function_context (TYPE_STUB_DECL (type)))
25957 vec_safe_push (incomplete_types, type);
25960 if (get_AT (type_die, DW_AT_name))
25961 add_pubtype (type, type_die);
25964 /* Generate a DIE for a subroutine _type_. */
25966 static void
25967 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25969 tree return_type = TREE_TYPE (type);
25970 dw_die_ref subr_die
25971 = new_die (DW_TAG_subroutine_type,
25972 scope_die_for (type, context_die), type);
25974 equate_type_number_to_die (type, subr_die);
25975 add_prototyped_attribute (subr_die, type);
25976 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25977 context_die);
25978 add_alignment_attribute (subr_die, type);
25979 gen_formal_types_die (type, subr_die);
25981 if (get_AT (subr_die, DW_AT_name))
25982 add_pubtype (type, subr_die);
25983 if ((dwarf_version >= 5 || !dwarf_strict)
25984 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25985 add_AT_flag (subr_die, DW_AT_reference, 1);
25986 if ((dwarf_version >= 5 || !dwarf_strict)
25987 && lang_hooks.types.type_dwarf_attribute (type,
25988 DW_AT_rvalue_reference) != -1)
25989 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25992 /* Generate a DIE for a type definition. */
25994 static void
25995 gen_typedef_die (tree decl, dw_die_ref context_die)
25997 dw_die_ref type_die;
25998 tree type;
26000 if (TREE_ASM_WRITTEN (decl))
26002 if (DECL_ORIGINAL_TYPE (decl))
26003 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
26004 return;
26007 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
26008 checks in process_scope_var and modified_type_die), this should be called
26009 only for original types. */
26010 gcc_assert (decl_ultimate_origin (decl) == NULL
26011 || decl_ultimate_origin (decl) == decl);
26013 TREE_ASM_WRITTEN (decl) = 1;
26014 type_die = new_die (DW_TAG_typedef, context_die, decl);
26016 add_name_and_src_coords_attributes (type_die, decl);
26017 if (DECL_ORIGINAL_TYPE (decl))
26019 type = DECL_ORIGINAL_TYPE (decl);
26020 if (type == error_mark_node)
26021 return;
26023 gcc_assert (type != TREE_TYPE (decl));
26024 equate_type_number_to_die (TREE_TYPE (decl), type_die);
26026 else
26028 type = TREE_TYPE (decl);
26029 if (type == error_mark_node)
26030 return;
26032 if (is_naming_typedef_decl (TYPE_NAME (type)))
26034 /* Here, we are in the case of decl being a typedef naming
26035 an anonymous type, e.g:
26036 typedef struct {...} foo;
26037 In that case TREE_TYPE (decl) is not a typedef variant
26038 type and TYPE_NAME of the anonymous type is set to the
26039 TYPE_DECL of the typedef. This construct is emitted by
26040 the C++ FE.
26042 TYPE is the anonymous struct named by the typedef
26043 DECL. As we need the DW_AT_type attribute of the
26044 DW_TAG_typedef to point to the DIE of TYPE, let's
26045 generate that DIE right away. add_type_attribute
26046 called below will then pick (via lookup_type_die) that
26047 anonymous struct DIE. */
26048 if (!TREE_ASM_WRITTEN (type))
26049 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
26051 /* This is a GNU Extension. We are adding a
26052 DW_AT_linkage_name attribute to the DIE of the
26053 anonymous struct TYPE. The value of that attribute
26054 is the name of the typedef decl naming the anonymous
26055 struct. This greatly eases the work of consumers of
26056 this debug info. */
26057 add_linkage_name_raw (lookup_type_die (type), decl);
26061 add_type_attribute (type_die, type, decl_quals (decl), false,
26062 context_die);
26064 if (is_naming_typedef_decl (decl))
26065 /* We want that all subsequent calls to lookup_type_die with
26066 TYPE in argument yield the DW_TAG_typedef we have just
26067 created. */
26068 equate_type_number_to_die (type, type_die);
26070 add_alignment_attribute (type_die, TREE_TYPE (decl));
26072 add_accessibility_attribute (type_die, decl);
26074 if (DECL_ABSTRACT_P (decl))
26075 equate_decl_number_to_die (decl, type_die);
26077 if (get_AT (type_die, DW_AT_name))
26078 add_pubtype (decl, type_die);
26081 /* Generate a DIE for a struct, class, enum or union type. */
26083 static void
26084 gen_tagged_type_die (tree type,
26085 dw_die_ref context_die,
26086 enum debug_info_usage usage)
26088 if (type == NULL_TREE
26089 || !is_tagged_type (type))
26090 return;
26092 if (TREE_ASM_WRITTEN (type))
26094 /* If this is a nested type whose containing class hasn't been written
26095 out yet, writing it out will cover this one, too. This does not apply
26096 to instantiations of member class templates; they need to be added to
26097 the containing class as they are generated. FIXME: This hurts the
26098 idea of combining type decls from multiple TUs, since we can't predict
26099 what set of template instantiations we'll get. */
26100 else if (TYPE_CONTEXT (type)
26101 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
26102 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
26104 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
26106 if (TREE_ASM_WRITTEN (type))
26107 return;
26109 /* If that failed, attach ourselves to the stub. */
26110 context_die = lookup_type_die (TYPE_CONTEXT (type));
26112 else if (TYPE_CONTEXT (type) != NULL_TREE
26113 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
26115 /* If this type is local to a function that hasn't been written
26116 out yet, use a NULL context for now; it will be fixed up in
26117 decls_for_scope. */
26118 context_die = lookup_decl_die (TYPE_CONTEXT (type));
26119 /* A declaration DIE doesn't count; nested types need to go in the
26120 specification. */
26121 if (context_die && is_declaration_die (context_die))
26122 context_die = NULL;
26124 else
26125 context_die = declare_in_namespace (type, context_die);
26127 if (TREE_CODE (type) == ENUMERAL_TYPE)
26129 /* This might have been written out by the call to
26130 declare_in_namespace. */
26131 if (!TREE_ASM_WRITTEN (type))
26132 gen_enumeration_type_die (type, context_die);
26134 else
26135 gen_struct_or_union_type_die (type, context_die, usage);
26137 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
26138 it up if it is ever completed. gen_*_type_die will set it for us
26139 when appropriate. */
26142 /* Generate a type description DIE. */
26144 static void
26145 gen_type_die_with_usage (tree type, dw_die_ref context_die,
26146 enum debug_info_usage usage)
26148 struct array_descr_info info;
26150 if (type == NULL_TREE || type == error_mark_node)
26151 return;
26153 if (flag_checking && type)
26154 verify_type (type);
26156 if (TYPE_NAME (type) != NULL_TREE
26157 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
26158 && is_redundant_typedef (TYPE_NAME (type))
26159 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26160 /* The DECL of this type is a typedef we don't want to emit debug
26161 info for but we want debug info for its underlying typedef.
26162 This can happen for e.g, the injected-class-name of a C++
26163 type. */
26164 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26166 /* If TYPE is a typedef type variant, let's generate debug info
26167 for the parent typedef which TYPE is a type of. */
26168 if (typedef_variant_p (type))
26170 if (TREE_ASM_WRITTEN (type))
26171 return;
26173 tree name = TYPE_NAME (type);
26174 tree origin = decl_ultimate_origin (name);
26175 if (origin != NULL && origin != name)
26177 gen_decl_die (origin, NULL, NULL, context_die);
26178 return;
26181 /* Prevent broken recursion; we can't hand off to the same type. */
26182 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26184 /* Give typedefs the right scope. */
26185 context_die = scope_die_for (type, context_die);
26187 TREE_ASM_WRITTEN (type) = 1;
26189 gen_decl_die (name, NULL, NULL, context_die);
26190 return;
26193 /* If type is an anonymous tagged type named by a typedef, let's
26194 generate debug info for the typedef. */
26195 if (is_naming_typedef_decl (TYPE_NAME (type)))
26197 /* Give typedefs the right scope. */
26198 context_die = scope_die_for (type, context_die);
26200 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26201 return;
26204 if (lang_hooks.types.get_debug_type)
26206 tree debug_type = lang_hooks.types.get_debug_type (type);
26208 if (debug_type != NULL_TREE && debug_type != type)
26210 gen_type_die_with_usage (debug_type, context_die, usage);
26211 return;
26215 /* We are going to output a DIE to represent the unqualified version
26216 of this type (i.e. without any const or volatile qualifiers) so
26217 get the main variant (i.e. the unqualified version) of this type
26218 now. (Vectors and arrays are special because the debugging info is in the
26219 cloned type itself. Similarly function/method types can contain extra
26220 ref-qualification). */
26221 if (TREE_CODE (type) == FUNCTION_TYPE
26222 || TREE_CODE (type) == METHOD_TYPE)
26224 /* For function/method types, can't use type_main_variant here,
26225 because that can have different ref-qualifiers for C++,
26226 but try to canonicalize. */
26227 tree main = TYPE_MAIN_VARIANT (type);
26228 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26229 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26230 && check_base_type (t, main)
26231 && check_lang_type (t, type))
26233 type = t;
26234 break;
26237 else if (TREE_CODE (type) != VECTOR_TYPE
26238 && TREE_CODE (type) != ARRAY_TYPE)
26239 type = type_main_variant (type);
26241 /* If this is an array type with hidden descriptor, handle it first. */
26242 if (!TREE_ASM_WRITTEN (type)
26243 && lang_hooks.types.get_array_descr_info)
26245 memset (&info, 0, sizeof (info));
26246 if (lang_hooks.types.get_array_descr_info (type, &info))
26248 /* Fortran sometimes emits array types with no dimension. */
26249 gcc_assert (info.ndimensions >= 0
26250 && (info.ndimensions
26251 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26252 gen_descr_array_type_die (type, &info, context_die);
26253 TREE_ASM_WRITTEN (type) = 1;
26254 return;
26258 if (TREE_ASM_WRITTEN (type))
26260 /* Variable-length types may be incomplete even if
26261 TREE_ASM_WRITTEN. For such types, fall through to
26262 gen_array_type_die() and possibly fill in
26263 DW_AT_{upper,lower}_bound attributes. */
26264 if ((TREE_CODE (type) != ARRAY_TYPE
26265 && TREE_CODE (type) != RECORD_TYPE
26266 && TREE_CODE (type) != UNION_TYPE
26267 && TREE_CODE (type) != QUAL_UNION_TYPE)
26268 || !variably_modified_type_p (type, NULL))
26269 return;
26272 switch (TREE_CODE (type))
26274 case ERROR_MARK:
26275 break;
26277 case POINTER_TYPE:
26278 case REFERENCE_TYPE:
26279 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26280 ensures that the gen_type_die recursion will terminate even if the
26281 type is recursive. Recursive types are possible in Ada. */
26282 /* ??? We could perhaps do this for all types before the switch
26283 statement. */
26284 TREE_ASM_WRITTEN (type) = 1;
26286 /* For these types, all that is required is that we output a DIE (or a
26287 set of DIEs) to represent the "basis" type. */
26288 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26289 DINFO_USAGE_IND_USE);
26290 break;
26292 case OFFSET_TYPE:
26293 /* This code is used for C++ pointer-to-data-member types.
26294 Output a description of the relevant class type. */
26295 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26296 DINFO_USAGE_IND_USE);
26298 /* Output a description of the type of the object pointed to. */
26299 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26300 DINFO_USAGE_IND_USE);
26302 /* Now output a DIE to represent this pointer-to-data-member type
26303 itself. */
26304 gen_ptr_to_mbr_type_die (type, context_die);
26305 break;
26307 case FUNCTION_TYPE:
26308 /* Force out return type (in case it wasn't forced out already). */
26309 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26310 DINFO_USAGE_DIR_USE);
26311 gen_subroutine_type_die (type, context_die);
26312 break;
26314 case METHOD_TYPE:
26315 /* Force out return type (in case it wasn't forced out already). */
26316 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26317 DINFO_USAGE_DIR_USE);
26318 gen_subroutine_type_die (type, context_die);
26319 break;
26321 case ARRAY_TYPE:
26322 case VECTOR_TYPE:
26323 gen_array_type_die (type, context_die);
26324 break;
26326 case ENUMERAL_TYPE:
26327 case RECORD_TYPE:
26328 case UNION_TYPE:
26329 case QUAL_UNION_TYPE:
26330 gen_tagged_type_die (type, context_die, usage);
26331 return;
26333 case VOID_TYPE:
26334 case OPAQUE_TYPE:
26335 case INTEGER_TYPE:
26336 case REAL_TYPE:
26337 case FIXED_POINT_TYPE:
26338 case COMPLEX_TYPE:
26339 case BOOLEAN_TYPE:
26340 /* No DIEs needed for fundamental types. */
26341 break;
26343 case NULLPTR_TYPE:
26344 case LANG_TYPE:
26345 /* Just use DW_TAG_unspecified_type. */
26347 dw_die_ref type_die = lookup_type_die (type);
26348 if (type_die == NULL)
26350 tree name = TYPE_IDENTIFIER (type);
26351 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26352 type);
26353 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26354 equate_type_number_to_die (type, type_die);
26357 break;
26359 default:
26360 if (is_cxx_auto (type))
26362 tree name = TYPE_IDENTIFIER (type);
26363 dw_die_ref *die = (name == get_identifier ("auto")
26364 ? &auto_die : &decltype_auto_die);
26365 if (!*die)
26367 *die = new_die (DW_TAG_unspecified_type,
26368 comp_unit_die (), NULL_TREE);
26369 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26371 equate_type_number_to_die (type, *die);
26372 break;
26374 gcc_unreachable ();
26377 TREE_ASM_WRITTEN (type) = 1;
26380 static void
26381 gen_type_die (tree type, dw_die_ref context_die)
26383 if (type != error_mark_node)
26385 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26386 if (flag_checking)
26388 dw_die_ref die = lookup_type_die (type);
26389 if (die)
26390 check_die (die);
26395 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26396 things which are local to the given block. */
26398 static void
26399 gen_block_die (tree stmt, dw_die_ref context_die)
26401 int must_output_die = 0;
26402 bool inlined_func;
26404 /* Ignore blocks that are NULL. */
26405 if (stmt == NULL_TREE)
26406 return;
26408 inlined_func = inlined_function_outer_scope_p (stmt);
26410 /* If the block is one fragment of a non-contiguous block, do not
26411 process the variables, since they will have been done by the
26412 origin block. Do process subblocks. */
26413 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26415 tree sub;
26417 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26418 gen_block_die (sub, context_die);
26420 return;
26423 /* Determine if we need to output any Dwarf DIEs at all to represent this
26424 block. */
26425 if (inlined_func)
26426 /* The outer scopes for inlinings *must* always be represented. We
26427 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26428 must_output_die = 1;
26429 else if (lookup_block_die (stmt))
26430 /* If we already have a DIE then it was filled early. Meanwhile
26431 we might have pruned all BLOCK_VARS as optimized out but we
26432 still want to generate high/low PC attributes so output it. */
26433 must_output_die = 1;
26434 else if (TREE_USED (stmt)
26435 || TREE_ASM_WRITTEN (stmt))
26437 /* Determine if this block directly contains any "significant"
26438 local declarations which we will need to output DIEs for. */
26439 if (debug_info_level > DINFO_LEVEL_TERSE)
26441 /* We are not in terse mode so any local declaration that
26442 is not ignored for debug purposes counts as being a
26443 "significant" one. */
26444 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26445 must_output_die = 1;
26446 else
26447 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26448 if (!DECL_IGNORED_P (var))
26450 must_output_die = 1;
26451 break;
26454 else if (!dwarf2out_ignore_block (stmt))
26455 must_output_die = 1;
26458 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26459 DIE for any block which contains no significant local declarations at
26460 all. Rather, in such cases we just call `decls_for_scope' so that any
26461 needed Dwarf info for any sub-blocks will get properly generated. Note
26462 that in terse mode, our definition of what constitutes a "significant"
26463 local declaration gets restricted to include only inlined function
26464 instances and local (nested) function definitions. */
26465 if (must_output_die)
26467 if (inlined_func)
26468 gen_inlined_subroutine_die (stmt, context_die);
26469 else
26470 gen_lexical_block_die (stmt, context_die);
26472 else
26473 decls_for_scope (stmt, context_die);
26476 /* Process variable DECL (or variable with origin ORIGIN) within
26477 block STMT and add it to CONTEXT_DIE. */
26478 static void
26479 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26481 dw_die_ref die;
26482 tree decl_or_origin = decl ? decl : origin;
26484 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26485 die = lookup_decl_die (decl_or_origin);
26486 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26488 if (TYPE_DECL_IS_STUB (decl_or_origin))
26489 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26490 else
26491 die = lookup_decl_die (decl_or_origin);
26492 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26493 if (! die && ! early_dwarf)
26494 return;
26496 else
26497 die = NULL;
26499 /* Avoid creating DIEs for local typedefs and concrete static variables that
26500 will only be pruned later. */
26501 if ((origin || decl_ultimate_origin (decl))
26502 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26503 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26505 origin = decl_ultimate_origin (decl_or_origin);
26506 if (decl && VAR_P (decl) && die != NULL)
26508 die = lookup_decl_die (origin);
26509 if (die != NULL)
26510 equate_decl_number_to_die (decl, die);
26512 return;
26515 if (die != NULL && die->die_parent == NULL)
26516 add_child_die (context_die, die);
26517 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26519 if (early_dwarf)
26520 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26521 stmt, context_die);
26523 else
26525 if (decl && DECL_P (decl))
26527 die = lookup_decl_die (decl);
26529 /* Early created DIEs do not have a parent as the decls refer
26530 to the function as DECL_CONTEXT rather than the BLOCK. */
26531 if (die && die->die_parent == NULL)
26533 gcc_assert (in_lto_p);
26534 add_child_die (context_die, die);
26538 gen_decl_die (decl, origin, NULL, context_die);
26542 /* Generate all of the decls declared within a given scope and (recursively)
26543 all of its sub-blocks. */
26545 static void
26546 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26548 tree decl;
26549 unsigned int i;
26550 tree subblocks;
26552 /* Ignore NULL blocks. */
26553 if (stmt == NULL_TREE)
26554 return;
26556 /* Output the DIEs to represent all of the data objects and typedefs
26557 declared directly within this block but not within any nested
26558 sub-blocks. Also, nested function and tag DIEs have been
26559 generated with a parent of NULL; fix that up now. We don't
26560 have to do this if we're at -g1. */
26561 if (debug_info_level > DINFO_LEVEL_TERSE)
26563 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26564 process_scope_var (stmt, decl, NULL_TREE, context_die);
26565 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26566 origin - avoid doing this twice as we have no good way to see
26567 if we've done it once already. */
26568 if (! early_dwarf)
26569 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26571 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26572 if (decl == current_function_decl)
26573 /* Ignore declarations of the current function, while they
26574 are declarations, gen_subprogram_die would treat them
26575 as definitions again, because they are equal to
26576 current_function_decl and endlessly recurse. */;
26577 else if (TREE_CODE (decl) == FUNCTION_DECL)
26578 process_scope_var (stmt, decl, NULL_TREE, context_die);
26579 else
26580 process_scope_var (stmt, NULL_TREE, decl, context_die);
26584 /* Even if we're at -g1, we need to process the subblocks in order to get
26585 inlined call information. */
26587 /* Output the DIEs to represent all sub-blocks (and the items declared
26588 therein) of this block. */
26589 if (recurse)
26590 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26591 subblocks != NULL;
26592 subblocks = BLOCK_CHAIN (subblocks))
26593 gen_block_die (subblocks, context_die);
26596 /* Is this a typedef we can avoid emitting? */
26598 static bool
26599 is_redundant_typedef (const_tree decl)
26601 if (TYPE_DECL_IS_STUB (decl))
26602 return true;
26604 if (DECL_ARTIFICIAL (decl)
26605 && DECL_CONTEXT (decl)
26606 && is_tagged_type (DECL_CONTEXT (decl))
26607 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26608 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26609 /* Also ignore the artificial member typedef for the class name. */
26610 return true;
26612 return false;
26615 /* Return TRUE if TYPE is a typedef that names a type for linkage
26616 purposes. This kind of typedefs is produced by the C++ FE for
26617 constructs like:
26619 typedef struct {...} foo;
26621 In that case, there is no typedef variant type produced for foo.
26622 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26623 struct type. */
26625 static bool
26626 is_naming_typedef_decl (const_tree decl)
26628 if (decl == NULL_TREE
26629 || TREE_CODE (decl) != TYPE_DECL
26630 || DECL_NAMELESS (decl)
26631 || !is_tagged_type (TREE_TYPE (decl))
26632 || DECL_IS_UNDECLARED_BUILTIN (decl)
26633 || is_redundant_typedef (decl)
26634 /* It looks like Ada produces TYPE_DECLs that are very similar
26635 to C++ naming typedefs but that have different
26636 semantics. Let's be specific to c++ for now. */
26637 || !is_cxx (decl))
26638 return FALSE;
26640 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26641 && TYPE_NAME (TREE_TYPE (decl)) == decl
26642 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26643 != TYPE_NAME (TREE_TYPE (decl))));
26646 /* Looks up the DIE for a context. */
26648 static inline dw_die_ref
26649 lookup_context_die (tree context)
26651 if (context)
26653 /* Find die that represents this context. */
26654 if (TYPE_P (context))
26656 context = TYPE_MAIN_VARIANT (context);
26657 dw_die_ref ctx = lookup_type_die (context);
26658 if (!ctx)
26659 return NULL;
26660 return strip_naming_typedef (context, ctx);
26662 else
26663 return lookup_decl_die (context);
26665 return comp_unit_die ();
26668 /* Returns the DIE for a context. */
26670 static inline dw_die_ref
26671 get_context_die (tree context)
26673 if (context)
26675 /* Find die that represents this context. */
26676 if (TYPE_P (context))
26678 context = TYPE_MAIN_VARIANT (context);
26679 return strip_naming_typedef (context, force_type_die (context));
26681 else
26682 return force_decl_die (context);
26684 return comp_unit_die ();
26687 /* Returns the DIE for decl. A DIE will always be returned. */
26689 static dw_die_ref
26690 force_decl_die (tree decl)
26692 dw_die_ref decl_die;
26693 unsigned saved_external_flag;
26694 tree save_fn = NULL_TREE;
26695 decl_die = lookup_decl_die (decl);
26696 if (!decl_die)
26698 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26700 decl_die = lookup_decl_die (decl);
26701 if (decl_die)
26702 return decl_die;
26704 switch (TREE_CODE (decl))
26706 case FUNCTION_DECL:
26707 /* Clear current_function_decl, so that gen_subprogram_die thinks
26708 that this is a declaration. At this point, we just want to force
26709 declaration die. */
26710 save_fn = current_function_decl;
26711 current_function_decl = NULL_TREE;
26712 gen_subprogram_die (decl, context_die);
26713 current_function_decl = save_fn;
26714 break;
26716 case VAR_DECL:
26717 /* Set external flag to force declaration die. Restore it after
26718 gen_decl_die() call. */
26719 saved_external_flag = DECL_EXTERNAL (decl);
26720 DECL_EXTERNAL (decl) = 1;
26721 gen_decl_die (decl, NULL, NULL, context_die);
26722 DECL_EXTERNAL (decl) = saved_external_flag;
26723 break;
26725 case NAMESPACE_DECL:
26726 if (dwarf_version >= 3 || !dwarf_strict)
26727 dwarf2out_decl (decl);
26728 else
26729 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26730 decl_die = comp_unit_die ();
26731 break;
26733 case CONST_DECL:
26734 /* Enumerators shouldn't need force_decl_die. */
26735 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26736 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26737 gen_decl_die (decl, NULL, NULL, context_die);
26738 break;
26740 case TRANSLATION_UNIT_DECL:
26741 decl_die = comp_unit_die ();
26742 break;
26744 default:
26745 gcc_unreachable ();
26748 /* We should be able to find the DIE now. */
26749 if (!decl_die)
26750 decl_die = lookup_decl_die (decl);
26751 gcc_assert (decl_die);
26754 return decl_die;
26757 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26758 always returned. */
26760 static dw_die_ref
26761 force_type_die (tree type)
26763 dw_die_ref type_die;
26765 type_die = lookup_type_die (type);
26766 if (!type_die)
26768 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26770 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26771 false, context_die);
26772 gcc_assert (type_die);
26774 return type_die;
26777 /* Force out any required namespaces to be able to output DECL,
26778 and return the new context_die for it, if it's changed. */
26780 static dw_die_ref
26781 setup_namespace_context (tree thing, dw_die_ref context_die)
26783 tree context = (DECL_P (thing)
26784 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26785 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26786 /* Force out the namespace. */
26787 context_die = force_decl_die (context);
26789 return context_die;
26792 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26793 type) within its namespace, if appropriate.
26795 For compatibility with older debuggers, namespace DIEs only contain
26796 declarations; all definitions are emitted at CU scope, with
26797 DW_AT_specification pointing to the declaration (like with class
26798 members). */
26800 static dw_die_ref
26801 declare_in_namespace (tree thing, dw_die_ref context_die)
26803 dw_die_ref ns_context;
26805 if (debug_info_level <= DINFO_LEVEL_TERSE)
26806 return context_die;
26808 /* External declarations in the local scope only need to be emitted
26809 once, not once in the namespace and once in the scope.
26811 This avoids declaring the `extern' below in the
26812 namespace DIE as well as in the innermost scope:
26814 namespace S
26816 int i=5;
26817 int foo()
26819 int i=8;
26820 extern int i;
26821 return i;
26825 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26826 return context_die;
26828 /* If this decl is from an inlined function, then don't try to emit it in its
26829 namespace, as we will get confused. It would have already been emitted
26830 when the abstract instance of the inline function was emitted anyways. */
26831 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26832 return context_die;
26834 ns_context = setup_namespace_context (thing, context_die);
26836 if (ns_context != context_die)
26838 if (is_fortran () || is_dlang ())
26839 return ns_context;
26840 if (DECL_P (thing))
26841 gen_decl_die (thing, NULL, NULL, ns_context);
26842 else
26843 gen_type_die (thing, ns_context);
26845 return context_die;
26848 /* Generate a DIE for a namespace or namespace alias. */
26850 static void
26851 gen_namespace_die (tree decl, dw_die_ref context_die)
26853 dw_die_ref namespace_die;
26855 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26856 they are an alias of. */
26857 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26859 /* Output a real namespace or module. */
26860 context_die = setup_namespace_context (decl, comp_unit_die ());
26861 namespace_die = new_die (is_fortran () || is_dlang ()
26862 ? DW_TAG_module : DW_TAG_namespace,
26863 context_die, decl);
26864 /* For Fortran modules defined in different CU don't add src coords. */
26865 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26867 const char *name = dwarf2_name (decl, 0);
26868 if (name)
26869 add_name_attribute (namespace_die, name);
26871 else
26872 add_name_and_src_coords_attributes (namespace_die, decl);
26873 if (DECL_EXTERNAL (decl))
26874 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26875 equate_decl_number_to_die (decl, namespace_die);
26877 else
26879 /* Output a namespace alias. */
26881 /* Force out the namespace we are an alias of, if necessary. */
26882 dw_die_ref origin_die
26883 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26885 if (DECL_FILE_SCOPE_P (decl)
26886 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26887 context_die = setup_namespace_context (decl, comp_unit_die ());
26888 /* Now create the namespace alias DIE. */
26889 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26890 add_name_and_src_coords_attributes (namespace_die, decl);
26891 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26892 equate_decl_number_to_die (decl, namespace_die);
26894 if ((dwarf_version >= 5 || !dwarf_strict)
26895 && lang_hooks.decls.decl_dwarf_attribute (decl,
26896 DW_AT_export_symbols) == 1)
26897 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26899 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26900 if (want_pubnames ())
26901 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26904 /* Generate Dwarf debug information for a decl described by DECL.
26905 The return value is currently only meaningful for PARM_DECLs,
26906 for all other decls it returns NULL.
26908 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26909 It can be NULL otherwise. */
26911 static dw_die_ref
26912 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26913 dw_die_ref context_die)
26915 tree decl_or_origin = decl ? decl : origin;
26916 tree class_origin = NULL, ultimate_origin;
26918 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26919 return NULL;
26921 switch (TREE_CODE (decl_or_origin))
26923 case ERROR_MARK:
26924 break;
26926 case CONST_DECL:
26927 if (!is_fortran () && !is_ada () && !is_dlang ())
26929 /* The individual enumerators of an enum type get output when we output
26930 the Dwarf representation of the relevant enum type itself. */
26931 break;
26934 /* Emit its type. */
26935 gen_type_die (TREE_TYPE (decl), context_die);
26937 /* And its containing namespace. */
26938 context_die = declare_in_namespace (decl, context_die);
26940 gen_const_die (decl, context_die);
26941 break;
26943 case FUNCTION_DECL:
26944 #if 0
26945 /* FIXME */
26946 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26947 on local redeclarations of global functions. That seems broken. */
26948 if (current_function_decl != decl)
26949 /* This is only a declaration. */;
26950 #endif
26952 /* We should have abstract copies already and should not generate
26953 stray type DIEs in late LTO dumping. */
26954 if (! early_dwarf)
26957 /* If we're emitting a clone, emit info for the abstract instance. */
26958 else if (origin || DECL_ORIGIN (decl) != decl)
26959 dwarf2out_abstract_function (origin
26960 ? DECL_ORIGIN (origin)
26961 : DECL_ABSTRACT_ORIGIN (decl));
26963 /* If we're emitting a possibly inlined function emit it as
26964 abstract instance. */
26965 else if (cgraph_function_possibly_inlined_p (decl)
26966 && ! DECL_ABSTRACT_P (decl)
26967 && ! class_or_namespace_scope_p (context_die)
26968 /* dwarf2out_abstract_function won't emit a die if this is just
26969 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26970 that case, because that works only if we have a die. */
26971 && DECL_INITIAL (decl) != NULL_TREE)
26972 dwarf2out_abstract_function (decl);
26974 /* Otherwise we're emitting the primary DIE for this decl. */
26975 else if (debug_info_level > DINFO_LEVEL_TERSE)
26977 /* Before we describe the FUNCTION_DECL itself, make sure that we
26978 have its containing type. */
26979 if (!origin)
26980 origin = decl_class_context (decl);
26981 if (origin != NULL_TREE)
26982 gen_type_die (origin, context_die);
26984 /* And its return type. */
26985 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26987 /* And its virtual context. */
26988 if (DECL_VINDEX (decl) != NULL_TREE)
26989 gen_type_die (DECL_CONTEXT (decl), context_die);
26991 /* Make sure we have a member DIE for decl. */
26992 if (origin != NULL_TREE)
26993 gen_type_die_for_member (origin, decl, context_die);
26995 /* And its containing namespace. */
26996 context_die = declare_in_namespace (decl, context_die);
26999 /* Now output a DIE to represent the function itself. */
27000 if (decl)
27001 gen_subprogram_die (decl, context_die);
27002 break;
27004 case TYPE_DECL:
27005 /* If we are in terse mode, don't generate any DIEs to represent any
27006 actual typedefs. */
27007 if (debug_info_level <= DINFO_LEVEL_TERSE)
27008 break;
27010 /* In the special case of a TYPE_DECL node representing the declaration
27011 of some type tag, if the given TYPE_DECL is marked as having been
27012 instantiated from some other (original) TYPE_DECL node (e.g. one which
27013 was generated within the original definition of an inline function) we
27014 used to generate a special (abbreviated) DW_TAG_structure_type,
27015 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
27016 should be actually referencing those DIEs, as variable DIEs with that
27017 type would be emitted already in the abstract origin, so it was always
27018 removed during unused type prunning. Don't add anything in this
27019 case. */
27020 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
27021 break;
27023 if (is_redundant_typedef (decl))
27024 gen_type_die (TREE_TYPE (decl), context_die);
27025 else
27026 /* Output a DIE to represent the typedef itself. */
27027 gen_typedef_die (decl, context_die);
27028 break;
27030 case LABEL_DECL:
27031 if (debug_info_level >= DINFO_LEVEL_NORMAL)
27032 gen_label_die (decl, context_die);
27033 break;
27035 case VAR_DECL:
27036 case RESULT_DECL:
27037 /* If we are in terse mode, don't generate any DIEs to represent any
27038 variable declarations or definitions unless it is external. */
27039 if (debug_info_level < DINFO_LEVEL_TERSE
27040 || (debug_info_level == DINFO_LEVEL_TERSE
27041 && !TREE_PUBLIC (decl_or_origin)))
27042 break;
27044 if (debug_info_level > DINFO_LEVEL_TERSE)
27046 /* Avoid generating stray type DIEs during late dwarf dumping.
27047 All types have been dumped early. */
27048 if (early_dwarf
27049 /* ??? But in LTRANS we cannot annotate early created variably
27050 modified type DIEs without copying them and adjusting all
27051 references to them. Dump them again as happens for inlining
27052 which copies both the decl and the types. */
27053 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27054 in VLA bound information for example. */
27055 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27056 current_function_decl)))
27058 /* Output any DIEs that are needed to specify the type of this data
27059 object. */
27060 if (decl_by_reference_p (decl_or_origin))
27061 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27062 else
27063 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27066 if (early_dwarf)
27068 /* And its containing type. */
27069 class_origin = decl_class_context (decl_or_origin);
27070 if (class_origin != NULL_TREE)
27071 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
27073 /* And its containing namespace. */
27074 context_die = declare_in_namespace (decl_or_origin, context_die);
27078 /* Now output the DIE to represent the data object itself. This gets
27079 complicated because of the possibility that the VAR_DECL really
27080 represents an inlined instance of a formal parameter for an inline
27081 function. */
27082 ultimate_origin = decl_ultimate_origin (decl_or_origin);
27083 if (ultimate_origin != NULL_TREE
27084 && TREE_CODE (ultimate_origin) == PARM_DECL)
27085 gen_formal_parameter_die (decl, origin,
27086 true /* Emit name attribute. */,
27087 context_die);
27088 else
27089 gen_variable_die (decl, origin, context_die);
27090 break;
27092 case FIELD_DECL:
27093 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
27094 /* Ignore the nameless fields that are used to skip bits but handle C++
27095 anonymous unions and structs. */
27096 if (DECL_NAME (decl) != NULL_TREE
27097 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
27098 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
27100 gen_type_die (member_declared_type (decl), context_die);
27101 gen_field_die (decl, ctx, context_die);
27103 break;
27105 case PARM_DECL:
27106 /* Avoid generating stray type DIEs during late dwarf dumping.
27107 All types have been dumped early. */
27108 if (early_dwarf
27109 /* ??? But in LTRANS we cannot annotate early created variably
27110 modified type DIEs without copying them and adjusting all
27111 references to them. Dump them again as happens for inlining
27112 which copies both the decl and the types. */
27113 /* ??? And even non-LTO needs to re-visit type DIEs to fill
27114 in VLA bound information for example. */
27115 || (decl && variably_modified_type_p (TREE_TYPE (decl),
27116 current_function_decl)))
27118 if (DECL_BY_REFERENCE (decl_or_origin))
27119 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
27120 else
27121 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
27123 return gen_formal_parameter_die (decl, origin,
27124 true /* Emit name attribute. */,
27125 context_die);
27127 case NAMESPACE_DECL:
27128 if (dwarf_version >= 3 || !dwarf_strict)
27129 gen_namespace_die (decl, context_die);
27130 break;
27132 case IMPORTED_DECL:
27133 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
27134 DECL_CONTEXT (decl), context_die);
27135 break;
27137 case NAMELIST_DECL:
27138 gen_namelist_decl (DECL_NAME (decl), context_die,
27139 NAMELIST_DECL_ASSOCIATED_DECL (decl));
27140 break;
27142 default:
27143 /* Probably some frontend-internal decl. Assume we don't care. */
27144 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
27145 break;
27148 return NULL;
27151 /* Output initial debug information for global DECL. Called at the
27152 end of the parsing process.
27154 This is the initial debug generation process. As such, the DIEs
27155 generated may be incomplete. A later debug generation pass
27156 (dwarf2out_late_global_decl) will augment the information generated
27157 in this pass (e.g., with complete location info). */
27159 static void
27160 dwarf2out_early_global_decl (tree decl)
27162 set_early_dwarf s;
27164 /* gen_decl_die() will set DECL_ABSTRACT because
27165 cgraph_function_possibly_inlined_p() returns true. This is in
27166 turn will cause DW_AT_inline attributes to be set.
27168 This happens because at early dwarf generation, there is no
27169 cgraph information, causing cgraph_function_possibly_inlined_p()
27170 to return true. Trick cgraph_function_possibly_inlined_p()
27171 while we generate dwarf early. */
27172 bool save = symtab->global_info_ready;
27173 symtab->global_info_ready = true;
27175 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27176 other DECLs and they can point to template types or other things
27177 that dwarf2out can't handle when done via dwarf2out_decl. */
27178 if (TREE_CODE (decl) != TYPE_DECL
27179 && TREE_CODE (decl) != PARM_DECL)
27181 if (TREE_CODE (decl) == FUNCTION_DECL)
27183 tree save_fndecl = current_function_decl;
27185 /* For nested functions, make sure we have DIEs for the parents first
27186 so that all nested DIEs are generated at the proper scope in the
27187 first shot. */
27188 tree context = decl_function_context (decl);
27189 if (context != NULL)
27191 dw_die_ref context_die = lookup_decl_die (context);
27192 current_function_decl = context;
27194 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27195 enough so that it lands in its own context. This avoids type
27196 pruning issues later on. */
27197 if (context_die == NULL || is_declaration_die (context_die))
27198 dwarf2out_early_global_decl (context);
27201 /* Emit an abstract origin of a function first. This happens
27202 with C++ constructor clones for example and makes
27203 dwarf2out_abstract_function happy which requires the early
27204 DIE of the abstract instance to be present. */
27205 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27206 dw_die_ref origin_die;
27207 if (origin != NULL
27208 /* Do not emit the DIE multiple times but make sure to
27209 process it fully here in case we just saw a declaration. */
27210 && ((origin_die = lookup_decl_die (origin)) == NULL
27211 || is_declaration_die (origin_die)))
27213 current_function_decl = origin;
27214 dwarf2out_decl (origin);
27217 /* Emit the DIE for decl but avoid doing that multiple times. */
27218 dw_die_ref old_die;
27219 if ((old_die = lookup_decl_die (decl)) == NULL
27220 || is_declaration_die (old_die))
27222 current_function_decl = decl;
27223 dwarf2out_decl (decl);
27226 current_function_decl = save_fndecl;
27228 else
27229 dwarf2out_decl (decl);
27231 symtab->global_info_ready = save;
27234 /* Return whether EXPR is an expression with the following pattern:
27235 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27237 static bool
27238 is_trivial_indirect_ref (tree expr)
27240 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27241 return false;
27243 tree nop = TREE_OPERAND (expr, 0);
27244 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27245 return false;
27247 tree int_cst = TREE_OPERAND (nop, 0);
27248 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27251 /* Output debug information for global decl DECL. Called from
27252 toplev.cc after compilation proper has finished. */
27254 static void
27255 dwarf2out_late_global_decl (tree decl)
27257 /* Fill-in any location information we were unable to determine
27258 on the first pass. */
27259 if (VAR_P (decl))
27261 dw_die_ref die = lookup_decl_die (decl);
27263 /* We may have to generate full debug late for LTO in case debug
27264 was not enabled at compile-time or the target doesn't support
27265 the LTO early debug scheme. */
27266 if (! die && in_lto_p)
27267 dwarf2out_decl (decl);
27268 else if (die)
27270 /* We get called via the symtab code invoking late_global_decl
27271 for symbols that are optimized out.
27273 Do not add locations for those, except if they have a
27274 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27275 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27276 INDIRECT_REF expression, as this could generate relocations to
27277 text symbols in LTO object files, which is invalid. */
27278 varpool_node *node = varpool_node::get (decl);
27279 if ((! node || ! node->definition)
27280 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27281 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27282 tree_add_const_value_attribute_for_decl (die, decl);
27283 else
27284 add_location_or_const_value_attribute (die, decl, false);
27289 /* Output debug information for type decl DECL. Called from toplev.cc
27290 and from language front ends (to record built-in types). */
27291 static void
27292 dwarf2out_type_decl (tree decl, int local)
27294 if (!local)
27296 set_early_dwarf s;
27297 dwarf2out_decl (decl);
27301 /* Output debug information for imported module or decl DECL.
27302 NAME is non-NULL name in the lexical block if the decl has been renamed.
27303 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27304 that DECL belongs to.
27305 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27306 static void
27307 dwarf2out_imported_module_or_decl_1 (tree decl,
27308 tree name,
27309 tree lexical_block,
27310 dw_die_ref lexical_block_die)
27312 expanded_location xloc;
27313 dw_die_ref imported_die = NULL;
27314 dw_die_ref at_import_die;
27316 if (TREE_CODE (decl) == IMPORTED_DECL)
27318 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27319 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27320 gcc_assert (decl);
27322 else
27323 xloc = expand_location (input_location);
27325 if (TREE_CODE (decl) == TYPE_DECL)
27327 at_import_die = force_type_die (TREE_TYPE (decl));
27328 /* For namespace N { typedef void T; } using N::T; base_type_die
27329 returns NULL, but DW_TAG_imported_declaration requires
27330 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27331 if (!at_import_die)
27333 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27334 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27335 at_import_die = lookup_type_die (TREE_TYPE (decl));
27336 gcc_assert (at_import_die);
27339 else
27341 at_import_die = lookup_decl_die (decl);
27342 if (!at_import_die)
27344 /* If we're trying to avoid duplicate debug info, we may not have
27345 emitted the member decl for this field. Emit it now. */
27346 if (TREE_CODE (decl) == FIELD_DECL)
27348 tree type = DECL_CONTEXT (decl);
27350 if (TYPE_CONTEXT (type)
27351 && TYPE_P (TYPE_CONTEXT (type))
27352 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27353 DINFO_USAGE_DIR_USE))
27354 return;
27355 gen_type_die_for_member (type, decl,
27356 get_context_die (TYPE_CONTEXT (type)));
27358 if (TREE_CODE (decl) == CONST_DECL)
27360 /* Individual enumerators of an enum type do not get output here
27361 (see gen_decl_die), so we cannot call force_decl_die. */
27362 if (!is_fortran () && !is_ada () && !is_dlang ())
27363 return;
27365 if (TREE_CODE (decl) == NAMELIST_DECL)
27366 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27367 get_context_die (DECL_CONTEXT (decl)),
27368 NULL_TREE);
27369 else
27370 at_import_die = force_decl_die (decl);
27374 if (TREE_CODE (decl) == NAMESPACE_DECL)
27376 if (dwarf_version >= 3 || !dwarf_strict)
27377 imported_die = new_die (DW_TAG_imported_module,
27378 lexical_block_die,
27379 lexical_block);
27380 else
27381 return;
27383 else
27384 imported_die = new_die (DW_TAG_imported_declaration,
27385 lexical_block_die,
27386 lexical_block);
27388 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27389 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27390 if (debug_column_info && xloc.column)
27391 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27392 if (name)
27393 add_AT_string (imported_die, DW_AT_name,
27394 IDENTIFIER_POINTER (name));
27395 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27398 /* Output debug information for imported module or decl DECL.
27399 NAME is non-NULL name in context if the decl has been renamed.
27400 CHILD is true if decl is one of the renamed decls as part of
27401 importing whole module.
27402 IMPLICIT is set if this hook is called for an implicit import
27403 such as inline namespace. */
27405 static void
27406 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27407 bool child, bool implicit)
27409 /* dw_die_ref at_import_die; */
27410 dw_die_ref scope_die;
27412 if (debug_info_level <= DINFO_LEVEL_TERSE)
27413 return;
27415 gcc_assert (decl);
27417 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27418 should be enough, for DWARF4 and older even if we emit as extension
27419 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27420 for the benefit of consumers unaware of DW_AT_export_symbols. */
27421 if (implicit
27422 && dwarf_version >= 5
27423 && lang_hooks.decls.decl_dwarf_attribute (decl,
27424 DW_AT_export_symbols) == 1)
27425 return;
27427 set_early_dwarf s;
27429 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27430 We need decl DIE for reference and scope die. First, get DIE for the decl
27431 itself. */
27433 /* Get the scope die for decl context. Use comp_unit_die for global module
27434 or decl. If die is not found for non globals, force new die. */
27435 if (context
27436 && TYPE_P (context)
27437 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27438 return;
27440 scope_die = get_context_die (context);
27442 if (child)
27444 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27445 there is nothing we can do, here. */
27446 if (dwarf_version < 3 && dwarf_strict)
27447 return;
27449 gcc_assert (scope_die->die_child);
27450 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27451 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27452 scope_die = scope_die->die_child;
27455 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27456 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27459 /* Output debug information for namelists. */
27461 static dw_die_ref
27462 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27464 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27465 tree value;
27466 unsigned i;
27468 if (debug_info_level <= DINFO_LEVEL_TERSE)
27469 return NULL;
27471 gcc_assert (scope_die != NULL);
27472 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27473 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27475 /* If there are no item_decls, we have a nondefining namelist, e.g.
27476 with USE association; hence, set DW_AT_declaration. */
27477 if (item_decls == NULL_TREE)
27479 add_AT_flag (nml_die, DW_AT_declaration, 1);
27480 return nml_die;
27483 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27485 nml_item_ref_die = lookup_decl_die (value);
27486 if (!nml_item_ref_die)
27487 nml_item_ref_die = force_decl_die (value);
27489 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27490 add_AT_die_ref (nml_item_die, DW_AT_namelist_item, nml_item_ref_die);
27492 return nml_die;
27496 /* Write the debugging output for DECL and return the DIE. */
27498 static void
27499 dwarf2out_decl (tree decl)
27501 dw_die_ref context_die = comp_unit_die ();
27503 switch (TREE_CODE (decl))
27505 case ERROR_MARK:
27506 return;
27508 case FUNCTION_DECL:
27509 /* If we're a nested function, initially use a parent of NULL; if we're
27510 a plain function, this will be fixed up in decls_for_scope. If
27511 we're a method, it will be ignored, since we already have a DIE.
27512 Avoid doing this late though since clones of class methods may
27513 otherwise end up in limbo and create type DIEs late. */
27514 if (early_dwarf
27515 && decl_function_context (decl)
27516 /* But if we're in terse mode, we don't care about scope. */
27517 && debug_info_level > DINFO_LEVEL_TERSE)
27518 context_die = NULL;
27519 break;
27521 case VAR_DECL:
27522 /* For local statics lookup proper context die. */
27523 if (local_function_static (decl))
27524 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27526 /* If we are in terse mode, don't generate any DIEs to represent any
27527 variable declarations or definitions unless it is external. */
27528 if (debug_info_level < DINFO_LEVEL_TERSE
27529 || (debug_info_level == DINFO_LEVEL_TERSE
27530 && !TREE_PUBLIC (decl)))
27531 return;
27532 break;
27534 case CONST_DECL:
27535 if (debug_info_level <= DINFO_LEVEL_TERSE)
27536 return;
27537 if (!is_fortran () && !is_ada () && !is_dlang ())
27538 return;
27539 if (TREE_STATIC (decl) && decl_function_context (decl))
27540 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27541 break;
27543 case NAMESPACE_DECL:
27544 case IMPORTED_DECL:
27545 if (debug_info_level <= DINFO_LEVEL_TERSE)
27546 return;
27547 if (lookup_decl_die (decl) != NULL)
27548 return;
27549 break;
27551 case TYPE_DECL:
27552 /* Don't emit stubs for types unless they are needed by other DIEs. */
27553 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27554 return;
27556 /* Don't bother trying to generate any DIEs to represent any of the
27557 normal built-in types for the language we are compiling. */
27558 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27559 return;
27561 /* If we are in terse mode, don't generate any DIEs for types. */
27562 if (debug_info_level <= DINFO_LEVEL_TERSE)
27563 return;
27565 /* If we're a function-scope tag, initially use a parent of NULL;
27566 this will be fixed up in decls_for_scope. */
27567 if (decl_function_context (decl))
27568 context_die = NULL;
27570 break;
27572 case NAMELIST_DECL:
27573 break;
27575 default:
27576 return;
27579 gen_decl_die (decl, NULL, NULL, context_die);
27581 if (flag_checking)
27583 dw_die_ref die = lookup_decl_die (decl);
27584 if (die)
27585 check_die (die);
27589 /* Write the debugging output for DECL. */
27591 static void
27592 dwarf2out_function_decl (tree decl)
27594 dwarf2out_decl (decl);
27595 call_arg_locations = NULL;
27596 call_arg_loc_last = NULL;
27597 call_site_count = -1;
27598 tail_call_site_count = -1;
27599 decl_loc_table->empty ();
27600 cached_dw_loc_list_table->empty ();
27603 /* Output a marker (i.e. a label) for the beginning of the generated code for
27604 a lexical block. */
27606 static void
27607 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27608 unsigned int blocknum)
27610 switch_to_section (current_function_section ());
27611 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27614 /* Output a marker (i.e. a label) for the end of the generated code for a
27615 lexical block. */
27617 static void
27618 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27620 switch_to_section (current_function_section ());
27621 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27624 /* Returns nonzero if it is appropriate not to emit any debugging
27625 information for BLOCK, because it doesn't contain any instructions.
27627 Don't allow this for blocks with nested functions or local classes
27628 as we would end up with orphans, and in the presence of scheduling
27629 we may end up calling them anyway. */
27631 static bool
27632 dwarf2out_ignore_block (const_tree block)
27634 tree decl;
27635 unsigned int i;
27637 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27638 if (TREE_CODE (decl) == FUNCTION_DECL
27639 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27640 return 0;
27641 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27643 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27644 if (TREE_CODE (decl) == FUNCTION_DECL
27645 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27646 return 0;
27649 return 1;
27652 /* Hash table routines for file_hash. */
27654 bool
27655 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27657 return filename_cmp (p1->key, p2) == 0;
27660 hashval_t
27661 dwarf_file_hasher::hash (dwarf_file_data *p)
27663 return htab_hash_string (p->key);
27666 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27667 dwarf2out.cc) and return its "index". The index of each (known) filename is
27668 just a unique number which is associated with only that one filename. We
27669 need such numbers for the sake of generating labels (in the .debug_sfnames
27670 section) and references to those files numbers (in the .debug_srcinfo
27671 and .debug_macinfo sections). If the filename given as an argument is not
27672 found in our current list, add it to the list and assign it the next
27673 available unique index number. */
27675 static struct dwarf_file_data *
27676 lookup_filename (const char *file_name)
27678 struct dwarf_file_data * created;
27680 if (!file_name)
27681 return NULL;
27683 if (!file_name[0])
27684 file_name = "<stdin>";
27686 dwarf_file_data **slot
27687 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27688 INSERT);
27689 if (*slot)
27690 return *slot;
27692 created = ggc_alloc<dwarf_file_data> ();
27693 created->key = file_name;
27694 created->filename = remap_debug_filename (file_name);
27695 created->emitted_number = 0;
27696 *slot = created;
27697 return created;
27700 /* If the assembler will construct the file table, then translate the compiler
27701 internal file table number into the assembler file table number, and emit
27702 a .file directive if we haven't already emitted one yet. The file table
27703 numbers are different because we prune debug info for unused variables and
27704 types, which may include filenames. */
27706 static int
27707 maybe_emit_file (struct dwarf_file_data * fd)
27709 if (! fd->emitted_number)
27711 if (last_emitted_file)
27712 fd->emitted_number = last_emitted_file->emitted_number + 1;
27713 else
27714 fd->emitted_number = 1;
27715 last_emitted_file = fd;
27717 if (output_asm_line_debug_info ())
27719 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27720 output_quoted_string (asm_out_file, fd->filename);
27721 fputc ('\n', asm_out_file);
27725 return fd->emitted_number;
27728 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27729 That generation should happen after function debug info has been
27730 generated. The value of the attribute is the constant value of ARG. */
27732 static void
27733 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27735 die_arg_entry entry;
27737 if (!die || !arg)
27738 return;
27740 gcc_assert (early_dwarf);
27742 if (!tmpl_value_parm_die_table)
27743 vec_alloc (tmpl_value_parm_die_table, 32);
27745 entry.die = die;
27746 entry.arg = arg;
27747 vec_safe_push (tmpl_value_parm_die_table, entry);
27750 /* Return TRUE if T is an instance of generic type, FALSE
27751 otherwise. */
27753 static bool
27754 generic_type_p (tree t)
27756 if (t == NULL_TREE || !TYPE_P (t))
27757 return false;
27758 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27761 /* Schedule the generation of the generic parameter dies for the
27762 instance of generic type T. The proper generation itself is later
27763 done by gen_scheduled_generic_parms_dies. */
27765 static void
27766 schedule_generic_params_dies_gen (tree t)
27768 if (!generic_type_p (t))
27769 return;
27771 gcc_assert (early_dwarf);
27773 if (!generic_type_instances)
27774 vec_alloc (generic_type_instances, 256);
27776 vec_safe_push (generic_type_instances, t);
27779 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27780 by append_entry_to_tmpl_value_parm_die_table. This function must
27781 be called after function DIEs have been generated. */
27783 static void
27784 gen_remaining_tmpl_value_param_die_attribute (void)
27786 if (tmpl_value_parm_die_table)
27788 unsigned i, j;
27789 die_arg_entry *e;
27791 /* We do this in two phases - first get the cases we can
27792 handle during early-finish, preserving those we cannot
27793 (containing symbolic constants where we don't yet know
27794 whether we are going to output the referenced symbols).
27795 For those we try again at late-finish. */
27796 j = 0;
27797 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27799 if (!e->die->removed
27800 && !tree_add_const_value_attribute (e->die, e->arg))
27802 dw_loc_descr_ref loc = NULL;
27803 if (! early_dwarf
27804 && (dwarf_version >= 5 || !dwarf_strict))
27805 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27806 if (loc)
27807 add_AT_loc (e->die, DW_AT_location, loc);
27808 else
27809 (*tmpl_value_parm_die_table)[j++] = *e;
27812 tmpl_value_parm_die_table->truncate (j);
27816 /* Generate generic parameters DIEs for instances of generic types
27817 that have been previously scheduled by
27818 schedule_generic_params_dies_gen. This function must be called
27819 after all the types of the CU have been laid out. */
27821 static void
27822 gen_scheduled_generic_parms_dies (void)
27824 unsigned i;
27825 tree t;
27827 if (!generic_type_instances)
27828 return;
27830 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27831 if (COMPLETE_TYPE_P (t))
27832 gen_generic_params_dies (t);
27834 generic_type_instances = NULL;
27838 /* Replace DW_AT_name for the decl with name. */
27840 static void
27841 dwarf2out_set_name (tree decl, tree name)
27843 dw_die_ref die;
27844 dw_attr_node *attr;
27845 const char *dname;
27847 die = TYPE_SYMTAB_DIE (decl);
27848 if (!die)
27849 return;
27851 dname = dwarf2_name (name, 0);
27852 if (!dname)
27853 return;
27855 attr = get_AT (die, DW_AT_name);
27856 if (attr)
27858 struct indirect_string_node *node;
27860 node = find_AT_string (dname);
27861 /* replace the string. */
27862 attr->dw_attr_val.v.val_str = node;
27865 else
27866 add_name_attribute (die, dname);
27869 /* True if before or during processing of the first function being emitted. */
27870 static bool in_first_function_p = true;
27871 /* True if loc_note during dwarf2out_var_location call might still be
27872 before first real instruction at address equal to .Ltext0. */
27873 static bool maybe_at_text_label_p = true;
27874 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27875 static unsigned int first_loclabel_num_not_at_text_label;
27877 /* Look ahead for a real insn. */
27879 static rtx_insn *
27880 dwarf2out_next_real_insn (rtx_insn *loc_note)
27882 rtx_insn *next_real = NEXT_INSN (loc_note);
27884 while (next_real)
27885 if (INSN_P (next_real))
27886 break;
27887 else
27888 next_real = NEXT_INSN (next_real);
27890 return next_real;
27893 /* Called by the final INSN scan whenever we see a var location. We
27894 use it to drop labels in the right places, and throw the location in
27895 our lookup table. */
27897 static void
27898 dwarf2out_var_location (rtx_insn *loc_note)
27900 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27901 struct var_loc_node *newloc;
27902 rtx_insn *next_real;
27903 rtx_insn *call_insn = NULL;
27904 static const char *last_label;
27905 static const char *last_postcall_label;
27906 static bool last_in_cold_section_p;
27907 static rtx_insn *expected_next_loc_note;
27908 tree decl;
27909 bool var_loc_p;
27910 var_loc_view view = 0;
27912 if (!NOTE_P (loc_note))
27914 if (CALL_P (loc_note))
27916 maybe_reset_location_view (loc_note, cur_line_info_table);
27917 call_site_count++;
27918 if (SIBLING_CALL_P (loc_note))
27919 tail_call_site_count++;
27920 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27922 call_insn = loc_note;
27923 loc_note = NULL;
27924 var_loc_p = false;
27926 next_real = dwarf2out_next_real_insn (call_insn);
27927 cached_next_real_insn = NULL;
27928 goto create_label;
27930 if (optimize == 0 && !flag_var_tracking)
27932 /* When the var-tracking pass is not running, there is no note
27933 for indirect calls whose target is compile-time known. In this
27934 case, process such calls specifically so that we generate call
27935 sites for them anyway. */
27936 rtx x = PATTERN (loc_note);
27937 if (GET_CODE (x) == PARALLEL)
27938 x = XVECEXP (x, 0, 0);
27939 if (GET_CODE (x) == SET)
27940 x = SET_SRC (x);
27941 if (GET_CODE (x) == CALL)
27942 x = XEXP (x, 0);
27943 if (!MEM_P (x)
27944 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27945 || !SYMBOL_REF_DECL (XEXP (x, 0))
27946 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27947 != FUNCTION_DECL))
27949 call_insn = loc_note;
27950 loc_note = NULL;
27951 var_loc_p = false;
27953 next_real = dwarf2out_next_real_insn (call_insn);
27954 cached_next_real_insn = NULL;
27955 goto create_label;
27959 else if (!debug_variable_location_views)
27960 gcc_unreachable ();
27961 else
27962 maybe_reset_location_view (loc_note, cur_line_info_table);
27964 return;
27967 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27968 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27969 return;
27971 /* Optimize processing a large consecutive sequence of location
27972 notes so we don't spend too much time in next_real_insn. If the
27973 next insn is another location note, remember the next_real_insn
27974 calculation for next time. */
27975 next_real = cached_next_real_insn;
27976 if (next_real)
27978 if (expected_next_loc_note != loc_note)
27979 next_real = NULL;
27982 if (! next_real)
27983 next_real = dwarf2out_next_real_insn (loc_note);
27985 if (next_real)
27987 rtx_insn *next_note = NEXT_INSN (loc_note);
27988 while (next_note != next_real)
27990 if (! next_note->deleted ()
27991 && NOTE_P (next_note)
27992 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27993 break;
27994 next_note = NEXT_INSN (next_note);
27997 if (next_note == next_real)
27998 cached_next_real_insn = NULL;
27999 else
28001 expected_next_loc_note = next_note;
28002 cached_next_real_insn = next_real;
28005 else
28006 cached_next_real_insn = NULL;
28008 /* If there are no instructions which would be affected by this note,
28009 don't do anything. */
28010 if (var_loc_p
28011 && next_real == NULL_RTX
28012 && !NOTE_DURING_CALL_P (loc_note))
28013 return;
28015 create_label:
28017 if (next_real == NULL_RTX)
28018 next_real = get_last_insn ();
28020 /* If there were any real insns between note we processed last time
28021 and this note (or if it is the first note), clear
28022 last_{,postcall_}label so that they are not reused this time. */
28023 if (last_var_location_insn == NULL_RTX
28024 || last_var_location_insn != next_real
28025 || last_in_cold_section_p != in_cold_section_p)
28027 last_label = NULL;
28028 last_postcall_label = NULL;
28031 if (var_loc_p)
28033 const char *label
28034 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
28035 view = cur_line_info_table->view;
28036 decl = NOTE_VAR_LOCATION_DECL (loc_note);
28037 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
28038 if (newloc == NULL)
28039 return;
28041 else
28043 decl = NULL_TREE;
28044 newloc = NULL;
28047 /* If there were no real insns between note we processed last time
28048 and this note, use the label we emitted last time. Otherwise
28049 create a new label and emit it. */
28050 if (last_label == NULL)
28052 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
28053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
28054 loclabel_num++;
28055 last_label = ggc_strdup (loclabel);
28056 /* See if loclabel might be equal to .Ltext0. If yes,
28057 bump first_loclabel_num_not_at_text_label. */
28058 if (!have_multiple_function_sections
28059 && in_first_function_p
28060 && maybe_at_text_label_p)
28062 static rtx_insn *last_start;
28063 rtx_insn *insn;
28064 for (insn = loc_note; insn; insn = previous_insn (insn))
28065 if (insn == last_start)
28066 break;
28067 else if (!NONDEBUG_INSN_P (insn))
28068 continue;
28069 else
28071 rtx body = PATTERN (insn);
28072 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
28073 continue;
28074 /* Inline asm could occupy zero bytes. */
28075 else if (GET_CODE (body) == ASM_INPUT
28076 || asm_noperands (body) >= 0)
28077 continue;
28078 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
28079 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
28080 continue;
28081 #endif
28082 else
28084 /* Assume insn has non-zero length. */
28085 maybe_at_text_label_p = false;
28086 break;
28089 if (maybe_at_text_label_p)
28091 last_start = loc_note;
28092 first_loclabel_num_not_at_text_label = loclabel_num;
28097 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
28098 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
28100 if (!var_loc_p)
28102 struct call_arg_loc_node *ca_loc
28103 = ggc_cleared_alloc<call_arg_loc_node> ();
28104 rtx_insn *prev = call_insn;
28106 ca_loc->call_arg_loc_note
28107 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
28108 ca_loc->next = NULL;
28109 ca_loc->label = last_label;
28110 gcc_assert (prev
28111 && (CALL_P (prev)
28112 || (NONJUMP_INSN_P (prev)
28113 && GET_CODE (PATTERN (prev)) == SEQUENCE
28114 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
28115 if (!CALL_P (prev))
28116 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
28117 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
28119 /* Look for a SYMBOL_REF in the "prev" instruction. */
28120 rtx x = get_call_rtx_from (prev);
28121 if (x)
28123 /* Try to get the call symbol, if any. */
28124 if (MEM_P (XEXP (x, 0)))
28125 x = XEXP (x, 0);
28126 /* First, look for a memory access to a symbol_ref. */
28127 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
28128 && SYMBOL_REF_DECL (XEXP (x, 0))
28129 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
28130 ca_loc->symbol_ref = XEXP (x, 0);
28131 /* Otherwise, look at a compile-time known user-level function
28132 declaration. */
28133 else if (MEM_P (x)
28134 && MEM_EXPR (x)
28135 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
28136 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
28139 ca_loc->block = insn_scope (prev);
28140 if (call_arg_locations)
28141 call_arg_loc_last->next = ca_loc;
28142 else
28143 call_arg_locations = ca_loc;
28144 call_arg_loc_last = ca_loc;
28146 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
28148 newloc->label = last_label;
28149 newloc->view = view;
28151 else
28153 if (!last_postcall_label)
28155 sprintf (loclabel, "%s-1", last_label);
28156 last_postcall_label = ggc_strdup (loclabel);
28158 newloc->label = last_postcall_label;
28159 /* ??? This view is at last_label, not last_label-1, but we
28160 could only assume view at last_label-1 is zero if we could
28161 assume calls always have length greater than one. This is
28162 probably true in general, though there might be a rare
28163 exception to this rule, e.g. if a call insn is optimized out
28164 by target magic. Then, even the -1 in the label will be
28165 wrong, which might invalidate the range. Anyway, using view,
28166 though technically possibly incorrect, will work as far as
28167 ranges go: since L-1 is in the middle of the call insn,
28168 (L-1).0 and (L-1).V shouldn't make any difference, and having
28169 the loclist entry refer to the .loc entry might be useful, so
28170 leave it like this. */
28171 newloc->view = view;
28174 if (var_loc_p && flag_debug_asm)
28176 const char *name, *sep, *patstr;
28177 if (decl && DECL_NAME (decl))
28178 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28179 else
28180 name = "";
28181 if (NOTE_VAR_LOCATION_LOC (loc_note))
28183 sep = " => ";
28184 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28186 else
28188 sep = " ";
28189 patstr = "RESET";
28191 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28192 name, sep, patstr);
28195 last_var_location_insn = next_real;
28196 last_in_cold_section_p = in_cold_section_p;
28199 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28200 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28201 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28202 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28203 BLOCK_FRAGMENT_ORIGIN links. */
28204 static bool
28205 block_within_block_p (tree block, tree outer, bool bothways)
28207 if (block == outer)
28208 return true;
28210 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28211 for (tree context = BLOCK_SUPERCONTEXT (block);
28212 context != outer;
28213 context = BLOCK_SUPERCONTEXT (context))
28214 if (!context || TREE_CODE (context) != BLOCK)
28215 return false;
28217 if (!bothways)
28218 return true;
28220 /* Now check that each block is actually referenced by its
28221 parent. */
28222 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28223 context = BLOCK_SUPERCONTEXT (context))
28225 if (BLOCK_FRAGMENT_ORIGIN (context))
28227 gcc_assert (!BLOCK_SUBBLOCKS (context));
28228 context = BLOCK_FRAGMENT_ORIGIN (context);
28230 for (tree sub = BLOCK_SUBBLOCKS (context);
28231 sub != block;
28232 sub = BLOCK_CHAIN (sub))
28233 if (!sub)
28234 return false;
28235 if (context == outer)
28236 return true;
28237 else
28238 block = context;
28242 /* Called during final while assembling the marker of the entry point
28243 for an inlined function. */
28245 static void
28246 dwarf2out_inline_entry (tree block)
28248 gcc_assert (debug_inline_points);
28250 /* If we can't represent it, don't bother. */
28251 if (!(dwarf_version >= 3 || !dwarf_strict))
28252 return;
28254 gcc_assert (DECL_P (block_ultimate_origin (block)));
28256 /* Sanity check the block tree. This would catch a case in which
28257 BLOCK got removed from the tree reachable from the outermost
28258 lexical block, but got retained in markers. It would still link
28259 back to its parents, but some ancestor would be missing a link
28260 down the path to the sub BLOCK. If the block got removed, its
28261 BLOCK_NUMBER will not be a usable value. */
28262 if (flag_checking)
28263 gcc_assert (block_within_block_p (block,
28264 DECL_INITIAL (current_function_decl),
28265 true));
28267 gcc_assert (inlined_function_outer_scope_p (block));
28268 gcc_assert (!lookup_block_die (block));
28270 if (BLOCK_FRAGMENT_ORIGIN (block))
28271 block = BLOCK_FRAGMENT_ORIGIN (block);
28272 /* Can the entry point ever not be at the beginning of an
28273 unfragmented lexical block? */
28274 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28275 || (cur_line_info_table
28276 && !ZERO_VIEW_P (cur_line_info_table->view))))
28277 return;
28279 if (!inline_entry_data_table)
28280 inline_entry_data_table
28281 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28284 inline_entry_data **iedp
28285 = inline_entry_data_table->find_slot_with_hash (block,
28286 htab_hash_pointer (block),
28287 INSERT);
28288 if (*iedp)
28289 /* ??? Ideally, we'd record all entry points for the same inlined
28290 function (some may have been duplicated by e.g. unrolling), but
28291 we have no way to represent that ATM. */
28292 return;
28294 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28295 ied->block = block;
28296 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28297 ied->label_num = BLOCK_NUMBER (block);
28298 if (cur_line_info_table)
28299 ied->view = cur_line_info_table->view;
28301 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28302 BLOCK_NUMBER (block));
28305 /* Called from finalize_size_functions for size functions so that their body
28306 can be encoded in the debug info to describe the layout of variable-length
28307 structures. */
28309 static void
28310 dwarf2out_size_function (tree decl)
28312 set_early_dwarf s;
28313 function_to_dwarf_procedure (decl);
28316 /* Note in one location list that text section has changed. */
28319 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28321 var_loc_list *list = *slot;
28322 if (list->first)
28323 list->last_before_switch
28324 = list->last->next ? list->last->next : list->last;
28325 return 1;
28328 /* Note in all location lists that text section has changed. */
28330 static void
28331 var_location_switch_text_section (void)
28333 if (decl_loc_table == NULL)
28334 return;
28336 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28339 /* Create a new line number table. */
28341 static dw_line_info_table *
28342 new_line_info_table (void)
28344 dw_line_info_table *table;
28346 table = ggc_cleared_alloc<dw_line_info_table> ();
28347 table->file_num = 1;
28348 table->line_num = 1;
28349 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28350 FORCE_RESET_NEXT_VIEW (table->view);
28351 table->symviews_since_reset = 0;
28353 return table;
28356 /* Lookup the "current" table into which we emit line info, so
28357 that we don't have to do it for every source line. */
28359 static void
28360 set_cur_line_info_table (section *sec)
28362 dw_line_info_table *table;
28364 if (sec == text_section)
28365 table = text_section_line_info;
28366 else if (sec == cold_text_section)
28368 table = cold_text_section_line_info;
28369 if (!table)
28371 cold_text_section_line_info = table = new_line_info_table ();
28372 table->end_label = cold_end_label;
28375 else
28377 const char *end_label;
28379 if (crtl->has_bb_partition)
28381 if (in_cold_section_p)
28382 end_label = crtl->subsections.cold_section_end_label;
28383 else
28384 end_label = crtl->subsections.hot_section_end_label;
28386 else
28388 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28389 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28390 current_function_funcdef_no);
28391 end_label = ggc_strdup (label);
28394 table = new_line_info_table ();
28395 table->end_label = end_label;
28397 vec_safe_push (separate_line_info, table);
28400 if (output_asm_line_debug_info ())
28401 table->is_stmt = (cur_line_info_table
28402 ? cur_line_info_table->is_stmt
28403 : DWARF_LINE_DEFAULT_IS_STMT_START);
28404 cur_line_info_table = table;
28408 /* We need to reset the locations at the beginning of each
28409 function. We can't do this in the end_function hook, because the
28410 declarations that use the locations won't have been output when
28411 that hook is called. Also compute have_multiple_function_sections here. */
28413 static void
28414 dwarf2out_begin_function (tree fun)
28416 section *sec = function_section (fun);
28418 if (sec != text_section)
28419 have_multiple_function_sections = true;
28421 if (crtl->has_bb_partition && !cold_text_section)
28423 gcc_assert (current_function_decl == fun);
28424 cold_text_section = unlikely_text_section ();
28425 switch_to_section (cold_text_section);
28426 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28427 switch_to_section (sec);
28430 call_site_count = 0;
28431 tail_call_site_count = 0;
28433 set_cur_line_info_table (sec);
28434 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28437 /* Helper function of dwarf2out_end_function, called only after emitting
28438 the very first function into assembly. Check if some .debug_loc range
28439 might end with a .LVL* label that could be equal to .Ltext0.
28440 In that case we must force using absolute addresses in .debug_loc ranges,
28441 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28442 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28443 list terminator.
28444 Set have_multiple_function_sections to true in that case and
28445 terminate htab traversal. */
28448 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28450 var_loc_list *entry = *slot;
28451 struct var_loc_node *node;
28453 node = entry->first;
28454 if (node && node->next && node->next->label)
28456 unsigned int i;
28457 const char *label = node->next->label;
28458 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28460 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28462 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28463 if (strcmp (label, loclabel) == 0)
28465 have_multiple_function_sections = true;
28466 return 0;
28470 return 1;
28473 /* Hook called after emitting a function into assembly.
28474 This does something only for the very first function emitted. */
28476 static void
28477 dwarf2out_end_function (unsigned int)
28479 if (in_first_function_p
28480 && !have_multiple_function_sections
28481 && first_loclabel_num_not_at_text_label
28482 && decl_loc_table)
28483 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28484 in_first_function_p = false;
28485 maybe_at_text_label_p = false;
28488 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28489 front-ends register a translation unit even before dwarf2out_init is
28490 called. */
28491 static tree main_translation_unit = NULL_TREE;
28493 /* Hook called by front-ends after they built their main translation unit.
28494 Associate comp_unit_die to UNIT. */
28496 static void
28497 dwarf2out_register_main_translation_unit (tree unit)
28499 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28500 && main_translation_unit == NULL_TREE);
28501 main_translation_unit = unit;
28502 /* If dwarf2out_init has not been called yet, it will perform the association
28503 itself looking at main_translation_unit. */
28504 if (decl_die_table != NULL)
28505 equate_decl_number_to_die (unit, comp_unit_die ());
28508 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28510 static void
28511 push_dw_line_info_entry (dw_line_info_table *table,
28512 enum dw_line_info_opcode opcode, unsigned int val)
28514 dw_line_info_entry e;
28515 e.opcode = opcode;
28516 e.val = val;
28517 vec_safe_push (table->entries, e);
28520 /* Output a label to mark the beginning of a source code line entry
28521 and record information relating to this source line, in
28522 'line_info_table' for later output of the .debug_line section. */
28523 /* ??? The discriminator parameter ought to be unsigned. */
28525 static void
28526 dwarf2out_source_line (unsigned int line, unsigned int column,
28527 const char *filename,
28528 int discriminator, bool is_stmt)
28530 unsigned int file_num;
28531 dw_line_info_table *table;
28532 static var_loc_view lvugid;
28534 /* 'line_info_table' information gathering is not needed when the debug
28535 info level is set to the lowest value. Also, the current DWARF-based
28536 debug formats do not use this info. */
28537 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28538 return;
28540 table = cur_line_info_table;
28542 if (line == 0)
28544 if (debug_variable_location_views
28545 && output_asm_line_debug_info ()
28546 && table && !RESETTING_VIEW_P (table->view))
28548 /* If we're using the assembler to compute view numbers, we
28549 can't issue a .loc directive for line zero, so we can't
28550 get a view number at this point. We might attempt to
28551 compute it from the previous view, or equate it to a
28552 subsequent view (though it might not be there!), but
28553 since we're omitting the line number entry, we might as
28554 well omit the view number as well. That means pretending
28555 it's a view number zero, which might very well turn out
28556 to be correct. ??? Extend the assembler so that the
28557 compiler could emit e.g. ".locview .LVU#", to output a
28558 view without changing line number information. We'd then
28559 have to count it in symviews_since_reset; when it's omitted,
28560 it doesn't count. */
28561 if (!zero_view_p)
28562 zero_view_p = BITMAP_GGC_ALLOC ();
28563 bitmap_set_bit (zero_view_p, table->view);
28564 if (flag_debug_asm)
28566 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28567 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28568 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28569 ASM_COMMENT_START);
28570 assemble_name (asm_out_file, label);
28571 putc ('\n', asm_out_file);
28573 table->view = ++lvugid;
28575 return;
28578 /* The discriminator column was added in dwarf4. Simplify the below
28579 by simply removing it if we're not supposed to output it. */
28580 if (dwarf_version < 4 && dwarf_strict)
28581 discriminator = 0;
28583 if (!debug_column_info)
28584 column = 0;
28586 file_num = maybe_emit_file (lookup_filename (filename));
28588 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28589 the debugger has used the second (possibly duplicate) line number
28590 at the beginning of the function to mark the end of the prologue.
28591 We could eliminate any other duplicates within the function. For
28592 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28593 that second line number entry. */
28594 /* Recall that this end-of-prologue indication is *not* the same thing
28595 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28596 to which the hook corresponds, follows the last insn that was
28597 emitted by gen_prologue. What we need is to precede the first insn
28598 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28599 insn that corresponds to something the user wrote. These may be
28600 very different locations once scheduling is enabled. */
28602 if (0 && file_num == table->file_num
28603 && line == table->line_num
28604 && column == table->column_num
28605 && discriminator == table->discrim_num
28606 && is_stmt == table->is_stmt)
28607 return;
28609 switch_to_section (current_function_section ());
28611 /* If requested, emit something human-readable. */
28612 if (flag_debug_asm)
28614 if (debug_column_info)
28615 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28616 filename, line, column);
28617 else
28618 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28619 filename, line);
28622 if (output_asm_line_debug_info ())
28624 /* Emit the .loc directive understood by GNU as. */
28625 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28626 file_num, line, is_stmt, discriminator */
28627 fputs ("\t.loc ", asm_out_file);
28628 fprint_ul (asm_out_file, file_num);
28629 putc (' ', asm_out_file);
28630 fprint_ul (asm_out_file, line);
28631 putc (' ', asm_out_file);
28632 fprint_ul (asm_out_file, column);
28634 if (is_stmt != table->is_stmt)
28636 #if HAVE_GAS_LOC_STMT
28637 fputs (" is_stmt ", asm_out_file);
28638 putc (is_stmt ? '1' : '0', asm_out_file);
28639 #endif
28641 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28643 gcc_assert (discriminator > 0);
28644 fputs (" discriminator ", asm_out_file);
28645 fprint_ul (asm_out_file, (unsigned long) discriminator);
28647 if (debug_variable_location_views)
28649 if (!RESETTING_VIEW_P (table->view))
28651 table->symviews_since_reset++;
28652 if (table->symviews_since_reset > symview_upper_bound)
28653 symview_upper_bound = table->symviews_since_reset;
28654 /* When we're using the assembler to compute view
28655 numbers, we output symbolic labels after "view" in
28656 .loc directives, and the assembler will set them for
28657 us, so that we can refer to the view numbers in
28658 location lists. The only exceptions are when we know
28659 a view will be zero: "-0" is a forced reset, used
28660 e.g. in the beginning of functions, whereas "0" tells
28661 the assembler to check that there was a PC change
28662 since the previous view, in a way that implicitly
28663 resets the next view. */
28664 fputs (" view ", asm_out_file);
28665 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28666 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28667 assemble_name (asm_out_file, label);
28668 table->view = ++lvugid;
28670 else
28672 table->symviews_since_reset = 0;
28673 if (FORCE_RESETTING_VIEW_P (table->view))
28674 fputs (" view -0", asm_out_file);
28675 else
28676 fputs (" view 0", asm_out_file);
28677 /* Mark the present view as a zero view. Earlier debug
28678 binds may have already added its id to loclists to be
28679 emitted later, so we can't reuse the id for something
28680 else. However, it's good to know whether a view is
28681 known to be zero, because then we may be able to
28682 optimize out locviews that are all zeros, so take
28683 note of it in zero_view_p. */
28684 if (!zero_view_p)
28685 zero_view_p = BITMAP_GGC_ALLOC ();
28686 bitmap_set_bit (zero_view_p, lvugid);
28687 table->view = ++lvugid;
28690 putc ('\n', asm_out_file);
28692 else
28694 unsigned int label_num = ++line_info_label_num;
28696 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28698 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28699 push_dw_line_info_entry (table, LI_adv_address, label_num);
28700 else
28701 push_dw_line_info_entry (table, LI_set_address, label_num);
28702 if (debug_variable_location_views)
28704 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28705 if (resetting)
28706 table->view = 0;
28708 if (flag_debug_asm)
28709 fprintf (asm_out_file, "\t%s view %s%d\n",
28710 ASM_COMMENT_START,
28711 resetting ? "-" : "",
28712 table->view);
28714 table->view++;
28716 if (file_num != table->file_num)
28717 push_dw_line_info_entry (table, LI_set_file, file_num);
28718 if (discriminator != table->discrim_num)
28719 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28720 if (is_stmt != table->is_stmt)
28721 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28722 push_dw_line_info_entry (table, LI_set_line, line);
28723 if (debug_column_info)
28724 push_dw_line_info_entry (table, LI_set_column, column);
28727 table->file_num = file_num;
28728 table->line_num = line;
28729 table->column_num = column;
28730 table->discrim_num = discriminator;
28731 table->is_stmt = is_stmt;
28732 table->in_use = true;
28735 /* Record a source file location for a DECL_IGNORED_P function. */
28737 static void
28738 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28739 const char *filename)
28741 dw_fde_ref fde = cfun->fde;
28743 fde->ignored_debug = false;
28744 set_cur_line_info_table (function_section (fde->decl));
28746 dwarf2out_source_line (line, column, filename, 0, true);
28749 /* Record the beginning of a new source file. */
28751 static void
28752 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28754 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28756 macinfo_entry e;
28757 e.code = DW_MACINFO_start_file;
28758 e.lineno = lineno;
28759 e.info = ggc_strdup (filename);
28760 vec_safe_push (macinfo_table, e);
28764 /* Record the end of a source file. */
28766 static void
28767 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28769 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28771 macinfo_entry e;
28772 e.code = DW_MACINFO_end_file;
28773 e.lineno = lineno;
28774 e.info = NULL;
28775 vec_safe_push (macinfo_table, e);
28779 /* Called from debug_define in toplev.cc. The `buffer' parameter contains
28780 the tail part of the directive line, i.e. the part which is past the
28781 initial whitespace, #, whitespace, directive-name, whitespace part. */
28783 static void
28784 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28785 const char *buffer ATTRIBUTE_UNUSED)
28787 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28789 macinfo_entry e;
28790 /* Insert a dummy first entry to be able to optimize the whole
28791 predefined macro block using DW_MACRO_import. */
28792 if (macinfo_table->is_empty () && lineno <= 1)
28794 e.code = 0;
28795 e.lineno = 0;
28796 e.info = NULL;
28797 vec_safe_push (macinfo_table, e);
28799 e.code = DW_MACINFO_define;
28800 e.lineno = lineno;
28801 e.info = ggc_strdup (buffer);
28802 vec_safe_push (macinfo_table, e);
28806 /* Called from debug_undef in toplev.cc. The `buffer' parameter contains
28807 the tail part of the directive line, i.e. the part which is past the
28808 initial whitespace, #, whitespace, directive-name, whitespace part. */
28810 static void
28811 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28812 const char *buffer ATTRIBUTE_UNUSED)
28814 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28816 macinfo_entry e;
28817 /* Insert a dummy first entry to be able to optimize the whole
28818 predefined macro block using DW_MACRO_import. */
28819 if (macinfo_table->is_empty () && lineno <= 1)
28821 e.code = 0;
28822 e.lineno = 0;
28823 e.info = NULL;
28824 vec_safe_push (macinfo_table, e);
28826 e.code = DW_MACINFO_undef;
28827 e.lineno = lineno;
28828 e.info = ggc_strdup (buffer);
28829 vec_safe_push (macinfo_table, e);
28833 /* Helpers to manipulate hash table of CUs. */
28835 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28837 static inline hashval_t hash (const macinfo_entry *);
28838 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28841 inline hashval_t
28842 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28844 return htab_hash_string (entry->info);
28847 inline bool
28848 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28849 const macinfo_entry *entry2)
28851 return !strcmp (entry1->info, entry2->info);
28854 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28856 /* Output a single .debug_macinfo entry. */
28858 static void
28859 output_macinfo_op (macinfo_entry *ref)
28861 int file_num;
28862 size_t len;
28863 struct indirect_string_node *node;
28864 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28865 struct dwarf_file_data *fd;
28867 switch (ref->code)
28869 case DW_MACINFO_start_file:
28870 fd = lookup_filename (ref->info);
28871 file_num = maybe_emit_file (fd);
28872 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28873 dw2_asm_output_data_uleb128 (ref->lineno,
28874 "Included from line number %lu",
28875 (unsigned long) ref->lineno);
28876 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28877 break;
28878 case DW_MACINFO_end_file:
28879 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28880 break;
28881 case DW_MACINFO_define:
28882 case DW_MACINFO_undef:
28883 len = strlen (ref->info) + 1;
28884 if ((!dwarf_strict || dwarf_version >= 5)
28885 && len > (size_t) dwarf_offset_size
28886 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28887 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28889 if (dwarf_split_debug_info && dwarf_version >= 5)
28890 ref->code = ref->code == DW_MACINFO_define
28891 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28892 else
28893 ref->code = ref->code == DW_MACINFO_define
28894 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28895 output_macinfo_op (ref);
28896 return;
28898 dw2_asm_output_data (1, ref->code,
28899 ref->code == DW_MACINFO_define
28900 ? "Define macro" : "Undefine macro");
28901 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28902 (unsigned long) ref->lineno);
28903 dw2_asm_output_nstring (ref->info, -1, "The macro");
28904 break;
28905 case DW_MACRO_define_strp:
28906 dw2_asm_output_data (1, ref->code, "Define macro strp");
28907 goto do_DW_MACRO_define_strpx;
28908 case DW_MACRO_undef_strp:
28909 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28910 goto do_DW_MACRO_define_strpx;
28911 case DW_MACRO_define_strx:
28912 dw2_asm_output_data (1, ref->code, "Define macro strx");
28913 goto do_DW_MACRO_define_strpx;
28914 case DW_MACRO_undef_strx:
28915 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28916 /* FALLTHRU */
28917 do_DW_MACRO_define_strpx:
28918 /* NB: dwarf2out_finish performs:
28919 1. save_macinfo_strings
28920 2. hash table traverse of index_string
28921 3. output_macinfo -> output_macinfo_op
28922 4. output_indirect_strings
28923 -> hash table traverse of output_index_string
28925 When output_macinfo_op is called, all index strings have been
28926 added to hash table by save_macinfo_strings and we can't pass
28927 INSERT to find_slot_with_hash which may expand hash table, even
28928 if no insertion is needed, and change hash table traverse order
28929 between index_string and output_index_string. */
28930 node = find_AT_string (ref->info, NO_INSERT);
28931 gcc_assert (node
28932 && (node->form == DW_FORM_strp
28933 || node->form == dwarf_FORM (DW_FORM_strx)));
28934 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28935 (unsigned long) ref->lineno);
28936 if (node->form == DW_FORM_strp)
28937 dw2_asm_output_offset (dwarf_offset_size, node->label,
28938 debug_str_section, "The macro: \"%s\"",
28939 ref->info);
28940 else
28941 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28942 ref->info);
28943 break;
28944 case DW_MACRO_import:
28945 dw2_asm_output_data (1, ref->code, "Import");
28946 ASM_GENERATE_INTERNAL_LABEL (label,
28947 DEBUG_MACRO_SECTION_LABEL,
28948 ref->lineno + macinfo_label_base);
28949 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28950 break;
28951 default:
28952 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28953 ASM_COMMENT_START, (unsigned long) ref->code);
28954 break;
28958 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28959 other compilation unit .debug_macinfo sections. IDX is the first
28960 index of a define/undef, return the number of ops that should be
28961 emitted in a comdat .debug_macinfo section and emit
28962 a DW_MACRO_import entry referencing it.
28963 If the define/undef entry should be emitted normally, return 0. */
28965 static unsigned
28966 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28967 macinfo_hash_type **macinfo_htab)
28969 macinfo_entry *first, *second, *cur, *inc;
28970 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28971 unsigned char checksum[16];
28972 struct md5_ctx ctx;
28973 char *grp_name, *tail;
28974 const char *base;
28975 unsigned int i, count, encoded_filename_len, linebuf_len;
28976 macinfo_entry **slot;
28978 first = &(*macinfo_table)[idx];
28979 second = &(*macinfo_table)[idx + 1];
28981 /* Optimize only if there are at least two consecutive define/undef ops,
28982 and either all of them are before first DW_MACINFO_start_file
28983 with lineno {0,1} (i.e. predefined macro block), or all of them are
28984 in some included header file. */
28985 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28986 return 0;
28987 if (vec_safe_is_empty (files))
28989 if (first->lineno > 1 || second->lineno > 1)
28990 return 0;
28992 else if (first->lineno == 0)
28993 return 0;
28995 /* Find the last define/undef entry that can be grouped together
28996 with first and at the same time compute md5 checksum of their
28997 codes, linenumbers and strings. */
28998 md5_init_ctx (&ctx);
28999 for (i = idx; macinfo_table->iterate (i, &cur); i++)
29000 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
29001 break;
29002 else if (vec_safe_is_empty (files) && cur->lineno > 1)
29003 break;
29004 else
29006 unsigned char code = cur->code;
29007 md5_process_bytes (&code, 1, &ctx);
29008 checksum_uleb128 (cur->lineno, &ctx);
29009 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
29011 md5_finish_ctx (&ctx, checksum);
29012 count = i - idx;
29014 /* From the containing include filename (if any) pick up just
29015 usable characters from its basename. */
29016 if (vec_safe_is_empty (files))
29017 base = "";
29018 else
29019 base = lbasename (files->last ().info);
29020 for (encoded_filename_len = 0, i = 0; base[i]; i++)
29021 if (ISIDNUM (base[i]) || base[i] == '.')
29022 encoded_filename_len++;
29023 /* Count . at the end. */
29024 if (encoded_filename_len)
29025 encoded_filename_len++;
29027 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
29028 linebuf_len = strlen (linebuf);
29030 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
29031 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
29032 + 16 * 2 + 1);
29033 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
29034 tail = grp_name + 4;
29035 if (encoded_filename_len)
29037 for (i = 0; base[i]; i++)
29038 if (ISIDNUM (base[i]) || base[i] == '.')
29039 *tail++ = base[i];
29040 *tail++ = '.';
29042 memcpy (tail, linebuf, linebuf_len);
29043 tail += linebuf_len;
29044 *tail++ = '.';
29045 for (i = 0; i < 16; i++)
29046 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
29048 /* Construct a macinfo_entry for DW_MACRO_import
29049 in the empty vector entry before the first define/undef. */
29050 inc = &(*macinfo_table)[idx - 1];
29051 inc->code = DW_MACRO_import;
29052 inc->lineno = 0;
29053 inc->info = ggc_strdup (grp_name);
29054 if (!*macinfo_htab)
29055 *macinfo_htab = new macinfo_hash_type (10);
29056 /* Avoid emitting duplicates. */
29057 slot = (*macinfo_htab)->find_slot (inc, INSERT);
29058 if (*slot != NULL)
29060 inc->code = 0;
29061 inc->info = NULL;
29062 /* If such an entry has been used before, just emit
29063 a DW_MACRO_import op. */
29064 inc = *slot;
29065 output_macinfo_op (inc);
29066 /* And clear all macinfo_entry in the range to avoid emitting them
29067 in the second pass. */
29068 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
29070 cur->code = 0;
29071 cur->info = NULL;
29074 else
29076 *slot = inc;
29077 inc->lineno = (*macinfo_htab)->elements ();
29078 output_macinfo_op (inc);
29080 return count;
29083 /* Save any strings needed by the macinfo table in the debug str
29084 table. All strings must be collected into the table by the time
29085 index_string is called. */
29087 static void
29088 save_macinfo_strings (void)
29090 unsigned len;
29091 unsigned i;
29092 macinfo_entry *ref;
29094 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
29096 switch (ref->code)
29098 /* Match the logic in output_macinfo_op to decide on
29099 indirect strings. */
29100 case DW_MACINFO_define:
29101 case DW_MACINFO_undef:
29102 len = strlen (ref->info) + 1;
29103 if ((!dwarf_strict || dwarf_version >= 5)
29104 && len > (unsigned) dwarf_offset_size
29105 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
29106 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
29107 set_indirect_string (find_AT_string (ref->info));
29108 break;
29109 case DW_MACINFO_start_file:
29110 /* -gsplit-dwarf -g3 will also output filename as indirect
29111 string. */
29112 if (!dwarf_split_debug_info)
29113 break;
29114 /* Fall through. */
29115 case DW_MACRO_define_strp:
29116 case DW_MACRO_undef_strp:
29117 case DW_MACRO_define_strx:
29118 case DW_MACRO_undef_strx:
29119 set_indirect_string (find_AT_string (ref->info));
29120 break;
29121 default:
29122 break;
29127 /* Output macinfo section(s). */
29129 static void
29130 output_macinfo (const char *debug_line_label, bool early_lto_debug)
29132 unsigned i;
29133 unsigned long length = vec_safe_length (macinfo_table);
29134 macinfo_entry *ref;
29135 vec<macinfo_entry, va_gc> *files = NULL;
29136 macinfo_hash_type *macinfo_htab = NULL;
29137 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29139 if (! length)
29140 return;
29142 /* output_macinfo* uses these interchangeably. */
29143 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
29144 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
29145 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
29146 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
29148 /* AIX Assembler inserts the length, so adjust the reference to match the
29149 offset expected by debuggers. */
29150 strcpy (dl_section_ref, debug_line_label);
29151 if (XCOFF_DEBUGGING_INFO)
29152 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29154 /* For .debug_macro emit the section header. */
29155 if (!dwarf_strict || dwarf_version >= 5)
29157 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29158 "DWARF macro version number");
29159 if (dwarf_offset_size == 8)
29160 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29161 else
29162 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29163 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29164 debug_line_section, NULL);
29167 /* In the first loop, it emits the primary .debug_macinfo section
29168 and after each emitted op the macinfo_entry is cleared.
29169 If a longer range of define/undef ops can be optimized using
29170 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29171 the vector before the first define/undef in the range and the
29172 whole range of define/undef ops is not emitted and kept. */
29173 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29175 switch (ref->code)
29177 case DW_MACINFO_start_file:
29178 vec_safe_push (files, *ref);
29179 break;
29180 case DW_MACINFO_end_file:
29181 if (!vec_safe_is_empty (files))
29182 files->pop ();
29183 break;
29184 case DW_MACINFO_define:
29185 case DW_MACINFO_undef:
29186 if ((!dwarf_strict || dwarf_version >= 5)
29187 && HAVE_COMDAT_GROUP
29188 && vec_safe_length (files) != 1
29189 && i > 0
29190 && i + 1 < length
29191 && (*macinfo_table)[i - 1].code == 0)
29193 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29194 if (count)
29196 i += count - 1;
29197 continue;
29200 break;
29201 case 0:
29202 /* A dummy entry may be inserted at the beginning to be able
29203 to optimize the whole block of predefined macros. */
29204 if (i == 0)
29205 continue;
29206 default:
29207 break;
29209 output_macinfo_op (ref);
29210 ref->info = NULL;
29211 ref->code = 0;
29214 if (!macinfo_htab)
29215 return;
29217 /* Save the number of transparent includes so we can adjust the
29218 label number for the fat LTO object DWARF. */
29219 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29221 delete macinfo_htab;
29222 macinfo_htab = NULL;
29224 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29225 terminate the current chain and switch to a new comdat .debug_macinfo
29226 section and emit the define/undef entries within it. */
29227 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29228 switch (ref->code)
29230 case 0:
29231 continue;
29232 case DW_MACRO_import:
29234 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29235 tree comdat_key = get_identifier (ref->info);
29236 /* Terminate the previous .debug_macinfo section. */
29237 dw2_asm_output_data (1, 0, "End compilation unit");
29238 targetm.asm_out.named_section (debug_macinfo_section_name,
29239 SECTION_DEBUG
29240 | SECTION_LINKONCE
29241 | (early_lto_debug
29242 ? SECTION_EXCLUDE : 0),
29243 comdat_key);
29244 ASM_GENERATE_INTERNAL_LABEL (label,
29245 DEBUG_MACRO_SECTION_LABEL,
29246 ref->lineno + macinfo_label_base);
29247 ASM_OUTPUT_LABEL (asm_out_file, label);
29248 ref->code = 0;
29249 ref->info = NULL;
29250 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29251 "DWARF macro version number");
29252 if (dwarf_offset_size == 8)
29253 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29254 else
29255 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29257 break;
29258 case DW_MACINFO_define:
29259 case DW_MACINFO_undef:
29260 output_macinfo_op (ref);
29261 ref->code = 0;
29262 ref->info = NULL;
29263 break;
29264 default:
29265 gcc_unreachable ();
29268 macinfo_label_base += macinfo_label_base_adj;
29271 /* As init_sections_and_labels may get called multiple times, have a
29272 generation count for labels. */
29273 static unsigned init_sections_and_labels_generation;
29275 /* Initialize the various sections and labels for dwarf output and prefix
29276 them with PREFIX if non-NULL. Returns the generation (zero based
29277 number of times function was called). */
29279 static unsigned
29280 init_sections_and_labels (bool early_lto_debug)
29282 if (early_lto_debug)
29284 if (!dwarf_split_debug_info)
29286 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29287 SECTION_DEBUG | SECTION_EXCLUDE,
29288 NULL);
29289 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29290 SECTION_DEBUG | SECTION_EXCLUDE,
29291 NULL);
29292 debug_macinfo_section_name
29293 = ((dwarf_strict && dwarf_version < 5)
29294 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29295 debug_macinfo_section = get_section (debug_macinfo_section_name,
29296 SECTION_DEBUG
29297 | SECTION_EXCLUDE, NULL);
29299 else
29301 /* ??? Which of the following do we need early? */
29302 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29303 SECTION_DEBUG | SECTION_EXCLUDE,
29304 NULL);
29305 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29306 SECTION_DEBUG | SECTION_EXCLUDE,
29307 NULL);
29308 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29309 SECTION_DEBUG
29310 | SECTION_EXCLUDE, NULL);
29311 debug_skeleton_abbrev_section
29312 = get_section (DEBUG_LTO_ABBREV_SECTION,
29313 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29314 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29315 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29316 init_sections_and_labels_generation);
29318 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29319 stay in the main .o, but the skeleton_line goes into the split
29320 off dwo. */
29321 debug_skeleton_line_section
29322 = get_section (DEBUG_LTO_LINE_SECTION,
29323 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29324 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29325 DEBUG_SKELETON_LINE_SECTION_LABEL,
29326 init_sections_and_labels_generation);
29327 debug_str_offsets_section
29328 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29329 SECTION_DEBUG | SECTION_EXCLUDE,
29330 NULL);
29331 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29332 DEBUG_SKELETON_INFO_SECTION_LABEL,
29333 init_sections_and_labels_generation);
29334 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29335 DEBUG_STR_DWO_SECTION_FLAGS,
29336 NULL);
29337 debug_macinfo_section_name
29338 = ((dwarf_strict && dwarf_version < 5)
29339 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29340 debug_macinfo_section = get_section (debug_macinfo_section_name,
29341 SECTION_DEBUG | SECTION_EXCLUDE,
29342 NULL);
29344 /* For macro info and the file table we have to refer to a
29345 debug_line section. */
29346 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29347 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29348 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29349 DEBUG_LINE_SECTION_LABEL,
29350 init_sections_and_labels_generation);
29352 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29353 DEBUG_STR_SECTION_FLAGS
29354 | SECTION_EXCLUDE, NULL);
29355 if (!dwarf_split_debug_info)
29356 debug_line_str_section
29357 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29358 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29360 else
29362 if (!dwarf_split_debug_info)
29364 debug_info_section = get_section (DEBUG_INFO_SECTION,
29365 SECTION_DEBUG, NULL);
29366 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29367 SECTION_DEBUG, NULL);
29368 debug_loc_section = get_section (dwarf_version >= 5
29369 ? DEBUG_LOCLISTS_SECTION
29370 : DEBUG_LOC_SECTION,
29371 SECTION_DEBUG, NULL);
29372 debug_macinfo_section_name
29373 = ((dwarf_strict && dwarf_version < 5)
29374 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29375 debug_macinfo_section = get_section (debug_macinfo_section_name,
29376 SECTION_DEBUG, NULL);
29378 else
29380 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29381 SECTION_DEBUG | SECTION_EXCLUDE,
29382 NULL);
29383 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29384 SECTION_DEBUG | SECTION_EXCLUDE,
29385 NULL);
29386 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29387 SECTION_DEBUG, NULL);
29388 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29389 SECTION_DEBUG, NULL);
29390 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29391 SECTION_DEBUG, NULL);
29392 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29393 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29394 init_sections_and_labels_generation);
29396 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29397 stay in the main .o, but the skeleton_line goes into the
29398 split off dwo. */
29399 debug_skeleton_line_section
29400 = get_section (DEBUG_DWO_LINE_SECTION,
29401 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29402 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29403 DEBUG_SKELETON_LINE_SECTION_LABEL,
29404 init_sections_and_labels_generation);
29405 debug_str_offsets_section
29406 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29407 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29408 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29409 DEBUG_SKELETON_INFO_SECTION_LABEL,
29410 init_sections_and_labels_generation);
29411 debug_loc_section = get_section (dwarf_version >= 5
29412 ? DEBUG_DWO_LOCLISTS_SECTION
29413 : DEBUG_DWO_LOC_SECTION,
29414 SECTION_DEBUG | SECTION_EXCLUDE,
29415 NULL);
29416 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29417 DEBUG_STR_DWO_SECTION_FLAGS,
29418 NULL);
29419 debug_macinfo_section_name
29420 = ((dwarf_strict && dwarf_version < 5)
29421 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29422 debug_macinfo_section = get_section (debug_macinfo_section_name,
29423 SECTION_DEBUG | SECTION_EXCLUDE,
29424 NULL);
29425 if (dwarf_version >= 5)
29426 debug_ranges_dwo_section
29427 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29428 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29430 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29431 SECTION_DEBUG, NULL);
29432 debug_line_section = get_section (DEBUG_LINE_SECTION,
29433 SECTION_DEBUG, NULL);
29434 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29435 SECTION_DEBUG, NULL);
29436 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29437 SECTION_DEBUG, NULL);
29438 debug_str_section = get_section (DEBUG_STR_SECTION,
29439 DEBUG_STR_SECTION_FLAGS, NULL);
29440 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29441 || asm_outputs_debug_line_str ())
29442 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29443 DEBUG_STR_SECTION_FLAGS, NULL);
29445 debug_ranges_section = get_section (dwarf_version >= 5
29446 ? DEBUG_RNGLISTS_SECTION
29447 : DEBUG_RANGES_SECTION,
29448 SECTION_DEBUG, NULL);
29449 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29450 SECTION_DEBUG, NULL);
29453 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29454 DEBUG_ABBREV_SECTION_LABEL,
29455 init_sections_and_labels_generation);
29456 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29457 DEBUG_INFO_SECTION_LABEL,
29458 init_sections_and_labels_generation);
29459 info_section_emitted = false;
29460 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29461 DEBUG_LINE_SECTION_LABEL,
29462 init_sections_and_labels_generation);
29463 /* There are up to 6 unique ranges labels per generation.
29464 See also output_rnglists. */
29465 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29466 DEBUG_RANGES_SECTION_LABEL,
29467 init_sections_and_labels_generation * 6);
29468 if (dwarf_version >= 5 && dwarf_split_debug_info)
29469 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29470 DEBUG_RANGES_SECTION_LABEL,
29471 1 + init_sections_and_labels_generation * 6);
29472 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29473 DEBUG_ADDR_SECTION_LABEL,
29474 init_sections_and_labels_generation);
29475 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29476 (dwarf_strict && dwarf_version < 5)
29477 ? DEBUG_MACINFO_SECTION_LABEL
29478 : DEBUG_MACRO_SECTION_LABEL,
29479 init_sections_and_labels_generation);
29480 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29481 init_sections_and_labels_generation);
29483 ++init_sections_and_labels_generation;
29484 return init_sections_and_labels_generation - 1;
29487 /* Set up for Dwarf output at the start of compilation. */
29489 static void
29490 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29492 /* Allocate the file_table. */
29493 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29495 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29496 /* Allocate the decl_die_table. */
29497 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29499 /* Allocate the decl_loc_table. */
29500 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29502 /* Allocate the cached_dw_loc_list_table. */
29503 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29505 /* Allocate the initial hunk of the abbrev_die_table. */
29506 vec_alloc (abbrev_die_table, 256);
29507 /* Zero-th entry is allocated, but unused. */
29508 abbrev_die_table->quick_push (NULL);
29510 /* Allocate the dwarf_proc_stack_usage_map. */
29511 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29513 /* Allocate the pubtypes and pubnames vectors. */
29514 vec_alloc (pubname_table, 32);
29515 vec_alloc (pubtype_table, 32);
29517 vec_alloc (incomplete_types, 64);
29519 vec_alloc (used_rtx_array, 32);
29521 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29522 vec_alloc (macinfo_table, 64);
29523 #endif
29525 /* If front-ends already registered a main translation unit but we were not
29526 ready to perform the association, do this now. */
29527 if (main_translation_unit != NULL_TREE)
29528 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29531 /* Called before compile () starts outputtting functions, variables
29532 and toplevel asms into assembly. */
29534 static void
29535 dwarf2out_assembly_start (void)
29537 if (text_section_line_info)
29538 return;
29540 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29541 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29542 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29543 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29544 COLD_TEXT_SECTION_LABEL, 0);
29545 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29547 switch_to_section (text_section);
29548 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29549 #endif
29551 /* Make sure the line number table for .text always exists. */
29552 text_section_line_info = new_line_info_table ();
29553 text_section_line_info->end_label = text_end_label;
29555 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29556 cur_line_info_table = text_section_line_info;
29557 #endif
29559 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29560 && dwarf2out_do_cfi_asm ()
29561 && !dwarf2out_do_eh_frame ())
29562 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29564 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29565 if (output_asm_line_debug_info () && dwarf_version >= 5)
29567 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29568 tell it the comp_dir and main file name for the zero entry
29569 line table. */
29570 const char *comp_dir, *filename0;
29572 comp_dir = comp_dir_string ();
29573 if (comp_dir == NULL)
29574 comp_dir = "";
29576 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29577 if (filename0 == NULL)
29578 filename0 = "";
29580 fprintf (asm_out_file, "\t.file 0 ");
29581 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29582 fputc (' ', asm_out_file);
29583 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29584 fputc ('\n', asm_out_file);
29586 else
29587 #endif
29588 /* Work around for PR101575: output a dummy .file directive. */
29589 if (!last_emitted_file && dwarf_debuginfo_p ()
29590 && debug_info_level >= DINFO_LEVEL_TERSE)
29592 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29594 if (filename0 == NULL)
29595 filename0 = "<dummy>";
29596 maybe_emit_file (lookup_filename (filename0));
29600 /* A helper function for dwarf2out_finish called through
29601 htab_traverse. Assign a string its index. All strings must be
29602 collected into the table by the time index_string is called,
29603 because the indexing code relies on htab_traverse to traverse nodes
29604 in the same order for each run. */
29607 index_string (indirect_string_node **h, unsigned int *index)
29609 indirect_string_node *node = *h;
29611 find_string_form (node);
29612 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29614 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29615 node->index = *index;
29616 *index += 1;
29618 return 1;
29621 /* A helper function for output_indirect_strings called through
29622 htab_traverse. Output the offset to a string and update the
29623 current offset. */
29626 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29628 indirect_string_node *node = *h;
29630 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29632 /* Assert that this node has been assigned an index. */
29633 gcc_assert (node->index != NO_INDEX_ASSIGNED
29634 && node->index != NOT_INDEXED);
29635 dw2_asm_output_data (dwarf_offset_size, *offset,
29636 "indexed string 0x%x: %s", node->index, node->str);
29637 *offset += strlen (node->str) + 1;
29639 return 1;
29642 /* A helper function for dwarf2out_finish called through
29643 htab_traverse. Output the indexed string. */
29646 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29648 struct indirect_string_node *node = *h;
29650 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29652 /* Assert that the strings are output in the same order as their
29653 indexes were assigned. */
29654 gcc_assert (*cur_idx == node->index);
29655 assemble_string (node->str, strlen (node->str) + 1);
29656 *cur_idx += 1;
29658 return 1;
29661 /* A helper function for output_indirect_strings. Counts the number
29662 of index strings offsets. Must match the logic of the functions
29663 output_index_string[_offsets] above. */
29665 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29667 struct indirect_string_node *node = *h;
29669 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29670 *last_idx += 1;
29671 return 1;
29674 /* A helper function for dwarf2out_finish called through
29675 htab_traverse. Emit one queued .debug_str string. */
29678 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29680 struct indirect_string_node *node = *h;
29682 node->form = find_string_form (node);
29683 if (node->form == form && node->refcount > 0)
29685 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29686 assemble_string (node->str, strlen (node->str) + 1);
29689 return 1;
29692 /* Output the indexed string table. */
29694 static void
29695 output_indirect_strings (void)
29697 switch_to_section (debug_str_section);
29698 if (!dwarf_split_debug_info)
29699 debug_str_hash->traverse<enum dwarf_form,
29700 output_indirect_string> (DW_FORM_strp);
29701 else
29703 unsigned int offset = 0;
29704 unsigned int cur_idx = 0;
29706 if (skeleton_debug_str_hash)
29707 skeleton_debug_str_hash->traverse<enum dwarf_form,
29708 output_indirect_string> (DW_FORM_strp);
29710 switch_to_section (debug_str_offsets_section);
29711 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29712 header. Note that we don't need to generate a label to the
29713 actual index table following the header here, because this is
29714 for the split dwarf case only. In an .dwo file there is only
29715 one string offsets table (and one debug info section). But
29716 if we would start using string offset tables for the main (or
29717 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29718 pointing to the actual index after the header. Split dwarf
29719 units will never have a string offsets base attribute. When
29720 a split unit is moved into a .dwp file the string offsets can
29721 be found through the .debug_cu_index section table. */
29722 if (dwarf_version >= 5)
29724 unsigned int last_idx = 0;
29725 unsigned long str_offsets_length;
29727 debug_str_hash->traverse_noresize
29728 <unsigned int *, count_index_strings> (&last_idx);
29729 str_offsets_length = last_idx * dwarf_offset_size + 4;
29730 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29731 dw2_asm_output_data (4, 0xffffffff,
29732 "Escape value for 64-bit DWARF extension");
29733 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29734 "Length of string offsets unit");
29735 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29736 dw2_asm_output_data (2, 0, "Header zero padding");
29738 debug_str_hash->traverse_noresize
29739 <unsigned int *, output_index_string_offset> (&offset);
29740 switch_to_section (debug_str_dwo_section);
29741 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29742 (&cur_idx);
29746 /* Callback for htab_traverse to assign an index to an entry in the
29747 table, and to write that entry to the .debug_addr section. */
29750 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29752 addr_table_entry *entry = *slot;
29754 if (entry->refcount == 0)
29756 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29757 || entry->index == NOT_INDEXED);
29758 return 1;
29761 gcc_assert (entry->index == *cur_index);
29762 (*cur_index)++;
29764 switch (entry->kind)
29766 case ate_kind_rtx:
29767 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29768 "0x%x", entry->index);
29769 break;
29770 case ate_kind_rtx_dtprel:
29771 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29772 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29773 DWARF2_ADDR_SIZE,
29774 entry->addr.rtl);
29775 fputc ('\n', asm_out_file);
29776 break;
29777 case ate_kind_label:
29778 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29779 "0x%x", entry->index);
29780 break;
29781 default:
29782 gcc_unreachable ();
29784 return 1;
29787 /* A helper function for dwarf2out_finish. Counts the number
29788 of indexed addresses. Must match the logic of the functions
29789 output_addr_table_entry above. */
29791 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29793 addr_table_entry *entry = *slot;
29795 if (entry->refcount > 0)
29796 *last_idx += 1;
29797 return 1;
29800 /* Produce the .debug_addr section. */
29802 static void
29803 output_addr_table (void)
29805 unsigned int index = 0;
29806 if (addr_index_table == NULL || addr_index_table->size () == 0)
29807 return;
29809 switch_to_section (debug_addr_section);
29810 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29811 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29812 before DWARF5, didn't have a header for .debug_addr units.
29813 DWARF5 specifies a small header when address tables are used. */
29814 if (dwarf_version >= 5)
29816 unsigned int last_idx = 0;
29817 unsigned long addrs_length;
29819 addr_index_table->traverse_noresize
29820 <unsigned int *, count_index_addrs> (&last_idx);
29821 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29823 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29824 dw2_asm_output_data (4, 0xffffffff,
29825 "Escape value for 64-bit DWARF extension");
29826 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29827 "Length of Address Unit");
29828 dw2_asm_output_data (2, 5, "DWARF addr version");
29829 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29830 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29832 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29834 addr_index_table
29835 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29838 #if ENABLE_ASSERT_CHECKING
29839 /* Verify that all marks are clear. */
29841 static void
29842 verify_marks_clear (dw_die_ref die)
29844 dw_die_ref c;
29846 gcc_assert (! die->die_mark);
29847 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29849 #endif /* ENABLE_ASSERT_CHECKING */
29851 /* Clear the marks for a die and its children.
29852 Be cool if the mark isn't set. */
29854 static void
29855 prune_unmark_dies (dw_die_ref die)
29857 dw_die_ref c;
29859 if (die->die_mark)
29860 die->die_mark = 0;
29861 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29864 /* Given LOC that is referenced by a DIE we're marking as used, find all
29865 referenced DWARF procedures it references and mark them as used. */
29867 static void
29868 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29870 for (; loc != NULL; loc = loc->dw_loc_next)
29871 switch (loc->dw_loc_opc)
29873 case DW_OP_implicit_pointer:
29874 case DW_OP_convert:
29875 case DW_OP_reinterpret:
29876 case DW_OP_GNU_implicit_pointer:
29877 case DW_OP_GNU_convert:
29878 case DW_OP_GNU_reinterpret:
29879 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29880 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29881 break;
29882 case DW_OP_GNU_variable_value:
29883 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29885 dw_die_ref ref
29886 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29887 if (ref == NULL)
29888 break;
29889 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29890 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29891 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29893 /* FALLTHRU */
29894 case DW_OP_call2:
29895 case DW_OP_call4:
29896 case DW_OP_call_ref:
29897 case DW_OP_const_type:
29898 case DW_OP_GNU_const_type:
29899 case DW_OP_GNU_parameter_ref:
29900 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29901 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29902 break;
29903 case DW_OP_regval_type:
29904 case DW_OP_deref_type:
29905 case DW_OP_GNU_regval_type:
29906 case DW_OP_GNU_deref_type:
29907 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29908 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29909 break;
29910 case DW_OP_entry_value:
29911 case DW_OP_GNU_entry_value:
29912 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29913 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29914 break;
29915 default:
29916 break;
29920 /* Given DIE that we're marking as used, find any other dies
29921 it references as attributes and mark them as used. */
29923 static void
29924 prune_unused_types_walk_attribs (dw_die_ref die)
29926 dw_attr_node *a;
29927 unsigned ix;
29929 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29931 switch (AT_class (a))
29933 /* Make sure DWARF procedures referenced by location descriptions will
29934 get emitted. */
29935 case dw_val_class_loc:
29936 prune_unused_types_walk_loc_descr (AT_loc (a));
29937 break;
29938 case dw_val_class_loc_list:
29939 for (dw_loc_list_ref list = AT_loc_list (a);
29940 list != NULL;
29941 list = list->dw_loc_next)
29942 prune_unused_types_walk_loc_descr (list->expr);
29943 break;
29945 case dw_val_class_view_list:
29946 /* This points to a loc_list in another attribute, so it's
29947 already covered. */
29948 break;
29950 case dw_val_class_die_ref:
29951 /* A reference to another DIE.
29952 Make sure that it will get emitted.
29953 If it was broken out into a comdat group, don't follow it. */
29954 if (! AT_ref (a)->comdat_type_p
29955 || a->dw_attr == DW_AT_specification)
29956 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29957 break;
29959 case dw_val_class_str:
29960 /* Set the string's refcount to 0 so that prune_unused_types_mark
29961 accounts properly for it. */
29962 a->dw_attr_val.v.val_str->refcount = 0;
29963 break;
29965 default:
29966 break;
29971 /* Mark the generic parameters and arguments children DIEs of DIE. */
29973 static void
29974 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29976 dw_die_ref c;
29978 if (die == NULL || die->die_child == NULL)
29979 return;
29980 c = die->die_child;
29983 if (is_template_parameter (c))
29984 prune_unused_types_mark (c, 1);
29985 c = c->die_sib;
29986 } while (c && c != die->die_child);
29989 /* Mark DIE as being used. If DOKIDS is true, then walk down
29990 to DIE's children. */
29992 static void
29993 prune_unused_types_mark (dw_die_ref die, int dokids)
29995 dw_die_ref c;
29997 if (die->die_mark == 0)
29999 /* We haven't done this node yet. Mark it as used. */
30000 die->die_mark = 1;
30001 /* If this is the DIE of a generic type instantiation,
30002 mark the children DIEs that describe its generic parms and
30003 args. */
30004 prune_unused_types_mark_generic_parms_dies (die);
30006 /* We also have to mark its parents as used.
30007 (But we don't want to mark our parent's kids due to this,
30008 unless it is a class.) */
30009 if (die->die_parent)
30010 prune_unused_types_mark (die->die_parent,
30011 class_scope_p (die->die_parent));
30013 /* Mark any referenced nodes. */
30014 prune_unused_types_walk_attribs (die);
30016 /* If this node is a specification,
30017 also mark the definition, if it exists. */
30018 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
30019 prune_unused_types_mark (die->die_definition, 1);
30022 if (dokids && die->die_mark != 2)
30024 /* We need to walk the children, but haven't done so yet.
30025 Remember that we've walked the kids. */
30026 die->die_mark = 2;
30028 /* If this is an array type, we need to make sure our
30029 kids get marked, even if they're types. If we're
30030 breaking out types into comdat sections, do this
30031 for all type definitions. */
30032 if (die->die_tag == DW_TAG_array_type
30033 || (use_debug_types
30034 && is_type_die (die) && ! is_declaration_die (die)))
30035 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
30036 else
30037 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30041 /* For local classes, look if any static member functions were emitted
30042 and if so, mark them. */
30044 static void
30045 prune_unused_types_walk_local_classes (dw_die_ref die)
30047 dw_die_ref c;
30049 if (die->die_mark == 2)
30050 return;
30052 switch (die->die_tag)
30054 case DW_TAG_structure_type:
30055 case DW_TAG_union_type:
30056 case DW_TAG_class_type:
30057 case DW_TAG_interface_type:
30058 break;
30060 case DW_TAG_subprogram:
30061 if (!get_AT_flag (die, DW_AT_declaration)
30062 || die->die_definition != NULL)
30063 prune_unused_types_mark (die, 1);
30064 return;
30066 default:
30067 return;
30070 /* Mark children. */
30071 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
30074 /* Walk the tree DIE and mark types that we actually use. */
30076 static void
30077 prune_unused_types_walk (dw_die_ref die)
30079 dw_die_ref c;
30081 /* Don't do anything if this node is already marked and
30082 children have been marked as well. */
30083 if (die->die_mark == 2)
30084 return;
30086 switch (die->die_tag)
30088 case DW_TAG_structure_type:
30089 case DW_TAG_union_type:
30090 case DW_TAG_class_type:
30091 case DW_TAG_interface_type:
30092 if (die->die_perennial_p)
30093 break;
30095 for (c = die->die_parent; c; c = c->die_parent)
30096 if (c->die_tag == DW_TAG_subprogram)
30097 break;
30099 /* Finding used static member functions inside of classes
30100 is needed just for local classes, because for other classes
30101 static member function DIEs with DW_AT_specification
30102 are emitted outside of the DW_TAG_*_type. If we ever change
30103 it, we'd need to call this even for non-local classes. */
30104 if (c)
30105 prune_unused_types_walk_local_classes (die);
30107 /* It's a type node --- don't mark it. */
30108 return;
30110 case DW_TAG_const_type:
30111 case DW_TAG_packed_type:
30112 case DW_TAG_pointer_type:
30113 case DW_TAG_reference_type:
30114 case DW_TAG_rvalue_reference_type:
30115 case DW_TAG_volatile_type:
30116 case DW_TAG_typedef:
30117 case DW_TAG_array_type:
30118 case DW_TAG_friend:
30119 case DW_TAG_enumeration_type:
30120 case DW_TAG_subroutine_type:
30121 case DW_TAG_string_type:
30122 case DW_TAG_set_type:
30123 case DW_TAG_subrange_type:
30124 case DW_TAG_ptr_to_member_type:
30125 case DW_TAG_file_type:
30126 /* Type nodes are useful only when other DIEs reference them --- don't
30127 mark them. */
30128 /* FALLTHROUGH */
30130 case DW_TAG_dwarf_procedure:
30131 /* Likewise for DWARF procedures. */
30133 if (die->die_perennial_p)
30134 break;
30136 return;
30138 case DW_TAG_variable:
30139 if (flag_debug_only_used_symbols)
30141 if (die->die_perennial_p)
30142 break;
30144 /* For static data members, the declaration in the class is supposed
30145 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
30146 DWARF5. DW_TAG_member will be marked, so mark even such
30147 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
30148 attribute. */
30149 if (dwarf_version >= 5
30150 && class_scope_p (die->die_parent)
30151 && get_AT (die, DW_AT_const_value))
30152 break;
30154 /* premark_used_variables marks external variables --- don't mark
30155 them here. But function-local externals are always considered
30156 used. */
30157 if (get_AT (die, DW_AT_external))
30159 for (c = die->die_parent; c; c = c->die_parent)
30160 if (c->die_tag == DW_TAG_subprogram)
30161 break;
30162 if (!c)
30163 return;
30166 /* FALLTHROUGH */
30168 default:
30169 /* Mark everything else. */
30170 break;
30173 if (die->die_mark == 0)
30175 die->die_mark = 1;
30177 /* Now, mark any dies referenced from here. */
30178 prune_unused_types_walk_attribs (die);
30181 die->die_mark = 2;
30183 /* Mark children. */
30184 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30187 /* Increment the string counts on strings referred to from DIE's
30188 attributes. */
30190 static void
30191 prune_unused_types_update_strings (dw_die_ref die)
30193 dw_attr_node *a;
30194 unsigned ix;
30196 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30197 if (AT_class (a) == dw_val_class_str)
30199 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30200 s->refcount++;
30201 /* Avoid unnecessarily putting strings that are used less than
30202 twice in the hash table. */
30203 if (s->form != DW_FORM_line_strp
30204 && (s->refcount
30205 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30207 indirect_string_node **slot
30208 = debug_str_hash->find_slot_with_hash (s->str,
30209 htab_hash_string (s->str),
30210 INSERT);
30211 gcc_assert (*slot == NULL);
30212 *slot = s;
30217 /* Mark DIE and its children as removed. */
30219 static void
30220 mark_removed (dw_die_ref die)
30222 dw_die_ref c;
30223 die->removed = true;
30224 FOR_EACH_CHILD (die, c, mark_removed (c));
30227 /* Remove from the tree DIE any dies that aren't marked. */
30229 static void
30230 prune_unused_types_prune (dw_die_ref die)
30232 dw_die_ref c;
30234 gcc_assert (die->die_mark);
30235 prune_unused_types_update_strings (die);
30237 if (! die->die_child)
30238 return;
30240 c = die->die_child;
30241 do {
30242 dw_die_ref prev = c, next;
30243 for (c = c->die_sib; ! c->die_mark; c = next)
30244 if (c == die->die_child)
30246 /* No marked children between 'prev' and the end of the list. */
30247 if (prev == c)
30248 /* No marked children at all. */
30249 die->die_child = NULL;
30250 else
30252 prev->die_sib = c->die_sib;
30253 die->die_child = prev;
30255 c->die_sib = NULL;
30256 mark_removed (c);
30257 return;
30259 else
30261 next = c->die_sib;
30262 c->die_sib = NULL;
30263 mark_removed (c);
30266 if (c != prev->die_sib)
30267 prev->die_sib = c;
30268 prune_unused_types_prune (c);
30269 } while (c != die->die_child);
30272 /* Remove dies representing declarations that we never use. */
30274 static void
30275 prune_unused_types (void)
30277 unsigned int i;
30278 limbo_die_node *node;
30279 comdat_type_node *ctnode;
30280 pubname_entry *pub;
30281 dw_die_ref base_type;
30283 #if ENABLE_ASSERT_CHECKING
30284 /* All the marks should already be clear. */
30285 verify_marks_clear (comp_unit_die ());
30286 for (node = limbo_die_list; node; node = node->next)
30287 verify_marks_clear (node->die);
30288 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30289 verify_marks_clear (ctnode->root_die);
30290 #endif /* ENABLE_ASSERT_CHECKING */
30292 /* Mark types that are used in global variables. */
30293 premark_types_used_by_global_vars ();
30295 /* Mark variables used in the symtab. */
30296 if (flag_debug_only_used_symbols)
30297 premark_used_variables ();
30299 /* Set the mark on nodes that are actually used. */
30300 prune_unused_types_walk (comp_unit_die ());
30301 for (node = limbo_die_list; node; node = node->next)
30302 prune_unused_types_walk (node->die);
30303 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30305 prune_unused_types_walk (ctnode->root_die);
30306 prune_unused_types_mark (ctnode->type_die, 1);
30309 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30310 are unusual in that they are pubnames that are the children of pubtypes.
30311 They should only be marked via their parent DW_TAG_enumeration_type die,
30312 not as roots in themselves. */
30313 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30314 if (pub->die->die_tag != DW_TAG_enumerator)
30315 prune_unused_types_mark (pub->die, 1);
30316 for (i = 0; base_types.iterate (i, &base_type); i++)
30317 prune_unused_types_mark (base_type, 1);
30319 /* Also set the mark on nodes that could be referenced by
30320 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30321 by DW_TAG_inlined_subroutine origins. */
30322 cgraph_node *cnode;
30323 FOR_EACH_FUNCTION (cnode)
30324 if (cnode->referred_to_p (false))
30326 dw_die_ref die = lookup_decl_die (cnode->decl);
30327 if (die == NULL || die->die_mark)
30328 continue;
30329 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30330 if (e->caller != cnode)
30332 prune_unused_types_mark (die, 1);
30333 break;
30337 if (debug_str_hash)
30338 debug_str_hash->empty ();
30339 if (skeleton_debug_str_hash)
30340 skeleton_debug_str_hash->empty ();
30341 prune_unused_types_prune (comp_unit_die ());
30342 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30344 node = *pnode;
30345 if (!node->die->die_mark)
30346 *pnode = node->next;
30347 else
30349 prune_unused_types_prune (node->die);
30350 pnode = &node->next;
30353 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30354 prune_unused_types_prune (ctnode->root_die);
30356 /* Leave the marks clear. */
30357 prune_unmark_dies (comp_unit_die ());
30358 for (node = limbo_die_list; node; node = node->next)
30359 prune_unmark_dies (node->die);
30360 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30361 prune_unmark_dies (ctnode->root_die);
30364 /* Helpers to manipulate hash table of comdat type units. */
30366 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30368 static inline hashval_t hash (const comdat_type_node *);
30369 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30372 inline hashval_t
30373 comdat_type_hasher::hash (const comdat_type_node *type_node)
30375 hashval_t h;
30376 memcpy (&h, type_node->signature, sizeof (h));
30377 return h;
30380 inline bool
30381 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30382 const comdat_type_node *type_node_2)
30384 return (! memcmp (type_node_1->signature, type_node_2->signature,
30385 DWARF_TYPE_SIGNATURE_SIZE));
30388 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30389 to the location it would have been added, should we know its
30390 DECL_ASSEMBLER_NAME when we added other attributes. This will
30391 probably improve compactness of debug info, removing equivalent
30392 abbrevs, and hide any differences caused by deferring the
30393 computation of the assembler name, triggered by e.g. PCH. */
30395 static inline void
30396 move_linkage_attr (dw_die_ref die)
30398 unsigned ix = vec_safe_length (die->die_attr);
30399 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30401 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30402 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30404 while (--ix > 0)
30406 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30408 if (prev->dw_attr == DW_AT_decl_line
30409 || prev->dw_attr == DW_AT_decl_column
30410 || prev->dw_attr == DW_AT_name)
30411 break;
30414 if (ix != vec_safe_length (die->die_attr) - 1)
30416 die->die_attr->pop ();
30417 die->die_attr->quick_insert (ix, linkage);
30421 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30422 referenced from typed stack ops and count how often they are used. */
30424 static void
30425 mark_base_types (dw_loc_descr_ref loc)
30427 dw_die_ref base_type = NULL;
30429 for (; loc; loc = loc->dw_loc_next)
30431 switch (loc->dw_loc_opc)
30433 case DW_OP_regval_type:
30434 case DW_OP_deref_type:
30435 case DW_OP_GNU_regval_type:
30436 case DW_OP_GNU_deref_type:
30437 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30438 break;
30439 case DW_OP_convert:
30440 case DW_OP_reinterpret:
30441 case DW_OP_GNU_convert:
30442 case DW_OP_GNU_reinterpret:
30443 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30444 continue;
30445 /* FALLTHRU */
30446 case DW_OP_const_type:
30447 case DW_OP_GNU_const_type:
30448 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30449 break;
30450 case DW_OP_entry_value:
30451 case DW_OP_GNU_entry_value:
30452 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30453 continue;
30454 default:
30455 continue;
30457 gcc_assert (base_type->die_parent == comp_unit_die ());
30458 if (base_type->die_mark)
30459 base_type->die_mark++;
30460 else
30462 base_types.safe_push (base_type);
30463 base_type->die_mark = 1;
30468 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30469 referenced from typed stack ops and count how often they are used. */
30471 static void
30472 mark_base_types (dw_die_ref die)
30474 dw_die_ref c;
30475 dw_attr_node *a;
30476 dw_loc_list_ref *curr;
30477 unsigned ix;
30479 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30480 switch (AT_class (a))
30482 case dw_val_class_loc_list:
30483 curr = AT_loc_list_ptr (a);
30484 while (*curr)
30486 mark_base_types ((*curr)->expr);
30487 curr = &(*curr)->dw_loc_next;
30489 break;
30491 case dw_val_class_loc:
30492 mark_base_types (AT_loc (a));
30493 break;
30495 default:
30496 break;
30499 FOR_EACH_CHILD (die, c, mark_base_types (c));
30502 /* Comparison function for sorting marked base types. */
30504 static int
30505 base_type_cmp (const void *x, const void *y)
30507 dw_die_ref dx = *(const dw_die_ref *) x;
30508 dw_die_ref dy = *(const dw_die_ref *) y;
30509 unsigned int byte_size1, byte_size2;
30510 unsigned int encoding1, encoding2;
30511 unsigned int align1, align2;
30512 if (dx->die_mark > dy->die_mark)
30513 return -1;
30514 if (dx->die_mark < dy->die_mark)
30515 return 1;
30516 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30517 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30518 if (byte_size1 < byte_size2)
30519 return 1;
30520 if (byte_size1 > byte_size2)
30521 return -1;
30522 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30523 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30524 if (encoding1 < encoding2)
30525 return 1;
30526 if (encoding1 > encoding2)
30527 return -1;
30528 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30529 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30530 if (align1 < align2)
30531 return 1;
30532 if (align1 > align2)
30533 return -1;
30534 return 0;
30537 /* Move base types marked by mark_base_types as early as possible
30538 in the CU, sorted by decreasing usage count both to make the
30539 uleb128 references as small as possible and to make sure they
30540 will have die_offset already computed by calc_die_sizes when
30541 sizes of typed stack loc ops is computed. */
30543 static void
30544 move_marked_base_types (void)
30546 unsigned int i;
30547 dw_die_ref base_type, die, c;
30549 if (base_types.is_empty ())
30550 return;
30552 /* Sort by decreasing usage count, they will be added again in that
30553 order later on. */
30554 base_types.qsort (base_type_cmp);
30555 die = comp_unit_die ();
30556 c = die->die_child;
30559 dw_die_ref prev = c;
30560 c = c->die_sib;
30561 while (c->die_mark)
30563 remove_child_with_prev (c, prev);
30564 /* As base types got marked, there must be at least
30565 one node other than DW_TAG_base_type. */
30566 gcc_assert (die->die_child != NULL);
30567 c = prev->die_sib;
30570 while (c != die->die_child);
30571 gcc_assert (die->die_child);
30572 c = die->die_child;
30573 for (i = 0; base_types.iterate (i, &base_type); i++)
30575 base_type->die_mark = 0;
30576 base_type->die_sib = c->die_sib;
30577 c->die_sib = base_type;
30578 c = base_type;
30582 /* Helper function for resolve_addr, attempt to resolve
30583 one CONST_STRING, return true if successful. Similarly verify that
30584 SYMBOL_REFs refer to variables emitted in the current CU. */
30586 static bool
30587 resolve_one_addr (rtx *addr)
30589 rtx rtl = *addr;
30591 if (GET_CODE (rtl) == CONST_STRING)
30593 size_t len = strlen (XSTR (rtl, 0)) + 1;
30594 tree t = build_string (len, XSTR (rtl, 0));
30595 tree tlen = size_int (len - 1);
30596 TREE_TYPE (t)
30597 = build_array_type (char_type_node, build_index_type (tlen));
30598 rtl = lookup_constant_def (t);
30599 if (!rtl || !MEM_P (rtl))
30600 return false;
30601 rtl = XEXP (rtl, 0);
30602 if (GET_CODE (rtl) == SYMBOL_REF
30603 && SYMBOL_REF_DECL (rtl)
30604 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30605 return false;
30606 vec_safe_push (used_rtx_array, rtl);
30607 *addr = rtl;
30608 return true;
30611 if (GET_CODE (rtl) == SYMBOL_REF
30612 && SYMBOL_REF_DECL (rtl))
30614 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30616 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30617 return false;
30619 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30620 return false;
30623 if (GET_CODE (rtl) == CONST)
30625 subrtx_ptr_iterator::array_type array;
30626 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30627 if (!resolve_one_addr (*iter))
30628 return false;
30631 return true;
30634 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30635 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30636 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30638 static rtx
30639 string_cst_pool_decl (tree t)
30641 rtx rtl = output_constant_def (t, 1);
30642 unsigned char *array;
30643 dw_loc_descr_ref l;
30644 tree decl;
30645 size_t len;
30646 dw_die_ref ref;
30648 if (!rtl || !MEM_P (rtl))
30649 return NULL_RTX;
30650 rtl = XEXP (rtl, 0);
30651 if (GET_CODE (rtl) != SYMBOL_REF
30652 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30653 return NULL_RTX;
30655 decl = SYMBOL_REF_DECL (rtl);
30656 if (!lookup_decl_die (decl))
30658 len = TREE_STRING_LENGTH (t);
30659 vec_safe_push (used_rtx_array, rtl);
30660 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30661 array = ggc_vec_alloc<unsigned char> (len);
30662 memcpy (array, TREE_STRING_POINTER (t), len);
30663 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30664 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30665 l->dw_loc_oprnd2.v.val_vec.length = len;
30666 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30667 l->dw_loc_oprnd2.v.val_vec.array = array;
30668 add_AT_loc (ref, DW_AT_location, l);
30669 equate_decl_number_to_die (decl, ref);
30671 return rtl;
30674 /* Helper function of resolve_addr_in_expr. LOC is
30675 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30676 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30677 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30678 with DW_OP_implicit_pointer if possible
30679 and return true, if unsuccessful, return false. */
30681 static bool
30682 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30684 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30685 HOST_WIDE_INT offset = 0;
30686 dw_die_ref ref = NULL;
30687 tree decl;
30689 if (GET_CODE (rtl) == CONST
30690 && GET_CODE (XEXP (rtl, 0)) == PLUS
30691 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30693 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30694 rtl = XEXP (XEXP (rtl, 0), 0);
30696 if (GET_CODE (rtl) == CONST_STRING)
30698 size_t len = strlen (XSTR (rtl, 0)) + 1;
30699 tree t = build_string (len, XSTR (rtl, 0));
30700 tree tlen = size_int (len - 1);
30702 TREE_TYPE (t)
30703 = build_array_type (char_type_node, build_index_type (tlen));
30704 rtl = string_cst_pool_decl (t);
30705 if (!rtl)
30706 return false;
30708 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30710 decl = SYMBOL_REF_DECL (rtl);
30711 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30713 ref = lookup_decl_die (decl);
30714 if (ref && (get_AT (ref, DW_AT_location)
30715 || get_AT (ref, DW_AT_const_value)))
30717 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30718 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30719 loc->dw_loc_oprnd1.val_entry = NULL;
30720 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30721 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30722 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30723 loc->dw_loc_oprnd2.v.val_int = offset;
30724 return true;
30728 return false;
30731 /* Helper function for resolve_addr, handle one location
30732 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30733 the location list couldn't be resolved. */
30735 static bool
30736 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30738 dw_loc_descr_ref keep = NULL;
30739 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30740 switch (loc->dw_loc_opc)
30742 case DW_OP_addr:
30743 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30745 if ((prev == NULL
30746 || prev->dw_loc_opc == DW_OP_piece
30747 || prev->dw_loc_opc == DW_OP_bit_piece)
30748 && loc->dw_loc_next
30749 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30750 && (!dwarf_strict || dwarf_version >= 5)
30751 && optimize_one_addr_into_implicit_ptr (loc))
30752 break;
30753 return false;
30755 break;
30756 case DW_OP_GNU_addr_index:
30757 case DW_OP_addrx:
30758 case DW_OP_GNU_const_index:
30759 case DW_OP_constx:
30760 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30761 || loc->dw_loc_opc == DW_OP_addrx)
30762 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30763 || loc->dw_loc_opc == DW_OP_constx)
30764 && loc->dtprel))
30766 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30767 if (!resolve_one_addr (&rtl))
30768 return false;
30769 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30770 loc->dw_loc_oprnd1.val_entry
30771 = add_addr_table_entry (rtl, ate_kind_rtx);
30773 break;
30774 case DW_OP_const4u:
30775 case DW_OP_const8u:
30776 if (loc->dtprel
30777 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30778 return false;
30779 break;
30780 case DW_OP_plus_uconst:
30781 if (size_of_loc_descr (loc)
30782 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30784 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30786 dw_loc_descr_ref repl
30787 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30788 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30789 add_loc_descr (&repl, loc->dw_loc_next);
30790 *loc = *repl;
30792 break;
30793 case DW_OP_implicit_value:
30794 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30795 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30796 return false;
30797 break;
30798 case DW_OP_implicit_pointer:
30799 case DW_OP_GNU_implicit_pointer:
30800 case DW_OP_GNU_parameter_ref:
30801 case DW_OP_GNU_variable_value:
30802 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30804 dw_die_ref ref
30805 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30806 if (ref == NULL)
30807 return false;
30808 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30809 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30810 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30812 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30814 if (prev == NULL
30815 && loc->dw_loc_next == NULL
30816 && AT_class (a) == dw_val_class_loc)
30817 switch (a->dw_attr)
30819 /* Following attributes allow both exprloc and reference,
30820 so if the whole expression is DW_OP_GNU_variable_value
30821 alone we could transform it into reference. */
30822 case DW_AT_byte_size:
30823 case DW_AT_bit_size:
30824 case DW_AT_lower_bound:
30825 case DW_AT_upper_bound:
30826 case DW_AT_bit_stride:
30827 case DW_AT_count:
30828 case DW_AT_allocated:
30829 case DW_AT_associated:
30830 case DW_AT_byte_stride:
30831 a->dw_attr_val.val_class = dw_val_class_die_ref;
30832 a->dw_attr_val.val_entry = NULL;
30833 a->dw_attr_val.v.val_die_ref.die
30834 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30835 a->dw_attr_val.v.val_die_ref.external = 0;
30836 return true;
30837 default:
30838 break;
30840 if (dwarf_strict)
30841 return false;
30843 break;
30844 case DW_OP_const_type:
30845 case DW_OP_regval_type:
30846 case DW_OP_deref_type:
30847 case DW_OP_convert:
30848 case DW_OP_reinterpret:
30849 case DW_OP_GNU_const_type:
30850 case DW_OP_GNU_regval_type:
30851 case DW_OP_GNU_deref_type:
30852 case DW_OP_GNU_convert:
30853 case DW_OP_GNU_reinterpret:
30854 while (loc->dw_loc_next
30855 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30856 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30858 dw_die_ref base1, base2;
30859 unsigned enc1, enc2, size1, size2;
30860 if (loc->dw_loc_opc == DW_OP_regval_type
30861 || loc->dw_loc_opc == DW_OP_deref_type
30862 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30863 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30864 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30865 else if (loc->dw_loc_oprnd1.val_class
30866 == dw_val_class_unsigned_const)
30867 break;
30868 else
30869 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30870 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30871 == dw_val_class_unsigned_const)
30872 break;
30873 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30874 gcc_assert (base1->die_tag == DW_TAG_base_type
30875 && base2->die_tag == DW_TAG_base_type);
30876 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30877 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30878 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30879 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30880 if (size1 == size2
30881 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30882 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30883 && loc != keep)
30884 || enc1 == enc2))
30886 /* Optimize away next DW_OP_convert after
30887 adjusting LOC's base type die reference. */
30888 if (loc->dw_loc_opc == DW_OP_regval_type
30889 || loc->dw_loc_opc == DW_OP_deref_type
30890 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30891 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30892 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30893 else
30894 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30895 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30896 continue;
30898 /* Don't change integer DW_OP_convert after e.g. floating
30899 point typed stack entry. */
30900 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30901 keep = loc->dw_loc_next;
30902 break;
30904 break;
30905 default:
30906 break;
30908 return true;
30911 /* Helper function of resolve_addr. DIE had DW_AT_location of
30912 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30913 and DW_OP_addr couldn't be resolved. resolve_addr has already
30914 removed the DW_AT_location attribute. This function attempts to
30915 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30916 to it or DW_AT_const_value attribute, if possible. */
30918 static void
30919 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30921 if (!VAR_P (decl)
30922 || lookup_decl_die (decl) != die
30923 || DECL_EXTERNAL (decl)
30924 || !TREE_STATIC (decl)
30925 || DECL_INITIAL (decl) == NULL_TREE
30926 || DECL_P (DECL_INITIAL (decl))
30927 || get_AT (die, DW_AT_const_value))
30928 return;
30930 tree init = DECL_INITIAL (decl);
30931 HOST_WIDE_INT offset = 0;
30932 /* For variables that have been optimized away and thus
30933 don't have a memory location, see if we can emit
30934 DW_AT_const_value instead. */
30935 if (tree_add_const_value_attribute (die, init))
30936 return;
30937 if (dwarf_strict && dwarf_version < 5)
30938 return;
30939 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30940 and ADDR_EXPR refers to a decl that has DW_AT_location or
30941 DW_AT_const_value (but isn't addressable, otherwise
30942 resolving the original DW_OP_addr wouldn't fail), see if
30943 we can add DW_OP_implicit_pointer. */
30944 STRIP_NOPS (init);
30945 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30946 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30948 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30949 init = TREE_OPERAND (init, 0);
30950 STRIP_NOPS (init);
30952 if (TREE_CODE (init) != ADDR_EXPR)
30953 return;
30954 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30955 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30956 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30957 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30958 && TREE_OPERAND (init, 0) != decl))
30960 dw_die_ref ref;
30961 dw_loc_descr_ref l;
30963 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30965 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30966 if (!rtl)
30967 return;
30968 decl = SYMBOL_REF_DECL (rtl);
30970 else
30971 decl = TREE_OPERAND (init, 0);
30972 ref = lookup_decl_die (decl);
30973 if (ref == NULL
30974 || (!get_AT (ref, DW_AT_location)
30975 && !get_AT (ref, DW_AT_const_value)))
30976 return;
30977 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30978 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30979 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30980 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30981 add_AT_loc (die, DW_AT_location, l);
30985 /* Return NULL if l is a DWARF expression, or first op that is not
30986 valid DWARF expression. */
30988 static dw_loc_descr_ref
30989 non_dwarf_expression (dw_loc_descr_ref l)
30991 while (l)
30993 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30994 return l;
30995 switch (l->dw_loc_opc)
30997 case DW_OP_regx:
30998 case DW_OP_implicit_value:
30999 case DW_OP_stack_value:
31000 case DW_OP_implicit_pointer:
31001 case DW_OP_GNU_implicit_pointer:
31002 case DW_OP_GNU_parameter_ref:
31003 case DW_OP_piece:
31004 case DW_OP_bit_piece:
31005 return l;
31006 default:
31007 break;
31009 l = l->dw_loc_next;
31011 return NULL;
31014 /* Return adjusted copy of EXPR:
31015 If it is empty DWARF expression, return it.
31016 If it is valid non-empty DWARF expression,
31017 return copy of EXPR with DW_OP_deref appended to it.
31018 If it is DWARF expression followed by DW_OP_reg{N,x}, return
31019 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
31020 If it is DWARF expression followed by DW_OP_stack_value, return
31021 copy of the DWARF expression without anything appended.
31022 Otherwise, return NULL. */
31024 static dw_loc_descr_ref
31025 copy_deref_exprloc (dw_loc_descr_ref expr)
31027 dw_loc_descr_ref tail = NULL;
31029 if (expr == NULL)
31030 return NULL;
31032 dw_loc_descr_ref l = non_dwarf_expression (expr);
31033 if (l && l->dw_loc_next)
31034 return NULL;
31036 if (l)
31038 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
31039 tail = new_loc_descr ((enum dwarf_location_atom)
31040 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
31041 0, 0);
31042 else
31043 switch (l->dw_loc_opc)
31045 case DW_OP_regx:
31046 tail = new_loc_descr (DW_OP_bregx,
31047 l->dw_loc_oprnd1.v.val_unsigned, 0);
31048 break;
31049 case DW_OP_stack_value:
31050 break;
31051 default:
31052 return NULL;
31055 else
31056 tail = new_loc_descr (DW_OP_deref, 0, 0);
31058 dw_loc_descr_ref ret = NULL, *p = &ret;
31059 while (expr != l)
31061 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
31062 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
31063 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
31064 p = &(*p)->dw_loc_next;
31065 expr = expr->dw_loc_next;
31067 *p = tail;
31068 return ret;
31071 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
31072 reference to a variable or argument, adjust it if needed and return:
31073 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
31074 attribute if present should be removed
31075 0 keep the attribute perhaps with minor modifications, no need to rescan
31076 1 if the attribute has been successfully adjusted. */
31078 static int
31079 optimize_string_length (dw_attr_node *a)
31081 dw_loc_descr_ref l = AT_loc (a), lv;
31082 dw_die_ref die;
31083 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31085 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
31086 die = lookup_decl_die (decl);
31087 if (die)
31089 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31090 l->dw_loc_oprnd1.v.val_die_ref.die = die;
31091 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
31093 else
31094 return -1;
31096 else
31097 die = l->dw_loc_oprnd1.v.val_die_ref.die;
31099 /* DWARF5 allows reference class, so we can then reference the DIE.
31100 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
31101 if (l->dw_loc_next != NULL && dwarf_version >= 5)
31103 a->dw_attr_val.val_class = dw_val_class_die_ref;
31104 a->dw_attr_val.val_entry = NULL;
31105 a->dw_attr_val.v.val_die_ref.die = die;
31106 a->dw_attr_val.v.val_die_ref.external = 0;
31107 return 0;
31110 dw_attr_node *av = get_AT (die, DW_AT_location);
31111 dw_loc_list_ref d;
31112 bool non_dwarf_expr = false;
31114 if (av == NULL)
31115 return dwarf_strict ? -1 : 0;
31116 switch (AT_class (av))
31118 case dw_val_class_loc_list:
31119 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31120 if (d->expr && non_dwarf_expression (d->expr))
31121 non_dwarf_expr = true;
31122 break;
31123 case dw_val_class_view_list:
31124 gcc_unreachable ();
31125 case dw_val_class_loc:
31126 lv = AT_loc (av);
31127 if (lv == NULL)
31128 return dwarf_strict ? -1 : 0;
31129 if (non_dwarf_expression (lv))
31130 non_dwarf_expr = true;
31131 break;
31132 default:
31133 return dwarf_strict ? -1 : 0;
31136 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
31137 into DW_OP_call4 or DW_OP_GNU_variable_value into
31138 DW_OP_call4 DW_OP_deref, do so. */
31139 if (!non_dwarf_expr
31140 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
31142 l->dw_loc_opc = DW_OP_call4;
31143 if (l->dw_loc_next)
31144 l->dw_loc_next = NULL;
31145 else
31146 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
31147 return 0;
31150 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
31151 copy over the DW_AT_location attribute from die to a. */
31152 if (l->dw_loc_next != NULL)
31154 a->dw_attr_val = av->dw_attr_val;
31155 return 1;
31158 dw_loc_list_ref list, *p;
31159 switch (AT_class (av))
31161 case dw_val_class_loc_list:
31162 p = &list;
31163 list = NULL;
31164 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31166 lv = copy_deref_exprloc (d->expr);
31167 if (lv)
31169 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31170 p = &(*p)->dw_loc_next;
31172 else if (!dwarf_strict && d->expr)
31173 return 0;
31175 if (list == NULL)
31176 return dwarf_strict ? -1 : 0;
31177 a->dw_attr_val.val_class = dw_val_class_loc_list;
31178 gen_llsym (list);
31179 *AT_loc_list_ptr (a) = list;
31180 return 1;
31181 case dw_val_class_loc:
31182 lv = copy_deref_exprloc (AT_loc (av));
31183 if (lv == NULL)
31184 return dwarf_strict ? -1 : 0;
31185 a->dw_attr_val.v.val_loc = lv;
31186 return 1;
31187 default:
31188 gcc_unreachable ();
31192 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31193 an address in .rodata section if the string literal is emitted there,
31194 or remove the containing location list or replace DW_AT_const_value
31195 with DW_AT_location and empty location expression, if it isn't found
31196 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31197 to something that has been emitted in the current CU. */
31199 static void
31200 resolve_addr (dw_die_ref die)
31202 dw_die_ref c;
31203 dw_attr_node *a;
31204 dw_loc_list_ref *curr, *start, loc;
31205 unsigned ix;
31206 bool remove_AT_byte_size = false;
31208 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31209 switch (AT_class (a))
31211 case dw_val_class_loc_list:
31212 start = curr = AT_loc_list_ptr (a);
31213 loc = *curr;
31214 gcc_assert (loc);
31215 /* The same list can be referenced more than once. See if we have
31216 already recorded the result from a previous pass. */
31217 if (loc->replaced)
31218 *curr = loc->dw_loc_next;
31219 else if (!loc->resolved_addr)
31221 /* As things stand, we do not expect or allow one die to
31222 reference a suffix of another die's location list chain.
31223 References must be identical or completely separate.
31224 There is therefore no need to cache the result of this
31225 pass on any list other than the first; doing so
31226 would lead to unnecessary writes. */
31227 while (*curr)
31229 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31230 if (!resolve_addr_in_expr (a, (*curr)->expr))
31232 dw_loc_list_ref next = (*curr)->dw_loc_next;
31233 dw_loc_descr_ref l = (*curr)->expr;
31235 if (next && (*curr)->ll_symbol)
31237 gcc_assert (!next->ll_symbol);
31238 next->ll_symbol = (*curr)->ll_symbol;
31239 next->vl_symbol = (*curr)->vl_symbol;
31241 if (dwarf_split_debug_info)
31242 remove_loc_list_addr_table_entries (l);
31243 *curr = next;
31245 else
31247 mark_base_types ((*curr)->expr);
31248 curr = &(*curr)->dw_loc_next;
31251 if (loc == *start)
31252 loc->resolved_addr = 1;
31253 else
31255 loc->replaced = 1;
31256 loc->dw_loc_next = *start;
31259 if (!*start)
31261 remove_AT (die, a->dw_attr);
31262 ix--;
31264 break;
31265 case dw_val_class_view_list:
31267 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31268 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31269 dw_val_node *llnode
31270 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31271 /* If we no longer have a loclist, or it no longer needs
31272 views, drop this attribute. */
31273 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31275 remove_AT (die, a->dw_attr);
31276 ix--;
31278 break;
31280 case dw_val_class_loc:
31282 dw_loc_descr_ref l = AT_loc (a);
31283 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31284 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31285 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31286 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31287 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31288 with DW_FORM_ref referencing the same DIE as
31289 DW_OP_GNU_variable_value used to reference. */
31290 if (a->dw_attr == DW_AT_string_length
31291 && l
31292 && l->dw_loc_opc == DW_OP_GNU_variable_value
31293 && (l->dw_loc_next == NULL
31294 || (l->dw_loc_next->dw_loc_next == NULL
31295 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31297 switch (optimize_string_length (a))
31299 case -1:
31300 remove_AT (die, a->dw_attr);
31301 ix--;
31302 /* If we drop DW_AT_string_length, we need to drop also
31303 DW_AT_{string_length_,}byte_size. */
31304 remove_AT_byte_size = true;
31305 continue;
31306 default:
31307 break;
31308 case 1:
31309 /* Even if we keep the optimized DW_AT_string_length,
31310 it might have changed AT_class, so process it again. */
31311 ix--;
31312 continue;
31315 /* For -gdwarf-2 don't attempt to optimize
31316 DW_AT_data_member_location containing
31317 DW_OP_plus_uconst - older consumers might
31318 rely on it being that op instead of a more complex,
31319 but shorter, location description. */
31320 if ((dwarf_version > 2
31321 || a->dw_attr != DW_AT_data_member_location
31322 || l == NULL
31323 || l->dw_loc_opc != DW_OP_plus_uconst
31324 || l->dw_loc_next != NULL)
31325 && !resolve_addr_in_expr (a, l))
31327 if (dwarf_split_debug_info)
31328 remove_loc_list_addr_table_entries (l);
31329 if (l != NULL
31330 && l->dw_loc_next == NULL
31331 && l->dw_loc_opc == DW_OP_addr
31332 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31333 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31334 && a->dw_attr == DW_AT_location)
31336 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31337 remove_AT (die, a->dw_attr);
31338 ix--;
31339 optimize_location_into_implicit_ptr (die, decl);
31340 break;
31342 if (a->dw_attr == DW_AT_string_length)
31343 /* If we drop DW_AT_string_length, we need to drop also
31344 DW_AT_{string_length_,}byte_size. */
31345 remove_AT_byte_size = true;
31346 remove_AT (die, a->dw_attr);
31347 ix--;
31349 else
31350 mark_base_types (l);
31352 break;
31353 case dw_val_class_addr:
31354 if (a->dw_attr == DW_AT_const_value
31355 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31357 if (AT_index (a) != NOT_INDEXED)
31358 remove_addr_table_entry (a->dw_attr_val.val_entry);
31359 remove_AT (die, a->dw_attr);
31360 ix--;
31362 if ((die->die_tag == DW_TAG_call_site
31363 && a->dw_attr == DW_AT_call_origin)
31364 || (die->die_tag == DW_TAG_GNU_call_site
31365 && a->dw_attr == DW_AT_abstract_origin))
31367 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31368 dw_die_ref tdie = lookup_decl_die (tdecl);
31369 dw_die_ref cdie;
31370 if (tdie == NULL
31371 && DECL_EXTERNAL (tdecl)
31372 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31373 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31375 dw_die_ref pdie = cdie;
31376 /* Make sure we don't add these DIEs into type units.
31377 We could emit skeleton DIEs for context (namespaces,
31378 outer structs/classes) and a skeleton DIE for the
31379 innermost context with DW_AT_signature pointing to the
31380 type unit. See PR78835. */
31381 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31382 pdie = pdie->die_parent;
31383 if (pdie == NULL)
31385 /* Creating a full DIE for tdecl is overly expensive and
31386 at this point even wrong when in the LTO phase
31387 as it can end up generating new type DIEs we didn't
31388 output and thus optimize_external_refs will crash. */
31389 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31390 add_AT_flag (tdie, DW_AT_external, 1);
31391 add_AT_flag (tdie, DW_AT_declaration, 1);
31392 add_linkage_attr (tdie, tdecl);
31393 add_name_and_src_coords_attributes (tdie, tdecl, true);
31394 equate_decl_number_to_die (tdecl, tdie);
31397 if (tdie)
31399 a->dw_attr_val.val_class = dw_val_class_die_ref;
31400 a->dw_attr_val.v.val_die_ref.die = tdie;
31401 a->dw_attr_val.v.val_die_ref.external = 0;
31403 else
31405 if (AT_index (a) != NOT_INDEXED)
31406 remove_addr_table_entry (a->dw_attr_val.val_entry);
31407 remove_AT (die, a->dw_attr);
31408 ix--;
31411 break;
31412 default:
31413 break;
31416 if (remove_AT_byte_size)
31417 remove_AT (die, dwarf_version >= 5
31418 ? DW_AT_string_length_byte_size
31419 : DW_AT_byte_size);
31421 FOR_EACH_CHILD (die, c, resolve_addr (c));
31424 /* Helper routines for optimize_location_lists.
31425 This pass tries to share identical local lists in .debug_loc
31426 section. */
31428 /* Iteratively hash operands of LOC opcode into HSTATE. */
31430 static void
31431 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31433 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31434 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31436 switch (loc->dw_loc_opc)
31438 case DW_OP_const4u:
31439 case DW_OP_const8u:
31440 if (loc->dtprel)
31441 goto hash_addr;
31442 /* FALLTHRU */
31443 case DW_OP_const1u:
31444 case DW_OP_const1s:
31445 case DW_OP_const2u:
31446 case DW_OP_const2s:
31447 case DW_OP_const4s:
31448 case DW_OP_const8s:
31449 case DW_OP_constu:
31450 case DW_OP_consts:
31451 case DW_OP_pick:
31452 case DW_OP_plus_uconst:
31453 case DW_OP_breg0:
31454 case DW_OP_breg1:
31455 case DW_OP_breg2:
31456 case DW_OP_breg3:
31457 case DW_OP_breg4:
31458 case DW_OP_breg5:
31459 case DW_OP_breg6:
31460 case DW_OP_breg7:
31461 case DW_OP_breg8:
31462 case DW_OP_breg9:
31463 case DW_OP_breg10:
31464 case DW_OP_breg11:
31465 case DW_OP_breg12:
31466 case DW_OP_breg13:
31467 case DW_OP_breg14:
31468 case DW_OP_breg15:
31469 case DW_OP_breg16:
31470 case DW_OP_breg17:
31471 case DW_OP_breg18:
31472 case DW_OP_breg19:
31473 case DW_OP_breg20:
31474 case DW_OP_breg21:
31475 case DW_OP_breg22:
31476 case DW_OP_breg23:
31477 case DW_OP_breg24:
31478 case DW_OP_breg25:
31479 case DW_OP_breg26:
31480 case DW_OP_breg27:
31481 case DW_OP_breg28:
31482 case DW_OP_breg29:
31483 case DW_OP_breg30:
31484 case DW_OP_breg31:
31485 case DW_OP_regx:
31486 case DW_OP_fbreg:
31487 case DW_OP_piece:
31488 case DW_OP_deref_size:
31489 case DW_OP_xderef_size:
31490 hstate.add_object (val1->v.val_int);
31491 break;
31492 case DW_OP_skip:
31493 case DW_OP_bra:
31495 int offset;
31497 gcc_assert (val1->val_class == dw_val_class_loc);
31498 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31499 hstate.add_object (offset);
31501 break;
31502 case DW_OP_implicit_value:
31503 hstate.add_object (val1->v.val_unsigned);
31504 switch (val2->val_class)
31506 case dw_val_class_const:
31507 hstate.add_object (val2->v.val_int);
31508 break;
31509 case dw_val_class_vec:
31511 unsigned int elt_size = val2->v.val_vec.elt_size;
31512 unsigned int len = val2->v.val_vec.length;
31514 hstate.add_int (elt_size);
31515 hstate.add_int (len);
31516 hstate.add (val2->v.val_vec.array, len * elt_size);
31518 break;
31519 case dw_val_class_const_double:
31520 hstate.add_object (val2->v.val_double.low);
31521 hstate.add_object (val2->v.val_double.high);
31522 break;
31523 case dw_val_class_wide_int:
31524 hstate.add (val2->v.val_wide->get_val (),
31525 get_full_len (*val2->v.val_wide)
31526 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31527 break;
31528 case dw_val_class_addr:
31529 inchash::add_rtx (val2->v.val_addr, hstate);
31530 break;
31531 default:
31532 gcc_unreachable ();
31534 break;
31535 case DW_OP_bregx:
31536 case DW_OP_bit_piece:
31537 hstate.add_object (val1->v.val_int);
31538 hstate.add_object (val2->v.val_int);
31539 break;
31540 case DW_OP_addr:
31541 hash_addr:
31542 if (loc->dtprel)
31544 unsigned char dtprel = 0xd1;
31545 hstate.add_object (dtprel);
31547 inchash::add_rtx (val1->v.val_addr, hstate);
31548 break;
31549 case DW_OP_GNU_addr_index:
31550 case DW_OP_addrx:
31551 case DW_OP_GNU_const_index:
31552 case DW_OP_constx:
31554 if (loc->dtprel)
31556 unsigned char dtprel = 0xd1;
31557 hstate.add_object (dtprel);
31559 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31561 break;
31562 case DW_OP_implicit_pointer:
31563 case DW_OP_GNU_implicit_pointer:
31564 hstate.add_int (val2->v.val_int);
31565 break;
31566 case DW_OP_entry_value:
31567 case DW_OP_GNU_entry_value:
31568 hstate.add_object (val1->v.val_loc);
31569 break;
31570 case DW_OP_regval_type:
31571 case DW_OP_deref_type:
31572 case DW_OP_GNU_regval_type:
31573 case DW_OP_GNU_deref_type:
31575 unsigned int byte_size
31576 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31577 unsigned int encoding
31578 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31579 hstate.add_object (val1->v.val_int);
31580 hstate.add_object (byte_size);
31581 hstate.add_object (encoding);
31583 break;
31584 case DW_OP_convert:
31585 case DW_OP_reinterpret:
31586 case DW_OP_GNU_convert:
31587 case DW_OP_GNU_reinterpret:
31588 if (val1->val_class == dw_val_class_unsigned_const)
31590 hstate.add_object (val1->v.val_unsigned);
31591 break;
31593 /* FALLTHRU */
31594 case DW_OP_const_type:
31595 case DW_OP_GNU_const_type:
31597 unsigned int byte_size
31598 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31599 unsigned int encoding
31600 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31601 hstate.add_object (byte_size);
31602 hstate.add_object (encoding);
31603 if (loc->dw_loc_opc != DW_OP_const_type
31604 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31605 break;
31606 hstate.add_object (val2->val_class);
31607 switch (val2->val_class)
31609 case dw_val_class_const:
31610 hstate.add_object (val2->v.val_int);
31611 break;
31612 case dw_val_class_vec:
31614 unsigned int elt_size = val2->v.val_vec.elt_size;
31615 unsigned int len = val2->v.val_vec.length;
31617 hstate.add_object (elt_size);
31618 hstate.add_object (len);
31619 hstate.add (val2->v.val_vec.array, len * elt_size);
31621 break;
31622 case dw_val_class_const_double:
31623 hstate.add_object (val2->v.val_double.low);
31624 hstate.add_object (val2->v.val_double.high);
31625 break;
31626 case dw_val_class_wide_int:
31627 hstate.add (val2->v.val_wide->get_val (),
31628 get_full_len (*val2->v.val_wide)
31629 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31630 break;
31631 default:
31632 gcc_unreachable ();
31635 break;
31637 default:
31638 /* Other codes have no operands. */
31639 break;
31643 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31645 static inline void
31646 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31648 dw_loc_descr_ref l;
31649 bool sizes_computed = false;
31650 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31651 size_of_locs (loc);
31653 for (l = loc; l != NULL; l = l->dw_loc_next)
31655 enum dwarf_location_atom opc = l->dw_loc_opc;
31656 hstate.add_object (opc);
31657 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31659 size_of_locs (loc);
31660 sizes_computed = true;
31662 hash_loc_operands (l, hstate);
31666 /* Compute hash of the whole location list LIST_HEAD. */
31668 static inline void
31669 hash_loc_list (dw_loc_list_ref list_head)
31671 dw_loc_list_ref curr = list_head;
31672 inchash::hash hstate;
31674 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31676 hstate.add (curr->begin, strlen (curr->begin) + 1);
31677 hstate.add (curr->end, strlen (curr->end) + 1);
31678 hstate.add_object (curr->vbegin);
31679 hstate.add_object (curr->vend);
31680 if (curr->section)
31681 hstate.add (curr->section, strlen (curr->section) + 1);
31682 hash_locs (curr->expr, hstate);
31684 list_head->hash = hstate.end ();
31687 /* Return true if X and Y opcodes have the same operands. */
31689 static inline bool
31690 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31692 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31693 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31694 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31695 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31697 switch (x->dw_loc_opc)
31699 case DW_OP_const4u:
31700 case DW_OP_const8u:
31701 if (x->dtprel)
31702 goto hash_addr;
31703 /* FALLTHRU */
31704 case DW_OP_const1u:
31705 case DW_OP_const1s:
31706 case DW_OP_const2u:
31707 case DW_OP_const2s:
31708 case DW_OP_const4s:
31709 case DW_OP_const8s:
31710 case DW_OP_constu:
31711 case DW_OP_consts:
31712 case DW_OP_pick:
31713 case DW_OP_plus_uconst:
31714 case DW_OP_breg0:
31715 case DW_OP_breg1:
31716 case DW_OP_breg2:
31717 case DW_OP_breg3:
31718 case DW_OP_breg4:
31719 case DW_OP_breg5:
31720 case DW_OP_breg6:
31721 case DW_OP_breg7:
31722 case DW_OP_breg8:
31723 case DW_OP_breg9:
31724 case DW_OP_breg10:
31725 case DW_OP_breg11:
31726 case DW_OP_breg12:
31727 case DW_OP_breg13:
31728 case DW_OP_breg14:
31729 case DW_OP_breg15:
31730 case DW_OP_breg16:
31731 case DW_OP_breg17:
31732 case DW_OP_breg18:
31733 case DW_OP_breg19:
31734 case DW_OP_breg20:
31735 case DW_OP_breg21:
31736 case DW_OP_breg22:
31737 case DW_OP_breg23:
31738 case DW_OP_breg24:
31739 case DW_OP_breg25:
31740 case DW_OP_breg26:
31741 case DW_OP_breg27:
31742 case DW_OP_breg28:
31743 case DW_OP_breg29:
31744 case DW_OP_breg30:
31745 case DW_OP_breg31:
31746 case DW_OP_regx:
31747 case DW_OP_fbreg:
31748 case DW_OP_piece:
31749 case DW_OP_deref_size:
31750 case DW_OP_xderef_size:
31751 return valx1->v.val_int == valy1->v.val_int;
31752 case DW_OP_skip:
31753 case DW_OP_bra:
31754 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31755 can cause irrelevant differences in dw_loc_addr. */
31756 gcc_assert (valx1->val_class == dw_val_class_loc
31757 && valy1->val_class == dw_val_class_loc
31758 && (dwarf_split_debug_info
31759 || x->dw_loc_addr == y->dw_loc_addr));
31760 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31761 case DW_OP_implicit_value:
31762 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31763 || valx2->val_class != valy2->val_class)
31764 return false;
31765 switch (valx2->val_class)
31767 case dw_val_class_const:
31768 return valx2->v.val_int == valy2->v.val_int;
31769 case dw_val_class_vec:
31770 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31771 && valx2->v.val_vec.length == valy2->v.val_vec.length
31772 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31773 valx2->v.val_vec.elt_size
31774 * valx2->v.val_vec.length) == 0;
31775 case dw_val_class_const_double:
31776 return valx2->v.val_double.low == valy2->v.val_double.low
31777 && valx2->v.val_double.high == valy2->v.val_double.high;
31778 case dw_val_class_wide_int:
31779 return *valx2->v.val_wide == *valy2->v.val_wide;
31780 case dw_val_class_addr:
31781 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31782 default:
31783 gcc_unreachable ();
31785 case DW_OP_bregx:
31786 case DW_OP_bit_piece:
31787 return valx1->v.val_int == valy1->v.val_int
31788 && valx2->v.val_int == valy2->v.val_int;
31789 case DW_OP_addr:
31790 hash_addr:
31791 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31792 case DW_OP_GNU_addr_index:
31793 case DW_OP_addrx:
31794 case DW_OP_GNU_const_index:
31795 case DW_OP_constx:
31797 rtx ax1 = valx1->val_entry->addr.rtl;
31798 rtx ay1 = valy1->val_entry->addr.rtl;
31799 return rtx_equal_p (ax1, ay1);
31801 case DW_OP_implicit_pointer:
31802 case DW_OP_GNU_implicit_pointer:
31803 return valx1->val_class == dw_val_class_die_ref
31804 && valx1->val_class == valy1->val_class
31805 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31806 && valx2->v.val_int == valy2->v.val_int;
31807 case DW_OP_entry_value:
31808 case DW_OP_GNU_entry_value:
31809 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31810 case DW_OP_const_type:
31811 case DW_OP_GNU_const_type:
31812 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31813 || valx2->val_class != valy2->val_class)
31814 return false;
31815 switch (valx2->val_class)
31817 case dw_val_class_const:
31818 return valx2->v.val_int == valy2->v.val_int;
31819 case dw_val_class_vec:
31820 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31821 && valx2->v.val_vec.length == valy2->v.val_vec.length
31822 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31823 valx2->v.val_vec.elt_size
31824 * valx2->v.val_vec.length) == 0;
31825 case dw_val_class_const_double:
31826 return valx2->v.val_double.low == valy2->v.val_double.low
31827 && valx2->v.val_double.high == valy2->v.val_double.high;
31828 case dw_val_class_wide_int:
31829 return *valx2->v.val_wide == *valy2->v.val_wide;
31830 default:
31831 gcc_unreachable ();
31833 case DW_OP_regval_type:
31834 case DW_OP_deref_type:
31835 case DW_OP_GNU_regval_type:
31836 case DW_OP_GNU_deref_type:
31837 return valx1->v.val_int == valy1->v.val_int
31838 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31839 case DW_OP_convert:
31840 case DW_OP_reinterpret:
31841 case DW_OP_GNU_convert:
31842 case DW_OP_GNU_reinterpret:
31843 if (valx1->val_class != valy1->val_class)
31844 return false;
31845 if (valx1->val_class == dw_val_class_unsigned_const)
31846 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31847 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31848 case DW_OP_GNU_parameter_ref:
31849 return valx1->val_class == dw_val_class_die_ref
31850 && valx1->val_class == valy1->val_class
31851 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31852 default:
31853 /* Other codes have no operands. */
31854 return true;
31858 /* Return true if DWARF location expressions X and Y are the same. */
31860 static inline bool
31861 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31863 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31864 if (x->dw_loc_opc != y->dw_loc_opc
31865 || x->dtprel != y->dtprel
31866 || !compare_loc_operands (x, y))
31867 break;
31868 return x == NULL && y == NULL;
31871 /* Hashtable helpers. */
31873 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31875 static inline hashval_t hash (const dw_loc_list_struct *);
31876 static inline bool equal (const dw_loc_list_struct *,
31877 const dw_loc_list_struct *);
31880 /* Return precomputed hash of location list X. */
31882 inline hashval_t
31883 loc_list_hasher::hash (const dw_loc_list_struct *x)
31885 return x->hash;
31888 /* Return true if location lists A and B are the same. */
31890 inline bool
31891 loc_list_hasher::equal (const dw_loc_list_struct *a,
31892 const dw_loc_list_struct *b)
31894 if (a == b)
31895 return 1;
31896 if (a->hash != b->hash)
31897 return 0;
31898 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31899 if (strcmp (a->begin, b->begin) != 0
31900 || strcmp (a->end, b->end) != 0
31901 || (a->section == NULL) != (b->section == NULL)
31902 || (a->section && strcmp (a->section, b->section) != 0)
31903 || a->vbegin != b->vbegin || a->vend != b->vend
31904 || !compare_locs (a->expr, b->expr))
31905 break;
31906 return a == NULL && b == NULL;
31909 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31912 /* Recursively optimize location lists referenced from DIE
31913 children and share them whenever possible. */
31915 static void
31916 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31918 dw_die_ref c;
31919 dw_attr_node *a;
31920 unsigned ix;
31921 dw_loc_list_struct **slot;
31922 bool drop_locviews = false;
31923 bool has_locviews = false;
31925 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31926 if (AT_class (a) == dw_val_class_loc_list)
31928 dw_loc_list_ref list = AT_loc_list (a);
31929 /* TODO: perform some optimizations here, before hashing
31930 it and storing into the hash table. */
31931 hash_loc_list (list);
31932 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31933 if (*slot == NULL)
31935 *slot = list;
31936 if (loc_list_has_views (list))
31937 gcc_assert (list->vl_symbol);
31938 else if (list->vl_symbol)
31940 drop_locviews = true;
31941 list->vl_symbol = NULL;
31944 else
31946 if (list->vl_symbol && !(*slot)->vl_symbol)
31947 drop_locviews = true;
31948 a->dw_attr_val.v.val_loc_list = *slot;
31951 else if (AT_class (a) == dw_val_class_view_list)
31953 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31954 has_locviews = true;
31958 if (drop_locviews && has_locviews)
31959 remove_AT (die, DW_AT_GNU_locviews);
31961 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31965 /* Recursively assign each location list a unique index into the debug_addr
31966 section. */
31968 static void
31969 index_location_lists (dw_die_ref die)
31971 dw_die_ref c;
31972 dw_attr_node *a;
31973 unsigned ix;
31975 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31976 if (AT_class (a) == dw_val_class_loc_list)
31978 dw_loc_list_ref list = AT_loc_list (a);
31979 dw_loc_list_ref curr;
31980 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31982 /* Don't index an entry that has already been indexed
31983 or won't be output. Make sure skip_loc_list_entry doesn't
31984 call size_of_locs, because that might cause circular dependency,
31985 index_location_lists requiring address table indexes to be
31986 computed, but adding new indexes through add_addr_table_entry
31987 and address table index computation requiring no new additions
31988 to the hash table. In the rare case of DWARF[234] >= 64KB
31989 location expression, we'll just waste unused address table entry
31990 for it. */
31991 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31992 continue;
31994 curr->begin_entry
31995 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31996 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31997 curr->end_entry
31998 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
32002 FOR_EACH_CHILD (die, c, index_location_lists (c));
32005 /* Optimize location lists referenced from DIE
32006 children and share them whenever possible. */
32008 static void
32009 optimize_location_lists (dw_die_ref die)
32011 loc_list_hash_type htab (500);
32012 optimize_location_lists_1 (die, &htab);
32015 /* Traverse the limbo die list, and add parent/child links. The only
32016 dies without parents that should be here are concrete instances of
32017 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
32018 For concrete instances, we can get the parent die from the abstract
32019 instance. */
32021 static void
32022 flush_limbo_die_list (void)
32024 limbo_die_node *node;
32026 /* get_context_die calls force_decl_die, which can put new DIEs on the
32027 limbo list in LTO mode when nested functions are put in a different
32028 partition than that of their parent function. */
32029 while ((node = limbo_die_list))
32031 dw_die_ref die = node->die;
32032 limbo_die_list = node->next;
32034 if (die->die_parent == NULL)
32036 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
32038 if (origin && origin->die_parent)
32039 add_child_die (origin->die_parent, die);
32040 else if (is_cu_die (die))
32042 else if (seen_error ())
32043 /* It's OK to be confused by errors in the input. */
32044 add_child_die (comp_unit_die (), die);
32045 else
32047 /* In certain situations, the lexical block containing a
32048 nested function can be optimized away, which results
32049 in the nested function die being orphaned. Likewise
32050 with the return type of that nested function. Force
32051 this to be a child of the containing function.
32053 It may happen that even the containing function got fully
32054 inlined and optimized out. In that case we are lost and
32055 assign the empty child. This should not be big issue as
32056 the function is likely unreachable too. */
32057 gcc_assert (node->created_for);
32059 if (DECL_P (node->created_for))
32060 origin = get_context_die (DECL_CONTEXT (node->created_for));
32061 else if (TYPE_P (node->created_for))
32062 origin = scope_die_for (node->created_for, comp_unit_die ());
32063 else
32064 origin = comp_unit_die ();
32066 add_child_die (origin, die);
32072 /* Reset DIEs so we can output them again. */
32074 static void
32075 reset_dies (dw_die_ref die)
32077 dw_die_ref c;
32079 /* Remove stuff we re-generate. */
32080 die->die_mark = 0;
32081 die->die_offset = 0;
32082 die->die_abbrev = 0;
32083 remove_AT (die, DW_AT_sibling);
32085 FOR_EACH_CHILD (die, c, reset_dies (c));
32088 /* reset_indirect_string removed the references coming from DW_AT_name
32089 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
32090 .debug_line_str strings again. */
32092 static void
32093 adjust_name_comp_dir (dw_die_ref die)
32095 for (int i = 0; i < 2; i++)
32097 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
32098 dw_attr_node *a = get_AT (die, attr_kind);
32099 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
32100 continue;
32102 if (!debug_line_str_hash)
32103 debug_line_str_hash
32104 = hash_table<indirect_string_hasher>::create_ggc (10);
32106 struct indirect_string_node *node
32107 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
32108 debug_line_str_hash);
32109 set_indirect_string (node);
32110 node->form = DW_FORM_line_strp;
32111 a->dw_attr_val.v.val_str = node;
32115 /* Output stuff that dwarf requires at the end of every file,
32116 and generate the DWARF-2 debugging info. */
32118 static void
32119 dwarf2out_finish (const char *filename)
32121 comdat_type_node *ctnode;
32122 dw_die_ref main_comp_unit_die;
32123 unsigned char checksum[16];
32124 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32126 /* Generate CTF/BTF debug info. */
32127 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32128 || btf_debuginfo_p ()) && lang_GNU_C ())
32129 ctf_debug_finish (filename);
32131 /* Skip emitting DWARF if not required. */
32132 if (!dwarf_debuginfo_p ())
32133 return;
32135 /* Flush out any latecomers to the limbo party. */
32136 flush_limbo_die_list ();
32138 if (inline_entry_data_table)
32139 gcc_assert (inline_entry_data_table->is_empty ());
32141 if (flag_checking)
32143 verify_die (comp_unit_die ());
32144 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32145 verify_die (node->die);
32148 /* We shouldn't have any symbols with delayed asm names for
32149 DIEs generated after early finish. */
32150 gcc_assert (deferred_asm_name == NULL);
32152 gen_remaining_tmpl_value_param_die_attribute ();
32154 if (flag_generate_lto || flag_generate_offload)
32156 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
32158 /* Prune stuff so that dwarf2out_finish runs successfully
32159 for the fat part of the object. */
32160 reset_dies (comp_unit_die ());
32161 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32162 reset_dies (node->die);
32164 hash_table<comdat_type_hasher> comdat_type_table (100);
32165 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32167 comdat_type_node **slot
32168 = comdat_type_table.find_slot (ctnode, INSERT);
32170 /* Don't reset types twice. */
32171 if (*slot != HTAB_EMPTY_ENTRY)
32172 continue;
32174 /* Remove the pointer to the line table. */
32175 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32177 if (debug_info_level >= DINFO_LEVEL_TERSE)
32178 reset_dies (ctnode->root_die);
32180 *slot = ctnode;
32183 /* Reset die CU symbol so we don't output it twice. */
32184 comp_unit_die ()->die_id.die_symbol = NULL;
32186 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32187 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32188 if (have_macinfo)
32189 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32191 /* Remove indirect string decisions. */
32192 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32193 if (debug_line_str_hash)
32195 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32196 debug_line_str_hash = NULL;
32197 if (asm_outputs_debug_line_str ())
32199 adjust_name_comp_dir (comp_unit_die ());
32200 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32201 adjust_name_comp_dir (node->die);
32206 #if ENABLE_ASSERT_CHECKING
32208 dw_die_ref die = comp_unit_die (), c;
32209 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32211 #endif
32212 base_types.truncate (0);
32213 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32214 resolve_addr (ctnode->root_die);
32215 resolve_addr (comp_unit_die ());
32216 move_marked_base_types ();
32218 if (dump_file)
32220 fprintf (dump_file, "DWARF for %s\n", filename);
32221 print_die (comp_unit_die (), dump_file);
32224 /* Initialize sections and labels used for actual assembler output. */
32225 unsigned generation = init_sections_and_labels (false);
32227 /* Traverse the DIE's and add sibling attributes to those DIE's that
32228 have children. */
32229 add_sibling_attributes (comp_unit_die ());
32230 limbo_die_node *node;
32231 for (node = cu_die_list; node; node = node->next)
32232 add_sibling_attributes (node->die);
32233 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32234 add_sibling_attributes (ctnode->root_die);
32236 /* When splitting DWARF info, we put some attributes in the
32237 skeleton compile_unit DIE that remains in the .o, while
32238 most attributes go in the DWO compile_unit_die. */
32239 if (dwarf_split_debug_info)
32241 limbo_die_node *cu;
32242 main_comp_unit_die = gen_compile_unit_die (NULL);
32243 if (dwarf_version >= 5)
32244 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32245 cu = limbo_die_list;
32246 gcc_assert (cu->die == main_comp_unit_die);
32247 limbo_die_list = limbo_die_list->next;
32248 cu->next = cu_die_list;
32249 cu_die_list = cu;
32251 else
32252 main_comp_unit_die = comp_unit_die ();
32254 /* Output a terminator label for the .text section. */
32255 switch_to_section (text_section);
32256 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32257 if (cold_text_section)
32259 switch_to_section (cold_text_section);
32260 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32263 /* We can only use the low/high_pc attributes if all of the code was
32264 in .text. */
32265 if ((!have_multiple_function_sections
32266 && vec_safe_length (switch_text_ranges) < 2)
32267 || (dwarf_version < 3 && dwarf_strict))
32269 const char *end_label = text_end_label;
32270 if (vec_safe_length (switch_text_ranges) == 1)
32271 end_label = (*switch_text_ranges)[0];
32272 /* Don't add if the CU has no associated code. */
32273 if (switch_text_ranges)
32274 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32275 end_label, true);
32277 else
32279 unsigned fde_idx;
32280 dw_fde_ref fde;
32281 bool range_list_added = false;
32282 if (switch_text_ranges)
32284 const char *prev_loc = text_section_label;
32285 const char *loc;
32286 unsigned idx;
32288 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32289 if (prev_loc)
32291 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32292 loc, &range_list_added, true);
32293 prev_loc = NULL;
32295 else
32296 prev_loc = loc;
32298 if (prev_loc)
32299 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32300 text_end_label, &range_list_added, true);
32303 if (switch_cold_ranges)
32305 const char *prev_loc = cold_text_section_label;
32306 const char *loc;
32307 unsigned idx;
32309 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32310 if (prev_loc)
32312 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32313 loc, &range_list_added, true);
32314 prev_loc = NULL;
32316 else
32317 prev_loc = loc;
32319 if (prev_loc)
32320 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32321 cold_end_label, &range_list_added, true);
32324 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32326 if (fde->ignored_debug)
32327 continue;
32328 if (!fde->in_std_section)
32329 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32330 fde->dw_fde_end, &range_list_added,
32331 true);
32332 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32333 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32334 fde->dw_fde_second_end, &range_list_added,
32335 true);
32338 if (range_list_added)
32340 /* We need to give .debug_loc and .debug_ranges an appropriate
32341 "base address". Use zero so that these addresses become
32342 absolute. Historically, we've emitted the unexpected
32343 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32344 Emit both to give time for other tools to adapt. */
32345 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32346 if (! dwarf_strict && dwarf_version < 4)
32347 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32349 add_ranges (NULL);
32350 have_multiple_function_sections = true;
32354 /* AIX Assembler inserts the length, so adjust the reference to match the
32355 offset expected by debuggers. */
32356 strcpy (dl_section_ref, debug_line_section_label);
32357 if (XCOFF_DEBUGGING_INFO)
32358 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32360 if (debug_info_level >= DINFO_LEVEL_TERSE)
32361 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32362 dl_section_ref);
32364 if (have_macinfo)
32365 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32366 macinfo_section_label);
32368 if (dwarf_split_debug_info)
32370 if (have_location_lists)
32372 /* Since we generate the loclists in the split DWARF .dwo
32373 file itself, we don't need to generate a loclists_base
32374 attribute for the split compile unit DIE. That attribute
32375 (and using relocatable sec_offset FORMs) isn't allowed
32376 for a split compile unit. Only if the .debug_loclists
32377 section was in the main file, would we need to generate a
32378 loclists_base attribute here (for the full or skeleton
32379 unit DIE). */
32381 /* optimize_location_lists calculates the size of the lists,
32382 so index them first, and assign indices to the entries.
32383 Although optimize_location_lists will remove entries from
32384 the table, it only does so for duplicates, and therefore
32385 only reduces ref_counts to 1. */
32386 index_location_lists (comp_unit_die ());
32389 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32390 index_rnglists ();
32392 if (addr_index_table != NULL)
32394 unsigned int index = 0;
32395 addr_index_table
32396 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32397 (&index);
32401 loc_list_idx = 0;
32402 if (have_location_lists)
32404 optimize_location_lists (comp_unit_die ());
32405 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32406 if (dwarf_version >= 5 && dwarf_split_debug_info)
32407 assign_location_list_indexes (comp_unit_die ());
32410 save_macinfo_strings ();
32412 if (dwarf_split_debug_info)
32414 unsigned int index = 0;
32416 /* Add attributes common to skeleton compile_units and
32417 type_units. Because these attributes include strings, it
32418 must be done before freezing the string table. Top-level
32419 skeleton die attrs are added when the skeleton type unit is
32420 created, so ensure it is created by this point. */
32421 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32422 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32425 /* Output all of the compilation units. We put the main one last so that
32426 the offsets are available to output_pubnames. */
32427 for (node = cu_die_list; node; node = node->next)
32428 output_comp_unit (node->die, 0, NULL);
32430 hash_table<comdat_type_hasher> comdat_type_table (100);
32431 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32433 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32435 /* Don't output duplicate types. */
32436 if (*slot != HTAB_EMPTY_ENTRY)
32437 continue;
32439 /* Add a pointer to the line table for the main compilation unit
32440 so that the debugger can make sense of DW_AT_decl_file
32441 attributes. */
32442 if (debug_info_level >= DINFO_LEVEL_TERSE)
32443 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32444 (!dwarf_split_debug_info
32445 ? dl_section_ref
32446 : debug_skeleton_line_section_label));
32448 output_comdat_type_unit (ctnode, false);
32449 *slot = ctnode;
32452 if (dwarf_split_debug_info)
32454 int mark;
32455 struct md5_ctx ctx;
32457 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32458 md5_init_ctx (&ctx);
32459 mark = 0;
32460 die_checksum (comp_unit_die (), &ctx, &mark);
32461 unmark_all_dies (comp_unit_die ());
32462 md5_finish_ctx (&ctx, checksum);
32464 if (dwarf_version < 5)
32466 /* Use the first 8 bytes of the checksum as the dwo_id,
32467 and add it to both comp-unit DIEs. */
32468 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32469 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32472 /* Add the base offset of the ranges table to the skeleton
32473 comp-unit DIE. */
32474 if (!vec_safe_is_empty (ranges_table))
32476 if (dwarf_version < 5)
32477 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32478 ranges_section_label);
32481 output_addr_table ();
32484 /* Output the main compilation unit if non-empty or if .debug_macinfo
32485 or .debug_macro will be emitted. */
32486 output_comp_unit (comp_unit_die (), have_macinfo,
32487 dwarf_split_debug_info ? checksum : NULL);
32489 if (dwarf_split_debug_info && info_section_emitted)
32490 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32492 /* Output the abbreviation table. */
32493 if (vec_safe_length (abbrev_die_table) != 1)
32495 switch_to_section (debug_abbrev_section);
32496 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32497 output_abbrev_section ();
32500 /* Output location list section if necessary. */
32501 if (have_location_lists)
32503 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32504 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32505 /* Output the location lists info. */
32506 switch_to_section (debug_loc_section);
32507 if (dwarf_version >= 5)
32509 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32510 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32511 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32512 dw2_asm_output_data (4, 0xffffffff,
32513 "Initial length escape value indicating "
32514 "64-bit DWARF extension");
32515 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32516 "Length of Location Lists");
32517 ASM_OUTPUT_LABEL (asm_out_file, l1);
32518 output_dwarf_version ();
32519 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32520 dw2_asm_output_data (1, 0, "Segment Size");
32521 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32522 "Offset Entry Count");
32524 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32525 if (dwarf_version >= 5 && dwarf_split_debug_info)
32527 unsigned int save_loc_list_idx = loc_list_idx;
32528 loc_list_idx = 0;
32529 output_loclists_offsets (comp_unit_die ());
32530 gcc_assert (save_loc_list_idx == loc_list_idx);
32532 output_location_lists (comp_unit_die ());
32533 if (dwarf_version >= 5)
32534 ASM_OUTPUT_LABEL (asm_out_file, l2);
32537 output_pubtables ();
32539 /* Output the address range information if a CU (.debug_info section)
32540 was emitted. We output an empty table even if we had no functions
32541 to put in it. This because the consumer has no way to tell the
32542 difference between an empty table that we omitted and failure to
32543 generate a table that would have contained data. */
32544 if (info_section_emitted)
32546 switch_to_section (debug_aranges_section);
32547 output_aranges ();
32550 /* Output ranges section if necessary. */
32551 if (!vec_safe_is_empty (ranges_table))
32553 if (dwarf_version >= 5)
32555 if (dwarf_split_debug_info)
32557 /* We don't know right now whether there are any
32558 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32559 Depending on into which of those two belongs the first
32560 ranges_table entry, emit that section first and that
32561 output_rnglists call will return true if the other kind of
32562 ranges needs to be emitted as well. */
32563 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32564 if (output_rnglists (generation, dwo))
32565 output_rnglists (generation, !dwo);
32567 else
32568 output_rnglists (generation, false);
32570 else
32571 output_ranges ();
32574 /* Have to end the macro section. */
32575 if (have_macinfo)
32577 switch_to_section (debug_macinfo_section);
32578 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32579 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32580 : debug_skeleton_line_section_label, false);
32581 dw2_asm_output_data (1, 0, "End compilation unit");
32584 /* Output the source line correspondence table. We must do this
32585 even if there is no line information. Otherwise, on an empty
32586 translation unit, we will generate a present, but empty,
32587 .debug_info section. IRIX 6.5 `nm' will then complain when
32588 examining the file. This is done late so that any filenames
32589 used by the debug_info section are marked as 'used'. */
32590 switch_to_section (debug_line_section);
32591 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32592 if (! output_asm_line_debug_info ())
32593 output_line_info (false);
32595 if (dwarf_split_debug_info && info_section_emitted)
32597 switch_to_section (debug_skeleton_line_section);
32598 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32599 output_line_info (true);
32602 /* If we emitted any indirect strings, output the string table too. */
32603 if (debug_str_hash || skeleton_debug_str_hash)
32604 output_indirect_strings ();
32605 if (debug_line_str_hash)
32607 switch_to_section (debug_line_str_section);
32608 const enum dwarf_form form = DW_FORM_line_strp;
32609 debug_line_str_hash->traverse<enum dwarf_form,
32610 output_indirect_string> (form);
32613 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32614 symview_upper_bound = 0;
32615 if (zero_view_p)
32616 bitmap_clear (zero_view_p);
32619 /* Returns a hash value for X (which really is a variable_value_struct). */
32621 inline hashval_t
32622 variable_value_hasher::hash (variable_value_struct *x)
32624 return (hashval_t) x->decl_id;
32627 /* Return nonzero if decl_id of variable_value_struct X is the same as
32628 UID of decl Y. */
32630 inline bool
32631 variable_value_hasher::equal (variable_value_struct *x, tree y)
32633 return x->decl_id == DECL_UID (y);
32636 /* Helper function for resolve_variable_value, handle
32637 DW_OP_GNU_variable_value in one location expression.
32638 Return true if exprloc has been changed into loclist. */
32640 static bool
32641 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32643 dw_loc_descr_ref next;
32644 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32646 next = loc->dw_loc_next;
32647 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32648 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32649 continue;
32651 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32652 if (DECL_CONTEXT (decl) != current_function_decl)
32653 continue;
32655 dw_die_ref ref = lookup_decl_die (decl);
32656 if (ref)
32658 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32659 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32660 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32661 continue;
32663 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32664 if (l == NULL)
32665 continue;
32666 if (l->dw_loc_next)
32668 if (AT_class (a) != dw_val_class_loc)
32669 continue;
32670 switch (a->dw_attr)
32672 /* Following attributes allow both exprloc and loclist
32673 classes, so we can change them into a loclist. */
32674 case DW_AT_location:
32675 case DW_AT_string_length:
32676 case DW_AT_return_addr:
32677 case DW_AT_data_member_location:
32678 case DW_AT_frame_base:
32679 case DW_AT_segment:
32680 case DW_AT_static_link:
32681 case DW_AT_use_location:
32682 case DW_AT_vtable_elem_location:
32683 if (prev)
32685 prev->dw_loc_next = NULL;
32686 prepend_loc_descr_to_each (l, AT_loc (a));
32688 if (next)
32689 add_loc_descr_to_each (l, next);
32690 a->dw_attr_val.val_class = dw_val_class_loc_list;
32691 a->dw_attr_val.val_entry = NULL;
32692 a->dw_attr_val.v.val_loc_list = l;
32693 have_location_lists = true;
32694 return true;
32695 /* Following attributes allow both exprloc and reference,
32696 so if the whole expression is DW_OP_GNU_variable_value alone
32697 we could transform it into reference. */
32698 case DW_AT_byte_size:
32699 case DW_AT_bit_size:
32700 case DW_AT_lower_bound:
32701 case DW_AT_upper_bound:
32702 case DW_AT_bit_stride:
32703 case DW_AT_count:
32704 case DW_AT_allocated:
32705 case DW_AT_associated:
32706 case DW_AT_byte_stride:
32707 if (prev == NULL && next == NULL)
32708 break;
32709 /* FALLTHRU */
32710 default:
32711 if (dwarf_strict)
32712 continue;
32713 break;
32715 /* Create DW_TAG_variable that we can refer to. */
32716 gen_decl_die (decl, NULL_TREE, NULL,
32717 lookup_decl_die (current_function_decl));
32718 ref = lookup_decl_die (decl);
32719 if (ref)
32721 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32722 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32723 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32725 continue;
32727 if (prev)
32729 prev->dw_loc_next = l->expr;
32730 add_loc_descr (&prev->dw_loc_next, next);
32731 free_loc_descr (loc, NULL);
32732 next = prev->dw_loc_next;
32734 else
32736 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32737 add_loc_descr (&loc, next);
32738 next = loc;
32740 loc = prev;
32742 return false;
32745 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32747 static void
32748 resolve_variable_value (dw_die_ref die)
32750 dw_attr_node *a;
32751 dw_loc_list_ref loc;
32752 unsigned ix;
32754 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32755 switch (AT_class (a))
32757 case dw_val_class_loc:
32758 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32759 break;
32760 /* FALLTHRU */
32761 case dw_val_class_loc_list:
32762 loc = AT_loc_list (a);
32763 gcc_assert (loc);
32764 for (; loc; loc = loc->dw_loc_next)
32765 resolve_variable_value_in_expr (a, loc->expr);
32766 break;
32767 default:
32768 break;
32772 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32773 temporaries in the current function. */
32775 static void
32776 resolve_variable_values (void)
32778 if (!variable_value_hash || !current_function_decl)
32779 return;
32781 struct variable_value_struct *node
32782 = variable_value_hash->find_with_hash (current_function_decl,
32783 DECL_UID (current_function_decl));
32785 if (node == NULL)
32786 return;
32788 unsigned int i;
32789 dw_die_ref die;
32790 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32791 resolve_variable_value (die);
32794 /* Helper function for note_variable_value, handle one location
32795 expression. */
32797 static void
32798 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32800 for (; loc; loc = loc->dw_loc_next)
32801 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32802 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32804 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32805 dw_die_ref ref = lookup_decl_die (decl);
32806 if (! ref && (flag_generate_lto || flag_generate_offload))
32808 /* ??? This is somewhat a hack because we do not create DIEs
32809 for variables not in BLOCK trees early but when generating
32810 early LTO output we need the dw_val_class_decl_ref to be
32811 fully resolved. For fat LTO objects we'd also like to
32812 undo this after LTO dwarf output. */
32813 gcc_assert (DECL_CONTEXT (decl));
32814 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32815 gcc_assert (ctx != NULL);
32816 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32817 ref = lookup_decl_die (decl);
32818 gcc_assert (ref != NULL);
32820 if (ref)
32822 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32823 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32824 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32825 continue;
32827 if (VAR_P (decl)
32828 && DECL_CONTEXT (decl)
32829 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32830 && lookup_decl_die (DECL_CONTEXT (decl)))
32832 if (!variable_value_hash)
32833 variable_value_hash
32834 = hash_table<variable_value_hasher>::create_ggc (10);
32836 tree fndecl = DECL_CONTEXT (decl);
32837 struct variable_value_struct *node;
32838 struct variable_value_struct **slot
32839 = variable_value_hash->find_slot_with_hash (fndecl,
32840 DECL_UID (fndecl),
32841 INSERT);
32842 if (*slot == NULL)
32844 node = ggc_cleared_alloc<variable_value_struct> ();
32845 node->decl_id = DECL_UID (fndecl);
32846 *slot = node;
32848 else
32849 node = *slot;
32851 vec_safe_push (node->dies, die);
32856 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32857 with dw_val_class_decl_ref operand. */
32859 static void
32860 note_variable_value (dw_die_ref die)
32862 dw_die_ref c;
32863 dw_attr_node *a;
32864 dw_loc_list_ref loc;
32865 unsigned ix;
32867 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32868 switch (AT_class (a))
32870 case dw_val_class_loc_list:
32871 loc = AT_loc_list (a);
32872 gcc_assert (loc);
32873 if (!loc->noted_variable_value)
32875 loc->noted_variable_value = 1;
32876 for (; loc; loc = loc->dw_loc_next)
32877 note_variable_value_in_expr (die, loc->expr);
32879 break;
32880 case dw_val_class_loc:
32881 note_variable_value_in_expr (die, AT_loc (a));
32882 break;
32883 default:
32884 break;
32887 /* Mark children. */
32888 FOR_EACH_CHILD (die, c, note_variable_value (c));
32891 /* Process DWARF dies for CTF generation. */
32893 static void
32894 ctf_debug_do_cu (dw_die_ref die)
32896 dw_die_ref c;
32898 if (!ctf_do_die (die))
32899 return;
32901 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32904 /* Perform any cleanups needed after the early debug generation pass
32905 has run. */
32907 static void
32908 dwarf2out_early_finish (const char *filename)
32910 comdat_type_node *ctnode;
32911 set_early_dwarf s;
32912 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32914 /* PCH might result in DW_AT_producer string being restored from the
32915 header compilation, so always fill it with empty string initially
32916 and overwrite only here. */
32917 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32919 if (dwarf_record_gcc_switches)
32920 producer_string = gen_producer_string (lang_hooks.name,
32921 save_decoded_options,
32922 save_decoded_options_count);
32923 else
32924 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32926 producer->dw_attr_val.v.val_str->refcount--;
32927 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32929 /* Add the name for the main input file now. We delayed this from
32930 dwarf2out_init to avoid complications with PCH. */
32931 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32932 add_comp_dir_attribute (comp_unit_die ());
32934 /* With LTO early dwarf was really finished at compile-time, so make
32935 sure to adjust the phase after annotating the LTRANS CU DIE. */
32936 if (in_lto_p)
32938 early_dwarf_finished = true;
32939 if (dump_file)
32941 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32942 print_die (comp_unit_die (), dump_file);
32944 return;
32947 /* Walk through the list of incomplete types again, trying once more to
32948 emit full debugging info for them. */
32949 retry_incomplete_types ();
32951 gen_scheduled_generic_parms_dies ();
32952 gen_remaining_tmpl_value_param_die_attribute ();
32954 /* The point here is to flush out the limbo list so that it is empty
32955 and we don't need to stream it for LTO. */
32956 flush_limbo_die_list ();
32958 /* Add DW_AT_linkage_name for all deferred DIEs. */
32959 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32961 tree decl = node->created_for;
32962 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32963 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32964 ended up in deferred_asm_name before we knew it was
32965 constant and never written to disk. */
32966 && DECL_ASSEMBLER_NAME (decl))
32968 add_linkage_attr (node->die, decl);
32969 move_linkage_attr (node->die);
32972 deferred_asm_name = NULL;
32974 if (flag_eliminate_unused_debug_types)
32975 prune_unused_types ();
32977 /* Generate separate COMDAT sections for type DIEs. */
32978 if (use_debug_types)
32980 break_out_comdat_types (comp_unit_die ());
32982 /* Each new type_unit DIE was added to the limbo die list when created.
32983 Since these have all been added to comdat_type_list, clear the
32984 limbo die list. */
32985 limbo_die_list = NULL;
32987 /* For each new comdat type unit, copy declarations for incomplete
32988 types to make the new unit self-contained (i.e., no direct
32989 references to the main compile unit). */
32990 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32991 copy_decls_for_unworthy_types (ctnode->root_die);
32992 copy_decls_for_unworthy_types (comp_unit_die ());
32994 /* In the process of copying declarations from one unit to another,
32995 we may have left some declarations behind that are no longer
32996 referenced. Prune them. */
32997 prune_unused_types ();
33000 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
33001 with dw_val_class_decl_ref operand. */
33002 note_variable_value (comp_unit_die ());
33003 for (limbo_die_node *node = cu_die_list; node; node = node->next)
33004 note_variable_value (node->die);
33005 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33006 note_variable_value (ctnode->root_die);
33007 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33008 note_variable_value (node->die);
33010 /* The AT_pubnames attribute needs to go in all skeleton dies, including
33011 both the main_cu and all skeleton TUs. Making this call unconditional
33012 would end up either adding a second copy of the AT_pubnames attribute, or
33013 requiring a special case in add_top_level_skeleton_die_attrs. */
33014 if (!dwarf_split_debug_info)
33015 add_AT_pubnames (comp_unit_die ());
33017 /* The early debug phase is now finished. */
33018 early_dwarf_finished = true;
33019 if (dump_file)
33021 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
33022 print_die (comp_unit_die (), dump_file);
33025 /* Generate CTF/BTF debug info. */
33026 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
33027 || btf_debuginfo_p ()) && lang_GNU_C ())
33029 ctf_debug_init ();
33030 ctf_debug_do_cu (comp_unit_die ());
33031 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33032 ctf_debug_do_cu (node->die);
33033 /* Post process the debug data in the CTF container if necessary. */
33034 ctf_debug_init_postprocess (btf_debuginfo_p ());
33036 ctf_debug_early_finish (filename);
33039 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
33040 if ((!flag_generate_lto && !flag_generate_offload)
33041 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
33042 copy_lto_debug_sections operation of the simple object support in
33043 libiberty is not implemented for them yet. */
33044 || TARGET_PECOFF || TARGET_COFF)
33045 return;
33047 /* Now as we are going to output for LTO initialize sections and labels
33048 to the LTO variants. We don't need a random-seed postfix as other
33049 LTO sections as linking the LTO debug sections into one in a partial
33050 link is fine. */
33051 init_sections_and_labels (true);
33053 /* The output below is modeled after dwarf2out_finish with all
33054 location related output removed and some LTO specific changes.
33055 Some refactoring might make both smaller and easier to match up. */
33057 base_types.truncate (0);
33058 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33059 mark_base_types (ctnode->root_die);
33060 mark_base_types (comp_unit_die ());
33061 move_marked_base_types ();
33063 /* Traverse the DIE's and add sibling attributes to those DIE's
33064 that have children. */
33065 add_sibling_attributes (comp_unit_die ());
33066 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33067 add_sibling_attributes (node->die);
33068 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33069 add_sibling_attributes (ctnode->root_die);
33071 /* AIX Assembler inserts the length, so adjust the reference to match the
33072 offset expected by debuggers. */
33073 strcpy (dl_section_ref, debug_line_section_label);
33074 if (XCOFF_DEBUGGING_INFO)
33075 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
33077 if (debug_info_level >= DINFO_LEVEL_TERSE)
33078 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
33080 if (have_macinfo)
33081 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
33082 macinfo_section_label);
33084 save_macinfo_strings ();
33086 if (dwarf_split_debug_info)
33088 unsigned int index = 0;
33089 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
33092 /* Output all of the compilation units. We put the main one last so that
33093 the offsets are available to output_pubnames. */
33094 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
33095 output_comp_unit (node->die, 0, NULL);
33097 hash_table<comdat_type_hasher> comdat_type_table (100);
33098 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
33100 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
33102 /* Don't output duplicate types. */
33103 if (*slot != HTAB_EMPTY_ENTRY)
33104 continue;
33106 /* Add a pointer to the line table for the main compilation unit
33107 so that the debugger can make sense of DW_AT_decl_file
33108 attributes. */
33109 if (debug_info_level >= DINFO_LEVEL_TERSE)
33110 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
33111 (!dwarf_split_debug_info
33112 ? debug_line_section_label
33113 : debug_skeleton_line_section_label));
33115 output_comdat_type_unit (ctnode, true);
33116 *slot = ctnode;
33119 /* Stick a unique symbol to the main debuginfo section. */
33120 compute_comp_unit_symbol (comp_unit_die ());
33122 /* Output the main compilation unit. We always need it if only for
33123 the CU symbol. */
33124 output_comp_unit (comp_unit_die (), true, NULL);
33126 /* Output the abbreviation table. */
33127 if (vec_safe_length (abbrev_die_table) != 1)
33129 switch_to_section (debug_abbrev_section);
33130 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
33131 output_abbrev_section ();
33134 /* Have to end the macro section. */
33135 if (have_macinfo)
33137 /* We have to save macinfo state if we need to output it again
33138 for the FAT part of the object. */
33139 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
33140 if (flag_fat_lto_objects)
33141 macinfo_table = macinfo_table->copy ();
33143 switch_to_section (debug_macinfo_section);
33144 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
33145 output_macinfo (debug_line_section_label, true);
33146 dw2_asm_output_data (1, 0, "End compilation unit");
33148 if (flag_fat_lto_objects)
33150 vec_free (macinfo_table);
33151 macinfo_table = saved_macinfo_table;
33155 /* Emit a skeleton debug_line section. */
33156 switch_to_section (debug_line_section);
33157 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
33158 output_line_info (true);
33160 /* If we emitted any indirect strings, output the string table too. */
33161 if (debug_str_hash || skeleton_debug_str_hash)
33162 output_indirect_strings ();
33163 if (debug_line_str_hash)
33165 switch_to_section (debug_line_str_section);
33166 const enum dwarf_form form = DW_FORM_line_strp;
33167 debug_line_str_hash->traverse<enum dwarf_form,
33168 output_indirect_string> (form);
33171 /* Switch back to the text section. */
33172 switch_to_section (text_section);
33175 /* Reset all state within dwarf2out.cc so that we can rerun the compiler
33176 within the same process. For use by toplev::finalize. */
33178 void
33179 dwarf2out_cc_finalize (void)
33181 last_var_location_insn = NULL;
33182 cached_next_real_insn = NULL;
33183 used_rtx_array = NULL;
33184 incomplete_types = NULL;
33185 debug_info_section = NULL;
33186 debug_skeleton_info_section = NULL;
33187 debug_abbrev_section = NULL;
33188 debug_skeleton_abbrev_section = NULL;
33189 debug_aranges_section = NULL;
33190 debug_addr_section = NULL;
33191 debug_macinfo_section = NULL;
33192 debug_line_section = NULL;
33193 debug_skeleton_line_section = NULL;
33194 debug_loc_section = NULL;
33195 debug_pubnames_section = NULL;
33196 debug_pubtypes_section = NULL;
33197 debug_str_section = NULL;
33198 debug_line_str_section = NULL;
33199 debug_str_dwo_section = NULL;
33200 debug_str_offsets_section = NULL;
33201 debug_ranges_section = NULL;
33202 debug_ranges_dwo_section = NULL;
33203 debug_frame_section = NULL;
33204 fde_vec = NULL;
33205 debug_str_hash = NULL;
33206 debug_line_str_hash = NULL;
33207 skeleton_debug_str_hash = NULL;
33208 dw2_string_counter = 0;
33209 have_multiple_function_sections = false;
33210 in_text_section_p = false;
33211 cold_text_section = NULL;
33212 last_text_label = NULL;
33213 last_cold_label = NULL;
33214 switch_text_ranges = NULL;
33215 switch_cold_ranges = NULL;
33216 current_unit_personality = NULL;
33218 early_dwarf = false;
33219 early_dwarf_finished = false;
33221 next_die_offset = 0;
33222 single_comp_unit_die = NULL;
33223 comdat_type_list = NULL;
33224 limbo_die_list = NULL;
33225 file_table = NULL;
33226 decl_die_table = NULL;
33227 common_block_die_table = NULL;
33228 decl_loc_table = NULL;
33229 call_arg_locations = NULL;
33230 call_arg_loc_last = NULL;
33231 call_site_count = -1;
33232 tail_call_site_count = -1;
33233 cached_dw_loc_list_table = NULL;
33234 abbrev_die_table = NULL;
33235 delete dwarf_proc_stack_usage_map;
33236 dwarf_proc_stack_usage_map = NULL;
33237 line_info_label_num = 0;
33238 cur_line_info_table = NULL;
33239 text_section_line_info = NULL;
33240 cold_text_section_line_info = NULL;
33241 separate_line_info = NULL;
33242 info_section_emitted = false;
33243 pubname_table = NULL;
33244 pubtype_table = NULL;
33245 macinfo_table = NULL;
33246 ranges_table = NULL;
33247 ranges_by_label = NULL;
33248 rnglist_idx = 0;
33249 have_location_lists = false;
33250 loclabel_num = 0;
33251 poc_label_num = 0;
33252 last_emitted_file = NULL;
33253 label_num = 0;
33254 tmpl_value_parm_die_table = NULL;
33255 generic_type_instances = NULL;
33256 frame_pointer_fb_offset = 0;
33257 frame_pointer_fb_offset_valid = false;
33258 base_types.release ();
33259 XDELETEVEC (producer_string);
33260 producer_string = NULL;
33261 output_line_info_generation = 0;
33262 init_sections_and_labels_generation = 0;
33265 #include "gt-dwarf2out.h"