testsuite: powerpc/vec_reve_1.c requires VSX.
[official-gcc.git] / gcc / dwarf2out.c
blobe36ef56a9829b76e5e1ab529845f508f2d6bd984
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2ctf.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tree-pretty-print.h"
87 #include "print-rtl.h"
88 #include "debug.h"
89 #include "common/common-target.h"
90 #include "langhooks.h"
91 #include "lra.h"
92 #include "dumpfile.h"
93 #include "opts.h"
94 #include "tree-dfa.h"
95 #include "gdb/gdb-index.h"
96 #include "rtl-iter.h"
97 #include "stringpool.h"
98 #include "attribs.h"
99 #include "file-prefix-map.h" /* remap_debug_filename() */
101 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
102 int, bool);
103 static rtx_insn *last_var_location_insn;
104 static rtx_insn *cached_next_real_insn;
105 static void dwarf2out_decl (tree);
106 static bool is_redundant_typedef (const_tree);
108 #ifndef XCOFF_DEBUGGING_INFO
109 #define XCOFF_DEBUGGING_INFO 0
110 #endif
112 #ifndef HAVE_XCOFF_DWARF_EXTRAS
113 #define HAVE_XCOFF_DWARF_EXTRAS 0
114 #endif
116 #ifdef VMS_DEBUGGING_INFO
117 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
119 /* Define this macro to be a nonzero value if the directory specifications
120 which are output in the debug info should end with a separator. */
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
122 /* Define this macro to evaluate to a nonzero value if GCC should refrain
123 from generating indirect strings in DWARF2 debug information, for instance
124 if your target is stuck with an old version of GDB that is unable to
125 process them properly or uses VMS Debug. */
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
127 #else
128 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
129 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
130 #endif
132 /* ??? Poison these here until it can be done generically. They've been
133 totally replaced in this file; make sure it stays that way. */
134 #undef DWARF2_UNWIND_INFO
135 #undef DWARF2_FRAME_INFO
136 #if (GCC_VERSION >= 3000)
137 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
138 #endif
140 /* The size of the target's pointer type. */
141 #ifndef PTR_SIZE
142 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
143 #endif
145 /* Array of RTXes referenced by the debugging information, which therefore
146 must be kept around forever. */
147 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
149 /* A pointer to the base of a list of incomplete types which might be
150 completed at some later time. incomplete_types_list needs to be a
151 vec<tree, va_gc> *because we want to tell the garbage collector about
152 it. */
153 static GTY(()) vec<tree, va_gc> *incomplete_types;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static const char *debug_macinfo_section_name;
164 static unsigned macinfo_label_base = 1;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_line_str_section;
172 static GTY(()) section *debug_str_dwo_section;
173 static GTY(()) section *debug_str_offsets_section;
174 static GTY(()) section *debug_ranges_section;
175 static GTY(()) section *debug_ranges_dwo_section;
176 static GTY(()) section *debug_frame_section;
178 /* Maximum size (in bytes) of an artificially generated label. */
179 #define MAX_ARTIFICIAL_LABEL_BYTES 40
181 /* According to the (draft) DWARF 3 specification, the initial length
182 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
183 bytes are 0xffffffff, followed by the length stored in the next 8
184 bytes.
186 However, the SGI/MIPS ABI uses an initial length which is equal to
187 dwarf_offset_size. It is defined (elsewhere) accordingly. */
189 #ifndef DWARF_INITIAL_LENGTH_SIZE
190 #define DWARF_INITIAL_LENGTH_SIZE (dwarf_offset_size == 4 ? 4 : 12)
191 #endif
193 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
194 #define DWARF_INITIAL_LENGTH_SIZE_STR (dwarf_offset_size == 4 ? "-4" : "-12")
195 #endif
197 /* Round SIZE up to the nearest BOUNDARY. */
198 #define DWARF_ROUND(SIZE,BOUNDARY) \
199 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
201 /* CIE identifier. */
202 #if HOST_BITS_PER_WIDE_INT >= 64
203 #define DWARF_CIE_ID \
204 (unsigned HOST_WIDE_INT) (dwarf_offset_size == 4 ? DW_CIE_ID : DW64_CIE_ID)
205 #else
206 #define DWARF_CIE_ID DW_CIE_ID
207 #endif
210 /* A vector for a table that contains frame description
211 information for each routine. */
212 #define NOT_INDEXED (-1U)
213 #define NO_INDEX_ASSIGNED (-2U)
215 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
217 struct GTY((for_user)) indirect_string_node {
218 const char *str;
219 unsigned int refcount;
220 enum dwarf_form form;
221 char *label;
222 unsigned int index;
225 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
227 typedef const char *compare_type;
229 static hashval_t hash (indirect_string_node *);
230 static bool equal (indirect_string_node *, const char *);
233 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
235 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
237 /* With split_debug_info, both the comp_dir and dwo_name go in the
238 main object file, rather than the dwo, similar to the force_direct
239 parameter elsewhere but with additional complications:
241 1) The string is needed in both the main object file and the dwo.
242 That is, the comp_dir and dwo_name will appear in both places.
244 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
245 DW_FORM_line_strp or DW_FORM_strx/GNU_str_index.
247 3) GCC chooses the form to use late, depending on the size and
248 reference count.
250 Rather than forcing the all debug string handling functions and
251 callers to deal with these complications, simply use a separate,
252 special-cased string table for any attribute that should go in the
253 main object file. This limits the complexity to just the places
254 that need it. */
256 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
258 static GTY(()) int dw2_string_counter;
260 /* True if the compilation unit places functions in more than one section. */
261 static GTY(()) bool have_multiple_function_sections = false;
263 /* The default cold text section. */
264 static GTY(()) section *cold_text_section;
266 /* True if currently in text section. */
267 static GTY(()) bool in_text_section_p = false;
269 /* Last debug-on location in corresponding section. */
270 static GTY(()) const char *last_text_label;
271 static GTY(()) const char *last_cold_label;
273 /* Mark debug-on/off locations per section.
274 NULL means the section is not used at all. */
275 static GTY(()) vec<const char *, va_gc> *switch_text_ranges;
276 static GTY(()) vec<const char *, va_gc> *switch_cold_ranges;
278 /* The DIE for C++14 'auto' in a function return type. */
279 static GTY(()) dw_die_ref auto_die;
281 /* The DIE for C++14 'decltype(auto)' in a function return type. */
282 static GTY(()) dw_die_ref decltype_auto_die;
284 /* Forward declarations for functions defined in this file. */
286 static void output_call_frame_info (int);
288 /* Personality decl of current unit. Used only when assembler does not support
289 personality CFI. */
290 static GTY(()) rtx current_unit_personality;
292 /* Whether an eh_frame section is required. */
293 static GTY(()) bool do_eh_frame = false;
295 /* .debug_rnglists next index. */
296 static unsigned int rnglist_idx;
298 /* Data and reference forms for relocatable data. */
299 #define DW_FORM_data (dwarf_offset_size == 8 ? DW_FORM_data8 : DW_FORM_data4)
300 #define DW_FORM_ref (dwarf_offset_size == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302 #ifndef DEBUG_FRAME_SECTION
303 #define DEBUG_FRAME_SECTION ".debug_frame"
304 #endif
306 #ifndef FUNC_BEGIN_LABEL
307 #define FUNC_BEGIN_LABEL "LFB"
308 #endif
310 #ifndef FUNC_SECOND_SECT_LABEL
311 #define FUNC_SECOND_SECT_LABEL "LFSB"
312 #endif
314 #ifndef FUNC_END_LABEL
315 #define FUNC_END_LABEL "LFE"
316 #endif
318 #ifndef PROLOGUE_END_LABEL
319 #define PROLOGUE_END_LABEL "LPE"
320 #endif
322 #ifndef EPILOGUE_BEGIN_LABEL
323 #define EPILOGUE_BEGIN_LABEL "LEB"
324 #endif
326 #ifndef FRAME_BEGIN_LABEL
327 #define FRAME_BEGIN_LABEL "Lframe"
328 #endif
329 #define CIE_AFTER_SIZE_LABEL "LSCIE"
330 #define CIE_END_LABEL "LECIE"
331 #define FDE_LABEL "LSFDE"
332 #define FDE_AFTER_SIZE_LABEL "LASFDE"
333 #define FDE_END_LABEL "LEFDE"
334 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
335 #define LINE_NUMBER_END_LABEL "LELT"
336 #define LN_PROLOG_AS_LABEL "LASLTP"
337 #define LN_PROLOG_END_LABEL "LELTP"
338 #define DIE_LABEL_PREFIX "DW"
340 /* Match the base name of a file to the base name of a compilation unit. */
342 static int
343 matches_main_base (const char *path)
345 /* Cache the last query. */
346 static const char *last_path = NULL;
347 static int last_match = 0;
348 if (path != last_path)
350 const char *base;
351 int length = base_of_path (path, &base);
352 last_path = path;
353 last_match = (length == main_input_baselength
354 && memcmp (base, main_input_basename, length) == 0);
356 return last_match;
359 #ifdef DEBUG_DEBUG_STRUCT
361 static int
362 dump_struct_debug (tree type, enum debug_info_usage usage,
363 enum debug_struct_file criterion, int generic,
364 int matches, int result)
366 /* Find the type name. */
367 tree type_decl = TYPE_STUB_DECL (type);
368 tree t = type_decl;
369 const char *name = 0;
370 if (TREE_CODE (t) == TYPE_DECL)
371 t = DECL_NAME (t);
372 if (t)
373 name = IDENTIFIER_POINTER (t);
375 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
376 criterion,
377 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
378 matches ? "bas" : "hdr",
379 generic ? "gen" : "ord",
380 usage == DINFO_USAGE_DFN ? ";" :
381 usage == DINFO_USAGE_DIR_USE ? "." : "*",
382 result,
383 (void*) type_decl, name);
384 return result;
386 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
387 dump_struct_debug (type, usage, criterion, generic, matches, result)
389 #else
391 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
392 (result)
394 #endif
396 /* Get the number of HOST_WIDE_INTs needed to represent the precision
397 of the number. */
399 static unsigned int
400 get_full_len (const wide_int &op)
402 int prec = wi::get_precision (op);
403 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
404 / HOST_BITS_PER_WIDE_INT);
407 static bool
408 should_emit_struct_debug (tree type, enum debug_info_usage usage)
410 if (debug_info_level <= DINFO_LEVEL_TERSE)
411 return false;
413 enum debug_struct_file criterion;
414 tree type_decl;
415 bool generic = lang_hooks.types.generic_p (type);
417 if (generic)
418 criterion = debug_struct_generic[usage];
419 else
420 criterion = debug_struct_ordinary[usage];
422 if (criterion == DINFO_STRUCT_FILE_NONE)
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
424 if (criterion == DINFO_STRUCT_FILE_ANY)
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
427 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
429 if (type_decl != NULL)
431 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
432 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
434 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
435 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
438 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
441 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
442 switch to the data section instead, and write out a synthetic start label
443 for collect2 the first time around. */
445 static void
446 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
448 if (eh_frame_section == 0)
450 int flags;
452 if (EH_TABLES_CAN_BE_READ_ONLY)
454 int fde_encoding;
455 int per_encoding;
456 int lsda_encoding;
458 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
459 /*global=*/0);
460 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
461 /*global=*/1);
462 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
463 /*global=*/0);
464 flags = ((! flag_pic
465 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
466 && (fde_encoding & 0x70) != DW_EH_PE_aligned
467 && (per_encoding & 0x70) != DW_EH_PE_absptr
468 && (per_encoding & 0x70) != DW_EH_PE_aligned
469 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
470 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
471 ? 0 : SECTION_WRITE);
473 else
474 flags = SECTION_WRITE;
476 #ifdef EH_FRAME_SECTION_NAME
477 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
478 #else
479 eh_frame_section = ((flags == SECTION_WRITE)
480 ? data_section : readonly_data_section);
481 #endif /* EH_FRAME_SECTION_NAME */
484 switch_to_section (eh_frame_section);
486 #ifdef EH_FRAME_THROUGH_COLLECT2
487 /* We have no special eh_frame section. Emit special labels to guide
488 collect2. */
489 if (!back)
491 tree label = get_file_function_name ("F");
492 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
493 targetm.asm_out.globalize_label (asm_out_file,
494 IDENTIFIER_POINTER (label));
495 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
497 #endif
500 /* Switch [BACK] to the eh or debug frame table section, depending on
501 FOR_EH. */
503 static void
504 switch_to_frame_table_section (int for_eh, bool back)
506 if (for_eh)
507 switch_to_eh_frame_section (back);
508 else
510 if (!debug_frame_section)
511 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
512 SECTION_DEBUG, NULL);
513 switch_to_section (debug_frame_section);
517 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
519 enum dw_cfi_oprnd_type
520 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
522 switch (cfi)
524 case DW_CFA_nop:
525 case DW_CFA_GNU_window_save:
526 case DW_CFA_remember_state:
527 case DW_CFA_restore_state:
528 return dw_cfi_oprnd_unused;
530 case DW_CFA_set_loc:
531 case DW_CFA_advance_loc1:
532 case DW_CFA_advance_loc2:
533 case DW_CFA_advance_loc4:
534 case DW_CFA_MIPS_advance_loc8:
535 return dw_cfi_oprnd_addr;
537 case DW_CFA_offset:
538 case DW_CFA_offset_extended:
539 case DW_CFA_def_cfa:
540 case DW_CFA_offset_extended_sf:
541 case DW_CFA_def_cfa_sf:
542 case DW_CFA_restore:
543 case DW_CFA_restore_extended:
544 case DW_CFA_undefined:
545 case DW_CFA_same_value:
546 case DW_CFA_def_cfa_register:
547 case DW_CFA_register:
548 case DW_CFA_expression:
549 case DW_CFA_val_expression:
550 return dw_cfi_oprnd_reg_num;
552 case DW_CFA_def_cfa_offset:
553 case DW_CFA_GNU_args_size:
554 case DW_CFA_def_cfa_offset_sf:
555 return dw_cfi_oprnd_offset;
557 case DW_CFA_def_cfa_expression:
558 return dw_cfi_oprnd_loc;
560 default:
561 gcc_unreachable ();
565 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
567 enum dw_cfi_oprnd_type
568 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
570 switch (cfi)
572 case DW_CFA_def_cfa:
573 case DW_CFA_def_cfa_sf:
574 case DW_CFA_offset:
575 case DW_CFA_offset_extended_sf:
576 case DW_CFA_offset_extended:
577 return dw_cfi_oprnd_offset;
579 case DW_CFA_register:
580 return dw_cfi_oprnd_reg_num;
582 case DW_CFA_expression:
583 case DW_CFA_val_expression:
584 return dw_cfi_oprnd_loc;
586 case DW_CFA_def_cfa_expression:
587 return dw_cfi_oprnd_cfa_loc;
589 default:
590 return dw_cfi_oprnd_unused;
594 /* Output one FDE. */
596 static void
597 output_fde (dw_fde_ref fde, bool for_eh, bool second,
598 char *section_start_label, int fde_encoding, char *augmentation,
599 bool any_lsda_needed, int lsda_encoding)
601 const char *begin, *end;
602 static unsigned int j;
603 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
605 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
606 /* empty */ 0);
607 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
608 for_eh + j);
609 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
610 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
611 if (!XCOFF_DEBUGGING_INFO || for_eh)
613 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
614 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
615 " indicating 64-bit DWARF extension");
616 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
617 "FDE Length");
619 ASM_OUTPUT_LABEL (asm_out_file, l1);
621 if (for_eh)
622 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
623 else
624 dw2_asm_output_offset (dwarf_offset_size, section_start_label,
625 debug_frame_section, "FDE CIE offset");
627 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
628 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
630 if (for_eh)
632 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
633 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
634 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
635 "FDE initial location");
636 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
637 end, begin, "FDE address range");
639 else
641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
645 if (augmentation[0])
647 if (any_lsda_needed)
649 int size = size_of_encoded_value (lsda_encoding);
651 if (lsda_encoding == DW_EH_PE_aligned)
653 int offset = ( 4 /* Length */
654 + 4 /* CIE offset */
655 + 2 * size_of_encoded_value (fde_encoding)
656 + 1 /* Augmentation size */ );
657 int pad = -offset & (PTR_SIZE - 1);
659 size += pad;
660 gcc_assert (size_of_uleb128 (size) == 1);
663 dw2_asm_output_data_uleb128 (size, "Augmentation size");
665 if (fde->uses_eh_lsda)
667 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
668 fde->funcdef_number);
669 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
670 gen_rtx_SYMBOL_REF (Pmode, l1),
671 false,
672 "Language Specific Data Area");
674 else
676 if (lsda_encoding == DW_EH_PE_aligned)
677 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
678 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
679 "Language Specific Data Area (none)");
682 else
683 dw2_asm_output_data_uleb128 (0, "Augmentation size");
686 /* Loop through the Call Frame Instructions associated with this FDE. */
687 fde->dw_fde_current_label = begin;
689 size_t from, until, i;
691 from = 0;
692 until = vec_safe_length (fde->dw_fde_cfi);
694 if (fde->dw_fde_second_begin == NULL)
696 else if (!second)
697 until = fde->dw_fde_switch_cfi_index;
698 else
699 from = fde->dw_fde_switch_cfi_index;
701 for (i = from; i < until; i++)
702 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
705 /* If we are to emit a ref/link from function bodies to their frame tables,
706 do it now. This is typically performed to make sure that tables
707 associated with functions are dragged with them and not discarded in
708 garbage collecting links. We need to do this on a per function basis to
709 cope with -ffunction-sections. */
711 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
712 /* Switch to the function section, emit the ref to the tables, and
713 switch *back* into the table section. */
714 switch_to_section (function_section (fde->decl));
715 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
716 switch_to_frame_table_section (for_eh, true);
717 #endif
719 /* Pad the FDE out to an address sized boundary. */
720 ASM_OUTPUT_ALIGN (asm_out_file,
721 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
722 ASM_OUTPUT_LABEL (asm_out_file, l2);
724 j += 2;
727 /* Return true if frame description entry FDE is needed for EH. */
729 static bool
730 fde_needed_for_eh_p (dw_fde_ref fde)
732 if (flag_asynchronous_unwind_tables)
733 return true;
735 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
736 return true;
738 if (fde->uses_eh_lsda)
739 return true;
741 /* If exceptions are enabled, we have collected nothrow info. */
742 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
743 return false;
745 return true;
748 /* Output the call frame information used to record information
749 that relates to calculating the frame pointer, and records the
750 location of saved registers. */
752 static void
753 output_call_frame_info (int for_eh)
755 unsigned int i;
756 dw_fde_ref fde;
757 dw_cfi_ref cfi;
758 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
759 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
760 bool any_lsda_needed = false;
761 char augmentation[6];
762 int augmentation_size;
763 int fde_encoding = DW_EH_PE_absptr;
764 int per_encoding = DW_EH_PE_absptr;
765 int lsda_encoding = DW_EH_PE_absptr;
766 int return_reg;
767 rtx personality = NULL;
768 int dw_cie_version;
770 /* Don't emit a CIE if there won't be any FDEs. */
771 if (!fde_vec)
772 return;
774 /* Nothing to do if the assembler's doing it all. */
775 if (dwarf2out_do_cfi_asm ())
776 return;
778 /* If we don't have any functions we'll want to unwind out of, don't emit
779 any EH unwind information. If we make FDEs linkonce, we may have to
780 emit an empty label for an FDE that wouldn't otherwise be emitted. We
781 want to avoid having an FDE kept around when the function it refers to
782 is discarded. Example where this matters: a primary function template
783 in C++ requires EH information, an explicit specialization doesn't. */
784 if (for_eh)
786 bool any_eh_needed = false;
788 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
790 if (fde->uses_eh_lsda)
791 any_eh_needed = any_lsda_needed = true;
792 else if (fde_needed_for_eh_p (fde))
793 any_eh_needed = true;
794 else if (TARGET_USES_WEAK_UNWIND_INFO)
795 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
798 if (!any_eh_needed)
799 return;
802 /* We're going to be generating comments, so turn on app. */
803 if (flag_debug_asm)
804 app_enable ();
806 /* Switch to the proper frame section, first time. */
807 switch_to_frame_table_section (for_eh, false);
809 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
810 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
812 /* Output the CIE. */
813 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
814 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
815 if (!XCOFF_DEBUGGING_INFO || for_eh)
817 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4 && !for_eh)
818 dw2_asm_output_data (4, 0xffffffff,
819 "Initial length escape value indicating 64-bit DWARF extension");
820 dw2_asm_output_delta (for_eh ? 4 : dwarf_offset_size, l2, l1,
821 "Length of Common Information Entry");
823 ASM_OUTPUT_LABEL (asm_out_file, l1);
825 /* Now that the CIE pointer is PC-relative for EH,
826 use 0 to identify the CIE. */
827 dw2_asm_output_data ((for_eh ? 4 : dwarf_offset_size),
828 (for_eh ? 0 : DWARF_CIE_ID),
829 "CIE Identifier Tag");
831 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
832 use CIE version 1, unless that would produce incorrect results
833 due to overflowing the return register column. */
834 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
835 dw_cie_version = 1;
836 if (return_reg >= 256 || dwarf_version > 2)
837 dw_cie_version = 3;
838 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
840 augmentation[0] = 0;
841 augmentation_size = 0;
843 personality = current_unit_personality;
844 if (for_eh)
846 char *p;
848 /* Augmentation:
849 z Indicates that a uleb128 is present to size the
850 augmentation section.
851 L Indicates the encoding (and thus presence) of
852 an LSDA pointer in the FDE augmentation.
853 R Indicates a non-default pointer encoding for
854 FDE code pointers.
855 P Indicates the presence of an encoding + language
856 personality routine in the CIE augmentation. */
858 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
859 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
860 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
862 p = augmentation + 1;
863 if (personality)
865 *p++ = 'P';
866 augmentation_size += 1 + size_of_encoded_value (per_encoding);
867 assemble_external_libcall (personality);
869 if (any_lsda_needed)
871 *p++ = 'L';
872 augmentation_size += 1;
874 if (fde_encoding != DW_EH_PE_absptr)
876 *p++ = 'R';
877 augmentation_size += 1;
879 if (p > augmentation + 1)
881 augmentation[0] = 'z';
882 *p = '\0';
885 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
886 if (personality && per_encoding == DW_EH_PE_aligned)
888 int offset = ( 4 /* Length */
889 + 4 /* CIE Id */
890 + 1 /* CIE version */
891 + strlen (augmentation) + 1 /* Augmentation */
892 + size_of_uleb128 (1) /* Code alignment */
893 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
894 + 1 /* RA column */
895 + 1 /* Augmentation size */
896 + 1 /* Personality encoding */ );
897 int pad = -offset & (PTR_SIZE - 1);
899 augmentation_size += pad;
901 /* Augmentations should be small, so there's scarce need to
902 iterate for a solution. Die if we exceed one uleb128 byte. */
903 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
907 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
908 if (dw_cie_version >= 4)
910 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
911 dw2_asm_output_data (1, 0, "CIE Segment Size");
913 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
914 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
915 "CIE Data Alignment Factor");
917 if (dw_cie_version == 1)
918 dw2_asm_output_data (1, return_reg, "CIE RA Column");
919 else
920 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
922 if (augmentation[0])
924 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
925 if (personality)
927 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
928 eh_data_format_name (per_encoding));
929 dw2_asm_output_encoded_addr_rtx (per_encoding,
930 personality,
931 true, NULL);
934 if (any_lsda_needed)
935 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
936 eh_data_format_name (lsda_encoding));
938 if (fde_encoding != DW_EH_PE_absptr)
939 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
940 eh_data_format_name (fde_encoding));
943 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
944 output_cfi (cfi, NULL, for_eh);
946 /* Pad the CIE out to an address sized boundary. */
947 ASM_OUTPUT_ALIGN (asm_out_file,
948 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
949 ASM_OUTPUT_LABEL (asm_out_file, l2);
951 /* Loop through all of the FDE's. */
952 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
954 unsigned int k;
956 /* Don't emit EH unwind info for leaf functions that don't need it. */
957 if (for_eh && !fde_needed_for_eh_p (fde))
958 continue;
960 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
961 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
962 augmentation, any_lsda_needed, lsda_encoding);
965 if (for_eh && targetm.terminate_dw2_eh_frame_info)
966 dw2_asm_output_data (4, 0, "End of Table");
968 /* Turn off app to make assembly quicker. */
969 if (flag_debug_asm)
970 app_disable ();
973 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
975 static void
976 dwarf2out_do_cfi_startproc (bool second)
978 int enc;
979 rtx ref;
981 fprintf (asm_out_file, "\t.cfi_startproc\n");
983 targetm.asm_out.post_cfi_startproc (asm_out_file, current_function_decl);
985 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
986 eh unwinders. */
987 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
988 return;
990 rtx personality = get_personality_function (current_function_decl);
992 if (personality)
994 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
995 ref = personality;
997 /* ??? The GAS support isn't entirely consistent. We have to
998 handle indirect support ourselves, but PC-relative is done
999 in the assembler. Further, the assembler can't handle any
1000 of the weirder relocation types. */
1001 if (enc & DW_EH_PE_indirect)
1003 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1004 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1005 else
1006 ref = dw2_force_const_mem (ref, true);
1009 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1010 output_addr_const (asm_out_file, ref);
1011 fputc ('\n', asm_out_file);
1014 if (crtl->uses_eh_lsda)
1016 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1018 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1019 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1020 current_function_funcdef_no);
1021 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1022 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1024 if (enc & DW_EH_PE_indirect)
1026 if (targetm.asm_out.make_eh_symbol_indirect != NULL)
1027 ref = targetm.asm_out.make_eh_symbol_indirect (ref, true);
1028 else
1029 ref = dw2_force_const_mem (ref, true);
1032 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1033 output_addr_const (asm_out_file, ref);
1034 fputc ('\n', asm_out_file);
1038 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1039 this allocation may be done before pass_final. */
1041 dw_fde_ref
1042 dwarf2out_alloc_current_fde (void)
1044 dw_fde_ref fde;
1046 fde = ggc_cleared_alloc<dw_fde_node> ();
1047 fde->decl = current_function_decl;
1048 fde->funcdef_number = current_function_funcdef_no;
1049 fde->fde_index = vec_safe_length (fde_vec);
1050 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1051 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1052 fde->nothrow = crtl->nothrow;
1053 fde->drap_reg = INVALID_REGNUM;
1054 fde->vdrap_reg = INVALID_REGNUM;
1056 /* Record the FDE associated with this function. */
1057 cfun->fde = fde;
1058 vec_safe_push (fde_vec, fde);
1060 return fde;
1063 /* Output a marker (i.e. a label) for the beginning of a function, before
1064 the prologue. */
1066 void
1067 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1068 unsigned int column ATTRIBUTE_UNUSED,
1069 const char *file ATTRIBUTE_UNUSED)
1071 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1072 char * dup_label;
1073 dw_fde_ref fde;
1074 section *fnsec;
1075 bool do_frame;
1077 current_function_func_begin_label = NULL;
1079 do_frame = dwarf2out_do_frame ();
1081 /* ??? current_function_func_begin_label is also used by except.c for
1082 call-site information. We must emit this label if it might be used. */
1083 if (!do_frame
1084 && (!flag_exceptions
1085 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1086 return;
1088 fnsec = function_section (current_function_decl);
1089 switch_to_section (fnsec);
1090 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1091 current_function_funcdef_no);
1092 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1093 current_function_funcdef_no);
1094 dup_label = xstrdup (label);
1095 current_function_func_begin_label = dup_label;
1097 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1098 if (!do_frame)
1099 return;
1101 /* Unlike the debug version, the EH version of frame unwind info is a per-
1102 function setting so we need to record whether we need it for the unit. */
1103 do_eh_frame |= dwarf2out_do_eh_frame ();
1105 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1106 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1107 would include pass_dwarf2_frame. If we've not created the FDE yet,
1108 do so now. */
1109 fde = cfun->fde;
1110 if (fde == NULL)
1111 fde = dwarf2out_alloc_current_fde ();
1113 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1114 fde->dw_fde_begin = dup_label;
1115 fde->dw_fde_current_label = dup_label;
1116 fde->in_std_section = (fnsec == text_section
1117 || (cold_text_section && fnsec == cold_text_section));
1118 fde->ignored_debug = DECL_IGNORED_P (current_function_decl);
1119 in_text_section_p = fnsec == text_section;
1121 /* We only want to output line number information for the genuine dwarf2
1122 prologue case, not the eh frame case. */
1123 #ifdef DWARF2_DEBUGGING_INFO
1124 if (file)
1125 dwarf2out_source_line (line, column, file, 0, true);
1126 #endif
1128 if (dwarf2out_do_cfi_asm ())
1129 dwarf2out_do_cfi_startproc (false);
1130 else
1132 rtx personality = get_personality_function (current_function_decl);
1133 if (!current_unit_personality)
1134 current_unit_personality = personality;
1136 /* We cannot keep a current personality per function as without CFI
1137 asm, at the point where we emit the CFI data, there is no current
1138 function anymore. */
1139 if (personality && current_unit_personality != personality)
1140 sorry ("multiple EH personalities are supported only with assemblers "
1141 "supporting %<.cfi_personality%> directive");
1145 /* Output a marker (i.e. a label) for the end of the generated code
1146 for a function prologue. This gets called *after* the prologue code has
1147 been generated. */
1149 void
1150 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1151 const char *file ATTRIBUTE_UNUSED)
1153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1160 current_function_funcdef_no);
1161 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the beginning of the generated code
1165 for a function epilogue. This gets called *before* the prologue code has
1166 been generated. */
1168 void
1169 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde = cfun->fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 if (fde->dw_fde_vms_begin_epilogue)
1176 return;
1178 /* Output a label to mark the endpoint of the code generated for this
1179 function. */
1180 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1181 current_function_funcdef_no);
1182 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1183 current_function_funcdef_no);
1184 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1187 /* Mark the ranges of non-debug subsections in the std text sections. */
1189 static void
1190 mark_ignored_debug_section (dw_fde_ref fde, bool second)
1192 bool std_section;
1193 const char *begin_label, *end_label;
1194 const char **last_end_label;
1195 vec<const char *, va_gc> **switch_ranges;
1197 if (second)
1199 std_section = fde->second_in_std_section;
1200 begin_label = fde->dw_fde_second_begin;
1201 end_label = fde->dw_fde_second_end;
1203 else
1205 std_section = fde->in_std_section;
1206 begin_label = fde->dw_fde_begin;
1207 end_label = fde->dw_fde_end;
1210 if (!std_section)
1211 return;
1213 if (in_text_section_p)
1215 last_end_label = &last_text_label;
1216 switch_ranges = &switch_text_ranges;
1218 else
1220 last_end_label = &last_cold_label;
1221 switch_ranges = &switch_cold_ranges;
1224 if (fde->ignored_debug)
1226 if (*switch_ranges && !(vec_safe_length (*switch_ranges) & 1))
1227 vec_safe_push (*switch_ranges, *last_end_label);
1229 else
1231 *last_end_label = end_label;
1233 if (!*switch_ranges)
1234 vec_alloc (*switch_ranges, 16);
1235 else if (vec_safe_length (*switch_ranges) & 1)
1236 vec_safe_push (*switch_ranges, begin_label);
1240 /* Output a marker (i.e. a label) for the absolute end of the generated code
1241 for a function definition. This gets called *after* the epilogue code has
1242 been generated. */
1244 void
1245 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1246 const char *file ATTRIBUTE_UNUSED)
1248 dw_fde_ref fde;
1249 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1251 last_var_location_insn = NULL;
1252 cached_next_real_insn = NULL;
1254 if (dwarf2out_do_cfi_asm ())
1255 fprintf (asm_out_file, "\t.cfi_endproc\n");
1257 /* Output a label to mark the endpoint of the code generated for this
1258 function. */
1259 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1260 current_function_funcdef_no);
1261 ASM_OUTPUT_LABEL (asm_out_file, label);
1262 fde = cfun->fde;
1263 gcc_assert (fde != NULL);
1264 if (fde->dw_fde_second_begin == NULL)
1265 fde->dw_fde_end = xstrdup (label);
1267 mark_ignored_debug_section (fde, fde->dw_fde_second_begin != NULL);
1270 void
1271 dwarf2out_frame_finish (void)
1273 /* Output call frame information. */
1274 if (targetm.debug_unwind_info () == UI_DWARF2)
1275 output_call_frame_info (0);
1277 /* Output another copy for the unwinder. */
1278 if (do_eh_frame)
1279 output_call_frame_info (1);
1282 static void var_location_switch_text_section (void);
1283 static void set_cur_line_info_table (section *);
1285 void
1286 dwarf2out_switch_text_section (void)
1288 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1289 section *sect;
1290 dw_fde_ref fde = cfun->fde;
1292 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1294 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_SECOND_SECT_LABEL,
1295 current_function_funcdef_no);
1297 fde->dw_fde_second_begin = ggc_strdup (label);
1298 if (!in_cold_section_p)
1300 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1301 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1303 else
1305 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1306 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1308 have_multiple_function_sections = true;
1310 if (dwarf2out_do_cfi_asm ())
1311 fprintf (asm_out_file, "\t.cfi_endproc\n");
1313 mark_ignored_debug_section (fde, false);
1315 /* Now do the real section switch. */
1316 sect = current_function_section ();
1317 switch_to_section (sect);
1319 fde->second_in_std_section
1320 = (sect == text_section
1321 || (cold_text_section && sect == cold_text_section));
1322 in_text_section_p = sect == text_section;
1324 if (dwarf2out_do_cfi_asm ())
1325 dwarf2out_do_cfi_startproc (true);
1327 var_location_switch_text_section ();
1329 if (cold_text_section != NULL)
1330 set_cur_line_info_table (sect);
1333 /* And now, the subset of the debugging information support code necessary
1334 for emitting location expressions. */
1336 /* Describe an entry into the .debug_addr section. */
1338 enum ate_kind {
1339 ate_kind_rtx,
1340 ate_kind_rtx_dtprel,
1341 ate_kind_label
1344 struct GTY((for_user)) addr_table_entry {
1345 enum ate_kind kind;
1346 unsigned int refcount;
1347 unsigned int index;
1348 union addr_table_entry_struct_union
1350 rtx GTY ((tag ("0"))) rtl;
1351 char * GTY ((tag ("1"))) label;
1353 GTY ((desc ("%1.kind"))) addr;
1356 typedef unsigned int var_loc_view;
1358 /* Location lists are ranges + location descriptions for that range,
1359 so you can track variables that are in different places over
1360 their entire life. */
1361 typedef struct GTY(()) dw_loc_list_struct {
1362 dw_loc_list_ref dw_loc_next;
1363 const char *begin; /* Label and addr_entry for start of range */
1364 addr_table_entry *begin_entry;
1365 const char *end; /* Label for end of range */
1366 addr_table_entry *end_entry;
1367 char *ll_symbol; /* Label for beginning of location list.
1368 Only on head of list. */
1369 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1370 const char *section; /* Section this loclist is relative to */
1371 dw_loc_descr_ref expr;
1372 var_loc_view vbegin, vend;
1373 hashval_t hash;
1374 /* True if all addresses in this and subsequent lists are known to be
1375 resolved. */
1376 bool resolved_addr;
1377 /* True if this list has been replaced by dw_loc_next. */
1378 bool replaced;
1379 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1380 section. */
1381 unsigned char emitted : 1;
1382 /* True if hash field is index rather than hash value. */
1383 unsigned char num_assigned : 1;
1384 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1385 unsigned char offset_emitted : 1;
1386 /* True if note_variable_value_in_expr has been called on it. */
1387 unsigned char noted_variable_value : 1;
1388 /* True if the range should be emitted even if begin and end
1389 are the same. */
1390 bool force;
1391 } dw_loc_list_node;
1393 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1394 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1396 /* Convert a DWARF stack opcode into its string name. */
1398 static const char *
1399 dwarf_stack_op_name (unsigned int op)
1401 const char *name = get_DW_OP_name (op);
1403 if (name != NULL)
1404 return name;
1406 return "OP_<unknown>";
1409 /* Return TRUE iff we're to output location view lists as a separate
1410 attribute next to the location lists, as an extension compatible
1411 with DWARF 2 and above. */
1413 static inline bool
1414 dwarf2out_locviews_in_attribute ()
1416 return debug_variable_location_views == 1;
1419 /* Return TRUE iff we're to output location view lists as part of the
1420 location lists, as proposed for standardization after DWARF 5. */
1422 static inline bool
1423 dwarf2out_locviews_in_loclist ()
1425 #ifndef DW_LLE_view_pair
1426 return false;
1427 #else
1428 return debug_variable_location_views == -1;
1429 #endif
1432 /* Return a pointer to a newly allocated location description. Location
1433 descriptions are simple expression terms that can be strung
1434 together to form more complicated location (address) descriptions. */
1436 static inline dw_loc_descr_ref
1437 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1438 unsigned HOST_WIDE_INT oprnd2)
1440 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1442 descr->dw_loc_opc = op;
1443 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1444 descr->dw_loc_oprnd1.val_entry = NULL;
1445 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1446 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1447 descr->dw_loc_oprnd2.val_entry = NULL;
1448 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1450 return descr;
1453 /* Add a location description term to a location description expression. */
1455 static inline void
1456 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1458 dw_loc_descr_ref *d;
1460 /* Find the end of the chain. */
1461 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1464 *d = descr;
1467 /* Compare two location operands for exact equality. */
1469 static bool
1470 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1472 if (a->val_class != b->val_class)
1473 return false;
1474 switch (a->val_class)
1476 case dw_val_class_none:
1477 return true;
1478 case dw_val_class_addr:
1479 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1481 case dw_val_class_offset:
1482 case dw_val_class_unsigned_const:
1483 case dw_val_class_const:
1484 case dw_val_class_unsigned_const_implicit:
1485 case dw_val_class_const_implicit:
1486 case dw_val_class_range_list:
1487 /* These are all HOST_WIDE_INT, signed or unsigned. */
1488 return a->v.val_unsigned == b->v.val_unsigned;
1490 case dw_val_class_loc:
1491 return a->v.val_loc == b->v.val_loc;
1492 case dw_val_class_loc_list:
1493 return a->v.val_loc_list == b->v.val_loc_list;
1494 case dw_val_class_view_list:
1495 return a->v.val_view_list == b->v.val_view_list;
1496 case dw_val_class_die_ref:
1497 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1498 case dw_val_class_fde_ref:
1499 return a->v.val_fde_index == b->v.val_fde_index;
1500 case dw_val_class_symview:
1501 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1502 case dw_val_class_lbl_id:
1503 case dw_val_class_lineptr:
1504 case dw_val_class_macptr:
1505 case dw_val_class_loclistsptr:
1506 case dw_val_class_high_pc:
1507 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1508 case dw_val_class_str:
1509 return a->v.val_str == b->v.val_str;
1510 case dw_val_class_flag:
1511 return a->v.val_flag == b->v.val_flag;
1512 case dw_val_class_file:
1513 case dw_val_class_file_implicit:
1514 return a->v.val_file == b->v.val_file;
1515 case dw_val_class_decl_ref:
1516 return a->v.val_decl_ref == b->v.val_decl_ref;
1518 case dw_val_class_const_double:
1519 return (a->v.val_double.high == b->v.val_double.high
1520 && a->v.val_double.low == b->v.val_double.low);
1522 case dw_val_class_wide_int:
1523 return *a->v.val_wide == *b->v.val_wide;
1525 case dw_val_class_vec:
1527 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1528 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1530 return (a_len == b_len
1531 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1534 case dw_val_class_data8:
1535 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1537 case dw_val_class_vms_delta:
1538 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1539 && !strcmp (a->v.val_vms_delta.lbl2, b->v.val_vms_delta.lbl2));
1541 case dw_val_class_discr_value:
1542 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1543 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1544 case dw_val_class_discr_list:
1545 /* It makes no sense comparing two discriminant value lists. */
1546 return false;
1548 gcc_unreachable ();
1551 /* Compare two location atoms for exact equality. */
1553 static bool
1554 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1556 if (a->dw_loc_opc != b->dw_loc_opc)
1557 return false;
1559 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1560 address size, but since we always allocate cleared storage it
1561 should be zero for other types of locations. */
1562 if (a->dtprel != b->dtprel)
1563 return false;
1565 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1566 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1569 /* Compare two complete location expressions for exact equality. */
1571 bool
1572 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1574 while (1)
1576 if (a == b)
1577 return true;
1578 if (a == NULL || b == NULL)
1579 return false;
1580 if (!loc_descr_equal_p_1 (a, b))
1581 return false;
1583 a = a->dw_loc_next;
1584 b = b->dw_loc_next;
1589 /* Add a constant POLY_OFFSET to a location expression. */
1591 static void
1592 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1594 dw_loc_descr_ref loc;
1595 HOST_WIDE_INT *p;
1597 gcc_assert (*list_head != NULL);
1599 if (known_eq (poly_offset, 0))
1600 return;
1602 /* Find the end of the chain. */
1603 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1606 HOST_WIDE_INT offset;
1607 if (!poly_offset.is_constant (&offset))
1609 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1610 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1611 return;
1614 p = NULL;
1615 if (loc->dw_loc_opc == DW_OP_fbreg
1616 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1617 p = &loc->dw_loc_oprnd1.v.val_int;
1618 else if (loc->dw_loc_opc == DW_OP_bregx)
1619 p = &loc->dw_loc_oprnd2.v.val_int;
1621 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1622 offset. Don't optimize if an signed integer overflow would happen. */
1623 if (p != NULL
1624 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1625 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1626 *p += offset;
1628 else if (offset > 0)
1629 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1631 else
1633 loc->dw_loc_next
1634 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1635 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1639 /* Return a pointer to a newly allocated location description for
1640 REG and OFFSET. */
1642 static inline dw_loc_descr_ref
1643 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1645 HOST_WIDE_INT const_offset;
1646 if (offset.is_constant (&const_offset))
1648 if (reg <= 31)
1649 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1650 const_offset, 0);
1651 else
1652 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1654 else
1656 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1657 loc_descr_plus_const (&ret, offset);
1658 return ret;
1662 /* Add a constant OFFSET to a location list. */
1664 static void
1665 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1667 dw_loc_list_ref d;
1668 for (d = list_head; d != NULL; d = d->dw_loc_next)
1669 loc_descr_plus_const (&d->expr, offset);
1672 #define DWARF_REF_SIZE \
1673 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : dwarf_offset_size)
1675 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1676 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1677 DW_FORM_data16 with 128 bits. */
1678 #define DWARF_LARGEST_DATA_FORM_BITS \
1679 (dwarf_version >= 5 ? 128 : 64)
1681 /* Utility inline function for construction of ops that were GNU extension
1682 before DWARF 5. */
1683 static inline enum dwarf_location_atom
1684 dwarf_OP (enum dwarf_location_atom op)
1686 switch (op)
1688 case DW_OP_implicit_pointer:
1689 if (dwarf_version < 5)
1690 return DW_OP_GNU_implicit_pointer;
1691 break;
1693 case DW_OP_entry_value:
1694 if (dwarf_version < 5)
1695 return DW_OP_GNU_entry_value;
1696 break;
1698 case DW_OP_const_type:
1699 if (dwarf_version < 5)
1700 return DW_OP_GNU_const_type;
1701 break;
1703 case DW_OP_regval_type:
1704 if (dwarf_version < 5)
1705 return DW_OP_GNU_regval_type;
1706 break;
1708 case DW_OP_deref_type:
1709 if (dwarf_version < 5)
1710 return DW_OP_GNU_deref_type;
1711 break;
1713 case DW_OP_convert:
1714 if (dwarf_version < 5)
1715 return DW_OP_GNU_convert;
1716 break;
1718 case DW_OP_reinterpret:
1719 if (dwarf_version < 5)
1720 return DW_OP_GNU_reinterpret;
1721 break;
1723 case DW_OP_addrx:
1724 if (dwarf_version < 5)
1725 return DW_OP_GNU_addr_index;
1726 break;
1728 case DW_OP_constx:
1729 if (dwarf_version < 5)
1730 return DW_OP_GNU_const_index;
1731 break;
1733 default:
1734 break;
1736 return op;
1739 /* Similarly for attributes. */
1740 static inline enum dwarf_attribute
1741 dwarf_AT (enum dwarf_attribute at)
1743 switch (at)
1745 case DW_AT_call_return_pc:
1746 if (dwarf_version < 5)
1747 return DW_AT_low_pc;
1748 break;
1750 case DW_AT_call_tail_call:
1751 if (dwarf_version < 5)
1752 return DW_AT_GNU_tail_call;
1753 break;
1755 case DW_AT_call_origin:
1756 if (dwarf_version < 5)
1757 return DW_AT_abstract_origin;
1758 break;
1760 case DW_AT_call_target:
1761 if (dwarf_version < 5)
1762 return DW_AT_GNU_call_site_target;
1763 break;
1765 case DW_AT_call_target_clobbered:
1766 if (dwarf_version < 5)
1767 return DW_AT_GNU_call_site_target_clobbered;
1768 break;
1770 case DW_AT_call_parameter:
1771 if (dwarf_version < 5)
1772 return DW_AT_abstract_origin;
1773 break;
1775 case DW_AT_call_value:
1776 if (dwarf_version < 5)
1777 return DW_AT_GNU_call_site_value;
1778 break;
1780 case DW_AT_call_data_value:
1781 if (dwarf_version < 5)
1782 return DW_AT_GNU_call_site_data_value;
1783 break;
1785 case DW_AT_call_all_calls:
1786 if (dwarf_version < 5)
1787 return DW_AT_GNU_all_call_sites;
1788 break;
1790 case DW_AT_call_all_tail_calls:
1791 if (dwarf_version < 5)
1792 return DW_AT_GNU_all_tail_call_sites;
1793 break;
1795 case DW_AT_dwo_name:
1796 if (dwarf_version < 5)
1797 return DW_AT_GNU_dwo_name;
1798 break;
1800 case DW_AT_addr_base:
1801 if (dwarf_version < 5)
1802 return DW_AT_GNU_addr_base;
1803 break;
1805 default:
1806 break;
1808 return at;
1811 /* And similarly for tags. */
1812 static inline enum dwarf_tag
1813 dwarf_TAG (enum dwarf_tag tag)
1815 switch (tag)
1817 case DW_TAG_call_site:
1818 if (dwarf_version < 5)
1819 return DW_TAG_GNU_call_site;
1820 break;
1822 case DW_TAG_call_site_parameter:
1823 if (dwarf_version < 5)
1824 return DW_TAG_GNU_call_site_parameter;
1825 break;
1827 default:
1828 break;
1830 return tag;
1833 /* And similarly for forms. */
1834 static inline enum dwarf_form
1835 dwarf_FORM (enum dwarf_form form)
1837 switch (form)
1839 case DW_FORM_addrx:
1840 if (dwarf_version < 5)
1841 return DW_FORM_GNU_addr_index;
1842 break;
1844 case DW_FORM_strx:
1845 if (dwarf_version < 5)
1846 return DW_FORM_GNU_str_index;
1847 break;
1849 default:
1850 break;
1852 return form;
1855 static unsigned long int get_base_type_offset (dw_die_ref);
1857 /* Return the size of a location descriptor. */
1859 static unsigned long
1860 size_of_loc_descr (dw_loc_descr_ref loc)
1862 unsigned long size = 1;
1864 switch (loc->dw_loc_opc)
1866 case DW_OP_addr:
1867 size += DWARF2_ADDR_SIZE;
1868 break;
1869 case DW_OP_GNU_addr_index:
1870 case DW_OP_addrx:
1871 case DW_OP_GNU_const_index:
1872 case DW_OP_constx:
1873 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1874 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1875 break;
1876 case DW_OP_const1u:
1877 case DW_OP_const1s:
1878 size += 1;
1879 break;
1880 case DW_OP_const2u:
1881 case DW_OP_const2s:
1882 size += 2;
1883 break;
1884 case DW_OP_const4u:
1885 case DW_OP_const4s:
1886 size += 4;
1887 break;
1888 case DW_OP_const8u:
1889 case DW_OP_const8s:
1890 size += 8;
1891 break;
1892 case DW_OP_constu:
1893 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1894 break;
1895 case DW_OP_consts:
1896 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1897 break;
1898 case DW_OP_pick:
1899 size += 1;
1900 break;
1901 case DW_OP_plus_uconst:
1902 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1903 break;
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 size += 2;
1907 break;
1908 case DW_OP_breg0:
1909 case DW_OP_breg1:
1910 case DW_OP_breg2:
1911 case DW_OP_breg3:
1912 case DW_OP_breg4:
1913 case DW_OP_breg5:
1914 case DW_OP_breg6:
1915 case DW_OP_breg7:
1916 case DW_OP_breg8:
1917 case DW_OP_breg9:
1918 case DW_OP_breg10:
1919 case DW_OP_breg11:
1920 case DW_OP_breg12:
1921 case DW_OP_breg13:
1922 case DW_OP_breg14:
1923 case DW_OP_breg15:
1924 case DW_OP_breg16:
1925 case DW_OP_breg17:
1926 case DW_OP_breg18:
1927 case DW_OP_breg19:
1928 case DW_OP_breg20:
1929 case DW_OP_breg21:
1930 case DW_OP_breg22:
1931 case DW_OP_breg23:
1932 case DW_OP_breg24:
1933 case DW_OP_breg25:
1934 case DW_OP_breg26:
1935 case DW_OP_breg27:
1936 case DW_OP_breg28:
1937 case DW_OP_breg29:
1938 case DW_OP_breg30:
1939 case DW_OP_breg31:
1940 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1941 break;
1942 case DW_OP_regx:
1943 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1944 break;
1945 case DW_OP_fbreg:
1946 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1947 break;
1948 case DW_OP_bregx:
1949 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1950 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1951 break;
1952 case DW_OP_piece:
1953 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1954 break;
1955 case DW_OP_bit_piece:
1956 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1957 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1958 break;
1959 case DW_OP_deref_size:
1960 case DW_OP_xderef_size:
1961 size += 1;
1962 break;
1963 case DW_OP_call2:
1964 size += 2;
1965 break;
1966 case DW_OP_call4:
1967 size += 4;
1968 break;
1969 case DW_OP_call_ref:
1970 case DW_OP_GNU_variable_value:
1971 size += DWARF_REF_SIZE;
1972 break;
1973 case DW_OP_implicit_value:
1974 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1975 + loc->dw_loc_oprnd1.v.val_unsigned;
1976 break;
1977 case DW_OP_implicit_pointer:
1978 case DW_OP_GNU_implicit_pointer:
1979 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1980 break;
1981 case DW_OP_entry_value:
1982 case DW_OP_GNU_entry_value:
1984 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1985 size += size_of_uleb128 (op_size) + op_size;
1986 break;
1988 case DW_OP_const_type:
1989 case DW_OP_GNU_const_type:
1991 unsigned long o
1992 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1993 size += size_of_uleb128 (o) + 1;
1994 switch (loc->dw_loc_oprnd2.val_class)
1996 case dw_val_class_vec:
1997 size += loc->dw_loc_oprnd2.v.val_vec.length
1998 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1999 break;
2000 case dw_val_class_const:
2001 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2002 break;
2003 case dw_val_class_const_double:
2004 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
2005 break;
2006 case dw_val_class_wide_int:
2007 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
2008 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
2009 break;
2010 default:
2011 gcc_unreachable ();
2013 break;
2015 case DW_OP_regval_type:
2016 case DW_OP_GNU_regval_type:
2018 unsigned long o
2019 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2020 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2021 + size_of_uleb128 (o);
2023 break;
2024 case DW_OP_deref_type:
2025 case DW_OP_GNU_deref_type:
2027 unsigned long o
2028 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2029 size += 1 + size_of_uleb128 (o);
2031 break;
2032 case DW_OP_convert:
2033 case DW_OP_reinterpret:
2034 case DW_OP_GNU_convert:
2035 case DW_OP_GNU_reinterpret:
2036 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2037 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2038 else
2040 unsigned long o
2041 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2042 size += size_of_uleb128 (o);
2044 break;
2045 case DW_OP_GNU_parameter_ref:
2046 size += 4;
2047 break;
2048 default:
2049 break;
2052 return size;
2055 /* Return the size of a series of location descriptors. */
2057 unsigned long
2058 size_of_locs (dw_loc_descr_ref loc)
2060 dw_loc_descr_ref l;
2061 unsigned long size;
2063 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2064 field, to avoid writing to a PCH file. */
2065 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2067 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2068 break;
2069 size += size_of_loc_descr (l);
2071 if (! l)
2072 return size;
2074 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2076 l->dw_loc_addr = size;
2077 size += size_of_loc_descr (l);
2080 return size;
2083 /* Return the size of the value in a DW_AT_discr_value attribute. */
2085 static int
2086 size_of_discr_value (dw_discr_value *discr_value)
2088 if (discr_value->pos)
2089 return size_of_uleb128 (discr_value->v.uval);
2090 else
2091 return size_of_sleb128 (discr_value->v.sval);
2094 /* Return the size of the value in a DW_AT_discr_list attribute. */
2096 static int
2097 size_of_discr_list (dw_discr_list_ref discr_list)
2099 int size = 0;
2101 for (dw_discr_list_ref list = discr_list;
2102 list != NULL;
2103 list = list->dw_discr_next)
2105 /* One byte for the discriminant value descriptor, and then one or two
2106 LEB128 numbers, depending on whether it's a single case label or a
2107 range label. */
2108 size += 1;
2109 size += size_of_discr_value (&list->dw_discr_lower_bound);
2110 if (list->dw_discr_range != 0)
2111 size += size_of_discr_value (&list->dw_discr_upper_bound);
2113 return size;
2116 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2117 static void get_ref_die_offset_label (char *, dw_die_ref);
2118 static unsigned long int get_ref_die_offset (dw_die_ref);
2120 /* Output location description stack opcode's operands (if any).
2121 The for_eh_or_skip parameter controls whether register numbers are
2122 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2123 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2124 info). This should be suppressed for the cases that have not been converted
2125 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2127 static void
2128 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2130 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2131 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2133 switch (loc->dw_loc_opc)
2135 #ifdef DWARF2_DEBUGGING_INFO
2136 case DW_OP_const2u:
2137 case DW_OP_const2s:
2138 dw2_asm_output_data (2, val1->v.val_int, NULL);
2139 break;
2140 case DW_OP_const4u:
2141 if (loc->dtprel)
2143 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2144 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2145 val1->v.val_addr);
2146 fputc ('\n', asm_out_file);
2147 break;
2149 /* FALLTHRU */
2150 case DW_OP_const4s:
2151 dw2_asm_output_data (4, val1->v.val_int, NULL);
2152 break;
2153 case DW_OP_const8u:
2154 if (loc->dtprel)
2156 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2157 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2158 val1->v.val_addr);
2159 fputc ('\n', asm_out_file);
2160 break;
2162 /* FALLTHRU */
2163 case DW_OP_const8s:
2164 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2165 dw2_asm_output_data (8, val1->v.val_int, NULL);
2166 break;
2167 case DW_OP_skip:
2168 case DW_OP_bra:
2170 int offset;
2172 gcc_assert (val1->val_class == dw_val_class_loc);
2173 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2175 dw2_asm_output_data (2, offset, NULL);
2177 break;
2178 case DW_OP_implicit_value:
2179 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2180 switch (val2->val_class)
2182 case dw_val_class_const:
2183 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2184 break;
2185 case dw_val_class_vec:
2187 unsigned int elt_size = val2->v.val_vec.elt_size;
2188 unsigned int len = val2->v.val_vec.length;
2189 unsigned int i;
2190 unsigned char *p;
2192 if (elt_size > sizeof (HOST_WIDE_INT))
2194 elt_size /= 2;
2195 len *= 2;
2197 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2198 i < len;
2199 i++, p += elt_size)
2200 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2201 "fp or vector constant word %u", i);
2203 break;
2204 case dw_val_class_const_double:
2206 unsigned HOST_WIDE_INT first, second;
2208 if (WORDS_BIG_ENDIAN)
2210 first = val2->v.val_double.high;
2211 second = val2->v.val_double.low;
2213 else
2215 first = val2->v.val_double.low;
2216 second = val2->v.val_double.high;
2218 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2219 first, NULL);
2220 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2221 second, NULL);
2223 break;
2224 case dw_val_class_wide_int:
2226 int i;
2227 int len = get_full_len (*val2->v.val_wide);
2228 if (WORDS_BIG_ENDIAN)
2229 for (i = len - 1; i >= 0; --i)
2230 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2231 val2->v.val_wide->elt (i), NULL);
2232 else
2233 for (i = 0; i < len; ++i)
2234 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2235 val2->v.val_wide->elt (i), NULL);
2237 break;
2238 case dw_val_class_addr:
2239 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2240 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2241 break;
2242 default:
2243 gcc_unreachable ();
2245 break;
2246 #else
2247 case DW_OP_const2u:
2248 case DW_OP_const2s:
2249 case DW_OP_const4u:
2250 case DW_OP_const4s:
2251 case DW_OP_const8u:
2252 case DW_OP_const8s:
2253 case DW_OP_skip:
2254 case DW_OP_bra:
2255 case DW_OP_implicit_value:
2256 /* We currently don't make any attempt to make sure these are
2257 aligned properly like we do for the main unwind info, so
2258 don't support emitting things larger than a byte if we're
2259 only doing unwinding. */
2260 gcc_unreachable ();
2261 #endif
2262 case DW_OP_const1u:
2263 case DW_OP_const1s:
2264 dw2_asm_output_data (1, val1->v.val_int, NULL);
2265 break;
2266 case DW_OP_constu:
2267 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2268 break;
2269 case DW_OP_consts:
2270 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2271 break;
2272 case DW_OP_pick:
2273 dw2_asm_output_data (1, val1->v.val_int, NULL);
2274 break;
2275 case DW_OP_plus_uconst:
2276 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2277 break;
2278 case DW_OP_breg0:
2279 case DW_OP_breg1:
2280 case DW_OP_breg2:
2281 case DW_OP_breg3:
2282 case DW_OP_breg4:
2283 case DW_OP_breg5:
2284 case DW_OP_breg6:
2285 case DW_OP_breg7:
2286 case DW_OP_breg8:
2287 case DW_OP_breg9:
2288 case DW_OP_breg10:
2289 case DW_OP_breg11:
2290 case DW_OP_breg12:
2291 case DW_OP_breg13:
2292 case DW_OP_breg14:
2293 case DW_OP_breg15:
2294 case DW_OP_breg16:
2295 case DW_OP_breg17:
2296 case DW_OP_breg18:
2297 case DW_OP_breg19:
2298 case DW_OP_breg20:
2299 case DW_OP_breg21:
2300 case DW_OP_breg22:
2301 case DW_OP_breg23:
2302 case DW_OP_breg24:
2303 case DW_OP_breg25:
2304 case DW_OP_breg26:
2305 case DW_OP_breg27:
2306 case DW_OP_breg28:
2307 case DW_OP_breg29:
2308 case DW_OP_breg30:
2309 case DW_OP_breg31:
2310 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2311 break;
2312 case DW_OP_regx:
2314 unsigned r = val1->v.val_unsigned;
2315 if (for_eh_or_skip >= 0)
2316 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 dw2_asm_output_data_uleb128 (r, NULL);
2321 break;
2322 case DW_OP_fbreg:
2323 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2324 break;
2325 case DW_OP_bregx:
2327 unsigned r = val1->v.val_unsigned;
2328 if (for_eh_or_skip >= 0)
2329 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2330 gcc_assert (size_of_uleb128 (r)
2331 == size_of_uleb128 (val1->v.val_unsigned));
2332 dw2_asm_output_data_uleb128 (r, NULL);
2333 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2335 break;
2336 case DW_OP_piece:
2337 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2338 break;
2339 case DW_OP_bit_piece:
2340 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2341 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2342 break;
2343 case DW_OP_deref_size:
2344 case DW_OP_xderef_size:
2345 dw2_asm_output_data (1, val1->v.val_int, NULL);
2346 break;
2348 case DW_OP_addr:
2349 if (loc->dtprel)
2351 if (targetm.asm_out.output_dwarf_dtprel)
2353 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2354 DWARF2_ADDR_SIZE,
2355 val1->v.val_addr);
2356 fputc ('\n', asm_out_file);
2358 else
2359 gcc_unreachable ();
2361 else
2363 #ifdef DWARF2_DEBUGGING_INFO
2364 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2365 #else
2366 gcc_unreachable ();
2367 #endif
2369 break;
2371 case DW_OP_GNU_addr_index:
2372 case DW_OP_addrx:
2373 case DW_OP_GNU_const_index:
2374 case DW_OP_constx:
2375 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2376 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2377 "(index into .debug_addr)");
2378 break;
2380 case DW_OP_call2:
2381 case DW_OP_call4:
2383 unsigned long die_offset
2384 = get_ref_die_offset (val1->v.val_die_ref.die);
2385 /* Make sure the offset has been computed and that we can encode it as
2386 an operand. */
2387 gcc_assert (die_offset > 0
2388 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2389 ? 0xffff
2390 : 0xffffffff));
2391 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2392 die_offset, NULL);
2394 break;
2396 case DW_OP_call_ref:
2397 case DW_OP_GNU_variable_value:
2399 char label[MAX_ARTIFICIAL_LABEL_BYTES
2400 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2401 gcc_assert (val1->val_class == dw_val_class_die_ref);
2402 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2403 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2405 break;
2407 case DW_OP_implicit_pointer:
2408 case DW_OP_GNU_implicit_pointer:
2410 char label[MAX_ARTIFICIAL_LABEL_BYTES
2411 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2412 gcc_assert (val1->val_class == dw_val_class_die_ref);
2413 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2414 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2415 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2417 break;
2419 case DW_OP_entry_value:
2420 case DW_OP_GNU_entry_value:
2421 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2422 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2423 break;
2425 case DW_OP_const_type:
2426 case DW_OP_GNU_const_type:
2428 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2429 gcc_assert (o);
2430 dw2_asm_output_data_uleb128 (o, NULL);
2431 switch (val2->val_class)
2433 case dw_val_class_const:
2434 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2435 dw2_asm_output_data (1, l, NULL);
2436 dw2_asm_output_data (l, val2->v.val_int, NULL);
2437 break;
2438 case dw_val_class_vec:
2440 unsigned int elt_size = val2->v.val_vec.elt_size;
2441 unsigned int len = val2->v.val_vec.length;
2442 unsigned int i;
2443 unsigned char *p;
2445 l = len * elt_size;
2446 dw2_asm_output_data (1, l, NULL);
2447 if (elt_size > sizeof (HOST_WIDE_INT))
2449 elt_size /= 2;
2450 len *= 2;
2452 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2453 i < len;
2454 i++, p += elt_size)
2455 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2456 "fp or vector constant word %u", i);
2458 break;
2459 case dw_val_class_const_double:
2461 unsigned HOST_WIDE_INT first, second;
2462 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2464 dw2_asm_output_data (1, 2 * l, NULL);
2465 if (WORDS_BIG_ENDIAN)
2467 first = val2->v.val_double.high;
2468 second = val2->v.val_double.low;
2470 else
2472 first = val2->v.val_double.low;
2473 second = val2->v.val_double.high;
2475 dw2_asm_output_data (l, first, NULL);
2476 dw2_asm_output_data (l, second, NULL);
2478 break;
2479 case dw_val_class_wide_int:
2481 int i;
2482 int len = get_full_len (*val2->v.val_wide);
2483 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2485 dw2_asm_output_data (1, len * l, NULL);
2486 if (WORDS_BIG_ENDIAN)
2487 for (i = len - 1; i >= 0; --i)
2488 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2489 else
2490 for (i = 0; i < len; ++i)
2491 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2493 break;
2494 default:
2495 gcc_unreachable ();
2498 break;
2499 case DW_OP_regval_type:
2500 case DW_OP_GNU_regval_type:
2502 unsigned r = val1->v.val_unsigned;
2503 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2504 gcc_assert (o);
2505 if (for_eh_or_skip >= 0)
2507 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2508 gcc_assert (size_of_uleb128 (r)
2509 == size_of_uleb128 (val1->v.val_unsigned));
2511 dw2_asm_output_data_uleb128 (r, NULL);
2512 dw2_asm_output_data_uleb128 (o, NULL);
2514 break;
2515 case DW_OP_deref_type:
2516 case DW_OP_GNU_deref_type:
2518 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2519 gcc_assert (o);
2520 dw2_asm_output_data (1, val1->v.val_int, NULL);
2521 dw2_asm_output_data_uleb128 (o, NULL);
2523 break;
2524 case DW_OP_convert:
2525 case DW_OP_reinterpret:
2526 case DW_OP_GNU_convert:
2527 case DW_OP_GNU_reinterpret:
2528 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2529 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2530 else
2532 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2533 gcc_assert (o);
2534 dw2_asm_output_data_uleb128 (o, NULL);
2536 break;
2538 case DW_OP_GNU_parameter_ref:
2540 unsigned long o;
2541 gcc_assert (val1->val_class == dw_val_class_die_ref);
2542 o = get_ref_die_offset (val1->v.val_die_ref.die);
2543 dw2_asm_output_data (4, o, NULL);
2545 break;
2547 default:
2548 /* Other codes have no operands. */
2549 break;
2553 /* Output a sequence of location operations.
2554 The for_eh_or_skip parameter controls whether register numbers are
2555 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2556 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2557 info). This should be suppressed for the cases that have not been converted
2558 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2560 void
2561 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2563 for (; loc != NULL; loc = loc->dw_loc_next)
2565 enum dwarf_location_atom opc = loc->dw_loc_opc;
2566 /* Output the opcode. */
2567 if (for_eh_or_skip >= 0
2568 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2570 unsigned r = (opc - DW_OP_breg0);
2571 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2572 gcc_assert (r <= 31);
2573 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2575 else if (for_eh_or_skip >= 0
2576 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2578 unsigned r = (opc - DW_OP_reg0);
2579 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2580 gcc_assert (r <= 31);
2581 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2584 dw2_asm_output_data (1, opc,
2585 "%s", dwarf_stack_op_name (opc));
2587 /* Output the operand(s) (if any). */
2588 output_loc_operands (loc, for_eh_or_skip);
2592 /* Output location description stack opcode's operands (if any).
2593 The output is single bytes on a line, suitable for .cfi_escape. */
2595 static void
2596 output_loc_operands_raw (dw_loc_descr_ref loc)
2598 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2599 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2601 switch (loc->dw_loc_opc)
2603 case DW_OP_addr:
2604 case DW_OP_GNU_addr_index:
2605 case DW_OP_addrx:
2606 case DW_OP_GNU_const_index:
2607 case DW_OP_constx:
2608 case DW_OP_implicit_value:
2609 /* We cannot output addresses in .cfi_escape, only bytes. */
2610 gcc_unreachable ();
2612 case DW_OP_const1u:
2613 case DW_OP_const1s:
2614 case DW_OP_pick:
2615 case DW_OP_deref_size:
2616 case DW_OP_xderef_size:
2617 fputc (',', asm_out_file);
2618 dw2_asm_output_data_raw (1, val1->v.val_int);
2619 break;
2621 case DW_OP_const2u:
2622 case DW_OP_const2s:
2623 fputc (',', asm_out_file);
2624 dw2_asm_output_data_raw (2, val1->v.val_int);
2625 break;
2627 case DW_OP_const4u:
2628 case DW_OP_const4s:
2629 fputc (',', asm_out_file);
2630 dw2_asm_output_data_raw (4, val1->v.val_int);
2631 break;
2633 case DW_OP_const8u:
2634 case DW_OP_const8s:
2635 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2636 fputc (',', asm_out_file);
2637 dw2_asm_output_data_raw (8, val1->v.val_int);
2638 break;
2640 case DW_OP_skip:
2641 case DW_OP_bra:
2643 int offset;
2645 gcc_assert (val1->val_class == dw_val_class_loc);
2646 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2648 fputc (',', asm_out_file);
2649 dw2_asm_output_data_raw (2, offset);
2651 break;
2653 case DW_OP_regx:
2655 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2656 gcc_assert (size_of_uleb128 (r)
2657 == size_of_uleb128 (val1->v.val_unsigned));
2658 fputc (',', asm_out_file);
2659 dw2_asm_output_data_uleb128_raw (r);
2661 break;
2663 case DW_OP_constu:
2664 case DW_OP_plus_uconst:
2665 case DW_OP_piece:
2666 fputc (',', asm_out_file);
2667 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2668 break;
2670 case DW_OP_bit_piece:
2671 fputc (',', asm_out_file);
2672 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2673 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2674 break;
2676 case DW_OP_consts:
2677 case DW_OP_breg0:
2678 case DW_OP_breg1:
2679 case DW_OP_breg2:
2680 case DW_OP_breg3:
2681 case DW_OP_breg4:
2682 case DW_OP_breg5:
2683 case DW_OP_breg6:
2684 case DW_OP_breg7:
2685 case DW_OP_breg8:
2686 case DW_OP_breg9:
2687 case DW_OP_breg10:
2688 case DW_OP_breg11:
2689 case DW_OP_breg12:
2690 case DW_OP_breg13:
2691 case DW_OP_breg14:
2692 case DW_OP_breg15:
2693 case DW_OP_breg16:
2694 case DW_OP_breg17:
2695 case DW_OP_breg18:
2696 case DW_OP_breg19:
2697 case DW_OP_breg20:
2698 case DW_OP_breg21:
2699 case DW_OP_breg22:
2700 case DW_OP_breg23:
2701 case DW_OP_breg24:
2702 case DW_OP_breg25:
2703 case DW_OP_breg26:
2704 case DW_OP_breg27:
2705 case DW_OP_breg28:
2706 case DW_OP_breg29:
2707 case DW_OP_breg30:
2708 case DW_OP_breg31:
2709 case DW_OP_fbreg:
2710 fputc (',', asm_out_file);
2711 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2712 break;
2714 case DW_OP_bregx:
2716 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2717 gcc_assert (size_of_uleb128 (r)
2718 == size_of_uleb128 (val1->v.val_unsigned));
2719 fputc (',', asm_out_file);
2720 dw2_asm_output_data_uleb128_raw (r);
2721 fputc (',', asm_out_file);
2722 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2724 break;
2726 case DW_OP_implicit_pointer:
2727 case DW_OP_entry_value:
2728 case DW_OP_const_type:
2729 case DW_OP_regval_type:
2730 case DW_OP_deref_type:
2731 case DW_OP_convert:
2732 case DW_OP_reinterpret:
2733 case DW_OP_GNU_implicit_pointer:
2734 case DW_OP_GNU_entry_value:
2735 case DW_OP_GNU_const_type:
2736 case DW_OP_GNU_regval_type:
2737 case DW_OP_GNU_deref_type:
2738 case DW_OP_GNU_convert:
2739 case DW_OP_GNU_reinterpret:
2740 case DW_OP_GNU_parameter_ref:
2741 gcc_unreachable ();
2742 break;
2744 default:
2745 /* Other codes have no operands. */
2746 break;
2750 void
2751 output_loc_sequence_raw (dw_loc_descr_ref loc)
2753 while (1)
2755 enum dwarf_location_atom opc = loc->dw_loc_opc;
2756 /* Output the opcode. */
2757 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2759 unsigned r = (opc - DW_OP_breg0);
2760 r = DWARF2_FRAME_REG_OUT (r, 1);
2761 gcc_assert (r <= 31);
2762 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2764 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2766 unsigned r = (opc - DW_OP_reg0);
2767 r = DWARF2_FRAME_REG_OUT (r, 1);
2768 gcc_assert (r <= 31);
2769 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2771 /* Output the opcode. */
2772 fprintf (asm_out_file, "%#x", opc);
2773 output_loc_operands_raw (loc);
2775 if (!loc->dw_loc_next)
2776 break;
2777 loc = loc->dw_loc_next;
2779 fputc (',', asm_out_file);
2783 /* This function builds a dwarf location descriptor sequence from a
2784 dw_cfa_location, adding the given OFFSET to the result of the
2785 expression. */
2787 struct dw_loc_descr_node *
2788 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2790 struct dw_loc_descr_node *head, *tmp;
2792 offset += cfa->offset;
2794 if (cfa->indirect)
2796 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2797 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2798 head->dw_loc_oprnd1.val_entry = NULL;
2799 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2800 add_loc_descr (&head, tmp);
2801 loc_descr_plus_const (&head, offset);
2803 else
2804 head = new_reg_loc_descr (cfa->reg, offset);
2806 return head;
2809 /* This function builds a dwarf location descriptor sequence for
2810 the address at OFFSET from the CFA when stack is aligned to
2811 ALIGNMENT byte. */
2813 struct dw_loc_descr_node *
2814 build_cfa_aligned_loc (dw_cfa_location *cfa,
2815 poly_int64 offset, HOST_WIDE_INT alignment)
2817 struct dw_loc_descr_node *head;
2818 unsigned int dwarf_fp
2819 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2821 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2822 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2824 head = new_reg_loc_descr (dwarf_fp, 0);
2825 add_loc_descr (&head, int_loc_descriptor (alignment));
2826 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2827 loc_descr_plus_const (&head, offset);
2829 else
2830 head = new_reg_loc_descr (dwarf_fp, offset);
2831 return head;
2834 /* And now, the support for symbolic debugging information. */
2836 /* .debug_str support. */
2838 static void dwarf2out_init (const char *);
2839 static void dwarf2out_finish (const char *);
2840 static void dwarf2out_early_finish (const char *);
2841 static void dwarf2out_assembly_start (void);
2842 static void dwarf2out_define (unsigned int, const char *);
2843 static void dwarf2out_undef (unsigned int, const char *);
2844 static void dwarf2out_start_source_file (unsigned, const char *);
2845 static void dwarf2out_end_source_file (unsigned);
2846 static void dwarf2out_function_decl (tree);
2847 static void dwarf2out_begin_block (unsigned, unsigned);
2848 static void dwarf2out_end_block (unsigned, unsigned);
2849 static bool dwarf2out_ignore_block (const_tree);
2850 static void dwarf2out_set_ignored_loc (unsigned, unsigned, const char *);
2851 static void dwarf2out_early_global_decl (tree);
2852 static void dwarf2out_late_global_decl (tree);
2853 static void dwarf2out_type_decl (tree, int);
2854 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2855 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2856 dw_die_ref);
2857 static void dwarf2out_abstract_function (tree);
2858 static void dwarf2out_var_location (rtx_insn *);
2859 static void dwarf2out_inline_entry (tree);
2860 static void dwarf2out_size_function (tree);
2861 static void dwarf2out_begin_function (tree);
2862 static void dwarf2out_end_function (unsigned int);
2863 static void dwarf2out_register_main_translation_unit (tree unit);
2864 static void dwarf2out_set_name (tree, tree);
2865 static void dwarf2out_register_external_die (tree decl, const char *sym,
2866 unsigned HOST_WIDE_INT off);
2867 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2868 unsigned HOST_WIDE_INT *off);
2870 /* The debug hooks structure. */
2872 const struct gcc_debug_hooks dwarf2_debug_hooks =
2874 dwarf2out_init,
2875 dwarf2out_finish,
2876 dwarf2out_early_finish,
2877 dwarf2out_assembly_start,
2878 dwarf2out_define,
2879 dwarf2out_undef,
2880 dwarf2out_start_source_file,
2881 dwarf2out_end_source_file,
2882 dwarf2out_begin_block,
2883 dwarf2out_end_block,
2884 dwarf2out_ignore_block,
2885 dwarf2out_source_line,
2886 dwarf2out_set_ignored_loc,
2887 dwarf2out_begin_prologue,
2888 #if VMS_DEBUGGING_INFO
2889 dwarf2out_vms_end_prologue,
2890 dwarf2out_vms_begin_epilogue,
2891 #else
2892 debug_nothing_int_charstar,
2893 debug_nothing_int_charstar,
2894 #endif
2895 dwarf2out_end_epilogue,
2896 dwarf2out_begin_function,
2897 dwarf2out_end_function, /* end_function */
2898 dwarf2out_register_main_translation_unit,
2899 dwarf2out_function_decl, /* function_decl */
2900 dwarf2out_early_global_decl,
2901 dwarf2out_late_global_decl,
2902 dwarf2out_type_decl, /* type_decl */
2903 dwarf2out_imported_module_or_decl,
2904 dwarf2out_die_ref_for_decl,
2905 dwarf2out_register_external_die,
2906 debug_nothing_tree, /* deferred_inline_function */
2907 /* The DWARF 2 backend tries to reduce debugging bloat by not
2908 emitting the abstract description of inline functions until
2909 something tries to reference them. */
2910 dwarf2out_abstract_function, /* outlining_inline_function */
2911 debug_nothing_rtx_code_label, /* label */
2912 debug_nothing_int, /* handle_pch */
2913 dwarf2out_var_location,
2914 dwarf2out_inline_entry, /* inline_entry */
2915 dwarf2out_size_function, /* size_function */
2916 dwarf2out_switch_text_section,
2917 dwarf2out_set_name,
2918 1, /* start_end_main_source_file */
2919 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2922 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2924 dwarf2out_init,
2925 debug_nothing_charstar,
2926 debug_nothing_charstar,
2927 dwarf2out_assembly_start,
2928 debug_nothing_int_charstar,
2929 debug_nothing_int_charstar,
2930 debug_nothing_int_charstar,
2931 debug_nothing_int,
2932 debug_nothing_int_int, /* begin_block */
2933 debug_nothing_int_int, /* end_block */
2934 debug_true_const_tree, /* ignore_block */
2935 dwarf2out_source_line, /* source_line */
2936 debug_nothing_int_int_charstar, /* set_ignored_loc */
2937 debug_nothing_int_int_charstar, /* begin_prologue */
2938 debug_nothing_int_charstar, /* end_prologue */
2939 debug_nothing_int_charstar, /* begin_epilogue */
2940 debug_nothing_int_charstar, /* end_epilogue */
2941 debug_nothing_tree, /* begin_function */
2942 debug_nothing_int, /* end_function */
2943 debug_nothing_tree, /* register_main_translation_unit */
2944 debug_nothing_tree, /* function_decl */
2945 debug_nothing_tree, /* early_global_decl */
2946 debug_nothing_tree, /* late_global_decl */
2947 debug_nothing_tree_int, /* type_decl */
2948 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2949 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2950 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2951 debug_nothing_tree, /* deferred_inline_function */
2952 debug_nothing_tree, /* outlining_inline_function */
2953 debug_nothing_rtx_code_label, /* label */
2954 debug_nothing_int, /* handle_pch */
2955 debug_nothing_rtx_insn, /* var_location */
2956 debug_nothing_tree, /* inline_entry */
2957 debug_nothing_tree, /* size_function */
2958 debug_nothing_void, /* switch_text_section */
2959 debug_nothing_tree_tree, /* set_name */
2960 0, /* start_end_main_source_file */
2961 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2964 /* NOTE: In the comments in this file, many references are made to
2965 "Debugging Information Entries". This term is abbreviated as `DIE'
2966 throughout the remainder of this file. */
2968 /* An internal representation of the DWARF output is built, and then
2969 walked to generate the DWARF debugging info. The walk of the internal
2970 representation is done after the entire program has been compiled.
2971 The types below are used to describe the internal representation. */
2973 /* Whether to put type DIEs into their own section .debug_types instead
2974 of making them part of the .debug_info section. Only supported for
2975 Dwarf V4 or higher and the user didn't disable them through
2976 -fno-debug-types-section. It is more efficient to put them in a
2977 separate comdat sections since the linker will then be able to
2978 remove duplicates. But not all tools support .debug_types sections
2979 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2980 it is DW_UT_type unit type in .debug_info section. For late LTO
2981 debug there should be almost no types emitted so avoid enabling
2982 -fdebug-types-section there. */
2984 #define use_debug_types (dwarf_version >= 4 \
2985 && flag_debug_types_section \
2986 && !in_lto_p)
2988 /* Various DIE's use offsets relative to the beginning of the
2989 .debug_info section to refer to each other. */
2991 typedef long int dw_offset;
2993 struct comdat_type_node;
2995 /* The entries in the line_info table more-or-less mirror the opcodes
2996 that are used in the real dwarf line table. Arrays of these entries
2997 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2998 supported. */
3000 enum dw_line_info_opcode {
3001 /* Emit DW_LNE_set_address; the operand is the label index. */
3002 LI_set_address,
3004 /* Emit a row to the matrix with the given line. This may be done
3005 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3006 special opcodes. */
3007 LI_set_line,
3009 /* Emit a DW_LNS_set_file. */
3010 LI_set_file,
3012 /* Emit a DW_LNS_set_column. */
3013 LI_set_column,
3015 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
3016 LI_negate_stmt,
3018 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
3019 LI_set_prologue_end,
3020 LI_set_epilogue_begin,
3022 /* Emit a DW_LNE_set_discriminator. */
3023 LI_set_discriminator,
3025 /* Output a Fixed Advance PC; the target PC is the label index; the
3026 base PC is the previous LI_adv_address or LI_set_address entry.
3027 We only use this when emitting debug views without assembler
3028 support, at explicit user request. Ideally, we should only use
3029 it when the offset might be zero but we can't tell: it's the only
3030 way to maybe change the PC without resetting the view number. */
3031 LI_adv_address
3034 typedef struct GTY(()) dw_line_info_struct {
3035 enum dw_line_info_opcode opcode;
3036 unsigned int val;
3037 } dw_line_info_entry;
3040 struct GTY(()) dw_line_info_table {
3041 /* The label that marks the end of this section. */
3042 const char *end_label;
3044 /* The values for the last row of the matrix, as collected in the table.
3045 These are used to minimize the changes to the next row. */
3046 unsigned int file_num;
3047 unsigned int line_num;
3048 unsigned int column_num;
3049 int discrim_num;
3050 bool is_stmt;
3051 bool in_use;
3053 /* This denotes the NEXT view number.
3055 If it is 0, it is known that the NEXT view will be the first view
3056 at the given PC.
3058 If it is -1, we're forcing the view number to be reset, e.g. at a
3059 function entry.
3061 The meaning of other nonzero values depends on whether we're
3062 computing views internally or leaving it for the assembler to do
3063 so. If we're emitting them internally, view denotes the view
3064 number since the last known advance of PC. If we're leaving it
3065 for the assembler, it denotes the LVU label number that we're
3066 going to ask the assembler to assign. */
3067 var_loc_view view;
3069 /* This counts the number of symbolic views emitted in this table
3070 since the latest view reset. Its max value, over all tables,
3071 sets symview_upper_bound. */
3072 var_loc_view symviews_since_reset;
3074 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
3075 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
3076 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
3077 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
3079 vec<dw_line_info_entry, va_gc> *entries;
3082 /* This is an upper bound for view numbers that the assembler may
3083 assign to symbolic views output in this translation. It is used to
3084 decide how big a field to use to represent view numbers in
3085 symview-classed attributes. */
3087 static var_loc_view symview_upper_bound;
3089 /* If we're keep track of location views and their reset points, and
3090 INSN is a reset point (i.e., it necessarily advances the PC), mark
3091 the next view in TABLE as reset. */
3093 static void
3094 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
3096 if (!debug_internal_reset_location_views)
3097 return;
3099 /* Maybe turn (part of?) this test into a default target hook. */
3100 int reset = 0;
3102 if (targetm.reset_location_view)
3103 reset = targetm.reset_location_view (insn);
3105 if (reset)
3107 else if (JUMP_TABLE_DATA_P (insn))
3108 reset = 1;
3109 else if (GET_CODE (insn) == USE
3110 || GET_CODE (insn) == CLOBBER
3111 || GET_CODE (insn) == ASM_INPUT
3112 || asm_noperands (insn) >= 0)
3114 else if (get_attr_min_length (insn) > 0)
3115 reset = 1;
3117 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3118 RESET_NEXT_VIEW (table->view);
3121 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3122 The children of each node form a circular list linked by
3123 die_sib. die_child points to the node *before* the "first" child node. */
3125 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3126 union die_symbol_or_type_node
3128 const char * GTY ((tag ("0"))) die_symbol;
3129 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3131 GTY ((desc ("%0.comdat_type_p"))) die_id;
3132 vec<dw_attr_node, va_gc> *die_attr;
3133 dw_die_ref die_parent;
3134 dw_die_ref die_child;
3135 dw_die_ref die_sib;
3136 dw_die_ref die_definition; /* ref from a specification to its definition */
3137 dw_offset die_offset;
3138 unsigned long die_abbrev;
3139 int die_mark;
3140 unsigned int decl_id;
3141 enum dwarf_tag die_tag;
3142 /* Die is used and must not be pruned as unused. */
3143 BOOL_BITFIELD die_perennial_p : 1;
3144 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3145 /* For an external ref to die_symbol if die_offset contains an extra
3146 offset to that symbol. */
3147 BOOL_BITFIELD with_offset : 1;
3148 /* Whether this DIE was removed from the DIE tree, for example via
3149 prune_unused_types. We don't consider those present from the
3150 DIE lookup routines. */
3151 BOOL_BITFIELD removed : 1;
3152 /* Lots of spare bits. */
3154 die_node;
3156 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3157 static bool early_dwarf;
3158 static bool early_dwarf_finished;
3159 class set_early_dwarf {
3160 public:
3161 bool saved;
3162 set_early_dwarf () : saved(early_dwarf)
3164 gcc_assert (! early_dwarf_finished);
3165 early_dwarf = true;
3167 ~set_early_dwarf () { early_dwarf = saved; }
3170 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3171 #define FOR_EACH_CHILD(die, c, expr) do { \
3172 c = die->die_child; \
3173 if (c) do { \
3174 c = c->die_sib; \
3175 expr; \
3176 } while (c != die->die_child); \
3177 } while (0)
3179 /* The pubname structure */
3181 typedef struct GTY(()) pubname_struct {
3182 dw_die_ref die;
3183 const char *name;
3185 pubname_entry;
3188 struct GTY(()) dw_ranges {
3189 const char *label;
3190 /* If this is positive, it's a block number, otherwise it's a
3191 bitwise-negated index into dw_ranges_by_label. */
3192 int num;
3193 /* If idx is equal to DW_RANGES_IDX_SKELETON, it should be emitted
3194 into .debug_rnglists section rather than .debug_rnglists.dwo
3195 for -gsplit-dwarf and DWARF >= 5. */
3196 #define DW_RANGES_IDX_SKELETON ((1U << 31) - 1)
3197 /* Index for the range list for DW_FORM_rnglistx. */
3198 unsigned int idx : 31;
3199 /* True if this range might be possibly in a different section
3200 from previous entry. */
3201 unsigned int maybe_new_sec : 1;
3202 addr_table_entry *begin_entry;
3203 addr_table_entry *end_entry;
3206 /* A structure to hold a macinfo entry. */
3208 typedef struct GTY(()) macinfo_struct {
3209 unsigned char code;
3210 unsigned HOST_WIDE_INT lineno;
3211 const char *info;
3213 macinfo_entry;
3216 struct GTY(()) dw_ranges_by_label {
3217 const char *begin;
3218 const char *end;
3221 /* The comdat type node structure. */
3222 struct GTY(()) comdat_type_node
3224 dw_die_ref root_die;
3225 dw_die_ref type_die;
3226 dw_die_ref skeleton_die;
3227 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3228 comdat_type_node *next;
3231 /* A list of DIEs for which we can't determine ancestry (parent_die
3232 field) just yet. Later in dwarf2out_finish we will fill in the
3233 missing bits. */
3234 typedef struct GTY(()) limbo_die_struct {
3235 dw_die_ref die;
3236 /* The tree for which this DIE was created. We use this to
3237 determine ancestry later. */
3238 tree created_for;
3239 struct limbo_die_struct *next;
3241 limbo_die_node;
3243 typedef struct skeleton_chain_struct
3245 dw_die_ref old_die;
3246 dw_die_ref new_die;
3247 struct skeleton_chain_struct *parent;
3249 skeleton_chain_node;
3251 /* Define a macro which returns nonzero for a TYPE_DECL which was
3252 implicitly generated for a type.
3254 Note that, unlike the C front-end (which generates a NULL named
3255 TYPE_DECL node for each complete tagged type, each array type,
3256 and each function type node created) the C++ front-end generates
3257 a _named_ TYPE_DECL node for each tagged type node created.
3258 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3259 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3260 front-end, but for each type, tagged or not. */
3262 #define TYPE_DECL_IS_STUB(decl) \
3263 (DECL_NAME (decl) == NULL_TREE \
3264 || (DECL_ARTIFICIAL (decl) \
3265 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3266 /* This is necessary for stub decls that \
3267 appear in nested inline functions. */ \
3268 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3269 && (decl_ultimate_origin (decl) \
3270 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3272 /* Information concerning the compilation unit's programming
3273 language, and compiler version. */
3275 /* Fixed size portion of the DWARF compilation unit header. */
3276 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3277 (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size \
3278 + (dwarf_version >= 5 ? 4 : 3))
3280 /* Fixed size portion of the DWARF comdat type unit header. */
3281 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3282 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3283 + DWARF_TYPE_SIGNATURE_SIZE + dwarf_offset_size)
3285 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3286 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3287 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3289 /* Fixed size portion of public names info. */
3290 #define DWARF_PUBNAMES_HEADER_SIZE (2 * dwarf_offset_size + 2)
3292 /* Fixed size portion of the address range info. */
3293 #define DWARF_ARANGES_HEADER_SIZE \
3294 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3295 DWARF2_ADDR_SIZE * 2) \
3296 - DWARF_INITIAL_LENGTH_SIZE)
3298 /* Size of padding portion in the address range info. It must be
3299 aligned to twice the pointer size. */
3300 #define DWARF_ARANGES_PAD_SIZE \
3301 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4, \
3302 DWARF2_ADDR_SIZE * 2) \
3303 - (DWARF_INITIAL_LENGTH_SIZE + dwarf_offset_size + 4))
3305 /* Use assembler line directives if available. */
3306 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3307 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3308 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3309 #else
3310 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3311 #endif
3312 #endif
3314 /* Use assembler views in line directives if available. */
3315 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3316 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3317 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3318 #else
3319 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3320 #endif
3321 #endif
3323 /* Return true if GCC configure detected assembler support for .loc. */
3325 bool
3326 dwarf2out_default_as_loc_support (void)
3328 return DWARF2_ASM_LINE_DEBUG_INFO;
3329 #if (GCC_VERSION >= 3000)
3330 # undef DWARF2_ASM_LINE_DEBUG_INFO
3331 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3332 #endif
3335 /* Return true if GCC configure detected assembler support for views
3336 in .loc directives. */
3338 bool
3339 dwarf2out_default_as_locview_support (void)
3341 return DWARF2_ASM_VIEW_DEBUG_INFO;
3342 #if (GCC_VERSION >= 3000)
3343 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3344 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3345 #endif
3348 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3349 view computation, and it refers to a view identifier for which we
3350 will not emit a label because it is known to map to a view number
3351 zero. We won't allocate the bitmap if we're not using assembler
3352 support for location views, but we have to make the variable
3353 visible for GGC and for code that will be optimized out for lack of
3354 support but that's still parsed and compiled. We could abstract it
3355 out with macros, but it's not worth it. */
3356 static GTY(()) bitmap zero_view_p;
3358 /* Evaluate to TRUE iff N is known to identify the first location view
3359 at its PC. When not using assembler location view computation,
3360 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3361 and views label numbers recorded in it are the ones known to be
3362 zero. */
3363 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3364 || (N) == (var_loc_view)-1 \
3365 || (zero_view_p \
3366 && bitmap_bit_p (zero_view_p, (N))))
3368 /* Return true iff we're to emit .loc directives for the assembler to
3369 generate line number sections.
3371 When we're not emitting views, all we need from the assembler is
3372 support for .loc directives.
3374 If we are emitting views, we can only use the assembler's .loc
3375 support if it also supports views.
3377 When the compiler is emitting the line number programs and
3378 computing view numbers itself, it resets view numbers at known PC
3379 changes and counts from that, and then it emits view numbers as
3380 literal constants in locviewlists. There are cases in which the
3381 compiler is not sure about PC changes, e.g. when extra alignment is
3382 requested for a label. In these cases, the compiler may not reset
3383 the view counter, and the potential PC advance in the line number
3384 program will use an opcode that does not reset the view counter
3385 even if the PC actually changes, so that compiler and debug info
3386 consumer can keep view numbers in sync.
3388 When the compiler defers view computation to the assembler, it
3389 emits symbolic view numbers in locviewlists, with the exception of
3390 views known to be zero (forced resets, or reset after
3391 compiler-visible PC changes): instead of emitting symbols for
3392 these, we emit literal zero and assert the assembler agrees with
3393 the compiler's assessment. We could use symbolic views everywhere,
3394 instead of special-casing zero views, but then we'd be unable to
3395 optimize out locviewlists that contain only zeros. */
3397 static bool
3398 output_asm_line_debug_info (void)
3400 return (dwarf2out_as_loc_support
3401 && (dwarf2out_as_locview_support
3402 || !debug_variable_location_views));
3405 static bool asm_outputs_debug_line_str (void);
3407 /* Minimum line offset in a special line info. opcode.
3408 This value was chosen to give a reasonable range of values. */
3409 #define DWARF_LINE_BASE -10
3411 /* First special line opcode - leave room for the standard opcodes. */
3412 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3414 /* Range of line offsets in a special line info. opcode. */
3415 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3417 /* Flag that indicates the initial value of the is_stmt_start flag.
3418 In the present implementation, we do not mark any lines as
3419 the beginning of a source statement, because that information
3420 is not made available by the GCC front-end. */
3421 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3423 /* Maximum number of operations per instruction bundle. */
3424 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3425 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3426 #endif
3428 /* This location is used by calc_die_sizes() to keep track
3429 the offset of each DIE within the .debug_info section. */
3430 static unsigned long next_die_offset;
3432 /* Record the root of the DIE's built for the current compilation unit. */
3433 static GTY(()) dw_die_ref single_comp_unit_die;
3435 /* A list of type DIEs that have been separated into comdat sections. */
3436 static GTY(()) comdat_type_node *comdat_type_list;
3438 /* A list of CU DIEs that have been separated. */
3439 static GTY(()) limbo_die_node *cu_die_list;
3441 /* A list of DIEs with a NULL parent waiting to be relocated. */
3442 static GTY(()) limbo_die_node *limbo_die_list;
3444 /* A list of DIEs for which we may have to generate
3445 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3446 static GTY(()) limbo_die_node *deferred_asm_name;
3448 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3450 typedef const char *compare_type;
3452 static hashval_t hash (dwarf_file_data *);
3453 static bool equal (dwarf_file_data *, const char *);
3456 /* Filenames referenced by this compilation unit. */
3457 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3459 struct decl_die_hasher : ggc_ptr_hash<die_node>
3461 typedef tree compare_type;
3463 static hashval_t hash (die_node *);
3464 static bool equal (die_node *, tree);
3466 /* A hash table of references to DIE's that describe declarations.
3467 The key is a DECL_UID() which is a unique number identifying each decl. */
3468 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3470 struct GTY ((for_user)) variable_value_struct {
3471 unsigned int decl_id;
3472 vec<dw_die_ref, va_gc> *dies;
3475 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3477 typedef tree compare_type;
3479 static hashval_t hash (variable_value_struct *);
3480 static bool equal (variable_value_struct *, tree);
3482 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3483 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3484 DECL_CONTEXT of the referenced VAR_DECLs. */
3485 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3487 struct block_die_hasher : ggc_ptr_hash<die_struct>
3489 static hashval_t hash (die_struct *);
3490 static bool equal (die_struct *, die_struct *);
3493 /* A hash table of references to DIE's that describe COMMON blocks.
3494 The key is DECL_UID() ^ die_parent. */
3495 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3497 typedef struct GTY(()) die_arg_entry_struct {
3498 dw_die_ref die;
3499 tree arg;
3500 } die_arg_entry;
3503 /* Node of the variable location list. */
3504 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3505 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3506 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3507 in mode of the EXPR_LIST node and first EXPR_LIST operand
3508 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3509 location or NULL for padding. For larger bitsizes,
3510 mode is 0 and first operand is a CONCAT with bitsize
3511 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3512 NULL as second operand. */
3513 rtx GTY (()) loc;
3514 const char * GTY (()) label;
3515 struct var_loc_node * GTY (()) next;
3516 var_loc_view view;
3519 /* Variable location list. */
3520 struct GTY ((for_user)) var_loc_list_def {
3521 struct var_loc_node * GTY (()) first;
3523 /* Pointer to the last but one or last element of the
3524 chained list. If the list is empty, both first and
3525 last are NULL, if the list contains just one node
3526 or the last node certainly is not redundant, it points
3527 to the last node, otherwise points to the last but one.
3528 Do not mark it for GC because it is marked through the chain. */
3529 struct var_loc_node * GTY ((skip ("%h"))) last;
3531 /* Pointer to the last element before section switch,
3532 if NULL, either sections weren't switched or first
3533 is after section switch. */
3534 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3536 /* DECL_UID of the variable decl. */
3537 unsigned int decl_id;
3539 typedef struct var_loc_list_def var_loc_list;
3541 /* Call argument location list. */
3542 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3543 rtx GTY (()) call_arg_loc_note;
3544 const char * GTY (()) label;
3545 tree GTY (()) block;
3546 bool tail_call_p;
3547 rtx GTY (()) symbol_ref;
3548 struct call_arg_loc_node * GTY (()) next;
3552 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3554 typedef const_tree compare_type;
3556 static hashval_t hash (var_loc_list *);
3557 static bool equal (var_loc_list *, const_tree);
3560 /* Table of decl location linked lists. */
3561 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3563 /* Head and tail of call_arg_loc chain. */
3564 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3565 static struct call_arg_loc_node *call_arg_loc_last;
3567 /* Number of call sites in the current function. */
3568 static int call_site_count = -1;
3569 /* Number of tail call sites in the current function. */
3570 static int tail_call_site_count = -1;
3572 /* A cached location list. */
3573 struct GTY ((for_user)) cached_dw_loc_list_def {
3574 /* The DECL_UID of the decl that this entry describes. */
3575 unsigned int decl_id;
3577 /* The cached location list. */
3578 dw_loc_list_ref loc_list;
3580 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3582 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3585 typedef const_tree compare_type;
3587 static hashval_t hash (cached_dw_loc_list *);
3588 static bool equal (cached_dw_loc_list *, const_tree);
3591 /* Table of cached location lists. */
3592 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3594 /* A vector of references to DIE's that are uniquely identified by their tag,
3595 presence/absence of children DIE's, and list of attribute/value pairs. */
3596 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3598 /* A hash map to remember the stack usage for DWARF procedures. The value
3599 stored is the stack size difference between before the DWARF procedure
3600 invokation and after it returned. In other words, for a DWARF procedure
3601 that consumes N stack slots and that pushes M ones, this stores M - N. */
3602 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3604 /* A global counter for generating labels for line number data. */
3605 static unsigned int line_info_label_num;
3607 /* The current table to which we should emit line number information
3608 for the current function. This will be set up at the beginning of
3609 assembly for the function. */
3610 static GTY(()) dw_line_info_table *cur_line_info_table;
3612 /* The two default tables of line number info. */
3613 static GTY(()) dw_line_info_table *text_section_line_info;
3614 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3616 /* The set of all non-default tables of line number info. */
3617 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3619 /* A flag to tell pubnames/types export if there is an info section to
3620 refer to. */
3621 static bool info_section_emitted;
3623 /* A pointer to the base of a table that contains a list of publicly
3624 accessible names. */
3625 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3627 /* A pointer to the base of a table that contains a list of publicly
3628 accessible types. */
3629 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3631 /* A pointer to the base of a table that contains a list of macro
3632 defines/undefines (and file start/end markers). */
3633 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3635 /* True if .debug_macinfo or .debug_macros section is going to be
3636 emitted. */
3637 #define have_macinfo \
3638 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3639 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3640 && !macinfo_table->is_empty ())
3642 /* Vector of dies for which we should generate .debug_ranges info. */
3643 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3645 /* Vector of pairs of labels referenced in ranges_table. */
3646 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3648 /* Whether we have location lists that need outputting */
3649 static GTY(()) bool have_location_lists;
3651 /* Unique label counter. */
3652 static GTY(()) unsigned int loclabel_num;
3654 /* Unique label counter for point-of-call tables. */
3655 static GTY(()) unsigned int poc_label_num;
3657 /* The last file entry emitted by maybe_emit_file(). */
3658 static GTY(()) struct dwarf_file_data * last_emitted_file;
3660 /* Number of internal labels generated by gen_internal_sym(). */
3661 static GTY(()) int label_num;
3663 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3665 /* Instances of generic types for which we need to generate debug
3666 info that describe their generic parameters and arguments. That
3667 generation needs to happen once all types are properly laid out so
3668 we do it at the end of compilation. */
3669 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3671 /* Offset from the "steady-state frame pointer" to the frame base,
3672 within the current function. */
3673 static poly_int64 frame_pointer_fb_offset;
3674 static bool frame_pointer_fb_offset_valid;
3676 static vec<dw_die_ref> base_types;
3678 /* Flags to represent a set of attribute classes for attributes that represent
3679 a scalar value (bounds, pointers, ...). */
3680 enum dw_scalar_form
3682 dw_scalar_form_constant = 0x01,
3683 dw_scalar_form_exprloc = 0x02,
3684 dw_scalar_form_reference = 0x04
3687 /* Forward declarations for functions defined in this file. */
3689 static int is_pseudo_reg (const_rtx);
3690 static tree type_main_variant (tree);
3691 static int is_tagged_type (const_tree);
3692 static const char *dwarf_tag_name (unsigned);
3693 static const char *dwarf_attr_name (unsigned);
3694 static const char *dwarf_form_name (unsigned);
3695 static tree decl_ultimate_origin (const_tree);
3696 static tree decl_class_context (tree);
3697 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3698 static inline unsigned int AT_index (dw_attr_node *);
3699 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3700 static inline unsigned AT_flag (dw_attr_node *);
3701 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3702 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3703 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3704 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3705 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3706 unsigned int, unsigned char *);
3707 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3708 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3709 static inline const char *AT_string (dw_attr_node *);
3710 static enum dwarf_form AT_string_form (dw_attr_node *);
3711 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3712 static void add_AT_specification (dw_die_ref, dw_die_ref);
3713 static inline dw_die_ref AT_ref (dw_attr_node *);
3714 static inline int AT_ref_external (dw_attr_node *);
3715 static inline void set_AT_ref_external (dw_attr_node *, int);
3716 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3717 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3718 dw_loc_list_ref);
3719 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3720 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3721 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3722 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3723 static void remove_addr_table_entry (addr_table_entry *);
3724 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3725 static inline rtx AT_addr (dw_attr_node *);
3726 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3727 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3728 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3729 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3730 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3731 unsigned long, bool);
3732 static inline const char *AT_lbl (dw_attr_node *);
3733 static const char *get_AT_low_pc (dw_die_ref);
3734 static bool is_c (void);
3735 static bool is_cxx (void);
3736 static bool is_cxx (const_tree);
3737 static bool is_fortran (void);
3738 static bool is_ada (void);
3739 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3740 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3741 static void add_child_die (dw_die_ref, dw_die_ref);
3742 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3743 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3744 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3745 static void equate_type_number_to_die (tree, dw_die_ref);
3746 static var_loc_list *lookup_decl_loc (const_tree);
3747 static void equate_decl_number_to_die (tree, dw_die_ref);
3748 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3749 static void print_spaces (FILE *);
3750 static void print_die (dw_die_ref, FILE *);
3751 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3752 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3753 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3754 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3755 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3756 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3757 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3758 struct md5_ctx *, int *);
3759 struct checksum_attributes;
3760 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3761 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3762 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3763 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3764 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3765 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3766 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3767 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3768 static int is_type_die (dw_die_ref);
3769 static inline bool is_template_instantiation (dw_die_ref);
3770 static int is_declaration_die (dw_die_ref);
3771 static int should_move_die_to_comdat (dw_die_ref);
3772 static dw_die_ref clone_as_declaration (dw_die_ref);
3773 static dw_die_ref clone_die (dw_die_ref);
3774 static dw_die_ref clone_tree (dw_die_ref);
3775 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3776 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3777 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3778 static dw_die_ref generate_skeleton (dw_die_ref);
3779 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3780 dw_die_ref,
3781 dw_die_ref);
3782 static void break_out_comdat_types (dw_die_ref);
3783 static void copy_decls_for_unworthy_types (dw_die_ref);
3785 static void add_sibling_attributes (dw_die_ref);
3786 static void output_location_lists (dw_die_ref);
3787 static int constant_size (unsigned HOST_WIDE_INT);
3788 static unsigned long size_of_die (dw_die_ref);
3789 static void calc_die_sizes (dw_die_ref);
3790 static void calc_base_type_die_sizes (void);
3791 static void mark_dies (dw_die_ref);
3792 static void unmark_dies (dw_die_ref);
3793 static void unmark_all_dies (dw_die_ref);
3794 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3795 static unsigned long size_of_aranges (void);
3796 static enum dwarf_form value_format (dw_attr_node *);
3797 static void output_value_format (dw_attr_node *);
3798 static void output_abbrev_section (void);
3799 static void output_die_abbrevs (unsigned long, dw_die_ref);
3800 static void output_die (dw_die_ref);
3801 static void output_compilation_unit_header (enum dwarf_unit_type);
3802 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3803 static void output_comdat_type_unit (comdat_type_node *, bool);
3804 static const char *dwarf2_name (tree, int);
3805 static void add_pubname (tree, dw_die_ref);
3806 static void add_enumerator_pubname (const char *, dw_die_ref);
3807 static void add_pubname_string (const char *, dw_die_ref);
3808 static void add_pubtype (tree, dw_die_ref);
3809 static void output_pubnames (vec<pubname_entry, va_gc> *);
3810 static void output_aranges (void);
3811 static unsigned int add_ranges (const_tree, bool = false);
3812 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3813 bool *, bool);
3814 static void output_ranges (void);
3815 static dw_line_info_table *new_line_info_table (void);
3816 static void output_line_info (bool);
3817 static void output_file_names (void);
3818 static int is_base_type (tree);
3819 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3820 static int decl_quals (const_tree);
3821 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3822 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3823 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3824 static unsigned int dbx_reg_number (const_rtx);
3825 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3826 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3827 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3828 enum var_init_status);
3829 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3830 enum var_init_status);
3831 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3832 enum var_init_status);
3833 static int is_based_loc (const_rtx);
3834 static bool resolve_one_addr (rtx *);
3835 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3836 enum var_init_status);
3837 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3838 enum var_init_status);
3839 struct loc_descr_context;
3840 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3841 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3842 static dw_loc_list_ref loc_list_from_tree (tree, int,
3843 struct loc_descr_context *);
3844 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3845 struct loc_descr_context *);
3846 static tree field_type (const_tree);
3847 static unsigned int simple_type_align_in_bits (const_tree);
3848 static unsigned int simple_decl_align_in_bits (const_tree);
3849 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3850 struct vlr_context;
3851 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3852 HOST_WIDE_INT *);
3853 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3854 dw_loc_list_ref);
3855 static void add_data_member_location_attribute (dw_die_ref, tree,
3856 struct vlr_context *);
3857 static bool add_const_value_attribute (dw_die_ref, machine_mode, rtx);
3858 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3859 static void insert_wide_int (const wide_int &, unsigned char *, int);
3860 static unsigned insert_float (const_rtx, unsigned char *);
3861 static rtx rtl_for_decl_location (tree);
3862 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3863 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3864 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3865 static void add_desc_attribute (dw_die_ref, tree);
3866 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3867 static void add_comp_dir_attribute (dw_die_ref);
3868 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3869 struct loc_descr_context *);
3870 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3871 struct loc_descr_context *);
3872 static void add_subscript_info (dw_die_ref, tree, bool);
3873 static void add_byte_size_attribute (dw_die_ref, tree);
3874 static void add_alignment_attribute (dw_die_ref, tree);
3875 static void add_bit_offset_attribute (dw_die_ref, tree);
3876 static void add_bit_size_attribute (dw_die_ref, tree);
3877 static void add_prototyped_attribute (dw_die_ref, tree);
3878 static void add_abstract_origin_attribute (dw_die_ref, tree);
3879 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3880 static void add_src_coords_attributes (dw_die_ref, tree);
3881 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3882 static void add_discr_value (dw_die_ref, dw_discr_value *);
3883 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3884 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3885 static dw_die_ref scope_die_for (tree, dw_die_ref);
3886 static inline int local_scope_p (dw_die_ref);
3887 static inline int class_scope_p (dw_die_ref);
3888 static inline int class_or_namespace_scope_p (dw_die_ref);
3889 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3890 static void add_calling_convention_attribute (dw_die_ref, tree);
3891 static const char *type_tag (const_tree);
3892 static tree member_declared_type (const_tree);
3893 #if 0
3894 static const char *decl_start_label (tree);
3895 #endif
3896 static void gen_array_type_die (tree, dw_die_ref);
3897 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3898 #if 0
3899 static void gen_entry_point_die (tree, dw_die_ref);
3900 #endif
3901 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3902 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3903 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3904 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3905 static void gen_formal_types_die (tree, dw_die_ref);
3906 static void gen_subprogram_die (tree, dw_die_ref);
3907 static void gen_variable_die (tree, tree, dw_die_ref);
3908 static void gen_const_die (tree, dw_die_ref);
3909 static void gen_label_die (tree, dw_die_ref);
3910 static void gen_lexical_block_die (tree, dw_die_ref);
3911 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3912 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3913 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3914 static dw_die_ref gen_compile_unit_die (const char *);
3915 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3916 static void gen_member_die (tree, dw_die_ref);
3917 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3918 enum debug_info_usage);
3919 static void gen_subroutine_type_die (tree, dw_die_ref);
3920 static void gen_typedef_die (tree, dw_die_ref);
3921 static void gen_type_die (tree, dw_die_ref);
3922 static void gen_block_die (tree, dw_die_ref);
3923 static void decls_for_scope (tree, dw_die_ref, bool = true);
3924 static bool is_naming_typedef_decl (const_tree);
3925 static inline dw_die_ref get_context_die (tree);
3926 static void gen_namespace_die (tree, dw_die_ref);
3927 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3928 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3929 static dw_die_ref force_decl_die (tree);
3930 static dw_die_ref force_type_die (tree);
3931 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3932 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3933 static struct dwarf_file_data * lookup_filename (const char *);
3934 static void retry_incomplete_types (void);
3935 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3936 static void gen_generic_params_dies (tree);
3937 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3938 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3939 static void splice_child_die (dw_die_ref, dw_die_ref);
3940 static int file_info_cmp (const void *, const void *);
3941 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3942 const char *, var_loc_view, const char *);
3943 static void output_loc_list (dw_loc_list_ref);
3944 static char *gen_internal_sym (const char *);
3945 static bool want_pubnames (void);
3947 static void prune_unmark_dies (dw_die_ref);
3948 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3949 static void prune_unused_types_mark (dw_die_ref, int);
3950 static void prune_unused_types_walk (dw_die_ref);
3951 static void prune_unused_types_walk_attribs (dw_die_ref);
3952 static void prune_unused_types_prune (dw_die_ref);
3953 static void prune_unused_types (void);
3954 static int maybe_emit_file (struct dwarf_file_data *fd);
3955 static inline const char *AT_vms_delta1 (dw_attr_node *);
3956 static inline const char *AT_vms_delta2 (dw_attr_node *);
3957 #if VMS_DEBUGGING_INFO
3958 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3959 const char *, const char *);
3960 #endif
3961 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3962 static void gen_remaining_tmpl_value_param_die_attribute (void);
3963 static bool generic_type_p (tree);
3964 static void schedule_generic_params_dies_gen (tree t);
3965 static void gen_scheduled_generic_parms_dies (void);
3966 static void resolve_variable_values (void);
3968 static const char *comp_dir_string (void);
3970 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3972 /* enum for tracking thread-local variables whose address is really an offset
3973 relative to the TLS pointer, which will need link-time relocation, but will
3974 not need relocation by the DWARF consumer. */
3976 enum dtprel_bool
3978 dtprel_false = 0,
3979 dtprel_true = 1
3982 /* Return the operator to use for an address of a variable. For dtprel_true, we
3983 use DW_OP_const*. For regular variables, which need both link-time
3984 relocation and consumer-level relocation (e.g., to account for shared objects
3985 loaded at a random address), we use DW_OP_addr*. */
3987 static inline enum dwarf_location_atom
3988 dw_addr_op (enum dtprel_bool dtprel)
3990 if (dtprel == dtprel_true)
3991 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3992 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3993 else
3994 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3997 /* Return a pointer to a newly allocated address location description. If
3998 dwarf_split_debug_info is true, then record the address with the appropriate
3999 relocation. */
4000 static inline dw_loc_descr_ref
4001 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
4003 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
4005 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
4006 ref->dw_loc_oprnd1.v.val_addr = addr;
4007 ref->dtprel = dtprel;
4008 if (dwarf_split_debug_info)
4009 ref->dw_loc_oprnd1.val_entry
4010 = add_addr_table_entry (addr,
4011 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
4012 else
4013 ref->dw_loc_oprnd1.val_entry = NULL;
4015 return ref;
4018 /* Section names used to hold DWARF debugging information. */
4020 #ifndef DEBUG_INFO_SECTION
4021 #define DEBUG_INFO_SECTION ".debug_info"
4022 #endif
4023 #ifndef DEBUG_DWO_INFO_SECTION
4024 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
4025 #endif
4026 #ifndef DEBUG_LTO_INFO_SECTION
4027 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
4028 #endif
4029 #ifndef DEBUG_LTO_DWO_INFO_SECTION
4030 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
4031 #endif
4032 #ifndef DEBUG_ABBREV_SECTION
4033 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
4034 #endif
4035 #ifndef DEBUG_LTO_ABBREV_SECTION
4036 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
4037 #endif
4038 #ifndef DEBUG_DWO_ABBREV_SECTION
4039 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
4040 #endif
4041 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
4042 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
4043 #endif
4044 #ifndef DEBUG_ARANGES_SECTION
4045 #define DEBUG_ARANGES_SECTION ".debug_aranges"
4046 #endif
4047 #ifndef DEBUG_ADDR_SECTION
4048 #define DEBUG_ADDR_SECTION ".debug_addr"
4049 #endif
4050 #ifndef DEBUG_MACINFO_SECTION
4051 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
4052 #endif
4053 #ifndef DEBUG_LTO_MACINFO_SECTION
4054 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
4055 #endif
4056 #ifndef DEBUG_DWO_MACINFO_SECTION
4057 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4058 #endif
4059 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4060 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4061 #endif
4062 #ifndef DEBUG_MACRO_SECTION
4063 #define DEBUG_MACRO_SECTION ".debug_macro"
4064 #endif
4065 #ifndef DEBUG_LTO_MACRO_SECTION
4066 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4067 #endif
4068 #ifndef DEBUG_DWO_MACRO_SECTION
4069 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4070 #endif
4071 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4072 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4073 #endif
4074 #ifndef DEBUG_LINE_SECTION
4075 #define DEBUG_LINE_SECTION ".debug_line"
4076 #endif
4077 #ifndef DEBUG_LTO_LINE_SECTION
4078 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4079 #endif
4080 #ifndef DEBUG_DWO_LINE_SECTION
4081 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4082 #endif
4083 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4084 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4085 #endif
4086 #ifndef DEBUG_LOC_SECTION
4087 #define DEBUG_LOC_SECTION ".debug_loc"
4088 #endif
4089 #ifndef DEBUG_DWO_LOC_SECTION
4090 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4091 #endif
4092 #ifndef DEBUG_LOCLISTS_SECTION
4093 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4094 #endif
4095 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4096 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4097 #endif
4098 #ifndef DEBUG_PUBNAMES_SECTION
4099 #define DEBUG_PUBNAMES_SECTION \
4100 ((debug_generate_pub_sections == 2) \
4101 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4102 #endif
4103 #ifndef DEBUG_PUBTYPES_SECTION
4104 #define DEBUG_PUBTYPES_SECTION \
4105 ((debug_generate_pub_sections == 2) \
4106 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4107 #endif
4108 #ifndef DEBUG_STR_OFFSETS_SECTION
4109 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4110 #endif
4111 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4112 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4113 #endif
4114 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4115 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4116 #endif
4117 #ifndef DEBUG_STR_SECTION
4118 #define DEBUG_STR_SECTION ".debug_str"
4119 #endif
4120 #ifndef DEBUG_LTO_STR_SECTION
4121 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4122 #endif
4123 #ifndef DEBUG_STR_DWO_SECTION
4124 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4125 #endif
4126 #ifndef DEBUG_LTO_STR_DWO_SECTION
4127 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4128 #endif
4129 #ifndef DEBUG_RANGES_SECTION
4130 #define DEBUG_RANGES_SECTION ".debug_ranges"
4131 #endif
4132 #ifndef DEBUG_RNGLISTS_SECTION
4133 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4134 #endif
4135 #ifndef DEBUG_DWO_RNGLISTS_SECTION
4136 #define DEBUG_DWO_RNGLISTS_SECTION ".debug_rnglists.dwo"
4137 #endif
4138 #ifndef DEBUG_LINE_STR_SECTION
4139 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4140 #endif
4141 #ifndef DEBUG_LTO_LINE_STR_SECTION
4142 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4143 #endif
4145 /* Standard ELF section names for compiled code and data. */
4146 #ifndef TEXT_SECTION_NAME
4147 #define TEXT_SECTION_NAME ".text"
4148 #endif
4150 /* Section flags for .debug_str section. */
4151 #define DEBUG_STR_SECTION_FLAGS \
4152 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4153 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4154 : SECTION_DEBUG)
4156 /* Section flags for .debug_str.dwo section. */
4157 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4159 /* Attribute used to refer to the macro section. */
4160 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4161 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4163 /* Labels we insert at beginning sections we can reference instead of
4164 the section names themselves. */
4166 #ifndef TEXT_SECTION_LABEL
4167 #define TEXT_SECTION_LABEL "Ltext"
4168 #endif
4169 #ifndef COLD_TEXT_SECTION_LABEL
4170 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4171 #endif
4172 #ifndef DEBUG_LINE_SECTION_LABEL
4173 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4174 #endif
4175 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4176 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4177 #endif
4178 #ifndef DEBUG_INFO_SECTION_LABEL
4179 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4180 #endif
4181 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4182 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4183 #endif
4184 #ifndef DEBUG_ABBREV_SECTION_LABEL
4185 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4186 #endif
4187 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4188 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4189 #endif
4190 #ifndef DEBUG_ADDR_SECTION_LABEL
4191 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4192 #endif
4193 #ifndef DEBUG_LOC_SECTION_LABEL
4194 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4195 #endif
4196 #ifndef DEBUG_RANGES_SECTION_LABEL
4197 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4198 #endif
4199 #ifndef DEBUG_MACINFO_SECTION_LABEL
4200 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4201 #endif
4202 #ifndef DEBUG_MACRO_SECTION_LABEL
4203 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4204 #endif
4205 #define SKELETON_COMP_DIE_ABBREV 1
4206 #define SKELETON_TYPE_DIE_ABBREV 2
4208 /* Definitions of defaults for formats and names of various special
4209 (artificial) labels which may be generated within this file (when the -g
4210 options is used and DWARF2_DEBUGGING_INFO is in effect.
4211 If necessary, these may be overridden from within the tm.h file, but
4212 typically, overriding these defaults is unnecessary. */
4214 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4215 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4216 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4217 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4218 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4219 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4220 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4221 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4222 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4223 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4224 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4225 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4226 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4227 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4228 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4230 #ifndef TEXT_END_LABEL
4231 #define TEXT_END_LABEL "Letext"
4232 #endif
4233 #ifndef COLD_END_LABEL
4234 #define COLD_END_LABEL "Letext_cold"
4235 #endif
4236 #ifndef BLOCK_BEGIN_LABEL
4237 #define BLOCK_BEGIN_LABEL "LBB"
4238 #endif
4239 #ifndef BLOCK_INLINE_ENTRY_LABEL
4240 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4241 #endif
4242 #ifndef BLOCK_END_LABEL
4243 #define BLOCK_END_LABEL "LBE"
4244 #endif
4245 #ifndef LINE_CODE_LABEL
4246 #define LINE_CODE_LABEL "LM"
4247 #endif
4250 /* Return the root of the DIE's built for the current compilation unit. */
4251 static dw_die_ref
4252 comp_unit_die (void)
4254 if (!single_comp_unit_die)
4255 single_comp_unit_die = gen_compile_unit_die (NULL);
4256 return single_comp_unit_die;
4259 /* We allow a language front-end to designate a function that is to be
4260 called to "demangle" any name before it is put into a DIE. */
4262 static const char *(*demangle_name_func) (const char *);
4264 void
4265 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4267 demangle_name_func = func;
4270 /* Test if rtl node points to a pseudo register. */
4272 static inline int
4273 is_pseudo_reg (const_rtx rtl)
4275 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4276 || (GET_CODE (rtl) == SUBREG
4277 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4280 /* Return a reference to a type, with its const and volatile qualifiers
4281 removed. */
4283 static inline tree
4284 type_main_variant (tree type)
4286 type = TYPE_MAIN_VARIANT (type);
4288 /* ??? There really should be only one main variant among any group of
4289 variants of a given type (and all of the MAIN_VARIANT values for all
4290 members of the group should point to that one type) but sometimes the C
4291 front-end messes this up for array types, so we work around that bug
4292 here. */
4293 if (TREE_CODE (type) == ARRAY_TYPE)
4294 while (type != TYPE_MAIN_VARIANT (type))
4295 type = TYPE_MAIN_VARIANT (type);
4297 return type;
4300 /* Return nonzero if the given type node represents a tagged type. */
4302 static inline int
4303 is_tagged_type (const_tree type)
4305 enum tree_code code = TREE_CODE (type);
4307 return (code == RECORD_TYPE || code == UNION_TYPE
4308 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4311 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4313 static void
4314 get_ref_die_offset_label (char *label, dw_die_ref ref)
4316 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4319 /* Return die_offset of a DIE reference to a base type. */
4321 static unsigned long int
4322 get_base_type_offset (dw_die_ref ref)
4324 if (ref->die_offset)
4325 return ref->die_offset;
4326 if (comp_unit_die ()->die_abbrev)
4328 calc_base_type_die_sizes ();
4329 gcc_assert (ref->die_offset);
4331 return ref->die_offset;
4334 /* Return die_offset of a DIE reference other than base type. */
4336 static unsigned long int
4337 get_ref_die_offset (dw_die_ref ref)
4339 gcc_assert (ref->die_offset);
4340 return ref->die_offset;
4343 /* Convert a DIE tag into its string name. */
4345 static const char *
4346 dwarf_tag_name (unsigned int tag)
4348 const char *name = get_DW_TAG_name (tag);
4350 if (name != NULL)
4351 return name;
4353 return "DW_TAG_<unknown>";
4356 /* Convert a DWARF attribute code into its string name. */
4358 static const char *
4359 dwarf_attr_name (unsigned int attr)
4361 const char *name;
4363 switch (attr)
4365 #if VMS_DEBUGGING_INFO
4366 case DW_AT_HP_prologue:
4367 return "DW_AT_HP_prologue";
4368 #else
4369 case DW_AT_MIPS_loop_unroll_factor:
4370 return "DW_AT_MIPS_loop_unroll_factor";
4371 #endif
4373 #if VMS_DEBUGGING_INFO
4374 case DW_AT_HP_epilogue:
4375 return "DW_AT_HP_epilogue";
4376 #else
4377 case DW_AT_MIPS_stride:
4378 return "DW_AT_MIPS_stride";
4379 #endif
4382 name = get_DW_AT_name (attr);
4384 if (name != NULL)
4385 return name;
4387 return "DW_AT_<unknown>";
4390 /* Convert a DWARF value form code into its string name. */
4392 static const char *
4393 dwarf_form_name (unsigned int form)
4395 const char *name = get_DW_FORM_name (form);
4397 if (name != NULL)
4398 return name;
4400 return "DW_FORM_<unknown>";
4403 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4404 instance of an inlined instance of a decl which is local to an inline
4405 function, so we have to trace all of the way back through the origin chain
4406 to find out what sort of node actually served as the original seed for the
4407 given block. */
4409 static tree
4410 decl_ultimate_origin (const_tree decl)
4412 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4413 return NULL_TREE;
4415 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4416 we're trying to output the abstract instance of this function. */
4417 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4418 return NULL_TREE;
4420 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4421 most distant ancestor, this should never happen. */
4422 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4424 return DECL_ABSTRACT_ORIGIN (decl);
4427 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4428 of a virtual function may refer to a base class, so we check the 'this'
4429 parameter. */
4431 static tree
4432 decl_class_context (tree decl)
4434 tree context = NULL_TREE;
4436 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4437 context = DECL_CONTEXT (decl);
4438 else
4439 context = TYPE_MAIN_VARIANT
4440 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4442 if (context && !TYPE_P (context))
4443 context = NULL_TREE;
4445 return context;
4448 /* Add an attribute/value pair to a DIE. */
4450 static inline void
4451 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4453 /* Maybe this should be an assert? */
4454 if (die == NULL)
4455 return;
4457 if (flag_checking)
4459 /* Check we do not add duplicate attrs. Can't use get_AT here
4460 because that recurses to the specification/abstract origin DIE. */
4461 dw_attr_node *a;
4462 unsigned ix;
4463 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4464 gcc_assert (a->dw_attr != attr->dw_attr);
4467 vec_safe_reserve (die->die_attr, 1);
4468 vec_safe_push (die->die_attr, *attr);
4471 enum dw_val_class
4472 AT_class (dw_attr_node *a)
4474 return a->dw_attr_val.val_class;
4477 /* Return the index for any attribute that will be referenced with a
4478 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4479 indices are stored in dw_attr_val.v.val_str for reference counting
4480 pruning. */
4482 static inline unsigned int
4483 AT_index (dw_attr_node *a)
4485 if (AT_class (a) == dw_val_class_str)
4486 return a->dw_attr_val.v.val_str->index;
4487 else if (a->dw_attr_val.val_entry != NULL)
4488 return a->dw_attr_val.val_entry->index;
4489 return NOT_INDEXED;
4492 /* Add a flag value attribute to a DIE. */
4494 static inline void
4495 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4497 dw_attr_node attr;
4499 attr.dw_attr = attr_kind;
4500 attr.dw_attr_val.val_class = dw_val_class_flag;
4501 attr.dw_attr_val.val_entry = NULL;
4502 attr.dw_attr_val.v.val_flag = flag;
4503 add_dwarf_attr (die, &attr);
4506 static inline unsigned
4507 AT_flag (dw_attr_node *a)
4509 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4510 return a->dw_attr_val.v.val_flag;
4513 /* Add a signed integer attribute value to a DIE. */
4515 static inline void
4516 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4518 dw_attr_node attr;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_const;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_int = int_val;
4524 add_dwarf_attr (die, &attr);
4527 HOST_WIDE_INT
4528 AT_int (dw_attr_node *a)
4530 gcc_assert (a && (AT_class (a) == dw_val_class_const
4531 || AT_class (a) == dw_val_class_const_implicit));
4532 return a->dw_attr_val.v.val_int;
4535 /* Add an unsigned integer attribute value to a DIE. */
4537 static inline void
4538 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4539 unsigned HOST_WIDE_INT unsigned_val)
4541 dw_attr_node attr;
4543 attr.dw_attr = attr_kind;
4544 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4545 attr.dw_attr_val.val_entry = NULL;
4546 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4547 add_dwarf_attr (die, &attr);
4550 unsigned HOST_WIDE_INT
4551 AT_unsigned (dw_attr_node *a)
4553 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4554 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4555 return a->dw_attr_val.v.val_unsigned;
4558 /* Add an unsigned wide integer attribute value to a DIE. */
4560 static inline void
4561 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4562 const wide_int& w)
4564 dw_attr_node attr;
4566 attr.dw_attr = attr_kind;
4567 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4568 attr.dw_attr_val.val_entry = NULL;
4569 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4570 *attr.dw_attr_val.v.val_wide = w;
4571 add_dwarf_attr (die, &attr);
4574 /* Add an unsigned double integer attribute value to a DIE. */
4576 static inline void
4577 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4578 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_const_double;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_double.high = high;
4586 attr.dw_attr_val.v.val_double.low = low;
4587 add_dwarf_attr (die, &attr);
4590 /* Add a floating point attribute value to a DIE and return it. */
4592 static inline void
4593 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4594 unsigned int length, unsigned int elt_size, unsigned char *array)
4596 dw_attr_node attr;
4598 attr.dw_attr = attr_kind;
4599 attr.dw_attr_val.val_class = dw_val_class_vec;
4600 attr.dw_attr_val.val_entry = NULL;
4601 attr.dw_attr_val.v.val_vec.length = length;
4602 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4603 attr.dw_attr_val.v.val_vec.array = array;
4604 add_dwarf_attr (die, &attr);
4607 /* Add an 8-byte data attribute value to a DIE. */
4609 static inline void
4610 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4611 unsigned char data8[8])
4613 dw_attr_node attr;
4615 attr.dw_attr = attr_kind;
4616 attr.dw_attr_val.val_class = dw_val_class_data8;
4617 attr.dw_attr_val.val_entry = NULL;
4618 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4619 add_dwarf_attr (die, &attr);
4622 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4623 dwarf_split_debug_info, address attributes in dies destined for the
4624 final executable have force_direct set to avoid using indexed
4625 references. */
4627 static inline void
4628 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4629 bool force_direct)
4631 dw_attr_node attr;
4632 char * lbl_id;
4634 lbl_id = xstrdup (lbl_low);
4635 attr.dw_attr = DW_AT_low_pc;
4636 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4637 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4638 if (dwarf_split_debug_info && !force_direct)
4639 attr.dw_attr_val.val_entry
4640 = add_addr_table_entry (lbl_id, ate_kind_label);
4641 else
4642 attr.dw_attr_val.val_entry = NULL;
4643 add_dwarf_attr (die, &attr);
4645 attr.dw_attr = DW_AT_high_pc;
4646 if (dwarf_version < 4)
4647 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4648 else
4649 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4650 lbl_id = xstrdup (lbl_high);
4651 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4652 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4653 && dwarf_split_debug_info && !force_direct)
4654 attr.dw_attr_val.val_entry
4655 = add_addr_table_entry (lbl_id, ate_kind_label);
4656 else
4657 attr.dw_attr_val.val_entry = NULL;
4658 add_dwarf_attr (die, &attr);
4661 /* Hash and equality functions for debug_str_hash. */
4663 hashval_t
4664 indirect_string_hasher::hash (indirect_string_node *x)
4666 return htab_hash_string (x->str);
4669 bool
4670 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4672 return strcmp (x1->str, x2) == 0;
4675 /* Add STR to the given string hash table. */
4677 static struct indirect_string_node *
4678 find_AT_string_in_table (const char *str,
4679 hash_table<indirect_string_hasher> *table,
4680 enum insert_option insert = INSERT)
4682 struct indirect_string_node *node;
4684 indirect_string_node **slot
4685 = table->find_slot_with_hash (str, htab_hash_string (str), insert);
4686 if (*slot == NULL)
4688 node = ggc_cleared_alloc<indirect_string_node> ();
4689 node->str = ggc_strdup (str);
4690 *slot = node;
4692 else
4693 node = *slot;
4695 node->refcount++;
4696 return node;
4699 /* Add STR to the indirect string hash table. */
4701 static struct indirect_string_node *
4702 find_AT_string (const char *str, enum insert_option insert = INSERT)
4704 if (! debug_str_hash)
4705 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4707 return find_AT_string_in_table (str, debug_str_hash, insert);
4710 /* Add a string attribute value to a DIE. */
4712 static inline void
4713 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4715 dw_attr_node attr;
4716 struct indirect_string_node *node;
4718 node = find_AT_string (str);
4720 attr.dw_attr = attr_kind;
4721 attr.dw_attr_val.val_class = dw_val_class_str;
4722 attr.dw_attr_val.val_entry = NULL;
4723 attr.dw_attr_val.v.val_str = node;
4724 add_dwarf_attr (die, &attr);
4727 static inline const char *
4728 AT_string (dw_attr_node *a)
4730 gcc_assert (a && AT_class (a) == dw_val_class_str);
4731 return a->dw_attr_val.v.val_str->str;
4734 /* Call this function directly to bypass AT_string_form's logic to put
4735 the string inline in the die. */
4737 static void
4738 set_indirect_string (struct indirect_string_node *node)
4740 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4741 /* Already indirect is a no op. */
4742 if (node->form == DW_FORM_strp
4743 || node->form == DW_FORM_line_strp
4744 || node->form == dwarf_FORM (DW_FORM_strx))
4746 gcc_assert (node->label);
4747 return;
4749 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4750 ++dw2_string_counter;
4751 node->label = xstrdup (label);
4753 if (!dwarf_split_debug_info)
4755 node->form = DW_FORM_strp;
4756 node->index = NOT_INDEXED;
4758 else
4760 node->form = dwarf_FORM (DW_FORM_strx);
4761 node->index = NO_INDEX_ASSIGNED;
4765 /* A helper function for dwarf2out_finish, called to reset indirect
4766 string decisions done for early LTO dwarf output before fat object
4767 dwarf output. */
4770 reset_indirect_string (indirect_string_node **h, void *)
4772 struct indirect_string_node *node = *h;
4773 if (node->form == DW_FORM_strp
4774 || node->form == DW_FORM_line_strp
4775 || node->form == dwarf_FORM (DW_FORM_strx))
4777 free (node->label);
4778 node->label = NULL;
4779 node->form = (dwarf_form) 0;
4780 node->index = 0;
4782 return 1;
4785 /* Add a string representing a file or filepath attribute value to a DIE. */
4787 static inline void
4788 add_filepath_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
4789 const char *str)
4791 if (! asm_outputs_debug_line_str ())
4792 add_AT_string (die, attr_kind, str);
4793 else
4795 dw_attr_node attr;
4796 struct indirect_string_node *node;
4798 if (!debug_line_str_hash)
4799 debug_line_str_hash
4800 = hash_table<indirect_string_hasher>::create_ggc (10);
4802 node = find_AT_string_in_table (str, debug_line_str_hash);
4803 set_indirect_string (node);
4804 node->form = DW_FORM_line_strp;
4806 attr.dw_attr = attr_kind;
4807 attr.dw_attr_val.val_class = dw_val_class_str;
4808 attr.dw_attr_val.val_entry = NULL;
4809 attr.dw_attr_val.v.val_str = node;
4810 add_dwarf_attr (die, &attr);
4814 /* Find out whether a string should be output inline in DIE
4815 or out-of-line in .debug_str section. */
4817 static enum dwarf_form
4818 find_string_form (struct indirect_string_node *node)
4820 unsigned int len;
4822 if (node->form)
4823 return node->form;
4825 len = strlen (node->str) + 1;
4827 /* If the string is shorter or equal to the size of the reference, it is
4828 always better to put it inline. */
4829 if (len <= (unsigned) dwarf_offset_size || node->refcount == 0)
4830 return node->form = DW_FORM_string;
4832 /* If we cannot expect the linker to merge strings in .debug_str
4833 section, only put it into .debug_str if it is worth even in this
4834 single module. */
4835 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4836 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4837 && (len - dwarf_offset_size) * node->refcount <= len))
4838 return node->form = DW_FORM_string;
4840 set_indirect_string (node);
4842 return node->form;
4845 /* Find out whether the string referenced from the attribute should be
4846 output inline in DIE or out-of-line in .debug_str section. */
4848 static enum dwarf_form
4849 AT_string_form (dw_attr_node *a)
4851 gcc_assert (a && AT_class (a) == dw_val_class_str);
4852 return find_string_form (a->dw_attr_val.v.val_str);
4855 /* Add a DIE reference attribute value to a DIE. */
4857 static inline void
4858 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4860 dw_attr_node attr;
4861 gcc_checking_assert (targ_die != NULL);
4863 /* With LTO we can end up trying to reference something we didn't create
4864 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4865 if (targ_die == NULL)
4866 return;
4868 attr.dw_attr = attr_kind;
4869 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4870 attr.dw_attr_val.val_entry = NULL;
4871 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4872 attr.dw_attr_val.v.val_die_ref.external = 0;
4873 add_dwarf_attr (die, &attr);
4876 /* Change DIE reference REF to point to NEW_DIE instead. */
4878 static inline void
4879 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4881 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4882 ref->dw_attr_val.v.val_die_ref.die = new_die;
4883 ref->dw_attr_val.v.val_die_ref.external = 0;
4886 /* Add an AT_specification attribute to a DIE, and also make the back
4887 pointer from the specification to the definition. */
4889 static inline void
4890 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4892 add_AT_die_ref (die, DW_AT_specification, targ_die);
4893 gcc_assert (!targ_die->die_definition);
4894 targ_die->die_definition = die;
4897 static inline dw_die_ref
4898 AT_ref (dw_attr_node *a)
4900 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4901 return a->dw_attr_val.v.val_die_ref.die;
4904 static inline int
4905 AT_ref_external (dw_attr_node *a)
4907 if (a && AT_class (a) == dw_val_class_die_ref)
4908 return a->dw_attr_val.v.val_die_ref.external;
4910 return 0;
4913 static inline void
4914 set_AT_ref_external (dw_attr_node *a, int i)
4916 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4917 a->dw_attr_val.v.val_die_ref.external = i;
4920 /* Add a location description attribute value to a DIE. */
4922 static inline void
4923 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4925 dw_attr_node attr;
4927 attr.dw_attr = attr_kind;
4928 attr.dw_attr_val.val_class = dw_val_class_loc;
4929 attr.dw_attr_val.val_entry = NULL;
4930 attr.dw_attr_val.v.val_loc = loc;
4931 add_dwarf_attr (die, &attr);
4934 dw_loc_descr_ref
4935 AT_loc (dw_attr_node *a)
4937 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4938 return a->dw_attr_val.v.val_loc;
4941 static inline void
4942 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4944 dw_attr_node attr;
4946 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4947 return;
4949 attr.dw_attr = attr_kind;
4950 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4951 attr.dw_attr_val.val_entry = NULL;
4952 attr.dw_attr_val.v.val_loc_list = loc_list;
4953 add_dwarf_attr (die, &attr);
4954 have_location_lists = true;
4957 static inline dw_loc_list_ref
4958 AT_loc_list (dw_attr_node *a)
4960 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4961 return a->dw_attr_val.v.val_loc_list;
4964 /* Add a view list attribute to DIE. It must have a DW_AT_location
4965 attribute, because the view list complements the location list. */
4967 static inline void
4968 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4970 dw_attr_node attr;
4972 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4973 return;
4975 attr.dw_attr = attr_kind;
4976 attr.dw_attr_val.val_class = dw_val_class_view_list;
4977 attr.dw_attr_val.val_entry = NULL;
4978 attr.dw_attr_val.v.val_view_list = die;
4979 add_dwarf_attr (die, &attr);
4980 gcc_checking_assert (get_AT (die, DW_AT_location));
4981 gcc_assert (have_location_lists);
4984 /* Return a pointer to the location list referenced by the attribute.
4985 If the named attribute is a view list, look up the corresponding
4986 DW_AT_location attribute and return its location list. */
4988 static inline dw_loc_list_ref *
4989 AT_loc_list_ptr (dw_attr_node *a)
4991 gcc_assert (a);
4992 switch (AT_class (a))
4994 case dw_val_class_loc_list:
4995 return &a->dw_attr_val.v.val_loc_list;
4996 case dw_val_class_view_list:
4998 dw_attr_node *l;
4999 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
5000 if (!l)
5001 return NULL;
5002 gcc_checking_assert (l + 1 == a);
5003 return AT_loc_list_ptr (l);
5005 default:
5006 gcc_unreachable ();
5010 /* Return the location attribute value associated with a view list
5011 attribute value. */
5013 static inline dw_val_node *
5014 view_list_to_loc_list_val_node (dw_val_node *val)
5016 gcc_assert (val->val_class == dw_val_class_view_list);
5017 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
5018 if (!loc)
5019 return NULL;
5020 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
5021 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
5022 return &loc->dw_attr_val;
5025 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
5027 static hashval_t hash (addr_table_entry *);
5028 static bool equal (addr_table_entry *, addr_table_entry *);
5031 /* Table of entries into the .debug_addr section. */
5033 static GTY (()) hash_table<addr_hasher> *addr_index_table;
5035 /* Hash an address_table_entry. */
5037 hashval_t
5038 addr_hasher::hash (addr_table_entry *a)
5040 inchash::hash hstate;
5041 switch (a->kind)
5043 case ate_kind_rtx:
5044 hstate.add_int (0);
5045 break;
5046 case ate_kind_rtx_dtprel:
5047 hstate.add_int (1);
5048 break;
5049 case ate_kind_label:
5050 return htab_hash_string (a->addr.label);
5051 default:
5052 gcc_unreachable ();
5054 inchash::add_rtx (a->addr.rtl, hstate);
5055 return hstate.end ();
5058 /* Determine equality for two address_table_entries. */
5060 bool
5061 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
5063 if (a1->kind != a2->kind)
5064 return 0;
5065 switch (a1->kind)
5067 case ate_kind_rtx:
5068 case ate_kind_rtx_dtprel:
5069 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
5070 case ate_kind_label:
5071 return strcmp (a1->addr.label, a2->addr.label) == 0;
5072 default:
5073 gcc_unreachable ();
5077 /* Initialize an addr_table_entry. */
5079 void
5080 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
5082 e->kind = kind;
5083 switch (kind)
5085 case ate_kind_rtx:
5086 case ate_kind_rtx_dtprel:
5087 e->addr.rtl = (rtx) addr;
5088 break;
5089 case ate_kind_label:
5090 e->addr.label = (char *) addr;
5091 break;
5093 e->refcount = 0;
5094 e->index = NO_INDEX_ASSIGNED;
5097 /* Add attr to the address table entry to the table. Defer setting an
5098 index until output time. */
5100 static addr_table_entry *
5101 add_addr_table_entry (void *addr, enum ate_kind kind)
5103 addr_table_entry *node;
5104 addr_table_entry finder;
5106 gcc_assert (dwarf_split_debug_info);
5107 if (! addr_index_table)
5108 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5109 init_addr_table_entry (&finder, kind, addr);
5110 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5112 if (*slot == HTAB_EMPTY_ENTRY)
5114 node = ggc_cleared_alloc<addr_table_entry> ();
5115 init_addr_table_entry (node, kind, addr);
5116 *slot = node;
5118 else
5119 node = *slot;
5121 node->refcount++;
5122 return node;
5125 /* Remove an entry from the addr table by decrementing its refcount.
5126 Strictly, decrementing the refcount would be enough, but the
5127 assertion that the entry is actually in the table has found
5128 bugs. */
5130 static void
5131 remove_addr_table_entry (addr_table_entry *entry)
5133 gcc_assert (dwarf_split_debug_info && addr_index_table);
5134 /* After an index is assigned, the table is frozen. */
5135 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5136 entry->refcount--;
5139 /* Given a location list, remove all addresses it refers to from the
5140 address_table. */
5142 static void
5143 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5145 for (; descr; descr = descr->dw_loc_next)
5146 if (descr->dw_loc_oprnd1.val_entry != NULL)
5148 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5149 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5153 /* A helper function for dwarf2out_finish called through
5154 htab_traverse. Assign an addr_table_entry its index. All entries
5155 must be collected into the table when this function is called,
5156 because the indexing code relies on htab_traverse to traverse nodes
5157 in the same order for each run. */
5160 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5162 addr_table_entry *node = *h;
5164 /* Don't index unreferenced nodes. */
5165 if (node->refcount == 0)
5166 return 1;
5168 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5169 node->index = *index;
5170 *index += 1;
5172 return 1;
5175 /* Return the tag of a given DIE. */
5177 enum dwarf_tag
5178 dw_get_die_tag (dw_die_ref die)
5180 return die->die_tag;
5183 /* Return a reference to the children list of a given DIE. */
5185 dw_die_ref
5186 dw_get_die_child (dw_die_ref die)
5188 return die->die_child;
5191 /* Return a reference to the sibling of a given DIE. */
5193 dw_die_ref
5194 dw_get_die_sib (dw_die_ref die)
5196 return die->die_sib;
5199 /* Add an address constant attribute value to a DIE. When using
5200 dwarf_split_debug_info, address attributes in dies destined for the
5201 final executable should be direct references--setting the parameter
5202 force_direct ensures this behavior. */
5204 static inline void
5205 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5206 bool force_direct)
5208 dw_attr_node attr;
5210 attr.dw_attr = attr_kind;
5211 attr.dw_attr_val.val_class = dw_val_class_addr;
5212 attr.dw_attr_val.v.val_addr = addr;
5213 if (dwarf_split_debug_info && !force_direct)
5214 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5215 else
5216 attr.dw_attr_val.val_entry = NULL;
5217 add_dwarf_attr (die, &attr);
5220 /* Get the RTX from to an address DIE attribute. */
5222 static inline rtx
5223 AT_addr (dw_attr_node *a)
5225 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5226 return a->dw_attr_val.v.val_addr;
5229 /* Add a file attribute value to a DIE. */
5231 static inline void
5232 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5233 struct dwarf_file_data *fd)
5235 dw_attr_node attr;
5237 attr.dw_attr = attr_kind;
5238 attr.dw_attr_val.val_class = dw_val_class_file;
5239 attr.dw_attr_val.val_entry = NULL;
5240 attr.dw_attr_val.v.val_file = fd;
5241 add_dwarf_attr (die, &attr);
5244 /* Get the dwarf_file_data from a file DIE attribute. */
5246 static inline struct dwarf_file_data *
5247 AT_file (dw_attr_node *a)
5249 gcc_assert (a && (AT_class (a) == dw_val_class_file
5250 || AT_class (a) == dw_val_class_file_implicit));
5251 return a->dw_attr_val.v.val_file;
5254 #if VMS_DEBUGGING_INFO
5255 /* Add a vms delta attribute value to a DIE. */
5257 static inline void
5258 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5259 const char *lbl1, const char *lbl2)
5261 dw_attr_node attr;
5263 attr.dw_attr = attr_kind;
5264 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5265 attr.dw_attr_val.val_entry = NULL;
5266 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5267 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5268 add_dwarf_attr (die, &attr);
5270 #endif
5272 /* Add a symbolic view identifier attribute value to a DIE. */
5274 static inline void
5275 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5276 const char *view_label)
5278 dw_attr_node attr;
5280 attr.dw_attr = attr_kind;
5281 attr.dw_attr_val.val_class = dw_val_class_symview;
5282 attr.dw_attr_val.val_entry = NULL;
5283 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5284 add_dwarf_attr (die, &attr);
5287 /* Add a label identifier attribute value to a DIE. */
5289 static inline void
5290 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5291 const char *lbl_id)
5293 dw_attr_node attr;
5295 attr.dw_attr = attr_kind;
5296 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5297 attr.dw_attr_val.val_entry = NULL;
5298 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5299 if (dwarf_split_debug_info)
5300 attr.dw_attr_val.val_entry
5301 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5302 ate_kind_label);
5303 add_dwarf_attr (die, &attr);
5306 /* Add a section offset attribute value to a DIE, an offset into the
5307 debug_line section. */
5309 static inline void
5310 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5311 const char *label)
5313 dw_attr_node attr;
5315 attr.dw_attr = attr_kind;
5316 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5317 attr.dw_attr_val.val_entry = NULL;
5318 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5319 add_dwarf_attr (die, &attr);
5322 /* Add a section offset attribute value to a DIE, an offset into the
5323 debug_macinfo section. */
5325 static inline void
5326 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5327 const char *label)
5329 dw_attr_node attr;
5331 attr.dw_attr = attr_kind;
5332 attr.dw_attr_val.val_class = dw_val_class_macptr;
5333 attr.dw_attr_val.val_entry = NULL;
5334 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5335 add_dwarf_attr (die, &attr);
5338 /* Add a range_list attribute value to a DIE. When using
5339 dwarf_split_debug_info, address attributes in dies destined for the
5340 final executable should be direct references--setting the parameter
5341 force_direct ensures this behavior. */
5343 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5344 #define RELOCATED_OFFSET (NULL)
5346 static void
5347 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5348 long unsigned int offset, bool force_direct)
5350 dw_attr_node attr;
5352 attr.dw_attr = attr_kind;
5353 attr.dw_attr_val.val_class = dw_val_class_range_list;
5354 /* For the range_list attribute, use val_entry to store whether the
5355 offset should follow split-debug-info or normal semantics. This
5356 value is read in output_range_list_offset. */
5357 if (dwarf_split_debug_info && !force_direct)
5358 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5359 else
5360 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5361 attr.dw_attr_val.v.val_offset = offset;
5362 add_dwarf_attr (die, &attr);
5365 /* Return the start label of a delta attribute. */
5367 static inline const char *
5368 AT_vms_delta1 (dw_attr_node *a)
5370 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5371 return a->dw_attr_val.v.val_vms_delta.lbl1;
5374 /* Return the end label of a delta attribute. */
5376 static inline const char *
5377 AT_vms_delta2 (dw_attr_node *a)
5379 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5380 return a->dw_attr_val.v.val_vms_delta.lbl2;
5383 static inline const char *
5384 AT_lbl (dw_attr_node *a)
5386 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5387 || AT_class (a) == dw_val_class_lineptr
5388 || AT_class (a) == dw_val_class_macptr
5389 || AT_class (a) == dw_val_class_loclistsptr
5390 || AT_class (a) == dw_val_class_high_pc));
5391 return a->dw_attr_val.v.val_lbl_id;
5394 /* Get the attribute of type attr_kind. */
5396 dw_attr_node *
5397 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5399 dw_attr_node *a;
5400 unsigned ix;
5401 dw_die_ref spec = NULL;
5403 if (! die)
5404 return NULL;
5406 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5407 if (a->dw_attr == attr_kind)
5408 return a;
5409 else if (a->dw_attr == DW_AT_specification
5410 || a->dw_attr == DW_AT_abstract_origin)
5411 spec = AT_ref (a);
5413 if (spec)
5414 return get_AT (spec, attr_kind);
5416 return NULL;
5419 /* Returns the parent of the declaration of DIE. */
5421 static dw_die_ref
5422 get_die_parent (dw_die_ref die)
5424 dw_die_ref t;
5426 if (!die)
5427 return NULL;
5429 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5430 || (t = get_AT_ref (die, DW_AT_specification)))
5431 die = t;
5433 return die->die_parent;
5436 /* Return the "low pc" attribute value, typically associated with a subprogram
5437 DIE. Return null if the "low pc" attribute is either not present, or if it
5438 cannot be represented as an assembler label identifier. */
5440 static inline const char *
5441 get_AT_low_pc (dw_die_ref die)
5443 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5445 return a ? AT_lbl (a) : NULL;
5448 /* Return the value of the string attribute designated by ATTR_KIND, or
5449 NULL if it is not present. */
5451 const char *
5452 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5454 dw_attr_node *a = get_AT (die, attr_kind);
5456 return a ? AT_string (a) : NULL;
5459 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5460 if it is not present. */
5463 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5465 dw_attr_node *a = get_AT (die, attr_kind);
5467 return a ? AT_flag (a) : 0;
5470 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5471 if it is not present. */
5473 unsigned
5474 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5476 dw_attr_node *a = get_AT (die, attr_kind);
5478 return a ? AT_unsigned (a) : 0;
5481 dw_die_ref
5482 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5484 dw_attr_node *a = get_AT (die, attr_kind);
5486 return a ? AT_ref (a) : NULL;
5489 struct dwarf_file_data *
5490 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5492 dw_attr_node *a = get_AT (die, attr_kind);
5494 return a ? AT_file (a) : NULL;
5497 /* Return TRUE if the language is C. */
5499 static inline bool
5500 is_c (void)
5502 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5504 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5505 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5510 /* Return TRUE if the language is C++. */
5512 static inline bool
5513 is_cxx (void)
5515 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5517 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5518 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5521 /* Return TRUE if DECL was created by the C++ frontend. */
5523 static bool
5524 is_cxx (const_tree decl)
5526 if (in_lto_p)
5528 const_tree context = get_ultimate_context (decl);
5529 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5530 return startswith (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++");
5532 return is_cxx ();
5535 /* Return TRUE if the language is Fortran. */
5537 static inline bool
5538 is_fortran (void)
5540 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5542 return (lang == DW_LANG_Fortran77
5543 || lang == DW_LANG_Fortran90
5544 || lang == DW_LANG_Fortran95
5545 || lang == DW_LANG_Fortran03
5546 || lang == DW_LANG_Fortran08);
5549 static inline bool
5550 is_fortran (const_tree decl)
5552 if (in_lto_p)
5554 const_tree context = get_ultimate_context (decl);
5555 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5556 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5557 "GNU Fortran", 11) == 0
5558 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5559 "GNU F77") == 0);
5561 return is_fortran ();
5564 /* Return TRUE if the language is Ada. */
5566 static inline bool
5567 is_ada (void)
5569 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5571 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5574 /* Return TRUE if the language is D. */
5576 static inline bool
5577 is_dlang (void)
5579 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5581 return lang == DW_LANG_D;
5584 /* Remove the specified attribute if present. Return TRUE if removal
5585 was successful. */
5587 static bool
5588 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5590 dw_attr_node *a;
5591 unsigned ix;
5593 if (! die)
5594 return false;
5596 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5597 if (a->dw_attr == attr_kind)
5599 if (AT_class (a) == dw_val_class_str)
5600 if (a->dw_attr_val.v.val_str->refcount)
5601 a->dw_attr_val.v.val_str->refcount--;
5603 /* vec::ordered_remove should help reduce the number of abbrevs
5604 that are needed. */
5605 die->die_attr->ordered_remove (ix);
5606 return true;
5608 return false;
5611 /* Remove CHILD from its parent. PREV must have the property that
5612 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5614 static void
5615 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5617 gcc_assert (child->die_parent == prev->die_parent);
5618 gcc_assert (prev->die_sib == child);
5619 if (prev == child)
5621 gcc_assert (child->die_parent->die_child == child);
5622 prev = NULL;
5624 else
5625 prev->die_sib = child->die_sib;
5626 if (child->die_parent->die_child == child)
5627 child->die_parent->die_child = prev;
5628 child->die_sib = NULL;
5631 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5632 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5634 static void
5635 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5637 dw_die_ref parent = old_child->die_parent;
5639 gcc_assert (parent == prev->die_parent);
5640 gcc_assert (prev->die_sib == old_child);
5642 new_child->die_parent = parent;
5643 if (prev == old_child)
5645 gcc_assert (parent->die_child == old_child);
5646 new_child->die_sib = new_child;
5648 else
5650 prev->die_sib = new_child;
5651 new_child->die_sib = old_child->die_sib;
5653 if (old_child->die_parent->die_child == old_child)
5654 old_child->die_parent->die_child = new_child;
5655 old_child->die_sib = NULL;
5658 /* Move all children from OLD_PARENT to NEW_PARENT. */
5660 static void
5661 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5663 dw_die_ref c;
5664 new_parent->die_child = old_parent->die_child;
5665 old_parent->die_child = NULL;
5666 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5669 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5670 matches TAG. */
5672 static void
5673 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5675 dw_die_ref c;
5677 c = die->die_child;
5678 if (c) do {
5679 dw_die_ref prev = c;
5680 c = c->die_sib;
5681 while (c->die_tag == tag)
5683 remove_child_with_prev (c, prev);
5684 c->die_parent = NULL;
5685 /* Might have removed every child. */
5686 if (die->die_child == NULL)
5687 return;
5688 c = prev->die_sib;
5690 } while (c != die->die_child);
5693 /* Add a CHILD_DIE as the last child of DIE. */
5695 static void
5696 add_child_die (dw_die_ref die, dw_die_ref child_die)
5698 /* FIXME this should probably be an assert. */
5699 if (! die || ! child_die)
5700 return;
5701 gcc_assert (die != child_die);
5703 child_die->die_parent = die;
5704 if (die->die_child)
5706 child_die->die_sib = die->die_child->die_sib;
5707 die->die_child->die_sib = child_die;
5709 else
5710 child_die->die_sib = child_die;
5711 die->die_child = child_die;
5714 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5716 static void
5717 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5718 dw_die_ref after_die)
5720 gcc_assert (die
5721 && child_die
5722 && after_die
5723 && die->die_child
5724 && die != child_die);
5726 child_die->die_parent = die;
5727 child_die->die_sib = after_die->die_sib;
5728 after_die->die_sib = child_die;
5729 if (die->die_child == after_die)
5730 die->die_child = child_die;
5733 /* Unassociate CHILD from its parent, and make its parent be
5734 NEW_PARENT. */
5736 static void
5737 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5739 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5740 if (p->die_sib == child)
5742 remove_child_with_prev (child, p);
5743 break;
5745 add_child_die (new_parent, child);
5748 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5749 is the specification, to the end of PARENT's list of children.
5750 This is done by removing and re-adding it. */
5752 static void
5753 splice_child_die (dw_die_ref parent, dw_die_ref child)
5755 /* We want the declaration DIE from inside the class, not the
5756 specification DIE at toplevel. */
5757 if (child->die_parent != parent)
5759 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5761 if (tmp)
5762 child = tmp;
5765 gcc_assert (child->die_parent == parent
5766 || (child->die_parent
5767 == get_AT_ref (parent, DW_AT_specification)));
5769 reparent_child (child, parent);
5772 /* Create and return a new die with TAG_VALUE as tag. */
5774 dw_die_ref
5775 new_die_raw (enum dwarf_tag tag_value)
5777 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5778 die->die_tag = tag_value;
5779 return die;
5782 /* Create and return a new die with a parent of PARENT_DIE. If
5783 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5784 associated tree T must be supplied to determine parenthood
5785 later. */
5787 static inline dw_die_ref
5788 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5790 dw_die_ref die = new_die_raw (tag_value);
5792 if (parent_die != NULL)
5793 add_child_die (parent_die, die);
5794 else
5796 limbo_die_node *limbo_node;
5798 /* No DIEs created after early dwarf should end up in limbo,
5799 because the limbo list should not persist past LTO
5800 streaming. */
5801 if (tag_value != DW_TAG_compile_unit
5802 /* These are allowed because they're generated while
5803 breaking out COMDAT units late. */
5804 && tag_value != DW_TAG_type_unit
5805 && tag_value != DW_TAG_skeleton_unit
5806 && !early_dwarf
5807 /* Allow nested functions to live in limbo because they will
5808 only temporarily live there, as decls_for_scope will fix
5809 them up. */
5810 && (TREE_CODE (t) != FUNCTION_DECL
5811 || !decl_function_context (t))
5812 /* Same as nested functions above but for types. Types that
5813 are local to a function will be fixed in
5814 decls_for_scope. */
5815 && (!RECORD_OR_UNION_TYPE_P (t)
5816 || !TYPE_CONTEXT (t)
5817 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5818 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5819 especially in the ltrans stage, but once we implement LTO
5820 dwarf streaming, we should remove this exception. */
5821 && !in_lto_p)
5823 fprintf (stderr, "symbol ended up in limbo too late:");
5824 debug_generic_stmt (t);
5825 gcc_unreachable ();
5828 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5829 limbo_node->die = die;
5830 limbo_node->created_for = t;
5831 limbo_node->next = limbo_die_list;
5832 limbo_die_list = limbo_node;
5835 return die;
5838 /* Return the DIE associated with the given type specifier. */
5840 dw_die_ref
5841 lookup_type_die (tree type)
5843 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5844 if (die && die->removed)
5846 TYPE_SYMTAB_DIE (type) = NULL;
5847 return NULL;
5849 return die;
5852 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5853 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5854 anonymous type instead the one of the naming typedef. */
5856 static inline dw_die_ref
5857 strip_naming_typedef (tree type, dw_die_ref type_die)
5859 if (type
5860 && TREE_CODE (type) == RECORD_TYPE
5861 && type_die
5862 && type_die->die_tag == DW_TAG_typedef
5863 && is_naming_typedef_decl (TYPE_NAME (type)))
5864 type_die = get_AT_ref (type_die, DW_AT_type);
5865 return type_die;
5868 /* Like lookup_type_die, but if type is an anonymous type named by a
5869 typedef[1], return the DIE of the anonymous type instead the one of
5870 the naming typedef. This is because in gen_typedef_die, we did
5871 equate the anonymous struct named by the typedef with the DIE of
5872 the naming typedef. So by default, lookup_type_die on an anonymous
5873 struct yields the DIE of the naming typedef.
5875 [1]: Read the comment of is_naming_typedef_decl to learn about what
5876 a naming typedef is. */
5878 static inline dw_die_ref
5879 lookup_type_die_strip_naming_typedef (tree type)
5881 dw_die_ref die = lookup_type_die (type);
5882 return strip_naming_typedef (type, die);
5885 /* Equate a DIE to a given type specifier. */
5887 static inline void
5888 equate_type_number_to_die (tree type, dw_die_ref type_die)
5890 TYPE_SYMTAB_DIE (type) = type_die;
5893 static dw_die_ref maybe_create_die_with_external_ref (tree);
5894 struct GTY(()) sym_off_pair
5896 const char * GTY((skip)) sym;
5897 unsigned HOST_WIDE_INT off;
5899 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5901 /* Returns a hash value for X (which really is a die_struct). */
5903 inline hashval_t
5904 decl_die_hasher::hash (die_node *x)
5906 return (hashval_t) x->decl_id;
5909 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5911 inline bool
5912 decl_die_hasher::equal (die_node *x, tree y)
5914 return (x->decl_id == DECL_UID (y));
5917 /* Return the DIE associated with a given declaration. */
5919 dw_die_ref
5920 lookup_decl_die (tree decl)
5922 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5923 NO_INSERT);
5924 if (!die)
5926 if (in_lto_p)
5927 return maybe_create_die_with_external_ref (decl);
5928 return NULL;
5930 if ((*die)->removed)
5932 decl_die_table->clear_slot (die);
5933 return NULL;
5935 return *die;
5939 /* Return the DIE associated with BLOCK. */
5941 static inline dw_die_ref
5942 lookup_block_die (tree block)
5944 dw_die_ref die = BLOCK_DIE (block);
5945 if (!die && in_lto_p)
5946 return maybe_create_die_with_external_ref (block);
5947 return die;
5950 /* Associate DIE with BLOCK. */
5952 static inline void
5953 equate_block_to_die (tree block, dw_die_ref die)
5955 BLOCK_DIE (block) = die;
5957 #undef BLOCK_DIE
5960 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5961 style reference. Return true if we found one refering to a DIE for
5962 DECL, otherwise return false. */
5964 static bool
5965 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5966 unsigned HOST_WIDE_INT *off)
5968 dw_die_ref die;
5970 if (in_lto_p)
5972 /* During WPA stage and incremental linking we use a hash-map
5973 to store the decl <-> label + offset map. */
5974 if (!external_die_map)
5975 return false;
5976 sym_off_pair *desc = external_die_map->get (decl);
5977 if (!desc)
5978 return false;
5979 *sym = desc->sym;
5980 *off = desc->off;
5981 return true;
5984 if (TREE_CODE (decl) == BLOCK)
5985 die = lookup_block_die (decl);
5986 else
5987 die = lookup_decl_die (decl);
5988 if (!die)
5989 return false;
5991 /* Similar to get_ref_die_offset_label, but using the "correct"
5992 label. */
5993 *off = die->die_offset;
5994 while (die->die_parent)
5995 die = die->die_parent;
5996 /* For the containing CU DIE we compute a die_symbol in
5997 compute_comp_unit_symbol. */
5998 gcc_assert (die->die_tag == DW_TAG_compile_unit
5999 && die->die_id.die_symbol != NULL);
6000 *sym = die->die_id.die_symbol;
6001 return true;
6004 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
6006 static void
6007 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
6008 const char *symbol, HOST_WIDE_INT offset)
6010 /* Create a fake DIE that contains the reference. Don't use
6011 new_die because we don't want to end up in the limbo list. */
6012 /* ??? We probably want to share these, thus put a ref to the DIE
6013 we create here to the external_die_map entry. */
6014 dw_die_ref ref = new_die_raw (die->die_tag);
6015 ref->die_id.die_symbol = symbol;
6016 ref->die_offset = offset;
6017 ref->with_offset = 1;
6018 add_AT_die_ref (die, attr_kind, ref);
6021 /* Create a DIE for DECL if required and add a reference to a DIE
6022 at SYMBOL + OFFSET which contains attributes dumped early. */
6024 static void
6025 dwarf2out_register_external_die (tree decl, const char *sym,
6026 unsigned HOST_WIDE_INT off)
6028 if (debug_info_level == DINFO_LEVEL_NONE)
6029 return;
6031 if (!external_die_map)
6032 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
6033 gcc_checking_assert (!external_die_map->get (decl));
6034 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
6035 external_die_map->put (decl, p);
6038 /* If we have a registered external DIE for DECL return a new DIE for
6039 the concrete instance with an appropriate abstract origin. */
6041 static dw_die_ref
6042 maybe_create_die_with_external_ref (tree decl)
6044 if (!external_die_map)
6045 return NULL;
6046 sym_off_pair *desc = external_die_map->get (decl);
6047 if (!desc)
6048 return NULL;
6050 const char *sym = desc->sym;
6051 unsigned HOST_WIDE_INT off = desc->off;
6052 external_die_map->remove (decl);
6054 in_lto_p = false;
6055 dw_die_ref die = (TREE_CODE (decl) == BLOCK
6056 ? lookup_block_die (decl) : lookup_decl_die (decl));
6057 gcc_assert (!die);
6058 in_lto_p = true;
6060 tree ctx;
6061 dw_die_ref parent = NULL;
6062 /* Need to lookup a DIE for the decls context - the containing
6063 function or translation unit. */
6064 if (TREE_CODE (decl) == BLOCK)
6066 ctx = BLOCK_SUPERCONTEXT (decl);
6067 /* ??? We do not output DIEs for all scopes thus skip as
6068 many DIEs as needed. */
6069 while (TREE_CODE (ctx) == BLOCK
6070 && !lookup_block_die (ctx))
6071 ctx = BLOCK_SUPERCONTEXT (ctx);
6073 else
6074 ctx = DECL_CONTEXT (decl);
6075 /* Peel types in the context stack. */
6076 while (ctx && TYPE_P (ctx))
6077 ctx = TYPE_CONTEXT (ctx);
6078 /* Likewise namespaces in case we do not want to emit DIEs for them. */
6079 if (debug_info_level <= DINFO_LEVEL_TERSE)
6080 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
6081 ctx = DECL_CONTEXT (ctx);
6082 if (ctx)
6084 if (TREE_CODE (ctx) == BLOCK)
6085 parent = lookup_block_die (ctx);
6086 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
6087 /* Keep the 1:1 association during WPA. */
6088 && !flag_wpa
6089 && flag_incremental_link != INCREMENTAL_LINK_LTO)
6090 /* Otherwise all late annotations go to the main CU which
6091 imports the original CUs. */
6092 parent = comp_unit_die ();
6093 else if (TREE_CODE (ctx) == FUNCTION_DECL
6094 && TREE_CODE (decl) != FUNCTION_DECL
6095 && TREE_CODE (decl) != PARM_DECL
6096 && TREE_CODE (decl) != RESULT_DECL
6097 && TREE_CODE (decl) != BLOCK)
6098 /* Leave function local entities parent determination to when
6099 we process scope vars. */
6101 else
6102 parent = lookup_decl_die (ctx);
6104 else
6105 /* In some cases the FEs fail to set DECL_CONTEXT properly.
6106 Handle this case gracefully by globalizing stuff. */
6107 parent = comp_unit_die ();
6108 /* Create a DIE "stub". */
6109 switch (TREE_CODE (decl))
6111 case TRANSLATION_UNIT_DECL:
6113 die = comp_unit_die ();
6114 /* We re-target all CU decls to the LTRANS CU DIE, so no need
6115 to create a DIE for the original CUs. */
6116 return die;
6118 case NAMESPACE_DECL:
6119 if (is_fortran (decl))
6120 die = new_die (DW_TAG_module, parent, decl);
6121 else
6122 die = new_die (DW_TAG_namespace, parent, decl);
6123 break;
6124 case FUNCTION_DECL:
6125 die = new_die (DW_TAG_subprogram, parent, decl);
6126 break;
6127 case VAR_DECL:
6128 die = new_die (DW_TAG_variable, parent, decl);
6129 break;
6130 case RESULT_DECL:
6131 die = new_die (DW_TAG_variable, parent, decl);
6132 break;
6133 case PARM_DECL:
6134 die = new_die (DW_TAG_formal_parameter, parent, decl);
6135 break;
6136 case CONST_DECL:
6137 die = new_die (DW_TAG_constant, parent, decl);
6138 break;
6139 case LABEL_DECL:
6140 die = new_die (DW_TAG_label, parent, decl);
6141 break;
6142 case BLOCK:
6143 die = new_die (DW_TAG_lexical_block, parent, decl);
6144 break;
6145 default:
6146 gcc_unreachable ();
6148 if (TREE_CODE (decl) == BLOCK)
6149 equate_block_to_die (decl, die);
6150 else
6151 equate_decl_number_to_die (decl, die);
6153 add_desc_attribute (die, decl);
6155 /* Add a reference to the DIE providing early debug at $sym + off. */
6156 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6158 return die;
6161 /* Returns a hash value for X (which really is a var_loc_list). */
6163 inline hashval_t
6164 decl_loc_hasher::hash (var_loc_list *x)
6166 return (hashval_t) x->decl_id;
6169 /* Return nonzero if decl_id of var_loc_list X is the same as
6170 UID of decl *Y. */
6172 inline bool
6173 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6175 return (x->decl_id == DECL_UID (y));
6178 /* Return the var_loc list associated with a given declaration. */
6180 static inline var_loc_list *
6181 lookup_decl_loc (const_tree decl)
6183 if (!decl_loc_table)
6184 return NULL;
6185 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6188 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6190 inline hashval_t
6191 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6193 return (hashval_t) x->decl_id;
6196 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6197 UID of decl *Y. */
6199 inline bool
6200 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6202 return (x->decl_id == DECL_UID (y));
6205 /* Equate a DIE to a particular declaration. */
6207 static void
6208 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6210 unsigned int decl_id = DECL_UID (decl);
6212 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6213 decl_die->decl_id = decl_id;
6216 /* Return how many bits covers PIECE EXPR_LIST. */
6218 static HOST_WIDE_INT
6219 decl_piece_bitsize (rtx piece)
6221 int ret = (int) GET_MODE (piece);
6222 if (ret)
6223 return ret;
6224 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6225 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6226 return INTVAL (XEXP (XEXP (piece, 0), 0));
6229 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6231 static rtx *
6232 decl_piece_varloc_ptr (rtx piece)
6234 if ((int) GET_MODE (piece))
6235 return &XEXP (piece, 0);
6236 else
6237 return &XEXP (XEXP (piece, 0), 1);
6240 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6241 Next is the chain of following piece nodes. */
6243 static rtx_expr_list *
6244 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6246 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6247 return alloc_EXPR_LIST (bitsize, loc_note, next);
6248 else
6249 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6250 GEN_INT (bitsize),
6251 loc_note), next);
6254 /* Return rtx that should be stored into loc field for
6255 LOC_NOTE and BITPOS/BITSIZE. */
6257 static rtx
6258 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6259 HOST_WIDE_INT bitsize)
6261 if (bitsize != -1)
6263 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6264 if (bitpos != 0)
6265 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6267 return loc_note;
6270 /* This function either modifies location piece list *DEST in
6271 place (if SRC and INNER is NULL), or copies location piece list
6272 *SRC to *DEST while modifying it. Location BITPOS is modified
6273 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6274 not copied and if needed some padding around it is added.
6275 When modifying in place, DEST should point to EXPR_LIST where
6276 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6277 to the start of the whole list and INNER points to the EXPR_LIST
6278 where earlier pieces cover PIECE_BITPOS bits. */
6280 static void
6281 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6282 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6283 HOST_WIDE_INT bitsize, rtx loc_note)
6285 HOST_WIDE_INT diff;
6286 bool copy = inner != NULL;
6288 if (copy)
6290 /* First copy all nodes preceding the current bitpos. */
6291 while (src != inner)
6293 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6294 decl_piece_bitsize (*src), NULL_RTX);
6295 dest = &XEXP (*dest, 1);
6296 src = &XEXP (*src, 1);
6299 /* Add padding if needed. */
6300 if (bitpos != piece_bitpos)
6302 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6303 copy ? NULL_RTX : *dest);
6304 dest = &XEXP (*dest, 1);
6306 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6308 gcc_assert (!copy);
6309 /* A piece with correct bitpos and bitsize already exist,
6310 just update the location for it and return. */
6311 *decl_piece_varloc_ptr (*dest) = loc_note;
6312 return;
6314 /* Add the piece that changed. */
6315 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6316 dest = &XEXP (*dest, 1);
6317 /* Skip over pieces that overlap it. */
6318 diff = bitpos - piece_bitpos + bitsize;
6319 if (!copy)
6320 src = dest;
6321 while (diff > 0 && *src)
6323 rtx piece = *src;
6324 diff -= decl_piece_bitsize (piece);
6325 if (copy)
6326 src = &XEXP (piece, 1);
6327 else
6329 *src = XEXP (piece, 1);
6330 free_EXPR_LIST_node (piece);
6333 /* Add padding if needed. */
6334 if (diff < 0 && *src)
6336 if (!copy)
6337 dest = src;
6338 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6339 dest = &XEXP (*dest, 1);
6341 if (!copy)
6342 return;
6343 /* Finally copy all nodes following it. */
6344 while (*src)
6346 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6347 decl_piece_bitsize (*src), NULL_RTX);
6348 dest = &XEXP (*dest, 1);
6349 src = &XEXP (*src, 1);
6353 /* Add a variable location node to the linked list for DECL. */
6355 static struct var_loc_node *
6356 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6358 unsigned int decl_id;
6359 var_loc_list *temp;
6360 struct var_loc_node *loc = NULL;
6361 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6363 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6365 tree realdecl = DECL_DEBUG_EXPR (decl);
6366 if (handled_component_p (realdecl)
6367 || (TREE_CODE (realdecl) == MEM_REF
6368 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6370 bool reverse;
6371 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6372 &bitsize, &reverse);
6373 if (!innerdecl
6374 || !DECL_P (innerdecl)
6375 || DECL_IGNORED_P (innerdecl)
6376 || TREE_STATIC (innerdecl)
6377 || bitsize == 0
6378 || bitpos + bitsize > 256)
6379 return NULL;
6380 decl = innerdecl;
6384 decl_id = DECL_UID (decl);
6385 var_loc_list **slot
6386 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6387 if (*slot == NULL)
6389 temp = ggc_cleared_alloc<var_loc_list> ();
6390 temp->decl_id = decl_id;
6391 *slot = temp;
6393 else
6394 temp = *slot;
6396 /* For PARM_DECLs try to keep around the original incoming value,
6397 even if that means we'll emit a zero-range .debug_loc entry. */
6398 if (temp->last
6399 && temp->first == temp->last
6400 && TREE_CODE (decl) == PARM_DECL
6401 && NOTE_P (temp->first->loc)
6402 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6403 && DECL_INCOMING_RTL (decl)
6404 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6405 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6406 == GET_CODE (DECL_INCOMING_RTL (decl))
6407 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6408 && (bitsize != -1
6409 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6410 NOTE_VAR_LOCATION_LOC (loc_note))
6411 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6412 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6414 loc = ggc_cleared_alloc<var_loc_node> ();
6415 temp->first->next = loc;
6416 temp->last = loc;
6417 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6419 else if (temp->last)
6421 struct var_loc_node *last = temp->last, *unused = NULL;
6422 rtx *piece_loc = NULL, last_loc_note;
6423 HOST_WIDE_INT piece_bitpos = 0;
6424 if (last->next)
6426 last = last->next;
6427 gcc_assert (last->next == NULL);
6429 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6431 piece_loc = &last->loc;
6434 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6435 if (piece_bitpos + cur_bitsize > bitpos)
6436 break;
6437 piece_bitpos += cur_bitsize;
6438 piece_loc = &XEXP (*piece_loc, 1);
6440 while (*piece_loc);
6442 /* TEMP->LAST here is either pointer to the last but one or
6443 last element in the chained list, LAST is pointer to the
6444 last element. */
6445 if (label && strcmp (last->label, label) == 0 && last->view == view)
6447 /* For SRA optimized variables if there weren't any real
6448 insns since last note, just modify the last node. */
6449 if (piece_loc != NULL)
6451 adjust_piece_list (piece_loc, NULL, NULL,
6452 bitpos, piece_bitpos, bitsize, loc_note);
6453 return NULL;
6455 /* If the last note doesn't cover any instructions, remove it. */
6456 if (temp->last != last)
6458 temp->last->next = NULL;
6459 unused = last;
6460 last = temp->last;
6461 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6463 else
6465 gcc_assert (temp->first == temp->last
6466 || (temp->first->next == temp->last
6467 && TREE_CODE (decl) == PARM_DECL));
6468 memset (temp->last, '\0', sizeof (*temp->last));
6469 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6470 return temp->last;
6473 if (bitsize == -1 && NOTE_P (last->loc))
6474 last_loc_note = last->loc;
6475 else if (piece_loc != NULL
6476 && *piece_loc != NULL_RTX
6477 && piece_bitpos == bitpos
6478 && decl_piece_bitsize (*piece_loc) == bitsize)
6479 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6480 else
6481 last_loc_note = NULL_RTX;
6482 /* If the current location is the same as the end of the list,
6483 and either both or neither of the locations is uninitialized,
6484 we have nothing to do. */
6485 if (last_loc_note == NULL_RTX
6486 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6487 NOTE_VAR_LOCATION_LOC (loc_note)))
6488 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6489 != NOTE_VAR_LOCATION_STATUS (loc_note))
6490 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6491 == VAR_INIT_STATUS_UNINITIALIZED)
6492 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6493 == VAR_INIT_STATUS_UNINITIALIZED))))
6495 /* Add LOC to the end of list and update LAST. If the last
6496 element of the list has been removed above, reuse its
6497 memory for the new node, otherwise allocate a new one. */
6498 if (unused)
6500 loc = unused;
6501 memset (loc, '\0', sizeof (*loc));
6503 else
6504 loc = ggc_cleared_alloc<var_loc_node> ();
6505 if (bitsize == -1 || piece_loc == NULL)
6506 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6507 else
6508 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6509 bitpos, piece_bitpos, bitsize, loc_note);
6510 last->next = loc;
6511 /* Ensure TEMP->LAST will point either to the new last but one
6512 element of the chain, or to the last element in it. */
6513 if (last != temp->last)
6514 temp->last = last;
6516 else if (unused)
6517 ggc_free (unused);
6519 else
6521 loc = ggc_cleared_alloc<var_loc_node> ();
6522 temp->first = loc;
6523 temp->last = loc;
6524 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6526 return loc;
6529 /* Keep track of the number of spaces used to indent the
6530 output of the debugging routines that print the structure of
6531 the DIE internal representation. */
6532 static int print_indent;
6534 /* Indent the line the number of spaces given by print_indent. */
6536 static inline void
6537 print_spaces (FILE *outfile)
6539 fprintf (outfile, "%*s", print_indent, "");
6542 /* Print a type signature in hex. */
6544 static inline void
6545 print_signature (FILE *outfile, char *sig)
6547 int i;
6549 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6550 fprintf (outfile, "%02x", sig[i] & 0xff);
6553 static inline void
6554 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6556 if (discr_value->pos)
6557 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6558 else
6559 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6562 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6564 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6565 RECURSE, output location descriptor operations. */
6567 static void
6568 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6570 switch (val->val_class)
6572 case dw_val_class_addr:
6573 fprintf (outfile, "address");
6574 break;
6575 case dw_val_class_offset:
6576 fprintf (outfile, "offset");
6577 break;
6578 case dw_val_class_loc:
6579 fprintf (outfile, "location descriptor");
6580 if (val->v.val_loc == NULL)
6581 fprintf (outfile, " -> <null>\n");
6582 else if (recurse)
6584 fprintf (outfile, ":\n");
6585 print_indent += 4;
6586 print_loc_descr (val->v.val_loc, outfile);
6587 print_indent -= 4;
6589 else
6591 if (flag_dump_noaddr || flag_dump_unnumbered)
6592 fprintf (outfile, " #\n");
6593 else
6594 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6596 break;
6597 case dw_val_class_loc_list:
6598 fprintf (outfile, "location list -> label:%s",
6599 val->v.val_loc_list->ll_symbol);
6600 break;
6601 case dw_val_class_view_list:
6602 val = view_list_to_loc_list_val_node (val);
6603 fprintf (outfile, "location list with views -> labels:%s and %s",
6604 val->v.val_loc_list->ll_symbol,
6605 val->v.val_loc_list->vl_symbol);
6606 break;
6607 case dw_val_class_range_list:
6608 fprintf (outfile, "range list");
6609 break;
6610 case dw_val_class_const:
6611 case dw_val_class_const_implicit:
6612 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6613 break;
6614 case dw_val_class_unsigned_const:
6615 case dw_val_class_unsigned_const_implicit:
6616 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6617 break;
6618 case dw_val_class_const_double:
6619 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6620 HOST_WIDE_INT_PRINT_UNSIGNED")",
6621 val->v.val_double.high,
6622 val->v.val_double.low);
6623 break;
6624 case dw_val_class_wide_int:
6626 int i = val->v.val_wide->get_len ();
6627 fprintf (outfile, "constant (");
6628 gcc_assert (i > 0);
6629 if (val->v.val_wide->elt (i - 1) == 0)
6630 fprintf (outfile, "0x");
6631 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6632 val->v.val_wide->elt (--i));
6633 while (--i >= 0)
6634 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6635 val->v.val_wide->elt (i));
6636 fprintf (outfile, ")");
6637 break;
6639 case dw_val_class_vec:
6640 fprintf (outfile, "floating-point or vector constant");
6641 break;
6642 case dw_val_class_flag:
6643 fprintf (outfile, "%u", val->v.val_flag);
6644 break;
6645 case dw_val_class_die_ref:
6646 if (val->v.val_die_ref.die != NULL)
6648 dw_die_ref die = val->v.val_die_ref.die;
6650 if (die->comdat_type_p)
6652 fprintf (outfile, "die -> signature: ");
6653 print_signature (outfile,
6654 die->die_id.die_type_node->signature);
6656 else if (die->die_id.die_symbol)
6658 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6659 if (die->with_offset)
6660 fprintf (outfile, " + %ld", die->die_offset);
6662 else
6663 fprintf (outfile, "die -> %ld", die->die_offset);
6664 if (flag_dump_noaddr || flag_dump_unnumbered)
6665 fprintf (outfile, " #");
6666 else
6667 fprintf (outfile, " (%p)", (void *) die);
6669 else
6670 fprintf (outfile, "die -> <null>");
6671 break;
6672 case dw_val_class_vms_delta:
6673 fprintf (outfile, "delta: @slotcount(%s-%s)",
6674 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6675 break;
6676 case dw_val_class_symview:
6677 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6678 break;
6679 case dw_val_class_lbl_id:
6680 case dw_val_class_lineptr:
6681 case dw_val_class_macptr:
6682 case dw_val_class_loclistsptr:
6683 case dw_val_class_high_pc:
6684 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6685 break;
6686 case dw_val_class_str:
6687 if (val->v.val_str->str != NULL)
6688 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6689 else
6690 fprintf (outfile, "<null>");
6691 break;
6692 case dw_val_class_file:
6693 case dw_val_class_file_implicit:
6694 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6695 val->v.val_file->emitted_number);
6696 break;
6697 case dw_val_class_data8:
6699 int i;
6701 for (i = 0; i < 8; i++)
6702 fprintf (outfile, "%02x", val->v.val_data8[i]);
6703 break;
6705 case dw_val_class_discr_value:
6706 print_discr_value (outfile, &val->v.val_discr_value);
6707 break;
6708 case dw_val_class_discr_list:
6709 for (dw_discr_list_ref node = val->v.val_discr_list;
6710 node != NULL;
6711 node = node->dw_discr_next)
6713 if (node->dw_discr_range)
6715 fprintf (outfile, " .. ");
6716 print_discr_value (outfile, &node->dw_discr_lower_bound);
6717 print_discr_value (outfile, &node->dw_discr_upper_bound);
6719 else
6720 print_discr_value (outfile, &node->dw_discr_lower_bound);
6722 if (node->dw_discr_next != NULL)
6723 fprintf (outfile, " | ");
6725 default:
6726 break;
6730 /* Likewise, for a DIE attribute. */
6732 static void
6733 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6735 print_dw_val (&a->dw_attr_val, recurse, outfile);
6739 /* Print the list of operands in the LOC location description to OUTFILE. This
6740 routine is a debugging aid only. */
6742 static void
6743 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6745 dw_loc_descr_ref l = loc;
6747 if (loc == NULL)
6749 print_spaces (outfile);
6750 fprintf (outfile, "<null>\n");
6751 return;
6754 for (l = loc; l != NULL; l = l->dw_loc_next)
6756 print_spaces (outfile);
6757 if (flag_dump_noaddr || flag_dump_unnumbered)
6758 fprintf (outfile, "#");
6759 else
6760 fprintf (outfile, "(%p)", (void *) l);
6761 fprintf (outfile, " %s",
6762 dwarf_stack_op_name (l->dw_loc_opc));
6763 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6765 fprintf (outfile, " ");
6766 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6768 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6770 fprintf (outfile, ", ");
6771 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6773 fprintf (outfile, "\n");
6777 /* Print the information associated with a given DIE, and its children.
6778 This routine is a debugging aid only. */
6780 static void
6781 print_die (dw_die_ref die, FILE *outfile)
6783 dw_attr_node *a;
6784 dw_die_ref c;
6785 unsigned ix;
6787 print_spaces (outfile);
6788 fprintf (outfile, "DIE %4ld: %s ",
6789 die->die_offset, dwarf_tag_name (die->die_tag));
6790 if (flag_dump_noaddr || flag_dump_unnumbered)
6791 fprintf (outfile, "#\n");
6792 else
6793 fprintf (outfile, "(%p)\n", (void*) die);
6794 print_spaces (outfile);
6795 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6796 fprintf (outfile, " offset: %ld", die->die_offset);
6797 fprintf (outfile, " mark: %d\n", die->die_mark);
6799 if (die->comdat_type_p)
6801 print_spaces (outfile);
6802 fprintf (outfile, " signature: ");
6803 print_signature (outfile, die->die_id.die_type_node->signature);
6804 fprintf (outfile, "\n");
6807 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6809 print_spaces (outfile);
6810 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6812 print_attribute (a, true, outfile);
6813 fprintf (outfile, "\n");
6816 if (die->die_child != NULL)
6818 print_indent += 4;
6819 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6820 print_indent -= 4;
6822 if (print_indent == 0)
6823 fprintf (outfile, "\n");
6826 /* Print the list of operations in the LOC location description. */
6828 DEBUG_FUNCTION void
6829 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6831 print_loc_descr (loc, stderr);
6834 /* Print the information collected for a given DIE. */
6836 DEBUG_FUNCTION void
6837 debug_dwarf_die (dw_die_ref die)
6839 print_die (die, stderr);
6842 DEBUG_FUNCTION void
6843 debug (die_struct &ref)
6845 print_die (&ref, stderr);
6848 DEBUG_FUNCTION void
6849 debug (die_struct *ptr)
6851 if (ptr)
6852 debug (*ptr);
6853 else
6854 fprintf (stderr, "<nil>\n");
6858 /* Print all DWARF information collected for the compilation unit.
6859 This routine is a debugging aid only. */
6861 DEBUG_FUNCTION void
6862 debug_dwarf (void)
6864 print_indent = 0;
6865 print_die (comp_unit_die (), stderr);
6868 /* Verify the DIE tree structure. */
6870 DEBUG_FUNCTION void
6871 verify_die (dw_die_ref die)
6873 gcc_assert (!die->die_mark);
6874 if (die->die_parent == NULL
6875 && die->die_sib == NULL)
6876 return;
6877 /* Verify the die_sib list is cyclic. */
6878 dw_die_ref x = die;
6881 x->die_mark = 1;
6882 x = x->die_sib;
6884 while (x && !x->die_mark);
6885 gcc_assert (x == die);
6886 x = die;
6889 /* Verify all dies have the same parent. */
6890 gcc_assert (x->die_parent == die->die_parent);
6891 if (x->die_child)
6893 /* Verify the child has the proper parent and recurse. */
6894 gcc_assert (x->die_child->die_parent == x);
6895 verify_die (x->die_child);
6897 x->die_mark = 0;
6898 x = x->die_sib;
6900 while (x && x->die_mark);
6903 /* Sanity checks on DIEs. */
6905 static void
6906 check_die (dw_die_ref die)
6908 unsigned ix;
6909 dw_attr_node *a;
6910 bool inline_found = false;
6911 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6912 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6913 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6915 switch (a->dw_attr)
6917 case DW_AT_inline:
6918 if (a->dw_attr_val.v.val_unsigned)
6919 inline_found = true;
6920 break;
6921 case DW_AT_location:
6922 ++n_location;
6923 break;
6924 case DW_AT_low_pc:
6925 ++n_low_pc;
6926 break;
6927 case DW_AT_high_pc:
6928 ++n_high_pc;
6929 break;
6930 case DW_AT_artificial:
6931 ++n_artificial;
6932 break;
6933 case DW_AT_decl_column:
6934 ++n_decl_column;
6935 break;
6936 case DW_AT_decl_line:
6937 ++n_decl_line;
6938 break;
6939 case DW_AT_decl_file:
6940 ++n_decl_file;
6941 break;
6942 default:
6943 break;
6946 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6947 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6949 fprintf (stderr, "Duplicate attributes in DIE:\n");
6950 debug_dwarf_die (die);
6951 gcc_unreachable ();
6953 if (inline_found)
6955 /* A debugging information entry that is a member of an abstract
6956 instance tree [that has DW_AT_inline] should not contain any
6957 attributes which describe aspects of the subroutine which vary
6958 between distinct inlined expansions or distinct out-of-line
6959 expansions. */
6960 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6961 gcc_assert (a->dw_attr != DW_AT_low_pc
6962 && a->dw_attr != DW_AT_high_pc
6963 && a->dw_attr != DW_AT_location
6964 && a->dw_attr != DW_AT_frame_base
6965 && a->dw_attr != DW_AT_call_all_calls
6966 && a->dw_attr != DW_AT_GNU_all_call_sites);
6970 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6971 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6972 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6974 /* Calculate the checksum of a location expression. */
6976 static inline void
6977 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6979 int tem;
6980 inchash::hash hstate;
6981 hashval_t hash;
6983 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6984 CHECKSUM (tem);
6985 hash_loc_operands (loc, hstate);
6986 hash = hstate.end();
6987 CHECKSUM (hash);
6990 /* Calculate the checksum of an attribute. */
6992 static void
6993 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6995 dw_loc_descr_ref loc;
6996 rtx r;
6998 CHECKSUM (at->dw_attr);
7000 /* We don't care that this was compiled with a different compiler
7001 snapshot; if the output is the same, that's what matters. */
7002 if (at->dw_attr == DW_AT_producer)
7003 return;
7005 switch (AT_class (at))
7007 case dw_val_class_const:
7008 case dw_val_class_const_implicit:
7009 CHECKSUM (at->dw_attr_val.v.val_int);
7010 break;
7011 case dw_val_class_unsigned_const:
7012 case dw_val_class_unsigned_const_implicit:
7013 CHECKSUM (at->dw_attr_val.v.val_unsigned);
7014 break;
7015 case dw_val_class_const_double:
7016 CHECKSUM (at->dw_attr_val.v.val_double);
7017 break;
7018 case dw_val_class_wide_int:
7019 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7020 get_full_len (*at->dw_attr_val.v.val_wide)
7021 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7022 break;
7023 case dw_val_class_vec:
7024 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7025 (at->dw_attr_val.v.val_vec.length
7026 * at->dw_attr_val.v.val_vec.elt_size));
7027 break;
7028 case dw_val_class_flag:
7029 CHECKSUM (at->dw_attr_val.v.val_flag);
7030 break;
7031 case dw_val_class_str:
7032 CHECKSUM_STRING (AT_string (at));
7033 break;
7035 case dw_val_class_addr:
7036 r = AT_addr (at);
7037 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7038 CHECKSUM_STRING (XSTR (r, 0));
7039 break;
7041 case dw_val_class_offset:
7042 CHECKSUM (at->dw_attr_val.v.val_offset);
7043 break;
7045 case dw_val_class_loc:
7046 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7047 loc_checksum (loc, ctx);
7048 break;
7050 case dw_val_class_die_ref:
7051 die_checksum (AT_ref (at), ctx, mark);
7052 break;
7054 case dw_val_class_fde_ref:
7055 case dw_val_class_vms_delta:
7056 case dw_val_class_symview:
7057 case dw_val_class_lbl_id:
7058 case dw_val_class_lineptr:
7059 case dw_val_class_macptr:
7060 case dw_val_class_loclistsptr:
7061 case dw_val_class_high_pc:
7062 break;
7064 case dw_val_class_file:
7065 case dw_val_class_file_implicit:
7066 CHECKSUM_STRING (AT_file (at)->filename);
7067 break;
7069 case dw_val_class_data8:
7070 CHECKSUM (at->dw_attr_val.v.val_data8);
7071 break;
7073 default:
7074 break;
7078 /* Calculate the checksum of a DIE. */
7080 static void
7081 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7083 dw_die_ref c;
7084 dw_attr_node *a;
7085 unsigned ix;
7087 /* To avoid infinite recursion. */
7088 if (die->die_mark)
7090 CHECKSUM (die->die_mark);
7091 return;
7093 die->die_mark = ++(*mark);
7095 CHECKSUM (die->die_tag);
7097 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7098 attr_checksum (a, ctx, mark);
7100 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7103 #undef CHECKSUM
7104 #undef CHECKSUM_BLOCK
7105 #undef CHECKSUM_STRING
7107 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
7108 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7109 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
7110 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
7111 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
7112 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
7113 #define CHECKSUM_ATTR(FOO) \
7114 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
7116 /* Calculate the checksum of a number in signed LEB128 format. */
7118 static void
7119 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
7121 unsigned char byte;
7122 bool more;
7124 while (1)
7126 byte = (value & 0x7f);
7127 value >>= 7;
7128 more = !((value == 0 && (byte & 0x40) == 0)
7129 || (value == -1 && (byte & 0x40) != 0));
7130 if (more)
7131 byte |= 0x80;
7132 CHECKSUM (byte);
7133 if (!more)
7134 break;
7138 /* Calculate the checksum of a number in unsigned LEB128 format. */
7140 static void
7141 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7143 while (1)
7145 unsigned char byte = (value & 0x7f);
7146 value >>= 7;
7147 if (value != 0)
7148 /* More bytes to follow. */
7149 byte |= 0x80;
7150 CHECKSUM (byte);
7151 if (value == 0)
7152 break;
7156 /* Checksum the context of the DIE. This adds the names of any
7157 surrounding namespaces or structures to the checksum. */
7159 static void
7160 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7162 const char *name;
7163 dw_die_ref spec;
7164 int tag = die->die_tag;
7166 if (tag != DW_TAG_namespace
7167 && tag != DW_TAG_structure_type
7168 && tag != DW_TAG_class_type)
7169 return;
7171 name = get_AT_string (die, DW_AT_name);
7173 spec = get_AT_ref (die, DW_AT_specification);
7174 if (spec != NULL)
7175 die = spec;
7177 if (die->die_parent != NULL)
7178 checksum_die_context (die->die_parent, ctx);
7180 CHECKSUM_ULEB128 ('C');
7181 CHECKSUM_ULEB128 (tag);
7182 if (name != NULL)
7183 CHECKSUM_STRING (name);
7186 /* Calculate the checksum of a location expression. */
7188 static inline void
7189 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7191 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7192 were emitted as a DW_FORM_sdata instead of a location expression. */
7193 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7195 CHECKSUM_ULEB128 (DW_FORM_sdata);
7196 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7197 return;
7200 /* Otherwise, just checksum the raw location expression. */
7201 while (loc != NULL)
7203 inchash::hash hstate;
7204 hashval_t hash;
7206 CHECKSUM_ULEB128 (loc->dtprel);
7207 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7208 hash_loc_operands (loc, hstate);
7209 hash = hstate.end ();
7210 CHECKSUM (hash);
7211 loc = loc->dw_loc_next;
7215 /* Calculate the checksum of an attribute. */
7217 static void
7218 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7219 struct md5_ctx *ctx, int *mark)
7221 dw_loc_descr_ref loc;
7222 rtx r;
7224 if (AT_class (at) == dw_val_class_die_ref)
7226 dw_die_ref target_die = AT_ref (at);
7228 /* For pointer and reference types, we checksum only the (qualified)
7229 name of the target type (if there is a name). For friend entries,
7230 we checksum only the (qualified) name of the target type or function.
7231 This allows the checksum to remain the same whether the target type
7232 is complete or not. */
7233 if ((at->dw_attr == DW_AT_type
7234 && (tag == DW_TAG_pointer_type
7235 || tag == DW_TAG_reference_type
7236 || tag == DW_TAG_rvalue_reference_type
7237 || tag == DW_TAG_ptr_to_member_type))
7238 || (at->dw_attr == DW_AT_friend
7239 && tag == DW_TAG_friend))
7241 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7243 if (name_attr != NULL)
7245 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7247 if (decl == NULL)
7248 decl = target_die;
7249 CHECKSUM_ULEB128 ('N');
7250 CHECKSUM_ULEB128 (at->dw_attr);
7251 if (decl->die_parent != NULL)
7252 checksum_die_context (decl->die_parent, ctx);
7253 CHECKSUM_ULEB128 ('E');
7254 CHECKSUM_STRING (AT_string (name_attr));
7255 return;
7259 /* For all other references to another DIE, we check to see if the
7260 target DIE has already been visited. If it has, we emit a
7261 backward reference; if not, we descend recursively. */
7262 if (target_die->die_mark > 0)
7264 CHECKSUM_ULEB128 ('R');
7265 CHECKSUM_ULEB128 (at->dw_attr);
7266 CHECKSUM_ULEB128 (target_die->die_mark);
7268 else
7270 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7272 if (decl == NULL)
7273 decl = target_die;
7274 target_die->die_mark = ++(*mark);
7275 CHECKSUM_ULEB128 ('T');
7276 CHECKSUM_ULEB128 (at->dw_attr);
7277 if (decl->die_parent != NULL)
7278 checksum_die_context (decl->die_parent, ctx);
7279 die_checksum_ordered (target_die, ctx, mark);
7281 return;
7284 CHECKSUM_ULEB128 ('A');
7285 CHECKSUM_ULEB128 (at->dw_attr);
7287 switch (AT_class (at))
7289 case dw_val_class_const:
7290 case dw_val_class_const_implicit:
7291 CHECKSUM_ULEB128 (DW_FORM_sdata);
7292 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7293 break;
7295 case dw_val_class_unsigned_const:
7296 case dw_val_class_unsigned_const_implicit:
7297 CHECKSUM_ULEB128 (DW_FORM_sdata);
7298 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7299 break;
7301 case dw_val_class_const_double:
7302 CHECKSUM_ULEB128 (DW_FORM_block);
7303 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7304 CHECKSUM (at->dw_attr_val.v.val_double);
7305 break;
7307 case dw_val_class_wide_int:
7308 CHECKSUM_ULEB128 (DW_FORM_block);
7309 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7310 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7311 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7312 get_full_len (*at->dw_attr_val.v.val_wide)
7313 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7314 break;
7316 case dw_val_class_vec:
7317 CHECKSUM_ULEB128 (DW_FORM_block);
7318 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7319 * at->dw_attr_val.v.val_vec.elt_size);
7320 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7321 (at->dw_attr_val.v.val_vec.length
7322 * at->dw_attr_val.v.val_vec.elt_size));
7323 break;
7325 case dw_val_class_flag:
7326 CHECKSUM_ULEB128 (DW_FORM_flag);
7327 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7328 break;
7330 case dw_val_class_str:
7331 CHECKSUM_ULEB128 (DW_FORM_string);
7332 CHECKSUM_STRING (AT_string (at));
7333 break;
7335 case dw_val_class_addr:
7336 r = AT_addr (at);
7337 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7338 CHECKSUM_ULEB128 (DW_FORM_string);
7339 CHECKSUM_STRING (XSTR (r, 0));
7340 break;
7342 case dw_val_class_offset:
7343 CHECKSUM_ULEB128 (DW_FORM_sdata);
7344 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7345 break;
7347 case dw_val_class_loc:
7348 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7349 loc_checksum_ordered (loc, ctx);
7350 break;
7352 case dw_val_class_fde_ref:
7353 case dw_val_class_symview:
7354 case dw_val_class_lbl_id:
7355 case dw_val_class_lineptr:
7356 case dw_val_class_macptr:
7357 case dw_val_class_loclistsptr:
7358 case dw_val_class_high_pc:
7359 break;
7361 case dw_val_class_file:
7362 case dw_val_class_file_implicit:
7363 CHECKSUM_ULEB128 (DW_FORM_string);
7364 CHECKSUM_STRING (AT_file (at)->filename);
7365 break;
7367 case dw_val_class_data8:
7368 CHECKSUM (at->dw_attr_val.v.val_data8);
7369 break;
7371 default:
7372 break;
7376 struct checksum_attributes
7378 dw_attr_node *at_name;
7379 dw_attr_node *at_type;
7380 dw_attr_node *at_friend;
7381 dw_attr_node *at_accessibility;
7382 dw_attr_node *at_address_class;
7383 dw_attr_node *at_alignment;
7384 dw_attr_node *at_allocated;
7385 dw_attr_node *at_artificial;
7386 dw_attr_node *at_associated;
7387 dw_attr_node *at_binary_scale;
7388 dw_attr_node *at_bit_offset;
7389 dw_attr_node *at_bit_size;
7390 dw_attr_node *at_bit_stride;
7391 dw_attr_node *at_byte_size;
7392 dw_attr_node *at_byte_stride;
7393 dw_attr_node *at_const_value;
7394 dw_attr_node *at_containing_type;
7395 dw_attr_node *at_count;
7396 dw_attr_node *at_data_location;
7397 dw_attr_node *at_data_member_location;
7398 dw_attr_node *at_decimal_scale;
7399 dw_attr_node *at_decimal_sign;
7400 dw_attr_node *at_default_value;
7401 dw_attr_node *at_digit_count;
7402 dw_attr_node *at_discr;
7403 dw_attr_node *at_discr_list;
7404 dw_attr_node *at_discr_value;
7405 dw_attr_node *at_encoding;
7406 dw_attr_node *at_endianity;
7407 dw_attr_node *at_explicit;
7408 dw_attr_node *at_is_optional;
7409 dw_attr_node *at_location;
7410 dw_attr_node *at_lower_bound;
7411 dw_attr_node *at_mutable;
7412 dw_attr_node *at_ordering;
7413 dw_attr_node *at_picture_string;
7414 dw_attr_node *at_prototyped;
7415 dw_attr_node *at_small;
7416 dw_attr_node *at_segment;
7417 dw_attr_node *at_string_length;
7418 dw_attr_node *at_string_length_bit_size;
7419 dw_attr_node *at_string_length_byte_size;
7420 dw_attr_node *at_threads_scaled;
7421 dw_attr_node *at_upper_bound;
7422 dw_attr_node *at_use_location;
7423 dw_attr_node *at_use_UTF8;
7424 dw_attr_node *at_variable_parameter;
7425 dw_attr_node *at_virtuality;
7426 dw_attr_node *at_visibility;
7427 dw_attr_node *at_vtable_elem_location;
7430 /* Collect the attributes that we will want to use for the checksum. */
7432 static void
7433 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7435 dw_attr_node *a;
7436 unsigned ix;
7438 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7440 switch (a->dw_attr)
7442 case DW_AT_name:
7443 attrs->at_name = a;
7444 break;
7445 case DW_AT_type:
7446 attrs->at_type = a;
7447 break;
7448 case DW_AT_friend:
7449 attrs->at_friend = a;
7450 break;
7451 case DW_AT_accessibility:
7452 attrs->at_accessibility = a;
7453 break;
7454 case DW_AT_address_class:
7455 attrs->at_address_class = a;
7456 break;
7457 case DW_AT_alignment:
7458 attrs->at_alignment = a;
7459 break;
7460 case DW_AT_allocated:
7461 attrs->at_allocated = a;
7462 break;
7463 case DW_AT_artificial:
7464 attrs->at_artificial = a;
7465 break;
7466 case DW_AT_associated:
7467 attrs->at_associated = a;
7468 break;
7469 case DW_AT_binary_scale:
7470 attrs->at_binary_scale = a;
7471 break;
7472 case DW_AT_bit_offset:
7473 attrs->at_bit_offset = a;
7474 break;
7475 case DW_AT_bit_size:
7476 attrs->at_bit_size = a;
7477 break;
7478 case DW_AT_bit_stride:
7479 attrs->at_bit_stride = a;
7480 break;
7481 case DW_AT_byte_size:
7482 attrs->at_byte_size = a;
7483 break;
7484 case DW_AT_byte_stride:
7485 attrs->at_byte_stride = a;
7486 break;
7487 case DW_AT_const_value:
7488 attrs->at_const_value = a;
7489 break;
7490 case DW_AT_containing_type:
7491 attrs->at_containing_type = a;
7492 break;
7493 case DW_AT_count:
7494 attrs->at_count = a;
7495 break;
7496 case DW_AT_data_location:
7497 attrs->at_data_location = a;
7498 break;
7499 case DW_AT_data_member_location:
7500 attrs->at_data_member_location = a;
7501 break;
7502 case DW_AT_decimal_scale:
7503 attrs->at_decimal_scale = a;
7504 break;
7505 case DW_AT_decimal_sign:
7506 attrs->at_decimal_sign = a;
7507 break;
7508 case DW_AT_default_value:
7509 attrs->at_default_value = a;
7510 break;
7511 case DW_AT_digit_count:
7512 attrs->at_digit_count = a;
7513 break;
7514 case DW_AT_discr:
7515 attrs->at_discr = a;
7516 break;
7517 case DW_AT_discr_list:
7518 attrs->at_discr_list = a;
7519 break;
7520 case DW_AT_discr_value:
7521 attrs->at_discr_value = a;
7522 break;
7523 case DW_AT_encoding:
7524 attrs->at_encoding = a;
7525 break;
7526 case DW_AT_endianity:
7527 attrs->at_endianity = a;
7528 break;
7529 case DW_AT_explicit:
7530 attrs->at_explicit = a;
7531 break;
7532 case DW_AT_is_optional:
7533 attrs->at_is_optional = a;
7534 break;
7535 case DW_AT_location:
7536 attrs->at_location = a;
7537 break;
7538 case DW_AT_lower_bound:
7539 attrs->at_lower_bound = a;
7540 break;
7541 case DW_AT_mutable:
7542 attrs->at_mutable = a;
7543 break;
7544 case DW_AT_ordering:
7545 attrs->at_ordering = a;
7546 break;
7547 case DW_AT_picture_string:
7548 attrs->at_picture_string = a;
7549 break;
7550 case DW_AT_prototyped:
7551 attrs->at_prototyped = a;
7552 break;
7553 case DW_AT_small:
7554 attrs->at_small = a;
7555 break;
7556 case DW_AT_segment:
7557 attrs->at_segment = a;
7558 break;
7559 case DW_AT_string_length:
7560 attrs->at_string_length = a;
7561 break;
7562 case DW_AT_string_length_bit_size:
7563 attrs->at_string_length_bit_size = a;
7564 break;
7565 case DW_AT_string_length_byte_size:
7566 attrs->at_string_length_byte_size = a;
7567 break;
7568 case DW_AT_threads_scaled:
7569 attrs->at_threads_scaled = a;
7570 break;
7571 case DW_AT_upper_bound:
7572 attrs->at_upper_bound = a;
7573 break;
7574 case DW_AT_use_location:
7575 attrs->at_use_location = a;
7576 break;
7577 case DW_AT_use_UTF8:
7578 attrs->at_use_UTF8 = a;
7579 break;
7580 case DW_AT_variable_parameter:
7581 attrs->at_variable_parameter = a;
7582 break;
7583 case DW_AT_virtuality:
7584 attrs->at_virtuality = a;
7585 break;
7586 case DW_AT_visibility:
7587 attrs->at_visibility = a;
7588 break;
7589 case DW_AT_vtable_elem_location:
7590 attrs->at_vtable_elem_location = a;
7591 break;
7592 default:
7593 break;
7598 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7600 static void
7601 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7603 dw_die_ref c;
7604 dw_die_ref decl;
7605 struct checksum_attributes attrs;
7607 CHECKSUM_ULEB128 ('D');
7608 CHECKSUM_ULEB128 (die->die_tag);
7610 memset (&attrs, 0, sizeof (attrs));
7612 decl = get_AT_ref (die, DW_AT_specification);
7613 if (decl != NULL)
7614 collect_checksum_attributes (&attrs, decl);
7615 collect_checksum_attributes (&attrs, die);
7617 CHECKSUM_ATTR (attrs.at_name);
7618 CHECKSUM_ATTR (attrs.at_accessibility);
7619 CHECKSUM_ATTR (attrs.at_address_class);
7620 CHECKSUM_ATTR (attrs.at_allocated);
7621 CHECKSUM_ATTR (attrs.at_artificial);
7622 CHECKSUM_ATTR (attrs.at_associated);
7623 CHECKSUM_ATTR (attrs.at_binary_scale);
7624 CHECKSUM_ATTR (attrs.at_bit_offset);
7625 CHECKSUM_ATTR (attrs.at_bit_size);
7626 CHECKSUM_ATTR (attrs.at_bit_stride);
7627 CHECKSUM_ATTR (attrs.at_byte_size);
7628 CHECKSUM_ATTR (attrs.at_byte_stride);
7629 CHECKSUM_ATTR (attrs.at_const_value);
7630 CHECKSUM_ATTR (attrs.at_containing_type);
7631 CHECKSUM_ATTR (attrs.at_count);
7632 CHECKSUM_ATTR (attrs.at_data_location);
7633 CHECKSUM_ATTR (attrs.at_data_member_location);
7634 CHECKSUM_ATTR (attrs.at_decimal_scale);
7635 CHECKSUM_ATTR (attrs.at_decimal_sign);
7636 CHECKSUM_ATTR (attrs.at_default_value);
7637 CHECKSUM_ATTR (attrs.at_digit_count);
7638 CHECKSUM_ATTR (attrs.at_discr);
7639 CHECKSUM_ATTR (attrs.at_discr_list);
7640 CHECKSUM_ATTR (attrs.at_discr_value);
7641 CHECKSUM_ATTR (attrs.at_encoding);
7642 CHECKSUM_ATTR (attrs.at_endianity);
7643 CHECKSUM_ATTR (attrs.at_explicit);
7644 CHECKSUM_ATTR (attrs.at_is_optional);
7645 CHECKSUM_ATTR (attrs.at_location);
7646 CHECKSUM_ATTR (attrs.at_lower_bound);
7647 CHECKSUM_ATTR (attrs.at_mutable);
7648 CHECKSUM_ATTR (attrs.at_ordering);
7649 CHECKSUM_ATTR (attrs.at_picture_string);
7650 CHECKSUM_ATTR (attrs.at_prototyped);
7651 CHECKSUM_ATTR (attrs.at_small);
7652 CHECKSUM_ATTR (attrs.at_segment);
7653 CHECKSUM_ATTR (attrs.at_string_length);
7654 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7655 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7656 CHECKSUM_ATTR (attrs.at_threads_scaled);
7657 CHECKSUM_ATTR (attrs.at_upper_bound);
7658 CHECKSUM_ATTR (attrs.at_use_location);
7659 CHECKSUM_ATTR (attrs.at_use_UTF8);
7660 CHECKSUM_ATTR (attrs.at_variable_parameter);
7661 CHECKSUM_ATTR (attrs.at_virtuality);
7662 CHECKSUM_ATTR (attrs.at_visibility);
7663 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7664 CHECKSUM_ATTR (attrs.at_type);
7665 CHECKSUM_ATTR (attrs.at_friend);
7666 CHECKSUM_ATTR (attrs.at_alignment);
7668 /* Checksum the child DIEs. */
7669 c = die->die_child;
7670 if (c) do {
7671 dw_attr_node *name_attr;
7673 c = c->die_sib;
7674 name_attr = get_AT (c, DW_AT_name);
7675 if (is_template_instantiation (c))
7677 /* Ignore instantiations of member type and function templates. */
7679 else if (name_attr != NULL
7680 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7682 /* Use a shallow checksum for named nested types and member
7683 functions. */
7684 CHECKSUM_ULEB128 ('S');
7685 CHECKSUM_ULEB128 (c->die_tag);
7686 CHECKSUM_STRING (AT_string (name_attr));
7688 else
7690 /* Use a deep checksum for other children. */
7691 /* Mark this DIE so it gets processed when unmarking. */
7692 if (c->die_mark == 0)
7693 c->die_mark = -1;
7694 die_checksum_ordered (c, ctx, mark);
7696 } while (c != die->die_child);
7698 CHECKSUM_ULEB128 (0);
7701 /* Add a type name and tag to a hash. */
7702 static void
7703 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7705 CHECKSUM_ULEB128 (tag);
7706 CHECKSUM_STRING (name);
7709 #undef CHECKSUM
7710 #undef CHECKSUM_STRING
7711 #undef CHECKSUM_ATTR
7712 #undef CHECKSUM_LEB128
7713 #undef CHECKSUM_ULEB128
7715 /* Generate the type signature for DIE. This is computed by generating an
7716 MD5 checksum over the DIE's tag, its relevant attributes, and its
7717 children. Attributes that are references to other DIEs are processed
7718 by recursion, using the MARK field to prevent infinite recursion.
7719 If the DIE is nested inside a namespace or another type, we also
7720 need to include that context in the signature. The lower 64 bits
7721 of the resulting MD5 checksum comprise the signature. */
7723 static void
7724 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7726 int mark;
7727 const char *name;
7728 unsigned char checksum[16];
7729 struct md5_ctx ctx;
7730 dw_die_ref decl;
7731 dw_die_ref parent;
7733 name = get_AT_string (die, DW_AT_name);
7734 decl = get_AT_ref (die, DW_AT_specification);
7735 parent = get_die_parent (die);
7737 /* First, compute a signature for just the type name (and its surrounding
7738 context, if any. This is stored in the type unit DIE for link-time
7739 ODR (one-definition rule) checking. */
7741 if (is_cxx () && name != NULL)
7743 md5_init_ctx (&ctx);
7745 /* Checksum the names of surrounding namespaces and structures. */
7746 if (parent != NULL)
7747 checksum_die_context (parent, &ctx);
7749 /* Checksum the current DIE. */
7750 die_odr_checksum (die->die_tag, name, &ctx);
7751 md5_finish_ctx (&ctx, checksum);
7753 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7756 /* Next, compute the complete type signature. */
7758 md5_init_ctx (&ctx);
7759 mark = 1;
7760 die->die_mark = mark;
7762 /* Checksum the names of surrounding namespaces and structures. */
7763 if (parent != NULL)
7764 checksum_die_context (parent, &ctx);
7766 /* Checksum the DIE and its children. */
7767 die_checksum_ordered (die, &ctx, &mark);
7768 unmark_all_dies (die);
7769 md5_finish_ctx (&ctx, checksum);
7771 /* Store the signature in the type node and link the type DIE and the
7772 type node together. */
7773 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7774 DWARF_TYPE_SIGNATURE_SIZE);
7775 die->comdat_type_p = true;
7776 die->die_id.die_type_node = type_node;
7777 type_node->type_die = die;
7779 /* If the DIE is a specification, link its declaration to the type node
7780 as well. */
7781 if (decl != NULL)
7783 decl->comdat_type_p = true;
7784 decl->die_id.die_type_node = type_node;
7788 /* Do the location expressions look same? */
7789 static inline int
7790 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7792 return loc1->dw_loc_opc == loc2->dw_loc_opc
7793 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7794 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7797 /* Do the values look the same? */
7798 static int
7799 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7801 dw_loc_descr_ref loc1, loc2;
7802 rtx r1, r2;
7804 if (v1->val_class != v2->val_class)
7805 return 0;
7807 switch (v1->val_class)
7809 case dw_val_class_const:
7810 case dw_val_class_const_implicit:
7811 return v1->v.val_int == v2->v.val_int;
7812 case dw_val_class_unsigned_const:
7813 case dw_val_class_unsigned_const_implicit:
7814 return v1->v.val_unsigned == v2->v.val_unsigned;
7815 case dw_val_class_const_double:
7816 return v1->v.val_double.high == v2->v.val_double.high
7817 && v1->v.val_double.low == v2->v.val_double.low;
7818 case dw_val_class_wide_int:
7819 return *v1->v.val_wide == *v2->v.val_wide;
7820 case dw_val_class_vec:
7821 if (v1->v.val_vec.length != v2->v.val_vec.length
7822 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7823 return 0;
7824 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7825 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7826 return 0;
7827 return 1;
7828 case dw_val_class_flag:
7829 return v1->v.val_flag == v2->v.val_flag;
7830 case dw_val_class_str:
7831 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7833 case dw_val_class_addr:
7834 r1 = v1->v.val_addr;
7835 r2 = v2->v.val_addr;
7836 if (GET_CODE (r1) != GET_CODE (r2))
7837 return 0;
7838 return !rtx_equal_p (r1, r2);
7840 case dw_val_class_offset:
7841 return v1->v.val_offset == v2->v.val_offset;
7843 case dw_val_class_loc:
7844 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7845 loc1 && loc2;
7846 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7847 if (!same_loc_p (loc1, loc2, mark))
7848 return 0;
7849 return !loc1 && !loc2;
7851 case dw_val_class_die_ref:
7852 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7854 case dw_val_class_symview:
7855 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7857 case dw_val_class_fde_ref:
7858 case dw_val_class_vms_delta:
7859 case dw_val_class_lbl_id:
7860 case dw_val_class_lineptr:
7861 case dw_val_class_macptr:
7862 case dw_val_class_loclistsptr:
7863 case dw_val_class_high_pc:
7864 return 1;
7866 case dw_val_class_file:
7867 case dw_val_class_file_implicit:
7868 return v1->v.val_file == v2->v.val_file;
7870 case dw_val_class_data8:
7871 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7873 default:
7874 return 1;
7878 /* Do the attributes look the same? */
7880 static int
7881 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7883 if (at1->dw_attr != at2->dw_attr)
7884 return 0;
7886 /* We don't care that this was compiled with a different compiler
7887 snapshot; if the output is the same, that's what matters. */
7888 if (at1->dw_attr == DW_AT_producer)
7889 return 1;
7891 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7894 /* Do the dies look the same? */
7896 static int
7897 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7899 dw_die_ref c1, c2;
7900 dw_attr_node *a1;
7901 unsigned ix;
7903 /* To avoid infinite recursion. */
7904 if (die1->die_mark)
7905 return die1->die_mark == die2->die_mark;
7906 die1->die_mark = die2->die_mark = ++(*mark);
7908 if (die1->die_tag != die2->die_tag)
7909 return 0;
7911 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7912 return 0;
7914 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7915 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7916 return 0;
7918 c1 = die1->die_child;
7919 c2 = die2->die_child;
7920 if (! c1)
7922 if (c2)
7923 return 0;
7925 else
7926 for (;;)
7928 if (!same_die_p (c1, c2, mark))
7929 return 0;
7930 c1 = c1->die_sib;
7931 c2 = c2->die_sib;
7932 if (c1 == die1->die_child)
7934 if (c2 == die2->die_child)
7935 break;
7936 else
7937 return 0;
7941 return 1;
7944 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7945 children, and set die_symbol. */
7947 static void
7948 compute_comp_unit_symbol (dw_die_ref unit_die)
7950 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7951 const char *base = die_name ? lbasename (die_name) : "anonymous";
7952 char *name = XALLOCAVEC (char, strlen (base) + 64);
7953 char *p;
7954 int i, mark;
7955 unsigned char checksum[16];
7956 struct md5_ctx ctx;
7958 /* Compute the checksum of the DIE, then append part of it as hex digits to
7959 the name filename of the unit. */
7961 md5_init_ctx (&ctx);
7962 mark = 0;
7963 die_checksum (unit_die, &ctx, &mark);
7964 unmark_all_dies (unit_die);
7965 md5_finish_ctx (&ctx, checksum);
7967 /* When we this for comp_unit_die () we have a DW_AT_name that might
7968 not start with a letter but with anything valid for filenames and
7969 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7970 character is not a letter. */
7971 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7972 clean_symbol_name (name);
7974 p = name + strlen (name);
7975 for (i = 0; i < 4; i++)
7977 sprintf (p, "%.2x", checksum[i]);
7978 p += 2;
7981 unit_die->die_id.die_symbol = xstrdup (name);
7984 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7986 static int
7987 is_type_die (dw_die_ref die)
7989 switch (die->die_tag)
7991 case DW_TAG_array_type:
7992 case DW_TAG_class_type:
7993 case DW_TAG_interface_type:
7994 case DW_TAG_enumeration_type:
7995 case DW_TAG_pointer_type:
7996 case DW_TAG_reference_type:
7997 case DW_TAG_rvalue_reference_type:
7998 case DW_TAG_string_type:
7999 case DW_TAG_structure_type:
8000 case DW_TAG_subroutine_type:
8001 case DW_TAG_union_type:
8002 case DW_TAG_ptr_to_member_type:
8003 case DW_TAG_set_type:
8004 case DW_TAG_subrange_type:
8005 case DW_TAG_base_type:
8006 case DW_TAG_const_type:
8007 case DW_TAG_file_type:
8008 case DW_TAG_packed_type:
8009 case DW_TAG_volatile_type:
8010 case DW_TAG_typedef:
8011 return 1;
8012 default:
8013 return 0;
8017 /* Returns true iff C is a compile-unit DIE. */
8019 static inline bool
8020 is_cu_die (dw_die_ref c)
8022 return c && (c->die_tag == DW_TAG_compile_unit
8023 || c->die_tag == DW_TAG_skeleton_unit);
8026 /* Returns true iff C is a unit DIE of some sort. */
8028 static inline bool
8029 is_unit_die (dw_die_ref c)
8031 return c && (c->die_tag == DW_TAG_compile_unit
8032 || c->die_tag == DW_TAG_partial_unit
8033 || c->die_tag == DW_TAG_type_unit
8034 || c->die_tag == DW_TAG_skeleton_unit);
8037 /* Returns true iff C is a namespace DIE. */
8039 static inline bool
8040 is_namespace_die (dw_die_ref c)
8042 return c && c->die_tag == DW_TAG_namespace;
8045 /* Return non-zero if this DIE is a template parameter. */
8047 static inline bool
8048 is_template_parameter (dw_die_ref die)
8050 switch (die->die_tag)
8052 case DW_TAG_template_type_param:
8053 case DW_TAG_template_value_param:
8054 case DW_TAG_GNU_template_template_param:
8055 case DW_TAG_GNU_template_parameter_pack:
8056 return true;
8057 default:
8058 return false;
8062 /* Return non-zero if this DIE represents a template instantiation. */
8064 static inline bool
8065 is_template_instantiation (dw_die_ref die)
8067 dw_die_ref c;
8069 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
8070 return false;
8071 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
8072 return false;
8075 static char *
8076 gen_internal_sym (const char *prefix)
8078 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
8080 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8081 return xstrdup (buf);
8084 /* Return non-zero if this DIE is a declaration. */
8086 static int
8087 is_declaration_die (dw_die_ref die)
8089 dw_attr_node *a;
8090 unsigned ix;
8092 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8093 if (a->dw_attr == DW_AT_declaration)
8094 return 1;
8096 return 0;
8099 /* Return non-zero if this DIE is nested inside a subprogram. */
8101 static int
8102 is_nested_in_subprogram (dw_die_ref die)
8104 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
8106 if (decl == NULL)
8107 decl = die;
8108 return local_scope_p (decl);
8111 /* Return non-zero if this DIE contains a defining declaration of a
8112 subprogram. */
8114 static int
8115 contains_subprogram_definition (dw_die_ref die)
8117 dw_die_ref c;
8119 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
8120 return 1;
8121 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
8122 return 0;
8125 /* Return non-zero if this is a type DIE that should be moved to a
8126 COMDAT .debug_types section or .debug_info section with DW_UT_*type
8127 unit type. */
8129 static int
8130 should_move_die_to_comdat (dw_die_ref die)
8132 switch (die->die_tag)
8134 case DW_TAG_class_type:
8135 case DW_TAG_structure_type:
8136 case DW_TAG_enumeration_type:
8137 case DW_TAG_union_type:
8138 /* Don't move declarations, inlined instances, types nested in a
8139 subprogram, or types that contain subprogram definitions. */
8140 if (is_declaration_die (die)
8141 || get_AT (die, DW_AT_abstract_origin)
8142 || is_nested_in_subprogram (die)
8143 || contains_subprogram_definition (die))
8144 return 0;
8145 return 1;
8146 case DW_TAG_array_type:
8147 case DW_TAG_interface_type:
8148 case DW_TAG_pointer_type:
8149 case DW_TAG_reference_type:
8150 case DW_TAG_rvalue_reference_type:
8151 case DW_TAG_string_type:
8152 case DW_TAG_subroutine_type:
8153 case DW_TAG_ptr_to_member_type:
8154 case DW_TAG_set_type:
8155 case DW_TAG_subrange_type:
8156 case DW_TAG_base_type:
8157 case DW_TAG_const_type:
8158 case DW_TAG_file_type:
8159 case DW_TAG_packed_type:
8160 case DW_TAG_volatile_type:
8161 case DW_TAG_typedef:
8162 default:
8163 return 0;
8167 /* Make a clone of DIE. */
8169 static dw_die_ref
8170 clone_die (dw_die_ref die)
8172 dw_die_ref clone = new_die_raw (die->die_tag);
8173 dw_attr_node *a;
8174 unsigned ix;
8176 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8177 add_dwarf_attr (clone, a);
8179 return clone;
8182 /* Make a clone of the tree rooted at DIE. */
8184 static dw_die_ref
8185 clone_tree (dw_die_ref die)
8187 dw_die_ref c;
8188 dw_die_ref clone = clone_die (die);
8190 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8192 return clone;
8195 /* Make a clone of DIE as a declaration. */
8197 static dw_die_ref
8198 clone_as_declaration (dw_die_ref die)
8200 dw_die_ref clone;
8201 dw_die_ref decl;
8202 dw_attr_node *a;
8203 unsigned ix;
8205 /* If the DIE is already a declaration, just clone it. */
8206 if (is_declaration_die (die))
8207 return clone_die (die);
8209 /* If the DIE is a specification, just clone its declaration DIE. */
8210 decl = get_AT_ref (die, DW_AT_specification);
8211 if (decl != NULL)
8213 clone = clone_die (decl);
8214 if (die->comdat_type_p)
8215 add_AT_die_ref (clone, DW_AT_signature, die);
8216 return clone;
8219 clone = new_die_raw (die->die_tag);
8221 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8223 /* We don't want to copy over all attributes.
8224 For example we don't want DW_AT_byte_size because otherwise we will no
8225 longer have a declaration and GDB will treat it as a definition. */
8227 switch (a->dw_attr)
8229 case DW_AT_abstract_origin:
8230 case DW_AT_artificial:
8231 case DW_AT_containing_type:
8232 case DW_AT_external:
8233 case DW_AT_name:
8234 case DW_AT_type:
8235 case DW_AT_virtuality:
8236 case DW_AT_linkage_name:
8237 case DW_AT_MIPS_linkage_name:
8238 add_dwarf_attr (clone, a);
8239 break;
8240 case DW_AT_byte_size:
8241 case DW_AT_alignment:
8242 default:
8243 break;
8247 if (die->comdat_type_p)
8248 add_AT_die_ref (clone, DW_AT_signature, die);
8250 add_AT_flag (clone, DW_AT_declaration, 1);
8251 return clone;
8255 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8257 struct decl_table_entry
8259 dw_die_ref orig;
8260 dw_die_ref copy;
8263 /* Helpers to manipulate hash table of copied declarations. */
8265 /* Hashtable helpers. */
8267 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8269 typedef die_struct *compare_type;
8270 static inline hashval_t hash (const decl_table_entry *);
8271 static inline bool equal (const decl_table_entry *, const die_struct *);
8274 inline hashval_t
8275 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8277 return htab_hash_pointer (entry->orig);
8280 inline bool
8281 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8282 const die_struct *entry2)
8284 return entry1->orig == entry2;
8287 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8289 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8290 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8291 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8292 to check if the ancestor has already been copied into UNIT. */
8294 static dw_die_ref
8295 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8296 decl_hash_type *decl_table)
8298 dw_die_ref parent = die->die_parent;
8299 dw_die_ref new_parent = unit;
8300 dw_die_ref copy;
8301 decl_table_entry **slot = NULL;
8302 struct decl_table_entry *entry = NULL;
8304 /* If DIE refers to a stub unfold that so we get the appropriate
8305 DIE registered as orig in decl_table. */
8306 if (dw_die_ref c = get_AT_ref (die, DW_AT_signature))
8307 die = c;
8309 if (decl_table)
8311 /* Check if the entry has already been copied to UNIT. */
8312 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8313 INSERT);
8314 if (*slot != HTAB_EMPTY_ENTRY)
8316 entry = *slot;
8317 return entry->copy;
8320 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8321 entry = XCNEW (struct decl_table_entry);
8322 entry->orig = die;
8323 entry->copy = NULL;
8324 *slot = entry;
8327 if (parent != NULL)
8329 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8330 if (spec != NULL)
8331 parent = spec;
8332 if (!is_unit_die (parent))
8333 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8336 copy = clone_as_declaration (die);
8337 add_child_die (new_parent, copy);
8339 if (decl_table)
8341 /* Record the pointer to the copy. */
8342 entry->copy = copy;
8345 return copy;
8347 /* Copy the declaration context to the new type unit DIE. This includes
8348 any surrounding namespace or type declarations. If the DIE has an
8349 AT_specification attribute, it also includes attributes and children
8350 attached to the specification, and returns a pointer to the original
8351 parent of the declaration DIE. Returns NULL otherwise. */
8353 static dw_die_ref
8354 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8356 dw_die_ref decl;
8357 dw_die_ref new_decl;
8358 dw_die_ref orig_parent = NULL;
8360 decl = get_AT_ref (die, DW_AT_specification);
8361 if (decl == NULL)
8362 decl = die;
8363 else
8365 unsigned ix;
8366 dw_die_ref c;
8367 dw_attr_node *a;
8369 /* The original DIE will be changed to a declaration, and must
8370 be moved to be a child of the original declaration DIE. */
8371 orig_parent = decl->die_parent;
8373 /* Copy the type node pointer from the new DIE to the original
8374 declaration DIE so we can forward references later. */
8375 decl->comdat_type_p = true;
8376 decl->die_id.die_type_node = die->die_id.die_type_node;
8378 remove_AT (die, DW_AT_specification);
8380 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8382 if (a->dw_attr != DW_AT_name
8383 && a->dw_attr != DW_AT_declaration
8384 && a->dw_attr != DW_AT_external)
8385 add_dwarf_attr (die, a);
8388 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8391 if (decl->die_parent != NULL
8392 && !is_unit_die (decl->die_parent))
8394 new_decl = copy_ancestor_tree (unit, decl, NULL);
8395 if (new_decl != NULL)
8397 remove_AT (new_decl, DW_AT_signature);
8398 add_AT_specification (die, new_decl);
8402 return orig_parent;
8405 /* Generate the skeleton ancestor tree for the given NODE, then clone
8406 the DIE and add the clone into the tree. */
8408 static void
8409 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8411 if (node->new_die != NULL)
8412 return;
8414 node->new_die = clone_as_declaration (node->old_die);
8416 if (node->parent != NULL)
8418 generate_skeleton_ancestor_tree (node->parent);
8419 add_child_die (node->parent->new_die, node->new_die);
8423 /* Generate a skeleton tree of DIEs containing any declarations that are
8424 found in the original tree. We traverse the tree looking for declaration
8425 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8427 static void
8428 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8430 skeleton_chain_node node;
8431 dw_die_ref c;
8432 dw_die_ref first;
8433 dw_die_ref prev = NULL;
8434 dw_die_ref next = NULL;
8436 node.parent = parent;
8438 first = c = parent->old_die->die_child;
8439 if (c)
8440 next = c->die_sib;
8441 if (c) do {
8442 if (prev == NULL || prev->die_sib == c)
8443 prev = c;
8444 c = next;
8445 next = (c == first ? NULL : c->die_sib);
8446 node.old_die = c;
8447 node.new_die = NULL;
8448 if (is_declaration_die (c))
8450 if (is_template_instantiation (c))
8452 /* Instantiated templates do not need to be cloned into the
8453 type unit. Just move the DIE and its children back to
8454 the skeleton tree (in the main CU). */
8455 remove_child_with_prev (c, prev);
8456 add_child_die (parent->new_die, c);
8457 c = prev;
8459 else if (c->comdat_type_p)
8461 /* This is the skeleton of earlier break_out_comdat_types
8462 type. Clone the existing DIE, but keep the children
8463 under the original (which is in the main CU). */
8464 dw_die_ref clone = clone_die (c);
8466 replace_child (c, clone, prev);
8467 generate_skeleton_ancestor_tree (parent);
8468 add_child_die (parent->new_die, c);
8469 c = clone;
8470 continue;
8472 else
8474 /* Clone the existing DIE, move the original to the skeleton
8475 tree (which is in the main CU), and put the clone, with
8476 all the original's children, where the original came from
8477 (which is about to be moved to the type unit). */
8478 dw_die_ref clone = clone_die (c);
8479 move_all_children (c, clone);
8481 /* If the original has a DW_AT_object_pointer attribute,
8482 it would now point to a child DIE just moved to the
8483 cloned tree, so we need to remove that attribute from
8484 the original. */
8485 remove_AT (c, DW_AT_object_pointer);
8487 replace_child (c, clone, prev);
8488 generate_skeleton_ancestor_tree (parent);
8489 add_child_die (parent->new_die, c);
8490 node.old_die = clone;
8491 node.new_die = c;
8492 c = clone;
8495 generate_skeleton_bottom_up (&node);
8496 } while (next != NULL);
8499 /* Wrapper function for generate_skeleton_bottom_up. */
8501 static dw_die_ref
8502 generate_skeleton (dw_die_ref die)
8504 skeleton_chain_node node;
8506 node.old_die = die;
8507 node.new_die = NULL;
8508 node.parent = NULL;
8510 /* If this type definition is nested inside another type,
8511 and is not an instantiation of a template, always leave
8512 at least a declaration in its place. */
8513 if (die->die_parent != NULL
8514 && is_type_die (die->die_parent)
8515 && !is_template_instantiation (die))
8516 node.new_die = clone_as_declaration (die);
8518 generate_skeleton_bottom_up (&node);
8519 return node.new_die;
8522 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8523 declaration. The original DIE is moved to a new compile unit so that
8524 existing references to it follow it to the new location. If any of the
8525 original DIE's descendants is a declaration, we need to replace the
8526 original DIE with a skeleton tree and move the declarations back into the
8527 skeleton tree. */
8529 static dw_die_ref
8530 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8531 dw_die_ref prev)
8533 dw_die_ref skeleton, orig_parent;
8535 /* Copy the declaration context to the type unit DIE. If the returned
8536 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8537 that DIE. */
8538 orig_parent = copy_declaration_context (unit, child);
8540 skeleton = generate_skeleton (child);
8541 if (skeleton == NULL)
8542 remove_child_with_prev (child, prev);
8543 else
8545 skeleton->comdat_type_p = true;
8546 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8548 /* If the original DIE was a specification, we need to put
8549 the skeleton under the parent DIE of the declaration.
8550 This leaves the original declaration in the tree, but
8551 it will be pruned later since there are no longer any
8552 references to it. */
8553 if (orig_parent != NULL)
8555 remove_child_with_prev (child, prev);
8556 add_child_die (orig_parent, skeleton);
8558 else
8559 replace_child (child, skeleton, prev);
8562 return skeleton;
8565 static void
8566 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8567 comdat_type_node *type_node,
8568 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8570 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8571 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8572 DWARF procedure references in the DW_AT_location attribute. */
8574 static dw_die_ref
8575 copy_dwarf_procedure (dw_die_ref die,
8576 comdat_type_node *type_node,
8577 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8579 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8581 /* DWARF procedures are not supposed to have children... */
8582 gcc_assert (die->die_child == NULL);
8584 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8585 gcc_assert (vec_safe_length (die->die_attr) == 1
8586 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8588 /* Do not copy more than once DWARF procedures. */
8589 bool existed;
8590 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8591 if (existed)
8592 return die_copy;
8594 die_copy = clone_die (die);
8595 add_child_die (type_node->root_die, die_copy);
8596 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8597 return die_copy;
8600 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8601 procedures in DIE's attributes. */
8603 static void
8604 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8605 comdat_type_node *type_node,
8606 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8608 dw_attr_node *a;
8609 unsigned i;
8611 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8613 dw_loc_descr_ref loc;
8615 if (a->dw_attr_val.val_class != dw_val_class_loc)
8616 continue;
8618 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8620 switch (loc->dw_loc_opc)
8622 case DW_OP_call2:
8623 case DW_OP_call4:
8624 case DW_OP_call_ref:
8625 gcc_assert (loc->dw_loc_oprnd1.val_class
8626 == dw_val_class_die_ref);
8627 loc->dw_loc_oprnd1.v.val_die_ref.die
8628 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8629 type_node,
8630 copied_dwarf_procs);
8632 default:
8633 break;
8639 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8640 rewrite references to point to the copies.
8642 References are looked for in DIE's attributes and recursively in all its
8643 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8644 mapping from old DWARF procedures to their copy. It is used not to copy
8645 twice the same DWARF procedure under TYPE_NODE. */
8647 static void
8648 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8649 comdat_type_node *type_node,
8650 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8652 dw_die_ref c;
8654 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8655 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8656 type_node,
8657 copied_dwarf_procs));
8660 /* Traverse the DIE and set up additional .debug_types or .debug_info
8661 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8662 section. */
8664 static void
8665 break_out_comdat_types (dw_die_ref die)
8667 dw_die_ref c;
8668 dw_die_ref first;
8669 dw_die_ref prev = NULL;
8670 dw_die_ref next = NULL;
8671 dw_die_ref unit = NULL;
8673 first = c = die->die_child;
8674 if (c)
8675 next = c->die_sib;
8676 if (c) do {
8677 if (prev == NULL || prev->die_sib == c)
8678 prev = c;
8679 c = next;
8680 next = (c == first ? NULL : c->die_sib);
8681 if (should_move_die_to_comdat (c))
8683 dw_die_ref replacement;
8684 comdat_type_node *type_node;
8686 /* Break out nested types into their own type units. */
8687 break_out_comdat_types (c);
8689 /* Create a new type unit DIE as the root for the new tree. */
8690 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8691 add_AT_unsigned (unit, DW_AT_language,
8692 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8694 /* Add the new unit's type DIE into the comdat type list. */
8695 type_node = ggc_cleared_alloc<comdat_type_node> ();
8696 type_node->root_die = unit;
8697 type_node->next = comdat_type_list;
8698 comdat_type_list = type_node;
8700 /* Generate the type signature. */
8701 generate_type_signature (c, type_node);
8703 /* Copy the declaration context, attributes, and children of the
8704 declaration into the new type unit DIE, then remove this DIE
8705 from the main CU (or replace it with a skeleton if necessary). */
8706 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8707 type_node->skeleton_die = replacement;
8709 /* Add the DIE to the new compunit. */
8710 add_child_die (unit, c);
8712 /* Types can reference DWARF procedures for type size or data location
8713 expressions. Calls in DWARF expressions cannot target procedures
8714 that are not in the same section. So we must copy DWARF procedures
8715 along with this type and then rewrite references to them. */
8716 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8717 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8719 if (replacement != NULL)
8720 c = replacement;
8722 else if (c->die_tag == DW_TAG_namespace
8723 || c->die_tag == DW_TAG_class_type
8724 || c->die_tag == DW_TAG_structure_type
8725 || c->die_tag == DW_TAG_union_type)
8727 /* Look for nested types that can be broken out. */
8728 break_out_comdat_types (c);
8730 } while (next != NULL);
8733 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8734 Enter all the cloned children into the hash table decl_table. */
8736 static dw_die_ref
8737 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8739 dw_die_ref c;
8740 dw_die_ref clone;
8741 struct decl_table_entry *entry;
8742 decl_table_entry **slot;
8744 if (die->die_tag == DW_TAG_subprogram)
8745 clone = clone_as_declaration (die);
8746 else
8747 clone = clone_die (die);
8749 slot = decl_table->find_slot_with_hash (die,
8750 htab_hash_pointer (die), INSERT);
8752 /* Assert that DIE isn't in the hash table yet. If it would be there
8753 before, the ancestors would be necessarily there as well, therefore
8754 clone_tree_partial wouldn't be called. */
8755 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8757 entry = XCNEW (struct decl_table_entry);
8758 entry->orig = die;
8759 entry->copy = clone;
8760 *slot = entry;
8762 if (die->die_tag != DW_TAG_subprogram)
8763 FOR_EACH_CHILD (die, c,
8764 add_child_die (clone, clone_tree_partial (c, decl_table)));
8766 return clone;
8769 /* Walk the DIE and its children, looking for references to incomplete
8770 or trivial types that are unmarked (i.e., that are not in the current
8771 type_unit). */
8773 static void
8774 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8776 dw_die_ref c;
8777 dw_attr_node *a;
8778 unsigned ix;
8780 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8782 if (AT_class (a) == dw_val_class_die_ref)
8784 dw_die_ref targ = AT_ref (a);
8785 decl_table_entry **slot;
8786 struct decl_table_entry *entry;
8788 if (targ->die_mark != 0 || targ->comdat_type_p)
8789 continue;
8791 slot = decl_table->find_slot_with_hash (targ,
8792 htab_hash_pointer (targ),
8793 INSERT);
8795 if (*slot != HTAB_EMPTY_ENTRY)
8797 /* TARG has already been copied, so we just need to
8798 modify the reference to point to the copy. */
8799 entry = *slot;
8800 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8802 else
8804 dw_die_ref parent = unit;
8805 dw_die_ref copy = clone_die (targ);
8807 /* Record in DECL_TABLE that TARG has been copied.
8808 Need to do this now, before the recursive call,
8809 because DECL_TABLE may be expanded and SLOT
8810 would no longer be a valid pointer. */
8811 entry = XCNEW (struct decl_table_entry);
8812 entry->orig = targ;
8813 entry->copy = copy;
8814 *slot = entry;
8816 /* If TARG is not a declaration DIE, we need to copy its
8817 children. */
8818 if (!is_declaration_die (targ))
8820 FOR_EACH_CHILD (
8821 targ, c,
8822 add_child_die (copy,
8823 clone_tree_partial (c, decl_table)));
8826 /* Make sure the cloned tree is marked as part of the
8827 type unit. */
8828 mark_dies (copy);
8830 /* If TARG has surrounding context, copy its ancestor tree
8831 into the new type unit. */
8832 if (targ->die_parent != NULL
8833 && !is_unit_die (targ->die_parent))
8834 parent = copy_ancestor_tree (unit, targ->die_parent,
8835 decl_table);
8837 add_child_die (parent, copy);
8838 a->dw_attr_val.v.val_die_ref.die = copy;
8840 /* Make sure the newly-copied DIE is walked. If it was
8841 installed in a previously-added context, it won't
8842 get visited otherwise. */
8843 if (parent != unit)
8845 /* Find the highest point of the newly-added tree,
8846 mark each node along the way, and walk from there. */
8847 parent->die_mark = 1;
8848 while (parent->die_parent
8849 && parent->die_parent->die_mark == 0)
8851 parent = parent->die_parent;
8852 parent->die_mark = 1;
8854 copy_decls_walk (unit, parent, decl_table);
8860 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8863 /* Collect skeleton dies in DIE created by break_out_comdat_types already
8864 and record them in DECL_TABLE. */
8866 static void
8867 collect_skeleton_dies (dw_die_ref die, decl_hash_type *decl_table)
8869 dw_die_ref c;
8871 if (dw_attr_node *a = get_AT (die, DW_AT_signature))
8873 dw_die_ref targ = AT_ref (a);
8874 gcc_assert (targ->die_mark == 0 && targ->comdat_type_p);
8875 decl_table_entry **slot
8876 = decl_table->find_slot_with_hash (targ,
8877 htab_hash_pointer (targ),
8878 INSERT);
8879 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8880 /* Record in DECL_TABLE that TARG has been already copied
8881 by remove_child_or_replace_with_skeleton. */
8882 decl_table_entry *entry = XCNEW (struct decl_table_entry);
8883 entry->orig = targ;
8884 entry->copy = die;
8885 *slot = entry;
8887 FOR_EACH_CHILD (die, c, collect_skeleton_dies (c, decl_table));
8890 /* Copy declarations for "unworthy" types into the new comdat section.
8891 Incomplete types, modified types, and certain other types aren't broken
8892 out into comdat sections of their own, so they don't have a signature,
8893 and we need to copy the declaration into the same section so that we
8894 don't have an external reference. */
8896 static void
8897 copy_decls_for_unworthy_types (dw_die_ref unit)
8899 mark_dies (unit);
8900 decl_hash_type decl_table (10);
8901 collect_skeleton_dies (unit, &decl_table);
8902 copy_decls_walk (unit, unit, &decl_table);
8903 unmark_dies (unit);
8906 /* Traverse the DIE and add a sibling attribute if it may have the
8907 effect of speeding up access to siblings. To save some space,
8908 avoid generating sibling attributes for DIE's without children. */
8910 static void
8911 add_sibling_attributes (dw_die_ref die)
8913 dw_die_ref c;
8915 if (! die->die_child)
8916 return;
8918 if (die->die_parent && die != die->die_parent->die_child)
8919 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8921 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8924 /* Output all location lists for the DIE and its children. */
8926 static void
8927 output_location_lists (dw_die_ref die)
8929 dw_die_ref c;
8930 dw_attr_node *a;
8931 unsigned ix;
8933 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8934 if (AT_class (a) == dw_val_class_loc_list)
8935 output_loc_list (AT_loc_list (a));
8937 FOR_EACH_CHILD (die, c, output_location_lists (c));
8940 /* During assign_location_list_indexes and output_loclists_offset the
8941 current index, after it the number of assigned indexes (i.e. how
8942 large the .debug_loclists* offset table should be). */
8943 static unsigned int loc_list_idx;
8945 /* Output all location list offsets for the DIE and its children. */
8947 static void
8948 output_loclists_offsets (dw_die_ref die)
8950 dw_die_ref c;
8951 dw_attr_node *a;
8952 unsigned ix;
8954 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8955 if (AT_class (a) == dw_val_class_loc_list)
8957 dw_loc_list_ref l = AT_loc_list (a);
8958 if (l->offset_emitted)
8959 continue;
8960 dw2_asm_output_delta (dwarf_offset_size, l->ll_symbol,
8961 loc_section_label, NULL);
8962 gcc_assert (l->hash == loc_list_idx);
8963 loc_list_idx++;
8964 l->offset_emitted = true;
8967 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8970 /* Recursively set indexes of location lists. */
8972 static void
8973 assign_location_list_indexes (dw_die_ref die)
8975 dw_die_ref c;
8976 dw_attr_node *a;
8977 unsigned ix;
8979 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8980 if (AT_class (a) == dw_val_class_loc_list)
8982 dw_loc_list_ref list = AT_loc_list (a);
8983 if (!list->num_assigned)
8985 list->num_assigned = true;
8986 list->hash = loc_list_idx++;
8990 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8993 /* We want to limit the number of external references, because they are
8994 larger than local references: a relocation takes multiple words, and
8995 even a sig8 reference is always eight bytes, whereas a local reference
8996 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8997 So if we encounter multiple external references to the same type DIE, we
8998 make a local typedef stub for it and redirect all references there.
9000 This is the element of the hash table for keeping track of these
9001 references. */
9003 struct external_ref
9005 dw_die_ref type;
9006 dw_die_ref stub;
9007 unsigned n_refs;
9010 /* Hashtable helpers. */
9012 struct external_ref_hasher : free_ptr_hash <external_ref>
9014 static inline hashval_t hash (const external_ref *);
9015 static inline bool equal (const external_ref *, const external_ref *);
9018 inline hashval_t
9019 external_ref_hasher::hash (const external_ref *r)
9021 dw_die_ref die = r->type;
9022 hashval_t h = 0;
9024 /* We can't use the address of the DIE for hashing, because
9025 that will make the order of the stub DIEs non-deterministic. */
9026 if (! die->comdat_type_p)
9027 /* We have a symbol; use it to compute a hash. */
9028 h = htab_hash_string (die->die_id.die_symbol);
9029 else
9031 /* We have a type signature; use a subset of the bits as the hash.
9032 The 8-byte signature is at least as large as hashval_t. */
9033 comdat_type_node *type_node = die->die_id.die_type_node;
9034 memcpy (&h, type_node->signature, sizeof (h));
9036 return h;
9039 inline bool
9040 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
9042 return r1->type == r2->type;
9045 typedef hash_table<external_ref_hasher> external_ref_hash_type;
9047 /* Return a pointer to the external_ref for references to DIE. */
9049 static struct external_ref *
9050 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
9052 struct external_ref ref, *ref_p;
9053 external_ref **slot;
9055 ref.type = die;
9056 slot = map->find_slot (&ref, INSERT);
9057 if (*slot != HTAB_EMPTY_ENTRY)
9058 return *slot;
9060 ref_p = XCNEW (struct external_ref);
9061 ref_p->type = die;
9062 *slot = ref_p;
9063 return ref_p;
9066 /* Subroutine of optimize_external_refs, below.
9068 If we see a type skeleton, record it as our stub. If we see external
9069 references, remember how many we've seen. */
9071 static void
9072 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
9074 dw_die_ref c;
9075 dw_attr_node *a;
9076 unsigned ix;
9077 struct external_ref *ref_p;
9079 if (is_type_die (die)
9080 && (c = get_AT_ref (die, DW_AT_signature)))
9082 /* This is a local skeleton; use it for local references. */
9083 ref_p = lookup_external_ref (map, c);
9084 ref_p->stub = die;
9087 /* Scan the DIE references, and remember any that refer to DIEs from
9088 other CUs (i.e. those which are not marked). */
9089 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9090 if (AT_class (a) == dw_val_class_die_ref
9091 && (c = AT_ref (a))->die_mark == 0
9092 && is_type_die (c))
9094 ref_p = lookup_external_ref (map, c);
9095 ref_p->n_refs++;
9098 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
9101 /* htab_traverse callback function for optimize_external_refs, below. SLOT
9102 points to an external_ref, DATA is the CU we're processing. If we don't
9103 already have a local stub, and we have multiple refs, build a stub. */
9106 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
9108 struct external_ref *ref_p = *slot;
9110 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
9112 /* We have multiple references to this type, so build a small stub.
9113 Both of these forms are a bit dodgy from the perspective of the
9114 DWARF standard, since technically they should have names. */
9115 dw_die_ref cu = data;
9116 dw_die_ref type = ref_p->type;
9117 dw_die_ref stub = NULL;
9119 if (type->comdat_type_p)
9121 /* If we refer to this type via sig8, use AT_signature. */
9122 stub = new_die (type->die_tag, cu, NULL_TREE);
9123 add_AT_die_ref (stub, DW_AT_signature, type);
9125 else
9127 /* Otherwise, use a typedef with no name. */
9128 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
9129 add_AT_die_ref (stub, DW_AT_type, type);
9132 stub->die_mark++;
9133 ref_p->stub = stub;
9135 return 1;
9138 /* DIE is a unit; look through all the DIE references to see if there are
9139 any external references to types, and if so, create local stubs for
9140 them which will be applied in build_abbrev_table. This is useful because
9141 references to local DIEs are smaller. */
9143 static external_ref_hash_type *
9144 optimize_external_refs (dw_die_ref die)
9146 external_ref_hash_type *map = new external_ref_hash_type (10);
9147 optimize_external_refs_1 (die, map);
9148 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
9149 return map;
9152 /* The following 3 variables are temporaries that are computed only during the
9153 build_abbrev_table call and used and released during the following
9154 optimize_abbrev_table call. */
9156 /* First abbrev_id that can be optimized based on usage. */
9157 static unsigned int abbrev_opt_start;
9159 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
9160 abbrev_id smaller than this, because they must be already sized
9161 during build_abbrev_table). */
9162 static unsigned int abbrev_opt_base_type_end;
9164 /* Vector of usage counts during build_abbrev_table. Indexed by
9165 abbrev_id - abbrev_opt_start. */
9166 static vec<unsigned int> abbrev_usage_count;
9168 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9169 static vec<dw_die_ref> sorted_abbrev_dies;
9171 /* The format of each DIE (and its attribute value pairs) is encoded in an
9172 abbreviation table. This routine builds the abbreviation table and assigns
9173 a unique abbreviation id for each abbreviation entry. The children of each
9174 die are visited recursively. */
9176 static void
9177 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9179 unsigned int abbrev_id = 0;
9180 dw_die_ref c;
9181 dw_attr_node *a;
9182 unsigned ix;
9183 dw_die_ref abbrev;
9185 /* Scan the DIE references, and replace any that refer to
9186 DIEs from other CUs (i.e. those which are not marked) with
9187 the local stubs we built in optimize_external_refs. */
9188 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9189 if (AT_class (a) == dw_val_class_die_ref
9190 && (c = AT_ref (a))->die_mark == 0)
9192 struct external_ref *ref_p;
9193 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9195 if (is_type_die (c)
9196 && (ref_p = lookup_external_ref (extern_map, c))
9197 && ref_p->stub && ref_p->stub != die)
9199 gcc_assert (a->dw_attr != DW_AT_signature);
9200 change_AT_die_ref (a, ref_p->stub);
9202 else
9203 /* We aren't changing this reference, so mark it external. */
9204 set_AT_ref_external (a, 1);
9207 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9209 dw_attr_node *die_a, *abbrev_a;
9210 unsigned ix;
9211 bool ok = true;
9213 if (abbrev_id == 0)
9214 continue;
9215 if (abbrev->die_tag != die->die_tag)
9216 continue;
9217 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9218 continue;
9220 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9221 continue;
9223 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9225 abbrev_a = &(*abbrev->die_attr)[ix];
9226 if ((abbrev_a->dw_attr != die_a->dw_attr)
9227 || (value_format (abbrev_a) != value_format (die_a)))
9229 ok = false;
9230 break;
9233 if (ok)
9234 break;
9237 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9239 vec_safe_push (abbrev_die_table, die);
9240 if (abbrev_opt_start)
9241 abbrev_usage_count.safe_push (0);
9243 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9245 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9246 sorted_abbrev_dies.safe_push (die);
9249 die->die_abbrev = abbrev_id;
9250 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9253 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9254 by die_abbrev's usage count, from the most commonly used
9255 abbreviation to the least. */
9257 static int
9258 die_abbrev_cmp (const void *p1, const void *p2)
9260 dw_die_ref die1 = *(const dw_die_ref *) p1;
9261 dw_die_ref die2 = *(const dw_die_ref *) p2;
9263 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9264 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9266 if (die1->die_abbrev >= abbrev_opt_base_type_end
9267 && die2->die_abbrev >= abbrev_opt_base_type_end)
9269 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9270 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9271 return -1;
9272 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9273 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9274 return 1;
9277 /* Stabilize the sort. */
9278 if (die1->die_abbrev < die2->die_abbrev)
9279 return -1;
9280 if (die1->die_abbrev > die2->die_abbrev)
9281 return 1;
9283 return 0;
9286 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9287 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9288 into dw_val_class_const_implicit or
9289 dw_val_class_unsigned_const_implicit. */
9291 static void
9292 optimize_implicit_const (unsigned int first_id, unsigned int end,
9293 vec<bool> &implicit_consts)
9295 /* It never makes sense if there is just one DIE using the abbreviation. */
9296 if (end < first_id + 2)
9297 return;
9299 dw_attr_node *a;
9300 unsigned ix, i;
9301 dw_die_ref die = sorted_abbrev_dies[first_id];
9302 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9303 if (implicit_consts[ix])
9305 enum dw_val_class new_class = dw_val_class_none;
9306 switch (AT_class (a))
9308 case dw_val_class_unsigned_const:
9309 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9310 continue;
9312 /* The .debug_abbrev section will grow by
9313 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9314 in all the DIEs using that abbreviation. */
9315 if (constant_size (AT_unsigned (a)) * (end - first_id)
9316 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9317 continue;
9319 new_class = dw_val_class_unsigned_const_implicit;
9320 break;
9322 case dw_val_class_const:
9323 new_class = dw_val_class_const_implicit;
9324 break;
9326 case dw_val_class_file:
9327 new_class = dw_val_class_file_implicit;
9328 break;
9330 default:
9331 continue;
9333 for (i = first_id; i < end; i++)
9334 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9335 = new_class;
9339 /* Attempt to optimize abbreviation table from abbrev_opt_start
9340 abbreviation above. */
9342 static void
9343 optimize_abbrev_table (void)
9345 if (abbrev_opt_start
9346 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9347 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9349 auto_vec<bool, 32> implicit_consts;
9350 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9352 unsigned int abbrev_id = abbrev_opt_start - 1;
9353 unsigned int first_id = ~0U;
9354 unsigned int last_abbrev_id = 0;
9355 unsigned int i;
9356 dw_die_ref die;
9357 if (abbrev_opt_base_type_end > abbrev_opt_start)
9358 abbrev_id = abbrev_opt_base_type_end - 1;
9359 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9360 most commonly used abbreviations come first. */
9361 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9363 dw_attr_node *a;
9364 unsigned ix;
9366 /* If calc_base_type_die_sizes has been called, the CU and
9367 base types after it can't be optimized, because we've already
9368 calculated their DIE offsets. We've sorted them first. */
9369 if (die->die_abbrev < abbrev_opt_base_type_end)
9370 continue;
9371 if (die->die_abbrev != last_abbrev_id)
9373 last_abbrev_id = die->die_abbrev;
9374 if (dwarf_version >= 5 && first_id != ~0U)
9375 optimize_implicit_const (first_id, i, implicit_consts);
9376 abbrev_id++;
9377 (*abbrev_die_table)[abbrev_id] = die;
9378 if (dwarf_version >= 5)
9380 first_id = i;
9381 implicit_consts.truncate (0);
9383 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9384 switch (AT_class (a))
9386 case dw_val_class_const:
9387 case dw_val_class_unsigned_const:
9388 case dw_val_class_file:
9389 implicit_consts.safe_push (true);
9390 break;
9391 default:
9392 implicit_consts.safe_push (false);
9393 break;
9397 else if (dwarf_version >= 5)
9399 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9400 if (!implicit_consts[ix])
9401 continue;
9402 else
9404 dw_attr_node *other_a
9405 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9406 if (!dw_val_equal_p (&a->dw_attr_val,
9407 &other_a->dw_attr_val))
9408 implicit_consts[ix] = false;
9411 die->die_abbrev = abbrev_id;
9413 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9414 if (dwarf_version >= 5 && first_id != ~0U)
9415 optimize_implicit_const (first_id, i, implicit_consts);
9418 abbrev_opt_start = 0;
9419 abbrev_opt_base_type_end = 0;
9420 abbrev_usage_count.release ();
9421 sorted_abbrev_dies.release ();
9424 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9426 static int
9427 constant_size (unsigned HOST_WIDE_INT value)
9429 int log;
9431 if (value == 0)
9432 log = 0;
9433 else
9434 log = floor_log2 (value);
9436 log = log / 8;
9437 log = 1 << (floor_log2 (log) + 1);
9439 return log;
9442 /* Return the size of a DIE as it is represented in the
9443 .debug_info section. */
9445 static unsigned long
9446 size_of_die (dw_die_ref die)
9448 unsigned long size = 0;
9449 dw_attr_node *a;
9450 unsigned ix;
9451 enum dwarf_form form;
9453 size += size_of_uleb128 (die->die_abbrev);
9454 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9456 switch (AT_class (a))
9458 case dw_val_class_addr:
9459 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9461 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9462 size += size_of_uleb128 (AT_index (a));
9464 else
9465 size += DWARF2_ADDR_SIZE;
9466 break;
9467 case dw_val_class_offset:
9468 size += dwarf_offset_size;
9469 break;
9470 case dw_val_class_loc:
9472 unsigned long lsize = size_of_locs (AT_loc (a));
9474 /* Block length. */
9475 if (dwarf_version >= 4)
9476 size += size_of_uleb128 (lsize);
9477 else
9478 size += constant_size (lsize);
9479 size += lsize;
9481 break;
9482 case dw_val_class_loc_list:
9483 if (dwarf_split_debug_info && dwarf_version >= 5)
9485 gcc_assert (AT_loc_list (a)->num_assigned);
9486 size += size_of_uleb128 (AT_loc_list (a)->hash);
9488 else
9489 size += dwarf_offset_size;
9490 break;
9491 case dw_val_class_view_list:
9492 size += dwarf_offset_size;
9493 break;
9494 case dw_val_class_range_list:
9495 if (value_format (a) == DW_FORM_rnglistx)
9497 gcc_assert (rnglist_idx);
9498 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9499 size += size_of_uleb128 (r->idx);
9501 else
9502 size += dwarf_offset_size;
9503 break;
9504 case dw_val_class_const:
9505 size += size_of_sleb128 (AT_int (a));
9506 break;
9507 case dw_val_class_unsigned_const:
9509 int csize = constant_size (AT_unsigned (a));
9510 if (dwarf_version == 3
9511 && a->dw_attr == DW_AT_data_member_location
9512 && csize >= 4)
9513 size += size_of_uleb128 (AT_unsigned (a));
9514 else
9515 size += csize;
9517 break;
9518 case dw_val_class_symview:
9519 if (symview_upper_bound <= 0xff)
9520 size += 1;
9521 else if (symview_upper_bound <= 0xffff)
9522 size += 2;
9523 else if (symview_upper_bound <= 0xffffffff)
9524 size += 4;
9525 else
9526 size += 8;
9527 break;
9528 case dw_val_class_const_implicit:
9529 case dw_val_class_unsigned_const_implicit:
9530 case dw_val_class_file_implicit:
9531 /* These occupy no size in the DIE, just an extra sleb128 in
9532 .debug_abbrev. */
9533 break;
9534 case dw_val_class_const_double:
9535 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9536 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9537 size++; /* block */
9538 break;
9539 case dw_val_class_wide_int:
9540 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9541 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9542 if (get_full_len (*a->dw_attr_val.v.val_wide)
9543 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9544 size++; /* block */
9545 break;
9546 case dw_val_class_vec:
9547 size += constant_size (a->dw_attr_val.v.val_vec.length
9548 * a->dw_attr_val.v.val_vec.elt_size)
9549 + a->dw_attr_val.v.val_vec.length
9550 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9551 break;
9552 case dw_val_class_flag:
9553 if (dwarf_version >= 4)
9554 /* Currently all add_AT_flag calls pass in 1 as last argument,
9555 so DW_FORM_flag_present can be used. If that ever changes,
9556 we'll need to use DW_FORM_flag and have some optimization
9557 in build_abbrev_table that will change those to
9558 DW_FORM_flag_present if it is set to 1 in all DIEs using
9559 the same abbrev entry. */
9560 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9561 else
9562 size += 1;
9563 break;
9564 case dw_val_class_die_ref:
9565 if (AT_ref_external (a))
9567 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9568 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9569 is sized by target address length, whereas in DWARF3
9570 it's always sized as an offset. */
9571 if (AT_ref (a)->comdat_type_p)
9572 size += DWARF_TYPE_SIGNATURE_SIZE;
9573 else if (dwarf_version == 2)
9574 size += DWARF2_ADDR_SIZE;
9575 else
9576 size += dwarf_offset_size;
9578 else
9579 size += dwarf_offset_size;
9580 break;
9581 case dw_val_class_fde_ref:
9582 size += dwarf_offset_size;
9583 break;
9584 case dw_val_class_lbl_id:
9585 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9587 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9588 size += size_of_uleb128 (AT_index (a));
9590 else
9591 size += DWARF2_ADDR_SIZE;
9592 break;
9593 case dw_val_class_lineptr:
9594 case dw_val_class_macptr:
9595 case dw_val_class_loclistsptr:
9596 size += dwarf_offset_size;
9597 break;
9598 case dw_val_class_str:
9599 form = AT_string_form (a);
9600 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9601 size += dwarf_offset_size;
9602 else if (form == dwarf_FORM (DW_FORM_strx))
9603 size += size_of_uleb128 (AT_index (a));
9604 else
9605 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9606 break;
9607 case dw_val_class_file:
9608 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9609 break;
9610 case dw_val_class_data8:
9611 size += 8;
9612 break;
9613 case dw_val_class_vms_delta:
9614 size += dwarf_offset_size;
9615 break;
9616 case dw_val_class_high_pc:
9617 size += DWARF2_ADDR_SIZE;
9618 break;
9619 case dw_val_class_discr_value:
9620 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9621 break;
9622 case dw_val_class_discr_list:
9624 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9626 /* This is a block, so we have the block length and then its
9627 data. */
9628 size += constant_size (block_size) + block_size;
9630 break;
9631 default:
9632 gcc_unreachable ();
9636 return size;
9639 /* Size the debugging information associated with a given DIE. Visits the
9640 DIE's children recursively. Updates the global variable next_die_offset, on
9641 each time through. Uses the current value of next_die_offset to update the
9642 die_offset field in each DIE. */
9644 static void
9645 calc_die_sizes (dw_die_ref die)
9647 dw_die_ref c;
9649 gcc_assert (die->die_offset == 0
9650 || (unsigned long int) die->die_offset == next_die_offset);
9651 die->die_offset = next_die_offset;
9652 next_die_offset += size_of_die (die);
9654 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9656 if (die->die_child != NULL)
9657 /* Count the null byte used to terminate sibling lists. */
9658 next_die_offset += 1;
9661 /* Size just the base type children at the start of the CU.
9662 This is needed because build_abbrev needs to size locs
9663 and sizing of type based stack ops needs to know die_offset
9664 values for the base types. */
9666 static void
9667 calc_base_type_die_sizes (void)
9669 unsigned long die_offset = (dwarf_split_debug_info
9670 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9671 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9672 unsigned int i;
9673 dw_die_ref base_type;
9674 #if ENABLE_ASSERT_CHECKING
9675 dw_die_ref prev = comp_unit_die ()->die_child;
9676 #endif
9678 die_offset += size_of_die (comp_unit_die ());
9679 for (i = 0; base_types.iterate (i, &base_type); i++)
9681 #if ENABLE_ASSERT_CHECKING
9682 gcc_assert (base_type->die_offset == 0
9683 && prev->die_sib == base_type
9684 && base_type->die_child == NULL
9685 && base_type->die_abbrev);
9686 prev = base_type;
9687 #endif
9688 if (abbrev_opt_start
9689 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9690 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9691 base_type->die_offset = die_offset;
9692 die_offset += size_of_die (base_type);
9696 /* Set the marks for a die and its children. We do this so
9697 that we know whether or not a reference needs to use FORM_ref_addr; only
9698 DIEs in the same CU will be marked. We used to clear out the offset
9699 and use that as the flag, but ran into ordering problems. */
9701 static void
9702 mark_dies (dw_die_ref die)
9704 dw_die_ref c;
9706 gcc_assert (!die->die_mark);
9708 die->die_mark = 1;
9709 FOR_EACH_CHILD (die, c, mark_dies (c));
9712 /* Clear the marks for a die and its children. */
9714 static void
9715 unmark_dies (dw_die_ref die)
9717 dw_die_ref c;
9719 if (! use_debug_types)
9720 gcc_assert (die->die_mark);
9722 die->die_mark = 0;
9723 FOR_EACH_CHILD (die, c, unmark_dies (c));
9726 /* Clear the marks for a die, its children and referred dies. */
9728 static void
9729 unmark_all_dies (dw_die_ref die)
9731 dw_die_ref c;
9732 dw_attr_node *a;
9733 unsigned ix;
9735 if (!die->die_mark)
9736 return;
9737 die->die_mark = 0;
9739 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9741 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9742 if (AT_class (a) == dw_val_class_die_ref)
9743 unmark_all_dies (AT_ref (a));
9746 /* Calculate if the entry should appear in the final output file. It may be
9747 from a pruned a type. */
9749 static bool
9750 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9752 /* By limiting gnu pubnames to definitions only, gold can generate a
9753 gdb index without entries for declarations, which don't include
9754 enough information to be useful. */
9755 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9756 return false;
9758 if (table == pubname_table)
9760 /* Enumerator names are part of the pubname table, but the
9761 parent DW_TAG_enumeration_type die may have been pruned.
9762 Don't output them if that is the case. */
9763 if (p->die->die_tag == DW_TAG_enumerator &&
9764 (p->die->die_parent == NULL
9765 || !p->die->die_parent->die_perennial_p))
9766 return false;
9768 /* Everything else in the pubname table is included. */
9769 return true;
9772 /* The pubtypes table shouldn't include types that have been
9773 pruned. */
9774 return (p->die->die_offset != 0
9775 || !flag_eliminate_unused_debug_types);
9778 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9779 generated for the compilation unit. */
9781 static unsigned long
9782 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9784 unsigned long size;
9785 unsigned i;
9786 pubname_entry *p;
9787 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9789 size = DWARF_PUBNAMES_HEADER_SIZE;
9790 FOR_EACH_VEC_ELT (*names, i, p)
9791 if (include_pubname_in_output (names, p))
9792 size += strlen (p->name) + dwarf_offset_size + 1 + space_for_flags;
9794 size += dwarf_offset_size;
9795 return size;
9798 /* Return the size of the information in the .debug_aranges section. */
9800 static unsigned long
9801 size_of_aranges (void)
9803 unsigned long size;
9805 size = DWARF_ARANGES_HEADER_SIZE;
9807 /* Count the address/length pair for this compilation unit. */
9808 if (switch_text_ranges)
9809 size += 2 * DWARF2_ADDR_SIZE
9810 * (vec_safe_length (switch_text_ranges) / 2 + 1);
9811 if (switch_cold_ranges)
9812 size += 2 * DWARF2_ADDR_SIZE
9813 * (vec_safe_length (switch_cold_ranges) / 2 + 1);
9814 if (have_multiple_function_sections)
9816 unsigned fde_idx;
9817 dw_fde_ref fde;
9819 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9821 if (fde->ignored_debug)
9822 continue;
9823 if (!fde->in_std_section)
9824 size += 2 * DWARF2_ADDR_SIZE;
9825 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9826 size += 2 * DWARF2_ADDR_SIZE;
9830 /* Count the two zero words used to terminated the address range table. */
9831 size += 2 * DWARF2_ADDR_SIZE;
9832 return size;
9835 /* Select the encoding of an attribute value. */
9837 static enum dwarf_form
9838 value_format (dw_attr_node *a)
9840 switch (AT_class (a))
9842 case dw_val_class_addr:
9843 /* Only very few attributes allow DW_FORM_addr. */
9844 switch (a->dw_attr)
9846 case DW_AT_low_pc:
9847 case DW_AT_high_pc:
9848 case DW_AT_entry_pc:
9849 case DW_AT_trampoline:
9850 return (AT_index (a) == NOT_INDEXED
9851 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9852 default:
9853 break;
9855 switch (DWARF2_ADDR_SIZE)
9857 case 1:
9858 return DW_FORM_data1;
9859 case 2:
9860 return DW_FORM_data2;
9861 case 4:
9862 return DW_FORM_data4;
9863 case 8:
9864 return DW_FORM_data8;
9865 default:
9866 gcc_unreachable ();
9868 case dw_val_class_loc_list:
9869 if (dwarf_split_debug_info
9870 && dwarf_version >= 5
9871 && AT_loc_list (a)->num_assigned)
9872 return DW_FORM_loclistx;
9873 /* FALLTHRU */
9874 case dw_val_class_view_list:
9875 case dw_val_class_range_list:
9876 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9877 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9878 care about sizes of .debug* sections in shared libraries and
9879 executables and don't take into account relocations that affect just
9880 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9881 table in the .debug_rnglists section. */
9882 if (dwarf_split_debug_info
9883 && dwarf_version >= 5
9884 && AT_class (a) == dw_val_class_range_list
9885 && rnglist_idx
9886 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9887 return DW_FORM_rnglistx;
9888 if (dwarf_version >= 4)
9889 return DW_FORM_sec_offset;
9890 /* FALLTHRU */
9891 case dw_val_class_vms_delta:
9892 case dw_val_class_offset:
9893 switch (dwarf_offset_size)
9895 case 4:
9896 return DW_FORM_data4;
9897 case 8:
9898 return DW_FORM_data8;
9899 default:
9900 gcc_unreachable ();
9902 case dw_val_class_loc:
9903 if (dwarf_version >= 4)
9904 return DW_FORM_exprloc;
9905 switch (constant_size (size_of_locs (AT_loc (a))))
9907 case 1:
9908 return DW_FORM_block1;
9909 case 2:
9910 return DW_FORM_block2;
9911 case 4:
9912 return DW_FORM_block4;
9913 default:
9914 gcc_unreachable ();
9916 case dw_val_class_const:
9917 return DW_FORM_sdata;
9918 case dw_val_class_unsigned_const:
9919 switch (constant_size (AT_unsigned (a)))
9921 case 1:
9922 return DW_FORM_data1;
9923 case 2:
9924 return DW_FORM_data2;
9925 case 4:
9926 /* In DWARF3 DW_AT_data_member_location with
9927 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9928 constant, so we need to use DW_FORM_udata if we need
9929 a large constant. */
9930 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9931 return DW_FORM_udata;
9932 return DW_FORM_data4;
9933 case 8:
9934 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9935 return DW_FORM_udata;
9936 return DW_FORM_data8;
9937 default:
9938 gcc_unreachable ();
9940 case dw_val_class_const_implicit:
9941 case dw_val_class_unsigned_const_implicit:
9942 case dw_val_class_file_implicit:
9943 return DW_FORM_implicit_const;
9944 case dw_val_class_const_double:
9945 switch (HOST_BITS_PER_WIDE_INT)
9947 case 8:
9948 return DW_FORM_data2;
9949 case 16:
9950 return DW_FORM_data4;
9951 case 32:
9952 return DW_FORM_data8;
9953 case 64:
9954 if (dwarf_version >= 5)
9955 return DW_FORM_data16;
9956 /* FALLTHRU */
9957 default:
9958 return DW_FORM_block1;
9960 case dw_val_class_wide_int:
9961 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9963 case 8:
9964 return DW_FORM_data1;
9965 case 16:
9966 return DW_FORM_data2;
9967 case 32:
9968 return DW_FORM_data4;
9969 case 64:
9970 return DW_FORM_data8;
9971 case 128:
9972 if (dwarf_version >= 5)
9973 return DW_FORM_data16;
9974 /* FALLTHRU */
9975 default:
9976 return DW_FORM_block1;
9978 case dw_val_class_symview:
9979 /* ??? We might use uleb128, but then we'd have to compute
9980 .debug_info offsets in the assembler. */
9981 if (symview_upper_bound <= 0xff)
9982 return DW_FORM_data1;
9983 else if (symview_upper_bound <= 0xffff)
9984 return DW_FORM_data2;
9985 else if (symview_upper_bound <= 0xffffffff)
9986 return DW_FORM_data4;
9987 else
9988 return DW_FORM_data8;
9989 case dw_val_class_vec:
9990 switch (constant_size (a->dw_attr_val.v.val_vec.length
9991 * a->dw_attr_val.v.val_vec.elt_size))
9993 case 1:
9994 return DW_FORM_block1;
9995 case 2:
9996 return DW_FORM_block2;
9997 case 4:
9998 return DW_FORM_block4;
9999 default:
10000 gcc_unreachable ();
10002 case dw_val_class_flag:
10003 if (dwarf_version >= 4)
10005 /* Currently all add_AT_flag calls pass in 1 as last argument,
10006 so DW_FORM_flag_present can be used. If that ever changes,
10007 we'll need to use DW_FORM_flag and have some optimization
10008 in build_abbrev_table that will change those to
10009 DW_FORM_flag_present if it is set to 1 in all DIEs using
10010 the same abbrev entry. */
10011 gcc_assert (a->dw_attr_val.v.val_flag == 1);
10012 return DW_FORM_flag_present;
10014 return DW_FORM_flag;
10015 case dw_val_class_die_ref:
10016 if (AT_ref_external (a))
10018 if (AT_ref (a)->comdat_type_p)
10019 return DW_FORM_ref_sig8;
10020 else
10021 return DW_FORM_ref_addr;
10023 else
10024 return DW_FORM_ref;
10025 case dw_val_class_fde_ref:
10026 return DW_FORM_data;
10027 case dw_val_class_lbl_id:
10028 return (AT_index (a) == NOT_INDEXED
10029 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
10030 case dw_val_class_lineptr:
10031 case dw_val_class_macptr:
10032 case dw_val_class_loclistsptr:
10033 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10034 case dw_val_class_str:
10035 return AT_string_form (a);
10036 case dw_val_class_file:
10037 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10039 case 1:
10040 return DW_FORM_data1;
10041 case 2:
10042 return DW_FORM_data2;
10043 case 4:
10044 return DW_FORM_data4;
10045 default:
10046 gcc_unreachable ();
10049 case dw_val_class_data8:
10050 return DW_FORM_data8;
10052 case dw_val_class_high_pc:
10053 switch (DWARF2_ADDR_SIZE)
10055 case 1:
10056 return DW_FORM_data1;
10057 case 2:
10058 return DW_FORM_data2;
10059 case 4:
10060 return DW_FORM_data4;
10061 case 8:
10062 return DW_FORM_data8;
10063 default:
10064 gcc_unreachable ();
10067 case dw_val_class_discr_value:
10068 return (a->dw_attr_val.v.val_discr_value.pos
10069 ? DW_FORM_udata
10070 : DW_FORM_sdata);
10071 case dw_val_class_discr_list:
10072 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
10074 case 1:
10075 return DW_FORM_block1;
10076 case 2:
10077 return DW_FORM_block2;
10078 case 4:
10079 return DW_FORM_block4;
10080 default:
10081 gcc_unreachable ();
10084 default:
10085 gcc_unreachable ();
10089 /* Output the encoding of an attribute value. */
10091 static void
10092 output_value_format (dw_attr_node *a)
10094 enum dwarf_form form = value_format (a);
10096 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10099 /* Given a die and id, produce the appropriate abbreviations. */
10101 static void
10102 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
10104 unsigned ix;
10105 dw_attr_node *a_attr;
10107 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10108 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10109 dwarf_tag_name (abbrev->die_tag));
10111 if (abbrev->die_child != NULL)
10112 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10113 else
10114 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10116 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
10118 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10119 dwarf_attr_name (a_attr->dw_attr));
10120 output_value_format (a_attr);
10121 if (value_format (a_attr) == DW_FORM_implicit_const)
10123 if (AT_class (a_attr) == dw_val_class_file_implicit)
10125 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
10126 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
10127 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
10129 else
10130 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
10134 dw2_asm_output_data (1, 0, NULL);
10135 dw2_asm_output_data (1, 0, NULL);
10139 /* Output the .debug_abbrev section which defines the DIE abbreviation
10140 table. */
10142 static void
10143 output_abbrev_section (void)
10145 unsigned int abbrev_id;
10146 dw_die_ref abbrev;
10148 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
10149 if (abbrev_id != 0)
10150 output_die_abbrevs (abbrev_id, abbrev);
10152 /* Terminate the table. */
10153 dw2_asm_output_data (1, 0, NULL);
10156 /* Return a new location list, given the begin and end range, and the
10157 expression. */
10159 static inline dw_loc_list_ref
10160 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
10161 const char *end, var_loc_view vend,
10162 const char *section)
10164 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
10166 retlist->begin = begin;
10167 retlist->begin_entry = NULL;
10168 retlist->end = end;
10169 retlist->end_entry = NULL;
10170 retlist->expr = expr;
10171 retlist->section = section;
10172 retlist->vbegin = vbegin;
10173 retlist->vend = vend;
10175 return retlist;
10178 /* Return true iff there's any nonzero view number in the loc list.
10180 ??? When views are not enabled, we'll often extend a single range
10181 to the entire function, so that we emit a single location
10182 expression rather than a location list. With views, even with a
10183 single range, we'll output a list if start or end have a nonzero
10184 view. If we change this, we may want to stop splitting a single
10185 range in dw_loc_list just because of a nonzero view, even if it
10186 straddles across hot/cold partitions. */
10188 static bool
10189 loc_list_has_views (dw_loc_list_ref list)
10191 if (!debug_variable_location_views)
10192 return false;
10194 for (dw_loc_list_ref loc = list;
10195 loc != NULL; loc = loc->dw_loc_next)
10196 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10197 return true;
10199 return false;
10202 /* Generate a new internal symbol for this location list node, if it
10203 hasn't got one yet. */
10205 static inline void
10206 gen_llsym (dw_loc_list_ref list)
10208 gcc_assert (!list->ll_symbol);
10209 list->ll_symbol = gen_internal_sym ("LLST");
10211 if (!loc_list_has_views (list))
10212 return;
10214 if (dwarf2out_locviews_in_attribute ())
10216 /* Use the same label_num for the view list. */
10217 label_num--;
10218 list->vl_symbol = gen_internal_sym ("LVUS");
10220 else
10221 list->vl_symbol = list->ll_symbol;
10224 /* Generate a symbol for the list, but only if we really want to emit
10225 it as a list. */
10227 static inline void
10228 maybe_gen_llsym (dw_loc_list_ref list)
10230 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10231 return;
10233 gen_llsym (list);
10236 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10237 NULL, don't consider size of the location expression. If we're not
10238 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10239 representation in *SIZEP. */
10241 static bool
10242 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10244 /* Don't output an entry that starts and ends at the same address. */
10245 if (strcmp (curr->begin, curr->end) == 0
10246 && curr->vbegin == curr->vend && !curr->force)
10247 return true;
10249 if (!sizep)
10250 return false;
10252 unsigned long size = size_of_locs (curr->expr);
10254 /* If the expression is too large, drop it on the floor. We could
10255 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10256 in the expression, but >= 64KB expressions for a single value
10257 in a single range are unlikely very useful. */
10258 if (dwarf_version < 5 && size > 0xffff)
10259 return true;
10261 *sizep = size;
10263 return false;
10266 /* Output a view pair loclist entry for CURR, if it requires one. */
10268 static void
10269 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10271 if (!dwarf2out_locviews_in_loclist ())
10272 return;
10274 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10275 return;
10277 #ifdef DW_LLE_view_pair
10278 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10280 if (dwarf2out_as_locview_support)
10282 if (ZERO_VIEW_P (curr->vbegin))
10283 dw2_asm_output_data_uleb128 (0, "Location view begin");
10284 else
10286 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10287 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10288 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10291 if (ZERO_VIEW_P (curr->vend))
10292 dw2_asm_output_data_uleb128 (0, "Location view end");
10293 else
10295 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10296 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10297 dw2_asm_output_symname_uleb128 (label, "Location view end");
10300 else
10302 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10303 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10305 #endif /* DW_LLE_view_pair */
10307 return;
10310 /* Output the location list given to us. */
10312 static void
10313 output_loc_list (dw_loc_list_ref list_head)
10315 int vcount = 0, lcount = 0;
10317 if (list_head->emitted)
10318 return;
10319 list_head->emitted = true;
10321 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10323 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10325 for (dw_loc_list_ref curr = list_head; curr != NULL;
10326 curr = curr->dw_loc_next)
10328 unsigned long size;
10330 if (skip_loc_list_entry (curr, &size))
10331 continue;
10333 vcount++;
10335 /* ?? dwarf_split_debug_info? */
10336 if (dwarf2out_as_locview_support)
10338 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10340 if (!ZERO_VIEW_P (curr->vbegin))
10342 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10343 dw2_asm_output_symname_uleb128 (label,
10344 "View list begin (%s)",
10345 list_head->vl_symbol);
10347 else
10348 dw2_asm_output_data_uleb128 (0,
10349 "View list begin (%s)",
10350 list_head->vl_symbol);
10352 if (!ZERO_VIEW_P (curr->vend))
10354 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10355 dw2_asm_output_symname_uleb128 (label,
10356 "View list end (%s)",
10357 list_head->vl_symbol);
10359 else
10360 dw2_asm_output_data_uleb128 (0,
10361 "View list end (%s)",
10362 list_head->vl_symbol);
10364 else
10366 dw2_asm_output_data_uleb128 (curr->vbegin,
10367 "View list begin (%s)",
10368 list_head->vl_symbol);
10369 dw2_asm_output_data_uleb128 (curr->vend,
10370 "View list end (%s)",
10371 list_head->vl_symbol);
10376 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10378 const char *last_section = NULL;
10379 const char *base_label = NULL;
10381 /* Walk the location list, and output each range + expression. */
10382 for (dw_loc_list_ref curr = list_head; curr != NULL;
10383 curr = curr->dw_loc_next)
10385 unsigned long size;
10387 /* Skip this entry? If we skip it here, we must skip it in the
10388 view list above as well. */
10389 if (skip_loc_list_entry (curr, &size))
10390 continue;
10392 lcount++;
10394 if (dwarf_version >= 5)
10396 if (dwarf_split_debug_info && HAVE_AS_LEB128)
10398 dwarf2out_maybe_output_loclist_view_pair (curr);
10399 /* For -gsplit-dwarf, emit DW_LLE_startx_length, which has
10400 uleb128 index into .debug_addr and uleb128 length. */
10401 dw2_asm_output_data (1, DW_LLE_startx_length,
10402 "DW_LLE_startx_length (%s)",
10403 list_head->ll_symbol);
10404 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10405 "Location list range start index "
10406 "(%s)", curr->begin);
10407 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10408 "Location list length (%s)",
10409 list_head->ll_symbol);
10411 else if (dwarf_split_debug_info)
10413 dwarf2out_maybe_output_loclist_view_pair (curr);
10414 /* For -gsplit-dwarf without usable .uleb128 support, emit
10415 DW_LLE_startx_endx, which has two uleb128 indexes into
10416 .debug_addr. */
10417 dw2_asm_output_data (1, DW_LLE_startx_endx,
10418 "DW_LLE_startx_endx (%s)",
10419 list_head->ll_symbol);
10420 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10421 "Location list range start index "
10422 "(%s)", curr->begin);
10423 dw2_asm_output_data_uleb128 (curr->end_entry->index,
10424 "Location list range end index "
10425 "(%s)", curr->end);
10427 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10429 dwarf2out_maybe_output_loclist_view_pair (curr);
10430 /* If all code is in .text section, the base address is
10431 already provided by the CU attributes. Use
10432 DW_LLE_offset_pair where both addresses are uleb128 encoded
10433 offsets against that base. */
10434 dw2_asm_output_data (1, DW_LLE_offset_pair,
10435 "DW_LLE_offset_pair (%s)",
10436 list_head->ll_symbol);
10437 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10438 "Location list begin address (%s)",
10439 list_head->ll_symbol);
10440 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10441 "Location list end address (%s)",
10442 list_head->ll_symbol);
10444 else if (HAVE_AS_LEB128)
10446 /* Otherwise, find out how many consecutive entries could share
10447 the same base entry. If just one, emit DW_LLE_start_length,
10448 otherwise emit DW_LLE_base_address for the base address
10449 followed by a series of DW_LLE_offset_pair. */
10450 if (last_section == NULL || curr->section != last_section)
10452 dw_loc_list_ref curr2;
10453 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10454 curr2 = curr2->dw_loc_next)
10456 if (strcmp (curr2->begin, curr2->end) == 0
10457 && !curr2->force)
10458 continue;
10459 break;
10461 if (curr2 == NULL || curr->section != curr2->section)
10462 last_section = NULL;
10463 else
10465 last_section = curr->section;
10466 base_label = curr->begin;
10467 dw2_asm_output_data (1, DW_LLE_base_address,
10468 "DW_LLE_base_address (%s)",
10469 list_head->ll_symbol);
10470 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10471 "Base address (%s)",
10472 list_head->ll_symbol);
10475 /* Only one entry with the same base address. Use
10476 DW_LLE_start_length with absolute address and uleb128
10477 length. */
10478 if (last_section == NULL)
10480 dwarf2out_maybe_output_loclist_view_pair (curr);
10481 dw2_asm_output_data (1, DW_LLE_start_length,
10482 "DW_LLE_start_length (%s)",
10483 list_head->ll_symbol);
10484 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10485 "Location list begin address (%s)",
10486 list_head->ll_symbol);
10487 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10488 "Location list length "
10489 "(%s)", list_head->ll_symbol);
10491 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10492 DW_LLE_base_address. */
10493 else
10495 dwarf2out_maybe_output_loclist_view_pair (curr);
10496 dw2_asm_output_data (1, DW_LLE_offset_pair,
10497 "DW_LLE_offset_pair (%s)",
10498 list_head->ll_symbol);
10499 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10500 "Location list begin address "
10501 "(%s)", list_head->ll_symbol);
10502 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10503 "Location list end address "
10504 "(%s)", list_head->ll_symbol);
10507 /* The assembler does not support .uleb128 directive. Emit
10508 DW_LLE_start_end with a pair of absolute addresses. */
10509 else
10511 dwarf2out_maybe_output_loclist_view_pair (curr);
10512 dw2_asm_output_data (1, DW_LLE_start_end,
10513 "DW_LLE_start_end (%s)",
10514 list_head->ll_symbol);
10515 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10516 "Location list begin address (%s)",
10517 list_head->ll_symbol);
10518 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10519 "Location list end address (%s)",
10520 list_head->ll_symbol);
10523 else if (dwarf_split_debug_info)
10525 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10526 and 4 byte length. */
10527 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10528 "Location list start/length entry (%s)",
10529 list_head->ll_symbol);
10530 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10531 "Location list range start index (%s)",
10532 curr->begin);
10533 /* The length field is 4 bytes. If we ever need to support
10534 an 8-byte length, we can add a new DW_LLE code or fall back
10535 to DW_LLE_GNU_start_end_entry. */
10536 dw2_asm_output_delta (4, curr->end, curr->begin,
10537 "Location list range length (%s)",
10538 list_head->ll_symbol);
10540 else if (!have_multiple_function_sections)
10542 /* Pair of relative addresses against start of text section. */
10543 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10544 "Location list begin address (%s)",
10545 list_head->ll_symbol);
10546 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10547 "Location list end address (%s)",
10548 list_head->ll_symbol);
10550 else
10552 /* Pair of absolute addresses. */
10553 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10554 "Location list begin address (%s)",
10555 list_head->ll_symbol);
10556 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10557 "Location list end address (%s)",
10558 list_head->ll_symbol);
10561 /* Output the block length for this list of location operations. */
10562 if (dwarf_version >= 5)
10563 dw2_asm_output_data_uleb128 (size, "Location expression size");
10564 else
10566 gcc_assert (size <= 0xffff);
10567 dw2_asm_output_data (2, size, "Location expression size");
10570 output_loc_sequence (curr->expr, -1);
10573 /* And finally list termination. */
10574 if (dwarf_version >= 5)
10575 dw2_asm_output_data (1, DW_LLE_end_of_list,
10576 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10577 else if (dwarf_split_debug_info)
10578 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10579 "Location list terminator (%s)",
10580 list_head->ll_symbol);
10581 else
10583 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10584 "Location list terminator begin (%s)",
10585 list_head->ll_symbol);
10586 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10587 "Location list terminator end (%s)",
10588 list_head->ll_symbol);
10591 gcc_assert (!list_head->vl_symbol
10592 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10595 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10596 section. Emit a relocated reference if val_entry is NULL, otherwise,
10597 emit an indirect reference. */
10599 static void
10600 output_range_list_offset (dw_attr_node *a)
10602 const char *name = dwarf_attr_name (a->dw_attr);
10604 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10606 if (dwarf_version >= 5)
10608 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10609 dw2_asm_output_offset (dwarf_offset_size, r->label,
10610 debug_ranges_section, "%s", name);
10612 else
10614 char *p = strchr (ranges_section_label, '\0');
10615 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10616 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10617 dw2_asm_output_offset (dwarf_offset_size, ranges_section_label,
10618 debug_ranges_section, "%s", name);
10619 *p = '\0';
10622 else if (dwarf_version >= 5)
10624 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10625 gcc_assert (rnglist_idx);
10626 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10628 else
10629 dw2_asm_output_data (dwarf_offset_size,
10630 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10631 "%s (offset from %s)", name, ranges_section_label);
10634 /* Output the offset into the debug_loc section. */
10636 static void
10637 output_loc_list_offset (dw_attr_node *a)
10639 char *sym = AT_loc_list (a)->ll_symbol;
10641 gcc_assert (sym);
10642 if (!dwarf_split_debug_info)
10643 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10644 "%s", dwarf_attr_name (a->dw_attr));
10645 else if (dwarf_version >= 5)
10647 gcc_assert (AT_loc_list (a)->num_assigned);
10648 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10649 dwarf_attr_name (a->dw_attr),
10650 sym);
10652 else
10653 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10654 "%s", dwarf_attr_name (a->dw_attr));
10657 /* Output the offset into the debug_loc section. */
10659 static void
10660 output_view_list_offset (dw_attr_node *a)
10662 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10664 gcc_assert (sym);
10665 if (dwarf_split_debug_info)
10666 dw2_asm_output_delta (dwarf_offset_size, sym, loc_section_label,
10667 "%s", dwarf_attr_name (a->dw_attr));
10668 else
10669 dw2_asm_output_offset (dwarf_offset_size, sym, debug_loc_section,
10670 "%s", dwarf_attr_name (a->dw_attr));
10673 /* Output an attribute's index or value appropriately. */
10675 static void
10676 output_attr_index_or_value (dw_attr_node *a)
10678 const char *name = dwarf_attr_name (a->dw_attr);
10680 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10682 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10683 return;
10685 switch (AT_class (a))
10687 case dw_val_class_addr:
10688 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10689 break;
10690 case dw_val_class_high_pc:
10691 case dw_val_class_lbl_id:
10692 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10693 break;
10694 default:
10695 gcc_unreachable ();
10699 /* Output a type signature. */
10701 static inline void
10702 output_signature (const char *sig, const char *name)
10704 int i;
10706 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10707 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10710 /* Output a discriminant value. */
10712 static inline void
10713 output_discr_value (dw_discr_value *discr_value, const char *name)
10715 if (discr_value->pos)
10716 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10717 else
10718 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10721 /* Output the DIE and its attributes. Called recursively to generate
10722 the definitions of each child DIE. */
10724 static void
10725 output_die (dw_die_ref die)
10727 dw_attr_node *a;
10728 dw_die_ref c;
10729 unsigned long size;
10730 unsigned ix;
10732 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10733 (unsigned long)die->die_offset,
10734 dwarf_tag_name (die->die_tag));
10736 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10738 const char *name = dwarf_attr_name (a->dw_attr);
10740 switch (AT_class (a))
10742 case dw_val_class_addr:
10743 output_attr_index_or_value (a);
10744 break;
10746 case dw_val_class_offset:
10747 dw2_asm_output_data (dwarf_offset_size, a->dw_attr_val.v.val_offset,
10748 "%s", name);
10749 break;
10751 case dw_val_class_range_list:
10752 output_range_list_offset (a);
10753 break;
10755 case dw_val_class_loc:
10756 size = size_of_locs (AT_loc (a));
10758 /* Output the block length for this list of location operations. */
10759 if (dwarf_version >= 4)
10760 dw2_asm_output_data_uleb128 (size, "%s", name);
10761 else
10762 dw2_asm_output_data (constant_size (size), size, "%s", name);
10764 output_loc_sequence (AT_loc (a), -1);
10765 break;
10767 case dw_val_class_const:
10768 /* ??? It would be slightly more efficient to use a scheme like is
10769 used for unsigned constants below, but gdb 4.x does not sign
10770 extend. Gdb 5.x does sign extend. */
10771 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10772 break;
10774 case dw_val_class_unsigned_const:
10776 int csize = constant_size (AT_unsigned (a));
10777 if (dwarf_version == 3
10778 && a->dw_attr == DW_AT_data_member_location
10779 && csize >= 4)
10780 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10781 else
10782 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10784 break;
10786 case dw_val_class_symview:
10788 int vsize;
10789 if (symview_upper_bound <= 0xff)
10790 vsize = 1;
10791 else if (symview_upper_bound <= 0xffff)
10792 vsize = 2;
10793 else if (symview_upper_bound <= 0xffffffff)
10794 vsize = 4;
10795 else
10796 vsize = 8;
10797 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10798 "%s", name);
10800 break;
10802 case dw_val_class_const_implicit:
10803 if (flag_debug_asm)
10804 fprintf (asm_out_file, "\t\t\t%s %s ("
10805 HOST_WIDE_INT_PRINT_DEC ")\n",
10806 ASM_COMMENT_START, name, AT_int (a));
10807 break;
10809 case dw_val_class_unsigned_const_implicit:
10810 if (flag_debug_asm)
10811 fprintf (asm_out_file, "\t\t\t%s %s ("
10812 HOST_WIDE_INT_PRINT_HEX ")\n",
10813 ASM_COMMENT_START, name, AT_unsigned (a));
10814 break;
10816 case dw_val_class_const_double:
10818 unsigned HOST_WIDE_INT first, second;
10820 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10821 dw2_asm_output_data (1,
10822 HOST_BITS_PER_DOUBLE_INT
10823 / HOST_BITS_PER_CHAR,
10824 NULL);
10826 if (WORDS_BIG_ENDIAN)
10828 first = a->dw_attr_val.v.val_double.high;
10829 second = a->dw_attr_val.v.val_double.low;
10831 else
10833 first = a->dw_attr_val.v.val_double.low;
10834 second = a->dw_attr_val.v.val_double.high;
10837 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10838 first, "%s", name);
10839 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10840 second, NULL);
10842 break;
10844 case dw_val_class_wide_int:
10846 int i;
10847 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10848 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10849 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10850 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10851 * l, NULL);
10853 if (WORDS_BIG_ENDIAN)
10854 for (i = len - 1; i >= 0; --i)
10856 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10857 "%s", name);
10858 name = "";
10860 else
10861 for (i = 0; i < len; ++i)
10863 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10864 "%s", name);
10865 name = "";
10868 break;
10870 case dw_val_class_vec:
10872 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10873 unsigned int len = a->dw_attr_val.v.val_vec.length;
10874 unsigned int i;
10875 unsigned char *p;
10877 dw2_asm_output_data (constant_size (len * elt_size),
10878 len * elt_size, "%s", name);
10879 if (elt_size > sizeof (HOST_WIDE_INT))
10881 elt_size /= 2;
10882 len *= 2;
10884 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10885 i < len;
10886 i++, p += elt_size)
10887 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10888 "fp or vector constant word %u", i);
10889 break;
10892 case dw_val_class_flag:
10893 if (dwarf_version >= 4)
10895 /* Currently all add_AT_flag calls pass in 1 as last argument,
10896 so DW_FORM_flag_present can be used. If that ever changes,
10897 we'll need to use DW_FORM_flag and have some optimization
10898 in build_abbrev_table that will change those to
10899 DW_FORM_flag_present if it is set to 1 in all DIEs using
10900 the same abbrev entry. */
10901 gcc_assert (AT_flag (a) == 1);
10902 if (flag_debug_asm)
10903 fprintf (asm_out_file, "\t\t\t%s %s\n",
10904 ASM_COMMENT_START, name);
10905 break;
10907 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10908 break;
10910 case dw_val_class_loc_list:
10911 output_loc_list_offset (a);
10912 break;
10914 case dw_val_class_view_list:
10915 output_view_list_offset (a);
10916 break;
10918 case dw_val_class_die_ref:
10919 if (AT_ref_external (a))
10921 if (AT_ref (a)->comdat_type_p)
10923 comdat_type_node *type_node
10924 = AT_ref (a)->die_id.die_type_node;
10926 gcc_assert (type_node);
10927 output_signature (type_node->signature, name);
10929 else
10931 const char *sym = AT_ref (a)->die_id.die_symbol;
10932 int size;
10934 gcc_assert (sym);
10935 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10936 length, whereas in DWARF3 it's always sized as an
10937 offset. */
10938 if (dwarf_version == 2)
10939 size = DWARF2_ADDR_SIZE;
10940 else
10941 size = dwarf_offset_size;
10942 /* ??? We cannot unconditionally output die_offset if
10943 non-zero - others might create references to those
10944 DIEs via symbols.
10945 And we do not clear its DIE offset after outputting it
10946 (and the label refers to the actual DIEs, not the
10947 DWARF CU unit header which is when using label + offset
10948 would be the correct thing to do).
10949 ??? This is the reason for the with_offset flag. */
10950 if (AT_ref (a)->with_offset)
10951 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10952 debug_info_section, "%s", name);
10953 else
10954 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10955 name);
10958 else
10960 gcc_assert (AT_ref (a)->die_offset);
10961 dw2_asm_output_data (dwarf_offset_size, AT_ref (a)->die_offset,
10962 "%s", name);
10964 break;
10966 case dw_val_class_fde_ref:
10968 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10970 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10971 a->dw_attr_val.v.val_fde_index * 2);
10972 dw2_asm_output_offset (dwarf_offset_size, l1, debug_frame_section,
10973 "%s", name);
10975 break;
10977 case dw_val_class_vms_delta:
10978 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10979 dw2_asm_output_vms_delta (dwarf_offset_size,
10980 AT_vms_delta2 (a), AT_vms_delta1 (a),
10981 "%s", name);
10982 #else
10983 dw2_asm_output_delta (dwarf_offset_size,
10984 AT_vms_delta2 (a), AT_vms_delta1 (a),
10985 "%s", name);
10986 #endif
10987 break;
10989 case dw_val_class_lbl_id:
10990 output_attr_index_or_value (a);
10991 break;
10993 case dw_val_class_lineptr:
10994 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
10995 debug_line_section, "%s", name);
10996 break;
10998 case dw_val_class_macptr:
10999 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11000 debug_macinfo_section, "%s", name);
11001 break;
11003 case dw_val_class_loclistsptr:
11004 dw2_asm_output_offset (dwarf_offset_size, AT_lbl (a),
11005 debug_loc_section, "%s", name);
11006 break;
11008 case dw_val_class_str:
11009 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
11010 dw2_asm_output_offset (dwarf_offset_size,
11011 a->dw_attr_val.v.val_str->label,
11012 debug_str_section,
11013 "%s: \"%s\"", name, AT_string (a));
11014 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
11015 dw2_asm_output_offset (dwarf_offset_size,
11016 a->dw_attr_val.v.val_str->label,
11017 debug_line_str_section,
11018 "%s: \"%s\"", name, AT_string (a));
11019 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
11020 dw2_asm_output_data_uleb128 (AT_index (a),
11021 "%s: \"%s\"", name, AT_string (a));
11022 else
11023 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11024 break;
11026 case dw_val_class_file:
11028 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11030 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11031 a->dw_attr_val.v.val_file->filename);
11032 break;
11035 case dw_val_class_file_implicit:
11036 if (flag_debug_asm)
11037 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
11038 ASM_COMMENT_START, name,
11039 maybe_emit_file (a->dw_attr_val.v.val_file),
11040 a->dw_attr_val.v.val_file->filename);
11041 break;
11043 case dw_val_class_data8:
11045 int i;
11047 for (i = 0; i < 8; i++)
11048 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11049 i == 0 ? "%s" : NULL, name);
11050 break;
11053 case dw_val_class_high_pc:
11054 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
11055 get_AT_low_pc (die), "DW_AT_high_pc");
11056 break;
11058 case dw_val_class_discr_value:
11059 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
11060 break;
11062 case dw_val_class_discr_list:
11064 dw_discr_list_ref list = AT_discr_list (a);
11065 const int size = size_of_discr_list (list);
11067 /* This is a block, so output its length first. */
11068 dw2_asm_output_data (constant_size (size), size,
11069 "%s: block size", name);
11071 for (; list != NULL; list = list->dw_discr_next)
11073 /* One byte for the discriminant value descriptor, and then as
11074 many LEB128 numbers as required. */
11075 if (list->dw_discr_range)
11076 dw2_asm_output_data (1, DW_DSC_range,
11077 "%s: DW_DSC_range", name);
11078 else
11079 dw2_asm_output_data (1, DW_DSC_label,
11080 "%s: DW_DSC_label", name);
11082 output_discr_value (&list->dw_discr_lower_bound, name);
11083 if (list->dw_discr_range)
11084 output_discr_value (&list->dw_discr_upper_bound, name);
11086 break;
11089 default:
11090 gcc_unreachable ();
11094 FOR_EACH_CHILD (die, c, output_die (c));
11096 /* Add null byte to terminate sibling list. */
11097 if (die->die_child != NULL)
11098 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11099 (unsigned long) die->die_offset);
11102 /* Output the dwarf version number. */
11104 static void
11105 output_dwarf_version ()
11107 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
11108 views in loclist. That will change eventually. */
11109 if (dwarf_version == 6)
11111 static bool once;
11112 if (!once)
11114 warning (0, "%<-gdwarf-6%> is output as version 5 with "
11115 "incompatibilities");
11116 once = true;
11118 dw2_asm_output_data (2, 5, "DWARF version number");
11120 else
11121 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
11124 /* Output the compilation unit that appears at the beginning of the
11125 .debug_info section, and precedes the DIE descriptions. */
11127 static void
11128 output_compilation_unit_header (enum dwarf_unit_type ut)
11130 if (!XCOFF_DEBUGGING_INFO)
11132 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11133 dw2_asm_output_data (4, 0xffffffff,
11134 "Initial length escape value indicating 64-bit DWARF extension");
11135 dw2_asm_output_data (dwarf_offset_size,
11136 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11137 "Length of Compilation Unit Info");
11140 output_dwarf_version ();
11141 if (dwarf_version >= 5)
11143 const char *name;
11144 switch (ut)
11146 case DW_UT_compile: name = "DW_UT_compile"; break;
11147 case DW_UT_type: name = "DW_UT_type"; break;
11148 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
11149 case DW_UT_split_type: name = "DW_UT_split_type"; break;
11150 default: gcc_unreachable ();
11152 dw2_asm_output_data (1, ut, "%s", name);
11153 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11155 dw2_asm_output_offset (dwarf_offset_size, abbrev_section_label,
11156 debug_abbrev_section,
11157 "Offset Into Abbrev. Section");
11158 if (dwarf_version < 5)
11159 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11162 /* Output the compilation unit DIE and its children. */
11164 static void
11165 output_comp_unit (dw_die_ref die, int output_if_empty,
11166 const unsigned char *dwo_id)
11168 const char *secname, *oldsym;
11169 char *tmp;
11171 /* Unless we are outputting main CU, we may throw away empty ones. */
11172 if (!output_if_empty && die->die_child == NULL)
11173 return;
11175 /* Even if there are no children of this DIE, we must output the information
11176 about the compilation unit. Otherwise, on an empty translation unit, we
11177 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
11178 will then complain when examining the file. First mark all the DIEs in
11179 this CU so we know which get local refs. */
11180 mark_dies (die);
11182 external_ref_hash_type *extern_map = optimize_external_refs (die);
11184 /* For now, optimize only the main CU, in order to optimize the rest
11185 we'd need to see all of them earlier. Leave the rest for post-linking
11186 tools like DWZ. */
11187 if (die == comp_unit_die ())
11188 abbrev_opt_start = vec_safe_length (abbrev_die_table);
11190 build_abbrev_table (die, extern_map);
11192 optimize_abbrev_table ();
11194 delete extern_map;
11196 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11197 next_die_offset = (dwo_id
11198 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11199 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11200 calc_die_sizes (die);
11202 oldsym = die->die_id.die_symbol;
11203 if (oldsym && die->comdat_type_p)
11205 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11207 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11208 secname = tmp;
11209 die->die_id.die_symbol = NULL;
11210 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11212 else
11214 switch_to_section (debug_info_section);
11215 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11216 info_section_emitted = true;
11219 /* For LTO cross unit DIE refs we want a symbol on the start of the
11220 debuginfo section, not on the CU DIE. */
11221 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11223 /* ??? No way to get visibility assembled without a decl. */
11224 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11225 get_identifier (oldsym), char_type_node);
11226 TREE_PUBLIC (decl) = true;
11227 TREE_STATIC (decl) = true;
11228 DECL_ARTIFICIAL (decl) = true;
11229 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11230 DECL_VISIBILITY_SPECIFIED (decl) = true;
11231 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11232 #ifdef ASM_WEAKEN_LABEL
11233 /* We prefer a .weak because that handles duplicates from duplicate
11234 archive members in a graceful way. */
11235 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11236 #else
11237 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11238 #endif
11239 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11242 /* Output debugging information. */
11243 output_compilation_unit_header (dwo_id
11244 ? DW_UT_split_compile : DW_UT_compile);
11245 if (dwarf_version >= 5)
11247 if (dwo_id != NULL)
11248 for (int i = 0; i < 8; i++)
11249 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11251 output_die (die);
11253 /* Leave the marks on the main CU, so we can check them in
11254 output_pubnames. */
11255 if (oldsym)
11257 unmark_dies (die);
11258 die->die_id.die_symbol = oldsym;
11262 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11263 and .debug_pubtypes. This is configured per-target, but can be
11264 overridden by the -gpubnames or -gno-pubnames options. */
11266 static inline bool
11267 want_pubnames (void)
11269 if (debug_info_level <= DINFO_LEVEL_TERSE
11270 /* Names and types go to the early debug part only. */
11271 || in_lto_p)
11272 return false;
11273 if (debug_generate_pub_sections != -1)
11274 return debug_generate_pub_sections;
11275 return targetm.want_debug_pub_sections;
11278 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11280 static void
11281 add_AT_pubnames (dw_die_ref die)
11283 if (want_pubnames ())
11284 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11287 /* Add a string attribute value to a skeleton DIE. */
11289 static inline void
11290 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11291 const char *str)
11293 dw_attr_node attr;
11294 struct indirect_string_node *node;
11296 if (! skeleton_debug_str_hash)
11297 skeleton_debug_str_hash
11298 = hash_table<indirect_string_hasher>::create_ggc (10);
11300 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11301 find_string_form (node);
11302 if (node->form == dwarf_FORM (DW_FORM_strx))
11303 node->form = DW_FORM_strp;
11305 attr.dw_attr = attr_kind;
11306 attr.dw_attr_val.val_class = dw_val_class_str;
11307 attr.dw_attr_val.val_entry = NULL;
11308 attr.dw_attr_val.v.val_str = node;
11309 add_dwarf_attr (die, &attr);
11312 /* Helper function to generate top-level dies for skeleton debug_info and
11313 debug_types. */
11315 static void
11316 add_top_level_skeleton_die_attrs (dw_die_ref die)
11318 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11319 const char *comp_dir = comp_dir_string ();
11321 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11322 if (comp_dir != NULL)
11323 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11324 add_AT_pubnames (die);
11325 if (addr_index_table != NULL && addr_index_table->size () > 0)
11326 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11329 /* Output skeleton debug sections that point to the dwo file. */
11331 static void
11332 output_skeleton_debug_sections (dw_die_ref comp_unit,
11333 const unsigned char *dwo_id)
11335 /* These attributes will be found in the full debug_info section. */
11336 remove_AT (comp_unit, DW_AT_producer);
11337 remove_AT (comp_unit, DW_AT_language);
11339 switch_to_section (debug_skeleton_info_section);
11340 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11342 /* Produce the skeleton compilation-unit header. This one differs enough from
11343 a normal CU header that it's better not to call output_compilation_unit
11344 header. */
11345 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11346 dw2_asm_output_data (4, 0xffffffff,
11347 "Initial length escape value indicating 64-bit "
11348 "DWARF extension");
11350 dw2_asm_output_data (dwarf_offset_size,
11351 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11352 - DWARF_INITIAL_LENGTH_SIZE
11353 + size_of_die (comp_unit),
11354 "Length of Compilation Unit Info");
11355 output_dwarf_version ();
11356 if (dwarf_version >= 5)
11358 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11359 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11361 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_abbrev_section_label,
11362 debug_skeleton_abbrev_section,
11363 "Offset Into Abbrev. Section");
11364 if (dwarf_version < 5)
11365 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11366 else
11367 for (int i = 0; i < 8; i++)
11368 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11370 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11371 output_die (comp_unit);
11373 /* Build the skeleton debug_abbrev section. */
11374 switch_to_section (debug_skeleton_abbrev_section);
11375 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11377 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11379 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11382 /* Output a comdat type unit DIE and its children. */
11384 static void
11385 output_comdat_type_unit (comdat_type_node *node,
11386 bool early_lto_debug ATTRIBUTE_UNUSED)
11388 const char *secname;
11389 char *tmp;
11390 int i;
11391 #if defined (OBJECT_FORMAT_ELF)
11392 tree comdat_key;
11393 #endif
11395 /* First mark all the DIEs in this CU so we know which get local refs. */
11396 mark_dies (node->root_die);
11398 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11400 build_abbrev_table (node->root_die, extern_map);
11402 delete extern_map;
11403 extern_map = NULL;
11405 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11406 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11407 calc_die_sizes (node->root_die);
11409 #if defined (OBJECT_FORMAT_ELF)
11410 if (dwarf_version >= 5)
11412 if (!dwarf_split_debug_info)
11413 secname = early_lto_debug ? DEBUG_LTO_INFO_SECTION : DEBUG_INFO_SECTION;
11414 else
11415 secname = (early_lto_debug
11416 ? DEBUG_LTO_DWO_INFO_SECTION : DEBUG_DWO_INFO_SECTION);
11418 else if (!dwarf_split_debug_info)
11419 secname = early_lto_debug ? ".gnu.debuglto_.debug_types" : ".debug_types";
11420 else
11421 secname = (early_lto_debug
11422 ? ".gnu.debuglto_.debug_types.dwo" : ".debug_types.dwo");
11424 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11425 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11426 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11427 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11428 comdat_key = get_identifier (tmp);
11429 targetm.asm_out.named_section (secname,
11430 SECTION_DEBUG | SECTION_LINKONCE,
11431 comdat_key);
11432 #else
11433 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11434 sprintf (tmp, (dwarf_version >= 5
11435 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11436 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11437 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11438 secname = tmp;
11439 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11440 #endif
11442 /* Output debugging information. */
11443 output_compilation_unit_header (dwarf_split_debug_info
11444 ? DW_UT_split_type : DW_UT_type);
11445 output_signature (node->signature, "Type Signature");
11446 dw2_asm_output_data (dwarf_offset_size, node->type_die->die_offset,
11447 "Offset to Type DIE");
11448 output_die (node->root_die);
11450 unmark_dies (node->root_die);
11453 /* Return the DWARF2/3 pubname associated with a decl. */
11455 static const char *
11456 dwarf2_name (tree decl, int scope)
11458 if (DECL_NAMELESS (decl))
11459 return NULL;
11460 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11463 /* Add a new entry to .debug_pubnames if appropriate. */
11465 static void
11466 add_pubname_string (const char *str, dw_die_ref die)
11468 pubname_entry e;
11470 e.die = die;
11471 e.name = xstrdup (str);
11472 vec_safe_push (pubname_table, e);
11475 static void
11476 add_pubname (tree decl, dw_die_ref die)
11478 if (!want_pubnames ())
11479 return;
11481 /* Don't add items to the table when we expect that the consumer will have
11482 just read the enclosing die. For example, if the consumer is looking at a
11483 class_member, it will either be inside the class already, or will have just
11484 looked up the class to find the member. Either way, searching the class is
11485 faster than searching the index. */
11486 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11487 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11489 const char *name = dwarf2_name (decl, 1);
11491 if (name)
11492 add_pubname_string (name, die);
11496 /* Add an enumerator to the pubnames section. */
11498 static void
11499 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11501 pubname_entry e;
11503 gcc_assert (scope_name);
11504 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11505 e.die = die;
11506 vec_safe_push (pubname_table, e);
11509 /* Add a new entry to .debug_pubtypes if appropriate. */
11511 static void
11512 add_pubtype (tree decl, dw_die_ref die)
11514 pubname_entry e;
11516 if (!want_pubnames ())
11517 return;
11519 if ((TREE_PUBLIC (decl)
11520 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11521 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11523 tree scope = NULL;
11524 const char *scope_name = "";
11525 const char *sep = is_cxx () ? "::" : ".";
11526 const char *name;
11528 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11529 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11531 scope_name = lang_hooks.dwarf_name (scope, 1);
11532 if (scope_name != NULL && scope_name[0] != '\0')
11533 scope_name = concat (scope_name, sep, NULL);
11534 else
11535 scope_name = "";
11538 if (TYPE_P (decl))
11539 name = type_tag (decl);
11540 else
11541 name = lang_hooks.dwarf_name (decl, 1);
11543 /* If we don't have a name for the type, there's no point in adding
11544 it to the table. */
11545 if (name != NULL && name[0] != '\0')
11547 e.die = die;
11548 e.name = concat (scope_name, name, NULL);
11549 vec_safe_push (pubtype_table, e);
11552 /* Although it might be more consistent to add the pubinfo for the
11553 enumerators as their dies are created, they should only be added if the
11554 enum type meets the criteria above. So rather than re-check the parent
11555 enum type whenever an enumerator die is created, just output them all
11556 here. This isn't protected by the name conditional because anonymous
11557 enums don't have names. */
11558 if (die->die_tag == DW_TAG_enumeration_type)
11560 dw_die_ref c;
11562 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11567 /* Output a single entry in the pubnames table. */
11569 static void
11570 output_pubname (dw_offset die_offset, pubname_entry *entry)
11572 dw_die_ref die = entry->die;
11573 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11575 dw2_asm_output_data (dwarf_offset_size, die_offset, "DIE offset");
11577 if (debug_generate_pub_sections == 2)
11579 /* This logic follows gdb's method for determining the value of the flag
11580 byte. */
11581 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11582 switch (die->die_tag)
11584 case DW_TAG_typedef:
11585 case DW_TAG_base_type:
11586 case DW_TAG_subrange_type:
11587 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11588 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11589 break;
11590 case DW_TAG_enumerator:
11591 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11592 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11593 if (!is_cxx ())
11594 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11595 break;
11596 case DW_TAG_subprogram:
11597 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11598 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11599 if (!is_ada ())
11600 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11601 break;
11602 case DW_TAG_constant:
11603 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11604 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11605 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11606 break;
11607 case DW_TAG_variable:
11608 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11609 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11610 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11611 break;
11612 case DW_TAG_namespace:
11613 case DW_TAG_imported_declaration:
11614 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11615 break;
11616 case DW_TAG_class_type:
11617 case DW_TAG_interface_type:
11618 case DW_TAG_structure_type:
11619 case DW_TAG_union_type:
11620 case DW_TAG_enumeration_type:
11621 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11622 if (!is_cxx ())
11623 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11624 break;
11625 default:
11626 /* An unusual tag. Leave the flag-byte empty. */
11627 break;
11629 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11630 "GDB-index flags");
11633 dw2_asm_output_nstring (entry->name, -1, "external name");
11637 /* Output the public names table used to speed up access to externally
11638 visible names; or the public types table used to find type definitions. */
11640 static void
11641 output_pubnames (vec<pubname_entry, va_gc> *names)
11643 unsigned i;
11644 unsigned long pubnames_length = size_of_pubnames (names);
11645 pubname_entry *pub;
11647 if (!XCOFF_DEBUGGING_INFO)
11649 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11650 dw2_asm_output_data (4, 0xffffffff,
11651 "Initial length escape value indicating 64-bit DWARF extension");
11652 dw2_asm_output_data (dwarf_offset_size, pubnames_length,
11653 "Pub Info Length");
11656 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11657 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11659 if (dwarf_split_debug_info)
11660 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11661 debug_skeleton_info_section,
11662 "Offset of Compilation Unit Info");
11663 else
11664 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11665 debug_info_section,
11666 "Offset of Compilation Unit Info");
11667 dw2_asm_output_data (dwarf_offset_size, next_die_offset,
11668 "Compilation Unit Length");
11670 FOR_EACH_VEC_ELT (*names, i, pub)
11672 if (include_pubname_in_output (names, pub))
11674 dw_offset die_offset = pub->die->die_offset;
11676 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11677 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11678 gcc_assert (pub->die->die_mark);
11680 /* If we're putting types in their own .debug_types sections,
11681 the .debug_pubtypes table will still point to the compile
11682 unit (not the type unit), so we want to use the offset of
11683 the skeleton DIE (if there is one). */
11684 if (pub->die->comdat_type_p && names == pubtype_table)
11686 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11688 if (type_node != NULL)
11689 die_offset = (type_node->skeleton_die != NULL
11690 ? type_node->skeleton_die->die_offset
11691 : comp_unit_die ()->die_offset);
11694 output_pubname (die_offset, pub);
11698 dw2_asm_output_data (dwarf_offset_size, 0, NULL);
11701 /* Output public names and types tables if necessary. */
11703 static void
11704 output_pubtables (void)
11706 if (!want_pubnames () || !info_section_emitted)
11707 return;
11709 switch_to_section (debug_pubnames_section);
11710 output_pubnames (pubname_table);
11711 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11712 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11713 simply won't look for the section. */
11714 switch_to_section (debug_pubtypes_section);
11715 output_pubnames (pubtype_table);
11719 /* Output the information that goes into the .debug_aranges table.
11720 Namely, define the beginning and ending address range of the
11721 text section generated for this compilation unit. */
11723 static void
11724 output_aranges (void)
11726 unsigned i;
11727 unsigned long aranges_length = size_of_aranges ();
11729 if (!XCOFF_DEBUGGING_INFO)
11731 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
11732 dw2_asm_output_data (4, 0xffffffff,
11733 "Initial length escape value indicating 64-bit DWARF extension");
11734 dw2_asm_output_data (dwarf_offset_size, aranges_length,
11735 "Length of Address Ranges Info");
11738 /* Version number for aranges is still 2, even up to DWARF5. */
11739 dw2_asm_output_data (2, 2, "DWARF aranges version");
11740 if (dwarf_split_debug_info)
11741 dw2_asm_output_offset (dwarf_offset_size, debug_skeleton_info_section_label,
11742 debug_skeleton_info_section,
11743 "Offset of Compilation Unit Info");
11744 else
11745 dw2_asm_output_offset (dwarf_offset_size, debug_info_section_label,
11746 debug_info_section,
11747 "Offset of Compilation Unit Info");
11748 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11749 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11751 /* We need to align to twice the pointer size here. */
11752 if (DWARF_ARANGES_PAD_SIZE)
11754 /* Pad using a 2 byte words so that padding is correct for any
11755 pointer size. */
11756 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11757 2 * DWARF2_ADDR_SIZE);
11758 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11759 dw2_asm_output_data (2, 0, NULL);
11762 /* It is necessary not to output these entries if the sections were
11763 not used; if the sections were not used, the length will be 0 and
11764 the address may end up as 0 if the section is discarded by ld
11765 --gc-sections, leaving an invalid (0, 0) entry that can be
11766 confused with the terminator. */
11767 if (switch_text_ranges)
11769 const char *prev_loc = text_section_label;
11770 const char *loc;
11771 unsigned idx;
11773 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
11774 if (prev_loc)
11776 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11777 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11778 prev_loc = NULL;
11780 else
11781 prev_loc = loc;
11783 if (prev_loc)
11785 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11786 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11787 prev_loc, "Length");
11791 if (switch_cold_ranges)
11793 const char *prev_loc = cold_text_section_label;
11794 const char *loc;
11795 unsigned idx;
11797 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
11798 if (prev_loc)
11800 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11801 dw2_asm_output_delta (DWARF2_ADDR_SIZE, loc, prev_loc, "Length");
11802 prev_loc = NULL;
11804 else
11805 prev_loc = loc;
11807 if (prev_loc)
11809 dw2_asm_output_addr (DWARF2_ADDR_SIZE, prev_loc, "Address");
11810 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11811 prev_loc, "Length");
11815 if (have_multiple_function_sections)
11817 unsigned fde_idx;
11818 dw_fde_ref fde;
11820 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11822 if (fde->ignored_debug)
11823 continue;
11824 if (!fde->in_std_section)
11826 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11827 "Address");
11828 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11829 fde->dw_fde_begin, "Length");
11831 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11833 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11834 "Address");
11835 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11836 fde->dw_fde_second_begin, "Length");
11841 /* Output the terminator words. */
11842 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11843 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11846 /* Add a new entry to .debug_ranges. Return its index into
11847 ranges_table vector. */
11849 static unsigned int
11850 add_ranges_num (int num, bool maybe_new_sec)
11852 dw_ranges r = { NULL, num, 0, maybe_new_sec, NULL, NULL };
11853 vec_safe_push (ranges_table, r);
11854 return vec_safe_length (ranges_table) - 1;
11857 /* Add a new entry to .debug_ranges corresponding to a block, or a
11858 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11859 this entry might be in a different section from previous range. */
11861 static unsigned int
11862 add_ranges (const_tree block, bool maybe_new_sec)
11864 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11867 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11868 chain, or middle entry of a chain that will be directly referred to. */
11870 static void
11871 note_rnglist_head (unsigned int offset)
11873 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11874 return;
11875 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11878 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11879 When using dwarf_split_debug_info, address attributes in dies destined
11880 for the final executable should be direct references--setting the
11881 parameter force_direct ensures this behavior. */
11883 static void
11884 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11885 bool *added, bool force_direct)
11887 unsigned int in_use = vec_safe_length (ranges_by_label);
11888 unsigned int offset;
11889 dw_ranges_by_label rbl = { begin, end };
11890 vec_safe_push (ranges_by_label, rbl);
11891 offset = add_ranges_num (-(int)in_use - 1, true);
11892 if (!*added)
11894 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11895 *added = true;
11896 note_rnglist_head (offset);
11897 if (dwarf_split_debug_info && force_direct)
11898 (*ranges_table)[offset].idx = DW_RANGES_IDX_SKELETON;
11902 /* Emit .debug_ranges section. */
11904 static void
11905 output_ranges (void)
11907 unsigned i;
11908 static const char *const start_fmt = "Offset %#x";
11909 const char *fmt = start_fmt;
11910 dw_ranges *r;
11912 switch_to_section (debug_ranges_section);
11913 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11914 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11916 int block_num = r->num;
11918 if (block_num > 0)
11920 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11921 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11923 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11924 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11926 /* If all code is in the text section, then the compilation
11927 unit base address defaults to DW_AT_low_pc, which is the
11928 base of the text section. */
11929 if (!have_multiple_function_sections)
11931 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11932 text_section_label,
11933 fmt, i * 2 * DWARF2_ADDR_SIZE);
11934 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11935 text_section_label, NULL);
11938 /* Otherwise, the compilation unit base address is zero,
11939 which allows us to use absolute addresses, and not worry
11940 about whether the target supports cross-section
11941 arithmetic. */
11942 else
11944 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11945 fmt, i * 2 * DWARF2_ADDR_SIZE);
11946 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11949 fmt = NULL;
11952 /* Negative block_num stands for an index into ranges_by_label. */
11953 else if (block_num < 0)
11955 int lab_idx = - block_num - 1;
11957 if (!have_multiple_function_sections)
11959 gcc_unreachable ();
11960 #if 0
11961 /* If we ever use add_ranges_by_labels () for a single
11962 function section, all we have to do is to take out
11963 the #if 0 above. */
11964 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11965 (*ranges_by_label)[lab_idx].begin,
11966 text_section_label,
11967 fmt, i * 2 * DWARF2_ADDR_SIZE);
11968 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11969 (*ranges_by_label)[lab_idx].end,
11970 text_section_label, NULL);
11971 #endif
11973 else
11975 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11976 (*ranges_by_label)[lab_idx].begin,
11977 fmt, i * 2 * DWARF2_ADDR_SIZE);
11978 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11979 (*ranges_by_label)[lab_idx].end,
11980 NULL);
11983 else
11985 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11986 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11987 fmt = start_fmt;
11992 /* Non-zero if .debug_line_str should be used for .debug_line section
11993 strings or strings that are likely shareable with those. */
11994 #define DWARF5_USE_DEBUG_LINE_STR \
11995 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11996 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11997 /* FIXME: there is no .debug_line_str.dwo section, \
11998 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11999 && !dwarf_split_debug_info)
12002 /* Returns TRUE if we are outputting DWARF5 and the assembler supports
12003 DWARF5 .debug_line tables using .debug_line_str or we generate
12004 it ourselves, except for split-dwarf which doesn't have a
12005 .debug_line_str. */
12006 static bool
12007 asm_outputs_debug_line_str (void)
12009 if (dwarf_version >= 5
12010 && ! output_asm_line_debug_info ()
12011 && DWARF5_USE_DEBUG_LINE_STR)
12012 return true;
12013 else
12015 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
12016 return !dwarf_split_debug_info && dwarf_version >= 5;
12017 #else
12018 return false;
12019 #endif
12023 /* Return true if it is beneficial to use DW_RLE_base_address{,x}.
12024 I is index of the following range. */
12026 static bool
12027 use_distinct_base_address_for_range (unsigned int i)
12029 if (i >= vec_safe_length (ranges_table))
12030 return false;
12032 dw_ranges *r2 = &(*ranges_table)[i];
12033 /* Use DW_RLE_base_address{,x} if there is a next range in the
12034 range list and is guaranteed to be in the same section. */
12035 return r2->num != 0 && r2->label == NULL && !r2->maybe_new_sec;
12038 /* Assign .debug_rnglists indexes and unique indexes into the debug_addr
12039 section when needed. */
12041 static void
12042 index_rnglists (void)
12044 unsigned i;
12045 dw_ranges *r;
12046 bool base = false;
12048 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12050 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12051 r->idx = rnglist_idx++;
12053 if (!have_multiple_function_sections)
12054 continue;
12055 int block_num = r->num;
12056 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12057 base = false;
12058 if (block_num > 0)
12060 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12061 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12063 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12064 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12066 if (HAVE_AS_LEB128)
12068 if (!base && use_distinct_base_address_for_range (i + 1))
12070 r->begin_entry = add_addr_table_entry (xstrdup (blabel),
12071 ate_kind_label);
12072 base = true;
12074 if (base)
12075 /* If we have a base, no need for further
12076 begin_entry/end_entry, as DW_RLE_offset_pair will be
12077 used. */
12078 continue;
12079 r->begin_entry
12080 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12081 /* No need for end_entry, DW_RLE_start{,x}_length will use
12082 length as opposed to a pair of addresses. */
12084 else
12086 r->begin_entry
12087 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12088 r->end_entry
12089 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12093 /* Negative block_num stands for an index into ranges_by_label. */
12094 else if (block_num < 0)
12096 int lab_idx = - block_num - 1;
12097 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12098 const char *elabel = (*ranges_by_label)[lab_idx].end;
12100 r->begin_entry
12101 = add_addr_table_entry (xstrdup (blabel), ate_kind_label);
12102 if (!HAVE_AS_LEB128)
12103 r->end_entry
12104 = add_addr_table_entry (xstrdup (elabel), ate_kind_label);
12109 /* Emit .debug_rnglists or (when DWO is true) .debug_rnglists.dwo section. */
12111 static bool
12112 output_rnglists (unsigned generation, bool dwo)
12114 unsigned i;
12115 dw_ranges *r;
12116 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
12117 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
12118 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
12120 if (dwo)
12121 switch_to_section (debug_ranges_dwo_section);
12122 else
12124 switch_to_section (debug_ranges_section);
12125 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
12127 /* There are up to 4 unique ranges labels per generation.
12128 See also init_sections_and_labels. */
12129 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
12130 2 + 2 * dwo + generation * 6);
12131 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
12132 3 + 2 * dwo + generation * 6);
12133 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
12134 dw2_asm_output_data (4, 0xffffffff,
12135 "Initial length escape value indicating "
12136 "64-bit DWARF extension");
12137 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
12138 "Length of Range Lists");
12139 ASM_OUTPUT_LABEL (asm_out_file, l1);
12140 output_dwarf_version ();
12141 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12142 dw2_asm_output_data (1, 0, "Segment Size");
12143 /* Emit the offset table only for -gsplit-dwarf. If we don't care
12144 about relocation sizes and primarily care about the size of .debug*
12145 sections in linked shared libraries and executables, then
12146 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
12147 into it are usually larger than just DW_FORM_sec_offset offsets
12148 into the .debug_rnglists section. */
12149 dw2_asm_output_data (4, dwo ? rnglist_idx : 0,
12150 "Offset Entry Count");
12151 if (dwo)
12153 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
12154 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12155 if (r->label && r->idx != DW_RANGES_IDX_SKELETON)
12156 dw2_asm_output_delta (dwarf_offset_size, r->label,
12157 ranges_base_label, NULL);
12160 const char *lab = "";
12161 const char *base = NULL;
12162 bool skipping = false;
12163 bool ret = false;
12164 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
12166 int block_num = r->num;
12168 if (r->label)
12170 if (dwarf_split_debug_info
12171 && (r->idx == DW_RANGES_IDX_SKELETON) == dwo)
12173 ret = true;
12174 skipping = true;
12175 continue;
12177 ASM_OUTPUT_LABEL (asm_out_file, r->label);
12178 lab = r->label;
12180 if (skipping)
12182 if (block_num == 0)
12183 skipping = false;
12184 continue;
12186 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
12187 base = NULL;
12188 if (block_num > 0)
12190 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12191 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12193 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12194 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12196 if (HAVE_AS_LEB128)
12198 /* If all code is in the text section, then the compilation
12199 unit base address defaults to DW_AT_low_pc, which is the
12200 base of the text section. */
12201 if (!have_multiple_function_sections)
12203 dw2_asm_output_data (1, DW_RLE_offset_pair,
12204 "DW_RLE_offset_pair (%s)", lab);
12205 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
12206 "Range begin address (%s)", lab);
12207 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
12208 "Range end address (%s)", lab);
12209 continue;
12211 if (base == NULL && use_distinct_base_address_for_range (i + 1))
12213 if (dwarf_split_debug_info)
12215 dw2_asm_output_data (1, DW_RLE_base_addressx,
12216 "DW_RLE_base_addressx (%s)", lab);
12217 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12218 "Base address index (%s)",
12219 blabel);
12221 else
12223 dw2_asm_output_data (1, DW_RLE_base_address,
12224 "DW_RLE_base_address (%s)", lab);
12225 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12226 "Base address (%s)", lab);
12228 strcpy (basebuf, blabel);
12229 base = basebuf;
12231 if (base)
12233 dw2_asm_output_data (1, DW_RLE_offset_pair,
12234 "DW_RLE_offset_pair (%s)", lab);
12235 dw2_asm_output_delta_uleb128 (blabel, base,
12236 "Range begin address (%s)", lab);
12237 dw2_asm_output_delta_uleb128 (elabel, base,
12238 "Range end address (%s)", lab);
12239 continue;
12241 if (dwarf_split_debug_info)
12243 dw2_asm_output_data (1, DW_RLE_startx_length,
12244 "DW_RLE_startx_length (%s)", lab);
12245 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12246 "Range begin address index "
12247 "(%s)", blabel);
12249 else
12251 dw2_asm_output_data (1, DW_RLE_start_length,
12252 "DW_RLE_start_length (%s)", lab);
12253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12254 "Range begin address (%s)", lab);
12256 dw2_asm_output_delta_uleb128 (elabel, blabel,
12257 "Range length (%s)", lab);
12259 else if (dwarf_split_debug_info)
12261 dw2_asm_output_data (1, DW_RLE_startx_endx,
12262 "DW_RLE_startx_endx (%s)", lab);
12263 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12264 "Range begin address index "
12265 "(%s)", blabel);
12266 dw2_asm_output_data_uleb128 (r->end_entry->index,
12267 "Range end address index "
12268 "(%s)", elabel);
12270 else
12272 dw2_asm_output_data (1, DW_RLE_start_end,
12273 "DW_RLE_start_end (%s)", lab);
12274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12275 "Range begin address (%s)", lab);
12276 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12277 "Range end address (%s)", lab);
12281 /* Negative block_num stands for an index into ranges_by_label. */
12282 else if (block_num < 0)
12284 int lab_idx = - block_num - 1;
12285 const char *blabel = (*ranges_by_label)[lab_idx].begin;
12286 const char *elabel = (*ranges_by_label)[lab_idx].end;
12288 if (!have_multiple_function_sections)
12289 gcc_unreachable ();
12290 if (HAVE_AS_LEB128)
12292 if (dwarf_split_debug_info)
12294 dw2_asm_output_data (1, DW_RLE_startx_length,
12295 "DW_RLE_startx_length (%s)", lab);
12296 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12297 "Range begin address index "
12298 "(%s)", blabel);
12300 else
12302 dw2_asm_output_data (1, DW_RLE_start_length,
12303 "DW_RLE_start_length (%s)", lab);
12304 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12305 "Range begin address (%s)", lab);
12307 dw2_asm_output_delta_uleb128 (elabel, blabel,
12308 "Range length (%s)", lab);
12310 else if (dwarf_split_debug_info)
12312 dw2_asm_output_data (1, DW_RLE_startx_endx,
12313 "DW_RLE_startx_endx (%s)", lab);
12314 dw2_asm_output_data_uleb128 (r->begin_entry->index,
12315 "Range begin address index "
12316 "(%s)", blabel);
12317 dw2_asm_output_data_uleb128 (r->end_entry->index,
12318 "Range end address index "
12319 "(%s)", elabel);
12321 else
12323 dw2_asm_output_data (1, DW_RLE_start_end,
12324 "DW_RLE_start_end (%s)", lab);
12325 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12326 "Range begin address (%s)", lab);
12327 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
12328 "Range end address (%s)", lab);
12331 else
12332 dw2_asm_output_data (1, DW_RLE_end_of_list,
12333 "DW_RLE_end_of_list (%s)", lab);
12335 ASM_OUTPUT_LABEL (asm_out_file, l2);
12336 return ret;
12339 /* Data structure containing information about input files. */
12340 struct file_info
12342 const char *path; /* Complete file name. */
12343 const char *fname; /* File name part. */
12344 int length; /* Length of entire string. */
12345 struct dwarf_file_data * file_idx; /* Index in input file table. */
12346 int dir_idx; /* Index in directory table. */
12349 /* Data structure containing information about directories with source
12350 files. */
12351 struct dir_info
12353 const char *path; /* Path including directory name. */
12354 int length; /* Path length. */
12355 int prefix; /* Index of directory entry which is a prefix. */
12356 int count; /* Number of files in this directory. */
12357 int dir_idx; /* Index of directory used as base. */
12360 /* Callback function for file_info comparison. We sort by looking at
12361 the directories in the path. */
12363 static int
12364 file_info_cmp (const void *p1, const void *p2)
12366 const struct file_info *const s1 = (const struct file_info *) p1;
12367 const struct file_info *const s2 = (const struct file_info *) p2;
12368 const unsigned char *cp1;
12369 const unsigned char *cp2;
12371 /* Take care of file names without directories. We need to make sure that
12372 we return consistent values to qsort since some will get confused if
12373 we return the same value when identical operands are passed in opposite
12374 orders. So if neither has a directory, return 0 and otherwise return
12375 1 or -1 depending on which one has the directory. We want the one with
12376 the directory to sort after the one without, so all no directory files
12377 are at the start (normally only the compilation unit file). */
12378 if ((s1->path == s1->fname || s2->path == s2->fname))
12379 return (s2->path == s2->fname) - (s1->path == s1->fname);
12381 cp1 = (const unsigned char *) s1->path;
12382 cp2 = (const unsigned char *) s2->path;
12384 while (1)
12386 ++cp1;
12387 ++cp2;
12388 /* Reached the end of the first path? If so, handle like above,
12389 but now we want longer directory prefixes before shorter ones. */
12390 if ((cp1 == (const unsigned char *) s1->fname)
12391 || (cp2 == (const unsigned char *) s2->fname))
12392 return ((cp1 == (const unsigned char *) s1->fname)
12393 - (cp2 == (const unsigned char *) s2->fname));
12395 /* Character of current path component the same? */
12396 else if (*cp1 != *cp2)
12397 return *cp1 - *cp2;
12401 struct file_name_acquire_data
12403 struct file_info *files;
12404 int used_files;
12405 int max_files;
12408 /* Traversal function for the hash table. */
12411 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12413 struct dwarf_file_data *d = *slot;
12414 struct file_info *fi;
12415 const char *f;
12417 gcc_assert (fnad->max_files >= d->emitted_number);
12419 if (! d->emitted_number)
12420 return 1;
12422 gcc_assert (fnad->max_files != fnad->used_files);
12424 fi = fnad->files + fnad->used_files++;
12426 f = d->filename;
12428 /* Skip all leading "./". */
12429 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12430 f += 2;
12432 /* Create a new array entry. */
12433 fi->path = f;
12434 fi->length = strlen (f);
12435 fi->file_idx = d;
12437 /* Search for the file name part. */
12438 f = strrchr (f, DIR_SEPARATOR);
12439 #if defined (DIR_SEPARATOR_2)
12441 const char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12443 if (g != NULL)
12445 if (f == NULL || f < g)
12446 f = g;
12449 #endif
12451 fi->fname = f == NULL ? fi->path : f + 1;
12452 return 1;
12455 /* Helper function for output_file_names. Emit a FORM encoded
12456 string STR, with assembly comment start ENTRY_KIND and
12457 index IDX */
12459 static void
12460 output_line_string (enum dwarf_form form, const char *str,
12461 const char *entry_kind, unsigned int idx)
12463 switch (form)
12465 case DW_FORM_string:
12466 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12467 break;
12468 case DW_FORM_line_strp:
12469 if (!debug_line_str_hash)
12470 debug_line_str_hash
12471 = hash_table<indirect_string_hasher>::create_ggc (10);
12473 struct indirect_string_node *node;
12474 node = find_AT_string_in_table (str, debug_line_str_hash);
12475 set_indirect_string (node);
12476 node->form = form;
12477 dw2_asm_output_offset (dwarf_offset_size, node->label,
12478 debug_line_str_section, "%s: %#x: \"%s\"",
12479 entry_kind, 0, node->str);
12480 break;
12481 default:
12482 gcc_unreachable ();
12486 /* Output the directory table and the file name table. We try to minimize
12487 the total amount of memory needed. A heuristic is used to avoid large
12488 slowdowns with many input files. */
12490 static void
12491 output_file_names (void)
12493 struct file_name_acquire_data fnad;
12494 int numfiles;
12495 struct file_info *files;
12496 struct dir_info *dirs;
12497 int *saved;
12498 int *savehere;
12499 int *backmap;
12500 int ndirs;
12501 int idx_offset;
12502 int i;
12504 if (!last_emitted_file)
12506 if (dwarf_version >= 5)
12508 const char *comp_dir = comp_dir_string ();
12509 if (comp_dir == NULL)
12510 comp_dir = "";
12511 dw2_asm_output_data (1, 1, "Directory entry format count");
12512 enum dwarf_form str_form = DW_FORM_string;
12513 if (DWARF5_USE_DEBUG_LINE_STR)
12514 str_form = DW_FORM_line_strp;
12515 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12516 dw2_asm_output_data_uleb128 (str_form, "%s",
12517 get_DW_FORM_name (str_form));
12518 dw2_asm_output_data_uleb128 (1, "Directories count");
12519 if (str_form == DW_FORM_string)
12520 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12521 else
12522 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12523 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12524 if (filename0 == NULL)
12525 filename0 = "";
12526 #ifdef VMS_DEBUGGING_INFO
12527 dw2_asm_output_data (1, 4, "File name entry format count");
12528 #else
12529 dw2_asm_output_data (1, 2, "File name entry format count");
12530 #endif
12531 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12532 dw2_asm_output_data_uleb128 (str_form, "%s",
12533 get_DW_FORM_name (str_form));
12534 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12535 "DW_LNCT_directory_index");
12536 dw2_asm_output_data_uleb128 (DW_FORM_data1, "%s",
12537 get_DW_FORM_name (DW_FORM_data1));
12538 #ifdef VMS_DEBUGGING_INFO
12539 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12540 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12541 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12542 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12543 #endif
12544 dw2_asm_output_data_uleb128 (1, "File names count");
12546 output_line_string (str_form, filename0, "File Entry", 0);
12547 dw2_asm_output_data (1, 0, NULL);
12548 #ifdef VMS_DEBUGGING_INFO
12549 dw2_asm_output_data_uleb128 (0, NULL);
12550 dw2_asm_output_data_uleb128 (0, NULL);
12551 #endif
12553 else
12555 dw2_asm_output_data (1, 0, "End directory table");
12556 dw2_asm_output_data (1, 0, "End file name table");
12558 return;
12561 numfiles = last_emitted_file->emitted_number;
12563 /* Allocate the various arrays we need. */
12564 files = XALLOCAVEC (struct file_info, numfiles);
12565 dirs = XALLOCAVEC (struct dir_info, numfiles);
12567 fnad.files = files;
12568 fnad.used_files = 0;
12569 fnad.max_files = numfiles;
12570 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12571 gcc_assert (fnad.used_files == fnad.max_files);
12573 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12575 /* Find all the different directories used. */
12576 dirs[0].path = files[0].path;
12577 dirs[0].length = files[0].fname - files[0].path;
12578 dirs[0].prefix = -1;
12579 dirs[0].count = 1;
12580 dirs[0].dir_idx = 0;
12581 files[0].dir_idx = 0;
12582 ndirs = 1;
12584 for (i = 1; i < numfiles; i++)
12585 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12586 && memcmp (dirs[ndirs - 1].path, files[i].path,
12587 dirs[ndirs - 1].length) == 0)
12589 /* Same directory as last entry. */
12590 files[i].dir_idx = ndirs - 1;
12591 ++dirs[ndirs - 1].count;
12593 else
12595 int j;
12597 /* This is a new directory. */
12598 dirs[ndirs].path = files[i].path;
12599 dirs[ndirs].length = files[i].fname - files[i].path;
12600 dirs[ndirs].count = 1;
12601 dirs[ndirs].dir_idx = ndirs;
12602 files[i].dir_idx = ndirs;
12604 /* Search for a prefix. */
12605 dirs[ndirs].prefix = -1;
12606 for (j = 0; j < ndirs; j++)
12607 if (dirs[j].length < dirs[ndirs].length
12608 && dirs[j].length > 1
12609 && (dirs[ndirs].prefix == -1
12610 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12611 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12612 dirs[ndirs].prefix = j;
12614 ++ndirs;
12617 /* Now to the actual work. We have to find a subset of the directories which
12618 allow expressing the file name using references to the directory table
12619 with the least amount of characters. We do not do an exhaustive search
12620 where we would have to check out every combination of every single
12621 possible prefix. Instead we use a heuristic which provides nearly optimal
12622 results in most cases and never is much off. */
12623 saved = XALLOCAVEC (int, ndirs);
12624 savehere = XALLOCAVEC (int, ndirs);
12626 memset (saved, '\0', ndirs * sizeof (saved[0]));
12627 for (i = 0; i < ndirs; i++)
12629 int j;
12630 int total;
12632 /* We can always save some space for the current directory. But this
12633 does not mean it will be enough to justify adding the directory. */
12634 savehere[i] = dirs[i].length;
12635 total = (savehere[i] - saved[i]) * dirs[i].count;
12637 for (j = i + 1; j < ndirs; j++)
12639 savehere[j] = 0;
12640 if (saved[j] < dirs[i].length)
12642 /* Determine whether the dirs[i] path is a prefix of the
12643 dirs[j] path. */
12644 int k;
12646 k = dirs[j].prefix;
12647 while (k != -1 && k != (int) i)
12648 k = dirs[k].prefix;
12650 if (k == (int) i)
12652 /* Yes it is. We can possibly save some memory by
12653 writing the filenames in dirs[j] relative to
12654 dirs[i]. */
12655 savehere[j] = dirs[i].length;
12656 total += (savehere[j] - saved[j]) * dirs[j].count;
12661 /* Check whether we can save enough to justify adding the dirs[i]
12662 directory. */
12663 if (total > dirs[i].length + 1)
12665 /* It's worthwhile adding. */
12666 for (j = i; j < ndirs; j++)
12667 if (savehere[j] > 0)
12669 /* Remember how much we saved for this directory so far. */
12670 saved[j] = savehere[j];
12672 /* Remember the prefix directory. */
12673 dirs[j].dir_idx = i;
12678 /* Emit the directory name table. */
12679 idx_offset = dirs[0].length > 0 ? 1 : 0;
12680 enum dwarf_form str_form = DW_FORM_string;
12681 enum dwarf_form idx_form = DW_FORM_udata;
12682 if (dwarf_version >= 5)
12684 const char *comp_dir = comp_dir_string ();
12685 if (comp_dir == NULL)
12686 comp_dir = "";
12687 dw2_asm_output_data (1, 1, "Directory entry format count");
12688 if (DWARF5_USE_DEBUG_LINE_STR)
12689 str_form = DW_FORM_line_strp;
12690 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12691 dw2_asm_output_data_uleb128 (str_form, "%s",
12692 get_DW_FORM_name (str_form));
12693 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12694 if (str_form == DW_FORM_string)
12696 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12697 for (i = 1 - idx_offset; i < ndirs; i++)
12698 dw2_asm_output_nstring (dirs[i].path,
12699 dirs[i].length
12700 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12701 "Directory Entry: %#x", i + idx_offset);
12703 else
12705 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12706 for (i = 1 - idx_offset; i < ndirs; i++)
12708 const char *str
12709 = ggc_alloc_string (dirs[i].path,
12710 dirs[i].length
12711 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12712 output_line_string (str_form, str, "Directory Entry",
12713 (unsigned) i + idx_offset);
12717 else
12719 for (i = 1 - idx_offset; i < ndirs; i++)
12720 dw2_asm_output_nstring (dirs[i].path,
12721 dirs[i].length
12722 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12723 "Directory Entry: %#x", i + idx_offset);
12725 dw2_asm_output_data (1, 0, "End directory table");
12728 /* We have to emit them in the order of emitted_number since that's
12729 used in the debug info generation. To do this efficiently we
12730 generate a back-mapping of the indices first. */
12731 backmap = XALLOCAVEC (int, numfiles);
12732 for (i = 0; i < numfiles; i++)
12733 backmap[files[i].file_idx->emitted_number - 1] = i;
12735 if (dwarf_version >= 5)
12737 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12738 if (filename0 == NULL)
12739 filename0 = "";
12740 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12741 DW_FORM_data2. Choose one based on the number of directories
12742 and how much space would they occupy in each encoding.
12743 If we have at most 256 directories, all indexes fit into
12744 a single byte, so DW_FORM_data1 is most compact (if there
12745 are at most 128 directories, DW_FORM_udata would be as
12746 compact as that, but not shorter and slower to decode). */
12747 if (ndirs + idx_offset <= 256)
12748 idx_form = DW_FORM_data1;
12749 /* If there are more than 65536 directories, we have to use
12750 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12751 Otherwise, compute what space would occupy if all the indexes
12752 used DW_FORM_udata - sum - and compare that to how large would
12753 be DW_FORM_data2 encoding, and pick the more efficient one. */
12754 else if (ndirs + idx_offset <= 65536)
12756 unsigned HOST_WIDE_INT sum = 1;
12757 for (i = 0; i < numfiles; i++)
12759 int file_idx = backmap[i];
12760 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12761 sum += size_of_uleb128 (dir_idx);
12763 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12764 idx_form = DW_FORM_data2;
12766 #ifdef VMS_DEBUGGING_INFO
12767 dw2_asm_output_data (1, 4, "File name entry format count");
12768 #else
12769 dw2_asm_output_data (1, 2, "File name entry format count");
12770 #endif
12771 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12772 dw2_asm_output_data_uleb128 (str_form, "%s",
12773 get_DW_FORM_name (str_form));
12774 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12775 "DW_LNCT_directory_index");
12776 dw2_asm_output_data_uleb128 (idx_form, "%s",
12777 get_DW_FORM_name (idx_form));
12778 #ifdef VMS_DEBUGGING_INFO
12779 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12780 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12781 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12782 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12783 #endif
12784 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12786 output_line_string (str_form, filename0, "File Entry", 0);
12788 /* Include directory index. */
12789 if (idx_form != DW_FORM_udata)
12790 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12791 0, NULL);
12792 else
12793 dw2_asm_output_data_uleb128 (0, NULL);
12795 #ifdef VMS_DEBUGGING_INFO
12796 dw2_asm_output_data_uleb128 (0, NULL);
12797 dw2_asm_output_data_uleb128 (0, NULL);
12798 #endif
12801 /* Now write all the file names. */
12802 for (i = 0; i < numfiles; i++)
12804 int file_idx = backmap[i];
12805 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12807 #ifdef VMS_DEBUGGING_INFO
12808 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12810 /* Setting these fields can lead to debugger miscomparisons,
12811 but VMS Debug requires them to be set correctly. */
12813 int ver;
12814 long long cdt;
12815 long siz;
12816 int maxfilelen = (strlen (files[file_idx].path)
12817 + dirs[dir_idx].length
12818 + MAX_VMS_VERSION_LEN + 1);
12819 char *filebuf = XALLOCAVEC (char, maxfilelen);
12821 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12822 snprintf (filebuf, maxfilelen, "%s;%d",
12823 files[file_idx].path + dirs[dir_idx].length, ver);
12825 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12827 /* Include directory index. */
12828 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12829 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12830 dir_idx + idx_offset, NULL);
12831 else
12832 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12834 /* Modification time. */
12835 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12836 &cdt, 0, 0, 0) == 0)
12837 ? cdt : 0, NULL);
12839 /* File length in bytes. */
12840 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12841 0, &siz, 0, 0) == 0)
12842 ? siz : 0, NULL);
12843 #else
12844 output_line_string (str_form,
12845 files[file_idx].path + dirs[dir_idx].length,
12846 "File Entry", (unsigned) i + 1);
12848 /* Include directory index. */
12849 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12850 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12851 dir_idx + idx_offset, NULL);
12852 else
12853 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12855 if (dwarf_version >= 5)
12856 continue;
12858 /* Modification time. */
12859 dw2_asm_output_data_uleb128 (0, NULL);
12861 /* File length in bytes. */
12862 dw2_asm_output_data_uleb128 (0, NULL);
12863 #endif /* VMS_DEBUGGING_INFO */
12866 if (dwarf_version < 5)
12867 dw2_asm_output_data (1, 0, "End file name table");
12871 /* Output one line number table into the .debug_line section. */
12873 static void
12874 output_one_line_info_table (dw_line_info_table *table)
12876 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12877 unsigned int current_line = 1;
12878 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12879 dw_line_info_entry *ent, *prev_addr;
12880 size_t i;
12881 unsigned int view;
12883 view = 0;
12885 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12887 switch (ent->opcode)
12889 case LI_set_address:
12890 /* ??? Unfortunately, we have little choice here currently, and
12891 must always use the most general form. GCC does not know the
12892 address delta itself, so we can't use DW_LNS_advance_pc. Many
12893 ports do have length attributes which will give an upper bound
12894 on the address range. We could perhaps use length attributes
12895 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12896 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12898 view = 0;
12900 /* This can handle any delta. This takes
12901 4+DWARF2_ADDR_SIZE bytes. */
12902 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12903 debug_variable_location_views
12904 ? ", reset view to 0" : "");
12905 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12906 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12907 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12909 prev_addr = ent;
12910 break;
12912 case LI_adv_address:
12914 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12915 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12916 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12918 view++;
12920 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12921 dw2_asm_output_delta (2, line_label, prev_label,
12922 "from %s to %s", prev_label, line_label);
12924 prev_addr = ent;
12925 break;
12928 case LI_set_line:
12929 if (ent->val == current_line)
12931 /* We still need to start a new row, so output a copy insn. */
12932 dw2_asm_output_data (1, DW_LNS_copy,
12933 "copy line %u", current_line);
12935 else
12937 int line_offset = ent->val - current_line;
12938 int line_delta = line_offset - DWARF_LINE_BASE;
12940 current_line = ent->val;
12941 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12943 /* This can handle deltas from -10 to 234, using the current
12944 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12945 This takes 1 byte. */
12946 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12947 "line %u", current_line);
12949 else
12951 /* This can handle any delta. This takes at least 4 bytes,
12952 depending on the value being encoded. */
12953 dw2_asm_output_data (1, DW_LNS_advance_line,
12954 "advance to line %u", current_line);
12955 dw2_asm_output_data_sleb128 (line_offset, NULL);
12956 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12959 break;
12961 case LI_set_file:
12962 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12963 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12964 break;
12966 case LI_set_column:
12967 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12968 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12969 break;
12971 case LI_negate_stmt:
12972 current_is_stmt = !current_is_stmt;
12973 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12974 "is_stmt %d", current_is_stmt);
12975 break;
12977 case LI_set_prologue_end:
12978 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12979 "set prologue end");
12980 break;
12982 case LI_set_epilogue_begin:
12983 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12984 "set epilogue begin");
12985 break;
12987 case LI_set_discriminator:
12988 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12989 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12990 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12991 dw2_asm_output_data_uleb128 (ent->val, NULL);
12992 break;
12996 /* Emit debug info for the address of the end of the table. */
12997 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12998 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12999 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
13000 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
13002 dw2_asm_output_data (1, 0, "end sequence");
13003 dw2_asm_output_data_uleb128 (1, NULL);
13004 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
13007 static unsigned int output_line_info_generation;
13009 /* Output the source line number correspondence information. This
13010 information goes into the .debug_line section. */
13012 static void
13013 output_line_info (bool prologue_only)
13015 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
13016 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
13017 bool saw_one = false;
13018 int opc;
13020 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
13021 output_line_info_generation);
13022 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
13023 output_line_info_generation);
13024 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
13025 output_line_info_generation);
13026 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
13027 output_line_info_generation++);
13029 if (!XCOFF_DEBUGGING_INFO)
13031 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
13032 dw2_asm_output_data (4, 0xffffffff,
13033 "Initial length escape value indicating 64-bit DWARF extension");
13034 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
13035 "Length of Source Line Info");
13038 ASM_OUTPUT_LABEL (asm_out_file, l1);
13040 output_dwarf_version ();
13041 if (dwarf_version >= 5)
13043 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
13044 dw2_asm_output_data (1, 0, "Segment Size");
13046 dw2_asm_output_delta (dwarf_offset_size, p2, p1, "Prolog Length");
13047 ASM_OUTPUT_LABEL (asm_out_file, p1);
13049 /* Define the architecture-dependent minimum instruction length (in bytes).
13050 In this implementation of DWARF, this field is used for information
13051 purposes only. Since GCC generates assembly language, we have no
13052 a priori knowledge of how many instruction bytes are generated for each
13053 source line, and therefore can use only the DW_LNE_set_address and
13054 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
13055 this as '1', which is "correct enough" for all architectures,
13056 and don't let the target override. */
13057 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
13059 if (dwarf_version >= 4)
13060 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
13061 "Maximum Operations Per Instruction");
13062 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
13063 "Default is_stmt_start flag");
13064 dw2_asm_output_data (1, DWARF_LINE_BASE,
13065 "Line Base Value (Special Opcodes)");
13066 dw2_asm_output_data (1, DWARF_LINE_RANGE,
13067 "Line Range Value (Special Opcodes)");
13068 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
13069 "Special Opcode Base");
13071 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
13073 int n_op_args;
13074 switch (opc)
13076 case DW_LNS_advance_pc:
13077 case DW_LNS_advance_line:
13078 case DW_LNS_set_file:
13079 case DW_LNS_set_column:
13080 case DW_LNS_fixed_advance_pc:
13081 case DW_LNS_set_isa:
13082 n_op_args = 1;
13083 break;
13084 default:
13085 n_op_args = 0;
13086 break;
13089 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
13090 opc, n_op_args);
13093 /* Write out the information about the files we use. */
13094 output_file_names ();
13095 ASM_OUTPUT_LABEL (asm_out_file, p2);
13096 if (prologue_only)
13098 /* Output the marker for the end of the line number info. */
13099 ASM_OUTPUT_LABEL (asm_out_file, l2);
13100 return;
13103 if (separate_line_info)
13105 dw_line_info_table *table;
13106 size_t i;
13108 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
13109 if (table->in_use)
13111 output_one_line_info_table (table);
13112 saw_one = true;
13115 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
13117 output_one_line_info_table (cold_text_section_line_info);
13118 saw_one = true;
13121 /* ??? Some Darwin linkers crash on a .debug_line section with no
13122 sequences. Further, merely a DW_LNE_end_sequence entry is not
13123 sufficient -- the address column must also be initialized.
13124 Make sure to output at least one set_address/end_sequence pair,
13125 choosing .text since that section is always present. */
13126 if (text_section_line_info->in_use || !saw_one)
13127 output_one_line_info_table (text_section_line_info);
13129 /* Output the marker for the end of the line number info. */
13130 ASM_OUTPUT_LABEL (asm_out_file, l2);
13133 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
13135 static inline bool
13136 need_endianity_attribute_p (bool reverse)
13138 return reverse && (dwarf_version >= 3 || !dwarf_strict);
13141 /* Given a pointer to a tree node for some base type, return a pointer to
13142 a DIE that describes the given type. REVERSE is true if the type is
13143 to be interpreted in the reverse storage order wrt the target order.
13145 This routine must only be called for GCC type nodes that correspond to
13146 Dwarf base (fundamental) types. */
13148 dw_die_ref
13149 base_type_die (tree type, bool reverse)
13151 dw_die_ref base_type_result;
13152 enum dwarf_type encoding;
13153 bool fpt_used = false;
13154 struct fixed_point_type_info fpt_info;
13155 tree type_bias = NULL_TREE;
13157 /* If this is a subtype that should not be emitted as a subrange type,
13158 use the base type. See subrange_type_for_debug_p. */
13159 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
13160 type = TREE_TYPE (type);
13162 switch (TREE_CODE (type))
13164 case INTEGER_TYPE:
13165 if ((dwarf_version >= 4 || !dwarf_strict)
13166 && TYPE_NAME (type)
13167 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13168 && DECL_IS_UNDECLARED_BUILTIN (TYPE_NAME (type))
13169 && DECL_NAME (TYPE_NAME (type)))
13171 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
13172 if (strcmp (name, "char16_t") == 0
13173 || strcmp (name, "char32_t") == 0)
13175 encoding = DW_ATE_UTF;
13176 break;
13179 if ((dwarf_version >= 3 || !dwarf_strict)
13180 && lang_hooks.types.get_fixed_point_type_info)
13182 memset (&fpt_info, 0, sizeof (fpt_info));
13183 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
13185 fpt_used = true;
13186 encoding = ((TYPE_UNSIGNED (type))
13187 ? DW_ATE_unsigned_fixed
13188 : DW_ATE_signed_fixed);
13189 break;
13192 if (TYPE_STRING_FLAG (type))
13194 if (TYPE_UNSIGNED (type))
13195 encoding = DW_ATE_unsigned_char;
13196 else
13197 encoding = DW_ATE_signed_char;
13199 else if (TYPE_UNSIGNED (type))
13200 encoding = DW_ATE_unsigned;
13201 else
13202 encoding = DW_ATE_signed;
13204 if (!dwarf_strict
13205 && lang_hooks.types.get_type_bias)
13206 type_bias = lang_hooks.types.get_type_bias (type);
13207 break;
13209 case REAL_TYPE:
13210 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
13212 if (dwarf_version >= 3 || !dwarf_strict)
13213 encoding = DW_ATE_decimal_float;
13214 else
13215 encoding = DW_ATE_lo_user;
13217 else
13218 encoding = DW_ATE_float;
13219 break;
13221 case FIXED_POINT_TYPE:
13222 if (!(dwarf_version >= 3 || !dwarf_strict))
13223 encoding = DW_ATE_lo_user;
13224 else if (TYPE_UNSIGNED (type))
13225 encoding = DW_ATE_unsigned_fixed;
13226 else
13227 encoding = DW_ATE_signed_fixed;
13228 break;
13230 /* Dwarf2 doesn't know anything about complex ints, so use
13231 a user defined type for it. */
13232 case COMPLEX_TYPE:
13233 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
13234 encoding = DW_ATE_complex_float;
13235 else
13236 encoding = DW_ATE_lo_user;
13237 break;
13239 case BOOLEAN_TYPE:
13240 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
13241 encoding = DW_ATE_boolean;
13242 break;
13244 default:
13245 /* No other TREE_CODEs are Dwarf fundamental types. */
13246 gcc_unreachable ();
13249 base_type_result = new_die_raw (DW_TAG_base_type);
13251 add_AT_unsigned (base_type_result, DW_AT_byte_size,
13252 int_size_in_bytes (type));
13253 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
13255 if (need_endianity_attribute_p (reverse))
13256 add_AT_unsigned (base_type_result, DW_AT_endianity,
13257 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
13259 add_alignment_attribute (base_type_result, type);
13261 if (fpt_used)
13263 switch (fpt_info.scale_factor_kind)
13265 case fixed_point_scale_factor_binary:
13266 add_AT_int (base_type_result, DW_AT_binary_scale,
13267 fpt_info.scale_factor.binary);
13268 break;
13270 case fixed_point_scale_factor_decimal:
13271 add_AT_int (base_type_result, DW_AT_decimal_scale,
13272 fpt_info.scale_factor.decimal);
13273 break;
13275 case fixed_point_scale_factor_arbitrary:
13276 /* Arbitrary scale factors cannot be described in standard DWARF. */
13277 if (!dwarf_strict)
13279 /* Describe the scale factor as a rational constant. */
13280 const dw_die_ref scale_factor
13281 = new_die (DW_TAG_constant, comp_unit_die (), type);
13283 add_scalar_info (scale_factor, DW_AT_GNU_numerator,
13284 fpt_info.scale_factor.arbitrary.numerator,
13285 dw_scalar_form_constant, NULL);
13286 add_scalar_info (scale_factor, DW_AT_GNU_denominator,
13287 fpt_info.scale_factor.arbitrary.denominator,
13288 dw_scalar_form_constant, NULL);
13290 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
13292 break;
13294 default:
13295 gcc_unreachable ();
13299 if (type_bias)
13300 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
13301 dw_scalar_form_constant
13302 | dw_scalar_form_exprloc
13303 | dw_scalar_form_reference,
13304 NULL);
13306 return base_type_result;
13309 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
13310 named 'auto' in its type: return true for it, false otherwise. */
13312 static inline bool
13313 is_cxx_auto (tree type)
13315 if (is_cxx ())
13317 tree name = TYPE_IDENTIFIER (type);
13318 if (name == get_identifier ("auto")
13319 || name == get_identifier ("decltype(auto)"))
13320 return true;
13322 return false;
13325 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
13326 given input type is a Dwarf "fundamental" type. Otherwise return null. */
13328 static inline int
13329 is_base_type (tree type)
13331 switch (TREE_CODE (type))
13333 case INTEGER_TYPE:
13334 case REAL_TYPE:
13335 case FIXED_POINT_TYPE:
13336 case COMPLEX_TYPE:
13337 case BOOLEAN_TYPE:
13338 return 1;
13340 case VOID_TYPE:
13341 case OPAQUE_TYPE:
13342 case ARRAY_TYPE:
13343 case RECORD_TYPE:
13344 case UNION_TYPE:
13345 case QUAL_UNION_TYPE:
13346 case ENUMERAL_TYPE:
13347 case FUNCTION_TYPE:
13348 case METHOD_TYPE:
13349 case POINTER_TYPE:
13350 case REFERENCE_TYPE:
13351 case NULLPTR_TYPE:
13352 case OFFSET_TYPE:
13353 case LANG_TYPE:
13354 case VECTOR_TYPE:
13355 return 0;
13357 default:
13358 if (is_cxx_auto (type))
13359 return 0;
13360 gcc_unreachable ();
13364 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13365 node, return the size in bits for the type if it is a constant, or else
13366 return the alignment for the type if the type's size is not constant, or
13367 else return BITS_PER_WORD if the type actually turns out to be an
13368 ERROR_MARK node. */
13370 static inline unsigned HOST_WIDE_INT
13371 simple_type_size_in_bits (const_tree type)
13373 if (TREE_CODE (type) == ERROR_MARK)
13374 return BITS_PER_WORD;
13375 else if (TYPE_SIZE (type) == NULL_TREE)
13376 return 0;
13377 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13378 return tree_to_uhwi (TYPE_SIZE (type));
13379 else
13380 return TYPE_ALIGN (type);
13383 /* Similarly, but return an offset_int instead of UHWI. */
13385 static inline offset_int
13386 offset_int_type_size_in_bits (const_tree type)
13388 if (TREE_CODE (type) == ERROR_MARK)
13389 return BITS_PER_WORD;
13390 else if (TYPE_SIZE (type) == NULL_TREE)
13391 return 0;
13392 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13393 return wi::to_offset (TYPE_SIZE (type));
13394 else
13395 return TYPE_ALIGN (type);
13398 /* Given a pointer to a tree node for a subrange type, return a pointer
13399 to a DIE that describes the given type. */
13401 static dw_die_ref
13402 subrange_type_die (tree type, tree low, tree high, tree bias,
13403 dw_die_ref context_die)
13405 dw_die_ref subrange_die;
13406 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13408 if (context_die == NULL)
13409 context_die = comp_unit_die ();
13411 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13413 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13415 /* The size of the subrange type and its base type do not match,
13416 so we need to generate a size attribute for the subrange type. */
13417 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13420 add_alignment_attribute (subrange_die, type);
13422 if (low)
13423 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13424 if (high)
13425 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13426 if (bias && !dwarf_strict)
13427 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13428 dw_scalar_form_constant
13429 | dw_scalar_form_exprloc
13430 | dw_scalar_form_reference,
13431 NULL);
13433 return subrange_die;
13436 /* Returns the (const and/or volatile) cv_qualifiers associated with
13437 the decl node. This will normally be augmented with the
13438 cv_qualifiers of the underlying type in add_type_attribute. */
13440 static int
13441 decl_quals (const_tree decl)
13443 return ((TREE_READONLY (decl)
13444 /* The C++ front-end correctly marks reference-typed
13445 variables as readonly, but from a language (and debug
13446 info) standpoint they are not const-qualified. */
13447 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13448 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13449 | (TREE_THIS_VOLATILE (decl)
13450 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13453 /* Determine the TYPE whose qualifiers match the largest strict subset
13454 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13455 qualifiers outside QUAL_MASK. */
13457 static int
13458 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13460 tree t;
13461 int best_rank = 0, best_qual = 0, max_rank;
13463 type_quals &= qual_mask;
13464 max_rank = popcount_hwi (type_quals) - 1;
13466 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13467 t = TYPE_NEXT_VARIANT (t))
13469 int q = TYPE_QUALS (t) & qual_mask;
13471 if ((q & type_quals) == q && q != type_quals
13472 && check_base_type (t, type))
13474 int rank = popcount_hwi (q);
13476 if (rank > best_rank)
13478 best_rank = rank;
13479 best_qual = q;
13484 return best_qual;
13487 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13488 static const dwarf_qual_info_t dwarf_qual_info[] =
13490 { TYPE_QUAL_CONST, DW_TAG_const_type },
13491 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13492 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13493 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13495 static const unsigned int dwarf_qual_info_size
13496 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13498 /* If DIE is a qualified DIE of some base DIE with the same parent,
13499 return the base DIE, otherwise return NULL. Set MASK to the
13500 qualifiers added compared to the returned DIE. */
13502 static dw_die_ref
13503 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13505 unsigned int i;
13506 for (i = 0; i < dwarf_qual_info_size; i++)
13507 if (die->die_tag == dwarf_qual_info[i].t)
13508 break;
13509 if (i == dwarf_qual_info_size)
13510 return NULL;
13511 if (vec_safe_length (die->die_attr) != 1)
13512 return NULL;
13513 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13514 if (type == NULL || type->die_parent != die->die_parent)
13515 return NULL;
13516 *mask |= dwarf_qual_info[i].q;
13517 if (depth)
13519 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13520 if (ret)
13521 return ret;
13523 return type;
13526 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13527 entry that chains the modifiers specified by CV_QUALS in front of the
13528 given type. REVERSE is true if the type is to be interpreted in the
13529 reverse storage order wrt the target order. */
13531 static dw_die_ref
13532 modified_type_die (tree type, int cv_quals, bool reverse,
13533 dw_die_ref context_die)
13535 enum tree_code code = TREE_CODE (type);
13536 dw_die_ref mod_type_die;
13537 dw_die_ref sub_die = NULL;
13538 tree item_type = NULL;
13539 tree qualified_type;
13540 tree name, low, high;
13541 dw_die_ref mod_scope;
13542 struct array_descr_info info;
13543 /* Only these cv-qualifiers are currently handled. */
13544 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13545 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13546 ENCODE_QUAL_ADDR_SPACE(~0U));
13547 const bool reverse_base_type
13548 = need_endianity_attribute_p (reverse) && is_base_type (type);
13550 if (code == ERROR_MARK)
13551 return NULL;
13553 if (lang_hooks.types.get_debug_type)
13555 tree debug_type = lang_hooks.types.get_debug_type (type);
13557 if (debug_type != NULL_TREE && debug_type != type)
13558 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13561 cv_quals &= cv_qual_mask;
13563 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13564 tag modifier (and not an attribute) old consumers won't be able
13565 to handle it. */
13566 if (dwarf_version < 3)
13567 cv_quals &= ~TYPE_QUAL_RESTRICT;
13569 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13570 if (dwarf_version < 5)
13571 cv_quals &= ~TYPE_QUAL_ATOMIC;
13573 /* See if we already have the appropriately qualified variant of
13574 this type. */
13575 qualified_type = get_qualified_type (type, cv_quals);
13577 if (qualified_type == sizetype)
13579 /* Try not to expose the internal sizetype type's name. */
13580 if (TYPE_NAME (qualified_type)
13581 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13583 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13585 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13586 && (TYPE_PRECISION (t)
13587 == TYPE_PRECISION (qualified_type))
13588 && (TYPE_UNSIGNED (t)
13589 == TYPE_UNSIGNED (qualified_type)));
13590 qualified_type = t;
13592 else if (qualified_type == sizetype
13593 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13594 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13595 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13596 qualified_type = size_type_node;
13597 if (type == sizetype)
13598 type = qualified_type;
13601 /* If we do, then we can just use its DIE, if it exists. */
13602 if (qualified_type)
13604 mod_type_die = lookup_type_die (qualified_type);
13606 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13607 dealt with specially: the DIE with the attribute, if it exists, is
13608 placed immediately after the regular DIE for the same base type. */
13609 if (mod_type_die
13610 && (!reverse_base_type
13611 || ((mod_type_die = mod_type_die->die_sib) != NULL
13612 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13613 return mod_type_die;
13616 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13618 /* Handle C typedef types. */
13619 if (name
13620 && TREE_CODE (name) == TYPE_DECL
13621 && DECL_ORIGINAL_TYPE (name)
13622 && !DECL_ARTIFICIAL (name))
13624 tree dtype = TREE_TYPE (name);
13626 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13627 if (qualified_type == dtype && !reverse_base_type)
13629 tree origin = decl_ultimate_origin (name);
13631 /* Typedef variants that have an abstract origin don't get their own
13632 type DIE (see gen_typedef_die), so fall back on the ultimate
13633 abstract origin instead. */
13634 if (origin != NULL && origin != name)
13635 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13636 context_die);
13638 /* For a named type, use the typedef. */
13639 gen_type_die (qualified_type, context_die);
13640 return lookup_type_die (qualified_type);
13642 else
13644 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13645 dquals &= cv_qual_mask;
13646 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13647 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13648 /* cv-unqualified version of named type. Just use
13649 the unnamed type to which it refers. */
13650 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13651 reverse, context_die);
13652 /* Else cv-qualified version of named type; fall through. */
13656 mod_scope = scope_die_for (type, context_die);
13658 if (cv_quals)
13660 int sub_quals = 0, first_quals = 0;
13661 unsigned i;
13662 dw_die_ref first = NULL, last = NULL;
13664 /* Determine a lesser qualified type that most closely matches
13665 this one. Then generate DW_TAG_* entries for the remaining
13666 qualifiers. */
13667 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13668 cv_qual_mask);
13669 if (sub_quals && use_debug_types)
13671 bool needed = false;
13672 /* If emitting type units, make sure the order of qualifiers
13673 is canonical. Thus, start from unqualified type if
13674 an earlier qualifier is missing in sub_quals, but some later
13675 one is present there. */
13676 for (i = 0; i < dwarf_qual_info_size; i++)
13677 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13678 needed = true;
13679 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13681 sub_quals = 0;
13682 break;
13685 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13686 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13688 /* As not all intermediate qualified DIEs have corresponding
13689 tree types, ensure that qualified DIEs in the same scope
13690 as their DW_AT_type are emitted after their DW_AT_type,
13691 only with other qualified DIEs for the same type possibly
13692 in between them. Determine the range of such qualified
13693 DIEs now (first being the base type, last being corresponding
13694 last qualified DIE for it). */
13695 unsigned int count = 0;
13696 first = qualified_die_p (mod_type_die, &first_quals,
13697 dwarf_qual_info_size);
13698 if (first == NULL)
13699 first = mod_type_die;
13700 gcc_assert ((first_quals & ~sub_quals) == 0);
13701 for (count = 0, last = first;
13702 count < (1U << dwarf_qual_info_size);
13703 count++, last = last->die_sib)
13705 int quals = 0;
13706 if (last == mod_scope->die_child)
13707 break;
13708 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13709 != first)
13710 break;
13714 for (i = 0; i < dwarf_qual_info_size; i++)
13715 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13717 dw_die_ref d;
13718 if (first && first != last)
13720 for (d = first->die_sib; ; d = d->die_sib)
13722 int quals = 0;
13723 qualified_die_p (d, &quals, dwarf_qual_info_size);
13724 if (quals == (first_quals | dwarf_qual_info[i].q))
13725 break;
13726 if (d == last)
13728 d = NULL;
13729 break;
13732 if (d)
13734 mod_type_die = d;
13735 continue;
13738 if (first)
13740 d = new_die_raw (dwarf_qual_info[i].t);
13741 add_child_die_after (mod_scope, d, last);
13742 last = d;
13744 else
13745 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13746 if (mod_type_die)
13747 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13748 mod_type_die = d;
13749 first_quals |= dwarf_qual_info[i].q;
13752 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13754 dwarf_tag tag = DW_TAG_pointer_type;
13755 if (code == REFERENCE_TYPE)
13757 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13758 tag = DW_TAG_rvalue_reference_type;
13759 else
13760 tag = DW_TAG_reference_type;
13762 mod_type_die = new_die (tag, mod_scope, type);
13764 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13765 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13766 add_alignment_attribute (mod_type_die, type);
13767 item_type = TREE_TYPE (type);
13769 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13770 if (!ADDR_SPACE_GENERIC_P (as))
13772 int action = targetm.addr_space.debug (as);
13773 if (action >= 0)
13775 /* Positive values indicate an address_class. */
13776 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13778 else
13780 /* Negative values indicate an (inverted) segment base reg. */
13781 dw_loc_descr_ref d
13782 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13783 add_AT_loc (mod_type_die, DW_AT_segment, d);
13787 else if (code == ARRAY_TYPE
13788 || (lang_hooks.types.get_array_descr_info
13789 && lang_hooks.types.get_array_descr_info (type, &info)))
13791 gen_type_die (type, context_die);
13792 return lookup_type_die (type);
13794 else if (code == INTEGER_TYPE
13795 && TREE_TYPE (type) != NULL_TREE
13796 && subrange_type_for_debug_p (type, &low, &high))
13798 tree bias = NULL_TREE;
13799 if (lang_hooks.types.get_type_bias)
13800 bias = lang_hooks.types.get_type_bias (type);
13801 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13802 item_type = TREE_TYPE (type);
13804 else if (is_base_type (type))
13806 mod_type_die = base_type_die (type, reverse);
13808 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13809 if (reverse_base_type)
13811 dw_die_ref after_die
13812 = modified_type_die (type, cv_quals, false, context_die);
13813 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13815 else
13816 add_child_die (comp_unit_die (), mod_type_die);
13818 add_pubtype (type, mod_type_die);
13820 else
13822 gen_type_die (type, context_die);
13824 /* We have to get the type_main_variant here (and pass that to the
13825 `lookup_type_die' routine) because the ..._TYPE node we have
13826 might simply be a *copy* of some original type node (where the
13827 copy was created to help us keep track of typedef names) and
13828 that copy might have a different TYPE_UID from the original
13829 ..._TYPE node. */
13830 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13832 /* For function/method types, can't just use type_main_variant here,
13833 because that can have different ref-qualifiers for C++,
13834 but try to canonicalize. */
13835 tree main = TYPE_MAIN_VARIANT (type);
13836 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13837 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13838 && check_base_type (t, main)
13839 && check_lang_type (t, type))
13840 return lookup_type_die (t);
13841 return lookup_type_die (type);
13843 /* Vectors have the debugging information in the type,
13844 not the main variant. */
13845 else if (code == VECTOR_TYPE)
13846 return lookup_type_die (type);
13847 else
13848 return lookup_type_die (type_main_variant (type));
13851 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13852 don't output a DW_TAG_typedef, since there isn't one in the
13853 user's program; just attach a DW_AT_name to the type.
13854 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13855 if the base type already has the same name. */
13856 if (name
13857 && ((TREE_CODE (name) != TYPE_DECL
13858 && (qualified_type == TYPE_MAIN_VARIANT (type)
13859 || (cv_quals == TYPE_UNQUALIFIED)))
13860 || (TREE_CODE (name) == TYPE_DECL
13861 && TREE_TYPE (name) == qualified_type
13862 && DECL_NAME (name))))
13864 if (TREE_CODE (name) == TYPE_DECL)
13865 /* Could just call add_name_and_src_coords_attributes here,
13866 but since this is a builtin type it doesn't have any
13867 useful source coordinates anyway. */
13868 name = DECL_NAME (name);
13869 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13871 /* This probably indicates a bug. */
13872 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13874 name = TYPE_IDENTIFIER (type);
13875 add_name_attribute (mod_type_die,
13876 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13879 if (qualified_type && !reverse_base_type)
13880 equate_type_number_to_die (qualified_type, mod_type_die);
13882 if (item_type)
13883 /* We must do this after the equate_type_number_to_die call, in case
13884 this is a recursive type. This ensures that the modified_type_die
13885 recursion will terminate even if the type is recursive. Recursive
13886 types are possible in Ada. */
13887 sub_die = modified_type_die (item_type,
13888 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13889 reverse,
13890 context_die);
13892 if (sub_die != NULL)
13893 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13895 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13896 if (TYPE_ARTIFICIAL (type))
13897 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13899 return mod_type_die;
13902 /* Generate DIEs for the generic parameters of T.
13903 T must be either a generic type or a generic function.
13904 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13906 static void
13907 gen_generic_params_dies (tree t)
13909 tree parms, args;
13910 int parms_num, i;
13911 dw_die_ref die = NULL;
13912 int non_default;
13914 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13915 return;
13917 if (TYPE_P (t))
13918 die = lookup_type_die (t);
13919 else if (DECL_P (t))
13920 die = lookup_decl_die (t);
13922 gcc_assert (die);
13924 parms = lang_hooks.get_innermost_generic_parms (t);
13925 if (!parms)
13926 /* T has no generic parameter. It means T is neither a generic type
13927 or function. End of story. */
13928 return;
13930 parms_num = TREE_VEC_LENGTH (parms);
13931 args = lang_hooks.get_innermost_generic_args (t);
13932 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13933 non_default = int_cst_value (TREE_CHAIN (args));
13934 else
13935 non_default = TREE_VEC_LENGTH (args);
13936 for (i = 0; i < parms_num; i++)
13938 tree parm, arg, arg_pack_elems;
13939 dw_die_ref parm_die;
13941 parm = TREE_VEC_ELT (parms, i);
13942 arg = TREE_VEC_ELT (args, i);
13943 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13944 gcc_assert (parm && TREE_VALUE (parm) && arg);
13946 if (parm && TREE_VALUE (parm) && arg)
13948 /* If PARM represents a template parameter pack,
13949 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13950 by DW_TAG_template_*_parameter DIEs for the argument
13951 pack elements of ARG. Note that ARG would then be
13952 an argument pack. */
13953 if (arg_pack_elems)
13954 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13955 arg_pack_elems,
13956 die);
13957 else
13958 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13959 true /* emit name */, die);
13960 if (i >= non_default)
13961 add_AT_flag (parm_die, DW_AT_default_value, 1);
13966 /* Create and return a DIE for PARM which should be
13967 the representation of a generic type parameter.
13968 For instance, in the C++ front end, PARM would be a template parameter.
13969 ARG is the argument to PARM.
13970 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13971 name of the PARM.
13972 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13973 as a child node. */
13975 static dw_die_ref
13976 generic_parameter_die (tree parm, tree arg,
13977 bool emit_name_p,
13978 dw_die_ref parent_die)
13980 dw_die_ref tmpl_die = NULL;
13981 const char *name = NULL;
13983 /* C++20 accepts class literals as template parameters, and var
13984 decls with initializers represent them. The VAR_DECLs would be
13985 rejected, but we can take the DECL_INITIAL constructor and
13986 attempt to expand it. */
13987 if (arg && VAR_P (arg))
13988 arg = DECL_INITIAL (arg);
13990 if (!parm || !DECL_NAME (parm) || !arg)
13991 return NULL;
13993 /* We support non-type generic parameters and arguments,
13994 type generic parameters and arguments, as well as
13995 generic generic parameters (a.k.a. template template parameters in C++)
13996 and arguments. */
13997 if (TREE_CODE (parm) == PARM_DECL)
13998 /* PARM is a nontype generic parameter */
13999 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14000 else if (TREE_CODE (parm) == TYPE_DECL)
14001 /* PARM is a type generic parameter. */
14002 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14003 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14004 /* PARM is a generic generic parameter.
14005 Its DIE is a GNU extension. It shall have a
14006 DW_AT_name attribute to represent the name of the template template
14007 parameter, and a DW_AT_GNU_template_name attribute to represent the
14008 name of the template template argument. */
14009 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14010 parent_die, parm);
14011 else
14012 gcc_unreachable ();
14014 if (tmpl_die)
14016 tree tmpl_type;
14018 /* If PARM is a generic parameter pack, it means we are
14019 emitting debug info for a template argument pack element.
14020 In other terms, ARG is a template argument pack element.
14021 In that case, we don't emit any DW_AT_name attribute for
14022 the die. */
14023 if (emit_name_p)
14025 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14026 gcc_assert (name);
14027 add_AT_string (tmpl_die, DW_AT_name, name);
14030 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14032 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14033 TMPL_DIE should have a child DW_AT_type attribute that is set
14034 to the type of the argument to PARM, which is ARG.
14035 If PARM is a type generic parameter, TMPL_DIE should have a
14036 child DW_AT_type that is set to ARG. */
14037 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14038 add_type_attribute (tmpl_die, tmpl_type,
14039 (TREE_THIS_VOLATILE (tmpl_type)
14040 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14041 false, parent_die);
14043 else
14045 /* So TMPL_DIE is a DIE representing a
14046 a generic generic template parameter, a.k.a template template
14047 parameter in C++ and arg is a template. */
14049 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14050 to the name of the argument. */
14051 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14052 if (name)
14053 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14056 if (TREE_CODE (parm) == PARM_DECL)
14057 /* So PARM is a non-type generic parameter.
14058 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14059 attribute of TMPL_DIE which value represents the value
14060 of ARG.
14061 We must be careful here:
14062 The value of ARG might reference some function decls.
14063 We might currently be emitting debug info for a generic
14064 type and types are emitted before function decls, we don't
14065 know if the function decls referenced by ARG will actually be
14066 emitted after cgraph computations.
14067 So must defer the generation of the DW_AT_const_value to
14068 after cgraph is ready. */
14069 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14072 return tmpl_die;
14075 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14076 PARM_PACK must be a template parameter pack. The returned DIE
14077 will be child DIE of PARENT_DIE. */
14079 static dw_die_ref
14080 template_parameter_pack_die (tree parm_pack,
14081 tree parm_pack_args,
14082 dw_die_ref parent_die)
14084 dw_die_ref die;
14085 int j;
14087 gcc_assert (parent_die && parm_pack);
14089 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14090 add_name_and_src_coords_attributes (die, parm_pack);
14091 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14092 generic_parameter_die (parm_pack,
14093 TREE_VEC_ELT (parm_pack_args, j),
14094 false /* Don't emit DW_AT_name */,
14095 die);
14096 return die;
14099 /* Return the DBX register number described by a given RTL node. */
14101 static unsigned int
14102 dbx_reg_number (const_rtx rtl)
14104 unsigned regno = REGNO (rtl);
14106 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14108 #ifdef LEAF_REG_REMAP
14109 if (crtl->uses_only_leaf_regs)
14111 int leaf_reg = LEAF_REG_REMAP (regno);
14112 if (leaf_reg != -1)
14113 regno = (unsigned) leaf_reg;
14115 #endif
14117 regno = DBX_REGISTER_NUMBER (regno);
14118 gcc_assert (regno != INVALID_REGNUM);
14119 return regno;
14122 /* Optionally add a DW_OP_piece term to a location description expression.
14123 DW_OP_piece is only added if the location description expression already
14124 doesn't end with DW_OP_piece. */
14126 static void
14127 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14129 dw_loc_descr_ref loc;
14131 if (*list_head != NULL)
14133 /* Find the end of the chain. */
14134 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14137 if (loc->dw_loc_opc != DW_OP_piece)
14138 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14142 /* Return a location descriptor that designates a machine register or
14143 zero if there is none. */
14145 static dw_loc_descr_ref
14146 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14148 rtx regs;
14150 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14151 return 0;
14153 /* We only use "frame base" when we're sure we're talking about the
14154 post-prologue local stack frame. We do this by *not* running
14155 register elimination until this point, and recognizing the special
14156 argument pointer and soft frame pointer rtx's.
14157 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14158 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14159 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14161 dw_loc_descr_ref result = NULL;
14163 if (dwarf_version >= 4 || !dwarf_strict)
14165 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14166 initialized);
14167 if (result)
14168 add_loc_descr (&result,
14169 new_loc_descr (DW_OP_stack_value, 0, 0));
14171 return result;
14174 regs = targetm.dwarf_register_span (rtl);
14176 if (REG_NREGS (rtl) > 1 || regs)
14177 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14178 else
14180 unsigned int dbx_regnum = dbx_reg_number (rtl);
14181 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14182 return 0;
14183 return one_reg_loc_descriptor (dbx_regnum, initialized);
14187 /* Return a location descriptor that designates a machine register for
14188 a given hard register number. */
14190 static dw_loc_descr_ref
14191 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14193 dw_loc_descr_ref reg_loc_descr;
14195 if (regno <= 31)
14196 reg_loc_descr
14197 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14198 else
14199 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14201 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14202 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14204 return reg_loc_descr;
14207 /* Given an RTL of a register, return a location descriptor that
14208 designates a value that spans more than one register. */
14210 static dw_loc_descr_ref
14211 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14212 enum var_init_status initialized)
14214 int size, i;
14215 dw_loc_descr_ref loc_result = NULL;
14217 /* Simple, contiguous registers. */
14218 if (regs == NULL_RTX)
14220 unsigned reg = REGNO (rtl);
14221 int nregs;
14223 #ifdef LEAF_REG_REMAP
14224 if (crtl->uses_only_leaf_regs)
14226 int leaf_reg = LEAF_REG_REMAP (reg);
14227 if (leaf_reg != -1)
14228 reg = (unsigned) leaf_reg;
14230 #endif
14232 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
14233 nregs = REG_NREGS (rtl);
14235 /* At present we only track constant-sized pieces. */
14236 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14237 return NULL;
14238 size /= nregs;
14240 loc_result = NULL;
14241 while (nregs--)
14243 dw_loc_descr_ref t;
14245 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
14246 VAR_INIT_STATUS_INITIALIZED);
14247 add_loc_descr (&loc_result, t);
14248 add_loc_descr_op_piece (&loc_result, size);
14249 ++reg;
14251 return loc_result;
14254 /* Now onto stupid register sets in non contiguous locations. */
14256 gcc_assert (GET_CODE (regs) == PARALLEL);
14258 /* At present we only track constant-sized pieces. */
14259 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14260 return NULL;
14261 loc_result = NULL;
14263 for (i = 0; i < XVECLEN (regs, 0); ++i)
14265 dw_loc_descr_ref t;
14267 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
14268 VAR_INIT_STATUS_INITIALIZED);
14269 add_loc_descr (&loc_result, t);
14270 add_loc_descr_op_piece (&loc_result, size);
14273 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14274 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14275 return loc_result;
14278 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14280 /* Return a location descriptor that designates a constant i,
14281 as a compound operation from constant (i >> shift), constant shift
14282 and DW_OP_shl. */
14284 static dw_loc_descr_ref
14285 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14287 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14288 add_loc_descr (&ret, int_loc_descriptor (shift));
14289 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14290 return ret;
14293 /* Return a location descriptor that designates constant POLY_I. */
14295 static dw_loc_descr_ref
14296 int_loc_descriptor (poly_int64 poly_i)
14298 enum dwarf_location_atom op;
14300 HOST_WIDE_INT i;
14301 if (!poly_i.is_constant (&i))
14303 /* Create location descriptions for the non-constant part and
14304 add any constant offset at the end. */
14305 dw_loc_descr_ref ret = NULL;
14306 HOST_WIDE_INT constant = poly_i.coeffs[0];
14307 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14309 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14310 if (coeff != 0)
14312 dw_loc_descr_ref start = ret;
14313 unsigned int factor;
14314 int bias;
14315 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14316 (j, &factor, &bias);
14318 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14319 add COEFF * (REGNO / FACTOR) now and subtract
14320 COEFF * BIAS from the final constant part. */
14321 constant -= coeff * bias;
14322 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14323 if (coeff % factor == 0)
14324 coeff /= factor;
14325 else
14327 int amount = exact_log2 (factor);
14328 gcc_assert (amount >= 0);
14329 add_loc_descr (&ret, int_loc_descriptor (amount));
14330 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14332 if (coeff != 1)
14334 add_loc_descr (&ret, int_loc_descriptor (coeff));
14335 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14337 if (start)
14338 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14341 loc_descr_plus_const (&ret, constant);
14342 return ret;
14345 /* Pick the smallest representation of a constant, rather than just
14346 defaulting to the LEB encoding. */
14347 if (i >= 0)
14349 int clz = clz_hwi (i);
14350 int ctz = ctz_hwi (i);
14351 if (i <= 31)
14352 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14353 else if (i <= 0xff)
14354 op = DW_OP_const1u;
14355 else if (i <= 0xffff)
14356 op = DW_OP_const2u;
14357 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14358 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14359 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14360 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14361 while DW_OP_const4u is 5 bytes. */
14362 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14363 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14364 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14365 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14366 while DW_OP_const4u is 5 bytes. */
14367 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14369 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14370 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14371 <= 4)
14373 /* As i >= 2**31, the double cast above will yield a negative number.
14374 Since wrapping is defined in DWARF expressions we can output big
14375 positive integers as small negative ones, regardless of the size
14376 of host wide ints.
14378 Here, since the evaluator will handle 32-bit values and since i >=
14379 2**31, we know it's going to be interpreted as a negative literal:
14380 store it this way if we can do better than 5 bytes this way. */
14381 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14383 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14384 op = DW_OP_const4u;
14386 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14387 least 6 bytes: see if we can do better before falling back to it. */
14388 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14389 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14390 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14391 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14392 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14393 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14394 >= HOST_BITS_PER_WIDE_INT)
14395 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14396 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14397 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14398 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14399 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14400 && size_of_uleb128 (i) > 6)
14401 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14402 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14403 else
14404 op = DW_OP_constu;
14406 else
14408 if (i >= -0x80)
14409 op = DW_OP_const1s;
14410 else if (i >= -0x8000)
14411 op = DW_OP_const2s;
14412 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14414 if (size_of_int_loc_descriptor (i) < 5)
14416 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14417 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14418 return ret;
14420 op = DW_OP_const4s;
14422 else
14424 if (size_of_int_loc_descriptor (i)
14425 < (unsigned long) 1 + size_of_sleb128 (i))
14427 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14428 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14429 return ret;
14431 op = DW_OP_consts;
14435 return new_loc_descr (op, i, 0);
14438 /* Likewise, for unsigned constants. */
14440 static dw_loc_descr_ref
14441 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14443 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14444 const unsigned HOST_WIDE_INT max_uint
14445 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14447 /* If possible, use the clever signed constants handling. */
14448 if (i <= max_int)
14449 return int_loc_descriptor ((HOST_WIDE_INT) i);
14451 /* Here, we are left with positive numbers that cannot be represented as
14452 HOST_WIDE_INT, i.e.:
14453 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14455 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14456 whereas may be better to output a negative integer: thanks to integer
14457 wrapping, we know that:
14458 x = x - 2 ** DWARF2_ADDR_SIZE
14459 = x - 2 * (max (HOST_WIDE_INT) + 1)
14460 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14461 small negative integers. Let's try that in cases it will clearly improve
14462 the encoding: there is no gain turning DW_OP_const4u into
14463 DW_OP_const4s. */
14464 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14465 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14466 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14468 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14470 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14471 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14472 const HOST_WIDE_INT second_shift
14473 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14475 /* So we finally have:
14476 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14477 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14478 return int_loc_descriptor (second_shift);
14481 /* Last chance: fallback to a simple constant operation. */
14482 return new_loc_descr
14483 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14484 ? DW_OP_const4u
14485 : DW_OP_const8u,
14486 i, 0);
14489 /* Generate and return a location description that computes the unsigned
14490 comparison of the two stack top entries (a OP b where b is the top-most
14491 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14492 LE_EXPR, GT_EXPR or GE_EXPR. */
14494 static dw_loc_descr_ref
14495 uint_comparison_loc_list (enum tree_code kind)
14497 enum dwarf_location_atom op, flip_op;
14498 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14500 switch (kind)
14502 case LT_EXPR:
14503 op = DW_OP_lt;
14504 break;
14505 case LE_EXPR:
14506 op = DW_OP_le;
14507 break;
14508 case GT_EXPR:
14509 op = DW_OP_gt;
14510 break;
14511 case GE_EXPR:
14512 op = DW_OP_ge;
14513 break;
14514 default:
14515 gcc_unreachable ();
14518 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14519 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14521 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14522 possible to perform unsigned comparisons: we just have to distinguish
14523 three cases:
14525 1. when a and b have the same sign (as signed integers); then we should
14526 return: a OP(signed) b;
14528 2. when a is a negative signed integer while b is a positive one, then a
14529 is a greater unsigned integer than b; likewise when a and b's roles
14530 are flipped.
14532 So first, compare the sign of the two operands. */
14533 ret = new_loc_descr (DW_OP_over, 0, 0);
14534 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14535 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14536 /* If they have different signs (i.e. they have different sign bits), then
14537 the stack top value has now the sign bit set and thus it's smaller than
14538 zero. */
14539 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14540 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14541 add_loc_descr (&ret, bra_node);
14543 /* We are in case 1. At this point, we know both operands have the same
14544 sign, to it's safe to use the built-in signed comparison. */
14545 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14546 add_loc_descr (&ret, jmp_node);
14548 /* We are in case 2. Here, we know both operands do not have the same sign,
14549 so we have to flip the signed comparison. */
14550 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14551 tmp = new_loc_descr (flip_op, 0, 0);
14552 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14553 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14554 add_loc_descr (&ret, tmp);
14556 /* This dummy operation is necessary to make the two branches join. */
14557 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14558 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14559 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14560 add_loc_descr (&ret, tmp);
14562 return ret;
14565 /* Likewise, but takes the location description lists (might be destructive on
14566 them). Return NULL if either is NULL or if concatenation fails. */
14568 static dw_loc_list_ref
14569 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14570 enum tree_code kind)
14572 if (left == NULL || right == NULL)
14573 return NULL;
14575 add_loc_list (&left, right);
14576 if (left == NULL)
14577 return NULL;
14579 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14580 return left;
14583 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14584 without actually allocating it. */
14586 static unsigned long
14587 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14589 return size_of_int_loc_descriptor (i >> shift)
14590 + size_of_int_loc_descriptor (shift)
14591 + 1;
14594 /* Return size_of_locs (int_loc_descriptor (i)) without
14595 actually allocating it. */
14597 static unsigned long
14598 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14600 unsigned long s;
14602 if (i >= 0)
14604 int clz, ctz;
14605 if (i <= 31)
14606 return 1;
14607 else if (i <= 0xff)
14608 return 2;
14609 else if (i <= 0xffff)
14610 return 3;
14611 clz = clz_hwi (i);
14612 ctz = ctz_hwi (i);
14613 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14614 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14615 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14616 - clz - 5);
14617 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14618 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14619 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14620 - clz - 8);
14621 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14622 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14623 <= 4)
14624 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14625 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14626 return 5;
14627 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14628 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14629 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14630 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14631 - clz - 8);
14632 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14633 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14634 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14635 - clz - 16);
14636 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14637 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14638 && s > 6)
14639 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14640 - clz - 32);
14641 else
14642 return 1 + s;
14644 else
14646 if (i >= -0x80)
14647 return 2;
14648 else if (i >= -0x8000)
14649 return 3;
14650 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14652 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14654 s = size_of_int_loc_descriptor (-i) + 1;
14655 if (s < 5)
14656 return s;
14658 return 5;
14660 else
14662 unsigned long r = 1 + size_of_sleb128 (i);
14663 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14665 s = size_of_int_loc_descriptor (-i) + 1;
14666 if (s < r)
14667 return s;
14669 return r;
14674 /* Return loc description representing "address" of integer value.
14675 This can appear only as toplevel expression. */
14677 static dw_loc_descr_ref
14678 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14680 int litsize;
14681 dw_loc_descr_ref loc_result = NULL;
14683 if (!(dwarf_version >= 4 || !dwarf_strict))
14684 return NULL;
14686 litsize = size_of_int_loc_descriptor (i);
14687 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14688 is more compact. For DW_OP_stack_value we need:
14689 litsize + 1 (DW_OP_stack_value)
14690 and for DW_OP_implicit_value:
14691 1 (DW_OP_implicit_value) + 1 (length) + size. */
14692 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14694 loc_result = int_loc_descriptor (i);
14695 add_loc_descr (&loc_result,
14696 new_loc_descr (DW_OP_stack_value, 0, 0));
14697 return loc_result;
14700 loc_result = new_loc_descr (DW_OP_implicit_value,
14701 size, 0);
14702 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14703 loc_result->dw_loc_oprnd2.v.val_int = i;
14704 return loc_result;
14707 /* Return a location descriptor that designates a base+offset location. */
14709 static dw_loc_descr_ref
14710 based_loc_descr (rtx reg, poly_int64 offset,
14711 enum var_init_status initialized)
14713 unsigned int regno;
14714 dw_loc_descr_ref result;
14715 dw_fde_ref fde = cfun->fde;
14717 /* We only use "frame base" when we're sure we're talking about the
14718 post-prologue local stack frame. We do this by *not* running
14719 register elimination until this point, and recognizing the special
14720 argument pointer and soft frame pointer rtx's. */
14721 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14723 rtx elim = (ira_use_lra_p
14724 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14725 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14727 if (elim != reg)
14729 /* Allow hard frame pointer here even if frame pointer
14730 isn't used since hard frame pointer is encoded with
14731 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14732 not hard frame pointer directly. */
14733 elim = strip_offset_and_add (elim, &offset);
14734 gcc_assert (elim == hard_frame_pointer_rtx
14735 || elim == stack_pointer_rtx);
14737 /* If drap register is used to align stack, use frame
14738 pointer + offset to access stack variables. If stack
14739 is aligned without drap, use stack pointer + offset to
14740 access stack variables. */
14741 if (crtl->stack_realign_tried
14742 && reg == frame_pointer_rtx)
14744 int base_reg
14745 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14746 ? HARD_FRAME_POINTER_REGNUM
14747 : REGNO (elim));
14748 return new_reg_loc_descr (base_reg, offset);
14751 gcc_assert (frame_pointer_fb_offset_valid);
14752 offset += frame_pointer_fb_offset;
14753 HOST_WIDE_INT const_offset;
14754 if (offset.is_constant (&const_offset))
14755 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14756 else
14758 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14759 loc_descr_plus_const (&ret, offset);
14760 return ret;
14765 regno = REGNO (reg);
14766 #ifdef LEAF_REG_REMAP
14767 if (crtl->uses_only_leaf_regs)
14769 int leaf_reg = LEAF_REG_REMAP (regno);
14770 if (leaf_reg != -1)
14771 regno = (unsigned) leaf_reg;
14773 #endif
14774 regno = DWARF_FRAME_REGNUM (regno);
14776 HOST_WIDE_INT const_offset;
14777 if (!optimize && fde
14778 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14779 && offset.is_constant (&const_offset))
14781 /* Use cfa+offset to represent the location of arguments passed
14782 on the stack when drap is used to align stack.
14783 Only do this when not optimizing, for optimized code var-tracking
14784 is supposed to track where the arguments live and the register
14785 used as vdrap or drap in some spot might be used for something
14786 else in other part of the routine. */
14787 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14790 result = new_reg_loc_descr (regno, offset);
14792 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14793 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14795 return result;
14798 /* Return true if this RTL expression describes a base+offset calculation. */
14800 static inline int
14801 is_based_loc (const_rtx rtl)
14803 return (GET_CODE (rtl) == PLUS
14804 && ((REG_P (XEXP (rtl, 0))
14805 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14806 && CONST_INT_P (XEXP (rtl, 1)))));
14809 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14810 failed. */
14812 static dw_loc_descr_ref
14813 tls_mem_loc_descriptor (rtx mem)
14815 tree base;
14816 dw_loc_descr_ref loc_result;
14818 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14819 return NULL;
14821 base = get_base_address (MEM_EXPR (mem));
14822 if (base == NULL
14823 || !VAR_P (base)
14824 || !DECL_THREAD_LOCAL_P (base))
14825 return NULL;
14827 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14828 if (loc_result == NULL)
14829 return NULL;
14831 if (maybe_ne (MEM_OFFSET (mem), 0))
14832 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14834 return loc_result;
14837 /* Output debug info about reason why we failed to expand expression as dwarf
14838 expression. */
14840 static void
14841 expansion_failed (tree expr, rtx rtl, char const *reason)
14843 if (dump_file && (dump_flags & TDF_DETAILS))
14845 fprintf (dump_file, "Failed to expand as dwarf: ");
14846 if (expr)
14847 print_generic_expr (dump_file, expr, dump_flags);
14848 if (rtl)
14850 fprintf (dump_file, "\n");
14851 print_rtl (dump_file, rtl);
14853 fprintf (dump_file, "\nReason: %s\n", reason);
14857 /* Helper function for const_ok_for_output. */
14859 static bool
14860 const_ok_for_output_1 (rtx rtl)
14862 if (targetm.const_not_ok_for_debug_p (rtl))
14864 if (GET_CODE (rtl) != UNSPEC)
14866 expansion_failed (NULL_TREE, rtl,
14867 "Expression rejected for debug by the backend.\n");
14868 return false;
14871 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14872 the target hook doesn't explicitly allow it in debug info, assume
14873 we can't express it in the debug info. */
14874 /* Don't complain about TLS UNSPECs, those are just too hard to
14875 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14876 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14877 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14878 if (flag_checking
14879 && (XVECLEN (rtl, 0) == 0
14880 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14881 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14882 inform (current_function_decl
14883 ? DECL_SOURCE_LOCATION (current_function_decl)
14884 : UNKNOWN_LOCATION,
14885 #if NUM_UNSPEC_VALUES > 0
14886 "non-delegitimized UNSPEC %s (%d) found in variable location",
14887 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14888 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14889 #else
14890 "non-delegitimized UNSPEC %d found in variable location",
14891 #endif
14892 XINT (rtl, 1));
14893 expansion_failed (NULL_TREE, rtl,
14894 "UNSPEC hasn't been delegitimized.\n");
14895 return false;
14898 if (CONST_POLY_INT_P (rtl))
14899 return false;
14901 /* FIXME: Refer to PR60655. It is possible for simplification
14902 of rtl expressions in var tracking to produce such expressions.
14903 We should really identify / validate expressions
14904 enclosed in CONST that can be handled by assemblers on various
14905 targets and only handle legitimate cases here. */
14906 switch (GET_CODE (rtl))
14908 case SYMBOL_REF:
14909 break;
14910 case NOT:
14911 case NEG:
14912 return false;
14913 case PLUS:
14915 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14916 operands. */
14917 subrtx_var_iterator::array_type array;
14918 bool first = false;
14919 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14920 if (SYMBOL_REF_P (*iter)
14921 || LABEL_P (*iter)
14922 || GET_CODE (*iter) == UNSPEC)
14924 first = true;
14925 break;
14927 if (!first)
14928 return true;
14929 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14930 if (SYMBOL_REF_P (*iter)
14931 || LABEL_P (*iter)
14932 || GET_CODE (*iter) == UNSPEC)
14933 return false;
14934 return true;
14936 case MINUS:
14938 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14939 appear in the second operand of MINUS. */
14940 subrtx_var_iterator::array_type array;
14941 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14942 if (SYMBOL_REF_P (*iter)
14943 || LABEL_P (*iter)
14944 || GET_CODE (*iter) == UNSPEC)
14945 return false;
14946 return true;
14948 default:
14949 return true;
14952 if (CONSTANT_POOL_ADDRESS_P (rtl))
14954 bool marked;
14955 get_pool_constant_mark (rtl, &marked);
14956 /* If all references to this pool constant were optimized away,
14957 it was not output and thus we can't represent it. */
14958 if (!marked)
14960 expansion_failed (NULL_TREE, rtl,
14961 "Constant was removed from constant pool.\n");
14962 return false;
14966 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14967 return false;
14969 /* Avoid references to external symbols in debug info, on several targets
14970 the linker might even refuse to link when linking a shared library,
14971 and in many other cases the relocations for .debug_info/.debug_loc are
14972 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14973 to be defined within the same shared library or executable are fine. */
14974 if (SYMBOL_REF_EXTERNAL_P (rtl))
14976 tree decl = SYMBOL_REF_DECL (rtl);
14978 if (decl == NULL || !targetm.binds_local_p (decl))
14980 expansion_failed (NULL_TREE, rtl,
14981 "Symbol not defined in current TU.\n");
14982 return false;
14986 return true;
14989 /* Return true if constant RTL can be emitted in DW_OP_addr or
14990 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14991 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14993 static bool
14994 const_ok_for_output (rtx rtl)
14996 if (GET_CODE (rtl) == SYMBOL_REF)
14997 return const_ok_for_output_1 (rtl);
14999 if (GET_CODE (rtl) == CONST)
15001 subrtx_var_iterator::array_type array;
15002 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15003 if (!const_ok_for_output_1 (*iter))
15004 return false;
15005 return true;
15008 return true;
15011 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15012 if possible, NULL otherwise. */
15014 static dw_die_ref
15015 base_type_for_mode (machine_mode mode, bool unsignedp)
15017 dw_die_ref type_die;
15018 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15020 if (type == NULL)
15021 return NULL;
15022 switch (TREE_CODE (type))
15024 case INTEGER_TYPE:
15025 case REAL_TYPE:
15026 break;
15027 default:
15028 return NULL;
15030 type_die = lookup_type_die (type);
15031 if (!type_die)
15032 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15033 comp_unit_die ());
15034 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15035 return NULL;
15036 return type_die;
15039 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15040 type matching MODE, or, if MODE is narrower than or as wide as
15041 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15042 possible. */
15044 static dw_loc_descr_ref
15045 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15047 machine_mode outer_mode = mode;
15048 dw_die_ref type_die;
15049 dw_loc_descr_ref cvt;
15051 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15053 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15054 return op;
15056 type_die = base_type_for_mode (outer_mode, 1);
15057 if (type_die == NULL)
15058 return NULL;
15059 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15060 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15061 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15062 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15063 add_loc_descr (&op, cvt);
15064 return op;
15067 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15069 static dw_loc_descr_ref
15070 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15071 dw_loc_descr_ref op1)
15073 dw_loc_descr_ref ret = op0;
15074 add_loc_descr (&ret, op1);
15075 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15076 if (STORE_FLAG_VALUE != 1)
15078 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15079 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15081 return ret;
15084 /* Subroutine of scompare_loc_descriptor for the case in which we're
15085 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15086 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15088 static dw_loc_descr_ref
15089 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15090 scalar_int_mode op_mode,
15091 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15093 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15094 dw_loc_descr_ref cvt;
15096 if (type_die == NULL)
15097 return NULL;
15098 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15099 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15100 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15101 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15102 add_loc_descr (&op0, cvt);
15103 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15104 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15105 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15106 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15107 add_loc_descr (&op1, cvt);
15108 return compare_loc_descriptor (op, op0, op1);
15111 /* Subroutine of scompare_loc_descriptor for the case in which we're
15112 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15113 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15115 static dw_loc_descr_ref
15116 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15117 scalar_int_mode op_mode,
15118 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15120 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15121 /* For eq/ne, if the operands are known to be zero-extended,
15122 there is no need to do the fancy shifting up. */
15123 if (op == DW_OP_eq || op == DW_OP_ne)
15125 dw_loc_descr_ref last0, last1;
15126 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15128 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15130 /* deref_size zero extends, and for constants we can check
15131 whether they are zero extended or not. */
15132 if (((last0->dw_loc_opc == DW_OP_deref_size
15133 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15134 || (CONST_INT_P (XEXP (rtl, 0))
15135 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15136 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15137 && ((last1->dw_loc_opc == DW_OP_deref_size
15138 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15139 || (CONST_INT_P (XEXP (rtl, 1))
15140 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15141 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15142 return compare_loc_descriptor (op, op0, op1);
15144 /* EQ/NE comparison against constant in narrower type than
15145 DWARF2_ADDR_SIZE can be performed either as
15146 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15147 DW_OP_{eq,ne}
15149 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15150 DW_OP_{eq,ne}. Pick whatever is shorter. */
15151 if (CONST_INT_P (XEXP (rtl, 1))
15152 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15153 && (size_of_int_loc_descriptor (shift) + 1
15154 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15155 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15156 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15157 & GET_MODE_MASK (op_mode))))
15159 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15160 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15161 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15162 & GET_MODE_MASK (op_mode));
15163 return compare_loc_descriptor (op, op0, op1);
15166 add_loc_descr (&op0, int_loc_descriptor (shift));
15167 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15168 if (CONST_INT_P (XEXP (rtl, 1)))
15169 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15170 else
15172 add_loc_descr (&op1, int_loc_descriptor (shift));
15173 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15175 return compare_loc_descriptor (op, op0, op1);
15178 /* Return location descriptor for signed comparison OP RTL. */
15180 static dw_loc_descr_ref
15181 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15182 machine_mode mem_mode)
15184 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15185 dw_loc_descr_ref op0, op1;
15187 if (op_mode == VOIDmode)
15188 op_mode = GET_MODE (XEXP (rtl, 1));
15189 if (op_mode == VOIDmode)
15190 return NULL;
15192 scalar_int_mode int_op_mode;
15193 if (dwarf_strict
15194 && dwarf_version < 5
15195 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15196 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15197 return NULL;
15199 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15200 VAR_INIT_STATUS_INITIALIZED);
15201 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15202 VAR_INIT_STATUS_INITIALIZED);
15204 if (op0 == NULL || op1 == NULL)
15205 return NULL;
15207 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15209 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15210 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15212 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15213 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15215 return compare_loc_descriptor (op, op0, op1);
15218 /* Return location descriptor for unsigned comparison OP RTL. */
15220 static dw_loc_descr_ref
15221 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15222 machine_mode mem_mode)
15224 dw_loc_descr_ref op0, op1;
15226 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15227 if (test_op_mode == VOIDmode)
15228 test_op_mode = GET_MODE (XEXP (rtl, 1));
15230 scalar_int_mode op_mode;
15231 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15232 return NULL;
15234 if (dwarf_strict
15235 && dwarf_version < 5
15236 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15237 return NULL;
15239 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15240 VAR_INIT_STATUS_INITIALIZED);
15241 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15242 VAR_INIT_STATUS_INITIALIZED);
15244 if (op0 == NULL || op1 == NULL)
15245 return NULL;
15247 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15249 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15250 dw_loc_descr_ref last0, last1;
15251 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15253 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15255 if (CONST_INT_P (XEXP (rtl, 0)))
15256 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15257 /* deref_size zero extends, so no need to mask it again. */
15258 else if (last0->dw_loc_opc != DW_OP_deref_size
15259 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15261 add_loc_descr (&op0, int_loc_descriptor (mask));
15262 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15264 if (CONST_INT_P (XEXP (rtl, 1)))
15265 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15266 /* deref_size zero extends, so no need to mask it again. */
15267 else if (last1->dw_loc_opc != DW_OP_deref_size
15268 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15270 add_loc_descr (&op1, int_loc_descriptor (mask));
15271 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15274 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15276 HOST_WIDE_INT bias = 1;
15277 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15278 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15279 if (CONST_INT_P (XEXP (rtl, 1)))
15280 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15281 + INTVAL (XEXP (rtl, 1)));
15282 else
15283 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15284 bias, 0));
15286 return compare_loc_descriptor (op, op0, op1);
15289 /* Return location descriptor for {U,S}{MIN,MAX}. */
15291 static dw_loc_descr_ref
15292 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15293 machine_mode mem_mode)
15295 enum dwarf_location_atom op;
15296 dw_loc_descr_ref op0, op1, ret;
15297 dw_loc_descr_ref bra_node, drop_node;
15299 scalar_int_mode int_mode;
15300 if (dwarf_strict
15301 && dwarf_version < 5
15302 && (!is_a <scalar_int_mode> (mode, &int_mode)
15303 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15304 return NULL;
15306 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15307 VAR_INIT_STATUS_INITIALIZED);
15308 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15309 VAR_INIT_STATUS_INITIALIZED);
15311 if (op0 == NULL || op1 == NULL)
15312 return NULL;
15314 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15315 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15316 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15317 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15319 /* Checked by the caller. */
15320 int_mode = as_a <scalar_int_mode> (mode);
15321 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15323 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15324 add_loc_descr (&op0, int_loc_descriptor (mask));
15325 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15326 add_loc_descr (&op1, int_loc_descriptor (mask));
15327 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15329 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15331 HOST_WIDE_INT bias = 1;
15332 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15333 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15334 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15337 else if (is_a <scalar_int_mode> (mode, &int_mode)
15338 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15340 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15341 add_loc_descr (&op0, int_loc_descriptor (shift));
15342 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15343 add_loc_descr (&op1, int_loc_descriptor (shift));
15344 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15346 else if (is_a <scalar_int_mode> (mode, &int_mode)
15347 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15349 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15350 dw_loc_descr_ref cvt;
15351 if (type_die == NULL)
15352 return NULL;
15353 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15354 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15355 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15356 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15357 add_loc_descr (&op0, cvt);
15358 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15359 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15360 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15361 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15362 add_loc_descr (&op1, cvt);
15365 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15366 op = DW_OP_lt;
15367 else
15368 op = DW_OP_gt;
15369 ret = op0;
15370 add_loc_descr (&ret, op1);
15371 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15372 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15373 add_loc_descr (&ret, bra_node);
15374 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15375 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15376 add_loc_descr (&ret, drop_node);
15377 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15378 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15379 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15380 && is_a <scalar_int_mode> (mode, &int_mode)
15381 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15382 ret = convert_descriptor_to_mode (int_mode, ret);
15383 return ret;
15386 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15387 but after converting arguments to type_die, afterwards
15388 convert back to unsigned. */
15390 static dw_loc_descr_ref
15391 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15392 scalar_int_mode mode, machine_mode mem_mode)
15394 dw_loc_descr_ref cvt, op0, op1;
15396 if (type_die == NULL)
15397 return NULL;
15398 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15399 VAR_INIT_STATUS_INITIALIZED);
15400 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15401 VAR_INIT_STATUS_INITIALIZED);
15402 if (op0 == NULL || op1 == NULL)
15403 return NULL;
15404 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15405 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15406 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15407 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15408 add_loc_descr (&op0, cvt);
15409 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15410 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15411 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15412 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15413 add_loc_descr (&op1, cvt);
15414 add_loc_descr (&op0, op1);
15415 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15416 return convert_descriptor_to_mode (mode, op0);
15419 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15420 const0 is DW_OP_lit0 or corresponding typed constant,
15421 const1 is DW_OP_lit1 or corresponding typed constant
15422 and constMSB is constant with just the MSB bit set
15423 for the mode):
15424 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15425 L1: const0 DW_OP_swap
15426 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15427 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15428 L3: DW_OP_drop
15429 L4: DW_OP_nop
15431 CTZ is similar:
15432 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15433 L1: const0 DW_OP_swap
15434 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15435 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15436 L3: DW_OP_drop
15437 L4: DW_OP_nop
15439 FFS is similar:
15440 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15441 L1: const1 DW_OP_swap
15442 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15443 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15444 L3: DW_OP_drop
15445 L4: DW_OP_nop */
15447 static dw_loc_descr_ref
15448 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15449 machine_mode mem_mode)
15451 dw_loc_descr_ref op0, ret, tmp;
15452 HOST_WIDE_INT valv;
15453 dw_loc_descr_ref l1jump, l1label;
15454 dw_loc_descr_ref l2jump, l2label;
15455 dw_loc_descr_ref l3jump, l3label;
15456 dw_loc_descr_ref l4jump, l4label;
15457 rtx msb;
15459 if (GET_MODE (XEXP (rtl, 0)) != mode)
15460 return NULL;
15462 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15463 VAR_INIT_STATUS_INITIALIZED);
15464 if (op0 == NULL)
15465 return NULL;
15466 ret = op0;
15467 if (GET_CODE (rtl) == CLZ)
15469 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15470 valv = GET_MODE_BITSIZE (mode);
15472 else if (GET_CODE (rtl) == FFS)
15473 valv = 0;
15474 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15475 valv = GET_MODE_BITSIZE (mode);
15476 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15477 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15478 add_loc_descr (&ret, l1jump);
15479 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15480 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15481 VAR_INIT_STATUS_INITIALIZED);
15482 if (tmp == NULL)
15483 return NULL;
15484 add_loc_descr (&ret, tmp);
15485 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15486 add_loc_descr (&ret, l4jump);
15487 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15488 ? const1_rtx : const0_rtx,
15489 mode, mem_mode,
15490 VAR_INIT_STATUS_INITIALIZED);
15491 if (l1label == NULL)
15492 return NULL;
15493 add_loc_descr (&ret, l1label);
15494 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15495 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15496 add_loc_descr (&ret, l2label);
15497 if (GET_CODE (rtl) != CLZ)
15498 msb = const1_rtx;
15499 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15500 msb = GEN_INT (HOST_WIDE_INT_1U
15501 << (GET_MODE_BITSIZE (mode) - 1));
15502 else
15503 msb = immed_wide_int_const
15504 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15505 GET_MODE_PRECISION (mode)), mode);
15506 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15507 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15508 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15509 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15510 else
15511 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15512 VAR_INIT_STATUS_INITIALIZED);
15513 if (tmp == NULL)
15514 return NULL;
15515 add_loc_descr (&ret, tmp);
15516 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15517 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15518 add_loc_descr (&ret, l3jump);
15519 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15520 VAR_INIT_STATUS_INITIALIZED);
15521 if (tmp == NULL)
15522 return NULL;
15523 add_loc_descr (&ret, tmp);
15524 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15525 ? DW_OP_shl : DW_OP_shr, 0, 0));
15526 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15527 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15528 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15529 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15530 add_loc_descr (&ret, l2jump);
15531 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15532 add_loc_descr (&ret, l3label);
15533 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15534 add_loc_descr (&ret, l4label);
15535 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15536 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15537 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15538 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15539 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15540 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15541 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15542 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15543 return ret;
15546 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15547 const1 is DW_OP_lit1 or corresponding typed constant):
15548 const0 DW_OP_swap
15549 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15550 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15551 L2: DW_OP_drop
15553 PARITY is similar:
15554 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15555 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15556 L2: DW_OP_drop */
15558 static dw_loc_descr_ref
15559 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15560 machine_mode mem_mode)
15562 dw_loc_descr_ref op0, ret, tmp;
15563 dw_loc_descr_ref l1jump, l1label;
15564 dw_loc_descr_ref l2jump, l2label;
15566 if (GET_MODE (XEXP (rtl, 0)) != mode)
15567 return NULL;
15569 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15570 VAR_INIT_STATUS_INITIALIZED);
15571 if (op0 == NULL)
15572 return NULL;
15573 ret = op0;
15574 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15575 VAR_INIT_STATUS_INITIALIZED);
15576 if (tmp == NULL)
15577 return NULL;
15578 add_loc_descr (&ret, tmp);
15579 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15580 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15581 add_loc_descr (&ret, l1label);
15582 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15583 add_loc_descr (&ret, l2jump);
15584 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15585 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15586 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15587 VAR_INIT_STATUS_INITIALIZED);
15588 if (tmp == NULL)
15589 return NULL;
15590 add_loc_descr (&ret, tmp);
15591 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15592 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15593 ? DW_OP_plus : DW_OP_xor, 0, 0));
15594 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15595 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15596 VAR_INIT_STATUS_INITIALIZED);
15597 add_loc_descr (&ret, tmp);
15598 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15599 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15600 add_loc_descr (&ret, l1jump);
15601 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15602 add_loc_descr (&ret, l2label);
15603 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15604 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15605 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15606 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15607 return ret;
15610 /* BSWAP (constS is initial shift count, either 56 or 24):
15611 constS const0
15612 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15613 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15614 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15615 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15616 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15618 static dw_loc_descr_ref
15619 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15620 machine_mode mem_mode)
15622 dw_loc_descr_ref op0, ret, tmp;
15623 dw_loc_descr_ref l1jump, l1label;
15624 dw_loc_descr_ref l2jump, l2label;
15626 if (BITS_PER_UNIT != 8
15627 || (GET_MODE_BITSIZE (mode) != 32
15628 && GET_MODE_BITSIZE (mode) != 64))
15629 return NULL;
15631 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15632 VAR_INIT_STATUS_INITIALIZED);
15633 if (op0 == NULL)
15634 return NULL;
15636 ret = op0;
15637 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15638 mode, mem_mode,
15639 VAR_INIT_STATUS_INITIALIZED);
15640 if (tmp == NULL)
15641 return NULL;
15642 add_loc_descr (&ret, tmp);
15643 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15644 VAR_INIT_STATUS_INITIALIZED);
15645 if (tmp == NULL)
15646 return NULL;
15647 add_loc_descr (&ret, tmp);
15648 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15649 add_loc_descr (&ret, l1label);
15650 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15651 mode, mem_mode,
15652 VAR_INIT_STATUS_INITIALIZED);
15653 add_loc_descr (&ret, tmp);
15654 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15655 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15656 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15657 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15658 VAR_INIT_STATUS_INITIALIZED);
15659 if (tmp == NULL)
15660 return NULL;
15661 add_loc_descr (&ret, tmp);
15662 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15663 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15664 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15665 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15666 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15667 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15668 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15669 VAR_INIT_STATUS_INITIALIZED);
15670 add_loc_descr (&ret, tmp);
15671 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15672 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15673 add_loc_descr (&ret, l2jump);
15674 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15675 VAR_INIT_STATUS_INITIALIZED);
15676 add_loc_descr (&ret, tmp);
15677 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15678 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15679 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15680 add_loc_descr (&ret, l1jump);
15681 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15682 add_loc_descr (&ret, l2label);
15683 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15684 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15685 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15686 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15687 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15688 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15689 return ret;
15692 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15693 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15694 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15695 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15697 ROTATERT is similar:
15698 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15699 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15700 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15702 static dw_loc_descr_ref
15703 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15704 machine_mode mem_mode)
15706 rtx rtlop1 = XEXP (rtl, 1);
15707 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15708 int i;
15710 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15711 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15712 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15713 VAR_INIT_STATUS_INITIALIZED);
15714 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15715 VAR_INIT_STATUS_INITIALIZED);
15716 if (op0 == NULL || op1 == NULL)
15717 return NULL;
15718 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15719 for (i = 0; i < 2; i++)
15721 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15722 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15723 mode, mem_mode,
15724 VAR_INIT_STATUS_INITIALIZED);
15725 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15726 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15727 ? DW_OP_const4u
15728 : HOST_BITS_PER_WIDE_INT == 64
15729 ? DW_OP_const8u : DW_OP_constu,
15730 GET_MODE_MASK (mode), 0);
15731 else
15732 mask[i] = NULL;
15733 if (mask[i] == NULL)
15734 return NULL;
15735 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15737 ret = op0;
15738 add_loc_descr (&ret, op1);
15739 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15740 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15741 if (GET_CODE (rtl) == ROTATERT)
15743 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15744 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15745 GET_MODE_BITSIZE (mode), 0));
15747 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15748 if (mask[0] != NULL)
15749 add_loc_descr (&ret, mask[0]);
15750 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15751 if (mask[1] != NULL)
15753 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15754 add_loc_descr (&ret, mask[1]);
15755 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15757 if (GET_CODE (rtl) == ROTATE)
15759 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15760 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15761 GET_MODE_BITSIZE (mode), 0));
15763 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15764 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15765 return ret;
15768 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15769 for DEBUG_PARAMETER_REF RTL. */
15771 static dw_loc_descr_ref
15772 parameter_ref_descriptor (rtx rtl)
15774 dw_loc_descr_ref ret;
15775 dw_die_ref ref;
15777 if (dwarf_strict)
15778 return NULL;
15779 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15780 /* With LTO during LTRANS we get the late DIE that refers to the early
15781 DIE, thus we add another indirection here. This seems to confuse
15782 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15783 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15784 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15785 if (ref)
15787 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15788 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15789 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15791 else
15793 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15794 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15796 return ret;
15799 /* The following routine converts the RTL for a variable or parameter
15800 (resident in memory) into an equivalent Dwarf representation of a
15801 mechanism for getting the address of that same variable onto the top of a
15802 hypothetical "address evaluation" stack.
15804 When creating memory location descriptors, we are effectively transforming
15805 the RTL for a memory-resident object into its Dwarf postfix expression
15806 equivalent. This routine recursively descends an RTL tree, turning
15807 it into Dwarf postfix code as it goes.
15809 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15811 MEM_MODE is the mode of the memory reference, needed to handle some
15812 autoincrement addressing modes.
15814 Return 0 if we can't represent the location. */
15816 dw_loc_descr_ref
15817 mem_loc_descriptor (rtx rtl, machine_mode mode,
15818 machine_mode mem_mode,
15819 enum var_init_status initialized)
15821 dw_loc_descr_ref mem_loc_result = NULL;
15822 enum dwarf_location_atom op;
15823 dw_loc_descr_ref op0, op1;
15824 rtx inner = NULL_RTX;
15825 poly_int64 offset;
15827 if (mode == VOIDmode)
15828 mode = GET_MODE (rtl);
15830 /* Note that for a dynamically sized array, the location we will generate a
15831 description of here will be the lowest numbered location which is
15832 actually within the array. That's *not* necessarily the same as the
15833 zeroth element of the array. */
15835 rtl = targetm.delegitimize_address (rtl);
15837 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15838 return NULL;
15840 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15841 switch (GET_CODE (rtl))
15843 case POST_INC:
15844 case POST_DEC:
15845 case POST_MODIFY:
15846 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15848 case SUBREG:
15849 /* The case of a subreg may arise when we have a local (register)
15850 variable or a formal (register) parameter which doesn't quite fill
15851 up an entire register. For now, just assume that it is
15852 legitimate to make the Dwarf info refer to the whole register which
15853 contains the given subreg. */
15854 if (!subreg_lowpart_p (rtl))
15855 break;
15856 inner = SUBREG_REG (rtl);
15857 /* FALLTHRU */
15858 case TRUNCATE:
15859 if (inner == NULL_RTX)
15860 inner = XEXP (rtl, 0);
15861 if (is_a <scalar_int_mode> (mode, &int_mode)
15862 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15863 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15864 #ifdef POINTERS_EXTEND_UNSIGNED
15865 || (int_mode == Pmode && mem_mode != VOIDmode)
15866 #endif
15868 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15870 mem_loc_result = mem_loc_descriptor (inner,
15871 inner_mode,
15872 mem_mode, initialized);
15873 break;
15875 if (dwarf_strict && dwarf_version < 5)
15876 break;
15877 if (is_a <scalar_int_mode> (mode, &int_mode)
15878 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15879 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15880 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15882 dw_die_ref type_die;
15883 dw_loc_descr_ref cvt;
15885 mem_loc_result = mem_loc_descriptor (inner,
15886 GET_MODE (inner),
15887 mem_mode, initialized);
15888 if (mem_loc_result == NULL)
15889 break;
15890 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15891 if (type_die == NULL)
15893 mem_loc_result = NULL;
15894 break;
15896 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15897 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15898 else
15899 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15900 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15901 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15902 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15903 add_loc_descr (&mem_loc_result, cvt);
15904 if (is_a <scalar_int_mode> (mode, &int_mode)
15905 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15907 /* Convert it to untyped afterwards. */
15908 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15909 add_loc_descr (&mem_loc_result, cvt);
15912 break;
15914 case REG:
15915 if (!is_a <scalar_int_mode> (mode, &int_mode)
15916 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15917 && rtl != arg_pointer_rtx
15918 && rtl != frame_pointer_rtx
15919 #ifdef POINTERS_EXTEND_UNSIGNED
15920 && (int_mode != Pmode || mem_mode == VOIDmode)
15921 #endif
15924 dw_die_ref type_die;
15925 unsigned int dbx_regnum;
15927 if (dwarf_strict && dwarf_version < 5)
15928 break;
15929 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15930 break;
15931 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15932 if (type_die == NULL)
15933 break;
15935 dbx_regnum = dbx_reg_number (rtl);
15936 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15937 break;
15938 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15939 dbx_regnum, 0);
15940 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15941 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15942 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15943 break;
15945 /* Whenever a register number forms a part of the description of the
15946 method for calculating the (dynamic) address of a memory resident
15947 object, DWARF rules require the register number be referred to as
15948 a "base register". This distinction is not based in any way upon
15949 what category of register the hardware believes the given register
15950 belongs to. This is strictly DWARF terminology we're dealing with
15951 here. Note that in cases where the location of a memory-resident
15952 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15953 OP_CONST (0)) the actual DWARF location descriptor that we generate
15954 may just be OP_BASEREG (basereg). This may look deceptively like
15955 the object in question was allocated to a register (rather than in
15956 memory) so DWARF consumers need to be aware of the subtle
15957 distinction between OP_REG and OP_BASEREG. */
15958 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15959 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15960 else if (stack_realign_drap
15961 && crtl->drap_reg
15962 && crtl->args.internal_arg_pointer == rtl
15963 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15965 /* If RTL is internal_arg_pointer, which has been optimized
15966 out, use DRAP instead. */
15967 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15968 VAR_INIT_STATUS_INITIALIZED);
15970 break;
15972 case SIGN_EXTEND:
15973 case ZERO_EXTEND:
15974 if (!is_a <scalar_int_mode> (mode, &int_mode)
15975 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15976 break;
15977 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15978 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15979 if (op0 == 0)
15980 break;
15981 else if (GET_CODE (rtl) == ZERO_EXTEND
15982 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15983 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15984 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15985 to expand zero extend as two shifts instead of
15986 masking. */
15987 && GET_MODE_SIZE (inner_mode) <= 4)
15989 mem_loc_result = op0;
15990 add_loc_descr (&mem_loc_result,
15991 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15992 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15994 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15996 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15997 shift *= BITS_PER_UNIT;
15998 if (GET_CODE (rtl) == SIGN_EXTEND)
15999 op = DW_OP_shra;
16000 else
16001 op = DW_OP_shr;
16002 mem_loc_result = op0;
16003 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16004 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16005 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16006 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16008 else if (!dwarf_strict || dwarf_version >= 5)
16010 dw_die_ref type_die1, type_die2;
16011 dw_loc_descr_ref cvt;
16013 type_die1 = base_type_for_mode (inner_mode,
16014 GET_CODE (rtl) == ZERO_EXTEND);
16015 if (type_die1 == NULL)
16016 break;
16017 type_die2 = base_type_for_mode (int_mode, 1);
16018 if (type_die2 == NULL)
16019 break;
16020 mem_loc_result = op0;
16021 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16022 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16023 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16024 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16025 add_loc_descr (&mem_loc_result, cvt);
16026 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16027 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16028 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16029 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16030 add_loc_descr (&mem_loc_result, cvt);
16032 break;
16034 case MEM:
16036 rtx new_rtl = avoid_constant_pool_reference (rtl);
16037 if (new_rtl != rtl)
16039 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16040 initialized);
16041 if (mem_loc_result != NULL)
16042 return mem_loc_result;
16045 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16046 get_address_mode (rtl), mode,
16047 VAR_INIT_STATUS_INITIALIZED);
16048 if (mem_loc_result == NULL)
16049 mem_loc_result = tls_mem_loc_descriptor (rtl);
16050 if (mem_loc_result != NULL)
16052 if (!is_a <scalar_int_mode> (mode, &int_mode)
16053 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16055 dw_die_ref type_die;
16056 dw_loc_descr_ref deref;
16057 HOST_WIDE_INT size;
16059 if (dwarf_strict && dwarf_version < 5)
16060 return NULL;
16061 if (!GET_MODE_SIZE (mode).is_constant (&size))
16062 return NULL;
16063 type_die
16064 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16065 if (type_die == NULL)
16066 return NULL;
16067 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16068 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16069 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16070 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16071 add_loc_descr (&mem_loc_result, deref);
16073 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16074 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16075 else
16076 add_loc_descr (&mem_loc_result,
16077 new_loc_descr (DW_OP_deref_size,
16078 GET_MODE_SIZE (int_mode), 0));
16080 break;
16082 case LO_SUM:
16083 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16085 case LABEL_REF:
16086 /* Some ports can transform a symbol ref into a label ref, because
16087 the symbol ref is too far away and has to be dumped into a constant
16088 pool. */
16089 case CONST:
16090 case SYMBOL_REF:
16091 case UNSPEC:
16092 if (!is_a <scalar_int_mode> (mode, &int_mode)
16093 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16094 #ifdef POINTERS_EXTEND_UNSIGNED
16095 && (int_mode != Pmode || mem_mode == VOIDmode)
16096 #endif
16098 break;
16100 if (GET_CODE (rtl) == UNSPEC)
16102 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16103 can't express it in the debug info. This can happen e.g. with some
16104 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16105 approves. */
16106 bool not_ok = false;
16107 subrtx_var_iterator::array_type array;
16108 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16109 if (*iter != rtl && !CONSTANT_P (*iter))
16111 not_ok = true;
16112 break;
16115 if (not_ok)
16116 break;
16118 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16119 if (!const_ok_for_output_1 (*iter))
16121 not_ok = true;
16122 break;
16125 if (not_ok)
16126 break;
16128 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16129 goto symref;
16132 if (GET_CODE (rtl) == SYMBOL_REF
16133 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16135 dw_loc_descr_ref temp;
16137 /* If this is not defined, we have no way to emit the data. */
16138 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16139 break;
16141 temp = new_addr_loc_descr (rtl, dtprel_true);
16143 /* We check for DWARF 5 here because gdb did not implement
16144 DW_OP_form_tls_address until after 7.12. */
16145 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16146 ? DW_OP_form_tls_address
16147 : DW_OP_GNU_push_tls_address),
16148 0, 0);
16149 add_loc_descr (&mem_loc_result, temp);
16151 break;
16154 if (!const_ok_for_output (rtl))
16156 if (GET_CODE (rtl) == CONST)
16157 switch (GET_CODE (XEXP (rtl, 0)))
16159 case NOT:
16160 op = DW_OP_not;
16161 goto try_const_unop;
16162 case NEG:
16163 op = DW_OP_neg;
16164 goto try_const_unop;
16165 try_const_unop:
16166 rtx arg;
16167 arg = XEXP (XEXP (rtl, 0), 0);
16168 if (!CONSTANT_P (arg))
16169 arg = gen_rtx_CONST (int_mode, arg);
16170 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16171 initialized);
16172 if (op0)
16174 mem_loc_result = op0;
16175 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16177 break;
16178 default:
16179 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16180 mem_mode, initialized);
16181 break;
16183 break;
16186 symref:
16187 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16188 vec_safe_push (used_rtx_array, rtl);
16189 break;
16191 case CONCAT:
16192 case CONCATN:
16193 case VAR_LOCATION:
16194 case DEBUG_IMPLICIT_PTR:
16195 expansion_failed (NULL_TREE, rtl,
16196 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16197 return 0;
16199 case ENTRY_VALUE:
16200 if (dwarf_strict && dwarf_version < 5)
16201 return NULL;
16202 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16204 if (!is_a <scalar_int_mode> (mode, &int_mode)
16205 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16206 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16207 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16208 else
16210 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
16211 if (dbx_regnum == IGNORED_DWARF_REGNUM)
16212 return NULL;
16213 op0 = one_reg_loc_descriptor (dbx_regnum,
16214 VAR_INIT_STATUS_INITIALIZED);
16217 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16218 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16220 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16221 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16222 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16223 return NULL;
16225 else
16226 gcc_unreachable ();
16227 if (op0 == NULL)
16228 return NULL;
16229 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16230 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16231 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16232 break;
16234 case DEBUG_PARAMETER_REF:
16235 mem_loc_result = parameter_ref_descriptor (rtl);
16236 break;
16238 case PRE_MODIFY:
16239 /* Extract the PLUS expression nested inside and fall into
16240 PLUS code below. */
16241 rtl = XEXP (rtl, 1);
16242 goto plus;
16244 case PRE_INC:
16245 case PRE_DEC:
16246 /* Turn these into a PLUS expression and fall into the PLUS code
16247 below. */
16248 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16249 gen_int_mode (GET_CODE (rtl) == PRE_INC
16250 ? GET_MODE_UNIT_SIZE (mem_mode)
16251 : -GET_MODE_UNIT_SIZE (mem_mode),
16252 mode));
16254 /* fall through */
16256 case PLUS:
16257 plus:
16258 if (is_based_loc (rtl)
16259 && is_a <scalar_int_mode> (mode, &int_mode)
16260 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16261 || XEXP (rtl, 0) == arg_pointer_rtx
16262 || XEXP (rtl, 0) == frame_pointer_rtx))
16263 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16264 INTVAL (XEXP (rtl, 1)),
16265 VAR_INIT_STATUS_INITIALIZED);
16266 else
16268 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16269 VAR_INIT_STATUS_INITIALIZED);
16270 if (mem_loc_result == 0)
16271 break;
16273 if (CONST_INT_P (XEXP (rtl, 1))
16274 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16275 <= DWARF2_ADDR_SIZE))
16276 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16277 else
16279 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16280 VAR_INIT_STATUS_INITIALIZED);
16281 if (op1 == 0)
16282 return NULL;
16283 add_loc_descr (&mem_loc_result, op1);
16284 add_loc_descr (&mem_loc_result,
16285 new_loc_descr (DW_OP_plus, 0, 0));
16288 break;
16290 /* If a pseudo-reg is optimized away, it is possible for it to
16291 be replaced with a MEM containing a multiply or shift. */
16292 case MINUS:
16293 op = DW_OP_minus;
16294 goto do_binop;
16296 case MULT:
16297 op = DW_OP_mul;
16298 goto do_binop;
16300 case DIV:
16301 if ((!dwarf_strict || dwarf_version >= 5)
16302 && is_a <scalar_int_mode> (mode, &int_mode)
16303 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16305 mem_loc_result = typed_binop (DW_OP_div, rtl,
16306 base_type_for_mode (mode, 0),
16307 int_mode, mem_mode);
16308 break;
16310 op = DW_OP_div;
16311 goto do_binop;
16313 case UMOD:
16314 op = DW_OP_mod;
16315 goto do_binop;
16317 case ASHIFT:
16318 op = DW_OP_shl;
16319 goto do_shift;
16321 case ASHIFTRT:
16322 op = DW_OP_shra;
16323 goto do_shift;
16325 case LSHIFTRT:
16326 op = DW_OP_shr;
16327 goto do_shift;
16329 do_shift:
16330 if (!is_a <scalar_int_mode> (mode, &int_mode))
16331 break;
16332 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16333 VAR_INIT_STATUS_INITIALIZED);
16335 rtx rtlop1 = XEXP (rtl, 1);
16336 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16337 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16338 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16339 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16340 VAR_INIT_STATUS_INITIALIZED);
16343 if (op0 == 0 || op1 == 0)
16344 break;
16346 mem_loc_result = op0;
16347 add_loc_descr (&mem_loc_result, op1);
16348 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16349 break;
16351 case AND:
16352 op = DW_OP_and;
16353 goto do_binop;
16355 case IOR:
16356 op = DW_OP_or;
16357 goto do_binop;
16359 case XOR:
16360 op = DW_OP_xor;
16361 goto do_binop;
16363 do_binop:
16364 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16365 VAR_INIT_STATUS_INITIALIZED);
16366 if (XEXP (rtl, 0) == XEXP (rtl, 1))
16368 if (op0 == 0)
16369 break;
16370 mem_loc_result = op0;
16371 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_dup, 0, 0));
16372 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16373 break;
16375 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16376 VAR_INIT_STATUS_INITIALIZED);
16378 if (op0 == 0 || op1 == 0)
16379 break;
16381 mem_loc_result = op0;
16382 add_loc_descr (&mem_loc_result, op1);
16383 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16384 break;
16386 case MOD:
16387 if ((!dwarf_strict || dwarf_version >= 5)
16388 && is_a <scalar_int_mode> (mode, &int_mode)
16389 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16391 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16392 base_type_for_mode (mode, 0),
16393 int_mode, mem_mode);
16394 break;
16397 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16398 VAR_INIT_STATUS_INITIALIZED);
16399 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16400 VAR_INIT_STATUS_INITIALIZED);
16402 if (op0 == 0 || op1 == 0)
16403 break;
16405 mem_loc_result = op0;
16406 add_loc_descr (&mem_loc_result, op1);
16407 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16408 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16409 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16410 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16411 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16412 break;
16414 case UDIV:
16415 if ((!dwarf_strict || dwarf_version >= 5)
16416 && is_a <scalar_int_mode> (mode, &int_mode))
16418 /* We can use a signed divide if the sign bit is not set. */
16419 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16421 op = DW_OP_div;
16422 goto do_binop;
16425 mem_loc_result = typed_binop (DW_OP_div, rtl,
16426 base_type_for_mode (int_mode, 1),
16427 int_mode, mem_mode);
16429 break;
16431 case NOT:
16432 op = DW_OP_not;
16433 goto do_unop;
16435 case ABS:
16436 op = DW_OP_abs;
16437 goto do_unop;
16439 case NEG:
16440 op = DW_OP_neg;
16441 goto do_unop;
16443 do_unop:
16444 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16445 VAR_INIT_STATUS_INITIALIZED);
16447 if (op0 == 0)
16448 break;
16450 mem_loc_result = op0;
16451 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16452 break;
16454 case CONST_INT:
16455 if (!is_a <scalar_int_mode> (mode, &int_mode)
16456 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16457 #ifdef POINTERS_EXTEND_UNSIGNED
16458 || (int_mode == Pmode
16459 && mem_mode != VOIDmode
16460 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16461 #endif
16464 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16465 break;
16467 if ((!dwarf_strict || dwarf_version >= 5)
16468 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16469 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16471 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16472 scalar_int_mode amode;
16473 if (type_die == NULL)
16474 return NULL;
16475 if (INTVAL (rtl) >= 0
16476 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16477 .exists (&amode))
16478 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16479 /* const DW_OP_convert <XXX> vs.
16480 DW_OP_const_type <XXX, 1, const>. */
16481 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16482 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16484 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16485 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16486 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16487 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16488 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16489 add_loc_descr (&mem_loc_result, op0);
16490 return mem_loc_result;
16492 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16493 INTVAL (rtl));
16494 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16495 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16496 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16497 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16498 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16499 else
16501 mem_loc_result->dw_loc_oprnd2.val_class
16502 = dw_val_class_const_double;
16503 mem_loc_result->dw_loc_oprnd2.v.val_double
16504 = double_int::from_shwi (INTVAL (rtl));
16507 break;
16509 case CONST_DOUBLE:
16510 if (!dwarf_strict || dwarf_version >= 5)
16512 dw_die_ref type_die;
16514 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16515 CONST_DOUBLE rtx could represent either a large integer
16516 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16517 the value is always a floating point constant.
16519 When it is an integer, a CONST_DOUBLE is used whenever
16520 the constant requires 2 HWIs to be adequately represented.
16521 We output CONST_DOUBLEs as blocks. */
16522 if (mode == VOIDmode
16523 || (GET_MODE (rtl) == VOIDmode
16524 && maybe_ne (GET_MODE_BITSIZE (mode),
16525 HOST_BITS_PER_DOUBLE_INT)))
16526 break;
16527 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16528 if (type_die == NULL)
16529 return NULL;
16530 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16531 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16532 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16533 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16534 #if TARGET_SUPPORTS_WIDE_INT == 0
16535 if (!SCALAR_FLOAT_MODE_P (mode))
16537 mem_loc_result->dw_loc_oprnd2.val_class
16538 = dw_val_class_const_double;
16539 mem_loc_result->dw_loc_oprnd2.v.val_double
16540 = rtx_to_double_int (rtl);
16542 else
16543 #endif
16545 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16546 unsigned int length = GET_MODE_SIZE (float_mode);
16547 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16548 unsigned int elt_size = insert_float (rtl, array);
16550 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16551 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16552 = length / elt_size;
16553 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16554 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16557 break;
16559 case CONST_WIDE_INT:
16560 if (!dwarf_strict || dwarf_version >= 5)
16562 dw_die_ref type_die;
16564 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16565 if (type_die == NULL)
16566 return NULL;
16567 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16568 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16569 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16570 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16571 mem_loc_result->dw_loc_oprnd2.val_class
16572 = dw_val_class_wide_int;
16573 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16574 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16576 break;
16578 case CONST_POLY_INT:
16579 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16580 break;
16582 case EQ:
16583 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16584 break;
16586 case GE:
16587 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16588 break;
16590 case GT:
16591 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16592 break;
16594 case LE:
16595 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16596 break;
16598 case LT:
16599 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16600 break;
16602 case NE:
16603 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16604 break;
16606 case GEU:
16607 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16608 break;
16610 case GTU:
16611 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16612 break;
16614 case LEU:
16615 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16616 break;
16618 case LTU:
16619 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16620 break;
16622 case UMIN:
16623 case UMAX:
16624 if (!SCALAR_INT_MODE_P (mode))
16625 break;
16626 /* FALLTHRU */
16627 case SMIN:
16628 case SMAX:
16629 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16630 break;
16632 case ZERO_EXTRACT:
16633 case SIGN_EXTRACT:
16634 if (CONST_INT_P (XEXP (rtl, 1))
16635 && CONST_INT_P (XEXP (rtl, 2))
16636 && is_a <scalar_int_mode> (mode, &int_mode)
16637 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16638 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16639 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16640 && ((unsigned) INTVAL (XEXP (rtl, 1))
16641 + (unsigned) INTVAL (XEXP (rtl, 2))
16642 <= GET_MODE_BITSIZE (int_mode)))
16644 int shift, size;
16645 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16646 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16647 if (op0 == 0)
16648 break;
16649 if (GET_CODE (rtl) == SIGN_EXTRACT)
16650 op = DW_OP_shra;
16651 else
16652 op = DW_OP_shr;
16653 mem_loc_result = op0;
16654 size = INTVAL (XEXP (rtl, 1));
16655 shift = INTVAL (XEXP (rtl, 2));
16656 if (BITS_BIG_ENDIAN)
16657 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16658 if (shift + size != (int) DWARF2_ADDR_SIZE)
16660 add_loc_descr (&mem_loc_result,
16661 int_loc_descriptor (DWARF2_ADDR_SIZE
16662 - shift - size));
16663 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16665 if (size != (int) DWARF2_ADDR_SIZE)
16667 add_loc_descr (&mem_loc_result,
16668 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16669 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16672 break;
16674 case IF_THEN_ELSE:
16676 dw_loc_descr_ref op2, bra_node, drop_node;
16677 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16678 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16679 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16680 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16681 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16682 VAR_INIT_STATUS_INITIALIZED);
16683 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16684 VAR_INIT_STATUS_INITIALIZED);
16685 if (op0 == NULL || op1 == NULL || op2 == NULL)
16686 break;
16688 mem_loc_result = op1;
16689 add_loc_descr (&mem_loc_result, op2);
16690 add_loc_descr (&mem_loc_result, op0);
16691 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16692 add_loc_descr (&mem_loc_result, bra_node);
16693 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16694 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16695 add_loc_descr (&mem_loc_result, drop_node);
16696 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16697 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16699 break;
16701 case FLOAT_EXTEND:
16702 case FLOAT_TRUNCATE:
16703 case FLOAT:
16704 case UNSIGNED_FLOAT:
16705 case FIX:
16706 case UNSIGNED_FIX:
16707 if (!dwarf_strict || dwarf_version >= 5)
16709 dw_die_ref type_die;
16710 dw_loc_descr_ref cvt;
16712 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16713 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16714 if (op0 == NULL)
16715 break;
16716 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16717 && (GET_CODE (rtl) == FLOAT
16718 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16720 type_die = base_type_for_mode (int_mode,
16721 GET_CODE (rtl) == UNSIGNED_FLOAT);
16722 if (type_die == NULL)
16723 break;
16724 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16725 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16726 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16727 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16728 add_loc_descr (&op0, cvt);
16730 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16731 if (type_die == NULL)
16732 break;
16733 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16734 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16735 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16736 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16737 add_loc_descr (&op0, cvt);
16738 if (is_a <scalar_int_mode> (mode, &int_mode)
16739 && (GET_CODE (rtl) == FIX
16740 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16742 op0 = convert_descriptor_to_mode (int_mode, op0);
16743 if (op0 == NULL)
16744 break;
16746 mem_loc_result = op0;
16748 break;
16750 case CLZ:
16751 case CTZ:
16752 case FFS:
16753 if (is_a <scalar_int_mode> (mode, &int_mode))
16754 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16755 break;
16757 case POPCOUNT:
16758 case PARITY:
16759 if (is_a <scalar_int_mode> (mode, &int_mode))
16760 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16761 break;
16763 case BSWAP:
16764 if (is_a <scalar_int_mode> (mode, &int_mode))
16765 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16766 break;
16768 case ROTATE:
16769 case ROTATERT:
16770 if (is_a <scalar_int_mode> (mode, &int_mode))
16771 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16772 break;
16774 case COMPARE:
16775 /* In theory, we could implement the above. */
16776 /* DWARF cannot represent the unsigned compare operations
16777 natively. */
16778 case SS_MULT:
16779 case US_MULT:
16780 case SS_DIV:
16781 case US_DIV:
16782 case SS_PLUS:
16783 case US_PLUS:
16784 case SS_MINUS:
16785 case US_MINUS:
16786 case SS_NEG:
16787 case US_NEG:
16788 case SS_ABS:
16789 case SS_ASHIFT:
16790 case US_ASHIFT:
16791 case SS_TRUNCATE:
16792 case US_TRUNCATE:
16793 case UNORDERED:
16794 case ORDERED:
16795 case UNEQ:
16796 case UNGE:
16797 case UNGT:
16798 case UNLE:
16799 case UNLT:
16800 case LTGT:
16801 case FRACT_CONVERT:
16802 case UNSIGNED_FRACT_CONVERT:
16803 case SAT_FRACT:
16804 case UNSIGNED_SAT_FRACT:
16805 case SQRT:
16806 case ASM_OPERANDS:
16807 case VEC_MERGE:
16808 case VEC_SELECT:
16809 case VEC_CONCAT:
16810 case VEC_DUPLICATE:
16811 case VEC_SERIES:
16812 case HIGH:
16813 case FMA:
16814 case STRICT_LOW_PART:
16815 case CONST_VECTOR:
16816 case CONST_FIXED:
16817 case CLRSB:
16818 case CLOBBER:
16819 case SMUL_HIGHPART:
16820 case UMUL_HIGHPART:
16821 break;
16823 case CONST_STRING:
16824 resolve_one_addr (&rtl);
16825 goto symref;
16827 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16828 the expression. An UNSPEC rtx represents a raw DWARF operation,
16829 new_loc_descr is called for it to build the operation directly.
16830 Otherwise mem_loc_descriptor is called recursively. */
16831 case PARALLEL:
16833 int index = 0;
16834 dw_loc_descr_ref exp_result = NULL;
16836 for (; index < XVECLEN (rtl, 0); index++)
16838 rtx elem = XVECEXP (rtl, 0, index);
16839 if (GET_CODE (elem) == UNSPEC)
16841 /* Each DWARF operation UNSPEC contain two operands, if
16842 one operand is not used for the operation, const0_rtx is
16843 passed. */
16844 gcc_assert (XVECLEN (elem, 0) == 2);
16846 HOST_WIDE_INT dw_op = XINT (elem, 1);
16847 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16848 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16849 exp_result
16850 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16851 oprnd2);
16853 else
16854 exp_result
16855 = mem_loc_descriptor (elem, mode, mem_mode,
16856 VAR_INIT_STATUS_INITIALIZED);
16858 if (!mem_loc_result)
16859 mem_loc_result = exp_result;
16860 else
16861 add_loc_descr (&mem_loc_result, exp_result);
16864 break;
16867 default:
16868 if (flag_checking)
16870 print_rtl (stderr, rtl);
16871 gcc_unreachable ();
16873 break;
16876 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16877 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16879 return mem_loc_result;
16882 /* Return a descriptor that describes the concatenation of two locations.
16883 This is typically a complex variable. */
16885 static dw_loc_descr_ref
16886 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16888 /* At present we only track constant-sized pieces. */
16889 unsigned int size0, size1;
16890 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16891 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16892 return 0;
16894 dw_loc_descr_ref cc_loc_result = NULL;
16895 dw_loc_descr_ref x0_ref
16896 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16897 dw_loc_descr_ref x1_ref
16898 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16900 if (x0_ref == 0 || x1_ref == 0)
16901 return 0;
16903 cc_loc_result = x0_ref;
16904 add_loc_descr_op_piece (&cc_loc_result, size0);
16906 add_loc_descr (&cc_loc_result, x1_ref);
16907 add_loc_descr_op_piece (&cc_loc_result, size1);
16909 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16910 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16912 return cc_loc_result;
16915 /* Return a descriptor that describes the concatenation of N
16916 locations. */
16918 static dw_loc_descr_ref
16919 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16921 unsigned int i;
16922 dw_loc_descr_ref cc_loc_result = NULL;
16923 unsigned int n = XVECLEN (concatn, 0);
16924 unsigned int size;
16926 for (i = 0; i < n; ++i)
16928 dw_loc_descr_ref ref;
16929 rtx x = XVECEXP (concatn, 0, i);
16931 /* At present we only track constant-sized pieces. */
16932 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16933 return NULL;
16935 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16936 if (ref == NULL)
16937 return NULL;
16939 add_loc_descr (&cc_loc_result, ref);
16940 add_loc_descr_op_piece (&cc_loc_result, size);
16943 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16944 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16946 return cc_loc_result;
16949 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16950 for DEBUG_IMPLICIT_PTR RTL. */
16952 static dw_loc_descr_ref
16953 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16955 dw_loc_descr_ref ret;
16956 dw_die_ref ref;
16958 if (dwarf_strict && dwarf_version < 5)
16959 return NULL;
16960 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16961 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16962 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16963 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16964 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16965 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16966 if (ref)
16968 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16969 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16970 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16972 else
16974 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16975 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16977 return ret;
16980 /* Output a proper Dwarf location descriptor for a variable or parameter
16981 which is either allocated in a register or in a memory location. For a
16982 register, we just generate an OP_REG and the register number. For a
16983 memory location we provide a Dwarf postfix expression describing how to
16984 generate the (dynamic) address of the object onto the address stack.
16986 MODE is mode of the decl if this loc_descriptor is going to be used in
16987 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16988 allowed, VOIDmode otherwise.
16990 If we don't know how to describe it, return 0. */
16992 static dw_loc_descr_ref
16993 loc_descriptor (rtx rtl, machine_mode mode,
16994 enum var_init_status initialized)
16996 dw_loc_descr_ref loc_result = NULL;
16997 scalar_int_mode int_mode;
16999 switch (GET_CODE (rtl))
17001 case SUBREG:
17002 /* The case of a subreg may arise when we have a local (register)
17003 variable or a formal (register) parameter which doesn't quite fill
17004 up an entire register. For now, just assume that it is
17005 legitimate to make the Dwarf info refer to the whole register which
17006 contains the given subreg. */
17007 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17008 loc_result = loc_descriptor (SUBREG_REG (rtl),
17009 GET_MODE (SUBREG_REG (rtl)), initialized);
17010 else
17011 goto do_default;
17012 break;
17014 case REG:
17015 loc_result = reg_loc_descriptor (rtl, initialized);
17016 break;
17018 case MEM:
17019 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17020 GET_MODE (rtl), initialized);
17021 if (loc_result == NULL)
17022 loc_result = tls_mem_loc_descriptor (rtl);
17023 if (loc_result == NULL)
17025 rtx new_rtl = avoid_constant_pool_reference (rtl);
17026 if (new_rtl != rtl)
17027 loc_result = loc_descriptor (new_rtl, mode, initialized);
17029 break;
17031 case CONCAT:
17032 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17033 initialized);
17034 break;
17036 case CONCATN:
17037 loc_result = concatn_loc_descriptor (rtl, initialized);
17038 break;
17040 case VAR_LOCATION:
17041 /* Single part. */
17042 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17044 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17045 if (GET_CODE (loc) == EXPR_LIST)
17046 loc = XEXP (loc, 0);
17047 loc_result = loc_descriptor (loc, mode, initialized);
17048 break;
17051 rtl = XEXP (rtl, 1);
17052 /* FALLTHRU */
17054 case PARALLEL:
17056 rtvec par_elems = XVEC (rtl, 0);
17057 int num_elem = GET_NUM_ELEM (par_elems);
17058 machine_mode mode;
17059 int i, size;
17061 /* Create the first one, so we have something to add to. */
17062 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17063 VOIDmode, initialized);
17064 if (loc_result == NULL)
17065 return NULL;
17066 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17067 /* At present we only track constant-sized pieces. */
17068 if (!GET_MODE_SIZE (mode).is_constant (&size))
17069 return NULL;
17070 add_loc_descr_op_piece (&loc_result, size);
17071 for (i = 1; i < num_elem; i++)
17073 dw_loc_descr_ref temp;
17075 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17076 VOIDmode, initialized);
17077 if (temp == NULL)
17078 return NULL;
17079 add_loc_descr (&loc_result, temp);
17080 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17081 /* At present we only track constant-sized pieces. */
17082 if (!GET_MODE_SIZE (mode).is_constant (&size))
17083 return NULL;
17084 add_loc_descr_op_piece (&loc_result, size);
17087 break;
17089 case CONST_INT:
17090 if (mode != VOIDmode && mode != BLKmode)
17092 int_mode = as_a <scalar_int_mode> (mode);
17093 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17094 INTVAL (rtl));
17096 break;
17098 case CONST_DOUBLE:
17099 if (mode == VOIDmode)
17100 mode = GET_MODE (rtl);
17102 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17104 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17106 /* Note that a CONST_DOUBLE rtx could represent either an integer
17107 or a floating-point constant. A CONST_DOUBLE is used whenever
17108 the constant requires more than one word in order to be
17109 adequately represented. We output CONST_DOUBLEs as blocks. */
17110 scalar_mode smode = as_a <scalar_mode> (mode);
17111 loc_result = new_loc_descr (DW_OP_implicit_value,
17112 GET_MODE_SIZE (smode), 0);
17113 #if TARGET_SUPPORTS_WIDE_INT == 0
17114 if (!SCALAR_FLOAT_MODE_P (smode))
17116 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17117 loc_result->dw_loc_oprnd2.v.val_double
17118 = rtx_to_double_int (rtl);
17120 else
17121 #endif
17123 unsigned int length = GET_MODE_SIZE (smode);
17124 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17125 unsigned int elt_size = insert_float (rtl, array);
17127 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17128 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17129 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17130 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17133 break;
17135 case CONST_WIDE_INT:
17136 if (mode == VOIDmode)
17137 mode = GET_MODE (rtl);
17139 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17141 int_mode = as_a <scalar_int_mode> (mode);
17142 loc_result = new_loc_descr (DW_OP_implicit_value,
17143 GET_MODE_SIZE (int_mode), 0);
17144 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17145 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17146 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17148 break;
17150 case CONST_VECTOR:
17151 if (mode == VOIDmode)
17152 mode = GET_MODE (rtl);
17154 if (mode != VOIDmode
17155 /* The combination of a length and byte elt_size doesn't extend
17156 naturally to boolean vectors, where several elements are packed
17157 into the same byte. */
17158 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17159 && (dwarf_version >= 4 || !dwarf_strict))
17161 unsigned int length;
17162 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17163 return NULL;
17165 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17166 unsigned char *array
17167 = ggc_vec_alloc<unsigned char> (length * elt_size);
17168 unsigned int i;
17169 unsigned char *p;
17170 machine_mode imode = GET_MODE_INNER (mode);
17172 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17173 switch (GET_MODE_CLASS (mode))
17175 case MODE_VECTOR_INT:
17176 for (i = 0, p = array; i < length; i++, p += elt_size)
17178 rtx elt = CONST_VECTOR_ELT (rtl, i);
17179 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17181 break;
17183 case MODE_VECTOR_FLOAT:
17184 for (i = 0, p = array; i < length; i++, p += elt_size)
17186 rtx elt = CONST_VECTOR_ELT (rtl, i);
17187 insert_float (elt, p);
17189 break;
17191 default:
17192 gcc_unreachable ();
17195 loc_result = new_loc_descr (DW_OP_implicit_value,
17196 length * elt_size, 0);
17197 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17198 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17199 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17200 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17202 break;
17204 case CONST:
17205 if (mode == VOIDmode
17206 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17207 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17208 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17210 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17211 break;
17213 /* FALLTHROUGH */
17214 case SYMBOL_REF:
17215 if (!const_ok_for_output (rtl))
17216 break;
17217 /* FALLTHROUGH */
17218 case LABEL_REF:
17219 if (is_a <scalar_int_mode> (mode, &int_mode)
17220 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17221 && (dwarf_version >= 4 || !dwarf_strict))
17223 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17224 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17225 vec_safe_push (used_rtx_array, rtl);
17227 break;
17229 case DEBUG_IMPLICIT_PTR:
17230 loc_result = implicit_ptr_descriptor (rtl, 0);
17231 break;
17233 case PLUS:
17234 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17235 && CONST_INT_P (XEXP (rtl, 1)))
17237 loc_result
17238 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17239 break;
17241 /* FALLTHRU */
17242 do_default:
17243 default:
17244 if ((is_a <scalar_int_mode> (mode, &int_mode)
17245 && GET_MODE (rtl) == int_mode
17246 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17247 && dwarf_version >= 4)
17248 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17250 /* Value expression. */
17251 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17252 if (loc_result)
17253 add_loc_descr (&loc_result,
17254 new_loc_descr (DW_OP_stack_value, 0, 0));
17256 break;
17259 return loc_result;
17262 /* We need to figure out what section we should use as the base for the
17263 address ranges where a given location is valid.
17264 1. If this particular DECL has a section associated with it, use that.
17265 2. If this function has a section associated with it, use that.
17266 3. Otherwise, use the text section.
17267 XXX: If you split a variable across multiple sections, we won't notice. */
17269 static const char *
17270 secname_for_decl (const_tree decl)
17272 const char *secname;
17274 if (VAR_OR_FUNCTION_DECL_P (decl)
17275 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17276 && DECL_SECTION_NAME (decl))
17277 secname = DECL_SECTION_NAME (decl);
17278 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17280 if (in_cold_section_p)
17282 section *sec = current_function_section ();
17283 if (sec->common.flags & SECTION_NAMED)
17284 return sec->named.name;
17286 secname = DECL_SECTION_NAME (current_function_decl);
17288 else if (cfun && in_cold_section_p)
17289 secname = crtl->subsections.cold_section_label;
17290 else
17291 secname = text_section_label;
17293 return secname;
17296 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17298 static bool
17299 decl_by_reference_p (tree decl)
17301 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17302 || VAR_P (decl))
17303 && DECL_BY_REFERENCE (decl));
17306 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17307 for VARLOC. */
17309 static dw_loc_descr_ref
17310 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17311 enum var_init_status initialized)
17313 int have_address = 0;
17314 dw_loc_descr_ref descr;
17315 machine_mode mode;
17317 if (want_address != 2)
17319 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17320 /* Single part. */
17321 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17323 varloc = PAT_VAR_LOCATION_LOC (varloc);
17324 if (GET_CODE (varloc) == EXPR_LIST)
17325 varloc = XEXP (varloc, 0);
17326 mode = GET_MODE (varloc);
17327 if (MEM_P (varloc))
17329 rtx addr = XEXP (varloc, 0);
17330 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17331 mode, initialized);
17332 if (descr)
17333 have_address = 1;
17334 else
17336 rtx x = avoid_constant_pool_reference (varloc);
17337 if (x != varloc)
17338 descr = mem_loc_descriptor (x, mode, VOIDmode,
17339 initialized);
17342 else
17343 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17345 else
17346 return 0;
17348 else
17350 if (GET_CODE (varloc) == VAR_LOCATION)
17351 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17352 else
17353 mode = DECL_MODE (loc);
17354 descr = loc_descriptor (varloc, mode, initialized);
17355 have_address = 1;
17358 if (!descr)
17359 return 0;
17361 if (want_address == 2 && !have_address
17362 && (dwarf_version >= 4 || !dwarf_strict))
17364 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17366 expansion_failed (loc, NULL_RTX,
17367 "DWARF address size mismatch");
17368 return 0;
17370 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17371 have_address = 1;
17373 /* Show if we can't fill the request for an address. */
17374 if (want_address && !have_address)
17376 expansion_failed (loc, NULL_RTX,
17377 "Want address and only have value");
17378 return 0;
17381 /* If we've got an address and don't want one, dereference. */
17382 if (!want_address && have_address)
17384 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17385 enum dwarf_location_atom op;
17387 if (size > DWARF2_ADDR_SIZE || size == -1)
17389 expansion_failed (loc, NULL_RTX,
17390 "DWARF address size mismatch");
17391 return 0;
17393 else if (size == DWARF2_ADDR_SIZE)
17394 op = DW_OP_deref;
17395 else
17396 op = DW_OP_deref_size;
17398 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17401 return descr;
17404 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17405 if it is not possible. */
17407 static dw_loc_descr_ref
17408 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17410 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17411 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17412 else if (dwarf_version >= 3 || !dwarf_strict)
17413 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17414 else
17415 return NULL;
17418 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17419 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17421 static dw_loc_descr_ref
17422 dw_sra_loc_expr (tree decl, rtx loc)
17424 rtx p;
17425 unsigned HOST_WIDE_INT padsize = 0;
17426 dw_loc_descr_ref descr, *descr_tail;
17427 unsigned HOST_WIDE_INT decl_size;
17428 rtx varloc;
17429 enum var_init_status initialized;
17431 if (DECL_SIZE (decl) == NULL
17432 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17433 return NULL;
17435 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17436 descr = NULL;
17437 descr_tail = &descr;
17439 for (p = loc; p; p = XEXP (p, 1))
17441 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17442 rtx loc_note = *decl_piece_varloc_ptr (p);
17443 dw_loc_descr_ref cur_descr;
17444 dw_loc_descr_ref *tail, last = NULL;
17445 unsigned HOST_WIDE_INT opsize = 0;
17447 if (loc_note == NULL_RTX
17448 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17450 padsize += bitsize;
17451 continue;
17453 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17454 varloc = NOTE_VAR_LOCATION (loc_note);
17455 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17456 if (cur_descr == NULL)
17458 padsize += bitsize;
17459 continue;
17462 /* Check that cur_descr either doesn't use
17463 DW_OP_*piece operations, or their sum is equal
17464 to bitsize. Otherwise we can't embed it. */
17465 for (tail = &cur_descr; *tail != NULL;
17466 tail = &(*tail)->dw_loc_next)
17467 if ((*tail)->dw_loc_opc == DW_OP_piece)
17469 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17470 * BITS_PER_UNIT;
17471 last = *tail;
17473 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17475 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17476 last = *tail;
17479 if (last != NULL && opsize != bitsize)
17481 padsize += bitsize;
17482 /* Discard the current piece of the descriptor and release any
17483 addr_table entries it uses. */
17484 remove_loc_list_addr_table_entries (cur_descr);
17485 continue;
17488 /* If there is a hole, add DW_OP_*piece after empty DWARF
17489 expression, which means that those bits are optimized out. */
17490 if (padsize)
17492 if (padsize > decl_size)
17494 remove_loc_list_addr_table_entries (cur_descr);
17495 goto discard_descr;
17497 decl_size -= padsize;
17498 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17499 if (*descr_tail == NULL)
17501 remove_loc_list_addr_table_entries (cur_descr);
17502 goto discard_descr;
17504 descr_tail = &(*descr_tail)->dw_loc_next;
17505 padsize = 0;
17507 *descr_tail = cur_descr;
17508 descr_tail = tail;
17509 if (bitsize > decl_size)
17510 goto discard_descr;
17511 decl_size -= bitsize;
17512 if (last == NULL)
17514 HOST_WIDE_INT offset = 0;
17515 if (GET_CODE (varloc) == VAR_LOCATION
17516 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17518 varloc = PAT_VAR_LOCATION_LOC (varloc);
17519 if (GET_CODE (varloc) == EXPR_LIST)
17520 varloc = XEXP (varloc, 0);
17524 if (GET_CODE (varloc) == CONST
17525 || GET_CODE (varloc) == SIGN_EXTEND
17526 || GET_CODE (varloc) == ZERO_EXTEND)
17527 varloc = XEXP (varloc, 0);
17528 else if (GET_CODE (varloc) == SUBREG)
17529 varloc = SUBREG_REG (varloc);
17530 else
17531 break;
17533 while (1);
17534 /* DW_OP_bit_size offset should be zero for register
17535 or implicit location descriptions and empty location
17536 descriptions, but for memory addresses needs big endian
17537 adjustment. */
17538 if (MEM_P (varloc))
17540 unsigned HOST_WIDE_INT memsize;
17541 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17542 goto discard_descr;
17543 memsize *= BITS_PER_UNIT;
17544 if (memsize != bitsize)
17546 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17547 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17548 goto discard_descr;
17549 if (memsize < bitsize)
17550 goto discard_descr;
17551 if (BITS_BIG_ENDIAN)
17552 offset = memsize - bitsize;
17556 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17557 if (*descr_tail == NULL)
17558 goto discard_descr;
17559 descr_tail = &(*descr_tail)->dw_loc_next;
17563 /* If there were any non-empty expressions, add padding till the end of
17564 the decl. */
17565 if (descr != NULL && decl_size != 0)
17567 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17568 if (*descr_tail == NULL)
17569 goto discard_descr;
17571 return descr;
17573 discard_descr:
17574 /* Discard the descriptor and release any addr_table entries it uses. */
17575 remove_loc_list_addr_table_entries (descr);
17576 return NULL;
17579 /* Return the dwarf representation of the location list LOC_LIST of
17580 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17581 function. */
17583 static dw_loc_list_ref
17584 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17586 const char *endname, *secname;
17587 var_loc_view endview;
17588 rtx varloc;
17589 enum var_init_status initialized;
17590 struct var_loc_node *node;
17591 dw_loc_descr_ref descr;
17592 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17593 dw_loc_list_ref list = NULL;
17594 dw_loc_list_ref *listp = &list;
17596 /* Now that we know what section we are using for a base,
17597 actually construct the list of locations.
17598 The first location information is what is passed to the
17599 function that creates the location list, and the remaining
17600 locations just get added on to that list.
17601 Note that we only know the start address for a location
17602 (IE location changes), so to build the range, we use
17603 the range [current location start, next location start].
17604 This means we have to special case the last node, and generate
17605 a range of [last location start, end of function label]. */
17607 if (cfun && crtl->has_bb_partition)
17609 bool save_in_cold_section_p = in_cold_section_p;
17610 in_cold_section_p = first_function_block_is_cold;
17611 if (loc_list->last_before_switch == NULL)
17612 in_cold_section_p = !in_cold_section_p;
17613 secname = secname_for_decl (decl);
17614 in_cold_section_p = save_in_cold_section_p;
17616 else
17617 secname = secname_for_decl (decl);
17619 for (node = loc_list->first; node; node = node->next)
17621 bool range_across_switch = false;
17622 if (GET_CODE (node->loc) == EXPR_LIST
17623 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17625 if (GET_CODE (node->loc) == EXPR_LIST)
17627 descr = NULL;
17628 /* This requires DW_OP_{,bit_}piece, which is not usable
17629 inside DWARF expressions. */
17630 if (want_address == 2)
17631 descr = dw_sra_loc_expr (decl, node->loc);
17633 else
17635 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17636 varloc = NOTE_VAR_LOCATION (node->loc);
17637 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17639 if (descr)
17641 /* If section switch happens in between node->label
17642 and node->next->label (or end of function) and
17643 we can't emit it as a single entry list,
17644 emit two ranges, first one ending at the end
17645 of first partition and second one starting at the
17646 beginning of second partition. */
17647 if (node == loc_list->last_before_switch
17648 && (node != loc_list->first || loc_list->first->next
17649 /* If we are to emit a view number, we will emit
17650 a loclist rather than a single location
17651 expression for the entire function (see
17652 loc_list_has_views), so we have to split the
17653 range that straddles across partitions. */
17654 || !ZERO_VIEW_P (node->view))
17655 && current_function_decl)
17657 endname = cfun->fde->dw_fde_end;
17658 endview = 0;
17659 range_across_switch = true;
17661 /* The variable has a location between NODE->LABEL and
17662 NODE->NEXT->LABEL. */
17663 else if (node->next)
17664 endname = node->next->label, endview = node->next->view;
17665 /* If the variable has a location at the last label
17666 it keeps its location until the end of function. */
17667 else if (!current_function_decl)
17668 endname = text_end_label, endview = 0;
17669 else
17671 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17672 current_function_funcdef_no);
17673 endname = ggc_strdup (label_id);
17674 endview = 0;
17677 *listp = new_loc_list (descr, node->label, node->view,
17678 endname, endview, secname);
17679 if (TREE_CODE (decl) == PARM_DECL
17680 && node == loc_list->first
17681 && NOTE_P (node->loc)
17682 && strcmp (node->label, endname) == 0)
17683 (*listp)->force = true;
17684 listp = &(*listp)->dw_loc_next;
17688 if (cfun
17689 && crtl->has_bb_partition
17690 && node == loc_list->last_before_switch)
17692 bool save_in_cold_section_p = in_cold_section_p;
17693 in_cold_section_p = !first_function_block_is_cold;
17694 secname = secname_for_decl (decl);
17695 in_cold_section_p = save_in_cold_section_p;
17698 if (range_across_switch)
17700 if (GET_CODE (node->loc) == EXPR_LIST)
17701 descr = dw_sra_loc_expr (decl, node->loc);
17702 else
17704 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17705 varloc = NOTE_VAR_LOCATION (node->loc);
17706 descr = dw_loc_list_1 (decl, varloc, want_address,
17707 initialized);
17709 gcc_assert (descr);
17710 /* The variable has a location between NODE->LABEL and
17711 NODE->NEXT->LABEL. */
17712 if (node->next)
17713 endname = node->next->label, endview = node->next->view;
17714 else
17715 endname = cfun->fde->dw_fde_second_end, endview = 0;
17716 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17717 endname, endview, secname);
17718 listp = &(*listp)->dw_loc_next;
17722 /* Try to avoid the overhead of a location list emitting a location
17723 expression instead, but only if we didn't have more than one
17724 location entry in the first place. If some entries were not
17725 representable, we don't want to pretend a single entry that was
17726 applies to the entire scope in which the variable is
17727 available. */
17728 if (list && loc_list->first->next)
17729 gen_llsym (list);
17730 else
17731 maybe_gen_llsym (list);
17733 return list;
17736 /* Return if the loc_list has only single element and thus can be represented
17737 as location description. */
17739 static bool
17740 single_element_loc_list_p (dw_loc_list_ref list)
17742 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17743 return !list->ll_symbol;
17746 /* Duplicate a single element of location list. */
17748 static inline dw_loc_descr_ref
17749 copy_loc_descr (dw_loc_descr_ref ref)
17751 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17752 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17753 return copy;
17756 /* To each location in list LIST append loc descr REF. */
17758 static void
17759 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17761 dw_loc_descr_ref copy;
17762 add_loc_descr (&list->expr, ref);
17763 list = list->dw_loc_next;
17764 while (list)
17766 copy = copy_loc_descr (ref);
17767 add_loc_descr (&list->expr, copy);
17768 while (copy->dw_loc_next)
17769 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17770 list = list->dw_loc_next;
17774 /* To each location in list LIST prepend loc descr REF. */
17776 static void
17777 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17779 dw_loc_descr_ref copy;
17780 dw_loc_descr_ref ref_end = list->expr;
17781 add_loc_descr (&ref, list->expr);
17782 list->expr = ref;
17783 list = list->dw_loc_next;
17784 while (list)
17786 dw_loc_descr_ref end = list->expr;
17787 list->expr = copy = copy_loc_descr (ref);
17788 while (copy->dw_loc_next != ref_end)
17789 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17790 copy->dw_loc_next = end;
17791 list = list->dw_loc_next;
17795 /* Given two lists RET and LIST
17796 produce location list that is result of adding expression in LIST
17797 to expression in RET on each position in program.
17798 Might be destructive on both RET and LIST.
17800 TODO: We handle only simple cases of RET or LIST having at most one
17801 element. General case would involve sorting the lists in program order
17802 and merging them that will need some additional work.
17803 Adding that will improve quality of debug info especially for SRA-ed
17804 structures. */
17806 static void
17807 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17809 if (!list)
17810 return;
17811 if (!*ret)
17813 *ret = list;
17814 return;
17816 if (!list->dw_loc_next)
17818 add_loc_descr_to_each (*ret, list->expr);
17819 return;
17821 if (!(*ret)->dw_loc_next)
17823 prepend_loc_descr_to_each (list, (*ret)->expr);
17824 *ret = list;
17825 return;
17827 expansion_failed (NULL_TREE, NULL_RTX,
17828 "Don't know how to merge two non-trivial"
17829 " location lists.\n");
17830 *ret = NULL;
17831 return;
17834 /* LOC is constant expression. Try a luck, look it up in constant
17835 pool and return its loc_descr of its address. */
17837 static dw_loc_descr_ref
17838 cst_pool_loc_descr (tree loc)
17840 /* Get an RTL for this, if something has been emitted. */
17841 rtx rtl = lookup_constant_def (loc);
17843 if (!rtl || !MEM_P (rtl))
17845 gcc_assert (!rtl);
17846 return 0;
17848 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17850 /* TODO: We might get more coverage if we was actually delaying expansion
17851 of all expressions till end of compilation when constant pools are fully
17852 populated. */
17853 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17855 expansion_failed (loc, NULL_RTX,
17856 "CST value in contant pool but not marked.");
17857 return 0;
17859 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17860 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17863 /* Return dw_loc_list representing address of addr_expr LOC
17864 by looking for inner INDIRECT_REF expression and turning
17865 it into simple arithmetics.
17867 See loc_list_from_tree for the meaning of CONTEXT. */
17869 static dw_loc_list_ref
17870 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17871 loc_descr_context *context)
17873 tree obj, offset;
17874 poly_int64 bitsize, bitpos, bytepos;
17875 machine_mode mode;
17876 int unsignedp, reversep, volatilep = 0;
17877 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17879 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17880 &bitsize, &bitpos, &offset, &mode,
17881 &unsignedp, &reversep, &volatilep);
17882 STRIP_NOPS (obj);
17883 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17885 expansion_failed (loc, NULL_RTX, "bitfield access");
17886 return 0;
17888 if (!INDIRECT_REF_P (obj))
17890 expansion_failed (obj,
17891 NULL_RTX, "no indirect ref in inner refrence");
17892 return 0;
17894 if (!offset && known_eq (bitpos, 0))
17895 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17896 context);
17897 else if (toplev
17898 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17899 && (dwarf_version >= 4 || !dwarf_strict))
17901 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17902 if (!list_ret)
17903 return 0;
17904 if (offset)
17906 /* Variable offset. */
17907 list_ret1 = loc_list_from_tree (offset, 0, context);
17908 if (list_ret1 == 0)
17909 return 0;
17910 add_loc_list (&list_ret, list_ret1);
17911 if (!list_ret)
17912 return 0;
17913 add_loc_descr_to_each (list_ret,
17914 new_loc_descr (DW_OP_plus, 0, 0));
17916 HOST_WIDE_INT value;
17917 if (bytepos.is_constant (&value) && value > 0)
17918 add_loc_descr_to_each (list_ret,
17919 new_loc_descr (DW_OP_plus_uconst, value, 0));
17920 else if (maybe_ne (bytepos, 0))
17921 loc_list_plus_const (list_ret, bytepos);
17922 add_loc_descr_to_each (list_ret,
17923 new_loc_descr (DW_OP_stack_value, 0, 0));
17925 return list_ret;
17928 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17929 all operations from LOC are nops, move to the last one. Insert in NOPS all
17930 operations that are skipped. */
17932 static void
17933 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17934 hash_set<dw_loc_descr_ref> &nops)
17936 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17938 nops.add (loc);
17939 loc = loc->dw_loc_next;
17943 /* Helper for loc_descr_without_nops: free the location description operation
17944 P. */
17946 bool
17947 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17949 ggc_free (loc);
17950 return true;
17953 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17954 finishes LOC. */
17956 static void
17957 loc_descr_without_nops (dw_loc_descr_ref &loc)
17959 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17960 return;
17962 /* Set of all DW_OP_nop operations we remove. */
17963 hash_set<dw_loc_descr_ref> nops;
17965 /* First, strip all prefix NOP operations in order to keep the head of the
17966 operations list. */
17967 loc_descr_to_next_no_nop (loc, nops);
17969 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17971 /* For control flow operations: strip "prefix" nops in destination
17972 labels. */
17973 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17974 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17975 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17976 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17978 /* Do the same for the operations that follow, then move to the next
17979 iteration. */
17980 if (cur->dw_loc_next != NULL)
17981 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17982 cur = cur->dw_loc_next;
17985 nops.traverse<void *, free_loc_descr> (NULL);
17989 struct dwarf_procedure_info;
17991 /* Helper structure for location descriptions generation. */
17992 struct loc_descr_context
17994 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17995 NULL_TREE if DW_OP_push_object_address in invalid for this location
17996 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17997 tree context_type;
17998 /* The ..._DECL node that should be translated as a
17999 DW_OP_push_object_address operation. */
18000 tree base_decl;
18001 /* Information about the DWARF procedure we are currently generating. NULL if
18002 we are not generating a DWARF procedure. */
18003 struct dwarf_procedure_info *dpi;
18004 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
18005 by consumer. Used for DW_TAG_generic_subrange attributes. */
18006 bool placeholder_arg;
18007 /* True if PLACEHOLDER_EXPR has been seen. */
18008 bool placeholder_seen;
18009 /* True if strict preservation of signedness has been requested. */
18010 bool strict_signedness;
18013 /* DWARF procedures generation
18015 DWARF expressions (aka. location descriptions) are used to encode variable
18016 things such as sizes or offsets. Such computations can have redundant parts
18017 that can be factorized in order to reduce the size of the output debug
18018 information. This is the whole point of DWARF procedures.
18020 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
18021 already factorized into functions ("size functions") in order to handle very
18022 big and complex types. Such functions are quite simple: they have integral
18023 arguments, they return an integral result and their body contains only a
18024 return statement with arithmetic expressions. This is the only kind of
18025 function we are interested in translating into DWARF procedures, here.
18027 DWARF expressions and DWARF procedure are executed using a stack, so we have
18028 to define some calling convention for them to interact. Let's say that:
18030 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18031 all arguments in reverse order (right-to-left) so that when the DWARF
18032 procedure execution starts, the first argument is the top of the stack.
18034 - Then, when returning, the DWARF procedure must have consumed all arguments
18035 on the stack, must have pushed the result and touched nothing else.
18037 - Each integral argument and the result are integral types can be hold in a
18038 single stack slot.
18040 - We call "frame offset" the number of stack slots that are "under DWARF
18041 procedure control": it includes the arguments slots, the temporaries and
18042 the result slot. Thus, it is equal to the number of arguments when the
18043 procedure execution starts and must be equal to one (the result) when it
18044 returns. */
18046 /* Helper structure used when generating operations for a DWARF procedure. */
18047 struct dwarf_procedure_info
18049 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18050 currently translated. */
18051 tree fndecl;
18052 /* The number of arguments FNDECL takes. */
18053 unsigned args_count;
18056 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18057 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18058 equate it to this DIE. */
18060 static dw_die_ref
18061 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18062 dw_die_ref parent_die)
18064 dw_die_ref dwarf_proc_die;
18066 if ((dwarf_version < 3 && dwarf_strict)
18067 || location == NULL)
18068 return NULL;
18070 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18071 if (fndecl)
18072 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18073 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18074 return dwarf_proc_die;
18077 /* Return whether TYPE is a supported type as a DWARF procedure argument
18078 type or return type (we handle only scalar types and pointer types that
18079 aren't wider than the DWARF expression evaluation stack). */
18081 static bool
18082 is_handled_procedure_type (tree type)
18084 return ((INTEGRAL_TYPE_P (type)
18085 || TREE_CODE (type) == OFFSET_TYPE
18086 || TREE_CODE (type) == POINTER_TYPE)
18087 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18090 /* Helper for resolve_args_picking: do the same but stop when coming across
18091 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18092 offset *before* evaluating the corresponding operation. */
18094 static bool
18095 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18096 struct dwarf_procedure_info *dpi,
18097 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18099 /* The "frame_offset" identifier is already used to name a macro... */
18100 unsigned frame_offset_ = initial_frame_offset;
18101 dw_loc_descr_ref l;
18103 for (l = loc; l != NULL;)
18105 bool existed;
18106 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18108 /* If we already met this node, there is nothing to compute anymore. */
18109 if (existed)
18111 /* Make sure that the stack size is consistent wherever the execution
18112 flow comes from. */
18113 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18114 break;
18116 l_frame_offset = frame_offset_;
18118 /* If needed, relocate the picking offset with respect to the frame
18119 offset. */
18120 if (l->frame_offset_rel)
18122 unsigned HOST_WIDE_INT off;
18123 switch (l->dw_loc_opc)
18125 case DW_OP_pick:
18126 off = l->dw_loc_oprnd1.v.val_unsigned;
18127 break;
18128 case DW_OP_dup:
18129 off = 0;
18130 break;
18131 case DW_OP_over:
18132 off = 1;
18133 break;
18134 default:
18135 gcc_unreachable ();
18137 /* frame_offset_ is the size of the current stack frame, including
18138 incoming arguments. Besides, the arguments are pushed
18139 right-to-left. Thus, in order to access the Nth argument from
18140 this operation node, the picking has to skip temporaries *plus*
18141 one stack slot per argument (0 for the first one, 1 for the second
18142 one, etc.).
18144 The targetted argument number (N) is already set as the operand,
18145 and the number of temporaries can be computed with:
18146 frame_offsets_ - dpi->args_count */
18147 off += frame_offset_ - dpi->args_count;
18149 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18150 if (off > 255)
18151 return false;
18153 if (off == 0)
18155 l->dw_loc_opc = DW_OP_dup;
18156 l->dw_loc_oprnd1.v.val_unsigned = 0;
18158 else if (off == 1)
18160 l->dw_loc_opc = DW_OP_over;
18161 l->dw_loc_oprnd1.v.val_unsigned = 0;
18163 else
18165 l->dw_loc_opc = DW_OP_pick;
18166 l->dw_loc_oprnd1.v.val_unsigned = off;
18170 /* Update frame_offset according to the effect the current operation has
18171 on the stack. */
18172 switch (l->dw_loc_opc)
18174 case DW_OP_deref:
18175 case DW_OP_swap:
18176 case DW_OP_rot:
18177 case DW_OP_abs:
18178 case DW_OP_neg:
18179 case DW_OP_not:
18180 case DW_OP_plus_uconst:
18181 case DW_OP_skip:
18182 case DW_OP_reg0:
18183 case DW_OP_reg1:
18184 case DW_OP_reg2:
18185 case DW_OP_reg3:
18186 case DW_OP_reg4:
18187 case DW_OP_reg5:
18188 case DW_OP_reg6:
18189 case DW_OP_reg7:
18190 case DW_OP_reg8:
18191 case DW_OP_reg9:
18192 case DW_OP_reg10:
18193 case DW_OP_reg11:
18194 case DW_OP_reg12:
18195 case DW_OP_reg13:
18196 case DW_OP_reg14:
18197 case DW_OP_reg15:
18198 case DW_OP_reg16:
18199 case DW_OP_reg17:
18200 case DW_OP_reg18:
18201 case DW_OP_reg19:
18202 case DW_OP_reg20:
18203 case DW_OP_reg21:
18204 case DW_OP_reg22:
18205 case DW_OP_reg23:
18206 case DW_OP_reg24:
18207 case DW_OP_reg25:
18208 case DW_OP_reg26:
18209 case DW_OP_reg27:
18210 case DW_OP_reg28:
18211 case DW_OP_reg29:
18212 case DW_OP_reg30:
18213 case DW_OP_reg31:
18214 case DW_OP_bregx:
18215 case DW_OP_piece:
18216 case DW_OP_deref_size:
18217 case DW_OP_nop:
18218 case DW_OP_bit_piece:
18219 case DW_OP_implicit_value:
18220 case DW_OP_stack_value:
18221 case DW_OP_deref_type:
18222 case DW_OP_convert:
18223 case DW_OP_reinterpret:
18224 case DW_OP_GNU_deref_type:
18225 case DW_OP_GNU_convert:
18226 case DW_OP_GNU_reinterpret:
18227 break;
18229 case DW_OP_addr:
18230 case DW_OP_const1u:
18231 case DW_OP_const1s:
18232 case DW_OP_const2u:
18233 case DW_OP_const2s:
18234 case DW_OP_const4u:
18235 case DW_OP_const4s:
18236 case DW_OP_const8u:
18237 case DW_OP_const8s:
18238 case DW_OP_constu:
18239 case DW_OP_consts:
18240 case DW_OP_dup:
18241 case DW_OP_over:
18242 case DW_OP_pick:
18243 case DW_OP_lit0:
18244 case DW_OP_lit1:
18245 case DW_OP_lit2:
18246 case DW_OP_lit3:
18247 case DW_OP_lit4:
18248 case DW_OP_lit5:
18249 case DW_OP_lit6:
18250 case DW_OP_lit7:
18251 case DW_OP_lit8:
18252 case DW_OP_lit9:
18253 case DW_OP_lit10:
18254 case DW_OP_lit11:
18255 case DW_OP_lit12:
18256 case DW_OP_lit13:
18257 case DW_OP_lit14:
18258 case DW_OP_lit15:
18259 case DW_OP_lit16:
18260 case DW_OP_lit17:
18261 case DW_OP_lit18:
18262 case DW_OP_lit19:
18263 case DW_OP_lit20:
18264 case DW_OP_lit21:
18265 case DW_OP_lit22:
18266 case DW_OP_lit23:
18267 case DW_OP_lit24:
18268 case DW_OP_lit25:
18269 case DW_OP_lit26:
18270 case DW_OP_lit27:
18271 case DW_OP_lit28:
18272 case DW_OP_lit29:
18273 case DW_OP_lit30:
18274 case DW_OP_lit31:
18275 case DW_OP_breg0:
18276 case DW_OP_breg1:
18277 case DW_OP_breg2:
18278 case DW_OP_breg3:
18279 case DW_OP_breg4:
18280 case DW_OP_breg5:
18281 case DW_OP_breg6:
18282 case DW_OP_breg7:
18283 case DW_OP_breg8:
18284 case DW_OP_breg9:
18285 case DW_OP_breg10:
18286 case DW_OP_breg11:
18287 case DW_OP_breg12:
18288 case DW_OP_breg13:
18289 case DW_OP_breg14:
18290 case DW_OP_breg15:
18291 case DW_OP_breg16:
18292 case DW_OP_breg17:
18293 case DW_OP_breg18:
18294 case DW_OP_breg19:
18295 case DW_OP_breg20:
18296 case DW_OP_breg21:
18297 case DW_OP_breg22:
18298 case DW_OP_breg23:
18299 case DW_OP_breg24:
18300 case DW_OP_breg25:
18301 case DW_OP_breg26:
18302 case DW_OP_breg27:
18303 case DW_OP_breg28:
18304 case DW_OP_breg29:
18305 case DW_OP_breg30:
18306 case DW_OP_breg31:
18307 case DW_OP_fbreg:
18308 case DW_OP_push_object_address:
18309 case DW_OP_call_frame_cfa:
18310 case DW_OP_GNU_variable_value:
18311 case DW_OP_GNU_addr_index:
18312 case DW_OP_GNU_const_index:
18313 ++frame_offset_;
18314 break;
18316 case DW_OP_drop:
18317 case DW_OP_xderef:
18318 case DW_OP_and:
18319 case DW_OP_div:
18320 case DW_OP_minus:
18321 case DW_OP_mod:
18322 case DW_OP_mul:
18323 case DW_OP_or:
18324 case DW_OP_plus:
18325 case DW_OP_shl:
18326 case DW_OP_shr:
18327 case DW_OP_shra:
18328 case DW_OP_xor:
18329 case DW_OP_bra:
18330 case DW_OP_eq:
18331 case DW_OP_ge:
18332 case DW_OP_gt:
18333 case DW_OP_le:
18334 case DW_OP_lt:
18335 case DW_OP_ne:
18336 case DW_OP_regx:
18337 case DW_OP_xderef_size:
18338 --frame_offset_;
18339 break;
18341 case DW_OP_call2:
18342 case DW_OP_call4:
18343 case DW_OP_call_ref:
18345 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18346 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18348 if (stack_usage == NULL)
18349 return false;
18350 frame_offset_ += *stack_usage;
18351 break;
18354 case DW_OP_implicit_pointer:
18355 case DW_OP_entry_value:
18356 case DW_OP_const_type:
18357 case DW_OP_regval_type:
18358 case DW_OP_form_tls_address:
18359 case DW_OP_GNU_push_tls_address:
18360 case DW_OP_GNU_uninit:
18361 case DW_OP_GNU_encoded_addr:
18362 case DW_OP_GNU_implicit_pointer:
18363 case DW_OP_GNU_entry_value:
18364 case DW_OP_GNU_const_type:
18365 case DW_OP_GNU_regval_type:
18366 case DW_OP_GNU_parameter_ref:
18367 /* loc_list_from_tree will probably not output these operations for
18368 size functions, so assume they will not appear here. */
18369 /* Fall through... */
18371 default:
18372 gcc_unreachable ();
18375 /* Now, follow the control flow (except subroutine calls). */
18376 switch (l->dw_loc_opc)
18378 case DW_OP_bra:
18379 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18380 frame_offsets))
18381 return false;
18382 /* Fall through. */
18384 case DW_OP_skip:
18385 l = l->dw_loc_oprnd1.v.val_loc;
18386 break;
18388 case DW_OP_stack_value:
18389 return true;
18391 default:
18392 l = l->dw_loc_next;
18393 break;
18397 return true;
18400 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18401 operations) in order to resolve the operand of DW_OP_pick operations that
18402 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18403 offset *before* LOC is executed. Return if all relocations were
18404 successful. */
18406 static bool
18407 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18408 struct dwarf_procedure_info *dpi)
18410 /* Associate to all visited operations the frame offset *before* evaluating
18411 this operation. */
18412 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18414 return
18415 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18418 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18419 Return NULL if it is not possible. */
18421 static dw_die_ref
18422 function_to_dwarf_procedure (tree fndecl)
18424 struct dwarf_procedure_info dpi;
18425 struct loc_descr_context ctx = {
18426 NULL_TREE, /* context_type */
18427 NULL_TREE, /* base_decl */
18428 &dpi, /* dpi */
18429 false, /* placeholder_arg */
18430 false, /* placeholder_seen */
18431 true /* strict_signedness */
18433 dw_die_ref dwarf_proc_die;
18434 tree tree_body = DECL_SAVED_TREE (fndecl);
18435 dw_loc_descr_ref loc_body, epilogue;
18437 tree cursor;
18438 unsigned i;
18440 /* Do not generate multiple DWARF procedures for the same function
18441 declaration. */
18442 dwarf_proc_die = lookup_decl_die (fndecl);
18443 if (dwarf_proc_die != NULL)
18444 return dwarf_proc_die;
18446 /* DWARF procedures are available starting with the DWARFv3 standard. */
18447 if (dwarf_version < 3 && dwarf_strict)
18448 return NULL;
18450 /* We handle only functions for which we still have a body, that return a
18451 supported type and that takes arguments with supported types. Note that
18452 there is no point translating functions that return nothing. */
18453 if (tree_body == NULL_TREE
18454 || DECL_RESULT (fndecl) == NULL_TREE
18455 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18456 return NULL;
18458 for (cursor = DECL_ARGUMENTS (fndecl);
18459 cursor != NULL_TREE;
18460 cursor = TREE_CHAIN (cursor))
18461 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18462 return NULL;
18464 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18465 if (TREE_CODE (tree_body) != RETURN_EXPR)
18466 return NULL;
18467 tree_body = TREE_OPERAND (tree_body, 0);
18468 if (TREE_CODE (tree_body) != MODIFY_EXPR
18469 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18470 return NULL;
18471 tree_body = TREE_OPERAND (tree_body, 1);
18473 /* Try to translate the body expression itself. Note that this will probably
18474 cause an infinite recursion if its call graph has a cycle. This is very
18475 unlikely for size functions, however, so don't bother with such things at
18476 the moment. */
18477 dpi.fndecl = fndecl;
18478 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18479 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18480 if (!loc_body)
18481 return NULL;
18483 /* After evaluating all operands in "loc_body", we should still have on the
18484 stack all arguments plus the desired function result (top of the stack).
18485 Generate code in order to keep only the result in our stack frame. */
18486 epilogue = NULL;
18487 for (i = 0; i < dpi.args_count; ++i)
18489 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18490 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18491 op_couple->dw_loc_next->dw_loc_next = epilogue;
18492 epilogue = op_couple;
18494 add_loc_descr (&loc_body, epilogue);
18495 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18496 return NULL;
18498 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18499 because they are considered useful. Now there is an epilogue, they are
18500 not anymore, so give it another try. */
18501 loc_descr_without_nops (loc_body);
18503 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18504 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18505 though, given that size functions do not come from source, so they should
18506 not have a dedicated DW_TAG_subprogram DIE. */
18507 dwarf_proc_die
18508 = new_dwarf_proc_die (loc_body, fndecl,
18509 get_context_die (DECL_CONTEXT (fndecl)));
18511 /* The called DWARF procedure consumes one stack slot per argument and
18512 returns one stack slot. */
18513 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18515 return dwarf_proc_die;
18518 /* Helper function for loc_list_from_tree. Perform OP binary op,
18519 but after converting arguments to type_die, afterwards convert
18520 back to unsigned. */
18522 static dw_loc_list_ref
18523 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18524 dw_die_ref type_die, scalar_int_mode mode,
18525 struct loc_descr_context *context)
18527 dw_loc_list_ref op0, op1;
18528 dw_loc_descr_ref cvt, binop;
18530 if (type_die == NULL)
18531 return NULL;
18533 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18534 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18535 if (op0 == NULL || op1 == NULL)
18536 return NULL;
18538 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18539 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18540 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18541 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18542 add_loc_descr_to_each (op0, cvt);
18544 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18545 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18546 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18547 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18548 add_loc_descr_to_each (op1, cvt);
18550 add_loc_list (&op0, op1);
18551 if (op0 == NULL)
18552 return NULL;
18554 binop = new_loc_descr (op, 0, 0);
18555 convert_descriptor_to_mode (mode, binop);
18556 add_loc_descr_to_each (op0, binop);
18558 return op0;
18561 /* Generate Dwarf location list representing LOC.
18562 If WANT_ADDRESS is false, expression computing LOC will be computed
18563 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18564 if WANT_ADDRESS is 2, expression computing address useable in location
18565 will be returned (i.e. DW_OP_reg can be used
18566 to refer to register values).
18568 CONTEXT provides information to customize the location descriptions
18569 generation. Its context_type field specifies what type is implicitly
18570 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18571 will not be generated.
18573 Its DPI field determines whether we are generating a DWARF expression for a
18574 DWARF procedure, so PARM_DECL references are processed specifically.
18576 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18577 and dpi fields were null. */
18579 static dw_loc_list_ref
18580 loc_list_from_tree_1 (tree loc, int want_address,
18581 struct loc_descr_context *context)
18583 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18584 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18585 int have_address = 0;
18586 enum dwarf_location_atom op;
18588 /* ??? Most of the time we do not take proper care for sign/zero
18589 extending the values properly. Hopefully this won't be a real
18590 problem... */
18592 if (context != NULL
18593 && context->base_decl == loc
18594 && want_address == 0)
18596 if (dwarf_version >= 3 || !dwarf_strict)
18597 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18598 NULL, 0, NULL, 0, NULL);
18599 else
18600 return NULL;
18603 switch (TREE_CODE (loc))
18605 case ERROR_MARK:
18606 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18607 return 0;
18609 case PLACEHOLDER_EXPR:
18610 /* This case involves extracting fields from an object to determine the
18611 position of other fields. It is supposed to appear only as the first
18612 operand of COMPONENT_REF nodes and to reference precisely the type
18613 that the context allows or its enclosing type. */
18614 if (context != NULL
18615 && (TREE_TYPE (loc) == context->context_type
18616 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18617 && want_address >= 1)
18619 if (dwarf_version >= 3 || !dwarf_strict)
18621 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18622 have_address = 1;
18623 break;
18625 else
18626 return NULL;
18628 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18629 the single argument passed by consumer. */
18630 else if (context != NULL
18631 && context->placeholder_arg
18632 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18633 && want_address == 0)
18635 ret = new_loc_descr (DW_OP_pick, 0, 0);
18636 ret->frame_offset_rel = 1;
18637 context->placeholder_seen = true;
18638 break;
18640 else
18641 expansion_failed (loc, NULL_RTX,
18642 "PLACEHOLDER_EXPR for an unexpected type");
18643 break;
18645 case CALL_EXPR:
18647 tree callee = get_callee_fndecl (loc);
18648 dw_die_ref dwarf_proc;
18650 if (callee
18651 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18652 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18654 /* DWARF procedures are used for size functions, which are built
18655 when size expressions contain conditional constructs, so we
18656 request strict preservation of signedness for comparisons. */
18657 bool old_strict_signedness;
18658 if (context)
18660 old_strict_signedness = context->strict_signedness;
18661 context->strict_signedness = true;
18664 /* Evaluate arguments right-to-left so that the first argument
18665 will be the top-most one on the stack. */
18666 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18668 tree arg = CALL_EXPR_ARG (loc, i);
18669 ret1 = loc_descriptor_from_tree (arg, 0, context);
18670 if (!ret1)
18672 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18673 return NULL;
18675 add_loc_descr (&ret, ret1);
18678 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18679 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18680 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18681 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18682 add_loc_descr (&ret, ret1);
18683 if (context)
18684 context->strict_signedness = old_strict_signedness;
18686 else
18687 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18688 break;
18691 case PREINCREMENT_EXPR:
18692 case PREDECREMENT_EXPR:
18693 case POSTINCREMENT_EXPR:
18694 case POSTDECREMENT_EXPR:
18695 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18696 /* There are no opcodes for these operations. */
18697 return 0;
18699 case ADDR_EXPR:
18700 /* If we already want an address, see if there is INDIRECT_REF inside
18701 e.g. for &this->field. */
18702 if (want_address)
18704 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18705 (loc, want_address == 2, context);
18706 if (list_ret)
18707 have_address = 1;
18708 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18709 && (ret = cst_pool_loc_descr (loc)))
18710 have_address = 1;
18712 /* Otherwise, process the argument and look for the address. */
18713 if (!list_ret && !ret)
18714 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18715 else
18717 if (want_address)
18718 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18719 return NULL;
18721 break;
18723 case VAR_DECL:
18724 if (DECL_THREAD_LOCAL_P (loc))
18726 rtx rtl;
18727 enum dwarf_location_atom tls_op;
18728 enum dtprel_bool dtprel = dtprel_false;
18730 if (targetm.have_tls)
18732 /* If this is not defined, we have no way to emit the
18733 data. */
18734 if (!targetm.asm_out.output_dwarf_dtprel)
18735 return 0;
18737 /* The way DW_OP_GNU_push_tls_address is specified, we
18738 can only look up addresses of objects in the current
18739 module. We used DW_OP_addr as first op, but that's
18740 wrong, because DW_OP_addr is relocated by the debug
18741 info consumer, while DW_OP_GNU_push_tls_address
18742 operand shouldn't be. */
18743 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18744 return 0;
18745 dtprel = dtprel_true;
18746 /* We check for DWARF 5 here because gdb did not implement
18747 DW_OP_form_tls_address until after 7.12. */
18748 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18749 : DW_OP_GNU_push_tls_address);
18751 else
18753 if (!targetm.emutls.debug_form_tls_address
18754 || !(dwarf_version >= 3 || !dwarf_strict))
18755 return 0;
18756 /* We stuffed the control variable into the DECL_VALUE_EXPR
18757 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18758 no longer appear in gimple code. We used the control
18759 variable in specific so that we could pick it up here. */
18760 loc = DECL_VALUE_EXPR (loc);
18761 tls_op = DW_OP_form_tls_address;
18764 rtl = rtl_for_decl_location (loc);
18765 if (rtl == NULL_RTX)
18766 return 0;
18768 if (!MEM_P (rtl))
18769 return 0;
18770 rtl = XEXP (rtl, 0);
18771 if (! CONSTANT_P (rtl))
18772 return 0;
18774 ret = new_addr_loc_descr (rtl, dtprel);
18775 ret1 = new_loc_descr (tls_op, 0, 0);
18776 add_loc_descr (&ret, ret1);
18778 have_address = 1;
18779 break;
18781 /* FALLTHRU */
18783 case PARM_DECL:
18784 if (context != NULL && context->dpi != NULL
18785 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18787 /* We are generating code for a DWARF procedure and we want to access
18788 one of its arguments: find the appropriate argument offset and let
18789 the resolve_args_picking pass compute the offset that complies
18790 with the stack frame size. */
18791 unsigned i = 0;
18792 tree cursor;
18794 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18795 cursor != NULL_TREE && cursor != loc;
18796 cursor = TREE_CHAIN (cursor), ++i)
18798 /* If we are translating a DWARF procedure, all referenced parameters
18799 must belong to the current function. */
18800 gcc_assert (cursor != NULL_TREE);
18802 ret = new_loc_descr (DW_OP_pick, i, 0);
18803 ret->frame_offset_rel = 1;
18804 break;
18806 /* FALLTHRU */
18808 case RESULT_DECL:
18809 if (DECL_HAS_VALUE_EXPR_P (loc))
18811 tree value_expr = DECL_VALUE_EXPR (loc);
18813 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18814 to wait until they get an RTX in order to reference them. */
18815 if (early_dwarf
18816 && TREE_CODE (value_expr) == COMPONENT_REF
18817 && VAR_P (TREE_OPERAND (value_expr, 0))
18818 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18820 else
18821 return loc_list_from_tree_1 (value_expr, want_address, context);
18824 /* FALLTHRU */
18826 case FUNCTION_DECL:
18828 rtx rtl;
18829 var_loc_list *loc_list = lookup_decl_loc (loc);
18831 if (loc_list && loc_list->first)
18833 list_ret = dw_loc_list (loc_list, loc, want_address);
18834 have_address = want_address != 0;
18835 break;
18837 rtl = rtl_for_decl_location (loc);
18838 if (rtl == NULL_RTX)
18840 if (TREE_CODE (loc) != FUNCTION_DECL
18841 && early_dwarf
18842 && want_address != 1
18843 && ! DECL_IGNORED_P (loc)
18844 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18845 || POINTER_TYPE_P (TREE_TYPE (loc)))
18846 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18847 <= DWARF2_ADDR_SIZE))
18849 dw_die_ref ref = lookup_decl_die (loc);
18850 if (ref)
18852 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18853 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18854 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18855 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18857 else if (current_function_decl
18858 && DECL_CONTEXT (loc) == current_function_decl)
18860 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18861 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18862 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18864 break;
18866 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18867 return 0;
18869 else if (CONST_INT_P (rtl))
18871 HOST_WIDE_INT val = INTVAL (rtl);
18872 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18873 val &= GET_MODE_MASK (DECL_MODE (loc));
18874 ret = int_loc_descriptor (val);
18876 else if (GET_CODE (rtl) == CONST_STRING)
18878 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18879 return 0;
18881 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18882 ret = new_addr_loc_descr (rtl, dtprel_false);
18883 else
18885 machine_mode mode, mem_mode;
18887 /* Certain constructs can only be represented at top-level. */
18888 if (want_address == 2)
18890 ret = loc_descriptor (rtl, VOIDmode,
18891 VAR_INIT_STATUS_INITIALIZED);
18892 have_address = 1;
18894 else
18896 mode = GET_MODE (rtl);
18897 mem_mode = VOIDmode;
18898 if (MEM_P (rtl))
18900 mem_mode = mode;
18901 mode = get_address_mode (rtl);
18902 rtl = XEXP (rtl, 0);
18903 have_address = 1;
18905 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18906 VAR_INIT_STATUS_INITIALIZED);
18908 if (!ret)
18909 expansion_failed (loc, rtl,
18910 "failed to produce loc descriptor for rtl");
18913 break;
18915 case MEM_REF:
18916 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18918 have_address = 1;
18919 goto do_plus;
18921 /* Fallthru. */
18922 case INDIRECT_REF:
18923 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18924 have_address = 1;
18925 break;
18927 case TARGET_MEM_REF:
18928 case SSA_NAME:
18929 case DEBUG_EXPR_DECL:
18930 return NULL;
18932 case COMPOUND_EXPR:
18933 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18934 context);
18936 CASE_CONVERT:
18937 case VIEW_CONVERT_EXPR:
18938 case SAVE_EXPR:
18939 case MODIFY_EXPR:
18940 case NON_LVALUE_EXPR:
18941 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18942 context);
18944 case COMPONENT_REF:
18945 case BIT_FIELD_REF:
18946 case ARRAY_REF:
18947 case ARRAY_RANGE_REF:
18948 case REALPART_EXPR:
18949 case IMAGPART_EXPR:
18951 tree obj, offset;
18952 poly_int64 bitsize, bitpos, bytepos;
18953 machine_mode mode;
18954 int unsignedp, reversep, volatilep = 0;
18956 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18957 &unsignedp, &reversep, &volatilep);
18959 gcc_assert (obj != loc);
18961 list_ret = loc_list_from_tree_1 (obj,
18962 want_address == 2
18963 && known_eq (bitpos, 0)
18964 && !offset ? 2 : 1,
18965 context);
18966 /* TODO: We can extract value of the small expression via shifting even
18967 for nonzero bitpos. */
18968 if (list_ret == 0)
18969 return 0;
18970 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18971 || !multiple_p (bitsize, BITS_PER_UNIT))
18973 expansion_failed (loc, NULL_RTX,
18974 "bitfield access");
18975 return 0;
18978 if (offset != NULL_TREE)
18980 /* Variable offset. */
18981 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18982 if (list_ret1 == 0)
18983 return 0;
18984 add_loc_list (&list_ret, list_ret1);
18985 if (!list_ret)
18986 return 0;
18987 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18990 HOST_WIDE_INT value;
18991 if (bytepos.is_constant (&value) && value > 0)
18992 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18993 value, 0));
18994 else if (maybe_ne (bytepos, 0))
18995 loc_list_plus_const (list_ret, bytepos);
18997 have_address = 1;
18998 break;
19001 case INTEGER_CST:
19002 if ((want_address || !tree_fits_shwi_p (loc))
19003 && (ret = cst_pool_loc_descr (loc)))
19004 have_address = 1;
19005 else if (want_address == 2
19006 && tree_fits_shwi_p (loc)
19007 && (ret = address_of_int_loc_descriptor
19008 (int_size_in_bytes (TREE_TYPE (loc)),
19009 tree_to_shwi (loc))))
19010 have_address = 1;
19011 else if (tree_fits_shwi_p (loc))
19012 ret = int_loc_descriptor (tree_to_shwi (loc));
19013 else if (tree_fits_uhwi_p (loc))
19014 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19015 else
19017 expansion_failed (loc, NULL_RTX,
19018 "Integer operand is not host integer");
19019 return 0;
19021 break;
19023 case POLY_INT_CST:
19025 if (want_address)
19027 expansion_failed (loc, NULL_RTX,
19028 "constant address with a runtime component");
19029 return 0;
19031 poly_int64 value;
19032 if (!poly_int_tree_p (loc, &value))
19034 expansion_failed (loc, NULL_RTX, "constant too big");
19035 return 0;
19037 ret = int_loc_descriptor (value);
19039 break;
19041 case CONSTRUCTOR:
19042 case REAL_CST:
19043 case STRING_CST:
19044 case COMPLEX_CST:
19045 if ((ret = cst_pool_loc_descr (loc)))
19046 have_address = 1;
19047 else if (TREE_CODE (loc) == CONSTRUCTOR)
19049 tree type = TREE_TYPE (loc);
19050 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19051 unsigned HOST_WIDE_INT offset = 0;
19052 unsigned HOST_WIDE_INT cnt;
19053 constructor_elt *ce;
19055 if (TREE_CODE (type) == RECORD_TYPE)
19057 /* This is very limited, but it's enough to output
19058 pointers to member functions, as long as the
19059 referenced function is defined in the current
19060 translation unit. */
19061 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19063 tree val = ce->value;
19065 tree field = ce->index;
19067 if (val)
19068 STRIP_NOPS (val);
19070 if (!field || DECL_BIT_FIELD (field))
19072 expansion_failed (loc, NULL_RTX,
19073 "bitfield in record type constructor");
19074 size = offset = (unsigned HOST_WIDE_INT)-1;
19075 ret = NULL;
19076 break;
19079 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19080 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19081 gcc_assert (pos + fieldsize <= size);
19082 if (pos < offset)
19084 expansion_failed (loc, NULL_RTX,
19085 "out-of-order fields in record constructor");
19086 size = offset = (unsigned HOST_WIDE_INT)-1;
19087 ret = NULL;
19088 break;
19090 if (pos > offset)
19092 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19093 add_loc_descr (&ret, ret1);
19094 offset = pos;
19096 if (val && fieldsize != 0)
19098 ret1 = loc_descriptor_from_tree (val, want_address, context);
19099 if (!ret1)
19101 expansion_failed (loc, NULL_RTX,
19102 "unsupported expression in field");
19103 size = offset = (unsigned HOST_WIDE_INT)-1;
19104 ret = NULL;
19105 break;
19107 add_loc_descr (&ret, ret1);
19109 if (fieldsize)
19111 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19112 add_loc_descr (&ret, ret1);
19113 offset = pos + fieldsize;
19117 if (offset != size)
19119 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19120 add_loc_descr (&ret, ret1);
19121 offset = size;
19124 have_address = !!want_address;
19126 else
19127 expansion_failed (loc, NULL_RTX,
19128 "constructor of non-record type");
19130 else
19131 /* We can construct small constants here using int_loc_descriptor. */
19132 expansion_failed (loc, NULL_RTX,
19133 "constructor or constant not in constant pool");
19134 break;
19136 case TRUTH_AND_EXPR:
19137 case TRUTH_ANDIF_EXPR:
19138 case BIT_AND_EXPR:
19139 op = DW_OP_and;
19140 goto do_binop;
19142 case TRUTH_XOR_EXPR:
19143 case BIT_XOR_EXPR:
19144 op = DW_OP_xor;
19145 goto do_binop;
19147 case TRUTH_OR_EXPR:
19148 case TRUTH_ORIF_EXPR:
19149 case BIT_IOR_EXPR:
19150 op = DW_OP_or;
19151 goto do_binop;
19153 case EXACT_DIV_EXPR:
19154 case FLOOR_DIV_EXPR:
19155 case TRUNC_DIV_EXPR:
19156 /* Turn a divide by a power of 2 into a shift when possible. */
19157 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19158 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19160 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19161 if (log2 > 0)
19163 list_ret
19164 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19165 if (list_ret == 0)
19166 return 0;
19168 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19169 add_loc_descr_to_each (list_ret,
19170 new_loc_descr (DW_OP_shr, 0, 0));
19171 break;
19175 /* fall through */
19177 case CEIL_DIV_EXPR:
19178 case ROUND_DIV_EXPR:
19179 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19181 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19182 scalar_int_mode int_mode;
19184 if ((dwarf_strict && dwarf_version < 5)
19185 || !is_a <scalar_int_mode> (mode, &int_mode))
19186 return 0;
19188 /* We can use a signed divide if the sign bit is not set. */
19189 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19191 op = DW_OP_div;
19192 goto do_binop;
19195 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19196 base_type_for_mode (int_mode, 1),
19197 int_mode, context);
19198 break;
19200 op = DW_OP_div;
19201 goto do_binop;
19203 case MINUS_EXPR:
19204 op = DW_OP_minus;
19205 goto do_binop;
19207 case FLOOR_MOD_EXPR:
19208 case CEIL_MOD_EXPR:
19209 case ROUND_MOD_EXPR:
19210 case TRUNC_MOD_EXPR:
19211 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19213 op = DW_OP_mod;
19214 goto do_binop;
19216 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19217 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19218 if (list_ret == 0 || list_ret1 == 0)
19219 return 0;
19221 add_loc_list (&list_ret, list_ret1);
19222 if (list_ret == 0)
19223 return 0;
19224 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19225 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19226 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19227 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19228 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19229 break;
19231 case MULT_EXPR:
19232 op = DW_OP_mul;
19233 goto do_binop;
19235 case LSHIFT_EXPR:
19236 op = DW_OP_shl;
19237 goto do_binop;
19239 case RSHIFT_EXPR:
19240 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19241 goto do_binop;
19243 case POINTER_PLUS_EXPR:
19244 case PLUS_EXPR:
19245 do_plus:
19246 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19248 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19249 smarter to encode their opposite. The DW_OP_plus_uconst operation
19250 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19251 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19252 bytes, Y being the size of the operation that pushes the opposite
19253 of the addend. So let's choose the smallest representation. */
19254 const tree tree_addend = TREE_OPERAND (loc, 1);
19255 offset_int wi_addend;
19256 HOST_WIDE_INT shwi_addend;
19257 dw_loc_descr_ref loc_naddend;
19259 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19260 if (list_ret == 0)
19261 return 0;
19263 /* Try to get the literal to push. It is the opposite of the addend,
19264 so as we rely on wrapping during DWARF evaluation, first decode
19265 the literal as a "DWARF-sized" signed number. */
19266 wi_addend = wi::to_offset (tree_addend);
19267 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19268 shwi_addend = wi_addend.to_shwi ();
19269 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19270 ? int_loc_descriptor (-shwi_addend)
19271 : NULL;
19273 if (loc_naddend != NULL
19274 && ((unsigned) size_of_uleb128 (shwi_addend)
19275 > size_of_loc_descr (loc_naddend)))
19277 add_loc_descr_to_each (list_ret, loc_naddend);
19278 add_loc_descr_to_each (list_ret,
19279 new_loc_descr (DW_OP_minus, 0, 0));
19281 else
19283 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19285 loc_naddend = loc_cur;
19286 loc_cur = loc_cur->dw_loc_next;
19287 ggc_free (loc_naddend);
19289 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19291 break;
19294 op = DW_OP_plus;
19295 goto do_binop;
19297 case LE_EXPR:
19298 op = DW_OP_le;
19299 goto do_comp_binop;
19301 case GE_EXPR:
19302 op = DW_OP_ge;
19303 goto do_comp_binop;
19305 case LT_EXPR:
19306 op = DW_OP_lt;
19307 goto do_comp_binop;
19309 case GT_EXPR:
19310 op = DW_OP_gt;
19311 goto do_comp_binop;
19313 do_comp_binop:
19314 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19316 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19317 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19318 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19319 TREE_CODE (loc));
19320 break;
19322 else
19323 goto do_binop;
19325 case EQ_EXPR:
19326 op = DW_OP_eq;
19327 goto do_binop;
19329 case NE_EXPR:
19330 op = DW_OP_ne;
19331 goto do_binop;
19333 do_binop:
19334 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19335 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19336 if (list_ret == 0 || list_ret1 == 0)
19337 return 0;
19339 add_loc_list (&list_ret, list_ret1);
19340 if (list_ret == 0)
19341 return 0;
19342 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19343 break;
19345 case TRUTH_NOT_EXPR:
19346 case BIT_NOT_EXPR:
19347 op = DW_OP_not;
19348 goto do_unop;
19350 case ABS_EXPR:
19351 op = DW_OP_abs;
19352 goto do_unop;
19354 case NEGATE_EXPR:
19355 op = DW_OP_neg;
19356 goto do_unop;
19358 do_unop:
19359 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19360 if (list_ret == 0)
19361 return 0;
19363 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19364 break;
19366 case MIN_EXPR:
19367 case MAX_EXPR:
19369 const enum tree_code code =
19370 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19372 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19373 build2 (code, integer_type_node,
19374 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19375 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19378 /* fall through */
19380 case COND_EXPR:
19382 dw_loc_descr_ref lhs
19383 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19384 dw_loc_list_ref rhs
19385 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19386 dw_loc_descr_ref bra_node, jump_node, tmp;
19388 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19389 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19390 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19391 list_ret
19392 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19393 0, context);
19394 else
19395 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19396 if (list_ret == 0 || lhs == 0 || rhs == 0)
19397 return 0;
19399 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19400 add_loc_descr_to_each (list_ret, bra_node);
19402 add_loc_list (&list_ret, rhs);
19403 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19404 add_loc_descr_to_each (list_ret, jump_node);
19406 add_loc_descr_to_each (list_ret, lhs);
19407 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19408 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19410 /* ??? Need a node to point the skip at. Use a nop. */
19411 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19412 add_loc_descr_to_each (list_ret, tmp);
19413 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19414 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19416 break;
19418 case FIX_TRUNC_EXPR:
19419 return 0;
19421 case COMPOUND_LITERAL_EXPR:
19422 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19423 0, context);
19425 default:
19426 /* Leave front-end specific codes as simply unknown. This comes
19427 up, for instance, with the C STMT_EXPR. */
19428 if ((unsigned int) TREE_CODE (loc)
19429 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19431 expansion_failed (loc, NULL_RTX,
19432 "language specific tree node");
19433 return 0;
19436 /* Otherwise this is a generic code; we should just lists all of
19437 these explicitly. We forgot one. */
19438 if (flag_checking)
19439 gcc_unreachable ();
19441 /* In a release build, we want to degrade gracefully: better to
19442 generate incomplete debugging information than to crash. */
19443 return NULL;
19446 if (!ret && !list_ret)
19447 return 0;
19449 if (want_address == 2 && !have_address
19450 && (dwarf_version >= 4 || !dwarf_strict))
19452 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19454 expansion_failed (loc, NULL_RTX,
19455 "DWARF address size mismatch");
19456 return 0;
19458 if (ret)
19459 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19460 else
19461 add_loc_descr_to_each (list_ret,
19462 new_loc_descr (DW_OP_stack_value, 0, 0));
19463 have_address = 1;
19465 /* Show if we can't fill the request for an address. */
19466 if (want_address && !have_address)
19468 expansion_failed (loc, NULL_RTX,
19469 "Want address and only have value");
19470 return 0;
19473 gcc_assert (!ret || !list_ret);
19475 /* If we've got an address and don't want one, dereference. */
19476 if (!want_address && have_address)
19478 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19479 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19480 scalar_int_mode int_mode;
19481 dw_die_ref type_die;
19482 dw_loc_descr_ref deref;
19484 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19485 if (size > DWARF2_ADDR_SIZE || size == -1)
19487 expansion_failed (loc, NULL_RTX,
19488 "DWARF address size mismatch");
19489 return 0;
19492 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19493 else if (size == DWARF2_ADDR_SIZE)
19494 deref = new_loc_descr (DW_OP_deref, size, 0);
19496 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19497 extend the value, which is really OK for unsigned types only. */
19498 else if (!(context && context->strict_signedness)
19499 || TYPE_UNSIGNED (TREE_TYPE (loc))
19500 || (dwarf_strict && dwarf_version < 5)
19501 || !is_a <scalar_int_mode> (mode, &int_mode)
19502 || !(type_die = base_type_for_mode (mode, false)))
19503 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19505 /* Use DW_OP_deref_type for signed integral types if possible, but
19506 convert back to the generic type to avoid type mismatches later. */
19507 else
19509 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19510 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19511 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19512 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19513 add_loc_descr (&deref,
19514 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19517 if (ret)
19518 add_loc_descr (&ret, deref);
19519 else
19520 add_loc_descr_to_each (list_ret, deref);
19523 if (ret)
19524 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19526 return list_ret;
19529 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19530 expressions. */
19532 static dw_loc_list_ref
19533 loc_list_from_tree (tree loc, int want_address,
19534 struct loc_descr_context *context)
19536 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19538 for (dw_loc_list_ref loc_cur = result;
19539 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19540 loc_descr_without_nops (loc_cur->expr);
19541 return result;
19544 /* Same as above but return only single location expression. */
19545 static dw_loc_descr_ref
19546 loc_descriptor_from_tree (tree loc, int want_address,
19547 struct loc_descr_context *context)
19549 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19550 if (!ret)
19551 return NULL;
19552 if (ret->dw_loc_next)
19554 expansion_failed (loc, NULL_RTX,
19555 "Location list where only loc descriptor needed");
19556 return NULL;
19558 return ret->expr;
19561 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19562 pointer to the declared type for the relevant field variable, or return
19563 `integer_type_node' if the given node turns out to be an
19564 ERROR_MARK node. */
19566 static inline tree
19567 field_type (const_tree decl)
19569 tree type;
19571 if (TREE_CODE (decl) == ERROR_MARK)
19572 return integer_type_node;
19574 type = DECL_BIT_FIELD_TYPE (decl);
19575 if (type == NULL_TREE)
19576 type = TREE_TYPE (decl);
19578 return type;
19581 /* Given a pointer to a tree node, return the alignment in bits for
19582 it, or else return BITS_PER_WORD if the node actually turns out to
19583 be an ERROR_MARK node. */
19585 static inline unsigned
19586 simple_type_align_in_bits (const_tree type)
19588 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19591 static inline unsigned
19592 simple_decl_align_in_bits (const_tree decl)
19594 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19597 /* Return the result of rounding T up to ALIGN. */
19599 static inline offset_int
19600 round_up_to_align (const offset_int &t, unsigned int align)
19602 return wi::udiv_trunc (t + align - 1, align) * align;
19605 /* Helper structure for RECORD_TYPE processing. */
19606 struct vlr_context
19608 /* Root RECORD_TYPE. It is needed to generate data member location
19609 descriptions in variable-length records (VLR), but also to cope with
19610 variants, which are composed of nested structures multiplexed with
19611 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19612 function processing a FIELD_DECL, it is required to be non null. */
19613 tree struct_type;
19615 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19616 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19617 this variant part as part of the root record (in storage units). For
19618 regular records, it must be NULL_TREE. */
19619 tree variant_part_offset;
19622 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19623 addressed byte of the "containing object" for the given FIELD_DECL. If
19624 possible, return a native constant through CST_OFFSET (in which case NULL is
19625 returned); otherwise return a DWARF expression that computes the offset.
19627 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19628 that offset is, either because the argument turns out to be a pointer to an
19629 ERROR_MARK node, or because the offset expression is too complex for us.
19631 CTX is required: see the comment for VLR_CONTEXT. */
19633 static dw_loc_descr_ref
19634 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19635 HOST_WIDE_INT *cst_offset)
19637 tree tree_result;
19638 dw_loc_list_ref loc_result;
19640 *cst_offset = 0;
19642 if (TREE_CODE (decl) == ERROR_MARK)
19643 return NULL;
19644 else
19645 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19647 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19648 case. */
19649 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19650 return NULL;
19652 /* We used to handle only constant offsets in all cases. Now, we handle
19653 properly dynamic byte offsets only when PCC bitfield type doesn't
19654 matter. */
19655 if (PCC_BITFIELD_TYPE_MATTERS
19656 && DECL_BIT_FIELD_TYPE (decl)
19657 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19659 offset_int object_offset_in_bits;
19660 offset_int object_offset_in_bytes;
19661 offset_int bitpos_int;
19662 tree type;
19663 tree field_size_tree;
19664 offset_int deepest_bitpos;
19665 offset_int field_size_in_bits;
19666 unsigned int type_align_in_bits;
19667 unsigned int decl_align_in_bits;
19668 offset_int type_size_in_bits;
19670 bitpos_int = wi::to_offset (bit_position (decl));
19671 type = field_type (decl);
19672 type_size_in_bits = offset_int_type_size_in_bits (type);
19673 type_align_in_bits = simple_type_align_in_bits (type);
19675 field_size_tree = DECL_SIZE (decl);
19677 /* The size could be unspecified if there was an error, or for
19678 a flexible array member. */
19679 if (!field_size_tree)
19680 field_size_tree = bitsize_zero_node;
19682 /* If the size of the field is not constant, use the type size. */
19683 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19684 field_size_in_bits = wi::to_offset (field_size_tree);
19685 else
19686 field_size_in_bits = type_size_in_bits;
19688 decl_align_in_bits = simple_decl_align_in_bits (decl);
19690 /* The GCC front-end doesn't make any attempt to keep track of the
19691 starting bit offset (relative to the start of the containing
19692 structure type) of the hypothetical "containing object" for a
19693 bit-field. Thus, when computing the byte offset value for the
19694 start of the "containing object" of a bit-field, we must deduce
19695 this information on our own. This can be rather tricky to do in
19696 some cases. For example, handling the following structure type
19697 definition when compiling for an i386/i486 target (which only
19698 aligns long long's to 32-bit boundaries) can be very tricky:
19700 struct S { int field1; long long field2:31; };
19702 Fortunately, there is a simple rule-of-thumb which can be used
19703 in such cases. When compiling for an i386/i486, GCC will
19704 allocate 8 bytes for the structure shown above. It decides to
19705 do this based upon one simple rule for bit-field allocation.
19706 GCC allocates each "containing object" for each bit-field at
19707 the first (i.e. lowest addressed) legitimate alignment boundary
19708 (based upon the required minimum alignment for the declared
19709 type of the field) which it can possibly use, subject to the
19710 condition that there is still enough available space remaining
19711 in the containing object (when allocated at the selected point)
19712 to fully accommodate all of the bits of the bit-field itself.
19714 This simple rule makes it obvious why GCC allocates 8 bytes for
19715 each object of the structure type shown above. When looking
19716 for a place to allocate the "containing object" for `field2',
19717 the compiler simply tries to allocate a 64-bit "containing
19718 object" at each successive 32-bit boundary (starting at zero)
19719 until it finds a place to allocate that 64- bit field such that
19720 at least 31 contiguous (and previously unallocated) bits remain
19721 within that selected 64 bit field. (As it turns out, for the
19722 example above, the compiler finds it is OK to allocate the
19723 "containing object" 64-bit field at bit-offset zero within the
19724 structure type.)
19726 Here we attempt to work backwards from the limited set of facts
19727 we're given, and we try to deduce from those facts, where GCC
19728 must have believed that the containing object started (within
19729 the structure type). The value we deduce is then used (by the
19730 callers of this routine) to generate DW_AT_location and
19731 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19732 the case of DW_AT_location, regular fields as well). */
19734 /* Figure out the bit-distance from the start of the structure to
19735 the "deepest" bit of the bit-field. */
19736 deepest_bitpos = bitpos_int + field_size_in_bits;
19738 /* This is the tricky part. Use some fancy footwork to deduce
19739 where the lowest addressed bit of the containing object must
19740 be. */
19741 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19743 /* Round up to type_align by default. This works best for
19744 bitfields. */
19745 object_offset_in_bits
19746 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19748 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19750 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19752 /* Round up to decl_align instead. */
19753 object_offset_in_bits
19754 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19757 object_offset_in_bytes
19758 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19759 if (ctx->variant_part_offset == NULL_TREE)
19761 *cst_offset = object_offset_in_bytes.to_shwi ();
19762 return NULL;
19764 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19766 else
19767 tree_result = byte_position (decl);
19769 if (ctx->variant_part_offset != NULL_TREE)
19770 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19771 ctx->variant_part_offset, tree_result);
19773 /* If the byte offset is a constant, it's simplier to handle a native
19774 constant rather than a DWARF expression. */
19775 if (TREE_CODE (tree_result) == INTEGER_CST)
19777 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19778 return NULL;
19781 struct loc_descr_context loc_ctx = {
19782 ctx->struct_type, /* context_type */
19783 NULL_TREE, /* base_decl */
19784 NULL, /* dpi */
19785 false, /* placeholder_arg */
19786 false, /* placeholder_seen */
19787 false /* strict_signedness */
19789 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19791 /* We want a DWARF expression: abort if we only have a location list with
19792 multiple elements. */
19793 if (!loc_result || !single_element_loc_list_p (loc_result))
19794 return NULL;
19795 else
19796 return loc_result->expr;
19799 /* The following routines define various Dwarf attributes and any data
19800 associated with them. */
19802 /* Add a location description attribute value to a DIE.
19804 This emits location attributes suitable for whole variables and
19805 whole parameters. Note that the location attributes for struct fields are
19806 generated by the routine `data_member_location_attribute' below. */
19808 static inline void
19809 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19810 dw_loc_list_ref descr)
19812 bool check_no_locviews = true;
19813 if (descr == 0)
19814 return;
19815 if (single_element_loc_list_p (descr))
19816 add_AT_loc (die, attr_kind, descr->expr);
19817 else
19819 add_AT_loc_list (die, attr_kind, descr);
19820 gcc_assert (descr->ll_symbol);
19821 if (attr_kind == DW_AT_location && descr->vl_symbol
19822 && dwarf2out_locviews_in_attribute ())
19824 add_AT_view_list (die, DW_AT_GNU_locviews);
19825 check_no_locviews = false;
19829 if (check_no_locviews)
19830 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19833 /* Add DW_AT_accessibility attribute to DIE if needed. */
19835 static void
19836 add_accessibility_attribute (dw_die_ref die, tree decl)
19838 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19839 children, otherwise the default is DW_ACCESS_public. In DWARF2
19840 the default has always been DW_ACCESS_public. */
19841 if (TREE_PROTECTED (decl))
19842 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19843 else if (TREE_PRIVATE (decl))
19845 if (dwarf_version == 2
19846 || die->die_parent == NULL
19847 || die->die_parent->die_tag != DW_TAG_class_type)
19848 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19850 else if (dwarf_version > 2
19851 && die->die_parent
19852 && die->die_parent->die_tag == DW_TAG_class_type)
19853 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19856 /* Attach the specialized form of location attribute used for data members of
19857 struct and union types. In the special case of a FIELD_DECL node which
19858 represents a bit-field, the "offset" part of this special location
19859 descriptor must indicate the distance in bytes from the lowest-addressed
19860 byte of the containing struct or union type to the lowest-addressed byte of
19861 the "containing object" for the bit-field. (See the `field_byte_offset'
19862 function above).
19864 For any given bit-field, the "containing object" is a hypothetical object
19865 (of some integral or enum type) within which the given bit-field lives. The
19866 type of this hypothetical "containing object" is always the same as the
19867 declared type of the individual bit-field itself (for GCC anyway... the
19868 DWARF spec doesn't actually mandate this). Note that it is the size (in
19869 bytes) of the hypothetical "containing object" which will be given in the
19870 DW_AT_byte_size attribute for this bit-field. (See the
19871 `byte_size_attribute' function below.) It is also used when calculating the
19872 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19873 function below.)
19875 CTX is required: see the comment for VLR_CONTEXT. */
19877 static void
19878 add_data_member_location_attribute (dw_die_ref die,
19879 tree decl,
19880 struct vlr_context *ctx)
19882 HOST_WIDE_INT offset;
19883 dw_loc_descr_ref loc_descr = 0;
19885 if (TREE_CODE (decl) == TREE_BINFO)
19887 /* We're working on the TAG_inheritance for a base class. */
19888 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19890 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19891 aren't at a fixed offset from all (sub)objects of the same
19892 type. We need to extract the appropriate offset from our
19893 vtable. The following dwarf expression means
19895 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19897 This is specific to the V3 ABI, of course. */
19899 dw_loc_descr_ref tmp;
19901 /* Make a copy of the object address. */
19902 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19903 add_loc_descr (&loc_descr, tmp);
19905 /* Extract the vtable address. */
19906 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19907 add_loc_descr (&loc_descr, tmp);
19909 /* Calculate the address of the offset. */
19910 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19911 gcc_assert (offset < 0);
19913 tmp = int_loc_descriptor (-offset);
19914 add_loc_descr (&loc_descr, tmp);
19915 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19916 add_loc_descr (&loc_descr, tmp);
19918 /* Extract the offset. */
19919 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19920 add_loc_descr (&loc_descr, tmp);
19922 /* Add it to the object address. */
19923 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19924 add_loc_descr (&loc_descr, tmp);
19926 else
19927 offset = tree_to_shwi (BINFO_OFFSET (decl));
19929 else
19931 loc_descr = field_byte_offset (decl, ctx, &offset);
19933 if (!loc_descr)
19936 /* If loc_descr is available, then we know the offset is dynamic. */
19937 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
19939 loc_descr = NULL;
19940 offset = 0;
19943 /* Data member location evaluation starts with the base address on the
19944 stack. Compute the field offset and add it to this base address. */
19945 else
19946 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19949 if (!loc_descr)
19951 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19952 e.g. GDB only added support to it in November 2016. For DWARF5
19953 we need newer debug info consumers anyway. We might change this
19954 to dwarf_version >= 4 once most consumers catched up. */
19955 if (dwarf_version >= 5
19956 && TREE_CODE (decl) == FIELD_DECL
19957 && DECL_BIT_FIELD_TYPE (decl)
19958 && (ctx->variant_part_offset == NULL_TREE
19959 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19961 tree off = bit_position (decl);
19962 if (ctx->variant_part_offset)
19963 off = bit_from_pos (ctx->variant_part_offset, off);
19964 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19966 remove_AT (die, DW_AT_byte_size);
19967 remove_AT (die, DW_AT_bit_offset);
19968 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19969 return;
19972 if (dwarf_version > 2)
19974 /* Don't need to output a location expression, just the constant. */
19975 if (offset < 0)
19976 add_AT_int (die, DW_AT_data_member_location, offset);
19977 else
19978 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19979 return;
19981 else
19983 enum dwarf_location_atom op;
19985 /* The DWARF2 standard says that we should assume that the structure
19986 address is already on the stack, so we can specify a structure
19987 field address by using DW_OP_plus_uconst. */
19988 op = DW_OP_plus_uconst;
19989 loc_descr = new_loc_descr (op, offset, 0);
19993 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19996 /* Writes integer values to dw_vec_const array. */
19998 static void
19999 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
20001 while (size != 0)
20003 *dest++ = val & 0xff;
20004 val >>= 8;
20005 --size;
20009 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20011 static HOST_WIDE_INT
20012 extract_int (const unsigned char *src, unsigned int size)
20014 HOST_WIDE_INT val = 0;
20016 src += size;
20017 while (size != 0)
20019 val <<= 8;
20020 val |= *--src & 0xff;
20021 --size;
20023 return val;
20026 /* Writes wide_int values to dw_vec_const array. */
20028 static void
20029 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20031 int i;
20033 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20035 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20036 return;
20039 /* We'd have to extend this code to support odd sizes. */
20040 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20042 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20044 if (WORDS_BIG_ENDIAN)
20045 for (i = n - 1; i >= 0; i--)
20047 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20048 dest += sizeof (HOST_WIDE_INT);
20050 else
20051 for (i = 0; i < n; i++)
20053 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20054 dest += sizeof (HOST_WIDE_INT);
20058 /* Writes floating point values to dw_vec_const array. */
20060 static unsigned
20061 insert_float (const_rtx rtl, unsigned char *array)
20063 long val[4];
20064 int i;
20065 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20067 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20069 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20070 if (GET_MODE_SIZE (mode) < 4)
20072 gcc_assert (GET_MODE_SIZE (mode) == 2);
20073 insert_int (val[0], 2, array);
20074 return 2;
20077 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20079 insert_int (val[i], 4, array);
20080 array += 4;
20082 return 4;
20085 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20086 does not have a "location" either in memory or in a register. These
20087 things can arise in GNU C when a constant is passed as an actual parameter
20088 to an inlined function. They can also arise in C++ where declared
20089 constants do not necessarily get memory "homes". */
20091 static bool
20092 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20094 scalar_mode int_mode;
20096 switch (GET_CODE (rtl))
20098 case CONST_INT:
20100 HOST_WIDE_INT val = INTVAL (rtl);
20102 if (val < 0)
20103 add_AT_int (die, DW_AT_const_value, val);
20104 else
20105 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20107 return true;
20109 case CONST_WIDE_INT:
20110 if (is_int_mode (mode, &int_mode)
20111 && (GET_MODE_PRECISION (int_mode)
20112 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20114 wide_int w = rtx_mode_t (rtl, int_mode);
20115 add_AT_wide (die, DW_AT_const_value, w);
20116 return true;
20118 return false;
20120 case CONST_DOUBLE:
20121 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20122 floating-point constant. A CONST_DOUBLE is used whenever the
20123 constant requires more than one word in order to be adequately
20124 represented. */
20125 if (TARGET_SUPPORTS_WIDE_INT == 0
20126 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20127 add_AT_double (die, DW_AT_const_value,
20128 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20129 else
20131 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20132 unsigned int length = GET_MODE_SIZE (mode);
20133 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20134 unsigned int elt_size = insert_float (rtl, array);
20136 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20137 array);
20139 return true;
20141 case CONST_VECTOR:
20143 unsigned int length;
20144 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20145 return false;
20147 machine_mode mode = GET_MODE (rtl);
20148 /* The combination of a length and byte elt_size doesn't extend
20149 naturally to boolean vectors, where several elements are packed
20150 into the same byte. */
20151 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20152 return false;
20154 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20155 unsigned char *array
20156 = ggc_vec_alloc<unsigned char> (length * elt_size);
20157 unsigned int i;
20158 unsigned char *p;
20159 machine_mode imode = GET_MODE_INNER (mode);
20161 switch (GET_MODE_CLASS (mode))
20163 case MODE_VECTOR_INT:
20164 for (i = 0, p = array; i < length; i++, p += elt_size)
20166 rtx elt = CONST_VECTOR_ELT (rtl, i);
20167 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20169 break;
20171 case MODE_VECTOR_FLOAT:
20172 for (i = 0, p = array; i < length; i++, p += elt_size)
20174 rtx elt = CONST_VECTOR_ELT (rtl, i);
20175 insert_float (elt, p);
20177 break;
20179 default:
20180 gcc_unreachable ();
20183 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20185 return true;
20187 case CONST_STRING:
20188 if (dwarf_version >= 4 || !dwarf_strict)
20190 dw_loc_descr_ref loc_result;
20191 resolve_one_addr (&rtl);
20192 rtl_addr:
20193 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20194 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20195 add_AT_loc (die, DW_AT_location, loc_result);
20196 vec_safe_push (used_rtx_array, rtl);
20197 return true;
20199 return false;
20201 case CONST:
20202 if (CONSTANT_P (XEXP (rtl, 0)))
20203 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20204 /* FALLTHROUGH */
20205 case SYMBOL_REF:
20206 if (!const_ok_for_output (rtl))
20207 return false;
20208 /* FALLTHROUGH */
20209 case LABEL_REF:
20210 if (dwarf_version >= 4 || !dwarf_strict)
20211 goto rtl_addr;
20212 return false;
20214 case PLUS:
20215 /* In cases where an inlined instance of an inline function is passed
20216 the address of an `auto' variable (which is local to the caller) we
20217 can get a situation where the DECL_RTL of the artificial local
20218 variable (for the inlining) which acts as a stand-in for the
20219 corresponding formal parameter (of the inline function) will look
20220 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20221 exactly a compile-time constant expression, but it isn't the address
20222 of the (artificial) local variable either. Rather, it represents the
20223 *value* which the artificial local variable always has during its
20224 lifetime. We currently have no way to represent such quasi-constant
20225 values in Dwarf, so for now we just punt and generate nothing. */
20226 return false;
20228 case HIGH:
20229 case CONST_FIXED:
20230 case MINUS:
20231 case SIGN_EXTEND:
20232 case ZERO_EXTEND:
20233 case CONST_POLY_INT:
20234 return false;
20236 case MEM:
20237 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20238 && MEM_READONLY_P (rtl)
20239 && GET_MODE (rtl) == BLKmode)
20241 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20242 return true;
20244 return false;
20246 default:
20247 /* No other kinds of rtx should be possible here. */
20248 gcc_unreachable ();
20252 /* Determine whether the evaluation of EXPR references any variables
20253 or functions which aren't otherwise used (and therefore may not be
20254 output). */
20255 static tree
20256 reference_to_unused (tree * tp, int * walk_subtrees,
20257 void * data ATTRIBUTE_UNUSED)
20259 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20260 *walk_subtrees = 0;
20262 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20263 && ! TREE_ASM_WRITTEN (*tp))
20264 return *tp;
20265 /* ??? The C++ FE emits debug information for using decls, so
20266 putting gcc_unreachable here falls over. See PR31899. For now
20267 be conservative. */
20268 else if (!symtab->global_info_ready && VAR_P (*tp))
20269 return *tp;
20270 else if (VAR_P (*tp))
20272 varpool_node *node = varpool_node::get (*tp);
20273 if (!node || !node->definition)
20274 return *tp;
20276 else if (TREE_CODE (*tp) == FUNCTION_DECL
20277 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20279 /* The call graph machinery must have finished analyzing,
20280 optimizing and gimplifying the CU by now.
20281 So if *TP has no call graph node associated
20282 to it, it means *TP will not be emitted. */
20283 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20284 return *tp;
20286 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20287 return *tp;
20289 return NULL_TREE;
20292 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20293 for use in a later add_const_value_attribute call. */
20295 static rtx
20296 rtl_for_decl_init (tree init, tree type)
20298 rtx rtl = NULL_RTX;
20300 STRIP_NOPS (init);
20302 /* If a variable is initialized with a string constant without embedded
20303 zeros, build CONST_STRING. */
20304 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20306 tree enttype = TREE_TYPE (type);
20307 tree domain = TYPE_DOMAIN (type);
20308 scalar_int_mode mode;
20310 if (is_int_mode (TYPE_MODE (enttype), &mode)
20311 && GET_MODE_SIZE (mode) == 1
20312 && domain
20313 && TYPE_MAX_VALUE (domain)
20314 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20315 && integer_zerop (TYPE_MIN_VALUE (domain))
20316 && compare_tree_int (TYPE_MAX_VALUE (domain),
20317 TREE_STRING_LENGTH (init) - 1) == 0
20318 && ((size_t) TREE_STRING_LENGTH (init)
20319 == strlen (TREE_STRING_POINTER (init)) + 1))
20321 rtl = gen_rtx_CONST_STRING (VOIDmode,
20322 ggc_strdup (TREE_STRING_POINTER (init)));
20323 rtl = gen_rtx_MEM (BLKmode, rtl);
20324 MEM_READONLY_P (rtl) = 1;
20327 /* Other aggregates, and complex values, could be represented using
20328 CONCAT: FIXME! */
20329 else if (AGGREGATE_TYPE_P (type)
20330 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20331 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20332 || TREE_CODE (type) == COMPLEX_TYPE)
20334 /* Vectors only work if their mode is supported by the target.
20335 FIXME: generic vectors ought to work too. */
20336 else if (TREE_CODE (type) == VECTOR_TYPE
20337 && !VECTOR_MODE_P (TYPE_MODE (type)))
20339 /* If the initializer is something that we know will expand into an
20340 immediate RTL constant, expand it now. We must be careful not to
20341 reference variables which won't be output. */
20342 else if (initializer_constant_valid_p (init, type)
20343 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20345 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20346 possible. */
20347 if (TREE_CODE (type) == VECTOR_TYPE)
20348 switch (TREE_CODE (init))
20350 case VECTOR_CST:
20351 break;
20352 case CONSTRUCTOR:
20353 if (TREE_CONSTANT (init))
20355 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20356 bool constant_p = true;
20357 tree value;
20358 unsigned HOST_WIDE_INT ix;
20360 /* Even when ctor is constant, it might contain non-*_CST
20361 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20362 belong into VECTOR_CST nodes. */
20363 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20364 if (!CONSTANT_CLASS_P (value))
20366 constant_p = false;
20367 break;
20370 if (constant_p)
20372 init = build_vector_from_ctor (type, elts);
20373 break;
20376 /* FALLTHRU */
20378 default:
20379 return NULL;
20382 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20384 /* If expand_expr returns a MEM, it wasn't immediate. */
20385 gcc_assert (!rtl || !MEM_P (rtl));
20388 return rtl;
20391 /* Generate RTL for the variable DECL to represent its location. */
20393 static rtx
20394 rtl_for_decl_location (tree decl)
20396 rtx rtl;
20398 /* Here we have to decide where we are going to say the parameter "lives"
20399 (as far as the debugger is concerned). We only have a couple of
20400 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20402 DECL_RTL normally indicates where the parameter lives during most of the
20403 activation of the function. If optimization is enabled however, this
20404 could be either NULL or else a pseudo-reg. Both of those cases indicate
20405 that the parameter doesn't really live anywhere (as far as the code
20406 generation parts of GCC are concerned) during most of the function's
20407 activation. That will happen (for example) if the parameter is never
20408 referenced within the function.
20410 We could just generate a location descriptor here for all non-NULL
20411 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20412 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20413 where DECL_RTL is NULL or is a pseudo-reg.
20415 Note however that we can only get away with using DECL_INCOMING_RTL as
20416 a backup substitute for DECL_RTL in certain limited cases. In cases
20417 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20418 we can be sure that the parameter was passed using the same type as it is
20419 declared to have within the function, and that its DECL_INCOMING_RTL
20420 points us to a place where a value of that type is passed.
20422 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20423 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20424 because in these cases DECL_INCOMING_RTL points us to a value of some
20425 type which is *different* from the type of the parameter itself. Thus,
20426 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20427 such cases, the debugger would end up (for example) trying to fetch a
20428 `float' from a place which actually contains the first part of a
20429 `double'. That would lead to really incorrect and confusing
20430 output at debug-time.
20432 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20433 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20434 are a couple of exceptions however. On little-endian machines we can
20435 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20436 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20437 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20438 when (on a little-endian machine) a non-prototyped function has a
20439 parameter declared to be of type `short' or `char'. In such cases,
20440 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20441 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20442 passed `int' value. If the debugger then uses that address to fetch
20443 a `short' or a `char' (on a little-endian machine) the result will be
20444 the correct data, so we allow for such exceptional cases below.
20446 Note that our goal here is to describe the place where the given formal
20447 parameter lives during most of the function's activation (i.e. between the
20448 end of the prologue and the start of the epilogue). We'll do that as best
20449 as we can. Note however that if the given formal parameter is modified
20450 sometime during the execution of the function, then a stack backtrace (at
20451 debug-time) will show the function as having been called with the *new*
20452 value rather than the value which was originally passed in. This happens
20453 rarely enough that it is not a major problem, but it *is* a problem, and
20454 I'd like to fix it.
20456 A future version of dwarf2out.c may generate two additional attributes for
20457 any given DW_TAG_formal_parameter DIE which will describe the "passed
20458 type" and the "passed location" for the given formal parameter in addition
20459 to the attributes we now generate to indicate the "declared type" and the
20460 "active location" for each parameter. This additional set of attributes
20461 could be used by debuggers for stack backtraces. Separately, note that
20462 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20463 This happens (for example) for inlined-instances of inline function formal
20464 parameters which are never referenced. This really shouldn't be
20465 happening. All PARM_DECL nodes should get valid non-NULL
20466 DECL_INCOMING_RTL values. FIXME. */
20468 /* Use DECL_RTL as the "location" unless we find something better. */
20469 rtl = DECL_RTL_IF_SET (decl);
20471 /* When generating abstract instances, ignore everything except
20472 constants, symbols living in memory, and symbols living in
20473 fixed registers. */
20474 if (! reload_completed)
20476 if (rtl
20477 && (CONSTANT_P (rtl)
20478 || (MEM_P (rtl)
20479 && CONSTANT_P (XEXP (rtl, 0)))
20480 || (REG_P (rtl)
20481 && VAR_P (decl)
20482 && TREE_STATIC (decl))))
20484 rtl = targetm.delegitimize_address (rtl);
20485 return rtl;
20487 rtl = NULL_RTX;
20489 else if (TREE_CODE (decl) == PARM_DECL)
20491 if (rtl == NULL_RTX
20492 || is_pseudo_reg (rtl)
20493 || (MEM_P (rtl)
20494 && is_pseudo_reg (XEXP (rtl, 0))
20495 && DECL_INCOMING_RTL (decl)
20496 && MEM_P (DECL_INCOMING_RTL (decl))
20497 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20499 tree declared_type = TREE_TYPE (decl);
20500 tree passed_type = DECL_ARG_TYPE (decl);
20501 machine_mode dmode = TYPE_MODE (declared_type);
20502 machine_mode pmode = TYPE_MODE (passed_type);
20504 /* This decl represents a formal parameter which was optimized out.
20505 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20506 all cases where (rtl == NULL_RTX) just below. */
20507 if (dmode == pmode)
20508 rtl = DECL_INCOMING_RTL (decl);
20509 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20510 && SCALAR_INT_MODE_P (dmode)
20511 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20512 && DECL_INCOMING_RTL (decl))
20514 rtx inc = DECL_INCOMING_RTL (decl);
20515 if (REG_P (inc))
20516 rtl = inc;
20517 else if (MEM_P (inc))
20519 if (BYTES_BIG_ENDIAN)
20520 rtl = adjust_address_nv (inc, dmode,
20521 GET_MODE_SIZE (pmode)
20522 - GET_MODE_SIZE (dmode));
20523 else
20524 rtl = inc;
20529 /* If the parm was passed in registers, but lives on the stack, then
20530 make a big endian correction if the mode of the type of the
20531 parameter is not the same as the mode of the rtl. */
20532 /* ??? This is the same series of checks that are made in dbxout.c before
20533 we reach the big endian correction code there. It isn't clear if all
20534 of these checks are necessary here, but keeping them all is the safe
20535 thing to do. */
20536 else if (MEM_P (rtl)
20537 && XEXP (rtl, 0) != const0_rtx
20538 && ! CONSTANT_P (XEXP (rtl, 0))
20539 /* Not passed in memory. */
20540 && !MEM_P (DECL_INCOMING_RTL (decl))
20541 /* Not passed by invisible reference. */
20542 && (!REG_P (XEXP (rtl, 0))
20543 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20544 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20545 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20546 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20547 #endif
20549 /* Big endian correction check. */
20550 && BYTES_BIG_ENDIAN
20551 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20552 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20553 UNITS_PER_WORD))
20555 machine_mode addr_mode = get_address_mode (rtl);
20556 poly_int64 offset = (UNITS_PER_WORD
20557 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20559 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20560 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20563 else if (VAR_P (decl)
20564 && rtl
20565 && MEM_P (rtl)
20566 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20568 machine_mode addr_mode = get_address_mode (rtl);
20569 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20570 GET_MODE (rtl));
20572 /* If a variable is declared "register" yet is smaller than
20573 a register, then if we store the variable to memory, it
20574 looks like we're storing a register-sized value, when in
20575 fact we are not. We need to adjust the offset of the
20576 storage location to reflect the actual value's bytes,
20577 else gdb will not be able to display it. */
20578 if (maybe_ne (offset, 0))
20579 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20580 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20583 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20584 and will have been substituted directly into all expressions that use it.
20585 C does not have such a concept, but C++ and other languages do. */
20586 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20587 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20589 if (rtl)
20590 rtl = targetm.delegitimize_address (rtl);
20592 /* If we don't look past the constant pool, we risk emitting a
20593 reference to a constant pool entry that isn't referenced from
20594 code, and thus is not emitted. */
20595 if (rtl)
20596 rtl = avoid_constant_pool_reference (rtl);
20598 /* Try harder to get a rtl. If this symbol ends up not being emitted
20599 in the current CU, resolve_addr will remove the expression referencing
20600 it. */
20601 if (rtl == NULL_RTX
20602 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20603 && VAR_P (decl)
20604 && !DECL_EXTERNAL (decl)
20605 && TREE_STATIC (decl)
20606 && DECL_NAME (decl)
20607 && !DECL_HARD_REGISTER (decl)
20608 && DECL_MODE (decl) != VOIDmode)
20610 rtl = make_decl_rtl_for_debug (decl);
20611 if (!MEM_P (rtl)
20612 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20613 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20614 rtl = NULL_RTX;
20617 return rtl;
20620 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20621 returned. If so, the decl for the COMMON block is returned, and the
20622 value is the offset into the common block for the symbol. */
20624 static tree
20625 fortran_common (tree decl, HOST_WIDE_INT *value)
20627 tree val_expr, cvar;
20628 machine_mode mode;
20629 poly_int64 bitsize, bitpos;
20630 tree offset;
20631 HOST_WIDE_INT cbitpos;
20632 int unsignedp, reversep, volatilep = 0;
20634 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20635 it does not have a value (the offset into the common area), or if it
20636 is thread local (as opposed to global) then it isn't common, and shouldn't
20637 be handled as such. */
20638 if (!VAR_P (decl)
20639 || !TREE_STATIC (decl)
20640 || !DECL_HAS_VALUE_EXPR_P (decl)
20641 || !is_fortran ())
20642 return NULL_TREE;
20644 val_expr = DECL_VALUE_EXPR (decl);
20645 if (TREE_CODE (val_expr) != COMPONENT_REF)
20646 return NULL_TREE;
20648 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20649 &unsignedp, &reversep, &volatilep);
20651 if (cvar == NULL_TREE
20652 || !VAR_P (cvar)
20653 || DECL_ARTIFICIAL (cvar)
20654 || !TREE_PUBLIC (cvar)
20655 /* We don't expect to have to cope with variable offsets,
20656 since at present all static data must have a constant size. */
20657 || !bitpos.is_constant (&cbitpos))
20658 return NULL_TREE;
20660 *value = 0;
20661 if (offset != NULL)
20663 if (!tree_fits_shwi_p (offset))
20664 return NULL_TREE;
20665 *value = tree_to_shwi (offset);
20667 if (cbitpos != 0)
20668 *value += cbitpos / BITS_PER_UNIT;
20670 return cvar;
20673 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20674 data attribute for a variable or a parameter. We generate the
20675 DW_AT_const_value attribute only in those cases where the given variable
20676 or parameter does not have a true "location" either in memory or in a
20677 register. This can happen (for example) when a constant is passed as an
20678 actual argument in a call to an inline function. (It's possible that
20679 these things can crop up in other ways also.) Note that one type of
20680 constant value which can be passed into an inlined function is a constant
20681 pointer. This can happen for example if an actual argument in an inlined
20682 function call evaluates to a compile-time constant address.
20684 CACHE_P is true if it is worth caching the location list for DECL,
20685 so that future calls can reuse it rather than regenerate it from scratch.
20686 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20687 since we will need to refer to them each time the function is inlined. */
20689 static bool
20690 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20692 rtx rtl;
20693 dw_loc_list_ref list;
20694 var_loc_list *loc_list;
20695 cached_dw_loc_list *cache;
20697 if (early_dwarf)
20698 return false;
20700 if (TREE_CODE (decl) == ERROR_MARK)
20701 return false;
20703 if (get_AT (die, DW_AT_location)
20704 || get_AT (die, DW_AT_const_value))
20705 return true;
20707 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20708 || TREE_CODE (decl) == RESULT_DECL);
20710 /* Try to get some constant RTL for this decl, and use that as the value of
20711 the location. */
20713 rtl = rtl_for_decl_location (decl);
20714 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20715 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20716 return true;
20718 /* See if we have single element location list that is equivalent to
20719 a constant value. That way we are better to use add_const_value_attribute
20720 rather than expanding constant value equivalent. */
20721 loc_list = lookup_decl_loc (decl);
20722 if (loc_list
20723 && loc_list->first
20724 && loc_list->first->next == NULL
20725 && NOTE_P (loc_list->first->loc)
20726 && NOTE_VAR_LOCATION (loc_list->first->loc)
20727 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20729 struct var_loc_node *node;
20731 node = loc_list->first;
20732 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20733 if (GET_CODE (rtl) == EXPR_LIST)
20734 rtl = XEXP (rtl, 0);
20735 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20736 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20737 return true;
20739 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20740 list several times. See if we've already cached the contents. */
20741 list = NULL;
20742 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20743 cache_p = false;
20744 if (cache_p)
20746 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20747 if (cache)
20748 list = cache->loc_list;
20750 if (list == NULL)
20752 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20753 NULL);
20754 /* It is usually worth caching this result if the decl is from
20755 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20756 if (cache_p && list && list->dw_loc_next)
20758 cached_dw_loc_list **slot
20759 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20760 DECL_UID (decl),
20761 INSERT);
20762 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20763 cache->decl_id = DECL_UID (decl);
20764 cache->loc_list = list;
20765 *slot = cache;
20768 if (list)
20770 add_AT_location_description (die, DW_AT_location, list);
20771 return true;
20773 /* None of that worked, so it must not really have a location;
20774 try adding a constant value attribute from the DECL_INITIAL. */
20775 return tree_add_const_value_attribute_for_decl (die, decl);
20778 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20779 attribute is the const value T. */
20781 static bool
20782 tree_add_const_value_attribute (dw_die_ref die, tree t)
20784 tree init;
20785 tree type = TREE_TYPE (t);
20786 rtx rtl;
20788 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20789 return false;
20791 init = t;
20792 gcc_assert (!DECL_P (init));
20794 if (TREE_CODE (init) == INTEGER_CST)
20796 if (tree_fits_uhwi_p (init))
20798 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20799 return true;
20801 if (tree_fits_shwi_p (init))
20803 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20804 return true;
20807 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20808 symbols. */
20809 rtl = rtl_for_decl_init (init, type);
20810 if (rtl && !early_dwarf)
20811 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
20812 /* If the host and target are sane, try harder. */
20813 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20814 && initializer_constant_valid_p (init, type))
20816 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20817 if (size > 0 && (int) size == size)
20819 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20821 if (native_encode_initializer (init, array, size) == size)
20823 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20824 return true;
20826 ggc_free (array);
20829 return false;
20832 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20833 attribute is the const value of T, where T is an integral constant
20834 variable with static storage duration
20835 (so it can't be a PARM_DECL or a RESULT_DECL). */
20837 static bool
20838 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20841 if (!decl
20842 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20843 || (VAR_P (decl) && !TREE_STATIC (decl)))
20844 return false;
20846 if (TREE_READONLY (decl)
20847 && ! TREE_THIS_VOLATILE (decl)
20848 && DECL_INITIAL (decl))
20849 /* OK */;
20850 else
20851 return false;
20853 /* Don't add DW_AT_const_value if abstract origin already has one. */
20854 if (get_AT (var_die, DW_AT_const_value))
20855 return false;
20857 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20860 /* Convert the CFI instructions for the current function into a
20861 location list. This is used for DW_AT_frame_base when we targeting
20862 a dwarf2 consumer that does not support the dwarf3
20863 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20864 expressions. */
20866 static dw_loc_list_ref
20867 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20869 int ix;
20870 dw_fde_ref fde;
20871 dw_loc_list_ref list, *list_tail;
20872 dw_cfi_ref cfi;
20873 dw_cfa_location last_cfa, next_cfa;
20874 const char *start_label, *last_label, *section;
20875 dw_cfa_location remember;
20877 fde = cfun->fde;
20878 gcc_assert (fde != NULL);
20880 section = secname_for_decl (current_function_decl);
20881 list_tail = &list;
20882 list = NULL;
20884 memset (&next_cfa, 0, sizeof (next_cfa));
20885 next_cfa.reg = INVALID_REGNUM;
20886 remember = next_cfa;
20888 start_label = fde->dw_fde_begin;
20890 /* ??? Bald assumption that the CIE opcode list does not contain
20891 advance opcodes. */
20892 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20893 lookup_cfa_1 (cfi, &next_cfa, &remember);
20895 last_cfa = next_cfa;
20896 last_label = start_label;
20898 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20900 /* If the first partition contained no CFI adjustments, the
20901 CIE opcodes apply to the whole first partition. */
20902 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20903 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20904 list_tail =&(*list_tail)->dw_loc_next;
20905 start_label = last_label = fde->dw_fde_second_begin;
20908 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20910 switch (cfi->dw_cfi_opc)
20912 case DW_CFA_set_loc:
20913 case DW_CFA_advance_loc1:
20914 case DW_CFA_advance_loc2:
20915 case DW_CFA_advance_loc4:
20916 if (!cfa_equal_p (&last_cfa, &next_cfa))
20918 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20919 start_label, 0, last_label, 0, section);
20921 list_tail = &(*list_tail)->dw_loc_next;
20922 last_cfa = next_cfa;
20923 start_label = last_label;
20925 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20926 break;
20928 case DW_CFA_advance_loc:
20929 /* The encoding is complex enough that we should never emit this. */
20930 gcc_unreachable ();
20932 default:
20933 lookup_cfa_1 (cfi, &next_cfa, &remember);
20934 break;
20936 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20938 if (!cfa_equal_p (&last_cfa, &next_cfa))
20940 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20941 start_label, 0, last_label, 0, section);
20943 list_tail = &(*list_tail)->dw_loc_next;
20944 last_cfa = next_cfa;
20945 start_label = last_label;
20947 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20948 start_label, 0, fde->dw_fde_end, 0, section);
20949 list_tail = &(*list_tail)->dw_loc_next;
20950 start_label = last_label = fde->dw_fde_second_begin;
20954 if (!cfa_equal_p (&last_cfa, &next_cfa))
20956 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20957 start_label, 0, last_label, 0, section);
20958 list_tail = &(*list_tail)->dw_loc_next;
20959 start_label = last_label;
20962 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20963 start_label, 0,
20964 fde->dw_fde_second_begin
20965 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20966 section);
20968 maybe_gen_llsym (list);
20970 return list;
20973 /* Compute a displacement from the "steady-state frame pointer" to the
20974 frame base (often the same as the CFA), and store it in
20975 frame_pointer_fb_offset. OFFSET is added to the displacement
20976 before the latter is negated. */
20978 static void
20979 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20981 rtx reg, elim;
20983 #ifdef FRAME_POINTER_CFA_OFFSET
20984 reg = frame_pointer_rtx;
20985 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20986 #else
20987 reg = arg_pointer_rtx;
20988 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20989 #endif
20991 elim = (ira_use_lra_p
20992 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20993 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20994 elim = strip_offset_and_add (elim, &offset);
20996 frame_pointer_fb_offset = -offset;
20998 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20999 in which to eliminate. This is because it's stack pointer isn't
21000 directly accessible as a register within the ISA. To work around
21001 this, assume that while we cannot provide a proper value for
21002 frame_pointer_fb_offset, we won't need one either. We can use
21003 hard frame pointer in debug info even if frame pointer isn't used
21004 since hard frame pointer in debug info is encoded with DW_OP_fbreg
21005 which uses the DW_AT_frame_base attribute, not hard frame pointer
21006 directly. */
21007 frame_pointer_fb_offset_valid
21008 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21011 /* Generate a DW_AT_name attribute given some string value to be included as
21012 the value of the attribute. */
21014 void
21015 add_name_attribute (dw_die_ref die, const char *name_string)
21017 if (name_string != NULL && *name_string != 0)
21019 if (demangle_name_func)
21020 name_string = (*demangle_name_func) (name_string);
21022 add_AT_string (die, DW_AT_name, name_string);
21026 /* Generate a DW_AT_name attribute given some string value representing a
21027 file or filepath to be included as value of the attribute. */
21028 static void
21029 add_filename_attribute (dw_die_ref die, const char *name_string)
21031 if (name_string != NULL && *name_string != 0)
21032 add_filepath_AT_string (die, DW_AT_name, name_string);
21035 /* Generate a DW_AT_description attribute given some string value to be included
21036 as the value of the attribute. */
21038 static void
21039 add_desc_attribute (dw_die_ref die, const char *name_string)
21041 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21042 return;
21044 if (name_string == NULL || *name_string == 0)
21045 return;
21047 if (demangle_name_func)
21048 name_string = (*demangle_name_func) (name_string);
21050 add_AT_string (die, DW_AT_description, name_string);
21053 /* Generate a DW_AT_description attribute given some decl to be included
21054 as the value of the attribute. */
21056 static void
21057 add_desc_attribute (dw_die_ref die, tree decl)
21059 tree decl_name;
21061 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21062 return;
21064 if (decl == NULL_TREE || !DECL_P (decl))
21065 return;
21066 decl_name = DECL_NAME (decl);
21068 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21070 const char *name = dwarf2_name (decl, 0);
21071 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21073 else
21075 char *desc = print_generic_expr_to_str (decl);
21076 add_desc_attribute (die, desc);
21077 free (desc);
21081 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21082 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21083 of TYPE accordingly.
21085 ??? This is a temporary measure until after we're able to generate
21086 regular DWARF for the complex Ada type system. */
21088 static void
21089 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21090 dw_die_ref context_die)
21092 tree dtype;
21093 dw_die_ref dtype_die;
21095 if (!lang_hooks.types.descriptive_type)
21096 return;
21098 dtype = lang_hooks.types.descriptive_type (type);
21099 if (!dtype)
21100 return;
21102 dtype_die = lookup_type_die (dtype);
21103 if (!dtype_die)
21105 gen_type_die (dtype, context_die);
21106 dtype_die = lookup_type_die (dtype);
21107 gcc_assert (dtype_die);
21110 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21113 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21115 static const char *
21116 comp_dir_string (void)
21118 const char *wd;
21119 char *wd_plus_sep = NULL;
21120 static const char *cached_wd = NULL;
21122 if (cached_wd != NULL)
21123 return cached_wd;
21125 wd = get_src_pwd ();
21126 if (wd == NULL)
21127 return NULL;
21129 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21131 size_t wdlen = strlen (wd);
21132 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21133 strcpy (wd_plus_sep, wd);
21134 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21135 wd_plus_sep [wdlen + 1] = 0;
21136 wd = wd_plus_sep;
21139 cached_wd = remap_debug_filename (wd);
21141 /* remap_debug_filename can just pass through wd or return a new gc string.
21142 These two types can't be both stored in a GTY(())-tagged string, but since
21143 the cached value lives forever just copy it if needed. */
21144 if (cached_wd != wd)
21146 cached_wd = xstrdup (cached_wd);
21147 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21148 free (wd_plus_sep);
21151 return cached_wd;
21154 /* Generate a DW_AT_comp_dir attribute for DIE. */
21156 static void
21157 add_comp_dir_attribute (dw_die_ref die)
21159 const char * wd = comp_dir_string ();
21160 if (wd != NULL)
21161 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21164 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21165 pointer computation, ...), output a representation for that bound according
21166 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21167 loc_list_from_tree for the meaning of CONTEXT. */
21169 static void
21170 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21171 int forms, struct loc_descr_context *context)
21173 dw_die_ref context_die, decl_die = NULL;
21174 dw_loc_list_ref list;
21175 bool strip_conversions = true;
21176 bool placeholder_seen = false;
21178 while (strip_conversions)
21179 switch (TREE_CODE (value))
21181 case ERROR_MARK:
21182 case SAVE_EXPR:
21183 return;
21185 CASE_CONVERT:
21186 case VIEW_CONVERT_EXPR:
21187 value = TREE_OPERAND (value, 0);
21188 break;
21190 default:
21191 strip_conversions = false;
21192 break;
21195 /* If possible and permitted, output the attribute as a constant. */
21196 if ((forms & dw_scalar_form_constant) != 0
21197 && TREE_CODE (value) == INTEGER_CST)
21199 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21201 /* If HOST_WIDE_INT is big enough then represent the bound as
21202 a constant value. We need to choose a form based on
21203 whether the type is signed or unsigned. We cannot just
21204 call add_AT_unsigned if the value itself is positive
21205 (add_AT_unsigned might add the unsigned value encoded as
21206 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21207 bounds type and then sign extend any unsigned values found
21208 for signed types. This is needed only for
21209 DW_AT_{lower,upper}_bound, since for most other attributes,
21210 consumers will treat DW_FORM_data[1248] as unsigned values,
21211 regardless of the underlying type. */
21212 if (prec <= HOST_BITS_PER_WIDE_INT
21213 || tree_fits_uhwi_p (value))
21215 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21216 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21217 else
21218 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21220 else if (dwarf_version >= 5
21221 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21222 /* Otherwise represent the bound as an unsigned value with
21223 the precision of its type. The precision and signedness
21224 of the type will be necessary to re-interpret it
21225 unambiguously. */
21226 add_AT_wide (die, attr, wi::to_wide (value));
21227 else
21229 rtx v = immed_wide_int_const (wi::to_wide (value),
21230 TYPE_MODE (TREE_TYPE (value)));
21231 dw_loc_descr_ref loc
21232 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21233 VAR_INIT_STATUS_INITIALIZED);
21234 if (loc)
21235 add_AT_loc (die, attr, loc);
21237 return;
21240 /* Otherwise, if it's possible and permitted too, output a reference to
21241 another DIE. */
21242 if ((forms & dw_scalar_form_reference) != 0)
21244 tree decl = NULL_TREE;
21246 /* Some type attributes reference an outer type. For instance, the upper
21247 bound of an array may reference an embedding record (this happens in
21248 Ada). */
21249 if (TREE_CODE (value) == COMPONENT_REF
21250 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21251 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21252 decl = TREE_OPERAND (value, 1);
21254 else if (VAR_P (value)
21255 || TREE_CODE (value) == PARM_DECL
21256 || TREE_CODE (value) == RESULT_DECL)
21257 decl = value;
21259 if (decl != NULL_TREE)
21261 decl_die = lookup_decl_die (decl);
21263 /* ??? Can this happen, or should the variable have been bound
21264 first? Probably it can, since I imagine that we try to create
21265 the types of parameters in the order in which they exist in
21266 the list, and won't have created a forward reference to a
21267 later parameter. */
21268 if (decl_die != NULL)
21270 if (get_AT (decl_die, DW_AT_location)
21271 || get_AT (decl_die, DW_AT_data_member_location)
21272 || get_AT (decl_die, DW_AT_data_bit_offset)
21273 || get_AT (decl_die, DW_AT_const_value))
21275 add_AT_die_ref (die, attr, decl_die);
21276 return;
21282 /* Last chance: try to create a stack operation procedure to evaluate the
21283 value. Do nothing if even that is not possible or permitted. */
21284 if ((forms & dw_scalar_form_exprloc) == 0)
21285 return;
21287 list = loc_list_from_tree (value, 2, context);
21288 if (context && context->placeholder_arg)
21290 placeholder_seen = context->placeholder_seen;
21291 context->placeholder_seen = false;
21293 if (list == NULL || single_element_loc_list_p (list))
21295 /* If this attribute is not a reference nor constant, it is
21296 a DWARF expression rather than location description. For that
21297 loc_list_from_tree (value, 0, &context) is needed. */
21298 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21299 if (list2 && single_element_loc_list_p (list2))
21301 if (placeholder_seen)
21303 struct dwarf_procedure_info dpi;
21304 dpi.fndecl = NULL_TREE;
21305 dpi.args_count = 1;
21306 if (!resolve_args_picking (list2->expr, 1, &dpi))
21307 return;
21309 add_AT_loc (die, attr, list2->expr);
21310 return;
21314 /* If that failed to give a single element location list, fall back to
21315 outputting this as a reference... still if permitted. */
21316 if (list == NULL
21317 || (forms & dw_scalar_form_reference) == 0
21318 || placeholder_seen)
21319 return;
21321 if (!decl_die)
21323 if (current_function_decl == 0)
21324 context_die = comp_unit_die ();
21325 else
21326 context_die = lookup_decl_die (current_function_decl);
21328 decl_die = new_die (DW_TAG_variable, context_die, value);
21329 add_AT_flag (decl_die, DW_AT_artificial, 1);
21330 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21331 context_die);
21334 add_AT_location_description (decl_die, DW_AT_location, list);
21335 add_AT_die_ref (die, attr, decl_die);
21338 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21339 default. */
21341 static int
21342 lower_bound_default (void)
21344 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21346 case DW_LANG_C:
21347 case DW_LANG_C89:
21348 case DW_LANG_C99:
21349 case DW_LANG_C11:
21350 case DW_LANG_C_plus_plus:
21351 case DW_LANG_C_plus_plus_11:
21352 case DW_LANG_C_plus_plus_14:
21353 case DW_LANG_ObjC:
21354 case DW_LANG_ObjC_plus_plus:
21355 return 0;
21356 case DW_LANG_Fortran77:
21357 case DW_LANG_Fortran90:
21358 case DW_LANG_Fortran95:
21359 case DW_LANG_Fortran03:
21360 case DW_LANG_Fortran08:
21361 return 1;
21362 case DW_LANG_UPC:
21363 case DW_LANG_D:
21364 case DW_LANG_Python:
21365 return dwarf_version >= 4 ? 0 : -1;
21366 case DW_LANG_Ada95:
21367 case DW_LANG_Ada83:
21368 case DW_LANG_Cobol74:
21369 case DW_LANG_Cobol85:
21370 case DW_LANG_Modula2:
21371 case DW_LANG_PLI:
21372 return dwarf_version >= 4 ? 1 : -1;
21373 default:
21374 return -1;
21378 /* Given a tree node describing an array bound (either lower or upper) output
21379 a representation for that bound. */
21381 static void
21382 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21383 tree bound, struct loc_descr_context *context)
21385 int dflt;
21387 while (1)
21388 switch (TREE_CODE (bound))
21390 /* Strip all conversions. */
21391 CASE_CONVERT:
21392 case VIEW_CONVERT_EXPR:
21393 bound = TREE_OPERAND (bound, 0);
21394 break;
21396 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21397 are even omitted when they are the default. */
21398 case INTEGER_CST:
21399 /* If the value for this bound is the default one, we can even omit the
21400 attribute. */
21401 if (bound_attr == DW_AT_lower_bound
21402 && tree_fits_shwi_p (bound)
21403 && (dflt = lower_bound_default ()) != -1
21404 && tree_to_shwi (bound) == dflt)
21405 return;
21407 /* FALLTHRU */
21409 default:
21410 /* Let GNAT encodings do the magic for self-referential bounds. */
21411 if (is_ada ()
21412 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21413 && contains_placeholder_p (bound))
21414 return;
21416 add_scalar_info (subrange_die, bound_attr, bound,
21417 dw_scalar_form_constant
21418 | dw_scalar_form_exprloc
21419 | dw_scalar_form_reference,
21420 context);
21421 return;
21425 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21426 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21428 This function reuses previously set type and bound information if
21429 available. */
21431 static void
21432 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21434 dw_die_ref child = type_die->die_child;
21435 struct array_descr_info info;
21436 int dimension_number;
21438 if (lang_hooks.types.get_array_descr_info)
21440 memset (&info, 0, sizeof (info));
21441 if (lang_hooks.types.get_array_descr_info (type, &info))
21442 /* Fortran sometimes emits array types with no dimension. */
21443 gcc_assert (info.ndimensions >= 0
21444 && info.ndimensions
21445 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21447 else
21448 info.ndimensions = 0;
21450 for (dimension_number = 0;
21451 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21452 type = TREE_TYPE (type), dimension_number++)
21454 tree domain = TYPE_DOMAIN (type);
21456 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21457 break;
21459 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21460 and (in GNU C only) variable bounds. Handle all three forms
21461 here. */
21463 /* Find and reuse a previously generated DW_TAG_subrange_type if
21464 available.
21466 For multi-dimensional arrays, as we iterate through the
21467 various dimensions in the enclosing for loop above, we also
21468 iterate through the DIE children and pick at each
21469 DW_TAG_subrange_type previously generated (if available).
21470 Each child DW_TAG_subrange_type DIE describes the range of
21471 the current dimension. At this point we should have as many
21472 DW_TAG_subrange_type's as we have dimensions in the
21473 array. */
21474 dw_die_ref subrange_die = NULL;
21475 if (child)
21476 while (1)
21478 child = child->die_sib;
21479 if (child->die_tag == DW_TAG_subrange_type)
21480 subrange_die = child;
21481 if (child == type_die->die_child)
21483 /* If we wrapped around, stop looking next time. */
21484 child = NULL;
21485 break;
21487 if (child->die_tag == DW_TAG_subrange_type)
21488 break;
21490 if (!subrange_die)
21491 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21493 if (domain)
21495 /* We have an array type with specified bounds. */
21496 tree lower = TYPE_MIN_VALUE (domain);
21497 tree upper = TYPE_MAX_VALUE (domain);
21498 tree index_type = TREE_TYPE (domain);
21500 if (dimension_number <= info.ndimensions - 1)
21502 lower = info.dimen[dimension_number].lower_bound;
21503 upper = info.dimen[dimension_number].upper_bound;
21504 index_type = info.dimen[dimension_number].bounds_type;
21507 /* Define the index type. */
21508 if (index_type && !get_AT (subrange_die, DW_AT_type))
21509 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21510 false, type_die);
21512 /* ??? If upper is NULL, the array has unspecified length,
21513 but it does have a lower bound. This happens with Fortran
21514 dimension arr(N:*)
21515 Since the debugger is definitely going to need to know N
21516 to produce useful results, go ahead and output the lower
21517 bound solo, and hope the debugger can cope. */
21519 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21520 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21522 if (!get_AT (subrange_die, DW_AT_upper_bound)
21523 && !get_AT (subrange_die, DW_AT_count))
21525 if (upper)
21526 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21527 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21528 /* Zero-length array. */
21529 add_bound_info (subrange_die, DW_AT_count,
21530 build_int_cst (TREE_TYPE (lower), 0), NULL);
21534 /* Otherwise we have an array type with an unspecified length. The
21535 DWARF-2 spec does not say how to handle this; let's just leave out the
21536 bounds. */
21540 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21542 static void
21543 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21545 dw_die_ref decl_die;
21546 HOST_WIDE_INT size;
21548 switch (TREE_CODE (tree_node))
21550 case ERROR_MARK:
21551 size = 0;
21552 break;
21553 case ENUMERAL_TYPE:
21554 case RECORD_TYPE:
21555 case UNION_TYPE:
21556 case QUAL_UNION_TYPE:
21557 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21558 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21560 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21561 return;
21563 size = int_size_in_bytes (tree_node);
21564 break;
21565 case FIELD_DECL:
21566 /* For a data member of a struct or union, the DW_AT_byte_size is
21567 generally given as the number of bytes normally allocated for an
21568 object of the *declared* type of the member itself. This is true
21569 even for bit-fields. */
21570 size = int_size_in_bytes (field_type (tree_node));
21571 break;
21572 default:
21573 gcc_unreachable ();
21576 /* Note that `size' might be -1 when we get to this point. If it is, that
21577 indicates that the byte size of the entity in question is variable. */
21578 if (size >= 0)
21579 add_AT_unsigned (die, DW_AT_byte_size, size);
21581 /* Support for dynamically-sized objects was introduced in DWARF3. */
21582 else if (TYPE_P (tree_node)
21583 && (dwarf_version >= 3 || !dwarf_strict)
21584 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21586 struct loc_descr_context ctx = {
21587 const_cast<tree> (tree_node), /* context_type */
21588 NULL_TREE, /* base_decl */
21589 NULL, /* dpi */
21590 false, /* placeholder_arg */
21591 false, /* placeholder_seen */
21592 false /* strict_signedness */
21595 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21596 add_scalar_info (die, DW_AT_byte_size, tree_size,
21597 dw_scalar_form_constant
21598 | dw_scalar_form_exprloc
21599 | dw_scalar_form_reference,
21600 &ctx);
21604 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21605 alignment. */
21607 static void
21608 add_alignment_attribute (dw_die_ref die, tree tree_node)
21610 if (dwarf_version < 5 && dwarf_strict)
21611 return;
21613 unsigned align;
21615 if (DECL_P (tree_node))
21617 if (!DECL_USER_ALIGN (tree_node))
21618 return;
21620 align = DECL_ALIGN_UNIT (tree_node);
21622 else if (TYPE_P (tree_node))
21624 if (!TYPE_USER_ALIGN (tree_node))
21625 return;
21627 align = TYPE_ALIGN_UNIT (tree_node);
21629 else
21630 gcc_unreachable ();
21632 add_AT_unsigned (die, DW_AT_alignment, align);
21635 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21636 which specifies the distance in bits from the highest order bit of the
21637 "containing object" for the bit-field to the highest order bit of the
21638 bit-field itself.
21640 For any given bit-field, the "containing object" is a hypothetical object
21641 (of some integral or enum type) within which the given bit-field lives. The
21642 type of this hypothetical "containing object" is always the same as the
21643 declared type of the individual bit-field itself. The determination of the
21644 exact location of the "containing object" for a bit-field is rather
21645 complicated. It's handled by the `field_byte_offset' function (above).
21647 Note that it is the size (in bytes) of the hypothetical "containing object"
21648 which will be given in the DW_AT_byte_size attribute for this bit-field.
21649 (See `byte_size_attribute' above). */
21651 static inline void
21652 add_bit_offset_attribute (dw_die_ref die, tree decl)
21654 HOST_WIDE_INT object_offset_in_bytes;
21655 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21656 HOST_WIDE_INT bitpos_int;
21657 HOST_WIDE_INT highest_order_object_bit_offset;
21658 HOST_WIDE_INT highest_order_field_bit_offset;
21659 HOST_WIDE_INT bit_offset;
21661 /* The containing object is within the DECL_CONTEXT. */
21662 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21664 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21666 /* Must be a field and a bit field. */
21667 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21669 /* We can't yet handle bit-fields whose offsets are variable, so if we
21670 encounter such things, just return without generating any attribute
21671 whatsoever. Likewise for variable or too large size. */
21672 if (! tree_fits_shwi_p (bit_position (decl))
21673 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21674 return;
21676 bitpos_int = int_bit_position (decl);
21678 /* Note that the bit offset is always the distance (in bits) from the
21679 highest-order bit of the "containing object" to the highest-order bit of
21680 the bit-field itself. Since the "high-order end" of any object or field
21681 is different on big-endian and little-endian machines, the computation
21682 below must take account of these differences. */
21683 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21684 highest_order_field_bit_offset = bitpos_int;
21686 if (! BYTES_BIG_ENDIAN)
21688 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21689 highest_order_object_bit_offset +=
21690 simple_type_size_in_bits (original_type);
21693 bit_offset
21694 = (! BYTES_BIG_ENDIAN
21695 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21696 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21698 if (bit_offset < 0)
21699 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21700 else
21701 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21704 /* For a FIELD_DECL node which represents a bit field, output an attribute
21705 which specifies the length in bits of the given field. */
21707 static inline void
21708 add_bit_size_attribute (dw_die_ref die, tree decl)
21710 /* Must be a field and a bit field. */
21711 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21712 && DECL_BIT_FIELD_TYPE (decl));
21714 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21715 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21718 /* If the compiled language is ANSI C, then add a 'prototyped'
21719 attribute, if arg types are given for the parameters of a function. */
21721 static inline void
21722 add_prototyped_attribute (dw_die_ref die, tree func_type)
21724 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21726 case DW_LANG_C:
21727 case DW_LANG_C89:
21728 case DW_LANG_C99:
21729 case DW_LANG_C11:
21730 case DW_LANG_ObjC:
21731 if (prototype_p (func_type))
21732 add_AT_flag (die, DW_AT_prototyped, 1);
21733 break;
21734 default:
21735 break;
21739 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21740 by looking in the type declaration, the object declaration equate table or
21741 the block mapping. */
21743 static inline void
21744 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21746 dw_die_ref origin_die = NULL;
21748 /* For late LTO debug output we want to refer directly to the abstract
21749 DIE in the early debug rather to the possibly existing concrete
21750 instance and avoid creating that just for this purpose. */
21751 sym_off_pair *desc;
21752 if (in_lto_p
21753 && external_die_map
21754 && (desc = external_die_map->get (origin)))
21756 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21757 desc->sym, desc->off);
21758 return;
21761 if (DECL_P (origin))
21762 origin_die = lookup_decl_die (origin);
21763 else if (TYPE_P (origin))
21764 origin_die = lookup_type_die (origin);
21765 else if (TREE_CODE (origin) == BLOCK)
21766 origin_die = lookup_block_die (origin);
21768 /* XXX: Functions that are never lowered don't always have correct block
21769 trees (in the case of java, they simply have no block tree, in some other
21770 languages). For these functions, there is nothing we can really do to
21771 output correct debug info for inlined functions in all cases. Rather
21772 than die, we'll just produce deficient debug info now, in that we will
21773 have variables without a proper abstract origin. In the future, when all
21774 functions are lowered, we should re-add a gcc_assert (origin_die)
21775 here. */
21777 if (origin_die)
21779 dw_attr_node *a;
21780 /* Like above, if we already created a concrete instance DIE
21781 do not use that for the abstract origin but the early DIE
21782 if present. */
21783 if (in_lto_p
21784 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21785 origin_die = AT_ref (a);
21786 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21790 /* We do not currently support the pure_virtual attribute. */
21792 static inline void
21793 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21795 if (DECL_VINDEX (func_decl))
21797 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21799 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21800 add_AT_loc (die, DW_AT_vtable_elem_location,
21801 new_loc_descr (DW_OP_constu,
21802 tree_to_shwi (DECL_VINDEX (func_decl)),
21803 0));
21805 /* GNU extension: Record what type this method came from originally. */
21806 if (debug_info_level > DINFO_LEVEL_TERSE
21807 && DECL_CONTEXT (func_decl))
21808 add_AT_die_ref (die, DW_AT_containing_type,
21809 lookup_type_die (DECL_CONTEXT (func_decl)));
21813 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21814 given decl. This used to be a vendor extension until after DWARF 4
21815 standardized it. */
21817 static void
21818 add_linkage_attr (dw_die_ref die, tree decl)
21820 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21822 /* Mimic what assemble_name_raw does with a leading '*'. */
21823 if (name[0] == '*')
21824 name = &name[1];
21826 if (dwarf_version >= 4)
21827 add_AT_string (die, DW_AT_linkage_name, name);
21828 else
21829 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21832 /* Add source coordinate attributes for the given decl. */
21834 static void
21835 add_src_coords_attributes (dw_die_ref die, tree decl)
21837 expanded_location s;
21839 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21840 return;
21841 s = expand_location (DECL_SOURCE_LOCATION (decl));
21842 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21843 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21844 if (debug_column_info && s.column)
21845 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21848 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21850 static void
21851 add_linkage_name_raw (dw_die_ref die, tree decl)
21853 /* Defer until we have an assembler name set. */
21854 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21856 limbo_die_node *asm_name;
21858 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21859 asm_name->die = die;
21860 asm_name->created_for = decl;
21861 asm_name->next = deferred_asm_name;
21862 deferred_asm_name = asm_name;
21864 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21865 add_linkage_attr (die, decl);
21868 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21870 static void
21871 add_linkage_name (dw_die_ref die, tree decl)
21873 if (debug_info_level > DINFO_LEVEL_NONE
21874 && VAR_OR_FUNCTION_DECL_P (decl)
21875 && TREE_PUBLIC (decl)
21876 && !(VAR_P (decl) && DECL_REGISTER (decl))
21877 && die->die_tag != DW_TAG_member)
21878 add_linkage_name_raw (die, decl);
21881 /* Add a DW_AT_name attribute and source coordinate attribute for the
21882 given decl, but only if it actually has a name. */
21884 static void
21885 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21886 bool no_linkage_name)
21888 tree decl_name;
21890 decl_name = DECL_NAME (decl);
21891 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21893 const char *name = dwarf2_name (decl, 0);
21894 if (name)
21895 add_name_attribute (die, name);
21896 else
21897 add_desc_attribute (die, decl);
21899 if (! DECL_ARTIFICIAL (decl))
21900 add_src_coords_attributes (die, decl);
21902 if (!no_linkage_name)
21903 add_linkage_name (die, decl);
21905 else
21906 add_desc_attribute (die, decl);
21908 #ifdef VMS_DEBUGGING_INFO
21909 /* Get the function's name, as described by its RTL. This may be different
21910 from the DECL_NAME name used in the source file. */
21911 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21913 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21914 XEXP (DECL_RTL (decl), 0), false);
21915 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21917 #endif /* VMS_DEBUGGING_INFO */
21920 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21922 static void
21923 add_discr_value (dw_die_ref die, dw_discr_value *value)
21925 dw_attr_node attr;
21927 attr.dw_attr = DW_AT_discr_value;
21928 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21929 attr.dw_attr_val.val_entry = NULL;
21930 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21931 if (value->pos)
21932 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21933 else
21934 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21935 add_dwarf_attr (die, &attr);
21938 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21940 static void
21941 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21943 dw_attr_node attr;
21945 attr.dw_attr = DW_AT_discr_list;
21946 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21947 attr.dw_attr_val.val_entry = NULL;
21948 attr.dw_attr_val.v.val_discr_list = discr_list;
21949 add_dwarf_attr (die, &attr);
21952 static inline dw_discr_list_ref
21953 AT_discr_list (dw_attr_node *attr)
21955 return attr->dw_attr_val.v.val_discr_list;
21958 #ifdef VMS_DEBUGGING_INFO
21959 /* Output the debug main pointer die for VMS */
21961 void
21962 dwarf2out_vms_debug_main_pointer (void)
21964 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21965 dw_die_ref die;
21967 /* Allocate the VMS debug main subprogram die. */
21968 die = new_die_raw (DW_TAG_subprogram);
21969 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21970 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21971 current_function_funcdef_no);
21972 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21974 /* Make it the first child of comp_unit_die (). */
21975 die->die_parent = comp_unit_die ();
21976 if (comp_unit_die ()->die_child)
21978 die->die_sib = comp_unit_die ()->die_child->die_sib;
21979 comp_unit_die ()->die_child->die_sib = die;
21981 else
21983 die->die_sib = die;
21984 comp_unit_die ()->die_child = die;
21987 #endif /* VMS_DEBUGGING_INFO */
21989 /* walk_tree helper function for uses_local_type, below. */
21991 static tree
21992 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21994 if (!TYPE_P (*tp))
21995 *walk_subtrees = 0;
21996 else
21998 tree name = TYPE_NAME (*tp);
21999 if (name && DECL_P (name) && decl_function_context (name))
22000 return *tp;
22002 return NULL_TREE;
22005 /* If TYPE involves a function-local type (including a local typedef to a
22006 non-local type), returns that type; otherwise returns NULL_TREE. */
22008 static tree
22009 uses_local_type (tree type)
22011 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22012 return used;
22015 /* Return the DIE for the scope that immediately contains this type.
22016 Non-named types that do not involve a function-local type get global
22017 scope. Named types nested in namespaces or other types get their
22018 containing scope. All other types (i.e. function-local named types) get
22019 the current active scope. */
22021 static dw_die_ref
22022 scope_die_for (tree t, dw_die_ref context_die)
22024 dw_die_ref scope_die = NULL;
22025 tree containing_scope;
22027 /* Non-types always go in the current scope. */
22028 gcc_assert (TYPE_P (t));
22030 /* Use the scope of the typedef, rather than the scope of the type
22031 it refers to. */
22032 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22033 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22034 else
22035 containing_scope = TYPE_CONTEXT (t);
22037 /* Use the containing namespace if there is one. */
22038 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22040 if (context_die == lookup_decl_die (containing_scope))
22041 /* OK */;
22042 else if (debug_info_level > DINFO_LEVEL_TERSE)
22043 context_die = get_context_die (containing_scope);
22044 else
22045 containing_scope = NULL_TREE;
22048 /* Ignore function type "scopes" from the C frontend. They mean that
22049 a tagged type is local to a parmlist of a function declarator, but
22050 that isn't useful to DWARF. */
22051 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22052 containing_scope = NULL_TREE;
22054 if (SCOPE_FILE_SCOPE_P (containing_scope))
22056 /* If T uses a local type keep it local as well, to avoid references
22057 to function-local DIEs from outside the function. */
22058 if (current_function_decl && uses_local_type (t))
22059 scope_die = context_die;
22060 else
22061 scope_die = comp_unit_die ();
22063 else if (TYPE_P (containing_scope))
22065 /* For types, we can just look up the appropriate DIE. */
22066 if (debug_info_level > DINFO_LEVEL_TERSE)
22067 scope_die = get_context_die (containing_scope);
22068 else
22070 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22071 if (scope_die == NULL)
22072 scope_die = comp_unit_die ();
22075 else
22076 scope_die = context_die;
22078 return scope_die;
22081 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22083 static inline int
22084 local_scope_p (dw_die_ref context_die)
22086 for (; context_die; context_die = context_die->die_parent)
22087 if (context_die->die_tag == DW_TAG_inlined_subroutine
22088 || context_die->die_tag == DW_TAG_subprogram)
22089 return 1;
22091 return 0;
22094 /* Returns nonzero if CONTEXT_DIE is a class. */
22096 static inline int
22097 class_scope_p (dw_die_ref context_die)
22099 return (context_die
22100 && (context_die->die_tag == DW_TAG_structure_type
22101 || context_die->die_tag == DW_TAG_class_type
22102 || context_die->die_tag == DW_TAG_interface_type
22103 || context_die->die_tag == DW_TAG_union_type));
22106 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22107 whether or not to treat a DIE in this context as a declaration. */
22109 static inline int
22110 class_or_namespace_scope_p (dw_die_ref context_die)
22112 return (class_scope_p (context_die)
22113 || (context_die && context_die->die_tag == DW_TAG_namespace));
22116 /* Many forms of DIEs require a "type description" attribute. This
22117 routine locates the proper "type descriptor" die for the type given
22118 by 'type' plus any additional qualifiers given by 'cv_quals', and
22119 adds a DW_AT_type attribute below the given die. */
22121 static void
22122 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22123 bool reverse, dw_die_ref context_die)
22125 enum tree_code code = TREE_CODE (type);
22126 dw_die_ref type_die = NULL;
22128 if (debug_info_level <= DINFO_LEVEL_TERSE)
22129 return;
22131 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22132 or fixed-point type, use the inner type. This is because we have no
22133 support for unnamed types in base_type_die. This can happen if this is
22134 an Ada subrange type. Correct solution is emit a subrange type die. */
22135 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22136 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22137 type = TREE_TYPE (type), code = TREE_CODE (type);
22139 if (code == ERROR_MARK
22140 /* Handle a special case. For functions whose return type is void, we
22141 generate *no* type attribute. (Note that no object may have type
22142 `void', so this only applies to function return types). */
22143 || code == VOID_TYPE)
22144 return;
22146 type_die = modified_type_die (type,
22147 cv_quals | TYPE_QUALS (type),
22148 reverse,
22149 context_die);
22151 if (type_die != NULL)
22152 add_AT_die_ref (object_die, DW_AT_type, type_die);
22155 /* Given an object die, add the calling convention attribute for the
22156 function call type. */
22157 static void
22158 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22160 enum dwarf_calling_convention value = DW_CC_normal;
22162 value = ((enum dwarf_calling_convention)
22163 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22165 if (is_fortran ()
22166 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22168 /* DWARF 2 doesn't provide a way to identify a program's source-level
22169 entry point. DW_AT_calling_convention attributes are only meant
22170 to describe functions' calling conventions. However, lacking a
22171 better way to signal the Fortran main program, we used this for
22172 a long time, following existing custom. Now, DWARF 4 has
22173 DW_AT_main_subprogram, which we add below, but some tools still
22174 rely on the old way, which we thus keep. */
22175 value = DW_CC_program;
22177 if (dwarf_version >= 4 || !dwarf_strict)
22178 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22181 /* Only add the attribute if the backend requests it, and
22182 is not DW_CC_normal. */
22183 if (value && (value != DW_CC_normal))
22184 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22187 /* Given a tree pointer to a struct, class, union, or enum type node, return
22188 a pointer to the (string) tag name for the given type, or zero if the type
22189 was declared without a tag. */
22191 static const char *
22192 type_tag (const_tree type)
22194 const char *name = 0;
22196 if (TYPE_NAME (type) != 0)
22198 tree t = 0;
22200 /* Find the IDENTIFIER_NODE for the type name. */
22201 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22202 && !TYPE_NAMELESS (type))
22203 t = TYPE_NAME (type);
22205 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22206 a TYPE_DECL node, regardless of whether or not a `typedef' was
22207 involved. */
22208 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22209 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22211 /* We want to be extra verbose. Don't call dwarf_name if
22212 DECL_NAME isn't set. The default hook for decl_printable_name
22213 doesn't like that, and in this context it's correct to return
22214 0, instead of "<anonymous>" or the like. */
22215 if (DECL_NAME (TYPE_NAME (type))
22216 && !DECL_NAMELESS (TYPE_NAME (type)))
22217 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22220 /* Now get the name as a string, or invent one. */
22221 if (!name && t != 0)
22222 name = IDENTIFIER_POINTER (t);
22225 return (name == 0 || *name == '\0') ? 0 : name;
22228 /* Return the type associated with a data member, make a special check
22229 for bit field types. */
22231 static inline tree
22232 member_declared_type (const_tree member)
22234 return (DECL_BIT_FIELD_TYPE (member)
22235 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22238 /* Get the decl's label, as described by its RTL. This may be different
22239 from the DECL_NAME name used in the source file. */
22241 #if 0
22242 static const char *
22243 decl_start_label (tree decl)
22245 rtx x;
22246 const char *fnname;
22248 x = DECL_RTL (decl);
22249 gcc_assert (MEM_P (x));
22251 x = XEXP (x, 0);
22252 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22254 fnname = XSTR (x, 0);
22255 return fnname;
22257 #endif
22259 /* For variable-length arrays that have been previously generated, but
22260 may be incomplete due to missing subscript info, fill the subscript
22261 info. Return TRUE if this is one of those cases. */
22263 static bool
22264 fill_variable_array_bounds (tree type)
22266 if (TREE_ASM_WRITTEN (type)
22267 && TREE_CODE (type) == ARRAY_TYPE
22268 && variably_modified_type_p (type, NULL))
22270 dw_die_ref array_die = lookup_type_die (type);
22271 if (!array_die)
22272 return false;
22273 add_subscript_info (array_die, type, !is_ada ());
22274 return true;
22276 return false;
22279 /* These routines generate the internal representation of the DIE's for
22280 the compilation unit. Debugging information is collected by walking
22281 the declaration trees passed in from dwarf2out_decl(). */
22283 static void
22284 gen_array_type_die (tree type, dw_die_ref context_die)
22286 dw_die_ref array_die;
22288 /* GNU compilers represent multidimensional array types as sequences of one
22289 dimensional array types whose element types are themselves array types.
22290 We sometimes squish that down to a single array_type DIE with multiple
22291 subscripts in the Dwarf debugging info. The draft Dwarf specification
22292 say that we are allowed to do this kind of compression in C, because
22293 there is no difference between an array of arrays and a multidimensional
22294 array. We don't do this for Ada to remain as close as possible to the
22295 actual representation, which is especially important against the language
22296 flexibilty wrt arrays of variable size. */
22298 bool collapse_nested_arrays = !is_ada ();
22300 if (fill_variable_array_bounds (type))
22301 return;
22303 dw_die_ref scope_die = scope_die_for (type, context_die);
22304 tree element_type;
22306 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22307 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22308 if (TREE_CODE (type) == ARRAY_TYPE
22309 && TYPE_STRING_FLAG (type)
22310 && is_fortran ()
22311 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22313 HOST_WIDE_INT size;
22315 array_die = new_die (DW_TAG_string_type, scope_die, type);
22316 add_name_attribute (array_die, type_tag (type));
22317 equate_type_number_to_die (type, array_die);
22318 size = int_size_in_bytes (type);
22319 if (size >= 0)
22320 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22321 /* ??? We can't annotate types late, but for LTO we may not
22322 generate a location early either (gfortran.dg/save_6.f90). */
22323 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22324 && TYPE_DOMAIN (type) != NULL_TREE
22325 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22327 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22328 tree rszdecl = szdecl;
22330 size = int_size_in_bytes (TREE_TYPE (szdecl));
22331 if (!DECL_P (szdecl))
22333 if (TREE_CODE (szdecl) == INDIRECT_REF
22334 && DECL_P (TREE_OPERAND (szdecl, 0)))
22336 rszdecl = TREE_OPERAND (szdecl, 0);
22337 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22338 != DWARF2_ADDR_SIZE)
22339 size = 0;
22341 else
22342 size = 0;
22344 if (size > 0)
22346 dw_loc_list_ref loc
22347 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22348 NULL);
22349 if (loc)
22351 add_AT_location_description (array_die, DW_AT_string_length,
22352 loc);
22353 if (size != DWARF2_ADDR_SIZE)
22354 add_AT_unsigned (array_die, dwarf_version >= 5
22355 ? DW_AT_string_length_byte_size
22356 : DW_AT_byte_size, size);
22360 return;
22363 array_die = new_die (DW_TAG_array_type, scope_die, type);
22364 add_name_attribute (array_die, type_tag (type));
22365 equate_type_number_to_die (type, array_die);
22367 if (TREE_CODE (type) == VECTOR_TYPE)
22368 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22370 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22371 if (is_fortran ()
22372 && TREE_CODE (type) == ARRAY_TYPE
22373 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22374 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22375 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22377 #if 0
22378 /* We default the array ordering. Debuggers will probably do the right
22379 things even if DW_AT_ordering is not present. It's not even an issue
22380 until we start to get into multidimensional arrays anyway. If a debugger
22381 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22382 then we'll have to put the DW_AT_ordering attribute back in. (But if
22383 and when we find out that we need to put these in, we will only do so
22384 for multidimensional arrays. */
22385 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22386 #endif
22388 if (TREE_CODE (type) == VECTOR_TYPE)
22390 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22391 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22392 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22393 add_bound_info (subrange_die, DW_AT_upper_bound,
22394 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22396 else
22397 add_subscript_info (array_die, type, collapse_nested_arrays);
22399 /* Add representation of the type of the elements of this array type and
22400 emit the corresponding DIE if we haven't done it already. */
22401 element_type = TREE_TYPE (type);
22402 if (collapse_nested_arrays)
22403 while (TREE_CODE (element_type) == ARRAY_TYPE)
22405 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22406 break;
22407 element_type = TREE_TYPE (element_type);
22410 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22411 TREE_CODE (type) == ARRAY_TYPE
22412 && TYPE_REVERSE_STORAGE_ORDER (type),
22413 context_die);
22415 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22416 if (TYPE_ARTIFICIAL (type))
22417 add_AT_flag (array_die, DW_AT_artificial, 1);
22419 if (get_AT (array_die, DW_AT_name))
22420 add_pubtype (type, array_die);
22422 add_alignment_attribute (array_die, type);
22425 /* This routine generates DIE for array with hidden descriptor, details
22426 are filled into *info by a langhook. */
22428 static void
22429 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22430 dw_die_ref context_die)
22432 const dw_die_ref scope_die = scope_die_for (type, context_die);
22433 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22434 struct loc_descr_context context = {
22435 type, /* context_type */
22436 info->base_decl, /* base_decl */
22437 NULL, /* dpi */
22438 false, /* placeholder_arg */
22439 false, /* placeholder_seen */
22440 false /* strict_signedness */
22442 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22443 int dim;
22445 add_name_attribute (array_die, type_tag (type));
22446 equate_type_number_to_die (type, array_die);
22448 if (info->ndimensions > 1)
22449 switch (info->ordering)
22451 case array_descr_ordering_row_major:
22452 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22453 break;
22454 case array_descr_ordering_column_major:
22455 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22456 break;
22457 default:
22458 break;
22461 if (dwarf_version >= 3 || !dwarf_strict)
22463 if (info->data_location)
22464 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22465 dw_scalar_form_exprloc, &context);
22466 if (info->associated)
22467 add_scalar_info (array_die, DW_AT_associated, info->associated,
22468 dw_scalar_form_constant
22469 | dw_scalar_form_exprloc
22470 | dw_scalar_form_reference, &context);
22471 if (info->allocated)
22472 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22473 dw_scalar_form_constant
22474 | dw_scalar_form_exprloc
22475 | dw_scalar_form_reference, &context);
22476 if (info->stride)
22478 const enum dwarf_attribute attr
22479 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22480 const int forms
22481 = (info->stride_in_bits)
22482 ? dw_scalar_form_constant
22483 : (dw_scalar_form_constant
22484 | dw_scalar_form_exprloc
22485 | dw_scalar_form_reference);
22487 add_scalar_info (array_die, attr, info->stride, forms, &context);
22490 if (dwarf_version >= 5)
22492 if (info->rank)
22494 add_scalar_info (array_die, DW_AT_rank, info->rank,
22495 dw_scalar_form_constant
22496 | dw_scalar_form_exprloc, &context);
22497 subrange_tag = DW_TAG_generic_subrange;
22498 context.placeholder_arg = true;
22502 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22504 for (dim = 0; dim < info->ndimensions; dim++)
22506 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22508 if (info->dimen[dim].bounds_type)
22509 add_type_attribute (subrange_die,
22510 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22511 false, context_die);
22512 if (info->dimen[dim].lower_bound)
22513 add_bound_info (subrange_die, DW_AT_lower_bound,
22514 info->dimen[dim].lower_bound, &context);
22515 if (info->dimen[dim].upper_bound)
22516 add_bound_info (subrange_die, DW_AT_upper_bound,
22517 info->dimen[dim].upper_bound, &context);
22518 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22519 add_scalar_info (subrange_die, DW_AT_byte_stride,
22520 info->dimen[dim].stride,
22521 dw_scalar_form_constant
22522 | dw_scalar_form_exprloc
22523 | dw_scalar_form_reference,
22524 &context);
22527 gen_type_die (info->element_type, context_die);
22528 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22529 TREE_CODE (type) == ARRAY_TYPE
22530 && TYPE_REVERSE_STORAGE_ORDER (type),
22531 context_die);
22533 if (get_AT (array_die, DW_AT_name))
22534 add_pubtype (type, array_die);
22536 add_alignment_attribute (array_die, type);
22539 #if 0
22540 static void
22541 gen_entry_point_die (tree decl, dw_die_ref context_die)
22543 tree origin = decl_ultimate_origin (decl);
22544 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22546 if (origin != NULL)
22547 add_abstract_origin_attribute (decl_die, origin);
22548 else
22550 add_name_and_src_coords_attributes (decl_die, decl);
22551 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22552 TYPE_UNQUALIFIED, false, context_die);
22555 if (DECL_ABSTRACT_P (decl))
22556 equate_decl_number_to_die (decl, decl_die);
22557 else
22558 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22560 #endif
22562 /* Walk through the list of incomplete types again, trying once more to
22563 emit full debugging info for them. */
22565 static void
22566 retry_incomplete_types (void)
22568 set_early_dwarf s;
22569 int i;
22571 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22572 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22573 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22574 vec_safe_truncate (incomplete_types, 0);
22577 /* Determine what tag to use for a record type. */
22579 static enum dwarf_tag
22580 record_type_tag (tree type)
22582 if (! lang_hooks.types.classify_record)
22583 return DW_TAG_structure_type;
22585 switch (lang_hooks.types.classify_record (type))
22587 case RECORD_IS_STRUCT:
22588 return DW_TAG_structure_type;
22590 case RECORD_IS_CLASS:
22591 return DW_TAG_class_type;
22593 case RECORD_IS_INTERFACE:
22594 if (dwarf_version >= 3 || !dwarf_strict)
22595 return DW_TAG_interface_type;
22596 return DW_TAG_structure_type;
22598 default:
22599 gcc_unreachable ();
22603 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22604 include all of the information about the enumeration values also. Each
22605 enumerated type name/value is listed as a child of the enumerated type
22606 DIE. */
22608 static dw_die_ref
22609 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22611 dw_die_ref type_die = lookup_type_die (type);
22612 dw_die_ref orig_type_die = type_die;
22614 if (type_die == NULL)
22616 type_die = new_die (DW_TAG_enumeration_type,
22617 scope_die_for (type, context_die), type);
22618 equate_type_number_to_die (type, type_die);
22619 add_name_attribute (type_die, type_tag (type));
22620 if ((dwarf_version >= 4 || !dwarf_strict)
22621 && ENUM_IS_SCOPED (type))
22622 add_AT_flag (type_die, DW_AT_enum_class, 1);
22623 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22624 add_AT_flag (type_die, DW_AT_declaration, 1);
22625 if (!dwarf_strict)
22626 add_AT_unsigned (type_die, DW_AT_encoding,
22627 TYPE_UNSIGNED (type)
22628 ? DW_ATE_unsigned
22629 : DW_ATE_signed);
22631 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22632 return type_die;
22633 else
22634 remove_AT (type_die, DW_AT_declaration);
22636 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22637 given enum type is incomplete, do not generate the DW_AT_byte_size
22638 attribute or the DW_AT_element_list attribute. */
22639 if (TYPE_SIZE (type))
22641 tree link;
22643 if (!ENUM_IS_OPAQUE (type))
22644 TREE_ASM_WRITTEN (type) = 1;
22645 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22646 add_byte_size_attribute (type_die, type);
22647 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22648 add_alignment_attribute (type_die, type);
22649 if ((dwarf_version >= 3 || !dwarf_strict)
22650 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22652 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22653 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22654 context_die);
22656 if (TYPE_STUB_DECL (type) != NULL_TREE)
22658 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22659 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22660 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22661 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22664 /* If the first reference to this type was as the return type of an
22665 inline function, then it may not have a parent. Fix this now. */
22666 if (type_die->die_parent == NULL)
22667 add_child_die (scope_die_for (type, context_die), type_die);
22669 for (link = TYPE_VALUES (type);
22670 link != NULL; link = TREE_CHAIN (link))
22672 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22673 tree value = TREE_VALUE (link);
22675 if (DECL_P (value))
22676 equate_decl_number_to_die (value, enum_die);
22678 gcc_assert (!ENUM_IS_OPAQUE (type));
22679 add_name_attribute (enum_die,
22680 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22682 if (TREE_CODE (value) == CONST_DECL)
22683 value = DECL_INITIAL (value);
22685 if (simple_type_size_in_bits (TREE_TYPE (value))
22686 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22688 /* For constant forms created by add_AT_unsigned DWARF
22689 consumers (GDB, elfutils, etc.) always zero extend
22690 the value. Only when the actual value is negative
22691 do we need to use add_AT_int to generate a constant
22692 form that can represent negative values. */
22693 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22694 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22695 add_AT_unsigned (enum_die, DW_AT_const_value,
22696 (unsigned HOST_WIDE_INT) val);
22697 else
22698 add_AT_int (enum_die, DW_AT_const_value, val);
22700 else
22701 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22702 that here. TODO: This should be re-worked to use correct
22703 signed/unsigned double tags for all cases. */
22704 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22707 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22708 if (TYPE_ARTIFICIAL (type)
22709 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22710 add_AT_flag (type_die, DW_AT_artificial, 1);
22712 else
22713 add_AT_flag (type_die, DW_AT_declaration, 1);
22715 add_pubtype (type, type_die);
22717 return type_die;
22720 /* Generate a DIE to represent either a real live formal parameter decl or to
22721 represent just the type of some formal parameter position in some function
22722 type.
22724 Note that this routine is a bit unusual because its argument may be a
22725 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22726 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22727 node. If it's the former then this function is being called to output a
22728 DIE to represent a formal parameter object (or some inlining thereof). If
22729 it's the latter, then this function is only being called to output a
22730 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22731 argument type of some subprogram type.
22732 If EMIT_NAME_P is true, name and source coordinate attributes
22733 are emitted. */
22735 static dw_die_ref
22736 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22737 dw_die_ref context_die)
22739 tree node_or_origin = node ? node : origin;
22740 tree ultimate_origin;
22741 dw_die_ref parm_die = NULL;
22743 if (DECL_P (node_or_origin))
22745 parm_die = lookup_decl_die (node);
22747 /* If the contexts differ, we may not be talking about the same
22748 thing.
22749 ??? When in LTO the DIE parent is the "abstract" copy and the
22750 context_die is the specification "copy". */
22751 if (parm_die
22752 && parm_die->die_parent != context_die
22753 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22754 || parm_die->die_parent->die_parent != context_die)
22755 && !in_lto_p)
22757 gcc_assert (!DECL_ABSTRACT_P (node));
22758 /* This can happen when creating a concrete instance, in
22759 which case we need to create a new DIE that will get
22760 annotated with DW_AT_abstract_origin. */
22761 parm_die = NULL;
22764 if (parm_die && parm_die->die_parent == NULL)
22766 /* Check that parm_die already has the right attributes that
22767 we would have added below. If any attributes are
22768 missing, fall through to add them. */
22769 if (! DECL_ABSTRACT_P (node_or_origin)
22770 && !get_AT (parm_die, DW_AT_location)
22771 && !get_AT (parm_die, DW_AT_const_value))
22772 /* We are missing location info, and are about to add it. */
22774 else
22776 add_child_die (context_die, parm_die);
22777 return parm_die;
22782 /* If we have a previously generated DIE, use it, unless this is an
22783 concrete instance (origin != NULL), in which case we need a new
22784 DIE with a corresponding DW_AT_abstract_origin. */
22785 bool reusing_die;
22786 if (parm_die && origin == NULL)
22787 reusing_die = true;
22788 else
22790 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22791 reusing_die = false;
22794 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22796 case tcc_declaration:
22797 ultimate_origin = decl_ultimate_origin (node_or_origin);
22798 if (node || ultimate_origin)
22799 origin = ultimate_origin;
22801 if (reusing_die)
22802 goto add_location;
22804 if (origin != NULL)
22805 add_abstract_origin_attribute (parm_die, origin);
22806 else if (emit_name_p)
22807 add_name_and_src_coords_attributes (parm_die, node);
22808 if (origin == NULL
22809 || (! DECL_ABSTRACT_P (node_or_origin)
22810 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22811 decl_function_context
22812 (node_or_origin))))
22814 tree type = TREE_TYPE (node_or_origin);
22815 if (decl_by_reference_p (node_or_origin))
22816 add_type_attribute (parm_die, TREE_TYPE (type),
22817 TYPE_UNQUALIFIED,
22818 false, context_die);
22819 else
22820 add_type_attribute (parm_die, type,
22821 decl_quals (node_or_origin),
22822 false, context_die);
22824 if (origin == NULL && DECL_ARTIFICIAL (node))
22825 add_AT_flag (parm_die, DW_AT_artificial, 1);
22826 add_location:
22827 if (node && node != origin)
22828 equate_decl_number_to_die (node, parm_die);
22829 if (! DECL_ABSTRACT_P (node_or_origin))
22830 add_location_or_const_value_attribute (parm_die, node_or_origin,
22831 node == NULL);
22833 break;
22835 case tcc_type:
22836 /* We were called with some kind of a ..._TYPE node. */
22837 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22838 context_die);
22839 break;
22841 default:
22842 gcc_unreachable ();
22845 return parm_die;
22848 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22849 children DW_TAG_formal_parameter DIEs representing the arguments of the
22850 parameter pack.
22852 PARM_PACK must be a function parameter pack.
22853 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22854 must point to the subsequent arguments of the function PACK_ARG belongs to.
22855 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22856 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22857 following the last one for which a DIE was generated. */
22859 static dw_die_ref
22860 gen_formal_parameter_pack_die (tree parm_pack,
22861 tree pack_arg,
22862 dw_die_ref subr_die,
22863 tree *next_arg)
22865 tree arg;
22866 dw_die_ref parm_pack_die;
22868 gcc_assert (parm_pack
22869 && lang_hooks.function_parameter_pack_p (parm_pack)
22870 && subr_die);
22872 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22873 add_src_coords_attributes (parm_pack_die, parm_pack);
22875 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22877 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22878 parm_pack))
22879 break;
22880 gen_formal_parameter_die (arg, NULL,
22881 false /* Don't emit name attribute. */,
22882 parm_pack_die);
22884 if (next_arg)
22885 *next_arg = arg;
22886 return parm_pack_die;
22889 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22890 at the end of an (ANSI prototyped) formal parameters list. */
22892 static void
22893 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22895 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22898 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22899 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22900 parameters as specified in some function type specification (except for
22901 those which appear as part of a function *definition*). */
22903 static void
22904 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22906 tree link;
22907 tree formal_type = NULL;
22908 tree first_parm_type;
22909 tree arg;
22911 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22913 arg = DECL_ARGUMENTS (function_or_method_type);
22914 function_or_method_type = TREE_TYPE (function_or_method_type);
22916 else
22917 arg = NULL_TREE;
22919 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22921 /* Make our first pass over the list of formal parameter types and output a
22922 DW_TAG_formal_parameter DIE for each one. */
22923 for (link = first_parm_type; link; )
22925 dw_die_ref parm_die;
22927 formal_type = TREE_VALUE (link);
22928 if (formal_type == void_type_node)
22929 break;
22931 /* Output a (nameless) DIE to represent the formal parameter itself. */
22932 parm_die = gen_formal_parameter_die (formal_type, NULL,
22933 true /* Emit name attribute. */,
22934 context_die);
22935 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22936 && link == first_parm_type)
22938 add_AT_flag (parm_die, DW_AT_artificial, 1);
22939 if (dwarf_version >= 3 || !dwarf_strict)
22940 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22942 else if (arg && DECL_ARTIFICIAL (arg))
22943 add_AT_flag (parm_die, DW_AT_artificial, 1);
22945 link = TREE_CHAIN (link);
22946 if (arg)
22947 arg = DECL_CHAIN (arg);
22950 /* If this function type has an ellipsis, add a
22951 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22952 if (formal_type != void_type_node)
22953 gen_unspecified_parameters_die (function_or_method_type, context_die);
22955 /* Make our second (and final) pass over the list of formal parameter types
22956 and output DIEs to represent those types (as necessary). */
22957 for (link = TYPE_ARG_TYPES (function_or_method_type);
22958 link && TREE_VALUE (link);
22959 link = TREE_CHAIN (link))
22960 gen_type_die (TREE_VALUE (link), context_die);
22963 /* We want to generate the DIE for TYPE so that we can generate the
22964 die for MEMBER, which has been defined; we will need to refer back
22965 to the member declaration nested within TYPE. If we're trying to
22966 generate minimal debug info for TYPE, processing TYPE won't do the
22967 trick; we need to attach the member declaration by hand. */
22969 static void
22970 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22972 gen_type_die (type, context_die);
22974 /* If we're trying to avoid duplicate debug info, we may not have
22975 emitted the member decl for this function. Emit it now. */
22976 if (TYPE_STUB_DECL (type)
22977 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22978 && ! lookup_decl_die (member))
22980 dw_die_ref type_die;
22981 gcc_assert (!decl_ultimate_origin (member));
22983 type_die = lookup_type_die_strip_naming_typedef (type);
22984 if (TREE_CODE (member) == FUNCTION_DECL)
22985 gen_subprogram_die (member, type_die);
22986 else if (TREE_CODE (member) == FIELD_DECL)
22988 /* Ignore the nameless fields that are used to skip bits but handle
22989 C++ anonymous unions and structs. */
22990 if (DECL_NAME (member) != NULL_TREE
22991 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22992 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22994 struct vlr_context vlr_ctx = {
22995 DECL_CONTEXT (member), /* struct_type */
22996 NULL_TREE /* variant_part_offset */
22998 gen_type_die (member_declared_type (member), type_die);
22999 gen_field_die (member, &vlr_ctx, type_die);
23002 else
23003 gen_variable_die (member, NULL_TREE, type_die);
23007 /* Forward declare these functions, because they are mutually recursive
23008 with their set_block_* pairing functions. */
23009 static void set_decl_origin_self (tree);
23011 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23012 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23013 that it points to the node itself, thus indicating that the node is its
23014 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23015 the given node is NULL, recursively descend the decl/block tree which
23016 it is the root of, and for each other ..._DECL or BLOCK node contained
23017 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23018 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23019 values to point to themselves. */
23021 static void
23022 set_block_origin_self (tree stmt)
23024 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23026 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23029 tree local_decl;
23031 for (local_decl = BLOCK_VARS (stmt);
23032 local_decl != NULL_TREE;
23033 local_decl = DECL_CHAIN (local_decl))
23034 /* Do not recurse on nested functions since the inlining status
23035 of parent and child can be different as per the DWARF spec. */
23036 if (TREE_CODE (local_decl) != FUNCTION_DECL
23037 && !DECL_EXTERNAL (local_decl))
23038 set_decl_origin_self (local_decl);
23042 tree subblock;
23044 for (subblock = BLOCK_SUBBLOCKS (stmt);
23045 subblock != NULL_TREE;
23046 subblock = BLOCK_CHAIN (subblock))
23047 set_block_origin_self (subblock); /* Recurse. */
23052 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23053 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23054 node to so that it points to the node itself, thus indicating that the
23055 node represents its own (abstract) origin. Additionally, if the
23056 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23057 the decl/block tree of which the given node is the root of, and for
23058 each other ..._DECL or BLOCK node contained therein whose
23059 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23060 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23061 point to themselves. */
23063 static void
23064 set_decl_origin_self (tree decl)
23066 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23068 DECL_ABSTRACT_ORIGIN (decl) = decl;
23069 if (TREE_CODE (decl) == FUNCTION_DECL)
23071 tree arg;
23073 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23074 DECL_ABSTRACT_ORIGIN (arg) = arg;
23075 if (DECL_INITIAL (decl) != NULL_TREE
23076 && DECL_INITIAL (decl) != error_mark_node)
23077 set_block_origin_self (DECL_INITIAL (decl));
23082 /* Mark the early DIE for DECL as the abstract instance. */
23084 static void
23085 dwarf2out_abstract_function (tree decl)
23087 dw_die_ref old_die;
23089 /* Make sure we have the actual abstract inline, not a clone. */
23090 decl = DECL_ORIGIN (decl);
23092 if (DECL_IGNORED_P (decl))
23093 return;
23095 /* In LTO we're all set. We already created abstract instances
23096 early and we want to avoid creating a concrete instance of that
23097 if we don't output it. */
23098 if (in_lto_p)
23099 return;
23101 old_die = lookup_decl_die (decl);
23102 gcc_assert (old_die != NULL);
23103 if (get_AT (old_die, DW_AT_inline))
23104 /* We've already generated the abstract instance. */
23105 return;
23107 /* Go ahead and put DW_AT_inline on the DIE. */
23108 if (DECL_DECLARED_INLINE_P (decl))
23110 if (cgraph_function_possibly_inlined_p (decl))
23111 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23112 else
23113 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23115 else
23117 if (cgraph_function_possibly_inlined_p (decl))
23118 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23119 else
23120 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23123 if (DECL_DECLARED_INLINE_P (decl)
23124 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23125 add_AT_flag (old_die, DW_AT_artificial, 1);
23127 set_decl_origin_self (decl);
23130 /* Helper function of premark_used_types() which gets called through
23131 htab_traverse.
23133 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23134 marked as unused by prune_unused_types. */
23136 bool
23137 premark_used_types_helper (tree const &type, void *)
23139 dw_die_ref die;
23141 die = lookup_type_die (type);
23142 if (die != NULL)
23143 die->die_perennial_p = 1;
23144 return true;
23147 /* Helper function of premark_types_used_by_global_vars which gets called
23148 through htab_traverse.
23150 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23151 marked as unused by prune_unused_types. The DIE of the type is marked
23152 only if the global variable using the type will actually be emitted. */
23155 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23156 void *)
23158 struct types_used_by_vars_entry *entry;
23159 dw_die_ref die;
23161 entry = (struct types_used_by_vars_entry *) *slot;
23162 gcc_assert (entry->type != NULL
23163 && entry->var_decl != NULL);
23164 die = lookup_type_die (entry->type);
23165 if (die)
23167 /* Ask cgraph if the global variable really is to be emitted.
23168 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23169 varpool_node *node = varpool_node::get (entry->var_decl);
23170 if (node && node->definition)
23172 die->die_perennial_p = 1;
23173 /* Keep the parent DIEs as well. */
23174 while ((die = die->die_parent) && die->die_perennial_p == 0)
23175 die->die_perennial_p = 1;
23178 return 1;
23181 /* Mark all members of used_types_hash as perennial. */
23183 static void
23184 premark_used_types (struct function *fun)
23186 if (fun && fun->used_types_hash)
23187 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23190 /* Mark all members of types_used_by_vars_entry as perennial. */
23192 static void
23193 premark_types_used_by_global_vars (void)
23195 if (types_used_by_vars_hash)
23196 types_used_by_vars_hash
23197 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23200 /* Mark all variables used by the symtab as perennial. */
23202 static void
23203 premark_used_variables (void)
23205 /* Mark DIEs in the symtab as used. */
23206 varpool_node *var;
23207 FOR_EACH_VARIABLE (var)
23209 dw_die_ref die = lookup_decl_die (var->decl);
23210 if (die)
23211 die->die_perennial_p = 1;
23215 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23216 for CA_LOC call arg loc node. */
23218 static dw_die_ref
23219 gen_call_site_die (tree decl, dw_die_ref subr_die,
23220 struct call_arg_loc_node *ca_loc)
23222 dw_die_ref stmt_die = NULL, die;
23223 tree block = ca_loc->block;
23225 while (block
23226 && block != DECL_INITIAL (decl)
23227 && TREE_CODE (block) == BLOCK)
23229 stmt_die = lookup_block_die (block);
23230 if (stmt_die)
23231 break;
23232 block = BLOCK_SUPERCONTEXT (block);
23234 if (stmt_die == NULL)
23235 stmt_die = subr_die;
23236 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23237 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23238 if (ca_loc->tail_call_p)
23239 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23240 if (ca_loc->symbol_ref)
23242 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23243 if (tdie)
23244 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23245 else
23246 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23247 false);
23249 return die;
23252 /* Generate a DIE to represent a declared function (either file-scope or
23253 block-local). */
23255 static void
23256 gen_subprogram_die (tree decl, dw_die_ref context_die)
23258 tree origin = decl_ultimate_origin (decl);
23259 dw_die_ref subr_die;
23260 dw_die_ref old_die = lookup_decl_die (decl);
23261 bool old_die_had_no_children = false;
23263 /* This function gets called multiple times for different stages of
23264 the debug process. For example, for func() in this code:
23266 namespace S
23268 void func() { ... }
23271 ...we get called 4 times. Twice in early debug and twice in
23272 late debug:
23274 Early debug
23275 -----------
23277 1. Once while generating func() within the namespace. This is
23278 the declaration. The declaration bit below is set, as the
23279 context is the namespace.
23281 A new DIE will be generated with DW_AT_declaration set.
23283 2. Once for func() itself. This is the specification. The
23284 declaration bit below is clear as the context is the CU.
23286 We will use the cached DIE from (1) to create a new DIE with
23287 DW_AT_specification pointing to the declaration in (1).
23289 Late debug via rest_of_handle_final()
23290 -------------------------------------
23292 3. Once generating func() within the namespace. This is also the
23293 declaration, as in (1), but this time we will early exit below
23294 as we have a cached DIE and a declaration needs no additional
23295 annotations (no locations), as the source declaration line
23296 info is enough.
23298 4. Once for func() itself. As in (2), this is the specification,
23299 but this time we will re-use the cached DIE, and just annotate
23300 it with the location information that should now be available.
23302 For something without namespaces, but with abstract instances, we
23303 are also called a multiple times:
23305 class Base
23307 public:
23308 Base (); // constructor declaration (1)
23311 Base::Base () { } // constructor specification (2)
23313 Early debug
23314 -----------
23316 1. Once for the Base() constructor by virtue of it being a
23317 member of the Base class. This is done via
23318 rest_of_type_compilation.
23320 This is a declaration, so a new DIE will be created with
23321 DW_AT_declaration.
23323 2. Once for the Base() constructor definition, but this time
23324 while generating the abstract instance of the base
23325 constructor (__base_ctor) which is being generated via early
23326 debug of reachable functions.
23328 Even though we have a cached version of the declaration (1),
23329 we will create a DW_AT_specification of the declaration DIE
23330 in (1).
23332 3. Once for the __base_ctor itself, but this time, we generate
23333 an DW_AT_abstract_origin version of the DW_AT_specification in
23334 (2).
23336 Late debug via rest_of_handle_final
23337 -----------------------------------
23339 4. One final time for the __base_ctor (which will have a cached
23340 DIE with DW_AT_abstract_origin created in (3). This time,
23341 we will just annotate the location information now
23342 available.
23344 int declaration = (current_function_decl != decl
23345 || (!DECL_INITIAL (decl) && !origin)
23346 || class_or_namespace_scope_p (context_die));
23348 /* A declaration that has been previously dumped needs no
23349 additional information. */
23350 if (old_die && declaration)
23351 return;
23353 if (in_lto_p && old_die && old_die->die_child == NULL)
23354 old_die_had_no_children = true;
23356 /* Now that the C++ front end lazily declares artificial member fns, we
23357 might need to retrofit the declaration into its class. */
23358 if (!declaration && !origin && !old_die
23359 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23360 && !class_or_namespace_scope_p (context_die)
23361 && debug_info_level > DINFO_LEVEL_TERSE)
23362 old_die = force_decl_die (decl);
23364 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23365 if (origin != NULL)
23367 gcc_assert (!declaration || local_scope_p (context_die));
23369 /* Fixup die_parent for the abstract instance of a nested
23370 inline function. */
23371 if (old_die && old_die->die_parent == NULL)
23372 add_child_die (context_die, old_die);
23374 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23376 /* If we have a DW_AT_abstract_origin we have a working
23377 cached version. */
23378 subr_die = old_die;
23380 else
23382 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23383 add_abstract_origin_attribute (subr_die, origin);
23384 /* This is where the actual code for a cloned function is.
23385 Let's emit linkage name attribute for it. This helps
23386 debuggers to e.g, set breakpoints into
23387 constructors/destructors when the user asks "break
23388 K::K". */
23389 add_linkage_name (subr_die, decl);
23392 /* A cached copy, possibly from early dwarf generation. Reuse as
23393 much as possible. */
23394 else if (old_die)
23396 if (!get_AT_flag (old_die, DW_AT_declaration)
23397 /* We can have a normal definition following an inline one in the
23398 case of redefinition of GNU C extern inlines.
23399 It seems reasonable to use AT_specification in this case. */
23400 && !get_AT (old_die, DW_AT_inline))
23402 /* Detect and ignore this case, where we are trying to output
23403 something we have already output. */
23404 if (get_AT (old_die, DW_AT_low_pc)
23405 || get_AT (old_die, DW_AT_ranges))
23406 return;
23408 /* If we have no location information, this must be a
23409 partially generated DIE from early dwarf generation.
23410 Fall through and generate it. */
23413 /* If the definition comes from the same place as the declaration,
23414 maybe use the old DIE. We always want the DIE for this function
23415 that has the *_pc attributes to be under comp_unit_die so the
23416 debugger can find it. We also need to do this for abstract
23417 instances of inlines, since the spec requires the out-of-line copy
23418 to have the same parent. For local class methods, this doesn't
23419 apply; we just use the old DIE. */
23420 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23421 struct dwarf_file_data * file_index = lookup_filename (s.file);
23422 if (((is_unit_die (old_die->die_parent)
23423 /* This condition fixes the inconsistency/ICE with the
23424 following Fortran test (or some derivative thereof) while
23425 building libgfortran:
23427 module some_m
23428 contains
23429 logical function funky (FLAG)
23430 funky = .true.
23431 end function
23432 end module
23434 || (old_die->die_parent
23435 && old_die->die_parent->die_tag == DW_TAG_module)
23436 || local_scope_p (old_die->die_parent)
23437 || context_die == NULL)
23438 && (DECL_ARTIFICIAL (decl)
23439 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23440 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23441 == (unsigned) s.line)
23442 && (!debug_column_info
23443 || s.column == 0
23444 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23445 == (unsigned) s.column)))))
23446 /* With LTO if there's an abstract instance for
23447 the old DIE, this is a concrete instance and
23448 thus re-use the DIE. */
23449 || get_AT (old_die, DW_AT_abstract_origin))
23451 subr_die = old_die;
23453 /* Clear out the declaration attribute, but leave the
23454 parameters so they can be augmented with location
23455 information later. Unless this was a declaration, in
23456 which case, wipe out the nameless parameters and recreate
23457 them further down. */
23458 if (remove_AT (subr_die, DW_AT_declaration))
23461 remove_AT (subr_die, DW_AT_object_pointer);
23462 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23465 /* Make a specification pointing to the previously built
23466 declaration. */
23467 else
23469 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23470 add_AT_specification (subr_die, old_die);
23471 add_pubname (decl, subr_die);
23472 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23473 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23474 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23475 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23476 if (debug_column_info
23477 && s.column
23478 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23479 != (unsigned) s.column))
23480 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23482 /* If the prototype had an 'auto' or 'decltype(auto)' in
23483 the return type, emit the real type on the definition die. */
23484 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23486 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23487 while (die
23488 && (die->die_tag == DW_TAG_reference_type
23489 || die->die_tag == DW_TAG_rvalue_reference_type
23490 || die->die_tag == DW_TAG_pointer_type
23491 || die->die_tag == DW_TAG_const_type
23492 || die->die_tag == DW_TAG_volatile_type
23493 || die->die_tag == DW_TAG_restrict_type
23494 || die->die_tag == DW_TAG_array_type
23495 || die->die_tag == DW_TAG_ptr_to_member_type
23496 || die->die_tag == DW_TAG_subroutine_type))
23497 die = get_AT_ref (die, DW_AT_type);
23498 if (die == auto_die || die == decltype_auto_die)
23499 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23500 TYPE_UNQUALIFIED, false, context_die);
23503 /* When we process the method declaration, we haven't seen
23504 the out-of-class defaulted definition yet, so we have to
23505 recheck now. */
23506 if ((dwarf_version >= 5 || ! dwarf_strict)
23507 && !get_AT (subr_die, DW_AT_defaulted))
23509 int defaulted
23510 = lang_hooks.decls.decl_dwarf_attribute (decl,
23511 DW_AT_defaulted);
23512 if (defaulted != -1)
23514 /* Other values must have been handled before. */
23515 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23516 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23521 /* Create a fresh DIE for anything else. */
23522 else
23524 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23526 if (TREE_PUBLIC (decl))
23527 add_AT_flag (subr_die, DW_AT_external, 1);
23529 add_name_and_src_coords_attributes (subr_die, decl);
23530 add_pubname (decl, subr_die);
23531 if (debug_info_level > DINFO_LEVEL_TERSE)
23533 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23534 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23535 TYPE_UNQUALIFIED, false, context_die);
23538 add_pure_or_virtual_attribute (subr_die, decl);
23539 if (DECL_ARTIFICIAL (decl))
23540 add_AT_flag (subr_die, DW_AT_artificial, 1);
23542 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23543 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23545 add_alignment_attribute (subr_die, decl);
23547 add_accessibility_attribute (subr_die, decl);
23550 /* Unless we have an existing non-declaration DIE, equate the new
23551 DIE. */
23552 if (!old_die || is_declaration_die (old_die))
23553 equate_decl_number_to_die (decl, subr_die);
23555 if (declaration)
23557 if (!old_die || !get_AT (old_die, DW_AT_inline))
23559 add_AT_flag (subr_die, DW_AT_declaration, 1);
23561 /* If this is an explicit function declaration then generate
23562 a DW_AT_explicit attribute. */
23563 if ((dwarf_version >= 3 || !dwarf_strict)
23564 && lang_hooks.decls.decl_dwarf_attribute (decl,
23565 DW_AT_explicit) == 1)
23566 add_AT_flag (subr_die, DW_AT_explicit, 1);
23568 /* If this is a C++11 deleted special function member then generate
23569 a DW_AT_deleted attribute. */
23570 if ((dwarf_version >= 5 || !dwarf_strict)
23571 && lang_hooks.decls.decl_dwarf_attribute (decl,
23572 DW_AT_deleted) == 1)
23573 add_AT_flag (subr_die, DW_AT_deleted, 1);
23575 /* If this is a C++11 defaulted special function member then
23576 generate a DW_AT_defaulted attribute. */
23577 if (dwarf_version >= 5 || !dwarf_strict)
23579 int defaulted
23580 = lang_hooks.decls.decl_dwarf_attribute (decl,
23581 DW_AT_defaulted);
23582 if (defaulted != -1)
23583 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23586 /* If this is a C++11 non-static member function with & ref-qualifier
23587 then generate a DW_AT_reference attribute. */
23588 if ((dwarf_version >= 5 || !dwarf_strict)
23589 && lang_hooks.decls.decl_dwarf_attribute (decl,
23590 DW_AT_reference) == 1)
23591 add_AT_flag (subr_die, DW_AT_reference, 1);
23593 /* If this is a C++11 non-static member function with &&
23594 ref-qualifier then generate a DW_AT_reference attribute. */
23595 if ((dwarf_version >= 5 || !dwarf_strict)
23596 && lang_hooks.decls.decl_dwarf_attribute (decl,
23597 DW_AT_rvalue_reference)
23598 == 1)
23599 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23602 /* For non DECL_EXTERNALs, if range information is available, fill
23603 the DIE with it. */
23604 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23606 HOST_WIDE_INT cfa_fb_offset;
23608 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23610 if (!crtl->has_bb_partition)
23612 dw_fde_ref fde = fun->fde;
23613 if (fde->dw_fde_begin)
23615 /* We have already generated the labels. */
23616 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23617 fde->dw_fde_end, false);
23619 else
23621 /* Create start/end labels and add the range. */
23622 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23623 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23624 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23625 current_function_funcdef_no);
23626 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23627 current_function_funcdef_no);
23628 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23629 false);
23632 #if VMS_DEBUGGING_INFO
23633 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23634 Section 2.3 Prologue and Epilogue Attributes:
23635 When a breakpoint is set on entry to a function, it is generally
23636 desirable for execution to be suspended, not on the very first
23637 instruction of the function, but rather at a point after the
23638 function's frame has been set up, after any language defined local
23639 declaration processing has been completed, and before execution of
23640 the first statement of the function begins. Debuggers generally
23641 cannot properly determine where this point is. Similarly for a
23642 breakpoint set on exit from a function. The prologue and epilogue
23643 attributes allow a compiler to communicate the location(s) to use. */
23646 if (fde->dw_fde_vms_end_prologue)
23647 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23648 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23650 if (fde->dw_fde_vms_begin_epilogue)
23651 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23652 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23654 #endif
23657 else
23659 /* Generate pubnames entries for the split function code ranges. */
23660 dw_fde_ref fde = fun->fde;
23662 if (fde->dw_fde_second_begin)
23664 if (dwarf_version >= 3 || !dwarf_strict)
23666 /* We should use ranges for non-contiguous code section
23667 addresses. Use the actual code range for the initial
23668 section, since the HOT/COLD labels might precede an
23669 alignment offset. */
23670 bool range_list_added = false;
23671 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23672 fde->dw_fde_end, &range_list_added,
23673 false);
23674 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23675 fde->dw_fde_second_end,
23676 &range_list_added, false);
23677 if (range_list_added)
23678 add_ranges (NULL);
23680 else
23682 /* There is no real support in DW2 for this .. so we make
23683 a work-around. First, emit the pub name for the segment
23684 containing the function label. Then make and emit a
23685 simplified subprogram DIE for the second segment with the
23686 name pre-fixed by __hot/cold_sect_of_. We use the same
23687 linkage name for the second die so that gdb will find both
23688 sections when given "b foo". */
23689 const char *name = NULL;
23690 tree decl_name = DECL_NAME (decl);
23691 dw_die_ref seg_die;
23693 /* Do the 'primary' section. */
23694 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23695 fde->dw_fde_end, false);
23697 /* Build a minimal DIE for the secondary section. */
23698 seg_die = new_die (DW_TAG_subprogram,
23699 subr_die->die_parent, decl);
23701 if (TREE_PUBLIC (decl))
23702 add_AT_flag (seg_die, DW_AT_external, 1);
23704 if (decl_name != NULL
23705 && IDENTIFIER_POINTER (decl_name) != NULL)
23707 name = dwarf2_name (decl, 1);
23708 if (! DECL_ARTIFICIAL (decl))
23709 add_src_coords_attributes (seg_die, decl);
23711 add_linkage_name (seg_die, decl);
23713 gcc_assert (name != NULL);
23714 add_pure_or_virtual_attribute (seg_die, decl);
23715 if (DECL_ARTIFICIAL (decl))
23716 add_AT_flag (seg_die, DW_AT_artificial, 1);
23718 name = concat ("__second_sect_of_", name, NULL);
23719 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23720 fde->dw_fde_second_end, false);
23721 add_name_attribute (seg_die, name);
23722 if (want_pubnames ())
23723 add_pubname_string (name, seg_die);
23726 else
23727 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23728 false);
23731 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23733 /* We define the "frame base" as the function's CFA. This is more
23734 convenient for several reasons: (1) It's stable across the prologue
23735 and epilogue, which makes it better than just a frame pointer,
23736 (2) With dwarf3, there exists a one-byte encoding that allows us
23737 to reference the .debug_frame data by proxy, but failing that,
23738 (3) We can at least reuse the code inspection and interpretation
23739 code that determines the CFA position at various points in the
23740 function. */
23741 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23743 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23744 add_AT_loc (subr_die, DW_AT_frame_base, op);
23746 else
23748 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23749 if (list->dw_loc_next)
23750 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23751 else
23752 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23755 /* Compute a displacement from the "steady-state frame pointer" to
23756 the CFA. The former is what all stack slots and argument slots
23757 will reference in the rtl; the latter is what we've told the
23758 debugger about. We'll need to adjust all frame_base references
23759 by this displacement. */
23760 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23762 if (fun->static_chain_decl)
23764 /* DWARF requires here a location expression that computes the
23765 address of the enclosing subprogram's frame base. The machinery
23766 in tree-nested.c is supposed to store this specific address in the
23767 last field of the FRAME record. */
23768 const tree frame_type
23769 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23770 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23772 tree fb_expr
23773 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23774 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23775 fb_expr, fb_decl, NULL_TREE);
23777 add_AT_location_description (subr_die, DW_AT_static_link,
23778 loc_list_from_tree (fb_expr, 0, NULL));
23781 resolve_variable_values ();
23784 /* Generate child dies for template parameters. */
23785 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23786 gen_generic_params_dies (decl);
23788 /* Now output descriptions of the arguments for this function. This gets
23789 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23790 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23791 `...' at the end of the formal parameter list. In order to find out if
23792 there was a trailing ellipsis or not, we must instead look at the type
23793 associated with the FUNCTION_DECL. This will be a node of type
23794 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23795 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23796 an ellipsis at the end. */
23798 /* In the case where we are describing a mere function declaration, all we
23799 need to do here (and all we *can* do here) is to describe the *types* of
23800 its formal parameters. */
23801 if (debug_info_level <= DINFO_LEVEL_TERSE)
23803 else if (declaration)
23804 gen_formal_types_die (decl, subr_die);
23805 else
23807 /* Generate DIEs to represent all known formal parameters. */
23808 tree parm = DECL_ARGUMENTS (decl);
23809 tree generic_decl = early_dwarf
23810 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23811 tree generic_decl_parm = generic_decl
23812 ? DECL_ARGUMENTS (generic_decl)
23813 : NULL;
23815 /* Now we want to walk the list of parameters of the function and
23816 emit their relevant DIEs.
23818 We consider the case of DECL being an instance of a generic function
23819 as well as it being a normal function.
23821 If DECL is an instance of a generic function we walk the
23822 parameters of the generic function declaration _and_ the parameters of
23823 DECL itself. This is useful because we want to emit specific DIEs for
23824 function parameter packs and those are declared as part of the
23825 generic function declaration. In that particular case,
23826 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23827 That DIE has children DIEs representing the set of arguments
23828 of the pack. Note that the set of pack arguments can be empty.
23829 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23830 children DIE.
23832 Otherwise, we just consider the parameters of DECL. */
23833 while (generic_decl_parm || parm)
23835 if (generic_decl_parm
23836 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23837 gen_formal_parameter_pack_die (generic_decl_parm,
23838 parm, subr_die,
23839 &parm);
23840 else if (parm)
23842 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23844 if (early_dwarf
23845 && parm == DECL_ARGUMENTS (decl)
23846 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23847 && parm_die
23848 && (dwarf_version >= 3 || !dwarf_strict))
23849 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23851 parm = DECL_CHAIN (parm);
23854 if (generic_decl_parm)
23855 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23858 /* Decide whether we need an unspecified_parameters DIE at the end.
23859 There are 2 more cases to do this for: 1) the ansi ... declaration -
23860 this is detectable when the end of the arg list is not a
23861 void_type_node 2) an unprototyped function declaration (not a
23862 definition). This just means that we have no info about the
23863 parameters at all. */
23864 if (early_dwarf)
23866 if (prototype_p (TREE_TYPE (decl)))
23868 /* This is the prototyped case, check for.... */
23869 if (stdarg_p (TREE_TYPE (decl)))
23870 gen_unspecified_parameters_die (decl, subr_die);
23872 else if (DECL_INITIAL (decl) == NULL_TREE)
23873 gen_unspecified_parameters_die (decl, subr_die);
23875 else if ((subr_die != old_die || old_die_had_no_children)
23876 && prototype_p (TREE_TYPE (decl))
23877 && stdarg_p (TREE_TYPE (decl)))
23878 gen_unspecified_parameters_die (decl, subr_die);
23881 if (subr_die != old_die)
23882 /* Add the calling convention attribute if requested. */
23883 add_calling_convention_attribute (subr_die, decl);
23885 /* Output Dwarf info for all of the stuff within the body of the function
23886 (if it has one - it may be just a declaration).
23888 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23889 a function. This BLOCK actually represents the outermost binding contour
23890 for the function, i.e. the contour in which the function's formal
23891 parameters and labels get declared. Curiously, it appears that the front
23892 end doesn't actually put the PARM_DECL nodes for the current function onto
23893 the BLOCK_VARS list for this outer scope, but are strung off of the
23894 DECL_ARGUMENTS list for the function instead.
23896 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23897 the LABEL_DECL nodes for the function however, and we output DWARF info
23898 for those in decls_for_scope. Just within the `outer_scope' there will be
23899 a BLOCK node representing the function's outermost pair of curly braces,
23900 and any blocks used for the base and member initializers of a C++
23901 constructor function. */
23902 tree outer_scope = DECL_INITIAL (decl);
23903 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23905 int call_site_note_count = 0;
23906 int tail_call_site_note_count = 0;
23908 /* Emit a DW_TAG_variable DIE for a named return value. */
23909 if (DECL_NAME (DECL_RESULT (decl)))
23910 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23912 /* The first time through decls_for_scope we will generate the
23913 DIEs for the locals. The second time, we fill in the
23914 location info. */
23915 decls_for_scope (outer_scope, subr_die);
23917 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23919 struct call_arg_loc_node *ca_loc;
23920 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23922 dw_die_ref die = NULL;
23923 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23924 rtx arg, next_arg;
23925 tree arg_decl = NULL_TREE;
23927 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23928 ? XEXP (ca_loc->call_arg_loc_note, 0)
23929 : NULL_RTX);
23930 arg; arg = next_arg)
23932 dw_loc_descr_ref reg, val;
23933 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23934 dw_die_ref cdie, tdie = NULL;
23936 next_arg = XEXP (arg, 1);
23937 if (REG_P (XEXP (XEXP (arg, 0), 0))
23938 && next_arg
23939 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23940 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23941 && REGNO (XEXP (XEXP (arg, 0), 0))
23942 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23943 next_arg = XEXP (next_arg, 1);
23944 if (mode == VOIDmode)
23946 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23947 if (mode == VOIDmode)
23948 mode = GET_MODE (XEXP (arg, 0));
23950 if (mode == VOIDmode || mode == BLKmode)
23951 continue;
23952 /* Get dynamic information about call target only if we
23953 have no static information: we cannot generate both
23954 DW_AT_call_origin and DW_AT_call_target
23955 attributes. */
23956 if (ca_loc->symbol_ref == NULL_RTX)
23958 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23960 tloc = XEXP (XEXP (arg, 0), 1);
23961 continue;
23963 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23964 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23966 tlocc = XEXP (XEXP (arg, 0), 1);
23967 continue;
23970 reg = NULL;
23971 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23972 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23973 VAR_INIT_STATUS_INITIALIZED);
23974 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23976 rtx mem = XEXP (XEXP (arg, 0), 0);
23977 reg = mem_loc_descriptor (XEXP (mem, 0),
23978 get_address_mode (mem),
23979 GET_MODE (mem),
23980 VAR_INIT_STATUS_INITIALIZED);
23982 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23983 == DEBUG_PARAMETER_REF)
23985 tree tdecl
23986 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23987 tdie = lookup_decl_die (tdecl);
23988 if (tdie == NULL)
23989 continue;
23990 arg_decl = tdecl;
23992 else
23993 continue;
23994 if (reg == NULL
23995 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23996 != DEBUG_PARAMETER_REF)
23997 continue;
23998 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23999 VOIDmode,
24000 VAR_INIT_STATUS_INITIALIZED);
24001 if (val == NULL)
24002 continue;
24003 if (die == NULL)
24004 die = gen_call_site_die (decl, subr_die, ca_loc);
24005 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24006 NULL_TREE);
24007 add_desc_attribute (cdie, arg_decl);
24008 if (reg != NULL)
24009 add_AT_loc (cdie, DW_AT_location, reg);
24010 else if (tdie != NULL)
24011 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24012 tdie);
24013 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24014 if (next_arg != XEXP (arg, 1))
24016 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24017 if (mode == VOIDmode)
24018 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24019 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24020 0), 1),
24021 mode, VOIDmode,
24022 VAR_INIT_STATUS_INITIALIZED);
24023 if (val != NULL)
24024 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24025 val);
24028 if (die == NULL
24029 && (ca_loc->symbol_ref || tloc))
24030 die = gen_call_site_die (decl, subr_die, ca_loc);
24031 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24033 dw_loc_descr_ref tval = NULL;
24035 if (tloc != NULL_RTX)
24036 tval = mem_loc_descriptor (tloc,
24037 GET_MODE (tloc) == VOIDmode
24038 ? Pmode : GET_MODE (tloc),
24039 VOIDmode,
24040 VAR_INIT_STATUS_INITIALIZED);
24041 if (tval)
24042 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24043 else if (tlocc != NULL_RTX)
24045 tval = mem_loc_descriptor (tlocc,
24046 GET_MODE (tlocc) == VOIDmode
24047 ? Pmode : GET_MODE (tlocc),
24048 VOIDmode,
24049 VAR_INIT_STATUS_INITIALIZED);
24050 if (tval)
24051 add_AT_loc (die,
24052 dwarf_AT (DW_AT_call_target_clobbered),
24053 tval);
24056 if (die != NULL)
24058 call_site_note_count++;
24059 if (ca_loc->tail_call_p)
24060 tail_call_site_note_count++;
24064 call_arg_locations = NULL;
24065 call_arg_loc_last = NULL;
24066 if (tail_call_site_count >= 0
24067 && tail_call_site_count == tail_call_site_note_count
24068 && (!dwarf_strict || dwarf_version >= 5))
24070 if (call_site_count >= 0
24071 && call_site_count == call_site_note_count)
24072 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24073 else
24074 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24076 call_site_count = -1;
24077 tail_call_site_count = -1;
24080 /* Mark used types after we have created DIEs for the functions scopes. */
24081 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24084 /* Returns a hash value for X (which really is a die_struct). */
24086 hashval_t
24087 block_die_hasher::hash (die_struct *d)
24089 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24092 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24093 as decl_id and die_parent of die_struct Y. */
24095 bool
24096 block_die_hasher::equal (die_struct *x, die_struct *y)
24098 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24101 /* Hold information about markers for inlined entry points. */
24102 struct GTY ((for_user)) inline_entry_data
24104 /* The block that's the inlined_function_outer_scope for an inlined
24105 function. */
24106 tree block;
24108 /* The label at the inlined entry point. */
24109 const char *label_pfx;
24110 unsigned int label_num;
24112 /* The view number to be used as the inlined entry point. */
24113 var_loc_view view;
24116 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24118 typedef tree compare_type;
24119 static inline hashval_t hash (const inline_entry_data *);
24120 static inline bool equal (const inline_entry_data *, const_tree);
24123 /* Hash table routines for inline_entry_data. */
24125 inline hashval_t
24126 inline_entry_data_hasher::hash (const inline_entry_data *data)
24128 return htab_hash_pointer (data->block);
24131 inline bool
24132 inline_entry_data_hasher::equal (const inline_entry_data *data,
24133 const_tree block)
24135 return data->block == block;
24138 /* Inlined entry points pending DIE creation in this compilation unit. */
24140 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24143 /* Return TRUE if DECL, which may have been previously generated as
24144 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24145 true if decl (or its origin) is either an extern declaration or a
24146 class/namespace scoped declaration.
24148 The declare_in_namespace support causes us to get two DIEs for one
24149 variable, both of which are declarations. We want to avoid
24150 considering one to be a specification, so we must test for
24151 DECLARATION and DW_AT_declaration. */
24152 static inline bool
24153 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24155 return (old_die && TREE_STATIC (decl) && !declaration
24156 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24159 /* Return true if DECL is a local static. */
24161 static inline bool
24162 local_function_static (tree decl)
24164 gcc_assert (VAR_P (decl));
24165 return TREE_STATIC (decl)
24166 && DECL_CONTEXT (decl)
24167 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24170 /* Return true iff DECL overrides (presumably completes) the type of
24171 OLD_DIE within CONTEXT_DIE. */
24173 static bool
24174 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24175 dw_die_ref context_die)
24177 tree type = TREE_TYPE (decl);
24178 int cv_quals;
24180 if (decl_by_reference_p (decl))
24182 type = TREE_TYPE (type);
24183 cv_quals = TYPE_UNQUALIFIED;
24185 else
24186 cv_quals = decl_quals (decl);
24188 dw_die_ref type_die = modified_type_die (type,
24189 cv_quals | TYPE_QUALS (type),
24190 false,
24191 context_die);
24193 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24195 return type_die != old_type_die;
24198 /* Generate a DIE to represent a declared data object.
24199 Either DECL or ORIGIN must be non-null. */
24201 static void
24202 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24204 HOST_WIDE_INT off = 0;
24205 tree com_decl;
24206 tree decl_or_origin = decl ? decl : origin;
24207 tree ultimate_origin;
24208 dw_die_ref var_die;
24209 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24210 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24211 || class_or_namespace_scope_p (context_die));
24212 bool specialization_p = false;
24213 bool no_linkage_name = false;
24215 /* While C++ inline static data members have definitions inside of the
24216 class, force the first DIE to be a declaration, then let gen_member_die
24217 reparent it to the class context and call gen_variable_die again
24218 to create the outside of the class DIE for the definition. */
24219 if (!declaration
24220 && old_die == NULL
24221 && decl
24222 && DECL_CONTEXT (decl)
24223 && TYPE_P (DECL_CONTEXT (decl))
24224 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24226 declaration = true;
24227 if (dwarf_version < 5)
24228 no_linkage_name = true;
24231 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24232 if (decl || ultimate_origin)
24233 origin = ultimate_origin;
24234 com_decl = fortran_common (decl_or_origin, &off);
24236 /* Symbol in common gets emitted as a child of the common block, in the form
24237 of a data member. */
24238 if (com_decl)
24240 dw_die_ref com_die;
24241 dw_loc_list_ref loc = NULL;
24242 die_node com_die_arg;
24244 var_die = lookup_decl_die (decl_or_origin);
24245 if (var_die)
24247 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24249 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24250 if (loc)
24252 if (off)
24254 /* Optimize the common case. */
24255 if (single_element_loc_list_p (loc)
24256 && loc->expr->dw_loc_opc == DW_OP_addr
24257 && loc->expr->dw_loc_next == NULL
24258 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24259 == SYMBOL_REF)
24261 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24262 loc->expr->dw_loc_oprnd1.v.val_addr
24263 = plus_constant (GET_MODE (x), x , off);
24265 else
24266 loc_list_plus_const (loc, off);
24268 add_AT_location_description (var_die, DW_AT_location, loc);
24269 remove_AT (var_die, DW_AT_declaration);
24272 return;
24275 if (common_block_die_table == NULL)
24276 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24278 com_die_arg.decl_id = DECL_UID (com_decl);
24279 com_die_arg.die_parent = context_die;
24280 com_die = common_block_die_table->find (&com_die_arg);
24281 if (! early_dwarf)
24282 loc = loc_list_from_tree (com_decl, 2, NULL);
24283 if (com_die == NULL)
24285 const char *cnam
24286 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24287 die_node **slot;
24289 com_die = new_die (DW_TAG_common_block, context_die, decl);
24290 add_name_and_src_coords_attributes (com_die, com_decl);
24291 if (loc)
24293 add_AT_location_description (com_die, DW_AT_location, loc);
24294 /* Avoid sharing the same loc descriptor between
24295 DW_TAG_common_block and DW_TAG_variable. */
24296 loc = loc_list_from_tree (com_decl, 2, NULL);
24298 else if (DECL_EXTERNAL (decl_or_origin))
24299 add_AT_flag (com_die, DW_AT_declaration, 1);
24300 if (want_pubnames ())
24301 add_pubname_string (cnam, com_die); /* ??? needed? */
24302 com_die->decl_id = DECL_UID (com_decl);
24303 slot = common_block_die_table->find_slot (com_die, INSERT);
24304 *slot = com_die;
24306 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24308 add_AT_location_description (com_die, DW_AT_location, loc);
24309 loc = loc_list_from_tree (com_decl, 2, NULL);
24310 remove_AT (com_die, DW_AT_declaration);
24312 var_die = new_die (DW_TAG_variable, com_die, decl);
24313 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24314 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24315 decl_quals (decl_or_origin), false,
24316 context_die);
24317 add_alignment_attribute (var_die, decl);
24318 add_AT_flag (var_die, DW_AT_external, 1);
24319 if (loc)
24321 if (off)
24323 /* Optimize the common case. */
24324 if (single_element_loc_list_p (loc)
24325 && loc->expr->dw_loc_opc == DW_OP_addr
24326 && loc->expr->dw_loc_next == NULL
24327 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24329 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24330 loc->expr->dw_loc_oprnd1.v.val_addr
24331 = plus_constant (GET_MODE (x), x, off);
24333 else
24334 loc_list_plus_const (loc, off);
24336 add_AT_location_description (var_die, DW_AT_location, loc);
24338 else if (DECL_EXTERNAL (decl_or_origin))
24339 add_AT_flag (var_die, DW_AT_declaration, 1);
24340 if (decl)
24341 equate_decl_number_to_die (decl, var_die);
24342 return;
24345 if (old_die)
24347 if (declaration)
24349 /* A declaration that has been previously dumped, needs no
24350 further annotations, since it doesn't need location on
24351 the second pass. */
24352 return;
24354 else if (decl_will_get_specification_p (old_die, decl, declaration)
24355 && !get_AT (old_die, DW_AT_specification))
24357 /* Fall-thru so we can make a new variable die along with a
24358 DW_AT_specification. */
24360 else if (origin && old_die->die_parent != context_die)
24362 /* If we will be creating an inlined instance, we need a
24363 new DIE that will get annotated with
24364 DW_AT_abstract_origin. */
24365 gcc_assert (!DECL_ABSTRACT_P (decl));
24367 else
24369 /* If a DIE was dumped early, it still needs location info.
24370 Skip to where we fill the location bits. */
24371 var_die = old_die;
24373 /* ??? In LTRANS we cannot annotate early created variably
24374 modified type DIEs without copying them and adjusting all
24375 references to them. Thus we dumped them again. Also add a
24376 reference to them but beware of -g0 compile and -g link
24377 in which case the reference will be already present. */
24378 tree type = TREE_TYPE (decl_or_origin);
24379 if (in_lto_p
24380 && ! get_AT (var_die, DW_AT_type)
24381 && variably_modified_type_p
24382 (type, decl_function_context (decl_or_origin)))
24384 if (decl_by_reference_p (decl_or_origin))
24385 add_type_attribute (var_die, TREE_TYPE (type),
24386 TYPE_UNQUALIFIED, false, context_die);
24387 else
24388 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24389 false, context_die);
24392 goto gen_variable_die_location;
24396 /* For static data members, the declaration in the class is supposed
24397 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24398 also in DWARF2; the specification should still be DW_TAG_variable
24399 referencing the DW_TAG_member DIE. */
24400 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24401 var_die = new_die (DW_TAG_member, context_die, decl);
24402 else
24403 var_die = new_die (DW_TAG_variable, context_die, decl);
24405 if (origin != NULL)
24406 add_abstract_origin_attribute (var_die, origin);
24408 /* Loop unrolling can create multiple blocks that refer to the same
24409 static variable, so we must test for the DW_AT_declaration flag.
24411 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24412 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24413 sharing them.
24415 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24416 else if (decl_will_get_specification_p (old_die, decl, declaration))
24418 /* This is a definition of a C++ class level static. */
24419 add_AT_specification (var_die, old_die);
24420 specialization_p = true;
24421 if (DECL_NAME (decl))
24423 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24424 struct dwarf_file_data * file_index = lookup_filename (s.file);
24426 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24427 add_AT_file (var_die, DW_AT_decl_file, file_index);
24429 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24430 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24432 if (debug_column_info
24433 && s.column
24434 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24435 != (unsigned) s.column))
24436 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24438 if (old_die->die_tag == DW_TAG_member)
24439 add_linkage_name (var_die, decl);
24442 else
24443 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24445 if ((origin == NULL && !specialization_p)
24446 || (origin != NULL
24447 && !DECL_ABSTRACT_P (decl_or_origin)
24448 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24449 decl_function_context
24450 (decl_or_origin)))
24451 || (old_die && specialization_p
24452 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24454 tree type = TREE_TYPE (decl_or_origin);
24456 if (decl_by_reference_p (decl_or_origin))
24457 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24458 context_die);
24459 else
24460 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24461 context_die);
24464 if (origin == NULL && !specialization_p)
24466 if (TREE_PUBLIC (decl))
24467 add_AT_flag (var_die, DW_AT_external, 1);
24469 if (DECL_ARTIFICIAL (decl))
24470 add_AT_flag (var_die, DW_AT_artificial, 1);
24472 add_alignment_attribute (var_die, decl);
24474 add_accessibility_attribute (var_die, decl);
24477 if (declaration)
24478 add_AT_flag (var_die, DW_AT_declaration, 1);
24480 if (decl && (DECL_ABSTRACT_P (decl)
24481 || !old_die || is_declaration_die (old_die)))
24482 equate_decl_number_to_die (decl, var_die);
24484 gen_variable_die_location:
24485 if (! declaration
24486 && (! DECL_ABSTRACT_P (decl_or_origin)
24487 /* Local static vars are shared between all clones/inlines,
24488 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24489 already set. */
24490 || (VAR_P (decl_or_origin)
24491 && TREE_STATIC (decl_or_origin)
24492 && DECL_RTL_SET_P (decl_or_origin))))
24494 if (early_dwarf)
24496 add_pubname (decl_or_origin, var_die);
24497 /* For global register variables, emit DW_AT_location if possible
24498 already during early_dwarf, as late_global_decl won't be usually
24499 called. */
24500 if (DECL_HARD_REGISTER (decl_or_origin)
24501 && TREE_STATIC (decl_or_origin)
24502 && !decl_by_reference_p (decl_or_origin)
24503 && !get_AT (var_die, DW_AT_location)
24504 && !get_AT (var_die, DW_AT_const_value)
24505 && DECL_RTL_SET_P (decl_or_origin)
24506 && REG_P (DECL_RTL (decl_or_origin)))
24508 dw_loc_descr_ref descr
24509 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24510 VAR_INIT_STATUS_INITIALIZED);
24511 if (descr)
24512 add_AT_loc (var_die, DW_AT_location, descr);
24515 else
24516 add_location_or_const_value_attribute (var_die, decl_or_origin,
24517 decl == NULL);
24519 else
24520 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24522 if ((dwarf_version >= 4 || !dwarf_strict)
24523 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24524 DW_AT_const_expr) == 1
24525 && !get_AT (var_die, DW_AT_const_expr)
24526 && !specialization_p)
24527 add_AT_flag (var_die, DW_AT_const_expr, 1);
24529 if (!dwarf_strict)
24531 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24532 DW_AT_inline);
24533 if (inl != -1
24534 && !get_AT (var_die, DW_AT_inline)
24535 && !specialization_p)
24536 add_AT_unsigned (var_die, DW_AT_inline, inl);
24540 /* Generate a DIE to represent a named constant. */
24542 static void
24543 gen_const_die (tree decl, dw_die_ref context_die)
24545 dw_die_ref const_die;
24546 tree type = TREE_TYPE (decl);
24548 const_die = lookup_decl_die (decl);
24549 if (const_die)
24550 return;
24552 const_die = new_die (DW_TAG_constant, context_die, decl);
24553 equate_decl_number_to_die (decl, const_die);
24554 add_name_and_src_coords_attributes (const_die, decl);
24555 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24556 if (TREE_PUBLIC (decl))
24557 add_AT_flag (const_die, DW_AT_external, 1);
24558 if (DECL_ARTIFICIAL (decl))
24559 add_AT_flag (const_die, DW_AT_artificial, 1);
24560 tree_add_const_value_attribute_for_decl (const_die, decl);
24563 /* Generate a DIE to represent a label identifier. */
24565 static void
24566 gen_label_die (tree decl, dw_die_ref context_die)
24568 tree origin = decl_ultimate_origin (decl);
24569 dw_die_ref lbl_die = lookup_decl_die (decl);
24570 rtx insn;
24571 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24573 if (!lbl_die)
24575 lbl_die = new_die (DW_TAG_label, context_die, decl);
24576 equate_decl_number_to_die (decl, lbl_die);
24578 if (origin != NULL)
24579 add_abstract_origin_attribute (lbl_die, origin);
24580 else
24581 add_name_and_src_coords_attributes (lbl_die, decl);
24584 if (DECL_ABSTRACT_P (decl))
24585 equate_decl_number_to_die (decl, lbl_die);
24586 else if (! early_dwarf)
24588 insn = DECL_RTL_IF_SET (decl);
24590 /* Deleted labels are programmer specified labels which have been
24591 eliminated because of various optimizations. We still emit them
24592 here so that it is possible to put breakpoints on them. */
24593 if (insn
24594 && (LABEL_P (insn)
24595 || ((NOTE_P (insn)
24596 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24598 /* When optimization is enabled (via -O) some parts of the compiler
24599 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24600 represent source-level labels which were explicitly declared by
24601 the user. This really shouldn't be happening though, so catch
24602 it if it ever does happen. */
24603 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24605 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24606 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24608 else if (insn
24609 && NOTE_P (insn)
24610 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24611 && CODE_LABEL_NUMBER (insn) != -1)
24613 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24614 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24619 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24620 attributes to the DIE for a block STMT, to describe where the inlined
24621 function was called from. This is similar to add_src_coords_attributes. */
24623 static inline void
24624 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24626 /* We can end up with BUILTINS_LOCATION here. */
24627 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24628 return;
24630 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24632 if (dwarf_version >= 3 || !dwarf_strict)
24634 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24635 add_AT_unsigned (die, DW_AT_call_line, s.line);
24636 if (debug_column_info && s.column)
24637 add_AT_unsigned (die, DW_AT_call_column, s.column);
24642 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24643 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24645 static inline void
24646 add_high_low_attributes (tree stmt, dw_die_ref die)
24648 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24650 if (inline_entry_data **iedp
24651 = !inline_entry_data_table ? NULL
24652 : inline_entry_data_table->find_slot_with_hash (stmt,
24653 htab_hash_pointer (stmt),
24654 NO_INSERT))
24656 inline_entry_data *ied = *iedp;
24657 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24658 gcc_assert (debug_inline_points);
24659 gcc_assert (inlined_function_outer_scope_p (stmt));
24661 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24662 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24664 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24665 && !dwarf_strict)
24667 if (!output_asm_line_debug_info ())
24668 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24669 else
24671 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24672 /* FIXME: this will resolve to a small number. Could we
24673 possibly emit smaller data? Ideally we'd emit a
24674 uleb128, but that would make the size of DIEs
24675 impossible for the compiler to compute, since it's
24676 the assembler that computes the value of the view
24677 label in this case. Ideally, we'd have a single form
24678 encompassing both the address and the view, and
24679 indirecting them through a table might make things
24680 easier, but even that would be more wasteful,
24681 space-wise, than what we have now. */
24682 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24686 inline_entry_data_table->clear_slot (iedp);
24689 if (BLOCK_FRAGMENT_CHAIN (stmt)
24690 && (dwarf_version >= 3 || !dwarf_strict))
24692 tree chain, superblock = NULL_TREE;
24693 dw_die_ref pdie;
24694 dw_attr_node *attr = NULL;
24696 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24698 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24699 BLOCK_NUMBER (stmt));
24700 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24703 /* Optimize duplicate .debug_ranges lists or even tails of
24704 lists. If this BLOCK has same ranges as its supercontext,
24705 lookup DW_AT_ranges attribute in the supercontext (and
24706 recursively so), verify that the ranges_table contains the
24707 right values and use it instead of adding a new .debug_range. */
24708 for (chain = stmt, pdie = die;
24709 BLOCK_SAME_RANGE (chain);
24710 chain = BLOCK_SUPERCONTEXT (chain))
24712 dw_attr_node *new_attr;
24714 pdie = pdie->die_parent;
24715 if (pdie == NULL)
24716 break;
24717 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24718 break;
24719 new_attr = get_AT (pdie, DW_AT_ranges);
24720 if (new_attr == NULL
24721 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24722 break;
24723 attr = new_attr;
24724 superblock = BLOCK_SUPERCONTEXT (chain);
24726 if (attr != NULL
24727 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24728 == (int)BLOCK_NUMBER (superblock))
24729 && BLOCK_FRAGMENT_CHAIN (superblock))
24731 unsigned long off = attr->dw_attr_val.v.val_offset;
24732 unsigned long supercnt = 0, thiscnt = 0;
24733 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24734 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24736 ++supercnt;
24737 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24738 == (int)BLOCK_NUMBER (chain));
24740 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24741 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24742 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24743 ++thiscnt;
24744 gcc_assert (supercnt >= thiscnt);
24745 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24746 false);
24747 note_rnglist_head (off + supercnt - thiscnt);
24748 return;
24751 unsigned int offset = add_ranges (stmt, true);
24752 add_AT_range_list (die, DW_AT_ranges, offset, false);
24753 note_rnglist_head (offset);
24755 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24756 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24759 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24760 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24761 chain = BLOCK_FRAGMENT_CHAIN (chain);
24763 while (chain);
24764 add_ranges (NULL);
24766 else
24768 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24769 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24770 BLOCK_NUMBER (stmt));
24771 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24772 BLOCK_NUMBER (stmt));
24773 add_AT_low_high_pc (die, label, label_high, false);
24777 /* Generate a DIE for a lexical block. */
24779 static void
24780 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24782 dw_die_ref old_die = lookup_block_die (stmt);
24783 dw_die_ref stmt_die = NULL;
24784 if (!old_die)
24786 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24787 equate_block_to_die (stmt, stmt_die);
24790 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24792 /* If this is an inlined or conrecte instance, create a new lexical
24793 die for anything below to attach DW_AT_abstract_origin to. */
24794 if (old_die)
24795 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24797 tree origin = block_ultimate_origin (stmt);
24798 if (origin != NULL_TREE && (origin != stmt || old_die))
24799 add_abstract_origin_attribute (stmt_die, origin);
24801 old_die = NULL;
24804 if (old_die)
24805 stmt_die = old_die;
24807 /* A non abstract block whose blocks have already been reordered
24808 should have the instruction range for this block. If so, set the
24809 high/low attributes. */
24810 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24812 gcc_assert (stmt_die);
24813 add_high_low_attributes (stmt, stmt_die);
24816 decls_for_scope (stmt, stmt_die);
24819 /* Generate a DIE for an inlined subprogram. */
24821 static void
24822 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24824 tree decl = block_ultimate_origin (stmt);
24826 /* Make sure any inlined functions are known to be inlineable. */
24827 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24828 || cgraph_function_possibly_inlined_p (decl));
24830 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24832 if (call_arg_locations || debug_inline_points)
24833 equate_block_to_die (stmt, subr_die);
24834 add_abstract_origin_attribute (subr_die, decl);
24835 if (TREE_ASM_WRITTEN (stmt))
24836 add_high_low_attributes (stmt, subr_die);
24837 add_call_src_coords_attributes (stmt, subr_die);
24839 /* The inliner creates an extra BLOCK for the parameter setup,
24840 we want to merge that with the actual outermost BLOCK of the
24841 inlined function to avoid duplicate locals in consumers.
24842 Do that by doing the recursion to subblocks on the single subblock
24843 of STMT. */
24844 bool unwrap_one = false;
24845 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24847 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24848 if (origin
24849 && TREE_CODE (origin) == BLOCK
24850 && BLOCK_SUPERCONTEXT (origin) == decl)
24851 unwrap_one = true;
24853 decls_for_scope (stmt, subr_die, !unwrap_one);
24854 if (unwrap_one)
24855 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24858 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24859 the comment for VLR_CONTEXT. */
24861 static void
24862 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24864 dw_die_ref decl_die;
24866 if (TREE_TYPE (decl) == error_mark_node)
24867 return;
24869 decl_die = new_die (DW_TAG_member, context_die, decl);
24870 add_name_and_src_coords_attributes (decl_die, decl);
24871 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24872 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24873 context_die);
24875 if (DECL_BIT_FIELD_TYPE (decl))
24877 add_byte_size_attribute (decl_die, decl);
24878 add_bit_size_attribute (decl_die, decl);
24879 add_bit_offset_attribute (decl_die, decl);
24882 add_alignment_attribute (decl_die, decl);
24884 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24885 add_data_member_location_attribute (decl_die, decl, ctx);
24887 if (DECL_ARTIFICIAL (decl))
24888 add_AT_flag (decl_die, DW_AT_artificial, 1);
24890 add_accessibility_attribute (decl_die, decl);
24892 /* Equate decl number to die, so that we can look up this decl later on. */
24893 equate_decl_number_to_die (decl, decl_die);
24896 /* Generate a DIE for a pointer to a member type. TYPE can be an
24897 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24898 pointer to member function. */
24900 static void
24901 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24903 if (lookup_type_die (type))
24904 return;
24906 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24907 scope_die_for (type, context_die), type);
24909 equate_type_number_to_die (type, ptr_die);
24910 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24911 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24912 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24913 context_die);
24914 add_alignment_attribute (ptr_die, type);
24916 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24917 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24919 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24920 add_AT_loc (ptr_die, DW_AT_use_location, op);
24924 static char *producer_string;
24926 /* Given a C and/or C++ language/version string return the "highest".
24927 C++ is assumed to be "higher" than C in this case. Used for merging
24928 LTO translation unit languages. */
24929 static const char *
24930 highest_c_language (const char *lang1, const char *lang2)
24932 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24933 return "GNU C++23";
24934 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24935 return "GNU C++20";
24936 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24937 return "GNU C++17";
24938 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24939 return "GNU C++14";
24940 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24941 return "GNU C++11";
24942 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24943 return "GNU C++98";
24945 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24946 return "GNU C2X";
24947 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24948 return "GNU C17";
24949 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24950 return "GNU C11";
24951 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24952 return "GNU C99";
24953 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24954 return "GNU C89";
24956 gcc_unreachable ();
24960 /* Generate the DIE for the compilation unit. */
24962 static dw_die_ref
24963 gen_compile_unit_die (const char *filename)
24965 dw_die_ref die;
24966 const char *language_string = lang_hooks.name;
24967 int language;
24969 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24971 if (filename)
24973 add_filename_attribute (die, filename);
24974 /* Don't add cwd for <built-in>. */
24975 if (filename[0] != '<')
24976 add_comp_dir_attribute (die);
24979 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24981 /* If our producer is LTO try to figure out a common language to use
24982 from the global list of translation units. */
24983 if (strcmp (language_string, "GNU GIMPLE") == 0)
24985 unsigned i;
24986 tree t;
24987 const char *common_lang = NULL;
24989 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24991 if (!TRANSLATION_UNIT_LANGUAGE (t))
24992 continue;
24993 if (!common_lang)
24994 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24995 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24997 else if (startswith (common_lang, "GNU C")
24998 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
24999 /* Mixing C and C++ is ok, use C++ in that case. */
25000 common_lang = highest_c_language (common_lang,
25001 TRANSLATION_UNIT_LANGUAGE (t));
25002 else
25004 /* Fall back to C. */
25005 common_lang = NULL;
25006 break;
25010 if (common_lang)
25011 language_string = common_lang;
25014 language = DW_LANG_C;
25015 if (startswith (language_string, "GNU C")
25016 && ISDIGIT (language_string[5]))
25018 language = DW_LANG_C89;
25019 if (dwarf_version >= 3 || !dwarf_strict)
25021 if (strcmp (language_string, "GNU C89") != 0)
25022 language = DW_LANG_C99;
25024 if (dwarf_version >= 5 /* || !dwarf_strict */)
25025 if (strcmp (language_string, "GNU C11") == 0
25026 || strcmp (language_string, "GNU C17") == 0
25027 || strcmp (language_string, "GNU C2X") == 0)
25028 language = DW_LANG_C11;
25031 else if (startswith (language_string, "GNU C++"))
25033 language = DW_LANG_C_plus_plus;
25034 if (dwarf_version >= 5 /* || !dwarf_strict */)
25036 if (strcmp (language_string, "GNU C++11") == 0)
25037 language = DW_LANG_C_plus_plus_11;
25038 else if (strcmp (language_string, "GNU C++14") == 0)
25039 language = DW_LANG_C_plus_plus_14;
25040 else if (strcmp (language_string, "GNU C++17") == 0
25041 || strcmp (language_string, "GNU C++20") == 0
25042 || strcmp (language_string, "GNU C++23") == 0)
25043 /* For now. */
25044 language = DW_LANG_C_plus_plus_14;
25047 else if (strcmp (language_string, "GNU F77") == 0)
25048 language = DW_LANG_Fortran77;
25049 else if (dwarf_version >= 3 || !dwarf_strict)
25051 if (strcmp (language_string, "GNU Ada") == 0)
25052 language = DW_LANG_Ada95;
25053 else if (startswith (language_string, "GNU Fortran"))
25055 language = DW_LANG_Fortran95;
25056 if (dwarf_version >= 5 /* || !dwarf_strict */)
25058 if (strcmp (language_string, "GNU Fortran2003") == 0)
25059 language = DW_LANG_Fortran03;
25060 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25061 language = DW_LANG_Fortran08;
25064 else if (strcmp (language_string, "GNU Objective-C") == 0)
25065 language = DW_LANG_ObjC;
25066 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25067 language = DW_LANG_ObjC_plus_plus;
25068 else if (strcmp (language_string, "GNU D") == 0)
25069 language = DW_LANG_D;
25070 else if (dwarf_version >= 5 || !dwarf_strict)
25072 if (strcmp (language_string, "GNU Go") == 0)
25073 language = DW_LANG_Go;
25076 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25077 else if (startswith (language_string, "GNU Fortran"))
25078 language = DW_LANG_Fortran90;
25079 /* Likewise for Ada. */
25080 else if (strcmp (language_string, "GNU Ada") == 0)
25081 language = DW_LANG_Ada83;
25083 add_AT_unsigned (die, DW_AT_language, language);
25085 switch (language)
25087 case DW_LANG_Fortran77:
25088 case DW_LANG_Fortran90:
25089 case DW_LANG_Fortran95:
25090 case DW_LANG_Fortran03:
25091 case DW_LANG_Fortran08:
25092 /* Fortran has case insensitive identifiers and the front-end
25093 lowercases everything. */
25094 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25095 break;
25096 default:
25097 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25098 break;
25100 return die;
25103 /* Generate the DIE for a base class. */
25105 static void
25106 gen_inheritance_die (tree binfo, tree access, tree type,
25107 dw_die_ref context_die)
25109 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25110 struct vlr_context ctx = { type, NULL };
25112 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25113 context_die);
25114 add_data_member_location_attribute (die, binfo, &ctx);
25116 if (BINFO_VIRTUAL_P (binfo))
25117 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25119 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25120 children, otherwise the default is DW_ACCESS_public. In DWARF2
25121 the default has always been DW_ACCESS_private. */
25122 if (access == access_public_node)
25124 if (dwarf_version == 2
25125 || context_die->die_tag == DW_TAG_class_type)
25126 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25128 else if (access == access_protected_node)
25129 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25130 else if (dwarf_version > 2
25131 && context_die->die_tag != DW_TAG_class_type)
25132 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25135 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25136 structure. */
25138 static bool
25139 is_variant_part (tree decl)
25141 return (TREE_CODE (decl) == FIELD_DECL
25142 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25145 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25146 return the FIELD_DECL. Return NULL_TREE otherwise. */
25148 static tree
25149 analyze_discr_in_predicate (tree operand, tree struct_type)
25151 while (CONVERT_EXPR_P (operand))
25152 operand = TREE_OPERAND (operand, 0);
25154 /* Match field access to members of struct_type only. */
25155 if (TREE_CODE (operand) == COMPONENT_REF
25156 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25157 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25158 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25159 return TREE_OPERAND (operand, 1);
25160 else
25161 return NULL_TREE;
25164 /* Check that SRC is a constant integer that can be represented as a native
25165 integer constant (either signed or unsigned). If so, store it into DEST and
25166 return true. Return false otherwise. */
25168 static bool
25169 get_discr_value (tree src, dw_discr_value *dest)
25171 tree discr_type = TREE_TYPE (src);
25173 if (lang_hooks.types.get_debug_type)
25175 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25176 if (debug_type != NULL)
25177 discr_type = debug_type;
25180 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25181 return false;
25183 /* Signedness can vary between the original type and the debug type. This
25184 can happen for character types in Ada for instance: the character type
25185 used for code generation can be signed, to be compatible with the C one,
25186 but from a debugger point of view, it must be unsigned. */
25187 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25188 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25190 if (is_orig_unsigned != is_debug_unsigned)
25191 src = fold_convert (discr_type, src);
25193 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25194 return false;
25196 dest->pos = is_debug_unsigned;
25197 if (is_debug_unsigned)
25198 dest->v.uval = tree_to_uhwi (src);
25199 else
25200 dest->v.sval = tree_to_shwi (src);
25202 return true;
25205 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25206 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25207 store NULL_TREE in DISCR_DECL. Otherwise:
25209 - store the discriminant field in STRUCT_TYPE that controls the variant
25210 part to *DISCR_DECL
25212 - put in *DISCR_LISTS_P an array where for each variant, the item
25213 represents the corresponding matching list of discriminant values.
25215 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25216 the above array.
25218 Note that when the array is allocated (i.e. when the analysis is
25219 successful), it is up to the caller to free the array. */
25221 static void
25222 analyze_variants_discr (tree variant_part_decl,
25223 tree struct_type,
25224 tree *discr_decl,
25225 dw_discr_list_ref **discr_lists_p,
25226 unsigned *discr_lists_length)
25228 tree variant_part_type = TREE_TYPE (variant_part_decl);
25229 tree variant;
25230 dw_discr_list_ref *discr_lists;
25231 unsigned i;
25233 /* Compute how many variants there are in this variant part. */
25234 *discr_lists_length = 0;
25235 for (variant = TYPE_FIELDS (variant_part_type);
25236 variant != NULL_TREE;
25237 variant = DECL_CHAIN (variant))
25238 ++*discr_lists_length;
25240 *discr_decl = NULL_TREE;
25241 *discr_lists_p
25242 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25243 sizeof (**discr_lists_p));
25244 discr_lists = *discr_lists_p;
25246 /* And then analyze all variants to extract discriminant information for all
25247 of them. This analysis is conservative: as soon as we detect something we
25248 do not support, abort everything and pretend we found nothing. */
25249 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25250 variant != NULL_TREE;
25251 variant = DECL_CHAIN (variant), ++i)
25253 tree match_expr = DECL_QUALIFIER (variant);
25255 /* Now, try to analyze the predicate and deduce a discriminant for
25256 it. */
25257 if (match_expr == boolean_true_node)
25258 /* Typically happens for the default variant: it matches all cases that
25259 previous variants rejected. Don't output any matching value for
25260 this one. */
25261 continue;
25263 /* The following loop tries to iterate over each discriminant
25264 possibility: single values or ranges. */
25265 while (match_expr != NULL_TREE)
25267 tree next_round_match_expr;
25268 tree candidate_discr = NULL_TREE;
25269 dw_discr_list_ref new_node = NULL;
25271 /* Possibilities are matched one after the other by nested
25272 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25273 continue with the rest at next iteration. */
25274 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25276 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25277 match_expr = TREE_OPERAND (match_expr, 1);
25279 else
25280 next_round_match_expr = NULL_TREE;
25282 if (match_expr == boolean_false_node)
25283 /* This sub-expression matches nothing: just wait for the next
25284 one. */
25287 else if (TREE_CODE (match_expr) == EQ_EXPR)
25289 /* We are matching: <discr_field> == <integer_cst>
25290 This sub-expression matches a single value. */
25291 tree integer_cst = TREE_OPERAND (match_expr, 1);
25293 candidate_discr
25294 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25295 struct_type);
25297 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25298 if (!get_discr_value (integer_cst,
25299 &new_node->dw_discr_lower_bound))
25300 goto abort;
25301 new_node->dw_discr_range = false;
25304 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25306 /* We are matching:
25307 <discr_field> > <integer_cst>
25308 && <discr_field> < <integer_cst>.
25309 This sub-expression matches the range of values between the
25310 two matched integer constants. Note that comparisons can be
25311 inclusive or exclusive. */
25312 tree candidate_discr_1, candidate_discr_2;
25313 tree lower_cst, upper_cst;
25314 bool lower_cst_included, upper_cst_included;
25315 tree lower_op = TREE_OPERAND (match_expr, 0);
25316 tree upper_op = TREE_OPERAND (match_expr, 1);
25318 /* When the comparison is exclusive, the integer constant is not
25319 the discriminant range bound we are looking for: we will have
25320 to increment or decrement it. */
25321 if (TREE_CODE (lower_op) == GE_EXPR)
25322 lower_cst_included = true;
25323 else if (TREE_CODE (lower_op) == GT_EXPR)
25324 lower_cst_included = false;
25325 else
25326 goto abort;
25328 if (TREE_CODE (upper_op) == LE_EXPR)
25329 upper_cst_included = true;
25330 else if (TREE_CODE (upper_op) == LT_EXPR)
25331 upper_cst_included = false;
25332 else
25333 goto abort;
25335 /* Extract the discriminant from the first operand and check it
25336 is consistant with the same analysis in the second
25337 operand. */
25338 candidate_discr_1
25339 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25340 struct_type);
25341 candidate_discr_2
25342 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25343 struct_type);
25344 if (candidate_discr_1 == candidate_discr_2)
25345 candidate_discr = candidate_discr_1;
25346 else
25347 goto abort;
25349 /* Extract bounds from both. */
25350 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25351 lower_cst = TREE_OPERAND (lower_op, 1);
25352 upper_cst = TREE_OPERAND (upper_op, 1);
25354 if (!lower_cst_included)
25355 lower_cst
25356 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25357 build_int_cst (TREE_TYPE (lower_cst), 1));
25358 if (!upper_cst_included)
25359 upper_cst
25360 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25361 build_int_cst (TREE_TYPE (upper_cst), 1));
25363 if (!get_discr_value (lower_cst,
25364 &new_node->dw_discr_lower_bound)
25365 || !get_discr_value (upper_cst,
25366 &new_node->dw_discr_upper_bound))
25367 goto abort;
25369 new_node->dw_discr_range = true;
25372 else if ((candidate_discr
25373 = analyze_discr_in_predicate (match_expr, struct_type))
25374 && (TREE_TYPE (candidate_discr) == boolean_type_node
25375 || TREE_TYPE (TREE_TYPE (candidate_discr))
25376 == boolean_type_node))
25378 /* We are matching: <discr_field> for a boolean discriminant.
25379 This sub-expression matches boolean_true_node. */
25380 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25381 if (!get_discr_value (boolean_true_node,
25382 &new_node->dw_discr_lower_bound))
25383 goto abort;
25384 new_node->dw_discr_range = false;
25387 else
25388 /* Unsupported sub-expression: we cannot determine the set of
25389 matching discriminant values. Abort everything. */
25390 goto abort;
25392 /* If the discriminant info is not consistant with what we saw so
25393 far, consider the analysis failed and abort everything. */
25394 if (candidate_discr == NULL_TREE
25395 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25396 goto abort;
25397 else
25398 *discr_decl = candidate_discr;
25400 if (new_node != NULL)
25402 new_node->dw_discr_next = discr_lists[i];
25403 discr_lists[i] = new_node;
25405 match_expr = next_round_match_expr;
25409 /* If we reach this point, we could match everything we were interested
25410 in. */
25411 return;
25413 abort:
25414 /* Clean all data structure and return no result. */
25415 free (*discr_lists_p);
25416 *discr_lists_p = NULL;
25417 *discr_decl = NULL_TREE;
25420 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25421 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25422 under CONTEXT_DIE.
25424 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25425 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25426 this type, which are record types, represent the available variants and each
25427 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25428 values are inferred from these attributes.
25430 In trees, the offsets for the fields inside these sub-records are relative
25431 to the variant part itself, whereas the corresponding DIEs should have
25432 offset attributes that are relative to the embedding record base address.
25433 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25434 must be an expression that computes the offset of the variant part to
25435 describe in DWARF. */
25437 static void
25438 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25439 dw_die_ref context_die)
25441 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25442 tree variant_part_offset = vlr_ctx->variant_part_offset;
25444 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25445 NULL_TREE if there is no such field. */
25446 tree discr_decl = NULL_TREE;
25447 dw_discr_list_ref *discr_lists;
25448 unsigned discr_lists_length = 0;
25449 unsigned i;
25451 dw_die_ref dwarf_proc_die = NULL;
25452 dw_die_ref variant_part_die
25453 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25455 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25457 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25458 &discr_decl, &discr_lists, &discr_lists_length);
25460 if (discr_decl != NULL_TREE)
25462 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25464 if (discr_die)
25465 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25466 else
25467 /* We have no DIE for the discriminant, so just discard all
25468 discrimimant information in the output. */
25469 discr_decl = NULL_TREE;
25472 /* If the offset for this variant part is more complex than a constant,
25473 create a DWARF procedure for it so that we will not have to generate
25474 DWARF expressions for it for each member. */
25475 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25476 && (dwarf_version >= 3 || !dwarf_strict))
25478 struct loc_descr_context ctx = {
25479 vlr_ctx->struct_type, /* context_type */
25480 NULL_TREE, /* base_decl */
25481 NULL, /* dpi */
25482 false, /* placeholder_arg */
25483 false, /* placeholder_seen */
25484 false /* strict_signedness */
25486 const tree dwarf_proc_fndecl
25487 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25488 build_function_type (TREE_TYPE (variant_part_offset),
25489 NULL_TREE));
25490 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25491 const dw_loc_descr_ref dwarf_proc_body
25492 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25494 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25495 dwarf_proc_fndecl, context_die);
25496 if (dwarf_proc_die != NULL)
25497 variant_part_offset = dwarf_proc_call;
25500 /* Output DIEs for all variants. */
25501 i = 0;
25502 for (tree variant = TYPE_FIELDS (variant_part_type);
25503 variant != NULL_TREE;
25504 variant = DECL_CHAIN (variant), ++i)
25506 tree variant_type = TREE_TYPE (variant);
25507 dw_die_ref variant_die;
25509 /* All variants (i.e. members of a variant part) are supposed to be
25510 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25511 under these records. */
25512 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25514 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25515 equate_decl_number_to_die (variant, variant_die);
25517 /* Output discriminant values this variant matches, if any. */
25518 if (discr_decl == NULL || discr_lists[i] == NULL)
25519 /* In the case we have discriminant information at all, this is
25520 probably the default variant: as the standard says, don't
25521 output any discriminant value/list attribute. */
25523 else if (discr_lists[i]->dw_discr_next == NULL
25524 && !discr_lists[i]->dw_discr_range)
25525 /* If there is only one accepted value, don't bother outputting a
25526 list. */
25527 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25528 else
25529 add_discr_list (variant_die, discr_lists[i]);
25531 for (tree member = TYPE_FIELDS (variant_type);
25532 member != NULL_TREE;
25533 member = DECL_CHAIN (member))
25535 struct vlr_context vlr_sub_ctx = {
25536 vlr_ctx->struct_type, /* struct_type */
25537 NULL /* variant_part_offset */
25539 if (is_variant_part (member))
25541 /* All offsets for fields inside variant parts are relative to
25542 the top-level embedding RECORD_TYPE's base address. On the
25543 other hand, offsets in GCC's types are relative to the
25544 nested-most variant part. So we have to sum offsets each time
25545 we recurse. */
25547 vlr_sub_ctx.variant_part_offset
25548 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25549 variant_part_offset, byte_position (member));
25550 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25552 else
25554 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25555 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25560 free (discr_lists);
25563 /* Generate a DIE for a class member. */
25565 static void
25566 gen_member_die (tree type, dw_die_ref context_die)
25568 tree member;
25569 tree binfo = TYPE_BINFO (type);
25571 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25573 /* If this is not an incomplete type, output descriptions of each of its
25574 members. Note that as we output the DIEs necessary to represent the
25575 members of this record or union type, we will also be trying to output
25576 DIEs to represent the *types* of those members. However the `type'
25577 function (above) will specifically avoid generating type DIEs for member
25578 types *within* the list of member DIEs for this (containing) type except
25579 for those types (of members) which are explicitly marked as also being
25580 members of this (containing) type themselves. The g++ front- end can
25581 force any given type to be treated as a member of some other (containing)
25582 type by setting the TYPE_CONTEXT of the given (member) type to point to
25583 the TREE node representing the appropriate (containing) type. */
25585 /* First output info about the base classes. */
25586 if (binfo && early_dwarf)
25588 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25589 int i;
25590 tree base;
25592 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25593 gen_inheritance_die (base,
25594 (accesses ? (*accesses)[i] : access_public_node),
25595 type,
25596 context_die);
25599 /* Now output info about the members. */
25600 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25602 /* Ignore clones. */
25603 if (DECL_ABSTRACT_ORIGIN (member))
25604 continue;
25606 struct vlr_context vlr_ctx = { type, NULL_TREE };
25607 bool static_inline_p
25608 = (VAR_P (member)
25609 && TREE_STATIC (member)
25610 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25611 != -1));
25613 /* If we thought we were generating minimal debug info for TYPE
25614 and then changed our minds, some of the member declarations
25615 may have already been defined. Don't define them again, but
25616 do put them in the right order. */
25618 if (dw_die_ref child = lookup_decl_die (member))
25620 /* Handle inline static data members, which only have in-class
25621 declarations. */
25622 bool splice = true;
25624 dw_die_ref ref = NULL;
25625 if (child->die_tag == DW_TAG_variable
25626 && child->die_parent == comp_unit_die ())
25628 ref = get_AT_ref (child, DW_AT_specification);
25630 /* For C++17 inline static data members followed by redundant
25631 out of class redeclaration, we might get here with
25632 child being the DIE created for the out of class
25633 redeclaration and with its DW_AT_specification being
25634 the DIE created for in-class definition. We want to
25635 reparent the latter, and don't want to create another
25636 DIE with DW_AT_specification in that case, because
25637 we already have one. */
25638 if (ref
25639 && static_inline_p
25640 && ref->die_tag == DW_TAG_variable
25641 && ref->die_parent == comp_unit_die ()
25642 && get_AT (ref, DW_AT_specification) == NULL)
25644 child = ref;
25645 ref = NULL;
25646 static_inline_p = false;
25649 if (!ref)
25651 reparent_child (child, context_die);
25652 if (dwarf_version < 5)
25653 child->die_tag = DW_TAG_member;
25654 splice = false;
25657 else if (child->die_tag == DW_TAG_enumerator)
25658 /* Enumerators remain under their enumeration even if
25659 their names are introduced in the enclosing scope. */
25660 splice = false;
25662 if (splice)
25663 splice_child_die (context_die, child);
25666 /* Do not generate DWARF for variant parts if we are generating the
25667 corresponding GNAT encodings: DIEs generated for the two schemes
25668 would conflict in our mappings. */
25669 else if (is_variant_part (member)
25670 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25672 vlr_ctx.variant_part_offset = byte_position (member);
25673 gen_variant_part (member, &vlr_ctx, context_die);
25675 else
25677 vlr_ctx.variant_part_offset = NULL_TREE;
25678 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25681 /* For C++ inline static data members emit immediately a DW_TAG_variable
25682 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25683 DW_AT_specification. */
25684 if (static_inline_p)
25686 int old_extern = DECL_EXTERNAL (member);
25687 DECL_EXTERNAL (member) = 0;
25688 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25689 DECL_EXTERNAL (member) = old_extern;
25694 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25695 is set, we pretend that the type was never defined, so we only get the
25696 member DIEs needed by later specification DIEs. */
25698 static void
25699 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25700 enum debug_info_usage usage)
25702 if (TREE_ASM_WRITTEN (type))
25704 /* Fill in the bound of variable-length fields in late dwarf if
25705 still incomplete. */
25706 if (!early_dwarf && variably_modified_type_p (type, NULL))
25707 for (tree member = TYPE_FIELDS (type);
25708 member;
25709 member = DECL_CHAIN (member))
25710 fill_variable_array_bounds (TREE_TYPE (member));
25711 return;
25714 dw_die_ref type_die = lookup_type_die (type);
25715 dw_die_ref scope_die = 0;
25716 int nested = 0;
25717 int complete = (TYPE_SIZE (type)
25718 && (! TYPE_STUB_DECL (type)
25719 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25720 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25721 complete = complete && should_emit_struct_debug (type, usage);
25723 if (type_die && ! complete)
25724 return;
25726 if (TYPE_CONTEXT (type) != NULL_TREE
25727 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25728 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25729 nested = 1;
25731 scope_die = scope_die_for (type, context_die);
25733 /* Generate child dies for template parameters. */
25734 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25735 schedule_generic_params_dies_gen (type);
25737 if (! type_die || (nested && is_cu_die (scope_die)))
25738 /* First occurrence of type or toplevel definition of nested class. */
25740 dw_die_ref old_die = type_die;
25742 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25743 ? record_type_tag (type) : DW_TAG_union_type,
25744 scope_die, type);
25745 equate_type_number_to_die (type, type_die);
25746 if (old_die)
25747 add_AT_specification (type_die, old_die);
25748 else
25749 add_name_attribute (type_die, type_tag (type));
25751 else
25752 remove_AT (type_die, DW_AT_declaration);
25754 /* If this type has been completed, then give it a byte_size attribute and
25755 then give a list of members. */
25756 if (complete && !ns_decl)
25758 /* Prevent infinite recursion in cases where the type of some member of
25759 this type is expressed in terms of this type itself. */
25760 TREE_ASM_WRITTEN (type) = 1;
25761 add_byte_size_attribute (type_die, type);
25762 add_alignment_attribute (type_die, type);
25763 if (TYPE_STUB_DECL (type) != NULL_TREE)
25765 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25766 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25769 /* If the first reference to this type was as the return type of an
25770 inline function, then it may not have a parent. Fix this now. */
25771 if (type_die->die_parent == NULL)
25772 add_child_die (scope_die, type_die);
25774 gen_member_die (type, type_die);
25776 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25777 if (TYPE_ARTIFICIAL (type))
25778 add_AT_flag (type_die, DW_AT_artificial, 1);
25780 /* GNU extension: Record what type our vtable lives in. */
25781 if (TYPE_VFIELD (type))
25783 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25785 gen_type_die (vtype, context_die);
25786 add_AT_die_ref (type_die, DW_AT_containing_type,
25787 lookup_type_die (vtype));
25790 else
25792 add_AT_flag (type_die, DW_AT_declaration, 1);
25794 /* We don't need to do this for function-local types. */
25795 if (TYPE_STUB_DECL (type)
25796 && ! decl_function_context (TYPE_STUB_DECL (type)))
25797 vec_safe_push (incomplete_types, type);
25800 if (get_AT (type_die, DW_AT_name))
25801 add_pubtype (type, type_die);
25804 /* Generate a DIE for a subroutine _type_. */
25806 static void
25807 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25809 tree return_type = TREE_TYPE (type);
25810 dw_die_ref subr_die
25811 = new_die (DW_TAG_subroutine_type,
25812 scope_die_for (type, context_die), type);
25814 equate_type_number_to_die (type, subr_die);
25815 add_prototyped_attribute (subr_die, type);
25816 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25817 context_die);
25818 add_alignment_attribute (subr_die, type);
25819 gen_formal_types_die (type, subr_die);
25821 if (get_AT (subr_die, DW_AT_name))
25822 add_pubtype (type, subr_die);
25823 if ((dwarf_version >= 5 || !dwarf_strict)
25824 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25825 add_AT_flag (subr_die, DW_AT_reference, 1);
25826 if ((dwarf_version >= 5 || !dwarf_strict)
25827 && lang_hooks.types.type_dwarf_attribute (type,
25828 DW_AT_rvalue_reference) != -1)
25829 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25832 /* Generate a DIE for a type definition. */
25834 static void
25835 gen_typedef_die (tree decl, dw_die_ref context_die)
25837 dw_die_ref type_die;
25838 tree type;
25840 if (TREE_ASM_WRITTEN (decl))
25842 if (DECL_ORIGINAL_TYPE (decl))
25843 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25844 return;
25847 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25848 checks in process_scope_var and modified_type_die), this should be called
25849 only for original types. */
25850 gcc_assert (decl_ultimate_origin (decl) == NULL
25851 || decl_ultimate_origin (decl) == decl);
25853 TREE_ASM_WRITTEN (decl) = 1;
25854 type_die = new_die (DW_TAG_typedef, context_die, decl);
25856 add_name_and_src_coords_attributes (type_die, decl);
25857 if (DECL_ORIGINAL_TYPE (decl))
25859 type = DECL_ORIGINAL_TYPE (decl);
25860 if (type == error_mark_node)
25861 return;
25863 gcc_assert (type != TREE_TYPE (decl));
25864 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25866 else
25868 type = TREE_TYPE (decl);
25869 if (type == error_mark_node)
25870 return;
25872 if (is_naming_typedef_decl (TYPE_NAME (type)))
25874 /* Here, we are in the case of decl being a typedef naming
25875 an anonymous type, e.g:
25876 typedef struct {...} foo;
25877 In that case TREE_TYPE (decl) is not a typedef variant
25878 type and TYPE_NAME of the anonymous type is set to the
25879 TYPE_DECL of the typedef. This construct is emitted by
25880 the C++ FE.
25882 TYPE is the anonymous struct named by the typedef
25883 DECL. As we need the DW_AT_type attribute of the
25884 DW_TAG_typedef to point to the DIE of TYPE, let's
25885 generate that DIE right away. add_type_attribute
25886 called below will then pick (via lookup_type_die) that
25887 anonymous struct DIE. */
25888 if (!TREE_ASM_WRITTEN (type))
25889 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25891 /* This is a GNU Extension. We are adding a
25892 DW_AT_linkage_name attribute to the DIE of the
25893 anonymous struct TYPE. The value of that attribute
25894 is the name of the typedef decl naming the anonymous
25895 struct. This greatly eases the work of consumers of
25896 this debug info. */
25897 add_linkage_name_raw (lookup_type_die (type), decl);
25901 add_type_attribute (type_die, type, decl_quals (decl), false,
25902 context_die);
25904 if (is_naming_typedef_decl (decl))
25905 /* We want that all subsequent calls to lookup_type_die with
25906 TYPE in argument yield the DW_TAG_typedef we have just
25907 created. */
25908 equate_type_number_to_die (type, type_die);
25910 add_alignment_attribute (type_die, TREE_TYPE (decl));
25912 add_accessibility_attribute (type_die, decl);
25914 if (DECL_ABSTRACT_P (decl))
25915 equate_decl_number_to_die (decl, type_die);
25917 if (get_AT (type_die, DW_AT_name))
25918 add_pubtype (decl, type_die);
25921 /* Generate a DIE for a struct, class, enum or union type. */
25923 static void
25924 gen_tagged_type_die (tree type,
25925 dw_die_ref context_die,
25926 enum debug_info_usage usage)
25928 if (type == NULL_TREE
25929 || !is_tagged_type (type))
25930 return;
25932 if (TREE_ASM_WRITTEN (type))
25934 /* If this is a nested type whose containing class hasn't been written
25935 out yet, writing it out will cover this one, too. This does not apply
25936 to instantiations of member class templates; they need to be added to
25937 the containing class as they are generated. FIXME: This hurts the
25938 idea of combining type decls from multiple TUs, since we can't predict
25939 what set of template instantiations we'll get. */
25940 else if (TYPE_CONTEXT (type)
25941 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25942 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25944 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25946 if (TREE_ASM_WRITTEN (type))
25947 return;
25949 /* If that failed, attach ourselves to the stub. */
25950 context_die = lookup_type_die (TYPE_CONTEXT (type));
25952 else if (TYPE_CONTEXT (type) != NULL_TREE
25953 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25955 /* If this type is local to a function that hasn't been written
25956 out yet, use a NULL context for now; it will be fixed up in
25957 decls_for_scope. */
25958 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25959 /* A declaration DIE doesn't count; nested types need to go in the
25960 specification. */
25961 if (context_die && is_declaration_die (context_die))
25962 context_die = NULL;
25964 else
25965 context_die = declare_in_namespace (type, context_die);
25967 if (TREE_CODE (type) == ENUMERAL_TYPE)
25969 /* This might have been written out by the call to
25970 declare_in_namespace. */
25971 if (!TREE_ASM_WRITTEN (type))
25972 gen_enumeration_type_die (type, context_die);
25974 else
25975 gen_struct_or_union_type_die (type, context_die, usage);
25977 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25978 it up if it is ever completed. gen_*_type_die will set it for us
25979 when appropriate. */
25982 /* Generate a type description DIE. */
25984 static void
25985 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25986 enum debug_info_usage usage)
25988 struct array_descr_info info;
25990 if (type == NULL_TREE || type == error_mark_node)
25991 return;
25993 if (flag_checking && type)
25994 verify_type (type);
25996 if (TYPE_NAME (type) != NULL_TREE
25997 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25998 && is_redundant_typedef (TYPE_NAME (type))
25999 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
26000 /* The DECL of this type is a typedef we don't want to emit debug
26001 info for but we want debug info for its underlying typedef.
26002 This can happen for e.g, the injected-class-name of a C++
26003 type. */
26004 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26006 /* If TYPE is a typedef type variant, let's generate debug info
26007 for the parent typedef which TYPE is a type of. */
26008 if (typedef_variant_p (type))
26010 if (TREE_ASM_WRITTEN (type))
26011 return;
26013 tree name = TYPE_NAME (type);
26014 tree origin = decl_ultimate_origin (name);
26015 if (origin != NULL && origin != name)
26017 gen_decl_die (origin, NULL, NULL, context_die);
26018 return;
26021 /* Prevent broken recursion; we can't hand off to the same type. */
26022 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26024 /* Give typedefs the right scope. */
26025 context_die = scope_die_for (type, context_die);
26027 TREE_ASM_WRITTEN (type) = 1;
26029 gen_decl_die (name, NULL, NULL, context_die);
26030 return;
26033 /* If type is an anonymous tagged type named by a typedef, let's
26034 generate debug info for the typedef. */
26035 if (is_naming_typedef_decl (TYPE_NAME (type)))
26037 /* Give typedefs the right scope. */
26038 context_die = scope_die_for (type, context_die);
26040 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26041 return;
26044 if (lang_hooks.types.get_debug_type)
26046 tree debug_type = lang_hooks.types.get_debug_type (type);
26048 if (debug_type != NULL_TREE && debug_type != type)
26050 gen_type_die_with_usage (debug_type, context_die, usage);
26051 return;
26055 /* We are going to output a DIE to represent the unqualified version
26056 of this type (i.e. without any const or volatile qualifiers) so
26057 get the main variant (i.e. the unqualified version) of this type
26058 now. (Vectors and arrays are special because the debugging info is in the
26059 cloned type itself. Similarly function/method types can contain extra
26060 ref-qualification). */
26061 if (TREE_CODE (type) == FUNCTION_TYPE
26062 || TREE_CODE (type) == METHOD_TYPE)
26064 /* For function/method types, can't use type_main_variant here,
26065 because that can have different ref-qualifiers for C++,
26066 but try to canonicalize. */
26067 tree main = TYPE_MAIN_VARIANT (type);
26068 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26069 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26070 && check_base_type (t, main)
26071 && check_lang_type (t, type))
26073 type = t;
26074 break;
26077 else if (TREE_CODE (type) != VECTOR_TYPE
26078 && TREE_CODE (type) != ARRAY_TYPE)
26079 type = type_main_variant (type);
26081 /* If this is an array type with hidden descriptor, handle it first. */
26082 if (!TREE_ASM_WRITTEN (type)
26083 && lang_hooks.types.get_array_descr_info)
26085 memset (&info, 0, sizeof (info));
26086 if (lang_hooks.types.get_array_descr_info (type, &info))
26088 /* Fortran sometimes emits array types with no dimension. */
26089 gcc_assert (info.ndimensions >= 0
26090 && (info.ndimensions
26091 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26092 gen_descr_array_type_die (type, &info, context_die);
26093 TREE_ASM_WRITTEN (type) = 1;
26094 return;
26098 if (TREE_ASM_WRITTEN (type))
26100 /* Variable-length types may be incomplete even if
26101 TREE_ASM_WRITTEN. For such types, fall through to
26102 gen_array_type_die() and possibly fill in
26103 DW_AT_{upper,lower}_bound attributes. */
26104 if ((TREE_CODE (type) != ARRAY_TYPE
26105 && TREE_CODE (type) != RECORD_TYPE
26106 && TREE_CODE (type) != UNION_TYPE
26107 && TREE_CODE (type) != QUAL_UNION_TYPE)
26108 || !variably_modified_type_p (type, NULL))
26109 return;
26112 switch (TREE_CODE (type))
26114 case ERROR_MARK:
26115 break;
26117 case POINTER_TYPE:
26118 case REFERENCE_TYPE:
26119 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26120 ensures that the gen_type_die recursion will terminate even if the
26121 type is recursive. Recursive types are possible in Ada. */
26122 /* ??? We could perhaps do this for all types before the switch
26123 statement. */
26124 TREE_ASM_WRITTEN (type) = 1;
26126 /* For these types, all that is required is that we output a DIE (or a
26127 set of DIEs) to represent the "basis" type. */
26128 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26129 DINFO_USAGE_IND_USE);
26130 break;
26132 case OFFSET_TYPE:
26133 /* This code is used for C++ pointer-to-data-member types.
26134 Output a description of the relevant class type. */
26135 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26136 DINFO_USAGE_IND_USE);
26138 /* Output a description of the type of the object pointed to. */
26139 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26140 DINFO_USAGE_IND_USE);
26142 /* Now output a DIE to represent this pointer-to-data-member type
26143 itself. */
26144 gen_ptr_to_mbr_type_die (type, context_die);
26145 break;
26147 case FUNCTION_TYPE:
26148 /* Force out return type (in case it wasn't forced out already). */
26149 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26150 DINFO_USAGE_DIR_USE);
26151 gen_subroutine_type_die (type, context_die);
26152 break;
26154 case METHOD_TYPE:
26155 /* Force out return type (in case it wasn't forced out already). */
26156 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26157 DINFO_USAGE_DIR_USE);
26158 gen_subroutine_type_die (type, context_die);
26159 break;
26161 case ARRAY_TYPE:
26162 case VECTOR_TYPE:
26163 gen_array_type_die (type, context_die);
26164 break;
26166 case ENUMERAL_TYPE:
26167 case RECORD_TYPE:
26168 case UNION_TYPE:
26169 case QUAL_UNION_TYPE:
26170 gen_tagged_type_die (type, context_die, usage);
26171 return;
26173 case VOID_TYPE:
26174 case OPAQUE_TYPE:
26175 case INTEGER_TYPE:
26176 case REAL_TYPE:
26177 case FIXED_POINT_TYPE:
26178 case COMPLEX_TYPE:
26179 case BOOLEAN_TYPE:
26180 /* No DIEs needed for fundamental types. */
26181 break;
26183 case NULLPTR_TYPE:
26184 case LANG_TYPE:
26185 /* Just use DW_TAG_unspecified_type. */
26187 dw_die_ref type_die = lookup_type_die (type);
26188 if (type_die == NULL)
26190 tree name = TYPE_IDENTIFIER (type);
26191 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26192 type);
26193 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26194 equate_type_number_to_die (type, type_die);
26197 break;
26199 default:
26200 if (is_cxx_auto (type))
26202 tree name = TYPE_IDENTIFIER (type);
26203 dw_die_ref *die = (name == get_identifier ("auto")
26204 ? &auto_die : &decltype_auto_die);
26205 if (!*die)
26207 *die = new_die (DW_TAG_unspecified_type,
26208 comp_unit_die (), NULL_TREE);
26209 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26211 equate_type_number_to_die (type, *die);
26212 break;
26214 gcc_unreachable ();
26217 TREE_ASM_WRITTEN (type) = 1;
26220 static void
26221 gen_type_die (tree type, dw_die_ref context_die)
26223 if (type != error_mark_node)
26225 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26226 if (flag_checking)
26228 dw_die_ref die = lookup_type_die (type);
26229 if (die)
26230 check_die (die);
26235 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26236 things which are local to the given block. */
26238 static void
26239 gen_block_die (tree stmt, dw_die_ref context_die)
26241 int must_output_die = 0;
26242 bool inlined_func;
26244 /* Ignore blocks that are NULL. */
26245 if (stmt == NULL_TREE)
26246 return;
26248 inlined_func = inlined_function_outer_scope_p (stmt);
26250 /* If the block is one fragment of a non-contiguous block, do not
26251 process the variables, since they will have been done by the
26252 origin block. Do process subblocks. */
26253 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26255 tree sub;
26257 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26258 gen_block_die (sub, context_die);
26260 return;
26263 /* Determine if we need to output any Dwarf DIEs at all to represent this
26264 block. */
26265 if (inlined_func)
26266 /* The outer scopes for inlinings *must* always be represented. We
26267 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26268 must_output_die = 1;
26269 else if (lookup_block_die (stmt))
26270 /* If we already have a DIE then it was filled early. Meanwhile
26271 we might have pruned all BLOCK_VARS as optimized out but we
26272 still want to generate high/low PC attributes so output it. */
26273 must_output_die = 1;
26274 else if (TREE_USED (stmt)
26275 || TREE_ASM_WRITTEN (stmt))
26277 /* Determine if this block directly contains any "significant"
26278 local declarations which we will need to output DIEs for. */
26279 if (debug_info_level > DINFO_LEVEL_TERSE)
26281 /* We are not in terse mode so any local declaration that
26282 is not ignored for debug purposes counts as being a
26283 "significant" one. */
26284 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26285 must_output_die = 1;
26286 else
26287 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26288 if (!DECL_IGNORED_P (var))
26290 must_output_die = 1;
26291 break;
26294 else if (!dwarf2out_ignore_block (stmt))
26295 must_output_die = 1;
26298 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26299 DIE for any block which contains no significant local declarations at
26300 all. Rather, in such cases we just call `decls_for_scope' so that any
26301 needed Dwarf info for any sub-blocks will get properly generated. Note
26302 that in terse mode, our definition of what constitutes a "significant"
26303 local declaration gets restricted to include only inlined function
26304 instances and local (nested) function definitions. */
26305 if (must_output_die)
26307 if (inlined_func)
26308 gen_inlined_subroutine_die (stmt, context_die);
26309 else
26310 gen_lexical_block_die (stmt, context_die);
26312 else
26313 decls_for_scope (stmt, context_die);
26316 /* Process variable DECL (or variable with origin ORIGIN) within
26317 block STMT and add it to CONTEXT_DIE. */
26318 static void
26319 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26321 dw_die_ref die;
26322 tree decl_or_origin = decl ? decl : origin;
26324 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26325 die = lookup_decl_die (decl_or_origin);
26326 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26328 if (TYPE_DECL_IS_STUB (decl_or_origin))
26329 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26330 else
26331 die = lookup_decl_die (decl_or_origin);
26332 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26333 if (! die && ! early_dwarf)
26334 return;
26336 else
26337 die = NULL;
26339 /* Avoid creating DIEs for local typedefs and concrete static variables that
26340 will only be pruned later. */
26341 if ((origin || decl_ultimate_origin (decl))
26342 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26343 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26345 origin = decl_ultimate_origin (decl_or_origin);
26346 if (decl && VAR_P (decl) && die != NULL)
26348 die = lookup_decl_die (origin);
26349 if (die != NULL)
26350 equate_decl_number_to_die (decl, die);
26352 return;
26355 if (die != NULL && die->die_parent == NULL)
26356 add_child_die (context_die, die);
26357 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26359 if (early_dwarf)
26360 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26361 stmt, context_die);
26363 else
26365 if (decl && DECL_P (decl))
26367 die = lookup_decl_die (decl);
26369 /* Early created DIEs do not have a parent as the decls refer
26370 to the function as DECL_CONTEXT rather than the BLOCK. */
26371 if (die && die->die_parent == NULL)
26373 gcc_assert (in_lto_p);
26374 add_child_die (context_die, die);
26378 gen_decl_die (decl, origin, NULL, context_die);
26382 /* Generate all of the decls declared within a given scope and (recursively)
26383 all of its sub-blocks. */
26385 static void
26386 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26388 tree decl;
26389 unsigned int i;
26390 tree subblocks;
26392 /* Ignore NULL blocks. */
26393 if (stmt == NULL_TREE)
26394 return;
26396 /* Output the DIEs to represent all of the data objects and typedefs
26397 declared directly within this block but not within any nested
26398 sub-blocks. Also, nested function and tag DIEs have been
26399 generated with a parent of NULL; fix that up now. We don't
26400 have to do this if we're at -g1. */
26401 if (debug_info_level > DINFO_LEVEL_TERSE)
26403 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26404 process_scope_var (stmt, decl, NULL_TREE, context_die);
26405 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26406 origin - avoid doing this twice as we have no good way to see
26407 if we've done it once already. */
26408 if (! early_dwarf)
26409 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26411 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26412 if (decl == current_function_decl)
26413 /* Ignore declarations of the current function, while they
26414 are declarations, gen_subprogram_die would treat them
26415 as definitions again, because they are equal to
26416 current_function_decl and endlessly recurse. */;
26417 else if (TREE_CODE (decl) == FUNCTION_DECL)
26418 process_scope_var (stmt, decl, NULL_TREE, context_die);
26419 else
26420 process_scope_var (stmt, NULL_TREE, decl, context_die);
26424 /* Even if we're at -g1, we need to process the subblocks in order to get
26425 inlined call information. */
26427 /* Output the DIEs to represent all sub-blocks (and the items declared
26428 therein) of this block. */
26429 if (recurse)
26430 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26431 subblocks != NULL;
26432 subblocks = BLOCK_CHAIN (subblocks))
26433 gen_block_die (subblocks, context_die);
26436 /* Is this a typedef we can avoid emitting? */
26438 static bool
26439 is_redundant_typedef (const_tree decl)
26441 if (TYPE_DECL_IS_STUB (decl))
26442 return true;
26444 if (DECL_ARTIFICIAL (decl)
26445 && DECL_CONTEXT (decl)
26446 && is_tagged_type (DECL_CONTEXT (decl))
26447 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26448 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26449 /* Also ignore the artificial member typedef for the class name. */
26450 return true;
26452 return false;
26455 /* Return TRUE if TYPE is a typedef that names a type for linkage
26456 purposes. This kind of typedefs is produced by the C++ FE for
26457 constructs like:
26459 typedef struct {...} foo;
26461 In that case, there is no typedef variant type produced for foo.
26462 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26463 struct type. */
26465 static bool
26466 is_naming_typedef_decl (const_tree decl)
26468 if (decl == NULL_TREE
26469 || TREE_CODE (decl) != TYPE_DECL
26470 || DECL_NAMELESS (decl)
26471 || !is_tagged_type (TREE_TYPE (decl))
26472 || DECL_IS_UNDECLARED_BUILTIN (decl)
26473 || is_redundant_typedef (decl)
26474 /* It looks like Ada produces TYPE_DECLs that are very similar
26475 to C++ naming typedefs but that have different
26476 semantics. Let's be specific to c++ for now. */
26477 || !is_cxx (decl))
26478 return FALSE;
26480 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26481 && TYPE_NAME (TREE_TYPE (decl)) == decl
26482 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26483 != TYPE_NAME (TREE_TYPE (decl))));
26486 /* Looks up the DIE for a context. */
26488 static inline dw_die_ref
26489 lookup_context_die (tree context)
26491 if (context)
26493 /* Find die that represents this context. */
26494 if (TYPE_P (context))
26496 context = TYPE_MAIN_VARIANT (context);
26497 dw_die_ref ctx = lookup_type_die (context);
26498 if (!ctx)
26499 return NULL;
26500 return strip_naming_typedef (context, ctx);
26502 else
26503 return lookup_decl_die (context);
26505 return comp_unit_die ();
26508 /* Returns the DIE for a context. */
26510 static inline dw_die_ref
26511 get_context_die (tree context)
26513 if (context)
26515 /* Find die that represents this context. */
26516 if (TYPE_P (context))
26518 context = TYPE_MAIN_VARIANT (context);
26519 return strip_naming_typedef (context, force_type_die (context));
26521 else
26522 return force_decl_die (context);
26524 return comp_unit_die ();
26527 /* Returns the DIE for decl. A DIE will always be returned. */
26529 static dw_die_ref
26530 force_decl_die (tree decl)
26532 dw_die_ref decl_die;
26533 unsigned saved_external_flag;
26534 tree save_fn = NULL_TREE;
26535 decl_die = lookup_decl_die (decl);
26536 if (!decl_die)
26538 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26540 decl_die = lookup_decl_die (decl);
26541 if (decl_die)
26542 return decl_die;
26544 switch (TREE_CODE (decl))
26546 case FUNCTION_DECL:
26547 /* Clear current_function_decl, so that gen_subprogram_die thinks
26548 that this is a declaration. At this point, we just want to force
26549 declaration die. */
26550 save_fn = current_function_decl;
26551 current_function_decl = NULL_TREE;
26552 gen_subprogram_die (decl, context_die);
26553 current_function_decl = save_fn;
26554 break;
26556 case VAR_DECL:
26557 /* Set external flag to force declaration die. Restore it after
26558 gen_decl_die() call. */
26559 saved_external_flag = DECL_EXTERNAL (decl);
26560 DECL_EXTERNAL (decl) = 1;
26561 gen_decl_die (decl, NULL, NULL, context_die);
26562 DECL_EXTERNAL (decl) = saved_external_flag;
26563 break;
26565 case NAMESPACE_DECL:
26566 if (dwarf_version >= 3 || !dwarf_strict)
26567 dwarf2out_decl (decl);
26568 else
26569 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26570 decl_die = comp_unit_die ();
26571 break;
26573 case CONST_DECL:
26574 /* Enumerators shouldn't need force_decl_die. */
26575 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26576 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26577 gen_decl_die (decl, NULL, NULL, context_die);
26578 break;
26580 case TRANSLATION_UNIT_DECL:
26581 decl_die = comp_unit_die ();
26582 break;
26584 default:
26585 gcc_unreachable ();
26588 /* We should be able to find the DIE now. */
26589 if (!decl_die)
26590 decl_die = lookup_decl_die (decl);
26591 gcc_assert (decl_die);
26594 return decl_die;
26597 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26598 always returned. */
26600 static dw_die_ref
26601 force_type_die (tree type)
26603 dw_die_ref type_die;
26605 type_die = lookup_type_die (type);
26606 if (!type_die)
26608 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26610 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26611 false, context_die);
26612 gcc_assert (type_die);
26614 return type_die;
26617 /* Force out any required namespaces to be able to output DECL,
26618 and return the new context_die for it, if it's changed. */
26620 static dw_die_ref
26621 setup_namespace_context (tree thing, dw_die_ref context_die)
26623 tree context = (DECL_P (thing)
26624 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26625 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26626 /* Force out the namespace. */
26627 context_die = force_decl_die (context);
26629 return context_die;
26632 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26633 type) within its namespace, if appropriate.
26635 For compatibility with older debuggers, namespace DIEs only contain
26636 declarations; all definitions are emitted at CU scope, with
26637 DW_AT_specification pointing to the declaration (like with class
26638 members). */
26640 static dw_die_ref
26641 declare_in_namespace (tree thing, dw_die_ref context_die)
26643 dw_die_ref ns_context;
26645 if (debug_info_level <= DINFO_LEVEL_TERSE)
26646 return context_die;
26648 /* External declarations in the local scope only need to be emitted
26649 once, not once in the namespace and once in the scope.
26651 This avoids declaring the `extern' below in the
26652 namespace DIE as well as in the innermost scope:
26654 namespace S
26656 int i=5;
26657 int foo()
26659 int i=8;
26660 extern int i;
26661 return i;
26665 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26666 return context_die;
26668 /* If this decl is from an inlined function, then don't try to emit it in its
26669 namespace, as we will get confused. It would have already been emitted
26670 when the abstract instance of the inline function was emitted anyways. */
26671 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26672 return context_die;
26674 ns_context = setup_namespace_context (thing, context_die);
26676 if (ns_context != context_die)
26678 if (is_fortran () || is_dlang ())
26679 return ns_context;
26680 if (DECL_P (thing))
26681 gen_decl_die (thing, NULL, NULL, ns_context);
26682 else
26683 gen_type_die (thing, ns_context);
26685 return context_die;
26688 /* Generate a DIE for a namespace or namespace alias. */
26690 static void
26691 gen_namespace_die (tree decl, dw_die_ref context_die)
26693 dw_die_ref namespace_die;
26695 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26696 they are an alias of. */
26697 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26699 /* Output a real namespace or module. */
26700 context_die = setup_namespace_context (decl, comp_unit_die ());
26701 namespace_die = new_die (is_fortran () || is_dlang ()
26702 ? DW_TAG_module : DW_TAG_namespace,
26703 context_die, decl);
26704 /* For Fortran modules defined in different CU don't add src coords. */
26705 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26707 const char *name = dwarf2_name (decl, 0);
26708 if (name)
26709 add_name_attribute (namespace_die, name);
26711 else
26712 add_name_and_src_coords_attributes (namespace_die, decl);
26713 if (DECL_EXTERNAL (decl))
26714 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26715 equate_decl_number_to_die (decl, namespace_die);
26717 else
26719 /* Output a namespace alias. */
26721 /* Force out the namespace we are an alias of, if necessary. */
26722 dw_die_ref origin_die
26723 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26725 if (DECL_FILE_SCOPE_P (decl)
26726 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26727 context_die = setup_namespace_context (decl, comp_unit_die ());
26728 /* Now create the namespace alias DIE. */
26729 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26730 add_name_and_src_coords_attributes (namespace_die, decl);
26731 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26732 equate_decl_number_to_die (decl, namespace_die);
26734 if ((dwarf_version >= 5 || !dwarf_strict)
26735 && lang_hooks.decls.decl_dwarf_attribute (decl,
26736 DW_AT_export_symbols) == 1)
26737 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26739 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26740 if (want_pubnames ())
26741 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26744 /* Generate Dwarf debug information for a decl described by DECL.
26745 The return value is currently only meaningful for PARM_DECLs,
26746 for all other decls it returns NULL.
26748 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26749 It can be NULL otherwise. */
26751 static dw_die_ref
26752 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26753 dw_die_ref context_die)
26755 tree decl_or_origin = decl ? decl : origin;
26756 tree class_origin = NULL, ultimate_origin;
26758 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26759 return NULL;
26761 switch (TREE_CODE (decl_or_origin))
26763 case ERROR_MARK:
26764 break;
26766 case CONST_DECL:
26767 if (!is_fortran () && !is_ada () && !is_dlang ())
26769 /* The individual enumerators of an enum type get output when we output
26770 the Dwarf representation of the relevant enum type itself. */
26771 break;
26774 /* Emit its type. */
26775 gen_type_die (TREE_TYPE (decl), context_die);
26777 /* And its containing namespace. */
26778 context_die = declare_in_namespace (decl, context_die);
26780 gen_const_die (decl, context_die);
26781 break;
26783 case FUNCTION_DECL:
26784 #if 0
26785 /* FIXME */
26786 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26787 on local redeclarations of global functions. That seems broken. */
26788 if (current_function_decl != decl)
26789 /* This is only a declaration. */;
26790 #endif
26792 /* We should have abstract copies already and should not generate
26793 stray type DIEs in late LTO dumping. */
26794 if (! early_dwarf)
26797 /* If we're emitting a clone, emit info for the abstract instance. */
26798 else if (origin || DECL_ORIGIN (decl) != decl)
26799 dwarf2out_abstract_function (origin
26800 ? DECL_ORIGIN (origin)
26801 : DECL_ABSTRACT_ORIGIN (decl));
26803 /* If we're emitting a possibly inlined function emit it as
26804 abstract instance. */
26805 else if (cgraph_function_possibly_inlined_p (decl)
26806 && ! DECL_ABSTRACT_P (decl)
26807 && ! class_or_namespace_scope_p (context_die)
26808 /* dwarf2out_abstract_function won't emit a die if this is just
26809 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26810 that case, because that works only if we have a die. */
26811 && DECL_INITIAL (decl) != NULL_TREE)
26812 dwarf2out_abstract_function (decl);
26814 /* Otherwise we're emitting the primary DIE for this decl. */
26815 else if (debug_info_level > DINFO_LEVEL_TERSE)
26817 /* Before we describe the FUNCTION_DECL itself, make sure that we
26818 have its containing type. */
26819 if (!origin)
26820 origin = decl_class_context (decl);
26821 if (origin != NULL_TREE)
26822 gen_type_die (origin, context_die);
26824 /* And its return type. */
26825 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26827 /* And its virtual context. */
26828 if (DECL_VINDEX (decl) != NULL_TREE)
26829 gen_type_die (DECL_CONTEXT (decl), context_die);
26831 /* Make sure we have a member DIE for decl. */
26832 if (origin != NULL_TREE)
26833 gen_type_die_for_member (origin, decl, context_die);
26835 /* And its containing namespace. */
26836 context_die = declare_in_namespace (decl, context_die);
26839 /* Now output a DIE to represent the function itself. */
26840 if (decl)
26841 gen_subprogram_die (decl, context_die);
26842 break;
26844 case TYPE_DECL:
26845 /* If we are in terse mode, don't generate any DIEs to represent any
26846 actual typedefs. */
26847 if (debug_info_level <= DINFO_LEVEL_TERSE)
26848 break;
26850 /* In the special case of a TYPE_DECL node representing the declaration
26851 of some type tag, if the given TYPE_DECL is marked as having been
26852 instantiated from some other (original) TYPE_DECL node (e.g. one which
26853 was generated within the original definition of an inline function) we
26854 used to generate a special (abbreviated) DW_TAG_structure_type,
26855 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26856 should be actually referencing those DIEs, as variable DIEs with that
26857 type would be emitted already in the abstract origin, so it was always
26858 removed during unused type prunning. Don't add anything in this
26859 case. */
26860 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26861 break;
26863 if (is_redundant_typedef (decl))
26864 gen_type_die (TREE_TYPE (decl), context_die);
26865 else
26866 /* Output a DIE to represent the typedef itself. */
26867 gen_typedef_die (decl, context_die);
26868 break;
26870 case LABEL_DECL:
26871 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26872 gen_label_die (decl, context_die);
26873 break;
26875 case VAR_DECL:
26876 case RESULT_DECL:
26877 /* If we are in terse mode, don't generate any DIEs to represent any
26878 variable declarations or definitions unless it is external. */
26879 if (debug_info_level < DINFO_LEVEL_TERSE
26880 || (debug_info_level == DINFO_LEVEL_TERSE
26881 && !TREE_PUBLIC (decl_or_origin)))
26882 break;
26884 if (debug_info_level > DINFO_LEVEL_TERSE)
26886 /* Avoid generating stray type DIEs during late dwarf dumping.
26887 All types have been dumped early. */
26888 if (early_dwarf
26889 /* ??? But in LTRANS we cannot annotate early created variably
26890 modified type DIEs without copying them and adjusting all
26891 references to them. Dump them again as happens for inlining
26892 which copies both the decl and the types. */
26893 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26894 in VLA bound information for example. */
26895 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26896 current_function_decl)))
26898 /* Output any DIEs that are needed to specify the type of this data
26899 object. */
26900 if (decl_by_reference_p (decl_or_origin))
26901 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26902 else
26903 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26906 if (early_dwarf)
26908 /* And its containing type. */
26909 class_origin = decl_class_context (decl_or_origin);
26910 if (class_origin != NULL_TREE)
26911 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26913 /* And its containing namespace. */
26914 context_die = declare_in_namespace (decl_or_origin, context_die);
26918 /* Now output the DIE to represent the data object itself. This gets
26919 complicated because of the possibility that the VAR_DECL really
26920 represents an inlined instance of a formal parameter for an inline
26921 function. */
26922 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26923 if (ultimate_origin != NULL_TREE
26924 && TREE_CODE (ultimate_origin) == PARM_DECL)
26925 gen_formal_parameter_die (decl, origin,
26926 true /* Emit name attribute. */,
26927 context_die);
26928 else
26929 gen_variable_die (decl, origin, context_die);
26930 break;
26932 case FIELD_DECL:
26933 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26934 /* Ignore the nameless fields that are used to skip bits but handle C++
26935 anonymous unions and structs. */
26936 if (DECL_NAME (decl) != NULL_TREE
26937 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26938 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26940 gen_type_die (member_declared_type (decl), context_die);
26941 gen_field_die (decl, ctx, context_die);
26943 break;
26945 case PARM_DECL:
26946 /* Avoid generating stray type DIEs during late dwarf dumping.
26947 All types have been dumped early. */
26948 if (early_dwarf
26949 /* ??? But in LTRANS we cannot annotate early created variably
26950 modified type DIEs without copying them and adjusting all
26951 references to them. Dump them again as happens for inlining
26952 which copies both the decl and the types. */
26953 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26954 in VLA bound information for example. */
26955 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26956 current_function_decl)))
26958 if (DECL_BY_REFERENCE (decl_or_origin))
26959 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26960 else
26961 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26963 return gen_formal_parameter_die (decl, origin,
26964 true /* Emit name attribute. */,
26965 context_die);
26967 case NAMESPACE_DECL:
26968 if (dwarf_version >= 3 || !dwarf_strict)
26969 gen_namespace_die (decl, context_die);
26970 break;
26972 case IMPORTED_DECL:
26973 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26974 DECL_CONTEXT (decl), context_die);
26975 break;
26977 case NAMELIST_DECL:
26978 gen_namelist_decl (DECL_NAME (decl), context_die,
26979 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26980 break;
26982 default:
26983 /* Probably some frontend-internal decl. Assume we don't care. */
26984 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26985 break;
26988 return NULL;
26991 /* Output initial debug information for global DECL. Called at the
26992 end of the parsing process.
26994 This is the initial debug generation process. As such, the DIEs
26995 generated may be incomplete. A later debug generation pass
26996 (dwarf2out_late_global_decl) will augment the information generated
26997 in this pass (e.g., with complete location info). */
26999 static void
27000 dwarf2out_early_global_decl (tree decl)
27002 set_early_dwarf s;
27004 /* gen_decl_die() will set DECL_ABSTRACT because
27005 cgraph_function_possibly_inlined_p() returns true. This is in
27006 turn will cause DW_AT_inline attributes to be set.
27008 This happens because at early dwarf generation, there is no
27009 cgraph information, causing cgraph_function_possibly_inlined_p()
27010 to return true. Trick cgraph_function_possibly_inlined_p()
27011 while we generate dwarf early. */
27012 bool save = symtab->global_info_ready;
27013 symtab->global_info_ready = true;
27015 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27016 other DECLs and they can point to template types or other things
27017 that dwarf2out can't handle when done via dwarf2out_decl. */
27018 if (TREE_CODE (decl) != TYPE_DECL
27019 && TREE_CODE (decl) != PARM_DECL)
27021 if (TREE_CODE (decl) == FUNCTION_DECL)
27023 tree save_fndecl = current_function_decl;
27025 /* For nested functions, make sure we have DIEs for the parents first
27026 so that all nested DIEs are generated at the proper scope in the
27027 first shot. */
27028 tree context = decl_function_context (decl);
27029 if (context != NULL)
27031 dw_die_ref context_die = lookup_decl_die (context);
27032 current_function_decl = context;
27034 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27035 enough so that it lands in its own context. This avoids type
27036 pruning issues later on. */
27037 if (context_die == NULL || is_declaration_die (context_die))
27038 dwarf2out_early_global_decl (context);
27041 /* Emit an abstract origin of a function first. This happens
27042 with C++ constructor clones for example and makes
27043 dwarf2out_abstract_function happy which requires the early
27044 DIE of the abstract instance to be present. */
27045 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27046 dw_die_ref origin_die;
27047 if (origin != NULL
27048 /* Do not emit the DIE multiple times but make sure to
27049 process it fully here in case we just saw a declaration. */
27050 && ((origin_die = lookup_decl_die (origin)) == NULL
27051 || is_declaration_die (origin_die)))
27053 current_function_decl = origin;
27054 dwarf2out_decl (origin);
27057 /* Emit the DIE for decl but avoid doing that multiple times. */
27058 dw_die_ref old_die;
27059 if ((old_die = lookup_decl_die (decl)) == NULL
27060 || is_declaration_die (old_die))
27062 current_function_decl = decl;
27063 dwarf2out_decl (decl);
27066 current_function_decl = save_fndecl;
27068 else
27069 dwarf2out_decl (decl);
27071 symtab->global_info_ready = save;
27074 /* Return whether EXPR is an expression with the following pattern:
27075 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27077 static bool
27078 is_trivial_indirect_ref (tree expr)
27080 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27081 return false;
27083 tree nop = TREE_OPERAND (expr, 0);
27084 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27085 return false;
27087 tree int_cst = TREE_OPERAND (nop, 0);
27088 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27091 /* Output debug information for global decl DECL. Called from
27092 toplev.c after compilation proper has finished. */
27094 static void
27095 dwarf2out_late_global_decl (tree decl)
27097 /* Fill-in any location information we were unable to determine
27098 on the first pass. */
27099 if (VAR_P (decl))
27101 dw_die_ref die = lookup_decl_die (decl);
27103 /* We may have to generate full debug late for LTO in case debug
27104 was not enabled at compile-time or the target doesn't support
27105 the LTO early debug scheme. */
27106 if (! die && in_lto_p)
27107 dwarf2out_decl (decl);
27108 else if (die)
27110 /* We get called via the symtab code invoking late_global_decl
27111 for symbols that are optimized out.
27113 Do not add locations for those, except if they have a
27114 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27115 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27116 INDIRECT_REF expression, as this could generate relocations to
27117 text symbols in LTO object files, which is invalid. */
27118 varpool_node *node = varpool_node::get (decl);
27119 if ((! node || ! node->definition)
27120 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27121 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27122 tree_add_const_value_attribute_for_decl (die, decl);
27123 else
27124 add_location_or_const_value_attribute (die, decl, false);
27129 /* Output debug information for type decl DECL. Called from toplev.c
27130 and from language front ends (to record built-in types). */
27131 static void
27132 dwarf2out_type_decl (tree decl, int local)
27134 if (!local)
27136 set_early_dwarf s;
27137 dwarf2out_decl (decl);
27141 /* Output debug information for imported module or decl DECL.
27142 NAME is non-NULL name in the lexical block if the decl has been renamed.
27143 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27144 that DECL belongs to.
27145 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27146 static void
27147 dwarf2out_imported_module_or_decl_1 (tree decl,
27148 tree name,
27149 tree lexical_block,
27150 dw_die_ref lexical_block_die)
27152 expanded_location xloc;
27153 dw_die_ref imported_die = NULL;
27154 dw_die_ref at_import_die;
27156 if (TREE_CODE (decl) == IMPORTED_DECL)
27158 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27159 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27160 gcc_assert (decl);
27162 else
27163 xloc = expand_location (input_location);
27165 if (TREE_CODE (decl) == TYPE_DECL)
27167 at_import_die = force_type_die (TREE_TYPE (decl));
27168 /* For namespace N { typedef void T; } using N::T; base_type_die
27169 returns NULL, but DW_TAG_imported_declaration requires
27170 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27171 if (!at_import_die)
27173 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27174 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27175 at_import_die = lookup_type_die (TREE_TYPE (decl));
27176 gcc_assert (at_import_die);
27179 else
27181 at_import_die = lookup_decl_die (decl);
27182 if (!at_import_die)
27184 /* If we're trying to avoid duplicate debug info, we may not have
27185 emitted the member decl for this field. Emit it now. */
27186 if (TREE_CODE (decl) == FIELD_DECL)
27188 tree type = DECL_CONTEXT (decl);
27190 if (TYPE_CONTEXT (type)
27191 && TYPE_P (TYPE_CONTEXT (type))
27192 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27193 DINFO_USAGE_DIR_USE))
27194 return;
27195 gen_type_die_for_member (type, decl,
27196 get_context_die (TYPE_CONTEXT (type)));
27198 if (TREE_CODE (decl) == CONST_DECL)
27200 /* Individual enumerators of an enum type do not get output here
27201 (see gen_decl_die), so we cannot call force_decl_die. */
27202 if (!is_fortran () && !is_ada () && !is_dlang ())
27203 return;
27205 if (TREE_CODE (decl) == NAMELIST_DECL)
27206 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27207 get_context_die (DECL_CONTEXT (decl)),
27208 NULL_TREE);
27209 else
27210 at_import_die = force_decl_die (decl);
27214 if (TREE_CODE (decl) == NAMESPACE_DECL)
27216 if (dwarf_version >= 3 || !dwarf_strict)
27217 imported_die = new_die (DW_TAG_imported_module,
27218 lexical_block_die,
27219 lexical_block);
27220 else
27221 return;
27223 else
27224 imported_die = new_die (DW_TAG_imported_declaration,
27225 lexical_block_die,
27226 lexical_block);
27228 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27229 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27230 if (debug_column_info && xloc.column)
27231 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27232 if (name)
27233 add_AT_string (imported_die, DW_AT_name,
27234 IDENTIFIER_POINTER (name));
27235 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27238 /* Output debug information for imported module or decl DECL.
27239 NAME is non-NULL name in context if the decl has been renamed.
27240 CHILD is true if decl is one of the renamed decls as part of
27241 importing whole module.
27242 IMPLICIT is set if this hook is called for an implicit import
27243 such as inline namespace. */
27245 static void
27246 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27247 bool child, bool implicit)
27249 /* dw_die_ref at_import_die; */
27250 dw_die_ref scope_die;
27252 if (debug_info_level <= DINFO_LEVEL_TERSE)
27253 return;
27255 gcc_assert (decl);
27257 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27258 should be enough, for DWARF4 and older even if we emit as extension
27259 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27260 for the benefit of consumers unaware of DW_AT_export_symbols. */
27261 if (implicit
27262 && dwarf_version >= 5
27263 && lang_hooks.decls.decl_dwarf_attribute (decl,
27264 DW_AT_export_symbols) == 1)
27265 return;
27267 set_early_dwarf s;
27269 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27270 We need decl DIE for reference and scope die. First, get DIE for the decl
27271 itself. */
27273 /* Get the scope die for decl context. Use comp_unit_die for global module
27274 or decl. If die is not found for non globals, force new die. */
27275 if (context
27276 && TYPE_P (context)
27277 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27278 return;
27280 scope_die = get_context_die (context);
27282 if (child)
27284 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27285 there is nothing we can do, here. */
27286 if (dwarf_version < 3 && dwarf_strict)
27287 return;
27289 gcc_assert (scope_die->die_child);
27290 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27291 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27292 scope_die = scope_die->die_child;
27295 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27296 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27299 /* Output debug information for namelists. */
27301 static dw_die_ref
27302 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27304 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27305 tree value;
27306 unsigned i;
27308 if (debug_info_level <= DINFO_LEVEL_TERSE)
27309 return NULL;
27311 gcc_assert (scope_die != NULL);
27312 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27313 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27315 /* If there are no item_decls, we have a nondefining namelist, e.g.
27316 with USE association; hence, set DW_AT_declaration. */
27317 if (item_decls == NULL_TREE)
27319 add_AT_flag (nml_die, DW_AT_declaration, 1);
27320 return nml_die;
27323 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27325 nml_item_ref_die = lookup_decl_die (value);
27326 if (!nml_item_ref_die)
27327 nml_item_ref_die = force_decl_die (value);
27329 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27330 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27332 return nml_die;
27336 /* Write the debugging output for DECL and return the DIE. */
27338 static void
27339 dwarf2out_decl (tree decl)
27341 dw_die_ref context_die = comp_unit_die ();
27343 switch (TREE_CODE (decl))
27345 case ERROR_MARK:
27346 return;
27348 case FUNCTION_DECL:
27349 /* If we're a nested function, initially use a parent of NULL; if we're
27350 a plain function, this will be fixed up in decls_for_scope. If
27351 we're a method, it will be ignored, since we already have a DIE.
27352 Avoid doing this late though since clones of class methods may
27353 otherwise end up in limbo and create type DIEs late. */
27354 if (early_dwarf
27355 && decl_function_context (decl)
27356 /* But if we're in terse mode, we don't care about scope. */
27357 && debug_info_level > DINFO_LEVEL_TERSE)
27358 context_die = NULL;
27359 break;
27361 case VAR_DECL:
27362 /* For local statics lookup proper context die. */
27363 if (local_function_static (decl))
27364 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27366 /* If we are in terse mode, don't generate any DIEs to represent any
27367 variable declarations or definitions unless it is external. */
27368 if (debug_info_level < DINFO_LEVEL_TERSE
27369 || (debug_info_level == DINFO_LEVEL_TERSE
27370 && !TREE_PUBLIC (decl)))
27371 return;
27372 break;
27374 case CONST_DECL:
27375 if (debug_info_level <= DINFO_LEVEL_TERSE)
27376 return;
27377 if (!is_fortran () && !is_ada () && !is_dlang ())
27378 return;
27379 if (TREE_STATIC (decl) && decl_function_context (decl))
27380 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27381 break;
27383 case NAMESPACE_DECL:
27384 case IMPORTED_DECL:
27385 if (debug_info_level <= DINFO_LEVEL_TERSE)
27386 return;
27387 if (lookup_decl_die (decl) != NULL)
27388 return;
27389 break;
27391 case TYPE_DECL:
27392 /* Don't emit stubs for types unless they are needed by other DIEs. */
27393 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27394 return;
27396 /* Don't bother trying to generate any DIEs to represent any of the
27397 normal built-in types for the language we are compiling. */
27398 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27399 return;
27401 /* If we are in terse mode, don't generate any DIEs for types. */
27402 if (debug_info_level <= DINFO_LEVEL_TERSE)
27403 return;
27405 /* If we're a function-scope tag, initially use a parent of NULL;
27406 this will be fixed up in decls_for_scope. */
27407 if (decl_function_context (decl))
27408 context_die = NULL;
27410 break;
27412 case NAMELIST_DECL:
27413 break;
27415 default:
27416 return;
27419 gen_decl_die (decl, NULL, NULL, context_die);
27421 if (flag_checking)
27423 dw_die_ref die = lookup_decl_die (decl);
27424 if (die)
27425 check_die (die);
27429 /* Write the debugging output for DECL. */
27431 static void
27432 dwarf2out_function_decl (tree decl)
27434 dwarf2out_decl (decl);
27435 call_arg_locations = NULL;
27436 call_arg_loc_last = NULL;
27437 call_site_count = -1;
27438 tail_call_site_count = -1;
27439 decl_loc_table->empty ();
27440 cached_dw_loc_list_table->empty ();
27443 /* Output a marker (i.e. a label) for the beginning of the generated code for
27444 a lexical block. */
27446 static void
27447 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27448 unsigned int blocknum)
27450 switch_to_section (current_function_section ());
27451 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27454 /* Output a marker (i.e. a label) for the end of the generated code for a
27455 lexical block. */
27457 static void
27458 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27460 switch_to_section (current_function_section ());
27461 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27464 /* Returns nonzero if it is appropriate not to emit any debugging
27465 information for BLOCK, because it doesn't contain any instructions.
27467 Don't allow this for blocks with nested functions or local classes
27468 as we would end up with orphans, and in the presence of scheduling
27469 we may end up calling them anyway. */
27471 static bool
27472 dwarf2out_ignore_block (const_tree block)
27474 tree decl;
27475 unsigned int i;
27477 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27478 if (TREE_CODE (decl) == FUNCTION_DECL
27479 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27480 return 0;
27481 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27483 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27484 if (TREE_CODE (decl) == FUNCTION_DECL
27485 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27486 return 0;
27489 return 1;
27492 /* Hash table routines for file_hash. */
27494 bool
27495 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27497 return filename_cmp (p1->key, p2) == 0;
27500 hashval_t
27501 dwarf_file_hasher::hash (dwarf_file_data *p)
27503 return htab_hash_string (p->key);
27506 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27507 dwarf2out.c) and return its "index". The index of each (known) filename is
27508 just a unique number which is associated with only that one filename. We
27509 need such numbers for the sake of generating labels (in the .debug_sfnames
27510 section) and references to those files numbers (in the .debug_srcinfo
27511 and .debug_macinfo sections). If the filename given as an argument is not
27512 found in our current list, add it to the list and assign it the next
27513 available unique index number. */
27515 static struct dwarf_file_data *
27516 lookup_filename (const char *file_name)
27518 struct dwarf_file_data * created;
27520 if (!file_name)
27521 return NULL;
27523 if (!file_name[0])
27524 file_name = "<stdin>";
27526 dwarf_file_data **slot
27527 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27528 INSERT);
27529 if (*slot)
27530 return *slot;
27532 created = ggc_alloc<dwarf_file_data> ();
27533 created->key = file_name;
27534 created->filename = remap_debug_filename (file_name);
27535 created->emitted_number = 0;
27536 *slot = created;
27537 return created;
27540 /* If the assembler will construct the file table, then translate the compiler
27541 internal file table number into the assembler file table number, and emit
27542 a .file directive if we haven't already emitted one yet. The file table
27543 numbers are different because we prune debug info for unused variables and
27544 types, which may include filenames. */
27546 static int
27547 maybe_emit_file (struct dwarf_file_data * fd)
27549 if (! fd->emitted_number)
27551 if (last_emitted_file)
27552 fd->emitted_number = last_emitted_file->emitted_number + 1;
27553 else
27554 fd->emitted_number = 1;
27555 last_emitted_file = fd;
27557 if (output_asm_line_debug_info ())
27559 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27560 output_quoted_string (asm_out_file, fd->filename);
27561 fputc ('\n', asm_out_file);
27565 return fd->emitted_number;
27568 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27569 That generation should happen after function debug info has been
27570 generated. The value of the attribute is the constant value of ARG. */
27572 static void
27573 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27575 die_arg_entry entry;
27577 if (!die || !arg)
27578 return;
27580 gcc_assert (early_dwarf);
27582 if (!tmpl_value_parm_die_table)
27583 vec_alloc (tmpl_value_parm_die_table, 32);
27585 entry.die = die;
27586 entry.arg = arg;
27587 vec_safe_push (tmpl_value_parm_die_table, entry);
27590 /* Return TRUE if T is an instance of generic type, FALSE
27591 otherwise. */
27593 static bool
27594 generic_type_p (tree t)
27596 if (t == NULL_TREE || !TYPE_P (t))
27597 return false;
27598 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27601 /* Schedule the generation of the generic parameter dies for the
27602 instance of generic type T. The proper generation itself is later
27603 done by gen_scheduled_generic_parms_dies. */
27605 static void
27606 schedule_generic_params_dies_gen (tree t)
27608 if (!generic_type_p (t))
27609 return;
27611 gcc_assert (early_dwarf);
27613 if (!generic_type_instances)
27614 vec_alloc (generic_type_instances, 256);
27616 vec_safe_push (generic_type_instances, t);
27619 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27620 by append_entry_to_tmpl_value_parm_die_table. This function must
27621 be called after function DIEs have been generated. */
27623 static void
27624 gen_remaining_tmpl_value_param_die_attribute (void)
27626 if (tmpl_value_parm_die_table)
27628 unsigned i, j;
27629 die_arg_entry *e;
27631 /* We do this in two phases - first get the cases we can
27632 handle during early-finish, preserving those we cannot
27633 (containing symbolic constants where we don't yet know
27634 whether we are going to output the referenced symbols).
27635 For those we try again at late-finish. */
27636 j = 0;
27637 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27639 if (!e->die->removed
27640 && !tree_add_const_value_attribute (e->die, e->arg))
27642 dw_loc_descr_ref loc = NULL;
27643 if (! early_dwarf
27644 && (dwarf_version >= 5 || !dwarf_strict))
27645 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27646 if (loc)
27647 add_AT_loc (e->die, DW_AT_location, loc);
27648 else
27649 (*tmpl_value_parm_die_table)[j++] = *e;
27652 tmpl_value_parm_die_table->truncate (j);
27656 /* Generate generic parameters DIEs for instances of generic types
27657 that have been previously scheduled by
27658 schedule_generic_params_dies_gen. This function must be called
27659 after all the types of the CU have been laid out. */
27661 static void
27662 gen_scheduled_generic_parms_dies (void)
27664 unsigned i;
27665 tree t;
27667 if (!generic_type_instances)
27668 return;
27670 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27671 if (COMPLETE_TYPE_P (t))
27672 gen_generic_params_dies (t);
27674 generic_type_instances = NULL;
27678 /* Replace DW_AT_name for the decl with name. */
27680 static void
27681 dwarf2out_set_name (tree decl, tree name)
27683 dw_die_ref die;
27684 dw_attr_node *attr;
27685 const char *dname;
27687 die = TYPE_SYMTAB_DIE (decl);
27688 if (!die)
27689 return;
27691 dname = dwarf2_name (name, 0);
27692 if (!dname)
27693 return;
27695 attr = get_AT (die, DW_AT_name);
27696 if (attr)
27698 struct indirect_string_node *node;
27700 node = find_AT_string (dname);
27701 /* replace the string. */
27702 attr->dw_attr_val.v.val_str = node;
27705 else
27706 add_name_attribute (die, dname);
27709 /* True if before or during processing of the first function being emitted. */
27710 static bool in_first_function_p = true;
27711 /* True if loc_note during dwarf2out_var_location call might still be
27712 before first real instruction at address equal to .Ltext0. */
27713 static bool maybe_at_text_label_p = true;
27714 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27715 static unsigned int first_loclabel_num_not_at_text_label;
27717 /* Look ahead for a real insn. */
27719 static rtx_insn *
27720 dwarf2out_next_real_insn (rtx_insn *loc_note)
27722 rtx_insn *next_real = NEXT_INSN (loc_note);
27724 while (next_real)
27725 if (INSN_P (next_real))
27726 break;
27727 else
27728 next_real = NEXT_INSN (next_real);
27730 return next_real;
27733 /* Called by the final INSN scan whenever we see a var location. We
27734 use it to drop labels in the right places, and throw the location in
27735 our lookup table. */
27737 static void
27738 dwarf2out_var_location (rtx_insn *loc_note)
27740 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27741 struct var_loc_node *newloc;
27742 rtx_insn *next_real;
27743 rtx_insn *call_insn = NULL;
27744 static const char *last_label;
27745 static const char *last_postcall_label;
27746 static bool last_in_cold_section_p;
27747 static rtx_insn *expected_next_loc_note;
27748 tree decl;
27749 bool var_loc_p;
27750 var_loc_view view = 0;
27752 if (!NOTE_P (loc_note))
27754 if (CALL_P (loc_note))
27756 maybe_reset_location_view (loc_note, cur_line_info_table);
27757 call_site_count++;
27758 if (SIBLING_CALL_P (loc_note))
27759 tail_call_site_count++;
27760 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27762 call_insn = loc_note;
27763 loc_note = NULL;
27764 var_loc_p = false;
27766 next_real = dwarf2out_next_real_insn (call_insn);
27767 cached_next_real_insn = NULL;
27768 goto create_label;
27770 if (optimize == 0 && !flag_var_tracking)
27772 /* When the var-tracking pass is not running, there is no note
27773 for indirect calls whose target is compile-time known. In this
27774 case, process such calls specifically so that we generate call
27775 sites for them anyway. */
27776 rtx x = PATTERN (loc_note);
27777 if (GET_CODE (x) == PARALLEL)
27778 x = XVECEXP (x, 0, 0);
27779 if (GET_CODE (x) == SET)
27780 x = SET_SRC (x);
27781 if (GET_CODE (x) == CALL)
27782 x = XEXP (x, 0);
27783 if (!MEM_P (x)
27784 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27785 || !SYMBOL_REF_DECL (XEXP (x, 0))
27786 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27787 != FUNCTION_DECL))
27789 call_insn = loc_note;
27790 loc_note = NULL;
27791 var_loc_p = false;
27793 next_real = dwarf2out_next_real_insn (call_insn);
27794 cached_next_real_insn = NULL;
27795 goto create_label;
27799 else if (!debug_variable_location_views)
27800 gcc_unreachable ();
27801 else
27802 maybe_reset_location_view (loc_note, cur_line_info_table);
27804 return;
27807 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27808 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27809 return;
27811 /* Optimize processing a large consecutive sequence of location
27812 notes so we don't spend too much time in next_real_insn. If the
27813 next insn is another location note, remember the next_real_insn
27814 calculation for next time. */
27815 next_real = cached_next_real_insn;
27816 if (next_real)
27818 if (expected_next_loc_note != loc_note)
27819 next_real = NULL;
27822 if (! next_real)
27823 next_real = dwarf2out_next_real_insn (loc_note);
27825 if (next_real)
27827 rtx_insn *next_note = NEXT_INSN (loc_note);
27828 while (next_note != next_real)
27830 if (! next_note->deleted ()
27831 && NOTE_P (next_note)
27832 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27833 break;
27834 next_note = NEXT_INSN (next_note);
27837 if (next_note == next_real)
27838 cached_next_real_insn = NULL;
27839 else
27841 expected_next_loc_note = next_note;
27842 cached_next_real_insn = next_real;
27845 else
27846 cached_next_real_insn = NULL;
27848 /* If there are no instructions which would be affected by this note,
27849 don't do anything. */
27850 if (var_loc_p
27851 && next_real == NULL_RTX
27852 && !NOTE_DURING_CALL_P (loc_note))
27853 return;
27855 create_label:
27857 if (next_real == NULL_RTX)
27858 next_real = get_last_insn ();
27860 /* If there were any real insns between note we processed last time
27861 and this note (or if it is the first note), clear
27862 last_{,postcall_}label so that they are not reused this time. */
27863 if (last_var_location_insn == NULL_RTX
27864 || last_var_location_insn != next_real
27865 || last_in_cold_section_p != in_cold_section_p)
27867 last_label = NULL;
27868 last_postcall_label = NULL;
27871 if (var_loc_p)
27873 const char *label
27874 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27875 view = cur_line_info_table->view;
27876 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27877 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27878 if (newloc == NULL)
27879 return;
27881 else
27883 decl = NULL_TREE;
27884 newloc = NULL;
27887 /* If there were no real insns between note we processed last time
27888 and this note, use the label we emitted last time. Otherwise
27889 create a new label and emit it. */
27890 if (last_label == NULL)
27892 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27893 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27894 loclabel_num++;
27895 last_label = ggc_strdup (loclabel);
27896 /* See if loclabel might be equal to .Ltext0. If yes,
27897 bump first_loclabel_num_not_at_text_label. */
27898 if (!have_multiple_function_sections
27899 && in_first_function_p
27900 && maybe_at_text_label_p)
27902 static rtx_insn *last_start;
27903 rtx_insn *insn;
27904 for (insn = loc_note; insn; insn = previous_insn (insn))
27905 if (insn == last_start)
27906 break;
27907 else if (!NONDEBUG_INSN_P (insn))
27908 continue;
27909 else
27911 rtx body = PATTERN (insn);
27912 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27913 continue;
27914 /* Inline asm could occupy zero bytes. */
27915 else if (GET_CODE (body) == ASM_INPUT
27916 || asm_noperands (body) >= 0)
27917 continue;
27918 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27919 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27920 continue;
27921 #endif
27922 else
27924 /* Assume insn has non-zero length. */
27925 maybe_at_text_label_p = false;
27926 break;
27929 if (maybe_at_text_label_p)
27931 last_start = loc_note;
27932 first_loclabel_num_not_at_text_label = loclabel_num;
27937 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27938 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27940 if (!var_loc_p)
27942 struct call_arg_loc_node *ca_loc
27943 = ggc_cleared_alloc<call_arg_loc_node> ();
27944 rtx_insn *prev = call_insn;
27946 ca_loc->call_arg_loc_note
27947 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27948 ca_loc->next = NULL;
27949 ca_loc->label = last_label;
27950 gcc_assert (prev
27951 && (CALL_P (prev)
27952 || (NONJUMP_INSN_P (prev)
27953 && GET_CODE (PATTERN (prev)) == SEQUENCE
27954 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27955 if (!CALL_P (prev))
27956 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27957 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27959 /* Look for a SYMBOL_REF in the "prev" instruction. */
27960 rtx x = get_call_rtx_from (prev);
27961 if (x)
27963 /* Try to get the call symbol, if any. */
27964 if (MEM_P (XEXP (x, 0)))
27965 x = XEXP (x, 0);
27966 /* First, look for a memory access to a symbol_ref. */
27967 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27968 && SYMBOL_REF_DECL (XEXP (x, 0))
27969 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27970 ca_loc->symbol_ref = XEXP (x, 0);
27971 /* Otherwise, look at a compile-time known user-level function
27972 declaration. */
27973 else if (MEM_P (x)
27974 && MEM_EXPR (x)
27975 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27976 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27979 ca_loc->block = insn_scope (prev);
27980 if (call_arg_locations)
27981 call_arg_loc_last->next = ca_loc;
27982 else
27983 call_arg_locations = ca_loc;
27984 call_arg_loc_last = ca_loc;
27986 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27988 newloc->label = last_label;
27989 newloc->view = view;
27991 else
27993 if (!last_postcall_label)
27995 sprintf (loclabel, "%s-1", last_label);
27996 last_postcall_label = ggc_strdup (loclabel);
27998 newloc->label = last_postcall_label;
27999 /* ??? This view is at last_label, not last_label-1, but we
28000 could only assume view at last_label-1 is zero if we could
28001 assume calls always have length greater than one. This is
28002 probably true in general, though there might be a rare
28003 exception to this rule, e.g. if a call insn is optimized out
28004 by target magic. Then, even the -1 in the label will be
28005 wrong, which might invalidate the range. Anyway, using view,
28006 though technically possibly incorrect, will work as far as
28007 ranges go: since L-1 is in the middle of the call insn,
28008 (L-1).0 and (L-1).V shouldn't make any difference, and having
28009 the loclist entry refer to the .loc entry might be useful, so
28010 leave it like this. */
28011 newloc->view = view;
28014 if (var_loc_p && flag_debug_asm)
28016 const char *name, *sep, *patstr;
28017 if (decl && DECL_NAME (decl))
28018 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28019 else
28020 name = "";
28021 if (NOTE_VAR_LOCATION_LOC (loc_note))
28023 sep = " => ";
28024 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28026 else
28028 sep = " ";
28029 patstr = "RESET";
28031 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28032 name, sep, patstr);
28035 last_var_location_insn = next_real;
28036 last_in_cold_section_p = in_cold_section_p;
28039 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28040 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28041 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28042 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28043 BLOCK_FRAGMENT_ORIGIN links. */
28044 static bool
28045 block_within_block_p (tree block, tree outer, bool bothways)
28047 if (block == outer)
28048 return true;
28050 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28051 for (tree context = BLOCK_SUPERCONTEXT (block);
28052 context != outer;
28053 context = BLOCK_SUPERCONTEXT (context))
28054 if (!context || TREE_CODE (context) != BLOCK)
28055 return false;
28057 if (!bothways)
28058 return true;
28060 /* Now check that each block is actually referenced by its
28061 parent. */
28062 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28063 context = BLOCK_SUPERCONTEXT (context))
28065 if (BLOCK_FRAGMENT_ORIGIN (context))
28067 gcc_assert (!BLOCK_SUBBLOCKS (context));
28068 context = BLOCK_FRAGMENT_ORIGIN (context);
28070 for (tree sub = BLOCK_SUBBLOCKS (context);
28071 sub != block;
28072 sub = BLOCK_CHAIN (sub))
28073 if (!sub)
28074 return false;
28075 if (context == outer)
28076 return true;
28077 else
28078 block = context;
28082 /* Called during final while assembling the marker of the entry point
28083 for an inlined function. */
28085 static void
28086 dwarf2out_inline_entry (tree block)
28088 gcc_assert (debug_inline_points);
28090 /* If we can't represent it, don't bother. */
28091 if (!(dwarf_version >= 3 || !dwarf_strict))
28092 return;
28094 gcc_assert (DECL_P (block_ultimate_origin (block)));
28096 /* Sanity check the block tree. This would catch a case in which
28097 BLOCK got removed from the tree reachable from the outermost
28098 lexical block, but got retained in markers. It would still link
28099 back to its parents, but some ancestor would be missing a link
28100 down the path to the sub BLOCK. If the block got removed, its
28101 BLOCK_NUMBER will not be a usable value. */
28102 if (flag_checking)
28103 gcc_assert (block_within_block_p (block,
28104 DECL_INITIAL (current_function_decl),
28105 true));
28107 gcc_assert (inlined_function_outer_scope_p (block));
28108 gcc_assert (!lookup_block_die (block));
28110 if (BLOCK_FRAGMENT_ORIGIN (block))
28111 block = BLOCK_FRAGMENT_ORIGIN (block);
28112 /* Can the entry point ever not be at the beginning of an
28113 unfragmented lexical block? */
28114 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28115 || (cur_line_info_table
28116 && !ZERO_VIEW_P (cur_line_info_table->view))))
28117 return;
28119 if (!inline_entry_data_table)
28120 inline_entry_data_table
28121 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28124 inline_entry_data **iedp
28125 = inline_entry_data_table->find_slot_with_hash (block,
28126 htab_hash_pointer (block),
28127 INSERT);
28128 if (*iedp)
28129 /* ??? Ideally, we'd record all entry points for the same inlined
28130 function (some may have been duplicated by e.g. unrolling), but
28131 we have no way to represent that ATM. */
28132 return;
28134 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28135 ied->block = block;
28136 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28137 ied->label_num = BLOCK_NUMBER (block);
28138 if (cur_line_info_table)
28139 ied->view = cur_line_info_table->view;
28141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28142 BLOCK_NUMBER (block));
28145 /* Called from finalize_size_functions for size functions so that their body
28146 can be encoded in the debug info to describe the layout of variable-length
28147 structures. */
28149 static void
28150 dwarf2out_size_function (tree decl)
28152 set_early_dwarf s;
28153 function_to_dwarf_procedure (decl);
28156 /* Note in one location list that text section has changed. */
28159 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28161 var_loc_list *list = *slot;
28162 if (list->first)
28163 list->last_before_switch
28164 = list->last->next ? list->last->next : list->last;
28165 return 1;
28168 /* Note in all location lists that text section has changed. */
28170 static void
28171 var_location_switch_text_section (void)
28173 if (decl_loc_table == NULL)
28174 return;
28176 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28179 /* Create a new line number table. */
28181 static dw_line_info_table *
28182 new_line_info_table (void)
28184 dw_line_info_table *table;
28186 table = ggc_cleared_alloc<dw_line_info_table> ();
28187 table->file_num = 1;
28188 table->line_num = 1;
28189 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28190 FORCE_RESET_NEXT_VIEW (table->view);
28191 table->symviews_since_reset = 0;
28193 return table;
28196 /* Lookup the "current" table into which we emit line info, so
28197 that we don't have to do it for every source line. */
28199 static void
28200 set_cur_line_info_table (section *sec)
28202 dw_line_info_table *table;
28204 if (sec == text_section)
28205 table = text_section_line_info;
28206 else if (sec == cold_text_section)
28208 table = cold_text_section_line_info;
28209 if (!table)
28211 cold_text_section_line_info = table = new_line_info_table ();
28212 table->end_label = cold_end_label;
28215 else
28217 const char *end_label;
28219 if (crtl->has_bb_partition)
28221 if (in_cold_section_p)
28222 end_label = crtl->subsections.cold_section_end_label;
28223 else
28224 end_label = crtl->subsections.hot_section_end_label;
28226 else
28228 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28229 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28230 current_function_funcdef_no);
28231 end_label = ggc_strdup (label);
28234 table = new_line_info_table ();
28235 table->end_label = end_label;
28237 vec_safe_push (separate_line_info, table);
28240 if (output_asm_line_debug_info ())
28241 table->is_stmt = (cur_line_info_table
28242 ? cur_line_info_table->is_stmt
28243 : DWARF_LINE_DEFAULT_IS_STMT_START);
28244 cur_line_info_table = table;
28248 /* We need to reset the locations at the beginning of each
28249 function. We can't do this in the end_function hook, because the
28250 declarations that use the locations won't have been output when
28251 that hook is called. Also compute have_multiple_function_sections here. */
28253 static void
28254 dwarf2out_begin_function (tree fun)
28256 section *sec = function_section (fun);
28258 if (sec != text_section)
28259 have_multiple_function_sections = true;
28261 if (crtl->has_bb_partition && !cold_text_section)
28263 gcc_assert (current_function_decl == fun);
28264 cold_text_section = unlikely_text_section ();
28265 switch_to_section (cold_text_section);
28266 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28267 switch_to_section (sec);
28270 call_site_count = 0;
28271 tail_call_site_count = 0;
28273 set_cur_line_info_table (sec);
28274 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28277 /* Helper function of dwarf2out_end_function, called only after emitting
28278 the very first function into assembly. Check if some .debug_loc range
28279 might end with a .LVL* label that could be equal to .Ltext0.
28280 In that case we must force using absolute addresses in .debug_loc ranges,
28281 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28282 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28283 list terminator.
28284 Set have_multiple_function_sections to true in that case and
28285 terminate htab traversal. */
28288 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28290 var_loc_list *entry = *slot;
28291 struct var_loc_node *node;
28293 node = entry->first;
28294 if (node && node->next && node->next->label)
28296 unsigned int i;
28297 const char *label = node->next->label;
28298 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28300 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28302 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28303 if (strcmp (label, loclabel) == 0)
28305 have_multiple_function_sections = true;
28306 return 0;
28310 return 1;
28313 /* Hook called after emitting a function into assembly.
28314 This does something only for the very first function emitted. */
28316 static void
28317 dwarf2out_end_function (unsigned int)
28319 if (in_first_function_p
28320 && !have_multiple_function_sections
28321 && first_loclabel_num_not_at_text_label
28322 && decl_loc_table)
28323 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28324 in_first_function_p = false;
28325 maybe_at_text_label_p = false;
28328 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28329 front-ends register a translation unit even before dwarf2out_init is
28330 called. */
28331 static tree main_translation_unit = NULL_TREE;
28333 /* Hook called by front-ends after they built their main translation unit.
28334 Associate comp_unit_die to UNIT. */
28336 static void
28337 dwarf2out_register_main_translation_unit (tree unit)
28339 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28340 && main_translation_unit == NULL_TREE);
28341 main_translation_unit = unit;
28342 /* If dwarf2out_init has not been called yet, it will perform the association
28343 itself looking at main_translation_unit. */
28344 if (decl_die_table != NULL)
28345 equate_decl_number_to_die (unit, comp_unit_die ());
28348 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28350 static void
28351 push_dw_line_info_entry (dw_line_info_table *table,
28352 enum dw_line_info_opcode opcode, unsigned int val)
28354 dw_line_info_entry e;
28355 e.opcode = opcode;
28356 e.val = val;
28357 vec_safe_push (table->entries, e);
28360 /* Output a label to mark the beginning of a source code line entry
28361 and record information relating to this source line, in
28362 'line_info_table' for later output of the .debug_line section. */
28363 /* ??? The discriminator parameter ought to be unsigned. */
28365 static void
28366 dwarf2out_source_line (unsigned int line, unsigned int column,
28367 const char *filename,
28368 int discriminator, bool is_stmt)
28370 unsigned int file_num;
28371 dw_line_info_table *table;
28372 static var_loc_view lvugid;
28374 /* 'line_info_table' information gathering is not needed when the debug
28375 info level is set to the lowest value. Also, the current DWARF-based
28376 debug formats do not use this info. */
28377 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28378 return;
28380 table = cur_line_info_table;
28382 if (line == 0)
28384 if (debug_variable_location_views
28385 && output_asm_line_debug_info ()
28386 && table && !RESETTING_VIEW_P (table->view))
28388 /* If we're using the assembler to compute view numbers, we
28389 can't issue a .loc directive for line zero, so we can't
28390 get a view number at this point. We might attempt to
28391 compute it from the previous view, or equate it to a
28392 subsequent view (though it might not be there!), but
28393 since we're omitting the line number entry, we might as
28394 well omit the view number as well. That means pretending
28395 it's a view number zero, which might very well turn out
28396 to be correct. ??? Extend the assembler so that the
28397 compiler could emit e.g. ".locview .LVU#", to output a
28398 view without changing line number information. We'd then
28399 have to count it in symviews_since_reset; when it's omitted,
28400 it doesn't count. */
28401 if (!zero_view_p)
28402 zero_view_p = BITMAP_GGC_ALLOC ();
28403 bitmap_set_bit (zero_view_p, table->view);
28404 if (flag_debug_asm)
28406 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28407 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28408 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28409 ASM_COMMENT_START);
28410 assemble_name (asm_out_file, label);
28411 putc ('\n', asm_out_file);
28413 table->view = ++lvugid;
28415 return;
28418 /* The discriminator column was added in dwarf4. Simplify the below
28419 by simply removing it if we're not supposed to output it. */
28420 if (dwarf_version < 4 && dwarf_strict)
28421 discriminator = 0;
28423 if (!debug_column_info)
28424 column = 0;
28426 file_num = maybe_emit_file (lookup_filename (filename));
28428 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28429 the debugger has used the second (possibly duplicate) line number
28430 at the beginning of the function to mark the end of the prologue.
28431 We could eliminate any other duplicates within the function. For
28432 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28433 that second line number entry. */
28434 /* Recall that this end-of-prologue indication is *not* the same thing
28435 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28436 to which the hook corresponds, follows the last insn that was
28437 emitted by gen_prologue. What we need is to precede the first insn
28438 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28439 insn that corresponds to something the user wrote. These may be
28440 very different locations once scheduling is enabled. */
28442 if (0 && file_num == table->file_num
28443 && line == table->line_num
28444 && column == table->column_num
28445 && discriminator == table->discrim_num
28446 && is_stmt == table->is_stmt)
28447 return;
28449 switch_to_section (current_function_section ());
28451 /* If requested, emit something human-readable. */
28452 if (flag_debug_asm)
28454 if (debug_column_info)
28455 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28456 filename, line, column);
28457 else
28458 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28459 filename, line);
28462 if (output_asm_line_debug_info ())
28464 /* Emit the .loc directive understood by GNU as. */
28465 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28466 file_num, line, is_stmt, discriminator */
28467 fputs ("\t.loc ", asm_out_file);
28468 fprint_ul (asm_out_file, file_num);
28469 putc (' ', asm_out_file);
28470 fprint_ul (asm_out_file, line);
28471 putc (' ', asm_out_file);
28472 fprint_ul (asm_out_file, column);
28474 if (is_stmt != table->is_stmt)
28476 #if HAVE_GAS_LOC_STMT
28477 fputs (" is_stmt ", asm_out_file);
28478 putc (is_stmt ? '1' : '0', asm_out_file);
28479 #endif
28481 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28483 gcc_assert (discriminator > 0);
28484 fputs (" discriminator ", asm_out_file);
28485 fprint_ul (asm_out_file, (unsigned long) discriminator);
28487 if (debug_variable_location_views)
28489 if (!RESETTING_VIEW_P (table->view))
28491 table->symviews_since_reset++;
28492 if (table->symviews_since_reset > symview_upper_bound)
28493 symview_upper_bound = table->symviews_since_reset;
28494 /* When we're using the assembler to compute view
28495 numbers, we output symbolic labels after "view" in
28496 .loc directives, and the assembler will set them for
28497 us, so that we can refer to the view numbers in
28498 location lists. The only exceptions are when we know
28499 a view will be zero: "-0" is a forced reset, used
28500 e.g. in the beginning of functions, whereas "0" tells
28501 the assembler to check that there was a PC change
28502 since the previous view, in a way that implicitly
28503 resets the next view. */
28504 fputs (" view ", asm_out_file);
28505 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28506 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28507 assemble_name (asm_out_file, label);
28508 table->view = ++lvugid;
28510 else
28512 table->symviews_since_reset = 0;
28513 if (FORCE_RESETTING_VIEW_P (table->view))
28514 fputs (" view -0", asm_out_file);
28515 else
28516 fputs (" view 0", asm_out_file);
28517 /* Mark the present view as a zero view. Earlier debug
28518 binds may have already added its id to loclists to be
28519 emitted later, so we can't reuse the id for something
28520 else. However, it's good to know whether a view is
28521 known to be zero, because then we may be able to
28522 optimize out locviews that are all zeros, so take
28523 note of it in zero_view_p. */
28524 if (!zero_view_p)
28525 zero_view_p = BITMAP_GGC_ALLOC ();
28526 bitmap_set_bit (zero_view_p, lvugid);
28527 table->view = ++lvugid;
28530 putc ('\n', asm_out_file);
28532 else
28534 unsigned int label_num = ++line_info_label_num;
28536 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28538 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28539 push_dw_line_info_entry (table, LI_adv_address, label_num);
28540 else
28541 push_dw_line_info_entry (table, LI_set_address, label_num);
28542 if (debug_variable_location_views)
28544 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28545 if (resetting)
28546 table->view = 0;
28548 if (flag_debug_asm)
28549 fprintf (asm_out_file, "\t%s view %s%d\n",
28550 ASM_COMMENT_START,
28551 resetting ? "-" : "",
28552 table->view);
28554 table->view++;
28556 if (file_num != table->file_num)
28557 push_dw_line_info_entry (table, LI_set_file, file_num);
28558 if (discriminator != table->discrim_num)
28559 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28560 if (is_stmt != table->is_stmt)
28561 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28562 push_dw_line_info_entry (table, LI_set_line, line);
28563 if (debug_column_info)
28564 push_dw_line_info_entry (table, LI_set_column, column);
28567 table->file_num = file_num;
28568 table->line_num = line;
28569 table->column_num = column;
28570 table->discrim_num = discriminator;
28571 table->is_stmt = is_stmt;
28572 table->in_use = true;
28575 /* Record a source file location for a DECL_IGNORED_P function. */
28577 static void
28578 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28579 const char *filename)
28581 dw_fde_ref fde = cfun->fde;
28583 fde->ignored_debug = false;
28584 set_cur_line_info_table (function_section (fde->decl));
28586 dwarf2out_source_line (line, column, filename, 0, true);
28589 /* Record the beginning of a new source file. */
28591 static void
28592 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28594 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28596 macinfo_entry e;
28597 e.code = DW_MACINFO_start_file;
28598 e.lineno = lineno;
28599 e.info = ggc_strdup (filename);
28600 vec_safe_push (macinfo_table, e);
28604 /* Record the end of a source file. */
28606 static void
28607 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28609 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28611 macinfo_entry e;
28612 e.code = DW_MACINFO_end_file;
28613 e.lineno = lineno;
28614 e.info = NULL;
28615 vec_safe_push (macinfo_table, e);
28619 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28620 the tail part of the directive line, i.e. the part which is past the
28621 initial whitespace, #, whitespace, directive-name, whitespace part. */
28623 static void
28624 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28625 const char *buffer ATTRIBUTE_UNUSED)
28627 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28629 macinfo_entry e;
28630 /* Insert a dummy first entry to be able to optimize the whole
28631 predefined macro block using DW_MACRO_import. */
28632 if (macinfo_table->is_empty () && lineno <= 1)
28634 e.code = 0;
28635 e.lineno = 0;
28636 e.info = NULL;
28637 vec_safe_push (macinfo_table, e);
28639 e.code = DW_MACINFO_define;
28640 e.lineno = lineno;
28641 e.info = ggc_strdup (buffer);
28642 vec_safe_push (macinfo_table, e);
28646 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28647 the tail part of the directive line, i.e. the part which is past the
28648 initial whitespace, #, whitespace, directive-name, whitespace part. */
28650 static void
28651 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28652 const char *buffer ATTRIBUTE_UNUSED)
28654 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28656 macinfo_entry e;
28657 /* Insert a dummy first entry to be able to optimize the whole
28658 predefined macro block using DW_MACRO_import. */
28659 if (macinfo_table->is_empty () && lineno <= 1)
28661 e.code = 0;
28662 e.lineno = 0;
28663 e.info = NULL;
28664 vec_safe_push (macinfo_table, e);
28666 e.code = DW_MACINFO_undef;
28667 e.lineno = lineno;
28668 e.info = ggc_strdup (buffer);
28669 vec_safe_push (macinfo_table, e);
28673 /* Helpers to manipulate hash table of CUs. */
28675 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28677 static inline hashval_t hash (const macinfo_entry *);
28678 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28681 inline hashval_t
28682 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28684 return htab_hash_string (entry->info);
28687 inline bool
28688 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28689 const macinfo_entry *entry2)
28691 return !strcmp (entry1->info, entry2->info);
28694 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28696 /* Output a single .debug_macinfo entry. */
28698 static void
28699 output_macinfo_op (macinfo_entry *ref)
28701 int file_num;
28702 size_t len;
28703 struct indirect_string_node *node;
28704 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28705 struct dwarf_file_data *fd;
28707 switch (ref->code)
28709 case DW_MACINFO_start_file:
28710 fd = lookup_filename (ref->info);
28711 file_num = maybe_emit_file (fd);
28712 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28713 dw2_asm_output_data_uleb128 (ref->lineno,
28714 "Included from line number %lu",
28715 (unsigned long) ref->lineno);
28716 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28717 break;
28718 case DW_MACINFO_end_file:
28719 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28720 break;
28721 case DW_MACINFO_define:
28722 case DW_MACINFO_undef:
28723 len = strlen (ref->info) + 1;
28724 if ((!dwarf_strict || dwarf_version >= 5)
28725 && len > (size_t) dwarf_offset_size
28726 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28727 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28729 if (dwarf_split_debug_info && dwarf_version >= 5)
28730 ref->code = ref->code == DW_MACINFO_define
28731 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28732 else
28733 ref->code = ref->code == DW_MACINFO_define
28734 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28735 output_macinfo_op (ref);
28736 return;
28738 dw2_asm_output_data (1, ref->code,
28739 ref->code == DW_MACINFO_define
28740 ? "Define macro" : "Undefine macro");
28741 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28742 (unsigned long) ref->lineno);
28743 dw2_asm_output_nstring (ref->info, -1, "The macro");
28744 break;
28745 case DW_MACRO_define_strp:
28746 dw2_asm_output_data (1, ref->code, "Define macro strp");
28747 goto do_DW_MACRO_define_strpx;
28748 case DW_MACRO_undef_strp:
28749 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28750 goto do_DW_MACRO_define_strpx;
28751 case DW_MACRO_define_strx:
28752 dw2_asm_output_data (1, ref->code, "Define macro strx");
28753 goto do_DW_MACRO_define_strpx;
28754 case DW_MACRO_undef_strx:
28755 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28756 /* FALLTHRU */
28757 do_DW_MACRO_define_strpx:
28758 /* NB: dwarf2out_finish performs:
28759 1. save_macinfo_strings
28760 2. hash table traverse of index_string
28761 3. output_macinfo -> output_macinfo_op
28762 4. output_indirect_strings
28763 -> hash table traverse of output_index_string
28765 When output_macinfo_op is called, all index strings have been
28766 added to hash table by save_macinfo_strings and we can't pass
28767 INSERT to find_slot_with_hash which may expand hash table, even
28768 if no insertion is needed, and change hash table traverse order
28769 between index_string and output_index_string. */
28770 node = find_AT_string (ref->info, NO_INSERT);
28771 gcc_assert (node
28772 && (node->form == DW_FORM_strp
28773 || node->form == dwarf_FORM (DW_FORM_strx)));
28774 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28775 (unsigned long) ref->lineno);
28776 if (node->form == DW_FORM_strp)
28777 dw2_asm_output_offset (dwarf_offset_size, node->label,
28778 debug_str_section, "The macro: \"%s\"",
28779 ref->info);
28780 else
28781 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28782 ref->info);
28783 break;
28784 case DW_MACRO_import:
28785 dw2_asm_output_data (1, ref->code, "Import");
28786 ASM_GENERATE_INTERNAL_LABEL (label,
28787 DEBUG_MACRO_SECTION_LABEL,
28788 ref->lineno + macinfo_label_base);
28789 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28790 break;
28791 default:
28792 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28793 ASM_COMMENT_START, (unsigned long) ref->code);
28794 break;
28798 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28799 other compilation unit .debug_macinfo sections. IDX is the first
28800 index of a define/undef, return the number of ops that should be
28801 emitted in a comdat .debug_macinfo section and emit
28802 a DW_MACRO_import entry referencing it.
28803 If the define/undef entry should be emitted normally, return 0. */
28805 static unsigned
28806 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28807 macinfo_hash_type **macinfo_htab)
28809 macinfo_entry *first, *second, *cur, *inc;
28810 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28811 unsigned char checksum[16];
28812 struct md5_ctx ctx;
28813 char *grp_name, *tail;
28814 const char *base;
28815 unsigned int i, count, encoded_filename_len, linebuf_len;
28816 macinfo_entry **slot;
28818 first = &(*macinfo_table)[idx];
28819 second = &(*macinfo_table)[idx + 1];
28821 /* Optimize only if there are at least two consecutive define/undef ops,
28822 and either all of them are before first DW_MACINFO_start_file
28823 with lineno {0,1} (i.e. predefined macro block), or all of them are
28824 in some included header file. */
28825 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28826 return 0;
28827 if (vec_safe_is_empty (files))
28829 if (first->lineno > 1 || second->lineno > 1)
28830 return 0;
28832 else if (first->lineno == 0)
28833 return 0;
28835 /* Find the last define/undef entry that can be grouped together
28836 with first and at the same time compute md5 checksum of their
28837 codes, linenumbers and strings. */
28838 md5_init_ctx (&ctx);
28839 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28840 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28841 break;
28842 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28843 break;
28844 else
28846 unsigned char code = cur->code;
28847 md5_process_bytes (&code, 1, &ctx);
28848 checksum_uleb128 (cur->lineno, &ctx);
28849 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28851 md5_finish_ctx (&ctx, checksum);
28852 count = i - idx;
28854 /* From the containing include filename (if any) pick up just
28855 usable characters from its basename. */
28856 if (vec_safe_is_empty (files))
28857 base = "";
28858 else
28859 base = lbasename (files->last ().info);
28860 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28861 if (ISIDNUM (base[i]) || base[i] == '.')
28862 encoded_filename_len++;
28863 /* Count . at the end. */
28864 if (encoded_filename_len)
28865 encoded_filename_len++;
28867 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28868 linebuf_len = strlen (linebuf);
28870 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28871 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28872 + 16 * 2 + 1);
28873 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28874 tail = grp_name + 4;
28875 if (encoded_filename_len)
28877 for (i = 0; base[i]; i++)
28878 if (ISIDNUM (base[i]) || base[i] == '.')
28879 *tail++ = base[i];
28880 *tail++ = '.';
28882 memcpy (tail, linebuf, linebuf_len);
28883 tail += linebuf_len;
28884 *tail++ = '.';
28885 for (i = 0; i < 16; i++)
28886 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28888 /* Construct a macinfo_entry for DW_MACRO_import
28889 in the empty vector entry before the first define/undef. */
28890 inc = &(*macinfo_table)[idx - 1];
28891 inc->code = DW_MACRO_import;
28892 inc->lineno = 0;
28893 inc->info = ggc_strdup (grp_name);
28894 if (!*macinfo_htab)
28895 *macinfo_htab = new macinfo_hash_type (10);
28896 /* Avoid emitting duplicates. */
28897 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28898 if (*slot != NULL)
28900 inc->code = 0;
28901 inc->info = NULL;
28902 /* If such an entry has been used before, just emit
28903 a DW_MACRO_import op. */
28904 inc = *slot;
28905 output_macinfo_op (inc);
28906 /* And clear all macinfo_entry in the range to avoid emitting them
28907 in the second pass. */
28908 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28910 cur->code = 0;
28911 cur->info = NULL;
28914 else
28916 *slot = inc;
28917 inc->lineno = (*macinfo_htab)->elements ();
28918 output_macinfo_op (inc);
28920 return count;
28923 /* Save any strings needed by the macinfo table in the debug str
28924 table. All strings must be collected into the table by the time
28925 index_string is called. */
28927 static void
28928 save_macinfo_strings (void)
28930 unsigned len;
28931 unsigned i;
28932 macinfo_entry *ref;
28934 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28936 switch (ref->code)
28938 /* Match the logic in output_macinfo_op to decide on
28939 indirect strings. */
28940 case DW_MACINFO_define:
28941 case DW_MACINFO_undef:
28942 len = strlen (ref->info) + 1;
28943 if ((!dwarf_strict || dwarf_version >= 5)
28944 && len > (unsigned) dwarf_offset_size
28945 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28946 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28947 set_indirect_string (find_AT_string (ref->info));
28948 break;
28949 case DW_MACINFO_start_file:
28950 /* -gsplit-dwarf -g3 will also output filename as indirect
28951 string. */
28952 if (!dwarf_split_debug_info)
28953 break;
28954 /* Fall through. */
28955 case DW_MACRO_define_strp:
28956 case DW_MACRO_undef_strp:
28957 case DW_MACRO_define_strx:
28958 case DW_MACRO_undef_strx:
28959 set_indirect_string (find_AT_string (ref->info));
28960 break;
28961 default:
28962 break;
28967 /* Output macinfo section(s). */
28969 static void
28970 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28972 unsigned i;
28973 unsigned long length = vec_safe_length (macinfo_table);
28974 macinfo_entry *ref;
28975 vec<macinfo_entry, va_gc> *files = NULL;
28976 macinfo_hash_type *macinfo_htab = NULL;
28977 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28979 if (! length)
28980 return;
28982 /* output_macinfo* uses these interchangeably. */
28983 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28984 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28985 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28986 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28988 /* AIX Assembler inserts the length, so adjust the reference to match the
28989 offset expected by debuggers. */
28990 strcpy (dl_section_ref, debug_line_label);
28991 if (XCOFF_DEBUGGING_INFO)
28992 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28994 /* For .debug_macro emit the section header. */
28995 if (!dwarf_strict || dwarf_version >= 5)
28997 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28998 "DWARF macro version number");
28999 if (dwarf_offset_size == 8)
29000 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
29001 else
29002 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
29003 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
29004 debug_line_section, NULL);
29007 /* In the first loop, it emits the primary .debug_macinfo section
29008 and after each emitted op the macinfo_entry is cleared.
29009 If a longer range of define/undef ops can be optimized using
29010 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29011 the vector before the first define/undef in the range and the
29012 whole range of define/undef ops is not emitted and kept. */
29013 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29015 switch (ref->code)
29017 case DW_MACINFO_start_file:
29018 vec_safe_push (files, *ref);
29019 break;
29020 case DW_MACINFO_end_file:
29021 if (!vec_safe_is_empty (files))
29022 files->pop ();
29023 break;
29024 case DW_MACINFO_define:
29025 case DW_MACINFO_undef:
29026 if ((!dwarf_strict || dwarf_version >= 5)
29027 && HAVE_COMDAT_GROUP
29028 && vec_safe_length (files) != 1
29029 && i > 0
29030 && i + 1 < length
29031 && (*macinfo_table)[i - 1].code == 0)
29033 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29034 if (count)
29036 i += count - 1;
29037 continue;
29040 break;
29041 case 0:
29042 /* A dummy entry may be inserted at the beginning to be able
29043 to optimize the whole block of predefined macros. */
29044 if (i == 0)
29045 continue;
29046 default:
29047 break;
29049 output_macinfo_op (ref);
29050 ref->info = NULL;
29051 ref->code = 0;
29054 if (!macinfo_htab)
29055 return;
29057 /* Save the number of transparent includes so we can adjust the
29058 label number for the fat LTO object DWARF. */
29059 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29061 delete macinfo_htab;
29062 macinfo_htab = NULL;
29064 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29065 terminate the current chain and switch to a new comdat .debug_macinfo
29066 section and emit the define/undef entries within it. */
29067 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29068 switch (ref->code)
29070 case 0:
29071 continue;
29072 case DW_MACRO_import:
29074 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29075 tree comdat_key = get_identifier (ref->info);
29076 /* Terminate the previous .debug_macinfo section. */
29077 dw2_asm_output_data (1, 0, "End compilation unit");
29078 targetm.asm_out.named_section (debug_macinfo_section_name,
29079 SECTION_DEBUG
29080 | SECTION_LINKONCE
29081 | (early_lto_debug
29082 ? SECTION_EXCLUDE : 0),
29083 comdat_key);
29084 ASM_GENERATE_INTERNAL_LABEL (label,
29085 DEBUG_MACRO_SECTION_LABEL,
29086 ref->lineno + macinfo_label_base);
29087 ASM_OUTPUT_LABEL (asm_out_file, label);
29088 ref->code = 0;
29089 ref->info = NULL;
29090 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29091 "DWARF macro version number");
29092 if (dwarf_offset_size == 8)
29093 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29094 else
29095 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29097 break;
29098 case DW_MACINFO_define:
29099 case DW_MACINFO_undef:
29100 output_macinfo_op (ref);
29101 ref->code = 0;
29102 ref->info = NULL;
29103 break;
29104 default:
29105 gcc_unreachable ();
29108 macinfo_label_base += macinfo_label_base_adj;
29111 /* As init_sections_and_labels may get called multiple times, have a
29112 generation count for labels. */
29113 static unsigned init_sections_and_labels_generation;
29115 /* Initialize the various sections and labels for dwarf output and prefix
29116 them with PREFIX if non-NULL. Returns the generation (zero based
29117 number of times function was called). */
29119 static unsigned
29120 init_sections_and_labels (bool early_lto_debug)
29122 if (early_lto_debug)
29124 if (!dwarf_split_debug_info)
29126 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29127 SECTION_DEBUG | SECTION_EXCLUDE,
29128 NULL);
29129 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29130 SECTION_DEBUG | SECTION_EXCLUDE,
29131 NULL);
29132 debug_macinfo_section_name
29133 = ((dwarf_strict && dwarf_version < 5)
29134 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29135 debug_macinfo_section = get_section (debug_macinfo_section_name,
29136 SECTION_DEBUG
29137 | SECTION_EXCLUDE, NULL);
29139 else
29141 /* ??? Which of the following do we need early? */
29142 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29143 SECTION_DEBUG | SECTION_EXCLUDE,
29144 NULL);
29145 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29146 SECTION_DEBUG | SECTION_EXCLUDE,
29147 NULL);
29148 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29149 SECTION_DEBUG
29150 | SECTION_EXCLUDE, NULL);
29151 debug_skeleton_abbrev_section
29152 = get_section (DEBUG_LTO_ABBREV_SECTION,
29153 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29154 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29155 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29156 init_sections_and_labels_generation);
29158 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29159 stay in the main .o, but the skeleton_line goes into the split
29160 off dwo. */
29161 debug_skeleton_line_section
29162 = get_section (DEBUG_LTO_LINE_SECTION,
29163 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29164 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29165 DEBUG_SKELETON_LINE_SECTION_LABEL,
29166 init_sections_and_labels_generation);
29167 debug_str_offsets_section
29168 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29169 SECTION_DEBUG | SECTION_EXCLUDE,
29170 NULL);
29171 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29172 DEBUG_SKELETON_INFO_SECTION_LABEL,
29173 init_sections_and_labels_generation);
29174 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29175 DEBUG_STR_DWO_SECTION_FLAGS,
29176 NULL);
29177 debug_macinfo_section_name
29178 = ((dwarf_strict && dwarf_version < 5)
29179 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29180 debug_macinfo_section = get_section (debug_macinfo_section_name,
29181 SECTION_DEBUG | SECTION_EXCLUDE,
29182 NULL);
29184 /* For macro info and the file table we have to refer to a
29185 debug_line section. */
29186 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29187 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29188 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29189 DEBUG_LINE_SECTION_LABEL,
29190 init_sections_and_labels_generation);
29192 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29193 DEBUG_STR_SECTION_FLAGS
29194 | SECTION_EXCLUDE, NULL);
29195 if (!dwarf_split_debug_info)
29196 debug_line_str_section
29197 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29198 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29200 else
29202 if (!dwarf_split_debug_info)
29204 debug_info_section = get_section (DEBUG_INFO_SECTION,
29205 SECTION_DEBUG, NULL);
29206 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29207 SECTION_DEBUG, NULL);
29208 debug_loc_section = get_section (dwarf_version >= 5
29209 ? DEBUG_LOCLISTS_SECTION
29210 : DEBUG_LOC_SECTION,
29211 SECTION_DEBUG, NULL);
29212 debug_macinfo_section_name
29213 = ((dwarf_strict && dwarf_version < 5)
29214 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29215 debug_macinfo_section = get_section (debug_macinfo_section_name,
29216 SECTION_DEBUG, NULL);
29218 else
29220 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29221 SECTION_DEBUG | SECTION_EXCLUDE,
29222 NULL);
29223 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29224 SECTION_DEBUG | SECTION_EXCLUDE,
29225 NULL);
29226 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29227 SECTION_DEBUG, NULL);
29228 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29229 SECTION_DEBUG, NULL);
29230 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29231 SECTION_DEBUG, NULL);
29232 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29233 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29234 init_sections_and_labels_generation);
29236 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29237 stay in the main .o, but the skeleton_line goes into the
29238 split off dwo. */
29239 debug_skeleton_line_section
29240 = get_section (DEBUG_DWO_LINE_SECTION,
29241 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29242 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29243 DEBUG_SKELETON_LINE_SECTION_LABEL,
29244 init_sections_and_labels_generation);
29245 debug_str_offsets_section
29246 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29247 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29248 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29249 DEBUG_SKELETON_INFO_SECTION_LABEL,
29250 init_sections_and_labels_generation);
29251 debug_loc_section = get_section (dwarf_version >= 5
29252 ? DEBUG_DWO_LOCLISTS_SECTION
29253 : DEBUG_DWO_LOC_SECTION,
29254 SECTION_DEBUG | SECTION_EXCLUDE,
29255 NULL);
29256 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29257 DEBUG_STR_DWO_SECTION_FLAGS,
29258 NULL);
29259 debug_macinfo_section_name
29260 = ((dwarf_strict && dwarf_version < 5)
29261 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29262 debug_macinfo_section = get_section (debug_macinfo_section_name,
29263 SECTION_DEBUG | SECTION_EXCLUDE,
29264 NULL);
29265 if (dwarf_version >= 5)
29266 debug_ranges_dwo_section
29267 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29268 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29270 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29271 SECTION_DEBUG, NULL);
29272 debug_line_section = get_section (DEBUG_LINE_SECTION,
29273 SECTION_DEBUG, NULL);
29274 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29275 SECTION_DEBUG, NULL);
29276 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29277 SECTION_DEBUG, NULL);
29278 debug_str_section = get_section (DEBUG_STR_SECTION,
29279 DEBUG_STR_SECTION_FLAGS, NULL);
29280 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29281 || asm_outputs_debug_line_str ())
29282 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29283 DEBUG_STR_SECTION_FLAGS, NULL);
29285 debug_ranges_section = get_section (dwarf_version >= 5
29286 ? DEBUG_RNGLISTS_SECTION
29287 : DEBUG_RANGES_SECTION,
29288 SECTION_DEBUG, NULL);
29289 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29290 SECTION_DEBUG, NULL);
29293 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29294 DEBUG_ABBREV_SECTION_LABEL,
29295 init_sections_and_labels_generation);
29296 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29297 DEBUG_INFO_SECTION_LABEL,
29298 init_sections_and_labels_generation);
29299 info_section_emitted = false;
29300 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29301 DEBUG_LINE_SECTION_LABEL,
29302 init_sections_and_labels_generation);
29303 /* There are up to 6 unique ranges labels per generation.
29304 See also output_rnglists. */
29305 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29306 DEBUG_RANGES_SECTION_LABEL,
29307 init_sections_and_labels_generation * 6);
29308 if (dwarf_version >= 5 && dwarf_split_debug_info)
29309 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29310 DEBUG_RANGES_SECTION_LABEL,
29311 1 + init_sections_and_labels_generation * 6);
29312 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29313 DEBUG_ADDR_SECTION_LABEL,
29314 init_sections_and_labels_generation);
29315 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29316 (dwarf_strict && dwarf_version < 5)
29317 ? DEBUG_MACINFO_SECTION_LABEL
29318 : DEBUG_MACRO_SECTION_LABEL,
29319 init_sections_and_labels_generation);
29320 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29321 init_sections_and_labels_generation);
29323 ++init_sections_and_labels_generation;
29324 return init_sections_and_labels_generation - 1;
29327 /* Set up for Dwarf output at the start of compilation. */
29329 static void
29330 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29332 /* Allocate the file_table. */
29333 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29335 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29336 /* Allocate the decl_die_table. */
29337 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29339 /* Allocate the decl_loc_table. */
29340 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29342 /* Allocate the cached_dw_loc_list_table. */
29343 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29345 /* Allocate the initial hunk of the abbrev_die_table. */
29346 vec_alloc (abbrev_die_table, 256);
29347 /* Zero-th entry is allocated, but unused. */
29348 abbrev_die_table->quick_push (NULL);
29350 /* Allocate the dwarf_proc_stack_usage_map. */
29351 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29353 /* Allocate the pubtypes and pubnames vectors. */
29354 vec_alloc (pubname_table, 32);
29355 vec_alloc (pubtype_table, 32);
29357 vec_alloc (incomplete_types, 64);
29359 vec_alloc (used_rtx_array, 32);
29361 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29362 vec_alloc (macinfo_table, 64);
29363 #endif
29365 /* If front-ends already registered a main translation unit but we were not
29366 ready to perform the association, do this now. */
29367 if (main_translation_unit != NULL_TREE)
29368 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29371 /* Called before compile () starts outputtting functions, variables
29372 and toplevel asms into assembly. */
29374 static void
29375 dwarf2out_assembly_start (void)
29377 if (text_section_line_info)
29378 return;
29380 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29381 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29382 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29383 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29384 COLD_TEXT_SECTION_LABEL, 0);
29385 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29387 switch_to_section (text_section);
29388 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29389 #endif
29391 /* Make sure the line number table for .text always exists. */
29392 text_section_line_info = new_line_info_table ();
29393 text_section_line_info->end_label = text_end_label;
29395 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29396 cur_line_info_table = text_section_line_info;
29397 #endif
29399 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29400 && dwarf2out_do_cfi_asm ()
29401 && !dwarf2out_do_eh_frame ())
29402 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29404 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29405 if (output_asm_line_debug_info () && dwarf_version >= 5)
29407 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29408 tell it the comp_dir and main file name for the zero entry
29409 line table. */
29410 const char *comp_dir, *filename0;
29412 comp_dir = comp_dir_string ();
29413 if (comp_dir == NULL)
29414 comp_dir = "";
29416 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29417 if (filename0 == NULL)
29418 filename0 = "";
29420 fprintf (asm_out_file, "\t.file 0 ");
29421 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29422 fputc (' ', asm_out_file);
29423 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29424 fputc ('\n', asm_out_file);
29426 else
29427 #endif
29428 /* Work around for PR101575: output a dummy .file directive. */
29429 if (!last_emitted_file && dwarf_debuginfo_p ()
29430 && debug_info_level >= DINFO_LEVEL_TERSE)
29432 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29434 if (filename0 == NULL)
29435 filename0 = "<dummy>";
29436 maybe_emit_file (lookup_filename (filename0));
29440 /* A helper function for dwarf2out_finish called through
29441 htab_traverse. Assign a string its index. All strings must be
29442 collected into the table by the time index_string is called,
29443 because the indexing code relies on htab_traverse to traverse nodes
29444 in the same order for each run. */
29447 index_string (indirect_string_node **h, unsigned int *index)
29449 indirect_string_node *node = *h;
29451 find_string_form (node);
29452 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29454 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29455 node->index = *index;
29456 *index += 1;
29458 return 1;
29461 /* A helper function for output_indirect_strings called through
29462 htab_traverse. Output the offset to a string and update the
29463 current offset. */
29466 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29468 indirect_string_node *node = *h;
29470 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29472 /* Assert that this node has been assigned an index. */
29473 gcc_assert (node->index != NO_INDEX_ASSIGNED
29474 && node->index != NOT_INDEXED);
29475 dw2_asm_output_data (dwarf_offset_size, *offset,
29476 "indexed string 0x%x: %s", node->index, node->str);
29477 *offset += strlen (node->str) + 1;
29479 return 1;
29482 /* A helper function for dwarf2out_finish called through
29483 htab_traverse. Output the indexed string. */
29486 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29488 struct indirect_string_node *node = *h;
29490 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29492 /* Assert that the strings are output in the same order as their
29493 indexes were assigned. */
29494 gcc_assert (*cur_idx == node->index);
29495 assemble_string (node->str, strlen (node->str) + 1);
29496 *cur_idx += 1;
29498 return 1;
29501 /* A helper function for output_indirect_strings. Counts the number
29502 of index strings offsets. Must match the logic of the functions
29503 output_index_string[_offsets] above. */
29505 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29507 struct indirect_string_node *node = *h;
29509 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29510 *last_idx += 1;
29511 return 1;
29514 /* A helper function for dwarf2out_finish called through
29515 htab_traverse. Emit one queued .debug_str string. */
29518 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29520 struct indirect_string_node *node = *h;
29522 node->form = find_string_form (node);
29523 if (node->form == form && node->refcount > 0)
29525 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29526 assemble_string (node->str, strlen (node->str) + 1);
29529 return 1;
29532 /* Output the indexed string table. */
29534 static void
29535 output_indirect_strings (void)
29537 switch_to_section (debug_str_section);
29538 if (!dwarf_split_debug_info)
29539 debug_str_hash->traverse<enum dwarf_form,
29540 output_indirect_string> (DW_FORM_strp);
29541 else
29543 unsigned int offset = 0;
29544 unsigned int cur_idx = 0;
29546 if (skeleton_debug_str_hash)
29547 skeleton_debug_str_hash->traverse<enum dwarf_form,
29548 output_indirect_string> (DW_FORM_strp);
29550 switch_to_section (debug_str_offsets_section);
29551 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29552 header. Note that we don't need to generate a label to the
29553 actual index table following the header here, because this is
29554 for the split dwarf case only. In an .dwo file there is only
29555 one string offsets table (and one debug info section). But
29556 if we would start using string offset tables for the main (or
29557 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29558 pointing to the actual index after the header. Split dwarf
29559 units will never have a string offsets base attribute. When
29560 a split unit is moved into a .dwp file the string offsets can
29561 be found through the .debug_cu_index section table. */
29562 if (dwarf_version >= 5)
29564 unsigned int last_idx = 0;
29565 unsigned long str_offsets_length;
29567 debug_str_hash->traverse_noresize
29568 <unsigned int *, count_index_strings> (&last_idx);
29569 str_offsets_length = last_idx * dwarf_offset_size + 4;
29570 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29571 dw2_asm_output_data (4, 0xffffffff,
29572 "Escape value for 64-bit DWARF extension");
29573 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29574 "Length of string offsets unit");
29575 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29576 dw2_asm_output_data (2, 0, "Header zero padding");
29578 debug_str_hash->traverse_noresize
29579 <unsigned int *, output_index_string_offset> (&offset);
29580 switch_to_section (debug_str_dwo_section);
29581 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29582 (&cur_idx);
29586 /* Callback for htab_traverse to assign an index to an entry in the
29587 table, and to write that entry to the .debug_addr section. */
29590 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29592 addr_table_entry *entry = *slot;
29594 if (entry->refcount == 0)
29596 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29597 || entry->index == NOT_INDEXED);
29598 return 1;
29601 gcc_assert (entry->index == *cur_index);
29602 (*cur_index)++;
29604 switch (entry->kind)
29606 case ate_kind_rtx:
29607 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29608 "0x%x", entry->index);
29609 break;
29610 case ate_kind_rtx_dtprel:
29611 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29612 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29613 DWARF2_ADDR_SIZE,
29614 entry->addr.rtl);
29615 fputc ('\n', asm_out_file);
29616 break;
29617 case ate_kind_label:
29618 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29619 "0x%x", entry->index);
29620 break;
29621 default:
29622 gcc_unreachable ();
29624 return 1;
29627 /* A helper function for dwarf2out_finish. Counts the number
29628 of indexed addresses. Must match the logic of the functions
29629 output_addr_table_entry above. */
29631 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29633 addr_table_entry *entry = *slot;
29635 if (entry->refcount > 0)
29636 *last_idx += 1;
29637 return 1;
29640 /* Produce the .debug_addr section. */
29642 static void
29643 output_addr_table (void)
29645 unsigned int index = 0;
29646 if (addr_index_table == NULL || addr_index_table->size () == 0)
29647 return;
29649 switch_to_section (debug_addr_section);
29650 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29651 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29652 before DWARF5, didn't have a header for .debug_addr units.
29653 DWARF5 specifies a small header when address tables are used. */
29654 if (dwarf_version >= 5)
29656 unsigned int last_idx = 0;
29657 unsigned long addrs_length;
29659 addr_index_table->traverse_noresize
29660 <unsigned int *, count_index_addrs> (&last_idx);
29661 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29663 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29664 dw2_asm_output_data (4, 0xffffffff,
29665 "Escape value for 64-bit DWARF extension");
29666 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29667 "Length of Address Unit");
29668 dw2_asm_output_data (2, 5, "DWARF addr version");
29669 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29670 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29672 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29674 addr_index_table
29675 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29678 #if ENABLE_ASSERT_CHECKING
29679 /* Verify that all marks are clear. */
29681 static void
29682 verify_marks_clear (dw_die_ref die)
29684 dw_die_ref c;
29686 gcc_assert (! die->die_mark);
29687 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29689 #endif /* ENABLE_ASSERT_CHECKING */
29691 /* Clear the marks for a die and its children.
29692 Be cool if the mark isn't set. */
29694 static void
29695 prune_unmark_dies (dw_die_ref die)
29697 dw_die_ref c;
29699 if (die->die_mark)
29700 die->die_mark = 0;
29701 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29704 /* Given LOC that is referenced by a DIE we're marking as used, find all
29705 referenced DWARF procedures it references and mark them as used. */
29707 static void
29708 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29710 for (; loc != NULL; loc = loc->dw_loc_next)
29711 switch (loc->dw_loc_opc)
29713 case DW_OP_implicit_pointer:
29714 case DW_OP_convert:
29715 case DW_OP_reinterpret:
29716 case DW_OP_GNU_implicit_pointer:
29717 case DW_OP_GNU_convert:
29718 case DW_OP_GNU_reinterpret:
29719 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29720 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29721 break;
29722 case DW_OP_GNU_variable_value:
29723 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29725 dw_die_ref ref
29726 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29727 if (ref == NULL)
29728 break;
29729 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29730 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29731 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29733 /* FALLTHRU */
29734 case DW_OP_call2:
29735 case DW_OP_call4:
29736 case DW_OP_call_ref:
29737 case DW_OP_const_type:
29738 case DW_OP_GNU_const_type:
29739 case DW_OP_GNU_parameter_ref:
29740 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29741 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29742 break;
29743 case DW_OP_regval_type:
29744 case DW_OP_deref_type:
29745 case DW_OP_GNU_regval_type:
29746 case DW_OP_GNU_deref_type:
29747 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29748 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29749 break;
29750 case DW_OP_entry_value:
29751 case DW_OP_GNU_entry_value:
29752 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29753 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29754 break;
29755 default:
29756 break;
29760 /* Given DIE that we're marking as used, find any other dies
29761 it references as attributes and mark them as used. */
29763 static void
29764 prune_unused_types_walk_attribs (dw_die_ref die)
29766 dw_attr_node *a;
29767 unsigned ix;
29769 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29771 switch (AT_class (a))
29773 /* Make sure DWARF procedures referenced by location descriptions will
29774 get emitted. */
29775 case dw_val_class_loc:
29776 prune_unused_types_walk_loc_descr (AT_loc (a));
29777 break;
29778 case dw_val_class_loc_list:
29779 for (dw_loc_list_ref list = AT_loc_list (a);
29780 list != NULL;
29781 list = list->dw_loc_next)
29782 prune_unused_types_walk_loc_descr (list->expr);
29783 break;
29785 case dw_val_class_view_list:
29786 /* This points to a loc_list in another attribute, so it's
29787 already covered. */
29788 break;
29790 case dw_val_class_die_ref:
29791 /* A reference to another DIE.
29792 Make sure that it will get emitted.
29793 If it was broken out into a comdat group, don't follow it. */
29794 if (! AT_ref (a)->comdat_type_p
29795 || a->dw_attr == DW_AT_specification)
29796 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29797 break;
29799 case dw_val_class_str:
29800 /* Set the string's refcount to 0 so that prune_unused_types_mark
29801 accounts properly for it. */
29802 a->dw_attr_val.v.val_str->refcount = 0;
29803 break;
29805 default:
29806 break;
29811 /* Mark the generic parameters and arguments children DIEs of DIE. */
29813 static void
29814 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29816 dw_die_ref c;
29818 if (die == NULL || die->die_child == NULL)
29819 return;
29820 c = die->die_child;
29823 if (is_template_parameter (c))
29824 prune_unused_types_mark (c, 1);
29825 c = c->die_sib;
29826 } while (c && c != die->die_child);
29829 /* Mark DIE as being used. If DOKIDS is true, then walk down
29830 to DIE's children. */
29832 static void
29833 prune_unused_types_mark (dw_die_ref die, int dokids)
29835 dw_die_ref c;
29837 if (die->die_mark == 0)
29839 /* We haven't done this node yet. Mark it as used. */
29840 die->die_mark = 1;
29841 /* If this is the DIE of a generic type instantiation,
29842 mark the children DIEs that describe its generic parms and
29843 args. */
29844 prune_unused_types_mark_generic_parms_dies (die);
29846 /* We also have to mark its parents as used.
29847 (But we don't want to mark our parent's kids due to this,
29848 unless it is a class.) */
29849 if (die->die_parent)
29850 prune_unused_types_mark (die->die_parent,
29851 class_scope_p (die->die_parent));
29853 /* Mark any referenced nodes. */
29854 prune_unused_types_walk_attribs (die);
29856 /* If this node is a specification,
29857 also mark the definition, if it exists. */
29858 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29859 prune_unused_types_mark (die->die_definition, 1);
29862 if (dokids && die->die_mark != 2)
29864 /* We need to walk the children, but haven't done so yet.
29865 Remember that we've walked the kids. */
29866 die->die_mark = 2;
29868 /* If this is an array type, we need to make sure our
29869 kids get marked, even if they're types. If we're
29870 breaking out types into comdat sections, do this
29871 for all type definitions. */
29872 if (die->die_tag == DW_TAG_array_type
29873 || (use_debug_types
29874 && is_type_die (die) && ! is_declaration_die (die)))
29875 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29876 else
29877 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29881 /* For local classes, look if any static member functions were emitted
29882 and if so, mark them. */
29884 static void
29885 prune_unused_types_walk_local_classes (dw_die_ref die)
29887 dw_die_ref c;
29889 if (die->die_mark == 2)
29890 return;
29892 switch (die->die_tag)
29894 case DW_TAG_structure_type:
29895 case DW_TAG_union_type:
29896 case DW_TAG_class_type:
29897 case DW_TAG_interface_type:
29898 break;
29900 case DW_TAG_subprogram:
29901 if (!get_AT_flag (die, DW_AT_declaration)
29902 || die->die_definition != NULL)
29903 prune_unused_types_mark (die, 1);
29904 return;
29906 default:
29907 return;
29910 /* Mark children. */
29911 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29914 /* Walk the tree DIE and mark types that we actually use. */
29916 static void
29917 prune_unused_types_walk (dw_die_ref die)
29919 dw_die_ref c;
29921 /* Don't do anything if this node is already marked and
29922 children have been marked as well. */
29923 if (die->die_mark == 2)
29924 return;
29926 switch (die->die_tag)
29928 case DW_TAG_structure_type:
29929 case DW_TAG_union_type:
29930 case DW_TAG_class_type:
29931 case DW_TAG_interface_type:
29932 if (die->die_perennial_p)
29933 break;
29935 for (c = die->die_parent; c; c = c->die_parent)
29936 if (c->die_tag == DW_TAG_subprogram)
29937 break;
29939 /* Finding used static member functions inside of classes
29940 is needed just for local classes, because for other classes
29941 static member function DIEs with DW_AT_specification
29942 are emitted outside of the DW_TAG_*_type. If we ever change
29943 it, we'd need to call this even for non-local classes. */
29944 if (c)
29945 prune_unused_types_walk_local_classes (die);
29947 /* It's a type node --- don't mark it. */
29948 return;
29950 case DW_TAG_const_type:
29951 case DW_TAG_packed_type:
29952 case DW_TAG_pointer_type:
29953 case DW_TAG_reference_type:
29954 case DW_TAG_rvalue_reference_type:
29955 case DW_TAG_volatile_type:
29956 case DW_TAG_typedef:
29957 case DW_TAG_array_type:
29958 case DW_TAG_friend:
29959 case DW_TAG_enumeration_type:
29960 case DW_TAG_subroutine_type:
29961 case DW_TAG_string_type:
29962 case DW_TAG_set_type:
29963 case DW_TAG_subrange_type:
29964 case DW_TAG_ptr_to_member_type:
29965 case DW_TAG_file_type:
29966 /* Type nodes are useful only when other DIEs reference them --- don't
29967 mark them. */
29968 /* FALLTHROUGH */
29970 case DW_TAG_dwarf_procedure:
29971 /* Likewise for DWARF procedures. */
29973 if (die->die_perennial_p)
29974 break;
29976 return;
29978 case DW_TAG_variable:
29979 if (flag_debug_only_used_symbols)
29981 if (die->die_perennial_p)
29982 break;
29984 /* For static data members, the declaration in the class is supposed
29985 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29986 DWARF5. DW_TAG_member will be marked, so mark even such
29987 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29988 attribute. */
29989 if (dwarf_version >= 5
29990 && class_scope_p (die->die_parent)
29991 && get_AT (die, DW_AT_const_value))
29992 break;
29994 /* premark_used_variables marks external variables --- don't mark
29995 them here. But function-local externals are always considered
29996 used. */
29997 if (get_AT (die, DW_AT_external))
29999 for (c = die->die_parent; c; c = c->die_parent)
30000 if (c->die_tag == DW_TAG_subprogram)
30001 break;
30002 if (!c)
30003 return;
30006 /* FALLTHROUGH */
30008 default:
30009 /* Mark everything else. */
30010 break;
30013 if (die->die_mark == 0)
30015 die->die_mark = 1;
30017 /* Now, mark any dies referenced from here. */
30018 prune_unused_types_walk_attribs (die);
30021 die->die_mark = 2;
30023 /* Mark children. */
30024 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30027 /* Increment the string counts on strings referred to from DIE's
30028 attributes. */
30030 static void
30031 prune_unused_types_update_strings (dw_die_ref die)
30033 dw_attr_node *a;
30034 unsigned ix;
30036 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30037 if (AT_class (a) == dw_val_class_str)
30039 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30040 s->refcount++;
30041 /* Avoid unnecessarily putting strings that are used less than
30042 twice in the hash table. */
30043 if (s->form != DW_FORM_line_strp
30044 && (s->refcount
30045 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30047 indirect_string_node **slot
30048 = debug_str_hash->find_slot_with_hash (s->str,
30049 htab_hash_string (s->str),
30050 INSERT);
30051 gcc_assert (*slot == NULL);
30052 *slot = s;
30057 /* Mark DIE and its children as removed. */
30059 static void
30060 mark_removed (dw_die_ref die)
30062 dw_die_ref c;
30063 die->removed = true;
30064 FOR_EACH_CHILD (die, c, mark_removed (c));
30067 /* Remove from the tree DIE any dies that aren't marked. */
30069 static void
30070 prune_unused_types_prune (dw_die_ref die)
30072 dw_die_ref c;
30074 gcc_assert (die->die_mark);
30075 prune_unused_types_update_strings (die);
30077 if (! die->die_child)
30078 return;
30080 c = die->die_child;
30081 do {
30082 dw_die_ref prev = c, next;
30083 for (c = c->die_sib; ! c->die_mark; c = next)
30084 if (c == die->die_child)
30086 /* No marked children between 'prev' and the end of the list. */
30087 if (prev == c)
30088 /* No marked children at all. */
30089 die->die_child = NULL;
30090 else
30092 prev->die_sib = c->die_sib;
30093 die->die_child = prev;
30095 c->die_sib = NULL;
30096 mark_removed (c);
30097 return;
30099 else
30101 next = c->die_sib;
30102 c->die_sib = NULL;
30103 mark_removed (c);
30106 if (c != prev->die_sib)
30107 prev->die_sib = c;
30108 prune_unused_types_prune (c);
30109 } while (c != die->die_child);
30112 /* Remove dies representing declarations that we never use. */
30114 static void
30115 prune_unused_types (void)
30117 unsigned int i;
30118 limbo_die_node *node;
30119 comdat_type_node *ctnode;
30120 pubname_entry *pub;
30121 dw_die_ref base_type;
30123 #if ENABLE_ASSERT_CHECKING
30124 /* All the marks should already be clear. */
30125 verify_marks_clear (comp_unit_die ());
30126 for (node = limbo_die_list; node; node = node->next)
30127 verify_marks_clear (node->die);
30128 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30129 verify_marks_clear (ctnode->root_die);
30130 #endif /* ENABLE_ASSERT_CHECKING */
30132 /* Mark types that are used in global variables. */
30133 premark_types_used_by_global_vars ();
30135 /* Mark variables used in the symtab. */
30136 if (flag_debug_only_used_symbols)
30137 premark_used_variables ();
30139 /* Set the mark on nodes that are actually used. */
30140 prune_unused_types_walk (comp_unit_die ());
30141 for (node = limbo_die_list; node; node = node->next)
30142 prune_unused_types_walk (node->die);
30143 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30145 prune_unused_types_walk (ctnode->root_die);
30146 prune_unused_types_mark (ctnode->type_die, 1);
30149 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30150 are unusual in that they are pubnames that are the children of pubtypes.
30151 They should only be marked via their parent DW_TAG_enumeration_type die,
30152 not as roots in themselves. */
30153 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30154 if (pub->die->die_tag != DW_TAG_enumerator)
30155 prune_unused_types_mark (pub->die, 1);
30156 for (i = 0; base_types.iterate (i, &base_type); i++)
30157 prune_unused_types_mark (base_type, 1);
30159 /* Also set the mark on nodes that could be referenced by
30160 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30161 by DW_TAG_inlined_subroutine origins. */
30162 cgraph_node *cnode;
30163 FOR_EACH_FUNCTION (cnode)
30164 if (cnode->referred_to_p (false))
30166 dw_die_ref die = lookup_decl_die (cnode->decl);
30167 if (die == NULL || die->die_mark)
30168 continue;
30169 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30170 if (e->caller != cnode)
30172 prune_unused_types_mark (die, 1);
30173 break;
30177 if (debug_str_hash)
30178 debug_str_hash->empty ();
30179 if (skeleton_debug_str_hash)
30180 skeleton_debug_str_hash->empty ();
30181 prune_unused_types_prune (comp_unit_die ());
30182 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30184 node = *pnode;
30185 if (!node->die->die_mark)
30186 *pnode = node->next;
30187 else
30189 prune_unused_types_prune (node->die);
30190 pnode = &node->next;
30193 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30194 prune_unused_types_prune (ctnode->root_die);
30196 /* Leave the marks clear. */
30197 prune_unmark_dies (comp_unit_die ());
30198 for (node = limbo_die_list; node; node = node->next)
30199 prune_unmark_dies (node->die);
30200 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30201 prune_unmark_dies (ctnode->root_die);
30204 /* Helpers to manipulate hash table of comdat type units. */
30206 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30208 static inline hashval_t hash (const comdat_type_node *);
30209 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30212 inline hashval_t
30213 comdat_type_hasher::hash (const comdat_type_node *type_node)
30215 hashval_t h;
30216 memcpy (&h, type_node->signature, sizeof (h));
30217 return h;
30220 inline bool
30221 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30222 const comdat_type_node *type_node_2)
30224 return (! memcmp (type_node_1->signature, type_node_2->signature,
30225 DWARF_TYPE_SIGNATURE_SIZE));
30228 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30229 to the location it would have been added, should we know its
30230 DECL_ASSEMBLER_NAME when we added other attributes. This will
30231 probably improve compactness of debug info, removing equivalent
30232 abbrevs, and hide any differences caused by deferring the
30233 computation of the assembler name, triggered by e.g. PCH. */
30235 static inline void
30236 move_linkage_attr (dw_die_ref die)
30238 unsigned ix = vec_safe_length (die->die_attr);
30239 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30241 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30242 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30244 while (--ix > 0)
30246 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30248 if (prev->dw_attr == DW_AT_decl_line
30249 || prev->dw_attr == DW_AT_decl_column
30250 || prev->dw_attr == DW_AT_name)
30251 break;
30254 if (ix != vec_safe_length (die->die_attr) - 1)
30256 die->die_attr->pop ();
30257 die->die_attr->quick_insert (ix, linkage);
30261 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30262 referenced from typed stack ops and count how often they are used. */
30264 static void
30265 mark_base_types (dw_loc_descr_ref loc)
30267 dw_die_ref base_type = NULL;
30269 for (; loc; loc = loc->dw_loc_next)
30271 switch (loc->dw_loc_opc)
30273 case DW_OP_regval_type:
30274 case DW_OP_deref_type:
30275 case DW_OP_GNU_regval_type:
30276 case DW_OP_GNU_deref_type:
30277 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30278 break;
30279 case DW_OP_convert:
30280 case DW_OP_reinterpret:
30281 case DW_OP_GNU_convert:
30282 case DW_OP_GNU_reinterpret:
30283 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30284 continue;
30285 /* FALLTHRU */
30286 case DW_OP_const_type:
30287 case DW_OP_GNU_const_type:
30288 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30289 break;
30290 case DW_OP_entry_value:
30291 case DW_OP_GNU_entry_value:
30292 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30293 continue;
30294 default:
30295 continue;
30297 gcc_assert (base_type->die_parent == comp_unit_die ());
30298 if (base_type->die_mark)
30299 base_type->die_mark++;
30300 else
30302 base_types.safe_push (base_type);
30303 base_type->die_mark = 1;
30308 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30309 referenced from typed stack ops and count how often they are used. */
30311 static void
30312 mark_base_types (dw_die_ref die)
30314 dw_die_ref c;
30315 dw_attr_node *a;
30316 dw_loc_list_ref *curr;
30317 unsigned ix;
30319 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30320 switch (AT_class (a))
30322 case dw_val_class_loc_list:
30323 curr = AT_loc_list_ptr (a);
30324 while (*curr)
30326 mark_base_types ((*curr)->expr);
30327 curr = &(*curr)->dw_loc_next;
30329 break;
30331 case dw_val_class_loc:
30332 mark_base_types (AT_loc (a));
30333 break;
30335 default:
30336 break;
30339 FOR_EACH_CHILD (die, c, mark_base_types (c));
30342 /* Comparison function for sorting marked base types. */
30344 static int
30345 base_type_cmp (const void *x, const void *y)
30347 dw_die_ref dx = *(const dw_die_ref *) x;
30348 dw_die_ref dy = *(const dw_die_ref *) y;
30349 unsigned int byte_size1, byte_size2;
30350 unsigned int encoding1, encoding2;
30351 unsigned int align1, align2;
30352 if (dx->die_mark > dy->die_mark)
30353 return -1;
30354 if (dx->die_mark < dy->die_mark)
30355 return 1;
30356 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30357 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30358 if (byte_size1 < byte_size2)
30359 return 1;
30360 if (byte_size1 > byte_size2)
30361 return -1;
30362 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30363 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30364 if (encoding1 < encoding2)
30365 return 1;
30366 if (encoding1 > encoding2)
30367 return -1;
30368 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30369 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30370 if (align1 < align2)
30371 return 1;
30372 if (align1 > align2)
30373 return -1;
30374 return 0;
30377 /* Move base types marked by mark_base_types as early as possible
30378 in the CU, sorted by decreasing usage count both to make the
30379 uleb128 references as small as possible and to make sure they
30380 will have die_offset already computed by calc_die_sizes when
30381 sizes of typed stack loc ops is computed. */
30383 static void
30384 move_marked_base_types (void)
30386 unsigned int i;
30387 dw_die_ref base_type, die, c;
30389 if (base_types.is_empty ())
30390 return;
30392 /* Sort by decreasing usage count, they will be added again in that
30393 order later on. */
30394 base_types.qsort (base_type_cmp);
30395 die = comp_unit_die ();
30396 c = die->die_child;
30399 dw_die_ref prev = c;
30400 c = c->die_sib;
30401 while (c->die_mark)
30403 remove_child_with_prev (c, prev);
30404 /* As base types got marked, there must be at least
30405 one node other than DW_TAG_base_type. */
30406 gcc_assert (die->die_child != NULL);
30407 c = prev->die_sib;
30410 while (c != die->die_child);
30411 gcc_assert (die->die_child);
30412 c = die->die_child;
30413 for (i = 0; base_types.iterate (i, &base_type); i++)
30415 base_type->die_mark = 0;
30416 base_type->die_sib = c->die_sib;
30417 c->die_sib = base_type;
30418 c = base_type;
30422 /* Helper function for resolve_addr, attempt to resolve
30423 one CONST_STRING, return true if successful. Similarly verify that
30424 SYMBOL_REFs refer to variables emitted in the current CU. */
30426 static bool
30427 resolve_one_addr (rtx *addr)
30429 rtx rtl = *addr;
30431 if (GET_CODE (rtl) == CONST_STRING)
30433 size_t len = strlen (XSTR (rtl, 0)) + 1;
30434 tree t = build_string (len, XSTR (rtl, 0));
30435 tree tlen = size_int (len - 1);
30436 TREE_TYPE (t)
30437 = build_array_type (char_type_node, build_index_type (tlen));
30438 rtl = lookup_constant_def (t);
30439 if (!rtl || !MEM_P (rtl))
30440 return false;
30441 rtl = XEXP (rtl, 0);
30442 if (GET_CODE (rtl) == SYMBOL_REF
30443 && SYMBOL_REF_DECL (rtl)
30444 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30445 return false;
30446 vec_safe_push (used_rtx_array, rtl);
30447 *addr = rtl;
30448 return true;
30451 if (GET_CODE (rtl) == SYMBOL_REF
30452 && SYMBOL_REF_DECL (rtl))
30454 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30456 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30457 return false;
30459 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30460 return false;
30463 if (GET_CODE (rtl) == CONST)
30465 subrtx_ptr_iterator::array_type array;
30466 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30467 if (!resolve_one_addr (*iter))
30468 return false;
30471 return true;
30474 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30475 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30476 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30478 static rtx
30479 string_cst_pool_decl (tree t)
30481 rtx rtl = output_constant_def (t, 1);
30482 unsigned char *array;
30483 dw_loc_descr_ref l;
30484 tree decl;
30485 size_t len;
30486 dw_die_ref ref;
30488 if (!rtl || !MEM_P (rtl))
30489 return NULL_RTX;
30490 rtl = XEXP (rtl, 0);
30491 if (GET_CODE (rtl) != SYMBOL_REF
30492 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30493 return NULL_RTX;
30495 decl = SYMBOL_REF_DECL (rtl);
30496 if (!lookup_decl_die (decl))
30498 len = TREE_STRING_LENGTH (t);
30499 vec_safe_push (used_rtx_array, rtl);
30500 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30501 array = ggc_vec_alloc<unsigned char> (len);
30502 memcpy (array, TREE_STRING_POINTER (t), len);
30503 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30504 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30505 l->dw_loc_oprnd2.v.val_vec.length = len;
30506 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30507 l->dw_loc_oprnd2.v.val_vec.array = array;
30508 add_AT_loc (ref, DW_AT_location, l);
30509 equate_decl_number_to_die (decl, ref);
30511 return rtl;
30514 /* Helper function of resolve_addr_in_expr. LOC is
30515 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30516 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30517 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30518 with DW_OP_implicit_pointer if possible
30519 and return true, if unsuccessful, return false. */
30521 static bool
30522 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30524 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30525 HOST_WIDE_INT offset = 0;
30526 dw_die_ref ref = NULL;
30527 tree decl;
30529 if (GET_CODE (rtl) == CONST
30530 && GET_CODE (XEXP (rtl, 0)) == PLUS
30531 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30533 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30534 rtl = XEXP (XEXP (rtl, 0), 0);
30536 if (GET_CODE (rtl) == CONST_STRING)
30538 size_t len = strlen (XSTR (rtl, 0)) + 1;
30539 tree t = build_string (len, XSTR (rtl, 0));
30540 tree tlen = size_int (len - 1);
30542 TREE_TYPE (t)
30543 = build_array_type (char_type_node, build_index_type (tlen));
30544 rtl = string_cst_pool_decl (t);
30545 if (!rtl)
30546 return false;
30548 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30550 decl = SYMBOL_REF_DECL (rtl);
30551 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30553 ref = lookup_decl_die (decl);
30554 if (ref && (get_AT (ref, DW_AT_location)
30555 || get_AT (ref, DW_AT_const_value)))
30557 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30558 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30559 loc->dw_loc_oprnd1.val_entry = NULL;
30560 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30561 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30562 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30563 loc->dw_loc_oprnd2.v.val_int = offset;
30564 return true;
30568 return false;
30571 /* Helper function for resolve_addr, handle one location
30572 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30573 the location list couldn't be resolved. */
30575 static bool
30576 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30578 dw_loc_descr_ref keep = NULL;
30579 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30580 switch (loc->dw_loc_opc)
30582 case DW_OP_addr:
30583 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30585 if ((prev == NULL
30586 || prev->dw_loc_opc == DW_OP_piece
30587 || prev->dw_loc_opc == DW_OP_bit_piece)
30588 && loc->dw_loc_next
30589 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30590 && (!dwarf_strict || dwarf_version >= 5)
30591 && optimize_one_addr_into_implicit_ptr (loc))
30592 break;
30593 return false;
30595 break;
30596 case DW_OP_GNU_addr_index:
30597 case DW_OP_addrx:
30598 case DW_OP_GNU_const_index:
30599 case DW_OP_constx:
30600 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30601 || loc->dw_loc_opc == DW_OP_addrx)
30602 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30603 || loc->dw_loc_opc == DW_OP_constx)
30604 && loc->dtprel))
30606 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30607 if (!resolve_one_addr (&rtl))
30608 return false;
30609 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30610 loc->dw_loc_oprnd1.val_entry
30611 = add_addr_table_entry (rtl, ate_kind_rtx);
30613 break;
30614 case DW_OP_const4u:
30615 case DW_OP_const8u:
30616 if (loc->dtprel
30617 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30618 return false;
30619 break;
30620 case DW_OP_plus_uconst:
30621 if (size_of_loc_descr (loc)
30622 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30624 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30626 dw_loc_descr_ref repl
30627 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30628 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30629 add_loc_descr (&repl, loc->dw_loc_next);
30630 *loc = *repl;
30632 break;
30633 case DW_OP_implicit_value:
30634 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30635 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30636 return false;
30637 break;
30638 case DW_OP_implicit_pointer:
30639 case DW_OP_GNU_implicit_pointer:
30640 case DW_OP_GNU_parameter_ref:
30641 case DW_OP_GNU_variable_value:
30642 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30644 dw_die_ref ref
30645 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30646 if (ref == NULL)
30647 return false;
30648 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30649 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30650 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30652 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30654 if (prev == NULL
30655 && loc->dw_loc_next == NULL
30656 && AT_class (a) == dw_val_class_loc)
30657 switch (a->dw_attr)
30659 /* Following attributes allow both exprloc and reference,
30660 so if the whole expression is DW_OP_GNU_variable_value
30661 alone we could transform it into reference. */
30662 case DW_AT_byte_size:
30663 case DW_AT_bit_size:
30664 case DW_AT_lower_bound:
30665 case DW_AT_upper_bound:
30666 case DW_AT_bit_stride:
30667 case DW_AT_count:
30668 case DW_AT_allocated:
30669 case DW_AT_associated:
30670 case DW_AT_byte_stride:
30671 a->dw_attr_val.val_class = dw_val_class_die_ref;
30672 a->dw_attr_val.val_entry = NULL;
30673 a->dw_attr_val.v.val_die_ref.die
30674 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30675 a->dw_attr_val.v.val_die_ref.external = 0;
30676 return true;
30677 default:
30678 break;
30680 if (dwarf_strict)
30681 return false;
30683 break;
30684 case DW_OP_const_type:
30685 case DW_OP_regval_type:
30686 case DW_OP_deref_type:
30687 case DW_OP_convert:
30688 case DW_OP_reinterpret:
30689 case DW_OP_GNU_const_type:
30690 case DW_OP_GNU_regval_type:
30691 case DW_OP_GNU_deref_type:
30692 case DW_OP_GNU_convert:
30693 case DW_OP_GNU_reinterpret:
30694 while (loc->dw_loc_next
30695 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30696 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30698 dw_die_ref base1, base2;
30699 unsigned enc1, enc2, size1, size2;
30700 if (loc->dw_loc_opc == DW_OP_regval_type
30701 || loc->dw_loc_opc == DW_OP_deref_type
30702 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30703 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30704 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30705 else if (loc->dw_loc_oprnd1.val_class
30706 == dw_val_class_unsigned_const)
30707 break;
30708 else
30709 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30710 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30711 == dw_val_class_unsigned_const)
30712 break;
30713 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30714 gcc_assert (base1->die_tag == DW_TAG_base_type
30715 && base2->die_tag == DW_TAG_base_type);
30716 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30717 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30718 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30719 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30720 if (size1 == size2
30721 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30722 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30723 && loc != keep)
30724 || enc1 == enc2))
30726 /* Optimize away next DW_OP_convert after
30727 adjusting LOC's base type die reference. */
30728 if (loc->dw_loc_opc == DW_OP_regval_type
30729 || loc->dw_loc_opc == DW_OP_deref_type
30730 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30731 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30732 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30733 else
30734 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30735 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30736 continue;
30738 /* Don't change integer DW_OP_convert after e.g. floating
30739 point typed stack entry. */
30740 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30741 keep = loc->dw_loc_next;
30742 break;
30744 break;
30745 default:
30746 break;
30748 return true;
30751 /* Helper function of resolve_addr. DIE had DW_AT_location of
30752 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30753 and DW_OP_addr couldn't be resolved. resolve_addr has already
30754 removed the DW_AT_location attribute. This function attempts to
30755 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30756 to it or DW_AT_const_value attribute, if possible. */
30758 static void
30759 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30761 if (!VAR_P (decl)
30762 || lookup_decl_die (decl) != die
30763 || DECL_EXTERNAL (decl)
30764 || !TREE_STATIC (decl)
30765 || DECL_INITIAL (decl) == NULL_TREE
30766 || DECL_P (DECL_INITIAL (decl))
30767 || get_AT (die, DW_AT_const_value))
30768 return;
30770 tree init = DECL_INITIAL (decl);
30771 HOST_WIDE_INT offset = 0;
30772 /* For variables that have been optimized away and thus
30773 don't have a memory location, see if we can emit
30774 DW_AT_const_value instead. */
30775 if (tree_add_const_value_attribute (die, init))
30776 return;
30777 if (dwarf_strict && dwarf_version < 5)
30778 return;
30779 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30780 and ADDR_EXPR refers to a decl that has DW_AT_location or
30781 DW_AT_const_value (but isn't addressable, otherwise
30782 resolving the original DW_OP_addr wouldn't fail), see if
30783 we can add DW_OP_implicit_pointer. */
30784 STRIP_NOPS (init);
30785 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30786 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30788 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30789 init = TREE_OPERAND (init, 0);
30790 STRIP_NOPS (init);
30792 if (TREE_CODE (init) != ADDR_EXPR)
30793 return;
30794 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30795 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30796 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30797 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30798 && TREE_OPERAND (init, 0) != decl))
30800 dw_die_ref ref;
30801 dw_loc_descr_ref l;
30803 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30805 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30806 if (!rtl)
30807 return;
30808 decl = SYMBOL_REF_DECL (rtl);
30810 else
30811 decl = TREE_OPERAND (init, 0);
30812 ref = lookup_decl_die (decl);
30813 if (ref == NULL
30814 || (!get_AT (ref, DW_AT_location)
30815 && !get_AT (ref, DW_AT_const_value)))
30816 return;
30817 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30818 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30819 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30820 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30821 add_AT_loc (die, DW_AT_location, l);
30825 /* Return NULL if l is a DWARF expression, or first op that is not
30826 valid DWARF expression. */
30828 static dw_loc_descr_ref
30829 non_dwarf_expression (dw_loc_descr_ref l)
30831 while (l)
30833 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30834 return l;
30835 switch (l->dw_loc_opc)
30837 case DW_OP_regx:
30838 case DW_OP_implicit_value:
30839 case DW_OP_stack_value:
30840 case DW_OP_implicit_pointer:
30841 case DW_OP_GNU_implicit_pointer:
30842 case DW_OP_GNU_parameter_ref:
30843 case DW_OP_piece:
30844 case DW_OP_bit_piece:
30845 return l;
30846 default:
30847 break;
30849 l = l->dw_loc_next;
30851 return NULL;
30854 /* Return adjusted copy of EXPR:
30855 If it is empty DWARF expression, return it.
30856 If it is valid non-empty DWARF expression,
30857 return copy of EXPR with DW_OP_deref appended to it.
30858 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30859 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30860 If it is DWARF expression followed by DW_OP_stack_value, return
30861 copy of the DWARF expression without anything appended.
30862 Otherwise, return NULL. */
30864 static dw_loc_descr_ref
30865 copy_deref_exprloc (dw_loc_descr_ref expr)
30867 dw_loc_descr_ref tail = NULL;
30869 if (expr == NULL)
30870 return NULL;
30872 dw_loc_descr_ref l = non_dwarf_expression (expr);
30873 if (l && l->dw_loc_next)
30874 return NULL;
30876 if (l)
30878 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30879 tail = new_loc_descr ((enum dwarf_location_atom)
30880 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30881 0, 0);
30882 else
30883 switch (l->dw_loc_opc)
30885 case DW_OP_regx:
30886 tail = new_loc_descr (DW_OP_bregx,
30887 l->dw_loc_oprnd1.v.val_unsigned, 0);
30888 break;
30889 case DW_OP_stack_value:
30890 break;
30891 default:
30892 return NULL;
30895 else
30896 tail = new_loc_descr (DW_OP_deref, 0, 0);
30898 dw_loc_descr_ref ret = NULL, *p = &ret;
30899 while (expr != l)
30901 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30902 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30903 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30904 p = &(*p)->dw_loc_next;
30905 expr = expr->dw_loc_next;
30907 *p = tail;
30908 return ret;
30911 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30912 reference to a variable or argument, adjust it if needed and return:
30913 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30914 attribute if present should be removed
30915 0 keep the attribute perhaps with minor modifications, no need to rescan
30916 1 if the attribute has been successfully adjusted. */
30918 static int
30919 optimize_string_length (dw_attr_node *a)
30921 dw_loc_descr_ref l = AT_loc (a), lv;
30922 dw_die_ref die;
30923 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30925 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30926 die = lookup_decl_die (decl);
30927 if (die)
30929 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30930 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30931 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30933 else
30934 return -1;
30936 else
30937 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30939 /* DWARF5 allows reference class, so we can then reference the DIE.
30940 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30941 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30943 a->dw_attr_val.val_class = dw_val_class_die_ref;
30944 a->dw_attr_val.val_entry = NULL;
30945 a->dw_attr_val.v.val_die_ref.die = die;
30946 a->dw_attr_val.v.val_die_ref.external = 0;
30947 return 0;
30950 dw_attr_node *av = get_AT (die, DW_AT_location);
30951 dw_loc_list_ref d;
30952 bool non_dwarf_expr = false;
30954 if (av == NULL)
30955 return dwarf_strict ? -1 : 0;
30956 switch (AT_class (av))
30958 case dw_val_class_loc_list:
30959 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30960 if (d->expr && non_dwarf_expression (d->expr))
30961 non_dwarf_expr = true;
30962 break;
30963 case dw_val_class_view_list:
30964 gcc_unreachable ();
30965 case dw_val_class_loc:
30966 lv = AT_loc (av);
30967 if (lv == NULL)
30968 return dwarf_strict ? -1 : 0;
30969 if (non_dwarf_expression (lv))
30970 non_dwarf_expr = true;
30971 break;
30972 default:
30973 return dwarf_strict ? -1 : 0;
30976 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30977 into DW_OP_call4 or DW_OP_GNU_variable_value into
30978 DW_OP_call4 DW_OP_deref, do so. */
30979 if (!non_dwarf_expr
30980 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30982 l->dw_loc_opc = DW_OP_call4;
30983 if (l->dw_loc_next)
30984 l->dw_loc_next = NULL;
30985 else
30986 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30987 return 0;
30990 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30991 copy over the DW_AT_location attribute from die to a. */
30992 if (l->dw_loc_next != NULL)
30994 a->dw_attr_val = av->dw_attr_val;
30995 return 1;
30998 dw_loc_list_ref list, *p;
30999 switch (AT_class (av))
31001 case dw_val_class_loc_list:
31002 p = &list;
31003 list = NULL;
31004 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31006 lv = copy_deref_exprloc (d->expr);
31007 if (lv)
31009 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31010 p = &(*p)->dw_loc_next;
31012 else if (!dwarf_strict && d->expr)
31013 return 0;
31015 if (list == NULL)
31016 return dwarf_strict ? -1 : 0;
31017 a->dw_attr_val.val_class = dw_val_class_loc_list;
31018 gen_llsym (list);
31019 *AT_loc_list_ptr (a) = list;
31020 return 1;
31021 case dw_val_class_loc:
31022 lv = copy_deref_exprloc (AT_loc (av));
31023 if (lv == NULL)
31024 return dwarf_strict ? -1 : 0;
31025 a->dw_attr_val.v.val_loc = lv;
31026 return 1;
31027 default:
31028 gcc_unreachable ();
31032 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31033 an address in .rodata section if the string literal is emitted there,
31034 or remove the containing location list or replace DW_AT_const_value
31035 with DW_AT_location and empty location expression, if it isn't found
31036 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31037 to something that has been emitted in the current CU. */
31039 static void
31040 resolve_addr (dw_die_ref die)
31042 dw_die_ref c;
31043 dw_attr_node *a;
31044 dw_loc_list_ref *curr, *start, loc;
31045 unsigned ix;
31046 bool remove_AT_byte_size = false;
31048 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31049 switch (AT_class (a))
31051 case dw_val_class_loc_list:
31052 start = curr = AT_loc_list_ptr (a);
31053 loc = *curr;
31054 gcc_assert (loc);
31055 /* The same list can be referenced more than once. See if we have
31056 already recorded the result from a previous pass. */
31057 if (loc->replaced)
31058 *curr = loc->dw_loc_next;
31059 else if (!loc->resolved_addr)
31061 /* As things stand, we do not expect or allow one die to
31062 reference a suffix of another die's location list chain.
31063 References must be identical or completely separate.
31064 There is therefore no need to cache the result of this
31065 pass on any list other than the first; doing so
31066 would lead to unnecessary writes. */
31067 while (*curr)
31069 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31070 if (!resolve_addr_in_expr (a, (*curr)->expr))
31072 dw_loc_list_ref next = (*curr)->dw_loc_next;
31073 dw_loc_descr_ref l = (*curr)->expr;
31075 if (next && (*curr)->ll_symbol)
31077 gcc_assert (!next->ll_symbol);
31078 next->ll_symbol = (*curr)->ll_symbol;
31079 next->vl_symbol = (*curr)->vl_symbol;
31081 if (dwarf_split_debug_info)
31082 remove_loc_list_addr_table_entries (l);
31083 *curr = next;
31085 else
31087 mark_base_types ((*curr)->expr);
31088 curr = &(*curr)->dw_loc_next;
31091 if (loc == *start)
31092 loc->resolved_addr = 1;
31093 else
31095 loc->replaced = 1;
31096 loc->dw_loc_next = *start;
31099 if (!*start)
31101 remove_AT (die, a->dw_attr);
31102 ix--;
31104 break;
31105 case dw_val_class_view_list:
31107 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31108 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31109 dw_val_node *llnode
31110 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31111 /* If we no longer have a loclist, or it no longer needs
31112 views, drop this attribute. */
31113 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31115 remove_AT (die, a->dw_attr);
31116 ix--;
31118 break;
31120 case dw_val_class_loc:
31122 dw_loc_descr_ref l = AT_loc (a);
31123 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31124 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31125 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31126 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31127 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31128 with DW_FORM_ref referencing the same DIE as
31129 DW_OP_GNU_variable_value used to reference. */
31130 if (a->dw_attr == DW_AT_string_length
31131 && l
31132 && l->dw_loc_opc == DW_OP_GNU_variable_value
31133 && (l->dw_loc_next == NULL
31134 || (l->dw_loc_next->dw_loc_next == NULL
31135 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31137 switch (optimize_string_length (a))
31139 case -1:
31140 remove_AT (die, a->dw_attr);
31141 ix--;
31142 /* If we drop DW_AT_string_length, we need to drop also
31143 DW_AT_{string_length_,}byte_size. */
31144 remove_AT_byte_size = true;
31145 continue;
31146 default:
31147 break;
31148 case 1:
31149 /* Even if we keep the optimized DW_AT_string_length,
31150 it might have changed AT_class, so process it again. */
31151 ix--;
31152 continue;
31155 /* For -gdwarf-2 don't attempt to optimize
31156 DW_AT_data_member_location containing
31157 DW_OP_plus_uconst - older consumers might
31158 rely on it being that op instead of a more complex,
31159 but shorter, location description. */
31160 if ((dwarf_version > 2
31161 || a->dw_attr != DW_AT_data_member_location
31162 || l == NULL
31163 || l->dw_loc_opc != DW_OP_plus_uconst
31164 || l->dw_loc_next != NULL)
31165 && !resolve_addr_in_expr (a, l))
31167 if (dwarf_split_debug_info)
31168 remove_loc_list_addr_table_entries (l);
31169 if (l != NULL
31170 && l->dw_loc_next == NULL
31171 && l->dw_loc_opc == DW_OP_addr
31172 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31173 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31174 && a->dw_attr == DW_AT_location)
31176 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31177 remove_AT (die, a->dw_attr);
31178 ix--;
31179 optimize_location_into_implicit_ptr (die, decl);
31180 break;
31182 if (a->dw_attr == DW_AT_string_length)
31183 /* If we drop DW_AT_string_length, we need to drop also
31184 DW_AT_{string_length_,}byte_size. */
31185 remove_AT_byte_size = true;
31186 remove_AT (die, a->dw_attr);
31187 ix--;
31189 else
31190 mark_base_types (l);
31192 break;
31193 case dw_val_class_addr:
31194 if (a->dw_attr == DW_AT_const_value
31195 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31197 if (AT_index (a) != NOT_INDEXED)
31198 remove_addr_table_entry (a->dw_attr_val.val_entry);
31199 remove_AT (die, a->dw_attr);
31200 ix--;
31202 if ((die->die_tag == DW_TAG_call_site
31203 && a->dw_attr == DW_AT_call_origin)
31204 || (die->die_tag == DW_TAG_GNU_call_site
31205 && a->dw_attr == DW_AT_abstract_origin))
31207 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31208 dw_die_ref tdie = lookup_decl_die (tdecl);
31209 dw_die_ref cdie;
31210 if (tdie == NULL
31211 && DECL_EXTERNAL (tdecl)
31212 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31213 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31215 dw_die_ref pdie = cdie;
31216 /* Make sure we don't add these DIEs into type units.
31217 We could emit skeleton DIEs for context (namespaces,
31218 outer structs/classes) and a skeleton DIE for the
31219 innermost context with DW_AT_signature pointing to the
31220 type unit. See PR78835. */
31221 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31222 pdie = pdie->die_parent;
31223 if (pdie == NULL)
31225 /* Creating a full DIE for tdecl is overly expensive and
31226 at this point even wrong when in the LTO phase
31227 as it can end up generating new type DIEs we didn't
31228 output and thus optimize_external_refs will crash. */
31229 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31230 add_AT_flag (tdie, DW_AT_external, 1);
31231 add_AT_flag (tdie, DW_AT_declaration, 1);
31232 add_linkage_attr (tdie, tdecl);
31233 add_name_and_src_coords_attributes (tdie, tdecl, true);
31234 equate_decl_number_to_die (tdecl, tdie);
31237 if (tdie)
31239 a->dw_attr_val.val_class = dw_val_class_die_ref;
31240 a->dw_attr_val.v.val_die_ref.die = tdie;
31241 a->dw_attr_val.v.val_die_ref.external = 0;
31243 else
31245 if (AT_index (a) != NOT_INDEXED)
31246 remove_addr_table_entry (a->dw_attr_val.val_entry);
31247 remove_AT (die, a->dw_attr);
31248 ix--;
31251 break;
31252 default:
31253 break;
31256 if (remove_AT_byte_size)
31257 remove_AT (die, dwarf_version >= 5
31258 ? DW_AT_string_length_byte_size
31259 : DW_AT_byte_size);
31261 FOR_EACH_CHILD (die, c, resolve_addr (c));
31264 /* Helper routines for optimize_location_lists.
31265 This pass tries to share identical local lists in .debug_loc
31266 section. */
31268 /* Iteratively hash operands of LOC opcode into HSTATE. */
31270 static void
31271 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31273 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31274 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31276 switch (loc->dw_loc_opc)
31278 case DW_OP_const4u:
31279 case DW_OP_const8u:
31280 if (loc->dtprel)
31281 goto hash_addr;
31282 /* FALLTHRU */
31283 case DW_OP_const1u:
31284 case DW_OP_const1s:
31285 case DW_OP_const2u:
31286 case DW_OP_const2s:
31287 case DW_OP_const4s:
31288 case DW_OP_const8s:
31289 case DW_OP_constu:
31290 case DW_OP_consts:
31291 case DW_OP_pick:
31292 case DW_OP_plus_uconst:
31293 case DW_OP_breg0:
31294 case DW_OP_breg1:
31295 case DW_OP_breg2:
31296 case DW_OP_breg3:
31297 case DW_OP_breg4:
31298 case DW_OP_breg5:
31299 case DW_OP_breg6:
31300 case DW_OP_breg7:
31301 case DW_OP_breg8:
31302 case DW_OP_breg9:
31303 case DW_OP_breg10:
31304 case DW_OP_breg11:
31305 case DW_OP_breg12:
31306 case DW_OP_breg13:
31307 case DW_OP_breg14:
31308 case DW_OP_breg15:
31309 case DW_OP_breg16:
31310 case DW_OP_breg17:
31311 case DW_OP_breg18:
31312 case DW_OP_breg19:
31313 case DW_OP_breg20:
31314 case DW_OP_breg21:
31315 case DW_OP_breg22:
31316 case DW_OP_breg23:
31317 case DW_OP_breg24:
31318 case DW_OP_breg25:
31319 case DW_OP_breg26:
31320 case DW_OP_breg27:
31321 case DW_OP_breg28:
31322 case DW_OP_breg29:
31323 case DW_OP_breg30:
31324 case DW_OP_breg31:
31325 case DW_OP_regx:
31326 case DW_OP_fbreg:
31327 case DW_OP_piece:
31328 case DW_OP_deref_size:
31329 case DW_OP_xderef_size:
31330 hstate.add_object (val1->v.val_int);
31331 break;
31332 case DW_OP_skip:
31333 case DW_OP_bra:
31335 int offset;
31337 gcc_assert (val1->val_class == dw_val_class_loc);
31338 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31339 hstate.add_object (offset);
31341 break;
31342 case DW_OP_implicit_value:
31343 hstate.add_object (val1->v.val_unsigned);
31344 switch (val2->val_class)
31346 case dw_val_class_const:
31347 hstate.add_object (val2->v.val_int);
31348 break;
31349 case dw_val_class_vec:
31351 unsigned int elt_size = val2->v.val_vec.elt_size;
31352 unsigned int len = val2->v.val_vec.length;
31354 hstate.add_int (elt_size);
31355 hstate.add_int (len);
31356 hstate.add (val2->v.val_vec.array, len * elt_size);
31358 break;
31359 case dw_val_class_const_double:
31360 hstate.add_object (val2->v.val_double.low);
31361 hstate.add_object (val2->v.val_double.high);
31362 break;
31363 case dw_val_class_wide_int:
31364 hstate.add (val2->v.val_wide->get_val (),
31365 get_full_len (*val2->v.val_wide)
31366 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31367 break;
31368 case dw_val_class_addr:
31369 inchash::add_rtx (val2->v.val_addr, hstate);
31370 break;
31371 default:
31372 gcc_unreachable ();
31374 break;
31375 case DW_OP_bregx:
31376 case DW_OP_bit_piece:
31377 hstate.add_object (val1->v.val_int);
31378 hstate.add_object (val2->v.val_int);
31379 break;
31380 case DW_OP_addr:
31381 hash_addr:
31382 if (loc->dtprel)
31384 unsigned char dtprel = 0xd1;
31385 hstate.add_object (dtprel);
31387 inchash::add_rtx (val1->v.val_addr, hstate);
31388 break;
31389 case DW_OP_GNU_addr_index:
31390 case DW_OP_addrx:
31391 case DW_OP_GNU_const_index:
31392 case DW_OP_constx:
31394 if (loc->dtprel)
31396 unsigned char dtprel = 0xd1;
31397 hstate.add_object (dtprel);
31399 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31401 break;
31402 case DW_OP_implicit_pointer:
31403 case DW_OP_GNU_implicit_pointer:
31404 hstate.add_int (val2->v.val_int);
31405 break;
31406 case DW_OP_entry_value:
31407 case DW_OP_GNU_entry_value:
31408 hstate.add_object (val1->v.val_loc);
31409 break;
31410 case DW_OP_regval_type:
31411 case DW_OP_deref_type:
31412 case DW_OP_GNU_regval_type:
31413 case DW_OP_GNU_deref_type:
31415 unsigned int byte_size
31416 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31417 unsigned int encoding
31418 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31419 hstate.add_object (val1->v.val_int);
31420 hstate.add_object (byte_size);
31421 hstate.add_object (encoding);
31423 break;
31424 case DW_OP_convert:
31425 case DW_OP_reinterpret:
31426 case DW_OP_GNU_convert:
31427 case DW_OP_GNU_reinterpret:
31428 if (val1->val_class == dw_val_class_unsigned_const)
31430 hstate.add_object (val1->v.val_unsigned);
31431 break;
31433 /* FALLTHRU */
31434 case DW_OP_const_type:
31435 case DW_OP_GNU_const_type:
31437 unsigned int byte_size
31438 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31439 unsigned int encoding
31440 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31441 hstate.add_object (byte_size);
31442 hstate.add_object (encoding);
31443 if (loc->dw_loc_opc != DW_OP_const_type
31444 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31445 break;
31446 hstate.add_object (val2->val_class);
31447 switch (val2->val_class)
31449 case dw_val_class_const:
31450 hstate.add_object (val2->v.val_int);
31451 break;
31452 case dw_val_class_vec:
31454 unsigned int elt_size = val2->v.val_vec.elt_size;
31455 unsigned int len = val2->v.val_vec.length;
31457 hstate.add_object (elt_size);
31458 hstate.add_object (len);
31459 hstate.add (val2->v.val_vec.array, len * elt_size);
31461 break;
31462 case dw_val_class_const_double:
31463 hstate.add_object (val2->v.val_double.low);
31464 hstate.add_object (val2->v.val_double.high);
31465 break;
31466 case dw_val_class_wide_int:
31467 hstate.add (val2->v.val_wide->get_val (),
31468 get_full_len (*val2->v.val_wide)
31469 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31470 break;
31471 default:
31472 gcc_unreachable ();
31475 break;
31477 default:
31478 /* Other codes have no operands. */
31479 break;
31483 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31485 static inline void
31486 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31488 dw_loc_descr_ref l;
31489 bool sizes_computed = false;
31490 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31491 size_of_locs (loc);
31493 for (l = loc; l != NULL; l = l->dw_loc_next)
31495 enum dwarf_location_atom opc = l->dw_loc_opc;
31496 hstate.add_object (opc);
31497 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31499 size_of_locs (loc);
31500 sizes_computed = true;
31502 hash_loc_operands (l, hstate);
31506 /* Compute hash of the whole location list LIST_HEAD. */
31508 static inline void
31509 hash_loc_list (dw_loc_list_ref list_head)
31511 dw_loc_list_ref curr = list_head;
31512 inchash::hash hstate;
31514 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31516 hstate.add (curr->begin, strlen (curr->begin) + 1);
31517 hstate.add (curr->end, strlen (curr->end) + 1);
31518 hstate.add_object (curr->vbegin);
31519 hstate.add_object (curr->vend);
31520 if (curr->section)
31521 hstate.add (curr->section, strlen (curr->section) + 1);
31522 hash_locs (curr->expr, hstate);
31524 list_head->hash = hstate.end ();
31527 /* Return true if X and Y opcodes have the same operands. */
31529 static inline bool
31530 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31532 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31533 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31534 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31535 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31537 switch (x->dw_loc_opc)
31539 case DW_OP_const4u:
31540 case DW_OP_const8u:
31541 if (x->dtprel)
31542 goto hash_addr;
31543 /* FALLTHRU */
31544 case DW_OP_const1u:
31545 case DW_OP_const1s:
31546 case DW_OP_const2u:
31547 case DW_OP_const2s:
31548 case DW_OP_const4s:
31549 case DW_OP_const8s:
31550 case DW_OP_constu:
31551 case DW_OP_consts:
31552 case DW_OP_pick:
31553 case DW_OP_plus_uconst:
31554 case DW_OP_breg0:
31555 case DW_OP_breg1:
31556 case DW_OP_breg2:
31557 case DW_OP_breg3:
31558 case DW_OP_breg4:
31559 case DW_OP_breg5:
31560 case DW_OP_breg6:
31561 case DW_OP_breg7:
31562 case DW_OP_breg8:
31563 case DW_OP_breg9:
31564 case DW_OP_breg10:
31565 case DW_OP_breg11:
31566 case DW_OP_breg12:
31567 case DW_OP_breg13:
31568 case DW_OP_breg14:
31569 case DW_OP_breg15:
31570 case DW_OP_breg16:
31571 case DW_OP_breg17:
31572 case DW_OP_breg18:
31573 case DW_OP_breg19:
31574 case DW_OP_breg20:
31575 case DW_OP_breg21:
31576 case DW_OP_breg22:
31577 case DW_OP_breg23:
31578 case DW_OP_breg24:
31579 case DW_OP_breg25:
31580 case DW_OP_breg26:
31581 case DW_OP_breg27:
31582 case DW_OP_breg28:
31583 case DW_OP_breg29:
31584 case DW_OP_breg30:
31585 case DW_OP_breg31:
31586 case DW_OP_regx:
31587 case DW_OP_fbreg:
31588 case DW_OP_piece:
31589 case DW_OP_deref_size:
31590 case DW_OP_xderef_size:
31591 return valx1->v.val_int == valy1->v.val_int;
31592 case DW_OP_skip:
31593 case DW_OP_bra:
31594 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31595 can cause irrelevant differences in dw_loc_addr. */
31596 gcc_assert (valx1->val_class == dw_val_class_loc
31597 && valy1->val_class == dw_val_class_loc
31598 && (dwarf_split_debug_info
31599 || x->dw_loc_addr == y->dw_loc_addr));
31600 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31601 case DW_OP_implicit_value:
31602 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31603 || valx2->val_class != valy2->val_class)
31604 return false;
31605 switch (valx2->val_class)
31607 case dw_val_class_const:
31608 return valx2->v.val_int == valy2->v.val_int;
31609 case dw_val_class_vec:
31610 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31611 && valx2->v.val_vec.length == valy2->v.val_vec.length
31612 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31613 valx2->v.val_vec.elt_size
31614 * valx2->v.val_vec.length) == 0;
31615 case dw_val_class_const_double:
31616 return valx2->v.val_double.low == valy2->v.val_double.low
31617 && valx2->v.val_double.high == valy2->v.val_double.high;
31618 case dw_val_class_wide_int:
31619 return *valx2->v.val_wide == *valy2->v.val_wide;
31620 case dw_val_class_addr:
31621 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31622 default:
31623 gcc_unreachable ();
31625 case DW_OP_bregx:
31626 case DW_OP_bit_piece:
31627 return valx1->v.val_int == valy1->v.val_int
31628 && valx2->v.val_int == valy2->v.val_int;
31629 case DW_OP_addr:
31630 hash_addr:
31631 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31632 case DW_OP_GNU_addr_index:
31633 case DW_OP_addrx:
31634 case DW_OP_GNU_const_index:
31635 case DW_OP_constx:
31637 rtx ax1 = valx1->val_entry->addr.rtl;
31638 rtx ay1 = valy1->val_entry->addr.rtl;
31639 return rtx_equal_p (ax1, ay1);
31641 case DW_OP_implicit_pointer:
31642 case DW_OP_GNU_implicit_pointer:
31643 return valx1->val_class == dw_val_class_die_ref
31644 && valx1->val_class == valy1->val_class
31645 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31646 && valx2->v.val_int == valy2->v.val_int;
31647 case DW_OP_entry_value:
31648 case DW_OP_GNU_entry_value:
31649 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31650 case DW_OP_const_type:
31651 case DW_OP_GNU_const_type:
31652 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31653 || valx2->val_class != valy2->val_class)
31654 return false;
31655 switch (valx2->val_class)
31657 case dw_val_class_const:
31658 return valx2->v.val_int == valy2->v.val_int;
31659 case dw_val_class_vec:
31660 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31661 && valx2->v.val_vec.length == valy2->v.val_vec.length
31662 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31663 valx2->v.val_vec.elt_size
31664 * valx2->v.val_vec.length) == 0;
31665 case dw_val_class_const_double:
31666 return valx2->v.val_double.low == valy2->v.val_double.low
31667 && valx2->v.val_double.high == valy2->v.val_double.high;
31668 case dw_val_class_wide_int:
31669 return *valx2->v.val_wide == *valy2->v.val_wide;
31670 default:
31671 gcc_unreachable ();
31673 case DW_OP_regval_type:
31674 case DW_OP_deref_type:
31675 case DW_OP_GNU_regval_type:
31676 case DW_OP_GNU_deref_type:
31677 return valx1->v.val_int == valy1->v.val_int
31678 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31679 case DW_OP_convert:
31680 case DW_OP_reinterpret:
31681 case DW_OP_GNU_convert:
31682 case DW_OP_GNU_reinterpret:
31683 if (valx1->val_class != valy1->val_class)
31684 return false;
31685 if (valx1->val_class == dw_val_class_unsigned_const)
31686 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31687 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31688 case DW_OP_GNU_parameter_ref:
31689 return valx1->val_class == dw_val_class_die_ref
31690 && valx1->val_class == valy1->val_class
31691 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31692 default:
31693 /* Other codes have no operands. */
31694 return true;
31698 /* Return true if DWARF location expressions X and Y are the same. */
31700 static inline bool
31701 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31703 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31704 if (x->dw_loc_opc != y->dw_loc_opc
31705 || x->dtprel != y->dtprel
31706 || !compare_loc_operands (x, y))
31707 break;
31708 return x == NULL && y == NULL;
31711 /* Hashtable helpers. */
31713 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31715 static inline hashval_t hash (const dw_loc_list_struct *);
31716 static inline bool equal (const dw_loc_list_struct *,
31717 const dw_loc_list_struct *);
31720 /* Return precomputed hash of location list X. */
31722 inline hashval_t
31723 loc_list_hasher::hash (const dw_loc_list_struct *x)
31725 return x->hash;
31728 /* Return true if location lists A and B are the same. */
31730 inline bool
31731 loc_list_hasher::equal (const dw_loc_list_struct *a,
31732 const dw_loc_list_struct *b)
31734 if (a == b)
31735 return 1;
31736 if (a->hash != b->hash)
31737 return 0;
31738 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31739 if (strcmp (a->begin, b->begin) != 0
31740 || strcmp (a->end, b->end) != 0
31741 || (a->section == NULL) != (b->section == NULL)
31742 || (a->section && strcmp (a->section, b->section) != 0)
31743 || a->vbegin != b->vbegin || a->vend != b->vend
31744 || !compare_locs (a->expr, b->expr))
31745 break;
31746 return a == NULL && b == NULL;
31749 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31752 /* Recursively optimize location lists referenced from DIE
31753 children and share them whenever possible. */
31755 static void
31756 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31758 dw_die_ref c;
31759 dw_attr_node *a;
31760 unsigned ix;
31761 dw_loc_list_struct **slot;
31762 bool drop_locviews = false;
31763 bool has_locviews = false;
31765 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31766 if (AT_class (a) == dw_val_class_loc_list)
31768 dw_loc_list_ref list = AT_loc_list (a);
31769 /* TODO: perform some optimizations here, before hashing
31770 it and storing into the hash table. */
31771 hash_loc_list (list);
31772 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31773 if (*slot == NULL)
31775 *slot = list;
31776 if (loc_list_has_views (list))
31777 gcc_assert (list->vl_symbol);
31778 else if (list->vl_symbol)
31780 drop_locviews = true;
31781 list->vl_symbol = NULL;
31784 else
31786 if (list->vl_symbol && !(*slot)->vl_symbol)
31787 drop_locviews = true;
31788 a->dw_attr_val.v.val_loc_list = *slot;
31791 else if (AT_class (a) == dw_val_class_view_list)
31793 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31794 has_locviews = true;
31798 if (drop_locviews && has_locviews)
31799 remove_AT (die, DW_AT_GNU_locviews);
31801 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31805 /* Recursively assign each location list a unique index into the debug_addr
31806 section. */
31808 static void
31809 index_location_lists (dw_die_ref die)
31811 dw_die_ref c;
31812 dw_attr_node *a;
31813 unsigned ix;
31815 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31816 if (AT_class (a) == dw_val_class_loc_list)
31818 dw_loc_list_ref list = AT_loc_list (a);
31819 dw_loc_list_ref curr;
31820 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31822 /* Don't index an entry that has already been indexed
31823 or won't be output. Make sure skip_loc_list_entry doesn't
31824 call size_of_locs, because that might cause circular dependency,
31825 index_location_lists requiring address table indexes to be
31826 computed, but adding new indexes through add_addr_table_entry
31827 and address table index computation requiring no new additions
31828 to the hash table. In the rare case of DWARF[234] >= 64KB
31829 location expression, we'll just waste unused address table entry
31830 for it. */
31831 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31832 continue;
31834 curr->begin_entry
31835 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31836 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31837 curr->end_entry
31838 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31842 FOR_EACH_CHILD (die, c, index_location_lists (c));
31845 /* Optimize location lists referenced from DIE
31846 children and share them whenever possible. */
31848 static void
31849 optimize_location_lists (dw_die_ref die)
31851 loc_list_hash_type htab (500);
31852 optimize_location_lists_1 (die, &htab);
31855 /* Traverse the limbo die list, and add parent/child links. The only
31856 dies without parents that should be here are concrete instances of
31857 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31858 For concrete instances, we can get the parent die from the abstract
31859 instance. */
31861 static void
31862 flush_limbo_die_list (void)
31864 limbo_die_node *node;
31866 /* get_context_die calls force_decl_die, which can put new DIEs on the
31867 limbo list in LTO mode when nested functions are put in a different
31868 partition than that of their parent function. */
31869 while ((node = limbo_die_list))
31871 dw_die_ref die = node->die;
31872 limbo_die_list = node->next;
31874 if (die->die_parent == NULL)
31876 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31878 if (origin && origin->die_parent)
31879 add_child_die (origin->die_parent, die);
31880 else if (is_cu_die (die))
31882 else if (seen_error ())
31883 /* It's OK to be confused by errors in the input. */
31884 add_child_die (comp_unit_die (), die);
31885 else
31887 /* In certain situations, the lexical block containing a
31888 nested function can be optimized away, which results
31889 in the nested function die being orphaned. Likewise
31890 with the return type of that nested function. Force
31891 this to be a child of the containing function.
31893 It may happen that even the containing function got fully
31894 inlined and optimized out. In that case we are lost and
31895 assign the empty child. This should not be big issue as
31896 the function is likely unreachable too. */
31897 gcc_assert (node->created_for);
31899 if (DECL_P (node->created_for))
31900 origin = get_context_die (DECL_CONTEXT (node->created_for));
31901 else if (TYPE_P (node->created_for))
31902 origin = scope_die_for (node->created_for, comp_unit_die ());
31903 else
31904 origin = comp_unit_die ();
31906 add_child_die (origin, die);
31912 /* Reset DIEs so we can output them again. */
31914 static void
31915 reset_dies (dw_die_ref die)
31917 dw_die_ref c;
31919 /* Remove stuff we re-generate. */
31920 die->die_mark = 0;
31921 die->die_offset = 0;
31922 die->die_abbrev = 0;
31923 remove_AT (die, DW_AT_sibling);
31925 FOR_EACH_CHILD (die, c, reset_dies (c));
31928 /* reset_indirect_string removed the references coming from DW_AT_name
31929 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31930 .debug_line_str strings again. */
31932 static void
31933 adjust_name_comp_dir (dw_die_ref die)
31935 for (int i = 0; i < 2; i++)
31937 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31938 dw_attr_node *a = get_AT (die, attr_kind);
31939 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31940 continue;
31942 if (!debug_line_str_hash)
31943 debug_line_str_hash
31944 = hash_table<indirect_string_hasher>::create_ggc (10);
31946 struct indirect_string_node *node
31947 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31948 debug_line_str_hash);
31949 set_indirect_string (node);
31950 node->form = DW_FORM_line_strp;
31951 a->dw_attr_val.v.val_str = node;
31955 /* Output stuff that dwarf requires at the end of every file,
31956 and generate the DWARF-2 debugging info. */
31958 static void
31959 dwarf2out_finish (const char *filename)
31961 comdat_type_node *ctnode;
31962 dw_die_ref main_comp_unit_die;
31963 unsigned char checksum[16];
31964 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31966 /* Generate CTF/BTF debug info. */
31967 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
31968 || btf_debuginfo_p ()) && lang_GNU_C ())
31969 ctf_debug_finish (filename);
31971 /* Skip emitting DWARF if not required. */
31972 if (!dwarf_debuginfo_p ())
31973 return;
31975 /* Flush out any latecomers to the limbo party. */
31976 flush_limbo_die_list ();
31978 if (inline_entry_data_table)
31979 gcc_assert (inline_entry_data_table->is_empty ());
31981 if (flag_checking)
31983 verify_die (comp_unit_die ());
31984 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31985 verify_die (node->die);
31988 /* We shouldn't have any symbols with delayed asm names for
31989 DIEs generated after early finish. */
31990 gcc_assert (deferred_asm_name == NULL);
31992 gen_remaining_tmpl_value_param_die_attribute ();
31994 if (flag_generate_lto || flag_generate_offload)
31996 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31998 /* Prune stuff so that dwarf2out_finish runs successfully
31999 for the fat part of the object. */
32000 reset_dies (comp_unit_die ());
32001 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32002 reset_dies (node->die);
32004 hash_table<comdat_type_hasher> comdat_type_table (100);
32005 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32007 comdat_type_node **slot
32008 = comdat_type_table.find_slot (ctnode, INSERT);
32010 /* Don't reset types twice. */
32011 if (*slot != HTAB_EMPTY_ENTRY)
32012 continue;
32014 /* Remove the pointer to the line table. */
32015 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32017 if (debug_info_level >= DINFO_LEVEL_TERSE)
32018 reset_dies (ctnode->root_die);
32020 *slot = ctnode;
32023 /* Reset die CU symbol so we don't output it twice. */
32024 comp_unit_die ()->die_id.die_symbol = NULL;
32026 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32027 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32028 if (have_macinfo)
32029 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32031 /* Remove indirect string decisions. */
32032 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32033 if (debug_line_str_hash)
32035 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32036 debug_line_str_hash = NULL;
32037 if (asm_outputs_debug_line_str ())
32039 adjust_name_comp_dir (comp_unit_die ());
32040 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32041 adjust_name_comp_dir (node->die);
32046 #if ENABLE_ASSERT_CHECKING
32048 dw_die_ref die = comp_unit_die (), c;
32049 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32051 #endif
32052 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32053 resolve_addr (ctnode->root_die);
32054 resolve_addr (comp_unit_die ());
32055 move_marked_base_types ();
32057 if (dump_file)
32059 fprintf (dump_file, "DWARF for %s\n", filename);
32060 print_die (comp_unit_die (), dump_file);
32063 /* Initialize sections and labels used for actual assembler output. */
32064 unsigned generation = init_sections_and_labels (false);
32066 /* Traverse the DIE's and add sibling attributes to those DIE's that
32067 have children. */
32068 add_sibling_attributes (comp_unit_die ());
32069 limbo_die_node *node;
32070 for (node = cu_die_list; node; node = node->next)
32071 add_sibling_attributes (node->die);
32072 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32073 add_sibling_attributes (ctnode->root_die);
32075 /* When splitting DWARF info, we put some attributes in the
32076 skeleton compile_unit DIE that remains in the .o, while
32077 most attributes go in the DWO compile_unit_die. */
32078 if (dwarf_split_debug_info)
32080 limbo_die_node *cu;
32081 main_comp_unit_die = gen_compile_unit_die (NULL);
32082 if (dwarf_version >= 5)
32083 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32084 cu = limbo_die_list;
32085 gcc_assert (cu->die == main_comp_unit_die);
32086 limbo_die_list = limbo_die_list->next;
32087 cu->next = cu_die_list;
32088 cu_die_list = cu;
32090 else
32091 main_comp_unit_die = comp_unit_die ();
32093 /* Output a terminator label for the .text section. */
32094 switch_to_section (text_section);
32095 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32096 if (cold_text_section)
32098 switch_to_section (cold_text_section);
32099 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32102 /* We can only use the low/high_pc attributes if all of the code was
32103 in .text. */
32104 if ((!have_multiple_function_sections
32105 && vec_safe_length (switch_text_ranges) < 2)
32106 || (dwarf_version < 3 && dwarf_strict))
32108 const char *end_label = text_end_label;
32109 if (vec_safe_length (switch_text_ranges) == 1)
32110 end_label = (*switch_text_ranges)[0];
32111 /* Don't add if the CU has no associated code. */
32112 if (switch_text_ranges)
32113 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32114 end_label, true);
32116 else
32118 unsigned fde_idx;
32119 dw_fde_ref fde;
32120 bool range_list_added = false;
32121 if (switch_text_ranges)
32123 const char *prev_loc = text_section_label;
32124 const char *loc;
32125 unsigned idx;
32127 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32128 if (prev_loc)
32130 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32131 loc, &range_list_added, true);
32132 prev_loc = NULL;
32134 else
32135 prev_loc = loc;
32137 if (prev_loc)
32138 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32139 text_end_label, &range_list_added, true);
32142 if (switch_cold_ranges)
32144 const char *prev_loc = cold_text_section_label;
32145 const char *loc;
32146 unsigned idx;
32148 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32149 if (prev_loc)
32151 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32152 loc, &range_list_added, true);
32153 prev_loc = NULL;
32155 else
32156 prev_loc = loc;
32158 if (prev_loc)
32159 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32160 cold_end_label, &range_list_added, true);
32163 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32165 if (fde->ignored_debug)
32166 continue;
32167 if (!fde->in_std_section)
32168 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32169 fde->dw_fde_end, &range_list_added,
32170 true);
32171 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32172 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32173 fde->dw_fde_second_end, &range_list_added,
32174 true);
32177 if (range_list_added)
32179 /* We need to give .debug_loc and .debug_ranges an appropriate
32180 "base address". Use zero so that these addresses become
32181 absolute. Historically, we've emitted the unexpected
32182 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32183 Emit both to give time for other tools to adapt. */
32184 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32185 if (! dwarf_strict && dwarf_version < 4)
32186 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32188 add_ranges (NULL);
32189 have_multiple_function_sections = true;
32193 /* AIX Assembler inserts the length, so adjust the reference to match the
32194 offset expected by debuggers. */
32195 strcpy (dl_section_ref, debug_line_section_label);
32196 if (XCOFF_DEBUGGING_INFO)
32197 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32199 if (debug_info_level >= DINFO_LEVEL_TERSE)
32200 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32201 dl_section_ref);
32203 if (have_macinfo)
32204 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32205 macinfo_section_label);
32207 if (dwarf_split_debug_info)
32209 if (have_location_lists)
32211 /* Since we generate the loclists in the split DWARF .dwo
32212 file itself, we don't need to generate a loclists_base
32213 attribute for the split compile unit DIE. That attribute
32214 (and using relocatable sec_offset FORMs) isn't allowed
32215 for a split compile unit. Only if the .debug_loclists
32216 section was in the main file, would we need to generate a
32217 loclists_base attribute here (for the full or skeleton
32218 unit DIE). */
32220 /* optimize_location_lists calculates the size of the lists,
32221 so index them first, and assign indices to the entries.
32222 Although optimize_location_lists will remove entries from
32223 the table, it only does so for duplicates, and therefore
32224 only reduces ref_counts to 1. */
32225 index_location_lists (comp_unit_die ());
32228 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32229 index_rnglists ();
32231 if (addr_index_table != NULL)
32233 unsigned int index = 0;
32234 addr_index_table
32235 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32236 (&index);
32240 loc_list_idx = 0;
32241 if (have_location_lists)
32243 optimize_location_lists (comp_unit_die ());
32244 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32245 if (dwarf_version >= 5 && dwarf_split_debug_info)
32246 assign_location_list_indexes (comp_unit_die ());
32249 save_macinfo_strings ();
32251 if (dwarf_split_debug_info)
32253 unsigned int index = 0;
32255 /* Add attributes common to skeleton compile_units and
32256 type_units. Because these attributes include strings, it
32257 must be done before freezing the string table. Top-level
32258 skeleton die attrs are added when the skeleton type unit is
32259 created, so ensure it is created by this point. */
32260 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32261 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32264 /* Output all of the compilation units. We put the main one last so that
32265 the offsets are available to output_pubnames. */
32266 for (node = cu_die_list; node; node = node->next)
32267 output_comp_unit (node->die, 0, NULL);
32269 hash_table<comdat_type_hasher> comdat_type_table (100);
32270 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32272 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32274 /* Don't output duplicate types. */
32275 if (*slot != HTAB_EMPTY_ENTRY)
32276 continue;
32278 /* Add a pointer to the line table for the main compilation unit
32279 so that the debugger can make sense of DW_AT_decl_file
32280 attributes. */
32281 if (debug_info_level >= DINFO_LEVEL_TERSE)
32282 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32283 (!dwarf_split_debug_info
32284 ? dl_section_ref
32285 : debug_skeleton_line_section_label));
32287 output_comdat_type_unit (ctnode, false);
32288 *slot = ctnode;
32291 if (dwarf_split_debug_info)
32293 int mark;
32294 struct md5_ctx ctx;
32296 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32297 md5_init_ctx (&ctx);
32298 mark = 0;
32299 die_checksum (comp_unit_die (), &ctx, &mark);
32300 unmark_all_dies (comp_unit_die ());
32301 md5_finish_ctx (&ctx, checksum);
32303 if (dwarf_version < 5)
32305 /* Use the first 8 bytes of the checksum as the dwo_id,
32306 and add it to both comp-unit DIEs. */
32307 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32308 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32311 /* Add the base offset of the ranges table to the skeleton
32312 comp-unit DIE. */
32313 if (!vec_safe_is_empty (ranges_table))
32315 if (dwarf_version < 5)
32316 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32317 ranges_section_label);
32320 output_addr_table ();
32323 /* Output the main compilation unit if non-empty or if .debug_macinfo
32324 or .debug_macro will be emitted. */
32325 output_comp_unit (comp_unit_die (), have_macinfo,
32326 dwarf_split_debug_info ? checksum : NULL);
32328 if (dwarf_split_debug_info && info_section_emitted)
32329 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32331 /* Output the abbreviation table. */
32332 if (vec_safe_length (abbrev_die_table) != 1)
32334 switch_to_section (debug_abbrev_section);
32335 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32336 output_abbrev_section ();
32339 /* Output location list section if necessary. */
32340 if (have_location_lists)
32342 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32343 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32344 /* Output the location lists info. */
32345 switch_to_section (debug_loc_section);
32346 if (dwarf_version >= 5)
32348 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32349 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32350 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32351 dw2_asm_output_data (4, 0xffffffff,
32352 "Initial length escape value indicating "
32353 "64-bit DWARF extension");
32354 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32355 "Length of Location Lists");
32356 ASM_OUTPUT_LABEL (asm_out_file, l1);
32357 output_dwarf_version ();
32358 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32359 dw2_asm_output_data (1, 0, "Segment Size");
32360 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32361 "Offset Entry Count");
32363 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32364 if (dwarf_version >= 5 && dwarf_split_debug_info)
32366 unsigned int save_loc_list_idx = loc_list_idx;
32367 loc_list_idx = 0;
32368 output_loclists_offsets (comp_unit_die ());
32369 gcc_assert (save_loc_list_idx == loc_list_idx);
32371 output_location_lists (comp_unit_die ());
32372 if (dwarf_version >= 5)
32373 ASM_OUTPUT_LABEL (asm_out_file, l2);
32376 output_pubtables ();
32378 /* Output the address range information if a CU (.debug_info section)
32379 was emitted. We output an empty table even if we had no functions
32380 to put in it. This because the consumer has no way to tell the
32381 difference between an empty table that we omitted and failure to
32382 generate a table that would have contained data. */
32383 if (info_section_emitted)
32385 switch_to_section (debug_aranges_section);
32386 output_aranges ();
32389 /* Output ranges section if necessary. */
32390 if (!vec_safe_is_empty (ranges_table))
32392 if (dwarf_version >= 5)
32394 if (dwarf_split_debug_info)
32396 /* We don't know right now whether there are any
32397 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32398 Depending on into which of those two belongs the first
32399 ranges_table entry, emit that section first and that
32400 output_rnglists call will return true if the other kind of
32401 ranges needs to be emitted as well. */
32402 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32403 if (output_rnglists (generation, dwo))
32404 output_rnglists (generation, !dwo);
32406 else
32407 output_rnglists (generation, false);
32409 else
32410 output_ranges ();
32413 /* Have to end the macro section. */
32414 if (have_macinfo)
32416 switch_to_section (debug_macinfo_section);
32417 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32418 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32419 : debug_skeleton_line_section_label, false);
32420 dw2_asm_output_data (1, 0, "End compilation unit");
32423 /* Output the source line correspondence table. We must do this
32424 even if there is no line information. Otherwise, on an empty
32425 translation unit, we will generate a present, but empty,
32426 .debug_info section. IRIX 6.5 `nm' will then complain when
32427 examining the file. This is done late so that any filenames
32428 used by the debug_info section are marked as 'used'. */
32429 switch_to_section (debug_line_section);
32430 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32431 if (! output_asm_line_debug_info ())
32432 output_line_info (false);
32434 if (dwarf_split_debug_info && info_section_emitted)
32436 switch_to_section (debug_skeleton_line_section);
32437 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32438 output_line_info (true);
32441 /* If we emitted any indirect strings, output the string table too. */
32442 if (debug_str_hash || skeleton_debug_str_hash)
32443 output_indirect_strings ();
32444 if (debug_line_str_hash)
32446 switch_to_section (debug_line_str_section);
32447 const enum dwarf_form form = DW_FORM_line_strp;
32448 debug_line_str_hash->traverse<enum dwarf_form,
32449 output_indirect_string> (form);
32452 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32453 symview_upper_bound = 0;
32454 if (zero_view_p)
32455 bitmap_clear (zero_view_p);
32458 /* Returns a hash value for X (which really is a variable_value_struct). */
32460 inline hashval_t
32461 variable_value_hasher::hash (variable_value_struct *x)
32463 return (hashval_t) x->decl_id;
32466 /* Return nonzero if decl_id of variable_value_struct X is the same as
32467 UID of decl Y. */
32469 inline bool
32470 variable_value_hasher::equal (variable_value_struct *x, tree y)
32472 return x->decl_id == DECL_UID (y);
32475 /* Helper function for resolve_variable_value, handle
32476 DW_OP_GNU_variable_value in one location expression.
32477 Return true if exprloc has been changed into loclist. */
32479 static bool
32480 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32482 dw_loc_descr_ref next;
32483 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32485 next = loc->dw_loc_next;
32486 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32487 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32488 continue;
32490 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32491 if (DECL_CONTEXT (decl) != current_function_decl)
32492 continue;
32494 dw_die_ref ref = lookup_decl_die (decl);
32495 if (ref)
32497 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32498 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32499 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32500 continue;
32502 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32503 if (l == NULL)
32504 continue;
32505 if (l->dw_loc_next)
32507 if (AT_class (a) != dw_val_class_loc)
32508 continue;
32509 switch (a->dw_attr)
32511 /* Following attributes allow both exprloc and loclist
32512 classes, so we can change them into a loclist. */
32513 case DW_AT_location:
32514 case DW_AT_string_length:
32515 case DW_AT_return_addr:
32516 case DW_AT_data_member_location:
32517 case DW_AT_frame_base:
32518 case DW_AT_segment:
32519 case DW_AT_static_link:
32520 case DW_AT_use_location:
32521 case DW_AT_vtable_elem_location:
32522 if (prev)
32524 prev->dw_loc_next = NULL;
32525 prepend_loc_descr_to_each (l, AT_loc (a));
32527 if (next)
32528 add_loc_descr_to_each (l, next);
32529 a->dw_attr_val.val_class = dw_val_class_loc_list;
32530 a->dw_attr_val.val_entry = NULL;
32531 a->dw_attr_val.v.val_loc_list = l;
32532 have_location_lists = true;
32533 return true;
32534 /* Following attributes allow both exprloc and reference,
32535 so if the whole expression is DW_OP_GNU_variable_value alone
32536 we could transform it into reference. */
32537 case DW_AT_byte_size:
32538 case DW_AT_bit_size:
32539 case DW_AT_lower_bound:
32540 case DW_AT_upper_bound:
32541 case DW_AT_bit_stride:
32542 case DW_AT_count:
32543 case DW_AT_allocated:
32544 case DW_AT_associated:
32545 case DW_AT_byte_stride:
32546 if (prev == NULL && next == NULL)
32547 break;
32548 /* FALLTHRU */
32549 default:
32550 if (dwarf_strict)
32551 continue;
32552 break;
32554 /* Create DW_TAG_variable that we can refer to. */
32555 gen_decl_die (decl, NULL_TREE, NULL,
32556 lookup_decl_die (current_function_decl));
32557 ref = lookup_decl_die (decl);
32558 if (ref)
32560 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32561 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32562 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32564 continue;
32566 if (prev)
32568 prev->dw_loc_next = l->expr;
32569 add_loc_descr (&prev->dw_loc_next, next);
32570 free_loc_descr (loc, NULL);
32571 next = prev->dw_loc_next;
32573 else
32575 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32576 add_loc_descr (&loc, next);
32577 next = loc;
32579 loc = prev;
32581 return false;
32584 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32586 static void
32587 resolve_variable_value (dw_die_ref die)
32589 dw_attr_node *a;
32590 dw_loc_list_ref loc;
32591 unsigned ix;
32593 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32594 switch (AT_class (a))
32596 case dw_val_class_loc:
32597 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32598 break;
32599 /* FALLTHRU */
32600 case dw_val_class_loc_list:
32601 loc = AT_loc_list (a);
32602 gcc_assert (loc);
32603 for (; loc; loc = loc->dw_loc_next)
32604 resolve_variable_value_in_expr (a, loc->expr);
32605 break;
32606 default:
32607 break;
32611 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32612 temporaries in the current function. */
32614 static void
32615 resolve_variable_values (void)
32617 if (!variable_value_hash || !current_function_decl)
32618 return;
32620 struct variable_value_struct *node
32621 = variable_value_hash->find_with_hash (current_function_decl,
32622 DECL_UID (current_function_decl));
32624 if (node == NULL)
32625 return;
32627 unsigned int i;
32628 dw_die_ref die;
32629 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32630 resolve_variable_value (die);
32633 /* Helper function for note_variable_value, handle one location
32634 expression. */
32636 static void
32637 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32639 for (; loc; loc = loc->dw_loc_next)
32640 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32641 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32643 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32644 dw_die_ref ref = lookup_decl_die (decl);
32645 if (! ref && (flag_generate_lto || flag_generate_offload))
32647 /* ??? This is somewhat a hack because we do not create DIEs
32648 for variables not in BLOCK trees early but when generating
32649 early LTO output we need the dw_val_class_decl_ref to be
32650 fully resolved. For fat LTO objects we'd also like to
32651 undo this after LTO dwarf output. */
32652 gcc_assert (DECL_CONTEXT (decl));
32653 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32654 gcc_assert (ctx != NULL);
32655 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32656 ref = lookup_decl_die (decl);
32657 gcc_assert (ref != NULL);
32659 if (ref)
32661 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32662 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32663 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32664 continue;
32666 if (VAR_P (decl)
32667 && DECL_CONTEXT (decl)
32668 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32669 && lookup_decl_die (DECL_CONTEXT (decl)))
32671 if (!variable_value_hash)
32672 variable_value_hash
32673 = hash_table<variable_value_hasher>::create_ggc (10);
32675 tree fndecl = DECL_CONTEXT (decl);
32676 struct variable_value_struct *node;
32677 struct variable_value_struct **slot
32678 = variable_value_hash->find_slot_with_hash (fndecl,
32679 DECL_UID (fndecl),
32680 INSERT);
32681 if (*slot == NULL)
32683 node = ggc_cleared_alloc<variable_value_struct> ();
32684 node->decl_id = DECL_UID (fndecl);
32685 *slot = node;
32687 else
32688 node = *slot;
32690 vec_safe_push (node->dies, die);
32695 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32696 with dw_val_class_decl_ref operand. */
32698 static void
32699 note_variable_value (dw_die_ref die)
32701 dw_die_ref c;
32702 dw_attr_node *a;
32703 dw_loc_list_ref loc;
32704 unsigned ix;
32706 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32707 switch (AT_class (a))
32709 case dw_val_class_loc_list:
32710 loc = AT_loc_list (a);
32711 gcc_assert (loc);
32712 if (!loc->noted_variable_value)
32714 loc->noted_variable_value = 1;
32715 for (; loc; loc = loc->dw_loc_next)
32716 note_variable_value_in_expr (die, loc->expr);
32718 break;
32719 case dw_val_class_loc:
32720 note_variable_value_in_expr (die, AT_loc (a));
32721 break;
32722 default:
32723 break;
32726 /* Mark children. */
32727 FOR_EACH_CHILD (die, c, note_variable_value (c));
32730 /* Process DWARF dies for CTF generation. */
32732 static void
32733 ctf_debug_do_cu (dw_die_ref die)
32735 dw_die_ref c;
32737 if (!ctf_do_die (die))
32738 return;
32740 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32743 /* Perform any cleanups needed after the early debug generation pass
32744 has run. */
32746 static void
32747 dwarf2out_early_finish (const char *filename)
32749 comdat_type_node *ctnode;
32750 set_early_dwarf s;
32751 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32753 /* PCH might result in DW_AT_producer string being restored from the
32754 header compilation, so always fill it with empty string initially
32755 and overwrite only here. */
32756 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32758 if (dwarf_record_gcc_switches)
32759 producer_string = gen_producer_string (lang_hooks.name,
32760 save_decoded_options,
32761 save_decoded_options_count);
32762 else
32763 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32765 producer->dw_attr_val.v.val_str->refcount--;
32766 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32768 /* Add the name for the main input file now. We delayed this from
32769 dwarf2out_init to avoid complications with PCH. */
32770 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32771 add_comp_dir_attribute (comp_unit_die ());
32773 /* With LTO early dwarf was really finished at compile-time, so make
32774 sure to adjust the phase after annotating the LTRANS CU DIE. */
32775 if (in_lto_p)
32777 early_dwarf_finished = true;
32778 if (dump_file)
32780 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32781 print_die (comp_unit_die (), dump_file);
32783 return;
32786 /* Walk through the list of incomplete types again, trying once more to
32787 emit full debugging info for them. */
32788 retry_incomplete_types ();
32790 gen_scheduled_generic_parms_dies ();
32791 gen_remaining_tmpl_value_param_die_attribute ();
32793 /* The point here is to flush out the limbo list so that it is empty
32794 and we don't need to stream it for LTO. */
32795 flush_limbo_die_list ();
32797 /* Add DW_AT_linkage_name for all deferred DIEs. */
32798 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32800 tree decl = node->created_for;
32801 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32802 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32803 ended up in deferred_asm_name before we knew it was
32804 constant and never written to disk. */
32805 && DECL_ASSEMBLER_NAME (decl))
32807 add_linkage_attr (node->die, decl);
32808 move_linkage_attr (node->die);
32811 deferred_asm_name = NULL;
32813 if (flag_eliminate_unused_debug_types)
32814 prune_unused_types ();
32816 /* Generate separate COMDAT sections for type DIEs. */
32817 if (use_debug_types)
32819 break_out_comdat_types (comp_unit_die ());
32821 /* Each new type_unit DIE was added to the limbo die list when created.
32822 Since these have all been added to comdat_type_list, clear the
32823 limbo die list. */
32824 limbo_die_list = NULL;
32826 /* For each new comdat type unit, copy declarations for incomplete
32827 types to make the new unit self-contained (i.e., no direct
32828 references to the main compile unit). */
32829 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32830 copy_decls_for_unworthy_types (ctnode->root_die);
32831 copy_decls_for_unworthy_types (comp_unit_die ());
32833 /* In the process of copying declarations from one unit to another,
32834 we may have left some declarations behind that are no longer
32835 referenced. Prune them. */
32836 prune_unused_types ();
32839 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32840 with dw_val_class_decl_ref operand. */
32841 note_variable_value (comp_unit_die ());
32842 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32843 note_variable_value (node->die);
32844 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32845 note_variable_value (ctnode->root_die);
32846 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32847 note_variable_value (node->die);
32849 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32850 both the main_cu and all skeleton TUs. Making this call unconditional
32851 would end up either adding a second copy of the AT_pubnames attribute, or
32852 requiring a special case in add_top_level_skeleton_die_attrs. */
32853 if (!dwarf_split_debug_info)
32854 add_AT_pubnames (comp_unit_die ());
32856 /* The early debug phase is now finished. */
32857 early_dwarf_finished = true;
32858 if (dump_file)
32860 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32861 print_die (comp_unit_die (), dump_file);
32864 /* Generate CTF/BTF debug info. */
32865 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32866 || btf_debuginfo_p ()) && lang_GNU_C ())
32868 ctf_debug_init ();
32869 ctf_debug_do_cu (comp_unit_die ());
32870 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32871 ctf_debug_do_cu (node->die);
32872 /* Post process the debug data in the CTF container if necessary. */
32873 ctf_debug_init_postprocess (btf_debuginfo_p ());
32875 ctf_debug_early_finish (filename);
32878 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32879 if ((!flag_generate_lto && !flag_generate_offload)
32880 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32881 copy_lto_debug_sections operation of the simple object support in
32882 libiberty is not implemented for them yet. */
32883 || TARGET_PECOFF || TARGET_COFF)
32884 return;
32886 /* Now as we are going to output for LTO initialize sections and labels
32887 to the LTO variants. We don't need a random-seed postfix as other
32888 LTO sections as linking the LTO debug sections into one in a partial
32889 link is fine. */
32890 init_sections_and_labels (true);
32892 /* The output below is modeled after dwarf2out_finish with all
32893 location related output removed and some LTO specific changes.
32894 Some refactoring might make both smaller and easier to match up. */
32896 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32897 mark_base_types (ctnode->root_die);
32898 mark_base_types (comp_unit_die ());
32899 move_marked_base_types ();
32901 /* Traverse the DIE's and add sibling attributes to those DIE's
32902 that have children. */
32903 add_sibling_attributes (comp_unit_die ());
32904 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32905 add_sibling_attributes (node->die);
32906 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32907 add_sibling_attributes (ctnode->root_die);
32909 /* AIX Assembler inserts the length, so adjust the reference to match the
32910 offset expected by debuggers. */
32911 strcpy (dl_section_ref, debug_line_section_label);
32912 if (XCOFF_DEBUGGING_INFO)
32913 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32915 if (debug_info_level >= DINFO_LEVEL_TERSE)
32916 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32918 if (have_macinfo)
32919 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32920 macinfo_section_label);
32922 save_macinfo_strings ();
32924 if (dwarf_split_debug_info)
32926 unsigned int index = 0;
32927 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32930 /* Output all of the compilation units. We put the main one last so that
32931 the offsets are available to output_pubnames. */
32932 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32933 output_comp_unit (node->die, 0, NULL);
32935 hash_table<comdat_type_hasher> comdat_type_table (100);
32936 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32938 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32940 /* Don't output duplicate types. */
32941 if (*slot != HTAB_EMPTY_ENTRY)
32942 continue;
32944 /* Add a pointer to the line table for the main compilation unit
32945 so that the debugger can make sense of DW_AT_decl_file
32946 attributes. */
32947 if (debug_info_level >= DINFO_LEVEL_TERSE)
32948 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32949 (!dwarf_split_debug_info
32950 ? debug_line_section_label
32951 : debug_skeleton_line_section_label));
32953 output_comdat_type_unit (ctnode, true);
32954 *slot = ctnode;
32957 /* Stick a unique symbol to the main debuginfo section. */
32958 compute_comp_unit_symbol (comp_unit_die ());
32960 /* Output the main compilation unit. We always need it if only for
32961 the CU symbol. */
32962 output_comp_unit (comp_unit_die (), true, NULL);
32964 /* Output the abbreviation table. */
32965 if (vec_safe_length (abbrev_die_table) != 1)
32967 switch_to_section (debug_abbrev_section);
32968 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32969 output_abbrev_section ();
32972 /* Have to end the macro section. */
32973 if (have_macinfo)
32975 /* We have to save macinfo state if we need to output it again
32976 for the FAT part of the object. */
32977 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32978 if (flag_fat_lto_objects)
32979 macinfo_table = macinfo_table->copy ();
32981 switch_to_section (debug_macinfo_section);
32982 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32983 output_macinfo (debug_line_section_label, true);
32984 dw2_asm_output_data (1, 0, "End compilation unit");
32986 if (flag_fat_lto_objects)
32988 vec_free (macinfo_table);
32989 macinfo_table = saved_macinfo_table;
32993 /* Emit a skeleton debug_line section. */
32994 switch_to_section (debug_line_section);
32995 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32996 output_line_info (true);
32998 /* If we emitted any indirect strings, output the string table too. */
32999 if (debug_str_hash || skeleton_debug_str_hash)
33000 output_indirect_strings ();
33001 if (debug_line_str_hash)
33003 switch_to_section (debug_line_str_section);
33004 const enum dwarf_form form = DW_FORM_line_strp;
33005 debug_line_str_hash->traverse<enum dwarf_form,
33006 output_indirect_string> (form);
33009 /* Switch back to the text section. */
33010 switch_to_section (text_section);
33013 /* Reset all state within dwarf2out.c so that we can rerun the compiler
33014 within the same process. For use by toplev::finalize. */
33016 void
33017 dwarf2out_c_finalize (void)
33019 last_var_location_insn = NULL;
33020 cached_next_real_insn = NULL;
33021 used_rtx_array = NULL;
33022 incomplete_types = NULL;
33023 debug_info_section = NULL;
33024 debug_skeleton_info_section = NULL;
33025 debug_abbrev_section = NULL;
33026 debug_skeleton_abbrev_section = NULL;
33027 debug_aranges_section = NULL;
33028 debug_addr_section = NULL;
33029 debug_macinfo_section = NULL;
33030 debug_line_section = NULL;
33031 debug_skeleton_line_section = NULL;
33032 debug_loc_section = NULL;
33033 debug_pubnames_section = NULL;
33034 debug_pubtypes_section = NULL;
33035 debug_str_section = NULL;
33036 debug_line_str_section = NULL;
33037 debug_str_dwo_section = NULL;
33038 debug_str_offsets_section = NULL;
33039 debug_ranges_section = NULL;
33040 debug_ranges_dwo_section = NULL;
33041 debug_frame_section = NULL;
33042 fde_vec = NULL;
33043 debug_str_hash = NULL;
33044 debug_line_str_hash = NULL;
33045 skeleton_debug_str_hash = NULL;
33046 dw2_string_counter = 0;
33047 have_multiple_function_sections = false;
33048 in_text_section_p = false;
33049 cold_text_section = NULL;
33050 last_text_label = NULL;
33051 last_cold_label = NULL;
33052 switch_text_ranges = NULL;
33053 switch_cold_ranges = NULL;
33054 current_unit_personality = NULL;
33056 early_dwarf = false;
33057 early_dwarf_finished = false;
33059 next_die_offset = 0;
33060 single_comp_unit_die = NULL;
33061 comdat_type_list = NULL;
33062 limbo_die_list = NULL;
33063 file_table = NULL;
33064 decl_die_table = NULL;
33065 common_block_die_table = NULL;
33066 decl_loc_table = NULL;
33067 call_arg_locations = NULL;
33068 call_arg_loc_last = NULL;
33069 call_site_count = -1;
33070 tail_call_site_count = -1;
33071 cached_dw_loc_list_table = NULL;
33072 abbrev_die_table = NULL;
33073 delete dwarf_proc_stack_usage_map;
33074 dwarf_proc_stack_usage_map = NULL;
33075 line_info_label_num = 0;
33076 cur_line_info_table = NULL;
33077 text_section_line_info = NULL;
33078 cold_text_section_line_info = NULL;
33079 separate_line_info = NULL;
33080 info_section_emitted = false;
33081 pubname_table = NULL;
33082 pubtype_table = NULL;
33083 macinfo_table = NULL;
33084 ranges_table = NULL;
33085 ranges_by_label = NULL;
33086 rnglist_idx = 0;
33087 have_location_lists = false;
33088 loclabel_num = 0;
33089 poc_label_num = 0;
33090 last_emitted_file = NULL;
33091 label_num = 0;
33092 tmpl_value_parm_die_table = NULL;
33093 generic_type_instances = NULL;
33094 frame_pointer_fb_offset = 0;
33095 frame_pointer_fb_offset_valid = false;
33096 base_types.release ();
33097 XDELETEVEC (producer_string);
33098 producer_string = NULL;
33099 output_line_info_generation = 0;
33100 init_sections_and_labels_generation = 0;
33103 #include "gt-dwarf2out.h"