Fix typo in t-dimode
[official-gcc.git] / gcc / dwarf2out.c
blobe1d6a79ecd7be60315f30b87999142fcb77f2d90
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 ();
13363 return 0;
13366 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
13367 node, return the size in bits for the type if it is a constant, or else
13368 return the alignment for the type if the type's size is not constant, or
13369 else return BITS_PER_WORD if the type actually turns out to be an
13370 ERROR_MARK node. */
13372 static inline unsigned HOST_WIDE_INT
13373 simple_type_size_in_bits (const_tree type)
13375 if (TREE_CODE (type) == ERROR_MARK)
13376 return BITS_PER_WORD;
13377 else if (TYPE_SIZE (type) == NULL_TREE)
13378 return 0;
13379 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
13380 return tree_to_uhwi (TYPE_SIZE (type));
13381 else
13382 return TYPE_ALIGN (type);
13385 /* Similarly, but return an offset_int instead of UHWI. */
13387 static inline offset_int
13388 offset_int_type_size_in_bits (const_tree type)
13390 if (TREE_CODE (type) == ERROR_MARK)
13391 return BITS_PER_WORD;
13392 else if (TYPE_SIZE (type) == NULL_TREE)
13393 return 0;
13394 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
13395 return wi::to_offset (TYPE_SIZE (type));
13396 else
13397 return TYPE_ALIGN (type);
13400 /* Given a pointer to a tree node for a subrange type, return a pointer
13401 to a DIE that describes the given type. */
13403 static dw_die_ref
13404 subrange_type_die (tree type, tree low, tree high, tree bias,
13405 dw_die_ref context_die)
13407 dw_die_ref subrange_die;
13408 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13410 if (context_die == NULL)
13411 context_die = comp_unit_die ();
13413 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13415 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13417 /* The size of the subrange type and its base type do not match,
13418 so we need to generate a size attribute for the subrange type. */
13419 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13422 add_alignment_attribute (subrange_die, type);
13424 if (low)
13425 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
13426 if (high)
13427 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
13428 if (bias && !dwarf_strict)
13429 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
13430 dw_scalar_form_constant
13431 | dw_scalar_form_exprloc
13432 | dw_scalar_form_reference,
13433 NULL);
13435 return subrange_die;
13438 /* Returns the (const and/or volatile) cv_qualifiers associated with
13439 the decl node. This will normally be augmented with the
13440 cv_qualifiers of the underlying type in add_type_attribute. */
13442 static int
13443 decl_quals (const_tree decl)
13445 return ((TREE_READONLY (decl)
13446 /* The C++ front-end correctly marks reference-typed
13447 variables as readonly, but from a language (and debug
13448 info) standpoint they are not const-qualified. */
13449 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13450 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13451 | (TREE_THIS_VOLATILE (decl)
13452 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13455 /* Determine the TYPE whose qualifiers match the largest strict subset
13456 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13457 qualifiers outside QUAL_MASK. */
13459 static int
13460 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13462 tree t;
13463 int best_rank = 0, best_qual = 0, max_rank;
13465 type_quals &= qual_mask;
13466 max_rank = popcount_hwi (type_quals) - 1;
13468 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13469 t = TYPE_NEXT_VARIANT (t))
13471 int q = TYPE_QUALS (t) & qual_mask;
13473 if ((q & type_quals) == q && q != type_quals
13474 && check_base_type (t, type))
13476 int rank = popcount_hwi (q);
13478 if (rank > best_rank)
13480 best_rank = rank;
13481 best_qual = q;
13486 return best_qual;
13489 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13490 static const dwarf_qual_info_t dwarf_qual_info[] =
13492 { TYPE_QUAL_CONST, DW_TAG_const_type },
13493 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13494 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13495 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13497 static const unsigned int dwarf_qual_info_size
13498 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13500 /* If DIE is a qualified DIE of some base DIE with the same parent,
13501 return the base DIE, otherwise return NULL. Set MASK to the
13502 qualifiers added compared to the returned DIE. */
13504 static dw_die_ref
13505 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13507 unsigned int i;
13508 for (i = 0; i < dwarf_qual_info_size; i++)
13509 if (die->die_tag == dwarf_qual_info[i].t)
13510 break;
13511 if (i == dwarf_qual_info_size)
13512 return NULL;
13513 if (vec_safe_length (die->die_attr) != 1)
13514 return NULL;
13515 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13516 if (type == NULL || type->die_parent != die->die_parent)
13517 return NULL;
13518 *mask |= dwarf_qual_info[i].q;
13519 if (depth)
13521 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13522 if (ret)
13523 return ret;
13525 return type;
13528 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13529 entry that chains the modifiers specified by CV_QUALS in front of the
13530 given type. REVERSE is true if the type is to be interpreted in the
13531 reverse storage order wrt the target order. */
13533 static dw_die_ref
13534 modified_type_die (tree type, int cv_quals, bool reverse,
13535 dw_die_ref context_die)
13537 enum tree_code code = TREE_CODE (type);
13538 dw_die_ref mod_type_die;
13539 dw_die_ref sub_die = NULL;
13540 tree item_type = NULL;
13541 tree qualified_type;
13542 tree name, low, high;
13543 dw_die_ref mod_scope;
13544 struct array_descr_info info;
13545 /* Only these cv-qualifiers are currently handled. */
13546 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13547 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13548 ENCODE_QUAL_ADDR_SPACE(~0U));
13549 const bool reverse_base_type
13550 = need_endianity_attribute_p (reverse) && is_base_type (type);
13552 if (code == ERROR_MARK)
13553 return NULL;
13555 if (lang_hooks.types.get_debug_type)
13557 tree debug_type = lang_hooks.types.get_debug_type (type);
13559 if (debug_type != NULL_TREE && debug_type != type)
13560 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13563 cv_quals &= cv_qual_mask;
13565 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13566 tag modifier (and not an attribute) old consumers won't be able
13567 to handle it. */
13568 if (dwarf_version < 3)
13569 cv_quals &= ~TYPE_QUAL_RESTRICT;
13571 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13572 if (dwarf_version < 5)
13573 cv_quals &= ~TYPE_QUAL_ATOMIC;
13575 /* See if we already have the appropriately qualified variant of
13576 this type. */
13577 qualified_type = get_qualified_type (type, cv_quals);
13579 if (qualified_type == sizetype)
13581 /* Try not to expose the internal sizetype type's name. */
13582 if (TYPE_NAME (qualified_type)
13583 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13585 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13587 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13588 && (TYPE_PRECISION (t)
13589 == TYPE_PRECISION (qualified_type))
13590 && (TYPE_UNSIGNED (t)
13591 == TYPE_UNSIGNED (qualified_type)));
13592 qualified_type = t;
13594 else if (qualified_type == sizetype
13595 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13596 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13597 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13598 qualified_type = size_type_node;
13599 if (type == sizetype)
13600 type = qualified_type;
13603 /* If we do, then we can just use its DIE, if it exists. */
13604 if (qualified_type)
13606 mod_type_die = lookup_type_die (qualified_type);
13608 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13609 dealt with specially: the DIE with the attribute, if it exists, is
13610 placed immediately after the regular DIE for the same base type. */
13611 if (mod_type_die
13612 && (!reverse_base_type
13613 || ((mod_type_die = mod_type_die->die_sib) != NULL
13614 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13615 return mod_type_die;
13618 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13620 /* Handle C typedef types. */
13621 if (name
13622 && TREE_CODE (name) == TYPE_DECL
13623 && DECL_ORIGINAL_TYPE (name)
13624 && !DECL_ARTIFICIAL (name))
13626 tree dtype = TREE_TYPE (name);
13628 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13629 if (qualified_type == dtype && !reverse_base_type)
13631 tree origin = decl_ultimate_origin (name);
13633 /* Typedef variants that have an abstract origin don't get their own
13634 type DIE (see gen_typedef_die), so fall back on the ultimate
13635 abstract origin instead. */
13636 if (origin != NULL && origin != name)
13637 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13638 context_die);
13640 /* For a named type, use the typedef. */
13641 gen_type_die (qualified_type, context_die);
13642 return lookup_type_die (qualified_type);
13644 else
13646 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13647 dquals &= cv_qual_mask;
13648 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13649 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13650 /* cv-unqualified version of named type. Just use
13651 the unnamed type to which it refers. */
13652 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13653 reverse, context_die);
13654 /* Else cv-qualified version of named type; fall through. */
13658 mod_scope = scope_die_for (type, context_die);
13660 if (cv_quals)
13662 int sub_quals = 0, first_quals = 0;
13663 unsigned i;
13664 dw_die_ref first = NULL, last = NULL;
13666 /* Determine a lesser qualified type that most closely matches
13667 this one. Then generate DW_TAG_* entries for the remaining
13668 qualifiers. */
13669 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13670 cv_qual_mask);
13671 if (sub_quals && use_debug_types)
13673 bool needed = false;
13674 /* If emitting type units, make sure the order of qualifiers
13675 is canonical. Thus, start from unqualified type if
13676 an earlier qualifier is missing in sub_quals, but some later
13677 one is present there. */
13678 for (i = 0; i < dwarf_qual_info_size; i++)
13679 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13680 needed = true;
13681 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13683 sub_quals = 0;
13684 break;
13687 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13688 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13690 /* As not all intermediate qualified DIEs have corresponding
13691 tree types, ensure that qualified DIEs in the same scope
13692 as their DW_AT_type are emitted after their DW_AT_type,
13693 only with other qualified DIEs for the same type possibly
13694 in between them. Determine the range of such qualified
13695 DIEs now (first being the base type, last being corresponding
13696 last qualified DIE for it). */
13697 unsigned int count = 0;
13698 first = qualified_die_p (mod_type_die, &first_quals,
13699 dwarf_qual_info_size);
13700 if (first == NULL)
13701 first = mod_type_die;
13702 gcc_assert ((first_quals & ~sub_quals) == 0);
13703 for (count = 0, last = first;
13704 count < (1U << dwarf_qual_info_size);
13705 count++, last = last->die_sib)
13707 int quals = 0;
13708 if (last == mod_scope->die_child)
13709 break;
13710 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13711 != first)
13712 break;
13716 for (i = 0; i < dwarf_qual_info_size; i++)
13717 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13719 dw_die_ref d;
13720 if (first && first != last)
13722 for (d = first->die_sib; ; d = d->die_sib)
13724 int quals = 0;
13725 qualified_die_p (d, &quals, dwarf_qual_info_size);
13726 if (quals == (first_quals | dwarf_qual_info[i].q))
13727 break;
13728 if (d == last)
13730 d = NULL;
13731 break;
13734 if (d)
13736 mod_type_die = d;
13737 continue;
13740 if (first)
13742 d = new_die_raw (dwarf_qual_info[i].t);
13743 add_child_die_after (mod_scope, d, last);
13744 last = d;
13746 else
13747 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13748 if (mod_type_die)
13749 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13750 mod_type_die = d;
13751 first_quals |= dwarf_qual_info[i].q;
13754 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13756 dwarf_tag tag = DW_TAG_pointer_type;
13757 if (code == REFERENCE_TYPE)
13759 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13760 tag = DW_TAG_rvalue_reference_type;
13761 else
13762 tag = DW_TAG_reference_type;
13764 mod_type_die = new_die (tag, mod_scope, type);
13766 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13767 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13768 add_alignment_attribute (mod_type_die, type);
13769 item_type = TREE_TYPE (type);
13771 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13772 if (!ADDR_SPACE_GENERIC_P (as))
13774 int action = targetm.addr_space.debug (as);
13775 if (action >= 0)
13777 /* Positive values indicate an address_class. */
13778 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13780 else
13782 /* Negative values indicate an (inverted) segment base reg. */
13783 dw_loc_descr_ref d
13784 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13785 add_AT_loc (mod_type_die, DW_AT_segment, d);
13789 else if (code == ARRAY_TYPE
13790 || (lang_hooks.types.get_array_descr_info
13791 && lang_hooks.types.get_array_descr_info (type, &info)))
13793 gen_type_die (type, context_die);
13794 return lookup_type_die (type);
13796 else if (code == INTEGER_TYPE
13797 && TREE_TYPE (type) != NULL_TREE
13798 && subrange_type_for_debug_p (type, &low, &high))
13800 tree bias = NULL_TREE;
13801 if (lang_hooks.types.get_type_bias)
13802 bias = lang_hooks.types.get_type_bias (type);
13803 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13804 item_type = TREE_TYPE (type);
13806 else if (is_base_type (type))
13808 mod_type_die = base_type_die (type, reverse);
13810 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13811 if (reverse_base_type)
13813 dw_die_ref after_die
13814 = modified_type_die (type, cv_quals, false, context_die);
13815 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13817 else
13818 add_child_die (comp_unit_die (), mod_type_die);
13820 add_pubtype (type, mod_type_die);
13822 else
13824 gen_type_die (type, context_die);
13826 /* We have to get the type_main_variant here (and pass that to the
13827 `lookup_type_die' routine) because the ..._TYPE node we have
13828 might simply be a *copy* of some original type node (where the
13829 copy was created to help us keep track of typedef names) and
13830 that copy might have a different TYPE_UID from the original
13831 ..._TYPE node. */
13832 if (code == FUNCTION_TYPE || code == METHOD_TYPE)
13834 /* For function/method types, can't just use type_main_variant here,
13835 because that can have different ref-qualifiers for C++,
13836 but try to canonicalize. */
13837 tree main = TYPE_MAIN_VARIANT (type);
13838 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13839 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13840 && check_base_type (t, main)
13841 && check_lang_type (t, type))
13842 return lookup_type_die (t);
13843 return lookup_type_die (type);
13845 /* Vectors have the debugging information in the type,
13846 not the main variant. */
13847 else if (code == VECTOR_TYPE)
13848 return lookup_type_die (type);
13849 else
13850 return lookup_type_die (type_main_variant (type));
13853 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13854 don't output a DW_TAG_typedef, since there isn't one in the
13855 user's program; just attach a DW_AT_name to the type.
13856 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13857 if the base type already has the same name. */
13858 if (name
13859 && ((TREE_CODE (name) != TYPE_DECL
13860 && (qualified_type == TYPE_MAIN_VARIANT (type)
13861 || (cv_quals == TYPE_UNQUALIFIED)))
13862 || (TREE_CODE (name) == TYPE_DECL
13863 && TREE_TYPE (name) == qualified_type
13864 && DECL_NAME (name))))
13866 if (TREE_CODE (name) == TYPE_DECL)
13867 /* Could just call add_name_and_src_coords_attributes here,
13868 but since this is a builtin type it doesn't have any
13869 useful source coordinates anyway. */
13870 name = DECL_NAME (name);
13871 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13873 /* This probably indicates a bug. */
13874 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13876 name = TYPE_IDENTIFIER (type);
13877 add_name_attribute (mod_type_die,
13878 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13881 if (qualified_type && !reverse_base_type)
13882 equate_type_number_to_die (qualified_type, mod_type_die);
13884 if (item_type)
13885 /* We must do this after the equate_type_number_to_die call, in case
13886 this is a recursive type. This ensures that the modified_type_die
13887 recursion will terminate even if the type is recursive. Recursive
13888 types are possible in Ada. */
13889 sub_die = modified_type_die (item_type,
13890 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13891 reverse,
13892 context_die);
13894 if (sub_die != NULL)
13895 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13897 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13898 if (TYPE_ARTIFICIAL (type))
13899 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13901 return mod_type_die;
13904 /* Generate DIEs for the generic parameters of T.
13905 T must be either a generic type or a generic function.
13906 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13908 static void
13909 gen_generic_params_dies (tree t)
13911 tree parms, args;
13912 int parms_num, i;
13913 dw_die_ref die = NULL;
13914 int non_default;
13916 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13917 return;
13919 if (TYPE_P (t))
13920 die = lookup_type_die (t);
13921 else if (DECL_P (t))
13922 die = lookup_decl_die (t);
13924 gcc_assert (die);
13926 parms = lang_hooks.get_innermost_generic_parms (t);
13927 if (!parms)
13928 /* T has no generic parameter. It means T is neither a generic type
13929 or function. End of story. */
13930 return;
13932 parms_num = TREE_VEC_LENGTH (parms);
13933 args = lang_hooks.get_innermost_generic_args (t);
13934 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13935 non_default = int_cst_value (TREE_CHAIN (args));
13936 else
13937 non_default = TREE_VEC_LENGTH (args);
13938 for (i = 0; i < parms_num; i++)
13940 tree parm, arg, arg_pack_elems;
13941 dw_die_ref parm_die;
13943 parm = TREE_VEC_ELT (parms, i);
13944 arg = TREE_VEC_ELT (args, i);
13945 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13946 gcc_assert (parm && TREE_VALUE (parm) && arg);
13948 if (parm && TREE_VALUE (parm) && arg)
13950 /* If PARM represents a template parameter pack,
13951 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13952 by DW_TAG_template_*_parameter DIEs for the argument
13953 pack elements of ARG. Note that ARG would then be
13954 an argument pack. */
13955 if (arg_pack_elems)
13956 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13957 arg_pack_elems,
13958 die);
13959 else
13960 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13961 true /* emit name */, die);
13962 if (i >= non_default)
13963 add_AT_flag (parm_die, DW_AT_default_value, 1);
13968 /* Create and return a DIE for PARM which should be
13969 the representation of a generic type parameter.
13970 For instance, in the C++ front end, PARM would be a template parameter.
13971 ARG is the argument to PARM.
13972 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13973 name of the PARM.
13974 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13975 as a child node. */
13977 static dw_die_ref
13978 generic_parameter_die (tree parm, tree arg,
13979 bool emit_name_p,
13980 dw_die_ref parent_die)
13982 dw_die_ref tmpl_die = NULL;
13983 const char *name = NULL;
13985 /* C++20 accepts class literals as template parameters, and var
13986 decls with initializers represent them. The VAR_DECLs would be
13987 rejected, but we can take the DECL_INITIAL constructor and
13988 attempt to expand it. */
13989 if (arg && VAR_P (arg))
13990 arg = DECL_INITIAL (arg);
13992 if (!parm || !DECL_NAME (parm) || !arg)
13993 return NULL;
13995 /* We support non-type generic parameters and arguments,
13996 type generic parameters and arguments, as well as
13997 generic generic parameters (a.k.a. template template parameters in C++)
13998 and arguments. */
13999 if (TREE_CODE (parm) == PARM_DECL)
14000 /* PARM is a nontype generic parameter */
14001 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
14002 else if (TREE_CODE (parm) == TYPE_DECL)
14003 /* PARM is a type generic parameter. */
14004 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
14005 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14006 /* PARM is a generic generic parameter.
14007 Its DIE is a GNU extension. It shall have a
14008 DW_AT_name attribute to represent the name of the template template
14009 parameter, and a DW_AT_GNU_template_name attribute to represent the
14010 name of the template template argument. */
14011 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
14012 parent_die, parm);
14013 else
14014 gcc_unreachable ();
14016 if (tmpl_die)
14018 tree tmpl_type;
14020 /* If PARM is a generic parameter pack, it means we are
14021 emitting debug info for a template argument pack element.
14022 In other terms, ARG is a template argument pack element.
14023 In that case, we don't emit any DW_AT_name attribute for
14024 the die. */
14025 if (emit_name_p)
14027 name = IDENTIFIER_POINTER (DECL_NAME (parm));
14028 gcc_assert (name);
14029 add_AT_string (tmpl_die, DW_AT_name, name);
14032 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
14034 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
14035 TMPL_DIE should have a child DW_AT_type attribute that is set
14036 to the type of the argument to PARM, which is ARG.
14037 If PARM is a type generic parameter, TMPL_DIE should have a
14038 child DW_AT_type that is set to ARG. */
14039 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
14040 add_type_attribute (tmpl_die, tmpl_type,
14041 (TREE_THIS_VOLATILE (tmpl_type)
14042 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
14043 false, parent_die);
14045 else
14047 /* So TMPL_DIE is a DIE representing a
14048 a generic generic template parameter, a.k.a template template
14049 parameter in C++ and arg is a template. */
14051 /* The DW_AT_GNU_template_name attribute of the DIE must be set
14052 to the name of the argument. */
14053 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
14054 if (name)
14055 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
14058 if (TREE_CODE (parm) == PARM_DECL)
14059 /* So PARM is a non-type generic parameter.
14060 DWARF3 5.6.8 says we must set a DW_AT_const_value child
14061 attribute of TMPL_DIE which value represents the value
14062 of ARG.
14063 We must be careful here:
14064 The value of ARG might reference some function decls.
14065 We might currently be emitting debug info for a generic
14066 type and types are emitted before function decls, we don't
14067 know if the function decls referenced by ARG will actually be
14068 emitted after cgraph computations.
14069 So must defer the generation of the DW_AT_const_value to
14070 after cgraph is ready. */
14071 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
14074 return tmpl_die;
14077 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
14078 PARM_PACK must be a template parameter pack. The returned DIE
14079 will be child DIE of PARENT_DIE. */
14081 static dw_die_ref
14082 template_parameter_pack_die (tree parm_pack,
14083 tree parm_pack_args,
14084 dw_die_ref parent_die)
14086 dw_die_ref die;
14087 int j;
14089 gcc_assert (parent_die && parm_pack);
14091 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
14092 add_name_and_src_coords_attributes (die, parm_pack);
14093 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
14094 generic_parameter_die (parm_pack,
14095 TREE_VEC_ELT (parm_pack_args, j),
14096 false /* Don't emit DW_AT_name */,
14097 die);
14098 return die;
14101 /* Return the DBX register number described by a given RTL node. */
14103 static unsigned int
14104 dbx_reg_number (const_rtx rtl)
14106 unsigned regno = REGNO (rtl);
14108 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
14110 #ifdef LEAF_REG_REMAP
14111 if (crtl->uses_only_leaf_regs)
14113 int leaf_reg = LEAF_REG_REMAP (regno);
14114 if (leaf_reg != -1)
14115 regno = (unsigned) leaf_reg;
14117 #endif
14119 regno = DBX_REGISTER_NUMBER (regno);
14120 gcc_assert (regno != INVALID_REGNUM);
14121 return regno;
14124 /* Optionally add a DW_OP_piece term to a location description expression.
14125 DW_OP_piece is only added if the location description expression already
14126 doesn't end with DW_OP_piece. */
14128 static void
14129 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
14131 dw_loc_descr_ref loc;
14133 if (*list_head != NULL)
14135 /* Find the end of the chain. */
14136 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
14139 if (loc->dw_loc_opc != DW_OP_piece)
14140 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
14144 /* Return a location descriptor that designates a machine register or
14145 zero if there is none. */
14147 static dw_loc_descr_ref
14148 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
14150 rtx regs;
14152 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
14153 return 0;
14155 /* We only use "frame base" when we're sure we're talking about the
14156 post-prologue local stack frame. We do this by *not* running
14157 register elimination until this point, and recognizing the special
14158 argument pointer and soft frame pointer rtx's.
14159 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
14160 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
14161 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
14163 dw_loc_descr_ref result = NULL;
14165 if (dwarf_version >= 4 || !dwarf_strict)
14167 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
14168 initialized);
14169 if (result)
14170 add_loc_descr (&result,
14171 new_loc_descr (DW_OP_stack_value, 0, 0));
14173 return result;
14176 regs = targetm.dwarf_register_span (rtl);
14178 if (REG_NREGS (rtl) > 1 || regs)
14179 return multiple_reg_loc_descriptor (rtl, regs, initialized);
14180 else
14182 unsigned int dbx_regnum = dbx_reg_number (rtl);
14183 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14184 return 0;
14185 return one_reg_loc_descriptor (dbx_regnum, initialized);
14189 /* Return a location descriptor that designates a machine register for
14190 a given hard register number. */
14192 static dw_loc_descr_ref
14193 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
14195 dw_loc_descr_ref reg_loc_descr;
14197 if (regno <= 31)
14198 reg_loc_descr
14199 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
14200 else
14201 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
14203 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14204 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14206 return reg_loc_descr;
14209 /* Given an RTL of a register, return a location descriptor that
14210 designates a value that spans more than one register. */
14212 static dw_loc_descr_ref
14213 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
14214 enum var_init_status initialized)
14216 int size, i;
14217 dw_loc_descr_ref loc_result = NULL;
14219 /* Simple, contiguous registers. */
14220 if (regs == NULL_RTX)
14222 unsigned reg = REGNO (rtl);
14223 int nregs;
14225 #ifdef LEAF_REG_REMAP
14226 if (crtl->uses_only_leaf_regs)
14228 int leaf_reg = LEAF_REG_REMAP (reg);
14229 if (leaf_reg != -1)
14230 reg = (unsigned) leaf_reg;
14232 #endif
14234 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
14235 nregs = REG_NREGS (rtl);
14237 /* At present we only track constant-sized pieces. */
14238 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
14239 return NULL;
14240 size /= nregs;
14242 loc_result = NULL;
14243 while (nregs--)
14245 dw_loc_descr_ref t;
14247 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
14248 VAR_INIT_STATUS_INITIALIZED);
14249 add_loc_descr (&loc_result, t);
14250 add_loc_descr_op_piece (&loc_result, size);
14251 ++reg;
14253 return loc_result;
14256 /* Now onto stupid register sets in non contiguous locations. */
14258 gcc_assert (GET_CODE (regs) == PARALLEL);
14260 /* At present we only track constant-sized pieces. */
14261 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
14262 return NULL;
14263 loc_result = NULL;
14265 for (i = 0; i < XVECLEN (regs, 0); ++i)
14267 dw_loc_descr_ref t;
14269 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
14270 VAR_INIT_STATUS_INITIALIZED);
14271 add_loc_descr (&loc_result, t);
14272 add_loc_descr_op_piece (&loc_result, size);
14275 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14276 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14277 return loc_result;
14280 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
14282 /* Return a location descriptor that designates a constant i,
14283 as a compound operation from constant (i >> shift), constant shift
14284 and DW_OP_shl. */
14286 static dw_loc_descr_ref
14287 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14289 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
14290 add_loc_descr (&ret, int_loc_descriptor (shift));
14291 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14292 return ret;
14295 /* Return a location descriptor that designates constant POLY_I. */
14297 static dw_loc_descr_ref
14298 int_loc_descriptor (poly_int64 poly_i)
14300 enum dwarf_location_atom op;
14302 HOST_WIDE_INT i;
14303 if (!poly_i.is_constant (&i))
14305 /* Create location descriptions for the non-constant part and
14306 add any constant offset at the end. */
14307 dw_loc_descr_ref ret = NULL;
14308 HOST_WIDE_INT constant = poly_i.coeffs[0];
14309 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
14311 HOST_WIDE_INT coeff = poly_i.coeffs[j];
14312 if (coeff != 0)
14314 dw_loc_descr_ref start = ret;
14315 unsigned int factor;
14316 int bias;
14317 unsigned int regno = targetm.dwarf_poly_indeterminate_value
14318 (j, &factor, &bias);
14320 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
14321 add COEFF * (REGNO / FACTOR) now and subtract
14322 COEFF * BIAS from the final constant part. */
14323 constant -= coeff * bias;
14324 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
14325 if (coeff % factor == 0)
14326 coeff /= factor;
14327 else
14329 int amount = exact_log2 (factor);
14330 gcc_assert (amount >= 0);
14331 add_loc_descr (&ret, int_loc_descriptor (amount));
14332 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14334 if (coeff != 1)
14336 add_loc_descr (&ret, int_loc_descriptor (coeff));
14337 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14339 if (start)
14340 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
14343 loc_descr_plus_const (&ret, constant);
14344 return ret;
14347 /* Pick the smallest representation of a constant, rather than just
14348 defaulting to the LEB encoding. */
14349 if (i >= 0)
14351 int clz = clz_hwi (i);
14352 int ctz = ctz_hwi (i);
14353 if (i <= 31)
14354 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
14355 else if (i <= 0xff)
14356 op = DW_OP_const1u;
14357 else if (i <= 0xffff)
14358 op = DW_OP_const2u;
14359 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14360 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14361 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
14362 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
14363 while DW_OP_const4u is 5 bytes. */
14364 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
14365 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14366 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14367 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
14368 while DW_OP_const4u is 5 bytes. */
14369 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14371 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14372 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14373 <= 4)
14375 /* As i >= 2**31, the double cast above will yield a negative number.
14376 Since wrapping is defined in DWARF expressions we can output big
14377 positive integers as small negative ones, regardless of the size
14378 of host wide ints.
14380 Here, since the evaluator will handle 32-bit values and since i >=
14381 2**31, we know it's going to be interpreted as a negative literal:
14382 store it this way if we can do better than 5 bytes this way. */
14383 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14385 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14386 op = DW_OP_const4u;
14388 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
14389 least 6 bytes: see if we can do better before falling back to it. */
14390 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14391 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14392 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
14393 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
14394 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14395 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
14396 >= HOST_BITS_PER_WIDE_INT)
14397 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
14398 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
14399 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
14400 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14401 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14402 && size_of_uleb128 (i) > 6)
14403 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
14404 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
14405 else
14406 op = DW_OP_constu;
14408 else
14410 if (i >= -0x80)
14411 op = DW_OP_const1s;
14412 else if (i >= -0x8000)
14413 op = DW_OP_const2s;
14414 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14416 if (size_of_int_loc_descriptor (i) < 5)
14418 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14420 return ret;
14422 op = DW_OP_const4s;
14424 else
14426 if (size_of_int_loc_descriptor (i)
14427 < (unsigned long) 1 + size_of_sleb128 (i))
14429 dw_loc_descr_ref ret = int_loc_descriptor (-i);
14430 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14431 return ret;
14433 op = DW_OP_consts;
14437 return new_loc_descr (op, i, 0);
14440 /* Likewise, for unsigned constants. */
14442 static dw_loc_descr_ref
14443 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
14445 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
14446 const unsigned HOST_WIDE_INT max_uint
14447 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
14449 /* If possible, use the clever signed constants handling. */
14450 if (i <= max_int)
14451 return int_loc_descriptor ((HOST_WIDE_INT) i);
14453 /* Here, we are left with positive numbers that cannot be represented as
14454 HOST_WIDE_INT, i.e.:
14455 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
14457 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
14458 whereas may be better to output a negative integer: thanks to integer
14459 wrapping, we know that:
14460 x = x - 2 ** DWARF2_ADDR_SIZE
14461 = x - 2 * (max (HOST_WIDE_INT) + 1)
14462 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14463 small negative integers. Let's try that in cases it will clearly improve
14464 the encoding: there is no gain turning DW_OP_const4u into
14465 DW_OP_const4s. */
14466 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14467 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14468 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14470 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14472 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14473 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14474 const HOST_WIDE_INT second_shift
14475 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14477 /* So we finally have:
14478 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14479 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14480 return int_loc_descriptor (second_shift);
14483 /* Last chance: fallback to a simple constant operation. */
14484 return new_loc_descr
14485 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14486 ? DW_OP_const4u
14487 : DW_OP_const8u,
14488 i, 0);
14491 /* Generate and return a location description that computes the unsigned
14492 comparison of the two stack top entries (a OP b where b is the top-most
14493 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14494 LE_EXPR, GT_EXPR or GE_EXPR. */
14496 static dw_loc_descr_ref
14497 uint_comparison_loc_list (enum tree_code kind)
14499 enum dwarf_location_atom op, flip_op;
14500 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14502 switch (kind)
14504 case LT_EXPR:
14505 op = DW_OP_lt;
14506 break;
14507 case LE_EXPR:
14508 op = DW_OP_le;
14509 break;
14510 case GT_EXPR:
14511 op = DW_OP_gt;
14512 break;
14513 case GE_EXPR:
14514 op = DW_OP_ge;
14515 break;
14516 default:
14517 gcc_unreachable ();
14520 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14521 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14523 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14524 possible to perform unsigned comparisons: we just have to distinguish
14525 three cases:
14527 1. when a and b have the same sign (as signed integers); then we should
14528 return: a OP(signed) b;
14530 2. when a is a negative signed integer while b is a positive one, then a
14531 is a greater unsigned integer than b; likewise when a and b's roles
14532 are flipped.
14534 So first, compare the sign of the two operands. */
14535 ret = new_loc_descr (DW_OP_over, 0, 0);
14536 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14537 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14538 /* If they have different signs (i.e. they have different sign bits), then
14539 the stack top value has now the sign bit set and thus it's smaller than
14540 zero. */
14541 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14542 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14543 add_loc_descr (&ret, bra_node);
14545 /* We are in case 1. At this point, we know both operands have the same
14546 sign, to it's safe to use the built-in signed comparison. */
14547 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14548 add_loc_descr (&ret, jmp_node);
14550 /* We are in case 2. Here, we know both operands do not have the same sign,
14551 so we have to flip the signed comparison. */
14552 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14553 tmp = new_loc_descr (flip_op, 0, 0);
14554 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14555 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14556 add_loc_descr (&ret, tmp);
14558 /* This dummy operation is necessary to make the two branches join. */
14559 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14560 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14561 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14562 add_loc_descr (&ret, tmp);
14564 return ret;
14567 /* Likewise, but takes the location description lists (might be destructive on
14568 them). Return NULL if either is NULL or if concatenation fails. */
14570 static dw_loc_list_ref
14571 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14572 enum tree_code kind)
14574 if (left == NULL || right == NULL)
14575 return NULL;
14577 add_loc_list (&left, right);
14578 if (left == NULL)
14579 return NULL;
14581 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14582 return left;
14585 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14586 without actually allocating it. */
14588 static unsigned long
14589 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14591 return size_of_int_loc_descriptor (i >> shift)
14592 + size_of_int_loc_descriptor (shift)
14593 + 1;
14596 /* Return size_of_locs (int_loc_descriptor (i)) without
14597 actually allocating it. */
14599 static unsigned long
14600 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14602 unsigned long s;
14604 if (i >= 0)
14606 int clz, ctz;
14607 if (i <= 31)
14608 return 1;
14609 else if (i <= 0xff)
14610 return 2;
14611 else if (i <= 0xffff)
14612 return 3;
14613 clz = clz_hwi (i);
14614 ctz = ctz_hwi (i);
14615 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14616 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14617 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14618 - clz - 5);
14619 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14620 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14621 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14622 - clz - 8);
14623 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14624 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14625 <= 4)
14626 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14627 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14628 return 5;
14629 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14630 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14631 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14632 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14633 - clz - 8);
14634 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14635 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14636 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14637 - clz - 16);
14638 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14639 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14640 && s > 6)
14641 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14642 - clz - 32);
14643 else
14644 return 1 + s;
14646 else
14648 if (i >= -0x80)
14649 return 2;
14650 else if (i >= -0x8000)
14651 return 3;
14652 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14654 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14656 s = size_of_int_loc_descriptor (-i) + 1;
14657 if (s < 5)
14658 return s;
14660 return 5;
14662 else
14664 unsigned long r = 1 + size_of_sleb128 (i);
14665 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14667 s = size_of_int_loc_descriptor (-i) + 1;
14668 if (s < r)
14669 return s;
14671 return r;
14676 /* Return loc description representing "address" of integer value.
14677 This can appear only as toplevel expression. */
14679 static dw_loc_descr_ref
14680 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14682 int litsize;
14683 dw_loc_descr_ref loc_result = NULL;
14685 if (!(dwarf_version >= 4 || !dwarf_strict))
14686 return NULL;
14688 litsize = size_of_int_loc_descriptor (i);
14689 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14690 is more compact. For DW_OP_stack_value we need:
14691 litsize + 1 (DW_OP_stack_value)
14692 and for DW_OP_implicit_value:
14693 1 (DW_OP_implicit_value) + 1 (length) + size. */
14694 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14696 loc_result = int_loc_descriptor (i);
14697 add_loc_descr (&loc_result,
14698 new_loc_descr (DW_OP_stack_value, 0, 0));
14699 return loc_result;
14702 loc_result = new_loc_descr (DW_OP_implicit_value,
14703 size, 0);
14704 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14705 loc_result->dw_loc_oprnd2.v.val_int = i;
14706 return loc_result;
14709 /* Return a location descriptor that designates a base+offset location. */
14711 static dw_loc_descr_ref
14712 based_loc_descr (rtx reg, poly_int64 offset,
14713 enum var_init_status initialized)
14715 unsigned int regno;
14716 dw_loc_descr_ref result;
14717 dw_fde_ref fde = cfun->fde;
14719 /* We only use "frame base" when we're sure we're talking about the
14720 post-prologue local stack frame. We do this by *not* running
14721 register elimination until this point, and recognizing the special
14722 argument pointer and soft frame pointer rtx's. */
14723 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14725 rtx elim = (ira_use_lra_p
14726 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14727 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14729 if (elim != reg)
14731 /* Allow hard frame pointer here even if frame pointer
14732 isn't used since hard frame pointer is encoded with
14733 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14734 not hard frame pointer directly. */
14735 elim = strip_offset_and_add (elim, &offset);
14736 gcc_assert (elim == hard_frame_pointer_rtx
14737 || elim == stack_pointer_rtx);
14739 /* If drap register is used to align stack, use frame
14740 pointer + offset to access stack variables. If stack
14741 is aligned without drap, use stack pointer + offset to
14742 access stack variables. */
14743 if (crtl->stack_realign_tried
14744 && reg == frame_pointer_rtx)
14746 int base_reg
14747 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14748 ? HARD_FRAME_POINTER_REGNUM
14749 : REGNO (elim));
14750 return new_reg_loc_descr (base_reg, offset);
14753 gcc_assert (frame_pointer_fb_offset_valid);
14754 offset += frame_pointer_fb_offset;
14755 HOST_WIDE_INT const_offset;
14756 if (offset.is_constant (&const_offset))
14757 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14758 else
14760 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14761 loc_descr_plus_const (&ret, offset);
14762 return ret;
14767 regno = REGNO (reg);
14768 #ifdef LEAF_REG_REMAP
14769 if (crtl->uses_only_leaf_regs)
14771 int leaf_reg = LEAF_REG_REMAP (regno);
14772 if (leaf_reg != -1)
14773 regno = (unsigned) leaf_reg;
14775 #endif
14776 regno = DWARF_FRAME_REGNUM (regno);
14778 HOST_WIDE_INT const_offset;
14779 if (!optimize && fde
14780 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14781 && offset.is_constant (&const_offset))
14783 /* Use cfa+offset to represent the location of arguments passed
14784 on the stack when drap is used to align stack.
14785 Only do this when not optimizing, for optimized code var-tracking
14786 is supposed to track where the arguments live and the register
14787 used as vdrap or drap in some spot might be used for something
14788 else in other part of the routine. */
14789 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14792 result = new_reg_loc_descr (regno, offset);
14794 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14795 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14797 return result;
14800 /* Return true if this RTL expression describes a base+offset calculation. */
14802 static inline int
14803 is_based_loc (const_rtx rtl)
14805 return (GET_CODE (rtl) == PLUS
14806 && ((REG_P (XEXP (rtl, 0))
14807 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14808 && CONST_INT_P (XEXP (rtl, 1)))));
14811 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14812 failed. */
14814 static dw_loc_descr_ref
14815 tls_mem_loc_descriptor (rtx mem)
14817 tree base;
14818 dw_loc_descr_ref loc_result;
14820 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14821 return NULL;
14823 base = get_base_address (MEM_EXPR (mem));
14824 if (base == NULL
14825 || !VAR_P (base)
14826 || !DECL_THREAD_LOCAL_P (base))
14827 return NULL;
14829 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14830 if (loc_result == NULL)
14831 return NULL;
14833 if (maybe_ne (MEM_OFFSET (mem), 0))
14834 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14836 return loc_result;
14839 /* Output debug info about reason why we failed to expand expression as dwarf
14840 expression. */
14842 static void
14843 expansion_failed (tree expr, rtx rtl, char const *reason)
14845 if (dump_file && (dump_flags & TDF_DETAILS))
14847 fprintf (dump_file, "Failed to expand as dwarf: ");
14848 if (expr)
14849 print_generic_expr (dump_file, expr, dump_flags);
14850 if (rtl)
14852 fprintf (dump_file, "\n");
14853 print_rtl (dump_file, rtl);
14855 fprintf (dump_file, "\nReason: %s\n", reason);
14859 /* Helper function for const_ok_for_output. */
14861 static bool
14862 const_ok_for_output_1 (rtx rtl)
14864 if (targetm.const_not_ok_for_debug_p (rtl))
14866 if (GET_CODE (rtl) != UNSPEC)
14868 expansion_failed (NULL_TREE, rtl,
14869 "Expression rejected for debug by the backend.\n");
14870 return false;
14873 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14874 the target hook doesn't explicitly allow it in debug info, assume
14875 we can't express it in the debug info. */
14876 /* Don't complain about TLS UNSPECs, those are just too hard to
14877 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14878 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14879 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14880 if (flag_checking
14881 && (XVECLEN (rtl, 0) == 0
14882 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14883 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14884 inform (current_function_decl
14885 ? DECL_SOURCE_LOCATION (current_function_decl)
14886 : UNKNOWN_LOCATION,
14887 #if NUM_UNSPEC_VALUES > 0
14888 "non-delegitimized UNSPEC %s (%d) found in variable location",
14889 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14890 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14891 #else
14892 "non-delegitimized UNSPEC %d found in variable location",
14893 #endif
14894 XINT (rtl, 1));
14895 expansion_failed (NULL_TREE, rtl,
14896 "UNSPEC hasn't been delegitimized.\n");
14897 return false;
14900 if (CONST_POLY_INT_P (rtl))
14901 return false;
14903 /* FIXME: Refer to PR60655. It is possible for simplification
14904 of rtl expressions in var tracking to produce such expressions.
14905 We should really identify / validate expressions
14906 enclosed in CONST that can be handled by assemblers on various
14907 targets and only handle legitimate cases here. */
14908 switch (GET_CODE (rtl))
14910 case SYMBOL_REF:
14911 break;
14912 case NOT:
14913 case NEG:
14914 return false;
14915 case PLUS:
14917 /* Make sure SYMBOL_REFs/UNSPECs are at most in one of the
14918 operands. */
14919 subrtx_var_iterator::array_type array;
14920 bool first = false;
14921 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14922 if (SYMBOL_REF_P (*iter)
14923 || LABEL_P (*iter)
14924 || GET_CODE (*iter) == UNSPEC)
14926 first = true;
14927 break;
14929 if (!first)
14930 return true;
14931 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14932 if (SYMBOL_REF_P (*iter)
14933 || LABEL_P (*iter)
14934 || GET_CODE (*iter) == UNSPEC)
14935 return false;
14936 return true;
14938 case MINUS:
14940 /* Disallow negation of SYMBOL_REFs or UNSPECs when they
14941 appear in the second operand of MINUS. */
14942 subrtx_var_iterator::array_type array;
14943 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 1), ALL)
14944 if (SYMBOL_REF_P (*iter)
14945 || LABEL_P (*iter)
14946 || GET_CODE (*iter) == UNSPEC)
14947 return false;
14948 return true;
14950 default:
14951 return true;
14954 if (CONSTANT_POOL_ADDRESS_P (rtl))
14956 bool marked;
14957 get_pool_constant_mark (rtl, &marked);
14958 /* If all references to this pool constant were optimized away,
14959 it was not output and thus we can't represent it. */
14960 if (!marked)
14962 expansion_failed (NULL_TREE, rtl,
14963 "Constant was removed from constant pool.\n");
14964 return false;
14968 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14969 return false;
14971 /* Avoid references to external symbols in debug info, on several targets
14972 the linker might even refuse to link when linking a shared library,
14973 and in many other cases the relocations for .debug_info/.debug_loc are
14974 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14975 to be defined within the same shared library or executable are fine. */
14976 if (SYMBOL_REF_EXTERNAL_P (rtl))
14978 tree decl = SYMBOL_REF_DECL (rtl);
14980 if (decl == NULL || !targetm.binds_local_p (decl))
14982 expansion_failed (NULL_TREE, rtl,
14983 "Symbol not defined in current TU.\n");
14984 return false;
14988 return true;
14991 /* Return true if constant RTL can be emitted in DW_OP_addr or
14992 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14993 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14995 static bool
14996 const_ok_for_output (rtx rtl)
14998 if (GET_CODE (rtl) == SYMBOL_REF)
14999 return const_ok_for_output_1 (rtl);
15001 if (GET_CODE (rtl) == CONST)
15003 subrtx_var_iterator::array_type array;
15004 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
15005 if (!const_ok_for_output_1 (*iter))
15006 return false;
15007 return true;
15010 return true;
15013 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
15014 if possible, NULL otherwise. */
15016 static dw_die_ref
15017 base_type_for_mode (machine_mode mode, bool unsignedp)
15019 dw_die_ref type_die;
15020 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
15022 if (type == NULL)
15023 return NULL;
15024 switch (TREE_CODE (type))
15026 case INTEGER_TYPE:
15027 case REAL_TYPE:
15028 break;
15029 default:
15030 return NULL;
15032 type_die = lookup_type_die (type);
15033 if (!type_die)
15034 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
15035 comp_unit_die ());
15036 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
15037 return NULL;
15038 return type_die;
15041 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
15042 type matching MODE, or, if MODE is narrower than or as wide as
15043 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
15044 possible. */
15046 static dw_loc_descr_ref
15047 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
15049 machine_mode outer_mode = mode;
15050 dw_die_ref type_die;
15051 dw_loc_descr_ref cvt;
15053 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15055 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
15056 return op;
15058 type_die = base_type_for_mode (outer_mode, 1);
15059 if (type_die == NULL)
15060 return NULL;
15061 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15062 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15063 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15064 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15065 add_loc_descr (&op, cvt);
15066 return op;
15069 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
15071 static dw_loc_descr_ref
15072 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
15073 dw_loc_descr_ref op1)
15075 dw_loc_descr_ref ret = op0;
15076 add_loc_descr (&ret, op1);
15077 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15078 if (STORE_FLAG_VALUE != 1)
15080 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
15081 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
15083 return ret;
15086 /* Subroutine of scompare_loc_descriptor for the case in which we're
15087 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15088 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
15090 static dw_loc_descr_ref
15091 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
15092 scalar_int_mode op_mode,
15093 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15095 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
15096 dw_loc_descr_ref cvt;
15098 if (type_die == NULL)
15099 return NULL;
15100 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15101 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15102 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15103 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15104 add_loc_descr (&op0, cvt);
15105 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15106 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15107 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15108 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15109 add_loc_descr (&op1, cvt);
15110 return compare_loc_descriptor (op, op0, op1);
15113 /* Subroutine of scompare_loc_descriptor for the case in which we're
15114 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
15115 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
15117 static dw_loc_descr_ref
15118 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
15119 scalar_int_mode op_mode,
15120 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
15122 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
15123 /* For eq/ne, if the operands are known to be zero-extended,
15124 there is no need to do the fancy shifting up. */
15125 if (op == DW_OP_eq || op == DW_OP_ne)
15127 dw_loc_descr_ref last0, last1;
15128 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15130 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15132 /* deref_size zero extends, and for constants we can check
15133 whether they are zero extended or not. */
15134 if (((last0->dw_loc_opc == DW_OP_deref_size
15135 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15136 || (CONST_INT_P (XEXP (rtl, 0))
15137 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
15138 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
15139 && ((last1->dw_loc_opc == DW_OP_deref_size
15140 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
15141 || (CONST_INT_P (XEXP (rtl, 1))
15142 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
15143 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
15144 return compare_loc_descriptor (op, op0, op1);
15146 /* EQ/NE comparison against constant in narrower type than
15147 DWARF2_ADDR_SIZE can be performed either as
15148 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
15149 DW_OP_{eq,ne}
15151 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
15152 DW_OP_{eq,ne}. Pick whatever is shorter. */
15153 if (CONST_INT_P (XEXP (rtl, 1))
15154 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
15155 && (size_of_int_loc_descriptor (shift) + 1
15156 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
15157 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
15158 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15159 & GET_MODE_MASK (op_mode))))
15161 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
15162 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15163 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
15164 & GET_MODE_MASK (op_mode));
15165 return compare_loc_descriptor (op, op0, op1);
15168 add_loc_descr (&op0, int_loc_descriptor (shift));
15169 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15170 if (CONST_INT_P (XEXP (rtl, 1)))
15171 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
15172 else
15174 add_loc_descr (&op1, int_loc_descriptor (shift));
15175 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15177 return compare_loc_descriptor (op, op0, op1);
15180 /* Return location descriptor for signed comparison OP RTL. */
15182 static dw_loc_descr_ref
15183 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15184 machine_mode mem_mode)
15186 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
15187 dw_loc_descr_ref op0, op1;
15189 if (op_mode == VOIDmode)
15190 op_mode = GET_MODE (XEXP (rtl, 1));
15191 if (op_mode == VOIDmode)
15192 return NULL;
15194 scalar_int_mode int_op_mode;
15195 if (dwarf_strict
15196 && dwarf_version < 5
15197 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
15198 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
15199 return NULL;
15201 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15202 VAR_INIT_STATUS_INITIALIZED);
15203 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15204 VAR_INIT_STATUS_INITIALIZED);
15206 if (op0 == NULL || op1 == NULL)
15207 return NULL;
15209 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
15211 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
15212 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
15214 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
15215 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
15217 return compare_loc_descriptor (op, op0, op1);
15220 /* Return location descriptor for unsigned comparison OP RTL. */
15222 static dw_loc_descr_ref
15223 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
15224 machine_mode mem_mode)
15226 dw_loc_descr_ref op0, op1;
15228 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
15229 if (test_op_mode == VOIDmode)
15230 test_op_mode = GET_MODE (XEXP (rtl, 1));
15232 scalar_int_mode op_mode;
15233 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
15234 return NULL;
15236 if (dwarf_strict
15237 && dwarf_version < 5
15238 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
15239 return NULL;
15241 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
15242 VAR_INIT_STATUS_INITIALIZED);
15243 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
15244 VAR_INIT_STATUS_INITIALIZED);
15246 if (op0 == NULL || op1 == NULL)
15247 return NULL;
15249 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
15251 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
15252 dw_loc_descr_ref last0, last1;
15253 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
15255 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
15257 if (CONST_INT_P (XEXP (rtl, 0)))
15258 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
15259 /* deref_size zero extends, so no need to mask it again. */
15260 else if (last0->dw_loc_opc != DW_OP_deref_size
15261 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15263 add_loc_descr (&op0, int_loc_descriptor (mask));
15264 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15266 if (CONST_INT_P (XEXP (rtl, 1)))
15267 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
15268 /* deref_size zero extends, so no need to mask it again. */
15269 else if (last1->dw_loc_opc != DW_OP_deref_size
15270 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
15272 add_loc_descr (&op1, int_loc_descriptor (mask));
15273 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15276 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
15278 HOST_WIDE_INT bias = 1;
15279 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15280 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15281 if (CONST_INT_P (XEXP (rtl, 1)))
15282 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
15283 + INTVAL (XEXP (rtl, 1)));
15284 else
15285 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
15286 bias, 0));
15288 return compare_loc_descriptor (op, op0, op1);
15291 /* Return location descriptor for {U,S}{MIN,MAX}. */
15293 static dw_loc_descr_ref
15294 minmax_loc_descriptor (rtx rtl, machine_mode mode,
15295 machine_mode mem_mode)
15297 enum dwarf_location_atom op;
15298 dw_loc_descr_ref op0, op1, ret;
15299 dw_loc_descr_ref bra_node, drop_node;
15301 scalar_int_mode int_mode;
15302 if (dwarf_strict
15303 && dwarf_version < 5
15304 && (!is_a <scalar_int_mode> (mode, &int_mode)
15305 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
15306 return NULL;
15308 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15309 VAR_INIT_STATUS_INITIALIZED);
15310 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15311 VAR_INIT_STATUS_INITIALIZED);
15313 if (op0 == NULL || op1 == NULL)
15314 return NULL;
15316 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
15317 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
15318 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
15319 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
15321 /* Checked by the caller. */
15322 int_mode = as_a <scalar_int_mode> (mode);
15323 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15325 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
15326 add_loc_descr (&op0, int_loc_descriptor (mask));
15327 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
15328 add_loc_descr (&op1, int_loc_descriptor (mask));
15329 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
15331 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15333 HOST_WIDE_INT bias = 1;
15334 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
15335 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15336 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
15339 else if (is_a <scalar_int_mode> (mode, &int_mode)
15340 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
15342 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
15343 add_loc_descr (&op0, int_loc_descriptor (shift));
15344 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
15345 add_loc_descr (&op1, int_loc_descriptor (shift));
15346 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
15348 else if (is_a <scalar_int_mode> (mode, &int_mode)
15349 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15351 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
15352 dw_loc_descr_ref cvt;
15353 if (type_die == NULL)
15354 return NULL;
15355 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15356 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15357 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15358 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15359 add_loc_descr (&op0, cvt);
15360 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15361 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15362 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15363 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15364 add_loc_descr (&op1, cvt);
15367 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
15368 op = DW_OP_lt;
15369 else
15370 op = DW_OP_gt;
15371 ret = op0;
15372 add_loc_descr (&ret, op1);
15373 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
15374 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15375 add_loc_descr (&ret, bra_node);
15376 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15377 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15378 add_loc_descr (&ret, drop_node);
15379 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15380 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15381 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
15382 && is_a <scalar_int_mode> (mode, &int_mode)
15383 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15384 ret = convert_descriptor_to_mode (int_mode, ret);
15385 return ret;
15388 /* Helper function for mem_loc_descriptor. Perform OP binary op,
15389 but after converting arguments to type_die, afterwards
15390 convert back to unsigned. */
15392 static dw_loc_descr_ref
15393 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
15394 scalar_int_mode mode, machine_mode mem_mode)
15396 dw_loc_descr_ref cvt, op0, op1;
15398 if (type_die == NULL)
15399 return NULL;
15400 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15401 VAR_INIT_STATUS_INITIALIZED);
15402 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15403 VAR_INIT_STATUS_INITIALIZED);
15404 if (op0 == NULL || op1 == NULL)
15405 return NULL;
15406 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15407 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15408 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15409 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15410 add_loc_descr (&op0, cvt);
15411 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15412 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15413 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15414 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15415 add_loc_descr (&op1, cvt);
15416 add_loc_descr (&op0, op1);
15417 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
15418 return convert_descriptor_to_mode (mode, op0);
15421 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
15422 const0 is DW_OP_lit0 or corresponding typed constant,
15423 const1 is DW_OP_lit1 or corresponding typed constant
15424 and constMSB is constant with just the MSB bit set
15425 for the mode):
15426 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15427 L1: const0 DW_OP_swap
15428 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
15429 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15430 L3: DW_OP_drop
15431 L4: DW_OP_nop
15433 CTZ is similar:
15434 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
15435 L1: const0 DW_OP_swap
15436 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15437 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15438 L3: DW_OP_drop
15439 L4: DW_OP_nop
15441 FFS is similar:
15442 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
15443 L1: const1 DW_OP_swap
15444 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
15445 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
15446 L3: DW_OP_drop
15447 L4: DW_OP_nop */
15449 static dw_loc_descr_ref
15450 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
15451 machine_mode mem_mode)
15453 dw_loc_descr_ref op0, ret, tmp;
15454 HOST_WIDE_INT valv;
15455 dw_loc_descr_ref l1jump, l1label;
15456 dw_loc_descr_ref l2jump, l2label;
15457 dw_loc_descr_ref l3jump, l3label;
15458 dw_loc_descr_ref l4jump, l4label;
15459 rtx msb;
15461 if (GET_MODE (XEXP (rtl, 0)) != mode)
15462 return NULL;
15464 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15465 VAR_INIT_STATUS_INITIALIZED);
15466 if (op0 == NULL)
15467 return NULL;
15468 ret = op0;
15469 if (GET_CODE (rtl) == CLZ)
15471 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15472 valv = GET_MODE_BITSIZE (mode);
15474 else if (GET_CODE (rtl) == FFS)
15475 valv = 0;
15476 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
15477 valv = GET_MODE_BITSIZE (mode);
15478 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15479 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
15480 add_loc_descr (&ret, l1jump);
15481 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15482 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
15483 VAR_INIT_STATUS_INITIALIZED);
15484 if (tmp == NULL)
15485 return NULL;
15486 add_loc_descr (&ret, tmp);
15487 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15488 add_loc_descr (&ret, l4jump);
15489 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15490 ? const1_rtx : const0_rtx,
15491 mode, mem_mode,
15492 VAR_INIT_STATUS_INITIALIZED);
15493 if (l1label == NULL)
15494 return NULL;
15495 add_loc_descr (&ret, l1label);
15496 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15497 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15498 add_loc_descr (&ret, l2label);
15499 if (GET_CODE (rtl) != CLZ)
15500 msb = const1_rtx;
15501 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15502 msb = GEN_INT (HOST_WIDE_INT_1U
15503 << (GET_MODE_BITSIZE (mode) - 1));
15504 else
15505 msb = immed_wide_int_const
15506 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15507 GET_MODE_PRECISION (mode)), mode);
15508 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15509 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15510 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15511 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15512 else
15513 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15514 VAR_INIT_STATUS_INITIALIZED);
15515 if (tmp == NULL)
15516 return NULL;
15517 add_loc_descr (&ret, tmp);
15518 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15519 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15520 add_loc_descr (&ret, l3jump);
15521 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15522 VAR_INIT_STATUS_INITIALIZED);
15523 if (tmp == NULL)
15524 return NULL;
15525 add_loc_descr (&ret, tmp);
15526 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15527 ? DW_OP_shl : DW_OP_shr, 0, 0));
15528 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15529 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15530 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15531 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15532 add_loc_descr (&ret, l2jump);
15533 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15534 add_loc_descr (&ret, l3label);
15535 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15536 add_loc_descr (&ret, l4label);
15537 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15538 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15539 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15540 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15541 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15542 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15543 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15544 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15545 return ret;
15548 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15549 const1 is DW_OP_lit1 or corresponding typed constant):
15550 const0 DW_OP_swap
15551 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15552 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15553 L2: DW_OP_drop
15555 PARITY is similar:
15556 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15557 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15558 L2: DW_OP_drop */
15560 static dw_loc_descr_ref
15561 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15562 machine_mode mem_mode)
15564 dw_loc_descr_ref op0, ret, tmp;
15565 dw_loc_descr_ref l1jump, l1label;
15566 dw_loc_descr_ref l2jump, l2label;
15568 if (GET_MODE (XEXP (rtl, 0)) != mode)
15569 return NULL;
15571 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15572 VAR_INIT_STATUS_INITIALIZED);
15573 if (op0 == NULL)
15574 return NULL;
15575 ret = op0;
15576 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15577 VAR_INIT_STATUS_INITIALIZED);
15578 if (tmp == NULL)
15579 return NULL;
15580 add_loc_descr (&ret, tmp);
15581 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15582 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15583 add_loc_descr (&ret, l1label);
15584 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15585 add_loc_descr (&ret, l2jump);
15586 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15587 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15588 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15589 VAR_INIT_STATUS_INITIALIZED);
15590 if (tmp == NULL)
15591 return NULL;
15592 add_loc_descr (&ret, tmp);
15593 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15594 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15595 ? DW_OP_plus : DW_OP_xor, 0, 0));
15596 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15597 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15598 VAR_INIT_STATUS_INITIALIZED);
15599 add_loc_descr (&ret, tmp);
15600 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15601 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15602 add_loc_descr (&ret, l1jump);
15603 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15604 add_loc_descr (&ret, l2label);
15605 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15606 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15607 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15608 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15609 return ret;
15612 /* BSWAP (constS is initial shift count, either 56 or 24):
15613 constS const0
15614 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15615 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15616 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15617 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15618 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15620 static dw_loc_descr_ref
15621 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15622 machine_mode mem_mode)
15624 dw_loc_descr_ref op0, ret, tmp;
15625 dw_loc_descr_ref l1jump, l1label;
15626 dw_loc_descr_ref l2jump, l2label;
15628 if (BITS_PER_UNIT != 8
15629 || (GET_MODE_BITSIZE (mode) != 32
15630 && GET_MODE_BITSIZE (mode) != 64))
15631 return NULL;
15633 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15634 VAR_INIT_STATUS_INITIALIZED);
15635 if (op0 == NULL)
15636 return NULL;
15638 ret = op0;
15639 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15640 mode, mem_mode,
15641 VAR_INIT_STATUS_INITIALIZED);
15642 if (tmp == NULL)
15643 return NULL;
15644 add_loc_descr (&ret, tmp);
15645 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15646 VAR_INIT_STATUS_INITIALIZED);
15647 if (tmp == NULL)
15648 return NULL;
15649 add_loc_descr (&ret, tmp);
15650 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15651 add_loc_descr (&ret, l1label);
15652 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15653 mode, mem_mode,
15654 VAR_INIT_STATUS_INITIALIZED);
15655 add_loc_descr (&ret, tmp);
15656 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15657 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15658 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15659 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15660 VAR_INIT_STATUS_INITIALIZED);
15661 if (tmp == NULL)
15662 return NULL;
15663 add_loc_descr (&ret, tmp);
15664 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15665 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15666 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15667 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15668 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15669 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15670 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15671 VAR_INIT_STATUS_INITIALIZED);
15672 add_loc_descr (&ret, tmp);
15673 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15674 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15675 add_loc_descr (&ret, l2jump);
15676 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15677 VAR_INIT_STATUS_INITIALIZED);
15678 add_loc_descr (&ret, tmp);
15679 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15680 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15681 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15682 add_loc_descr (&ret, l1jump);
15683 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15684 add_loc_descr (&ret, l2label);
15685 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15686 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15687 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15688 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15689 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15690 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15691 return ret;
15694 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15695 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15696 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15697 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15699 ROTATERT is similar:
15700 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15701 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15702 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15704 static dw_loc_descr_ref
15705 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15706 machine_mode mem_mode)
15708 rtx rtlop1 = XEXP (rtl, 1);
15709 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15710 int i;
15712 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15713 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15714 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15715 VAR_INIT_STATUS_INITIALIZED);
15716 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15717 VAR_INIT_STATUS_INITIALIZED);
15718 if (op0 == NULL || op1 == NULL)
15719 return NULL;
15720 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15721 for (i = 0; i < 2; i++)
15723 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15724 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15725 mode, mem_mode,
15726 VAR_INIT_STATUS_INITIALIZED);
15727 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15728 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15729 ? DW_OP_const4u
15730 : HOST_BITS_PER_WIDE_INT == 64
15731 ? DW_OP_const8u : DW_OP_constu,
15732 GET_MODE_MASK (mode), 0);
15733 else
15734 mask[i] = NULL;
15735 if (mask[i] == NULL)
15736 return NULL;
15737 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15739 ret = op0;
15740 add_loc_descr (&ret, op1);
15741 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15742 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15743 if (GET_CODE (rtl) == ROTATERT)
15745 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15746 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15747 GET_MODE_BITSIZE (mode), 0));
15749 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15750 if (mask[0] != NULL)
15751 add_loc_descr (&ret, mask[0]);
15752 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15753 if (mask[1] != NULL)
15755 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15756 add_loc_descr (&ret, mask[1]);
15757 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15759 if (GET_CODE (rtl) == ROTATE)
15761 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15762 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15763 GET_MODE_BITSIZE (mode), 0));
15765 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15766 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15767 return ret;
15770 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15771 for DEBUG_PARAMETER_REF RTL. */
15773 static dw_loc_descr_ref
15774 parameter_ref_descriptor (rtx rtl)
15776 dw_loc_descr_ref ret;
15777 dw_die_ref ref;
15779 if (dwarf_strict)
15780 return NULL;
15781 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15782 /* With LTO during LTRANS we get the late DIE that refers to the early
15783 DIE, thus we add another indirection here. This seems to confuse
15784 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15785 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15786 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15787 if (ref)
15789 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15790 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15791 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15793 else
15795 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15796 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15798 return ret;
15801 /* The following routine converts the RTL for a variable or parameter
15802 (resident in memory) into an equivalent Dwarf representation of a
15803 mechanism for getting the address of that same variable onto the top of a
15804 hypothetical "address evaluation" stack.
15806 When creating memory location descriptors, we are effectively transforming
15807 the RTL for a memory-resident object into its Dwarf postfix expression
15808 equivalent. This routine recursively descends an RTL tree, turning
15809 it into Dwarf postfix code as it goes.
15811 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15813 MEM_MODE is the mode of the memory reference, needed to handle some
15814 autoincrement addressing modes.
15816 Return 0 if we can't represent the location. */
15818 dw_loc_descr_ref
15819 mem_loc_descriptor (rtx rtl, machine_mode mode,
15820 machine_mode mem_mode,
15821 enum var_init_status initialized)
15823 dw_loc_descr_ref mem_loc_result = NULL;
15824 enum dwarf_location_atom op;
15825 dw_loc_descr_ref op0, op1;
15826 rtx inner = NULL_RTX;
15827 poly_int64 offset;
15829 if (mode == VOIDmode)
15830 mode = GET_MODE (rtl);
15832 /* Note that for a dynamically sized array, the location we will generate a
15833 description of here will be the lowest numbered location which is
15834 actually within the array. That's *not* necessarily the same as the
15835 zeroth element of the array. */
15837 rtl = targetm.delegitimize_address (rtl);
15839 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15840 return NULL;
15842 scalar_int_mode int_mode = BImode, inner_mode, op1_mode;
15843 switch (GET_CODE (rtl))
15845 case POST_INC:
15846 case POST_DEC:
15847 case POST_MODIFY:
15848 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15850 case SUBREG:
15851 /* The case of a subreg may arise when we have a local (register)
15852 variable or a formal (register) parameter which doesn't quite fill
15853 up an entire register. For now, just assume that it is
15854 legitimate to make the Dwarf info refer to the whole register which
15855 contains the given subreg. */
15856 if (!subreg_lowpart_p (rtl))
15857 break;
15858 inner = SUBREG_REG (rtl);
15859 /* FALLTHRU */
15860 case TRUNCATE:
15861 if (inner == NULL_RTX)
15862 inner = XEXP (rtl, 0);
15863 if (is_a <scalar_int_mode> (mode, &int_mode)
15864 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15865 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15866 #ifdef POINTERS_EXTEND_UNSIGNED
15867 || (int_mode == Pmode && mem_mode != VOIDmode)
15868 #endif
15870 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15872 mem_loc_result = mem_loc_descriptor (inner,
15873 inner_mode,
15874 mem_mode, initialized);
15875 break;
15877 if (dwarf_strict && dwarf_version < 5)
15878 break;
15879 if (is_a <scalar_int_mode> (mode, &int_mode)
15880 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15881 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15882 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15884 dw_die_ref type_die;
15885 dw_loc_descr_ref cvt;
15887 mem_loc_result = mem_loc_descriptor (inner,
15888 GET_MODE (inner),
15889 mem_mode, initialized);
15890 if (mem_loc_result == NULL)
15891 break;
15892 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15893 if (type_die == NULL)
15895 mem_loc_result = NULL;
15896 break;
15898 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15899 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15900 else
15901 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15902 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15903 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15904 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15905 add_loc_descr (&mem_loc_result, cvt);
15906 if (is_a <scalar_int_mode> (mode, &int_mode)
15907 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15909 /* Convert it to untyped afterwards. */
15910 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15911 add_loc_descr (&mem_loc_result, cvt);
15914 break;
15916 case REG:
15917 if (!is_a <scalar_int_mode> (mode, &int_mode)
15918 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15919 && rtl != arg_pointer_rtx
15920 && rtl != frame_pointer_rtx
15921 #ifdef POINTERS_EXTEND_UNSIGNED
15922 && (int_mode != Pmode || mem_mode == VOIDmode)
15923 #endif
15926 dw_die_ref type_die;
15927 unsigned int dbx_regnum;
15929 if (dwarf_strict && dwarf_version < 5)
15930 break;
15931 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15932 break;
15933 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15934 if (type_die == NULL)
15935 break;
15937 dbx_regnum = dbx_reg_number (rtl);
15938 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15939 break;
15940 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15941 dbx_regnum, 0);
15942 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15943 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15944 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15945 break;
15947 /* Whenever a register number forms a part of the description of the
15948 method for calculating the (dynamic) address of a memory resident
15949 object, DWARF rules require the register number be referred to as
15950 a "base register". This distinction is not based in any way upon
15951 what category of register the hardware believes the given register
15952 belongs to. This is strictly DWARF terminology we're dealing with
15953 here. Note that in cases where the location of a memory-resident
15954 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15955 OP_CONST (0)) the actual DWARF location descriptor that we generate
15956 may just be OP_BASEREG (basereg). This may look deceptively like
15957 the object in question was allocated to a register (rather than in
15958 memory) so DWARF consumers need to be aware of the subtle
15959 distinction between OP_REG and OP_BASEREG. */
15960 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15961 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15962 else if (stack_realign_drap
15963 && crtl->drap_reg
15964 && crtl->args.internal_arg_pointer == rtl
15965 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15967 /* If RTL is internal_arg_pointer, which has been optimized
15968 out, use DRAP instead. */
15969 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15970 VAR_INIT_STATUS_INITIALIZED);
15972 break;
15974 case SIGN_EXTEND:
15975 case ZERO_EXTEND:
15976 if (!is_a <scalar_int_mode> (mode, &int_mode)
15977 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15978 break;
15979 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15980 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15981 if (op0 == 0)
15982 break;
15983 else if (GET_CODE (rtl) == ZERO_EXTEND
15984 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15985 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15986 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15987 to expand zero extend as two shifts instead of
15988 masking. */
15989 && GET_MODE_SIZE (inner_mode) <= 4)
15991 mem_loc_result = op0;
15992 add_loc_descr (&mem_loc_result,
15993 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15994 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15996 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15998 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15999 shift *= BITS_PER_UNIT;
16000 if (GET_CODE (rtl) == SIGN_EXTEND)
16001 op = DW_OP_shra;
16002 else
16003 op = DW_OP_shr;
16004 mem_loc_result = op0;
16005 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16006 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16007 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
16008 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16010 else if (!dwarf_strict || dwarf_version >= 5)
16012 dw_die_ref type_die1, type_die2;
16013 dw_loc_descr_ref cvt;
16015 type_die1 = base_type_for_mode (inner_mode,
16016 GET_CODE (rtl) == ZERO_EXTEND);
16017 if (type_die1 == NULL)
16018 break;
16019 type_die2 = base_type_for_mode (int_mode, 1);
16020 if (type_die2 == NULL)
16021 break;
16022 mem_loc_result = op0;
16023 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16024 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16025 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
16026 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16027 add_loc_descr (&mem_loc_result, cvt);
16028 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16029 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16030 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
16031 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16032 add_loc_descr (&mem_loc_result, cvt);
16034 break;
16036 case MEM:
16038 rtx new_rtl = avoid_constant_pool_reference (rtl);
16039 if (new_rtl != rtl)
16041 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
16042 initialized);
16043 if (mem_loc_result != NULL)
16044 return mem_loc_result;
16047 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
16048 get_address_mode (rtl), mode,
16049 VAR_INIT_STATUS_INITIALIZED);
16050 if (mem_loc_result == NULL)
16051 mem_loc_result = tls_mem_loc_descriptor (rtl);
16052 if (mem_loc_result != NULL)
16054 if (!is_a <scalar_int_mode> (mode, &int_mode)
16055 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16057 dw_die_ref type_die;
16058 dw_loc_descr_ref deref;
16059 HOST_WIDE_INT size;
16061 if (dwarf_strict && dwarf_version < 5)
16062 return NULL;
16063 if (!GET_MODE_SIZE (mode).is_constant (&size))
16064 return NULL;
16065 type_die
16066 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16067 if (type_die == NULL)
16068 return NULL;
16069 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
16070 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
16071 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
16072 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
16073 add_loc_descr (&mem_loc_result, deref);
16075 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
16076 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
16077 else
16078 add_loc_descr (&mem_loc_result,
16079 new_loc_descr (DW_OP_deref_size,
16080 GET_MODE_SIZE (int_mode), 0));
16082 break;
16084 case LO_SUM:
16085 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
16087 case LABEL_REF:
16088 /* Some ports can transform a symbol ref into a label ref, because
16089 the symbol ref is too far away and has to be dumped into a constant
16090 pool. */
16091 case CONST:
16092 case SYMBOL_REF:
16093 case UNSPEC:
16094 if (!is_a <scalar_int_mode> (mode, &int_mode)
16095 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
16096 #ifdef POINTERS_EXTEND_UNSIGNED
16097 && (int_mode != Pmode || mem_mode == VOIDmode)
16098 #endif
16100 break;
16102 if (GET_CODE (rtl) == UNSPEC)
16104 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16105 can't express it in the debug info. This can happen e.g. with some
16106 TLS UNSPECs. Allow UNSPECs formerly from CONST that the backend
16107 approves. */
16108 bool not_ok = false;
16109 subrtx_var_iterator::array_type array;
16110 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16111 if (*iter != rtl && !CONSTANT_P (*iter))
16113 not_ok = true;
16114 break;
16117 if (not_ok)
16118 break;
16120 FOR_EACH_SUBRTX_VAR (iter, array, rtl, ALL)
16121 if (!const_ok_for_output_1 (*iter))
16123 not_ok = true;
16124 break;
16127 if (not_ok)
16128 break;
16130 rtl = gen_rtx_CONST (GET_MODE (rtl), rtl);
16131 goto symref;
16134 if (GET_CODE (rtl) == SYMBOL_REF
16135 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
16137 dw_loc_descr_ref temp;
16139 /* If this is not defined, we have no way to emit the data. */
16140 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
16141 break;
16143 temp = new_addr_loc_descr (rtl, dtprel_true);
16145 /* We check for DWARF 5 here because gdb did not implement
16146 DW_OP_form_tls_address until after 7.12. */
16147 mem_loc_result = new_loc_descr ((dwarf_version >= 5
16148 ? DW_OP_form_tls_address
16149 : DW_OP_GNU_push_tls_address),
16150 0, 0);
16151 add_loc_descr (&mem_loc_result, temp);
16153 break;
16156 if (!const_ok_for_output (rtl))
16158 if (GET_CODE (rtl) == CONST)
16159 switch (GET_CODE (XEXP (rtl, 0)))
16161 case NOT:
16162 op = DW_OP_not;
16163 goto try_const_unop;
16164 case NEG:
16165 op = DW_OP_neg;
16166 goto try_const_unop;
16167 try_const_unop:
16168 rtx arg;
16169 arg = XEXP (XEXP (rtl, 0), 0);
16170 if (!CONSTANT_P (arg))
16171 arg = gen_rtx_CONST (int_mode, arg);
16172 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
16173 initialized);
16174 if (op0)
16176 mem_loc_result = op0;
16177 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16179 break;
16180 default:
16181 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
16182 mem_mode, initialized);
16183 break;
16185 break;
16188 symref:
16189 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
16190 vec_safe_push (used_rtx_array, rtl);
16191 break;
16193 case CONCAT:
16194 case CONCATN:
16195 case VAR_LOCATION:
16196 case DEBUG_IMPLICIT_PTR:
16197 expansion_failed (NULL_TREE, rtl,
16198 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
16199 return 0;
16201 case ENTRY_VALUE:
16202 if (dwarf_strict && dwarf_version < 5)
16203 return NULL;
16204 if (REG_P (ENTRY_VALUE_EXP (rtl)))
16206 if (!is_a <scalar_int_mode> (mode, &int_mode)
16207 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16208 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16209 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16210 else
16212 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
16213 if (dbx_regnum == IGNORED_DWARF_REGNUM)
16214 return NULL;
16215 op0 = one_reg_loc_descriptor (dbx_regnum,
16216 VAR_INIT_STATUS_INITIALIZED);
16219 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
16220 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
16222 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
16223 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16224 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
16225 return NULL;
16227 else
16228 gcc_unreachable ();
16229 if (op0 == NULL)
16230 return NULL;
16231 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
16232 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
16233 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
16234 break;
16236 case DEBUG_PARAMETER_REF:
16237 mem_loc_result = parameter_ref_descriptor (rtl);
16238 break;
16240 case PRE_MODIFY:
16241 /* Extract the PLUS expression nested inside and fall into
16242 PLUS code below. */
16243 rtl = XEXP (rtl, 1);
16244 goto plus;
16246 case PRE_INC:
16247 case PRE_DEC:
16248 /* Turn these into a PLUS expression and fall into the PLUS code
16249 below. */
16250 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
16251 gen_int_mode (GET_CODE (rtl) == PRE_INC
16252 ? GET_MODE_UNIT_SIZE (mem_mode)
16253 : -GET_MODE_UNIT_SIZE (mem_mode),
16254 mode));
16256 /* fall through */
16258 case PLUS:
16259 plus:
16260 if (is_based_loc (rtl)
16261 && is_a <scalar_int_mode> (mode, &int_mode)
16262 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16263 || XEXP (rtl, 0) == arg_pointer_rtx
16264 || XEXP (rtl, 0) == frame_pointer_rtx))
16265 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
16266 INTVAL (XEXP (rtl, 1)),
16267 VAR_INIT_STATUS_INITIALIZED);
16268 else
16270 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16271 VAR_INIT_STATUS_INITIALIZED);
16272 if (mem_loc_result == 0)
16273 break;
16275 if (CONST_INT_P (XEXP (rtl, 1))
16276 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
16277 <= DWARF2_ADDR_SIZE))
16278 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
16279 else
16281 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16282 VAR_INIT_STATUS_INITIALIZED);
16283 if (op1 == 0)
16284 return NULL;
16285 add_loc_descr (&mem_loc_result, op1);
16286 add_loc_descr (&mem_loc_result,
16287 new_loc_descr (DW_OP_plus, 0, 0));
16290 break;
16292 /* If a pseudo-reg is optimized away, it is possible for it to
16293 be replaced with a MEM containing a multiply or shift. */
16294 case MINUS:
16295 op = DW_OP_minus;
16296 goto do_binop;
16298 case MULT:
16299 op = DW_OP_mul;
16300 goto do_binop;
16302 case DIV:
16303 if ((!dwarf_strict || dwarf_version >= 5)
16304 && is_a <scalar_int_mode> (mode, &int_mode)
16305 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16307 mem_loc_result = typed_binop (DW_OP_div, rtl,
16308 base_type_for_mode (mode, 0),
16309 int_mode, mem_mode);
16310 break;
16312 op = DW_OP_div;
16313 goto do_binop;
16315 case UMOD:
16316 op = DW_OP_mod;
16317 goto do_binop;
16319 case ASHIFT:
16320 op = DW_OP_shl;
16321 goto do_shift;
16323 case ASHIFTRT:
16324 op = DW_OP_shra;
16325 goto do_shift;
16327 case LSHIFTRT:
16328 op = DW_OP_shr;
16329 goto do_shift;
16331 do_shift:
16332 if (!is_a <scalar_int_mode> (mode, &int_mode))
16333 break;
16334 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
16335 VAR_INIT_STATUS_INITIALIZED);
16337 rtx rtlop1 = XEXP (rtl, 1);
16338 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
16339 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
16340 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
16341 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
16342 VAR_INIT_STATUS_INITIALIZED);
16345 if (op0 == 0 || op1 == 0)
16346 break;
16348 mem_loc_result = op0;
16349 add_loc_descr (&mem_loc_result, op1);
16350 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16351 break;
16353 case AND:
16354 op = DW_OP_and;
16355 goto do_binop;
16357 case IOR:
16358 op = DW_OP_or;
16359 goto do_binop;
16361 case XOR:
16362 op = DW_OP_xor;
16363 goto do_binop;
16365 do_binop:
16366 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16367 VAR_INIT_STATUS_INITIALIZED);
16368 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16369 VAR_INIT_STATUS_INITIALIZED);
16371 if (op0 == 0 || op1 == 0)
16372 break;
16374 mem_loc_result = op0;
16375 add_loc_descr (&mem_loc_result, op1);
16376 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16377 break;
16379 case MOD:
16380 if ((!dwarf_strict || dwarf_version >= 5)
16381 && is_a <scalar_int_mode> (mode, &int_mode)
16382 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
16384 mem_loc_result = typed_binop (DW_OP_mod, rtl,
16385 base_type_for_mode (mode, 0),
16386 int_mode, mem_mode);
16387 break;
16390 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16391 VAR_INIT_STATUS_INITIALIZED);
16392 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16393 VAR_INIT_STATUS_INITIALIZED);
16395 if (op0 == 0 || op1 == 0)
16396 break;
16398 mem_loc_result = op0;
16399 add_loc_descr (&mem_loc_result, op1);
16400 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16401 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
16402 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
16403 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
16404 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
16405 break;
16407 case UDIV:
16408 if ((!dwarf_strict || dwarf_version >= 5)
16409 && is_a <scalar_int_mode> (mode, &int_mode))
16411 /* We can use a signed divide if the sign bit is not set. */
16412 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
16414 op = DW_OP_div;
16415 goto do_binop;
16418 mem_loc_result = typed_binop (DW_OP_div, rtl,
16419 base_type_for_mode (int_mode, 1),
16420 int_mode, mem_mode);
16422 break;
16424 case NOT:
16425 op = DW_OP_not;
16426 goto do_unop;
16428 case ABS:
16429 op = DW_OP_abs;
16430 goto do_unop;
16432 case NEG:
16433 op = DW_OP_neg;
16434 goto do_unop;
16436 do_unop:
16437 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
16438 VAR_INIT_STATUS_INITIALIZED);
16440 if (op0 == 0)
16441 break;
16443 mem_loc_result = op0;
16444 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16445 break;
16447 case CONST_INT:
16448 if (!is_a <scalar_int_mode> (mode, &int_mode)
16449 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16450 #ifdef POINTERS_EXTEND_UNSIGNED
16451 || (int_mode == Pmode
16452 && mem_mode != VOIDmode
16453 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
16454 #endif
16457 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16458 break;
16460 if ((!dwarf_strict || dwarf_version >= 5)
16461 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
16462 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
16464 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
16465 scalar_int_mode amode;
16466 if (type_die == NULL)
16467 return NULL;
16468 if (INTVAL (rtl) >= 0
16469 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
16470 .exists (&amode))
16471 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
16472 /* const DW_OP_convert <XXX> vs.
16473 DW_OP_const_type <XXX, 1, const>. */
16474 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
16475 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
16477 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
16478 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16479 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16480 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16481 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
16482 add_loc_descr (&mem_loc_result, op0);
16483 return mem_loc_result;
16485 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
16486 INTVAL (rtl));
16487 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16488 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16489 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16490 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
16491 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
16492 else
16494 mem_loc_result->dw_loc_oprnd2.val_class
16495 = dw_val_class_const_double;
16496 mem_loc_result->dw_loc_oprnd2.v.val_double
16497 = double_int::from_shwi (INTVAL (rtl));
16500 break;
16502 case CONST_DOUBLE:
16503 if (!dwarf_strict || dwarf_version >= 5)
16505 dw_die_ref type_die;
16507 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
16508 CONST_DOUBLE rtx could represent either a large integer
16509 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
16510 the value is always a floating point constant.
16512 When it is an integer, a CONST_DOUBLE is used whenever
16513 the constant requires 2 HWIs to be adequately represented.
16514 We output CONST_DOUBLEs as blocks. */
16515 if (mode == VOIDmode
16516 || (GET_MODE (rtl) == VOIDmode
16517 && maybe_ne (GET_MODE_BITSIZE (mode),
16518 HOST_BITS_PER_DOUBLE_INT)))
16519 break;
16520 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16521 if (type_die == NULL)
16522 return NULL;
16523 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16524 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16525 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16526 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16527 #if TARGET_SUPPORTS_WIDE_INT == 0
16528 if (!SCALAR_FLOAT_MODE_P (mode))
16530 mem_loc_result->dw_loc_oprnd2.val_class
16531 = dw_val_class_const_double;
16532 mem_loc_result->dw_loc_oprnd2.v.val_double
16533 = rtx_to_double_int (rtl);
16535 else
16536 #endif
16538 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16539 unsigned int length = GET_MODE_SIZE (float_mode);
16540 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16541 unsigned int elt_size = insert_float (rtl, array);
16543 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16544 mem_loc_result->dw_loc_oprnd2.v.val_vec.length
16545 = length / elt_size;
16546 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16547 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16550 break;
16552 case CONST_WIDE_INT:
16553 if (!dwarf_strict || dwarf_version >= 5)
16555 dw_die_ref type_die;
16557 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16558 if (type_die == NULL)
16559 return NULL;
16560 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16561 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16562 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16563 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16564 mem_loc_result->dw_loc_oprnd2.val_class
16565 = dw_val_class_wide_int;
16566 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16567 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16569 break;
16571 case CONST_POLY_INT:
16572 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16573 break;
16575 case EQ:
16576 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16577 break;
16579 case GE:
16580 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16581 break;
16583 case GT:
16584 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16585 break;
16587 case LE:
16588 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16589 break;
16591 case LT:
16592 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16593 break;
16595 case NE:
16596 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16597 break;
16599 case GEU:
16600 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16601 break;
16603 case GTU:
16604 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16605 break;
16607 case LEU:
16608 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16609 break;
16611 case LTU:
16612 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16613 break;
16615 case UMIN:
16616 case UMAX:
16617 if (!SCALAR_INT_MODE_P (mode))
16618 break;
16619 /* FALLTHRU */
16620 case SMIN:
16621 case SMAX:
16622 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16623 break;
16625 case ZERO_EXTRACT:
16626 case SIGN_EXTRACT:
16627 if (CONST_INT_P (XEXP (rtl, 1))
16628 && CONST_INT_P (XEXP (rtl, 2))
16629 && is_a <scalar_int_mode> (mode, &int_mode)
16630 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16631 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16632 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16633 && ((unsigned) INTVAL (XEXP (rtl, 1))
16634 + (unsigned) INTVAL (XEXP (rtl, 2))
16635 <= GET_MODE_BITSIZE (int_mode)))
16637 int shift, size;
16638 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16639 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16640 if (op0 == 0)
16641 break;
16642 if (GET_CODE (rtl) == SIGN_EXTRACT)
16643 op = DW_OP_shra;
16644 else
16645 op = DW_OP_shr;
16646 mem_loc_result = op0;
16647 size = INTVAL (XEXP (rtl, 1));
16648 shift = INTVAL (XEXP (rtl, 2));
16649 if (BITS_BIG_ENDIAN)
16650 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16651 if (shift + size != (int) DWARF2_ADDR_SIZE)
16653 add_loc_descr (&mem_loc_result,
16654 int_loc_descriptor (DWARF2_ADDR_SIZE
16655 - shift - size));
16656 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16658 if (size != (int) DWARF2_ADDR_SIZE)
16660 add_loc_descr (&mem_loc_result,
16661 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16662 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16665 break;
16667 case IF_THEN_ELSE:
16669 dw_loc_descr_ref op2, bra_node, drop_node;
16670 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16671 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16672 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16673 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16674 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16675 VAR_INIT_STATUS_INITIALIZED);
16676 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16677 VAR_INIT_STATUS_INITIALIZED);
16678 if (op0 == NULL || op1 == NULL || op2 == NULL)
16679 break;
16681 mem_loc_result = op1;
16682 add_loc_descr (&mem_loc_result, op2);
16683 add_loc_descr (&mem_loc_result, op0);
16684 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16685 add_loc_descr (&mem_loc_result, bra_node);
16686 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16687 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16688 add_loc_descr (&mem_loc_result, drop_node);
16689 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16690 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16692 break;
16694 case FLOAT_EXTEND:
16695 case FLOAT_TRUNCATE:
16696 case FLOAT:
16697 case UNSIGNED_FLOAT:
16698 case FIX:
16699 case UNSIGNED_FIX:
16700 if (!dwarf_strict || dwarf_version >= 5)
16702 dw_die_ref type_die;
16703 dw_loc_descr_ref cvt;
16705 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16706 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16707 if (op0 == NULL)
16708 break;
16709 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16710 && (GET_CODE (rtl) == FLOAT
16711 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16713 type_die = base_type_for_mode (int_mode,
16714 GET_CODE (rtl) == UNSIGNED_FLOAT);
16715 if (type_die == NULL)
16716 break;
16717 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16718 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16719 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16720 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16721 add_loc_descr (&op0, cvt);
16723 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16724 if (type_die == NULL)
16725 break;
16726 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16727 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16728 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16729 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16730 add_loc_descr (&op0, cvt);
16731 if (is_a <scalar_int_mode> (mode, &int_mode)
16732 && (GET_CODE (rtl) == FIX
16733 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16735 op0 = convert_descriptor_to_mode (int_mode, op0);
16736 if (op0 == NULL)
16737 break;
16739 mem_loc_result = op0;
16741 break;
16743 case CLZ:
16744 case CTZ:
16745 case FFS:
16746 if (is_a <scalar_int_mode> (mode, &int_mode))
16747 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16748 break;
16750 case POPCOUNT:
16751 case PARITY:
16752 if (is_a <scalar_int_mode> (mode, &int_mode))
16753 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16754 break;
16756 case BSWAP:
16757 if (is_a <scalar_int_mode> (mode, &int_mode))
16758 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16759 break;
16761 case ROTATE:
16762 case ROTATERT:
16763 if (is_a <scalar_int_mode> (mode, &int_mode))
16764 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16765 break;
16767 case COMPARE:
16768 /* In theory, we could implement the above. */
16769 /* DWARF cannot represent the unsigned compare operations
16770 natively. */
16771 case SS_MULT:
16772 case US_MULT:
16773 case SS_DIV:
16774 case US_DIV:
16775 case SS_PLUS:
16776 case US_PLUS:
16777 case SS_MINUS:
16778 case US_MINUS:
16779 case SS_NEG:
16780 case US_NEG:
16781 case SS_ABS:
16782 case SS_ASHIFT:
16783 case US_ASHIFT:
16784 case SS_TRUNCATE:
16785 case US_TRUNCATE:
16786 case UNORDERED:
16787 case ORDERED:
16788 case UNEQ:
16789 case UNGE:
16790 case UNGT:
16791 case UNLE:
16792 case UNLT:
16793 case LTGT:
16794 case FRACT_CONVERT:
16795 case UNSIGNED_FRACT_CONVERT:
16796 case SAT_FRACT:
16797 case UNSIGNED_SAT_FRACT:
16798 case SQRT:
16799 case ASM_OPERANDS:
16800 case VEC_MERGE:
16801 case VEC_SELECT:
16802 case VEC_CONCAT:
16803 case VEC_DUPLICATE:
16804 case VEC_SERIES:
16805 case HIGH:
16806 case FMA:
16807 case STRICT_LOW_PART:
16808 case CONST_VECTOR:
16809 case CONST_FIXED:
16810 case CLRSB:
16811 case CLOBBER:
16812 case SMUL_HIGHPART:
16813 case UMUL_HIGHPART:
16814 break;
16816 case CONST_STRING:
16817 resolve_one_addr (&rtl);
16818 goto symref;
16820 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16821 the expression. An UNSPEC rtx represents a raw DWARF operation,
16822 new_loc_descr is called for it to build the operation directly.
16823 Otherwise mem_loc_descriptor is called recursively. */
16824 case PARALLEL:
16826 int index = 0;
16827 dw_loc_descr_ref exp_result = NULL;
16829 for (; index < XVECLEN (rtl, 0); index++)
16831 rtx elem = XVECEXP (rtl, 0, index);
16832 if (GET_CODE (elem) == UNSPEC)
16834 /* Each DWARF operation UNSPEC contain two operands, if
16835 one operand is not used for the operation, const0_rtx is
16836 passed. */
16837 gcc_assert (XVECLEN (elem, 0) == 2);
16839 HOST_WIDE_INT dw_op = XINT (elem, 1);
16840 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16841 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16842 exp_result
16843 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16844 oprnd2);
16846 else
16847 exp_result
16848 = mem_loc_descriptor (elem, mode, mem_mode,
16849 VAR_INIT_STATUS_INITIALIZED);
16851 if (!mem_loc_result)
16852 mem_loc_result = exp_result;
16853 else
16854 add_loc_descr (&mem_loc_result, exp_result);
16857 break;
16860 default:
16861 if (flag_checking)
16863 print_rtl (stderr, rtl);
16864 gcc_unreachable ();
16866 break;
16869 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16870 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16872 return mem_loc_result;
16875 /* Return a descriptor that describes the concatenation of two locations.
16876 This is typically a complex variable. */
16878 static dw_loc_descr_ref
16879 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16881 /* At present we only track constant-sized pieces. */
16882 unsigned int size0, size1;
16883 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16884 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16885 return 0;
16887 dw_loc_descr_ref cc_loc_result = NULL;
16888 dw_loc_descr_ref x0_ref
16889 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16890 dw_loc_descr_ref x1_ref
16891 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16893 if (x0_ref == 0 || x1_ref == 0)
16894 return 0;
16896 cc_loc_result = x0_ref;
16897 add_loc_descr_op_piece (&cc_loc_result, size0);
16899 add_loc_descr (&cc_loc_result, x1_ref);
16900 add_loc_descr_op_piece (&cc_loc_result, size1);
16902 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16903 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16905 return cc_loc_result;
16908 /* Return a descriptor that describes the concatenation of N
16909 locations. */
16911 static dw_loc_descr_ref
16912 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16914 unsigned int i;
16915 dw_loc_descr_ref cc_loc_result = NULL;
16916 unsigned int n = XVECLEN (concatn, 0);
16917 unsigned int size;
16919 for (i = 0; i < n; ++i)
16921 dw_loc_descr_ref ref;
16922 rtx x = XVECEXP (concatn, 0, i);
16924 /* At present we only track constant-sized pieces. */
16925 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16926 return NULL;
16928 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16929 if (ref == NULL)
16930 return NULL;
16932 add_loc_descr (&cc_loc_result, ref);
16933 add_loc_descr_op_piece (&cc_loc_result, size);
16936 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16937 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16939 return cc_loc_result;
16942 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16943 for DEBUG_IMPLICIT_PTR RTL. */
16945 static dw_loc_descr_ref
16946 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16948 dw_loc_descr_ref ret;
16949 dw_die_ref ref;
16951 if (dwarf_strict && dwarf_version < 5)
16952 return NULL;
16953 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16954 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16955 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16956 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16957 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16958 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16959 if (ref)
16961 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16962 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16963 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16965 else
16967 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16968 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16970 return ret;
16973 /* Output a proper Dwarf location descriptor for a variable or parameter
16974 which is either allocated in a register or in a memory location. For a
16975 register, we just generate an OP_REG and the register number. For a
16976 memory location we provide a Dwarf postfix expression describing how to
16977 generate the (dynamic) address of the object onto the address stack.
16979 MODE is mode of the decl if this loc_descriptor is going to be used in
16980 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16981 allowed, VOIDmode otherwise.
16983 If we don't know how to describe it, return 0. */
16985 static dw_loc_descr_ref
16986 loc_descriptor (rtx rtl, machine_mode mode,
16987 enum var_init_status initialized)
16989 dw_loc_descr_ref loc_result = NULL;
16990 scalar_int_mode int_mode;
16992 switch (GET_CODE (rtl))
16994 case SUBREG:
16995 /* The case of a subreg may arise when we have a local (register)
16996 variable or a formal (register) parameter which doesn't quite fill
16997 up an entire register. For now, just assume that it is
16998 legitimate to make the Dwarf info refer to the whole register which
16999 contains the given subreg. */
17000 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
17001 loc_result = loc_descriptor (SUBREG_REG (rtl),
17002 GET_MODE (SUBREG_REG (rtl)), initialized);
17003 else
17004 goto do_default;
17005 break;
17007 case REG:
17008 loc_result = reg_loc_descriptor (rtl, initialized);
17009 break;
17011 case MEM:
17012 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17013 GET_MODE (rtl), initialized);
17014 if (loc_result == NULL)
17015 loc_result = tls_mem_loc_descriptor (rtl);
17016 if (loc_result == NULL)
17018 rtx new_rtl = avoid_constant_pool_reference (rtl);
17019 if (new_rtl != rtl)
17020 loc_result = loc_descriptor (new_rtl, mode, initialized);
17022 break;
17024 case CONCAT:
17025 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
17026 initialized);
17027 break;
17029 case CONCATN:
17030 loc_result = concatn_loc_descriptor (rtl, initialized);
17031 break;
17033 case VAR_LOCATION:
17034 /* Single part. */
17035 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
17037 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
17038 if (GET_CODE (loc) == EXPR_LIST)
17039 loc = XEXP (loc, 0);
17040 loc_result = loc_descriptor (loc, mode, initialized);
17041 break;
17044 rtl = XEXP (rtl, 1);
17045 /* FALLTHRU */
17047 case PARALLEL:
17049 rtvec par_elems = XVEC (rtl, 0);
17050 int num_elem = GET_NUM_ELEM (par_elems);
17051 machine_mode mode;
17052 int i, size;
17054 /* Create the first one, so we have something to add to. */
17055 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
17056 VOIDmode, initialized);
17057 if (loc_result == NULL)
17058 return NULL;
17059 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
17060 /* At present we only track constant-sized pieces. */
17061 if (!GET_MODE_SIZE (mode).is_constant (&size))
17062 return NULL;
17063 add_loc_descr_op_piece (&loc_result, size);
17064 for (i = 1; i < num_elem; i++)
17066 dw_loc_descr_ref temp;
17068 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
17069 VOIDmode, initialized);
17070 if (temp == NULL)
17071 return NULL;
17072 add_loc_descr (&loc_result, temp);
17073 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
17074 /* At present we only track constant-sized pieces. */
17075 if (!GET_MODE_SIZE (mode).is_constant (&size))
17076 return NULL;
17077 add_loc_descr_op_piece (&loc_result, size);
17080 break;
17082 case CONST_INT:
17083 if (mode != VOIDmode && mode != BLKmode)
17085 int_mode = as_a <scalar_int_mode> (mode);
17086 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
17087 INTVAL (rtl));
17089 break;
17091 case CONST_DOUBLE:
17092 if (mode == VOIDmode)
17093 mode = GET_MODE (rtl);
17095 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17097 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17099 /* Note that a CONST_DOUBLE rtx could represent either an integer
17100 or a floating-point constant. A CONST_DOUBLE is used whenever
17101 the constant requires more than one word in order to be
17102 adequately represented. We output CONST_DOUBLEs as blocks. */
17103 scalar_mode smode = as_a <scalar_mode> (mode);
17104 loc_result = new_loc_descr (DW_OP_implicit_value,
17105 GET_MODE_SIZE (smode), 0);
17106 #if TARGET_SUPPORTS_WIDE_INT == 0
17107 if (!SCALAR_FLOAT_MODE_P (smode))
17109 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
17110 loc_result->dw_loc_oprnd2.v.val_double
17111 = rtx_to_double_int (rtl);
17113 else
17114 #endif
17116 unsigned int length = GET_MODE_SIZE (smode);
17117 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17118 unsigned int elt_size = insert_float (rtl, array);
17120 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17121 loc_result->dw_loc_oprnd2.v.val_vec.length = length / elt_size;
17122 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17123 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17126 break;
17128 case CONST_WIDE_INT:
17129 if (mode == VOIDmode)
17130 mode = GET_MODE (rtl);
17132 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
17134 int_mode = as_a <scalar_int_mode> (mode);
17135 loc_result = new_loc_descr (DW_OP_implicit_value,
17136 GET_MODE_SIZE (int_mode), 0);
17137 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
17138 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
17139 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
17141 break;
17143 case CONST_VECTOR:
17144 if (mode == VOIDmode)
17145 mode = GET_MODE (rtl);
17147 if (mode != VOIDmode
17148 /* The combination of a length and byte elt_size doesn't extend
17149 naturally to boolean vectors, where several elements are packed
17150 into the same byte. */
17151 && GET_MODE_CLASS (mode) != MODE_VECTOR_BOOL
17152 && (dwarf_version >= 4 || !dwarf_strict))
17154 unsigned int length;
17155 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
17156 return NULL;
17158 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
17159 unsigned char *array
17160 = ggc_vec_alloc<unsigned char> (length * elt_size);
17161 unsigned int i;
17162 unsigned char *p;
17163 machine_mode imode = GET_MODE_INNER (mode);
17165 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
17166 switch (GET_MODE_CLASS (mode))
17168 case MODE_VECTOR_INT:
17169 for (i = 0, p = array; i < length; i++, p += elt_size)
17171 rtx elt = CONST_VECTOR_ELT (rtl, i);
17172 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
17174 break;
17176 case MODE_VECTOR_FLOAT:
17177 for (i = 0, p = array; i < length; i++, p += elt_size)
17179 rtx elt = CONST_VECTOR_ELT (rtl, i);
17180 insert_float (elt, p);
17182 break;
17184 default:
17185 gcc_unreachable ();
17188 loc_result = new_loc_descr (DW_OP_implicit_value,
17189 length * elt_size, 0);
17190 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
17191 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
17192 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
17193 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
17195 break;
17197 case CONST:
17198 if (mode == VOIDmode
17199 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
17200 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
17201 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
17203 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
17204 break;
17206 /* FALLTHROUGH */
17207 case SYMBOL_REF:
17208 if (!const_ok_for_output (rtl))
17209 break;
17210 /* FALLTHROUGH */
17211 case LABEL_REF:
17212 if (is_a <scalar_int_mode> (mode, &int_mode)
17213 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
17214 && (dwarf_version >= 4 || !dwarf_strict))
17216 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17217 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17218 vec_safe_push (used_rtx_array, rtl);
17220 break;
17222 case DEBUG_IMPLICIT_PTR:
17223 loc_result = implicit_ptr_descriptor (rtl, 0);
17224 break;
17226 case PLUS:
17227 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
17228 && CONST_INT_P (XEXP (rtl, 1)))
17230 loc_result
17231 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
17232 break;
17234 /* FALLTHRU */
17235 do_default:
17236 default:
17237 if ((is_a <scalar_int_mode> (mode, &int_mode)
17238 && GET_MODE (rtl) == int_mode
17239 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
17240 && dwarf_version >= 4)
17241 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
17243 /* Value expression. */
17244 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
17245 if (loc_result)
17246 add_loc_descr (&loc_result,
17247 new_loc_descr (DW_OP_stack_value, 0, 0));
17249 break;
17252 return loc_result;
17255 /* We need to figure out what section we should use as the base for the
17256 address ranges where a given location is valid.
17257 1. If this particular DECL has a section associated with it, use that.
17258 2. If this function has a section associated with it, use that.
17259 3. Otherwise, use the text section.
17260 XXX: If you split a variable across multiple sections, we won't notice. */
17262 static const char *
17263 secname_for_decl (const_tree decl)
17265 const char *secname;
17267 if (VAR_OR_FUNCTION_DECL_P (decl)
17268 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
17269 && DECL_SECTION_NAME (decl))
17270 secname = DECL_SECTION_NAME (decl);
17271 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
17273 if (in_cold_section_p)
17275 section *sec = current_function_section ();
17276 if (sec->common.flags & SECTION_NAMED)
17277 return sec->named.name;
17279 secname = DECL_SECTION_NAME (current_function_decl);
17281 else if (cfun && in_cold_section_p)
17282 secname = crtl->subsections.cold_section_label;
17283 else
17284 secname = text_section_label;
17286 return secname;
17289 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
17291 static bool
17292 decl_by_reference_p (tree decl)
17294 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
17295 || VAR_P (decl))
17296 && DECL_BY_REFERENCE (decl));
17299 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17300 for VARLOC. */
17302 static dw_loc_descr_ref
17303 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
17304 enum var_init_status initialized)
17306 int have_address = 0;
17307 dw_loc_descr_ref descr;
17308 machine_mode mode;
17310 if (want_address != 2)
17312 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
17313 /* Single part. */
17314 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17316 varloc = PAT_VAR_LOCATION_LOC (varloc);
17317 if (GET_CODE (varloc) == EXPR_LIST)
17318 varloc = XEXP (varloc, 0);
17319 mode = GET_MODE (varloc);
17320 if (MEM_P (varloc))
17322 rtx addr = XEXP (varloc, 0);
17323 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
17324 mode, initialized);
17325 if (descr)
17326 have_address = 1;
17327 else
17329 rtx x = avoid_constant_pool_reference (varloc);
17330 if (x != varloc)
17331 descr = mem_loc_descriptor (x, mode, VOIDmode,
17332 initialized);
17335 else
17336 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
17338 else
17339 return 0;
17341 else
17343 if (GET_CODE (varloc) == VAR_LOCATION)
17344 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
17345 else
17346 mode = DECL_MODE (loc);
17347 descr = loc_descriptor (varloc, mode, initialized);
17348 have_address = 1;
17351 if (!descr)
17352 return 0;
17354 if (want_address == 2 && !have_address
17355 && (dwarf_version >= 4 || !dwarf_strict))
17357 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17359 expansion_failed (loc, NULL_RTX,
17360 "DWARF address size mismatch");
17361 return 0;
17363 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
17364 have_address = 1;
17366 /* Show if we can't fill the request for an address. */
17367 if (want_address && !have_address)
17369 expansion_failed (loc, NULL_RTX,
17370 "Want address and only have value");
17371 return 0;
17374 /* If we've got an address and don't want one, dereference. */
17375 if (!want_address && have_address)
17377 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17378 enum dwarf_location_atom op;
17380 if (size > DWARF2_ADDR_SIZE || size == -1)
17382 expansion_failed (loc, NULL_RTX,
17383 "DWARF address size mismatch");
17384 return 0;
17386 else if (size == DWARF2_ADDR_SIZE)
17387 op = DW_OP_deref;
17388 else
17389 op = DW_OP_deref_size;
17391 add_loc_descr (&descr, new_loc_descr (op, size, 0));
17394 return descr;
17397 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
17398 if it is not possible. */
17400 static dw_loc_descr_ref
17401 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
17403 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
17404 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
17405 else if (dwarf_version >= 3 || !dwarf_strict)
17406 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
17407 else
17408 return NULL;
17411 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
17412 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
17414 static dw_loc_descr_ref
17415 dw_sra_loc_expr (tree decl, rtx loc)
17417 rtx p;
17418 unsigned HOST_WIDE_INT padsize = 0;
17419 dw_loc_descr_ref descr, *descr_tail;
17420 unsigned HOST_WIDE_INT decl_size;
17421 rtx varloc;
17422 enum var_init_status initialized;
17424 if (DECL_SIZE (decl) == NULL
17425 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
17426 return NULL;
17428 decl_size = tree_to_uhwi (DECL_SIZE (decl));
17429 descr = NULL;
17430 descr_tail = &descr;
17432 for (p = loc; p; p = XEXP (p, 1))
17434 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
17435 rtx loc_note = *decl_piece_varloc_ptr (p);
17436 dw_loc_descr_ref cur_descr;
17437 dw_loc_descr_ref *tail, last = NULL;
17438 unsigned HOST_WIDE_INT opsize = 0;
17440 if (loc_note == NULL_RTX
17441 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
17443 padsize += bitsize;
17444 continue;
17446 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
17447 varloc = NOTE_VAR_LOCATION (loc_note);
17448 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
17449 if (cur_descr == NULL)
17451 padsize += bitsize;
17452 continue;
17455 /* Check that cur_descr either doesn't use
17456 DW_OP_*piece operations, or their sum is equal
17457 to bitsize. Otherwise we can't embed it. */
17458 for (tail = &cur_descr; *tail != NULL;
17459 tail = &(*tail)->dw_loc_next)
17460 if ((*tail)->dw_loc_opc == DW_OP_piece)
17462 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
17463 * BITS_PER_UNIT;
17464 last = *tail;
17466 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
17468 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
17469 last = *tail;
17472 if (last != NULL && opsize != bitsize)
17474 padsize += bitsize;
17475 /* Discard the current piece of the descriptor and release any
17476 addr_table entries it uses. */
17477 remove_loc_list_addr_table_entries (cur_descr);
17478 continue;
17481 /* If there is a hole, add DW_OP_*piece after empty DWARF
17482 expression, which means that those bits are optimized out. */
17483 if (padsize)
17485 if (padsize > decl_size)
17487 remove_loc_list_addr_table_entries (cur_descr);
17488 goto discard_descr;
17490 decl_size -= padsize;
17491 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
17492 if (*descr_tail == NULL)
17494 remove_loc_list_addr_table_entries (cur_descr);
17495 goto discard_descr;
17497 descr_tail = &(*descr_tail)->dw_loc_next;
17498 padsize = 0;
17500 *descr_tail = cur_descr;
17501 descr_tail = tail;
17502 if (bitsize > decl_size)
17503 goto discard_descr;
17504 decl_size -= bitsize;
17505 if (last == NULL)
17507 HOST_WIDE_INT offset = 0;
17508 if (GET_CODE (varloc) == VAR_LOCATION
17509 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
17511 varloc = PAT_VAR_LOCATION_LOC (varloc);
17512 if (GET_CODE (varloc) == EXPR_LIST)
17513 varloc = XEXP (varloc, 0);
17517 if (GET_CODE (varloc) == CONST
17518 || GET_CODE (varloc) == SIGN_EXTEND
17519 || GET_CODE (varloc) == ZERO_EXTEND)
17520 varloc = XEXP (varloc, 0);
17521 else if (GET_CODE (varloc) == SUBREG)
17522 varloc = SUBREG_REG (varloc);
17523 else
17524 break;
17526 while (1);
17527 /* DW_OP_bit_size offset should be zero for register
17528 or implicit location descriptions and empty location
17529 descriptions, but for memory addresses needs big endian
17530 adjustment. */
17531 if (MEM_P (varloc))
17533 unsigned HOST_WIDE_INT memsize;
17534 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17535 goto discard_descr;
17536 memsize *= BITS_PER_UNIT;
17537 if (memsize != bitsize)
17539 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17540 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17541 goto discard_descr;
17542 if (memsize < bitsize)
17543 goto discard_descr;
17544 if (BITS_BIG_ENDIAN)
17545 offset = memsize - bitsize;
17549 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17550 if (*descr_tail == NULL)
17551 goto discard_descr;
17552 descr_tail = &(*descr_tail)->dw_loc_next;
17556 /* If there were any non-empty expressions, add padding till the end of
17557 the decl. */
17558 if (descr != NULL && decl_size != 0)
17560 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17561 if (*descr_tail == NULL)
17562 goto discard_descr;
17564 return descr;
17566 discard_descr:
17567 /* Discard the descriptor and release any addr_table entries it uses. */
17568 remove_loc_list_addr_table_entries (descr);
17569 return NULL;
17572 /* Return the dwarf representation of the location list LOC_LIST of
17573 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17574 function. */
17576 static dw_loc_list_ref
17577 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17579 const char *endname, *secname;
17580 var_loc_view endview;
17581 rtx varloc;
17582 enum var_init_status initialized;
17583 struct var_loc_node *node;
17584 dw_loc_descr_ref descr;
17585 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17586 dw_loc_list_ref list = NULL;
17587 dw_loc_list_ref *listp = &list;
17589 /* Now that we know what section we are using for a base,
17590 actually construct the list of locations.
17591 The first location information is what is passed to the
17592 function that creates the location list, and the remaining
17593 locations just get added on to that list.
17594 Note that we only know the start address for a location
17595 (IE location changes), so to build the range, we use
17596 the range [current location start, next location start].
17597 This means we have to special case the last node, and generate
17598 a range of [last location start, end of function label]. */
17600 if (cfun && crtl->has_bb_partition)
17602 bool save_in_cold_section_p = in_cold_section_p;
17603 in_cold_section_p = first_function_block_is_cold;
17604 if (loc_list->last_before_switch == NULL)
17605 in_cold_section_p = !in_cold_section_p;
17606 secname = secname_for_decl (decl);
17607 in_cold_section_p = save_in_cold_section_p;
17609 else
17610 secname = secname_for_decl (decl);
17612 for (node = loc_list->first; node; node = node->next)
17614 bool range_across_switch = false;
17615 if (GET_CODE (node->loc) == EXPR_LIST
17616 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17618 if (GET_CODE (node->loc) == EXPR_LIST)
17620 descr = NULL;
17621 /* This requires DW_OP_{,bit_}piece, which is not usable
17622 inside DWARF expressions. */
17623 if (want_address == 2)
17624 descr = dw_sra_loc_expr (decl, node->loc);
17626 else
17628 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17629 varloc = NOTE_VAR_LOCATION (node->loc);
17630 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17632 if (descr)
17634 /* If section switch happens in between node->label
17635 and node->next->label (or end of function) and
17636 we can't emit it as a single entry list,
17637 emit two ranges, first one ending at the end
17638 of first partition and second one starting at the
17639 beginning of second partition. */
17640 if (node == loc_list->last_before_switch
17641 && (node != loc_list->first || loc_list->first->next
17642 /* If we are to emit a view number, we will emit
17643 a loclist rather than a single location
17644 expression for the entire function (see
17645 loc_list_has_views), so we have to split the
17646 range that straddles across partitions. */
17647 || !ZERO_VIEW_P (node->view))
17648 && current_function_decl)
17650 endname = cfun->fde->dw_fde_end;
17651 endview = 0;
17652 range_across_switch = true;
17654 /* The variable has a location between NODE->LABEL and
17655 NODE->NEXT->LABEL. */
17656 else if (node->next)
17657 endname = node->next->label, endview = node->next->view;
17658 /* If the variable has a location at the last label
17659 it keeps its location until the end of function. */
17660 else if (!current_function_decl)
17661 endname = text_end_label, endview = 0;
17662 else
17664 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17665 current_function_funcdef_no);
17666 endname = ggc_strdup (label_id);
17667 endview = 0;
17670 *listp = new_loc_list (descr, node->label, node->view,
17671 endname, endview, secname);
17672 if (TREE_CODE (decl) == PARM_DECL
17673 && node == loc_list->first
17674 && NOTE_P (node->loc)
17675 && strcmp (node->label, endname) == 0)
17676 (*listp)->force = true;
17677 listp = &(*listp)->dw_loc_next;
17681 if (cfun
17682 && crtl->has_bb_partition
17683 && node == loc_list->last_before_switch)
17685 bool save_in_cold_section_p = in_cold_section_p;
17686 in_cold_section_p = !first_function_block_is_cold;
17687 secname = secname_for_decl (decl);
17688 in_cold_section_p = save_in_cold_section_p;
17691 if (range_across_switch)
17693 if (GET_CODE (node->loc) == EXPR_LIST)
17694 descr = dw_sra_loc_expr (decl, node->loc);
17695 else
17697 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17698 varloc = NOTE_VAR_LOCATION (node->loc);
17699 descr = dw_loc_list_1 (decl, varloc, want_address,
17700 initialized);
17702 gcc_assert (descr);
17703 /* The variable has a location between NODE->LABEL and
17704 NODE->NEXT->LABEL. */
17705 if (node->next)
17706 endname = node->next->label, endview = node->next->view;
17707 else
17708 endname = cfun->fde->dw_fde_second_end, endview = 0;
17709 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17710 endname, endview, secname);
17711 listp = &(*listp)->dw_loc_next;
17715 /* Try to avoid the overhead of a location list emitting a location
17716 expression instead, but only if we didn't have more than one
17717 location entry in the first place. If some entries were not
17718 representable, we don't want to pretend a single entry that was
17719 applies to the entire scope in which the variable is
17720 available. */
17721 if (list && loc_list->first->next)
17722 gen_llsym (list);
17723 else
17724 maybe_gen_llsym (list);
17726 return list;
17729 /* Return if the loc_list has only single element and thus can be represented
17730 as location description. */
17732 static bool
17733 single_element_loc_list_p (dw_loc_list_ref list)
17735 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17736 return !list->ll_symbol;
17739 /* Duplicate a single element of location list. */
17741 static inline dw_loc_descr_ref
17742 copy_loc_descr (dw_loc_descr_ref ref)
17744 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17745 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17746 return copy;
17749 /* To each location in list LIST append loc descr REF. */
17751 static void
17752 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17754 dw_loc_descr_ref copy;
17755 add_loc_descr (&list->expr, ref);
17756 list = list->dw_loc_next;
17757 while (list)
17759 copy = copy_loc_descr (ref);
17760 add_loc_descr (&list->expr, copy);
17761 while (copy->dw_loc_next)
17762 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17763 list = list->dw_loc_next;
17767 /* To each location in list LIST prepend loc descr REF. */
17769 static void
17770 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17772 dw_loc_descr_ref copy;
17773 dw_loc_descr_ref ref_end = list->expr;
17774 add_loc_descr (&ref, list->expr);
17775 list->expr = ref;
17776 list = list->dw_loc_next;
17777 while (list)
17779 dw_loc_descr_ref end = list->expr;
17780 list->expr = copy = copy_loc_descr (ref);
17781 while (copy->dw_loc_next != ref_end)
17782 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17783 copy->dw_loc_next = end;
17784 list = list->dw_loc_next;
17788 /* Given two lists RET and LIST
17789 produce location list that is result of adding expression in LIST
17790 to expression in RET on each position in program.
17791 Might be destructive on both RET and LIST.
17793 TODO: We handle only simple cases of RET or LIST having at most one
17794 element. General case would involve sorting the lists in program order
17795 and merging them that will need some additional work.
17796 Adding that will improve quality of debug info especially for SRA-ed
17797 structures. */
17799 static void
17800 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17802 if (!list)
17803 return;
17804 if (!*ret)
17806 *ret = list;
17807 return;
17809 if (!list->dw_loc_next)
17811 add_loc_descr_to_each (*ret, list->expr);
17812 return;
17814 if (!(*ret)->dw_loc_next)
17816 prepend_loc_descr_to_each (list, (*ret)->expr);
17817 *ret = list;
17818 return;
17820 expansion_failed (NULL_TREE, NULL_RTX,
17821 "Don't know how to merge two non-trivial"
17822 " location lists.\n");
17823 *ret = NULL;
17824 return;
17827 /* LOC is constant expression. Try a luck, look it up in constant
17828 pool and return its loc_descr of its address. */
17830 static dw_loc_descr_ref
17831 cst_pool_loc_descr (tree loc)
17833 /* Get an RTL for this, if something has been emitted. */
17834 rtx rtl = lookup_constant_def (loc);
17836 if (!rtl || !MEM_P (rtl))
17838 gcc_assert (!rtl);
17839 return 0;
17841 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17843 /* TODO: We might get more coverage if we was actually delaying expansion
17844 of all expressions till end of compilation when constant pools are fully
17845 populated. */
17846 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17848 expansion_failed (loc, NULL_RTX,
17849 "CST value in contant pool but not marked.");
17850 return 0;
17852 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17853 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17856 /* Return dw_loc_list representing address of addr_expr LOC
17857 by looking for inner INDIRECT_REF expression and turning
17858 it into simple arithmetics.
17860 See loc_list_from_tree for the meaning of CONTEXT. */
17862 static dw_loc_list_ref
17863 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17864 loc_descr_context *context)
17866 tree obj, offset;
17867 poly_int64 bitsize, bitpos, bytepos;
17868 machine_mode mode;
17869 int unsignedp, reversep, volatilep = 0;
17870 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17872 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17873 &bitsize, &bitpos, &offset, &mode,
17874 &unsignedp, &reversep, &volatilep);
17875 STRIP_NOPS (obj);
17876 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17878 expansion_failed (loc, NULL_RTX, "bitfield access");
17879 return 0;
17881 if (!INDIRECT_REF_P (obj))
17883 expansion_failed (obj,
17884 NULL_RTX, "no indirect ref in inner refrence");
17885 return 0;
17887 if (!offset && known_eq (bitpos, 0))
17888 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17889 context);
17890 else if (toplev
17891 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17892 && (dwarf_version >= 4 || !dwarf_strict))
17894 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17895 if (!list_ret)
17896 return 0;
17897 if (offset)
17899 /* Variable offset. */
17900 list_ret1 = loc_list_from_tree (offset, 0, context);
17901 if (list_ret1 == 0)
17902 return 0;
17903 add_loc_list (&list_ret, list_ret1);
17904 if (!list_ret)
17905 return 0;
17906 add_loc_descr_to_each (list_ret,
17907 new_loc_descr (DW_OP_plus, 0, 0));
17909 HOST_WIDE_INT value;
17910 if (bytepos.is_constant (&value) && value > 0)
17911 add_loc_descr_to_each (list_ret,
17912 new_loc_descr (DW_OP_plus_uconst, value, 0));
17913 else if (maybe_ne (bytepos, 0))
17914 loc_list_plus_const (list_ret, bytepos);
17915 add_loc_descr_to_each (list_ret,
17916 new_loc_descr (DW_OP_stack_value, 0, 0));
17918 return list_ret;
17921 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17922 all operations from LOC are nops, move to the last one. Insert in NOPS all
17923 operations that are skipped. */
17925 static void
17926 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17927 hash_set<dw_loc_descr_ref> &nops)
17929 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17931 nops.add (loc);
17932 loc = loc->dw_loc_next;
17936 /* Helper for loc_descr_without_nops: free the location description operation
17937 P. */
17939 bool
17940 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17942 ggc_free (loc);
17943 return true;
17946 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17947 finishes LOC. */
17949 static void
17950 loc_descr_without_nops (dw_loc_descr_ref &loc)
17952 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17953 return;
17955 /* Set of all DW_OP_nop operations we remove. */
17956 hash_set<dw_loc_descr_ref> nops;
17958 /* First, strip all prefix NOP operations in order to keep the head of the
17959 operations list. */
17960 loc_descr_to_next_no_nop (loc, nops);
17962 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17964 /* For control flow operations: strip "prefix" nops in destination
17965 labels. */
17966 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17967 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17968 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17969 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17971 /* Do the same for the operations that follow, then move to the next
17972 iteration. */
17973 if (cur->dw_loc_next != NULL)
17974 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17975 cur = cur->dw_loc_next;
17978 nops.traverse<void *, free_loc_descr> (NULL);
17982 struct dwarf_procedure_info;
17984 /* Helper structure for location descriptions generation. */
17985 struct loc_descr_context
17987 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17988 NULL_TREE if DW_OP_push_object_address in invalid for this location
17989 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17990 tree context_type;
17991 /* The ..._DECL node that should be translated as a
17992 DW_OP_push_object_address operation. */
17993 tree base_decl;
17994 /* Information about the DWARF procedure we are currently generating. NULL if
17995 we are not generating a DWARF procedure. */
17996 struct dwarf_procedure_info *dpi;
17997 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17998 by consumer. Used for DW_TAG_generic_subrange attributes. */
17999 bool placeholder_arg;
18000 /* True if PLACEHOLDER_EXPR has been seen. */
18001 bool placeholder_seen;
18002 /* True if strict preservation of signedness has been requested. */
18003 bool strict_signedness;
18006 /* DWARF procedures generation
18008 DWARF expressions (aka. location descriptions) are used to encode variable
18009 things such as sizes or offsets. Such computations can have redundant parts
18010 that can be factorized in order to reduce the size of the output debug
18011 information. This is the whole point of DWARF procedures.
18013 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
18014 already factorized into functions ("size functions") in order to handle very
18015 big and complex types. Such functions are quite simple: they have integral
18016 arguments, they return an integral result and their body contains only a
18017 return statement with arithmetic expressions. This is the only kind of
18018 function we are interested in translating into DWARF procedures, here.
18020 DWARF expressions and DWARF procedure are executed using a stack, so we have
18021 to define some calling convention for them to interact. Let's say that:
18023 - Before calling a DWARF procedure, DWARF expressions must push on the stack
18024 all arguments in reverse order (right-to-left) so that when the DWARF
18025 procedure execution starts, the first argument is the top of the stack.
18027 - Then, when returning, the DWARF procedure must have consumed all arguments
18028 on the stack, must have pushed the result and touched nothing else.
18030 - Each integral argument and the result are integral types can be hold in a
18031 single stack slot.
18033 - We call "frame offset" the number of stack slots that are "under DWARF
18034 procedure control": it includes the arguments slots, the temporaries and
18035 the result slot. Thus, it is equal to the number of arguments when the
18036 procedure execution starts and must be equal to one (the result) when it
18037 returns. */
18039 /* Helper structure used when generating operations for a DWARF procedure. */
18040 struct dwarf_procedure_info
18042 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
18043 currently translated. */
18044 tree fndecl;
18045 /* The number of arguments FNDECL takes. */
18046 unsigned args_count;
18049 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
18050 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
18051 equate it to this DIE. */
18053 static dw_die_ref
18054 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
18055 dw_die_ref parent_die)
18057 dw_die_ref dwarf_proc_die;
18059 if ((dwarf_version < 3 && dwarf_strict)
18060 || location == NULL)
18061 return NULL;
18063 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
18064 if (fndecl)
18065 equate_decl_number_to_die (fndecl, dwarf_proc_die);
18066 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
18067 return dwarf_proc_die;
18070 /* Return whether TYPE is a supported type as a DWARF procedure argument
18071 type or return type (we handle only scalar types and pointer types that
18072 aren't wider than the DWARF expression evaluation stack). */
18074 static bool
18075 is_handled_procedure_type (tree type)
18077 return ((INTEGRAL_TYPE_P (type)
18078 || TREE_CODE (type) == OFFSET_TYPE
18079 || TREE_CODE (type) == POINTER_TYPE)
18080 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
18083 /* Helper for resolve_args_picking: do the same but stop when coming across
18084 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
18085 offset *before* evaluating the corresponding operation. */
18087 static bool
18088 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18089 struct dwarf_procedure_info *dpi,
18090 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
18092 /* The "frame_offset" identifier is already used to name a macro... */
18093 unsigned frame_offset_ = initial_frame_offset;
18094 dw_loc_descr_ref l;
18096 for (l = loc; l != NULL;)
18098 bool existed;
18099 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
18101 /* If we already met this node, there is nothing to compute anymore. */
18102 if (existed)
18104 /* Make sure that the stack size is consistent wherever the execution
18105 flow comes from. */
18106 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
18107 break;
18109 l_frame_offset = frame_offset_;
18111 /* If needed, relocate the picking offset with respect to the frame
18112 offset. */
18113 if (l->frame_offset_rel)
18115 unsigned HOST_WIDE_INT off;
18116 switch (l->dw_loc_opc)
18118 case DW_OP_pick:
18119 off = l->dw_loc_oprnd1.v.val_unsigned;
18120 break;
18121 case DW_OP_dup:
18122 off = 0;
18123 break;
18124 case DW_OP_over:
18125 off = 1;
18126 break;
18127 default:
18128 gcc_unreachable ();
18130 /* frame_offset_ is the size of the current stack frame, including
18131 incoming arguments. Besides, the arguments are pushed
18132 right-to-left. Thus, in order to access the Nth argument from
18133 this operation node, the picking has to skip temporaries *plus*
18134 one stack slot per argument (0 for the first one, 1 for the second
18135 one, etc.).
18137 The targetted argument number (N) is already set as the operand,
18138 and the number of temporaries can be computed with:
18139 frame_offsets_ - dpi->args_count */
18140 off += frame_offset_ - dpi->args_count;
18142 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
18143 if (off > 255)
18144 return false;
18146 if (off == 0)
18148 l->dw_loc_opc = DW_OP_dup;
18149 l->dw_loc_oprnd1.v.val_unsigned = 0;
18151 else if (off == 1)
18153 l->dw_loc_opc = DW_OP_over;
18154 l->dw_loc_oprnd1.v.val_unsigned = 0;
18156 else
18158 l->dw_loc_opc = DW_OP_pick;
18159 l->dw_loc_oprnd1.v.val_unsigned = off;
18163 /* Update frame_offset according to the effect the current operation has
18164 on the stack. */
18165 switch (l->dw_loc_opc)
18167 case DW_OP_deref:
18168 case DW_OP_swap:
18169 case DW_OP_rot:
18170 case DW_OP_abs:
18171 case DW_OP_neg:
18172 case DW_OP_not:
18173 case DW_OP_plus_uconst:
18174 case DW_OP_skip:
18175 case DW_OP_reg0:
18176 case DW_OP_reg1:
18177 case DW_OP_reg2:
18178 case DW_OP_reg3:
18179 case DW_OP_reg4:
18180 case DW_OP_reg5:
18181 case DW_OP_reg6:
18182 case DW_OP_reg7:
18183 case DW_OP_reg8:
18184 case DW_OP_reg9:
18185 case DW_OP_reg10:
18186 case DW_OP_reg11:
18187 case DW_OP_reg12:
18188 case DW_OP_reg13:
18189 case DW_OP_reg14:
18190 case DW_OP_reg15:
18191 case DW_OP_reg16:
18192 case DW_OP_reg17:
18193 case DW_OP_reg18:
18194 case DW_OP_reg19:
18195 case DW_OP_reg20:
18196 case DW_OP_reg21:
18197 case DW_OP_reg22:
18198 case DW_OP_reg23:
18199 case DW_OP_reg24:
18200 case DW_OP_reg25:
18201 case DW_OP_reg26:
18202 case DW_OP_reg27:
18203 case DW_OP_reg28:
18204 case DW_OP_reg29:
18205 case DW_OP_reg30:
18206 case DW_OP_reg31:
18207 case DW_OP_bregx:
18208 case DW_OP_piece:
18209 case DW_OP_deref_size:
18210 case DW_OP_nop:
18211 case DW_OP_bit_piece:
18212 case DW_OP_implicit_value:
18213 case DW_OP_stack_value:
18214 case DW_OP_deref_type:
18215 case DW_OP_convert:
18216 case DW_OP_reinterpret:
18217 case DW_OP_GNU_deref_type:
18218 case DW_OP_GNU_convert:
18219 case DW_OP_GNU_reinterpret:
18220 break;
18222 case DW_OP_addr:
18223 case DW_OP_const1u:
18224 case DW_OP_const1s:
18225 case DW_OP_const2u:
18226 case DW_OP_const2s:
18227 case DW_OP_const4u:
18228 case DW_OP_const4s:
18229 case DW_OP_const8u:
18230 case DW_OP_const8s:
18231 case DW_OP_constu:
18232 case DW_OP_consts:
18233 case DW_OP_dup:
18234 case DW_OP_over:
18235 case DW_OP_pick:
18236 case DW_OP_lit0:
18237 case DW_OP_lit1:
18238 case DW_OP_lit2:
18239 case DW_OP_lit3:
18240 case DW_OP_lit4:
18241 case DW_OP_lit5:
18242 case DW_OP_lit6:
18243 case DW_OP_lit7:
18244 case DW_OP_lit8:
18245 case DW_OP_lit9:
18246 case DW_OP_lit10:
18247 case DW_OP_lit11:
18248 case DW_OP_lit12:
18249 case DW_OP_lit13:
18250 case DW_OP_lit14:
18251 case DW_OP_lit15:
18252 case DW_OP_lit16:
18253 case DW_OP_lit17:
18254 case DW_OP_lit18:
18255 case DW_OP_lit19:
18256 case DW_OP_lit20:
18257 case DW_OP_lit21:
18258 case DW_OP_lit22:
18259 case DW_OP_lit23:
18260 case DW_OP_lit24:
18261 case DW_OP_lit25:
18262 case DW_OP_lit26:
18263 case DW_OP_lit27:
18264 case DW_OP_lit28:
18265 case DW_OP_lit29:
18266 case DW_OP_lit30:
18267 case DW_OP_lit31:
18268 case DW_OP_breg0:
18269 case DW_OP_breg1:
18270 case DW_OP_breg2:
18271 case DW_OP_breg3:
18272 case DW_OP_breg4:
18273 case DW_OP_breg5:
18274 case DW_OP_breg6:
18275 case DW_OP_breg7:
18276 case DW_OP_breg8:
18277 case DW_OP_breg9:
18278 case DW_OP_breg10:
18279 case DW_OP_breg11:
18280 case DW_OP_breg12:
18281 case DW_OP_breg13:
18282 case DW_OP_breg14:
18283 case DW_OP_breg15:
18284 case DW_OP_breg16:
18285 case DW_OP_breg17:
18286 case DW_OP_breg18:
18287 case DW_OP_breg19:
18288 case DW_OP_breg20:
18289 case DW_OP_breg21:
18290 case DW_OP_breg22:
18291 case DW_OP_breg23:
18292 case DW_OP_breg24:
18293 case DW_OP_breg25:
18294 case DW_OP_breg26:
18295 case DW_OP_breg27:
18296 case DW_OP_breg28:
18297 case DW_OP_breg29:
18298 case DW_OP_breg30:
18299 case DW_OP_breg31:
18300 case DW_OP_fbreg:
18301 case DW_OP_push_object_address:
18302 case DW_OP_call_frame_cfa:
18303 case DW_OP_GNU_variable_value:
18304 case DW_OP_GNU_addr_index:
18305 case DW_OP_GNU_const_index:
18306 ++frame_offset_;
18307 break;
18309 case DW_OP_drop:
18310 case DW_OP_xderef:
18311 case DW_OP_and:
18312 case DW_OP_div:
18313 case DW_OP_minus:
18314 case DW_OP_mod:
18315 case DW_OP_mul:
18316 case DW_OP_or:
18317 case DW_OP_plus:
18318 case DW_OP_shl:
18319 case DW_OP_shr:
18320 case DW_OP_shra:
18321 case DW_OP_xor:
18322 case DW_OP_bra:
18323 case DW_OP_eq:
18324 case DW_OP_ge:
18325 case DW_OP_gt:
18326 case DW_OP_le:
18327 case DW_OP_lt:
18328 case DW_OP_ne:
18329 case DW_OP_regx:
18330 case DW_OP_xderef_size:
18331 --frame_offset_;
18332 break;
18334 case DW_OP_call2:
18335 case DW_OP_call4:
18336 case DW_OP_call_ref:
18338 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
18339 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
18341 if (stack_usage == NULL)
18342 return false;
18343 frame_offset_ += *stack_usage;
18344 break;
18347 case DW_OP_implicit_pointer:
18348 case DW_OP_entry_value:
18349 case DW_OP_const_type:
18350 case DW_OP_regval_type:
18351 case DW_OP_form_tls_address:
18352 case DW_OP_GNU_push_tls_address:
18353 case DW_OP_GNU_uninit:
18354 case DW_OP_GNU_encoded_addr:
18355 case DW_OP_GNU_implicit_pointer:
18356 case DW_OP_GNU_entry_value:
18357 case DW_OP_GNU_const_type:
18358 case DW_OP_GNU_regval_type:
18359 case DW_OP_GNU_parameter_ref:
18360 /* loc_list_from_tree will probably not output these operations for
18361 size functions, so assume they will not appear here. */
18362 /* Fall through... */
18364 default:
18365 gcc_unreachable ();
18368 /* Now, follow the control flow (except subroutine calls). */
18369 switch (l->dw_loc_opc)
18371 case DW_OP_bra:
18372 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
18373 frame_offsets))
18374 return false;
18375 /* Fall through. */
18377 case DW_OP_skip:
18378 l = l->dw_loc_oprnd1.v.val_loc;
18379 break;
18381 case DW_OP_stack_value:
18382 return true;
18384 default:
18385 l = l->dw_loc_next;
18386 break;
18390 return true;
18393 /* Make a DFS over operations reachable through LOC (i.e. follow branch
18394 operations) in order to resolve the operand of DW_OP_pick operations that
18395 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
18396 offset *before* LOC is executed. Return if all relocations were
18397 successful. */
18399 static bool
18400 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
18401 struct dwarf_procedure_info *dpi)
18403 /* Associate to all visited operations the frame offset *before* evaluating
18404 this operation. */
18405 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
18407 return
18408 resolve_args_picking_1 (loc, initial_frame_offset, dpi, frame_offsets);
18411 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
18412 Return NULL if it is not possible. */
18414 static dw_die_ref
18415 function_to_dwarf_procedure (tree fndecl)
18417 struct dwarf_procedure_info dpi;
18418 struct loc_descr_context ctx = {
18419 NULL_TREE, /* context_type */
18420 NULL_TREE, /* base_decl */
18421 &dpi, /* dpi */
18422 false, /* placeholder_arg */
18423 false, /* placeholder_seen */
18424 true /* strict_signedness */
18426 dw_die_ref dwarf_proc_die;
18427 tree tree_body = DECL_SAVED_TREE (fndecl);
18428 dw_loc_descr_ref loc_body, epilogue;
18430 tree cursor;
18431 unsigned i;
18433 /* Do not generate multiple DWARF procedures for the same function
18434 declaration. */
18435 dwarf_proc_die = lookup_decl_die (fndecl);
18436 if (dwarf_proc_die != NULL)
18437 return dwarf_proc_die;
18439 /* DWARF procedures are available starting with the DWARFv3 standard. */
18440 if (dwarf_version < 3 && dwarf_strict)
18441 return NULL;
18443 /* We handle only functions for which we still have a body, that return a
18444 supported type and that takes arguments with supported types. Note that
18445 there is no point translating functions that return nothing. */
18446 if (tree_body == NULL_TREE
18447 || DECL_RESULT (fndecl) == NULL_TREE
18448 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
18449 return NULL;
18451 for (cursor = DECL_ARGUMENTS (fndecl);
18452 cursor != NULL_TREE;
18453 cursor = TREE_CHAIN (cursor))
18454 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
18455 return NULL;
18457 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
18458 if (TREE_CODE (tree_body) != RETURN_EXPR)
18459 return NULL;
18460 tree_body = TREE_OPERAND (tree_body, 0);
18461 if (TREE_CODE (tree_body) != MODIFY_EXPR
18462 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
18463 return NULL;
18464 tree_body = TREE_OPERAND (tree_body, 1);
18466 /* Try to translate the body expression itself. Note that this will probably
18467 cause an infinite recursion if its call graph has a cycle. This is very
18468 unlikely for size functions, however, so don't bother with such things at
18469 the moment. */
18470 dpi.fndecl = fndecl;
18471 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
18472 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
18473 if (!loc_body)
18474 return NULL;
18476 /* After evaluating all operands in "loc_body", we should still have on the
18477 stack all arguments plus the desired function result (top of the stack).
18478 Generate code in order to keep only the result in our stack frame. */
18479 epilogue = NULL;
18480 for (i = 0; i < dpi.args_count; ++i)
18482 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
18483 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
18484 op_couple->dw_loc_next->dw_loc_next = epilogue;
18485 epilogue = op_couple;
18487 add_loc_descr (&loc_body, epilogue);
18488 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
18489 return NULL;
18491 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
18492 because they are considered useful. Now there is an epilogue, they are
18493 not anymore, so give it another try. */
18494 loc_descr_without_nops (loc_body);
18496 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
18497 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
18498 though, given that size functions do not come from source, so they should
18499 not have a dedicated DW_TAG_subprogram DIE. */
18500 dwarf_proc_die
18501 = new_dwarf_proc_die (loc_body, fndecl,
18502 get_context_die (DECL_CONTEXT (fndecl)));
18504 /* The called DWARF procedure consumes one stack slot per argument and
18505 returns one stack slot. */
18506 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
18508 return dwarf_proc_die;
18511 /* Helper function for loc_list_from_tree. Perform OP binary op,
18512 but after converting arguments to type_die, afterwards convert
18513 back to unsigned. */
18515 static dw_loc_list_ref
18516 typed_binop_from_tree (enum dwarf_location_atom op, tree loc,
18517 dw_die_ref type_die, scalar_int_mode mode,
18518 struct loc_descr_context *context)
18520 dw_loc_list_ref op0, op1;
18521 dw_loc_descr_ref cvt, binop;
18523 if (type_die == NULL)
18524 return NULL;
18526 op0 = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18527 op1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18528 if (op0 == NULL || op1 == NULL)
18529 return NULL;
18531 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18532 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18533 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18534 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18535 add_loc_descr_to_each (op0, cvt);
18537 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
18538 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18539 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
18540 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
18541 add_loc_descr_to_each (op1, cvt);
18543 add_loc_list (&op0, op1);
18544 if (op0 == NULL)
18545 return NULL;
18547 binop = new_loc_descr (op, 0, 0);
18548 convert_descriptor_to_mode (mode, binop);
18549 add_loc_descr_to_each (op0, binop);
18551 return op0;
18554 /* Generate Dwarf location list representing LOC.
18555 If WANT_ADDRESS is false, expression computing LOC will be computed
18556 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
18557 if WANT_ADDRESS is 2, expression computing address useable in location
18558 will be returned (i.e. DW_OP_reg can be used
18559 to refer to register values).
18561 CONTEXT provides information to customize the location descriptions
18562 generation. Its context_type field specifies what type is implicitly
18563 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
18564 will not be generated.
18566 Its DPI field determines whether we are generating a DWARF expression for a
18567 DWARF procedure, so PARM_DECL references are processed specifically.
18569 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
18570 and dpi fields were null. */
18572 static dw_loc_list_ref
18573 loc_list_from_tree_1 (tree loc, int want_address,
18574 struct loc_descr_context *context)
18576 dw_loc_descr_ref ret = NULL, ret1 = NULL;
18577 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
18578 int have_address = 0;
18579 enum dwarf_location_atom op;
18581 /* ??? Most of the time we do not take proper care for sign/zero
18582 extending the values properly. Hopefully this won't be a real
18583 problem... */
18585 if (context != NULL
18586 && context->base_decl == loc
18587 && want_address == 0)
18589 if (dwarf_version >= 3 || !dwarf_strict)
18590 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18591 NULL, 0, NULL, 0, NULL);
18592 else
18593 return NULL;
18596 switch (TREE_CODE (loc))
18598 case ERROR_MARK:
18599 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18600 return 0;
18602 case PLACEHOLDER_EXPR:
18603 /* This case involves extracting fields from an object to determine the
18604 position of other fields. It is supposed to appear only as the first
18605 operand of COMPONENT_REF nodes and to reference precisely the type
18606 that the context allows or its enclosing type. */
18607 if (context != NULL
18608 && (TREE_TYPE (loc) == context->context_type
18609 || TREE_TYPE (loc) == TYPE_CONTEXT (context->context_type))
18610 && want_address >= 1)
18612 if (dwarf_version >= 3 || !dwarf_strict)
18614 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18615 have_address = 1;
18616 break;
18618 else
18619 return NULL;
18621 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18622 the single argument passed by consumer. */
18623 else if (context != NULL
18624 && context->placeholder_arg
18625 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18626 && want_address == 0)
18628 ret = new_loc_descr (DW_OP_pick, 0, 0);
18629 ret->frame_offset_rel = 1;
18630 context->placeholder_seen = true;
18631 break;
18633 else
18634 expansion_failed (loc, NULL_RTX,
18635 "PLACEHOLDER_EXPR for an unexpected type");
18636 break;
18638 case CALL_EXPR:
18640 tree callee = get_callee_fndecl (loc);
18641 dw_die_ref dwarf_proc;
18643 if (callee
18644 && is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee)))
18645 && (dwarf_proc = function_to_dwarf_procedure (callee)))
18647 /* DWARF procedures are used for size functions, which are built
18648 when size expressions contain conditional constructs, so we
18649 request strict preservation of signedness for comparisons. */
18650 bool old_strict_signedness;
18651 if (context)
18653 old_strict_signedness = context->strict_signedness;
18654 context->strict_signedness = true;
18657 /* Evaluate arguments right-to-left so that the first argument
18658 will be the top-most one on the stack. */
18659 for (int i = call_expr_nargs (loc) - 1; i >= 0; --i)
18661 tree arg = CALL_EXPR_ARG (loc, i);
18662 ret1 = loc_descriptor_from_tree (arg, 0, context);
18663 if (!ret1)
18665 expansion_failed (arg, NULL_RTX, "CALL_EXPR argument");
18666 return NULL;
18668 add_loc_descr (&ret, ret1);
18671 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18672 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18673 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18674 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18675 add_loc_descr (&ret, ret1);
18676 if (context)
18677 context->strict_signedness = old_strict_signedness;
18679 else
18680 expansion_failed (loc, NULL_RTX, "CALL_EXPR target");
18681 break;
18684 case PREINCREMENT_EXPR:
18685 case PREDECREMENT_EXPR:
18686 case POSTINCREMENT_EXPR:
18687 case POSTDECREMENT_EXPR:
18688 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18689 /* There are no opcodes for these operations. */
18690 return 0;
18692 case ADDR_EXPR:
18693 /* If we already want an address, see if there is INDIRECT_REF inside
18694 e.g. for &this->field. */
18695 if (want_address)
18697 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18698 (loc, want_address == 2, context);
18699 if (list_ret)
18700 have_address = 1;
18701 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18702 && (ret = cst_pool_loc_descr (loc)))
18703 have_address = 1;
18705 /* Otherwise, process the argument and look for the address. */
18706 if (!list_ret && !ret)
18707 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18708 else
18710 if (want_address)
18711 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18712 return NULL;
18714 break;
18716 case VAR_DECL:
18717 if (DECL_THREAD_LOCAL_P (loc))
18719 rtx rtl;
18720 enum dwarf_location_atom tls_op;
18721 enum dtprel_bool dtprel = dtprel_false;
18723 if (targetm.have_tls)
18725 /* If this is not defined, we have no way to emit the
18726 data. */
18727 if (!targetm.asm_out.output_dwarf_dtprel)
18728 return 0;
18730 /* The way DW_OP_GNU_push_tls_address is specified, we
18731 can only look up addresses of objects in the current
18732 module. We used DW_OP_addr as first op, but that's
18733 wrong, because DW_OP_addr is relocated by the debug
18734 info consumer, while DW_OP_GNU_push_tls_address
18735 operand shouldn't be. */
18736 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18737 return 0;
18738 dtprel = dtprel_true;
18739 /* We check for DWARF 5 here because gdb did not implement
18740 DW_OP_form_tls_address until after 7.12. */
18741 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18742 : DW_OP_GNU_push_tls_address);
18744 else
18746 if (!targetm.emutls.debug_form_tls_address
18747 || !(dwarf_version >= 3 || !dwarf_strict))
18748 return 0;
18749 /* We stuffed the control variable into the DECL_VALUE_EXPR
18750 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18751 no longer appear in gimple code. We used the control
18752 variable in specific so that we could pick it up here. */
18753 loc = DECL_VALUE_EXPR (loc);
18754 tls_op = DW_OP_form_tls_address;
18757 rtl = rtl_for_decl_location (loc);
18758 if (rtl == NULL_RTX)
18759 return 0;
18761 if (!MEM_P (rtl))
18762 return 0;
18763 rtl = XEXP (rtl, 0);
18764 if (! CONSTANT_P (rtl))
18765 return 0;
18767 ret = new_addr_loc_descr (rtl, dtprel);
18768 ret1 = new_loc_descr (tls_op, 0, 0);
18769 add_loc_descr (&ret, ret1);
18771 have_address = 1;
18772 break;
18774 /* FALLTHRU */
18776 case PARM_DECL:
18777 if (context != NULL && context->dpi != NULL
18778 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18780 /* We are generating code for a DWARF procedure and we want to access
18781 one of its arguments: find the appropriate argument offset and let
18782 the resolve_args_picking pass compute the offset that complies
18783 with the stack frame size. */
18784 unsigned i = 0;
18785 tree cursor;
18787 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18788 cursor != NULL_TREE && cursor != loc;
18789 cursor = TREE_CHAIN (cursor), ++i)
18791 /* If we are translating a DWARF procedure, all referenced parameters
18792 must belong to the current function. */
18793 gcc_assert (cursor != NULL_TREE);
18795 ret = new_loc_descr (DW_OP_pick, i, 0);
18796 ret->frame_offset_rel = 1;
18797 break;
18799 /* FALLTHRU */
18801 case RESULT_DECL:
18802 if (DECL_HAS_VALUE_EXPR_P (loc))
18804 tree value_expr = DECL_VALUE_EXPR (loc);
18806 /* Non-local frame structures are DECL_IGNORED_P variables so we need
18807 to wait until they get an RTX in order to reference them. */
18808 if (early_dwarf
18809 && TREE_CODE (value_expr) == COMPONENT_REF
18810 && VAR_P (TREE_OPERAND (value_expr, 0))
18811 && DECL_NONLOCAL_FRAME (TREE_OPERAND (value_expr, 0)))
18813 else
18814 return loc_list_from_tree_1 (value_expr, want_address, context);
18817 /* FALLTHRU */
18819 case FUNCTION_DECL:
18821 rtx rtl;
18822 var_loc_list *loc_list = lookup_decl_loc (loc);
18824 if (loc_list && loc_list->first)
18826 list_ret = dw_loc_list (loc_list, loc, want_address);
18827 have_address = want_address != 0;
18828 break;
18830 rtl = rtl_for_decl_location (loc);
18831 if (rtl == NULL_RTX)
18833 if (TREE_CODE (loc) != FUNCTION_DECL
18834 && early_dwarf
18835 && want_address != 1
18836 && ! DECL_IGNORED_P (loc)
18837 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18838 || POINTER_TYPE_P (TREE_TYPE (loc)))
18839 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18840 <= DWARF2_ADDR_SIZE))
18842 dw_die_ref ref = lookup_decl_die (loc);
18843 if (ref)
18845 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18846 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18847 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18848 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18850 else if (current_function_decl
18851 && DECL_CONTEXT (loc) == current_function_decl)
18853 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18854 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18855 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18857 break;
18859 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18860 return 0;
18862 else if (CONST_INT_P (rtl))
18864 HOST_WIDE_INT val = INTVAL (rtl);
18865 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18866 val &= GET_MODE_MASK (DECL_MODE (loc));
18867 ret = int_loc_descriptor (val);
18869 else if (GET_CODE (rtl) == CONST_STRING)
18871 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18872 return 0;
18874 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18875 ret = new_addr_loc_descr (rtl, dtprel_false);
18876 else
18878 machine_mode mode, mem_mode;
18880 /* Certain constructs can only be represented at top-level. */
18881 if (want_address == 2)
18883 ret = loc_descriptor (rtl, VOIDmode,
18884 VAR_INIT_STATUS_INITIALIZED);
18885 have_address = 1;
18887 else
18889 mode = GET_MODE (rtl);
18890 mem_mode = VOIDmode;
18891 if (MEM_P (rtl))
18893 mem_mode = mode;
18894 mode = get_address_mode (rtl);
18895 rtl = XEXP (rtl, 0);
18896 have_address = 1;
18898 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18899 VAR_INIT_STATUS_INITIALIZED);
18901 if (!ret)
18902 expansion_failed (loc, rtl,
18903 "failed to produce loc descriptor for rtl");
18906 break;
18908 case MEM_REF:
18909 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18911 have_address = 1;
18912 goto do_plus;
18914 /* Fallthru. */
18915 case INDIRECT_REF:
18916 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18917 have_address = 1;
18918 break;
18920 case TARGET_MEM_REF:
18921 case SSA_NAME:
18922 case DEBUG_EXPR_DECL:
18923 return NULL;
18925 case COMPOUND_EXPR:
18926 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18927 context);
18929 CASE_CONVERT:
18930 case VIEW_CONVERT_EXPR:
18931 case SAVE_EXPR:
18932 case MODIFY_EXPR:
18933 case NON_LVALUE_EXPR:
18934 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18935 context);
18937 case COMPONENT_REF:
18938 case BIT_FIELD_REF:
18939 case ARRAY_REF:
18940 case ARRAY_RANGE_REF:
18941 case REALPART_EXPR:
18942 case IMAGPART_EXPR:
18944 tree obj, offset;
18945 poly_int64 bitsize, bitpos, bytepos;
18946 machine_mode mode;
18947 int unsignedp, reversep, volatilep = 0;
18949 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18950 &unsignedp, &reversep, &volatilep);
18952 gcc_assert (obj != loc);
18954 list_ret = loc_list_from_tree_1 (obj,
18955 want_address == 2
18956 && known_eq (bitpos, 0)
18957 && !offset ? 2 : 1,
18958 context);
18959 /* TODO: We can extract value of the small expression via shifting even
18960 for nonzero bitpos. */
18961 if (list_ret == 0)
18962 return 0;
18963 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18964 || !multiple_p (bitsize, BITS_PER_UNIT))
18966 expansion_failed (loc, NULL_RTX,
18967 "bitfield access");
18968 return 0;
18971 if (offset != NULL_TREE)
18973 /* Variable offset. */
18974 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18975 if (list_ret1 == 0)
18976 return 0;
18977 add_loc_list (&list_ret, list_ret1);
18978 if (!list_ret)
18979 return 0;
18980 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18983 HOST_WIDE_INT value;
18984 if (bytepos.is_constant (&value) && value > 0)
18985 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18986 value, 0));
18987 else if (maybe_ne (bytepos, 0))
18988 loc_list_plus_const (list_ret, bytepos);
18990 have_address = 1;
18991 break;
18994 case INTEGER_CST:
18995 if ((want_address || !tree_fits_shwi_p (loc))
18996 && (ret = cst_pool_loc_descr (loc)))
18997 have_address = 1;
18998 else if (want_address == 2
18999 && tree_fits_shwi_p (loc)
19000 && (ret = address_of_int_loc_descriptor
19001 (int_size_in_bytes (TREE_TYPE (loc)),
19002 tree_to_shwi (loc))))
19003 have_address = 1;
19004 else if (tree_fits_shwi_p (loc))
19005 ret = int_loc_descriptor (tree_to_shwi (loc));
19006 else if (tree_fits_uhwi_p (loc))
19007 ret = uint_loc_descriptor (tree_to_uhwi (loc));
19008 else
19010 expansion_failed (loc, NULL_RTX,
19011 "Integer operand is not host integer");
19012 return 0;
19014 break;
19016 case POLY_INT_CST:
19018 if (want_address)
19020 expansion_failed (loc, NULL_RTX,
19021 "constant address with a runtime component");
19022 return 0;
19024 poly_int64 value;
19025 if (!poly_int_tree_p (loc, &value))
19027 expansion_failed (loc, NULL_RTX, "constant too big");
19028 return 0;
19030 ret = int_loc_descriptor (value);
19032 break;
19034 case CONSTRUCTOR:
19035 case REAL_CST:
19036 case STRING_CST:
19037 case COMPLEX_CST:
19038 if ((ret = cst_pool_loc_descr (loc)))
19039 have_address = 1;
19040 else if (TREE_CODE (loc) == CONSTRUCTOR)
19042 tree type = TREE_TYPE (loc);
19043 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
19044 unsigned HOST_WIDE_INT offset = 0;
19045 unsigned HOST_WIDE_INT cnt;
19046 constructor_elt *ce;
19048 if (TREE_CODE (type) == RECORD_TYPE)
19050 /* This is very limited, but it's enough to output
19051 pointers to member functions, as long as the
19052 referenced function is defined in the current
19053 translation unit. */
19054 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
19056 tree val = ce->value;
19058 tree field = ce->index;
19060 if (val)
19061 STRIP_NOPS (val);
19063 if (!field || DECL_BIT_FIELD (field))
19065 expansion_failed (loc, NULL_RTX,
19066 "bitfield in record type constructor");
19067 size = offset = (unsigned HOST_WIDE_INT)-1;
19068 ret = NULL;
19069 break;
19072 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19073 unsigned HOST_WIDE_INT pos = int_byte_position (field);
19074 gcc_assert (pos + fieldsize <= size);
19075 if (pos < offset)
19077 expansion_failed (loc, NULL_RTX,
19078 "out-of-order fields in record constructor");
19079 size = offset = (unsigned HOST_WIDE_INT)-1;
19080 ret = NULL;
19081 break;
19083 if (pos > offset)
19085 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
19086 add_loc_descr (&ret, ret1);
19087 offset = pos;
19089 if (val && fieldsize != 0)
19091 ret1 = loc_descriptor_from_tree (val, want_address, context);
19092 if (!ret1)
19094 expansion_failed (loc, NULL_RTX,
19095 "unsupported expression in field");
19096 size = offset = (unsigned HOST_WIDE_INT)-1;
19097 ret = NULL;
19098 break;
19100 add_loc_descr (&ret, ret1);
19102 if (fieldsize)
19104 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
19105 add_loc_descr (&ret, ret1);
19106 offset = pos + fieldsize;
19110 if (offset != size)
19112 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
19113 add_loc_descr (&ret, ret1);
19114 offset = size;
19117 have_address = !!want_address;
19119 else
19120 expansion_failed (loc, NULL_RTX,
19121 "constructor of non-record type");
19123 else
19124 /* We can construct small constants here using int_loc_descriptor. */
19125 expansion_failed (loc, NULL_RTX,
19126 "constructor or constant not in constant pool");
19127 break;
19129 case TRUTH_AND_EXPR:
19130 case TRUTH_ANDIF_EXPR:
19131 case BIT_AND_EXPR:
19132 op = DW_OP_and;
19133 goto do_binop;
19135 case TRUTH_XOR_EXPR:
19136 case BIT_XOR_EXPR:
19137 op = DW_OP_xor;
19138 goto do_binop;
19140 case TRUTH_OR_EXPR:
19141 case TRUTH_ORIF_EXPR:
19142 case BIT_IOR_EXPR:
19143 op = DW_OP_or;
19144 goto do_binop;
19146 case EXACT_DIV_EXPR:
19147 case FLOOR_DIV_EXPR:
19148 case TRUNC_DIV_EXPR:
19149 /* Turn a divide by a power of 2 into a shift when possible. */
19150 if (TYPE_UNSIGNED (TREE_TYPE (loc))
19151 && tree_fits_uhwi_p (TREE_OPERAND (loc, 1)))
19153 const int log2 = exact_log2 (tree_to_uhwi (TREE_OPERAND (loc, 1)));
19154 if (log2 > 0)
19156 list_ret
19157 = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19158 if (list_ret == 0)
19159 return 0;
19161 add_loc_descr_to_each (list_ret, uint_loc_descriptor (log2));
19162 add_loc_descr_to_each (list_ret,
19163 new_loc_descr (DW_OP_shr, 0, 0));
19164 break;
19168 /* fall through */
19170 case CEIL_DIV_EXPR:
19171 case ROUND_DIV_EXPR:
19172 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19174 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19175 scalar_int_mode int_mode;
19177 if ((dwarf_strict && dwarf_version < 5)
19178 || !is_a <scalar_int_mode> (mode, &int_mode))
19179 return 0;
19181 /* We can use a signed divide if the sign bit is not set. */
19182 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
19184 op = DW_OP_div;
19185 goto do_binop;
19188 list_ret = typed_binop_from_tree (DW_OP_div, loc,
19189 base_type_for_mode (int_mode, 1),
19190 int_mode, context);
19191 break;
19193 op = DW_OP_div;
19194 goto do_binop;
19196 case MINUS_EXPR:
19197 op = DW_OP_minus;
19198 goto do_binop;
19200 case FLOOR_MOD_EXPR:
19201 case CEIL_MOD_EXPR:
19202 case ROUND_MOD_EXPR:
19203 case TRUNC_MOD_EXPR:
19204 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
19206 op = DW_OP_mod;
19207 goto do_binop;
19209 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19210 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19211 if (list_ret == 0 || list_ret1 == 0)
19212 return 0;
19214 add_loc_list (&list_ret, list_ret1);
19215 if (list_ret == 0)
19216 return 0;
19217 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19218 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
19219 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
19220 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
19221 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
19222 break;
19224 case MULT_EXPR:
19225 op = DW_OP_mul;
19226 goto do_binop;
19228 case LSHIFT_EXPR:
19229 op = DW_OP_shl;
19230 goto do_binop;
19232 case RSHIFT_EXPR:
19233 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
19234 goto do_binop;
19236 case POINTER_PLUS_EXPR:
19237 case PLUS_EXPR:
19238 do_plus:
19239 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
19241 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
19242 smarter to encode their opposite. The DW_OP_plus_uconst operation
19243 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
19244 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
19245 bytes, Y being the size of the operation that pushes the opposite
19246 of the addend. So let's choose the smallest representation. */
19247 const tree tree_addend = TREE_OPERAND (loc, 1);
19248 offset_int wi_addend;
19249 HOST_WIDE_INT shwi_addend;
19250 dw_loc_descr_ref loc_naddend;
19252 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19253 if (list_ret == 0)
19254 return 0;
19256 /* Try to get the literal to push. It is the opposite of the addend,
19257 so as we rely on wrapping during DWARF evaluation, first decode
19258 the literal as a "DWARF-sized" signed number. */
19259 wi_addend = wi::to_offset (tree_addend);
19260 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
19261 shwi_addend = wi_addend.to_shwi ();
19262 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
19263 ? int_loc_descriptor (-shwi_addend)
19264 : NULL;
19266 if (loc_naddend != NULL
19267 && ((unsigned) size_of_uleb128 (shwi_addend)
19268 > size_of_loc_descr (loc_naddend)))
19270 add_loc_descr_to_each (list_ret, loc_naddend);
19271 add_loc_descr_to_each (list_ret,
19272 new_loc_descr (DW_OP_minus, 0, 0));
19274 else
19276 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
19278 loc_naddend = loc_cur;
19279 loc_cur = loc_cur->dw_loc_next;
19280 ggc_free (loc_naddend);
19282 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
19284 break;
19287 op = DW_OP_plus;
19288 goto do_binop;
19290 case LE_EXPR:
19291 op = DW_OP_le;
19292 goto do_comp_binop;
19294 case GE_EXPR:
19295 op = DW_OP_ge;
19296 goto do_comp_binop;
19298 case LT_EXPR:
19299 op = DW_OP_lt;
19300 goto do_comp_binop;
19302 case GT_EXPR:
19303 op = DW_OP_gt;
19304 goto do_comp_binop;
19306 do_comp_binop:
19307 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
19309 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
19310 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
19311 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
19312 TREE_CODE (loc));
19313 break;
19315 else
19316 goto do_binop;
19318 case EQ_EXPR:
19319 op = DW_OP_eq;
19320 goto do_binop;
19322 case NE_EXPR:
19323 op = DW_OP_ne;
19324 goto do_binop;
19326 do_binop:
19327 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19328 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
19329 if (list_ret == 0 || list_ret1 == 0)
19330 return 0;
19332 add_loc_list (&list_ret, list_ret1);
19333 if (list_ret == 0)
19334 return 0;
19335 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19336 break;
19338 case TRUTH_NOT_EXPR:
19339 case BIT_NOT_EXPR:
19340 op = DW_OP_not;
19341 goto do_unop;
19343 case ABS_EXPR:
19344 op = DW_OP_abs;
19345 goto do_unop;
19347 case NEGATE_EXPR:
19348 op = DW_OP_neg;
19349 goto do_unop;
19351 do_unop:
19352 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19353 if (list_ret == 0)
19354 return 0;
19356 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
19357 break;
19359 case MIN_EXPR:
19360 case MAX_EXPR:
19362 const enum tree_code code =
19363 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
19365 loc = build3 (COND_EXPR, TREE_TYPE (loc),
19366 build2 (code, integer_type_node,
19367 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
19368 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
19371 /* fall through */
19373 case COND_EXPR:
19375 dw_loc_descr_ref lhs
19376 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
19377 dw_loc_list_ref rhs
19378 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
19379 dw_loc_descr_ref bra_node, jump_node, tmp;
19381 /* DW_OP_bra is branch-on-nonzero so avoid doing useless work. */
19382 if (TREE_CODE (TREE_OPERAND (loc, 0)) == NE_EXPR
19383 && integer_zerop (TREE_OPERAND (TREE_OPERAND (loc, 0), 1)))
19384 list_ret
19385 = loc_list_from_tree_1 (TREE_OPERAND (TREE_OPERAND (loc, 0), 0),
19386 0, context);
19387 else
19388 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
19389 if (list_ret == 0 || lhs == 0 || rhs == 0)
19390 return 0;
19392 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
19393 add_loc_descr_to_each (list_ret, bra_node);
19395 add_loc_list (&list_ret, rhs);
19396 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
19397 add_loc_descr_to_each (list_ret, jump_node);
19399 add_loc_descr_to_each (list_ret, lhs);
19400 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19401 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
19403 /* ??? Need a node to point the skip at. Use a nop. */
19404 tmp = new_loc_descr (DW_OP_nop, 0, 0);
19405 add_loc_descr_to_each (list_ret, tmp);
19406 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
19407 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
19409 break;
19411 case FIX_TRUNC_EXPR:
19412 return 0;
19414 case COMPOUND_LITERAL_EXPR:
19415 return loc_list_from_tree_1 (COMPOUND_LITERAL_EXPR_DECL (loc),
19416 0, context);
19418 default:
19419 /* Leave front-end specific codes as simply unknown. This comes
19420 up, for instance, with the C STMT_EXPR. */
19421 if ((unsigned int) TREE_CODE (loc)
19422 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
19424 expansion_failed (loc, NULL_RTX,
19425 "language specific tree node");
19426 return 0;
19429 /* Otherwise this is a generic code; we should just lists all of
19430 these explicitly. We forgot one. */
19431 if (flag_checking)
19432 gcc_unreachable ();
19434 /* In a release build, we want to degrade gracefully: better to
19435 generate incomplete debugging information than to crash. */
19436 return NULL;
19439 if (!ret && !list_ret)
19440 return 0;
19442 if (want_address == 2 && !have_address
19443 && (dwarf_version >= 4 || !dwarf_strict))
19445 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
19447 expansion_failed (loc, NULL_RTX,
19448 "DWARF address size mismatch");
19449 return 0;
19451 if (ret)
19452 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
19453 else
19454 add_loc_descr_to_each (list_ret,
19455 new_loc_descr (DW_OP_stack_value, 0, 0));
19456 have_address = 1;
19458 /* Show if we can't fill the request for an address. */
19459 if (want_address && !have_address)
19461 expansion_failed (loc, NULL_RTX,
19462 "Want address and only have value");
19463 return 0;
19466 gcc_assert (!ret || !list_ret);
19468 /* If we've got an address and don't want one, dereference. */
19469 if (!want_address && have_address)
19471 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
19472 enum machine_mode mode = TYPE_MODE (TREE_TYPE (loc));
19473 scalar_int_mode int_mode;
19474 dw_die_ref type_die;
19475 dw_loc_descr_ref deref;
19477 /* If the size is greater than DWARF2_ADDR_SIZE, bail out. */
19478 if (size > DWARF2_ADDR_SIZE || size == -1)
19480 expansion_failed (loc, NULL_RTX,
19481 "DWARF address size mismatch");
19482 return 0;
19485 /* If it is equal to DWARF2_ADDR_SIZE, extension does not matter. */
19486 else if (size == DWARF2_ADDR_SIZE)
19487 deref = new_loc_descr (DW_OP_deref, size, 0);
19489 /* If it is lower than DWARF2_ADDR_SIZE, DW_OP_deref_size will zero-
19490 extend the value, which is really OK for unsigned types only. */
19491 else if (!(context && context->strict_signedness)
19492 || TYPE_UNSIGNED (TREE_TYPE (loc))
19493 || (dwarf_strict && dwarf_version < 5)
19494 || !is_a <scalar_int_mode> (mode, &int_mode)
19495 || !(type_die = base_type_for_mode (mode, false)))
19496 deref = new_loc_descr (DW_OP_deref_size, size, 0);
19498 /* Use DW_OP_deref_type for signed integral types if possible, but
19499 convert back to the generic type to avoid type mismatches later. */
19500 else
19502 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
19503 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
19504 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
19505 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
19506 add_loc_descr (&deref,
19507 new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
19510 if (ret)
19511 add_loc_descr (&ret, deref);
19512 else
19513 add_loc_descr_to_each (list_ret, deref);
19516 if (ret)
19517 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
19519 return list_ret;
19522 /* Likewise, but strip useless DW_OP_nop operations in the resulting
19523 expressions. */
19525 static dw_loc_list_ref
19526 loc_list_from_tree (tree loc, int want_address,
19527 struct loc_descr_context *context)
19529 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
19531 for (dw_loc_list_ref loc_cur = result;
19532 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
19533 loc_descr_without_nops (loc_cur->expr);
19534 return result;
19537 /* Same as above but return only single location expression. */
19538 static dw_loc_descr_ref
19539 loc_descriptor_from_tree (tree loc, int want_address,
19540 struct loc_descr_context *context)
19542 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
19543 if (!ret)
19544 return NULL;
19545 if (ret->dw_loc_next)
19547 expansion_failed (loc, NULL_RTX,
19548 "Location list where only loc descriptor needed");
19549 return NULL;
19551 return ret->expr;
19554 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
19555 pointer to the declared type for the relevant field variable, or return
19556 `integer_type_node' if the given node turns out to be an
19557 ERROR_MARK node. */
19559 static inline tree
19560 field_type (const_tree decl)
19562 tree type;
19564 if (TREE_CODE (decl) == ERROR_MARK)
19565 return integer_type_node;
19567 type = DECL_BIT_FIELD_TYPE (decl);
19568 if (type == NULL_TREE)
19569 type = TREE_TYPE (decl);
19571 return type;
19574 /* Given a pointer to a tree node, return the alignment in bits for
19575 it, or else return BITS_PER_WORD if the node actually turns out to
19576 be an ERROR_MARK node. */
19578 static inline unsigned
19579 simple_type_align_in_bits (const_tree type)
19581 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
19584 static inline unsigned
19585 simple_decl_align_in_bits (const_tree decl)
19587 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
19590 /* Return the result of rounding T up to ALIGN. */
19592 static inline offset_int
19593 round_up_to_align (const offset_int &t, unsigned int align)
19595 return wi::udiv_trunc (t + align - 1, align) * align;
19598 /* Helper structure for RECORD_TYPE processing. */
19599 struct vlr_context
19601 /* Root RECORD_TYPE. It is needed to generate data member location
19602 descriptions in variable-length records (VLR), but also to cope with
19603 variants, which are composed of nested structures multiplexed with
19604 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
19605 function processing a FIELD_DECL, it is required to be non null. */
19606 tree struct_type;
19608 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
19609 QUAL_UNION_TYPE), this holds an expression that computes the offset for
19610 this variant part as part of the root record (in storage units). For
19611 regular records, it must be NULL_TREE. */
19612 tree variant_part_offset;
19615 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
19616 addressed byte of the "containing object" for the given FIELD_DECL. If
19617 possible, return a native constant through CST_OFFSET (in which case NULL is
19618 returned); otherwise return a DWARF expression that computes the offset.
19620 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
19621 that offset is, either because the argument turns out to be a pointer to an
19622 ERROR_MARK node, or because the offset expression is too complex for us.
19624 CTX is required: see the comment for VLR_CONTEXT. */
19626 static dw_loc_descr_ref
19627 field_byte_offset (const_tree decl, struct vlr_context *ctx,
19628 HOST_WIDE_INT *cst_offset)
19630 tree tree_result;
19631 dw_loc_list_ref loc_result;
19633 *cst_offset = 0;
19635 if (TREE_CODE (decl) == ERROR_MARK)
19636 return NULL;
19637 else
19638 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
19640 /* We cannot handle variable bit offsets at the moment, so abort if it's the
19641 case. */
19642 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
19643 return NULL;
19645 /* We used to handle only constant offsets in all cases. Now, we handle
19646 properly dynamic byte offsets only when PCC bitfield type doesn't
19647 matter. */
19648 if (PCC_BITFIELD_TYPE_MATTERS
19649 && DECL_BIT_FIELD_TYPE (decl)
19650 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19652 offset_int object_offset_in_bits;
19653 offset_int object_offset_in_bytes;
19654 offset_int bitpos_int;
19655 tree type;
19656 tree field_size_tree;
19657 offset_int deepest_bitpos;
19658 offset_int field_size_in_bits;
19659 unsigned int type_align_in_bits;
19660 unsigned int decl_align_in_bits;
19661 offset_int type_size_in_bits;
19663 bitpos_int = wi::to_offset (bit_position (decl));
19664 type = field_type (decl);
19665 type_size_in_bits = offset_int_type_size_in_bits (type);
19666 type_align_in_bits = simple_type_align_in_bits (type);
19668 field_size_tree = DECL_SIZE (decl);
19670 /* The size could be unspecified if there was an error, or for
19671 a flexible array member. */
19672 if (!field_size_tree)
19673 field_size_tree = bitsize_zero_node;
19675 /* If the size of the field is not constant, use the type size. */
19676 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19677 field_size_in_bits = wi::to_offset (field_size_tree);
19678 else
19679 field_size_in_bits = type_size_in_bits;
19681 decl_align_in_bits = simple_decl_align_in_bits (decl);
19683 /* The GCC front-end doesn't make any attempt to keep track of the
19684 starting bit offset (relative to the start of the containing
19685 structure type) of the hypothetical "containing object" for a
19686 bit-field. Thus, when computing the byte offset value for the
19687 start of the "containing object" of a bit-field, we must deduce
19688 this information on our own. This can be rather tricky to do in
19689 some cases. For example, handling the following structure type
19690 definition when compiling for an i386/i486 target (which only
19691 aligns long long's to 32-bit boundaries) can be very tricky:
19693 struct S { int field1; long long field2:31; };
19695 Fortunately, there is a simple rule-of-thumb which can be used
19696 in such cases. When compiling for an i386/i486, GCC will
19697 allocate 8 bytes for the structure shown above. It decides to
19698 do this based upon one simple rule for bit-field allocation.
19699 GCC allocates each "containing object" for each bit-field at
19700 the first (i.e. lowest addressed) legitimate alignment boundary
19701 (based upon the required minimum alignment for the declared
19702 type of the field) which it can possibly use, subject to the
19703 condition that there is still enough available space remaining
19704 in the containing object (when allocated at the selected point)
19705 to fully accommodate all of the bits of the bit-field itself.
19707 This simple rule makes it obvious why GCC allocates 8 bytes for
19708 each object of the structure type shown above. When looking
19709 for a place to allocate the "containing object" for `field2',
19710 the compiler simply tries to allocate a 64-bit "containing
19711 object" at each successive 32-bit boundary (starting at zero)
19712 until it finds a place to allocate that 64- bit field such that
19713 at least 31 contiguous (and previously unallocated) bits remain
19714 within that selected 64 bit field. (As it turns out, for the
19715 example above, the compiler finds it is OK to allocate the
19716 "containing object" 64-bit field at bit-offset zero within the
19717 structure type.)
19719 Here we attempt to work backwards from the limited set of facts
19720 we're given, and we try to deduce from those facts, where GCC
19721 must have believed that the containing object started (within
19722 the structure type). The value we deduce is then used (by the
19723 callers of this routine) to generate DW_AT_location and
19724 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19725 the case of DW_AT_location, regular fields as well). */
19727 /* Figure out the bit-distance from the start of the structure to
19728 the "deepest" bit of the bit-field. */
19729 deepest_bitpos = bitpos_int + field_size_in_bits;
19731 /* This is the tricky part. Use some fancy footwork to deduce
19732 where the lowest addressed bit of the containing object must
19733 be. */
19734 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19736 /* Round up to type_align by default. This works best for
19737 bitfields. */
19738 object_offset_in_bits
19739 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19741 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19743 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19745 /* Round up to decl_align instead. */
19746 object_offset_in_bits
19747 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19750 object_offset_in_bytes
19751 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19752 if (ctx->variant_part_offset == NULL_TREE)
19754 *cst_offset = object_offset_in_bytes.to_shwi ();
19755 return NULL;
19757 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19759 else
19760 tree_result = byte_position (decl);
19762 if (ctx->variant_part_offset != NULL_TREE)
19763 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19764 ctx->variant_part_offset, tree_result);
19766 /* If the byte offset is a constant, it's simplier to handle a native
19767 constant rather than a DWARF expression. */
19768 if (TREE_CODE (tree_result) == INTEGER_CST)
19770 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19771 return NULL;
19774 struct loc_descr_context loc_ctx = {
19775 ctx->struct_type, /* context_type */
19776 NULL_TREE, /* base_decl */
19777 NULL, /* dpi */
19778 false, /* placeholder_arg */
19779 false, /* placeholder_seen */
19780 false /* strict_signedness */
19782 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19784 /* We want a DWARF expression: abort if we only have a location list with
19785 multiple elements. */
19786 if (!loc_result || !single_element_loc_list_p (loc_result))
19787 return NULL;
19788 else
19789 return loc_result->expr;
19792 /* The following routines define various Dwarf attributes and any data
19793 associated with them. */
19795 /* Add a location description attribute value to a DIE.
19797 This emits location attributes suitable for whole variables and
19798 whole parameters. Note that the location attributes for struct fields are
19799 generated by the routine `data_member_location_attribute' below. */
19801 static inline void
19802 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19803 dw_loc_list_ref descr)
19805 bool check_no_locviews = true;
19806 if (descr == 0)
19807 return;
19808 if (single_element_loc_list_p (descr))
19809 add_AT_loc (die, attr_kind, descr->expr);
19810 else
19812 add_AT_loc_list (die, attr_kind, descr);
19813 gcc_assert (descr->ll_symbol);
19814 if (attr_kind == DW_AT_location && descr->vl_symbol
19815 && dwarf2out_locviews_in_attribute ())
19817 add_AT_view_list (die, DW_AT_GNU_locviews);
19818 check_no_locviews = false;
19822 if (check_no_locviews)
19823 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19826 /* Add DW_AT_accessibility attribute to DIE if needed. */
19828 static void
19829 add_accessibility_attribute (dw_die_ref die, tree decl)
19831 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19832 children, otherwise the default is DW_ACCESS_public. In DWARF2
19833 the default has always been DW_ACCESS_public. */
19834 if (TREE_PROTECTED (decl))
19835 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19836 else if (TREE_PRIVATE (decl))
19838 if (dwarf_version == 2
19839 || die->die_parent == NULL
19840 || die->die_parent->die_tag != DW_TAG_class_type)
19841 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19843 else if (dwarf_version > 2
19844 && die->die_parent
19845 && die->die_parent->die_tag == DW_TAG_class_type)
19846 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19849 /* Attach the specialized form of location attribute used for data members of
19850 struct and union types. In the special case of a FIELD_DECL node which
19851 represents a bit-field, the "offset" part of this special location
19852 descriptor must indicate the distance in bytes from the lowest-addressed
19853 byte of the containing struct or union type to the lowest-addressed byte of
19854 the "containing object" for the bit-field. (See the `field_byte_offset'
19855 function above).
19857 For any given bit-field, the "containing object" is a hypothetical object
19858 (of some integral or enum type) within which the given bit-field lives. The
19859 type of this hypothetical "containing object" is always the same as the
19860 declared type of the individual bit-field itself (for GCC anyway... the
19861 DWARF spec doesn't actually mandate this). Note that it is the size (in
19862 bytes) of the hypothetical "containing object" which will be given in the
19863 DW_AT_byte_size attribute for this bit-field. (See the
19864 `byte_size_attribute' function below.) It is also used when calculating the
19865 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19866 function below.)
19868 CTX is required: see the comment for VLR_CONTEXT. */
19870 static void
19871 add_data_member_location_attribute (dw_die_ref die,
19872 tree decl,
19873 struct vlr_context *ctx)
19875 HOST_WIDE_INT offset;
19876 dw_loc_descr_ref loc_descr = 0;
19878 if (TREE_CODE (decl) == TREE_BINFO)
19880 /* We're working on the TAG_inheritance for a base class. */
19881 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19883 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19884 aren't at a fixed offset from all (sub)objects of the same
19885 type. We need to extract the appropriate offset from our
19886 vtable. The following dwarf expression means
19888 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19890 This is specific to the V3 ABI, of course. */
19892 dw_loc_descr_ref tmp;
19894 /* Make a copy of the object address. */
19895 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19896 add_loc_descr (&loc_descr, tmp);
19898 /* Extract the vtable address. */
19899 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19900 add_loc_descr (&loc_descr, tmp);
19902 /* Calculate the address of the offset. */
19903 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19904 gcc_assert (offset < 0);
19906 tmp = int_loc_descriptor (-offset);
19907 add_loc_descr (&loc_descr, tmp);
19908 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19909 add_loc_descr (&loc_descr, tmp);
19911 /* Extract the offset. */
19912 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19913 add_loc_descr (&loc_descr, tmp);
19915 /* Add it to the object address. */
19916 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19917 add_loc_descr (&loc_descr, tmp);
19919 else
19920 offset = tree_to_shwi (BINFO_OFFSET (decl));
19922 else
19924 loc_descr = field_byte_offset (decl, ctx, &offset);
19926 if (!loc_descr)
19929 /* If loc_descr is available, then we know the offset is dynamic. */
19930 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
19932 loc_descr = NULL;
19933 offset = 0;
19936 /* Data member location evaluation starts with the base address on the
19937 stack. Compute the field offset and add it to this base address. */
19938 else
19939 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19942 if (!loc_descr)
19944 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19945 e.g. GDB only added support to it in November 2016. For DWARF5
19946 we need newer debug info consumers anyway. We might change this
19947 to dwarf_version >= 4 once most consumers catched up. */
19948 if (dwarf_version >= 5
19949 && TREE_CODE (decl) == FIELD_DECL
19950 && DECL_BIT_FIELD_TYPE (decl)
19951 && (ctx->variant_part_offset == NULL_TREE
19952 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19954 tree off = bit_position (decl);
19955 if (ctx->variant_part_offset)
19956 off = bit_from_pos (ctx->variant_part_offset, off);
19957 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19959 remove_AT (die, DW_AT_byte_size);
19960 remove_AT (die, DW_AT_bit_offset);
19961 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19962 return;
19965 if (dwarf_version > 2)
19967 /* Don't need to output a location expression, just the constant. */
19968 if (offset < 0)
19969 add_AT_int (die, DW_AT_data_member_location, offset);
19970 else
19971 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19972 return;
19974 else
19976 enum dwarf_location_atom op;
19978 /* The DWARF2 standard says that we should assume that the structure
19979 address is already on the stack, so we can specify a structure
19980 field address by using DW_OP_plus_uconst. */
19981 op = DW_OP_plus_uconst;
19982 loc_descr = new_loc_descr (op, offset, 0);
19986 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19989 /* Writes integer values to dw_vec_const array. */
19991 static void
19992 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19994 while (size != 0)
19996 *dest++ = val & 0xff;
19997 val >>= 8;
19998 --size;
20002 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20004 static HOST_WIDE_INT
20005 extract_int (const unsigned char *src, unsigned int size)
20007 HOST_WIDE_INT val = 0;
20009 src += size;
20010 while (size != 0)
20012 val <<= 8;
20013 val |= *--src & 0xff;
20014 --size;
20016 return val;
20019 /* Writes wide_int values to dw_vec_const array. */
20021 static void
20022 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20024 int i;
20026 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20028 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20029 return;
20032 /* We'd have to extend this code to support odd sizes. */
20033 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20035 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20037 if (WORDS_BIG_ENDIAN)
20038 for (i = n - 1; i >= 0; i--)
20040 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20041 dest += sizeof (HOST_WIDE_INT);
20043 else
20044 for (i = 0; i < n; i++)
20046 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20047 dest += sizeof (HOST_WIDE_INT);
20051 /* Writes floating point values to dw_vec_const array. */
20053 static unsigned
20054 insert_float (const_rtx rtl, unsigned char *array)
20056 long val[4];
20057 int i;
20058 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20060 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20062 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20063 if (GET_MODE_SIZE (mode) < 4)
20065 gcc_assert (GET_MODE_SIZE (mode) == 2);
20066 insert_int (val[0], 2, array);
20067 return 2;
20070 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20072 insert_int (val[i], 4, array);
20073 array += 4;
20075 return 4;
20078 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20079 does not have a "location" either in memory or in a register. These
20080 things can arise in GNU C when a constant is passed as an actual parameter
20081 to an inlined function. They can also arise in C++ where declared
20082 constants do not necessarily get memory "homes". */
20084 static bool
20085 add_const_value_attribute (dw_die_ref die, machine_mode mode, rtx rtl)
20087 scalar_mode int_mode;
20089 switch (GET_CODE (rtl))
20091 case CONST_INT:
20093 HOST_WIDE_INT val = INTVAL (rtl);
20095 if (val < 0)
20096 add_AT_int (die, DW_AT_const_value, val);
20097 else
20098 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20100 return true;
20102 case CONST_WIDE_INT:
20103 if (is_int_mode (mode, &int_mode)
20104 && (GET_MODE_PRECISION (int_mode)
20105 & (HOST_BITS_PER_WIDE_INT - 1)) == 0)
20107 wide_int w = rtx_mode_t (rtl, int_mode);
20108 add_AT_wide (die, DW_AT_const_value, w);
20109 return true;
20111 return false;
20113 case CONST_DOUBLE:
20114 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20115 floating-point constant. A CONST_DOUBLE is used whenever the
20116 constant requires more than one word in order to be adequately
20117 represented. */
20118 if (TARGET_SUPPORTS_WIDE_INT == 0
20119 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20120 add_AT_double (die, DW_AT_const_value,
20121 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20122 else
20124 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20125 unsigned int length = GET_MODE_SIZE (mode);
20126 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20127 unsigned int elt_size = insert_float (rtl, array);
20129 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20130 array);
20132 return true;
20134 case CONST_VECTOR:
20136 unsigned int length;
20137 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20138 return false;
20140 machine_mode mode = GET_MODE (rtl);
20141 /* The combination of a length and byte elt_size doesn't extend
20142 naturally to boolean vectors, where several elements are packed
20143 into the same byte. */
20144 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20145 return false;
20147 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20148 unsigned char *array
20149 = ggc_vec_alloc<unsigned char> (length * elt_size);
20150 unsigned int i;
20151 unsigned char *p;
20152 machine_mode imode = GET_MODE_INNER (mode);
20154 switch (GET_MODE_CLASS (mode))
20156 case MODE_VECTOR_INT:
20157 for (i = 0, p = array; i < length; i++, p += elt_size)
20159 rtx elt = CONST_VECTOR_ELT (rtl, i);
20160 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20162 break;
20164 case MODE_VECTOR_FLOAT:
20165 for (i = 0, p = array; i < length; i++, p += elt_size)
20167 rtx elt = CONST_VECTOR_ELT (rtl, i);
20168 insert_float (elt, p);
20170 break;
20172 default:
20173 gcc_unreachable ();
20176 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20178 return true;
20180 case CONST_STRING:
20181 if (dwarf_version >= 4 || !dwarf_strict)
20183 dw_loc_descr_ref loc_result;
20184 resolve_one_addr (&rtl);
20185 rtl_addr:
20186 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20187 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20188 add_AT_loc (die, DW_AT_location, loc_result);
20189 vec_safe_push (used_rtx_array, rtl);
20190 return true;
20192 return false;
20194 case CONST:
20195 if (CONSTANT_P (XEXP (rtl, 0)))
20196 return add_const_value_attribute (die, mode, XEXP (rtl, 0));
20197 /* FALLTHROUGH */
20198 case SYMBOL_REF:
20199 if (!const_ok_for_output (rtl))
20200 return false;
20201 /* FALLTHROUGH */
20202 case LABEL_REF:
20203 if (dwarf_version >= 4 || !dwarf_strict)
20204 goto rtl_addr;
20205 return false;
20207 case PLUS:
20208 /* In cases where an inlined instance of an inline function is passed
20209 the address of an `auto' variable (which is local to the caller) we
20210 can get a situation where the DECL_RTL of the artificial local
20211 variable (for the inlining) which acts as a stand-in for the
20212 corresponding formal parameter (of the inline function) will look
20213 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20214 exactly a compile-time constant expression, but it isn't the address
20215 of the (artificial) local variable either. Rather, it represents the
20216 *value* which the artificial local variable always has during its
20217 lifetime. We currently have no way to represent such quasi-constant
20218 values in Dwarf, so for now we just punt and generate nothing. */
20219 return false;
20221 case HIGH:
20222 case CONST_FIXED:
20223 case MINUS:
20224 case SIGN_EXTEND:
20225 case ZERO_EXTEND:
20226 case CONST_POLY_INT:
20227 return false;
20229 case MEM:
20230 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20231 && MEM_READONLY_P (rtl)
20232 && GET_MODE (rtl) == BLKmode)
20234 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20235 return true;
20237 return false;
20239 default:
20240 /* No other kinds of rtx should be possible here. */
20241 gcc_unreachable ();
20243 return false;
20246 /* Determine whether the evaluation of EXPR references any variables
20247 or functions which aren't otherwise used (and therefore may not be
20248 output). */
20249 static tree
20250 reference_to_unused (tree * tp, int * walk_subtrees,
20251 void * data ATTRIBUTE_UNUSED)
20253 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20254 *walk_subtrees = 0;
20256 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20257 && ! TREE_ASM_WRITTEN (*tp))
20258 return *tp;
20259 /* ??? The C++ FE emits debug information for using decls, so
20260 putting gcc_unreachable here falls over. See PR31899. For now
20261 be conservative. */
20262 else if (!symtab->global_info_ready && VAR_P (*tp))
20263 return *tp;
20264 else if (VAR_P (*tp))
20266 varpool_node *node = varpool_node::get (*tp);
20267 if (!node || !node->definition)
20268 return *tp;
20270 else if (TREE_CODE (*tp) == FUNCTION_DECL
20271 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20273 /* The call graph machinery must have finished analyzing,
20274 optimizing and gimplifying the CU by now.
20275 So if *TP has no call graph node associated
20276 to it, it means *TP will not be emitted. */
20277 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20278 return *tp;
20280 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20281 return *tp;
20283 return NULL_TREE;
20286 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20287 for use in a later add_const_value_attribute call. */
20289 static rtx
20290 rtl_for_decl_init (tree init, tree type)
20292 rtx rtl = NULL_RTX;
20294 STRIP_NOPS (init);
20296 /* If a variable is initialized with a string constant without embedded
20297 zeros, build CONST_STRING. */
20298 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20300 tree enttype = TREE_TYPE (type);
20301 tree domain = TYPE_DOMAIN (type);
20302 scalar_int_mode mode;
20304 if (is_int_mode (TYPE_MODE (enttype), &mode)
20305 && GET_MODE_SIZE (mode) == 1
20306 && domain
20307 && TYPE_MAX_VALUE (domain)
20308 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20309 && integer_zerop (TYPE_MIN_VALUE (domain))
20310 && compare_tree_int (TYPE_MAX_VALUE (domain),
20311 TREE_STRING_LENGTH (init) - 1) == 0
20312 && ((size_t) TREE_STRING_LENGTH (init)
20313 == strlen (TREE_STRING_POINTER (init)) + 1))
20315 rtl = gen_rtx_CONST_STRING (VOIDmode,
20316 ggc_strdup (TREE_STRING_POINTER (init)));
20317 rtl = gen_rtx_MEM (BLKmode, rtl);
20318 MEM_READONLY_P (rtl) = 1;
20321 /* Other aggregates, and complex values, could be represented using
20322 CONCAT: FIXME! */
20323 else if (AGGREGATE_TYPE_P (type)
20324 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20325 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20326 || TREE_CODE (type) == COMPLEX_TYPE)
20328 /* Vectors only work if their mode is supported by the target.
20329 FIXME: generic vectors ought to work too. */
20330 else if (TREE_CODE (type) == VECTOR_TYPE
20331 && !VECTOR_MODE_P (TYPE_MODE (type)))
20333 /* If the initializer is something that we know will expand into an
20334 immediate RTL constant, expand it now. We must be careful not to
20335 reference variables which won't be output. */
20336 else if (initializer_constant_valid_p (init, type)
20337 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20339 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20340 possible. */
20341 if (TREE_CODE (type) == VECTOR_TYPE)
20342 switch (TREE_CODE (init))
20344 case VECTOR_CST:
20345 break;
20346 case CONSTRUCTOR:
20347 if (TREE_CONSTANT (init))
20349 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20350 bool constant_p = true;
20351 tree value;
20352 unsigned HOST_WIDE_INT ix;
20354 /* Even when ctor is constant, it might contain non-*_CST
20355 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20356 belong into VECTOR_CST nodes. */
20357 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20358 if (!CONSTANT_CLASS_P (value))
20360 constant_p = false;
20361 break;
20364 if (constant_p)
20366 init = build_vector_from_ctor (type, elts);
20367 break;
20370 /* FALLTHRU */
20372 default:
20373 return NULL;
20376 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20378 /* If expand_expr returns a MEM, it wasn't immediate. */
20379 gcc_assert (!rtl || !MEM_P (rtl));
20382 return rtl;
20385 /* Generate RTL for the variable DECL to represent its location. */
20387 static rtx
20388 rtl_for_decl_location (tree decl)
20390 rtx rtl;
20392 /* Here we have to decide where we are going to say the parameter "lives"
20393 (as far as the debugger is concerned). We only have a couple of
20394 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20396 DECL_RTL normally indicates where the parameter lives during most of the
20397 activation of the function. If optimization is enabled however, this
20398 could be either NULL or else a pseudo-reg. Both of those cases indicate
20399 that the parameter doesn't really live anywhere (as far as the code
20400 generation parts of GCC are concerned) during most of the function's
20401 activation. That will happen (for example) if the parameter is never
20402 referenced within the function.
20404 We could just generate a location descriptor here for all non-NULL
20405 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20406 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20407 where DECL_RTL is NULL or is a pseudo-reg.
20409 Note however that we can only get away with using DECL_INCOMING_RTL as
20410 a backup substitute for DECL_RTL in certain limited cases. In cases
20411 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20412 we can be sure that the parameter was passed using the same type as it is
20413 declared to have within the function, and that its DECL_INCOMING_RTL
20414 points us to a place where a value of that type is passed.
20416 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20417 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20418 because in these cases DECL_INCOMING_RTL points us to a value of some
20419 type which is *different* from the type of the parameter itself. Thus,
20420 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20421 such cases, the debugger would end up (for example) trying to fetch a
20422 `float' from a place which actually contains the first part of a
20423 `double'. That would lead to really incorrect and confusing
20424 output at debug-time.
20426 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20427 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20428 are a couple of exceptions however. On little-endian machines we can
20429 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20430 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20431 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20432 when (on a little-endian machine) a non-prototyped function has a
20433 parameter declared to be of type `short' or `char'. In such cases,
20434 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20435 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20436 passed `int' value. If the debugger then uses that address to fetch
20437 a `short' or a `char' (on a little-endian machine) the result will be
20438 the correct data, so we allow for such exceptional cases below.
20440 Note that our goal here is to describe the place where the given formal
20441 parameter lives during most of the function's activation (i.e. between the
20442 end of the prologue and the start of the epilogue). We'll do that as best
20443 as we can. Note however that if the given formal parameter is modified
20444 sometime during the execution of the function, then a stack backtrace (at
20445 debug-time) will show the function as having been called with the *new*
20446 value rather than the value which was originally passed in. This happens
20447 rarely enough that it is not a major problem, but it *is* a problem, and
20448 I'd like to fix it.
20450 A future version of dwarf2out.c may generate two additional attributes for
20451 any given DW_TAG_formal_parameter DIE which will describe the "passed
20452 type" and the "passed location" for the given formal parameter in addition
20453 to the attributes we now generate to indicate the "declared type" and the
20454 "active location" for each parameter. This additional set of attributes
20455 could be used by debuggers for stack backtraces. Separately, note that
20456 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20457 This happens (for example) for inlined-instances of inline function formal
20458 parameters which are never referenced. This really shouldn't be
20459 happening. All PARM_DECL nodes should get valid non-NULL
20460 DECL_INCOMING_RTL values. FIXME. */
20462 /* Use DECL_RTL as the "location" unless we find something better. */
20463 rtl = DECL_RTL_IF_SET (decl);
20465 /* When generating abstract instances, ignore everything except
20466 constants, symbols living in memory, and symbols living in
20467 fixed registers. */
20468 if (! reload_completed)
20470 if (rtl
20471 && (CONSTANT_P (rtl)
20472 || (MEM_P (rtl)
20473 && CONSTANT_P (XEXP (rtl, 0)))
20474 || (REG_P (rtl)
20475 && VAR_P (decl)
20476 && TREE_STATIC (decl))))
20478 rtl = targetm.delegitimize_address (rtl);
20479 return rtl;
20481 rtl = NULL_RTX;
20483 else if (TREE_CODE (decl) == PARM_DECL)
20485 if (rtl == NULL_RTX
20486 || is_pseudo_reg (rtl)
20487 || (MEM_P (rtl)
20488 && is_pseudo_reg (XEXP (rtl, 0))
20489 && DECL_INCOMING_RTL (decl)
20490 && MEM_P (DECL_INCOMING_RTL (decl))
20491 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20493 tree declared_type = TREE_TYPE (decl);
20494 tree passed_type = DECL_ARG_TYPE (decl);
20495 machine_mode dmode = TYPE_MODE (declared_type);
20496 machine_mode pmode = TYPE_MODE (passed_type);
20498 /* This decl represents a formal parameter which was optimized out.
20499 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20500 all cases where (rtl == NULL_RTX) just below. */
20501 if (dmode == pmode)
20502 rtl = DECL_INCOMING_RTL (decl);
20503 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20504 && SCALAR_INT_MODE_P (dmode)
20505 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20506 && DECL_INCOMING_RTL (decl))
20508 rtx inc = DECL_INCOMING_RTL (decl);
20509 if (REG_P (inc))
20510 rtl = inc;
20511 else if (MEM_P (inc))
20513 if (BYTES_BIG_ENDIAN)
20514 rtl = adjust_address_nv (inc, dmode,
20515 GET_MODE_SIZE (pmode)
20516 - GET_MODE_SIZE (dmode));
20517 else
20518 rtl = inc;
20523 /* If the parm was passed in registers, but lives on the stack, then
20524 make a big endian correction if the mode of the type of the
20525 parameter is not the same as the mode of the rtl. */
20526 /* ??? This is the same series of checks that are made in dbxout.c before
20527 we reach the big endian correction code there. It isn't clear if all
20528 of these checks are necessary here, but keeping them all is the safe
20529 thing to do. */
20530 else if (MEM_P (rtl)
20531 && XEXP (rtl, 0) != const0_rtx
20532 && ! CONSTANT_P (XEXP (rtl, 0))
20533 /* Not passed in memory. */
20534 && !MEM_P (DECL_INCOMING_RTL (decl))
20535 /* Not passed by invisible reference. */
20536 && (!REG_P (XEXP (rtl, 0))
20537 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20538 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20539 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20540 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20541 #endif
20543 /* Big endian correction check. */
20544 && BYTES_BIG_ENDIAN
20545 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20546 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20547 UNITS_PER_WORD))
20549 machine_mode addr_mode = get_address_mode (rtl);
20550 poly_int64 offset = (UNITS_PER_WORD
20551 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20553 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20554 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20557 else if (VAR_P (decl)
20558 && rtl
20559 && MEM_P (rtl)
20560 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20562 machine_mode addr_mode = get_address_mode (rtl);
20563 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20564 GET_MODE (rtl));
20566 /* If a variable is declared "register" yet is smaller than
20567 a register, then if we store the variable to memory, it
20568 looks like we're storing a register-sized value, when in
20569 fact we are not. We need to adjust the offset of the
20570 storage location to reflect the actual value's bytes,
20571 else gdb will not be able to display it. */
20572 if (maybe_ne (offset, 0))
20573 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20574 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20577 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20578 and will have been substituted directly into all expressions that use it.
20579 C does not have such a concept, but C++ and other languages do. */
20580 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20581 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20583 if (rtl)
20584 rtl = targetm.delegitimize_address (rtl);
20586 /* If we don't look past the constant pool, we risk emitting a
20587 reference to a constant pool entry that isn't referenced from
20588 code, and thus is not emitted. */
20589 if (rtl)
20590 rtl = avoid_constant_pool_reference (rtl);
20592 /* Try harder to get a rtl. If this symbol ends up not being emitted
20593 in the current CU, resolve_addr will remove the expression referencing
20594 it. */
20595 if (rtl == NULL_RTX
20596 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20597 && VAR_P (decl)
20598 && !DECL_EXTERNAL (decl)
20599 && TREE_STATIC (decl)
20600 && DECL_NAME (decl)
20601 && !DECL_HARD_REGISTER (decl)
20602 && DECL_MODE (decl) != VOIDmode)
20604 rtl = make_decl_rtl_for_debug (decl);
20605 if (!MEM_P (rtl)
20606 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20607 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20608 rtl = NULL_RTX;
20611 return rtl;
20614 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20615 returned. If so, the decl for the COMMON block is returned, and the
20616 value is the offset into the common block for the symbol. */
20618 static tree
20619 fortran_common (tree decl, HOST_WIDE_INT *value)
20621 tree val_expr, cvar;
20622 machine_mode mode;
20623 poly_int64 bitsize, bitpos;
20624 tree offset;
20625 HOST_WIDE_INT cbitpos;
20626 int unsignedp, reversep, volatilep = 0;
20628 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20629 it does not have a value (the offset into the common area), or if it
20630 is thread local (as opposed to global) then it isn't common, and shouldn't
20631 be handled as such. */
20632 if (!VAR_P (decl)
20633 || !TREE_STATIC (decl)
20634 || !DECL_HAS_VALUE_EXPR_P (decl)
20635 || !is_fortran ())
20636 return NULL_TREE;
20638 val_expr = DECL_VALUE_EXPR (decl);
20639 if (TREE_CODE (val_expr) != COMPONENT_REF)
20640 return NULL_TREE;
20642 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20643 &unsignedp, &reversep, &volatilep);
20645 if (cvar == NULL_TREE
20646 || !VAR_P (cvar)
20647 || DECL_ARTIFICIAL (cvar)
20648 || !TREE_PUBLIC (cvar)
20649 /* We don't expect to have to cope with variable offsets,
20650 since at present all static data must have a constant size. */
20651 || !bitpos.is_constant (&cbitpos))
20652 return NULL_TREE;
20654 *value = 0;
20655 if (offset != NULL)
20657 if (!tree_fits_shwi_p (offset))
20658 return NULL_TREE;
20659 *value = tree_to_shwi (offset);
20661 if (cbitpos != 0)
20662 *value += cbitpos / BITS_PER_UNIT;
20664 return cvar;
20667 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20668 data attribute for a variable or a parameter. We generate the
20669 DW_AT_const_value attribute only in those cases where the given variable
20670 or parameter does not have a true "location" either in memory or in a
20671 register. This can happen (for example) when a constant is passed as an
20672 actual argument in a call to an inline function. (It's possible that
20673 these things can crop up in other ways also.) Note that one type of
20674 constant value which can be passed into an inlined function is a constant
20675 pointer. This can happen for example if an actual argument in an inlined
20676 function call evaluates to a compile-time constant address.
20678 CACHE_P is true if it is worth caching the location list for DECL,
20679 so that future calls can reuse it rather than regenerate it from scratch.
20680 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20681 since we will need to refer to them each time the function is inlined. */
20683 static bool
20684 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20686 rtx rtl;
20687 dw_loc_list_ref list;
20688 var_loc_list *loc_list;
20689 cached_dw_loc_list *cache;
20691 if (early_dwarf)
20692 return false;
20694 if (TREE_CODE (decl) == ERROR_MARK)
20695 return false;
20697 if (get_AT (die, DW_AT_location)
20698 || get_AT (die, DW_AT_const_value))
20699 return true;
20701 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20702 || TREE_CODE (decl) == RESULT_DECL);
20704 /* Try to get some constant RTL for this decl, and use that as the value of
20705 the location. */
20707 rtl = rtl_for_decl_location (decl);
20708 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20709 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20710 return true;
20712 /* See if we have single element location list that is equivalent to
20713 a constant value. That way we are better to use add_const_value_attribute
20714 rather than expanding constant value equivalent. */
20715 loc_list = lookup_decl_loc (decl);
20716 if (loc_list
20717 && loc_list->first
20718 && loc_list->first->next == NULL
20719 && NOTE_P (loc_list->first->loc)
20720 && NOTE_VAR_LOCATION (loc_list->first->loc)
20721 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20723 struct var_loc_node *node;
20725 node = loc_list->first;
20726 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20727 if (GET_CODE (rtl) == EXPR_LIST)
20728 rtl = XEXP (rtl, 0);
20729 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20730 && add_const_value_attribute (die, DECL_MODE (decl), rtl))
20731 return true;
20733 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20734 list several times. See if we've already cached the contents. */
20735 list = NULL;
20736 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20737 cache_p = false;
20738 if (cache_p)
20740 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20741 if (cache)
20742 list = cache->loc_list;
20744 if (list == NULL)
20746 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20747 NULL);
20748 /* It is usually worth caching this result if the decl is from
20749 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20750 if (cache_p && list && list->dw_loc_next)
20752 cached_dw_loc_list **slot
20753 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20754 DECL_UID (decl),
20755 INSERT);
20756 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20757 cache->decl_id = DECL_UID (decl);
20758 cache->loc_list = list;
20759 *slot = cache;
20762 if (list)
20764 add_AT_location_description (die, DW_AT_location, list);
20765 return true;
20767 /* None of that worked, so it must not really have a location;
20768 try adding a constant value attribute from the DECL_INITIAL. */
20769 return tree_add_const_value_attribute_for_decl (die, decl);
20772 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20773 attribute is the const value T. */
20775 static bool
20776 tree_add_const_value_attribute (dw_die_ref die, tree t)
20778 tree init;
20779 tree type = TREE_TYPE (t);
20780 rtx rtl;
20782 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20783 return false;
20785 init = t;
20786 gcc_assert (!DECL_P (init));
20788 if (TREE_CODE (init) == INTEGER_CST)
20790 if (tree_fits_uhwi_p (init))
20792 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20793 return true;
20795 if (tree_fits_shwi_p (init))
20797 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20798 return true;
20801 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20802 symbols. */
20803 rtl = rtl_for_decl_init (init, type);
20804 if (rtl && !early_dwarf)
20805 return add_const_value_attribute (die, TYPE_MODE (type), rtl);
20806 /* If the host and target are sane, try harder. */
20807 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20808 && initializer_constant_valid_p (init, type))
20810 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20811 if (size > 0 && (int) size == size)
20813 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20815 if (native_encode_initializer (init, array, size) == size)
20817 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20818 return true;
20820 ggc_free (array);
20823 return false;
20826 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20827 attribute is the const value of T, where T is an integral constant
20828 variable with static storage duration
20829 (so it can't be a PARM_DECL or a RESULT_DECL). */
20831 static bool
20832 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20835 if (!decl
20836 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20837 || (VAR_P (decl) && !TREE_STATIC (decl)))
20838 return false;
20840 if (TREE_READONLY (decl)
20841 && ! TREE_THIS_VOLATILE (decl)
20842 && DECL_INITIAL (decl))
20843 /* OK */;
20844 else
20845 return false;
20847 /* Don't add DW_AT_const_value if abstract origin already has one. */
20848 if (get_AT (var_die, DW_AT_const_value))
20849 return false;
20851 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20854 /* Convert the CFI instructions for the current function into a
20855 location list. This is used for DW_AT_frame_base when we targeting
20856 a dwarf2 consumer that does not support the dwarf3
20857 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20858 expressions. */
20860 static dw_loc_list_ref
20861 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20863 int ix;
20864 dw_fde_ref fde;
20865 dw_loc_list_ref list, *list_tail;
20866 dw_cfi_ref cfi;
20867 dw_cfa_location last_cfa, next_cfa;
20868 const char *start_label, *last_label, *section;
20869 dw_cfa_location remember;
20871 fde = cfun->fde;
20872 gcc_assert (fde != NULL);
20874 section = secname_for_decl (current_function_decl);
20875 list_tail = &list;
20876 list = NULL;
20878 memset (&next_cfa, 0, sizeof (next_cfa));
20879 next_cfa.reg = INVALID_REGNUM;
20880 remember = next_cfa;
20882 start_label = fde->dw_fde_begin;
20884 /* ??? Bald assumption that the CIE opcode list does not contain
20885 advance opcodes. */
20886 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20887 lookup_cfa_1 (cfi, &next_cfa, &remember);
20889 last_cfa = next_cfa;
20890 last_label = start_label;
20892 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20894 /* If the first partition contained no CFI adjustments, the
20895 CIE opcodes apply to the whole first partition. */
20896 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20897 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20898 list_tail =&(*list_tail)->dw_loc_next;
20899 start_label = last_label = fde->dw_fde_second_begin;
20902 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20904 switch (cfi->dw_cfi_opc)
20906 case DW_CFA_set_loc:
20907 case DW_CFA_advance_loc1:
20908 case DW_CFA_advance_loc2:
20909 case DW_CFA_advance_loc4:
20910 if (!cfa_equal_p (&last_cfa, &next_cfa))
20912 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20913 start_label, 0, last_label, 0, section);
20915 list_tail = &(*list_tail)->dw_loc_next;
20916 last_cfa = next_cfa;
20917 start_label = last_label;
20919 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20920 break;
20922 case DW_CFA_advance_loc:
20923 /* The encoding is complex enough that we should never emit this. */
20924 gcc_unreachable ();
20926 default:
20927 lookup_cfa_1 (cfi, &next_cfa, &remember);
20928 break;
20930 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20932 if (!cfa_equal_p (&last_cfa, &next_cfa))
20934 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20935 start_label, 0, last_label, 0, section);
20937 list_tail = &(*list_tail)->dw_loc_next;
20938 last_cfa = next_cfa;
20939 start_label = last_label;
20941 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20942 start_label, 0, fde->dw_fde_end, 0, section);
20943 list_tail = &(*list_tail)->dw_loc_next;
20944 start_label = last_label = fde->dw_fde_second_begin;
20948 if (!cfa_equal_p (&last_cfa, &next_cfa))
20950 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20951 start_label, 0, last_label, 0, section);
20952 list_tail = &(*list_tail)->dw_loc_next;
20953 start_label = last_label;
20956 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20957 start_label, 0,
20958 fde->dw_fde_second_begin
20959 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20960 section);
20962 maybe_gen_llsym (list);
20964 return list;
20967 /* Compute a displacement from the "steady-state frame pointer" to the
20968 frame base (often the same as the CFA), and store it in
20969 frame_pointer_fb_offset. OFFSET is added to the displacement
20970 before the latter is negated. */
20972 static void
20973 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20975 rtx reg, elim;
20977 #ifdef FRAME_POINTER_CFA_OFFSET
20978 reg = frame_pointer_rtx;
20979 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20980 #else
20981 reg = arg_pointer_rtx;
20982 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20983 #endif
20985 elim = (ira_use_lra_p
20986 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20987 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20988 elim = strip_offset_and_add (elim, &offset);
20990 frame_pointer_fb_offset = -offset;
20992 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20993 in which to eliminate. This is because it's stack pointer isn't
20994 directly accessible as a register within the ISA. To work around
20995 this, assume that while we cannot provide a proper value for
20996 frame_pointer_fb_offset, we won't need one either. We can use
20997 hard frame pointer in debug info even if frame pointer isn't used
20998 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20999 which uses the DW_AT_frame_base attribute, not hard frame pointer
21000 directly. */
21001 frame_pointer_fb_offset_valid
21002 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21005 /* Generate a DW_AT_name attribute given some string value to be included as
21006 the value of the attribute. */
21008 void
21009 add_name_attribute (dw_die_ref die, const char *name_string)
21011 if (name_string != NULL && *name_string != 0)
21013 if (demangle_name_func)
21014 name_string = (*demangle_name_func) (name_string);
21016 add_AT_string (die, DW_AT_name, name_string);
21020 /* Generate a DW_AT_name attribute given some string value representing a
21021 file or filepath to be included as value of the attribute. */
21022 static void
21023 add_filename_attribute (dw_die_ref die, const char *name_string)
21025 if (name_string != NULL && *name_string != 0)
21026 add_filepath_AT_string (die, DW_AT_name, name_string);
21029 /* Generate a DW_AT_description attribute given some string value to be included
21030 as the value of the attribute. */
21032 static void
21033 add_desc_attribute (dw_die_ref die, const char *name_string)
21035 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21036 return;
21038 if (name_string == NULL || *name_string == 0)
21039 return;
21041 if (demangle_name_func)
21042 name_string = (*demangle_name_func) (name_string);
21044 add_AT_string (die, DW_AT_description, name_string);
21047 /* Generate a DW_AT_description attribute given some decl to be included
21048 as the value of the attribute. */
21050 static void
21051 add_desc_attribute (dw_die_ref die, tree decl)
21053 tree decl_name;
21055 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21056 return;
21058 if (decl == NULL_TREE || !DECL_P (decl))
21059 return;
21060 decl_name = DECL_NAME (decl);
21062 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21064 const char *name = dwarf2_name (decl, 0);
21065 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21067 else
21069 char *desc = print_generic_expr_to_str (decl);
21070 add_desc_attribute (die, desc);
21071 free (desc);
21075 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21076 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21077 of TYPE accordingly.
21079 ??? This is a temporary measure until after we're able to generate
21080 regular DWARF for the complex Ada type system. */
21082 static void
21083 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21084 dw_die_ref context_die)
21086 tree dtype;
21087 dw_die_ref dtype_die;
21089 if (!lang_hooks.types.descriptive_type)
21090 return;
21092 dtype = lang_hooks.types.descriptive_type (type);
21093 if (!dtype)
21094 return;
21096 dtype_die = lookup_type_die (dtype);
21097 if (!dtype_die)
21099 gen_type_die (dtype, context_die);
21100 dtype_die = lookup_type_die (dtype);
21101 gcc_assert (dtype_die);
21104 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21107 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21109 static const char *
21110 comp_dir_string (void)
21112 const char *wd;
21113 char *wd_plus_sep = NULL;
21114 static const char *cached_wd = NULL;
21116 if (cached_wd != NULL)
21117 return cached_wd;
21119 wd = get_src_pwd ();
21120 if (wd == NULL)
21121 return NULL;
21123 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21125 size_t wdlen = strlen (wd);
21126 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21127 strcpy (wd_plus_sep, wd);
21128 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21129 wd_plus_sep [wdlen + 1] = 0;
21130 wd = wd_plus_sep;
21133 cached_wd = remap_debug_filename (wd);
21135 /* remap_debug_filename can just pass through wd or return a new gc string.
21136 These two types can't be both stored in a GTY(())-tagged string, but since
21137 the cached value lives forever just copy it if needed. */
21138 if (cached_wd != wd)
21140 cached_wd = xstrdup (cached_wd);
21141 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21142 free (wd_plus_sep);
21145 return cached_wd;
21148 /* Generate a DW_AT_comp_dir attribute for DIE. */
21150 static void
21151 add_comp_dir_attribute (dw_die_ref die)
21153 const char * wd = comp_dir_string ();
21154 if (wd != NULL)
21155 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21158 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21159 pointer computation, ...), output a representation for that bound according
21160 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21161 loc_list_from_tree for the meaning of CONTEXT. */
21163 static void
21164 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21165 int forms, struct loc_descr_context *context)
21167 dw_die_ref context_die, decl_die = NULL;
21168 dw_loc_list_ref list;
21169 bool strip_conversions = true;
21170 bool placeholder_seen = false;
21172 while (strip_conversions)
21173 switch (TREE_CODE (value))
21175 case ERROR_MARK:
21176 case SAVE_EXPR:
21177 return;
21179 CASE_CONVERT:
21180 case VIEW_CONVERT_EXPR:
21181 value = TREE_OPERAND (value, 0);
21182 break;
21184 default:
21185 strip_conversions = false;
21186 break;
21189 /* If possible and permitted, output the attribute as a constant. */
21190 if ((forms & dw_scalar_form_constant) != 0
21191 && TREE_CODE (value) == INTEGER_CST)
21193 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21195 /* If HOST_WIDE_INT is big enough then represent the bound as
21196 a constant value. We need to choose a form based on
21197 whether the type is signed or unsigned. We cannot just
21198 call add_AT_unsigned if the value itself is positive
21199 (add_AT_unsigned might add the unsigned value encoded as
21200 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21201 bounds type and then sign extend any unsigned values found
21202 for signed types. This is needed only for
21203 DW_AT_{lower,upper}_bound, since for most other attributes,
21204 consumers will treat DW_FORM_data[1248] as unsigned values,
21205 regardless of the underlying type. */
21206 if (prec <= HOST_BITS_PER_WIDE_INT
21207 || tree_fits_uhwi_p (value))
21209 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21210 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21211 else
21212 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21214 else if (dwarf_version >= 5
21215 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21216 /* Otherwise represent the bound as an unsigned value with
21217 the precision of its type. The precision and signedness
21218 of the type will be necessary to re-interpret it
21219 unambiguously. */
21220 add_AT_wide (die, attr, wi::to_wide (value));
21221 else
21223 rtx v = immed_wide_int_const (wi::to_wide (value),
21224 TYPE_MODE (TREE_TYPE (value)));
21225 dw_loc_descr_ref loc
21226 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21227 VAR_INIT_STATUS_INITIALIZED);
21228 if (loc)
21229 add_AT_loc (die, attr, loc);
21231 return;
21234 /* Otherwise, if it's possible and permitted too, output a reference to
21235 another DIE. */
21236 if ((forms & dw_scalar_form_reference) != 0)
21238 tree decl = NULL_TREE;
21240 /* Some type attributes reference an outer type. For instance, the upper
21241 bound of an array may reference an embedding record (this happens in
21242 Ada). */
21243 if (TREE_CODE (value) == COMPONENT_REF
21244 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21245 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21246 decl = TREE_OPERAND (value, 1);
21248 else if (VAR_P (value)
21249 || TREE_CODE (value) == PARM_DECL
21250 || TREE_CODE (value) == RESULT_DECL)
21251 decl = value;
21253 if (decl != NULL_TREE)
21255 decl_die = lookup_decl_die (decl);
21257 /* ??? Can this happen, or should the variable have been bound
21258 first? Probably it can, since I imagine that we try to create
21259 the types of parameters in the order in which they exist in
21260 the list, and won't have created a forward reference to a
21261 later parameter. */
21262 if (decl_die != NULL)
21264 if (get_AT (decl_die, DW_AT_location)
21265 || get_AT (decl_die, DW_AT_data_member_location)
21266 || get_AT (decl_die, DW_AT_data_bit_offset)
21267 || get_AT (decl_die, DW_AT_const_value))
21269 add_AT_die_ref (die, attr, decl_die);
21270 return;
21276 /* Last chance: try to create a stack operation procedure to evaluate the
21277 value. Do nothing if even that is not possible or permitted. */
21278 if ((forms & dw_scalar_form_exprloc) == 0)
21279 return;
21281 list = loc_list_from_tree (value, 2, context);
21282 if (context && context->placeholder_arg)
21284 placeholder_seen = context->placeholder_seen;
21285 context->placeholder_seen = false;
21287 if (list == NULL || single_element_loc_list_p (list))
21289 /* If this attribute is not a reference nor constant, it is
21290 a DWARF expression rather than location description. For that
21291 loc_list_from_tree (value, 0, &context) is needed. */
21292 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21293 if (list2 && single_element_loc_list_p (list2))
21295 if (placeholder_seen)
21297 struct dwarf_procedure_info dpi;
21298 dpi.fndecl = NULL_TREE;
21299 dpi.args_count = 1;
21300 if (!resolve_args_picking (list2->expr, 1, &dpi))
21301 return;
21303 add_AT_loc (die, attr, list2->expr);
21304 return;
21308 /* If that failed to give a single element location list, fall back to
21309 outputting this as a reference... still if permitted. */
21310 if (list == NULL
21311 || (forms & dw_scalar_form_reference) == 0
21312 || placeholder_seen)
21313 return;
21315 if (!decl_die)
21317 if (current_function_decl == 0)
21318 context_die = comp_unit_die ();
21319 else
21320 context_die = lookup_decl_die (current_function_decl);
21322 decl_die = new_die (DW_TAG_variable, context_die, value);
21323 add_AT_flag (decl_die, DW_AT_artificial, 1);
21324 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21325 context_die);
21328 add_AT_location_description (decl_die, DW_AT_location, list);
21329 add_AT_die_ref (die, attr, decl_die);
21332 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21333 default. */
21335 static int
21336 lower_bound_default (void)
21338 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21340 case DW_LANG_C:
21341 case DW_LANG_C89:
21342 case DW_LANG_C99:
21343 case DW_LANG_C11:
21344 case DW_LANG_C_plus_plus:
21345 case DW_LANG_C_plus_plus_11:
21346 case DW_LANG_C_plus_plus_14:
21347 case DW_LANG_ObjC:
21348 case DW_LANG_ObjC_plus_plus:
21349 return 0;
21350 case DW_LANG_Fortran77:
21351 case DW_LANG_Fortran90:
21352 case DW_LANG_Fortran95:
21353 case DW_LANG_Fortran03:
21354 case DW_LANG_Fortran08:
21355 return 1;
21356 case DW_LANG_UPC:
21357 case DW_LANG_D:
21358 case DW_LANG_Python:
21359 return dwarf_version >= 4 ? 0 : -1;
21360 case DW_LANG_Ada95:
21361 case DW_LANG_Ada83:
21362 case DW_LANG_Cobol74:
21363 case DW_LANG_Cobol85:
21364 case DW_LANG_Modula2:
21365 case DW_LANG_PLI:
21366 return dwarf_version >= 4 ? 1 : -1;
21367 default:
21368 return -1;
21372 /* Given a tree node describing an array bound (either lower or upper) output
21373 a representation for that bound. */
21375 static void
21376 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21377 tree bound, struct loc_descr_context *context)
21379 int dflt;
21381 while (1)
21382 switch (TREE_CODE (bound))
21384 /* Strip all conversions. */
21385 CASE_CONVERT:
21386 case VIEW_CONVERT_EXPR:
21387 bound = TREE_OPERAND (bound, 0);
21388 break;
21390 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21391 are even omitted when they are the default. */
21392 case INTEGER_CST:
21393 /* If the value for this bound is the default one, we can even omit the
21394 attribute. */
21395 if (bound_attr == DW_AT_lower_bound
21396 && tree_fits_shwi_p (bound)
21397 && (dflt = lower_bound_default ()) != -1
21398 && tree_to_shwi (bound) == dflt)
21399 return;
21401 /* FALLTHRU */
21403 default:
21404 /* Let GNAT encodings do the magic for self-referential bounds. */
21405 if (is_ada ()
21406 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21407 && contains_placeholder_p (bound))
21408 return;
21410 add_scalar_info (subrange_die, bound_attr, bound,
21411 dw_scalar_form_constant
21412 | dw_scalar_form_exprloc
21413 | dw_scalar_form_reference,
21414 context);
21415 return;
21419 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21420 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21422 This function reuses previously set type and bound information if
21423 available. */
21425 static void
21426 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21428 dw_die_ref child = type_die->die_child;
21429 struct array_descr_info info;
21430 int dimension_number;
21432 if (lang_hooks.types.get_array_descr_info)
21434 memset (&info, 0, sizeof (info));
21435 if (lang_hooks.types.get_array_descr_info (type, &info))
21436 /* Fortran sometimes emits array types with no dimension. */
21437 gcc_assert (info.ndimensions >= 0
21438 && info.ndimensions
21439 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21441 else
21442 info.ndimensions = 0;
21444 for (dimension_number = 0;
21445 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21446 type = TREE_TYPE (type), dimension_number++)
21448 tree domain = TYPE_DOMAIN (type);
21450 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21451 break;
21453 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21454 and (in GNU C only) variable bounds. Handle all three forms
21455 here. */
21457 /* Find and reuse a previously generated DW_TAG_subrange_type if
21458 available.
21460 For multi-dimensional arrays, as we iterate through the
21461 various dimensions in the enclosing for loop above, we also
21462 iterate through the DIE children and pick at each
21463 DW_TAG_subrange_type previously generated (if available).
21464 Each child DW_TAG_subrange_type DIE describes the range of
21465 the current dimension. At this point we should have as many
21466 DW_TAG_subrange_type's as we have dimensions in the
21467 array. */
21468 dw_die_ref subrange_die = NULL;
21469 if (child)
21470 while (1)
21472 child = child->die_sib;
21473 if (child->die_tag == DW_TAG_subrange_type)
21474 subrange_die = child;
21475 if (child == type_die->die_child)
21477 /* If we wrapped around, stop looking next time. */
21478 child = NULL;
21479 break;
21481 if (child->die_tag == DW_TAG_subrange_type)
21482 break;
21484 if (!subrange_die)
21485 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21487 if (domain)
21489 /* We have an array type with specified bounds. */
21490 tree lower = TYPE_MIN_VALUE (domain);
21491 tree upper = TYPE_MAX_VALUE (domain);
21492 tree index_type = TREE_TYPE (domain);
21494 if (dimension_number <= info.ndimensions - 1)
21496 lower = info.dimen[dimension_number].lower_bound;
21497 upper = info.dimen[dimension_number].upper_bound;
21498 index_type = info.dimen[dimension_number].bounds_type;
21501 /* Define the index type. */
21502 if (index_type && !get_AT (subrange_die, DW_AT_type))
21503 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21504 false, type_die);
21506 /* ??? If upper is NULL, the array has unspecified length,
21507 but it does have a lower bound. This happens with Fortran
21508 dimension arr(N:*)
21509 Since the debugger is definitely going to need to know N
21510 to produce useful results, go ahead and output the lower
21511 bound solo, and hope the debugger can cope. */
21513 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21514 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21516 if (!get_AT (subrange_die, DW_AT_upper_bound)
21517 && !get_AT (subrange_die, DW_AT_count))
21519 if (upper)
21520 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21521 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21522 /* Zero-length array. */
21523 add_bound_info (subrange_die, DW_AT_count,
21524 build_int_cst (TREE_TYPE (lower), 0), NULL);
21528 /* Otherwise we have an array type with an unspecified length. The
21529 DWARF-2 spec does not say how to handle this; let's just leave out the
21530 bounds. */
21534 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21536 static void
21537 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21539 dw_die_ref decl_die;
21540 HOST_WIDE_INT size;
21542 switch (TREE_CODE (tree_node))
21544 case ERROR_MARK:
21545 size = 0;
21546 break;
21547 case ENUMERAL_TYPE:
21548 case RECORD_TYPE:
21549 case UNION_TYPE:
21550 case QUAL_UNION_TYPE:
21551 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21552 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21554 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21555 return;
21557 size = int_size_in_bytes (tree_node);
21558 break;
21559 case FIELD_DECL:
21560 /* For a data member of a struct or union, the DW_AT_byte_size is
21561 generally given as the number of bytes normally allocated for an
21562 object of the *declared* type of the member itself. This is true
21563 even for bit-fields. */
21564 size = int_size_in_bytes (field_type (tree_node));
21565 break;
21566 default:
21567 gcc_unreachable ();
21570 /* Note that `size' might be -1 when we get to this point. If it is, that
21571 indicates that the byte size of the entity in question is variable. */
21572 if (size >= 0)
21573 add_AT_unsigned (die, DW_AT_byte_size, size);
21575 /* Support for dynamically-sized objects was introduced in DWARF3. */
21576 else if (TYPE_P (tree_node)
21577 && (dwarf_version >= 3 || !dwarf_strict)
21578 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21580 struct loc_descr_context ctx = {
21581 const_cast<tree> (tree_node), /* context_type */
21582 NULL_TREE, /* base_decl */
21583 NULL, /* dpi */
21584 false, /* placeholder_arg */
21585 false, /* placeholder_seen */
21586 false /* strict_signedness */
21589 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21590 add_scalar_info (die, DW_AT_byte_size, tree_size,
21591 dw_scalar_form_constant
21592 | dw_scalar_form_exprloc
21593 | dw_scalar_form_reference,
21594 &ctx);
21598 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21599 alignment. */
21601 static void
21602 add_alignment_attribute (dw_die_ref die, tree tree_node)
21604 if (dwarf_version < 5 && dwarf_strict)
21605 return;
21607 unsigned align;
21609 if (DECL_P (tree_node))
21611 if (!DECL_USER_ALIGN (tree_node))
21612 return;
21614 align = DECL_ALIGN_UNIT (tree_node);
21616 else if (TYPE_P (tree_node))
21618 if (!TYPE_USER_ALIGN (tree_node))
21619 return;
21621 align = TYPE_ALIGN_UNIT (tree_node);
21623 else
21624 gcc_unreachable ();
21626 add_AT_unsigned (die, DW_AT_alignment, align);
21629 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21630 which specifies the distance in bits from the highest order bit of the
21631 "containing object" for the bit-field to the highest order bit of the
21632 bit-field itself.
21634 For any given bit-field, the "containing object" is a hypothetical object
21635 (of some integral or enum type) within which the given bit-field lives. The
21636 type of this hypothetical "containing object" is always the same as the
21637 declared type of the individual bit-field itself. The determination of the
21638 exact location of the "containing object" for a bit-field is rather
21639 complicated. It's handled by the `field_byte_offset' function (above).
21641 Note that it is the size (in bytes) of the hypothetical "containing object"
21642 which will be given in the DW_AT_byte_size attribute for this bit-field.
21643 (See `byte_size_attribute' above). */
21645 static inline void
21646 add_bit_offset_attribute (dw_die_ref die, tree decl)
21648 HOST_WIDE_INT object_offset_in_bytes;
21649 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21650 HOST_WIDE_INT bitpos_int;
21651 HOST_WIDE_INT highest_order_object_bit_offset;
21652 HOST_WIDE_INT highest_order_field_bit_offset;
21653 HOST_WIDE_INT bit_offset;
21655 /* The containing object is within the DECL_CONTEXT. */
21656 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21658 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21660 /* Must be a field and a bit field. */
21661 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21663 /* We can't yet handle bit-fields whose offsets are variable, so if we
21664 encounter such things, just return without generating any attribute
21665 whatsoever. Likewise for variable or too large size. */
21666 if (! tree_fits_shwi_p (bit_position (decl))
21667 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21668 return;
21670 bitpos_int = int_bit_position (decl);
21672 /* Note that the bit offset is always the distance (in bits) from the
21673 highest-order bit of the "containing object" to the highest-order bit of
21674 the bit-field itself. Since the "high-order end" of any object or field
21675 is different on big-endian and little-endian machines, the computation
21676 below must take account of these differences. */
21677 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21678 highest_order_field_bit_offset = bitpos_int;
21680 if (! BYTES_BIG_ENDIAN)
21682 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21683 highest_order_object_bit_offset +=
21684 simple_type_size_in_bits (original_type);
21687 bit_offset
21688 = (! BYTES_BIG_ENDIAN
21689 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21690 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21692 if (bit_offset < 0)
21693 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21694 else
21695 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21698 /* For a FIELD_DECL node which represents a bit field, output an attribute
21699 which specifies the length in bits of the given field. */
21701 static inline void
21702 add_bit_size_attribute (dw_die_ref die, tree decl)
21704 /* Must be a field and a bit field. */
21705 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21706 && DECL_BIT_FIELD_TYPE (decl));
21708 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21709 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21712 /* If the compiled language is ANSI C, then add a 'prototyped'
21713 attribute, if arg types are given for the parameters of a function. */
21715 static inline void
21716 add_prototyped_attribute (dw_die_ref die, tree func_type)
21718 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21720 case DW_LANG_C:
21721 case DW_LANG_C89:
21722 case DW_LANG_C99:
21723 case DW_LANG_C11:
21724 case DW_LANG_ObjC:
21725 if (prototype_p (func_type))
21726 add_AT_flag (die, DW_AT_prototyped, 1);
21727 break;
21728 default:
21729 break;
21733 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21734 by looking in the type declaration, the object declaration equate table or
21735 the block mapping. */
21737 static inline void
21738 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21740 dw_die_ref origin_die = NULL;
21742 /* For late LTO debug output we want to refer directly to the abstract
21743 DIE in the early debug rather to the possibly existing concrete
21744 instance and avoid creating that just for this purpose. */
21745 sym_off_pair *desc;
21746 if (in_lto_p
21747 && external_die_map
21748 && (desc = external_die_map->get (origin)))
21750 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21751 desc->sym, desc->off);
21752 return;
21755 if (DECL_P (origin))
21756 origin_die = lookup_decl_die (origin);
21757 else if (TYPE_P (origin))
21758 origin_die = lookup_type_die (origin);
21759 else if (TREE_CODE (origin) == BLOCK)
21760 origin_die = lookup_block_die (origin);
21762 /* XXX: Functions that are never lowered don't always have correct block
21763 trees (in the case of java, they simply have no block tree, in some other
21764 languages). For these functions, there is nothing we can really do to
21765 output correct debug info for inlined functions in all cases. Rather
21766 than die, we'll just produce deficient debug info now, in that we will
21767 have variables without a proper abstract origin. In the future, when all
21768 functions are lowered, we should re-add a gcc_assert (origin_die)
21769 here. */
21771 if (origin_die)
21773 dw_attr_node *a;
21774 /* Like above, if we already created a concrete instance DIE
21775 do not use that for the abstract origin but the early DIE
21776 if present. */
21777 if (in_lto_p
21778 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21779 origin_die = AT_ref (a);
21780 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21784 /* We do not currently support the pure_virtual attribute. */
21786 static inline void
21787 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21789 if (DECL_VINDEX (func_decl))
21791 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21793 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21794 add_AT_loc (die, DW_AT_vtable_elem_location,
21795 new_loc_descr (DW_OP_constu,
21796 tree_to_shwi (DECL_VINDEX (func_decl)),
21797 0));
21799 /* GNU extension: Record what type this method came from originally. */
21800 if (debug_info_level > DINFO_LEVEL_TERSE
21801 && DECL_CONTEXT (func_decl))
21802 add_AT_die_ref (die, DW_AT_containing_type,
21803 lookup_type_die (DECL_CONTEXT (func_decl)));
21807 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21808 given decl. This used to be a vendor extension until after DWARF 4
21809 standardized it. */
21811 static void
21812 add_linkage_attr (dw_die_ref die, tree decl)
21814 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21816 /* Mimic what assemble_name_raw does with a leading '*'. */
21817 if (name[0] == '*')
21818 name = &name[1];
21820 if (dwarf_version >= 4)
21821 add_AT_string (die, DW_AT_linkage_name, name);
21822 else
21823 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21826 /* Add source coordinate attributes for the given decl. */
21828 static void
21829 add_src_coords_attributes (dw_die_ref die, tree decl)
21831 expanded_location s;
21833 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21834 return;
21835 s = expand_location (DECL_SOURCE_LOCATION (decl));
21836 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21837 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21838 if (debug_column_info && s.column)
21839 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21842 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21844 static void
21845 add_linkage_name_raw (dw_die_ref die, tree decl)
21847 /* Defer until we have an assembler name set. */
21848 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21850 limbo_die_node *asm_name;
21852 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21853 asm_name->die = die;
21854 asm_name->created_for = decl;
21855 asm_name->next = deferred_asm_name;
21856 deferred_asm_name = asm_name;
21858 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21859 add_linkage_attr (die, decl);
21862 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21864 static void
21865 add_linkage_name (dw_die_ref die, tree decl)
21867 if (debug_info_level > DINFO_LEVEL_NONE
21868 && VAR_OR_FUNCTION_DECL_P (decl)
21869 && TREE_PUBLIC (decl)
21870 && !(VAR_P (decl) && DECL_REGISTER (decl))
21871 && die->die_tag != DW_TAG_member)
21872 add_linkage_name_raw (die, decl);
21875 /* Add a DW_AT_name attribute and source coordinate attribute for the
21876 given decl, but only if it actually has a name. */
21878 static void
21879 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21880 bool no_linkage_name)
21882 tree decl_name;
21884 decl_name = DECL_NAME (decl);
21885 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21887 const char *name = dwarf2_name (decl, 0);
21888 if (name)
21889 add_name_attribute (die, name);
21890 else
21891 add_desc_attribute (die, decl);
21893 if (! DECL_ARTIFICIAL (decl))
21894 add_src_coords_attributes (die, decl);
21896 if (!no_linkage_name)
21897 add_linkage_name (die, decl);
21899 else
21900 add_desc_attribute (die, decl);
21902 #ifdef VMS_DEBUGGING_INFO
21903 /* Get the function's name, as described by its RTL. This may be different
21904 from the DECL_NAME name used in the source file. */
21905 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21907 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21908 XEXP (DECL_RTL (decl), 0), false);
21909 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21911 #endif /* VMS_DEBUGGING_INFO */
21914 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21916 static void
21917 add_discr_value (dw_die_ref die, dw_discr_value *value)
21919 dw_attr_node attr;
21921 attr.dw_attr = DW_AT_discr_value;
21922 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21923 attr.dw_attr_val.val_entry = NULL;
21924 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21925 if (value->pos)
21926 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21927 else
21928 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21929 add_dwarf_attr (die, &attr);
21932 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21934 static void
21935 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21937 dw_attr_node attr;
21939 attr.dw_attr = DW_AT_discr_list;
21940 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21941 attr.dw_attr_val.val_entry = NULL;
21942 attr.dw_attr_val.v.val_discr_list = discr_list;
21943 add_dwarf_attr (die, &attr);
21946 static inline dw_discr_list_ref
21947 AT_discr_list (dw_attr_node *attr)
21949 return attr->dw_attr_val.v.val_discr_list;
21952 #ifdef VMS_DEBUGGING_INFO
21953 /* Output the debug main pointer die for VMS */
21955 void
21956 dwarf2out_vms_debug_main_pointer (void)
21958 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21959 dw_die_ref die;
21961 /* Allocate the VMS debug main subprogram die. */
21962 die = new_die_raw (DW_TAG_subprogram);
21963 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21964 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21965 current_function_funcdef_no);
21966 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21968 /* Make it the first child of comp_unit_die (). */
21969 die->die_parent = comp_unit_die ();
21970 if (comp_unit_die ()->die_child)
21972 die->die_sib = comp_unit_die ()->die_child->die_sib;
21973 comp_unit_die ()->die_child->die_sib = die;
21975 else
21977 die->die_sib = die;
21978 comp_unit_die ()->die_child = die;
21981 #endif /* VMS_DEBUGGING_INFO */
21983 /* walk_tree helper function for uses_local_type, below. */
21985 static tree
21986 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21988 if (!TYPE_P (*tp))
21989 *walk_subtrees = 0;
21990 else
21992 tree name = TYPE_NAME (*tp);
21993 if (name && DECL_P (name) && decl_function_context (name))
21994 return *tp;
21996 return NULL_TREE;
21999 /* If TYPE involves a function-local type (including a local typedef to a
22000 non-local type), returns that type; otherwise returns NULL_TREE. */
22002 static tree
22003 uses_local_type (tree type)
22005 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22006 return used;
22009 /* Return the DIE for the scope that immediately contains this type.
22010 Non-named types that do not involve a function-local type get global
22011 scope. Named types nested in namespaces or other types get their
22012 containing scope. All other types (i.e. function-local named types) get
22013 the current active scope. */
22015 static dw_die_ref
22016 scope_die_for (tree t, dw_die_ref context_die)
22018 dw_die_ref scope_die = NULL;
22019 tree containing_scope;
22021 /* Non-types always go in the current scope. */
22022 gcc_assert (TYPE_P (t));
22024 /* Use the scope of the typedef, rather than the scope of the type
22025 it refers to. */
22026 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22027 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22028 else
22029 containing_scope = TYPE_CONTEXT (t);
22031 /* Use the containing namespace if there is one. */
22032 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22034 if (context_die == lookup_decl_die (containing_scope))
22035 /* OK */;
22036 else if (debug_info_level > DINFO_LEVEL_TERSE)
22037 context_die = get_context_die (containing_scope);
22038 else
22039 containing_scope = NULL_TREE;
22042 /* Ignore function type "scopes" from the C frontend. They mean that
22043 a tagged type is local to a parmlist of a function declarator, but
22044 that isn't useful to DWARF. */
22045 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22046 containing_scope = NULL_TREE;
22048 if (SCOPE_FILE_SCOPE_P (containing_scope))
22050 /* If T uses a local type keep it local as well, to avoid references
22051 to function-local DIEs from outside the function. */
22052 if (current_function_decl && uses_local_type (t))
22053 scope_die = context_die;
22054 else
22055 scope_die = comp_unit_die ();
22057 else if (TYPE_P (containing_scope))
22059 /* For types, we can just look up the appropriate DIE. */
22060 if (debug_info_level > DINFO_LEVEL_TERSE)
22061 scope_die = get_context_die (containing_scope);
22062 else
22064 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22065 if (scope_die == NULL)
22066 scope_die = comp_unit_die ();
22069 else
22070 scope_die = context_die;
22072 return scope_die;
22075 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22077 static inline int
22078 local_scope_p (dw_die_ref context_die)
22080 for (; context_die; context_die = context_die->die_parent)
22081 if (context_die->die_tag == DW_TAG_inlined_subroutine
22082 || context_die->die_tag == DW_TAG_subprogram)
22083 return 1;
22085 return 0;
22088 /* Returns nonzero if CONTEXT_DIE is a class. */
22090 static inline int
22091 class_scope_p (dw_die_ref context_die)
22093 return (context_die
22094 && (context_die->die_tag == DW_TAG_structure_type
22095 || context_die->die_tag == DW_TAG_class_type
22096 || context_die->die_tag == DW_TAG_interface_type
22097 || context_die->die_tag == DW_TAG_union_type));
22100 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22101 whether or not to treat a DIE in this context as a declaration. */
22103 static inline int
22104 class_or_namespace_scope_p (dw_die_ref context_die)
22106 return (class_scope_p (context_die)
22107 || (context_die && context_die->die_tag == DW_TAG_namespace));
22110 /* Many forms of DIEs require a "type description" attribute. This
22111 routine locates the proper "type descriptor" die for the type given
22112 by 'type' plus any additional qualifiers given by 'cv_quals', and
22113 adds a DW_AT_type attribute below the given die. */
22115 static void
22116 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22117 bool reverse, dw_die_ref context_die)
22119 enum tree_code code = TREE_CODE (type);
22120 dw_die_ref type_die = NULL;
22122 if (debug_info_level <= DINFO_LEVEL_TERSE)
22123 return;
22125 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22126 or fixed-point type, use the inner type. This is because we have no
22127 support for unnamed types in base_type_die. This can happen if this is
22128 an Ada subrange type. Correct solution is emit a subrange type die. */
22129 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22130 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22131 type = TREE_TYPE (type), code = TREE_CODE (type);
22133 if (code == ERROR_MARK
22134 /* Handle a special case. For functions whose return type is void, we
22135 generate *no* type attribute. (Note that no object may have type
22136 `void', so this only applies to function return types). */
22137 || code == VOID_TYPE)
22138 return;
22140 type_die = modified_type_die (type,
22141 cv_quals | TYPE_QUALS (type),
22142 reverse,
22143 context_die);
22145 if (type_die != NULL)
22146 add_AT_die_ref (object_die, DW_AT_type, type_die);
22149 /* Given an object die, add the calling convention attribute for the
22150 function call type. */
22151 static void
22152 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22154 enum dwarf_calling_convention value = DW_CC_normal;
22156 value = ((enum dwarf_calling_convention)
22157 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22159 if (is_fortran ()
22160 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22162 /* DWARF 2 doesn't provide a way to identify a program's source-level
22163 entry point. DW_AT_calling_convention attributes are only meant
22164 to describe functions' calling conventions. However, lacking a
22165 better way to signal the Fortran main program, we used this for
22166 a long time, following existing custom. Now, DWARF 4 has
22167 DW_AT_main_subprogram, which we add below, but some tools still
22168 rely on the old way, which we thus keep. */
22169 value = DW_CC_program;
22171 if (dwarf_version >= 4 || !dwarf_strict)
22172 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22175 /* Only add the attribute if the backend requests it, and
22176 is not DW_CC_normal. */
22177 if (value && (value != DW_CC_normal))
22178 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22181 /* Given a tree pointer to a struct, class, union, or enum type node, return
22182 a pointer to the (string) tag name for the given type, or zero if the type
22183 was declared without a tag. */
22185 static const char *
22186 type_tag (const_tree type)
22188 const char *name = 0;
22190 if (TYPE_NAME (type) != 0)
22192 tree t = 0;
22194 /* Find the IDENTIFIER_NODE for the type name. */
22195 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22196 && !TYPE_NAMELESS (type))
22197 t = TYPE_NAME (type);
22199 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22200 a TYPE_DECL node, regardless of whether or not a `typedef' was
22201 involved. */
22202 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22203 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22205 /* We want to be extra verbose. Don't call dwarf_name if
22206 DECL_NAME isn't set. The default hook for decl_printable_name
22207 doesn't like that, and in this context it's correct to return
22208 0, instead of "<anonymous>" or the like. */
22209 if (DECL_NAME (TYPE_NAME (type))
22210 && !DECL_NAMELESS (TYPE_NAME (type)))
22211 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22214 /* Now get the name as a string, or invent one. */
22215 if (!name && t != 0)
22216 name = IDENTIFIER_POINTER (t);
22219 return (name == 0 || *name == '\0') ? 0 : name;
22222 /* Return the type associated with a data member, make a special check
22223 for bit field types. */
22225 static inline tree
22226 member_declared_type (const_tree member)
22228 return (DECL_BIT_FIELD_TYPE (member)
22229 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22232 /* Get the decl's label, as described by its RTL. This may be different
22233 from the DECL_NAME name used in the source file. */
22235 #if 0
22236 static const char *
22237 decl_start_label (tree decl)
22239 rtx x;
22240 const char *fnname;
22242 x = DECL_RTL (decl);
22243 gcc_assert (MEM_P (x));
22245 x = XEXP (x, 0);
22246 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22248 fnname = XSTR (x, 0);
22249 return fnname;
22251 #endif
22253 /* For variable-length arrays that have been previously generated, but
22254 may be incomplete due to missing subscript info, fill the subscript
22255 info. Return TRUE if this is one of those cases. */
22257 static bool
22258 fill_variable_array_bounds (tree type)
22260 if (TREE_ASM_WRITTEN (type)
22261 && TREE_CODE (type) == ARRAY_TYPE
22262 && variably_modified_type_p (type, NULL))
22264 dw_die_ref array_die = lookup_type_die (type);
22265 if (!array_die)
22266 return false;
22267 add_subscript_info (array_die, type, !is_ada ());
22268 return true;
22270 return false;
22273 /* These routines generate the internal representation of the DIE's for
22274 the compilation unit. Debugging information is collected by walking
22275 the declaration trees passed in from dwarf2out_decl(). */
22277 static void
22278 gen_array_type_die (tree type, dw_die_ref context_die)
22280 dw_die_ref array_die;
22282 /* GNU compilers represent multidimensional array types as sequences of one
22283 dimensional array types whose element types are themselves array types.
22284 We sometimes squish that down to a single array_type DIE with multiple
22285 subscripts in the Dwarf debugging info. The draft Dwarf specification
22286 say that we are allowed to do this kind of compression in C, because
22287 there is no difference between an array of arrays and a multidimensional
22288 array. We don't do this for Ada to remain as close as possible to the
22289 actual representation, which is especially important against the language
22290 flexibilty wrt arrays of variable size. */
22292 bool collapse_nested_arrays = !is_ada ();
22294 if (fill_variable_array_bounds (type))
22295 return;
22297 dw_die_ref scope_die = scope_die_for (type, context_die);
22298 tree element_type;
22300 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22301 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22302 if (TREE_CODE (type) == ARRAY_TYPE
22303 && TYPE_STRING_FLAG (type)
22304 && is_fortran ()
22305 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22307 HOST_WIDE_INT size;
22309 array_die = new_die (DW_TAG_string_type, scope_die, type);
22310 add_name_attribute (array_die, type_tag (type));
22311 equate_type_number_to_die (type, array_die);
22312 size = int_size_in_bytes (type);
22313 if (size >= 0)
22314 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22315 /* ??? We can't annotate types late, but for LTO we may not
22316 generate a location early either (gfortran.dg/save_6.f90). */
22317 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22318 && TYPE_DOMAIN (type) != NULL_TREE
22319 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22321 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22322 tree rszdecl = szdecl;
22324 size = int_size_in_bytes (TREE_TYPE (szdecl));
22325 if (!DECL_P (szdecl))
22327 if (TREE_CODE (szdecl) == INDIRECT_REF
22328 && DECL_P (TREE_OPERAND (szdecl, 0)))
22330 rszdecl = TREE_OPERAND (szdecl, 0);
22331 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22332 != DWARF2_ADDR_SIZE)
22333 size = 0;
22335 else
22336 size = 0;
22338 if (size > 0)
22340 dw_loc_list_ref loc
22341 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22342 NULL);
22343 if (loc)
22345 add_AT_location_description (array_die, DW_AT_string_length,
22346 loc);
22347 if (size != DWARF2_ADDR_SIZE)
22348 add_AT_unsigned (array_die, dwarf_version >= 5
22349 ? DW_AT_string_length_byte_size
22350 : DW_AT_byte_size, size);
22354 return;
22357 array_die = new_die (DW_TAG_array_type, scope_die, type);
22358 add_name_attribute (array_die, type_tag (type));
22359 equate_type_number_to_die (type, array_die);
22361 if (TREE_CODE (type) == VECTOR_TYPE)
22362 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22364 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22365 if (is_fortran ()
22366 && TREE_CODE (type) == ARRAY_TYPE
22367 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22368 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22369 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22371 #if 0
22372 /* We default the array ordering. Debuggers will probably do the right
22373 things even if DW_AT_ordering is not present. It's not even an issue
22374 until we start to get into multidimensional arrays anyway. If a debugger
22375 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22376 then we'll have to put the DW_AT_ordering attribute back in. (But if
22377 and when we find out that we need to put these in, we will only do so
22378 for multidimensional arrays. */
22379 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22380 #endif
22382 if (TREE_CODE (type) == VECTOR_TYPE)
22384 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22385 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22386 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22387 add_bound_info (subrange_die, DW_AT_upper_bound,
22388 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22390 else
22391 add_subscript_info (array_die, type, collapse_nested_arrays);
22393 /* Add representation of the type of the elements of this array type and
22394 emit the corresponding DIE if we haven't done it already. */
22395 element_type = TREE_TYPE (type);
22396 if (collapse_nested_arrays)
22397 while (TREE_CODE (element_type) == ARRAY_TYPE)
22399 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22400 break;
22401 element_type = TREE_TYPE (element_type);
22404 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22405 TREE_CODE (type) == ARRAY_TYPE
22406 && TYPE_REVERSE_STORAGE_ORDER (type),
22407 context_die);
22409 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22410 if (TYPE_ARTIFICIAL (type))
22411 add_AT_flag (array_die, DW_AT_artificial, 1);
22413 if (get_AT (array_die, DW_AT_name))
22414 add_pubtype (type, array_die);
22416 add_alignment_attribute (array_die, type);
22419 /* This routine generates DIE for array with hidden descriptor, details
22420 are filled into *info by a langhook. */
22422 static void
22423 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22424 dw_die_ref context_die)
22426 const dw_die_ref scope_die = scope_die_for (type, context_die);
22427 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22428 struct loc_descr_context context = {
22429 type, /* context_type */
22430 info->base_decl, /* base_decl */
22431 NULL, /* dpi */
22432 false, /* placeholder_arg */
22433 false, /* placeholder_seen */
22434 false /* strict_signedness */
22436 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22437 int dim;
22439 add_name_attribute (array_die, type_tag (type));
22440 equate_type_number_to_die (type, array_die);
22442 if (info->ndimensions > 1)
22443 switch (info->ordering)
22445 case array_descr_ordering_row_major:
22446 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22447 break;
22448 case array_descr_ordering_column_major:
22449 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22450 break;
22451 default:
22452 break;
22455 if (dwarf_version >= 3 || !dwarf_strict)
22457 if (info->data_location)
22458 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22459 dw_scalar_form_exprloc, &context);
22460 if (info->associated)
22461 add_scalar_info (array_die, DW_AT_associated, info->associated,
22462 dw_scalar_form_constant
22463 | dw_scalar_form_exprloc
22464 | dw_scalar_form_reference, &context);
22465 if (info->allocated)
22466 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22467 dw_scalar_form_constant
22468 | dw_scalar_form_exprloc
22469 | dw_scalar_form_reference, &context);
22470 if (info->stride)
22472 const enum dwarf_attribute attr
22473 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22474 const int forms
22475 = (info->stride_in_bits)
22476 ? dw_scalar_form_constant
22477 : (dw_scalar_form_constant
22478 | dw_scalar_form_exprloc
22479 | dw_scalar_form_reference);
22481 add_scalar_info (array_die, attr, info->stride, forms, &context);
22484 if (dwarf_version >= 5)
22486 if (info->rank)
22488 add_scalar_info (array_die, DW_AT_rank, info->rank,
22489 dw_scalar_form_constant
22490 | dw_scalar_form_exprloc, &context);
22491 subrange_tag = DW_TAG_generic_subrange;
22492 context.placeholder_arg = true;
22496 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22498 for (dim = 0; dim < info->ndimensions; dim++)
22500 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22502 if (info->dimen[dim].bounds_type)
22503 add_type_attribute (subrange_die,
22504 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22505 false, context_die);
22506 if (info->dimen[dim].lower_bound)
22507 add_bound_info (subrange_die, DW_AT_lower_bound,
22508 info->dimen[dim].lower_bound, &context);
22509 if (info->dimen[dim].upper_bound)
22510 add_bound_info (subrange_die, DW_AT_upper_bound,
22511 info->dimen[dim].upper_bound, &context);
22512 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22513 add_scalar_info (subrange_die, DW_AT_byte_stride,
22514 info->dimen[dim].stride,
22515 dw_scalar_form_constant
22516 | dw_scalar_form_exprloc
22517 | dw_scalar_form_reference,
22518 &context);
22521 gen_type_die (info->element_type, context_die);
22522 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22523 TREE_CODE (type) == ARRAY_TYPE
22524 && TYPE_REVERSE_STORAGE_ORDER (type),
22525 context_die);
22527 if (get_AT (array_die, DW_AT_name))
22528 add_pubtype (type, array_die);
22530 add_alignment_attribute (array_die, type);
22533 #if 0
22534 static void
22535 gen_entry_point_die (tree decl, dw_die_ref context_die)
22537 tree origin = decl_ultimate_origin (decl);
22538 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22540 if (origin != NULL)
22541 add_abstract_origin_attribute (decl_die, origin);
22542 else
22544 add_name_and_src_coords_attributes (decl_die, decl);
22545 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22546 TYPE_UNQUALIFIED, false, context_die);
22549 if (DECL_ABSTRACT_P (decl))
22550 equate_decl_number_to_die (decl, decl_die);
22551 else
22552 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22554 #endif
22556 /* Walk through the list of incomplete types again, trying once more to
22557 emit full debugging info for them. */
22559 static void
22560 retry_incomplete_types (void)
22562 set_early_dwarf s;
22563 int i;
22565 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22566 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22567 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22568 vec_safe_truncate (incomplete_types, 0);
22571 /* Determine what tag to use for a record type. */
22573 static enum dwarf_tag
22574 record_type_tag (tree type)
22576 if (! lang_hooks.types.classify_record)
22577 return DW_TAG_structure_type;
22579 switch (lang_hooks.types.classify_record (type))
22581 case RECORD_IS_STRUCT:
22582 return DW_TAG_structure_type;
22584 case RECORD_IS_CLASS:
22585 return DW_TAG_class_type;
22587 case RECORD_IS_INTERFACE:
22588 if (dwarf_version >= 3 || !dwarf_strict)
22589 return DW_TAG_interface_type;
22590 return DW_TAG_structure_type;
22592 default:
22593 gcc_unreachable ();
22597 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22598 include all of the information about the enumeration values also. Each
22599 enumerated type name/value is listed as a child of the enumerated type
22600 DIE. */
22602 static dw_die_ref
22603 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22605 dw_die_ref type_die = lookup_type_die (type);
22606 dw_die_ref orig_type_die = type_die;
22608 if (type_die == NULL)
22610 type_die = new_die (DW_TAG_enumeration_type,
22611 scope_die_for (type, context_die), type);
22612 equate_type_number_to_die (type, type_die);
22613 add_name_attribute (type_die, type_tag (type));
22614 if ((dwarf_version >= 4 || !dwarf_strict)
22615 && ENUM_IS_SCOPED (type))
22616 add_AT_flag (type_die, DW_AT_enum_class, 1);
22617 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22618 add_AT_flag (type_die, DW_AT_declaration, 1);
22619 if (!dwarf_strict)
22620 add_AT_unsigned (type_die, DW_AT_encoding,
22621 TYPE_UNSIGNED (type)
22622 ? DW_ATE_unsigned
22623 : DW_ATE_signed);
22625 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22626 return type_die;
22627 else
22628 remove_AT (type_die, DW_AT_declaration);
22630 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22631 given enum type is incomplete, do not generate the DW_AT_byte_size
22632 attribute or the DW_AT_element_list attribute. */
22633 if (TYPE_SIZE (type))
22635 tree link;
22637 if (!ENUM_IS_OPAQUE (type))
22638 TREE_ASM_WRITTEN (type) = 1;
22639 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22640 add_byte_size_attribute (type_die, type);
22641 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22642 add_alignment_attribute (type_die, type);
22643 if ((dwarf_version >= 3 || !dwarf_strict)
22644 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22646 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22647 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22648 context_die);
22650 if (TYPE_STUB_DECL (type) != NULL_TREE)
22652 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22653 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22654 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22655 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22658 /* If the first reference to this type was as the return type of an
22659 inline function, then it may not have a parent. Fix this now. */
22660 if (type_die->die_parent == NULL)
22661 add_child_die (scope_die_for (type, context_die), type_die);
22663 for (link = TYPE_VALUES (type);
22664 link != NULL; link = TREE_CHAIN (link))
22666 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22667 tree value = TREE_VALUE (link);
22669 if (DECL_P (value))
22670 equate_decl_number_to_die (value, enum_die);
22672 gcc_assert (!ENUM_IS_OPAQUE (type));
22673 add_name_attribute (enum_die,
22674 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22676 if (TREE_CODE (value) == CONST_DECL)
22677 value = DECL_INITIAL (value);
22679 if (simple_type_size_in_bits (TREE_TYPE (value))
22680 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22682 /* For constant forms created by add_AT_unsigned DWARF
22683 consumers (GDB, elfutils, etc.) always zero extend
22684 the value. Only when the actual value is negative
22685 do we need to use add_AT_int to generate a constant
22686 form that can represent negative values. */
22687 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22688 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22689 add_AT_unsigned (enum_die, DW_AT_const_value,
22690 (unsigned HOST_WIDE_INT) val);
22691 else
22692 add_AT_int (enum_die, DW_AT_const_value, val);
22694 else
22695 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22696 that here. TODO: This should be re-worked to use correct
22697 signed/unsigned double tags for all cases. */
22698 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22701 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22702 if (TYPE_ARTIFICIAL (type)
22703 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22704 add_AT_flag (type_die, DW_AT_artificial, 1);
22706 else
22707 add_AT_flag (type_die, DW_AT_declaration, 1);
22709 add_pubtype (type, type_die);
22711 return type_die;
22714 /* Generate a DIE to represent either a real live formal parameter decl or to
22715 represent just the type of some formal parameter position in some function
22716 type.
22718 Note that this routine is a bit unusual because its argument may be a
22719 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22720 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22721 node. If it's the former then this function is being called to output a
22722 DIE to represent a formal parameter object (or some inlining thereof). If
22723 it's the latter, then this function is only being called to output a
22724 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22725 argument type of some subprogram type.
22726 If EMIT_NAME_P is true, name and source coordinate attributes
22727 are emitted. */
22729 static dw_die_ref
22730 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22731 dw_die_ref context_die)
22733 tree node_or_origin = node ? node : origin;
22734 tree ultimate_origin;
22735 dw_die_ref parm_die = NULL;
22737 if (DECL_P (node_or_origin))
22739 parm_die = lookup_decl_die (node);
22741 /* If the contexts differ, we may not be talking about the same
22742 thing.
22743 ??? When in LTO the DIE parent is the "abstract" copy and the
22744 context_die is the specification "copy". */
22745 if (parm_die
22746 && parm_die->die_parent != context_die
22747 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22748 || parm_die->die_parent->die_parent != context_die)
22749 && !in_lto_p)
22751 gcc_assert (!DECL_ABSTRACT_P (node));
22752 /* This can happen when creating a concrete instance, in
22753 which case we need to create a new DIE that will get
22754 annotated with DW_AT_abstract_origin. */
22755 parm_die = NULL;
22758 if (parm_die && parm_die->die_parent == NULL)
22760 /* Check that parm_die already has the right attributes that
22761 we would have added below. If any attributes are
22762 missing, fall through to add them. */
22763 if (! DECL_ABSTRACT_P (node_or_origin)
22764 && !get_AT (parm_die, DW_AT_location)
22765 && !get_AT (parm_die, DW_AT_const_value))
22766 /* We are missing location info, and are about to add it. */
22768 else
22770 add_child_die (context_die, parm_die);
22771 return parm_die;
22776 /* If we have a previously generated DIE, use it, unless this is an
22777 concrete instance (origin != NULL), in which case we need a new
22778 DIE with a corresponding DW_AT_abstract_origin. */
22779 bool reusing_die;
22780 if (parm_die && origin == NULL)
22781 reusing_die = true;
22782 else
22784 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22785 reusing_die = false;
22788 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22790 case tcc_declaration:
22791 ultimate_origin = decl_ultimate_origin (node_or_origin);
22792 if (node || ultimate_origin)
22793 origin = ultimate_origin;
22795 if (reusing_die)
22796 goto add_location;
22798 if (origin != NULL)
22799 add_abstract_origin_attribute (parm_die, origin);
22800 else if (emit_name_p)
22801 add_name_and_src_coords_attributes (parm_die, node);
22802 if (origin == NULL
22803 || (! DECL_ABSTRACT_P (node_or_origin)
22804 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22805 decl_function_context
22806 (node_or_origin))))
22808 tree type = TREE_TYPE (node_or_origin);
22809 if (decl_by_reference_p (node_or_origin))
22810 add_type_attribute (parm_die, TREE_TYPE (type),
22811 TYPE_UNQUALIFIED,
22812 false, context_die);
22813 else
22814 add_type_attribute (parm_die, type,
22815 decl_quals (node_or_origin),
22816 false, context_die);
22818 if (origin == NULL && DECL_ARTIFICIAL (node))
22819 add_AT_flag (parm_die, DW_AT_artificial, 1);
22820 add_location:
22821 if (node && node != origin)
22822 equate_decl_number_to_die (node, parm_die);
22823 if (! DECL_ABSTRACT_P (node_or_origin))
22824 add_location_or_const_value_attribute (parm_die, node_or_origin,
22825 node == NULL);
22827 break;
22829 case tcc_type:
22830 /* We were called with some kind of a ..._TYPE node. */
22831 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22832 context_die);
22833 break;
22835 default:
22836 gcc_unreachable ();
22839 return parm_die;
22842 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22843 children DW_TAG_formal_parameter DIEs representing the arguments of the
22844 parameter pack.
22846 PARM_PACK must be a function parameter pack.
22847 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22848 must point to the subsequent arguments of the function PACK_ARG belongs to.
22849 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22850 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22851 following the last one for which a DIE was generated. */
22853 static dw_die_ref
22854 gen_formal_parameter_pack_die (tree parm_pack,
22855 tree pack_arg,
22856 dw_die_ref subr_die,
22857 tree *next_arg)
22859 tree arg;
22860 dw_die_ref parm_pack_die;
22862 gcc_assert (parm_pack
22863 && lang_hooks.function_parameter_pack_p (parm_pack)
22864 && subr_die);
22866 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22867 add_src_coords_attributes (parm_pack_die, parm_pack);
22869 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22871 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22872 parm_pack))
22873 break;
22874 gen_formal_parameter_die (arg, NULL,
22875 false /* Don't emit name attribute. */,
22876 parm_pack_die);
22878 if (next_arg)
22879 *next_arg = arg;
22880 return parm_pack_die;
22883 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22884 at the end of an (ANSI prototyped) formal parameters list. */
22886 static void
22887 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22889 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22892 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22893 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22894 parameters as specified in some function type specification (except for
22895 those which appear as part of a function *definition*). */
22897 static void
22898 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22900 tree link;
22901 tree formal_type = NULL;
22902 tree first_parm_type;
22903 tree arg;
22905 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22907 arg = DECL_ARGUMENTS (function_or_method_type);
22908 function_or_method_type = TREE_TYPE (function_or_method_type);
22910 else
22911 arg = NULL_TREE;
22913 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22915 /* Make our first pass over the list of formal parameter types and output a
22916 DW_TAG_formal_parameter DIE for each one. */
22917 for (link = first_parm_type; link; )
22919 dw_die_ref parm_die;
22921 formal_type = TREE_VALUE (link);
22922 if (formal_type == void_type_node)
22923 break;
22925 /* Output a (nameless) DIE to represent the formal parameter itself. */
22926 parm_die = gen_formal_parameter_die (formal_type, NULL,
22927 true /* Emit name attribute. */,
22928 context_die);
22929 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22930 && link == first_parm_type)
22932 add_AT_flag (parm_die, DW_AT_artificial, 1);
22933 if (dwarf_version >= 3 || !dwarf_strict)
22934 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22936 else if (arg && DECL_ARTIFICIAL (arg))
22937 add_AT_flag (parm_die, DW_AT_artificial, 1);
22939 link = TREE_CHAIN (link);
22940 if (arg)
22941 arg = DECL_CHAIN (arg);
22944 /* If this function type has an ellipsis, add a
22945 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22946 if (formal_type != void_type_node)
22947 gen_unspecified_parameters_die (function_or_method_type, context_die);
22949 /* Make our second (and final) pass over the list of formal parameter types
22950 and output DIEs to represent those types (as necessary). */
22951 for (link = TYPE_ARG_TYPES (function_or_method_type);
22952 link && TREE_VALUE (link);
22953 link = TREE_CHAIN (link))
22954 gen_type_die (TREE_VALUE (link), context_die);
22957 /* We want to generate the DIE for TYPE so that we can generate the
22958 die for MEMBER, which has been defined; we will need to refer back
22959 to the member declaration nested within TYPE. If we're trying to
22960 generate minimal debug info for TYPE, processing TYPE won't do the
22961 trick; we need to attach the member declaration by hand. */
22963 static void
22964 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22966 gen_type_die (type, context_die);
22968 /* If we're trying to avoid duplicate debug info, we may not have
22969 emitted the member decl for this function. Emit it now. */
22970 if (TYPE_STUB_DECL (type)
22971 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22972 && ! lookup_decl_die (member))
22974 dw_die_ref type_die;
22975 gcc_assert (!decl_ultimate_origin (member));
22977 type_die = lookup_type_die_strip_naming_typedef (type);
22978 if (TREE_CODE (member) == FUNCTION_DECL)
22979 gen_subprogram_die (member, type_die);
22980 else if (TREE_CODE (member) == FIELD_DECL)
22982 /* Ignore the nameless fields that are used to skip bits but handle
22983 C++ anonymous unions and structs. */
22984 if (DECL_NAME (member) != NULL_TREE
22985 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22986 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22988 struct vlr_context vlr_ctx = {
22989 DECL_CONTEXT (member), /* struct_type */
22990 NULL_TREE /* variant_part_offset */
22992 gen_type_die (member_declared_type (member), type_die);
22993 gen_field_die (member, &vlr_ctx, type_die);
22996 else
22997 gen_variable_die (member, NULL_TREE, type_die);
23001 /* Forward declare these functions, because they are mutually recursive
23002 with their set_block_* pairing functions. */
23003 static void set_decl_origin_self (tree);
23005 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23006 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23007 that it points to the node itself, thus indicating that the node is its
23008 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23009 the given node is NULL, recursively descend the decl/block tree which
23010 it is the root of, and for each other ..._DECL or BLOCK node contained
23011 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23012 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23013 values to point to themselves. */
23015 static void
23016 set_block_origin_self (tree stmt)
23018 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23020 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23023 tree local_decl;
23025 for (local_decl = BLOCK_VARS (stmt);
23026 local_decl != NULL_TREE;
23027 local_decl = DECL_CHAIN (local_decl))
23028 /* Do not recurse on nested functions since the inlining status
23029 of parent and child can be different as per the DWARF spec. */
23030 if (TREE_CODE (local_decl) != FUNCTION_DECL
23031 && !DECL_EXTERNAL (local_decl))
23032 set_decl_origin_self (local_decl);
23036 tree subblock;
23038 for (subblock = BLOCK_SUBBLOCKS (stmt);
23039 subblock != NULL_TREE;
23040 subblock = BLOCK_CHAIN (subblock))
23041 set_block_origin_self (subblock); /* Recurse. */
23046 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23047 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23048 node to so that it points to the node itself, thus indicating that the
23049 node represents its own (abstract) origin. Additionally, if the
23050 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23051 the decl/block tree of which the given node is the root of, and for
23052 each other ..._DECL or BLOCK node contained therein whose
23053 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23054 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23055 point to themselves. */
23057 static void
23058 set_decl_origin_self (tree decl)
23060 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23062 DECL_ABSTRACT_ORIGIN (decl) = decl;
23063 if (TREE_CODE (decl) == FUNCTION_DECL)
23065 tree arg;
23067 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23068 DECL_ABSTRACT_ORIGIN (arg) = arg;
23069 if (DECL_INITIAL (decl) != NULL_TREE
23070 && DECL_INITIAL (decl) != error_mark_node)
23071 set_block_origin_self (DECL_INITIAL (decl));
23076 /* Mark the early DIE for DECL as the abstract instance. */
23078 static void
23079 dwarf2out_abstract_function (tree decl)
23081 dw_die_ref old_die;
23083 /* Make sure we have the actual abstract inline, not a clone. */
23084 decl = DECL_ORIGIN (decl);
23086 if (DECL_IGNORED_P (decl))
23087 return;
23089 /* In LTO we're all set. We already created abstract instances
23090 early and we want to avoid creating a concrete instance of that
23091 if we don't output it. */
23092 if (in_lto_p)
23093 return;
23095 old_die = lookup_decl_die (decl);
23096 gcc_assert (old_die != NULL);
23097 if (get_AT (old_die, DW_AT_inline))
23098 /* We've already generated the abstract instance. */
23099 return;
23101 /* Go ahead and put DW_AT_inline on the DIE. */
23102 if (DECL_DECLARED_INLINE_P (decl))
23104 if (cgraph_function_possibly_inlined_p (decl))
23105 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23106 else
23107 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23109 else
23111 if (cgraph_function_possibly_inlined_p (decl))
23112 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23113 else
23114 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23117 if (DECL_DECLARED_INLINE_P (decl)
23118 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23119 add_AT_flag (old_die, DW_AT_artificial, 1);
23121 set_decl_origin_self (decl);
23124 /* Helper function of premark_used_types() which gets called through
23125 htab_traverse.
23127 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23128 marked as unused by prune_unused_types. */
23130 bool
23131 premark_used_types_helper (tree const &type, void *)
23133 dw_die_ref die;
23135 die = lookup_type_die (type);
23136 if (die != NULL)
23137 die->die_perennial_p = 1;
23138 return true;
23141 /* Helper function of premark_types_used_by_global_vars which gets called
23142 through htab_traverse.
23144 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23145 marked as unused by prune_unused_types. The DIE of the type is marked
23146 only if the global variable using the type will actually be emitted. */
23149 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23150 void *)
23152 struct types_used_by_vars_entry *entry;
23153 dw_die_ref die;
23155 entry = (struct types_used_by_vars_entry *) *slot;
23156 gcc_assert (entry->type != NULL
23157 && entry->var_decl != NULL);
23158 die = lookup_type_die (entry->type);
23159 if (die)
23161 /* Ask cgraph if the global variable really is to be emitted.
23162 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23163 varpool_node *node = varpool_node::get (entry->var_decl);
23164 if (node && node->definition)
23166 die->die_perennial_p = 1;
23167 /* Keep the parent DIEs as well. */
23168 while ((die = die->die_parent) && die->die_perennial_p == 0)
23169 die->die_perennial_p = 1;
23172 return 1;
23175 /* Mark all members of used_types_hash as perennial. */
23177 static void
23178 premark_used_types (struct function *fun)
23180 if (fun && fun->used_types_hash)
23181 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23184 /* Mark all members of types_used_by_vars_entry as perennial. */
23186 static void
23187 premark_types_used_by_global_vars (void)
23189 if (types_used_by_vars_hash)
23190 types_used_by_vars_hash
23191 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23194 /* Mark all variables used by the symtab as perennial. */
23196 static void
23197 premark_used_variables (void)
23199 /* Mark DIEs in the symtab as used. */
23200 varpool_node *var;
23201 FOR_EACH_VARIABLE (var)
23203 dw_die_ref die = lookup_decl_die (var->decl);
23204 if (die)
23205 die->die_perennial_p = 1;
23209 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23210 for CA_LOC call arg loc node. */
23212 static dw_die_ref
23213 gen_call_site_die (tree decl, dw_die_ref subr_die,
23214 struct call_arg_loc_node *ca_loc)
23216 dw_die_ref stmt_die = NULL, die;
23217 tree block = ca_loc->block;
23219 while (block
23220 && block != DECL_INITIAL (decl)
23221 && TREE_CODE (block) == BLOCK)
23223 stmt_die = lookup_block_die (block);
23224 if (stmt_die)
23225 break;
23226 block = BLOCK_SUPERCONTEXT (block);
23228 if (stmt_die == NULL)
23229 stmt_die = subr_die;
23230 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23231 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23232 if (ca_loc->tail_call_p)
23233 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23234 if (ca_loc->symbol_ref)
23236 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23237 if (tdie)
23238 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23239 else
23240 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23241 false);
23243 return die;
23246 /* Generate a DIE to represent a declared function (either file-scope or
23247 block-local). */
23249 static void
23250 gen_subprogram_die (tree decl, dw_die_ref context_die)
23252 tree origin = decl_ultimate_origin (decl);
23253 dw_die_ref subr_die;
23254 dw_die_ref old_die = lookup_decl_die (decl);
23255 bool old_die_had_no_children = false;
23257 /* This function gets called multiple times for different stages of
23258 the debug process. For example, for func() in this code:
23260 namespace S
23262 void func() { ... }
23265 ...we get called 4 times. Twice in early debug and twice in
23266 late debug:
23268 Early debug
23269 -----------
23271 1. Once while generating func() within the namespace. This is
23272 the declaration. The declaration bit below is set, as the
23273 context is the namespace.
23275 A new DIE will be generated with DW_AT_declaration set.
23277 2. Once for func() itself. This is the specification. The
23278 declaration bit below is clear as the context is the CU.
23280 We will use the cached DIE from (1) to create a new DIE with
23281 DW_AT_specification pointing to the declaration in (1).
23283 Late debug via rest_of_handle_final()
23284 -------------------------------------
23286 3. Once generating func() within the namespace. This is also the
23287 declaration, as in (1), but this time we will early exit below
23288 as we have a cached DIE and a declaration needs no additional
23289 annotations (no locations), as the source declaration line
23290 info is enough.
23292 4. Once for func() itself. As in (2), this is the specification,
23293 but this time we will re-use the cached DIE, and just annotate
23294 it with the location information that should now be available.
23296 For something without namespaces, but with abstract instances, we
23297 are also called a multiple times:
23299 class Base
23301 public:
23302 Base (); // constructor declaration (1)
23305 Base::Base () { } // constructor specification (2)
23307 Early debug
23308 -----------
23310 1. Once for the Base() constructor by virtue of it being a
23311 member of the Base class. This is done via
23312 rest_of_type_compilation.
23314 This is a declaration, so a new DIE will be created with
23315 DW_AT_declaration.
23317 2. Once for the Base() constructor definition, but this time
23318 while generating the abstract instance of the base
23319 constructor (__base_ctor) which is being generated via early
23320 debug of reachable functions.
23322 Even though we have a cached version of the declaration (1),
23323 we will create a DW_AT_specification of the declaration DIE
23324 in (1).
23326 3. Once for the __base_ctor itself, but this time, we generate
23327 an DW_AT_abstract_origin version of the DW_AT_specification in
23328 (2).
23330 Late debug via rest_of_handle_final
23331 -----------------------------------
23333 4. One final time for the __base_ctor (which will have a cached
23334 DIE with DW_AT_abstract_origin created in (3). This time,
23335 we will just annotate the location information now
23336 available.
23338 int declaration = (current_function_decl != decl
23339 || (!DECL_INITIAL (decl) && !origin)
23340 || class_or_namespace_scope_p (context_die));
23342 /* A declaration that has been previously dumped needs no
23343 additional information. */
23344 if (old_die && declaration)
23345 return;
23347 if (in_lto_p && old_die && old_die->die_child == NULL)
23348 old_die_had_no_children = true;
23350 /* Now that the C++ front end lazily declares artificial member fns, we
23351 might need to retrofit the declaration into its class. */
23352 if (!declaration && !origin && !old_die
23353 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23354 && !class_or_namespace_scope_p (context_die)
23355 && debug_info_level > DINFO_LEVEL_TERSE)
23356 old_die = force_decl_die (decl);
23358 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23359 if (origin != NULL)
23361 gcc_assert (!declaration || local_scope_p (context_die));
23363 /* Fixup die_parent for the abstract instance of a nested
23364 inline function. */
23365 if (old_die && old_die->die_parent == NULL)
23366 add_child_die (context_die, old_die);
23368 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23370 /* If we have a DW_AT_abstract_origin we have a working
23371 cached version. */
23372 subr_die = old_die;
23374 else
23376 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23377 add_abstract_origin_attribute (subr_die, origin);
23378 /* This is where the actual code for a cloned function is.
23379 Let's emit linkage name attribute for it. This helps
23380 debuggers to e.g, set breakpoints into
23381 constructors/destructors when the user asks "break
23382 K::K". */
23383 add_linkage_name (subr_die, decl);
23386 /* A cached copy, possibly from early dwarf generation. Reuse as
23387 much as possible. */
23388 else if (old_die)
23390 if (!get_AT_flag (old_die, DW_AT_declaration)
23391 /* We can have a normal definition following an inline one in the
23392 case of redefinition of GNU C extern inlines.
23393 It seems reasonable to use AT_specification in this case. */
23394 && !get_AT (old_die, DW_AT_inline))
23396 /* Detect and ignore this case, where we are trying to output
23397 something we have already output. */
23398 if (get_AT (old_die, DW_AT_low_pc)
23399 || get_AT (old_die, DW_AT_ranges))
23400 return;
23402 /* If we have no location information, this must be a
23403 partially generated DIE from early dwarf generation.
23404 Fall through and generate it. */
23407 /* If the definition comes from the same place as the declaration,
23408 maybe use the old DIE. We always want the DIE for this function
23409 that has the *_pc attributes to be under comp_unit_die so the
23410 debugger can find it. We also need to do this for abstract
23411 instances of inlines, since the spec requires the out-of-line copy
23412 to have the same parent. For local class methods, this doesn't
23413 apply; we just use the old DIE. */
23414 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23415 struct dwarf_file_data * file_index = lookup_filename (s.file);
23416 if (((is_unit_die (old_die->die_parent)
23417 /* This condition fixes the inconsistency/ICE with the
23418 following Fortran test (or some derivative thereof) while
23419 building libgfortran:
23421 module some_m
23422 contains
23423 logical function funky (FLAG)
23424 funky = .true.
23425 end function
23426 end module
23428 || (old_die->die_parent
23429 && old_die->die_parent->die_tag == DW_TAG_module)
23430 || local_scope_p (old_die->die_parent)
23431 || context_die == NULL)
23432 && (DECL_ARTIFICIAL (decl)
23433 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23434 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23435 == (unsigned) s.line)
23436 && (!debug_column_info
23437 || s.column == 0
23438 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23439 == (unsigned) s.column)))))
23440 /* With LTO if there's an abstract instance for
23441 the old DIE, this is a concrete instance and
23442 thus re-use the DIE. */
23443 || get_AT (old_die, DW_AT_abstract_origin))
23445 subr_die = old_die;
23447 /* Clear out the declaration attribute, but leave the
23448 parameters so they can be augmented with location
23449 information later. Unless this was a declaration, in
23450 which case, wipe out the nameless parameters and recreate
23451 them further down. */
23452 if (remove_AT (subr_die, DW_AT_declaration))
23455 remove_AT (subr_die, DW_AT_object_pointer);
23456 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23459 /* Make a specification pointing to the previously built
23460 declaration. */
23461 else
23463 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23464 add_AT_specification (subr_die, old_die);
23465 add_pubname (decl, subr_die);
23466 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23467 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23468 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23469 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23470 if (debug_column_info
23471 && s.column
23472 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23473 != (unsigned) s.column))
23474 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23476 /* If the prototype had an 'auto' or 'decltype(auto)' in
23477 the return type, emit the real type on the definition die. */
23478 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23480 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23481 while (die
23482 && (die->die_tag == DW_TAG_reference_type
23483 || die->die_tag == DW_TAG_rvalue_reference_type
23484 || die->die_tag == DW_TAG_pointer_type
23485 || die->die_tag == DW_TAG_const_type
23486 || die->die_tag == DW_TAG_volatile_type
23487 || die->die_tag == DW_TAG_restrict_type
23488 || die->die_tag == DW_TAG_array_type
23489 || die->die_tag == DW_TAG_ptr_to_member_type
23490 || die->die_tag == DW_TAG_subroutine_type))
23491 die = get_AT_ref (die, DW_AT_type);
23492 if (die == auto_die || die == decltype_auto_die)
23493 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23494 TYPE_UNQUALIFIED, false, context_die);
23497 /* When we process the method declaration, we haven't seen
23498 the out-of-class defaulted definition yet, so we have to
23499 recheck now. */
23500 if ((dwarf_version >= 5 || ! dwarf_strict)
23501 && !get_AT (subr_die, DW_AT_defaulted))
23503 int defaulted
23504 = lang_hooks.decls.decl_dwarf_attribute (decl,
23505 DW_AT_defaulted);
23506 if (defaulted != -1)
23508 /* Other values must have been handled before. */
23509 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23510 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23515 /* Create a fresh DIE for anything else. */
23516 else
23518 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23520 if (TREE_PUBLIC (decl))
23521 add_AT_flag (subr_die, DW_AT_external, 1);
23523 add_name_and_src_coords_attributes (subr_die, decl);
23524 add_pubname (decl, subr_die);
23525 if (debug_info_level > DINFO_LEVEL_TERSE)
23527 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23528 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23529 TYPE_UNQUALIFIED, false, context_die);
23532 add_pure_or_virtual_attribute (subr_die, decl);
23533 if (DECL_ARTIFICIAL (decl))
23534 add_AT_flag (subr_die, DW_AT_artificial, 1);
23536 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23537 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23539 add_alignment_attribute (subr_die, decl);
23541 add_accessibility_attribute (subr_die, decl);
23544 /* Unless we have an existing non-declaration DIE, equate the new
23545 DIE. */
23546 if (!old_die || is_declaration_die (old_die))
23547 equate_decl_number_to_die (decl, subr_die);
23549 if (declaration)
23551 if (!old_die || !get_AT (old_die, DW_AT_inline))
23553 add_AT_flag (subr_die, DW_AT_declaration, 1);
23555 /* If this is an explicit function declaration then generate
23556 a DW_AT_explicit attribute. */
23557 if ((dwarf_version >= 3 || !dwarf_strict)
23558 && lang_hooks.decls.decl_dwarf_attribute (decl,
23559 DW_AT_explicit) == 1)
23560 add_AT_flag (subr_die, DW_AT_explicit, 1);
23562 /* If this is a C++11 deleted special function member then generate
23563 a DW_AT_deleted attribute. */
23564 if ((dwarf_version >= 5 || !dwarf_strict)
23565 && lang_hooks.decls.decl_dwarf_attribute (decl,
23566 DW_AT_deleted) == 1)
23567 add_AT_flag (subr_die, DW_AT_deleted, 1);
23569 /* If this is a C++11 defaulted special function member then
23570 generate a DW_AT_defaulted attribute. */
23571 if (dwarf_version >= 5 || !dwarf_strict)
23573 int defaulted
23574 = lang_hooks.decls.decl_dwarf_attribute (decl,
23575 DW_AT_defaulted);
23576 if (defaulted != -1)
23577 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23580 /* If this is a C++11 non-static member function with & ref-qualifier
23581 then generate a DW_AT_reference attribute. */
23582 if ((dwarf_version >= 5 || !dwarf_strict)
23583 && lang_hooks.decls.decl_dwarf_attribute (decl,
23584 DW_AT_reference) == 1)
23585 add_AT_flag (subr_die, DW_AT_reference, 1);
23587 /* If this is a C++11 non-static member function with &&
23588 ref-qualifier then generate a DW_AT_reference attribute. */
23589 if ((dwarf_version >= 5 || !dwarf_strict)
23590 && lang_hooks.decls.decl_dwarf_attribute (decl,
23591 DW_AT_rvalue_reference)
23592 == 1)
23593 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23596 /* For non DECL_EXTERNALs, if range information is available, fill
23597 the DIE with it. */
23598 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23600 HOST_WIDE_INT cfa_fb_offset;
23602 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23604 if (!crtl->has_bb_partition)
23606 dw_fde_ref fde = fun->fde;
23607 if (fde->dw_fde_begin)
23609 /* We have already generated the labels. */
23610 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23611 fde->dw_fde_end, false);
23613 else
23615 /* Create start/end labels and add the range. */
23616 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23617 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23618 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23619 current_function_funcdef_no);
23620 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23621 current_function_funcdef_no);
23622 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23623 false);
23626 #if VMS_DEBUGGING_INFO
23627 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23628 Section 2.3 Prologue and Epilogue Attributes:
23629 When a breakpoint is set on entry to a function, it is generally
23630 desirable for execution to be suspended, not on the very first
23631 instruction of the function, but rather at a point after the
23632 function's frame has been set up, after any language defined local
23633 declaration processing has been completed, and before execution of
23634 the first statement of the function begins. Debuggers generally
23635 cannot properly determine where this point is. Similarly for a
23636 breakpoint set on exit from a function. The prologue and epilogue
23637 attributes allow a compiler to communicate the location(s) to use. */
23640 if (fde->dw_fde_vms_end_prologue)
23641 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23642 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23644 if (fde->dw_fde_vms_begin_epilogue)
23645 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23646 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23648 #endif
23651 else
23653 /* Generate pubnames entries for the split function code ranges. */
23654 dw_fde_ref fde = fun->fde;
23656 if (fde->dw_fde_second_begin)
23658 if (dwarf_version >= 3 || !dwarf_strict)
23660 /* We should use ranges for non-contiguous code section
23661 addresses. Use the actual code range for the initial
23662 section, since the HOT/COLD labels might precede an
23663 alignment offset. */
23664 bool range_list_added = false;
23665 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23666 fde->dw_fde_end, &range_list_added,
23667 false);
23668 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23669 fde->dw_fde_second_end,
23670 &range_list_added, false);
23671 if (range_list_added)
23672 add_ranges (NULL);
23674 else
23676 /* There is no real support in DW2 for this .. so we make
23677 a work-around. First, emit the pub name for the segment
23678 containing the function label. Then make and emit a
23679 simplified subprogram DIE for the second segment with the
23680 name pre-fixed by __hot/cold_sect_of_. We use the same
23681 linkage name for the second die so that gdb will find both
23682 sections when given "b foo". */
23683 const char *name = NULL;
23684 tree decl_name = DECL_NAME (decl);
23685 dw_die_ref seg_die;
23687 /* Do the 'primary' section. */
23688 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23689 fde->dw_fde_end, false);
23691 /* Build a minimal DIE for the secondary section. */
23692 seg_die = new_die (DW_TAG_subprogram,
23693 subr_die->die_parent, decl);
23695 if (TREE_PUBLIC (decl))
23696 add_AT_flag (seg_die, DW_AT_external, 1);
23698 if (decl_name != NULL
23699 && IDENTIFIER_POINTER (decl_name) != NULL)
23701 name = dwarf2_name (decl, 1);
23702 if (! DECL_ARTIFICIAL (decl))
23703 add_src_coords_attributes (seg_die, decl);
23705 add_linkage_name (seg_die, decl);
23707 gcc_assert (name != NULL);
23708 add_pure_or_virtual_attribute (seg_die, decl);
23709 if (DECL_ARTIFICIAL (decl))
23710 add_AT_flag (seg_die, DW_AT_artificial, 1);
23712 name = concat ("__second_sect_of_", name, NULL);
23713 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23714 fde->dw_fde_second_end, false);
23715 add_name_attribute (seg_die, name);
23716 if (want_pubnames ())
23717 add_pubname_string (name, seg_die);
23720 else
23721 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23722 false);
23725 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23727 /* We define the "frame base" as the function's CFA. This is more
23728 convenient for several reasons: (1) It's stable across the prologue
23729 and epilogue, which makes it better than just a frame pointer,
23730 (2) With dwarf3, there exists a one-byte encoding that allows us
23731 to reference the .debug_frame data by proxy, but failing that,
23732 (3) We can at least reuse the code inspection and interpretation
23733 code that determines the CFA position at various points in the
23734 function. */
23735 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23737 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23738 add_AT_loc (subr_die, DW_AT_frame_base, op);
23740 else
23742 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23743 if (list->dw_loc_next)
23744 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23745 else
23746 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23749 /* Compute a displacement from the "steady-state frame pointer" to
23750 the CFA. The former is what all stack slots and argument slots
23751 will reference in the rtl; the latter is what we've told the
23752 debugger about. We'll need to adjust all frame_base references
23753 by this displacement. */
23754 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23756 if (fun->static_chain_decl)
23758 /* DWARF requires here a location expression that computes the
23759 address of the enclosing subprogram's frame base. The machinery
23760 in tree-nested.c is supposed to store this specific address in the
23761 last field of the FRAME record. */
23762 const tree frame_type
23763 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23764 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23766 tree fb_expr
23767 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23768 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23769 fb_expr, fb_decl, NULL_TREE);
23771 add_AT_location_description (subr_die, DW_AT_static_link,
23772 loc_list_from_tree (fb_expr, 0, NULL));
23775 resolve_variable_values ();
23778 /* Generate child dies for template parameters. */
23779 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23780 gen_generic_params_dies (decl);
23782 /* Now output descriptions of the arguments for this function. This gets
23783 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23784 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23785 `...' at the end of the formal parameter list. In order to find out if
23786 there was a trailing ellipsis or not, we must instead look at the type
23787 associated with the FUNCTION_DECL. This will be a node of type
23788 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23789 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23790 an ellipsis at the end. */
23792 /* In the case where we are describing a mere function declaration, all we
23793 need to do here (and all we *can* do here) is to describe the *types* of
23794 its formal parameters. */
23795 if (debug_info_level <= DINFO_LEVEL_TERSE)
23797 else if (declaration)
23798 gen_formal_types_die (decl, subr_die);
23799 else
23801 /* Generate DIEs to represent all known formal parameters. */
23802 tree parm = DECL_ARGUMENTS (decl);
23803 tree generic_decl = early_dwarf
23804 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23805 tree generic_decl_parm = generic_decl
23806 ? DECL_ARGUMENTS (generic_decl)
23807 : NULL;
23809 /* Now we want to walk the list of parameters of the function and
23810 emit their relevant DIEs.
23812 We consider the case of DECL being an instance of a generic function
23813 as well as it being a normal function.
23815 If DECL is an instance of a generic function we walk the
23816 parameters of the generic function declaration _and_ the parameters of
23817 DECL itself. This is useful because we want to emit specific DIEs for
23818 function parameter packs and those are declared as part of the
23819 generic function declaration. In that particular case,
23820 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23821 That DIE has children DIEs representing the set of arguments
23822 of the pack. Note that the set of pack arguments can be empty.
23823 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23824 children DIE.
23826 Otherwise, we just consider the parameters of DECL. */
23827 while (generic_decl_parm || parm)
23829 if (generic_decl_parm
23830 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23831 gen_formal_parameter_pack_die (generic_decl_parm,
23832 parm, subr_die,
23833 &parm);
23834 else if (parm)
23836 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23838 if (early_dwarf
23839 && parm == DECL_ARGUMENTS (decl)
23840 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23841 && parm_die
23842 && (dwarf_version >= 3 || !dwarf_strict))
23843 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23845 parm = DECL_CHAIN (parm);
23848 if (generic_decl_parm)
23849 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23852 /* Decide whether we need an unspecified_parameters DIE at the end.
23853 There are 2 more cases to do this for: 1) the ansi ... declaration -
23854 this is detectable when the end of the arg list is not a
23855 void_type_node 2) an unprototyped function declaration (not a
23856 definition). This just means that we have no info about the
23857 parameters at all. */
23858 if (early_dwarf)
23860 if (prototype_p (TREE_TYPE (decl)))
23862 /* This is the prototyped case, check for.... */
23863 if (stdarg_p (TREE_TYPE (decl)))
23864 gen_unspecified_parameters_die (decl, subr_die);
23866 else if (DECL_INITIAL (decl) == NULL_TREE)
23867 gen_unspecified_parameters_die (decl, subr_die);
23869 else if ((subr_die != old_die || old_die_had_no_children)
23870 && prototype_p (TREE_TYPE (decl))
23871 && stdarg_p (TREE_TYPE (decl)))
23872 gen_unspecified_parameters_die (decl, subr_die);
23875 if (subr_die != old_die)
23876 /* Add the calling convention attribute if requested. */
23877 add_calling_convention_attribute (subr_die, decl);
23879 /* Output Dwarf info for all of the stuff within the body of the function
23880 (if it has one - it may be just a declaration).
23882 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23883 a function. This BLOCK actually represents the outermost binding contour
23884 for the function, i.e. the contour in which the function's formal
23885 parameters and labels get declared. Curiously, it appears that the front
23886 end doesn't actually put the PARM_DECL nodes for the current function onto
23887 the BLOCK_VARS list for this outer scope, but are strung off of the
23888 DECL_ARGUMENTS list for the function instead.
23890 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23891 the LABEL_DECL nodes for the function however, and we output DWARF info
23892 for those in decls_for_scope. Just within the `outer_scope' there will be
23893 a BLOCK node representing the function's outermost pair of curly braces,
23894 and any blocks used for the base and member initializers of a C++
23895 constructor function. */
23896 tree outer_scope = DECL_INITIAL (decl);
23897 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23899 int call_site_note_count = 0;
23900 int tail_call_site_note_count = 0;
23902 /* Emit a DW_TAG_variable DIE for a named return value. */
23903 if (DECL_NAME (DECL_RESULT (decl)))
23904 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23906 /* The first time through decls_for_scope we will generate the
23907 DIEs for the locals. The second time, we fill in the
23908 location info. */
23909 decls_for_scope (outer_scope, subr_die);
23911 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23913 struct call_arg_loc_node *ca_loc;
23914 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23916 dw_die_ref die = NULL;
23917 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23918 rtx arg, next_arg;
23919 tree arg_decl = NULL_TREE;
23921 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23922 ? XEXP (ca_loc->call_arg_loc_note, 0)
23923 : NULL_RTX);
23924 arg; arg = next_arg)
23926 dw_loc_descr_ref reg, val;
23927 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23928 dw_die_ref cdie, tdie = NULL;
23930 next_arg = XEXP (arg, 1);
23931 if (REG_P (XEXP (XEXP (arg, 0), 0))
23932 && next_arg
23933 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23934 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23935 && REGNO (XEXP (XEXP (arg, 0), 0))
23936 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23937 next_arg = XEXP (next_arg, 1);
23938 if (mode == VOIDmode)
23940 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23941 if (mode == VOIDmode)
23942 mode = GET_MODE (XEXP (arg, 0));
23944 if (mode == VOIDmode || mode == BLKmode)
23945 continue;
23946 /* Get dynamic information about call target only if we
23947 have no static information: we cannot generate both
23948 DW_AT_call_origin and DW_AT_call_target
23949 attributes. */
23950 if (ca_loc->symbol_ref == NULL_RTX)
23952 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23954 tloc = XEXP (XEXP (arg, 0), 1);
23955 continue;
23957 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23958 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23960 tlocc = XEXP (XEXP (arg, 0), 1);
23961 continue;
23964 reg = NULL;
23965 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23966 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23967 VAR_INIT_STATUS_INITIALIZED);
23968 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23970 rtx mem = XEXP (XEXP (arg, 0), 0);
23971 reg = mem_loc_descriptor (XEXP (mem, 0),
23972 get_address_mode (mem),
23973 GET_MODE (mem),
23974 VAR_INIT_STATUS_INITIALIZED);
23976 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23977 == DEBUG_PARAMETER_REF)
23979 tree tdecl
23980 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23981 tdie = lookup_decl_die (tdecl);
23982 if (tdie == NULL)
23983 continue;
23984 arg_decl = tdecl;
23986 else
23987 continue;
23988 if (reg == NULL
23989 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23990 != DEBUG_PARAMETER_REF)
23991 continue;
23992 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23993 VOIDmode,
23994 VAR_INIT_STATUS_INITIALIZED);
23995 if (val == NULL)
23996 continue;
23997 if (die == NULL)
23998 die = gen_call_site_die (decl, subr_die, ca_loc);
23999 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
24000 NULL_TREE);
24001 add_desc_attribute (cdie, arg_decl);
24002 if (reg != NULL)
24003 add_AT_loc (cdie, DW_AT_location, reg);
24004 else if (tdie != NULL)
24005 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24006 tdie);
24007 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24008 if (next_arg != XEXP (arg, 1))
24010 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24011 if (mode == VOIDmode)
24012 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24013 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24014 0), 1),
24015 mode, VOIDmode,
24016 VAR_INIT_STATUS_INITIALIZED);
24017 if (val != NULL)
24018 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24019 val);
24022 if (die == NULL
24023 && (ca_loc->symbol_ref || tloc))
24024 die = gen_call_site_die (decl, subr_die, ca_loc);
24025 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24027 dw_loc_descr_ref tval = NULL;
24029 if (tloc != NULL_RTX)
24030 tval = mem_loc_descriptor (tloc,
24031 GET_MODE (tloc) == VOIDmode
24032 ? Pmode : GET_MODE (tloc),
24033 VOIDmode,
24034 VAR_INIT_STATUS_INITIALIZED);
24035 if (tval)
24036 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24037 else if (tlocc != NULL_RTX)
24039 tval = mem_loc_descriptor (tlocc,
24040 GET_MODE (tlocc) == VOIDmode
24041 ? Pmode : GET_MODE (tlocc),
24042 VOIDmode,
24043 VAR_INIT_STATUS_INITIALIZED);
24044 if (tval)
24045 add_AT_loc (die,
24046 dwarf_AT (DW_AT_call_target_clobbered),
24047 tval);
24050 if (die != NULL)
24052 call_site_note_count++;
24053 if (ca_loc->tail_call_p)
24054 tail_call_site_note_count++;
24058 call_arg_locations = NULL;
24059 call_arg_loc_last = NULL;
24060 if (tail_call_site_count >= 0
24061 && tail_call_site_count == tail_call_site_note_count
24062 && (!dwarf_strict || dwarf_version >= 5))
24064 if (call_site_count >= 0
24065 && call_site_count == call_site_note_count)
24066 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24067 else
24068 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24070 call_site_count = -1;
24071 tail_call_site_count = -1;
24074 /* Mark used types after we have created DIEs for the functions scopes. */
24075 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24078 /* Returns a hash value for X (which really is a die_struct). */
24080 hashval_t
24081 block_die_hasher::hash (die_struct *d)
24083 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24086 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24087 as decl_id and die_parent of die_struct Y. */
24089 bool
24090 block_die_hasher::equal (die_struct *x, die_struct *y)
24092 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24095 /* Hold information about markers for inlined entry points. */
24096 struct GTY ((for_user)) inline_entry_data
24098 /* The block that's the inlined_function_outer_scope for an inlined
24099 function. */
24100 tree block;
24102 /* The label at the inlined entry point. */
24103 const char *label_pfx;
24104 unsigned int label_num;
24106 /* The view number to be used as the inlined entry point. */
24107 var_loc_view view;
24110 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24112 typedef tree compare_type;
24113 static inline hashval_t hash (const inline_entry_data *);
24114 static inline bool equal (const inline_entry_data *, const_tree);
24117 /* Hash table routines for inline_entry_data. */
24119 inline hashval_t
24120 inline_entry_data_hasher::hash (const inline_entry_data *data)
24122 return htab_hash_pointer (data->block);
24125 inline bool
24126 inline_entry_data_hasher::equal (const inline_entry_data *data,
24127 const_tree block)
24129 return data->block == block;
24132 /* Inlined entry points pending DIE creation in this compilation unit. */
24134 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24137 /* Return TRUE if DECL, which may have been previously generated as
24138 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24139 true if decl (or its origin) is either an extern declaration or a
24140 class/namespace scoped declaration.
24142 The declare_in_namespace support causes us to get two DIEs for one
24143 variable, both of which are declarations. We want to avoid
24144 considering one to be a specification, so we must test for
24145 DECLARATION and DW_AT_declaration. */
24146 static inline bool
24147 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24149 return (old_die && TREE_STATIC (decl) && !declaration
24150 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24153 /* Return true if DECL is a local static. */
24155 static inline bool
24156 local_function_static (tree decl)
24158 gcc_assert (VAR_P (decl));
24159 return TREE_STATIC (decl)
24160 && DECL_CONTEXT (decl)
24161 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24164 /* Return true iff DECL overrides (presumably completes) the type of
24165 OLD_DIE within CONTEXT_DIE. */
24167 static bool
24168 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24169 dw_die_ref context_die)
24171 tree type = TREE_TYPE (decl);
24172 int cv_quals;
24174 if (decl_by_reference_p (decl))
24176 type = TREE_TYPE (type);
24177 cv_quals = TYPE_UNQUALIFIED;
24179 else
24180 cv_quals = decl_quals (decl);
24182 dw_die_ref type_die = modified_type_die (type,
24183 cv_quals | TYPE_QUALS (type),
24184 false,
24185 context_die);
24187 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24189 return type_die != old_type_die;
24192 /* Generate a DIE to represent a declared data object.
24193 Either DECL or ORIGIN must be non-null. */
24195 static void
24196 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24198 HOST_WIDE_INT off = 0;
24199 tree com_decl;
24200 tree decl_or_origin = decl ? decl : origin;
24201 tree ultimate_origin;
24202 dw_die_ref var_die;
24203 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24204 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24205 || class_or_namespace_scope_p (context_die));
24206 bool specialization_p = false;
24207 bool no_linkage_name = false;
24209 /* While C++ inline static data members have definitions inside of the
24210 class, force the first DIE to be a declaration, then let gen_member_die
24211 reparent it to the class context and call gen_variable_die again
24212 to create the outside of the class DIE for the definition. */
24213 if (!declaration
24214 && old_die == NULL
24215 && decl
24216 && DECL_CONTEXT (decl)
24217 && TYPE_P (DECL_CONTEXT (decl))
24218 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24220 declaration = true;
24221 if (dwarf_version < 5)
24222 no_linkage_name = true;
24225 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24226 if (decl || ultimate_origin)
24227 origin = ultimate_origin;
24228 com_decl = fortran_common (decl_or_origin, &off);
24230 /* Symbol in common gets emitted as a child of the common block, in the form
24231 of a data member. */
24232 if (com_decl)
24234 dw_die_ref com_die;
24235 dw_loc_list_ref loc = NULL;
24236 die_node com_die_arg;
24238 var_die = lookup_decl_die (decl_or_origin);
24239 if (var_die)
24241 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24243 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24244 if (loc)
24246 if (off)
24248 /* Optimize the common case. */
24249 if (single_element_loc_list_p (loc)
24250 && loc->expr->dw_loc_opc == DW_OP_addr
24251 && loc->expr->dw_loc_next == NULL
24252 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24253 == SYMBOL_REF)
24255 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24256 loc->expr->dw_loc_oprnd1.v.val_addr
24257 = plus_constant (GET_MODE (x), x , off);
24259 else
24260 loc_list_plus_const (loc, off);
24262 add_AT_location_description (var_die, DW_AT_location, loc);
24263 remove_AT (var_die, DW_AT_declaration);
24266 return;
24269 if (common_block_die_table == NULL)
24270 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24272 com_die_arg.decl_id = DECL_UID (com_decl);
24273 com_die_arg.die_parent = context_die;
24274 com_die = common_block_die_table->find (&com_die_arg);
24275 if (! early_dwarf)
24276 loc = loc_list_from_tree (com_decl, 2, NULL);
24277 if (com_die == NULL)
24279 const char *cnam
24280 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24281 die_node **slot;
24283 com_die = new_die (DW_TAG_common_block, context_die, decl);
24284 add_name_and_src_coords_attributes (com_die, com_decl);
24285 if (loc)
24287 add_AT_location_description (com_die, DW_AT_location, loc);
24288 /* Avoid sharing the same loc descriptor between
24289 DW_TAG_common_block and DW_TAG_variable. */
24290 loc = loc_list_from_tree (com_decl, 2, NULL);
24292 else if (DECL_EXTERNAL (decl_or_origin))
24293 add_AT_flag (com_die, DW_AT_declaration, 1);
24294 if (want_pubnames ())
24295 add_pubname_string (cnam, com_die); /* ??? needed? */
24296 com_die->decl_id = DECL_UID (com_decl);
24297 slot = common_block_die_table->find_slot (com_die, INSERT);
24298 *slot = com_die;
24300 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24302 add_AT_location_description (com_die, DW_AT_location, loc);
24303 loc = loc_list_from_tree (com_decl, 2, NULL);
24304 remove_AT (com_die, DW_AT_declaration);
24306 var_die = new_die (DW_TAG_variable, com_die, decl);
24307 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24308 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24309 decl_quals (decl_or_origin), false,
24310 context_die);
24311 add_alignment_attribute (var_die, decl);
24312 add_AT_flag (var_die, DW_AT_external, 1);
24313 if (loc)
24315 if (off)
24317 /* Optimize the common case. */
24318 if (single_element_loc_list_p (loc)
24319 && loc->expr->dw_loc_opc == DW_OP_addr
24320 && loc->expr->dw_loc_next == NULL
24321 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24323 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24324 loc->expr->dw_loc_oprnd1.v.val_addr
24325 = plus_constant (GET_MODE (x), x, off);
24327 else
24328 loc_list_plus_const (loc, off);
24330 add_AT_location_description (var_die, DW_AT_location, loc);
24332 else if (DECL_EXTERNAL (decl_or_origin))
24333 add_AT_flag (var_die, DW_AT_declaration, 1);
24334 if (decl)
24335 equate_decl_number_to_die (decl, var_die);
24336 return;
24339 if (old_die)
24341 if (declaration)
24343 /* A declaration that has been previously dumped, needs no
24344 further annotations, since it doesn't need location on
24345 the second pass. */
24346 return;
24348 else if (decl_will_get_specification_p (old_die, decl, declaration)
24349 && !get_AT (old_die, DW_AT_specification))
24351 /* Fall-thru so we can make a new variable die along with a
24352 DW_AT_specification. */
24354 else if (origin && old_die->die_parent != context_die)
24356 /* If we will be creating an inlined instance, we need a
24357 new DIE that will get annotated with
24358 DW_AT_abstract_origin. */
24359 gcc_assert (!DECL_ABSTRACT_P (decl));
24361 else
24363 /* If a DIE was dumped early, it still needs location info.
24364 Skip to where we fill the location bits. */
24365 var_die = old_die;
24367 /* ??? In LTRANS we cannot annotate early created variably
24368 modified type DIEs without copying them and adjusting all
24369 references to them. Thus we dumped them again. Also add a
24370 reference to them but beware of -g0 compile and -g link
24371 in which case the reference will be already present. */
24372 tree type = TREE_TYPE (decl_or_origin);
24373 if (in_lto_p
24374 && ! get_AT (var_die, DW_AT_type)
24375 && variably_modified_type_p
24376 (type, decl_function_context (decl_or_origin)))
24378 if (decl_by_reference_p (decl_or_origin))
24379 add_type_attribute (var_die, TREE_TYPE (type),
24380 TYPE_UNQUALIFIED, false, context_die);
24381 else
24382 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24383 false, context_die);
24386 goto gen_variable_die_location;
24390 /* For static data members, the declaration in the class is supposed
24391 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24392 also in DWARF2; the specification should still be DW_TAG_variable
24393 referencing the DW_TAG_member DIE. */
24394 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24395 var_die = new_die (DW_TAG_member, context_die, decl);
24396 else
24397 var_die = new_die (DW_TAG_variable, context_die, decl);
24399 if (origin != NULL)
24400 add_abstract_origin_attribute (var_die, origin);
24402 /* Loop unrolling can create multiple blocks that refer to the same
24403 static variable, so we must test for the DW_AT_declaration flag.
24405 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24406 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24407 sharing them.
24409 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24410 else if (decl_will_get_specification_p (old_die, decl, declaration))
24412 /* This is a definition of a C++ class level static. */
24413 add_AT_specification (var_die, old_die);
24414 specialization_p = true;
24415 if (DECL_NAME (decl))
24417 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24418 struct dwarf_file_data * file_index = lookup_filename (s.file);
24420 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24421 add_AT_file (var_die, DW_AT_decl_file, file_index);
24423 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24424 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24426 if (debug_column_info
24427 && s.column
24428 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24429 != (unsigned) s.column))
24430 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24432 if (old_die->die_tag == DW_TAG_member)
24433 add_linkage_name (var_die, decl);
24436 else
24437 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24439 if ((origin == NULL && !specialization_p)
24440 || (origin != NULL
24441 && !DECL_ABSTRACT_P (decl_or_origin)
24442 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24443 decl_function_context
24444 (decl_or_origin)))
24445 || (old_die && specialization_p
24446 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24448 tree type = TREE_TYPE (decl_or_origin);
24450 if (decl_by_reference_p (decl_or_origin))
24451 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24452 context_die);
24453 else
24454 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24455 context_die);
24458 if (origin == NULL && !specialization_p)
24460 if (TREE_PUBLIC (decl))
24461 add_AT_flag (var_die, DW_AT_external, 1);
24463 if (DECL_ARTIFICIAL (decl))
24464 add_AT_flag (var_die, DW_AT_artificial, 1);
24466 add_alignment_attribute (var_die, decl);
24468 add_accessibility_attribute (var_die, decl);
24471 if (declaration)
24472 add_AT_flag (var_die, DW_AT_declaration, 1);
24474 if (decl && (DECL_ABSTRACT_P (decl)
24475 || !old_die || is_declaration_die (old_die)))
24476 equate_decl_number_to_die (decl, var_die);
24478 gen_variable_die_location:
24479 if (! declaration
24480 && (! DECL_ABSTRACT_P (decl_or_origin)
24481 /* Local static vars are shared between all clones/inlines,
24482 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24483 already set. */
24484 || (VAR_P (decl_or_origin)
24485 && TREE_STATIC (decl_or_origin)
24486 && DECL_RTL_SET_P (decl_or_origin))))
24488 if (early_dwarf)
24490 add_pubname (decl_or_origin, var_die);
24491 /* For global register variables, emit DW_AT_location if possible
24492 already during early_dwarf, as late_global_decl won't be usually
24493 called. */
24494 if (DECL_HARD_REGISTER (decl_or_origin)
24495 && TREE_STATIC (decl_or_origin)
24496 && !decl_by_reference_p (decl_or_origin)
24497 && !get_AT (var_die, DW_AT_location)
24498 && !get_AT (var_die, DW_AT_const_value)
24499 && DECL_RTL_SET_P (decl_or_origin)
24500 && REG_P (DECL_RTL (decl_or_origin)))
24502 dw_loc_descr_ref descr
24503 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24504 VAR_INIT_STATUS_INITIALIZED);
24505 if (descr)
24506 add_AT_loc (var_die, DW_AT_location, descr);
24509 else
24510 add_location_or_const_value_attribute (var_die, decl_or_origin,
24511 decl == NULL);
24513 else
24514 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24516 if ((dwarf_version >= 4 || !dwarf_strict)
24517 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24518 DW_AT_const_expr) == 1
24519 && !get_AT (var_die, DW_AT_const_expr)
24520 && !specialization_p)
24521 add_AT_flag (var_die, DW_AT_const_expr, 1);
24523 if (!dwarf_strict)
24525 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24526 DW_AT_inline);
24527 if (inl != -1
24528 && !get_AT (var_die, DW_AT_inline)
24529 && !specialization_p)
24530 add_AT_unsigned (var_die, DW_AT_inline, inl);
24534 /* Generate a DIE to represent a named constant. */
24536 static void
24537 gen_const_die (tree decl, dw_die_ref context_die)
24539 dw_die_ref const_die;
24540 tree type = TREE_TYPE (decl);
24542 const_die = lookup_decl_die (decl);
24543 if (const_die)
24544 return;
24546 const_die = new_die (DW_TAG_constant, context_die, decl);
24547 equate_decl_number_to_die (decl, const_die);
24548 add_name_and_src_coords_attributes (const_die, decl);
24549 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24550 if (TREE_PUBLIC (decl))
24551 add_AT_flag (const_die, DW_AT_external, 1);
24552 if (DECL_ARTIFICIAL (decl))
24553 add_AT_flag (const_die, DW_AT_artificial, 1);
24554 tree_add_const_value_attribute_for_decl (const_die, decl);
24557 /* Generate a DIE to represent a label identifier. */
24559 static void
24560 gen_label_die (tree decl, dw_die_ref context_die)
24562 tree origin = decl_ultimate_origin (decl);
24563 dw_die_ref lbl_die = lookup_decl_die (decl);
24564 rtx insn;
24565 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24567 if (!lbl_die)
24569 lbl_die = new_die (DW_TAG_label, context_die, decl);
24570 equate_decl_number_to_die (decl, lbl_die);
24572 if (origin != NULL)
24573 add_abstract_origin_attribute (lbl_die, origin);
24574 else
24575 add_name_and_src_coords_attributes (lbl_die, decl);
24578 if (DECL_ABSTRACT_P (decl))
24579 equate_decl_number_to_die (decl, lbl_die);
24580 else if (! early_dwarf)
24582 insn = DECL_RTL_IF_SET (decl);
24584 /* Deleted labels are programmer specified labels which have been
24585 eliminated because of various optimizations. We still emit them
24586 here so that it is possible to put breakpoints on them. */
24587 if (insn
24588 && (LABEL_P (insn)
24589 || ((NOTE_P (insn)
24590 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24592 /* When optimization is enabled (via -O) some parts of the compiler
24593 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24594 represent source-level labels which were explicitly declared by
24595 the user. This really shouldn't be happening though, so catch
24596 it if it ever does happen. */
24597 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24599 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24600 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24602 else if (insn
24603 && NOTE_P (insn)
24604 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24605 && CODE_LABEL_NUMBER (insn) != -1)
24607 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24608 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24613 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24614 attributes to the DIE for a block STMT, to describe where the inlined
24615 function was called from. This is similar to add_src_coords_attributes. */
24617 static inline void
24618 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24620 /* We can end up with BUILTINS_LOCATION here. */
24621 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24622 return;
24624 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24626 if (dwarf_version >= 3 || !dwarf_strict)
24628 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24629 add_AT_unsigned (die, DW_AT_call_line, s.line);
24630 if (debug_column_info && s.column)
24631 add_AT_unsigned (die, DW_AT_call_column, s.column);
24636 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24637 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24639 static inline void
24640 add_high_low_attributes (tree stmt, dw_die_ref die)
24642 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24644 if (inline_entry_data **iedp
24645 = !inline_entry_data_table ? NULL
24646 : inline_entry_data_table->find_slot_with_hash (stmt,
24647 htab_hash_pointer (stmt),
24648 NO_INSERT))
24650 inline_entry_data *ied = *iedp;
24651 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24652 gcc_assert (debug_inline_points);
24653 gcc_assert (inlined_function_outer_scope_p (stmt));
24655 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24656 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24658 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24659 && !dwarf_strict)
24661 if (!output_asm_line_debug_info ())
24662 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24663 else
24665 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24666 /* FIXME: this will resolve to a small number. Could we
24667 possibly emit smaller data? Ideally we'd emit a
24668 uleb128, but that would make the size of DIEs
24669 impossible for the compiler to compute, since it's
24670 the assembler that computes the value of the view
24671 label in this case. Ideally, we'd have a single form
24672 encompassing both the address and the view, and
24673 indirecting them through a table might make things
24674 easier, but even that would be more wasteful,
24675 space-wise, than what we have now. */
24676 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24680 inline_entry_data_table->clear_slot (iedp);
24683 if (BLOCK_FRAGMENT_CHAIN (stmt)
24684 && (dwarf_version >= 3 || !dwarf_strict))
24686 tree chain, superblock = NULL_TREE;
24687 dw_die_ref pdie;
24688 dw_attr_node *attr = NULL;
24690 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24692 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24693 BLOCK_NUMBER (stmt));
24694 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24697 /* Optimize duplicate .debug_ranges lists or even tails of
24698 lists. If this BLOCK has same ranges as its supercontext,
24699 lookup DW_AT_ranges attribute in the supercontext (and
24700 recursively so), verify that the ranges_table contains the
24701 right values and use it instead of adding a new .debug_range. */
24702 for (chain = stmt, pdie = die;
24703 BLOCK_SAME_RANGE (chain);
24704 chain = BLOCK_SUPERCONTEXT (chain))
24706 dw_attr_node *new_attr;
24708 pdie = pdie->die_parent;
24709 if (pdie == NULL)
24710 break;
24711 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24712 break;
24713 new_attr = get_AT (pdie, DW_AT_ranges);
24714 if (new_attr == NULL
24715 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24716 break;
24717 attr = new_attr;
24718 superblock = BLOCK_SUPERCONTEXT (chain);
24720 if (attr != NULL
24721 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24722 == (int)BLOCK_NUMBER (superblock))
24723 && BLOCK_FRAGMENT_CHAIN (superblock))
24725 unsigned long off = attr->dw_attr_val.v.val_offset;
24726 unsigned long supercnt = 0, thiscnt = 0;
24727 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24728 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24730 ++supercnt;
24731 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24732 == (int)BLOCK_NUMBER (chain));
24734 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24735 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24736 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24737 ++thiscnt;
24738 gcc_assert (supercnt >= thiscnt);
24739 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24740 false);
24741 note_rnglist_head (off + supercnt - thiscnt);
24742 return;
24745 unsigned int offset = add_ranges (stmt, true);
24746 add_AT_range_list (die, DW_AT_ranges, offset, false);
24747 note_rnglist_head (offset);
24749 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24750 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24753 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24754 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24755 chain = BLOCK_FRAGMENT_CHAIN (chain);
24757 while (chain);
24758 add_ranges (NULL);
24760 else
24762 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24763 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24764 BLOCK_NUMBER (stmt));
24765 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24766 BLOCK_NUMBER (stmt));
24767 add_AT_low_high_pc (die, label, label_high, false);
24771 /* Generate a DIE for a lexical block. */
24773 static void
24774 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24776 dw_die_ref old_die = lookup_block_die (stmt);
24777 dw_die_ref stmt_die = NULL;
24778 if (!old_die)
24780 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24781 equate_block_to_die (stmt, stmt_die);
24784 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24786 /* If this is an inlined or conrecte instance, create a new lexical
24787 die for anything below to attach DW_AT_abstract_origin to. */
24788 if (old_die)
24789 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24791 tree origin = block_ultimate_origin (stmt);
24792 if (origin != NULL_TREE && (origin != stmt || old_die))
24793 add_abstract_origin_attribute (stmt_die, origin);
24795 old_die = NULL;
24798 if (old_die)
24799 stmt_die = old_die;
24801 /* A non abstract block whose blocks have already been reordered
24802 should have the instruction range for this block. If so, set the
24803 high/low attributes. */
24804 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24806 gcc_assert (stmt_die);
24807 add_high_low_attributes (stmt, stmt_die);
24810 decls_for_scope (stmt, stmt_die);
24813 /* Generate a DIE for an inlined subprogram. */
24815 static void
24816 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24818 tree decl = block_ultimate_origin (stmt);
24820 /* Make sure any inlined functions are known to be inlineable. */
24821 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24822 || cgraph_function_possibly_inlined_p (decl));
24824 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24826 if (call_arg_locations || debug_inline_points)
24827 equate_block_to_die (stmt, subr_die);
24828 add_abstract_origin_attribute (subr_die, decl);
24829 if (TREE_ASM_WRITTEN (stmt))
24830 add_high_low_attributes (stmt, subr_die);
24831 add_call_src_coords_attributes (stmt, subr_die);
24833 /* The inliner creates an extra BLOCK for the parameter setup,
24834 we want to merge that with the actual outermost BLOCK of the
24835 inlined function to avoid duplicate locals in consumers.
24836 Do that by doing the recursion to subblocks on the single subblock
24837 of STMT. */
24838 bool unwrap_one = false;
24839 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24841 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24842 if (origin
24843 && TREE_CODE (origin) == BLOCK
24844 && BLOCK_SUPERCONTEXT (origin) == decl)
24845 unwrap_one = true;
24847 decls_for_scope (stmt, subr_die, !unwrap_one);
24848 if (unwrap_one)
24849 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24852 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24853 the comment for VLR_CONTEXT. */
24855 static void
24856 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24858 dw_die_ref decl_die;
24860 if (TREE_TYPE (decl) == error_mark_node)
24861 return;
24863 decl_die = new_die (DW_TAG_member, context_die, decl);
24864 add_name_and_src_coords_attributes (decl_die, decl);
24865 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24866 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24867 context_die);
24869 if (DECL_BIT_FIELD_TYPE (decl))
24871 add_byte_size_attribute (decl_die, decl);
24872 add_bit_size_attribute (decl_die, decl);
24873 add_bit_offset_attribute (decl_die, decl);
24876 add_alignment_attribute (decl_die, decl);
24878 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24879 add_data_member_location_attribute (decl_die, decl, ctx);
24881 if (DECL_ARTIFICIAL (decl))
24882 add_AT_flag (decl_die, DW_AT_artificial, 1);
24884 add_accessibility_attribute (decl_die, decl);
24886 /* Equate decl number to die, so that we can look up this decl later on. */
24887 equate_decl_number_to_die (decl, decl_die);
24890 /* Generate a DIE for a pointer to a member type. TYPE can be an
24891 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24892 pointer to member function. */
24894 static void
24895 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24897 if (lookup_type_die (type))
24898 return;
24900 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24901 scope_die_for (type, context_die), type);
24903 equate_type_number_to_die (type, ptr_die);
24904 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24905 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24906 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24907 context_die);
24908 add_alignment_attribute (ptr_die, type);
24910 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24911 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24913 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24914 add_AT_loc (ptr_die, DW_AT_use_location, op);
24918 static char *producer_string;
24920 /* Given a C and/or C++ language/version string return the "highest".
24921 C++ is assumed to be "higher" than C in this case. Used for merging
24922 LTO translation unit languages. */
24923 static const char *
24924 highest_c_language (const char *lang1, const char *lang2)
24926 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24927 return "GNU C++23";
24928 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24929 return "GNU C++20";
24930 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24931 return "GNU C++17";
24932 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24933 return "GNU C++14";
24934 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24935 return "GNU C++11";
24936 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24937 return "GNU C++98";
24939 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24940 return "GNU C2X";
24941 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24942 return "GNU C17";
24943 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24944 return "GNU C11";
24945 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24946 return "GNU C99";
24947 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24948 return "GNU C89";
24950 gcc_unreachable ();
24954 /* Generate the DIE for the compilation unit. */
24956 static dw_die_ref
24957 gen_compile_unit_die (const char *filename)
24959 dw_die_ref die;
24960 const char *language_string = lang_hooks.name;
24961 int language;
24963 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24965 if (filename)
24967 add_filename_attribute (die, filename);
24968 /* Don't add cwd for <built-in>. */
24969 if (filename[0] != '<')
24970 add_comp_dir_attribute (die);
24973 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24975 /* If our producer is LTO try to figure out a common language to use
24976 from the global list of translation units. */
24977 if (strcmp (language_string, "GNU GIMPLE") == 0)
24979 unsigned i;
24980 tree t;
24981 const char *common_lang = NULL;
24983 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24985 if (!TRANSLATION_UNIT_LANGUAGE (t))
24986 continue;
24987 if (!common_lang)
24988 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24989 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24991 else if (startswith (common_lang, "GNU C")
24992 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
24993 /* Mixing C and C++ is ok, use C++ in that case. */
24994 common_lang = highest_c_language (common_lang,
24995 TRANSLATION_UNIT_LANGUAGE (t));
24996 else
24998 /* Fall back to C. */
24999 common_lang = NULL;
25000 break;
25004 if (common_lang)
25005 language_string = common_lang;
25008 language = DW_LANG_C;
25009 if (startswith (language_string, "GNU C")
25010 && ISDIGIT (language_string[5]))
25012 language = DW_LANG_C89;
25013 if (dwarf_version >= 3 || !dwarf_strict)
25015 if (strcmp (language_string, "GNU C89") != 0)
25016 language = DW_LANG_C99;
25018 if (dwarf_version >= 5 /* || !dwarf_strict */)
25019 if (strcmp (language_string, "GNU C11") == 0
25020 || strcmp (language_string, "GNU C17") == 0
25021 || strcmp (language_string, "GNU C2X") == 0)
25022 language = DW_LANG_C11;
25025 else if (startswith (language_string, "GNU C++"))
25027 language = DW_LANG_C_plus_plus;
25028 if (dwarf_version >= 5 /* || !dwarf_strict */)
25030 if (strcmp (language_string, "GNU C++11") == 0)
25031 language = DW_LANG_C_plus_plus_11;
25032 else if (strcmp (language_string, "GNU C++14") == 0)
25033 language = DW_LANG_C_plus_plus_14;
25034 else if (strcmp (language_string, "GNU C++17") == 0
25035 || strcmp (language_string, "GNU C++20") == 0
25036 || strcmp (language_string, "GNU C++23") == 0)
25037 /* For now. */
25038 language = DW_LANG_C_plus_plus_14;
25041 else if (strcmp (language_string, "GNU F77") == 0)
25042 language = DW_LANG_Fortran77;
25043 else if (dwarf_version >= 3 || !dwarf_strict)
25045 if (strcmp (language_string, "GNU Ada") == 0)
25046 language = DW_LANG_Ada95;
25047 else if (startswith (language_string, "GNU Fortran"))
25049 language = DW_LANG_Fortran95;
25050 if (dwarf_version >= 5 /* || !dwarf_strict */)
25052 if (strcmp (language_string, "GNU Fortran2003") == 0)
25053 language = DW_LANG_Fortran03;
25054 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25055 language = DW_LANG_Fortran08;
25058 else if (strcmp (language_string, "GNU Objective-C") == 0)
25059 language = DW_LANG_ObjC;
25060 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25061 language = DW_LANG_ObjC_plus_plus;
25062 else if (strcmp (language_string, "GNU D") == 0)
25063 language = DW_LANG_D;
25064 else if (dwarf_version >= 5 || !dwarf_strict)
25066 if (strcmp (language_string, "GNU Go") == 0)
25067 language = DW_LANG_Go;
25070 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25071 else if (startswith (language_string, "GNU Fortran"))
25072 language = DW_LANG_Fortran90;
25073 /* Likewise for Ada. */
25074 else if (strcmp (language_string, "GNU Ada") == 0)
25075 language = DW_LANG_Ada83;
25077 add_AT_unsigned (die, DW_AT_language, language);
25079 switch (language)
25081 case DW_LANG_Fortran77:
25082 case DW_LANG_Fortran90:
25083 case DW_LANG_Fortran95:
25084 case DW_LANG_Fortran03:
25085 case DW_LANG_Fortran08:
25086 /* Fortran has case insensitive identifiers and the front-end
25087 lowercases everything. */
25088 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25089 break;
25090 default:
25091 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25092 break;
25094 return die;
25097 /* Generate the DIE for a base class. */
25099 static void
25100 gen_inheritance_die (tree binfo, tree access, tree type,
25101 dw_die_ref context_die)
25103 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25104 struct vlr_context ctx = { type, NULL };
25106 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25107 context_die);
25108 add_data_member_location_attribute (die, binfo, &ctx);
25110 if (BINFO_VIRTUAL_P (binfo))
25111 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25113 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25114 children, otherwise the default is DW_ACCESS_public. In DWARF2
25115 the default has always been DW_ACCESS_private. */
25116 if (access == access_public_node)
25118 if (dwarf_version == 2
25119 || context_die->die_tag == DW_TAG_class_type)
25120 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25122 else if (access == access_protected_node)
25123 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25124 else if (dwarf_version > 2
25125 && context_die->die_tag != DW_TAG_class_type)
25126 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25129 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25130 structure. */
25132 static bool
25133 is_variant_part (tree decl)
25135 return (TREE_CODE (decl) == FIELD_DECL
25136 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25139 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25140 return the FIELD_DECL. Return NULL_TREE otherwise. */
25142 static tree
25143 analyze_discr_in_predicate (tree operand, tree struct_type)
25145 while (CONVERT_EXPR_P (operand))
25146 operand = TREE_OPERAND (operand, 0);
25148 /* Match field access to members of struct_type only. */
25149 if (TREE_CODE (operand) == COMPONENT_REF
25150 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25151 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25152 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25153 return TREE_OPERAND (operand, 1);
25154 else
25155 return NULL_TREE;
25158 /* Check that SRC is a constant integer that can be represented as a native
25159 integer constant (either signed or unsigned). If so, store it into DEST and
25160 return true. Return false otherwise. */
25162 static bool
25163 get_discr_value (tree src, dw_discr_value *dest)
25165 tree discr_type = TREE_TYPE (src);
25167 if (lang_hooks.types.get_debug_type)
25169 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25170 if (debug_type != NULL)
25171 discr_type = debug_type;
25174 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25175 return false;
25177 /* Signedness can vary between the original type and the debug type. This
25178 can happen for character types in Ada for instance: the character type
25179 used for code generation can be signed, to be compatible with the C one,
25180 but from a debugger point of view, it must be unsigned. */
25181 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25182 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25184 if (is_orig_unsigned != is_debug_unsigned)
25185 src = fold_convert (discr_type, src);
25187 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25188 return false;
25190 dest->pos = is_debug_unsigned;
25191 if (is_debug_unsigned)
25192 dest->v.uval = tree_to_uhwi (src);
25193 else
25194 dest->v.sval = tree_to_shwi (src);
25196 return true;
25199 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25200 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25201 store NULL_TREE in DISCR_DECL. Otherwise:
25203 - store the discriminant field in STRUCT_TYPE that controls the variant
25204 part to *DISCR_DECL
25206 - put in *DISCR_LISTS_P an array where for each variant, the item
25207 represents the corresponding matching list of discriminant values.
25209 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25210 the above array.
25212 Note that when the array is allocated (i.e. when the analysis is
25213 successful), it is up to the caller to free the array. */
25215 static void
25216 analyze_variants_discr (tree variant_part_decl,
25217 tree struct_type,
25218 tree *discr_decl,
25219 dw_discr_list_ref **discr_lists_p,
25220 unsigned *discr_lists_length)
25222 tree variant_part_type = TREE_TYPE (variant_part_decl);
25223 tree variant;
25224 dw_discr_list_ref *discr_lists;
25225 unsigned i;
25227 /* Compute how many variants there are in this variant part. */
25228 *discr_lists_length = 0;
25229 for (variant = TYPE_FIELDS (variant_part_type);
25230 variant != NULL_TREE;
25231 variant = DECL_CHAIN (variant))
25232 ++*discr_lists_length;
25234 *discr_decl = NULL_TREE;
25235 *discr_lists_p
25236 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25237 sizeof (**discr_lists_p));
25238 discr_lists = *discr_lists_p;
25240 /* And then analyze all variants to extract discriminant information for all
25241 of them. This analysis is conservative: as soon as we detect something we
25242 do not support, abort everything and pretend we found nothing. */
25243 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25244 variant != NULL_TREE;
25245 variant = DECL_CHAIN (variant), ++i)
25247 tree match_expr = DECL_QUALIFIER (variant);
25249 /* Now, try to analyze the predicate and deduce a discriminant for
25250 it. */
25251 if (match_expr == boolean_true_node)
25252 /* Typically happens for the default variant: it matches all cases that
25253 previous variants rejected. Don't output any matching value for
25254 this one. */
25255 continue;
25257 /* The following loop tries to iterate over each discriminant
25258 possibility: single values or ranges. */
25259 while (match_expr != NULL_TREE)
25261 tree next_round_match_expr;
25262 tree candidate_discr = NULL_TREE;
25263 dw_discr_list_ref new_node = NULL;
25265 /* Possibilities are matched one after the other by nested
25266 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25267 continue with the rest at next iteration. */
25268 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25270 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25271 match_expr = TREE_OPERAND (match_expr, 1);
25273 else
25274 next_round_match_expr = NULL_TREE;
25276 if (match_expr == boolean_false_node)
25277 /* This sub-expression matches nothing: just wait for the next
25278 one. */
25281 else if (TREE_CODE (match_expr) == EQ_EXPR)
25283 /* We are matching: <discr_field> == <integer_cst>
25284 This sub-expression matches a single value. */
25285 tree integer_cst = TREE_OPERAND (match_expr, 1);
25287 candidate_discr
25288 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25289 struct_type);
25291 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25292 if (!get_discr_value (integer_cst,
25293 &new_node->dw_discr_lower_bound))
25294 goto abort;
25295 new_node->dw_discr_range = false;
25298 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25300 /* We are matching:
25301 <discr_field> > <integer_cst>
25302 && <discr_field> < <integer_cst>.
25303 This sub-expression matches the range of values between the
25304 two matched integer constants. Note that comparisons can be
25305 inclusive or exclusive. */
25306 tree candidate_discr_1, candidate_discr_2;
25307 tree lower_cst, upper_cst;
25308 bool lower_cst_included, upper_cst_included;
25309 tree lower_op = TREE_OPERAND (match_expr, 0);
25310 tree upper_op = TREE_OPERAND (match_expr, 1);
25312 /* When the comparison is exclusive, the integer constant is not
25313 the discriminant range bound we are looking for: we will have
25314 to increment or decrement it. */
25315 if (TREE_CODE (lower_op) == GE_EXPR)
25316 lower_cst_included = true;
25317 else if (TREE_CODE (lower_op) == GT_EXPR)
25318 lower_cst_included = false;
25319 else
25320 goto abort;
25322 if (TREE_CODE (upper_op) == LE_EXPR)
25323 upper_cst_included = true;
25324 else if (TREE_CODE (upper_op) == LT_EXPR)
25325 upper_cst_included = false;
25326 else
25327 goto abort;
25329 /* Extract the discriminant from the first operand and check it
25330 is consistant with the same analysis in the second
25331 operand. */
25332 candidate_discr_1
25333 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25334 struct_type);
25335 candidate_discr_2
25336 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25337 struct_type);
25338 if (candidate_discr_1 == candidate_discr_2)
25339 candidate_discr = candidate_discr_1;
25340 else
25341 goto abort;
25343 /* Extract bounds from both. */
25344 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25345 lower_cst = TREE_OPERAND (lower_op, 1);
25346 upper_cst = TREE_OPERAND (upper_op, 1);
25348 if (!lower_cst_included)
25349 lower_cst
25350 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25351 build_int_cst (TREE_TYPE (lower_cst), 1));
25352 if (!upper_cst_included)
25353 upper_cst
25354 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25355 build_int_cst (TREE_TYPE (upper_cst), 1));
25357 if (!get_discr_value (lower_cst,
25358 &new_node->dw_discr_lower_bound)
25359 || !get_discr_value (upper_cst,
25360 &new_node->dw_discr_upper_bound))
25361 goto abort;
25363 new_node->dw_discr_range = true;
25366 else if ((candidate_discr
25367 = analyze_discr_in_predicate (match_expr, struct_type))
25368 && (TREE_TYPE (candidate_discr) == boolean_type_node
25369 || TREE_TYPE (TREE_TYPE (candidate_discr))
25370 == boolean_type_node))
25372 /* We are matching: <discr_field> for a boolean discriminant.
25373 This sub-expression matches boolean_true_node. */
25374 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25375 if (!get_discr_value (boolean_true_node,
25376 &new_node->dw_discr_lower_bound))
25377 goto abort;
25378 new_node->dw_discr_range = false;
25381 else
25382 /* Unsupported sub-expression: we cannot determine the set of
25383 matching discriminant values. Abort everything. */
25384 goto abort;
25386 /* If the discriminant info is not consistant with what we saw so
25387 far, consider the analysis failed and abort everything. */
25388 if (candidate_discr == NULL_TREE
25389 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25390 goto abort;
25391 else
25392 *discr_decl = candidate_discr;
25394 if (new_node != NULL)
25396 new_node->dw_discr_next = discr_lists[i];
25397 discr_lists[i] = new_node;
25399 match_expr = next_round_match_expr;
25403 /* If we reach this point, we could match everything we were interested
25404 in. */
25405 return;
25407 abort:
25408 /* Clean all data structure and return no result. */
25409 free (*discr_lists_p);
25410 *discr_lists_p = NULL;
25411 *discr_decl = NULL_TREE;
25414 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25415 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25416 under CONTEXT_DIE.
25418 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25419 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25420 this type, which are record types, represent the available variants and each
25421 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25422 values are inferred from these attributes.
25424 In trees, the offsets for the fields inside these sub-records are relative
25425 to the variant part itself, whereas the corresponding DIEs should have
25426 offset attributes that are relative to the embedding record base address.
25427 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25428 must be an expression that computes the offset of the variant part to
25429 describe in DWARF. */
25431 static void
25432 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25433 dw_die_ref context_die)
25435 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25436 tree variant_part_offset = vlr_ctx->variant_part_offset;
25438 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25439 NULL_TREE if there is no such field. */
25440 tree discr_decl = NULL_TREE;
25441 dw_discr_list_ref *discr_lists;
25442 unsigned discr_lists_length = 0;
25443 unsigned i;
25445 dw_die_ref dwarf_proc_die = NULL;
25446 dw_die_ref variant_part_die
25447 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25449 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25451 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25452 &discr_decl, &discr_lists, &discr_lists_length);
25454 if (discr_decl != NULL_TREE)
25456 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25458 if (discr_die)
25459 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25460 else
25461 /* We have no DIE for the discriminant, so just discard all
25462 discrimimant information in the output. */
25463 discr_decl = NULL_TREE;
25466 /* If the offset for this variant part is more complex than a constant,
25467 create a DWARF procedure for it so that we will not have to generate
25468 DWARF expressions for it for each member. */
25469 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25470 && (dwarf_version >= 3 || !dwarf_strict))
25472 struct loc_descr_context ctx = {
25473 vlr_ctx->struct_type, /* context_type */
25474 NULL_TREE, /* base_decl */
25475 NULL, /* dpi */
25476 false, /* placeholder_arg */
25477 false, /* placeholder_seen */
25478 false /* strict_signedness */
25480 const tree dwarf_proc_fndecl
25481 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25482 build_function_type (TREE_TYPE (variant_part_offset),
25483 NULL_TREE));
25484 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25485 const dw_loc_descr_ref dwarf_proc_body
25486 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25488 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25489 dwarf_proc_fndecl, context_die);
25490 if (dwarf_proc_die != NULL)
25491 variant_part_offset = dwarf_proc_call;
25494 /* Output DIEs for all variants. */
25495 i = 0;
25496 for (tree variant = TYPE_FIELDS (variant_part_type);
25497 variant != NULL_TREE;
25498 variant = DECL_CHAIN (variant), ++i)
25500 tree variant_type = TREE_TYPE (variant);
25501 dw_die_ref variant_die;
25503 /* All variants (i.e. members of a variant part) are supposed to be
25504 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25505 under these records. */
25506 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25508 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25509 equate_decl_number_to_die (variant, variant_die);
25511 /* Output discriminant values this variant matches, if any. */
25512 if (discr_decl == NULL || discr_lists[i] == NULL)
25513 /* In the case we have discriminant information at all, this is
25514 probably the default variant: as the standard says, don't
25515 output any discriminant value/list attribute. */
25517 else if (discr_lists[i]->dw_discr_next == NULL
25518 && !discr_lists[i]->dw_discr_range)
25519 /* If there is only one accepted value, don't bother outputting a
25520 list. */
25521 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25522 else
25523 add_discr_list (variant_die, discr_lists[i]);
25525 for (tree member = TYPE_FIELDS (variant_type);
25526 member != NULL_TREE;
25527 member = DECL_CHAIN (member))
25529 struct vlr_context vlr_sub_ctx = {
25530 vlr_ctx->struct_type, /* struct_type */
25531 NULL /* variant_part_offset */
25533 if (is_variant_part (member))
25535 /* All offsets for fields inside variant parts are relative to
25536 the top-level embedding RECORD_TYPE's base address. On the
25537 other hand, offsets in GCC's types are relative to the
25538 nested-most variant part. So we have to sum offsets each time
25539 we recurse. */
25541 vlr_sub_ctx.variant_part_offset
25542 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25543 variant_part_offset, byte_position (member));
25544 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25546 else
25548 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25549 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25554 free (discr_lists);
25557 /* Generate a DIE for a class member. */
25559 static void
25560 gen_member_die (tree type, dw_die_ref context_die)
25562 tree member;
25563 tree binfo = TYPE_BINFO (type);
25565 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25567 /* If this is not an incomplete type, output descriptions of each of its
25568 members. Note that as we output the DIEs necessary to represent the
25569 members of this record or union type, we will also be trying to output
25570 DIEs to represent the *types* of those members. However the `type'
25571 function (above) will specifically avoid generating type DIEs for member
25572 types *within* the list of member DIEs for this (containing) type except
25573 for those types (of members) which are explicitly marked as also being
25574 members of this (containing) type themselves. The g++ front- end can
25575 force any given type to be treated as a member of some other (containing)
25576 type by setting the TYPE_CONTEXT of the given (member) type to point to
25577 the TREE node representing the appropriate (containing) type. */
25579 /* First output info about the base classes. */
25580 if (binfo && early_dwarf)
25582 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25583 int i;
25584 tree base;
25586 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25587 gen_inheritance_die (base,
25588 (accesses ? (*accesses)[i] : access_public_node),
25589 type,
25590 context_die);
25593 /* Now output info about the members. */
25594 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25596 /* Ignore clones. */
25597 if (DECL_ABSTRACT_ORIGIN (member))
25598 continue;
25600 struct vlr_context vlr_ctx = { type, NULL_TREE };
25601 bool static_inline_p
25602 = (VAR_P (member)
25603 && TREE_STATIC (member)
25604 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25605 != -1));
25607 /* If we thought we were generating minimal debug info for TYPE
25608 and then changed our minds, some of the member declarations
25609 may have already been defined. Don't define them again, but
25610 do put them in the right order. */
25612 if (dw_die_ref child = lookup_decl_die (member))
25614 /* Handle inline static data members, which only have in-class
25615 declarations. */
25616 bool splice = true;
25618 dw_die_ref ref = NULL;
25619 if (child->die_tag == DW_TAG_variable
25620 && child->die_parent == comp_unit_die ())
25622 ref = get_AT_ref (child, DW_AT_specification);
25624 /* For C++17 inline static data members followed by redundant
25625 out of class redeclaration, we might get here with
25626 child being the DIE created for the out of class
25627 redeclaration and with its DW_AT_specification being
25628 the DIE created for in-class definition. We want to
25629 reparent the latter, and don't want to create another
25630 DIE with DW_AT_specification in that case, because
25631 we already have one. */
25632 if (ref
25633 && static_inline_p
25634 && ref->die_tag == DW_TAG_variable
25635 && ref->die_parent == comp_unit_die ()
25636 && get_AT (ref, DW_AT_specification) == NULL)
25638 child = ref;
25639 ref = NULL;
25640 static_inline_p = false;
25643 if (!ref)
25645 reparent_child (child, context_die);
25646 if (dwarf_version < 5)
25647 child->die_tag = DW_TAG_member;
25648 splice = false;
25651 else if (child->die_tag == DW_TAG_enumerator)
25652 /* Enumerators remain under their enumeration even if
25653 their names are introduced in the enclosing scope. */
25654 splice = false;
25656 if (splice)
25657 splice_child_die (context_die, child);
25660 /* Do not generate DWARF for variant parts if we are generating the
25661 corresponding GNAT encodings: DIEs generated for the two schemes
25662 would conflict in our mappings. */
25663 else if (is_variant_part (member)
25664 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25666 vlr_ctx.variant_part_offset = byte_position (member);
25667 gen_variant_part (member, &vlr_ctx, context_die);
25669 else
25671 vlr_ctx.variant_part_offset = NULL_TREE;
25672 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25675 /* For C++ inline static data members emit immediately a DW_TAG_variable
25676 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25677 DW_AT_specification. */
25678 if (static_inline_p)
25680 int old_extern = DECL_EXTERNAL (member);
25681 DECL_EXTERNAL (member) = 0;
25682 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25683 DECL_EXTERNAL (member) = old_extern;
25688 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25689 is set, we pretend that the type was never defined, so we only get the
25690 member DIEs needed by later specification DIEs. */
25692 static void
25693 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25694 enum debug_info_usage usage)
25696 if (TREE_ASM_WRITTEN (type))
25698 /* Fill in the bound of variable-length fields in late dwarf if
25699 still incomplete. */
25700 if (!early_dwarf && variably_modified_type_p (type, NULL))
25701 for (tree member = TYPE_FIELDS (type);
25702 member;
25703 member = DECL_CHAIN (member))
25704 fill_variable_array_bounds (TREE_TYPE (member));
25705 return;
25708 dw_die_ref type_die = lookup_type_die (type);
25709 dw_die_ref scope_die = 0;
25710 int nested = 0;
25711 int complete = (TYPE_SIZE (type)
25712 && (! TYPE_STUB_DECL (type)
25713 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25714 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25715 complete = complete && should_emit_struct_debug (type, usage);
25717 if (type_die && ! complete)
25718 return;
25720 if (TYPE_CONTEXT (type) != NULL_TREE
25721 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25722 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25723 nested = 1;
25725 scope_die = scope_die_for (type, context_die);
25727 /* Generate child dies for template parameters. */
25728 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25729 schedule_generic_params_dies_gen (type);
25731 if (! type_die || (nested && is_cu_die (scope_die)))
25732 /* First occurrence of type or toplevel definition of nested class. */
25734 dw_die_ref old_die = type_die;
25736 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25737 ? record_type_tag (type) : DW_TAG_union_type,
25738 scope_die, type);
25739 equate_type_number_to_die (type, type_die);
25740 if (old_die)
25741 add_AT_specification (type_die, old_die);
25742 else
25743 add_name_attribute (type_die, type_tag (type));
25745 else
25746 remove_AT (type_die, DW_AT_declaration);
25748 /* If this type has been completed, then give it a byte_size attribute and
25749 then give a list of members. */
25750 if (complete && !ns_decl)
25752 /* Prevent infinite recursion in cases where the type of some member of
25753 this type is expressed in terms of this type itself. */
25754 TREE_ASM_WRITTEN (type) = 1;
25755 add_byte_size_attribute (type_die, type);
25756 add_alignment_attribute (type_die, type);
25757 if (TYPE_STUB_DECL (type) != NULL_TREE)
25759 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25760 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25763 /* If the first reference to this type was as the return type of an
25764 inline function, then it may not have a parent. Fix this now. */
25765 if (type_die->die_parent == NULL)
25766 add_child_die (scope_die, type_die);
25768 gen_member_die (type, type_die);
25770 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25771 if (TYPE_ARTIFICIAL (type))
25772 add_AT_flag (type_die, DW_AT_artificial, 1);
25774 /* GNU extension: Record what type our vtable lives in. */
25775 if (TYPE_VFIELD (type))
25777 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25779 gen_type_die (vtype, context_die);
25780 add_AT_die_ref (type_die, DW_AT_containing_type,
25781 lookup_type_die (vtype));
25784 else
25786 add_AT_flag (type_die, DW_AT_declaration, 1);
25788 /* We don't need to do this for function-local types. */
25789 if (TYPE_STUB_DECL (type)
25790 && ! decl_function_context (TYPE_STUB_DECL (type)))
25791 vec_safe_push (incomplete_types, type);
25794 if (get_AT (type_die, DW_AT_name))
25795 add_pubtype (type, type_die);
25798 /* Generate a DIE for a subroutine _type_. */
25800 static void
25801 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25803 tree return_type = TREE_TYPE (type);
25804 dw_die_ref subr_die
25805 = new_die (DW_TAG_subroutine_type,
25806 scope_die_for (type, context_die), type);
25808 equate_type_number_to_die (type, subr_die);
25809 add_prototyped_attribute (subr_die, type);
25810 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25811 context_die);
25812 add_alignment_attribute (subr_die, type);
25813 gen_formal_types_die (type, subr_die);
25815 if (get_AT (subr_die, DW_AT_name))
25816 add_pubtype (type, subr_die);
25817 if ((dwarf_version >= 5 || !dwarf_strict)
25818 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25819 add_AT_flag (subr_die, DW_AT_reference, 1);
25820 if ((dwarf_version >= 5 || !dwarf_strict)
25821 && lang_hooks.types.type_dwarf_attribute (type,
25822 DW_AT_rvalue_reference) != -1)
25823 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25826 /* Generate a DIE for a type definition. */
25828 static void
25829 gen_typedef_die (tree decl, dw_die_ref context_die)
25831 dw_die_ref type_die;
25832 tree type;
25834 if (TREE_ASM_WRITTEN (decl))
25836 if (DECL_ORIGINAL_TYPE (decl))
25837 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25838 return;
25841 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25842 checks in process_scope_var and modified_type_die), this should be called
25843 only for original types. */
25844 gcc_assert (decl_ultimate_origin (decl) == NULL
25845 || decl_ultimate_origin (decl) == decl);
25847 TREE_ASM_WRITTEN (decl) = 1;
25848 type_die = new_die (DW_TAG_typedef, context_die, decl);
25850 add_name_and_src_coords_attributes (type_die, decl);
25851 if (DECL_ORIGINAL_TYPE (decl))
25853 type = DECL_ORIGINAL_TYPE (decl);
25854 if (type == error_mark_node)
25855 return;
25857 gcc_assert (type != TREE_TYPE (decl));
25858 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25860 else
25862 type = TREE_TYPE (decl);
25863 if (type == error_mark_node)
25864 return;
25866 if (is_naming_typedef_decl (TYPE_NAME (type)))
25868 /* Here, we are in the case of decl being a typedef naming
25869 an anonymous type, e.g:
25870 typedef struct {...} foo;
25871 In that case TREE_TYPE (decl) is not a typedef variant
25872 type and TYPE_NAME of the anonymous type is set to the
25873 TYPE_DECL of the typedef. This construct is emitted by
25874 the C++ FE.
25876 TYPE is the anonymous struct named by the typedef
25877 DECL. As we need the DW_AT_type attribute of the
25878 DW_TAG_typedef to point to the DIE of TYPE, let's
25879 generate that DIE right away. add_type_attribute
25880 called below will then pick (via lookup_type_die) that
25881 anonymous struct DIE. */
25882 if (!TREE_ASM_WRITTEN (type))
25883 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25885 /* This is a GNU Extension. We are adding a
25886 DW_AT_linkage_name attribute to the DIE of the
25887 anonymous struct TYPE. The value of that attribute
25888 is the name of the typedef decl naming the anonymous
25889 struct. This greatly eases the work of consumers of
25890 this debug info. */
25891 add_linkage_name_raw (lookup_type_die (type), decl);
25895 add_type_attribute (type_die, type, decl_quals (decl), false,
25896 context_die);
25898 if (is_naming_typedef_decl (decl))
25899 /* We want that all subsequent calls to lookup_type_die with
25900 TYPE in argument yield the DW_TAG_typedef we have just
25901 created. */
25902 equate_type_number_to_die (type, type_die);
25904 add_alignment_attribute (type_die, TREE_TYPE (decl));
25906 add_accessibility_attribute (type_die, decl);
25908 if (DECL_ABSTRACT_P (decl))
25909 equate_decl_number_to_die (decl, type_die);
25911 if (get_AT (type_die, DW_AT_name))
25912 add_pubtype (decl, type_die);
25915 /* Generate a DIE for a struct, class, enum or union type. */
25917 static void
25918 gen_tagged_type_die (tree type,
25919 dw_die_ref context_die,
25920 enum debug_info_usage usage)
25922 if (type == NULL_TREE
25923 || !is_tagged_type (type))
25924 return;
25926 if (TREE_ASM_WRITTEN (type))
25928 /* If this is a nested type whose containing class hasn't been written
25929 out yet, writing it out will cover this one, too. This does not apply
25930 to instantiations of member class templates; they need to be added to
25931 the containing class as they are generated. FIXME: This hurts the
25932 idea of combining type decls from multiple TUs, since we can't predict
25933 what set of template instantiations we'll get. */
25934 else if (TYPE_CONTEXT (type)
25935 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25936 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25938 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25940 if (TREE_ASM_WRITTEN (type))
25941 return;
25943 /* If that failed, attach ourselves to the stub. */
25944 context_die = lookup_type_die (TYPE_CONTEXT (type));
25946 else if (TYPE_CONTEXT (type) != NULL_TREE
25947 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25949 /* If this type is local to a function that hasn't been written
25950 out yet, use a NULL context for now; it will be fixed up in
25951 decls_for_scope. */
25952 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25953 /* A declaration DIE doesn't count; nested types need to go in the
25954 specification. */
25955 if (context_die && is_declaration_die (context_die))
25956 context_die = NULL;
25958 else
25959 context_die = declare_in_namespace (type, context_die);
25961 if (TREE_CODE (type) == ENUMERAL_TYPE)
25963 /* This might have been written out by the call to
25964 declare_in_namespace. */
25965 if (!TREE_ASM_WRITTEN (type))
25966 gen_enumeration_type_die (type, context_die);
25968 else
25969 gen_struct_or_union_type_die (type, context_die, usage);
25971 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25972 it up if it is ever completed. gen_*_type_die will set it for us
25973 when appropriate. */
25976 /* Generate a type description DIE. */
25978 static void
25979 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25980 enum debug_info_usage usage)
25982 struct array_descr_info info;
25984 if (type == NULL_TREE || type == error_mark_node)
25985 return;
25987 if (flag_checking && type)
25988 verify_type (type);
25990 if (TYPE_NAME (type) != NULL_TREE
25991 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25992 && is_redundant_typedef (TYPE_NAME (type))
25993 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25994 /* The DECL of this type is a typedef we don't want to emit debug
25995 info for but we want debug info for its underlying typedef.
25996 This can happen for e.g, the injected-class-name of a C++
25997 type. */
25998 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
26000 /* If TYPE is a typedef type variant, let's generate debug info
26001 for the parent typedef which TYPE is a type of. */
26002 if (typedef_variant_p (type))
26004 if (TREE_ASM_WRITTEN (type))
26005 return;
26007 tree name = TYPE_NAME (type);
26008 tree origin = decl_ultimate_origin (name);
26009 if (origin != NULL && origin != name)
26011 gen_decl_die (origin, NULL, NULL, context_die);
26012 return;
26015 /* Prevent broken recursion; we can't hand off to the same type. */
26016 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26018 /* Give typedefs the right scope. */
26019 context_die = scope_die_for (type, context_die);
26021 TREE_ASM_WRITTEN (type) = 1;
26023 gen_decl_die (name, NULL, NULL, context_die);
26024 return;
26027 /* If type is an anonymous tagged type named by a typedef, let's
26028 generate debug info for the typedef. */
26029 if (is_naming_typedef_decl (TYPE_NAME (type)))
26031 /* Give typedefs the right scope. */
26032 context_die = scope_die_for (type, context_die);
26034 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26035 return;
26038 if (lang_hooks.types.get_debug_type)
26040 tree debug_type = lang_hooks.types.get_debug_type (type);
26042 if (debug_type != NULL_TREE && debug_type != type)
26044 gen_type_die_with_usage (debug_type, context_die, usage);
26045 return;
26049 /* We are going to output a DIE to represent the unqualified version
26050 of this type (i.e. without any const or volatile qualifiers) so
26051 get the main variant (i.e. the unqualified version) of this type
26052 now. (Vectors and arrays are special because the debugging info is in the
26053 cloned type itself. Similarly function/method types can contain extra
26054 ref-qualification). */
26055 if (TREE_CODE (type) == FUNCTION_TYPE
26056 || TREE_CODE (type) == METHOD_TYPE)
26058 /* For function/method types, can't use type_main_variant here,
26059 because that can have different ref-qualifiers for C++,
26060 but try to canonicalize. */
26061 tree main = TYPE_MAIN_VARIANT (type);
26062 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26063 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26064 && check_base_type (t, main)
26065 && check_lang_type (t, type))
26067 type = t;
26068 break;
26071 else if (TREE_CODE (type) != VECTOR_TYPE
26072 && TREE_CODE (type) != ARRAY_TYPE)
26073 type = type_main_variant (type);
26075 /* If this is an array type with hidden descriptor, handle it first. */
26076 if (!TREE_ASM_WRITTEN (type)
26077 && lang_hooks.types.get_array_descr_info)
26079 memset (&info, 0, sizeof (info));
26080 if (lang_hooks.types.get_array_descr_info (type, &info))
26082 /* Fortran sometimes emits array types with no dimension. */
26083 gcc_assert (info.ndimensions >= 0
26084 && (info.ndimensions
26085 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26086 gen_descr_array_type_die (type, &info, context_die);
26087 TREE_ASM_WRITTEN (type) = 1;
26088 return;
26092 if (TREE_ASM_WRITTEN (type))
26094 /* Variable-length types may be incomplete even if
26095 TREE_ASM_WRITTEN. For such types, fall through to
26096 gen_array_type_die() and possibly fill in
26097 DW_AT_{upper,lower}_bound attributes. */
26098 if ((TREE_CODE (type) != ARRAY_TYPE
26099 && TREE_CODE (type) != RECORD_TYPE
26100 && TREE_CODE (type) != UNION_TYPE
26101 && TREE_CODE (type) != QUAL_UNION_TYPE)
26102 || !variably_modified_type_p (type, NULL))
26103 return;
26106 switch (TREE_CODE (type))
26108 case ERROR_MARK:
26109 break;
26111 case POINTER_TYPE:
26112 case REFERENCE_TYPE:
26113 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26114 ensures that the gen_type_die recursion will terminate even if the
26115 type is recursive. Recursive types are possible in Ada. */
26116 /* ??? We could perhaps do this for all types before the switch
26117 statement. */
26118 TREE_ASM_WRITTEN (type) = 1;
26120 /* For these types, all that is required is that we output a DIE (or a
26121 set of DIEs) to represent the "basis" type. */
26122 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26123 DINFO_USAGE_IND_USE);
26124 break;
26126 case OFFSET_TYPE:
26127 /* This code is used for C++ pointer-to-data-member types.
26128 Output a description of the relevant class type. */
26129 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26130 DINFO_USAGE_IND_USE);
26132 /* Output a description of the type of the object pointed to. */
26133 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26134 DINFO_USAGE_IND_USE);
26136 /* Now output a DIE to represent this pointer-to-data-member type
26137 itself. */
26138 gen_ptr_to_mbr_type_die (type, context_die);
26139 break;
26141 case FUNCTION_TYPE:
26142 /* Force out return type (in case it wasn't forced out already). */
26143 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26144 DINFO_USAGE_DIR_USE);
26145 gen_subroutine_type_die (type, context_die);
26146 break;
26148 case METHOD_TYPE:
26149 /* Force out return type (in case it wasn't forced out already). */
26150 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26151 DINFO_USAGE_DIR_USE);
26152 gen_subroutine_type_die (type, context_die);
26153 break;
26155 case ARRAY_TYPE:
26156 case VECTOR_TYPE:
26157 gen_array_type_die (type, context_die);
26158 break;
26160 case ENUMERAL_TYPE:
26161 case RECORD_TYPE:
26162 case UNION_TYPE:
26163 case QUAL_UNION_TYPE:
26164 gen_tagged_type_die (type, context_die, usage);
26165 return;
26167 case VOID_TYPE:
26168 case OPAQUE_TYPE:
26169 case INTEGER_TYPE:
26170 case REAL_TYPE:
26171 case FIXED_POINT_TYPE:
26172 case COMPLEX_TYPE:
26173 case BOOLEAN_TYPE:
26174 /* No DIEs needed for fundamental types. */
26175 break;
26177 case NULLPTR_TYPE:
26178 case LANG_TYPE:
26179 /* Just use DW_TAG_unspecified_type. */
26181 dw_die_ref type_die = lookup_type_die (type);
26182 if (type_die == NULL)
26184 tree name = TYPE_IDENTIFIER (type);
26185 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26186 type);
26187 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26188 equate_type_number_to_die (type, type_die);
26191 break;
26193 default:
26194 if (is_cxx_auto (type))
26196 tree name = TYPE_IDENTIFIER (type);
26197 dw_die_ref *die = (name == get_identifier ("auto")
26198 ? &auto_die : &decltype_auto_die);
26199 if (!*die)
26201 *die = new_die (DW_TAG_unspecified_type,
26202 comp_unit_die (), NULL_TREE);
26203 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26205 equate_type_number_to_die (type, *die);
26206 break;
26208 gcc_unreachable ();
26211 TREE_ASM_WRITTEN (type) = 1;
26214 static void
26215 gen_type_die (tree type, dw_die_ref context_die)
26217 if (type != error_mark_node)
26219 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26220 if (flag_checking)
26222 dw_die_ref die = lookup_type_die (type);
26223 if (die)
26224 check_die (die);
26229 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26230 things which are local to the given block. */
26232 static void
26233 gen_block_die (tree stmt, dw_die_ref context_die)
26235 int must_output_die = 0;
26236 bool inlined_func;
26238 /* Ignore blocks that are NULL. */
26239 if (stmt == NULL_TREE)
26240 return;
26242 inlined_func = inlined_function_outer_scope_p (stmt);
26244 /* If the block is one fragment of a non-contiguous block, do not
26245 process the variables, since they will have been done by the
26246 origin block. Do process subblocks. */
26247 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26249 tree sub;
26251 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26252 gen_block_die (sub, context_die);
26254 return;
26257 /* Determine if we need to output any Dwarf DIEs at all to represent this
26258 block. */
26259 if (inlined_func)
26260 /* The outer scopes for inlinings *must* always be represented. We
26261 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26262 must_output_die = 1;
26263 else if (lookup_block_die (stmt))
26264 /* If we already have a DIE then it was filled early. Meanwhile
26265 we might have pruned all BLOCK_VARS as optimized out but we
26266 still want to generate high/low PC attributes so output it. */
26267 must_output_die = 1;
26268 else if (TREE_USED (stmt)
26269 || TREE_ASM_WRITTEN (stmt))
26271 /* Determine if this block directly contains any "significant"
26272 local declarations which we will need to output DIEs for. */
26273 if (debug_info_level > DINFO_LEVEL_TERSE)
26275 /* We are not in terse mode so any local declaration that
26276 is not ignored for debug purposes counts as being a
26277 "significant" one. */
26278 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26279 must_output_die = 1;
26280 else
26281 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26282 if (!DECL_IGNORED_P (var))
26284 must_output_die = 1;
26285 break;
26288 else if (!dwarf2out_ignore_block (stmt))
26289 must_output_die = 1;
26292 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26293 DIE for any block which contains no significant local declarations at
26294 all. Rather, in such cases we just call `decls_for_scope' so that any
26295 needed Dwarf info for any sub-blocks will get properly generated. Note
26296 that in terse mode, our definition of what constitutes a "significant"
26297 local declaration gets restricted to include only inlined function
26298 instances and local (nested) function definitions. */
26299 if (must_output_die)
26301 if (inlined_func)
26302 gen_inlined_subroutine_die (stmt, context_die);
26303 else
26304 gen_lexical_block_die (stmt, context_die);
26306 else
26307 decls_for_scope (stmt, context_die);
26310 /* Process variable DECL (or variable with origin ORIGIN) within
26311 block STMT and add it to CONTEXT_DIE. */
26312 static void
26313 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26315 dw_die_ref die;
26316 tree decl_or_origin = decl ? decl : origin;
26318 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26319 die = lookup_decl_die (decl_or_origin);
26320 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26322 if (TYPE_DECL_IS_STUB (decl_or_origin))
26323 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26324 else
26325 die = lookup_decl_die (decl_or_origin);
26326 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26327 if (! die && ! early_dwarf)
26328 return;
26330 else
26331 die = NULL;
26333 /* Avoid creating DIEs for local typedefs and concrete static variables that
26334 will only be pruned later. */
26335 if ((origin || decl_ultimate_origin (decl))
26336 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26337 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26339 origin = decl_ultimate_origin (decl_or_origin);
26340 if (decl && VAR_P (decl) && die != NULL)
26342 die = lookup_decl_die (origin);
26343 if (die != NULL)
26344 equate_decl_number_to_die (decl, die);
26346 return;
26349 if (die != NULL && die->die_parent == NULL)
26350 add_child_die (context_die, die);
26351 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26353 if (early_dwarf)
26354 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26355 stmt, context_die);
26357 else
26359 if (decl && DECL_P (decl))
26361 die = lookup_decl_die (decl);
26363 /* Early created DIEs do not have a parent as the decls refer
26364 to the function as DECL_CONTEXT rather than the BLOCK. */
26365 if (die && die->die_parent == NULL)
26367 gcc_assert (in_lto_p);
26368 add_child_die (context_die, die);
26372 gen_decl_die (decl, origin, NULL, context_die);
26376 /* Generate all of the decls declared within a given scope and (recursively)
26377 all of its sub-blocks. */
26379 static void
26380 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26382 tree decl;
26383 unsigned int i;
26384 tree subblocks;
26386 /* Ignore NULL blocks. */
26387 if (stmt == NULL_TREE)
26388 return;
26390 /* Output the DIEs to represent all of the data objects and typedefs
26391 declared directly within this block but not within any nested
26392 sub-blocks. Also, nested function and tag DIEs have been
26393 generated with a parent of NULL; fix that up now. We don't
26394 have to do this if we're at -g1. */
26395 if (debug_info_level > DINFO_LEVEL_TERSE)
26397 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26398 process_scope_var (stmt, decl, NULL_TREE, context_die);
26399 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26400 origin - avoid doing this twice as we have no good way to see
26401 if we've done it once already. */
26402 if (! early_dwarf)
26403 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26405 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26406 if (decl == current_function_decl)
26407 /* Ignore declarations of the current function, while they
26408 are declarations, gen_subprogram_die would treat them
26409 as definitions again, because they are equal to
26410 current_function_decl and endlessly recurse. */;
26411 else if (TREE_CODE (decl) == FUNCTION_DECL)
26412 process_scope_var (stmt, decl, NULL_TREE, context_die);
26413 else
26414 process_scope_var (stmt, NULL_TREE, decl, context_die);
26418 /* Even if we're at -g1, we need to process the subblocks in order to get
26419 inlined call information. */
26421 /* Output the DIEs to represent all sub-blocks (and the items declared
26422 therein) of this block. */
26423 if (recurse)
26424 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26425 subblocks != NULL;
26426 subblocks = BLOCK_CHAIN (subblocks))
26427 gen_block_die (subblocks, context_die);
26430 /* Is this a typedef we can avoid emitting? */
26432 static bool
26433 is_redundant_typedef (const_tree decl)
26435 if (TYPE_DECL_IS_STUB (decl))
26436 return true;
26438 if (DECL_ARTIFICIAL (decl)
26439 && DECL_CONTEXT (decl)
26440 && is_tagged_type (DECL_CONTEXT (decl))
26441 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26442 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26443 /* Also ignore the artificial member typedef for the class name. */
26444 return true;
26446 return false;
26449 /* Return TRUE if TYPE is a typedef that names a type for linkage
26450 purposes. This kind of typedefs is produced by the C++ FE for
26451 constructs like:
26453 typedef struct {...} foo;
26455 In that case, there is no typedef variant type produced for foo.
26456 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26457 struct type. */
26459 static bool
26460 is_naming_typedef_decl (const_tree decl)
26462 if (decl == NULL_TREE
26463 || TREE_CODE (decl) != TYPE_DECL
26464 || DECL_NAMELESS (decl)
26465 || !is_tagged_type (TREE_TYPE (decl))
26466 || DECL_IS_UNDECLARED_BUILTIN (decl)
26467 || is_redundant_typedef (decl)
26468 /* It looks like Ada produces TYPE_DECLs that are very similar
26469 to C++ naming typedefs but that have different
26470 semantics. Let's be specific to c++ for now. */
26471 || !is_cxx (decl))
26472 return FALSE;
26474 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26475 && TYPE_NAME (TREE_TYPE (decl)) == decl
26476 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26477 != TYPE_NAME (TREE_TYPE (decl))));
26480 /* Looks up the DIE for a context. */
26482 static inline dw_die_ref
26483 lookup_context_die (tree context)
26485 if (context)
26487 /* Find die that represents this context. */
26488 if (TYPE_P (context))
26490 context = TYPE_MAIN_VARIANT (context);
26491 dw_die_ref ctx = lookup_type_die (context);
26492 if (!ctx)
26493 return NULL;
26494 return strip_naming_typedef (context, ctx);
26496 else
26497 return lookup_decl_die (context);
26499 return comp_unit_die ();
26502 /* Returns the DIE for a context. */
26504 static inline dw_die_ref
26505 get_context_die (tree context)
26507 if (context)
26509 /* Find die that represents this context. */
26510 if (TYPE_P (context))
26512 context = TYPE_MAIN_VARIANT (context);
26513 return strip_naming_typedef (context, force_type_die (context));
26515 else
26516 return force_decl_die (context);
26518 return comp_unit_die ();
26521 /* Returns the DIE for decl. A DIE will always be returned. */
26523 static dw_die_ref
26524 force_decl_die (tree decl)
26526 dw_die_ref decl_die;
26527 unsigned saved_external_flag;
26528 tree save_fn = NULL_TREE;
26529 decl_die = lookup_decl_die (decl);
26530 if (!decl_die)
26532 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26534 decl_die = lookup_decl_die (decl);
26535 if (decl_die)
26536 return decl_die;
26538 switch (TREE_CODE (decl))
26540 case FUNCTION_DECL:
26541 /* Clear current_function_decl, so that gen_subprogram_die thinks
26542 that this is a declaration. At this point, we just want to force
26543 declaration die. */
26544 save_fn = current_function_decl;
26545 current_function_decl = NULL_TREE;
26546 gen_subprogram_die (decl, context_die);
26547 current_function_decl = save_fn;
26548 break;
26550 case VAR_DECL:
26551 /* Set external flag to force declaration die. Restore it after
26552 gen_decl_die() call. */
26553 saved_external_flag = DECL_EXTERNAL (decl);
26554 DECL_EXTERNAL (decl) = 1;
26555 gen_decl_die (decl, NULL, NULL, context_die);
26556 DECL_EXTERNAL (decl) = saved_external_flag;
26557 break;
26559 case NAMESPACE_DECL:
26560 if (dwarf_version >= 3 || !dwarf_strict)
26561 dwarf2out_decl (decl);
26562 else
26563 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26564 decl_die = comp_unit_die ();
26565 break;
26567 case CONST_DECL:
26568 /* Enumerators shouldn't need force_decl_die. */
26569 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26570 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26571 gen_decl_die (decl, NULL, NULL, context_die);
26572 break;
26574 case TRANSLATION_UNIT_DECL:
26575 decl_die = comp_unit_die ();
26576 break;
26578 default:
26579 gcc_unreachable ();
26582 /* We should be able to find the DIE now. */
26583 if (!decl_die)
26584 decl_die = lookup_decl_die (decl);
26585 gcc_assert (decl_die);
26588 return decl_die;
26591 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26592 always returned. */
26594 static dw_die_ref
26595 force_type_die (tree type)
26597 dw_die_ref type_die;
26599 type_die = lookup_type_die (type);
26600 if (!type_die)
26602 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26604 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26605 false, context_die);
26606 gcc_assert (type_die);
26608 return type_die;
26611 /* Force out any required namespaces to be able to output DECL,
26612 and return the new context_die for it, if it's changed. */
26614 static dw_die_ref
26615 setup_namespace_context (tree thing, dw_die_ref context_die)
26617 tree context = (DECL_P (thing)
26618 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26619 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26620 /* Force out the namespace. */
26621 context_die = force_decl_die (context);
26623 return context_die;
26626 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26627 type) within its namespace, if appropriate.
26629 For compatibility with older debuggers, namespace DIEs only contain
26630 declarations; all definitions are emitted at CU scope, with
26631 DW_AT_specification pointing to the declaration (like with class
26632 members). */
26634 static dw_die_ref
26635 declare_in_namespace (tree thing, dw_die_ref context_die)
26637 dw_die_ref ns_context;
26639 if (debug_info_level <= DINFO_LEVEL_TERSE)
26640 return context_die;
26642 /* External declarations in the local scope only need to be emitted
26643 once, not once in the namespace and once in the scope.
26645 This avoids declaring the `extern' below in the
26646 namespace DIE as well as in the innermost scope:
26648 namespace S
26650 int i=5;
26651 int foo()
26653 int i=8;
26654 extern int i;
26655 return i;
26659 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26660 return context_die;
26662 /* If this decl is from an inlined function, then don't try to emit it in its
26663 namespace, as we will get confused. It would have already been emitted
26664 when the abstract instance of the inline function was emitted anyways. */
26665 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26666 return context_die;
26668 ns_context = setup_namespace_context (thing, context_die);
26670 if (ns_context != context_die)
26672 if (is_fortran () || is_dlang ())
26673 return ns_context;
26674 if (DECL_P (thing))
26675 gen_decl_die (thing, NULL, NULL, ns_context);
26676 else
26677 gen_type_die (thing, ns_context);
26679 return context_die;
26682 /* Generate a DIE for a namespace or namespace alias. */
26684 static void
26685 gen_namespace_die (tree decl, dw_die_ref context_die)
26687 dw_die_ref namespace_die;
26689 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26690 they are an alias of. */
26691 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26693 /* Output a real namespace or module. */
26694 context_die = setup_namespace_context (decl, comp_unit_die ());
26695 namespace_die = new_die (is_fortran () || is_dlang ()
26696 ? DW_TAG_module : DW_TAG_namespace,
26697 context_die, decl);
26698 /* For Fortran modules defined in different CU don't add src coords. */
26699 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26701 const char *name = dwarf2_name (decl, 0);
26702 if (name)
26703 add_name_attribute (namespace_die, name);
26705 else
26706 add_name_and_src_coords_attributes (namespace_die, decl);
26707 if (DECL_EXTERNAL (decl))
26708 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26709 equate_decl_number_to_die (decl, namespace_die);
26711 else
26713 /* Output a namespace alias. */
26715 /* Force out the namespace we are an alias of, if necessary. */
26716 dw_die_ref origin_die
26717 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26719 if (DECL_FILE_SCOPE_P (decl)
26720 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26721 context_die = setup_namespace_context (decl, comp_unit_die ());
26722 /* Now create the namespace alias DIE. */
26723 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26724 add_name_and_src_coords_attributes (namespace_die, decl);
26725 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26726 equate_decl_number_to_die (decl, namespace_die);
26728 if ((dwarf_version >= 5 || !dwarf_strict)
26729 && lang_hooks.decls.decl_dwarf_attribute (decl,
26730 DW_AT_export_symbols) == 1)
26731 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26733 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26734 if (want_pubnames ())
26735 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26738 /* Generate Dwarf debug information for a decl described by DECL.
26739 The return value is currently only meaningful for PARM_DECLs,
26740 for all other decls it returns NULL.
26742 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26743 It can be NULL otherwise. */
26745 static dw_die_ref
26746 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26747 dw_die_ref context_die)
26749 tree decl_or_origin = decl ? decl : origin;
26750 tree class_origin = NULL, ultimate_origin;
26752 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26753 return NULL;
26755 switch (TREE_CODE (decl_or_origin))
26757 case ERROR_MARK:
26758 break;
26760 case CONST_DECL:
26761 if (!is_fortran () && !is_ada () && !is_dlang ())
26763 /* The individual enumerators of an enum type get output when we output
26764 the Dwarf representation of the relevant enum type itself. */
26765 break;
26768 /* Emit its type. */
26769 gen_type_die (TREE_TYPE (decl), context_die);
26771 /* And its containing namespace. */
26772 context_die = declare_in_namespace (decl, context_die);
26774 gen_const_die (decl, context_die);
26775 break;
26777 case FUNCTION_DECL:
26778 #if 0
26779 /* FIXME */
26780 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26781 on local redeclarations of global functions. That seems broken. */
26782 if (current_function_decl != decl)
26783 /* This is only a declaration. */;
26784 #endif
26786 /* We should have abstract copies already and should not generate
26787 stray type DIEs in late LTO dumping. */
26788 if (! early_dwarf)
26791 /* If we're emitting a clone, emit info for the abstract instance. */
26792 else if (origin || DECL_ORIGIN (decl) != decl)
26793 dwarf2out_abstract_function (origin
26794 ? DECL_ORIGIN (origin)
26795 : DECL_ABSTRACT_ORIGIN (decl));
26797 /* If we're emitting a possibly inlined function emit it as
26798 abstract instance. */
26799 else if (cgraph_function_possibly_inlined_p (decl)
26800 && ! DECL_ABSTRACT_P (decl)
26801 && ! class_or_namespace_scope_p (context_die)
26802 /* dwarf2out_abstract_function won't emit a die if this is just
26803 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26804 that case, because that works only if we have a die. */
26805 && DECL_INITIAL (decl) != NULL_TREE)
26806 dwarf2out_abstract_function (decl);
26808 /* Otherwise we're emitting the primary DIE for this decl. */
26809 else if (debug_info_level > DINFO_LEVEL_TERSE)
26811 /* Before we describe the FUNCTION_DECL itself, make sure that we
26812 have its containing type. */
26813 if (!origin)
26814 origin = decl_class_context (decl);
26815 if (origin != NULL_TREE)
26816 gen_type_die (origin, context_die);
26818 /* And its return type. */
26819 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26821 /* And its virtual context. */
26822 if (DECL_VINDEX (decl) != NULL_TREE)
26823 gen_type_die (DECL_CONTEXT (decl), context_die);
26825 /* Make sure we have a member DIE for decl. */
26826 if (origin != NULL_TREE)
26827 gen_type_die_for_member (origin, decl, context_die);
26829 /* And its containing namespace. */
26830 context_die = declare_in_namespace (decl, context_die);
26833 /* Now output a DIE to represent the function itself. */
26834 if (decl)
26835 gen_subprogram_die (decl, context_die);
26836 break;
26838 case TYPE_DECL:
26839 /* If we are in terse mode, don't generate any DIEs to represent any
26840 actual typedefs. */
26841 if (debug_info_level <= DINFO_LEVEL_TERSE)
26842 break;
26844 /* In the special case of a TYPE_DECL node representing the declaration
26845 of some type tag, if the given TYPE_DECL is marked as having been
26846 instantiated from some other (original) TYPE_DECL node (e.g. one which
26847 was generated within the original definition of an inline function) we
26848 used to generate a special (abbreviated) DW_TAG_structure_type,
26849 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26850 should be actually referencing those DIEs, as variable DIEs with that
26851 type would be emitted already in the abstract origin, so it was always
26852 removed during unused type prunning. Don't add anything in this
26853 case. */
26854 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26855 break;
26857 if (is_redundant_typedef (decl))
26858 gen_type_die (TREE_TYPE (decl), context_die);
26859 else
26860 /* Output a DIE to represent the typedef itself. */
26861 gen_typedef_die (decl, context_die);
26862 break;
26864 case LABEL_DECL:
26865 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26866 gen_label_die (decl, context_die);
26867 break;
26869 case VAR_DECL:
26870 case RESULT_DECL:
26871 /* If we are in terse mode, don't generate any DIEs to represent any
26872 variable declarations or definitions unless it is external. */
26873 if (debug_info_level < DINFO_LEVEL_TERSE
26874 || (debug_info_level == DINFO_LEVEL_TERSE
26875 && !TREE_PUBLIC (decl_or_origin)))
26876 break;
26878 if (debug_info_level > DINFO_LEVEL_TERSE)
26880 /* Avoid generating stray type DIEs during late dwarf dumping.
26881 All types have been dumped early. */
26882 if (early_dwarf
26883 /* ??? But in LTRANS we cannot annotate early created variably
26884 modified type DIEs without copying them and adjusting all
26885 references to them. Dump them again as happens for inlining
26886 which copies both the decl and the types. */
26887 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26888 in VLA bound information for example. */
26889 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26890 current_function_decl)))
26892 /* Output any DIEs that are needed to specify the type of this data
26893 object. */
26894 if (decl_by_reference_p (decl_or_origin))
26895 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26896 else
26897 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26900 if (early_dwarf)
26902 /* And its containing type. */
26903 class_origin = decl_class_context (decl_or_origin);
26904 if (class_origin != NULL_TREE)
26905 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26907 /* And its containing namespace. */
26908 context_die = declare_in_namespace (decl_or_origin, context_die);
26912 /* Now output the DIE to represent the data object itself. This gets
26913 complicated because of the possibility that the VAR_DECL really
26914 represents an inlined instance of a formal parameter for an inline
26915 function. */
26916 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26917 if (ultimate_origin != NULL_TREE
26918 && TREE_CODE (ultimate_origin) == PARM_DECL)
26919 gen_formal_parameter_die (decl, origin,
26920 true /* Emit name attribute. */,
26921 context_die);
26922 else
26923 gen_variable_die (decl, origin, context_die);
26924 break;
26926 case FIELD_DECL:
26927 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26928 /* Ignore the nameless fields that are used to skip bits but handle C++
26929 anonymous unions and structs. */
26930 if (DECL_NAME (decl) != NULL_TREE
26931 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26932 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26934 gen_type_die (member_declared_type (decl), context_die);
26935 gen_field_die (decl, ctx, context_die);
26937 break;
26939 case PARM_DECL:
26940 /* Avoid generating stray type DIEs during late dwarf dumping.
26941 All types have been dumped early. */
26942 if (early_dwarf
26943 /* ??? But in LTRANS we cannot annotate early created variably
26944 modified type DIEs without copying them and adjusting all
26945 references to them. Dump them again as happens for inlining
26946 which copies both the decl and the types. */
26947 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26948 in VLA bound information for example. */
26949 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26950 current_function_decl)))
26952 if (DECL_BY_REFERENCE (decl_or_origin))
26953 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26954 else
26955 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26957 return gen_formal_parameter_die (decl, origin,
26958 true /* Emit name attribute. */,
26959 context_die);
26961 case NAMESPACE_DECL:
26962 if (dwarf_version >= 3 || !dwarf_strict)
26963 gen_namespace_die (decl, context_die);
26964 break;
26966 case IMPORTED_DECL:
26967 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26968 DECL_CONTEXT (decl), context_die);
26969 break;
26971 case NAMELIST_DECL:
26972 gen_namelist_decl (DECL_NAME (decl), context_die,
26973 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26974 break;
26976 default:
26977 /* Probably some frontend-internal decl. Assume we don't care. */
26978 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26979 break;
26982 return NULL;
26985 /* Output initial debug information for global DECL. Called at the
26986 end of the parsing process.
26988 This is the initial debug generation process. As such, the DIEs
26989 generated may be incomplete. A later debug generation pass
26990 (dwarf2out_late_global_decl) will augment the information generated
26991 in this pass (e.g., with complete location info). */
26993 static void
26994 dwarf2out_early_global_decl (tree decl)
26996 set_early_dwarf s;
26998 /* gen_decl_die() will set DECL_ABSTRACT because
26999 cgraph_function_possibly_inlined_p() returns true. This is in
27000 turn will cause DW_AT_inline attributes to be set.
27002 This happens because at early dwarf generation, there is no
27003 cgraph information, causing cgraph_function_possibly_inlined_p()
27004 to return true. Trick cgraph_function_possibly_inlined_p()
27005 while we generate dwarf early. */
27006 bool save = symtab->global_info_ready;
27007 symtab->global_info_ready = true;
27009 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27010 other DECLs and they can point to template types or other things
27011 that dwarf2out can't handle when done via dwarf2out_decl. */
27012 if (TREE_CODE (decl) != TYPE_DECL
27013 && TREE_CODE (decl) != PARM_DECL)
27015 if (TREE_CODE (decl) == FUNCTION_DECL)
27017 tree save_fndecl = current_function_decl;
27019 /* For nested functions, make sure we have DIEs for the parents first
27020 so that all nested DIEs are generated at the proper scope in the
27021 first shot. */
27022 tree context = decl_function_context (decl);
27023 if (context != NULL)
27025 dw_die_ref context_die = lookup_decl_die (context);
27026 current_function_decl = context;
27028 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27029 enough so that it lands in its own context. This avoids type
27030 pruning issues later on. */
27031 if (context_die == NULL || is_declaration_die (context_die))
27032 dwarf2out_early_global_decl (context);
27035 /* Emit an abstract origin of a function first. This happens
27036 with C++ constructor clones for example and makes
27037 dwarf2out_abstract_function happy which requires the early
27038 DIE of the abstract instance to be present. */
27039 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27040 dw_die_ref origin_die;
27041 if (origin != NULL
27042 /* Do not emit the DIE multiple times but make sure to
27043 process it fully here in case we just saw a declaration. */
27044 && ((origin_die = lookup_decl_die (origin)) == NULL
27045 || is_declaration_die (origin_die)))
27047 current_function_decl = origin;
27048 dwarf2out_decl (origin);
27051 /* Emit the DIE for decl but avoid doing that multiple times. */
27052 dw_die_ref old_die;
27053 if ((old_die = lookup_decl_die (decl)) == NULL
27054 || is_declaration_die (old_die))
27056 current_function_decl = decl;
27057 dwarf2out_decl (decl);
27060 current_function_decl = save_fndecl;
27062 else
27063 dwarf2out_decl (decl);
27065 symtab->global_info_ready = save;
27068 /* Return whether EXPR is an expression with the following pattern:
27069 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27071 static bool
27072 is_trivial_indirect_ref (tree expr)
27074 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27075 return false;
27077 tree nop = TREE_OPERAND (expr, 0);
27078 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27079 return false;
27081 tree int_cst = TREE_OPERAND (nop, 0);
27082 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27085 /* Output debug information for global decl DECL. Called from
27086 toplev.c after compilation proper has finished. */
27088 static void
27089 dwarf2out_late_global_decl (tree decl)
27091 /* Fill-in any location information we were unable to determine
27092 on the first pass. */
27093 if (VAR_P (decl))
27095 dw_die_ref die = lookup_decl_die (decl);
27097 /* We may have to generate full debug late for LTO in case debug
27098 was not enabled at compile-time or the target doesn't support
27099 the LTO early debug scheme. */
27100 if (! die && in_lto_p)
27101 dwarf2out_decl (decl);
27102 else if (die)
27104 /* We get called via the symtab code invoking late_global_decl
27105 for symbols that are optimized out.
27107 Do not add locations for those, except if they have a
27108 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27109 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27110 INDIRECT_REF expression, as this could generate relocations to
27111 text symbols in LTO object files, which is invalid. */
27112 varpool_node *node = varpool_node::get (decl);
27113 if ((! node || ! node->definition)
27114 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27115 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27116 tree_add_const_value_attribute_for_decl (die, decl);
27117 else
27118 add_location_or_const_value_attribute (die, decl, false);
27123 /* Output debug information for type decl DECL. Called from toplev.c
27124 and from language front ends (to record built-in types). */
27125 static void
27126 dwarf2out_type_decl (tree decl, int local)
27128 if (!local)
27130 set_early_dwarf s;
27131 dwarf2out_decl (decl);
27135 /* Output debug information for imported module or decl DECL.
27136 NAME is non-NULL name in the lexical block if the decl has been renamed.
27137 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27138 that DECL belongs to.
27139 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27140 static void
27141 dwarf2out_imported_module_or_decl_1 (tree decl,
27142 tree name,
27143 tree lexical_block,
27144 dw_die_ref lexical_block_die)
27146 expanded_location xloc;
27147 dw_die_ref imported_die = NULL;
27148 dw_die_ref at_import_die;
27150 if (TREE_CODE (decl) == IMPORTED_DECL)
27152 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27153 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27154 gcc_assert (decl);
27156 else
27157 xloc = expand_location (input_location);
27159 if (TREE_CODE (decl) == TYPE_DECL)
27161 at_import_die = force_type_die (TREE_TYPE (decl));
27162 /* For namespace N { typedef void T; } using N::T; base_type_die
27163 returns NULL, but DW_TAG_imported_declaration requires
27164 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27165 if (!at_import_die)
27167 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27168 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27169 at_import_die = lookup_type_die (TREE_TYPE (decl));
27170 gcc_assert (at_import_die);
27173 else
27175 at_import_die = lookup_decl_die (decl);
27176 if (!at_import_die)
27178 /* If we're trying to avoid duplicate debug info, we may not have
27179 emitted the member decl for this field. Emit it now. */
27180 if (TREE_CODE (decl) == FIELD_DECL)
27182 tree type = DECL_CONTEXT (decl);
27184 if (TYPE_CONTEXT (type)
27185 && TYPE_P (TYPE_CONTEXT (type))
27186 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27187 DINFO_USAGE_DIR_USE))
27188 return;
27189 gen_type_die_for_member (type, decl,
27190 get_context_die (TYPE_CONTEXT (type)));
27192 if (TREE_CODE (decl) == CONST_DECL)
27194 /* Individual enumerators of an enum type do not get output here
27195 (see gen_decl_die), so we cannot call force_decl_die. */
27196 if (!is_fortran () && !is_ada () && !is_dlang ())
27197 return;
27199 if (TREE_CODE (decl) == NAMELIST_DECL)
27200 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27201 get_context_die (DECL_CONTEXT (decl)),
27202 NULL_TREE);
27203 else
27204 at_import_die = force_decl_die (decl);
27208 if (TREE_CODE (decl) == NAMESPACE_DECL)
27210 if (dwarf_version >= 3 || !dwarf_strict)
27211 imported_die = new_die (DW_TAG_imported_module,
27212 lexical_block_die,
27213 lexical_block);
27214 else
27215 return;
27217 else
27218 imported_die = new_die (DW_TAG_imported_declaration,
27219 lexical_block_die,
27220 lexical_block);
27222 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27223 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27224 if (debug_column_info && xloc.column)
27225 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27226 if (name)
27227 add_AT_string (imported_die, DW_AT_name,
27228 IDENTIFIER_POINTER (name));
27229 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27232 /* Output debug information for imported module or decl DECL.
27233 NAME is non-NULL name in context if the decl has been renamed.
27234 CHILD is true if decl is one of the renamed decls as part of
27235 importing whole module.
27236 IMPLICIT is set if this hook is called for an implicit import
27237 such as inline namespace. */
27239 static void
27240 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27241 bool child, bool implicit)
27243 /* dw_die_ref at_import_die; */
27244 dw_die_ref scope_die;
27246 if (debug_info_level <= DINFO_LEVEL_TERSE)
27247 return;
27249 gcc_assert (decl);
27251 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27252 should be enough, for DWARF4 and older even if we emit as extension
27253 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27254 for the benefit of consumers unaware of DW_AT_export_symbols. */
27255 if (implicit
27256 && dwarf_version >= 5
27257 && lang_hooks.decls.decl_dwarf_attribute (decl,
27258 DW_AT_export_symbols) == 1)
27259 return;
27261 set_early_dwarf s;
27263 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27264 We need decl DIE for reference and scope die. First, get DIE for the decl
27265 itself. */
27267 /* Get the scope die for decl context. Use comp_unit_die for global module
27268 or decl. If die is not found for non globals, force new die. */
27269 if (context
27270 && TYPE_P (context)
27271 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27272 return;
27274 scope_die = get_context_die (context);
27276 if (child)
27278 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27279 there is nothing we can do, here. */
27280 if (dwarf_version < 3 && dwarf_strict)
27281 return;
27283 gcc_assert (scope_die->die_child);
27284 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27285 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27286 scope_die = scope_die->die_child;
27289 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27290 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27293 /* Output debug information for namelists. */
27295 static dw_die_ref
27296 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27298 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27299 tree value;
27300 unsigned i;
27302 if (debug_info_level <= DINFO_LEVEL_TERSE)
27303 return NULL;
27305 gcc_assert (scope_die != NULL);
27306 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27307 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27309 /* If there are no item_decls, we have a nondefining namelist, e.g.
27310 with USE association; hence, set DW_AT_declaration. */
27311 if (item_decls == NULL_TREE)
27313 add_AT_flag (nml_die, DW_AT_declaration, 1);
27314 return nml_die;
27317 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27319 nml_item_ref_die = lookup_decl_die (value);
27320 if (!nml_item_ref_die)
27321 nml_item_ref_die = force_decl_die (value);
27323 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27324 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27326 return nml_die;
27330 /* Write the debugging output for DECL and return the DIE. */
27332 static void
27333 dwarf2out_decl (tree decl)
27335 dw_die_ref context_die = comp_unit_die ();
27337 switch (TREE_CODE (decl))
27339 case ERROR_MARK:
27340 return;
27342 case FUNCTION_DECL:
27343 /* If we're a nested function, initially use a parent of NULL; if we're
27344 a plain function, this will be fixed up in decls_for_scope. If
27345 we're a method, it will be ignored, since we already have a DIE.
27346 Avoid doing this late though since clones of class methods may
27347 otherwise end up in limbo and create type DIEs late. */
27348 if (early_dwarf
27349 && decl_function_context (decl)
27350 /* But if we're in terse mode, we don't care about scope. */
27351 && debug_info_level > DINFO_LEVEL_TERSE)
27352 context_die = NULL;
27353 break;
27355 case VAR_DECL:
27356 /* For local statics lookup proper context die. */
27357 if (local_function_static (decl))
27358 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27360 /* If we are in terse mode, don't generate any DIEs to represent any
27361 variable declarations or definitions unless it is external. */
27362 if (debug_info_level < DINFO_LEVEL_TERSE
27363 || (debug_info_level == DINFO_LEVEL_TERSE
27364 && !TREE_PUBLIC (decl)))
27365 return;
27366 break;
27368 case CONST_DECL:
27369 if (debug_info_level <= DINFO_LEVEL_TERSE)
27370 return;
27371 if (!is_fortran () && !is_ada () && !is_dlang ())
27372 return;
27373 if (TREE_STATIC (decl) && decl_function_context (decl))
27374 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27375 break;
27377 case NAMESPACE_DECL:
27378 case IMPORTED_DECL:
27379 if (debug_info_level <= DINFO_LEVEL_TERSE)
27380 return;
27381 if (lookup_decl_die (decl) != NULL)
27382 return;
27383 break;
27385 case TYPE_DECL:
27386 /* Don't emit stubs for types unless they are needed by other DIEs. */
27387 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27388 return;
27390 /* Don't bother trying to generate any DIEs to represent any of the
27391 normal built-in types for the language we are compiling. */
27392 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27393 return;
27395 /* If we are in terse mode, don't generate any DIEs for types. */
27396 if (debug_info_level <= DINFO_LEVEL_TERSE)
27397 return;
27399 /* If we're a function-scope tag, initially use a parent of NULL;
27400 this will be fixed up in decls_for_scope. */
27401 if (decl_function_context (decl))
27402 context_die = NULL;
27404 break;
27406 case NAMELIST_DECL:
27407 break;
27409 default:
27410 return;
27413 gen_decl_die (decl, NULL, NULL, context_die);
27415 if (flag_checking)
27417 dw_die_ref die = lookup_decl_die (decl);
27418 if (die)
27419 check_die (die);
27423 /* Write the debugging output for DECL. */
27425 static void
27426 dwarf2out_function_decl (tree decl)
27428 dwarf2out_decl (decl);
27429 call_arg_locations = NULL;
27430 call_arg_loc_last = NULL;
27431 call_site_count = -1;
27432 tail_call_site_count = -1;
27433 decl_loc_table->empty ();
27434 cached_dw_loc_list_table->empty ();
27437 /* Output a marker (i.e. a label) for the beginning of the generated code for
27438 a lexical block. */
27440 static void
27441 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27442 unsigned int blocknum)
27444 switch_to_section (current_function_section ());
27445 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27448 /* Output a marker (i.e. a label) for the end of the generated code for a
27449 lexical block. */
27451 static void
27452 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27454 switch_to_section (current_function_section ());
27455 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27458 /* Returns nonzero if it is appropriate not to emit any debugging
27459 information for BLOCK, because it doesn't contain any instructions.
27461 Don't allow this for blocks with nested functions or local classes
27462 as we would end up with orphans, and in the presence of scheduling
27463 we may end up calling them anyway. */
27465 static bool
27466 dwarf2out_ignore_block (const_tree block)
27468 tree decl;
27469 unsigned int i;
27471 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27472 if (TREE_CODE (decl) == FUNCTION_DECL
27473 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27474 return 0;
27475 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27477 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27478 if (TREE_CODE (decl) == FUNCTION_DECL
27479 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27480 return 0;
27483 return 1;
27486 /* Hash table routines for file_hash. */
27488 bool
27489 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27491 return filename_cmp (p1->key, p2) == 0;
27494 hashval_t
27495 dwarf_file_hasher::hash (dwarf_file_data *p)
27497 return htab_hash_string (p->key);
27500 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27501 dwarf2out.c) and return its "index". The index of each (known) filename is
27502 just a unique number which is associated with only that one filename. We
27503 need such numbers for the sake of generating labels (in the .debug_sfnames
27504 section) and references to those files numbers (in the .debug_srcinfo
27505 and .debug_macinfo sections). If the filename given as an argument is not
27506 found in our current list, add it to the list and assign it the next
27507 available unique index number. */
27509 static struct dwarf_file_data *
27510 lookup_filename (const char *file_name)
27512 struct dwarf_file_data * created;
27514 if (!file_name)
27515 return NULL;
27517 if (!file_name[0])
27518 file_name = "<stdin>";
27520 dwarf_file_data **slot
27521 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27522 INSERT);
27523 if (*slot)
27524 return *slot;
27526 created = ggc_alloc<dwarf_file_data> ();
27527 created->key = file_name;
27528 created->filename = remap_debug_filename (file_name);
27529 created->emitted_number = 0;
27530 *slot = created;
27531 return created;
27534 /* If the assembler will construct the file table, then translate the compiler
27535 internal file table number into the assembler file table number, and emit
27536 a .file directive if we haven't already emitted one yet. The file table
27537 numbers are different because we prune debug info for unused variables and
27538 types, which may include filenames. */
27540 static int
27541 maybe_emit_file (struct dwarf_file_data * fd)
27543 if (! fd->emitted_number)
27545 if (last_emitted_file)
27546 fd->emitted_number = last_emitted_file->emitted_number + 1;
27547 else
27548 fd->emitted_number = 1;
27549 last_emitted_file = fd;
27551 if (output_asm_line_debug_info ())
27553 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27554 output_quoted_string (asm_out_file, fd->filename);
27555 fputc ('\n', asm_out_file);
27559 return fd->emitted_number;
27562 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27563 That generation should happen after function debug info has been
27564 generated. The value of the attribute is the constant value of ARG. */
27566 static void
27567 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27569 die_arg_entry entry;
27571 if (!die || !arg)
27572 return;
27574 gcc_assert (early_dwarf);
27576 if (!tmpl_value_parm_die_table)
27577 vec_alloc (tmpl_value_parm_die_table, 32);
27579 entry.die = die;
27580 entry.arg = arg;
27581 vec_safe_push (tmpl_value_parm_die_table, entry);
27584 /* Return TRUE if T is an instance of generic type, FALSE
27585 otherwise. */
27587 static bool
27588 generic_type_p (tree t)
27590 if (t == NULL_TREE || !TYPE_P (t))
27591 return false;
27592 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27595 /* Schedule the generation of the generic parameter dies for the
27596 instance of generic type T. The proper generation itself is later
27597 done by gen_scheduled_generic_parms_dies. */
27599 static void
27600 schedule_generic_params_dies_gen (tree t)
27602 if (!generic_type_p (t))
27603 return;
27605 gcc_assert (early_dwarf);
27607 if (!generic_type_instances)
27608 vec_alloc (generic_type_instances, 256);
27610 vec_safe_push (generic_type_instances, t);
27613 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27614 by append_entry_to_tmpl_value_parm_die_table. This function must
27615 be called after function DIEs have been generated. */
27617 static void
27618 gen_remaining_tmpl_value_param_die_attribute (void)
27620 if (tmpl_value_parm_die_table)
27622 unsigned i, j;
27623 die_arg_entry *e;
27625 /* We do this in two phases - first get the cases we can
27626 handle during early-finish, preserving those we cannot
27627 (containing symbolic constants where we don't yet know
27628 whether we are going to output the referenced symbols).
27629 For those we try again at late-finish. */
27630 j = 0;
27631 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27633 if (!e->die->removed
27634 && !tree_add_const_value_attribute (e->die, e->arg))
27636 dw_loc_descr_ref loc = NULL;
27637 if (! early_dwarf
27638 && (dwarf_version >= 5 || !dwarf_strict))
27639 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27640 if (loc)
27641 add_AT_loc (e->die, DW_AT_location, loc);
27642 else
27643 (*tmpl_value_parm_die_table)[j++] = *e;
27646 tmpl_value_parm_die_table->truncate (j);
27650 /* Generate generic parameters DIEs for instances of generic types
27651 that have been previously scheduled by
27652 schedule_generic_params_dies_gen. This function must be called
27653 after all the types of the CU have been laid out. */
27655 static void
27656 gen_scheduled_generic_parms_dies (void)
27658 unsigned i;
27659 tree t;
27661 if (!generic_type_instances)
27662 return;
27664 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27665 if (COMPLETE_TYPE_P (t))
27666 gen_generic_params_dies (t);
27668 generic_type_instances = NULL;
27672 /* Replace DW_AT_name for the decl with name. */
27674 static void
27675 dwarf2out_set_name (tree decl, tree name)
27677 dw_die_ref die;
27678 dw_attr_node *attr;
27679 const char *dname;
27681 die = TYPE_SYMTAB_DIE (decl);
27682 if (!die)
27683 return;
27685 dname = dwarf2_name (name, 0);
27686 if (!dname)
27687 return;
27689 attr = get_AT (die, DW_AT_name);
27690 if (attr)
27692 struct indirect_string_node *node;
27694 node = find_AT_string (dname);
27695 /* replace the string. */
27696 attr->dw_attr_val.v.val_str = node;
27699 else
27700 add_name_attribute (die, dname);
27703 /* True if before or during processing of the first function being emitted. */
27704 static bool in_first_function_p = true;
27705 /* True if loc_note during dwarf2out_var_location call might still be
27706 before first real instruction at address equal to .Ltext0. */
27707 static bool maybe_at_text_label_p = true;
27708 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27709 static unsigned int first_loclabel_num_not_at_text_label;
27711 /* Look ahead for a real insn. */
27713 static rtx_insn *
27714 dwarf2out_next_real_insn (rtx_insn *loc_note)
27716 rtx_insn *next_real = NEXT_INSN (loc_note);
27718 while (next_real)
27719 if (INSN_P (next_real))
27720 break;
27721 else
27722 next_real = NEXT_INSN (next_real);
27724 return next_real;
27727 /* Called by the final INSN scan whenever we see a var location. We
27728 use it to drop labels in the right places, and throw the location in
27729 our lookup table. */
27731 static void
27732 dwarf2out_var_location (rtx_insn *loc_note)
27734 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27735 struct var_loc_node *newloc;
27736 rtx_insn *next_real;
27737 rtx_insn *call_insn = NULL;
27738 static const char *last_label;
27739 static const char *last_postcall_label;
27740 static bool last_in_cold_section_p;
27741 static rtx_insn *expected_next_loc_note;
27742 tree decl;
27743 bool var_loc_p;
27744 var_loc_view view = 0;
27746 if (!NOTE_P (loc_note))
27748 if (CALL_P (loc_note))
27750 maybe_reset_location_view (loc_note, cur_line_info_table);
27751 call_site_count++;
27752 if (SIBLING_CALL_P (loc_note))
27753 tail_call_site_count++;
27754 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27756 call_insn = loc_note;
27757 loc_note = NULL;
27758 var_loc_p = false;
27760 next_real = dwarf2out_next_real_insn (call_insn);
27761 cached_next_real_insn = NULL;
27762 goto create_label;
27764 if (optimize == 0 && !flag_var_tracking)
27766 /* When the var-tracking pass is not running, there is no note
27767 for indirect calls whose target is compile-time known. In this
27768 case, process such calls specifically so that we generate call
27769 sites for them anyway. */
27770 rtx x = PATTERN (loc_note);
27771 if (GET_CODE (x) == PARALLEL)
27772 x = XVECEXP (x, 0, 0);
27773 if (GET_CODE (x) == SET)
27774 x = SET_SRC (x);
27775 if (GET_CODE (x) == CALL)
27776 x = XEXP (x, 0);
27777 if (!MEM_P (x)
27778 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27779 || !SYMBOL_REF_DECL (XEXP (x, 0))
27780 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27781 != FUNCTION_DECL))
27783 call_insn = loc_note;
27784 loc_note = NULL;
27785 var_loc_p = false;
27787 next_real = dwarf2out_next_real_insn (call_insn);
27788 cached_next_real_insn = NULL;
27789 goto create_label;
27793 else if (!debug_variable_location_views)
27794 gcc_unreachable ();
27795 else
27796 maybe_reset_location_view (loc_note, cur_line_info_table);
27798 return;
27801 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27802 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27803 return;
27805 /* Optimize processing a large consecutive sequence of location
27806 notes so we don't spend too much time in next_real_insn. If the
27807 next insn is another location note, remember the next_real_insn
27808 calculation for next time. */
27809 next_real = cached_next_real_insn;
27810 if (next_real)
27812 if (expected_next_loc_note != loc_note)
27813 next_real = NULL;
27816 if (! next_real)
27817 next_real = dwarf2out_next_real_insn (loc_note);
27819 if (next_real)
27821 rtx_insn *next_note = NEXT_INSN (loc_note);
27822 while (next_note != next_real)
27824 if (! next_note->deleted ()
27825 && NOTE_P (next_note)
27826 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27827 break;
27828 next_note = NEXT_INSN (next_note);
27831 if (next_note == next_real)
27832 cached_next_real_insn = NULL;
27833 else
27835 expected_next_loc_note = next_note;
27836 cached_next_real_insn = next_real;
27839 else
27840 cached_next_real_insn = NULL;
27842 /* If there are no instructions which would be affected by this note,
27843 don't do anything. */
27844 if (var_loc_p
27845 && next_real == NULL_RTX
27846 && !NOTE_DURING_CALL_P (loc_note))
27847 return;
27849 create_label:
27851 if (next_real == NULL_RTX)
27852 next_real = get_last_insn ();
27854 /* If there were any real insns between note we processed last time
27855 and this note (or if it is the first note), clear
27856 last_{,postcall_}label so that they are not reused this time. */
27857 if (last_var_location_insn == NULL_RTX
27858 || last_var_location_insn != next_real
27859 || last_in_cold_section_p != in_cold_section_p)
27861 last_label = NULL;
27862 last_postcall_label = NULL;
27865 if (var_loc_p)
27867 const char *label
27868 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27869 view = cur_line_info_table->view;
27870 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27871 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27872 if (newloc == NULL)
27873 return;
27875 else
27877 decl = NULL_TREE;
27878 newloc = NULL;
27881 /* If there were no real insns between note we processed last time
27882 and this note, use the label we emitted last time. Otherwise
27883 create a new label and emit it. */
27884 if (last_label == NULL)
27886 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27887 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27888 loclabel_num++;
27889 last_label = ggc_strdup (loclabel);
27890 /* See if loclabel might be equal to .Ltext0. If yes,
27891 bump first_loclabel_num_not_at_text_label. */
27892 if (!have_multiple_function_sections
27893 && in_first_function_p
27894 && maybe_at_text_label_p)
27896 static rtx_insn *last_start;
27897 rtx_insn *insn;
27898 for (insn = loc_note; insn; insn = previous_insn (insn))
27899 if (insn == last_start)
27900 break;
27901 else if (!NONDEBUG_INSN_P (insn))
27902 continue;
27903 else
27905 rtx body = PATTERN (insn);
27906 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27907 continue;
27908 /* Inline asm could occupy zero bytes. */
27909 else if (GET_CODE (body) == ASM_INPUT
27910 || asm_noperands (body) >= 0)
27911 continue;
27912 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27913 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27914 continue;
27915 #endif
27916 else
27918 /* Assume insn has non-zero length. */
27919 maybe_at_text_label_p = false;
27920 break;
27923 if (maybe_at_text_label_p)
27925 last_start = loc_note;
27926 first_loclabel_num_not_at_text_label = loclabel_num;
27931 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27932 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27934 if (!var_loc_p)
27936 struct call_arg_loc_node *ca_loc
27937 = ggc_cleared_alloc<call_arg_loc_node> ();
27938 rtx_insn *prev = call_insn;
27940 ca_loc->call_arg_loc_note
27941 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27942 ca_loc->next = NULL;
27943 ca_loc->label = last_label;
27944 gcc_assert (prev
27945 && (CALL_P (prev)
27946 || (NONJUMP_INSN_P (prev)
27947 && GET_CODE (PATTERN (prev)) == SEQUENCE
27948 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27949 if (!CALL_P (prev))
27950 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27951 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27953 /* Look for a SYMBOL_REF in the "prev" instruction. */
27954 rtx x = get_call_rtx_from (prev);
27955 if (x)
27957 /* Try to get the call symbol, if any. */
27958 if (MEM_P (XEXP (x, 0)))
27959 x = XEXP (x, 0);
27960 /* First, look for a memory access to a symbol_ref. */
27961 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27962 && SYMBOL_REF_DECL (XEXP (x, 0))
27963 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27964 ca_loc->symbol_ref = XEXP (x, 0);
27965 /* Otherwise, look at a compile-time known user-level function
27966 declaration. */
27967 else if (MEM_P (x)
27968 && MEM_EXPR (x)
27969 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27970 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27973 ca_loc->block = insn_scope (prev);
27974 if (call_arg_locations)
27975 call_arg_loc_last->next = ca_loc;
27976 else
27977 call_arg_locations = ca_loc;
27978 call_arg_loc_last = ca_loc;
27980 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27982 newloc->label = last_label;
27983 newloc->view = view;
27985 else
27987 if (!last_postcall_label)
27989 sprintf (loclabel, "%s-1", last_label);
27990 last_postcall_label = ggc_strdup (loclabel);
27992 newloc->label = last_postcall_label;
27993 /* ??? This view is at last_label, not last_label-1, but we
27994 could only assume view at last_label-1 is zero if we could
27995 assume calls always have length greater than one. This is
27996 probably true in general, though there might be a rare
27997 exception to this rule, e.g. if a call insn is optimized out
27998 by target magic. Then, even the -1 in the label will be
27999 wrong, which might invalidate the range. Anyway, using view,
28000 though technically possibly incorrect, will work as far as
28001 ranges go: since L-1 is in the middle of the call insn,
28002 (L-1).0 and (L-1).V shouldn't make any difference, and having
28003 the loclist entry refer to the .loc entry might be useful, so
28004 leave it like this. */
28005 newloc->view = view;
28008 if (var_loc_p && flag_debug_asm)
28010 const char *name, *sep, *patstr;
28011 if (decl && DECL_NAME (decl))
28012 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28013 else
28014 name = "";
28015 if (NOTE_VAR_LOCATION_LOC (loc_note))
28017 sep = " => ";
28018 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28020 else
28022 sep = " ";
28023 patstr = "RESET";
28025 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28026 name, sep, patstr);
28029 last_var_location_insn = next_real;
28030 last_in_cold_section_p = in_cold_section_p;
28033 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28034 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28035 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28036 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28037 BLOCK_FRAGMENT_ORIGIN links. */
28038 static bool
28039 block_within_block_p (tree block, tree outer, bool bothways)
28041 if (block == outer)
28042 return true;
28044 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28045 for (tree context = BLOCK_SUPERCONTEXT (block);
28046 context != outer;
28047 context = BLOCK_SUPERCONTEXT (context))
28048 if (!context || TREE_CODE (context) != BLOCK)
28049 return false;
28051 if (!bothways)
28052 return true;
28054 /* Now check that each block is actually referenced by its
28055 parent. */
28056 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28057 context = BLOCK_SUPERCONTEXT (context))
28059 if (BLOCK_FRAGMENT_ORIGIN (context))
28061 gcc_assert (!BLOCK_SUBBLOCKS (context));
28062 context = BLOCK_FRAGMENT_ORIGIN (context);
28064 for (tree sub = BLOCK_SUBBLOCKS (context);
28065 sub != block;
28066 sub = BLOCK_CHAIN (sub))
28067 if (!sub)
28068 return false;
28069 if (context == outer)
28070 return true;
28071 else
28072 block = context;
28076 /* Called during final while assembling the marker of the entry point
28077 for an inlined function. */
28079 static void
28080 dwarf2out_inline_entry (tree block)
28082 gcc_assert (debug_inline_points);
28084 /* If we can't represent it, don't bother. */
28085 if (!(dwarf_version >= 3 || !dwarf_strict))
28086 return;
28088 gcc_assert (DECL_P (block_ultimate_origin (block)));
28090 /* Sanity check the block tree. This would catch a case in which
28091 BLOCK got removed from the tree reachable from the outermost
28092 lexical block, but got retained in markers. It would still link
28093 back to its parents, but some ancestor would be missing a link
28094 down the path to the sub BLOCK. If the block got removed, its
28095 BLOCK_NUMBER will not be a usable value. */
28096 if (flag_checking)
28097 gcc_assert (block_within_block_p (block,
28098 DECL_INITIAL (current_function_decl),
28099 true));
28101 gcc_assert (inlined_function_outer_scope_p (block));
28102 gcc_assert (!lookup_block_die (block));
28104 if (BLOCK_FRAGMENT_ORIGIN (block))
28105 block = BLOCK_FRAGMENT_ORIGIN (block);
28106 /* Can the entry point ever not be at the beginning of an
28107 unfragmented lexical block? */
28108 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28109 || (cur_line_info_table
28110 && !ZERO_VIEW_P (cur_line_info_table->view))))
28111 return;
28113 if (!inline_entry_data_table)
28114 inline_entry_data_table
28115 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28118 inline_entry_data **iedp
28119 = inline_entry_data_table->find_slot_with_hash (block,
28120 htab_hash_pointer (block),
28121 INSERT);
28122 if (*iedp)
28123 /* ??? Ideally, we'd record all entry points for the same inlined
28124 function (some may have been duplicated by e.g. unrolling), but
28125 we have no way to represent that ATM. */
28126 return;
28128 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28129 ied->block = block;
28130 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28131 ied->label_num = BLOCK_NUMBER (block);
28132 if (cur_line_info_table)
28133 ied->view = cur_line_info_table->view;
28135 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28136 BLOCK_NUMBER (block));
28139 /* Called from finalize_size_functions for size functions so that their body
28140 can be encoded in the debug info to describe the layout of variable-length
28141 structures. */
28143 static void
28144 dwarf2out_size_function (tree decl)
28146 set_early_dwarf s;
28147 function_to_dwarf_procedure (decl);
28150 /* Note in one location list that text section has changed. */
28153 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28155 var_loc_list *list = *slot;
28156 if (list->first)
28157 list->last_before_switch
28158 = list->last->next ? list->last->next : list->last;
28159 return 1;
28162 /* Note in all location lists that text section has changed. */
28164 static void
28165 var_location_switch_text_section (void)
28167 if (decl_loc_table == NULL)
28168 return;
28170 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28173 /* Create a new line number table. */
28175 static dw_line_info_table *
28176 new_line_info_table (void)
28178 dw_line_info_table *table;
28180 table = ggc_cleared_alloc<dw_line_info_table> ();
28181 table->file_num = 1;
28182 table->line_num = 1;
28183 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28184 FORCE_RESET_NEXT_VIEW (table->view);
28185 table->symviews_since_reset = 0;
28187 return table;
28190 /* Lookup the "current" table into which we emit line info, so
28191 that we don't have to do it for every source line. */
28193 static void
28194 set_cur_line_info_table (section *sec)
28196 dw_line_info_table *table;
28198 if (sec == text_section)
28199 table = text_section_line_info;
28200 else if (sec == cold_text_section)
28202 table = cold_text_section_line_info;
28203 if (!table)
28205 cold_text_section_line_info = table = new_line_info_table ();
28206 table->end_label = cold_end_label;
28209 else
28211 const char *end_label;
28213 if (crtl->has_bb_partition)
28215 if (in_cold_section_p)
28216 end_label = crtl->subsections.cold_section_end_label;
28217 else
28218 end_label = crtl->subsections.hot_section_end_label;
28220 else
28222 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28223 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28224 current_function_funcdef_no);
28225 end_label = ggc_strdup (label);
28228 table = new_line_info_table ();
28229 table->end_label = end_label;
28231 vec_safe_push (separate_line_info, table);
28234 if (output_asm_line_debug_info ())
28235 table->is_stmt = (cur_line_info_table
28236 ? cur_line_info_table->is_stmt
28237 : DWARF_LINE_DEFAULT_IS_STMT_START);
28238 cur_line_info_table = table;
28242 /* We need to reset the locations at the beginning of each
28243 function. We can't do this in the end_function hook, because the
28244 declarations that use the locations won't have been output when
28245 that hook is called. Also compute have_multiple_function_sections here. */
28247 static void
28248 dwarf2out_begin_function (tree fun)
28250 section *sec = function_section (fun);
28252 if (sec != text_section)
28253 have_multiple_function_sections = true;
28255 if (crtl->has_bb_partition && !cold_text_section)
28257 gcc_assert (current_function_decl == fun);
28258 cold_text_section = unlikely_text_section ();
28259 switch_to_section (cold_text_section);
28260 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28261 switch_to_section (sec);
28264 call_site_count = 0;
28265 tail_call_site_count = 0;
28267 set_cur_line_info_table (sec);
28268 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28271 /* Helper function of dwarf2out_end_function, called only after emitting
28272 the very first function into assembly. Check if some .debug_loc range
28273 might end with a .LVL* label that could be equal to .Ltext0.
28274 In that case we must force using absolute addresses in .debug_loc ranges,
28275 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28276 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28277 list terminator.
28278 Set have_multiple_function_sections to true in that case and
28279 terminate htab traversal. */
28282 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28284 var_loc_list *entry = *slot;
28285 struct var_loc_node *node;
28287 node = entry->first;
28288 if (node && node->next && node->next->label)
28290 unsigned int i;
28291 const char *label = node->next->label;
28292 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28294 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28296 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28297 if (strcmp (label, loclabel) == 0)
28299 have_multiple_function_sections = true;
28300 return 0;
28304 return 1;
28307 /* Hook called after emitting a function into assembly.
28308 This does something only for the very first function emitted. */
28310 static void
28311 dwarf2out_end_function (unsigned int)
28313 if (in_first_function_p
28314 && !have_multiple_function_sections
28315 && first_loclabel_num_not_at_text_label
28316 && decl_loc_table)
28317 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28318 in_first_function_p = false;
28319 maybe_at_text_label_p = false;
28322 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28323 front-ends register a translation unit even before dwarf2out_init is
28324 called. */
28325 static tree main_translation_unit = NULL_TREE;
28327 /* Hook called by front-ends after they built their main translation unit.
28328 Associate comp_unit_die to UNIT. */
28330 static void
28331 dwarf2out_register_main_translation_unit (tree unit)
28333 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28334 && main_translation_unit == NULL_TREE);
28335 main_translation_unit = unit;
28336 /* If dwarf2out_init has not been called yet, it will perform the association
28337 itself looking at main_translation_unit. */
28338 if (decl_die_table != NULL)
28339 equate_decl_number_to_die (unit, comp_unit_die ());
28342 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28344 static void
28345 push_dw_line_info_entry (dw_line_info_table *table,
28346 enum dw_line_info_opcode opcode, unsigned int val)
28348 dw_line_info_entry e;
28349 e.opcode = opcode;
28350 e.val = val;
28351 vec_safe_push (table->entries, e);
28354 /* Output a label to mark the beginning of a source code line entry
28355 and record information relating to this source line, in
28356 'line_info_table' for later output of the .debug_line section. */
28357 /* ??? The discriminator parameter ought to be unsigned. */
28359 static void
28360 dwarf2out_source_line (unsigned int line, unsigned int column,
28361 const char *filename,
28362 int discriminator, bool is_stmt)
28364 unsigned int file_num;
28365 dw_line_info_table *table;
28366 static var_loc_view lvugid;
28368 /* 'line_info_table' information gathering is not needed when the debug
28369 info level is set to the lowest value. Also, the current DWARF-based
28370 debug formats do not use this info. */
28371 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28372 return;
28374 table = cur_line_info_table;
28376 if (line == 0)
28378 if (debug_variable_location_views
28379 && output_asm_line_debug_info ()
28380 && table && !RESETTING_VIEW_P (table->view))
28382 /* If we're using the assembler to compute view numbers, we
28383 can't issue a .loc directive for line zero, so we can't
28384 get a view number at this point. We might attempt to
28385 compute it from the previous view, or equate it to a
28386 subsequent view (though it might not be there!), but
28387 since we're omitting the line number entry, we might as
28388 well omit the view number as well. That means pretending
28389 it's a view number zero, which might very well turn out
28390 to be correct. ??? Extend the assembler so that the
28391 compiler could emit e.g. ".locview .LVU#", to output a
28392 view without changing line number information. We'd then
28393 have to count it in symviews_since_reset; when it's omitted,
28394 it doesn't count. */
28395 if (!zero_view_p)
28396 zero_view_p = BITMAP_GGC_ALLOC ();
28397 bitmap_set_bit (zero_view_p, table->view);
28398 if (flag_debug_asm)
28400 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28401 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28402 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28403 ASM_COMMENT_START);
28404 assemble_name (asm_out_file, label);
28405 putc ('\n', asm_out_file);
28407 table->view = ++lvugid;
28409 return;
28412 /* The discriminator column was added in dwarf4. Simplify the below
28413 by simply removing it if we're not supposed to output it. */
28414 if (dwarf_version < 4 && dwarf_strict)
28415 discriminator = 0;
28417 if (!debug_column_info)
28418 column = 0;
28420 file_num = maybe_emit_file (lookup_filename (filename));
28422 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28423 the debugger has used the second (possibly duplicate) line number
28424 at the beginning of the function to mark the end of the prologue.
28425 We could eliminate any other duplicates within the function. For
28426 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28427 that second line number entry. */
28428 /* Recall that this end-of-prologue indication is *not* the same thing
28429 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28430 to which the hook corresponds, follows the last insn that was
28431 emitted by gen_prologue. What we need is to precede the first insn
28432 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28433 insn that corresponds to something the user wrote. These may be
28434 very different locations once scheduling is enabled. */
28436 if (0 && file_num == table->file_num
28437 && line == table->line_num
28438 && column == table->column_num
28439 && discriminator == table->discrim_num
28440 && is_stmt == table->is_stmt)
28441 return;
28443 switch_to_section (current_function_section ());
28445 /* If requested, emit something human-readable. */
28446 if (flag_debug_asm)
28448 if (debug_column_info)
28449 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28450 filename, line, column);
28451 else
28452 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28453 filename, line);
28456 if (output_asm_line_debug_info ())
28458 /* Emit the .loc directive understood by GNU as. */
28459 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28460 file_num, line, is_stmt, discriminator */
28461 fputs ("\t.loc ", asm_out_file);
28462 fprint_ul (asm_out_file, file_num);
28463 putc (' ', asm_out_file);
28464 fprint_ul (asm_out_file, line);
28465 putc (' ', asm_out_file);
28466 fprint_ul (asm_out_file, column);
28468 if (is_stmt != table->is_stmt)
28470 #if HAVE_GAS_LOC_STMT
28471 fputs (" is_stmt ", asm_out_file);
28472 putc (is_stmt ? '1' : '0', asm_out_file);
28473 #endif
28475 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28477 gcc_assert (discriminator > 0);
28478 fputs (" discriminator ", asm_out_file);
28479 fprint_ul (asm_out_file, (unsigned long) discriminator);
28481 if (debug_variable_location_views)
28483 if (!RESETTING_VIEW_P (table->view))
28485 table->symviews_since_reset++;
28486 if (table->symviews_since_reset > symview_upper_bound)
28487 symview_upper_bound = table->symviews_since_reset;
28488 /* When we're using the assembler to compute view
28489 numbers, we output symbolic labels after "view" in
28490 .loc directives, and the assembler will set them for
28491 us, so that we can refer to the view numbers in
28492 location lists. The only exceptions are when we know
28493 a view will be zero: "-0" is a forced reset, used
28494 e.g. in the beginning of functions, whereas "0" tells
28495 the assembler to check that there was a PC change
28496 since the previous view, in a way that implicitly
28497 resets the next view. */
28498 fputs (" view ", asm_out_file);
28499 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28500 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28501 assemble_name (asm_out_file, label);
28502 table->view = ++lvugid;
28504 else
28506 table->symviews_since_reset = 0;
28507 if (FORCE_RESETTING_VIEW_P (table->view))
28508 fputs (" view -0", asm_out_file);
28509 else
28510 fputs (" view 0", asm_out_file);
28511 /* Mark the present view as a zero view. Earlier debug
28512 binds may have already added its id to loclists to be
28513 emitted later, so we can't reuse the id for something
28514 else. However, it's good to know whether a view is
28515 known to be zero, because then we may be able to
28516 optimize out locviews that are all zeros, so take
28517 note of it in zero_view_p. */
28518 if (!zero_view_p)
28519 zero_view_p = BITMAP_GGC_ALLOC ();
28520 bitmap_set_bit (zero_view_p, lvugid);
28521 table->view = ++lvugid;
28524 putc ('\n', asm_out_file);
28526 else
28528 unsigned int label_num = ++line_info_label_num;
28530 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28532 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28533 push_dw_line_info_entry (table, LI_adv_address, label_num);
28534 else
28535 push_dw_line_info_entry (table, LI_set_address, label_num);
28536 if (debug_variable_location_views)
28538 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28539 if (resetting)
28540 table->view = 0;
28542 if (flag_debug_asm)
28543 fprintf (asm_out_file, "\t%s view %s%d\n",
28544 ASM_COMMENT_START,
28545 resetting ? "-" : "",
28546 table->view);
28548 table->view++;
28550 if (file_num != table->file_num)
28551 push_dw_line_info_entry (table, LI_set_file, file_num);
28552 if (discriminator != table->discrim_num)
28553 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28554 if (is_stmt != table->is_stmt)
28555 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28556 push_dw_line_info_entry (table, LI_set_line, line);
28557 if (debug_column_info)
28558 push_dw_line_info_entry (table, LI_set_column, column);
28561 table->file_num = file_num;
28562 table->line_num = line;
28563 table->column_num = column;
28564 table->discrim_num = discriminator;
28565 table->is_stmt = is_stmt;
28566 table->in_use = true;
28569 /* Record a source file location for a DECL_IGNORED_P function. */
28571 static void
28572 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28573 const char *filename)
28575 dw_fde_ref fde = cfun->fde;
28577 fde->ignored_debug = false;
28578 set_cur_line_info_table (function_section (fde->decl));
28580 dwarf2out_source_line (line, column, filename, 0, true);
28583 /* Record the beginning of a new source file. */
28585 static void
28586 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28588 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28590 macinfo_entry e;
28591 e.code = DW_MACINFO_start_file;
28592 e.lineno = lineno;
28593 e.info = ggc_strdup (filename);
28594 vec_safe_push (macinfo_table, e);
28598 /* Record the end of a source file. */
28600 static void
28601 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28603 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28605 macinfo_entry e;
28606 e.code = DW_MACINFO_end_file;
28607 e.lineno = lineno;
28608 e.info = NULL;
28609 vec_safe_push (macinfo_table, e);
28613 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28614 the tail part of the directive line, i.e. the part which is past the
28615 initial whitespace, #, whitespace, directive-name, whitespace part. */
28617 static void
28618 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28619 const char *buffer ATTRIBUTE_UNUSED)
28621 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28623 macinfo_entry e;
28624 /* Insert a dummy first entry to be able to optimize the whole
28625 predefined macro block using DW_MACRO_import. */
28626 if (macinfo_table->is_empty () && lineno <= 1)
28628 e.code = 0;
28629 e.lineno = 0;
28630 e.info = NULL;
28631 vec_safe_push (macinfo_table, e);
28633 e.code = DW_MACINFO_define;
28634 e.lineno = lineno;
28635 e.info = ggc_strdup (buffer);
28636 vec_safe_push (macinfo_table, e);
28640 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28641 the tail part of the directive line, i.e. the part which is past the
28642 initial whitespace, #, whitespace, directive-name, whitespace part. */
28644 static void
28645 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28646 const char *buffer ATTRIBUTE_UNUSED)
28648 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28650 macinfo_entry e;
28651 /* Insert a dummy first entry to be able to optimize the whole
28652 predefined macro block using DW_MACRO_import. */
28653 if (macinfo_table->is_empty () && lineno <= 1)
28655 e.code = 0;
28656 e.lineno = 0;
28657 e.info = NULL;
28658 vec_safe_push (macinfo_table, e);
28660 e.code = DW_MACINFO_undef;
28661 e.lineno = lineno;
28662 e.info = ggc_strdup (buffer);
28663 vec_safe_push (macinfo_table, e);
28667 /* Helpers to manipulate hash table of CUs. */
28669 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28671 static inline hashval_t hash (const macinfo_entry *);
28672 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28675 inline hashval_t
28676 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28678 return htab_hash_string (entry->info);
28681 inline bool
28682 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28683 const macinfo_entry *entry2)
28685 return !strcmp (entry1->info, entry2->info);
28688 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28690 /* Output a single .debug_macinfo entry. */
28692 static void
28693 output_macinfo_op (macinfo_entry *ref)
28695 int file_num;
28696 size_t len;
28697 struct indirect_string_node *node;
28698 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28699 struct dwarf_file_data *fd;
28701 switch (ref->code)
28703 case DW_MACINFO_start_file:
28704 fd = lookup_filename (ref->info);
28705 file_num = maybe_emit_file (fd);
28706 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28707 dw2_asm_output_data_uleb128 (ref->lineno,
28708 "Included from line number %lu",
28709 (unsigned long) ref->lineno);
28710 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28711 break;
28712 case DW_MACINFO_end_file:
28713 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28714 break;
28715 case DW_MACINFO_define:
28716 case DW_MACINFO_undef:
28717 len = strlen (ref->info) + 1;
28718 if ((!dwarf_strict || dwarf_version >= 5)
28719 && len > (size_t) dwarf_offset_size
28720 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28721 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28723 if (dwarf_split_debug_info && dwarf_version >= 5)
28724 ref->code = ref->code == DW_MACINFO_define
28725 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28726 else
28727 ref->code = ref->code == DW_MACINFO_define
28728 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28729 output_macinfo_op (ref);
28730 return;
28732 dw2_asm_output_data (1, ref->code,
28733 ref->code == DW_MACINFO_define
28734 ? "Define macro" : "Undefine macro");
28735 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28736 (unsigned long) ref->lineno);
28737 dw2_asm_output_nstring (ref->info, -1, "The macro");
28738 break;
28739 case DW_MACRO_define_strp:
28740 dw2_asm_output_data (1, ref->code, "Define macro strp");
28741 goto do_DW_MACRO_define_strpx;
28742 case DW_MACRO_undef_strp:
28743 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28744 goto do_DW_MACRO_define_strpx;
28745 case DW_MACRO_define_strx:
28746 dw2_asm_output_data (1, ref->code, "Define macro strx");
28747 goto do_DW_MACRO_define_strpx;
28748 case DW_MACRO_undef_strx:
28749 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28750 /* FALLTHRU */
28751 do_DW_MACRO_define_strpx:
28752 /* NB: dwarf2out_finish performs:
28753 1. save_macinfo_strings
28754 2. hash table traverse of index_string
28755 3. output_macinfo -> output_macinfo_op
28756 4. output_indirect_strings
28757 -> hash table traverse of output_index_string
28759 When output_macinfo_op is called, all index strings have been
28760 added to hash table by save_macinfo_strings and we can't pass
28761 INSERT to find_slot_with_hash which may expand hash table, even
28762 if no insertion is needed, and change hash table traverse order
28763 between index_string and output_index_string. */
28764 node = find_AT_string (ref->info, NO_INSERT);
28765 gcc_assert (node
28766 && (node->form == DW_FORM_strp
28767 || node->form == dwarf_FORM (DW_FORM_strx)));
28768 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28769 (unsigned long) ref->lineno);
28770 if (node->form == DW_FORM_strp)
28771 dw2_asm_output_offset (dwarf_offset_size, node->label,
28772 debug_str_section, "The macro: \"%s\"",
28773 ref->info);
28774 else
28775 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28776 ref->info);
28777 break;
28778 case DW_MACRO_import:
28779 dw2_asm_output_data (1, ref->code, "Import");
28780 ASM_GENERATE_INTERNAL_LABEL (label,
28781 DEBUG_MACRO_SECTION_LABEL,
28782 ref->lineno + macinfo_label_base);
28783 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28784 break;
28785 default:
28786 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28787 ASM_COMMENT_START, (unsigned long) ref->code);
28788 break;
28792 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28793 other compilation unit .debug_macinfo sections. IDX is the first
28794 index of a define/undef, return the number of ops that should be
28795 emitted in a comdat .debug_macinfo section and emit
28796 a DW_MACRO_import entry referencing it.
28797 If the define/undef entry should be emitted normally, return 0. */
28799 static unsigned
28800 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28801 macinfo_hash_type **macinfo_htab)
28803 macinfo_entry *first, *second, *cur, *inc;
28804 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28805 unsigned char checksum[16];
28806 struct md5_ctx ctx;
28807 char *grp_name, *tail;
28808 const char *base;
28809 unsigned int i, count, encoded_filename_len, linebuf_len;
28810 macinfo_entry **slot;
28812 first = &(*macinfo_table)[idx];
28813 second = &(*macinfo_table)[idx + 1];
28815 /* Optimize only if there are at least two consecutive define/undef ops,
28816 and either all of them are before first DW_MACINFO_start_file
28817 with lineno {0,1} (i.e. predefined macro block), or all of them are
28818 in some included header file. */
28819 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28820 return 0;
28821 if (vec_safe_is_empty (files))
28823 if (first->lineno > 1 || second->lineno > 1)
28824 return 0;
28826 else if (first->lineno == 0)
28827 return 0;
28829 /* Find the last define/undef entry that can be grouped together
28830 with first and at the same time compute md5 checksum of their
28831 codes, linenumbers and strings. */
28832 md5_init_ctx (&ctx);
28833 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28834 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28835 break;
28836 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28837 break;
28838 else
28840 unsigned char code = cur->code;
28841 md5_process_bytes (&code, 1, &ctx);
28842 checksum_uleb128 (cur->lineno, &ctx);
28843 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28845 md5_finish_ctx (&ctx, checksum);
28846 count = i - idx;
28848 /* From the containing include filename (if any) pick up just
28849 usable characters from its basename. */
28850 if (vec_safe_is_empty (files))
28851 base = "";
28852 else
28853 base = lbasename (files->last ().info);
28854 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28855 if (ISIDNUM (base[i]) || base[i] == '.')
28856 encoded_filename_len++;
28857 /* Count . at the end. */
28858 if (encoded_filename_len)
28859 encoded_filename_len++;
28861 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28862 linebuf_len = strlen (linebuf);
28864 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28865 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28866 + 16 * 2 + 1);
28867 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28868 tail = grp_name + 4;
28869 if (encoded_filename_len)
28871 for (i = 0; base[i]; i++)
28872 if (ISIDNUM (base[i]) || base[i] == '.')
28873 *tail++ = base[i];
28874 *tail++ = '.';
28876 memcpy (tail, linebuf, linebuf_len);
28877 tail += linebuf_len;
28878 *tail++ = '.';
28879 for (i = 0; i < 16; i++)
28880 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28882 /* Construct a macinfo_entry for DW_MACRO_import
28883 in the empty vector entry before the first define/undef. */
28884 inc = &(*macinfo_table)[idx - 1];
28885 inc->code = DW_MACRO_import;
28886 inc->lineno = 0;
28887 inc->info = ggc_strdup (grp_name);
28888 if (!*macinfo_htab)
28889 *macinfo_htab = new macinfo_hash_type (10);
28890 /* Avoid emitting duplicates. */
28891 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28892 if (*slot != NULL)
28894 inc->code = 0;
28895 inc->info = NULL;
28896 /* If such an entry has been used before, just emit
28897 a DW_MACRO_import op. */
28898 inc = *slot;
28899 output_macinfo_op (inc);
28900 /* And clear all macinfo_entry in the range to avoid emitting them
28901 in the second pass. */
28902 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28904 cur->code = 0;
28905 cur->info = NULL;
28908 else
28910 *slot = inc;
28911 inc->lineno = (*macinfo_htab)->elements ();
28912 output_macinfo_op (inc);
28914 return count;
28917 /* Save any strings needed by the macinfo table in the debug str
28918 table. All strings must be collected into the table by the time
28919 index_string is called. */
28921 static void
28922 save_macinfo_strings (void)
28924 unsigned len;
28925 unsigned i;
28926 macinfo_entry *ref;
28928 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28930 switch (ref->code)
28932 /* Match the logic in output_macinfo_op to decide on
28933 indirect strings. */
28934 case DW_MACINFO_define:
28935 case DW_MACINFO_undef:
28936 len = strlen (ref->info) + 1;
28937 if ((!dwarf_strict || dwarf_version >= 5)
28938 && len > (unsigned) dwarf_offset_size
28939 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28940 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28941 set_indirect_string (find_AT_string (ref->info));
28942 break;
28943 case DW_MACINFO_start_file:
28944 /* -gsplit-dwarf -g3 will also output filename as indirect
28945 string. */
28946 if (!dwarf_split_debug_info)
28947 break;
28948 /* Fall through. */
28949 case DW_MACRO_define_strp:
28950 case DW_MACRO_undef_strp:
28951 case DW_MACRO_define_strx:
28952 case DW_MACRO_undef_strx:
28953 set_indirect_string (find_AT_string (ref->info));
28954 break;
28955 default:
28956 break;
28961 /* Output macinfo section(s). */
28963 static void
28964 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28966 unsigned i;
28967 unsigned long length = vec_safe_length (macinfo_table);
28968 macinfo_entry *ref;
28969 vec<macinfo_entry, va_gc> *files = NULL;
28970 macinfo_hash_type *macinfo_htab = NULL;
28971 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28973 if (! length)
28974 return;
28976 /* output_macinfo* uses these interchangeably. */
28977 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28978 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28979 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28980 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28982 /* AIX Assembler inserts the length, so adjust the reference to match the
28983 offset expected by debuggers. */
28984 strcpy (dl_section_ref, debug_line_label);
28985 if (XCOFF_DEBUGGING_INFO)
28986 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28988 /* For .debug_macro emit the section header. */
28989 if (!dwarf_strict || dwarf_version >= 5)
28991 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28992 "DWARF macro version number");
28993 if (dwarf_offset_size == 8)
28994 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28995 else
28996 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28997 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
28998 debug_line_section, NULL);
29001 /* In the first loop, it emits the primary .debug_macinfo section
29002 and after each emitted op the macinfo_entry is cleared.
29003 If a longer range of define/undef ops can be optimized using
29004 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29005 the vector before the first define/undef in the range and the
29006 whole range of define/undef ops is not emitted and kept. */
29007 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29009 switch (ref->code)
29011 case DW_MACINFO_start_file:
29012 vec_safe_push (files, *ref);
29013 break;
29014 case DW_MACINFO_end_file:
29015 if (!vec_safe_is_empty (files))
29016 files->pop ();
29017 break;
29018 case DW_MACINFO_define:
29019 case DW_MACINFO_undef:
29020 if ((!dwarf_strict || dwarf_version >= 5)
29021 && HAVE_COMDAT_GROUP
29022 && vec_safe_length (files) != 1
29023 && i > 0
29024 && i + 1 < length
29025 && (*macinfo_table)[i - 1].code == 0)
29027 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29028 if (count)
29030 i += count - 1;
29031 continue;
29034 break;
29035 case 0:
29036 /* A dummy entry may be inserted at the beginning to be able
29037 to optimize the whole block of predefined macros. */
29038 if (i == 0)
29039 continue;
29040 default:
29041 break;
29043 output_macinfo_op (ref);
29044 ref->info = NULL;
29045 ref->code = 0;
29048 if (!macinfo_htab)
29049 return;
29051 /* Save the number of transparent includes so we can adjust the
29052 label number for the fat LTO object DWARF. */
29053 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29055 delete macinfo_htab;
29056 macinfo_htab = NULL;
29058 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29059 terminate the current chain and switch to a new comdat .debug_macinfo
29060 section and emit the define/undef entries within it. */
29061 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29062 switch (ref->code)
29064 case 0:
29065 continue;
29066 case DW_MACRO_import:
29068 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29069 tree comdat_key = get_identifier (ref->info);
29070 /* Terminate the previous .debug_macinfo section. */
29071 dw2_asm_output_data (1, 0, "End compilation unit");
29072 targetm.asm_out.named_section (debug_macinfo_section_name,
29073 SECTION_DEBUG
29074 | SECTION_LINKONCE
29075 | (early_lto_debug
29076 ? SECTION_EXCLUDE : 0),
29077 comdat_key);
29078 ASM_GENERATE_INTERNAL_LABEL (label,
29079 DEBUG_MACRO_SECTION_LABEL,
29080 ref->lineno + macinfo_label_base);
29081 ASM_OUTPUT_LABEL (asm_out_file, label);
29082 ref->code = 0;
29083 ref->info = NULL;
29084 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29085 "DWARF macro version number");
29086 if (dwarf_offset_size == 8)
29087 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29088 else
29089 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29091 break;
29092 case DW_MACINFO_define:
29093 case DW_MACINFO_undef:
29094 output_macinfo_op (ref);
29095 ref->code = 0;
29096 ref->info = NULL;
29097 break;
29098 default:
29099 gcc_unreachable ();
29102 macinfo_label_base += macinfo_label_base_adj;
29105 /* As init_sections_and_labels may get called multiple times, have a
29106 generation count for labels. */
29107 static unsigned init_sections_and_labels_generation;
29109 /* Initialize the various sections and labels for dwarf output and prefix
29110 them with PREFIX if non-NULL. Returns the generation (zero based
29111 number of times function was called). */
29113 static unsigned
29114 init_sections_and_labels (bool early_lto_debug)
29116 if (early_lto_debug)
29118 if (!dwarf_split_debug_info)
29120 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29121 SECTION_DEBUG | SECTION_EXCLUDE,
29122 NULL);
29123 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29124 SECTION_DEBUG | SECTION_EXCLUDE,
29125 NULL);
29126 debug_macinfo_section_name
29127 = ((dwarf_strict && dwarf_version < 5)
29128 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29129 debug_macinfo_section = get_section (debug_macinfo_section_name,
29130 SECTION_DEBUG
29131 | SECTION_EXCLUDE, NULL);
29133 else
29135 /* ??? Which of the following do we need early? */
29136 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29137 SECTION_DEBUG | SECTION_EXCLUDE,
29138 NULL);
29139 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29140 SECTION_DEBUG | SECTION_EXCLUDE,
29141 NULL);
29142 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29143 SECTION_DEBUG
29144 | SECTION_EXCLUDE, NULL);
29145 debug_skeleton_abbrev_section
29146 = get_section (DEBUG_LTO_ABBREV_SECTION,
29147 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29148 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29149 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29150 init_sections_and_labels_generation);
29152 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29153 stay in the main .o, but the skeleton_line goes into the split
29154 off dwo. */
29155 debug_skeleton_line_section
29156 = get_section (DEBUG_LTO_LINE_SECTION,
29157 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29158 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29159 DEBUG_SKELETON_LINE_SECTION_LABEL,
29160 init_sections_and_labels_generation);
29161 debug_str_offsets_section
29162 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29163 SECTION_DEBUG | SECTION_EXCLUDE,
29164 NULL);
29165 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29166 DEBUG_SKELETON_INFO_SECTION_LABEL,
29167 init_sections_and_labels_generation);
29168 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29169 DEBUG_STR_DWO_SECTION_FLAGS,
29170 NULL);
29171 debug_macinfo_section_name
29172 = ((dwarf_strict && dwarf_version < 5)
29173 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29174 debug_macinfo_section = get_section (debug_macinfo_section_name,
29175 SECTION_DEBUG | SECTION_EXCLUDE,
29176 NULL);
29178 /* For macro info and the file table we have to refer to a
29179 debug_line section. */
29180 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29181 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29182 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29183 DEBUG_LINE_SECTION_LABEL,
29184 init_sections_and_labels_generation);
29186 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29187 DEBUG_STR_SECTION_FLAGS
29188 | SECTION_EXCLUDE, NULL);
29189 if (!dwarf_split_debug_info)
29190 debug_line_str_section
29191 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29192 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29194 else
29196 if (!dwarf_split_debug_info)
29198 debug_info_section = get_section (DEBUG_INFO_SECTION,
29199 SECTION_DEBUG, NULL);
29200 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29201 SECTION_DEBUG, NULL);
29202 debug_loc_section = get_section (dwarf_version >= 5
29203 ? DEBUG_LOCLISTS_SECTION
29204 : DEBUG_LOC_SECTION,
29205 SECTION_DEBUG, NULL);
29206 debug_macinfo_section_name
29207 = ((dwarf_strict && dwarf_version < 5)
29208 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29209 debug_macinfo_section = get_section (debug_macinfo_section_name,
29210 SECTION_DEBUG, NULL);
29212 else
29214 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29215 SECTION_DEBUG | SECTION_EXCLUDE,
29216 NULL);
29217 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29218 SECTION_DEBUG | SECTION_EXCLUDE,
29219 NULL);
29220 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29221 SECTION_DEBUG, NULL);
29222 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29223 SECTION_DEBUG, NULL);
29224 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29225 SECTION_DEBUG, NULL);
29226 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29227 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29228 init_sections_and_labels_generation);
29230 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29231 stay in the main .o, but the skeleton_line goes into the
29232 split off dwo. */
29233 debug_skeleton_line_section
29234 = get_section (DEBUG_DWO_LINE_SECTION,
29235 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29236 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29237 DEBUG_SKELETON_LINE_SECTION_LABEL,
29238 init_sections_and_labels_generation);
29239 debug_str_offsets_section
29240 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29241 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29242 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29243 DEBUG_SKELETON_INFO_SECTION_LABEL,
29244 init_sections_and_labels_generation);
29245 debug_loc_section = get_section (dwarf_version >= 5
29246 ? DEBUG_DWO_LOCLISTS_SECTION
29247 : DEBUG_DWO_LOC_SECTION,
29248 SECTION_DEBUG | SECTION_EXCLUDE,
29249 NULL);
29250 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29251 DEBUG_STR_DWO_SECTION_FLAGS,
29252 NULL);
29253 debug_macinfo_section_name
29254 = ((dwarf_strict && dwarf_version < 5)
29255 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29256 debug_macinfo_section = get_section (debug_macinfo_section_name,
29257 SECTION_DEBUG | SECTION_EXCLUDE,
29258 NULL);
29259 if (dwarf_version >= 5)
29260 debug_ranges_dwo_section
29261 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29262 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29264 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29265 SECTION_DEBUG, NULL);
29266 debug_line_section = get_section (DEBUG_LINE_SECTION,
29267 SECTION_DEBUG, NULL);
29268 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29269 SECTION_DEBUG, NULL);
29270 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29271 SECTION_DEBUG, NULL);
29272 debug_str_section = get_section (DEBUG_STR_SECTION,
29273 DEBUG_STR_SECTION_FLAGS, NULL);
29274 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29275 || asm_outputs_debug_line_str ())
29276 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29277 DEBUG_STR_SECTION_FLAGS, NULL);
29279 debug_ranges_section = get_section (dwarf_version >= 5
29280 ? DEBUG_RNGLISTS_SECTION
29281 : DEBUG_RANGES_SECTION,
29282 SECTION_DEBUG, NULL);
29283 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29284 SECTION_DEBUG, NULL);
29287 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29288 DEBUG_ABBREV_SECTION_LABEL,
29289 init_sections_and_labels_generation);
29290 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29291 DEBUG_INFO_SECTION_LABEL,
29292 init_sections_and_labels_generation);
29293 info_section_emitted = false;
29294 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29295 DEBUG_LINE_SECTION_LABEL,
29296 init_sections_and_labels_generation);
29297 /* There are up to 6 unique ranges labels per generation.
29298 See also output_rnglists. */
29299 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29300 DEBUG_RANGES_SECTION_LABEL,
29301 init_sections_and_labels_generation * 6);
29302 if (dwarf_version >= 5 && dwarf_split_debug_info)
29303 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29304 DEBUG_RANGES_SECTION_LABEL,
29305 1 + init_sections_and_labels_generation * 6);
29306 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29307 DEBUG_ADDR_SECTION_LABEL,
29308 init_sections_and_labels_generation);
29309 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29310 (dwarf_strict && dwarf_version < 5)
29311 ? DEBUG_MACINFO_SECTION_LABEL
29312 : DEBUG_MACRO_SECTION_LABEL,
29313 init_sections_and_labels_generation);
29314 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29315 init_sections_and_labels_generation);
29317 ++init_sections_and_labels_generation;
29318 return init_sections_and_labels_generation - 1;
29321 /* Set up for Dwarf output at the start of compilation. */
29323 static void
29324 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29326 /* Allocate the file_table. */
29327 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29329 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29330 /* Allocate the decl_die_table. */
29331 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29333 /* Allocate the decl_loc_table. */
29334 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29336 /* Allocate the cached_dw_loc_list_table. */
29337 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29339 /* Allocate the initial hunk of the abbrev_die_table. */
29340 vec_alloc (abbrev_die_table, 256);
29341 /* Zero-th entry is allocated, but unused. */
29342 abbrev_die_table->quick_push (NULL);
29344 /* Allocate the dwarf_proc_stack_usage_map. */
29345 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29347 /* Allocate the pubtypes and pubnames vectors. */
29348 vec_alloc (pubname_table, 32);
29349 vec_alloc (pubtype_table, 32);
29351 vec_alloc (incomplete_types, 64);
29353 vec_alloc (used_rtx_array, 32);
29355 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29356 vec_alloc (macinfo_table, 64);
29357 #endif
29359 /* If front-ends already registered a main translation unit but we were not
29360 ready to perform the association, do this now. */
29361 if (main_translation_unit != NULL_TREE)
29362 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29365 /* Called before compile () starts outputtting functions, variables
29366 and toplevel asms into assembly. */
29368 static void
29369 dwarf2out_assembly_start (void)
29371 if (text_section_line_info)
29372 return;
29374 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29375 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29376 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29377 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29378 COLD_TEXT_SECTION_LABEL, 0);
29379 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29381 switch_to_section (text_section);
29382 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29383 #endif
29385 /* Make sure the line number table for .text always exists. */
29386 text_section_line_info = new_line_info_table ();
29387 text_section_line_info->end_label = text_end_label;
29389 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29390 cur_line_info_table = text_section_line_info;
29391 #endif
29393 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29394 && dwarf2out_do_cfi_asm ()
29395 && !dwarf2out_do_eh_frame ())
29396 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29398 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29399 if (output_asm_line_debug_info () && dwarf_version >= 5)
29401 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29402 tell it the comp_dir and main file name for the zero entry
29403 line table. */
29404 const char *comp_dir, *filename0;
29406 comp_dir = comp_dir_string ();
29407 if (comp_dir == NULL)
29408 comp_dir = "";
29410 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29411 if (filename0 == NULL)
29412 filename0 = "";
29414 fprintf (asm_out_file, "\t.file 0 ");
29415 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29416 fputc (' ', asm_out_file);
29417 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29418 fputc ('\n', asm_out_file);
29420 else
29421 #endif
29422 /* Work around for PR101575: output a dummy .file directive. */
29423 if (!last_emitted_file && dwarf_debuginfo_p ()
29424 && debug_info_level >= DINFO_LEVEL_TERSE)
29426 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29428 if (filename0 == NULL)
29429 filename0 = "<dummy>";
29430 maybe_emit_file (lookup_filename (filename0));
29434 /* A helper function for dwarf2out_finish called through
29435 htab_traverse. Assign a string its index. All strings must be
29436 collected into the table by the time index_string is called,
29437 because the indexing code relies on htab_traverse to traverse nodes
29438 in the same order for each run. */
29441 index_string (indirect_string_node **h, unsigned int *index)
29443 indirect_string_node *node = *h;
29445 find_string_form (node);
29446 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29448 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29449 node->index = *index;
29450 *index += 1;
29452 return 1;
29455 /* A helper function for output_indirect_strings called through
29456 htab_traverse. Output the offset to a string and update the
29457 current offset. */
29460 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29462 indirect_string_node *node = *h;
29464 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29466 /* Assert that this node has been assigned an index. */
29467 gcc_assert (node->index != NO_INDEX_ASSIGNED
29468 && node->index != NOT_INDEXED);
29469 dw2_asm_output_data (dwarf_offset_size, *offset,
29470 "indexed string 0x%x: %s", node->index, node->str);
29471 *offset += strlen (node->str) + 1;
29473 return 1;
29476 /* A helper function for dwarf2out_finish called through
29477 htab_traverse. Output the indexed string. */
29480 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29482 struct indirect_string_node *node = *h;
29484 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29486 /* Assert that the strings are output in the same order as their
29487 indexes were assigned. */
29488 gcc_assert (*cur_idx == node->index);
29489 assemble_string (node->str, strlen (node->str) + 1);
29490 *cur_idx += 1;
29492 return 1;
29495 /* A helper function for output_indirect_strings. Counts the number
29496 of index strings offsets. Must match the logic of the functions
29497 output_index_string[_offsets] above. */
29499 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29501 struct indirect_string_node *node = *h;
29503 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29504 *last_idx += 1;
29505 return 1;
29508 /* A helper function for dwarf2out_finish called through
29509 htab_traverse. Emit one queued .debug_str string. */
29512 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29514 struct indirect_string_node *node = *h;
29516 node->form = find_string_form (node);
29517 if (node->form == form && node->refcount > 0)
29519 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29520 assemble_string (node->str, strlen (node->str) + 1);
29523 return 1;
29526 /* Output the indexed string table. */
29528 static void
29529 output_indirect_strings (void)
29531 switch_to_section (debug_str_section);
29532 if (!dwarf_split_debug_info)
29533 debug_str_hash->traverse<enum dwarf_form,
29534 output_indirect_string> (DW_FORM_strp);
29535 else
29537 unsigned int offset = 0;
29538 unsigned int cur_idx = 0;
29540 if (skeleton_debug_str_hash)
29541 skeleton_debug_str_hash->traverse<enum dwarf_form,
29542 output_indirect_string> (DW_FORM_strp);
29544 switch_to_section (debug_str_offsets_section);
29545 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29546 header. Note that we don't need to generate a label to the
29547 actual index table following the header here, because this is
29548 for the split dwarf case only. In an .dwo file there is only
29549 one string offsets table (and one debug info section). But
29550 if we would start using string offset tables for the main (or
29551 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29552 pointing to the actual index after the header. Split dwarf
29553 units will never have a string offsets base attribute. When
29554 a split unit is moved into a .dwp file the string offsets can
29555 be found through the .debug_cu_index section table. */
29556 if (dwarf_version >= 5)
29558 unsigned int last_idx = 0;
29559 unsigned long str_offsets_length;
29561 debug_str_hash->traverse_noresize
29562 <unsigned int *, count_index_strings> (&last_idx);
29563 str_offsets_length = last_idx * dwarf_offset_size + 4;
29564 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29565 dw2_asm_output_data (4, 0xffffffff,
29566 "Escape value for 64-bit DWARF extension");
29567 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29568 "Length of string offsets unit");
29569 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29570 dw2_asm_output_data (2, 0, "Header zero padding");
29572 debug_str_hash->traverse_noresize
29573 <unsigned int *, output_index_string_offset> (&offset);
29574 switch_to_section (debug_str_dwo_section);
29575 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29576 (&cur_idx);
29580 /* Callback for htab_traverse to assign an index to an entry in the
29581 table, and to write that entry to the .debug_addr section. */
29584 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29586 addr_table_entry *entry = *slot;
29588 if (entry->refcount == 0)
29590 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29591 || entry->index == NOT_INDEXED);
29592 return 1;
29595 gcc_assert (entry->index == *cur_index);
29596 (*cur_index)++;
29598 switch (entry->kind)
29600 case ate_kind_rtx:
29601 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29602 "0x%x", entry->index);
29603 break;
29604 case ate_kind_rtx_dtprel:
29605 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29606 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29607 DWARF2_ADDR_SIZE,
29608 entry->addr.rtl);
29609 fputc ('\n', asm_out_file);
29610 break;
29611 case ate_kind_label:
29612 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29613 "0x%x", entry->index);
29614 break;
29615 default:
29616 gcc_unreachable ();
29618 return 1;
29621 /* A helper function for dwarf2out_finish. Counts the number
29622 of indexed addresses. Must match the logic of the functions
29623 output_addr_table_entry above. */
29625 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29627 addr_table_entry *entry = *slot;
29629 if (entry->refcount > 0)
29630 *last_idx += 1;
29631 return 1;
29634 /* Produce the .debug_addr section. */
29636 static void
29637 output_addr_table (void)
29639 unsigned int index = 0;
29640 if (addr_index_table == NULL || addr_index_table->size () == 0)
29641 return;
29643 switch_to_section (debug_addr_section);
29644 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29645 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29646 before DWARF5, didn't have a header for .debug_addr units.
29647 DWARF5 specifies a small header when address tables are used. */
29648 if (dwarf_version >= 5)
29650 unsigned int last_idx = 0;
29651 unsigned long addrs_length;
29653 addr_index_table->traverse_noresize
29654 <unsigned int *, count_index_addrs> (&last_idx);
29655 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29657 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29658 dw2_asm_output_data (4, 0xffffffff,
29659 "Escape value for 64-bit DWARF extension");
29660 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29661 "Length of Address Unit");
29662 dw2_asm_output_data (2, 5, "DWARF addr version");
29663 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29664 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29666 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29668 addr_index_table
29669 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29672 #if ENABLE_ASSERT_CHECKING
29673 /* Verify that all marks are clear. */
29675 static void
29676 verify_marks_clear (dw_die_ref die)
29678 dw_die_ref c;
29680 gcc_assert (! die->die_mark);
29681 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29683 #endif /* ENABLE_ASSERT_CHECKING */
29685 /* Clear the marks for a die and its children.
29686 Be cool if the mark isn't set. */
29688 static void
29689 prune_unmark_dies (dw_die_ref die)
29691 dw_die_ref c;
29693 if (die->die_mark)
29694 die->die_mark = 0;
29695 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29698 /* Given LOC that is referenced by a DIE we're marking as used, find all
29699 referenced DWARF procedures it references and mark them as used. */
29701 static void
29702 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29704 for (; loc != NULL; loc = loc->dw_loc_next)
29705 switch (loc->dw_loc_opc)
29707 case DW_OP_implicit_pointer:
29708 case DW_OP_convert:
29709 case DW_OP_reinterpret:
29710 case DW_OP_GNU_implicit_pointer:
29711 case DW_OP_GNU_convert:
29712 case DW_OP_GNU_reinterpret:
29713 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29714 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29715 break;
29716 case DW_OP_GNU_variable_value:
29717 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29719 dw_die_ref ref
29720 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29721 if (ref == NULL)
29722 break;
29723 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29724 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29725 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29727 /* FALLTHRU */
29728 case DW_OP_call2:
29729 case DW_OP_call4:
29730 case DW_OP_call_ref:
29731 case DW_OP_const_type:
29732 case DW_OP_GNU_const_type:
29733 case DW_OP_GNU_parameter_ref:
29734 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29735 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29736 break;
29737 case DW_OP_regval_type:
29738 case DW_OP_deref_type:
29739 case DW_OP_GNU_regval_type:
29740 case DW_OP_GNU_deref_type:
29741 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29742 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29743 break;
29744 case DW_OP_entry_value:
29745 case DW_OP_GNU_entry_value:
29746 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29747 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29748 break;
29749 default:
29750 break;
29754 /* Given DIE that we're marking as used, find any other dies
29755 it references as attributes and mark them as used. */
29757 static void
29758 prune_unused_types_walk_attribs (dw_die_ref die)
29760 dw_attr_node *a;
29761 unsigned ix;
29763 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29765 switch (AT_class (a))
29767 /* Make sure DWARF procedures referenced by location descriptions will
29768 get emitted. */
29769 case dw_val_class_loc:
29770 prune_unused_types_walk_loc_descr (AT_loc (a));
29771 break;
29772 case dw_val_class_loc_list:
29773 for (dw_loc_list_ref list = AT_loc_list (a);
29774 list != NULL;
29775 list = list->dw_loc_next)
29776 prune_unused_types_walk_loc_descr (list->expr);
29777 break;
29779 case dw_val_class_view_list:
29780 /* This points to a loc_list in another attribute, so it's
29781 already covered. */
29782 break;
29784 case dw_val_class_die_ref:
29785 /* A reference to another DIE.
29786 Make sure that it will get emitted.
29787 If it was broken out into a comdat group, don't follow it. */
29788 if (! AT_ref (a)->comdat_type_p
29789 || a->dw_attr == DW_AT_specification)
29790 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29791 break;
29793 case dw_val_class_str:
29794 /* Set the string's refcount to 0 so that prune_unused_types_mark
29795 accounts properly for it. */
29796 a->dw_attr_val.v.val_str->refcount = 0;
29797 break;
29799 default:
29800 break;
29805 /* Mark the generic parameters and arguments children DIEs of DIE. */
29807 static void
29808 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29810 dw_die_ref c;
29812 if (die == NULL || die->die_child == NULL)
29813 return;
29814 c = die->die_child;
29817 if (is_template_parameter (c))
29818 prune_unused_types_mark (c, 1);
29819 c = c->die_sib;
29820 } while (c && c != die->die_child);
29823 /* Mark DIE as being used. If DOKIDS is true, then walk down
29824 to DIE's children. */
29826 static void
29827 prune_unused_types_mark (dw_die_ref die, int dokids)
29829 dw_die_ref c;
29831 if (die->die_mark == 0)
29833 /* We haven't done this node yet. Mark it as used. */
29834 die->die_mark = 1;
29835 /* If this is the DIE of a generic type instantiation,
29836 mark the children DIEs that describe its generic parms and
29837 args. */
29838 prune_unused_types_mark_generic_parms_dies (die);
29840 /* We also have to mark its parents as used.
29841 (But we don't want to mark our parent's kids due to this,
29842 unless it is a class.) */
29843 if (die->die_parent)
29844 prune_unused_types_mark (die->die_parent,
29845 class_scope_p (die->die_parent));
29847 /* Mark any referenced nodes. */
29848 prune_unused_types_walk_attribs (die);
29850 /* If this node is a specification,
29851 also mark the definition, if it exists. */
29852 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29853 prune_unused_types_mark (die->die_definition, 1);
29856 if (dokids && die->die_mark != 2)
29858 /* We need to walk the children, but haven't done so yet.
29859 Remember that we've walked the kids. */
29860 die->die_mark = 2;
29862 /* If this is an array type, we need to make sure our
29863 kids get marked, even if they're types. If we're
29864 breaking out types into comdat sections, do this
29865 for all type definitions. */
29866 if (die->die_tag == DW_TAG_array_type
29867 || (use_debug_types
29868 && is_type_die (die) && ! is_declaration_die (die)))
29869 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29870 else
29871 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29875 /* For local classes, look if any static member functions were emitted
29876 and if so, mark them. */
29878 static void
29879 prune_unused_types_walk_local_classes (dw_die_ref die)
29881 dw_die_ref c;
29883 if (die->die_mark == 2)
29884 return;
29886 switch (die->die_tag)
29888 case DW_TAG_structure_type:
29889 case DW_TAG_union_type:
29890 case DW_TAG_class_type:
29891 case DW_TAG_interface_type:
29892 break;
29894 case DW_TAG_subprogram:
29895 if (!get_AT_flag (die, DW_AT_declaration)
29896 || die->die_definition != NULL)
29897 prune_unused_types_mark (die, 1);
29898 return;
29900 default:
29901 return;
29904 /* Mark children. */
29905 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29908 /* Walk the tree DIE and mark types that we actually use. */
29910 static void
29911 prune_unused_types_walk (dw_die_ref die)
29913 dw_die_ref c;
29915 /* Don't do anything if this node is already marked and
29916 children have been marked as well. */
29917 if (die->die_mark == 2)
29918 return;
29920 switch (die->die_tag)
29922 case DW_TAG_structure_type:
29923 case DW_TAG_union_type:
29924 case DW_TAG_class_type:
29925 case DW_TAG_interface_type:
29926 if (die->die_perennial_p)
29927 break;
29929 for (c = die->die_parent; c; c = c->die_parent)
29930 if (c->die_tag == DW_TAG_subprogram)
29931 break;
29933 /* Finding used static member functions inside of classes
29934 is needed just for local classes, because for other classes
29935 static member function DIEs with DW_AT_specification
29936 are emitted outside of the DW_TAG_*_type. If we ever change
29937 it, we'd need to call this even for non-local classes. */
29938 if (c)
29939 prune_unused_types_walk_local_classes (die);
29941 /* It's a type node --- don't mark it. */
29942 return;
29944 case DW_TAG_const_type:
29945 case DW_TAG_packed_type:
29946 case DW_TAG_pointer_type:
29947 case DW_TAG_reference_type:
29948 case DW_TAG_rvalue_reference_type:
29949 case DW_TAG_volatile_type:
29950 case DW_TAG_typedef:
29951 case DW_TAG_array_type:
29952 case DW_TAG_friend:
29953 case DW_TAG_enumeration_type:
29954 case DW_TAG_subroutine_type:
29955 case DW_TAG_string_type:
29956 case DW_TAG_set_type:
29957 case DW_TAG_subrange_type:
29958 case DW_TAG_ptr_to_member_type:
29959 case DW_TAG_file_type:
29960 /* Type nodes are useful only when other DIEs reference them --- don't
29961 mark them. */
29962 /* FALLTHROUGH */
29964 case DW_TAG_dwarf_procedure:
29965 /* Likewise for DWARF procedures. */
29967 if (die->die_perennial_p)
29968 break;
29970 return;
29972 case DW_TAG_variable:
29973 if (flag_debug_only_used_symbols)
29975 if (die->die_perennial_p)
29976 break;
29978 /* For static data members, the declaration in the class is supposed
29979 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29980 DWARF5. DW_TAG_member will be marked, so mark even such
29981 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29982 attribute. */
29983 if (dwarf_version >= 5
29984 && class_scope_p (die->die_parent)
29985 && get_AT (die, DW_AT_const_value))
29986 break;
29988 /* premark_used_variables marks external variables --- don't mark
29989 them here. But function-local externals are always considered
29990 used. */
29991 if (get_AT (die, DW_AT_external))
29993 for (c = die->die_parent; c; c = c->die_parent)
29994 if (c->die_tag == DW_TAG_subprogram)
29995 break;
29996 if (!c)
29997 return;
30000 /* FALLTHROUGH */
30002 default:
30003 /* Mark everything else. */
30004 break;
30007 if (die->die_mark == 0)
30009 die->die_mark = 1;
30011 /* Now, mark any dies referenced from here. */
30012 prune_unused_types_walk_attribs (die);
30015 die->die_mark = 2;
30017 /* Mark children. */
30018 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30021 /* Increment the string counts on strings referred to from DIE's
30022 attributes. */
30024 static void
30025 prune_unused_types_update_strings (dw_die_ref die)
30027 dw_attr_node *a;
30028 unsigned ix;
30030 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30031 if (AT_class (a) == dw_val_class_str)
30033 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30034 s->refcount++;
30035 /* Avoid unnecessarily putting strings that are used less than
30036 twice in the hash table. */
30037 if (s->form != DW_FORM_line_strp
30038 && (s->refcount
30039 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30041 indirect_string_node **slot
30042 = debug_str_hash->find_slot_with_hash (s->str,
30043 htab_hash_string (s->str),
30044 INSERT);
30045 gcc_assert (*slot == NULL);
30046 *slot = s;
30051 /* Mark DIE and its children as removed. */
30053 static void
30054 mark_removed (dw_die_ref die)
30056 dw_die_ref c;
30057 die->removed = true;
30058 FOR_EACH_CHILD (die, c, mark_removed (c));
30061 /* Remove from the tree DIE any dies that aren't marked. */
30063 static void
30064 prune_unused_types_prune (dw_die_ref die)
30066 dw_die_ref c;
30068 gcc_assert (die->die_mark);
30069 prune_unused_types_update_strings (die);
30071 if (! die->die_child)
30072 return;
30074 c = die->die_child;
30075 do {
30076 dw_die_ref prev = c, next;
30077 for (c = c->die_sib; ! c->die_mark; c = next)
30078 if (c == die->die_child)
30080 /* No marked children between 'prev' and the end of the list. */
30081 if (prev == c)
30082 /* No marked children at all. */
30083 die->die_child = NULL;
30084 else
30086 prev->die_sib = c->die_sib;
30087 die->die_child = prev;
30089 c->die_sib = NULL;
30090 mark_removed (c);
30091 return;
30093 else
30095 next = c->die_sib;
30096 c->die_sib = NULL;
30097 mark_removed (c);
30100 if (c != prev->die_sib)
30101 prev->die_sib = c;
30102 prune_unused_types_prune (c);
30103 } while (c != die->die_child);
30106 /* Remove dies representing declarations that we never use. */
30108 static void
30109 prune_unused_types (void)
30111 unsigned int i;
30112 limbo_die_node *node;
30113 comdat_type_node *ctnode;
30114 pubname_entry *pub;
30115 dw_die_ref base_type;
30117 #if ENABLE_ASSERT_CHECKING
30118 /* All the marks should already be clear. */
30119 verify_marks_clear (comp_unit_die ());
30120 for (node = limbo_die_list; node; node = node->next)
30121 verify_marks_clear (node->die);
30122 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30123 verify_marks_clear (ctnode->root_die);
30124 #endif /* ENABLE_ASSERT_CHECKING */
30126 /* Mark types that are used in global variables. */
30127 premark_types_used_by_global_vars ();
30129 /* Mark variables used in the symtab. */
30130 if (flag_debug_only_used_symbols)
30131 premark_used_variables ();
30133 /* Set the mark on nodes that are actually used. */
30134 prune_unused_types_walk (comp_unit_die ());
30135 for (node = limbo_die_list; node; node = node->next)
30136 prune_unused_types_walk (node->die);
30137 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30139 prune_unused_types_walk (ctnode->root_die);
30140 prune_unused_types_mark (ctnode->type_die, 1);
30143 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30144 are unusual in that they are pubnames that are the children of pubtypes.
30145 They should only be marked via their parent DW_TAG_enumeration_type die,
30146 not as roots in themselves. */
30147 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30148 if (pub->die->die_tag != DW_TAG_enumerator)
30149 prune_unused_types_mark (pub->die, 1);
30150 for (i = 0; base_types.iterate (i, &base_type); i++)
30151 prune_unused_types_mark (base_type, 1);
30153 /* Also set the mark on nodes that could be referenced by
30154 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30155 by DW_TAG_inlined_subroutine origins. */
30156 cgraph_node *cnode;
30157 FOR_EACH_FUNCTION (cnode)
30158 if (cnode->referred_to_p (false))
30160 dw_die_ref die = lookup_decl_die (cnode->decl);
30161 if (die == NULL || die->die_mark)
30162 continue;
30163 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30164 if (e->caller != cnode)
30166 prune_unused_types_mark (die, 1);
30167 break;
30171 if (debug_str_hash)
30172 debug_str_hash->empty ();
30173 if (skeleton_debug_str_hash)
30174 skeleton_debug_str_hash->empty ();
30175 prune_unused_types_prune (comp_unit_die ());
30176 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30178 node = *pnode;
30179 if (!node->die->die_mark)
30180 *pnode = node->next;
30181 else
30183 prune_unused_types_prune (node->die);
30184 pnode = &node->next;
30187 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30188 prune_unused_types_prune (ctnode->root_die);
30190 /* Leave the marks clear. */
30191 prune_unmark_dies (comp_unit_die ());
30192 for (node = limbo_die_list; node; node = node->next)
30193 prune_unmark_dies (node->die);
30194 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30195 prune_unmark_dies (ctnode->root_die);
30198 /* Helpers to manipulate hash table of comdat type units. */
30200 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30202 static inline hashval_t hash (const comdat_type_node *);
30203 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30206 inline hashval_t
30207 comdat_type_hasher::hash (const comdat_type_node *type_node)
30209 hashval_t h;
30210 memcpy (&h, type_node->signature, sizeof (h));
30211 return h;
30214 inline bool
30215 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30216 const comdat_type_node *type_node_2)
30218 return (! memcmp (type_node_1->signature, type_node_2->signature,
30219 DWARF_TYPE_SIGNATURE_SIZE));
30222 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30223 to the location it would have been added, should we know its
30224 DECL_ASSEMBLER_NAME when we added other attributes. This will
30225 probably improve compactness of debug info, removing equivalent
30226 abbrevs, and hide any differences caused by deferring the
30227 computation of the assembler name, triggered by e.g. PCH. */
30229 static inline void
30230 move_linkage_attr (dw_die_ref die)
30232 unsigned ix = vec_safe_length (die->die_attr);
30233 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30235 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30236 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30238 while (--ix > 0)
30240 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30242 if (prev->dw_attr == DW_AT_decl_line
30243 || prev->dw_attr == DW_AT_decl_column
30244 || prev->dw_attr == DW_AT_name)
30245 break;
30248 if (ix != vec_safe_length (die->die_attr) - 1)
30250 die->die_attr->pop ();
30251 die->die_attr->quick_insert (ix, linkage);
30255 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30256 referenced from typed stack ops and count how often they are used. */
30258 static void
30259 mark_base_types (dw_loc_descr_ref loc)
30261 dw_die_ref base_type = NULL;
30263 for (; loc; loc = loc->dw_loc_next)
30265 switch (loc->dw_loc_opc)
30267 case DW_OP_regval_type:
30268 case DW_OP_deref_type:
30269 case DW_OP_GNU_regval_type:
30270 case DW_OP_GNU_deref_type:
30271 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30272 break;
30273 case DW_OP_convert:
30274 case DW_OP_reinterpret:
30275 case DW_OP_GNU_convert:
30276 case DW_OP_GNU_reinterpret:
30277 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30278 continue;
30279 /* FALLTHRU */
30280 case DW_OP_const_type:
30281 case DW_OP_GNU_const_type:
30282 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30283 break;
30284 case DW_OP_entry_value:
30285 case DW_OP_GNU_entry_value:
30286 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30287 continue;
30288 default:
30289 continue;
30291 gcc_assert (base_type->die_parent == comp_unit_die ());
30292 if (base_type->die_mark)
30293 base_type->die_mark++;
30294 else
30296 base_types.safe_push (base_type);
30297 base_type->die_mark = 1;
30302 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30303 referenced from typed stack ops and count how often they are used. */
30305 static void
30306 mark_base_types (dw_die_ref die)
30308 dw_die_ref c;
30309 dw_attr_node *a;
30310 dw_loc_list_ref *curr;
30311 unsigned ix;
30313 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30314 switch (AT_class (a))
30316 case dw_val_class_loc_list:
30317 curr = AT_loc_list_ptr (a);
30318 while (*curr)
30320 mark_base_types ((*curr)->expr);
30321 curr = &(*curr)->dw_loc_next;
30323 break;
30325 case dw_val_class_loc:
30326 mark_base_types (AT_loc (a));
30327 break;
30329 default:
30330 break;
30333 FOR_EACH_CHILD (die, c, mark_base_types (c));
30336 /* Comparison function for sorting marked base types. */
30338 static int
30339 base_type_cmp (const void *x, const void *y)
30341 dw_die_ref dx = *(const dw_die_ref *) x;
30342 dw_die_ref dy = *(const dw_die_ref *) y;
30343 unsigned int byte_size1, byte_size2;
30344 unsigned int encoding1, encoding2;
30345 unsigned int align1, align2;
30346 if (dx->die_mark > dy->die_mark)
30347 return -1;
30348 if (dx->die_mark < dy->die_mark)
30349 return 1;
30350 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30351 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30352 if (byte_size1 < byte_size2)
30353 return 1;
30354 if (byte_size1 > byte_size2)
30355 return -1;
30356 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30357 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30358 if (encoding1 < encoding2)
30359 return 1;
30360 if (encoding1 > encoding2)
30361 return -1;
30362 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30363 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30364 if (align1 < align2)
30365 return 1;
30366 if (align1 > align2)
30367 return -1;
30368 return 0;
30371 /* Move base types marked by mark_base_types as early as possible
30372 in the CU, sorted by decreasing usage count both to make the
30373 uleb128 references as small as possible and to make sure they
30374 will have die_offset already computed by calc_die_sizes when
30375 sizes of typed stack loc ops is computed. */
30377 static void
30378 move_marked_base_types (void)
30380 unsigned int i;
30381 dw_die_ref base_type, die, c;
30383 if (base_types.is_empty ())
30384 return;
30386 /* Sort by decreasing usage count, they will be added again in that
30387 order later on. */
30388 base_types.qsort (base_type_cmp);
30389 die = comp_unit_die ();
30390 c = die->die_child;
30393 dw_die_ref prev = c;
30394 c = c->die_sib;
30395 while (c->die_mark)
30397 remove_child_with_prev (c, prev);
30398 /* As base types got marked, there must be at least
30399 one node other than DW_TAG_base_type. */
30400 gcc_assert (die->die_child != NULL);
30401 c = prev->die_sib;
30404 while (c != die->die_child);
30405 gcc_assert (die->die_child);
30406 c = die->die_child;
30407 for (i = 0; base_types.iterate (i, &base_type); i++)
30409 base_type->die_mark = 0;
30410 base_type->die_sib = c->die_sib;
30411 c->die_sib = base_type;
30412 c = base_type;
30416 /* Helper function for resolve_addr, attempt to resolve
30417 one CONST_STRING, return true if successful. Similarly verify that
30418 SYMBOL_REFs refer to variables emitted in the current CU. */
30420 static bool
30421 resolve_one_addr (rtx *addr)
30423 rtx rtl = *addr;
30425 if (GET_CODE (rtl) == CONST_STRING)
30427 size_t len = strlen (XSTR (rtl, 0)) + 1;
30428 tree t = build_string (len, XSTR (rtl, 0));
30429 tree tlen = size_int (len - 1);
30430 TREE_TYPE (t)
30431 = build_array_type (char_type_node, build_index_type (tlen));
30432 rtl = lookup_constant_def (t);
30433 if (!rtl || !MEM_P (rtl))
30434 return false;
30435 rtl = XEXP (rtl, 0);
30436 if (GET_CODE (rtl) == SYMBOL_REF
30437 && SYMBOL_REF_DECL (rtl)
30438 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30439 return false;
30440 vec_safe_push (used_rtx_array, rtl);
30441 *addr = rtl;
30442 return true;
30445 if (GET_CODE (rtl) == SYMBOL_REF
30446 && SYMBOL_REF_DECL (rtl))
30448 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30450 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30451 return false;
30453 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30454 return false;
30457 if (GET_CODE (rtl) == CONST)
30459 subrtx_ptr_iterator::array_type array;
30460 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30461 if (!resolve_one_addr (*iter))
30462 return false;
30465 return true;
30468 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30469 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30470 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30472 static rtx
30473 string_cst_pool_decl (tree t)
30475 rtx rtl = output_constant_def (t, 1);
30476 unsigned char *array;
30477 dw_loc_descr_ref l;
30478 tree decl;
30479 size_t len;
30480 dw_die_ref ref;
30482 if (!rtl || !MEM_P (rtl))
30483 return NULL_RTX;
30484 rtl = XEXP (rtl, 0);
30485 if (GET_CODE (rtl) != SYMBOL_REF
30486 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30487 return NULL_RTX;
30489 decl = SYMBOL_REF_DECL (rtl);
30490 if (!lookup_decl_die (decl))
30492 len = TREE_STRING_LENGTH (t);
30493 vec_safe_push (used_rtx_array, rtl);
30494 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30495 array = ggc_vec_alloc<unsigned char> (len);
30496 memcpy (array, TREE_STRING_POINTER (t), len);
30497 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30498 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30499 l->dw_loc_oprnd2.v.val_vec.length = len;
30500 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30501 l->dw_loc_oprnd2.v.val_vec.array = array;
30502 add_AT_loc (ref, DW_AT_location, l);
30503 equate_decl_number_to_die (decl, ref);
30505 return rtl;
30508 /* Helper function of resolve_addr_in_expr. LOC is
30509 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30510 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30511 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30512 with DW_OP_implicit_pointer if possible
30513 and return true, if unsuccessful, return false. */
30515 static bool
30516 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30518 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30519 HOST_WIDE_INT offset = 0;
30520 dw_die_ref ref = NULL;
30521 tree decl;
30523 if (GET_CODE (rtl) == CONST
30524 && GET_CODE (XEXP (rtl, 0)) == PLUS
30525 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30527 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30528 rtl = XEXP (XEXP (rtl, 0), 0);
30530 if (GET_CODE (rtl) == CONST_STRING)
30532 size_t len = strlen (XSTR (rtl, 0)) + 1;
30533 tree t = build_string (len, XSTR (rtl, 0));
30534 tree tlen = size_int (len - 1);
30536 TREE_TYPE (t)
30537 = build_array_type (char_type_node, build_index_type (tlen));
30538 rtl = string_cst_pool_decl (t);
30539 if (!rtl)
30540 return false;
30542 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30544 decl = SYMBOL_REF_DECL (rtl);
30545 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30547 ref = lookup_decl_die (decl);
30548 if (ref && (get_AT (ref, DW_AT_location)
30549 || get_AT (ref, DW_AT_const_value)))
30551 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30552 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30553 loc->dw_loc_oprnd1.val_entry = NULL;
30554 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30555 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30556 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30557 loc->dw_loc_oprnd2.v.val_int = offset;
30558 return true;
30562 return false;
30565 /* Helper function for resolve_addr, handle one location
30566 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30567 the location list couldn't be resolved. */
30569 static bool
30570 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30572 dw_loc_descr_ref keep = NULL;
30573 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30574 switch (loc->dw_loc_opc)
30576 case DW_OP_addr:
30577 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30579 if ((prev == NULL
30580 || prev->dw_loc_opc == DW_OP_piece
30581 || prev->dw_loc_opc == DW_OP_bit_piece)
30582 && loc->dw_loc_next
30583 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30584 && (!dwarf_strict || dwarf_version >= 5)
30585 && optimize_one_addr_into_implicit_ptr (loc))
30586 break;
30587 return false;
30589 break;
30590 case DW_OP_GNU_addr_index:
30591 case DW_OP_addrx:
30592 case DW_OP_GNU_const_index:
30593 case DW_OP_constx:
30594 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30595 || loc->dw_loc_opc == DW_OP_addrx)
30596 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30597 || loc->dw_loc_opc == DW_OP_constx)
30598 && loc->dtprel))
30600 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30601 if (!resolve_one_addr (&rtl))
30602 return false;
30603 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30604 loc->dw_loc_oprnd1.val_entry
30605 = add_addr_table_entry (rtl, ate_kind_rtx);
30607 break;
30608 case DW_OP_const4u:
30609 case DW_OP_const8u:
30610 if (loc->dtprel
30611 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30612 return false;
30613 break;
30614 case DW_OP_plus_uconst:
30615 if (size_of_loc_descr (loc)
30616 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30618 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30620 dw_loc_descr_ref repl
30621 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30622 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30623 add_loc_descr (&repl, loc->dw_loc_next);
30624 *loc = *repl;
30626 break;
30627 case DW_OP_implicit_value:
30628 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30629 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30630 return false;
30631 break;
30632 case DW_OP_implicit_pointer:
30633 case DW_OP_GNU_implicit_pointer:
30634 case DW_OP_GNU_parameter_ref:
30635 case DW_OP_GNU_variable_value:
30636 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30638 dw_die_ref ref
30639 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30640 if (ref == NULL)
30641 return false;
30642 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30643 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30644 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30646 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30648 if (prev == NULL
30649 && loc->dw_loc_next == NULL
30650 && AT_class (a) == dw_val_class_loc)
30651 switch (a->dw_attr)
30653 /* Following attributes allow both exprloc and reference,
30654 so if the whole expression is DW_OP_GNU_variable_value
30655 alone we could transform it into reference. */
30656 case DW_AT_byte_size:
30657 case DW_AT_bit_size:
30658 case DW_AT_lower_bound:
30659 case DW_AT_upper_bound:
30660 case DW_AT_bit_stride:
30661 case DW_AT_count:
30662 case DW_AT_allocated:
30663 case DW_AT_associated:
30664 case DW_AT_byte_stride:
30665 a->dw_attr_val.val_class = dw_val_class_die_ref;
30666 a->dw_attr_val.val_entry = NULL;
30667 a->dw_attr_val.v.val_die_ref.die
30668 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30669 a->dw_attr_val.v.val_die_ref.external = 0;
30670 return true;
30671 default:
30672 break;
30674 if (dwarf_strict)
30675 return false;
30677 break;
30678 case DW_OP_const_type:
30679 case DW_OP_regval_type:
30680 case DW_OP_deref_type:
30681 case DW_OP_convert:
30682 case DW_OP_reinterpret:
30683 case DW_OP_GNU_const_type:
30684 case DW_OP_GNU_regval_type:
30685 case DW_OP_GNU_deref_type:
30686 case DW_OP_GNU_convert:
30687 case DW_OP_GNU_reinterpret:
30688 while (loc->dw_loc_next
30689 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30690 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30692 dw_die_ref base1, base2;
30693 unsigned enc1, enc2, size1, size2;
30694 if (loc->dw_loc_opc == DW_OP_regval_type
30695 || loc->dw_loc_opc == DW_OP_deref_type
30696 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30697 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30698 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30699 else if (loc->dw_loc_oprnd1.val_class
30700 == dw_val_class_unsigned_const)
30701 break;
30702 else
30703 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30704 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30705 == dw_val_class_unsigned_const)
30706 break;
30707 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30708 gcc_assert (base1->die_tag == DW_TAG_base_type
30709 && base2->die_tag == DW_TAG_base_type);
30710 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30711 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30712 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30713 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30714 if (size1 == size2
30715 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30716 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30717 && loc != keep)
30718 || enc1 == enc2))
30720 /* Optimize away next DW_OP_convert after
30721 adjusting LOC's base type die reference. */
30722 if (loc->dw_loc_opc == DW_OP_regval_type
30723 || loc->dw_loc_opc == DW_OP_deref_type
30724 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30725 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30726 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30727 else
30728 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30729 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30730 continue;
30732 /* Don't change integer DW_OP_convert after e.g. floating
30733 point typed stack entry. */
30734 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30735 keep = loc->dw_loc_next;
30736 break;
30738 break;
30739 default:
30740 break;
30742 return true;
30745 /* Helper function of resolve_addr. DIE had DW_AT_location of
30746 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30747 and DW_OP_addr couldn't be resolved. resolve_addr has already
30748 removed the DW_AT_location attribute. This function attempts to
30749 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30750 to it or DW_AT_const_value attribute, if possible. */
30752 static void
30753 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30755 if (!VAR_P (decl)
30756 || lookup_decl_die (decl) != die
30757 || DECL_EXTERNAL (decl)
30758 || !TREE_STATIC (decl)
30759 || DECL_INITIAL (decl) == NULL_TREE
30760 || DECL_P (DECL_INITIAL (decl))
30761 || get_AT (die, DW_AT_const_value))
30762 return;
30764 tree init = DECL_INITIAL (decl);
30765 HOST_WIDE_INT offset = 0;
30766 /* For variables that have been optimized away and thus
30767 don't have a memory location, see if we can emit
30768 DW_AT_const_value instead. */
30769 if (tree_add_const_value_attribute (die, init))
30770 return;
30771 if (dwarf_strict && dwarf_version < 5)
30772 return;
30773 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30774 and ADDR_EXPR refers to a decl that has DW_AT_location or
30775 DW_AT_const_value (but isn't addressable, otherwise
30776 resolving the original DW_OP_addr wouldn't fail), see if
30777 we can add DW_OP_implicit_pointer. */
30778 STRIP_NOPS (init);
30779 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30780 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30782 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30783 init = TREE_OPERAND (init, 0);
30784 STRIP_NOPS (init);
30786 if (TREE_CODE (init) != ADDR_EXPR)
30787 return;
30788 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30789 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30790 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30791 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30792 && TREE_OPERAND (init, 0) != decl))
30794 dw_die_ref ref;
30795 dw_loc_descr_ref l;
30797 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30799 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30800 if (!rtl)
30801 return;
30802 decl = SYMBOL_REF_DECL (rtl);
30804 else
30805 decl = TREE_OPERAND (init, 0);
30806 ref = lookup_decl_die (decl);
30807 if (ref == NULL
30808 || (!get_AT (ref, DW_AT_location)
30809 && !get_AT (ref, DW_AT_const_value)))
30810 return;
30811 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30812 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30813 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30814 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30815 add_AT_loc (die, DW_AT_location, l);
30819 /* Return NULL if l is a DWARF expression, or first op that is not
30820 valid DWARF expression. */
30822 static dw_loc_descr_ref
30823 non_dwarf_expression (dw_loc_descr_ref l)
30825 while (l)
30827 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30828 return l;
30829 switch (l->dw_loc_opc)
30831 case DW_OP_regx:
30832 case DW_OP_implicit_value:
30833 case DW_OP_stack_value:
30834 case DW_OP_implicit_pointer:
30835 case DW_OP_GNU_implicit_pointer:
30836 case DW_OP_GNU_parameter_ref:
30837 case DW_OP_piece:
30838 case DW_OP_bit_piece:
30839 return l;
30840 default:
30841 break;
30843 l = l->dw_loc_next;
30845 return NULL;
30848 /* Return adjusted copy of EXPR:
30849 If it is empty DWARF expression, return it.
30850 If it is valid non-empty DWARF expression,
30851 return copy of EXPR with DW_OP_deref appended to it.
30852 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30853 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30854 If it is DWARF expression followed by DW_OP_stack_value, return
30855 copy of the DWARF expression without anything appended.
30856 Otherwise, return NULL. */
30858 static dw_loc_descr_ref
30859 copy_deref_exprloc (dw_loc_descr_ref expr)
30861 dw_loc_descr_ref tail = NULL;
30863 if (expr == NULL)
30864 return NULL;
30866 dw_loc_descr_ref l = non_dwarf_expression (expr);
30867 if (l && l->dw_loc_next)
30868 return NULL;
30870 if (l)
30872 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30873 tail = new_loc_descr ((enum dwarf_location_atom)
30874 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30875 0, 0);
30876 else
30877 switch (l->dw_loc_opc)
30879 case DW_OP_regx:
30880 tail = new_loc_descr (DW_OP_bregx,
30881 l->dw_loc_oprnd1.v.val_unsigned, 0);
30882 break;
30883 case DW_OP_stack_value:
30884 break;
30885 default:
30886 return NULL;
30889 else
30890 tail = new_loc_descr (DW_OP_deref, 0, 0);
30892 dw_loc_descr_ref ret = NULL, *p = &ret;
30893 while (expr != l)
30895 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30896 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30897 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30898 p = &(*p)->dw_loc_next;
30899 expr = expr->dw_loc_next;
30901 *p = tail;
30902 return ret;
30905 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30906 reference to a variable or argument, adjust it if needed and return:
30907 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30908 attribute if present should be removed
30909 0 keep the attribute perhaps with minor modifications, no need to rescan
30910 1 if the attribute has been successfully adjusted. */
30912 static int
30913 optimize_string_length (dw_attr_node *a)
30915 dw_loc_descr_ref l = AT_loc (a), lv;
30916 dw_die_ref die;
30917 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30919 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30920 die = lookup_decl_die (decl);
30921 if (die)
30923 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30924 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30925 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30927 else
30928 return -1;
30930 else
30931 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30933 /* DWARF5 allows reference class, so we can then reference the DIE.
30934 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30935 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30937 a->dw_attr_val.val_class = dw_val_class_die_ref;
30938 a->dw_attr_val.val_entry = NULL;
30939 a->dw_attr_val.v.val_die_ref.die = die;
30940 a->dw_attr_val.v.val_die_ref.external = 0;
30941 return 0;
30944 dw_attr_node *av = get_AT (die, DW_AT_location);
30945 dw_loc_list_ref d;
30946 bool non_dwarf_expr = false;
30948 if (av == NULL)
30949 return dwarf_strict ? -1 : 0;
30950 switch (AT_class (av))
30952 case dw_val_class_loc_list:
30953 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30954 if (d->expr && non_dwarf_expression (d->expr))
30955 non_dwarf_expr = true;
30956 break;
30957 case dw_val_class_view_list:
30958 gcc_unreachable ();
30959 case dw_val_class_loc:
30960 lv = AT_loc (av);
30961 if (lv == NULL)
30962 return dwarf_strict ? -1 : 0;
30963 if (non_dwarf_expression (lv))
30964 non_dwarf_expr = true;
30965 break;
30966 default:
30967 return dwarf_strict ? -1 : 0;
30970 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30971 into DW_OP_call4 or DW_OP_GNU_variable_value into
30972 DW_OP_call4 DW_OP_deref, do so. */
30973 if (!non_dwarf_expr
30974 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30976 l->dw_loc_opc = DW_OP_call4;
30977 if (l->dw_loc_next)
30978 l->dw_loc_next = NULL;
30979 else
30980 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30981 return 0;
30984 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30985 copy over the DW_AT_location attribute from die to a. */
30986 if (l->dw_loc_next != NULL)
30988 a->dw_attr_val = av->dw_attr_val;
30989 return 1;
30992 dw_loc_list_ref list, *p;
30993 switch (AT_class (av))
30995 case dw_val_class_loc_list:
30996 p = &list;
30997 list = NULL;
30998 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
31000 lv = copy_deref_exprloc (d->expr);
31001 if (lv)
31003 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31004 p = &(*p)->dw_loc_next;
31006 else if (!dwarf_strict && d->expr)
31007 return 0;
31009 if (list == NULL)
31010 return dwarf_strict ? -1 : 0;
31011 a->dw_attr_val.val_class = dw_val_class_loc_list;
31012 gen_llsym (list);
31013 *AT_loc_list_ptr (a) = list;
31014 return 1;
31015 case dw_val_class_loc:
31016 lv = copy_deref_exprloc (AT_loc (av));
31017 if (lv == NULL)
31018 return dwarf_strict ? -1 : 0;
31019 a->dw_attr_val.v.val_loc = lv;
31020 return 1;
31021 default:
31022 gcc_unreachable ();
31026 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31027 an address in .rodata section if the string literal is emitted there,
31028 or remove the containing location list or replace DW_AT_const_value
31029 with DW_AT_location and empty location expression, if it isn't found
31030 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31031 to something that has been emitted in the current CU. */
31033 static void
31034 resolve_addr (dw_die_ref die)
31036 dw_die_ref c;
31037 dw_attr_node *a;
31038 dw_loc_list_ref *curr, *start, loc;
31039 unsigned ix;
31040 bool remove_AT_byte_size = false;
31042 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31043 switch (AT_class (a))
31045 case dw_val_class_loc_list:
31046 start = curr = AT_loc_list_ptr (a);
31047 loc = *curr;
31048 gcc_assert (loc);
31049 /* The same list can be referenced more than once. See if we have
31050 already recorded the result from a previous pass. */
31051 if (loc->replaced)
31052 *curr = loc->dw_loc_next;
31053 else if (!loc->resolved_addr)
31055 /* As things stand, we do not expect or allow one die to
31056 reference a suffix of another die's location list chain.
31057 References must be identical or completely separate.
31058 There is therefore no need to cache the result of this
31059 pass on any list other than the first; doing so
31060 would lead to unnecessary writes. */
31061 while (*curr)
31063 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31064 if (!resolve_addr_in_expr (a, (*curr)->expr))
31066 dw_loc_list_ref next = (*curr)->dw_loc_next;
31067 dw_loc_descr_ref l = (*curr)->expr;
31069 if (next && (*curr)->ll_symbol)
31071 gcc_assert (!next->ll_symbol);
31072 next->ll_symbol = (*curr)->ll_symbol;
31073 next->vl_symbol = (*curr)->vl_symbol;
31075 if (dwarf_split_debug_info)
31076 remove_loc_list_addr_table_entries (l);
31077 *curr = next;
31079 else
31081 mark_base_types ((*curr)->expr);
31082 curr = &(*curr)->dw_loc_next;
31085 if (loc == *start)
31086 loc->resolved_addr = 1;
31087 else
31089 loc->replaced = 1;
31090 loc->dw_loc_next = *start;
31093 if (!*start)
31095 remove_AT (die, a->dw_attr);
31096 ix--;
31098 break;
31099 case dw_val_class_view_list:
31101 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31102 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31103 dw_val_node *llnode
31104 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31105 /* If we no longer have a loclist, or it no longer needs
31106 views, drop this attribute. */
31107 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31109 remove_AT (die, a->dw_attr);
31110 ix--;
31112 break;
31114 case dw_val_class_loc:
31116 dw_loc_descr_ref l = AT_loc (a);
31117 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31118 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31119 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31120 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31121 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31122 with DW_FORM_ref referencing the same DIE as
31123 DW_OP_GNU_variable_value used to reference. */
31124 if (a->dw_attr == DW_AT_string_length
31125 && l
31126 && l->dw_loc_opc == DW_OP_GNU_variable_value
31127 && (l->dw_loc_next == NULL
31128 || (l->dw_loc_next->dw_loc_next == NULL
31129 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31131 switch (optimize_string_length (a))
31133 case -1:
31134 remove_AT (die, a->dw_attr);
31135 ix--;
31136 /* If we drop DW_AT_string_length, we need to drop also
31137 DW_AT_{string_length_,}byte_size. */
31138 remove_AT_byte_size = true;
31139 continue;
31140 default:
31141 break;
31142 case 1:
31143 /* Even if we keep the optimized DW_AT_string_length,
31144 it might have changed AT_class, so process it again. */
31145 ix--;
31146 continue;
31149 /* For -gdwarf-2 don't attempt to optimize
31150 DW_AT_data_member_location containing
31151 DW_OP_plus_uconst - older consumers might
31152 rely on it being that op instead of a more complex,
31153 but shorter, location description. */
31154 if ((dwarf_version > 2
31155 || a->dw_attr != DW_AT_data_member_location
31156 || l == NULL
31157 || l->dw_loc_opc != DW_OP_plus_uconst
31158 || l->dw_loc_next != NULL)
31159 && !resolve_addr_in_expr (a, l))
31161 if (dwarf_split_debug_info)
31162 remove_loc_list_addr_table_entries (l);
31163 if (l != NULL
31164 && l->dw_loc_next == NULL
31165 && l->dw_loc_opc == DW_OP_addr
31166 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31167 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31168 && a->dw_attr == DW_AT_location)
31170 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31171 remove_AT (die, a->dw_attr);
31172 ix--;
31173 optimize_location_into_implicit_ptr (die, decl);
31174 break;
31176 if (a->dw_attr == DW_AT_string_length)
31177 /* If we drop DW_AT_string_length, we need to drop also
31178 DW_AT_{string_length_,}byte_size. */
31179 remove_AT_byte_size = true;
31180 remove_AT (die, a->dw_attr);
31181 ix--;
31183 else
31184 mark_base_types (l);
31186 break;
31187 case dw_val_class_addr:
31188 if (a->dw_attr == DW_AT_const_value
31189 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31191 if (AT_index (a) != NOT_INDEXED)
31192 remove_addr_table_entry (a->dw_attr_val.val_entry);
31193 remove_AT (die, a->dw_attr);
31194 ix--;
31196 if ((die->die_tag == DW_TAG_call_site
31197 && a->dw_attr == DW_AT_call_origin)
31198 || (die->die_tag == DW_TAG_GNU_call_site
31199 && a->dw_attr == DW_AT_abstract_origin))
31201 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31202 dw_die_ref tdie = lookup_decl_die (tdecl);
31203 dw_die_ref cdie;
31204 if (tdie == NULL
31205 && DECL_EXTERNAL (tdecl)
31206 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31207 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31209 dw_die_ref pdie = cdie;
31210 /* Make sure we don't add these DIEs into type units.
31211 We could emit skeleton DIEs for context (namespaces,
31212 outer structs/classes) and a skeleton DIE for the
31213 innermost context with DW_AT_signature pointing to the
31214 type unit. See PR78835. */
31215 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31216 pdie = pdie->die_parent;
31217 if (pdie == NULL)
31219 /* Creating a full DIE for tdecl is overly expensive and
31220 at this point even wrong when in the LTO phase
31221 as it can end up generating new type DIEs we didn't
31222 output and thus optimize_external_refs will crash. */
31223 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31224 add_AT_flag (tdie, DW_AT_external, 1);
31225 add_AT_flag (tdie, DW_AT_declaration, 1);
31226 add_linkage_attr (tdie, tdecl);
31227 add_name_and_src_coords_attributes (tdie, tdecl, true);
31228 equate_decl_number_to_die (tdecl, tdie);
31231 if (tdie)
31233 a->dw_attr_val.val_class = dw_val_class_die_ref;
31234 a->dw_attr_val.v.val_die_ref.die = tdie;
31235 a->dw_attr_val.v.val_die_ref.external = 0;
31237 else
31239 if (AT_index (a) != NOT_INDEXED)
31240 remove_addr_table_entry (a->dw_attr_val.val_entry);
31241 remove_AT (die, a->dw_attr);
31242 ix--;
31245 break;
31246 default:
31247 break;
31250 if (remove_AT_byte_size)
31251 remove_AT (die, dwarf_version >= 5
31252 ? DW_AT_string_length_byte_size
31253 : DW_AT_byte_size);
31255 FOR_EACH_CHILD (die, c, resolve_addr (c));
31258 /* Helper routines for optimize_location_lists.
31259 This pass tries to share identical local lists in .debug_loc
31260 section. */
31262 /* Iteratively hash operands of LOC opcode into HSTATE. */
31264 static void
31265 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31267 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31268 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31270 switch (loc->dw_loc_opc)
31272 case DW_OP_const4u:
31273 case DW_OP_const8u:
31274 if (loc->dtprel)
31275 goto hash_addr;
31276 /* FALLTHRU */
31277 case DW_OP_const1u:
31278 case DW_OP_const1s:
31279 case DW_OP_const2u:
31280 case DW_OP_const2s:
31281 case DW_OP_const4s:
31282 case DW_OP_const8s:
31283 case DW_OP_constu:
31284 case DW_OP_consts:
31285 case DW_OP_pick:
31286 case DW_OP_plus_uconst:
31287 case DW_OP_breg0:
31288 case DW_OP_breg1:
31289 case DW_OP_breg2:
31290 case DW_OP_breg3:
31291 case DW_OP_breg4:
31292 case DW_OP_breg5:
31293 case DW_OP_breg6:
31294 case DW_OP_breg7:
31295 case DW_OP_breg8:
31296 case DW_OP_breg9:
31297 case DW_OP_breg10:
31298 case DW_OP_breg11:
31299 case DW_OP_breg12:
31300 case DW_OP_breg13:
31301 case DW_OP_breg14:
31302 case DW_OP_breg15:
31303 case DW_OP_breg16:
31304 case DW_OP_breg17:
31305 case DW_OP_breg18:
31306 case DW_OP_breg19:
31307 case DW_OP_breg20:
31308 case DW_OP_breg21:
31309 case DW_OP_breg22:
31310 case DW_OP_breg23:
31311 case DW_OP_breg24:
31312 case DW_OP_breg25:
31313 case DW_OP_breg26:
31314 case DW_OP_breg27:
31315 case DW_OP_breg28:
31316 case DW_OP_breg29:
31317 case DW_OP_breg30:
31318 case DW_OP_breg31:
31319 case DW_OP_regx:
31320 case DW_OP_fbreg:
31321 case DW_OP_piece:
31322 case DW_OP_deref_size:
31323 case DW_OP_xderef_size:
31324 hstate.add_object (val1->v.val_int);
31325 break;
31326 case DW_OP_skip:
31327 case DW_OP_bra:
31329 int offset;
31331 gcc_assert (val1->val_class == dw_val_class_loc);
31332 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31333 hstate.add_object (offset);
31335 break;
31336 case DW_OP_implicit_value:
31337 hstate.add_object (val1->v.val_unsigned);
31338 switch (val2->val_class)
31340 case dw_val_class_const:
31341 hstate.add_object (val2->v.val_int);
31342 break;
31343 case dw_val_class_vec:
31345 unsigned int elt_size = val2->v.val_vec.elt_size;
31346 unsigned int len = val2->v.val_vec.length;
31348 hstate.add_int (elt_size);
31349 hstate.add_int (len);
31350 hstate.add (val2->v.val_vec.array, len * elt_size);
31352 break;
31353 case dw_val_class_const_double:
31354 hstate.add_object (val2->v.val_double.low);
31355 hstate.add_object (val2->v.val_double.high);
31356 break;
31357 case dw_val_class_wide_int:
31358 hstate.add (val2->v.val_wide->get_val (),
31359 get_full_len (*val2->v.val_wide)
31360 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31361 break;
31362 case dw_val_class_addr:
31363 inchash::add_rtx (val2->v.val_addr, hstate);
31364 break;
31365 default:
31366 gcc_unreachable ();
31368 break;
31369 case DW_OP_bregx:
31370 case DW_OP_bit_piece:
31371 hstate.add_object (val1->v.val_int);
31372 hstate.add_object (val2->v.val_int);
31373 break;
31374 case DW_OP_addr:
31375 hash_addr:
31376 if (loc->dtprel)
31378 unsigned char dtprel = 0xd1;
31379 hstate.add_object (dtprel);
31381 inchash::add_rtx (val1->v.val_addr, hstate);
31382 break;
31383 case DW_OP_GNU_addr_index:
31384 case DW_OP_addrx:
31385 case DW_OP_GNU_const_index:
31386 case DW_OP_constx:
31388 if (loc->dtprel)
31390 unsigned char dtprel = 0xd1;
31391 hstate.add_object (dtprel);
31393 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31395 break;
31396 case DW_OP_implicit_pointer:
31397 case DW_OP_GNU_implicit_pointer:
31398 hstate.add_int (val2->v.val_int);
31399 break;
31400 case DW_OP_entry_value:
31401 case DW_OP_GNU_entry_value:
31402 hstate.add_object (val1->v.val_loc);
31403 break;
31404 case DW_OP_regval_type:
31405 case DW_OP_deref_type:
31406 case DW_OP_GNU_regval_type:
31407 case DW_OP_GNU_deref_type:
31409 unsigned int byte_size
31410 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31411 unsigned int encoding
31412 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31413 hstate.add_object (val1->v.val_int);
31414 hstate.add_object (byte_size);
31415 hstate.add_object (encoding);
31417 break;
31418 case DW_OP_convert:
31419 case DW_OP_reinterpret:
31420 case DW_OP_GNU_convert:
31421 case DW_OP_GNU_reinterpret:
31422 if (val1->val_class == dw_val_class_unsigned_const)
31424 hstate.add_object (val1->v.val_unsigned);
31425 break;
31427 /* FALLTHRU */
31428 case DW_OP_const_type:
31429 case DW_OP_GNU_const_type:
31431 unsigned int byte_size
31432 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31433 unsigned int encoding
31434 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31435 hstate.add_object (byte_size);
31436 hstate.add_object (encoding);
31437 if (loc->dw_loc_opc != DW_OP_const_type
31438 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31439 break;
31440 hstate.add_object (val2->val_class);
31441 switch (val2->val_class)
31443 case dw_val_class_const:
31444 hstate.add_object (val2->v.val_int);
31445 break;
31446 case dw_val_class_vec:
31448 unsigned int elt_size = val2->v.val_vec.elt_size;
31449 unsigned int len = val2->v.val_vec.length;
31451 hstate.add_object (elt_size);
31452 hstate.add_object (len);
31453 hstate.add (val2->v.val_vec.array, len * elt_size);
31455 break;
31456 case dw_val_class_const_double:
31457 hstate.add_object (val2->v.val_double.low);
31458 hstate.add_object (val2->v.val_double.high);
31459 break;
31460 case dw_val_class_wide_int:
31461 hstate.add (val2->v.val_wide->get_val (),
31462 get_full_len (*val2->v.val_wide)
31463 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31464 break;
31465 default:
31466 gcc_unreachable ();
31469 break;
31471 default:
31472 /* Other codes have no operands. */
31473 break;
31477 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31479 static inline void
31480 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31482 dw_loc_descr_ref l;
31483 bool sizes_computed = false;
31484 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31485 size_of_locs (loc);
31487 for (l = loc; l != NULL; l = l->dw_loc_next)
31489 enum dwarf_location_atom opc = l->dw_loc_opc;
31490 hstate.add_object (opc);
31491 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31493 size_of_locs (loc);
31494 sizes_computed = true;
31496 hash_loc_operands (l, hstate);
31500 /* Compute hash of the whole location list LIST_HEAD. */
31502 static inline void
31503 hash_loc_list (dw_loc_list_ref list_head)
31505 dw_loc_list_ref curr = list_head;
31506 inchash::hash hstate;
31508 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31510 hstate.add (curr->begin, strlen (curr->begin) + 1);
31511 hstate.add (curr->end, strlen (curr->end) + 1);
31512 hstate.add_object (curr->vbegin);
31513 hstate.add_object (curr->vend);
31514 if (curr->section)
31515 hstate.add (curr->section, strlen (curr->section) + 1);
31516 hash_locs (curr->expr, hstate);
31518 list_head->hash = hstate.end ();
31521 /* Return true if X and Y opcodes have the same operands. */
31523 static inline bool
31524 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31526 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31527 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31528 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31529 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31531 switch (x->dw_loc_opc)
31533 case DW_OP_const4u:
31534 case DW_OP_const8u:
31535 if (x->dtprel)
31536 goto hash_addr;
31537 /* FALLTHRU */
31538 case DW_OP_const1u:
31539 case DW_OP_const1s:
31540 case DW_OP_const2u:
31541 case DW_OP_const2s:
31542 case DW_OP_const4s:
31543 case DW_OP_const8s:
31544 case DW_OP_constu:
31545 case DW_OP_consts:
31546 case DW_OP_pick:
31547 case DW_OP_plus_uconst:
31548 case DW_OP_breg0:
31549 case DW_OP_breg1:
31550 case DW_OP_breg2:
31551 case DW_OP_breg3:
31552 case DW_OP_breg4:
31553 case DW_OP_breg5:
31554 case DW_OP_breg6:
31555 case DW_OP_breg7:
31556 case DW_OP_breg8:
31557 case DW_OP_breg9:
31558 case DW_OP_breg10:
31559 case DW_OP_breg11:
31560 case DW_OP_breg12:
31561 case DW_OP_breg13:
31562 case DW_OP_breg14:
31563 case DW_OP_breg15:
31564 case DW_OP_breg16:
31565 case DW_OP_breg17:
31566 case DW_OP_breg18:
31567 case DW_OP_breg19:
31568 case DW_OP_breg20:
31569 case DW_OP_breg21:
31570 case DW_OP_breg22:
31571 case DW_OP_breg23:
31572 case DW_OP_breg24:
31573 case DW_OP_breg25:
31574 case DW_OP_breg26:
31575 case DW_OP_breg27:
31576 case DW_OP_breg28:
31577 case DW_OP_breg29:
31578 case DW_OP_breg30:
31579 case DW_OP_breg31:
31580 case DW_OP_regx:
31581 case DW_OP_fbreg:
31582 case DW_OP_piece:
31583 case DW_OP_deref_size:
31584 case DW_OP_xderef_size:
31585 return valx1->v.val_int == valy1->v.val_int;
31586 case DW_OP_skip:
31587 case DW_OP_bra:
31588 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31589 can cause irrelevant differences in dw_loc_addr. */
31590 gcc_assert (valx1->val_class == dw_val_class_loc
31591 && valy1->val_class == dw_val_class_loc
31592 && (dwarf_split_debug_info
31593 || x->dw_loc_addr == y->dw_loc_addr));
31594 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31595 case DW_OP_implicit_value:
31596 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31597 || valx2->val_class != valy2->val_class)
31598 return false;
31599 switch (valx2->val_class)
31601 case dw_val_class_const:
31602 return valx2->v.val_int == valy2->v.val_int;
31603 case dw_val_class_vec:
31604 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31605 && valx2->v.val_vec.length == valy2->v.val_vec.length
31606 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31607 valx2->v.val_vec.elt_size
31608 * valx2->v.val_vec.length) == 0;
31609 case dw_val_class_const_double:
31610 return valx2->v.val_double.low == valy2->v.val_double.low
31611 && valx2->v.val_double.high == valy2->v.val_double.high;
31612 case dw_val_class_wide_int:
31613 return *valx2->v.val_wide == *valy2->v.val_wide;
31614 case dw_val_class_addr:
31615 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31616 default:
31617 gcc_unreachable ();
31619 case DW_OP_bregx:
31620 case DW_OP_bit_piece:
31621 return valx1->v.val_int == valy1->v.val_int
31622 && valx2->v.val_int == valy2->v.val_int;
31623 case DW_OP_addr:
31624 hash_addr:
31625 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31626 case DW_OP_GNU_addr_index:
31627 case DW_OP_addrx:
31628 case DW_OP_GNU_const_index:
31629 case DW_OP_constx:
31631 rtx ax1 = valx1->val_entry->addr.rtl;
31632 rtx ay1 = valy1->val_entry->addr.rtl;
31633 return rtx_equal_p (ax1, ay1);
31635 case DW_OP_implicit_pointer:
31636 case DW_OP_GNU_implicit_pointer:
31637 return valx1->val_class == dw_val_class_die_ref
31638 && valx1->val_class == valy1->val_class
31639 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31640 && valx2->v.val_int == valy2->v.val_int;
31641 case DW_OP_entry_value:
31642 case DW_OP_GNU_entry_value:
31643 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31644 case DW_OP_const_type:
31645 case DW_OP_GNU_const_type:
31646 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31647 || valx2->val_class != valy2->val_class)
31648 return false;
31649 switch (valx2->val_class)
31651 case dw_val_class_const:
31652 return valx2->v.val_int == valy2->v.val_int;
31653 case dw_val_class_vec:
31654 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31655 && valx2->v.val_vec.length == valy2->v.val_vec.length
31656 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31657 valx2->v.val_vec.elt_size
31658 * valx2->v.val_vec.length) == 0;
31659 case dw_val_class_const_double:
31660 return valx2->v.val_double.low == valy2->v.val_double.low
31661 && valx2->v.val_double.high == valy2->v.val_double.high;
31662 case dw_val_class_wide_int:
31663 return *valx2->v.val_wide == *valy2->v.val_wide;
31664 default:
31665 gcc_unreachable ();
31667 case DW_OP_regval_type:
31668 case DW_OP_deref_type:
31669 case DW_OP_GNU_regval_type:
31670 case DW_OP_GNU_deref_type:
31671 return valx1->v.val_int == valy1->v.val_int
31672 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31673 case DW_OP_convert:
31674 case DW_OP_reinterpret:
31675 case DW_OP_GNU_convert:
31676 case DW_OP_GNU_reinterpret:
31677 if (valx1->val_class != valy1->val_class)
31678 return false;
31679 if (valx1->val_class == dw_val_class_unsigned_const)
31680 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31681 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31682 case DW_OP_GNU_parameter_ref:
31683 return valx1->val_class == dw_val_class_die_ref
31684 && valx1->val_class == valy1->val_class
31685 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31686 default:
31687 /* Other codes have no operands. */
31688 return true;
31692 /* Return true if DWARF location expressions X and Y are the same. */
31694 static inline bool
31695 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31697 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31698 if (x->dw_loc_opc != y->dw_loc_opc
31699 || x->dtprel != y->dtprel
31700 || !compare_loc_operands (x, y))
31701 break;
31702 return x == NULL && y == NULL;
31705 /* Hashtable helpers. */
31707 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31709 static inline hashval_t hash (const dw_loc_list_struct *);
31710 static inline bool equal (const dw_loc_list_struct *,
31711 const dw_loc_list_struct *);
31714 /* Return precomputed hash of location list X. */
31716 inline hashval_t
31717 loc_list_hasher::hash (const dw_loc_list_struct *x)
31719 return x->hash;
31722 /* Return true if location lists A and B are the same. */
31724 inline bool
31725 loc_list_hasher::equal (const dw_loc_list_struct *a,
31726 const dw_loc_list_struct *b)
31728 if (a == b)
31729 return 1;
31730 if (a->hash != b->hash)
31731 return 0;
31732 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31733 if (strcmp (a->begin, b->begin) != 0
31734 || strcmp (a->end, b->end) != 0
31735 || (a->section == NULL) != (b->section == NULL)
31736 || (a->section && strcmp (a->section, b->section) != 0)
31737 || a->vbegin != b->vbegin || a->vend != b->vend
31738 || !compare_locs (a->expr, b->expr))
31739 break;
31740 return a == NULL && b == NULL;
31743 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31746 /* Recursively optimize location lists referenced from DIE
31747 children and share them whenever possible. */
31749 static void
31750 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31752 dw_die_ref c;
31753 dw_attr_node *a;
31754 unsigned ix;
31755 dw_loc_list_struct **slot;
31756 bool drop_locviews = false;
31757 bool has_locviews = false;
31759 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31760 if (AT_class (a) == dw_val_class_loc_list)
31762 dw_loc_list_ref list = AT_loc_list (a);
31763 /* TODO: perform some optimizations here, before hashing
31764 it and storing into the hash table. */
31765 hash_loc_list (list);
31766 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31767 if (*slot == NULL)
31769 *slot = list;
31770 if (loc_list_has_views (list))
31771 gcc_assert (list->vl_symbol);
31772 else if (list->vl_symbol)
31774 drop_locviews = true;
31775 list->vl_symbol = NULL;
31778 else
31780 if (list->vl_symbol && !(*slot)->vl_symbol)
31781 drop_locviews = true;
31782 a->dw_attr_val.v.val_loc_list = *slot;
31785 else if (AT_class (a) == dw_val_class_view_list)
31787 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31788 has_locviews = true;
31792 if (drop_locviews && has_locviews)
31793 remove_AT (die, DW_AT_GNU_locviews);
31795 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31799 /* Recursively assign each location list a unique index into the debug_addr
31800 section. */
31802 static void
31803 index_location_lists (dw_die_ref die)
31805 dw_die_ref c;
31806 dw_attr_node *a;
31807 unsigned ix;
31809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31810 if (AT_class (a) == dw_val_class_loc_list)
31812 dw_loc_list_ref list = AT_loc_list (a);
31813 dw_loc_list_ref curr;
31814 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31816 /* Don't index an entry that has already been indexed
31817 or won't be output. Make sure skip_loc_list_entry doesn't
31818 call size_of_locs, because that might cause circular dependency,
31819 index_location_lists requiring address table indexes to be
31820 computed, but adding new indexes through add_addr_table_entry
31821 and address table index computation requiring no new additions
31822 to the hash table. In the rare case of DWARF[234] >= 64KB
31823 location expression, we'll just waste unused address table entry
31824 for it. */
31825 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31826 continue;
31828 curr->begin_entry
31829 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31830 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31831 curr->end_entry
31832 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31836 FOR_EACH_CHILD (die, c, index_location_lists (c));
31839 /* Optimize location lists referenced from DIE
31840 children and share them whenever possible. */
31842 static void
31843 optimize_location_lists (dw_die_ref die)
31845 loc_list_hash_type htab (500);
31846 optimize_location_lists_1 (die, &htab);
31849 /* Traverse the limbo die list, and add parent/child links. The only
31850 dies without parents that should be here are concrete instances of
31851 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31852 For concrete instances, we can get the parent die from the abstract
31853 instance. */
31855 static void
31856 flush_limbo_die_list (void)
31858 limbo_die_node *node;
31860 /* get_context_die calls force_decl_die, which can put new DIEs on the
31861 limbo list in LTO mode when nested functions are put in a different
31862 partition than that of their parent function. */
31863 while ((node = limbo_die_list))
31865 dw_die_ref die = node->die;
31866 limbo_die_list = node->next;
31868 if (die->die_parent == NULL)
31870 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31872 if (origin && origin->die_parent)
31873 add_child_die (origin->die_parent, die);
31874 else if (is_cu_die (die))
31876 else if (seen_error ())
31877 /* It's OK to be confused by errors in the input. */
31878 add_child_die (comp_unit_die (), die);
31879 else
31881 /* In certain situations, the lexical block containing a
31882 nested function can be optimized away, which results
31883 in the nested function die being orphaned. Likewise
31884 with the return type of that nested function. Force
31885 this to be a child of the containing function.
31887 It may happen that even the containing function got fully
31888 inlined and optimized out. In that case we are lost and
31889 assign the empty child. This should not be big issue as
31890 the function is likely unreachable too. */
31891 gcc_assert (node->created_for);
31893 if (DECL_P (node->created_for))
31894 origin = get_context_die (DECL_CONTEXT (node->created_for));
31895 else if (TYPE_P (node->created_for))
31896 origin = scope_die_for (node->created_for, comp_unit_die ());
31897 else
31898 origin = comp_unit_die ();
31900 add_child_die (origin, die);
31906 /* Reset DIEs so we can output them again. */
31908 static void
31909 reset_dies (dw_die_ref die)
31911 dw_die_ref c;
31913 /* Remove stuff we re-generate. */
31914 die->die_mark = 0;
31915 die->die_offset = 0;
31916 die->die_abbrev = 0;
31917 remove_AT (die, DW_AT_sibling);
31919 FOR_EACH_CHILD (die, c, reset_dies (c));
31922 /* reset_indirect_string removed the references coming from DW_AT_name
31923 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31924 .debug_line_str strings again. */
31926 static void
31927 adjust_name_comp_dir (dw_die_ref die)
31929 for (int i = 0; i < 2; i++)
31931 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31932 dw_attr_node *a = get_AT (die, attr_kind);
31933 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31934 continue;
31936 if (!debug_line_str_hash)
31937 debug_line_str_hash
31938 = hash_table<indirect_string_hasher>::create_ggc (10);
31940 struct indirect_string_node *node
31941 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31942 debug_line_str_hash);
31943 set_indirect_string (node);
31944 node->form = DW_FORM_line_strp;
31945 a->dw_attr_val.v.val_str = node;
31949 /* Output stuff that dwarf requires at the end of every file,
31950 and generate the DWARF-2 debugging info. */
31952 static void
31953 dwarf2out_finish (const char *filename)
31955 comdat_type_node *ctnode;
31956 dw_die_ref main_comp_unit_die;
31957 unsigned char checksum[16];
31958 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31960 /* Generate CTF/BTF debug info. */
31961 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
31962 || btf_debuginfo_p ()) && lang_GNU_C ())
31963 ctf_debug_finish (filename);
31965 /* Skip emitting DWARF if not required. */
31966 if (!dwarf_debuginfo_p ())
31967 return;
31969 /* Flush out any latecomers to the limbo party. */
31970 flush_limbo_die_list ();
31972 if (inline_entry_data_table)
31973 gcc_assert (inline_entry_data_table->is_empty ());
31975 if (flag_checking)
31977 verify_die (comp_unit_die ());
31978 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31979 verify_die (node->die);
31982 /* We shouldn't have any symbols with delayed asm names for
31983 DIEs generated after early finish. */
31984 gcc_assert (deferred_asm_name == NULL);
31986 gen_remaining_tmpl_value_param_die_attribute ();
31988 if (flag_generate_lto || flag_generate_offload)
31990 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31992 /* Prune stuff so that dwarf2out_finish runs successfully
31993 for the fat part of the object. */
31994 reset_dies (comp_unit_die ());
31995 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31996 reset_dies (node->die);
31998 hash_table<comdat_type_hasher> comdat_type_table (100);
31999 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32001 comdat_type_node **slot
32002 = comdat_type_table.find_slot (ctnode, INSERT);
32004 /* Don't reset types twice. */
32005 if (*slot != HTAB_EMPTY_ENTRY)
32006 continue;
32008 /* Remove the pointer to the line table. */
32009 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32011 if (debug_info_level >= DINFO_LEVEL_TERSE)
32012 reset_dies (ctnode->root_die);
32014 *slot = ctnode;
32017 /* Reset die CU symbol so we don't output it twice. */
32018 comp_unit_die ()->die_id.die_symbol = NULL;
32020 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32021 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32022 if (have_macinfo)
32023 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32025 /* Remove indirect string decisions. */
32026 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32027 if (debug_line_str_hash)
32029 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32030 debug_line_str_hash = NULL;
32031 if (asm_outputs_debug_line_str ())
32033 adjust_name_comp_dir (comp_unit_die ());
32034 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32035 adjust_name_comp_dir (node->die);
32040 #if ENABLE_ASSERT_CHECKING
32042 dw_die_ref die = comp_unit_die (), c;
32043 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32045 #endif
32046 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32047 resolve_addr (ctnode->root_die);
32048 resolve_addr (comp_unit_die ());
32049 move_marked_base_types ();
32051 if (dump_file)
32053 fprintf (dump_file, "DWARF for %s\n", filename);
32054 print_die (comp_unit_die (), dump_file);
32057 /* Initialize sections and labels used for actual assembler output. */
32058 unsigned generation = init_sections_and_labels (false);
32060 /* Traverse the DIE's and add sibling attributes to those DIE's that
32061 have children. */
32062 add_sibling_attributes (comp_unit_die ());
32063 limbo_die_node *node;
32064 for (node = cu_die_list; node; node = node->next)
32065 add_sibling_attributes (node->die);
32066 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32067 add_sibling_attributes (ctnode->root_die);
32069 /* When splitting DWARF info, we put some attributes in the
32070 skeleton compile_unit DIE that remains in the .o, while
32071 most attributes go in the DWO compile_unit_die. */
32072 if (dwarf_split_debug_info)
32074 limbo_die_node *cu;
32075 main_comp_unit_die = gen_compile_unit_die (NULL);
32076 if (dwarf_version >= 5)
32077 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32078 cu = limbo_die_list;
32079 gcc_assert (cu->die == main_comp_unit_die);
32080 limbo_die_list = limbo_die_list->next;
32081 cu->next = cu_die_list;
32082 cu_die_list = cu;
32084 else
32085 main_comp_unit_die = comp_unit_die ();
32087 /* Output a terminator label for the .text section. */
32088 switch_to_section (text_section);
32089 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32090 if (cold_text_section)
32092 switch_to_section (cold_text_section);
32093 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32096 /* We can only use the low/high_pc attributes if all of the code was
32097 in .text. */
32098 if ((!have_multiple_function_sections
32099 && vec_safe_length (switch_text_ranges) < 2)
32100 || (dwarf_version < 3 && dwarf_strict))
32102 const char *end_label = text_end_label;
32103 if (vec_safe_length (switch_text_ranges) == 1)
32104 end_label = (*switch_text_ranges)[0];
32105 /* Don't add if the CU has no associated code. */
32106 if (switch_text_ranges)
32107 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32108 end_label, true);
32110 else
32112 unsigned fde_idx;
32113 dw_fde_ref fde;
32114 bool range_list_added = false;
32115 if (switch_text_ranges)
32117 const char *prev_loc = text_section_label;
32118 const char *loc;
32119 unsigned idx;
32121 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32122 if (prev_loc)
32124 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32125 loc, &range_list_added, true);
32126 prev_loc = NULL;
32128 else
32129 prev_loc = loc;
32131 if (prev_loc)
32132 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32133 text_end_label, &range_list_added, true);
32136 if (switch_cold_ranges)
32138 const char *prev_loc = cold_text_section_label;
32139 const char *loc;
32140 unsigned idx;
32142 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32143 if (prev_loc)
32145 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32146 loc, &range_list_added, true);
32147 prev_loc = NULL;
32149 else
32150 prev_loc = loc;
32152 if (prev_loc)
32153 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32154 cold_end_label, &range_list_added, true);
32157 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32159 if (fde->ignored_debug)
32160 continue;
32161 if (!fde->in_std_section)
32162 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32163 fde->dw_fde_end, &range_list_added,
32164 true);
32165 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32166 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32167 fde->dw_fde_second_end, &range_list_added,
32168 true);
32171 if (range_list_added)
32173 /* We need to give .debug_loc and .debug_ranges an appropriate
32174 "base address". Use zero so that these addresses become
32175 absolute. Historically, we've emitted the unexpected
32176 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32177 Emit both to give time for other tools to adapt. */
32178 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32179 if (! dwarf_strict && dwarf_version < 4)
32180 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32182 add_ranges (NULL);
32183 have_multiple_function_sections = true;
32187 /* AIX Assembler inserts the length, so adjust the reference to match the
32188 offset expected by debuggers. */
32189 strcpy (dl_section_ref, debug_line_section_label);
32190 if (XCOFF_DEBUGGING_INFO)
32191 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32193 if (debug_info_level >= DINFO_LEVEL_TERSE)
32194 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32195 dl_section_ref);
32197 if (have_macinfo)
32198 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32199 macinfo_section_label);
32201 if (dwarf_split_debug_info)
32203 if (have_location_lists)
32205 /* Since we generate the loclists in the split DWARF .dwo
32206 file itself, we don't need to generate a loclists_base
32207 attribute for the split compile unit DIE. That attribute
32208 (and using relocatable sec_offset FORMs) isn't allowed
32209 for a split compile unit. Only if the .debug_loclists
32210 section was in the main file, would we need to generate a
32211 loclists_base attribute here (for the full or skeleton
32212 unit DIE). */
32214 /* optimize_location_lists calculates the size of the lists,
32215 so index them first, and assign indices to the entries.
32216 Although optimize_location_lists will remove entries from
32217 the table, it only does so for duplicates, and therefore
32218 only reduces ref_counts to 1. */
32219 index_location_lists (comp_unit_die ());
32222 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32223 index_rnglists ();
32225 if (addr_index_table != NULL)
32227 unsigned int index = 0;
32228 addr_index_table
32229 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32230 (&index);
32234 loc_list_idx = 0;
32235 if (have_location_lists)
32237 optimize_location_lists (comp_unit_die ());
32238 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32239 if (dwarf_version >= 5 && dwarf_split_debug_info)
32240 assign_location_list_indexes (comp_unit_die ());
32243 save_macinfo_strings ();
32245 if (dwarf_split_debug_info)
32247 unsigned int index = 0;
32249 /* Add attributes common to skeleton compile_units and
32250 type_units. Because these attributes include strings, it
32251 must be done before freezing the string table. Top-level
32252 skeleton die attrs are added when the skeleton type unit is
32253 created, so ensure it is created by this point. */
32254 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32255 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32258 /* Output all of the compilation units. We put the main one last so that
32259 the offsets are available to output_pubnames. */
32260 for (node = cu_die_list; node; node = node->next)
32261 output_comp_unit (node->die, 0, NULL);
32263 hash_table<comdat_type_hasher> comdat_type_table (100);
32264 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32266 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32268 /* Don't output duplicate types. */
32269 if (*slot != HTAB_EMPTY_ENTRY)
32270 continue;
32272 /* Add a pointer to the line table for the main compilation unit
32273 so that the debugger can make sense of DW_AT_decl_file
32274 attributes. */
32275 if (debug_info_level >= DINFO_LEVEL_TERSE)
32276 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32277 (!dwarf_split_debug_info
32278 ? dl_section_ref
32279 : debug_skeleton_line_section_label));
32281 output_comdat_type_unit (ctnode, false);
32282 *slot = ctnode;
32285 if (dwarf_split_debug_info)
32287 int mark;
32288 struct md5_ctx ctx;
32290 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32291 md5_init_ctx (&ctx);
32292 mark = 0;
32293 die_checksum (comp_unit_die (), &ctx, &mark);
32294 unmark_all_dies (comp_unit_die ());
32295 md5_finish_ctx (&ctx, checksum);
32297 if (dwarf_version < 5)
32299 /* Use the first 8 bytes of the checksum as the dwo_id,
32300 and add it to both comp-unit DIEs. */
32301 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32302 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32305 /* Add the base offset of the ranges table to the skeleton
32306 comp-unit DIE. */
32307 if (!vec_safe_is_empty (ranges_table))
32309 if (dwarf_version < 5)
32310 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32311 ranges_section_label);
32314 output_addr_table ();
32317 /* Output the main compilation unit if non-empty or if .debug_macinfo
32318 or .debug_macro will be emitted. */
32319 output_comp_unit (comp_unit_die (), have_macinfo,
32320 dwarf_split_debug_info ? checksum : NULL);
32322 if (dwarf_split_debug_info && info_section_emitted)
32323 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32325 /* Output the abbreviation table. */
32326 if (vec_safe_length (abbrev_die_table) != 1)
32328 switch_to_section (debug_abbrev_section);
32329 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32330 output_abbrev_section ();
32333 /* Output location list section if necessary. */
32334 if (have_location_lists)
32336 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32337 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32338 /* Output the location lists info. */
32339 switch_to_section (debug_loc_section);
32340 if (dwarf_version >= 5)
32342 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32343 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32344 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32345 dw2_asm_output_data (4, 0xffffffff,
32346 "Initial length escape value indicating "
32347 "64-bit DWARF extension");
32348 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32349 "Length of Location Lists");
32350 ASM_OUTPUT_LABEL (asm_out_file, l1);
32351 output_dwarf_version ();
32352 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32353 dw2_asm_output_data (1, 0, "Segment Size");
32354 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32355 "Offset Entry Count");
32357 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32358 if (dwarf_version >= 5 && dwarf_split_debug_info)
32360 unsigned int save_loc_list_idx = loc_list_idx;
32361 loc_list_idx = 0;
32362 output_loclists_offsets (comp_unit_die ());
32363 gcc_assert (save_loc_list_idx == loc_list_idx);
32365 output_location_lists (comp_unit_die ());
32366 if (dwarf_version >= 5)
32367 ASM_OUTPUT_LABEL (asm_out_file, l2);
32370 output_pubtables ();
32372 /* Output the address range information if a CU (.debug_info section)
32373 was emitted. We output an empty table even if we had no functions
32374 to put in it. This because the consumer has no way to tell the
32375 difference between an empty table that we omitted and failure to
32376 generate a table that would have contained data. */
32377 if (info_section_emitted)
32379 switch_to_section (debug_aranges_section);
32380 output_aranges ();
32383 /* Output ranges section if necessary. */
32384 if (!vec_safe_is_empty (ranges_table))
32386 if (dwarf_version >= 5)
32388 if (dwarf_split_debug_info)
32390 /* We don't know right now whether there are any
32391 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32392 Depending on into which of those two belongs the first
32393 ranges_table entry, emit that section first and that
32394 output_rnglists call will return true if the other kind of
32395 ranges needs to be emitted as well. */
32396 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32397 if (output_rnglists (generation, dwo))
32398 output_rnglists (generation, !dwo);
32400 else
32401 output_rnglists (generation, false);
32403 else
32404 output_ranges ();
32407 /* Have to end the macro section. */
32408 if (have_macinfo)
32410 switch_to_section (debug_macinfo_section);
32411 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32412 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32413 : debug_skeleton_line_section_label, false);
32414 dw2_asm_output_data (1, 0, "End compilation unit");
32417 /* Output the source line correspondence table. We must do this
32418 even if there is no line information. Otherwise, on an empty
32419 translation unit, we will generate a present, but empty,
32420 .debug_info section. IRIX 6.5 `nm' will then complain when
32421 examining the file. This is done late so that any filenames
32422 used by the debug_info section are marked as 'used'. */
32423 switch_to_section (debug_line_section);
32424 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32425 if (! output_asm_line_debug_info ())
32426 output_line_info (false);
32428 if (dwarf_split_debug_info && info_section_emitted)
32430 switch_to_section (debug_skeleton_line_section);
32431 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32432 output_line_info (true);
32435 /* If we emitted any indirect strings, output the string table too. */
32436 if (debug_str_hash || skeleton_debug_str_hash)
32437 output_indirect_strings ();
32438 if (debug_line_str_hash)
32440 switch_to_section (debug_line_str_section);
32441 const enum dwarf_form form = DW_FORM_line_strp;
32442 debug_line_str_hash->traverse<enum dwarf_form,
32443 output_indirect_string> (form);
32446 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32447 symview_upper_bound = 0;
32448 if (zero_view_p)
32449 bitmap_clear (zero_view_p);
32452 /* Returns a hash value for X (which really is a variable_value_struct). */
32454 inline hashval_t
32455 variable_value_hasher::hash (variable_value_struct *x)
32457 return (hashval_t) x->decl_id;
32460 /* Return nonzero if decl_id of variable_value_struct X is the same as
32461 UID of decl Y. */
32463 inline bool
32464 variable_value_hasher::equal (variable_value_struct *x, tree y)
32466 return x->decl_id == DECL_UID (y);
32469 /* Helper function for resolve_variable_value, handle
32470 DW_OP_GNU_variable_value in one location expression.
32471 Return true if exprloc has been changed into loclist. */
32473 static bool
32474 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32476 dw_loc_descr_ref next;
32477 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32479 next = loc->dw_loc_next;
32480 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32481 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32482 continue;
32484 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32485 if (DECL_CONTEXT (decl) != current_function_decl)
32486 continue;
32488 dw_die_ref ref = lookup_decl_die (decl);
32489 if (ref)
32491 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32492 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32493 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32494 continue;
32496 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32497 if (l == NULL)
32498 continue;
32499 if (l->dw_loc_next)
32501 if (AT_class (a) != dw_val_class_loc)
32502 continue;
32503 switch (a->dw_attr)
32505 /* Following attributes allow both exprloc and loclist
32506 classes, so we can change them into a loclist. */
32507 case DW_AT_location:
32508 case DW_AT_string_length:
32509 case DW_AT_return_addr:
32510 case DW_AT_data_member_location:
32511 case DW_AT_frame_base:
32512 case DW_AT_segment:
32513 case DW_AT_static_link:
32514 case DW_AT_use_location:
32515 case DW_AT_vtable_elem_location:
32516 if (prev)
32518 prev->dw_loc_next = NULL;
32519 prepend_loc_descr_to_each (l, AT_loc (a));
32521 if (next)
32522 add_loc_descr_to_each (l, next);
32523 a->dw_attr_val.val_class = dw_val_class_loc_list;
32524 a->dw_attr_val.val_entry = NULL;
32525 a->dw_attr_val.v.val_loc_list = l;
32526 have_location_lists = true;
32527 return true;
32528 /* Following attributes allow both exprloc and reference,
32529 so if the whole expression is DW_OP_GNU_variable_value alone
32530 we could transform it into reference. */
32531 case DW_AT_byte_size:
32532 case DW_AT_bit_size:
32533 case DW_AT_lower_bound:
32534 case DW_AT_upper_bound:
32535 case DW_AT_bit_stride:
32536 case DW_AT_count:
32537 case DW_AT_allocated:
32538 case DW_AT_associated:
32539 case DW_AT_byte_stride:
32540 if (prev == NULL && next == NULL)
32541 break;
32542 /* FALLTHRU */
32543 default:
32544 if (dwarf_strict)
32545 continue;
32546 break;
32548 /* Create DW_TAG_variable that we can refer to. */
32549 gen_decl_die (decl, NULL_TREE, NULL,
32550 lookup_decl_die (current_function_decl));
32551 ref = lookup_decl_die (decl);
32552 if (ref)
32554 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32555 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32556 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32558 continue;
32560 if (prev)
32562 prev->dw_loc_next = l->expr;
32563 add_loc_descr (&prev->dw_loc_next, next);
32564 free_loc_descr (loc, NULL);
32565 next = prev->dw_loc_next;
32567 else
32569 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32570 add_loc_descr (&loc, next);
32571 next = loc;
32573 loc = prev;
32575 return false;
32578 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32580 static void
32581 resolve_variable_value (dw_die_ref die)
32583 dw_attr_node *a;
32584 dw_loc_list_ref loc;
32585 unsigned ix;
32587 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32588 switch (AT_class (a))
32590 case dw_val_class_loc:
32591 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32592 break;
32593 /* FALLTHRU */
32594 case dw_val_class_loc_list:
32595 loc = AT_loc_list (a);
32596 gcc_assert (loc);
32597 for (; loc; loc = loc->dw_loc_next)
32598 resolve_variable_value_in_expr (a, loc->expr);
32599 break;
32600 default:
32601 break;
32605 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32606 temporaries in the current function. */
32608 static void
32609 resolve_variable_values (void)
32611 if (!variable_value_hash || !current_function_decl)
32612 return;
32614 struct variable_value_struct *node
32615 = variable_value_hash->find_with_hash (current_function_decl,
32616 DECL_UID (current_function_decl));
32618 if (node == NULL)
32619 return;
32621 unsigned int i;
32622 dw_die_ref die;
32623 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32624 resolve_variable_value (die);
32627 /* Helper function for note_variable_value, handle one location
32628 expression. */
32630 static void
32631 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32633 for (; loc; loc = loc->dw_loc_next)
32634 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32635 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32637 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32638 dw_die_ref ref = lookup_decl_die (decl);
32639 if (! ref && (flag_generate_lto || flag_generate_offload))
32641 /* ??? This is somewhat a hack because we do not create DIEs
32642 for variables not in BLOCK trees early but when generating
32643 early LTO output we need the dw_val_class_decl_ref to be
32644 fully resolved. For fat LTO objects we'd also like to
32645 undo this after LTO dwarf output. */
32646 gcc_assert (DECL_CONTEXT (decl));
32647 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32648 gcc_assert (ctx != NULL);
32649 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32650 ref = lookup_decl_die (decl);
32651 gcc_assert (ref != NULL);
32653 if (ref)
32655 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32656 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32657 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32658 continue;
32660 if (VAR_P (decl)
32661 && DECL_CONTEXT (decl)
32662 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32663 && lookup_decl_die (DECL_CONTEXT (decl)))
32665 if (!variable_value_hash)
32666 variable_value_hash
32667 = hash_table<variable_value_hasher>::create_ggc (10);
32669 tree fndecl = DECL_CONTEXT (decl);
32670 struct variable_value_struct *node;
32671 struct variable_value_struct **slot
32672 = variable_value_hash->find_slot_with_hash (fndecl,
32673 DECL_UID (fndecl),
32674 INSERT);
32675 if (*slot == NULL)
32677 node = ggc_cleared_alloc<variable_value_struct> ();
32678 node->decl_id = DECL_UID (fndecl);
32679 *slot = node;
32681 else
32682 node = *slot;
32684 vec_safe_push (node->dies, die);
32689 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32690 with dw_val_class_decl_ref operand. */
32692 static void
32693 note_variable_value (dw_die_ref die)
32695 dw_die_ref c;
32696 dw_attr_node *a;
32697 dw_loc_list_ref loc;
32698 unsigned ix;
32700 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32701 switch (AT_class (a))
32703 case dw_val_class_loc_list:
32704 loc = AT_loc_list (a);
32705 gcc_assert (loc);
32706 if (!loc->noted_variable_value)
32708 loc->noted_variable_value = 1;
32709 for (; loc; loc = loc->dw_loc_next)
32710 note_variable_value_in_expr (die, loc->expr);
32712 break;
32713 case dw_val_class_loc:
32714 note_variable_value_in_expr (die, AT_loc (a));
32715 break;
32716 default:
32717 break;
32720 /* Mark children. */
32721 FOR_EACH_CHILD (die, c, note_variable_value (c));
32724 /* Process DWARF dies for CTF generation. */
32726 static void
32727 ctf_debug_do_cu (dw_die_ref die)
32729 dw_die_ref c;
32731 if (!ctf_do_die (die))
32732 return;
32734 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32737 /* Perform any cleanups needed after the early debug generation pass
32738 has run. */
32740 static void
32741 dwarf2out_early_finish (const char *filename)
32743 comdat_type_node *ctnode;
32744 set_early_dwarf s;
32745 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32747 /* PCH might result in DW_AT_producer string being restored from the
32748 header compilation, so always fill it with empty string initially
32749 and overwrite only here. */
32750 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32752 if (dwarf_record_gcc_switches)
32753 producer_string = gen_producer_string (lang_hooks.name,
32754 save_decoded_options,
32755 save_decoded_options_count);
32756 else
32757 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32759 producer->dw_attr_val.v.val_str->refcount--;
32760 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32762 /* Add the name for the main input file now. We delayed this from
32763 dwarf2out_init to avoid complications with PCH. */
32764 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32765 add_comp_dir_attribute (comp_unit_die ());
32767 /* With LTO early dwarf was really finished at compile-time, so make
32768 sure to adjust the phase after annotating the LTRANS CU DIE. */
32769 if (in_lto_p)
32771 early_dwarf_finished = true;
32772 if (dump_file)
32774 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32775 print_die (comp_unit_die (), dump_file);
32777 return;
32780 /* Walk through the list of incomplete types again, trying once more to
32781 emit full debugging info for them. */
32782 retry_incomplete_types ();
32784 gen_scheduled_generic_parms_dies ();
32785 gen_remaining_tmpl_value_param_die_attribute ();
32787 /* The point here is to flush out the limbo list so that it is empty
32788 and we don't need to stream it for LTO. */
32789 flush_limbo_die_list ();
32791 /* Add DW_AT_linkage_name for all deferred DIEs. */
32792 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32794 tree decl = node->created_for;
32795 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32796 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32797 ended up in deferred_asm_name before we knew it was
32798 constant and never written to disk. */
32799 && DECL_ASSEMBLER_NAME (decl))
32801 add_linkage_attr (node->die, decl);
32802 move_linkage_attr (node->die);
32805 deferred_asm_name = NULL;
32807 if (flag_eliminate_unused_debug_types)
32808 prune_unused_types ();
32810 /* Generate separate COMDAT sections for type DIEs. */
32811 if (use_debug_types)
32813 break_out_comdat_types (comp_unit_die ());
32815 /* Each new type_unit DIE was added to the limbo die list when created.
32816 Since these have all been added to comdat_type_list, clear the
32817 limbo die list. */
32818 limbo_die_list = NULL;
32820 /* For each new comdat type unit, copy declarations for incomplete
32821 types to make the new unit self-contained (i.e., no direct
32822 references to the main compile unit). */
32823 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32824 copy_decls_for_unworthy_types (ctnode->root_die);
32825 copy_decls_for_unworthy_types (comp_unit_die ());
32827 /* In the process of copying declarations from one unit to another,
32828 we may have left some declarations behind that are no longer
32829 referenced. Prune them. */
32830 prune_unused_types ();
32833 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32834 with dw_val_class_decl_ref operand. */
32835 note_variable_value (comp_unit_die ());
32836 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32837 note_variable_value (node->die);
32838 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32839 note_variable_value (ctnode->root_die);
32840 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32841 note_variable_value (node->die);
32843 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32844 both the main_cu and all skeleton TUs. Making this call unconditional
32845 would end up either adding a second copy of the AT_pubnames attribute, or
32846 requiring a special case in add_top_level_skeleton_die_attrs. */
32847 if (!dwarf_split_debug_info)
32848 add_AT_pubnames (comp_unit_die ());
32850 /* The early debug phase is now finished. */
32851 early_dwarf_finished = true;
32852 if (dump_file)
32854 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32855 print_die (comp_unit_die (), dump_file);
32858 /* Generate CTF/BTF debug info. */
32859 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32860 || btf_debuginfo_p ()) && lang_GNU_C ())
32862 ctf_debug_init ();
32863 ctf_debug_do_cu (comp_unit_die ());
32864 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32865 ctf_debug_do_cu (node->die);
32866 /* Post process the debug data in the CTF container if necessary. */
32867 ctf_debug_init_postprocess (btf_debuginfo_p ());
32869 ctf_debug_early_finish (filename);
32872 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32873 if ((!flag_generate_lto && !flag_generate_offload)
32874 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32875 copy_lto_debug_sections operation of the simple object support in
32876 libiberty is not implemented for them yet. */
32877 || TARGET_PECOFF || TARGET_COFF)
32878 return;
32880 /* Now as we are going to output for LTO initialize sections and labels
32881 to the LTO variants. We don't need a random-seed postfix as other
32882 LTO sections as linking the LTO debug sections into one in a partial
32883 link is fine. */
32884 init_sections_and_labels (true);
32886 /* The output below is modeled after dwarf2out_finish with all
32887 location related output removed and some LTO specific changes.
32888 Some refactoring might make both smaller and easier to match up. */
32890 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32891 mark_base_types (ctnode->root_die);
32892 mark_base_types (comp_unit_die ());
32893 move_marked_base_types ();
32895 /* Traverse the DIE's and add sibling attributes to those DIE's
32896 that have children. */
32897 add_sibling_attributes (comp_unit_die ());
32898 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32899 add_sibling_attributes (node->die);
32900 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32901 add_sibling_attributes (ctnode->root_die);
32903 /* AIX Assembler inserts the length, so adjust the reference to match the
32904 offset expected by debuggers. */
32905 strcpy (dl_section_ref, debug_line_section_label);
32906 if (XCOFF_DEBUGGING_INFO)
32907 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32909 if (debug_info_level >= DINFO_LEVEL_TERSE)
32910 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32912 if (have_macinfo)
32913 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32914 macinfo_section_label);
32916 save_macinfo_strings ();
32918 if (dwarf_split_debug_info)
32920 unsigned int index = 0;
32921 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32924 /* Output all of the compilation units. We put the main one last so that
32925 the offsets are available to output_pubnames. */
32926 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32927 output_comp_unit (node->die, 0, NULL);
32929 hash_table<comdat_type_hasher> comdat_type_table (100);
32930 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32932 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32934 /* Don't output duplicate types. */
32935 if (*slot != HTAB_EMPTY_ENTRY)
32936 continue;
32938 /* Add a pointer to the line table for the main compilation unit
32939 so that the debugger can make sense of DW_AT_decl_file
32940 attributes. */
32941 if (debug_info_level >= DINFO_LEVEL_TERSE)
32942 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32943 (!dwarf_split_debug_info
32944 ? debug_line_section_label
32945 : debug_skeleton_line_section_label));
32947 output_comdat_type_unit (ctnode, true);
32948 *slot = ctnode;
32951 /* Stick a unique symbol to the main debuginfo section. */
32952 compute_comp_unit_symbol (comp_unit_die ());
32954 /* Output the main compilation unit. We always need it if only for
32955 the CU symbol. */
32956 output_comp_unit (comp_unit_die (), true, NULL);
32958 /* Output the abbreviation table. */
32959 if (vec_safe_length (abbrev_die_table) != 1)
32961 switch_to_section (debug_abbrev_section);
32962 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32963 output_abbrev_section ();
32966 /* Have to end the macro section. */
32967 if (have_macinfo)
32969 /* We have to save macinfo state if we need to output it again
32970 for the FAT part of the object. */
32971 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32972 if (flag_fat_lto_objects)
32973 macinfo_table = macinfo_table->copy ();
32975 switch_to_section (debug_macinfo_section);
32976 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32977 output_macinfo (debug_line_section_label, true);
32978 dw2_asm_output_data (1, 0, "End compilation unit");
32980 if (flag_fat_lto_objects)
32982 vec_free (macinfo_table);
32983 macinfo_table = saved_macinfo_table;
32987 /* Emit a skeleton debug_line section. */
32988 switch_to_section (debug_line_section);
32989 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32990 output_line_info (true);
32992 /* If we emitted any indirect strings, output the string table too. */
32993 if (debug_str_hash || skeleton_debug_str_hash)
32994 output_indirect_strings ();
32995 if (debug_line_str_hash)
32997 switch_to_section (debug_line_str_section);
32998 const enum dwarf_form form = DW_FORM_line_strp;
32999 debug_line_str_hash->traverse<enum dwarf_form,
33000 output_indirect_string> (form);
33003 /* Switch back to the text section. */
33004 switch_to_section (text_section);
33007 /* Reset all state within dwarf2out.c so that we can rerun the compiler
33008 within the same process. For use by toplev::finalize. */
33010 void
33011 dwarf2out_c_finalize (void)
33013 last_var_location_insn = NULL;
33014 cached_next_real_insn = NULL;
33015 used_rtx_array = NULL;
33016 incomplete_types = NULL;
33017 debug_info_section = NULL;
33018 debug_skeleton_info_section = NULL;
33019 debug_abbrev_section = NULL;
33020 debug_skeleton_abbrev_section = NULL;
33021 debug_aranges_section = NULL;
33022 debug_addr_section = NULL;
33023 debug_macinfo_section = NULL;
33024 debug_line_section = NULL;
33025 debug_skeleton_line_section = NULL;
33026 debug_loc_section = NULL;
33027 debug_pubnames_section = NULL;
33028 debug_pubtypes_section = NULL;
33029 debug_str_section = NULL;
33030 debug_line_str_section = NULL;
33031 debug_str_dwo_section = NULL;
33032 debug_str_offsets_section = NULL;
33033 debug_ranges_section = NULL;
33034 debug_ranges_dwo_section = NULL;
33035 debug_frame_section = NULL;
33036 fde_vec = NULL;
33037 debug_str_hash = NULL;
33038 debug_line_str_hash = NULL;
33039 skeleton_debug_str_hash = NULL;
33040 dw2_string_counter = 0;
33041 have_multiple_function_sections = false;
33042 in_text_section_p = false;
33043 cold_text_section = NULL;
33044 last_text_label = NULL;
33045 last_cold_label = NULL;
33046 switch_text_ranges = NULL;
33047 switch_cold_ranges = NULL;
33048 current_unit_personality = NULL;
33050 early_dwarf = false;
33051 early_dwarf_finished = false;
33053 next_die_offset = 0;
33054 single_comp_unit_die = NULL;
33055 comdat_type_list = NULL;
33056 limbo_die_list = NULL;
33057 file_table = NULL;
33058 decl_die_table = NULL;
33059 common_block_die_table = NULL;
33060 decl_loc_table = NULL;
33061 call_arg_locations = NULL;
33062 call_arg_loc_last = NULL;
33063 call_site_count = -1;
33064 tail_call_site_count = -1;
33065 cached_dw_loc_list_table = NULL;
33066 abbrev_die_table = NULL;
33067 delete dwarf_proc_stack_usage_map;
33068 dwarf_proc_stack_usage_map = NULL;
33069 line_info_label_num = 0;
33070 cur_line_info_table = NULL;
33071 text_section_line_info = NULL;
33072 cold_text_section_line_info = NULL;
33073 separate_line_info = NULL;
33074 info_section_emitted = false;
33075 pubname_table = NULL;
33076 pubtype_table = NULL;
33077 macinfo_table = NULL;
33078 ranges_table = NULL;
33079 ranges_by_label = NULL;
33080 rnglist_idx = 0;
33081 have_location_lists = false;
33082 loclabel_num = 0;
33083 poc_label_num = 0;
33084 last_emitted_file = NULL;
33085 label_num = 0;
33086 tmpl_value_parm_die_table = NULL;
33087 generic_type_instances = NULL;
33088 frame_pointer_fb_offset = 0;
33089 frame_pointer_fb_offset_valid = false;
33090 base_types.release ();
33091 XDELETEVEC (producer_string);
33092 producer_string = NULL;
33093 output_line_info_generation = 0;
33094 init_sections_and_labels_generation = 0;
33097 #include "gt-dwarf2out.h"