Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob20f2c5da023f12baab13d4495c8c7d4e5de10005
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, 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 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
19651 offset_int object_offset_in_bits;
19652 offset_int object_offset_in_bytes;
19653 offset_int bitpos_int;
19654 tree type;
19655 tree field_size_tree;
19656 offset_int deepest_bitpos;
19657 offset_int field_size_in_bits;
19658 unsigned int type_align_in_bits;
19659 unsigned int decl_align_in_bits;
19660 offset_int type_size_in_bits;
19662 bitpos_int = wi::to_offset (bit_position (decl));
19663 type = field_type (decl);
19664 type_size_in_bits = offset_int_type_size_in_bits (type);
19665 type_align_in_bits = simple_type_align_in_bits (type);
19667 field_size_tree = DECL_SIZE (decl);
19669 /* The size could be unspecified if there was an error, or for
19670 a flexible array member. */
19671 if (!field_size_tree)
19672 field_size_tree = bitsize_zero_node;
19674 /* If the size of the field is not constant, use the type size. */
19675 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19676 field_size_in_bits = wi::to_offset (field_size_tree);
19677 else
19678 field_size_in_bits = type_size_in_bits;
19680 decl_align_in_bits = simple_decl_align_in_bits (decl);
19682 /* The GCC front-end doesn't make any attempt to keep track of the
19683 starting bit offset (relative to the start of the containing
19684 structure type) of the hypothetical "containing object" for a
19685 bit-field. Thus, when computing the byte offset value for the
19686 start of the "containing object" of a bit-field, we must deduce
19687 this information on our own. This can be rather tricky to do in
19688 some cases. For example, handling the following structure type
19689 definition when compiling for an i386/i486 target (which only
19690 aligns long long's to 32-bit boundaries) can be very tricky:
19692 struct S { int field1; long long field2:31; };
19694 Fortunately, there is a simple rule-of-thumb which can be used
19695 in such cases. When compiling for an i386/i486, GCC will
19696 allocate 8 bytes for the structure shown above. It decides to
19697 do this based upon one simple rule for bit-field allocation.
19698 GCC allocates each "containing object" for each bit-field at
19699 the first (i.e. lowest addressed) legitimate alignment boundary
19700 (based upon the required minimum alignment for the declared
19701 type of the field) which it can possibly use, subject to the
19702 condition that there is still enough available space remaining
19703 in the containing object (when allocated at the selected point)
19704 to fully accommodate all of the bits of the bit-field itself.
19706 This simple rule makes it obvious why GCC allocates 8 bytes for
19707 each object of the structure type shown above. When looking
19708 for a place to allocate the "containing object" for `field2',
19709 the compiler simply tries to allocate a 64-bit "containing
19710 object" at each successive 32-bit boundary (starting at zero)
19711 until it finds a place to allocate that 64- bit field such that
19712 at least 31 contiguous (and previously unallocated) bits remain
19713 within that selected 64 bit field. (As it turns out, for the
19714 example above, the compiler finds it is OK to allocate the
19715 "containing object" 64-bit field at bit-offset zero within the
19716 structure type.)
19718 Here we attempt to work backwards from the limited set of facts
19719 we're given, and we try to deduce from those facts, where GCC
19720 must have believed that the containing object started (within
19721 the structure type). The value we deduce is then used (by the
19722 callers of this routine) to generate DW_AT_location and
19723 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19724 the case of DW_AT_location, regular fields as well). */
19726 /* Figure out the bit-distance from the start of the structure to
19727 the "deepest" bit of the bit-field. */
19728 deepest_bitpos = bitpos_int + field_size_in_bits;
19730 /* This is the tricky part. Use some fancy footwork to deduce
19731 where the lowest addressed bit of the containing object must
19732 be. */
19733 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19735 /* Round up to type_align by default. This works best for
19736 bitfields. */
19737 object_offset_in_bits
19738 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19740 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19742 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19744 /* Round up to decl_align instead. */
19745 object_offset_in_bits
19746 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19749 object_offset_in_bytes
19750 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19751 if (ctx->variant_part_offset == NULL_TREE)
19753 *cst_offset = object_offset_in_bytes.to_shwi ();
19754 return NULL;
19756 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19758 else
19759 tree_result = byte_position (decl);
19761 if (ctx->variant_part_offset != NULL_TREE)
19762 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19763 ctx->variant_part_offset, tree_result);
19765 /* If the byte offset is a constant, it's simplier to handle a native
19766 constant rather than a DWARF expression. */
19767 if (TREE_CODE (tree_result) == INTEGER_CST)
19769 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19770 return NULL;
19773 struct loc_descr_context loc_ctx = {
19774 ctx->struct_type, /* context_type */
19775 NULL_TREE, /* base_decl */
19776 NULL, /* dpi */
19777 false, /* placeholder_arg */
19778 false, /* placeholder_seen */
19779 false /* strict_signedness */
19781 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19783 /* We want a DWARF expression: abort if we only have a location list with
19784 multiple elements. */
19785 if (!loc_result || !single_element_loc_list_p (loc_result))
19786 return NULL;
19787 else
19788 return loc_result->expr;
19791 /* The following routines define various Dwarf attributes and any data
19792 associated with them. */
19794 /* Add a location description attribute value to a DIE.
19796 This emits location attributes suitable for whole variables and
19797 whole parameters. Note that the location attributes for struct fields are
19798 generated by the routine `data_member_location_attribute' below. */
19800 static inline void
19801 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19802 dw_loc_list_ref descr)
19804 bool check_no_locviews = true;
19805 if (descr == 0)
19806 return;
19807 if (single_element_loc_list_p (descr))
19808 add_AT_loc (die, attr_kind, descr->expr);
19809 else
19811 add_AT_loc_list (die, attr_kind, descr);
19812 gcc_assert (descr->ll_symbol);
19813 if (attr_kind == DW_AT_location && descr->vl_symbol
19814 && dwarf2out_locviews_in_attribute ())
19816 add_AT_view_list (die, DW_AT_GNU_locviews);
19817 check_no_locviews = false;
19821 if (check_no_locviews)
19822 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19825 /* Add DW_AT_accessibility attribute to DIE if needed. */
19827 static void
19828 add_accessibility_attribute (dw_die_ref die, tree decl)
19830 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19831 children, otherwise the default is DW_ACCESS_public. In DWARF2
19832 the default has always been DW_ACCESS_public. */
19833 if (TREE_PROTECTED (decl))
19834 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19835 else if (TREE_PRIVATE (decl))
19837 if (dwarf_version == 2
19838 || die->die_parent == NULL
19839 || die->die_parent->die_tag != DW_TAG_class_type)
19840 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19842 else if (dwarf_version > 2
19843 && die->die_parent
19844 && die->die_parent->die_tag == DW_TAG_class_type)
19845 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19848 /* Attach the specialized form of location attribute used for data members of
19849 struct and union types. In the special case of a FIELD_DECL node which
19850 represents a bit-field, the "offset" part of this special location
19851 descriptor must indicate the distance in bytes from the lowest-addressed
19852 byte of the containing struct or union type to the lowest-addressed byte of
19853 the "containing object" for the bit-field. (See the `field_byte_offset'
19854 function above).
19856 For any given bit-field, the "containing object" is a hypothetical object
19857 (of some integral or enum type) within which the given bit-field lives. The
19858 type of this hypothetical "containing object" is always the same as the
19859 declared type of the individual bit-field itself (for GCC anyway... the
19860 DWARF spec doesn't actually mandate this). Note that it is the size (in
19861 bytes) of the hypothetical "containing object" which will be given in the
19862 DW_AT_byte_size attribute for this bit-field. (See the
19863 `byte_size_attribute' function below.) It is also used when calculating the
19864 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19865 function below.)
19867 CTX is required: see the comment for VLR_CONTEXT. */
19869 static void
19870 add_data_member_location_attribute (dw_die_ref die,
19871 tree decl,
19872 struct vlr_context *ctx)
19874 HOST_WIDE_INT offset;
19875 dw_loc_descr_ref loc_descr = 0;
19877 if (TREE_CODE (decl) == TREE_BINFO)
19879 /* We're working on the TAG_inheritance for a base class. */
19880 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19882 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19883 aren't at a fixed offset from all (sub)objects of the same
19884 type. We need to extract the appropriate offset from our
19885 vtable. The following dwarf expression means
19887 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19889 This is specific to the V3 ABI, of course. */
19891 dw_loc_descr_ref tmp;
19893 /* Make a copy of the object address. */
19894 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19895 add_loc_descr (&loc_descr, tmp);
19897 /* Extract the vtable address. */
19898 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19899 add_loc_descr (&loc_descr, tmp);
19901 /* Calculate the address of the offset. */
19902 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19903 gcc_assert (offset < 0);
19905 tmp = int_loc_descriptor (-offset);
19906 add_loc_descr (&loc_descr, tmp);
19907 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19908 add_loc_descr (&loc_descr, tmp);
19910 /* Extract the offset. */
19911 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19912 add_loc_descr (&loc_descr, tmp);
19914 /* Add it to the object address. */
19915 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19916 add_loc_descr (&loc_descr, tmp);
19918 else
19919 offset = tree_to_shwi (BINFO_OFFSET (decl));
19921 else
19923 loc_descr = field_byte_offset (decl, ctx, &offset);
19925 if (!loc_descr)
19928 /* If loc_descr is available, then we know the offset is dynamic. */
19929 else if (gnat_encodings == DWARF_GNAT_ENCODINGS_ALL)
19931 loc_descr = NULL;
19932 offset = 0;
19935 /* Data member location evaluation starts with the base address on the
19936 stack. Compute the field offset and add it to this base address. */
19937 else
19938 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19941 if (!loc_descr)
19943 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19944 e.g. GDB only added support to it in November 2016. For DWARF5
19945 we need newer debug info consumers anyway. We might change this
19946 to dwarf_version >= 4 once most consumers catched up. */
19947 if (dwarf_version >= 5
19948 && TREE_CODE (decl) == FIELD_DECL
19949 && DECL_BIT_FIELD_TYPE (decl)
19950 && (ctx->variant_part_offset == NULL_TREE
19951 || TREE_CODE (ctx->variant_part_offset) == INTEGER_CST))
19953 tree off = bit_position (decl);
19954 if (ctx->variant_part_offset)
19955 off = bit_from_pos (ctx->variant_part_offset, off);
19956 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19958 remove_AT (die, DW_AT_byte_size);
19959 remove_AT (die, DW_AT_bit_offset);
19960 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19961 return;
19964 if (dwarf_version > 2)
19966 /* Don't need to output a location expression, just the constant. */
19967 if (offset < 0)
19968 add_AT_int (die, DW_AT_data_member_location, offset);
19969 else
19970 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19971 return;
19973 else
19975 enum dwarf_location_atom op;
19977 /* The DWARF2 standard says that we should assume that the structure
19978 address is already on the stack, so we can specify a structure
19979 field address by using DW_OP_plus_uconst. */
19980 op = DW_OP_plus_uconst;
19981 loc_descr = new_loc_descr (op, offset, 0);
19985 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19988 /* Writes integer values to dw_vec_const array. */
19990 static void
19991 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19993 while (size != 0)
19995 *dest++ = val & 0xff;
19996 val >>= 8;
19997 --size;
20001 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
20003 static HOST_WIDE_INT
20004 extract_int (const unsigned char *src, unsigned int size)
20006 HOST_WIDE_INT val = 0;
20008 src += size;
20009 while (size != 0)
20011 val <<= 8;
20012 val |= *--src & 0xff;
20013 --size;
20015 return val;
20018 /* Writes wide_int values to dw_vec_const array. */
20020 static void
20021 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
20023 int i;
20025 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
20027 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
20028 return;
20031 /* We'd have to extend this code to support odd sizes. */
20032 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
20034 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
20036 if (WORDS_BIG_ENDIAN)
20037 for (i = n - 1; i >= 0; i--)
20039 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20040 dest += sizeof (HOST_WIDE_INT);
20042 else
20043 for (i = 0; i < n; i++)
20045 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
20046 dest += sizeof (HOST_WIDE_INT);
20050 /* Writes floating point values to dw_vec_const array. */
20052 static unsigned
20053 insert_float (const_rtx rtl, unsigned char *array)
20055 long val[4];
20056 int i;
20057 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20059 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
20061 /* real_to_target puts 32-bit pieces in each long. Pack them. */
20062 if (GET_MODE_SIZE (mode) < 4)
20064 gcc_assert (GET_MODE_SIZE (mode) == 2);
20065 insert_int (val[0], 2, array);
20066 return 2;
20069 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
20071 insert_int (val[i], 4, array);
20072 array += 4;
20074 return 4;
20077 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
20078 does not have a "location" either in memory or in a register. These
20079 things can arise in GNU C when a constant is passed as an actual parameter
20080 to an inlined function. They can also arise in C++ where declared
20081 constants do not necessarily get memory "homes". */
20083 static bool
20084 add_const_value_attribute (dw_die_ref die, rtx rtl)
20086 switch (GET_CODE (rtl))
20088 case CONST_INT:
20090 HOST_WIDE_INT val = INTVAL (rtl);
20092 if (val < 0)
20093 add_AT_int (die, DW_AT_const_value, val);
20094 else
20095 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
20097 return true;
20099 case CONST_WIDE_INT:
20101 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
20102 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
20103 (unsigned int) CONST_WIDE_INT_NUNITS (rtl)
20104 * HOST_BITS_PER_WIDE_INT);
20105 wide_int w = wide_int::from (w1, prec, UNSIGNED);
20106 add_AT_wide (die, DW_AT_const_value, w);
20108 return true;
20110 case CONST_DOUBLE:
20111 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
20112 floating-point constant. A CONST_DOUBLE is used whenever the
20113 constant requires more than one word in order to be adequately
20114 represented. */
20115 if (TARGET_SUPPORTS_WIDE_INT == 0
20116 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
20117 add_AT_double (die, DW_AT_const_value,
20118 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
20119 else
20121 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
20122 unsigned int length = GET_MODE_SIZE (mode);
20123 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
20124 unsigned int elt_size = insert_float (rtl, array);
20126 add_AT_vec (die, DW_AT_const_value, length / elt_size, elt_size,
20127 array);
20129 return true;
20131 case CONST_VECTOR:
20133 unsigned int length;
20134 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
20135 return false;
20137 machine_mode mode = GET_MODE (rtl);
20138 /* The combination of a length and byte elt_size doesn't extend
20139 naturally to boolean vectors, where several elements are packed
20140 into the same byte. */
20141 if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL)
20142 return false;
20144 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
20145 unsigned char *array
20146 = ggc_vec_alloc<unsigned char> (length * elt_size);
20147 unsigned int i;
20148 unsigned char *p;
20149 machine_mode imode = GET_MODE_INNER (mode);
20151 switch (GET_MODE_CLASS (mode))
20153 case MODE_VECTOR_INT:
20154 for (i = 0, p = array; i < length; i++, p += elt_size)
20156 rtx elt = CONST_VECTOR_ELT (rtl, i);
20157 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
20159 break;
20161 case MODE_VECTOR_FLOAT:
20162 for (i = 0, p = array; i < length; i++, p += elt_size)
20164 rtx elt = CONST_VECTOR_ELT (rtl, i);
20165 insert_float (elt, p);
20167 break;
20169 default:
20170 gcc_unreachable ();
20173 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
20175 return true;
20177 case CONST_STRING:
20178 if (dwarf_version >= 4 || !dwarf_strict)
20180 dw_loc_descr_ref loc_result;
20181 resolve_one_addr (&rtl);
20182 rtl_addr:
20183 loc_result = new_addr_loc_descr (rtl, dtprel_false);
20184 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
20185 add_AT_loc (die, DW_AT_location, loc_result);
20186 vec_safe_push (used_rtx_array, rtl);
20187 return true;
20189 return false;
20191 case CONST:
20192 if (CONSTANT_P (XEXP (rtl, 0)))
20193 return add_const_value_attribute (die, XEXP (rtl, 0));
20194 /* FALLTHROUGH */
20195 case SYMBOL_REF:
20196 if (!const_ok_for_output (rtl))
20197 return false;
20198 /* FALLTHROUGH */
20199 case LABEL_REF:
20200 if (dwarf_version >= 4 || !dwarf_strict)
20201 goto rtl_addr;
20202 return false;
20204 case PLUS:
20205 /* In cases where an inlined instance of an inline function is passed
20206 the address of an `auto' variable (which is local to the caller) we
20207 can get a situation where the DECL_RTL of the artificial local
20208 variable (for the inlining) which acts as a stand-in for the
20209 corresponding formal parameter (of the inline function) will look
20210 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
20211 exactly a compile-time constant expression, but it isn't the address
20212 of the (artificial) local variable either. Rather, it represents the
20213 *value* which the artificial local variable always has during its
20214 lifetime. We currently have no way to represent such quasi-constant
20215 values in Dwarf, so for now we just punt and generate nothing. */
20216 return false;
20218 case HIGH:
20219 case CONST_FIXED:
20220 case MINUS:
20221 case SIGN_EXTEND:
20222 case ZERO_EXTEND:
20223 case CONST_POLY_INT:
20224 return false;
20226 case MEM:
20227 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
20228 && MEM_READONLY_P (rtl)
20229 && GET_MODE (rtl) == BLKmode)
20231 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
20232 return true;
20234 return false;
20236 default:
20237 /* No other kinds of rtx should be possible here. */
20238 gcc_unreachable ();
20240 return false;
20243 /* Determine whether the evaluation of EXPR references any variables
20244 or functions which aren't otherwise used (and therefore may not be
20245 output). */
20246 static tree
20247 reference_to_unused (tree * tp, int * walk_subtrees,
20248 void * data ATTRIBUTE_UNUSED)
20250 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
20251 *walk_subtrees = 0;
20253 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
20254 && ! TREE_ASM_WRITTEN (*tp))
20255 return *tp;
20256 /* ??? The C++ FE emits debug information for using decls, so
20257 putting gcc_unreachable here falls over. See PR31899. For now
20258 be conservative. */
20259 else if (!symtab->global_info_ready && VAR_P (*tp))
20260 return *tp;
20261 else if (VAR_P (*tp))
20263 varpool_node *node = varpool_node::get (*tp);
20264 if (!node || !node->definition)
20265 return *tp;
20267 else if (TREE_CODE (*tp) == FUNCTION_DECL
20268 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
20270 /* The call graph machinery must have finished analyzing,
20271 optimizing and gimplifying the CU by now.
20272 So if *TP has no call graph node associated
20273 to it, it means *TP will not be emitted. */
20274 if (!symtab->global_info_ready || !cgraph_node::get (*tp))
20275 return *tp;
20277 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
20278 return *tp;
20280 return NULL_TREE;
20283 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
20284 for use in a later add_const_value_attribute call. */
20286 static rtx
20287 rtl_for_decl_init (tree init, tree type)
20289 rtx rtl = NULL_RTX;
20291 STRIP_NOPS (init);
20293 /* If a variable is initialized with a string constant without embedded
20294 zeros, build CONST_STRING. */
20295 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
20297 tree enttype = TREE_TYPE (type);
20298 tree domain = TYPE_DOMAIN (type);
20299 scalar_int_mode mode;
20301 if (is_int_mode (TYPE_MODE (enttype), &mode)
20302 && GET_MODE_SIZE (mode) == 1
20303 && domain
20304 && TYPE_MAX_VALUE (domain)
20305 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
20306 && integer_zerop (TYPE_MIN_VALUE (domain))
20307 && compare_tree_int (TYPE_MAX_VALUE (domain),
20308 TREE_STRING_LENGTH (init) - 1) == 0
20309 && ((size_t) TREE_STRING_LENGTH (init)
20310 == strlen (TREE_STRING_POINTER (init)) + 1))
20312 rtl = gen_rtx_CONST_STRING (VOIDmode,
20313 ggc_strdup (TREE_STRING_POINTER (init)));
20314 rtl = gen_rtx_MEM (BLKmode, rtl);
20315 MEM_READONLY_P (rtl) = 1;
20318 /* Other aggregates, and complex values, could be represented using
20319 CONCAT: FIXME! */
20320 else if (AGGREGATE_TYPE_P (type)
20321 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
20322 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
20323 || TREE_CODE (type) == COMPLEX_TYPE)
20325 /* Vectors only work if their mode is supported by the target.
20326 FIXME: generic vectors ought to work too. */
20327 else if (TREE_CODE (type) == VECTOR_TYPE
20328 && !VECTOR_MODE_P (TYPE_MODE (type)))
20330 /* If the initializer is something that we know will expand into an
20331 immediate RTL constant, expand it now. We must be careful not to
20332 reference variables which won't be output. */
20333 else if (initializer_constant_valid_p (init, type)
20334 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
20336 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
20337 possible. */
20338 if (TREE_CODE (type) == VECTOR_TYPE)
20339 switch (TREE_CODE (init))
20341 case VECTOR_CST:
20342 break;
20343 case CONSTRUCTOR:
20344 if (TREE_CONSTANT (init))
20346 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
20347 bool constant_p = true;
20348 tree value;
20349 unsigned HOST_WIDE_INT ix;
20351 /* Even when ctor is constant, it might contain non-*_CST
20352 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
20353 belong into VECTOR_CST nodes. */
20354 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
20355 if (!CONSTANT_CLASS_P (value))
20357 constant_p = false;
20358 break;
20361 if (constant_p)
20363 init = build_vector_from_ctor (type, elts);
20364 break;
20367 /* FALLTHRU */
20369 default:
20370 return NULL;
20373 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
20375 /* If expand_expr returns a MEM, it wasn't immediate. */
20376 gcc_assert (!rtl || !MEM_P (rtl));
20379 return rtl;
20382 /* Generate RTL for the variable DECL to represent its location. */
20384 static rtx
20385 rtl_for_decl_location (tree decl)
20387 rtx rtl;
20389 /* Here we have to decide where we are going to say the parameter "lives"
20390 (as far as the debugger is concerned). We only have a couple of
20391 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
20393 DECL_RTL normally indicates where the parameter lives during most of the
20394 activation of the function. If optimization is enabled however, this
20395 could be either NULL or else a pseudo-reg. Both of those cases indicate
20396 that the parameter doesn't really live anywhere (as far as the code
20397 generation parts of GCC are concerned) during most of the function's
20398 activation. That will happen (for example) if the parameter is never
20399 referenced within the function.
20401 We could just generate a location descriptor here for all non-NULL
20402 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
20403 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
20404 where DECL_RTL is NULL or is a pseudo-reg.
20406 Note however that we can only get away with using DECL_INCOMING_RTL as
20407 a backup substitute for DECL_RTL in certain limited cases. In cases
20408 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
20409 we can be sure that the parameter was passed using the same type as it is
20410 declared to have within the function, and that its DECL_INCOMING_RTL
20411 points us to a place where a value of that type is passed.
20413 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
20414 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
20415 because in these cases DECL_INCOMING_RTL points us to a value of some
20416 type which is *different* from the type of the parameter itself. Thus,
20417 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
20418 such cases, the debugger would end up (for example) trying to fetch a
20419 `float' from a place which actually contains the first part of a
20420 `double'. That would lead to really incorrect and confusing
20421 output at debug-time.
20423 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
20424 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
20425 are a couple of exceptions however. On little-endian machines we can
20426 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
20427 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
20428 an integral type that is smaller than TREE_TYPE (decl). These cases arise
20429 when (on a little-endian machine) a non-prototyped function has a
20430 parameter declared to be of type `short' or `char'. In such cases,
20431 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
20432 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
20433 passed `int' value. If the debugger then uses that address to fetch
20434 a `short' or a `char' (on a little-endian machine) the result will be
20435 the correct data, so we allow for such exceptional cases below.
20437 Note that our goal here is to describe the place where the given formal
20438 parameter lives during most of the function's activation (i.e. between the
20439 end of the prologue and the start of the epilogue). We'll do that as best
20440 as we can. Note however that if the given formal parameter is modified
20441 sometime during the execution of the function, then a stack backtrace (at
20442 debug-time) will show the function as having been called with the *new*
20443 value rather than the value which was originally passed in. This happens
20444 rarely enough that it is not a major problem, but it *is* a problem, and
20445 I'd like to fix it.
20447 A future version of dwarf2out.c may generate two additional attributes for
20448 any given DW_TAG_formal_parameter DIE which will describe the "passed
20449 type" and the "passed location" for the given formal parameter in addition
20450 to the attributes we now generate to indicate the "declared type" and the
20451 "active location" for each parameter. This additional set of attributes
20452 could be used by debuggers for stack backtraces. Separately, note that
20453 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
20454 This happens (for example) for inlined-instances of inline function formal
20455 parameters which are never referenced. This really shouldn't be
20456 happening. All PARM_DECL nodes should get valid non-NULL
20457 DECL_INCOMING_RTL values. FIXME. */
20459 /* Use DECL_RTL as the "location" unless we find something better. */
20460 rtl = DECL_RTL_IF_SET (decl);
20462 /* When generating abstract instances, ignore everything except
20463 constants, symbols living in memory, and symbols living in
20464 fixed registers. */
20465 if (! reload_completed)
20467 if (rtl
20468 && (CONSTANT_P (rtl)
20469 || (MEM_P (rtl)
20470 && CONSTANT_P (XEXP (rtl, 0)))
20471 || (REG_P (rtl)
20472 && VAR_P (decl)
20473 && TREE_STATIC (decl))))
20475 rtl = targetm.delegitimize_address (rtl);
20476 return rtl;
20478 rtl = NULL_RTX;
20480 else if (TREE_CODE (decl) == PARM_DECL)
20482 if (rtl == NULL_RTX
20483 || is_pseudo_reg (rtl)
20484 || (MEM_P (rtl)
20485 && is_pseudo_reg (XEXP (rtl, 0))
20486 && DECL_INCOMING_RTL (decl)
20487 && MEM_P (DECL_INCOMING_RTL (decl))
20488 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
20490 tree declared_type = TREE_TYPE (decl);
20491 tree passed_type = DECL_ARG_TYPE (decl);
20492 machine_mode dmode = TYPE_MODE (declared_type);
20493 machine_mode pmode = TYPE_MODE (passed_type);
20495 /* This decl represents a formal parameter which was optimized out.
20496 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
20497 all cases where (rtl == NULL_RTX) just below. */
20498 if (dmode == pmode)
20499 rtl = DECL_INCOMING_RTL (decl);
20500 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
20501 && SCALAR_INT_MODE_P (dmode)
20502 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
20503 && DECL_INCOMING_RTL (decl))
20505 rtx inc = DECL_INCOMING_RTL (decl);
20506 if (REG_P (inc))
20507 rtl = inc;
20508 else if (MEM_P (inc))
20510 if (BYTES_BIG_ENDIAN)
20511 rtl = adjust_address_nv (inc, dmode,
20512 GET_MODE_SIZE (pmode)
20513 - GET_MODE_SIZE (dmode));
20514 else
20515 rtl = inc;
20520 /* If the parm was passed in registers, but lives on the stack, then
20521 make a big endian correction if the mode of the type of the
20522 parameter is not the same as the mode of the rtl. */
20523 /* ??? This is the same series of checks that are made in dbxout.c before
20524 we reach the big endian correction code there. It isn't clear if all
20525 of these checks are necessary here, but keeping them all is the safe
20526 thing to do. */
20527 else if (MEM_P (rtl)
20528 && XEXP (rtl, 0) != const0_rtx
20529 && ! CONSTANT_P (XEXP (rtl, 0))
20530 /* Not passed in memory. */
20531 && !MEM_P (DECL_INCOMING_RTL (decl))
20532 /* Not passed by invisible reference. */
20533 && (!REG_P (XEXP (rtl, 0))
20534 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
20535 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
20536 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
20537 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
20538 #endif
20540 /* Big endian correction check. */
20541 && BYTES_BIG_ENDIAN
20542 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
20543 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
20544 UNITS_PER_WORD))
20546 machine_mode addr_mode = get_address_mode (rtl);
20547 poly_int64 offset = (UNITS_PER_WORD
20548 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
20550 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20551 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20554 else if (VAR_P (decl)
20555 && rtl
20556 && MEM_P (rtl)
20557 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
20559 machine_mode addr_mode = get_address_mode (rtl);
20560 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
20561 GET_MODE (rtl));
20563 /* If a variable is declared "register" yet is smaller than
20564 a register, then if we store the variable to memory, it
20565 looks like we're storing a register-sized value, when in
20566 fact we are not. We need to adjust the offset of the
20567 storage location to reflect the actual value's bytes,
20568 else gdb will not be able to display it. */
20569 if (maybe_ne (offset, 0))
20570 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
20571 plus_constant (addr_mode, XEXP (rtl, 0), offset));
20574 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
20575 and will have been substituted directly into all expressions that use it.
20576 C does not have such a concept, but C++ and other languages do. */
20577 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
20578 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
20580 if (rtl)
20581 rtl = targetm.delegitimize_address (rtl);
20583 /* If we don't look past the constant pool, we risk emitting a
20584 reference to a constant pool entry that isn't referenced from
20585 code, and thus is not emitted. */
20586 if (rtl)
20587 rtl = avoid_constant_pool_reference (rtl);
20589 /* Try harder to get a rtl. If this symbol ends up not being emitted
20590 in the current CU, resolve_addr will remove the expression referencing
20591 it. */
20592 if (rtl == NULL_RTX
20593 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
20594 && VAR_P (decl)
20595 && !DECL_EXTERNAL (decl)
20596 && TREE_STATIC (decl)
20597 && DECL_NAME (decl)
20598 && !DECL_HARD_REGISTER (decl)
20599 && DECL_MODE (decl) != VOIDmode)
20601 rtl = make_decl_rtl_for_debug (decl);
20602 if (!MEM_P (rtl)
20603 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
20604 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
20605 rtl = NULL_RTX;
20608 return rtl;
20611 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
20612 returned. If so, the decl for the COMMON block is returned, and the
20613 value is the offset into the common block for the symbol. */
20615 static tree
20616 fortran_common (tree decl, HOST_WIDE_INT *value)
20618 tree val_expr, cvar;
20619 machine_mode mode;
20620 poly_int64 bitsize, bitpos;
20621 tree offset;
20622 HOST_WIDE_INT cbitpos;
20623 int unsignedp, reversep, volatilep = 0;
20625 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
20626 it does not have a value (the offset into the common area), or if it
20627 is thread local (as opposed to global) then it isn't common, and shouldn't
20628 be handled as such. */
20629 if (!VAR_P (decl)
20630 || !TREE_STATIC (decl)
20631 || !DECL_HAS_VALUE_EXPR_P (decl)
20632 || !is_fortran ())
20633 return NULL_TREE;
20635 val_expr = DECL_VALUE_EXPR (decl);
20636 if (TREE_CODE (val_expr) != COMPONENT_REF)
20637 return NULL_TREE;
20639 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
20640 &unsignedp, &reversep, &volatilep);
20642 if (cvar == NULL_TREE
20643 || !VAR_P (cvar)
20644 || DECL_ARTIFICIAL (cvar)
20645 || !TREE_PUBLIC (cvar)
20646 /* We don't expect to have to cope with variable offsets,
20647 since at present all static data must have a constant size. */
20648 || !bitpos.is_constant (&cbitpos))
20649 return NULL_TREE;
20651 *value = 0;
20652 if (offset != NULL)
20654 if (!tree_fits_shwi_p (offset))
20655 return NULL_TREE;
20656 *value = tree_to_shwi (offset);
20658 if (cbitpos != 0)
20659 *value += cbitpos / BITS_PER_UNIT;
20661 return cvar;
20664 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
20665 data attribute for a variable or a parameter. We generate the
20666 DW_AT_const_value attribute only in those cases where the given variable
20667 or parameter does not have a true "location" either in memory or in a
20668 register. This can happen (for example) when a constant is passed as an
20669 actual argument in a call to an inline function. (It's possible that
20670 these things can crop up in other ways also.) Note that one type of
20671 constant value which can be passed into an inlined function is a constant
20672 pointer. This can happen for example if an actual argument in an inlined
20673 function call evaluates to a compile-time constant address.
20675 CACHE_P is true if it is worth caching the location list for DECL,
20676 so that future calls can reuse it rather than regenerate it from scratch.
20677 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
20678 since we will need to refer to them each time the function is inlined. */
20680 static bool
20681 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
20683 rtx rtl;
20684 dw_loc_list_ref list;
20685 var_loc_list *loc_list;
20686 cached_dw_loc_list *cache;
20688 if (early_dwarf)
20689 return false;
20691 if (TREE_CODE (decl) == ERROR_MARK)
20692 return false;
20694 if (get_AT (die, DW_AT_location)
20695 || get_AT (die, DW_AT_const_value))
20696 return true;
20698 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20699 || TREE_CODE (decl) == RESULT_DECL);
20701 /* Try to get some constant RTL for this decl, and use that as the value of
20702 the location. */
20704 rtl = rtl_for_decl_location (decl);
20705 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20706 && add_const_value_attribute (die, rtl))
20707 return true;
20709 /* See if we have single element location list that is equivalent to
20710 a constant value. That way we are better to use add_const_value_attribute
20711 rather than expanding constant value equivalent. */
20712 loc_list = lookup_decl_loc (decl);
20713 if (loc_list
20714 && loc_list->first
20715 && loc_list->first->next == NULL
20716 && NOTE_P (loc_list->first->loc)
20717 && NOTE_VAR_LOCATION (loc_list->first->loc)
20718 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20720 struct var_loc_node *node;
20722 node = loc_list->first;
20723 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20724 if (GET_CODE (rtl) == EXPR_LIST)
20725 rtl = XEXP (rtl, 0);
20726 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20727 && add_const_value_attribute (die, rtl))
20728 return true;
20730 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20731 list several times. See if we've already cached the contents. */
20732 list = NULL;
20733 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20734 cache_p = false;
20735 if (cache_p)
20737 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20738 if (cache)
20739 list = cache->loc_list;
20741 if (list == NULL)
20743 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20744 NULL);
20745 /* It is usually worth caching this result if the decl is from
20746 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20747 if (cache_p && list && list->dw_loc_next)
20749 cached_dw_loc_list **slot
20750 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20751 DECL_UID (decl),
20752 INSERT);
20753 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20754 cache->decl_id = DECL_UID (decl);
20755 cache->loc_list = list;
20756 *slot = cache;
20759 if (list)
20761 add_AT_location_description (die, DW_AT_location, list);
20762 return true;
20764 /* None of that worked, so it must not really have a location;
20765 try adding a constant value attribute from the DECL_INITIAL. */
20766 return tree_add_const_value_attribute_for_decl (die, decl);
20769 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20770 attribute is the const value T. */
20772 static bool
20773 tree_add_const_value_attribute (dw_die_ref die, tree t)
20775 tree init;
20776 tree type = TREE_TYPE (t);
20777 rtx rtl;
20779 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20780 return false;
20782 init = t;
20783 gcc_assert (!DECL_P (init));
20785 if (TREE_CODE (init) == INTEGER_CST)
20787 if (tree_fits_uhwi_p (init))
20789 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20790 return true;
20792 if (tree_fits_shwi_p (init))
20794 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20795 return true;
20798 /* Generate the RTL even if early_dwarf to force mangling of all refered to
20799 symbols. */
20800 rtl = rtl_for_decl_init (init, type);
20801 if (rtl && !early_dwarf)
20802 return add_const_value_attribute (die, rtl);
20803 /* If the host and target are sane, try harder. */
20804 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20805 && initializer_constant_valid_p (init, type))
20807 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20808 if (size > 0 && (int) size == size)
20810 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20812 if (native_encode_initializer (init, array, size) == size)
20814 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20815 return true;
20817 ggc_free (array);
20820 return false;
20823 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20824 attribute is the const value of T, where T is an integral constant
20825 variable with static storage duration
20826 (so it can't be a PARM_DECL or a RESULT_DECL). */
20828 static bool
20829 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20832 if (!decl
20833 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20834 || (VAR_P (decl) && !TREE_STATIC (decl)))
20835 return false;
20837 if (TREE_READONLY (decl)
20838 && ! TREE_THIS_VOLATILE (decl)
20839 && DECL_INITIAL (decl))
20840 /* OK */;
20841 else
20842 return false;
20844 /* Don't add DW_AT_const_value if abstract origin already has one. */
20845 if (get_AT (var_die, DW_AT_const_value))
20846 return false;
20848 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20851 /* Convert the CFI instructions for the current function into a
20852 location list. This is used for DW_AT_frame_base when we targeting
20853 a dwarf2 consumer that does not support the dwarf3
20854 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20855 expressions. */
20857 static dw_loc_list_ref
20858 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20860 int ix;
20861 dw_fde_ref fde;
20862 dw_loc_list_ref list, *list_tail;
20863 dw_cfi_ref cfi;
20864 dw_cfa_location last_cfa, next_cfa;
20865 const char *start_label, *last_label, *section;
20866 dw_cfa_location remember;
20868 fde = cfun->fde;
20869 gcc_assert (fde != NULL);
20871 section = secname_for_decl (current_function_decl);
20872 list_tail = &list;
20873 list = NULL;
20875 memset (&next_cfa, 0, sizeof (next_cfa));
20876 next_cfa.reg = INVALID_REGNUM;
20877 remember = next_cfa;
20879 start_label = fde->dw_fde_begin;
20881 /* ??? Bald assumption that the CIE opcode list does not contain
20882 advance opcodes. */
20883 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20884 lookup_cfa_1 (cfi, &next_cfa, &remember);
20886 last_cfa = next_cfa;
20887 last_label = start_label;
20889 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20891 /* If the first partition contained no CFI adjustments, the
20892 CIE opcodes apply to the whole first partition. */
20893 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20894 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20895 list_tail =&(*list_tail)->dw_loc_next;
20896 start_label = last_label = fde->dw_fde_second_begin;
20899 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20901 switch (cfi->dw_cfi_opc)
20903 case DW_CFA_set_loc:
20904 case DW_CFA_advance_loc1:
20905 case DW_CFA_advance_loc2:
20906 case DW_CFA_advance_loc4:
20907 if (!cfa_equal_p (&last_cfa, &next_cfa))
20909 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20910 start_label, 0, last_label, 0, section);
20912 list_tail = &(*list_tail)->dw_loc_next;
20913 last_cfa = next_cfa;
20914 start_label = last_label;
20916 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20917 break;
20919 case DW_CFA_advance_loc:
20920 /* The encoding is complex enough that we should never emit this. */
20921 gcc_unreachable ();
20923 default:
20924 lookup_cfa_1 (cfi, &next_cfa, &remember);
20925 break;
20927 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20929 if (!cfa_equal_p (&last_cfa, &next_cfa))
20931 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20932 start_label, 0, last_label, 0, section);
20934 list_tail = &(*list_tail)->dw_loc_next;
20935 last_cfa = next_cfa;
20936 start_label = last_label;
20938 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20939 start_label, 0, fde->dw_fde_end, 0, section);
20940 list_tail = &(*list_tail)->dw_loc_next;
20941 start_label = last_label = fde->dw_fde_second_begin;
20945 if (!cfa_equal_p (&last_cfa, &next_cfa))
20947 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20948 start_label, 0, last_label, 0, section);
20949 list_tail = &(*list_tail)->dw_loc_next;
20950 start_label = last_label;
20953 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20954 start_label, 0,
20955 fde->dw_fde_second_begin
20956 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20957 section);
20959 maybe_gen_llsym (list);
20961 return list;
20964 /* Compute a displacement from the "steady-state frame pointer" to the
20965 frame base (often the same as the CFA), and store it in
20966 frame_pointer_fb_offset. OFFSET is added to the displacement
20967 before the latter is negated. */
20969 static void
20970 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20972 rtx reg, elim;
20974 #ifdef FRAME_POINTER_CFA_OFFSET
20975 reg = frame_pointer_rtx;
20976 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20977 #else
20978 reg = arg_pointer_rtx;
20979 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20980 #endif
20982 elim = (ira_use_lra_p
20983 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20984 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20985 elim = strip_offset_and_add (elim, &offset);
20987 frame_pointer_fb_offset = -offset;
20989 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20990 in which to eliminate. This is because it's stack pointer isn't
20991 directly accessible as a register within the ISA. To work around
20992 this, assume that while we cannot provide a proper value for
20993 frame_pointer_fb_offset, we won't need one either. We can use
20994 hard frame pointer in debug info even if frame pointer isn't used
20995 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20996 which uses the DW_AT_frame_base attribute, not hard frame pointer
20997 directly. */
20998 frame_pointer_fb_offset_valid
20999 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
21002 /* Generate a DW_AT_name attribute given some string value to be included as
21003 the value of the attribute. */
21005 void
21006 add_name_attribute (dw_die_ref die, const char *name_string)
21008 if (name_string != NULL && *name_string != 0)
21010 if (demangle_name_func)
21011 name_string = (*demangle_name_func) (name_string);
21013 add_AT_string (die, DW_AT_name, name_string);
21017 /* Generate a DW_AT_name attribute given some string value representing a
21018 file or filepath to be included as value of the attribute. */
21019 static void
21020 add_filename_attribute (dw_die_ref die, const char *name_string)
21022 if (name_string != NULL && *name_string != 0)
21023 add_filepath_AT_string (die, DW_AT_name, name_string);
21026 /* Generate a DW_AT_description attribute given some string value to be included
21027 as the value of the attribute. */
21029 static void
21030 add_desc_attribute (dw_die_ref die, const char *name_string)
21032 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21033 return;
21035 if (name_string == NULL || *name_string == 0)
21036 return;
21038 if (demangle_name_func)
21039 name_string = (*demangle_name_func) (name_string);
21041 add_AT_string (die, DW_AT_description, name_string);
21044 /* Generate a DW_AT_description attribute given some decl to be included
21045 as the value of the attribute. */
21047 static void
21048 add_desc_attribute (dw_die_ref die, tree decl)
21050 tree decl_name;
21052 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
21053 return;
21055 if (decl == NULL_TREE || !DECL_P (decl))
21056 return;
21057 decl_name = DECL_NAME (decl);
21059 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21061 const char *name = dwarf2_name (decl, 0);
21062 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
21064 else
21066 char *desc = print_generic_expr_to_str (decl);
21067 add_desc_attribute (die, desc);
21068 free (desc);
21072 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
21073 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
21074 of TYPE accordingly.
21076 ??? This is a temporary measure until after we're able to generate
21077 regular DWARF for the complex Ada type system. */
21079 static void
21080 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
21081 dw_die_ref context_die)
21083 tree dtype;
21084 dw_die_ref dtype_die;
21086 if (!lang_hooks.types.descriptive_type)
21087 return;
21089 dtype = lang_hooks.types.descriptive_type (type);
21090 if (!dtype)
21091 return;
21093 dtype_die = lookup_type_die (dtype);
21094 if (!dtype_die)
21096 gen_type_die (dtype, context_die);
21097 dtype_die = lookup_type_die (dtype);
21098 gcc_assert (dtype_die);
21101 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
21104 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
21106 static const char *
21107 comp_dir_string (void)
21109 const char *wd;
21110 char *wd_plus_sep = NULL;
21111 static const char *cached_wd = NULL;
21113 if (cached_wd != NULL)
21114 return cached_wd;
21116 wd = get_src_pwd ();
21117 if (wd == NULL)
21118 return NULL;
21120 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
21122 size_t wdlen = strlen (wd);
21123 wd_plus_sep = XNEWVEC (char, wdlen + 2);
21124 strcpy (wd_plus_sep, wd);
21125 wd_plus_sep [wdlen] = DIR_SEPARATOR;
21126 wd_plus_sep [wdlen + 1] = 0;
21127 wd = wd_plus_sep;
21130 cached_wd = remap_debug_filename (wd);
21132 /* remap_debug_filename can just pass through wd or return a new gc string.
21133 These two types can't be both stored in a GTY(())-tagged string, but since
21134 the cached value lives forever just copy it if needed. */
21135 if (cached_wd != wd)
21137 cached_wd = xstrdup (cached_wd);
21138 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR && wd_plus_sep != NULL)
21139 free (wd_plus_sep);
21142 return cached_wd;
21145 /* Generate a DW_AT_comp_dir attribute for DIE. */
21147 static void
21148 add_comp_dir_attribute (dw_die_ref die)
21150 const char * wd = comp_dir_string ();
21151 if (wd != NULL)
21152 add_filepath_AT_string (die, DW_AT_comp_dir, wd);
21155 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
21156 pointer computation, ...), output a representation for that bound according
21157 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
21158 loc_list_from_tree for the meaning of CONTEXT. */
21160 static void
21161 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
21162 int forms, struct loc_descr_context *context)
21164 dw_die_ref context_die, decl_die = NULL;
21165 dw_loc_list_ref list;
21166 bool strip_conversions = true;
21167 bool placeholder_seen = false;
21169 while (strip_conversions)
21170 switch (TREE_CODE (value))
21172 case ERROR_MARK:
21173 case SAVE_EXPR:
21174 return;
21176 CASE_CONVERT:
21177 case VIEW_CONVERT_EXPR:
21178 value = TREE_OPERAND (value, 0);
21179 break;
21181 default:
21182 strip_conversions = false;
21183 break;
21186 /* If possible and permitted, output the attribute as a constant. */
21187 if ((forms & dw_scalar_form_constant) != 0
21188 && TREE_CODE (value) == INTEGER_CST)
21190 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
21192 /* If HOST_WIDE_INT is big enough then represent the bound as
21193 a constant value. We need to choose a form based on
21194 whether the type is signed or unsigned. We cannot just
21195 call add_AT_unsigned if the value itself is positive
21196 (add_AT_unsigned might add the unsigned value encoded as
21197 DW_FORM_data[1248]). Some DWARF consumers will lookup the
21198 bounds type and then sign extend any unsigned values found
21199 for signed types. This is needed only for
21200 DW_AT_{lower,upper}_bound, since for most other attributes,
21201 consumers will treat DW_FORM_data[1248] as unsigned values,
21202 regardless of the underlying type. */
21203 if (prec <= HOST_BITS_PER_WIDE_INT
21204 || tree_fits_uhwi_p (value))
21206 if (TYPE_UNSIGNED (TREE_TYPE (value)))
21207 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
21208 else
21209 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
21211 else if (dwarf_version >= 5
21212 && TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (value))) == 128)
21213 /* Otherwise represent the bound as an unsigned value with
21214 the precision of its type. The precision and signedness
21215 of the type will be necessary to re-interpret it
21216 unambiguously. */
21217 add_AT_wide (die, attr, wi::to_wide (value));
21218 else
21220 rtx v = immed_wide_int_const (wi::to_wide (value),
21221 TYPE_MODE (TREE_TYPE (value)));
21222 dw_loc_descr_ref loc
21223 = loc_descriptor (v, TYPE_MODE (TREE_TYPE (value)),
21224 VAR_INIT_STATUS_INITIALIZED);
21225 if (loc)
21226 add_AT_loc (die, attr, loc);
21228 return;
21231 /* Otherwise, if it's possible and permitted too, output a reference to
21232 another DIE. */
21233 if ((forms & dw_scalar_form_reference) != 0)
21235 tree decl = NULL_TREE;
21237 /* Some type attributes reference an outer type. For instance, the upper
21238 bound of an array may reference an embedding record (this happens in
21239 Ada). */
21240 if (TREE_CODE (value) == COMPONENT_REF
21241 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
21242 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
21243 decl = TREE_OPERAND (value, 1);
21245 else if (VAR_P (value)
21246 || TREE_CODE (value) == PARM_DECL
21247 || TREE_CODE (value) == RESULT_DECL)
21248 decl = value;
21250 if (decl != NULL_TREE)
21252 decl_die = lookup_decl_die (decl);
21254 /* ??? Can this happen, or should the variable have been bound
21255 first? Probably it can, since I imagine that we try to create
21256 the types of parameters in the order in which they exist in
21257 the list, and won't have created a forward reference to a
21258 later parameter. */
21259 if (decl_die != NULL)
21261 if (get_AT (decl_die, DW_AT_location)
21262 || get_AT (decl_die, DW_AT_data_member_location)
21263 || get_AT (decl_die, DW_AT_data_bit_offset)
21264 || get_AT (decl_die, DW_AT_const_value))
21266 add_AT_die_ref (die, attr, decl_die);
21267 return;
21273 /* Last chance: try to create a stack operation procedure to evaluate the
21274 value. Do nothing if even that is not possible or permitted. */
21275 if ((forms & dw_scalar_form_exprloc) == 0)
21276 return;
21278 list = loc_list_from_tree (value, 2, context);
21279 if (context && context->placeholder_arg)
21281 placeholder_seen = context->placeholder_seen;
21282 context->placeholder_seen = false;
21284 if (list == NULL || single_element_loc_list_p (list))
21286 /* If this attribute is not a reference nor constant, it is
21287 a DWARF expression rather than location description. For that
21288 loc_list_from_tree (value, 0, &context) is needed. */
21289 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
21290 if (list2 && single_element_loc_list_p (list2))
21292 if (placeholder_seen)
21294 struct dwarf_procedure_info dpi;
21295 dpi.fndecl = NULL_TREE;
21296 dpi.args_count = 1;
21297 if (!resolve_args_picking (list2->expr, 1, &dpi))
21298 return;
21300 add_AT_loc (die, attr, list2->expr);
21301 return;
21305 /* If that failed to give a single element location list, fall back to
21306 outputting this as a reference... still if permitted. */
21307 if (list == NULL
21308 || (forms & dw_scalar_form_reference) == 0
21309 || placeholder_seen)
21310 return;
21312 if (!decl_die)
21314 if (current_function_decl == 0)
21315 context_die = comp_unit_die ();
21316 else
21317 context_die = lookup_decl_die (current_function_decl);
21319 decl_die = new_die (DW_TAG_variable, context_die, value);
21320 add_AT_flag (decl_die, DW_AT_artificial, 1);
21321 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
21322 context_die);
21325 add_AT_location_description (decl_die, DW_AT_location, list);
21326 add_AT_die_ref (die, attr, decl_die);
21329 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
21330 default. */
21332 static int
21333 lower_bound_default (void)
21335 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21337 case DW_LANG_C:
21338 case DW_LANG_C89:
21339 case DW_LANG_C99:
21340 case DW_LANG_C11:
21341 case DW_LANG_C_plus_plus:
21342 case DW_LANG_C_plus_plus_11:
21343 case DW_LANG_C_plus_plus_14:
21344 case DW_LANG_ObjC:
21345 case DW_LANG_ObjC_plus_plus:
21346 return 0;
21347 case DW_LANG_Fortran77:
21348 case DW_LANG_Fortran90:
21349 case DW_LANG_Fortran95:
21350 case DW_LANG_Fortran03:
21351 case DW_LANG_Fortran08:
21352 return 1;
21353 case DW_LANG_UPC:
21354 case DW_LANG_D:
21355 case DW_LANG_Python:
21356 return dwarf_version >= 4 ? 0 : -1;
21357 case DW_LANG_Ada95:
21358 case DW_LANG_Ada83:
21359 case DW_LANG_Cobol74:
21360 case DW_LANG_Cobol85:
21361 case DW_LANG_Modula2:
21362 case DW_LANG_PLI:
21363 return dwarf_version >= 4 ? 1 : -1;
21364 default:
21365 return -1;
21369 /* Given a tree node describing an array bound (either lower or upper) output
21370 a representation for that bound. */
21372 static void
21373 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
21374 tree bound, struct loc_descr_context *context)
21376 int dflt;
21378 while (1)
21379 switch (TREE_CODE (bound))
21381 /* Strip all conversions. */
21382 CASE_CONVERT:
21383 case VIEW_CONVERT_EXPR:
21384 bound = TREE_OPERAND (bound, 0);
21385 break;
21387 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
21388 are even omitted when they are the default. */
21389 case INTEGER_CST:
21390 /* If the value for this bound is the default one, we can even omit the
21391 attribute. */
21392 if (bound_attr == DW_AT_lower_bound
21393 && tree_fits_shwi_p (bound)
21394 && (dflt = lower_bound_default ()) != -1
21395 && tree_to_shwi (bound) == dflt)
21396 return;
21398 /* FALLTHRU */
21400 default:
21401 /* Let GNAT encodings do the magic for self-referential bounds. */
21402 if (is_ada ()
21403 && gnat_encodings == DWARF_GNAT_ENCODINGS_ALL
21404 && contains_placeholder_p (bound))
21405 return;
21407 add_scalar_info (subrange_die, bound_attr, bound,
21408 dw_scalar_form_constant
21409 | dw_scalar_form_exprloc
21410 | dw_scalar_form_reference,
21411 context);
21412 return;
21416 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
21417 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
21419 This function reuses previously set type and bound information if
21420 available. */
21422 static void
21423 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
21425 dw_die_ref child = type_die->die_child;
21426 struct array_descr_info info;
21427 int dimension_number;
21429 if (lang_hooks.types.get_array_descr_info)
21431 memset (&info, 0, sizeof (info));
21432 if (lang_hooks.types.get_array_descr_info (type, &info))
21433 /* Fortran sometimes emits array types with no dimension. */
21434 gcc_assert (info.ndimensions >= 0
21435 && info.ndimensions
21436 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN);
21438 else
21439 info.ndimensions = 0;
21441 for (dimension_number = 0;
21442 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
21443 type = TREE_TYPE (type), dimension_number++)
21445 tree domain = TYPE_DOMAIN (type);
21447 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
21448 break;
21450 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
21451 and (in GNU C only) variable bounds. Handle all three forms
21452 here. */
21454 /* Find and reuse a previously generated DW_TAG_subrange_type if
21455 available.
21457 For multi-dimensional arrays, as we iterate through the
21458 various dimensions in the enclosing for loop above, we also
21459 iterate through the DIE children and pick at each
21460 DW_TAG_subrange_type previously generated (if available).
21461 Each child DW_TAG_subrange_type DIE describes the range of
21462 the current dimension. At this point we should have as many
21463 DW_TAG_subrange_type's as we have dimensions in the
21464 array. */
21465 dw_die_ref subrange_die = NULL;
21466 if (child)
21467 while (1)
21469 child = child->die_sib;
21470 if (child->die_tag == DW_TAG_subrange_type)
21471 subrange_die = child;
21472 if (child == type_die->die_child)
21474 /* If we wrapped around, stop looking next time. */
21475 child = NULL;
21476 break;
21478 if (child->die_tag == DW_TAG_subrange_type)
21479 break;
21481 if (!subrange_die)
21482 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
21484 if (domain)
21486 /* We have an array type with specified bounds. */
21487 tree lower = TYPE_MIN_VALUE (domain);
21488 tree upper = TYPE_MAX_VALUE (domain);
21489 tree index_type = TREE_TYPE (domain);
21491 if (dimension_number <= info.ndimensions - 1)
21493 lower = info.dimen[dimension_number].lower_bound;
21494 upper = info.dimen[dimension_number].upper_bound;
21495 index_type = info.dimen[dimension_number].bounds_type;
21498 /* Define the index type. */
21499 if (index_type && !get_AT (subrange_die, DW_AT_type))
21500 add_type_attribute (subrange_die, index_type, TYPE_UNQUALIFIED,
21501 false, type_die);
21503 /* ??? If upper is NULL, the array has unspecified length,
21504 but it does have a lower bound. This happens with Fortran
21505 dimension arr(N:*)
21506 Since the debugger is definitely going to need to know N
21507 to produce useful results, go ahead and output the lower
21508 bound solo, and hope the debugger can cope. */
21510 if (lower && !get_AT (subrange_die, DW_AT_lower_bound))
21511 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
21513 if (!get_AT (subrange_die, DW_AT_upper_bound)
21514 && !get_AT (subrange_die, DW_AT_count))
21516 if (upper)
21517 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
21518 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
21519 /* Zero-length array. */
21520 add_bound_info (subrange_die, DW_AT_count,
21521 build_int_cst (TREE_TYPE (lower), 0), NULL);
21525 /* Otherwise we have an array type with an unspecified length. The
21526 DWARF-2 spec does not say how to handle this; let's just leave out the
21527 bounds. */
21531 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
21533 static void
21534 add_byte_size_attribute (dw_die_ref die, tree tree_node)
21536 dw_die_ref decl_die;
21537 HOST_WIDE_INT size;
21539 switch (TREE_CODE (tree_node))
21541 case ERROR_MARK:
21542 size = 0;
21543 break;
21544 case ENUMERAL_TYPE:
21545 case RECORD_TYPE:
21546 case UNION_TYPE:
21547 case QUAL_UNION_TYPE:
21548 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
21549 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
21551 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
21552 return;
21554 size = int_size_in_bytes (tree_node);
21555 break;
21556 case FIELD_DECL:
21557 /* For a data member of a struct or union, the DW_AT_byte_size is
21558 generally given as the number of bytes normally allocated for an
21559 object of the *declared* type of the member itself. This is true
21560 even for bit-fields. */
21561 size = int_size_in_bytes (field_type (tree_node));
21562 break;
21563 default:
21564 gcc_unreachable ();
21567 /* Note that `size' might be -1 when we get to this point. If it is, that
21568 indicates that the byte size of the entity in question is variable. */
21569 if (size >= 0)
21570 add_AT_unsigned (die, DW_AT_byte_size, size);
21572 /* Support for dynamically-sized objects was introduced in DWARF3. */
21573 else if (TYPE_P (tree_node)
21574 && (dwarf_version >= 3 || !dwarf_strict)
21575 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
21577 struct loc_descr_context ctx = {
21578 const_cast<tree> (tree_node), /* context_type */
21579 NULL_TREE, /* base_decl */
21580 NULL, /* dpi */
21581 false, /* placeholder_arg */
21582 false, /* placeholder_seen */
21583 false /* strict_signedness */
21586 tree tree_size = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (tree_node));
21587 add_scalar_info (die, DW_AT_byte_size, tree_size,
21588 dw_scalar_form_constant
21589 | dw_scalar_form_exprloc
21590 | dw_scalar_form_reference,
21591 &ctx);
21595 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21596 alignment. */
21598 static void
21599 add_alignment_attribute (dw_die_ref die, tree tree_node)
21601 if (dwarf_version < 5 && dwarf_strict)
21602 return;
21604 unsigned align;
21606 if (DECL_P (tree_node))
21608 if (!DECL_USER_ALIGN (tree_node))
21609 return;
21611 align = DECL_ALIGN_UNIT (tree_node);
21613 else if (TYPE_P (tree_node))
21615 if (!TYPE_USER_ALIGN (tree_node))
21616 return;
21618 align = TYPE_ALIGN_UNIT (tree_node);
21620 else
21621 gcc_unreachable ();
21623 add_AT_unsigned (die, DW_AT_alignment, align);
21626 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21627 which specifies the distance in bits from the highest order bit of the
21628 "containing object" for the bit-field to the highest order bit of the
21629 bit-field itself.
21631 For any given bit-field, the "containing object" is a hypothetical object
21632 (of some integral or enum type) within which the given bit-field lives. The
21633 type of this hypothetical "containing object" is always the same as the
21634 declared type of the individual bit-field itself. The determination of the
21635 exact location of the "containing object" for a bit-field is rather
21636 complicated. It's handled by the `field_byte_offset' function (above).
21638 Note that it is the size (in bytes) of the hypothetical "containing object"
21639 which will be given in the DW_AT_byte_size attribute for this bit-field.
21640 (See `byte_size_attribute' above). */
21642 static inline void
21643 add_bit_offset_attribute (dw_die_ref die, tree decl)
21645 HOST_WIDE_INT object_offset_in_bytes;
21646 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21647 HOST_WIDE_INT bitpos_int;
21648 HOST_WIDE_INT highest_order_object_bit_offset;
21649 HOST_WIDE_INT highest_order_field_bit_offset;
21650 HOST_WIDE_INT bit_offset;
21652 /* The containing object is within the DECL_CONTEXT. */
21653 struct vlr_context ctx = { DECL_CONTEXT (decl), NULL_TREE };
21655 field_byte_offset (decl, &ctx, &object_offset_in_bytes);
21657 /* Must be a field and a bit field. */
21658 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21660 /* We can't yet handle bit-fields whose offsets are variable, so if we
21661 encounter such things, just return without generating any attribute
21662 whatsoever. Likewise for variable or too large size. */
21663 if (! tree_fits_shwi_p (bit_position (decl))
21664 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21665 return;
21667 bitpos_int = int_bit_position (decl);
21669 /* Note that the bit offset is always the distance (in bits) from the
21670 highest-order bit of the "containing object" to the highest-order bit of
21671 the bit-field itself. Since the "high-order end" of any object or field
21672 is different on big-endian and little-endian machines, the computation
21673 below must take account of these differences. */
21674 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21675 highest_order_field_bit_offset = bitpos_int;
21677 if (! BYTES_BIG_ENDIAN)
21679 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21680 highest_order_object_bit_offset +=
21681 simple_type_size_in_bits (original_type);
21684 bit_offset
21685 = (! BYTES_BIG_ENDIAN
21686 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21687 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21689 if (bit_offset < 0)
21690 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21691 else
21692 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21695 /* For a FIELD_DECL node which represents a bit field, output an attribute
21696 which specifies the length in bits of the given field. */
21698 static inline void
21699 add_bit_size_attribute (dw_die_ref die, tree decl)
21701 /* Must be a field and a bit field. */
21702 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21703 && DECL_BIT_FIELD_TYPE (decl));
21705 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21706 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21709 /* If the compiled language is ANSI C, then add a 'prototyped'
21710 attribute, if arg types are given for the parameters of a function. */
21712 static inline void
21713 add_prototyped_attribute (dw_die_ref die, tree func_type)
21715 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21717 case DW_LANG_C:
21718 case DW_LANG_C89:
21719 case DW_LANG_C99:
21720 case DW_LANG_C11:
21721 case DW_LANG_ObjC:
21722 if (prototype_p (func_type))
21723 add_AT_flag (die, DW_AT_prototyped, 1);
21724 break;
21725 default:
21726 break;
21730 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21731 by looking in the type declaration, the object declaration equate table or
21732 the block mapping. */
21734 static inline void
21735 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21737 dw_die_ref origin_die = NULL;
21739 /* For late LTO debug output we want to refer directly to the abstract
21740 DIE in the early debug rather to the possibly existing concrete
21741 instance and avoid creating that just for this purpose. */
21742 sym_off_pair *desc;
21743 if (in_lto_p
21744 && external_die_map
21745 && (desc = external_die_map->get (origin)))
21747 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21748 desc->sym, desc->off);
21749 return;
21752 if (DECL_P (origin))
21753 origin_die = lookup_decl_die (origin);
21754 else if (TYPE_P (origin))
21755 origin_die = lookup_type_die (origin);
21756 else if (TREE_CODE (origin) == BLOCK)
21757 origin_die = lookup_block_die (origin);
21759 /* XXX: Functions that are never lowered don't always have correct block
21760 trees (in the case of java, they simply have no block tree, in some other
21761 languages). For these functions, there is nothing we can really do to
21762 output correct debug info for inlined functions in all cases. Rather
21763 than die, we'll just produce deficient debug info now, in that we will
21764 have variables without a proper abstract origin. In the future, when all
21765 functions are lowered, we should re-add a gcc_assert (origin_die)
21766 here. */
21768 if (origin_die)
21770 dw_attr_node *a;
21771 /* Like above, if we already created a concrete instance DIE
21772 do not use that for the abstract origin but the early DIE
21773 if present. */
21774 if (in_lto_p
21775 && (a = get_AT (origin_die, DW_AT_abstract_origin)))
21776 origin_die = AT_ref (a);
21777 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21781 /* We do not currently support the pure_virtual attribute. */
21783 static inline void
21784 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21786 if (DECL_VINDEX (func_decl))
21788 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21790 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21791 add_AT_loc (die, DW_AT_vtable_elem_location,
21792 new_loc_descr (DW_OP_constu,
21793 tree_to_shwi (DECL_VINDEX (func_decl)),
21794 0));
21796 /* GNU extension: Record what type this method came from originally. */
21797 if (debug_info_level > DINFO_LEVEL_TERSE
21798 && DECL_CONTEXT (func_decl))
21799 add_AT_die_ref (die, DW_AT_containing_type,
21800 lookup_type_die (DECL_CONTEXT (func_decl)));
21804 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21805 given decl. This used to be a vendor extension until after DWARF 4
21806 standardized it. */
21808 static void
21809 add_linkage_attr (dw_die_ref die, tree decl)
21811 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21813 /* Mimic what assemble_name_raw does with a leading '*'. */
21814 if (name[0] == '*')
21815 name = &name[1];
21817 if (dwarf_version >= 4)
21818 add_AT_string (die, DW_AT_linkage_name, name);
21819 else
21820 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21823 /* Add source coordinate attributes for the given decl. */
21825 static void
21826 add_src_coords_attributes (dw_die_ref die, tree decl)
21828 expanded_location s;
21830 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21831 return;
21832 s = expand_location (DECL_SOURCE_LOCATION (decl));
21833 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21834 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21835 if (debug_column_info && s.column)
21836 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21839 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21841 static void
21842 add_linkage_name_raw (dw_die_ref die, tree decl)
21844 /* Defer until we have an assembler name set. */
21845 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21847 limbo_die_node *asm_name;
21849 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21850 asm_name->die = die;
21851 asm_name->created_for = decl;
21852 asm_name->next = deferred_asm_name;
21853 deferred_asm_name = asm_name;
21855 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21856 add_linkage_attr (die, decl);
21859 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21861 static void
21862 add_linkage_name (dw_die_ref die, tree decl)
21864 if (debug_info_level > DINFO_LEVEL_NONE
21865 && VAR_OR_FUNCTION_DECL_P (decl)
21866 && TREE_PUBLIC (decl)
21867 && !(VAR_P (decl) && DECL_REGISTER (decl))
21868 && die->die_tag != DW_TAG_member)
21869 add_linkage_name_raw (die, decl);
21872 /* Add a DW_AT_name attribute and source coordinate attribute for the
21873 given decl, but only if it actually has a name. */
21875 static void
21876 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21877 bool no_linkage_name)
21879 tree decl_name;
21881 decl_name = DECL_NAME (decl);
21882 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21884 const char *name = dwarf2_name (decl, 0);
21885 if (name)
21886 add_name_attribute (die, name);
21887 else
21888 add_desc_attribute (die, decl);
21890 if (! DECL_ARTIFICIAL (decl))
21891 add_src_coords_attributes (die, decl);
21893 if (!no_linkage_name)
21894 add_linkage_name (die, decl);
21896 else
21897 add_desc_attribute (die, decl);
21899 #ifdef VMS_DEBUGGING_INFO
21900 /* Get the function's name, as described by its RTL. This may be different
21901 from the DECL_NAME name used in the source file. */
21902 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21904 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21905 XEXP (DECL_RTL (decl), 0), false);
21906 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21908 #endif /* VMS_DEBUGGING_INFO */
21911 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21913 static void
21914 add_discr_value (dw_die_ref die, dw_discr_value *value)
21916 dw_attr_node attr;
21918 attr.dw_attr = DW_AT_discr_value;
21919 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21920 attr.dw_attr_val.val_entry = NULL;
21921 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21922 if (value->pos)
21923 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21924 else
21925 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21926 add_dwarf_attr (die, &attr);
21929 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21931 static void
21932 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21934 dw_attr_node attr;
21936 attr.dw_attr = DW_AT_discr_list;
21937 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21938 attr.dw_attr_val.val_entry = NULL;
21939 attr.dw_attr_val.v.val_discr_list = discr_list;
21940 add_dwarf_attr (die, &attr);
21943 static inline dw_discr_list_ref
21944 AT_discr_list (dw_attr_node *attr)
21946 return attr->dw_attr_val.v.val_discr_list;
21949 #ifdef VMS_DEBUGGING_INFO
21950 /* Output the debug main pointer die for VMS */
21952 void
21953 dwarf2out_vms_debug_main_pointer (void)
21955 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21956 dw_die_ref die;
21958 /* Allocate the VMS debug main subprogram die. */
21959 die = new_die_raw (DW_TAG_subprogram);
21960 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21961 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21962 current_function_funcdef_no);
21963 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21965 /* Make it the first child of comp_unit_die (). */
21966 die->die_parent = comp_unit_die ();
21967 if (comp_unit_die ()->die_child)
21969 die->die_sib = comp_unit_die ()->die_child->die_sib;
21970 comp_unit_die ()->die_child->die_sib = die;
21972 else
21974 die->die_sib = die;
21975 comp_unit_die ()->die_child = die;
21978 #endif /* VMS_DEBUGGING_INFO */
21980 /* walk_tree helper function for uses_local_type, below. */
21982 static tree
21983 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21985 if (!TYPE_P (*tp))
21986 *walk_subtrees = 0;
21987 else
21989 tree name = TYPE_NAME (*tp);
21990 if (name && DECL_P (name) && decl_function_context (name))
21991 return *tp;
21993 return NULL_TREE;
21996 /* If TYPE involves a function-local type (including a local typedef to a
21997 non-local type), returns that type; otherwise returns NULL_TREE. */
21999 static tree
22000 uses_local_type (tree type)
22002 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
22003 return used;
22006 /* Return the DIE for the scope that immediately contains this type.
22007 Non-named types that do not involve a function-local type get global
22008 scope. Named types nested in namespaces or other types get their
22009 containing scope. All other types (i.e. function-local named types) get
22010 the current active scope. */
22012 static dw_die_ref
22013 scope_die_for (tree t, dw_die_ref context_die)
22015 dw_die_ref scope_die = NULL;
22016 tree containing_scope;
22018 /* Non-types always go in the current scope. */
22019 gcc_assert (TYPE_P (t));
22021 /* Use the scope of the typedef, rather than the scope of the type
22022 it refers to. */
22023 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
22024 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
22025 else
22026 containing_scope = TYPE_CONTEXT (t);
22028 /* Use the containing namespace if there is one. */
22029 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
22031 if (context_die == lookup_decl_die (containing_scope))
22032 /* OK */;
22033 else if (debug_info_level > DINFO_LEVEL_TERSE)
22034 context_die = get_context_die (containing_scope);
22035 else
22036 containing_scope = NULL_TREE;
22039 /* Ignore function type "scopes" from the C frontend. They mean that
22040 a tagged type is local to a parmlist of a function declarator, but
22041 that isn't useful to DWARF. */
22042 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
22043 containing_scope = NULL_TREE;
22045 if (SCOPE_FILE_SCOPE_P (containing_scope))
22047 /* If T uses a local type keep it local as well, to avoid references
22048 to function-local DIEs from outside the function. */
22049 if (current_function_decl && uses_local_type (t))
22050 scope_die = context_die;
22051 else
22052 scope_die = comp_unit_die ();
22054 else if (TYPE_P (containing_scope))
22056 /* For types, we can just look up the appropriate DIE. */
22057 if (debug_info_level > DINFO_LEVEL_TERSE)
22058 scope_die = get_context_die (containing_scope);
22059 else
22061 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
22062 if (scope_die == NULL)
22063 scope_die = comp_unit_die ();
22066 else
22067 scope_die = context_die;
22069 return scope_die;
22072 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
22074 static inline int
22075 local_scope_p (dw_die_ref context_die)
22077 for (; context_die; context_die = context_die->die_parent)
22078 if (context_die->die_tag == DW_TAG_inlined_subroutine
22079 || context_die->die_tag == DW_TAG_subprogram)
22080 return 1;
22082 return 0;
22085 /* Returns nonzero if CONTEXT_DIE is a class. */
22087 static inline int
22088 class_scope_p (dw_die_ref context_die)
22090 return (context_die
22091 && (context_die->die_tag == DW_TAG_structure_type
22092 || context_die->die_tag == DW_TAG_class_type
22093 || context_die->die_tag == DW_TAG_interface_type
22094 || context_die->die_tag == DW_TAG_union_type));
22097 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
22098 whether or not to treat a DIE in this context as a declaration. */
22100 static inline int
22101 class_or_namespace_scope_p (dw_die_ref context_die)
22103 return (class_scope_p (context_die)
22104 || (context_die && context_die->die_tag == DW_TAG_namespace));
22107 /* Many forms of DIEs require a "type description" attribute. This
22108 routine locates the proper "type descriptor" die for the type given
22109 by 'type' plus any additional qualifiers given by 'cv_quals', and
22110 adds a DW_AT_type attribute below the given die. */
22112 static void
22113 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
22114 bool reverse, dw_die_ref context_die)
22116 enum tree_code code = TREE_CODE (type);
22117 dw_die_ref type_die = NULL;
22119 if (debug_info_level <= DINFO_LEVEL_TERSE)
22120 return;
22122 /* ??? If this type is an unnamed subrange type of an integral, floating-point
22123 or fixed-point type, use the inner type. This is because we have no
22124 support for unnamed types in base_type_die. This can happen if this is
22125 an Ada subrange type. Correct solution is emit a subrange type die. */
22126 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
22127 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
22128 type = TREE_TYPE (type), code = TREE_CODE (type);
22130 if (code == ERROR_MARK
22131 /* Handle a special case. For functions whose return type is void, we
22132 generate *no* type attribute. (Note that no object may have type
22133 `void', so this only applies to function return types). */
22134 || code == VOID_TYPE)
22135 return;
22137 type_die = modified_type_die (type,
22138 cv_quals | TYPE_QUALS (type),
22139 reverse,
22140 context_die);
22142 if (type_die != NULL)
22143 add_AT_die_ref (object_die, DW_AT_type, type_die);
22146 /* Given an object die, add the calling convention attribute for the
22147 function call type. */
22148 static void
22149 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
22151 enum dwarf_calling_convention value = DW_CC_normal;
22153 value = ((enum dwarf_calling_convention)
22154 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
22156 if (is_fortran ()
22157 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
22159 /* DWARF 2 doesn't provide a way to identify a program's source-level
22160 entry point. DW_AT_calling_convention attributes are only meant
22161 to describe functions' calling conventions. However, lacking a
22162 better way to signal the Fortran main program, we used this for
22163 a long time, following existing custom. Now, DWARF 4 has
22164 DW_AT_main_subprogram, which we add below, but some tools still
22165 rely on the old way, which we thus keep. */
22166 value = DW_CC_program;
22168 if (dwarf_version >= 4 || !dwarf_strict)
22169 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
22172 /* Only add the attribute if the backend requests it, and
22173 is not DW_CC_normal. */
22174 if (value && (value != DW_CC_normal))
22175 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
22178 /* Given a tree pointer to a struct, class, union, or enum type node, return
22179 a pointer to the (string) tag name for the given type, or zero if the type
22180 was declared without a tag. */
22182 static const char *
22183 type_tag (const_tree type)
22185 const char *name = 0;
22187 if (TYPE_NAME (type) != 0)
22189 tree t = 0;
22191 /* Find the IDENTIFIER_NODE for the type name. */
22192 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
22193 && !TYPE_NAMELESS (type))
22194 t = TYPE_NAME (type);
22196 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
22197 a TYPE_DECL node, regardless of whether or not a `typedef' was
22198 involved. */
22199 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22200 && ! DECL_IGNORED_P (TYPE_NAME (type)))
22202 /* We want to be extra verbose. Don't call dwarf_name if
22203 DECL_NAME isn't set. The default hook for decl_printable_name
22204 doesn't like that, and in this context it's correct to return
22205 0, instead of "<anonymous>" or the like. */
22206 if (DECL_NAME (TYPE_NAME (type))
22207 && !DECL_NAMELESS (TYPE_NAME (type)))
22208 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
22211 /* Now get the name as a string, or invent one. */
22212 if (!name && t != 0)
22213 name = IDENTIFIER_POINTER (t);
22216 return (name == 0 || *name == '\0') ? 0 : name;
22219 /* Return the type associated with a data member, make a special check
22220 for bit field types. */
22222 static inline tree
22223 member_declared_type (const_tree member)
22225 return (DECL_BIT_FIELD_TYPE (member)
22226 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
22229 /* Get the decl's label, as described by its RTL. This may be different
22230 from the DECL_NAME name used in the source file. */
22232 #if 0
22233 static const char *
22234 decl_start_label (tree decl)
22236 rtx x;
22237 const char *fnname;
22239 x = DECL_RTL (decl);
22240 gcc_assert (MEM_P (x));
22242 x = XEXP (x, 0);
22243 gcc_assert (GET_CODE (x) == SYMBOL_REF);
22245 fnname = XSTR (x, 0);
22246 return fnname;
22248 #endif
22250 /* For variable-length arrays that have been previously generated, but
22251 may be incomplete due to missing subscript info, fill the subscript
22252 info. Return TRUE if this is one of those cases. */
22254 static bool
22255 fill_variable_array_bounds (tree type)
22257 if (TREE_ASM_WRITTEN (type)
22258 && TREE_CODE (type) == ARRAY_TYPE
22259 && variably_modified_type_p (type, NULL))
22261 dw_die_ref array_die = lookup_type_die (type);
22262 if (!array_die)
22263 return false;
22264 add_subscript_info (array_die, type, !is_ada ());
22265 return true;
22267 return false;
22270 /* These routines generate the internal representation of the DIE's for
22271 the compilation unit. Debugging information is collected by walking
22272 the declaration trees passed in from dwarf2out_decl(). */
22274 static void
22275 gen_array_type_die (tree type, dw_die_ref context_die)
22277 dw_die_ref array_die;
22279 /* GNU compilers represent multidimensional array types as sequences of one
22280 dimensional array types whose element types are themselves array types.
22281 We sometimes squish that down to a single array_type DIE with multiple
22282 subscripts in the Dwarf debugging info. The draft Dwarf specification
22283 say that we are allowed to do this kind of compression in C, because
22284 there is no difference between an array of arrays and a multidimensional
22285 array. We don't do this for Ada to remain as close as possible to the
22286 actual representation, which is especially important against the language
22287 flexibilty wrt arrays of variable size. */
22289 bool collapse_nested_arrays = !is_ada ();
22291 if (fill_variable_array_bounds (type))
22292 return;
22294 dw_die_ref scope_die = scope_die_for (type, context_die);
22295 tree element_type;
22297 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
22298 DW_TAG_string_type doesn't have DW_AT_type attribute). */
22299 if (TREE_CODE (type) == ARRAY_TYPE
22300 && TYPE_STRING_FLAG (type)
22301 && is_fortran ()
22302 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
22304 HOST_WIDE_INT size;
22306 array_die = new_die (DW_TAG_string_type, scope_die, type);
22307 add_name_attribute (array_die, type_tag (type));
22308 equate_type_number_to_die (type, array_die);
22309 size = int_size_in_bytes (type);
22310 if (size >= 0)
22311 add_AT_unsigned (array_die, DW_AT_byte_size, size);
22312 /* ??? We can't annotate types late, but for LTO we may not
22313 generate a location early either (gfortran.dg/save_6.f90). */
22314 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
22315 && TYPE_DOMAIN (type) != NULL_TREE
22316 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
22318 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
22319 tree rszdecl = szdecl;
22321 size = int_size_in_bytes (TREE_TYPE (szdecl));
22322 if (!DECL_P (szdecl))
22324 if (TREE_CODE (szdecl) == INDIRECT_REF
22325 && DECL_P (TREE_OPERAND (szdecl, 0)))
22327 rszdecl = TREE_OPERAND (szdecl, 0);
22328 if (int_size_in_bytes (TREE_TYPE (rszdecl))
22329 != DWARF2_ADDR_SIZE)
22330 size = 0;
22332 else
22333 size = 0;
22335 if (size > 0)
22337 dw_loc_list_ref loc
22338 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
22339 NULL);
22340 if (loc)
22342 add_AT_location_description (array_die, DW_AT_string_length,
22343 loc);
22344 if (size != DWARF2_ADDR_SIZE)
22345 add_AT_unsigned (array_die, dwarf_version >= 5
22346 ? DW_AT_string_length_byte_size
22347 : DW_AT_byte_size, size);
22351 return;
22354 array_die = new_die (DW_TAG_array_type, scope_die, type);
22355 add_name_attribute (array_die, type_tag (type));
22356 equate_type_number_to_die (type, array_die);
22358 if (TREE_CODE (type) == VECTOR_TYPE)
22359 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
22361 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
22362 if (is_fortran ()
22363 && TREE_CODE (type) == ARRAY_TYPE
22364 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
22365 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
22366 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22368 #if 0
22369 /* We default the array ordering. Debuggers will probably do the right
22370 things even if DW_AT_ordering is not present. It's not even an issue
22371 until we start to get into multidimensional arrays anyway. If a debugger
22372 is ever caught doing the Wrong Thing for multi-dimensional arrays,
22373 then we'll have to put the DW_AT_ordering attribute back in. (But if
22374 and when we find out that we need to put these in, we will only do so
22375 for multidimensional arrays. */
22376 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22377 #endif
22379 if (TREE_CODE (type) == VECTOR_TYPE)
22381 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
22382 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
22383 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
22384 add_bound_info (subrange_die, DW_AT_upper_bound,
22385 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
22387 else
22388 add_subscript_info (array_die, type, collapse_nested_arrays);
22390 /* Add representation of the type of the elements of this array type and
22391 emit the corresponding DIE if we haven't done it already. */
22392 element_type = TREE_TYPE (type);
22393 if (collapse_nested_arrays)
22394 while (TREE_CODE (element_type) == ARRAY_TYPE)
22396 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
22397 break;
22398 element_type = TREE_TYPE (element_type);
22401 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
22402 TREE_CODE (type) == ARRAY_TYPE
22403 && TYPE_REVERSE_STORAGE_ORDER (type),
22404 context_die);
22406 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22407 if (TYPE_ARTIFICIAL (type))
22408 add_AT_flag (array_die, DW_AT_artificial, 1);
22410 if (get_AT (array_die, DW_AT_name))
22411 add_pubtype (type, array_die);
22413 add_alignment_attribute (array_die, type);
22416 /* This routine generates DIE for array with hidden descriptor, details
22417 are filled into *info by a langhook. */
22419 static void
22420 gen_descr_array_type_die (tree type, struct array_descr_info *info,
22421 dw_die_ref context_die)
22423 const dw_die_ref scope_die = scope_die_for (type, context_die);
22424 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
22425 struct loc_descr_context context = {
22426 type, /* context_type */
22427 info->base_decl, /* base_decl */
22428 NULL, /* dpi */
22429 false, /* placeholder_arg */
22430 false, /* placeholder_seen */
22431 false /* strict_signedness */
22433 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
22434 int dim;
22436 add_name_attribute (array_die, type_tag (type));
22437 equate_type_number_to_die (type, array_die);
22439 if (info->ndimensions > 1)
22440 switch (info->ordering)
22442 case array_descr_ordering_row_major:
22443 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
22444 break;
22445 case array_descr_ordering_column_major:
22446 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
22447 break;
22448 default:
22449 break;
22452 if (dwarf_version >= 3 || !dwarf_strict)
22454 if (info->data_location)
22455 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
22456 dw_scalar_form_exprloc, &context);
22457 if (info->associated)
22458 add_scalar_info (array_die, DW_AT_associated, info->associated,
22459 dw_scalar_form_constant
22460 | dw_scalar_form_exprloc
22461 | dw_scalar_form_reference, &context);
22462 if (info->allocated)
22463 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
22464 dw_scalar_form_constant
22465 | dw_scalar_form_exprloc
22466 | dw_scalar_form_reference, &context);
22467 if (info->stride)
22469 const enum dwarf_attribute attr
22470 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
22471 const int forms
22472 = (info->stride_in_bits)
22473 ? dw_scalar_form_constant
22474 : (dw_scalar_form_constant
22475 | dw_scalar_form_exprloc
22476 | dw_scalar_form_reference);
22478 add_scalar_info (array_die, attr, info->stride, forms, &context);
22481 if (dwarf_version >= 5)
22483 if (info->rank)
22485 add_scalar_info (array_die, DW_AT_rank, info->rank,
22486 dw_scalar_form_constant
22487 | dw_scalar_form_exprloc, &context);
22488 subrange_tag = DW_TAG_generic_subrange;
22489 context.placeholder_arg = true;
22493 add_gnat_descriptive_type_attribute (array_die, type, context_die);
22495 for (dim = 0; dim < info->ndimensions; dim++)
22497 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
22499 if (info->dimen[dim].bounds_type)
22500 add_type_attribute (subrange_die,
22501 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
22502 false, context_die);
22503 if (info->dimen[dim].lower_bound)
22504 add_bound_info (subrange_die, DW_AT_lower_bound,
22505 info->dimen[dim].lower_bound, &context);
22506 if (info->dimen[dim].upper_bound)
22507 add_bound_info (subrange_die, DW_AT_upper_bound,
22508 info->dimen[dim].upper_bound, &context);
22509 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
22510 add_scalar_info (subrange_die, DW_AT_byte_stride,
22511 info->dimen[dim].stride,
22512 dw_scalar_form_constant
22513 | dw_scalar_form_exprloc
22514 | dw_scalar_form_reference,
22515 &context);
22518 gen_type_die (info->element_type, context_die);
22519 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
22520 TREE_CODE (type) == ARRAY_TYPE
22521 && TYPE_REVERSE_STORAGE_ORDER (type),
22522 context_die);
22524 if (get_AT (array_die, DW_AT_name))
22525 add_pubtype (type, array_die);
22527 add_alignment_attribute (array_die, type);
22530 #if 0
22531 static void
22532 gen_entry_point_die (tree decl, dw_die_ref context_die)
22534 tree origin = decl_ultimate_origin (decl);
22535 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
22537 if (origin != NULL)
22538 add_abstract_origin_attribute (decl_die, origin);
22539 else
22541 add_name_and_src_coords_attributes (decl_die, decl);
22542 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
22543 TYPE_UNQUALIFIED, false, context_die);
22546 if (DECL_ABSTRACT_P (decl))
22547 equate_decl_number_to_die (decl, decl_die);
22548 else
22549 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
22551 #endif
22553 /* Walk through the list of incomplete types again, trying once more to
22554 emit full debugging info for them. */
22556 static void
22557 retry_incomplete_types (void)
22559 set_early_dwarf s;
22560 int i;
22562 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
22563 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
22564 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
22565 vec_safe_truncate (incomplete_types, 0);
22568 /* Determine what tag to use for a record type. */
22570 static enum dwarf_tag
22571 record_type_tag (tree type)
22573 if (! lang_hooks.types.classify_record)
22574 return DW_TAG_structure_type;
22576 switch (lang_hooks.types.classify_record (type))
22578 case RECORD_IS_STRUCT:
22579 return DW_TAG_structure_type;
22581 case RECORD_IS_CLASS:
22582 return DW_TAG_class_type;
22584 case RECORD_IS_INTERFACE:
22585 if (dwarf_version >= 3 || !dwarf_strict)
22586 return DW_TAG_interface_type;
22587 return DW_TAG_structure_type;
22589 default:
22590 gcc_unreachable ();
22594 /* Generate a DIE to represent an enumeration type. Note that these DIEs
22595 include all of the information about the enumeration values also. Each
22596 enumerated type name/value is listed as a child of the enumerated type
22597 DIE. */
22599 static dw_die_ref
22600 gen_enumeration_type_die (tree type, dw_die_ref context_die)
22602 dw_die_ref type_die = lookup_type_die (type);
22603 dw_die_ref orig_type_die = type_die;
22605 if (type_die == NULL)
22607 type_die = new_die (DW_TAG_enumeration_type,
22608 scope_die_for (type, context_die), type);
22609 equate_type_number_to_die (type, type_die);
22610 add_name_attribute (type_die, type_tag (type));
22611 if ((dwarf_version >= 4 || !dwarf_strict)
22612 && ENUM_IS_SCOPED (type))
22613 add_AT_flag (type_die, DW_AT_enum_class, 1);
22614 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22615 add_AT_flag (type_die, DW_AT_declaration, 1);
22616 if (!dwarf_strict)
22617 add_AT_unsigned (type_die, DW_AT_encoding,
22618 TYPE_UNSIGNED (type)
22619 ? DW_ATE_unsigned
22620 : DW_ATE_signed);
22622 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22623 return type_die;
22624 else
22625 remove_AT (type_die, DW_AT_declaration);
22627 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22628 given enum type is incomplete, do not generate the DW_AT_byte_size
22629 attribute or the DW_AT_element_list attribute. */
22630 if (TYPE_SIZE (type))
22632 tree link;
22634 if (!ENUM_IS_OPAQUE (type))
22635 TREE_ASM_WRITTEN (type) = 1;
22636 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22637 add_byte_size_attribute (type_die, type);
22638 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22639 add_alignment_attribute (type_die, type);
22640 if ((dwarf_version >= 3 || !dwarf_strict)
22641 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22643 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22644 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22645 context_die);
22647 if (TYPE_STUB_DECL (type) != NULL_TREE)
22649 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22650 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22651 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22652 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22655 /* If the first reference to this type was as the return type of an
22656 inline function, then it may not have a parent. Fix this now. */
22657 if (type_die->die_parent == NULL)
22658 add_child_die (scope_die_for (type, context_die), type_die);
22660 for (link = TYPE_VALUES (type);
22661 link != NULL; link = TREE_CHAIN (link))
22663 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22664 tree value = TREE_VALUE (link);
22666 if (DECL_P (value))
22667 equate_decl_number_to_die (value, enum_die);
22669 gcc_assert (!ENUM_IS_OPAQUE (type));
22670 add_name_attribute (enum_die,
22671 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22673 if (TREE_CODE (value) == CONST_DECL)
22674 value = DECL_INITIAL (value);
22676 if (simple_type_size_in_bits (TREE_TYPE (value))
22677 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22679 /* For constant forms created by add_AT_unsigned DWARF
22680 consumers (GDB, elfutils, etc.) always zero extend
22681 the value. Only when the actual value is negative
22682 do we need to use add_AT_int to generate a constant
22683 form that can represent negative values. */
22684 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22685 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22686 add_AT_unsigned (enum_die, DW_AT_const_value,
22687 (unsigned HOST_WIDE_INT) val);
22688 else
22689 add_AT_int (enum_die, DW_AT_const_value, val);
22691 else
22692 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22693 that here. TODO: This should be re-worked to use correct
22694 signed/unsigned double tags for all cases. */
22695 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22698 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22699 if (TYPE_ARTIFICIAL (type)
22700 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22701 add_AT_flag (type_die, DW_AT_artificial, 1);
22703 else
22704 add_AT_flag (type_die, DW_AT_declaration, 1);
22706 add_pubtype (type, type_die);
22708 return type_die;
22711 /* Generate a DIE to represent either a real live formal parameter decl or to
22712 represent just the type of some formal parameter position in some function
22713 type.
22715 Note that this routine is a bit unusual because its argument may be a
22716 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22717 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22718 node. If it's the former then this function is being called to output a
22719 DIE to represent a formal parameter object (or some inlining thereof). If
22720 it's the latter, then this function is only being called to output a
22721 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22722 argument type of some subprogram type.
22723 If EMIT_NAME_P is true, name and source coordinate attributes
22724 are emitted. */
22726 static dw_die_ref
22727 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22728 dw_die_ref context_die)
22730 tree node_or_origin = node ? node : origin;
22731 tree ultimate_origin;
22732 dw_die_ref parm_die = NULL;
22734 if (DECL_P (node_or_origin))
22736 parm_die = lookup_decl_die (node);
22738 /* If the contexts differ, we may not be talking about the same
22739 thing.
22740 ??? When in LTO the DIE parent is the "abstract" copy and the
22741 context_die is the specification "copy". */
22742 if (parm_die
22743 && parm_die->die_parent != context_die
22744 && (parm_die->die_parent->die_tag != DW_TAG_GNU_formal_parameter_pack
22745 || parm_die->die_parent->die_parent != context_die)
22746 && !in_lto_p)
22748 gcc_assert (!DECL_ABSTRACT_P (node));
22749 /* This can happen when creating a concrete instance, in
22750 which case we need to create a new DIE that will get
22751 annotated with DW_AT_abstract_origin. */
22752 parm_die = NULL;
22755 if (parm_die && parm_die->die_parent == NULL)
22757 /* Check that parm_die already has the right attributes that
22758 we would have added below. If any attributes are
22759 missing, fall through to add them. */
22760 if (! DECL_ABSTRACT_P (node_or_origin)
22761 && !get_AT (parm_die, DW_AT_location)
22762 && !get_AT (parm_die, DW_AT_const_value))
22763 /* We are missing location info, and are about to add it. */
22765 else
22767 add_child_die (context_die, parm_die);
22768 return parm_die;
22773 /* If we have a previously generated DIE, use it, unless this is an
22774 concrete instance (origin != NULL), in which case we need a new
22775 DIE with a corresponding DW_AT_abstract_origin. */
22776 bool reusing_die;
22777 if (parm_die && origin == NULL)
22778 reusing_die = true;
22779 else
22781 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22782 reusing_die = false;
22785 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22787 case tcc_declaration:
22788 ultimate_origin = decl_ultimate_origin (node_or_origin);
22789 if (node || ultimate_origin)
22790 origin = ultimate_origin;
22792 if (reusing_die)
22793 goto add_location;
22795 if (origin != NULL)
22796 add_abstract_origin_attribute (parm_die, origin);
22797 else if (emit_name_p)
22798 add_name_and_src_coords_attributes (parm_die, node);
22799 if (origin == NULL
22800 || (! DECL_ABSTRACT_P (node_or_origin)
22801 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22802 decl_function_context
22803 (node_or_origin))))
22805 tree type = TREE_TYPE (node_or_origin);
22806 if (decl_by_reference_p (node_or_origin))
22807 add_type_attribute (parm_die, TREE_TYPE (type),
22808 TYPE_UNQUALIFIED,
22809 false, context_die);
22810 else
22811 add_type_attribute (parm_die, type,
22812 decl_quals (node_or_origin),
22813 false, context_die);
22815 if (origin == NULL && DECL_ARTIFICIAL (node))
22816 add_AT_flag (parm_die, DW_AT_artificial, 1);
22817 add_location:
22818 if (node && node != origin)
22819 equate_decl_number_to_die (node, parm_die);
22820 if (! DECL_ABSTRACT_P (node_or_origin))
22821 add_location_or_const_value_attribute (parm_die, node_or_origin,
22822 node == NULL);
22824 break;
22826 case tcc_type:
22827 /* We were called with some kind of a ..._TYPE node. */
22828 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22829 context_die);
22830 break;
22832 default:
22833 gcc_unreachable ();
22836 return parm_die;
22839 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22840 children DW_TAG_formal_parameter DIEs representing the arguments of the
22841 parameter pack.
22843 PARM_PACK must be a function parameter pack.
22844 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22845 must point to the subsequent arguments of the function PACK_ARG belongs to.
22846 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22847 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22848 following the last one for which a DIE was generated. */
22850 static dw_die_ref
22851 gen_formal_parameter_pack_die (tree parm_pack,
22852 tree pack_arg,
22853 dw_die_ref subr_die,
22854 tree *next_arg)
22856 tree arg;
22857 dw_die_ref parm_pack_die;
22859 gcc_assert (parm_pack
22860 && lang_hooks.function_parameter_pack_p (parm_pack)
22861 && subr_die);
22863 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22864 add_src_coords_attributes (parm_pack_die, parm_pack);
22866 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22868 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22869 parm_pack))
22870 break;
22871 gen_formal_parameter_die (arg, NULL,
22872 false /* Don't emit name attribute. */,
22873 parm_pack_die);
22875 if (next_arg)
22876 *next_arg = arg;
22877 return parm_pack_die;
22880 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22881 at the end of an (ANSI prototyped) formal parameters list. */
22883 static void
22884 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22886 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22889 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22890 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22891 parameters as specified in some function type specification (except for
22892 those which appear as part of a function *definition*). */
22894 static void
22895 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22897 tree link;
22898 tree formal_type = NULL;
22899 tree first_parm_type;
22900 tree arg;
22902 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22904 arg = DECL_ARGUMENTS (function_or_method_type);
22905 function_or_method_type = TREE_TYPE (function_or_method_type);
22907 else
22908 arg = NULL_TREE;
22910 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22912 /* Make our first pass over the list of formal parameter types and output a
22913 DW_TAG_formal_parameter DIE for each one. */
22914 for (link = first_parm_type; link; )
22916 dw_die_ref parm_die;
22918 formal_type = TREE_VALUE (link);
22919 if (formal_type == void_type_node)
22920 break;
22922 /* Output a (nameless) DIE to represent the formal parameter itself. */
22923 parm_die = gen_formal_parameter_die (formal_type, NULL,
22924 true /* Emit name attribute. */,
22925 context_die);
22926 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22927 && link == first_parm_type)
22929 add_AT_flag (parm_die, DW_AT_artificial, 1);
22930 if (dwarf_version >= 3 || !dwarf_strict)
22931 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22933 else if (arg && DECL_ARTIFICIAL (arg))
22934 add_AT_flag (parm_die, DW_AT_artificial, 1);
22936 link = TREE_CHAIN (link);
22937 if (arg)
22938 arg = DECL_CHAIN (arg);
22941 /* If this function type has an ellipsis, add a
22942 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22943 if (formal_type != void_type_node)
22944 gen_unspecified_parameters_die (function_or_method_type, context_die);
22946 /* Make our second (and final) pass over the list of formal parameter types
22947 and output DIEs to represent those types (as necessary). */
22948 for (link = TYPE_ARG_TYPES (function_or_method_type);
22949 link && TREE_VALUE (link);
22950 link = TREE_CHAIN (link))
22951 gen_type_die (TREE_VALUE (link), context_die);
22954 /* We want to generate the DIE for TYPE so that we can generate the
22955 die for MEMBER, which has been defined; we will need to refer back
22956 to the member declaration nested within TYPE. If we're trying to
22957 generate minimal debug info for TYPE, processing TYPE won't do the
22958 trick; we need to attach the member declaration by hand. */
22960 static void
22961 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22963 gen_type_die (type, context_die);
22965 /* If we're trying to avoid duplicate debug info, we may not have
22966 emitted the member decl for this function. Emit it now. */
22967 if (TYPE_STUB_DECL (type)
22968 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22969 && ! lookup_decl_die (member))
22971 dw_die_ref type_die;
22972 gcc_assert (!decl_ultimate_origin (member));
22974 type_die = lookup_type_die_strip_naming_typedef (type);
22975 if (TREE_CODE (member) == FUNCTION_DECL)
22976 gen_subprogram_die (member, type_die);
22977 else if (TREE_CODE (member) == FIELD_DECL)
22979 /* Ignore the nameless fields that are used to skip bits but handle
22980 C++ anonymous unions and structs. */
22981 if (DECL_NAME (member) != NULL_TREE
22982 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22983 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22985 struct vlr_context vlr_ctx = {
22986 DECL_CONTEXT (member), /* struct_type */
22987 NULL_TREE /* variant_part_offset */
22989 gen_type_die (member_declared_type (member), type_die);
22990 gen_field_die (member, &vlr_ctx, type_die);
22993 else
22994 gen_variable_die (member, NULL_TREE, type_die);
22998 /* Forward declare these functions, because they are mutually recursive
22999 with their set_block_* pairing functions. */
23000 static void set_decl_origin_self (tree);
23002 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
23003 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
23004 that it points to the node itself, thus indicating that the node is its
23005 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
23006 the given node is NULL, recursively descend the decl/block tree which
23007 it is the root of, and for each other ..._DECL or BLOCK node contained
23008 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
23009 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
23010 values to point to themselves. */
23012 static void
23013 set_block_origin_self (tree stmt)
23015 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
23017 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
23020 tree local_decl;
23022 for (local_decl = BLOCK_VARS (stmt);
23023 local_decl != NULL_TREE;
23024 local_decl = DECL_CHAIN (local_decl))
23025 /* Do not recurse on nested functions since the inlining status
23026 of parent and child can be different as per the DWARF spec. */
23027 if (TREE_CODE (local_decl) != FUNCTION_DECL
23028 && !DECL_EXTERNAL (local_decl))
23029 set_decl_origin_self (local_decl);
23033 tree subblock;
23035 for (subblock = BLOCK_SUBBLOCKS (stmt);
23036 subblock != NULL_TREE;
23037 subblock = BLOCK_CHAIN (subblock))
23038 set_block_origin_self (subblock); /* Recurse. */
23043 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
23044 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
23045 node to so that it points to the node itself, thus indicating that the
23046 node represents its own (abstract) origin. Additionally, if the
23047 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
23048 the decl/block tree of which the given node is the root of, and for
23049 each other ..._DECL or BLOCK node contained therein whose
23050 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
23051 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
23052 point to themselves. */
23054 static void
23055 set_decl_origin_self (tree decl)
23057 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
23059 DECL_ABSTRACT_ORIGIN (decl) = decl;
23060 if (TREE_CODE (decl) == FUNCTION_DECL)
23062 tree arg;
23064 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
23065 DECL_ABSTRACT_ORIGIN (arg) = arg;
23066 if (DECL_INITIAL (decl) != NULL_TREE
23067 && DECL_INITIAL (decl) != error_mark_node)
23068 set_block_origin_self (DECL_INITIAL (decl));
23073 /* Mark the early DIE for DECL as the abstract instance. */
23075 static void
23076 dwarf2out_abstract_function (tree decl)
23078 dw_die_ref old_die;
23080 /* Make sure we have the actual abstract inline, not a clone. */
23081 decl = DECL_ORIGIN (decl);
23083 if (DECL_IGNORED_P (decl))
23084 return;
23086 /* In LTO we're all set. We already created abstract instances
23087 early and we want to avoid creating a concrete instance of that
23088 if we don't output it. */
23089 if (in_lto_p)
23090 return;
23092 old_die = lookup_decl_die (decl);
23093 gcc_assert (old_die != NULL);
23094 if (get_AT (old_die, DW_AT_inline))
23095 /* We've already generated the abstract instance. */
23096 return;
23098 /* Go ahead and put DW_AT_inline on the DIE. */
23099 if (DECL_DECLARED_INLINE_P (decl))
23101 if (cgraph_function_possibly_inlined_p (decl))
23102 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
23103 else
23104 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
23106 else
23108 if (cgraph_function_possibly_inlined_p (decl))
23109 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
23110 else
23111 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
23114 if (DECL_DECLARED_INLINE_P (decl)
23115 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
23116 add_AT_flag (old_die, DW_AT_artificial, 1);
23118 set_decl_origin_self (decl);
23121 /* Helper function of premark_used_types() which gets called through
23122 htab_traverse.
23124 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23125 marked as unused by prune_unused_types. */
23127 bool
23128 premark_used_types_helper (tree const &type, void *)
23130 dw_die_ref die;
23132 die = lookup_type_die (type);
23133 if (die != NULL)
23134 die->die_perennial_p = 1;
23135 return true;
23138 /* Helper function of premark_types_used_by_global_vars which gets called
23139 through htab_traverse.
23141 Marks the DIE of a given type in *SLOT as perennial, so it never gets
23142 marked as unused by prune_unused_types. The DIE of the type is marked
23143 only if the global variable using the type will actually be emitted. */
23146 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
23147 void *)
23149 struct types_used_by_vars_entry *entry;
23150 dw_die_ref die;
23152 entry = (struct types_used_by_vars_entry *) *slot;
23153 gcc_assert (entry->type != NULL
23154 && entry->var_decl != NULL);
23155 die = lookup_type_die (entry->type);
23156 if (die)
23158 /* Ask cgraph if the global variable really is to be emitted.
23159 If yes, then we'll keep the DIE of ENTRY->TYPE. */
23160 varpool_node *node = varpool_node::get (entry->var_decl);
23161 if (node && node->definition)
23163 die->die_perennial_p = 1;
23164 /* Keep the parent DIEs as well. */
23165 while ((die = die->die_parent) && die->die_perennial_p == 0)
23166 die->die_perennial_p = 1;
23169 return 1;
23172 /* Mark all members of used_types_hash as perennial. */
23174 static void
23175 premark_used_types (struct function *fun)
23177 if (fun && fun->used_types_hash)
23178 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
23181 /* Mark all members of types_used_by_vars_entry as perennial. */
23183 static void
23184 premark_types_used_by_global_vars (void)
23186 if (types_used_by_vars_hash)
23187 types_used_by_vars_hash
23188 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
23191 /* Mark all variables used by the symtab as perennial. */
23193 static void
23194 premark_used_variables (void)
23196 /* Mark DIEs in the symtab as used. */
23197 varpool_node *var;
23198 FOR_EACH_VARIABLE (var)
23200 dw_die_ref die = lookup_decl_die (var->decl);
23201 if (die)
23202 die->die_perennial_p = 1;
23206 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
23207 for CA_LOC call arg loc node. */
23209 static dw_die_ref
23210 gen_call_site_die (tree decl, dw_die_ref subr_die,
23211 struct call_arg_loc_node *ca_loc)
23213 dw_die_ref stmt_die = NULL, die;
23214 tree block = ca_loc->block;
23216 while (block
23217 && block != DECL_INITIAL (decl)
23218 && TREE_CODE (block) == BLOCK)
23220 stmt_die = lookup_block_die (block);
23221 if (stmt_die)
23222 break;
23223 block = BLOCK_SUPERCONTEXT (block);
23225 if (stmt_die == NULL)
23226 stmt_die = subr_die;
23227 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
23228 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
23229 if (ca_loc->tail_call_p)
23230 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
23231 if (ca_loc->symbol_ref)
23233 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
23234 if (tdie)
23235 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
23236 else
23237 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
23238 false);
23240 return die;
23243 /* Generate a DIE to represent a declared function (either file-scope or
23244 block-local). */
23246 static void
23247 gen_subprogram_die (tree decl, dw_die_ref context_die)
23249 tree origin = decl_ultimate_origin (decl);
23250 dw_die_ref subr_die;
23251 dw_die_ref old_die = lookup_decl_die (decl);
23252 bool old_die_had_no_children = false;
23254 /* This function gets called multiple times for different stages of
23255 the debug process. For example, for func() in this code:
23257 namespace S
23259 void func() { ... }
23262 ...we get called 4 times. Twice in early debug and twice in
23263 late debug:
23265 Early debug
23266 -----------
23268 1. Once while generating func() within the namespace. This is
23269 the declaration. The declaration bit below is set, as the
23270 context is the namespace.
23272 A new DIE will be generated with DW_AT_declaration set.
23274 2. Once for func() itself. This is the specification. The
23275 declaration bit below is clear as the context is the CU.
23277 We will use the cached DIE from (1) to create a new DIE with
23278 DW_AT_specification pointing to the declaration in (1).
23280 Late debug via rest_of_handle_final()
23281 -------------------------------------
23283 3. Once generating func() within the namespace. This is also the
23284 declaration, as in (1), but this time we will early exit below
23285 as we have a cached DIE and a declaration needs no additional
23286 annotations (no locations), as the source declaration line
23287 info is enough.
23289 4. Once for func() itself. As in (2), this is the specification,
23290 but this time we will re-use the cached DIE, and just annotate
23291 it with the location information that should now be available.
23293 For something without namespaces, but with abstract instances, we
23294 are also called a multiple times:
23296 class Base
23298 public:
23299 Base (); // constructor declaration (1)
23302 Base::Base () { } // constructor specification (2)
23304 Early debug
23305 -----------
23307 1. Once for the Base() constructor by virtue of it being a
23308 member of the Base class. This is done via
23309 rest_of_type_compilation.
23311 This is a declaration, so a new DIE will be created with
23312 DW_AT_declaration.
23314 2. Once for the Base() constructor definition, but this time
23315 while generating the abstract instance of the base
23316 constructor (__base_ctor) which is being generated via early
23317 debug of reachable functions.
23319 Even though we have a cached version of the declaration (1),
23320 we will create a DW_AT_specification of the declaration DIE
23321 in (1).
23323 3. Once for the __base_ctor itself, but this time, we generate
23324 an DW_AT_abstract_origin version of the DW_AT_specification in
23325 (2).
23327 Late debug via rest_of_handle_final
23328 -----------------------------------
23330 4. One final time for the __base_ctor (which will have a cached
23331 DIE with DW_AT_abstract_origin created in (3). This time,
23332 we will just annotate the location information now
23333 available.
23335 int declaration = (current_function_decl != decl
23336 || (!DECL_INITIAL (decl) && !origin)
23337 || class_or_namespace_scope_p (context_die));
23339 /* A declaration that has been previously dumped needs no
23340 additional information. */
23341 if (old_die && declaration)
23342 return;
23344 if (in_lto_p && old_die && old_die->die_child == NULL)
23345 old_die_had_no_children = true;
23347 /* Now that the C++ front end lazily declares artificial member fns, we
23348 might need to retrofit the declaration into its class. */
23349 if (!declaration && !origin && !old_die
23350 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
23351 && !class_or_namespace_scope_p (context_die)
23352 && debug_info_level > DINFO_LEVEL_TERSE)
23353 old_die = force_decl_die (decl);
23355 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
23356 if (origin != NULL)
23358 gcc_assert (!declaration || local_scope_p (context_die));
23360 /* Fixup die_parent for the abstract instance of a nested
23361 inline function. */
23362 if (old_die && old_die->die_parent == NULL)
23363 add_child_die (context_die, old_die);
23365 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
23367 /* If we have a DW_AT_abstract_origin we have a working
23368 cached version. */
23369 subr_die = old_die;
23371 else
23373 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23374 add_abstract_origin_attribute (subr_die, origin);
23375 /* This is where the actual code for a cloned function is.
23376 Let's emit linkage name attribute for it. This helps
23377 debuggers to e.g, set breakpoints into
23378 constructors/destructors when the user asks "break
23379 K::K". */
23380 add_linkage_name (subr_die, decl);
23383 /* A cached copy, possibly from early dwarf generation. Reuse as
23384 much as possible. */
23385 else if (old_die)
23387 if (!get_AT_flag (old_die, DW_AT_declaration)
23388 /* We can have a normal definition following an inline one in the
23389 case of redefinition of GNU C extern inlines.
23390 It seems reasonable to use AT_specification in this case. */
23391 && !get_AT (old_die, DW_AT_inline))
23393 /* Detect and ignore this case, where we are trying to output
23394 something we have already output. */
23395 if (get_AT (old_die, DW_AT_low_pc)
23396 || get_AT (old_die, DW_AT_ranges))
23397 return;
23399 /* If we have no location information, this must be a
23400 partially generated DIE from early dwarf generation.
23401 Fall through and generate it. */
23404 /* If the definition comes from the same place as the declaration,
23405 maybe use the old DIE. We always want the DIE for this function
23406 that has the *_pc attributes to be under comp_unit_die so the
23407 debugger can find it. We also need to do this for abstract
23408 instances of inlines, since the spec requires the out-of-line copy
23409 to have the same parent. For local class methods, this doesn't
23410 apply; we just use the old DIE. */
23411 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23412 struct dwarf_file_data * file_index = lookup_filename (s.file);
23413 if (((is_unit_die (old_die->die_parent)
23414 /* This condition fixes the inconsistency/ICE with the
23415 following Fortran test (or some derivative thereof) while
23416 building libgfortran:
23418 module some_m
23419 contains
23420 logical function funky (FLAG)
23421 funky = .true.
23422 end function
23423 end module
23425 || (old_die->die_parent
23426 && old_die->die_parent->die_tag == DW_TAG_module)
23427 || local_scope_p (old_die->die_parent)
23428 || context_die == NULL)
23429 && (DECL_ARTIFICIAL (decl)
23430 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
23431 && (get_AT_unsigned (old_die, DW_AT_decl_line)
23432 == (unsigned) s.line)
23433 && (!debug_column_info
23434 || s.column == 0
23435 || (get_AT_unsigned (old_die, DW_AT_decl_column)
23436 == (unsigned) s.column)))))
23437 /* With LTO if there's an abstract instance for
23438 the old DIE, this is a concrete instance and
23439 thus re-use the DIE. */
23440 || get_AT (old_die, DW_AT_abstract_origin))
23442 subr_die = old_die;
23444 /* Clear out the declaration attribute, but leave the
23445 parameters so they can be augmented with location
23446 information later. Unless this was a declaration, in
23447 which case, wipe out the nameless parameters and recreate
23448 them further down. */
23449 if (remove_AT (subr_die, DW_AT_declaration))
23452 remove_AT (subr_die, DW_AT_object_pointer);
23453 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
23456 /* Make a specification pointing to the previously built
23457 declaration. */
23458 else
23460 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23461 add_AT_specification (subr_die, old_die);
23462 add_pubname (decl, subr_die);
23463 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23464 add_AT_file (subr_die, DW_AT_decl_file, file_index);
23465 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23466 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
23467 if (debug_column_info
23468 && s.column
23469 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23470 != (unsigned) s.column))
23471 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
23473 /* If the prototype had an 'auto' or 'decltype(auto)' in
23474 the return type, emit the real type on the definition die. */
23475 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
23477 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
23478 while (die
23479 && (die->die_tag == DW_TAG_reference_type
23480 || die->die_tag == DW_TAG_rvalue_reference_type
23481 || die->die_tag == DW_TAG_pointer_type
23482 || die->die_tag == DW_TAG_const_type
23483 || die->die_tag == DW_TAG_volatile_type
23484 || die->die_tag == DW_TAG_restrict_type
23485 || die->die_tag == DW_TAG_array_type
23486 || die->die_tag == DW_TAG_ptr_to_member_type
23487 || die->die_tag == DW_TAG_subroutine_type))
23488 die = get_AT_ref (die, DW_AT_type);
23489 if (die == auto_die || die == decltype_auto_die)
23490 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23491 TYPE_UNQUALIFIED, false, context_die);
23494 /* When we process the method declaration, we haven't seen
23495 the out-of-class defaulted definition yet, so we have to
23496 recheck now. */
23497 if ((dwarf_version >= 5 || ! dwarf_strict)
23498 && !get_AT (subr_die, DW_AT_defaulted))
23500 int defaulted
23501 = lang_hooks.decls.decl_dwarf_attribute (decl,
23502 DW_AT_defaulted);
23503 if (defaulted != -1)
23505 /* Other values must have been handled before. */
23506 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
23507 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23512 /* Create a fresh DIE for anything else. */
23513 else
23515 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
23517 if (TREE_PUBLIC (decl))
23518 add_AT_flag (subr_die, DW_AT_external, 1);
23520 add_name_and_src_coords_attributes (subr_die, decl);
23521 add_pubname (decl, subr_die);
23522 if (debug_info_level > DINFO_LEVEL_TERSE)
23524 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
23525 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
23526 TYPE_UNQUALIFIED, false, context_die);
23529 add_pure_or_virtual_attribute (subr_die, decl);
23530 if (DECL_ARTIFICIAL (decl))
23531 add_AT_flag (subr_die, DW_AT_artificial, 1);
23533 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
23534 add_AT_flag (subr_die, DW_AT_noreturn, 1);
23536 add_alignment_attribute (subr_die, decl);
23538 add_accessibility_attribute (subr_die, decl);
23541 /* Unless we have an existing non-declaration DIE, equate the new
23542 DIE. */
23543 if (!old_die || is_declaration_die (old_die))
23544 equate_decl_number_to_die (decl, subr_die);
23546 if (declaration)
23548 if (!old_die || !get_AT (old_die, DW_AT_inline))
23550 add_AT_flag (subr_die, DW_AT_declaration, 1);
23552 /* If this is an explicit function declaration then generate
23553 a DW_AT_explicit attribute. */
23554 if ((dwarf_version >= 3 || !dwarf_strict)
23555 && lang_hooks.decls.decl_dwarf_attribute (decl,
23556 DW_AT_explicit) == 1)
23557 add_AT_flag (subr_die, DW_AT_explicit, 1);
23559 /* If this is a C++11 deleted special function member then generate
23560 a DW_AT_deleted attribute. */
23561 if ((dwarf_version >= 5 || !dwarf_strict)
23562 && lang_hooks.decls.decl_dwarf_attribute (decl,
23563 DW_AT_deleted) == 1)
23564 add_AT_flag (subr_die, DW_AT_deleted, 1);
23566 /* If this is a C++11 defaulted special function member then
23567 generate a DW_AT_defaulted attribute. */
23568 if (dwarf_version >= 5 || !dwarf_strict)
23570 int defaulted
23571 = lang_hooks.decls.decl_dwarf_attribute (decl,
23572 DW_AT_defaulted);
23573 if (defaulted != -1)
23574 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
23577 /* If this is a C++11 non-static member function with & ref-qualifier
23578 then generate a DW_AT_reference attribute. */
23579 if ((dwarf_version >= 5 || !dwarf_strict)
23580 && lang_hooks.decls.decl_dwarf_attribute (decl,
23581 DW_AT_reference) == 1)
23582 add_AT_flag (subr_die, DW_AT_reference, 1);
23584 /* If this is a C++11 non-static member function with &&
23585 ref-qualifier then generate a DW_AT_reference attribute. */
23586 if ((dwarf_version >= 5 || !dwarf_strict)
23587 && lang_hooks.decls.decl_dwarf_attribute (decl,
23588 DW_AT_rvalue_reference)
23589 == 1)
23590 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
23593 /* For non DECL_EXTERNALs, if range information is available, fill
23594 the DIE with it. */
23595 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
23597 HOST_WIDE_INT cfa_fb_offset;
23599 struct function *fun = DECL_STRUCT_FUNCTION (decl);
23601 if (!crtl->has_bb_partition)
23603 dw_fde_ref fde = fun->fde;
23604 if (fde->dw_fde_begin)
23606 /* We have already generated the labels. */
23607 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23608 fde->dw_fde_end, false);
23610 else
23612 /* Create start/end labels and add the range. */
23613 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
23614 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
23615 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
23616 current_function_funcdef_no);
23617 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
23618 current_function_funcdef_no);
23619 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
23620 false);
23623 #if VMS_DEBUGGING_INFO
23624 /* HP OpenVMS Industry Standard 64: DWARF Extensions
23625 Section 2.3 Prologue and Epilogue Attributes:
23626 When a breakpoint is set on entry to a function, it is generally
23627 desirable for execution to be suspended, not on the very first
23628 instruction of the function, but rather at a point after the
23629 function's frame has been set up, after any language defined local
23630 declaration processing has been completed, and before execution of
23631 the first statement of the function begins. Debuggers generally
23632 cannot properly determine where this point is. Similarly for a
23633 breakpoint set on exit from a function. The prologue and epilogue
23634 attributes allow a compiler to communicate the location(s) to use. */
23637 if (fde->dw_fde_vms_end_prologue)
23638 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
23639 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
23641 if (fde->dw_fde_vms_begin_epilogue)
23642 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
23643 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23645 #endif
23648 else
23650 /* Generate pubnames entries for the split function code ranges. */
23651 dw_fde_ref fde = fun->fde;
23653 if (fde->dw_fde_second_begin)
23655 if (dwarf_version >= 3 || !dwarf_strict)
23657 /* We should use ranges for non-contiguous code section
23658 addresses. Use the actual code range for the initial
23659 section, since the HOT/COLD labels might precede an
23660 alignment offset. */
23661 bool range_list_added = false;
23662 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23663 fde->dw_fde_end, &range_list_added,
23664 false);
23665 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23666 fde->dw_fde_second_end,
23667 &range_list_added, false);
23668 if (range_list_added)
23669 add_ranges (NULL);
23671 else
23673 /* There is no real support in DW2 for this .. so we make
23674 a work-around. First, emit the pub name for the segment
23675 containing the function label. Then make and emit a
23676 simplified subprogram DIE for the second segment with the
23677 name pre-fixed by __hot/cold_sect_of_. We use the same
23678 linkage name for the second die so that gdb will find both
23679 sections when given "b foo". */
23680 const char *name = NULL;
23681 tree decl_name = DECL_NAME (decl);
23682 dw_die_ref seg_die;
23684 /* Do the 'primary' section. */
23685 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23686 fde->dw_fde_end, false);
23688 /* Build a minimal DIE for the secondary section. */
23689 seg_die = new_die (DW_TAG_subprogram,
23690 subr_die->die_parent, decl);
23692 if (TREE_PUBLIC (decl))
23693 add_AT_flag (seg_die, DW_AT_external, 1);
23695 if (decl_name != NULL
23696 && IDENTIFIER_POINTER (decl_name) != NULL)
23698 name = dwarf2_name (decl, 1);
23699 if (! DECL_ARTIFICIAL (decl))
23700 add_src_coords_attributes (seg_die, decl);
23702 add_linkage_name (seg_die, decl);
23704 gcc_assert (name != NULL);
23705 add_pure_or_virtual_attribute (seg_die, decl);
23706 if (DECL_ARTIFICIAL (decl))
23707 add_AT_flag (seg_die, DW_AT_artificial, 1);
23709 name = concat ("__second_sect_of_", name, NULL);
23710 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23711 fde->dw_fde_second_end, false);
23712 add_name_attribute (seg_die, name);
23713 if (want_pubnames ())
23714 add_pubname_string (name, seg_die);
23717 else
23718 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23719 false);
23722 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23724 /* We define the "frame base" as the function's CFA. This is more
23725 convenient for several reasons: (1) It's stable across the prologue
23726 and epilogue, which makes it better than just a frame pointer,
23727 (2) With dwarf3, there exists a one-byte encoding that allows us
23728 to reference the .debug_frame data by proxy, but failing that,
23729 (3) We can at least reuse the code inspection and interpretation
23730 code that determines the CFA position at various points in the
23731 function. */
23732 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23734 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23735 add_AT_loc (subr_die, DW_AT_frame_base, op);
23737 else
23739 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23740 if (list->dw_loc_next)
23741 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23742 else
23743 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23746 /* Compute a displacement from the "steady-state frame pointer" to
23747 the CFA. The former is what all stack slots and argument slots
23748 will reference in the rtl; the latter is what we've told the
23749 debugger about. We'll need to adjust all frame_base references
23750 by this displacement. */
23751 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23753 if (fun->static_chain_decl)
23755 /* DWARF requires here a location expression that computes the
23756 address of the enclosing subprogram's frame base. The machinery
23757 in tree-nested.c is supposed to store this specific address in the
23758 last field of the FRAME record. */
23759 const tree frame_type
23760 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23761 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23763 tree fb_expr
23764 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23765 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23766 fb_expr, fb_decl, NULL_TREE);
23768 add_AT_location_description (subr_die, DW_AT_static_link,
23769 loc_list_from_tree (fb_expr, 0, NULL));
23772 resolve_variable_values ();
23775 /* Generate child dies for template parameters. */
23776 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23777 gen_generic_params_dies (decl);
23779 /* Now output descriptions of the arguments for this function. This gets
23780 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23781 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23782 `...' at the end of the formal parameter list. In order to find out if
23783 there was a trailing ellipsis or not, we must instead look at the type
23784 associated with the FUNCTION_DECL. This will be a node of type
23785 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23786 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23787 an ellipsis at the end. */
23789 /* In the case where we are describing a mere function declaration, all we
23790 need to do here (and all we *can* do here) is to describe the *types* of
23791 its formal parameters. */
23792 if (debug_info_level <= DINFO_LEVEL_TERSE)
23794 else if (declaration)
23795 gen_formal_types_die (decl, subr_die);
23796 else
23798 /* Generate DIEs to represent all known formal parameters. */
23799 tree parm = DECL_ARGUMENTS (decl);
23800 tree generic_decl = early_dwarf
23801 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23802 tree generic_decl_parm = generic_decl
23803 ? DECL_ARGUMENTS (generic_decl)
23804 : NULL;
23806 /* Now we want to walk the list of parameters of the function and
23807 emit their relevant DIEs.
23809 We consider the case of DECL being an instance of a generic function
23810 as well as it being a normal function.
23812 If DECL is an instance of a generic function we walk the
23813 parameters of the generic function declaration _and_ the parameters of
23814 DECL itself. This is useful because we want to emit specific DIEs for
23815 function parameter packs and those are declared as part of the
23816 generic function declaration. In that particular case,
23817 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23818 That DIE has children DIEs representing the set of arguments
23819 of the pack. Note that the set of pack arguments can be empty.
23820 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23821 children DIE.
23823 Otherwise, we just consider the parameters of DECL. */
23824 while (generic_decl_parm || parm)
23826 if (generic_decl_parm
23827 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23828 gen_formal_parameter_pack_die (generic_decl_parm,
23829 parm, subr_die,
23830 &parm);
23831 else if (parm)
23833 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23835 if (early_dwarf
23836 && parm == DECL_ARGUMENTS (decl)
23837 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23838 && parm_die
23839 && (dwarf_version >= 3 || !dwarf_strict))
23840 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23842 parm = DECL_CHAIN (parm);
23845 if (generic_decl_parm)
23846 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23849 /* Decide whether we need an unspecified_parameters DIE at the end.
23850 There are 2 more cases to do this for: 1) the ansi ... declaration -
23851 this is detectable when the end of the arg list is not a
23852 void_type_node 2) an unprototyped function declaration (not a
23853 definition). This just means that we have no info about the
23854 parameters at all. */
23855 if (early_dwarf)
23857 if (prototype_p (TREE_TYPE (decl)))
23859 /* This is the prototyped case, check for.... */
23860 if (stdarg_p (TREE_TYPE (decl)))
23861 gen_unspecified_parameters_die (decl, subr_die);
23863 else if (DECL_INITIAL (decl) == NULL_TREE)
23864 gen_unspecified_parameters_die (decl, subr_die);
23866 else if ((subr_die != old_die || old_die_had_no_children)
23867 && prototype_p (TREE_TYPE (decl))
23868 && stdarg_p (TREE_TYPE (decl)))
23869 gen_unspecified_parameters_die (decl, subr_die);
23872 if (subr_die != old_die)
23873 /* Add the calling convention attribute if requested. */
23874 add_calling_convention_attribute (subr_die, decl);
23876 /* Output Dwarf info for all of the stuff within the body of the function
23877 (if it has one - it may be just a declaration).
23879 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23880 a function. This BLOCK actually represents the outermost binding contour
23881 for the function, i.e. the contour in which the function's formal
23882 parameters and labels get declared. Curiously, it appears that the front
23883 end doesn't actually put the PARM_DECL nodes for the current function onto
23884 the BLOCK_VARS list for this outer scope, but are strung off of the
23885 DECL_ARGUMENTS list for the function instead.
23887 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23888 the LABEL_DECL nodes for the function however, and we output DWARF info
23889 for those in decls_for_scope. Just within the `outer_scope' there will be
23890 a BLOCK node representing the function's outermost pair of curly braces,
23891 and any blocks used for the base and member initializers of a C++
23892 constructor function. */
23893 tree outer_scope = DECL_INITIAL (decl);
23894 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23896 int call_site_note_count = 0;
23897 int tail_call_site_note_count = 0;
23899 /* Emit a DW_TAG_variable DIE for a named return value. */
23900 if (DECL_NAME (DECL_RESULT (decl)))
23901 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23903 /* The first time through decls_for_scope we will generate the
23904 DIEs for the locals. The second time, we fill in the
23905 location info. */
23906 decls_for_scope (outer_scope, subr_die);
23908 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23910 struct call_arg_loc_node *ca_loc;
23911 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23913 dw_die_ref die = NULL;
23914 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23915 rtx arg, next_arg;
23916 tree arg_decl = NULL_TREE;
23918 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23919 ? XEXP (ca_loc->call_arg_loc_note, 0)
23920 : NULL_RTX);
23921 arg; arg = next_arg)
23923 dw_loc_descr_ref reg, val;
23924 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23925 dw_die_ref cdie, tdie = NULL;
23927 next_arg = XEXP (arg, 1);
23928 if (REG_P (XEXP (XEXP (arg, 0), 0))
23929 && next_arg
23930 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23931 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23932 && REGNO (XEXP (XEXP (arg, 0), 0))
23933 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23934 next_arg = XEXP (next_arg, 1);
23935 if (mode == VOIDmode)
23937 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23938 if (mode == VOIDmode)
23939 mode = GET_MODE (XEXP (arg, 0));
23941 if (mode == VOIDmode || mode == BLKmode)
23942 continue;
23943 /* Get dynamic information about call target only if we
23944 have no static information: we cannot generate both
23945 DW_AT_call_origin and DW_AT_call_target
23946 attributes. */
23947 if (ca_loc->symbol_ref == NULL_RTX)
23949 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23951 tloc = XEXP (XEXP (arg, 0), 1);
23952 continue;
23954 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23955 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23957 tlocc = XEXP (XEXP (arg, 0), 1);
23958 continue;
23961 reg = NULL;
23962 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23963 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23964 VAR_INIT_STATUS_INITIALIZED);
23965 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23967 rtx mem = XEXP (XEXP (arg, 0), 0);
23968 reg = mem_loc_descriptor (XEXP (mem, 0),
23969 get_address_mode (mem),
23970 GET_MODE (mem),
23971 VAR_INIT_STATUS_INITIALIZED);
23973 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23974 == DEBUG_PARAMETER_REF)
23976 tree tdecl
23977 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23978 tdie = lookup_decl_die (tdecl);
23979 if (tdie == NULL)
23980 continue;
23981 arg_decl = tdecl;
23983 else
23984 continue;
23985 if (reg == NULL
23986 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23987 != DEBUG_PARAMETER_REF)
23988 continue;
23989 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23990 VOIDmode,
23991 VAR_INIT_STATUS_INITIALIZED);
23992 if (val == NULL)
23993 continue;
23994 if (die == NULL)
23995 die = gen_call_site_die (decl, subr_die, ca_loc);
23996 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23997 NULL_TREE);
23998 add_desc_attribute (cdie, arg_decl);
23999 if (reg != NULL)
24000 add_AT_loc (cdie, DW_AT_location, reg);
24001 else if (tdie != NULL)
24002 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
24003 tdie);
24004 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
24005 if (next_arg != XEXP (arg, 1))
24007 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
24008 if (mode == VOIDmode)
24009 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
24010 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
24011 0), 1),
24012 mode, VOIDmode,
24013 VAR_INIT_STATUS_INITIALIZED);
24014 if (val != NULL)
24015 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
24016 val);
24019 if (die == NULL
24020 && (ca_loc->symbol_ref || tloc))
24021 die = gen_call_site_die (decl, subr_die, ca_loc);
24022 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
24024 dw_loc_descr_ref tval = NULL;
24026 if (tloc != NULL_RTX)
24027 tval = mem_loc_descriptor (tloc,
24028 GET_MODE (tloc) == VOIDmode
24029 ? Pmode : GET_MODE (tloc),
24030 VOIDmode,
24031 VAR_INIT_STATUS_INITIALIZED);
24032 if (tval)
24033 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
24034 else if (tlocc != NULL_RTX)
24036 tval = mem_loc_descriptor (tlocc,
24037 GET_MODE (tlocc) == VOIDmode
24038 ? Pmode : GET_MODE (tlocc),
24039 VOIDmode,
24040 VAR_INIT_STATUS_INITIALIZED);
24041 if (tval)
24042 add_AT_loc (die,
24043 dwarf_AT (DW_AT_call_target_clobbered),
24044 tval);
24047 if (die != NULL)
24049 call_site_note_count++;
24050 if (ca_loc->tail_call_p)
24051 tail_call_site_note_count++;
24055 call_arg_locations = NULL;
24056 call_arg_loc_last = NULL;
24057 if (tail_call_site_count >= 0
24058 && tail_call_site_count == tail_call_site_note_count
24059 && (!dwarf_strict || dwarf_version >= 5))
24061 if (call_site_count >= 0
24062 && call_site_count == call_site_note_count)
24063 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
24064 else
24065 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
24067 call_site_count = -1;
24068 tail_call_site_count = -1;
24071 /* Mark used types after we have created DIEs for the functions scopes. */
24072 premark_used_types (DECL_STRUCT_FUNCTION (decl));
24075 /* Returns a hash value for X (which really is a die_struct). */
24077 hashval_t
24078 block_die_hasher::hash (die_struct *d)
24080 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
24083 /* Return nonzero if decl_id and die_parent of die_struct X is the same
24084 as decl_id and die_parent of die_struct Y. */
24086 bool
24087 block_die_hasher::equal (die_struct *x, die_struct *y)
24089 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
24092 /* Hold information about markers for inlined entry points. */
24093 struct GTY ((for_user)) inline_entry_data
24095 /* The block that's the inlined_function_outer_scope for an inlined
24096 function. */
24097 tree block;
24099 /* The label at the inlined entry point. */
24100 const char *label_pfx;
24101 unsigned int label_num;
24103 /* The view number to be used as the inlined entry point. */
24104 var_loc_view view;
24107 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
24109 typedef tree compare_type;
24110 static inline hashval_t hash (const inline_entry_data *);
24111 static inline bool equal (const inline_entry_data *, const_tree);
24114 /* Hash table routines for inline_entry_data. */
24116 inline hashval_t
24117 inline_entry_data_hasher::hash (const inline_entry_data *data)
24119 return htab_hash_pointer (data->block);
24122 inline bool
24123 inline_entry_data_hasher::equal (const inline_entry_data *data,
24124 const_tree block)
24126 return data->block == block;
24129 /* Inlined entry points pending DIE creation in this compilation unit. */
24131 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
24134 /* Return TRUE if DECL, which may have been previously generated as
24135 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
24136 true if decl (or its origin) is either an extern declaration or a
24137 class/namespace scoped declaration.
24139 The declare_in_namespace support causes us to get two DIEs for one
24140 variable, both of which are declarations. We want to avoid
24141 considering one to be a specification, so we must test for
24142 DECLARATION and DW_AT_declaration. */
24143 static inline bool
24144 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
24146 return (old_die && TREE_STATIC (decl) && !declaration
24147 && get_AT_flag (old_die, DW_AT_declaration) == 1);
24150 /* Return true if DECL is a local static. */
24152 static inline bool
24153 local_function_static (tree decl)
24155 gcc_assert (VAR_P (decl));
24156 return TREE_STATIC (decl)
24157 && DECL_CONTEXT (decl)
24158 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
24161 /* Return true iff DECL overrides (presumably completes) the type of
24162 OLD_DIE within CONTEXT_DIE. */
24164 static bool
24165 override_type_for_decl_p (tree decl, dw_die_ref old_die,
24166 dw_die_ref context_die)
24168 tree type = TREE_TYPE (decl);
24169 int cv_quals;
24171 if (decl_by_reference_p (decl))
24173 type = TREE_TYPE (type);
24174 cv_quals = TYPE_UNQUALIFIED;
24176 else
24177 cv_quals = decl_quals (decl);
24179 dw_die_ref type_die = modified_type_die (type,
24180 cv_quals | TYPE_QUALS (type),
24181 false,
24182 context_die);
24184 dw_die_ref old_type_die = get_AT_ref (old_die, DW_AT_type);
24186 return type_die != old_type_die;
24189 /* Generate a DIE to represent a declared data object.
24190 Either DECL or ORIGIN must be non-null. */
24192 static void
24193 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
24195 HOST_WIDE_INT off = 0;
24196 tree com_decl;
24197 tree decl_or_origin = decl ? decl : origin;
24198 tree ultimate_origin;
24199 dw_die_ref var_die;
24200 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
24201 bool declaration = (DECL_EXTERNAL (decl_or_origin)
24202 || class_or_namespace_scope_p (context_die));
24203 bool specialization_p = false;
24204 bool no_linkage_name = false;
24206 /* While C++ inline static data members have definitions inside of the
24207 class, force the first DIE to be a declaration, then let gen_member_die
24208 reparent it to the class context and call gen_variable_die again
24209 to create the outside of the class DIE for the definition. */
24210 if (!declaration
24211 && old_die == NULL
24212 && decl
24213 && DECL_CONTEXT (decl)
24214 && TYPE_P (DECL_CONTEXT (decl))
24215 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
24217 declaration = true;
24218 if (dwarf_version < 5)
24219 no_linkage_name = true;
24222 ultimate_origin = decl_ultimate_origin (decl_or_origin);
24223 if (decl || ultimate_origin)
24224 origin = ultimate_origin;
24225 com_decl = fortran_common (decl_or_origin, &off);
24227 /* Symbol in common gets emitted as a child of the common block, in the form
24228 of a data member. */
24229 if (com_decl)
24231 dw_die_ref com_die;
24232 dw_loc_list_ref loc = NULL;
24233 die_node com_die_arg;
24235 var_die = lookup_decl_die (decl_or_origin);
24236 if (var_die)
24238 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
24240 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
24241 if (loc)
24243 if (off)
24245 /* Optimize the common case. */
24246 if (single_element_loc_list_p (loc)
24247 && loc->expr->dw_loc_opc == DW_OP_addr
24248 && loc->expr->dw_loc_next == NULL
24249 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
24250 == SYMBOL_REF)
24252 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24253 loc->expr->dw_loc_oprnd1.v.val_addr
24254 = plus_constant (GET_MODE (x), x , off);
24256 else
24257 loc_list_plus_const (loc, off);
24259 add_AT_location_description (var_die, DW_AT_location, loc);
24260 remove_AT (var_die, DW_AT_declaration);
24263 return;
24266 if (common_block_die_table == NULL)
24267 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
24269 com_die_arg.decl_id = DECL_UID (com_decl);
24270 com_die_arg.die_parent = context_die;
24271 com_die = common_block_die_table->find (&com_die_arg);
24272 if (! early_dwarf)
24273 loc = loc_list_from_tree (com_decl, 2, NULL);
24274 if (com_die == NULL)
24276 const char *cnam
24277 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
24278 die_node **slot;
24280 com_die = new_die (DW_TAG_common_block, context_die, decl);
24281 add_name_and_src_coords_attributes (com_die, com_decl);
24282 if (loc)
24284 add_AT_location_description (com_die, DW_AT_location, loc);
24285 /* Avoid sharing the same loc descriptor between
24286 DW_TAG_common_block and DW_TAG_variable. */
24287 loc = loc_list_from_tree (com_decl, 2, NULL);
24289 else if (DECL_EXTERNAL (decl_or_origin))
24290 add_AT_flag (com_die, DW_AT_declaration, 1);
24291 if (want_pubnames ())
24292 add_pubname_string (cnam, com_die); /* ??? needed? */
24293 com_die->decl_id = DECL_UID (com_decl);
24294 slot = common_block_die_table->find_slot (com_die, INSERT);
24295 *slot = com_die;
24297 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
24299 add_AT_location_description (com_die, DW_AT_location, loc);
24300 loc = loc_list_from_tree (com_decl, 2, NULL);
24301 remove_AT (com_die, DW_AT_declaration);
24303 var_die = new_die (DW_TAG_variable, com_die, decl);
24304 add_name_and_src_coords_attributes (var_die, decl_or_origin);
24305 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
24306 decl_quals (decl_or_origin), false,
24307 context_die);
24308 add_alignment_attribute (var_die, decl);
24309 add_AT_flag (var_die, DW_AT_external, 1);
24310 if (loc)
24312 if (off)
24314 /* Optimize the common case. */
24315 if (single_element_loc_list_p (loc)
24316 && loc->expr->dw_loc_opc == DW_OP_addr
24317 && loc->expr->dw_loc_next == NULL
24318 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
24320 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
24321 loc->expr->dw_loc_oprnd1.v.val_addr
24322 = plus_constant (GET_MODE (x), x, off);
24324 else
24325 loc_list_plus_const (loc, off);
24327 add_AT_location_description (var_die, DW_AT_location, loc);
24329 else if (DECL_EXTERNAL (decl_or_origin))
24330 add_AT_flag (var_die, DW_AT_declaration, 1);
24331 if (decl)
24332 equate_decl_number_to_die (decl, var_die);
24333 return;
24336 if (old_die)
24338 if (declaration)
24340 /* A declaration that has been previously dumped, needs no
24341 further annotations, since it doesn't need location on
24342 the second pass. */
24343 return;
24345 else if (decl_will_get_specification_p (old_die, decl, declaration)
24346 && !get_AT (old_die, DW_AT_specification))
24348 /* Fall-thru so we can make a new variable die along with a
24349 DW_AT_specification. */
24351 else if (origin && old_die->die_parent != context_die)
24353 /* If we will be creating an inlined instance, we need a
24354 new DIE that will get annotated with
24355 DW_AT_abstract_origin. */
24356 gcc_assert (!DECL_ABSTRACT_P (decl));
24358 else
24360 /* If a DIE was dumped early, it still needs location info.
24361 Skip to where we fill the location bits. */
24362 var_die = old_die;
24364 /* ??? In LTRANS we cannot annotate early created variably
24365 modified type DIEs without copying them and adjusting all
24366 references to them. Thus we dumped them again. Also add a
24367 reference to them but beware of -g0 compile and -g link
24368 in which case the reference will be already present. */
24369 tree type = TREE_TYPE (decl_or_origin);
24370 if (in_lto_p
24371 && ! get_AT (var_die, DW_AT_type)
24372 && variably_modified_type_p
24373 (type, decl_function_context (decl_or_origin)))
24375 if (decl_by_reference_p (decl_or_origin))
24376 add_type_attribute (var_die, TREE_TYPE (type),
24377 TYPE_UNQUALIFIED, false, context_die);
24378 else
24379 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
24380 false, context_die);
24383 goto gen_variable_die_location;
24387 /* For static data members, the declaration in the class is supposed
24388 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
24389 also in DWARF2; the specification should still be DW_TAG_variable
24390 referencing the DW_TAG_member DIE. */
24391 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
24392 var_die = new_die (DW_TAG_member, context_die, decl);
24393 else
24394 var_die = new_die (DW_TAG_variable, context_die, decl);
24396 if (origin != NULL)
24397 add_abstract_origin_attribute (var_die, origin);
24399 /* Loop unrolling can create multiple blocks that refer to the same
24400 static variable, so we must test for the DW_AT_declaration flag.
24402 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
24403 copy decls and set the DECL_ABSTRACT_P flag on them instead of
24404 sharing them.
24406 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
24407 else if (decl_will_get_specification_p (old_die, decl, declaration))
24409 /* This is a definition of a C++ class level static. */
24410 add_AT_specification (var_die, old_die);
24411 specialization_p = true;
24412 if (DECL_NAME (decl))
24414 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
24415 struct dwarf_file_data * file_index = lookup_filename (s.file);
24417 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
24418 add_AT_file (var_die, DW_AT_decl_file, file_index);
24420 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
24421 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
24423 if (debug_column_info
24424 && s.column
24425 && (get_AT_unsigned (old_die, DW_AT_decl_column)
24426 != (unsigned) s.column))
24427 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
24429 if (old_die->die_tag == DW_TAG_member)
24430 add_linkage_name (var_die, decl);
24433 else
24434 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
24436 if ((origin == NULL && !specialization_p)
24437 || (origin != NULL
24438 && !DECL_ABSTRACT_P (decl_or_origin)
24439 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
24440 decl_function_context
24441 (decl_or_origin)))
24442 || (old_die && specialization_p
24443 && override_type_for_decl_p (decl_or_origin, old_die, context_die)))
24445 tree type = TREE_TYPE (decl_or_origin);
24447 if (decl_by_reference_p (decl_or_origin))
24448 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24449 context_die);
24450 else
24451 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
24452 context_die);
24455 if (origin == NULL && !specialization_p)
24457 if (TREE_PUBLIC (decl))
24458 add_AT_flag (var_die, DW_AT_external, 1);
24460 if (DECL_ARTIFICIAL (decl))
24461 add_AT_flag (var_die, DW_AT_artificial, 1);
24463 add_alignment_attribute (var_die, decl);
24465 add_accessibility_attribute (var_die, decl);
24468 if (declaration)
24469 add_AT_flag (var_die, DW_AT_declaration, 1);
24471 if (decl && (DECL_ABSTRACT_P (decl)
24472 || !old_die || is_declaration_die (old_die)))
24473 equate_decl_number_to_die (decl, var_die);
24475 gen_variable_die_location:
24476 if (! declaration
24477 && (! DECL_ABSTRACT_P (decl_or_origin)
24478 /* Local static vars are shared between all clones/inlines,
24479 so emit DW_AT_location on the abstract DIE if DECL_RTL is
24480 already set. */
24481 || (VAR_P (decl_or_origin)
24482 && TREE_STATIC (decl_or_origin)
24483 && DECL_RTL_SET_P (decl_or_origin))))
24485 if (early_dwarf)
24487 add_pubname (decl_or_origin, var_die);
24488 /* For global register variables, emit DW_AT_location if possible
24489 already during early_dwarf, as late_global_decl won't be usually
24490 called. */
24491 if (DECL_HARD_REGISTER (decl_or_origin)
24492 && TREE_STATIC (decl_or_origin)
24493 && !decl_by_reference_p (decl_or_origin)
24494 && !get_AT (var_die, DW_AT_location)
24495 && !get_AT (var_die, DW_AT_const_value)
24496 && DECL_RTL_SET_P (decl_or_origin)
24497 && REG_P (DECL_RTL (decl_or_origin)))
24499 dw_loc_descr_ref descr
24500 = reg_loc_descriptor (DECL_RTL (decl_or_origin),
24501 VAR_INIT_STATUS_INITIALIZED);
24502 if (descr)
24503 add_AT_loc (var_die, DW_AT_location, descr);
24506 else
24507 add_location_or_const_value_attribute (var_die, decl_or_origin,
24508 decl == NULL);
24510 else
24511 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
24513 if ((dwarf_version >= 4 || !dwarf_strict)
24514 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24515 DW_AT_const_expr) == 1
24516 && !get_AT (var_die, DW_AT_const_expr)
24517 && !specialization_p)
24518 add_AT_flag (var_die, DW_AT_const_expr, 1);
24520 if (!dwarf_strict)
24522 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
24523 DW_AT_inline);
24524 if (inl != -1
24525 && !get_AT (var_die, DW_AT_inline)
24526 && !specialization_p)
24527 add_AT_unsigned (var_die, DW_AT_inline, inl);
24531 /* Generate a DIE to represent a named constant. */
24533 static void
24534 gen_const_die (tree decl, dw_die_ref context_die)
24536 dw_die_ref const_die;
24537 tree type = TREE_TYPE (decl);
24539 const_die = lookup_decl_die (decl);
24540 if (const_die)
24541 return;
24543 const_die = new_die (DW_TAG_constant, context_die, decl);
24544 equate_decl_number_to_die (decl, const_die);
24545 add_name_and_src_coords_attributes (const_die, decl);
24546 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
24547 if (TREE_PUBLIC (decl))
24548 add_AT_flag (const_die, DW_AT_external, 1);
24549 if (DECL_ARTIFICIAL (decl))
24550 add_AT_flag (const_die, DW_AT_artificial, 1);
24551 tree_add_const_value_attribute_for_decl (const_die, decl);
24554 /* Generate a DIE to represent a label identifier. */
24556 static void
24557 gen_label_die (tree decl, dw_die_ref context_die)
24559 tree origin = decl_ultimate_origin (decl);
24560 dw_die_ref lbl_die = lookup_decl_die (decl);
24561 rtx insn;
24562 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24564 if (!lbl_die)
24566 lbl_die = new_die (DW_TAG_label, context_die, decl);
24567 equate_decl_number_to_die (decl, lbl_die);
24569 if (origin != NULL)
24570 add_abstract_origin_attribute (lbl_die, origin);
24571 else
24572 add_name_and_src_coords_attributes (lbl_die, decl);
24575 if (DECL_ABSTRACT_P (decl))
24576 equate_decl_number_to_die (decl, lbl_die);
24577 else if (! early_dwarf)
24579 insn = DECL_RTL_IF_SET (decl);
24581 /* Deleted labels are programmer specified labels which have been
24582 eliminated because of various optimizations. We still emit them
24583 here so that it is possible to put breakpoints on them. */
24584 if (insn
24585 && (LABEL_P (insn)
24586 || ((NOTE_P (insn)
24587 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
24589 /* When optimization is enabled (via -O) some parts of the compiler
24590 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
24591 represent source-level labels which were explicitly declared by
24592 the user. This really shouldn't be happening though, so catch
24593 it if it ever does happen. */
24594 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
24596 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
24597 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24599 else if (insn
24600 && NOTE_P (insn)
24601 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
24602 && CODE_LABEL_NUMBER (insn) != -1)
24604 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
24605 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
24610 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
24611 attributes to the DIE for a block STMT, to describe where the inlined
24612 function was called from. This is similar to add_src_coords_attributes. */
24614 static inline void
24615 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
24617 /* We can end up with BUILTINS_LOCATION here. */
24618 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
24619 return;
24621 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
24623 if (dwarf_version >= 3 || !dwarf_strict)
24625 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
24626 add_AT_unsigned (die, DW_AT_call_line, s.line);
24627 if (debug_column_info && s.column)
24628 add_AT_unsigned (die, DW_AT_call_column, s.column);
24633 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
24634 Add low_pc and high_pc attributes to the DIE for a block STMT. */
24636 static inline void
24637 add_high_low_attributes (tree stmt, dw_die_ref die)
24639 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24641 if (inline_entry_data **iedp
24642 = !inline_entry_data_table ? NULL
24643 : inline_entry_data_table->find_slot_with_hash (stmt,
24644 htab_hash_pointer (stmt),
24645 NO_INSERT))
24647 inline_entry_data *ied = *iedp;
24648 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
24649 gcc_assert (debug_inline_points);
24650 gcc_assert (inlined_function_outer_scope_p (stmt));
24652 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
24653 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24655 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
24656 && !dwarf_strict)
24658 if (!output_asm_line_debug_info ())
24659 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
24660 else
24662 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
24663 /* FIXME: this will resolve to a small number. Could we
24664 possibly emit smaller data? Ideally we'd emit a
24665 uleb128, but that would make the size of DIEs
24666 impossible for the compiler to compute, since it's
24667 the assembler that computes the value of the view
24668 label in this case. Ideally, we'd have a single form
24669 encompassing both the address and the view, and
24670 indirecting them through a table might make things
24671 easier, but even that would be more wasteful,
24672 space-wise, than what we have now. */
24673 add_AT_symview (die, DW_AT_GNU_entry_view, label);
24677 inline_entry_data_table->clear_slot (iedp);
24680 if (BLOCK_FRAGMENT_CHAIN (stmt)
24681 && (dwarf_version >= 3 || !dwarf_strict))
24683 tree chain, superblock = NULL_TREE;
24684 dw_die_ref pdie;
24685 dw_attr_node *attr = NULL;
24687 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
24689 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24690 BLOCK_NUMBER (stmt));
24691 add_AT_lbl_id (die, DW_AT_entry_pc, label);
24694 /* Optimize duplicate .debug_ranges lists or even tails of
24695 lists. If this BLOCK has same ranges as its supercontext,
24696 lookup DW_AT_ranges attribute in the supercontext (and
24697 recursively so), verify that the ranges_table contains the
24698 right values and use it instead of adding a new .debug_range. */
24699 for (chain = stmt, pdie = die;
24700 BLOCK_SAME_RANGE (chain);
24701 chain = BLOCK_SUPERCONTEXT (chain))
24703 dw_attr_node *new_attr;
24705 pdie = pdie->die_parent;
24706 if (pdie == NULL)
24707 break;
24708 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24709 break;
24710 new_attr = get_AT (pdie, DW_AT_ranges);
24711 if (new_attr == NULL
24712 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24713 break;
24714 attr = new_attr;
24715 superblock = BLOCK_SUPERCONTEXT (chain);
24717 if (attr != NULL
24718 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24719 == (int)BLOCK_NUMBER (superblock))
24720 && BLOCK_FRAGMENT_CHAIN (superblock))
24722 unsigned long off = attr->dw_attr_val.v.val_offset;
24723 unsigned long supercnt = 0, thiscnt = 0;
24724 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24725 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24727 ++supercnt;
24728 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24729 == (int)BLOCK_NUMBER (chain));
24731 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24732 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24733 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24734 ++thiscnt;
24735 gcc_assert (supercnt >= thiscnt);
24736 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24737 false);
24738 note_rnglist_head (off + supercnt - thiscnt);
24739 return;
24742 unsigned int offset = add_ranges (stmt, true);
24743 add_AT_range_list (die, DW_AT_ranges, offset, false);
24744 note_rnglist_head (offset);
24746 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24747 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24750 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24751 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24752 chain = BLOCK_FRAGMENT_CHAIN (chain);
24754 while (chain);
24755 add_ranges (NULL);
24757 else
24759 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24760 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24761 BLOCK_NUMBER (stmt));
24762 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24763 BLOCK_NUMBER (stmt));
24764 add_AT_low_high_pc (die, label, label_high, false);
24768 /* Generate a DIE for a lexical block. */
24770 static void
24771 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24773 dw_die_ref old_die = lookup_block_die (stmt);
24774 dw_die_ref stmt_die = NULL;
24775 if (!old_die)
24777 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24778 equate_block_to_die (stmt, stmt_die);
24781 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24783 /* If this is an inlined or conrecte instance, create a new lexical
24784 die for anything below to attach DW_AT_abstract_origin to. */
24785 if (old_die)
24786 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24788 tree origin = block_ultimate_origin (stmt);
24789 if (origin != NULL_TREE && (origin != stmt || old_die))
24790 add_abstract_origin_attribute (stmt_die, origin);
24792 old_die = NULL;
24795 if (old_die)
24796 stmt_die = old_die;
24798 /* A non abstract block whose blocks have already been reordered
24799 should have the instruction range for this block. If so, set the
24800 high/low attributes. */
24801 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24803 gcc_assert (stmt_die);
24804 add_high_low_attributes (stmt, stmt_die);
24807 decls_for_scope (stmt, stmt_die);
24810 /* Generate a DIE for an inlined subprogram. */
24812 static void
24813 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24815 tree decl = block_ultimate_origin (stmt);
24817 /* Make sure any inlined functions are known to be inlineable. */
24818 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24819 || cgraph_function_possibly_inlined_p (decl));
24821 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24823 if (call_arg_locations || debug_inline_points)
24824 equate_block_to_die (stmt, subr_die);
24825 add_abstract_origin_attribute (subr_die, decl);
24826 if (TREE_ASM_WRITTEN (stmt))
24827 add_high_low_attributes (stmt, subr_die);
24828 add_call_src_coords_attributes (stmt, subr_die);
24830 /* The inliner creates an extra BLOCK for the parameter setup,
24831 we want to merge that with the actual outermost BLOCK of the
24832 inlined function to avoid duplicate locals in consumers.
24833 Do that by doing the recursion to subblocks on the single subblock
24834 of STMT. */
24835 bool unwrap_one = false;
24836 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24838 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24839 if (origin
24840 && TREE_CODE (origin) == BLOCK
24841 && BLOCK_SUPERCONTEXT (origin) == decl)
24842 unwrap_one = true;
24844 decls_for_scope (stmt, subr_die, !unwrap_one);
24845 if (unwrap_one)
24846 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24849 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24850 the comment for VLR_CONTEXT. */
24852 static void
24853 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24855 dw_die_ref decl_die;
24857 if (TREE_TYPE (decl) == error_mark_node)
24858 return;
24860 decl_die = new_die (DW_TAG_member, context_die, decl);
24861 add_name_and_src_coords_attributes (decl_die, decl);
24862 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24863 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24864 context_die);
24866 if (DECL_BIT_FIELD_TYPE (decl))
24868 add_byte_size_attribute (decl_die, decl);
24869 add_bit_size_attribute (decl_die, decl);
24870 add_bit_offset_attribute (decl_die, decl);
24873 add_alignment_attribute (decl_die, decl);
24875 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24876 add_data_member_location_attribute (decl_die, decl, ctx);
24878 if (DECL_ARTIFICIAL (decl))
24879 add_AT_flag (decl_die, DW_AT_artificial, 1);
24881 add_accessibility_attribute (decl_die, decl);
24883 /* Equate decl number to die, so that we can look up this decl later on. */
24884 equate_decl_number_to_die (decl, decl_die);
24887 /* Generate a DIE for a pointer to a member type. TYPE can be an
24888 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24889 pointer to member function. */
24891 static void
24892 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24894 if (lookup_type_die (type))
24895 return;
24897 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24898 scope_die_for (type, context_die), type);
24900 equate_type_number_to_die (type, ptr_die);
24901 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24902 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24903 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24904 context_die);
24905 add_alignment_attribute (ptr_die, type);
24907 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24908 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24910 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24911 add_AT_loc (ptr_die, DW_AT_use_location, op);
24915 static char *producer_string;
24917 /* Given a C and/or C++ language/version string return the "highest".
24918 C++ is assumed to be "higher" than C in this case. Used for merging
24919 LTO translation unit languages. */
24920 static const char *
24921 highest_c_language (const char *lang1, const char *lang2)
24923 if (strcmp ("GNU C++23", lang1) == 0 || strcmp ("GNU C++23", lang2) == 0)
24924 return "GNU C++23";
24925 if (strcmp ("GNU C++20", lang1) == 0 || strcmp ("GNU C++20", lang2) == 0)
24926 return "GNU C++20";
24927 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24928 return "GNU C++17";
24929 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24930 return "GNU C++14";
24931 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24932 return "GNU C++11";
24933 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24934 return "GNU C++98";
24936 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24937 return "GNU C2X";
24938 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24939 return "GNU C17";
24940 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24941 return "GNU C11";
24942 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24943 return "GNU C99";
24944 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24945 return "GNU C89";
24947 gcc_unreachable ();
24951 /* Generate the DIE for the compilation unit. */
24953 static dw_die_ref
24954 gen_compile_unit_die (const char *filename)
24956 dw_die_ref die;
24957 const char *language_string = lang_hooks.name;
24958 int language;
24960 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24962 if (filename)
24964 add_filename_attribute (die, filename);
24965 /* Don't add cwd for <built-in>. */
24966 if (filename[0] != '<')
24967 add_comp_dir_attribute (die);
24970 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24972 /* If our producer is LTO try to figure out a common language to use
24973 from the global list of translation units. */
24974 if (strcmp (language_string, "GNU GIMPLE") == 0)
24976 unsigned i;
24977 tree t;
24978 const char *common_lang = NULL;
24980 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24982 if (!TRANSLATION_UNIT_LANGUAGE (t))
24983 continue;
24984 if (!common_lang)
24985 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24986 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24988 else if (startswith (common_lang, "GNU C")
24989 && startswith (TRANSLATION_UNIT_LANGUAGE (t), "GNU C"))
24990 /* Mixing C and C++ is ok, use C++ in that case. */
24991 common_lang = highest_c_language (common_lang,
24992 TRANSLATION_UNIT_LANGUAGE (t));
24993 else
24995 /* Fall back to C. */
24996 common_lang = NULL;
24997 break;
25001 if (common_lang)
25002 language_string = common_lang;
25005 language = DW_LANG_C;
25006 if (startswith (language_string, "GNU C")
25007 && ISDIGIT (language_string[5]))
25009 language = DW_LANG_C89;
25010 if (dwarf_version >= 3 || !dwarf_strict)
25012 if (strcmp (language_string, "GNU C89") != 0)
25013 language = DW_LANG_C99;
25015 if (dwarf_version >= 5 /* || !dwarf_strict */)
25016 if (strcmp (language_string, "GNU C11") == 0
25017 || strcmp (language_string, "GNU C17") == 0
25018 || strcmp (language_string, "GNU C2X") == 0)
25019 language = DW_LANG_C11;
25022 else if (startswith (language_string, "GNU C++"))
25024 language = DW_LANG_C_plus_plus;
25025 if (dwarf_version >= 5 /* || !dwarf_strict */)
25027 if (strcmp (language_string, "GNU C++11") == 0)
25028 language = DW_LANG_C_plus_plus_11;
25029 else if (strcmp (language_string, "GNU C++14") == 0)
25030 language = DW_LANG_C_plus_plus_14;
25031 else if (strcmp (language_string, "GNU C++17") == 0
25032 || strcmp (language_string, "GNU C++20") == 0
25033 || strcmp (language_string, "GNU C++23") == 0)
25034 /* For now. */
25035 language = DW_LANG_C_plus_plus_14;
25038 else if (strcmp (language_string, "GNU F77") == 0)
25039 language = DW_LANG_Fortran77;
25040 else if (dwarf_version >= 3 || !dwarf_strict)
25042 if (strcmp (language_string, "GNU Ada") == 0)
25043 language = DW_LANG_Ada95;
25044 else if (startswith (language_string, "GNU Fortran"))
25046 language = DW_LANG_Fortran95;
25047 if (dwarf_version >= 5 /* || !dwarf_strict */)
25049 if (strcmp (language_string, "GNU Fortran2003") == 0)
25050 language = DW_LANG_Fortran03;
25051 else if (strcmp (language_string, "GNU Fortran2008") == 0)
25052 language = DW_LANG_Fortran08;
25055 else if (strcmp (language_string, "GNU Objective-C") == 0)
25056 language = DW_LANG_ObjC;
25057 else if (strcmp (language_string, "GNU Objective-C++") == 0)
25058 language = DW_LANG_ObjC_plus_plus;
25059 else if (strcmp (language_string, "GNU D") == 0)
25060 language = DW_LANG_D;
25061 else if (dwarf_version >= 5 || !dwarf_strict)
25063 if (strcmp (language_string, "GNU Go") == 0)
25064 language = DW_LANG_Go;
25067 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
25068 else if (startswith (language_string, "GNU Fortran"))
25069 language = DW_LANG_Fortran90;
25070 /* Likewise for Ada. */
25071 else if (strcmp (language_string, "GNU Ada") == 0)
25072 language = DW_LANG_Ada83;
25074 add_AT_unsigned (die, DW_AT_language, language);
25076 switch (language)
25078 case DW_LANG_Fortran77:
25079 case DW_LANG_Fortran90:
25080 case DW_LANG_Fortran95:
25081 case DW_LANG_Fortran03:
25082 case DW_LANG_Fortran08:
25083 /* Fortran has case insensitive identifiers and the front-end
25084 lowercases everything. */
25085 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
25086 break;
25087 default:
25088 /* The default DW_ID_case_sensitive doesn't need to be specified. */
25089 break;
25091 return die;
25094 /* Generate the DIE for a base class. */
25096 static void
25097 gen_inheritance_die (tree binfo, tree access, tree type,
25098 dw_die_ref context_die)
25100 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
25101 struct vlr_context ctx = { type, NULL };
25103 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
25104 context_die);
25105 add_data_member_location_attribute (die, binfo, &ctx);
25107 if (BINFO_VIRTUAL_P (binfo))
25108 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
25110 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
25111 children, otherwise the default is DW_ACCESS_public. In DWARF2
25112 the default has always been DW_ACCESS_private. */
25113 if (access == access_public_node)
25115 if (dwarf_version == 2
25116 || context_die->die_tag == DW_TAG_class_type)
25117 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
25119 else if (access == access_protected_node)
25120 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
25121 else if (dwarf_version > 2
25122 && context_die->die_tag != DW_TAG_class_type)
25123 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
25126 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
25127 structure. */
25129 static bool
25130 is_variant_part (tree decl)
25132 return (TREE_CODE (decl) == FIELD_DECL
25133 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
25136 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
25137 return the FIELD_DECL. Return NULL_TREE otherwise. */
25139 static tree
25140 analyze_discr_in_predicate (tree operand, tree struct_type)
25142 while (CONVERT_EXPR_P (operand))
25143 operand = TREE_OPERAND (operand, 0);
25145 /* Match field access to members of struct_type only. */
25146 if (TREE_CODE (operand) == COMPONENT_REF
25147 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
25148 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
25149 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
25150 return TREE_OPERAND (operand, 1);
25151 else
25152 return NULL_TREE;
25155 /* Check that SRC is a constant integer that can be represented as a native
25156 integer constant (either signed or unsigned). If so, store it into DEST and
25157 return true. Return false otherwise. */
25159 static bool
25160 get_discr_value (tree src, dw_discr_value *dest)
25162 tree discr_type = TREE_TYPE (src);
25164 if (lang_hooks.types.get_debug_type)
25166 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
25167 if (debug_type != NULL)
25168 discr_type = debug_type;
25171 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
25172 return false;
25174 /* Signedness can vary between the original type and the debug type. This
25175 can happen for character types in Ada for instance: the character type
25176 used for code generation can be signed, to be compatible with the C one,
25177 but from a debugger point of view, it must be unsigned. */
25178 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
25179 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
25181 if (is_orig_unsigned != is_debug_unsigned)
25182 src = fold_convert (discr_type, src);
25184 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
25185 return false;
25187 dest->pos = is_debug_unsigned;
25188 if (is_debug_unsigned)
25189 dest->v.uval = tree_to_uhwi (src);
25190 else
25191 dest->v.sval = tree_to_shwi (src);
25193 return true;
25196 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
25197 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
25198 store NULL_TREE in DISCR_DECL. Otherwise:
25200 - store the discriminant field in STRUCT_TYPE that controls the variant
25201 part to *DISCR_DECL
25203 - put in *DISCR_LISTS_P an array where for each variant, the item
25204 represents the corresponding matching list of discriminant values.
25206 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
25207 the above array.
25209 Note that when the array is allocated (i.e. when the analysis is
25210 successful), it is up to the caller to free the array. */
25212 static void
25213 analyze_variants_discr (tree variant_part_decl,
25214 tree struct_type,
25215 tree *discr_decl,
25216 dw_discr_list_ref **discr_lists_p,
25217 unsigned *discr_lists_length)
25219 tree variant_part_type = TREE_TYPE (variant_part_decl);
25220 tree variant;
25221 dw_discr_list_ref *discr_lists;
25222 unsigned i;
25224 /* Compute how many variants there are in this variant part. */
25225 *discr_lists_length = 0;
25226 for (variant = TYPE_FIELDS (variant_part_type);
25227 variant != NULL_TREE;
25228 variant = DECL_CHAIN (variant))
25229 ++*discr_lists_length;
25231 *discr_decl = NULL_TREE;
25232 *discr_lists_p
25233 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
25234 sizeof (**discr_lists_p));
25235 discr_lists = *discr_lists_p;
25237 /* And then analyze all variants to extract discriminant information for all
25238 of them. This analysis is conservative: as soon as we detect something we
25239 do not support, abort everything and pretend we found nothing. */
25240 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
25241 variant != NULL_TREE;
25242 variant = DECL_CHAIN (variant), ++i)
25244 tree match_expr = DECL_QUALIFIER (variant);
25246 /* Now, try to analyze the predicate and deduce a discriminant for
25247 it. */
25248 if (match_expr == boolean_true_node)
25249 /* Typically happens for the default variant: it matches all cases that
25250 previous variants rejected. Don't output any matching value for
25251 this one. */
25252 continue;
25254 /* The following loop tries to iterate over each discriminant
25255 possibility: single values or ranges. */
25256 while (match_expr != NULL_TREE)
25258 tree next_round_match_expr;
25259 tree candidate_discr = NULL_TREE;
25260 dw_discr_list_ref new_node = NULL;
25262 /* Possibilities are matched one after the other by nested
25263 TRUTH_ORIF_EXPR expressions. Process the current possibility and
25264 continue with the rest at next iteration. */
25265 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
25267 next_round_match_expr = TREE_OPERAND (match_expr, 0);
25268 match_expr = TREE_OPERAND (match_expr, 1);
25270 else
25271 next_round_match_expr = NULL_TREE;
25273 if (match_expr == boolean_false_node)
25274 /* This sub-expression matches nothing: just wait for the next
25275 one. */
25278 else if (TREE_CODE (match_expr) == EQ_EXPR)
25280 /* We are matching: <discr_field> == <integer_cst>
25281 This sub-expression matches a single value. */
25282 tree integer_cst = TREE_OPERAND (match_expr, 1);
25284 candidate_discr
25285 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
25286 struct_type);
25288 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25289 if (!get_discr_value (integer_cst,
25290 &new_node->dw_discr_lower_bound))
25291 goto abort;
25292 new_node->dw_discr_range = false;
25295 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
25297 /* We are matching:
25298 <discr_field> > <integer_cst>
25299 && <discr_field> < <integer_cst>.
25300 This sub-expression matches the range of values between the
25301 two matched integer constants. Note that comparisons can be
25302 inclusive or exclusive. */
25303 tree candidate_discr_1, candidate_discr_2;
25304 tree lower_cst, upper_cst;
25305 bool lower_cst_included, upper_cst_included;
25306 tree lower_op = TREE_OPERAND (match_expr, 0);
25307 tree upper_op = TREE_OPERAND (match_expr, 1);
25309 /* When the comparison is exclusive, the integer constant is not
25310 the discriminant range bound we are looking for: we will have
25311 to increment or decrement it. */
25312 if (TREE_CODE (lower_op) == GE_EXPR)
25313 lower_cst_included = true;
25314 else if (TREE_CODE (lower_op) == GT_EXPR)
25315 lower_cst_included = false;
25316 else
25317 goto abort;
25319 if (TREE_CODE (upper_op) == LE_EXPR)
25320 upper_cst_included = true;
25321 else if (TREE_CODE (upper_op) == LT_EXPR)
25322 upper_cst_included = false;
25323 else
25324 goto abort;
25326 /* Extract the discriminant from the first operand and check it
25327 is consistant with the same analysis in the second
25328 operand. */
25329 candidate_discr_1
25330 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
25331 struct_type);
25332 candidate_discr_2
25333 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
25334 struct_type);
25335 if (candidate_discr_1 == candidate_discr_2)
25336 candidate_discr = candidate_discr_1;
25337 else
25338 goto abort;
25340 /* Extract bounds from both. */
25341 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25342 lower_cst = TREE_OPERAND (lower_op, 1);
25343 upper_cst = TREE_OPERAND (upper_op, 1);
25345 if (!lower_cst_included)
25346 lower_cst
25347 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
25348 build_int_cst (TREE_TYPE (lower_cst), 1));
25349 if (!upper_cst_included)
25350 upper_cst
25351 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
25352 build_int_cst (TREE_TYPE (upper_cst), 1));
25354 if (!get_discr_value (lower_cst,
25355 &new_node->dw_discr_lower_bound)
25356 || !get_discr_value (upper_cst,
25357 &new_node->dw_discr_upper_bound))
25358 goto abort;
25360 new_node->dw_discr_range = true;
25363 else if ((candidate_discr
25364 = analyze_discr_in_predicate (match_expr, struct_type))
25365 && (TREE_TYPE (candidate_discr) == boolean_type_node
25366 || TREE_TYPE (TREE_TYPE (candidate_discr))
25367 == boolean_type_node))
25369 /* We are matching: <discr_field> for a boolean discriminant.
25370 This sub-expression matches boolean_true_node. */
25371 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
25372 if (!get_discr_value (boolean_true_node,
25373 &new_node->dw_discr_lower_bound))
25374 goto abort;
25375 new_node->dw_discr_range = false;
25378 else
25379 /* Unsupported sub-expression: we cannot determine the set of
25380 matching discriminant values. Abort everything. */
25381 goto abort;
25383 /* If the discriminant info is not consistant with what we saw so
25384 far, consider the analysis failed and abort everything. */
25385 if (candidate_discr == NULL_TREE
25386 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
25387 goto abort;
25388 else
25389 *discr_decl = candidate_discr;
25391 if (new_node != NULL)
25393 new_node->dw_discr_next = discr_lists[i];
25394 discr_lists[i] = new_node;
25396 match_expr = next_round_match_expr;
25400 /* If we reach this point, we could match everything we were interested
25401 in. */
25402 return;
25404 abort:
25405 /* Clean all data structure and return no result. */
25406 free (*discr_lists_p);
25407 *discr_lists_p = NULL;
25408 *discr_decl = NULL_TREE;
25411 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
25412 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
25413 under CONTEXT_DIE.
25415 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
25416 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
25417 this type, which are record types, represent the available variants and each
25418 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
25419 values are inferred from these attributes.
25421 In trees, the offsets for the fields inside these sub-records are relative
25422 to the variant part itself, whereas the corresponding DIEs should have
25423 offset attributes that are relative to the embedding record base address.
25424 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
25425 must be an expression that computes the offset of the variant part to
25426 describe in DWARF. */
25428 static void
25429 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
25430 dw_die_ref context_die)
25432 const tree variant_part_type = TREE_TYPE (variant_part_decl);
25433 tree variant_part_offset = vlr_ctx->variant_part_offset;
25435 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
25436 NULL_TREE if there is no such field. */
25437 tree discr_decl = NULL_TREE;
25438 dw_discr_list_ref *discr_lists;
25439 unsigned discr_lists_length = 0;
25440 unsigned i;
25442 dw_die_ref dwarf_proc_die = NULL;
25443 dw_die_ref variant_part_die
25444 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
25446 equate_decl_number_to_die (variant_part_decl, variant_part_die);
25448 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
25449 &discr_decl, &discr_lists, &discr_lists_length);
25451 if (discr_decl != NULL_TREE)
25453 dw_die_ref discr_die = lookup_decl_die (discr_decl);
25455 if (discr_die)
25456 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
25457 else
25458 /* We have no DIE for the discriminant, so just discard all
25459 discrimimant information in the output. */
25460 discr_decl = NULL_TREE;
25463 /* If the offset for this variant part is more complex than a constant,
25464 create a DWARF procedure for it so that we will not have to generate
25465 DWARF expressions for it for each member. */
25466 if (TREE_CODE (variant_part_offset) != INTEGER_CST
25467 && (dwarf_version >= 3 || !dwarf_strict))
25469 struct loc_descr_context ctx = {
25470 vlr_ctx->struct_type, /* context_type */
25471 NULL_TREE, /* base_decl */
25472 NULL, /* dpi */
25473 false, /* placeholder_arg */
25474 false, /* placeholder_seen */
25475 false /* strict_signedness */
25477 const tree dwarf_proc_fndecl
25478 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
25479 build_function_type (TREE_TYPE (variant_part_offset),
25480 NULL_TREE));
25481 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
25482 const dw_loc_descr_ref dwarf_proc_body
25483 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
25485 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
25486 dwarf_proc_fndecl, context_die);
25487 if (dwarf_proc_die != NULL)
25488 variant_part_offset = dwarf_proc_call;
25491 /* Output DIEs for all variants. */
25492 i = 0;
25493 for (tree variant = TYPE_FIELDS (variant_part_type);
25494 variant != NULL_TREE;
25495 variant = DECL_CHAIN (variant), ++i)
25497 tree variant_type = TREE_TYPE (variant);
25498 dw_die_ref variant_die;
25500 /* All variants (i.e. members of a variant part) are supposed to be
25501 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
25502 under these records. */
25503 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
25505 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
25506 equate_decl_number_to_die (variant, variant_die);
25508 /* Output discriminant values this variant matches, if any. */
25509 if (discr_decl == NULL || discr_lists[i] == NULL)
25510 /* In the case we have discriminant information at all, this is
25511 probably the default variant: as the standard says, don't
25512 output any discriminant value/list attribute. */
25514 else if (discr_lists[i]->dw_discr_next == NULL
25515 && !discr_lists[i]->dw_discr_range)
25516 /* If there is only one accepted value, don't bother outputting a
25517 list. */
25518 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
25519 else
25520 add_discr_list (variant_die, discr_lists[i]);
25522 for (tree member = TYPE_FIELDS (variant_type);
25523 member != NULL_TREE;
25524 member = DECL_CHAIN (member))
25526 struct vlr_context vlr_sub_ctx = {
25527 vlr_ctx->struct_type, /* struct_type */
25528 NULL /* variant_part_offset */
25530 if (is_variant_part (member))
25532 /* All offsets for fields inside variant parts are relative to
25533 the top-level embedding RECORD_TYPE's base address. On the
25534 other hand, offsets in GCC's types are relative to the
25535 nested-most variant part. So we have to sum offsets each time
25536 we recurse. */
25538 vlr_sub_ctx.variant_part_offset
25539 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
25540 variant_part_offset, byte_position (member));
25541 gen_variant_part (member, &vlr_sub_ctx, variant_die);
25543 else
25545 vlr_sub_ctx.variant_part_offset = variant_part_offset;
25546 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
25551 free (discr_lists);
25554 /* Generate a DIE for a class member. */
25556 static void
25557 gen_member_die (tree type, dw_die_ref context_die)
25559 tree member;
25560 tree binfo = TYPE_BINFO (type);
25562 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
25564 /* If this is not an incomplete type, output descriptions of each of its
25565 members. Note that as we output the DIEs necessary to represent the
25566 members of this record or union type, we will also be trying to output
25567 DIEs to represent the *types* of those members. However the `type'
25568 function (above) will specifically avoid generating type DIEs for member
25569 types *within* the list of member DIEs for this (containing) type except
25570 for those types (of members) which are explicitly marked as also being
25571 members of this (containing) type themselves. The g++ front- end can
25572 force any given type to be treated as a member of some other (containing)
25573 type by setting the TYPE_CONTEXT of the given (member) type to point to
25574 the TREE node representing the appropriate (containing) type. */
25576 /* First output info about the base classes. */
25577 if (binfo && early_dwarf)
25579 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
25580 int i;
25581 tree base;
25583 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
25584 gen_inheritance_die (base,
25585 (accesses ? (*accesses)[i] : access_public_node),
25586 type,
25587 context_die);
25590 /* Now output info about the members. */
25591 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
25593 /* Ignore clones. */
25594 if (DECL_ABSTRACT_ORIGIN (member))
25595 continue;
25597 struct vlr_context vlr_ctx = { type, NULL_TREE };
25598 bool static_inline_p
25599 = (VAR_P (member)
25600 && TREE_STATIC (member)
25601 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
25602 != -1));
25604 /* If we thought we were generating minimal debug info for TYPE
25605 and then changed our minds, some of the member declarations
25606 may have already been defined. Don't define them again, but
25607 do put them in the right order. */
25609 if (dw_die_ref child = lookup_decl_die (member))
25611 /* Handle inline static data members, which only have in-class
25612 declarations. */
25613 bool splice = true;
25615 dw_die_ref ref = NULL;
25616 if (child->die_tag == DW_TAG_variable
25617 && child->die_parent == comp_unit_die ())
25619 ref = get_AT_ref (child, DW_AT_specification);
25621 /* For C++17 inline static data members followed by redundant
25622 out of class redeclaration, we might get here with
25623 child being the DIE created for the out of class
25624 redeclaration and with its DW_AT_specification being
25625 the DIE created for in-class definition. We want to
25626 reparent the latter, and don't want to create another
25627 DIE with DW_AT_specification in that case, because
25628 we already have one. */
25629 if (ref
25630 && static_inline_p
25631 && ref->die_tag == DW_TAG_variable
25632 && ref->die_parent == comp_unit_die ()
25633 && get_AT (ref, DW_AT_specification) == NULL)
25635 child = ref;
25636 ref = NULL;
25637 static_inline_p = false;
25640 if (!ref)
25642 reparent_child (child, context_die);
25643 if (dwarf_version < 5)
25644 child->die_tag = DW_TAG_member;
25645 splice = false;
25648 else if (child->die_tag == DW_TAG_enumerator)
25649 /* Enumerators remain under their enumeration even if
25650 their names are introduced in the enclosing scope. */
25651 splice = false;
25653 if (splice)
25654 splice_child_die (context_die, child);
25657 /* Do not generate DWARF for variant parts if we are generating the
25658 corresponding GNAT encodings: DIEs generated for the two schemes
25659 would conflict in our mappings. */
25660 else if (is_variant_part (member)
25661 && gnat_encodings != DWARF_GNAT_ENCODINGS_ALL)
25663 vlr_ctx.variant_part_offset = byte_position (member);
25664 gen_variant_part (member, &vlr_ctx, context_die);
25666 else
25668 vlr_ctx.variant_part_offset = NULL_TREE;
25669 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25672 /* For C++ inline static data members emit immediately a DW_TAG_variable
25673 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25674 DW_AT_specification. */
25675 if (static_inline_p)
25677 int old_extern = DECL_EXTERNAL (member);
25678 DECL_EXTERNAL (member) = 0;
25679 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25680 DECL_EXTERNAL (member) = old_extern;
25685 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25686 is set, we pretend that the type was never defined, so we only get the
25687 member DIEs needed by later specification DIEs. */
25689 static void
25690 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25691 enum debug_info_usage usage)
25693 if (TREE_ASM_WRITTEN (type))
25695 /* Fill in the bound of variable-length fields in late dwarf if
25696 still incomplete. */
25697 if (!early_dwarf && variably_modified_type_p (type, NULL))
25698 for (tree member = TYPE_FIELDS (type);
25699 member;
25700 member = DECL_CHAIN (member))
25701 fill_variable_array_bounds (TREE_TYPE (member));
25702 return;
25705 dw_die_ref type_die = lookup_type_die (type);
25706 dw_die_ref scope_die = 0;
25707 int nested = 0;
25708 int complete = (TYPE_SIZE (type)
25709 && (! TYPE_STUB_DECL (type)
25710 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25711 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25712 complete = complete && should_emit_struct_debug (type, usage);
25714 if (type_die && ! complete)
25715 return;
25717 if (TYPE_CONTEXT (type) != NULL_TREE
25718 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25719 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25720 nested = 1;
25722 scope_die = scope_die_for (type, context_die);
25724 /* Generate child dies for template parameters. */
25725 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25726 schedule_generic_params_dies_gen (type);
25728 if (! type_die || (nested && is_cu_die (scope_die)))
25729 /* First occurrence of type or toplevel definition of nested class. */
25731 dw_die_ref old_die = type_die;
25733 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25734 ? record_type_tag (type) : DW_TAG_union_type,
25735 scope_die, type);
25736 equate_type_number_to_die (type, type_die);
25737 if (old_die)
25738 add_AT_specification (type_die, old_die);
25739 else
25740 add_name_attribute (type_die, type_tag (type));
25742 else
25743 remove_AT (type_die, DW_AT_declaration);
25745 /* If this type has been completed, then give it a byte_size attribute and
25746 then give a list of members. */
25747 if (complete && !ns_decl)
25749 /* Prevent infinite recursion in cases where the type of some member of
25750 this type is expressed in terms of this type itself. */
25751 TREE_ASM_WRITTEN (type) = 1;
25752 add_byte_size_attribute (type_die, type);
25753 add_alignment_attribute (type_die, type);
25754 if (TYPE_STUB_DECL (type) != NULL_TREE)
25756 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25757 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25760 /* If the first reference to this type was as the return type of an
25761 inline function, then it may not have a parent. Fix this now. */
25762 if (type_die->die_parent == NULL)
25763 add_child_die (scope_die, type_die);
25765 gen_member_die (type, type_die);
25767 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25768 if (TYPE_ARTIFICIAL (type))
25769 add_AT_flag (type_die, DW_AT_artificial, 1);
25771 /* GNU extension: Record what type our vtable lives in. */
25772 if (TYPE_VFIELD (type))
25774 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25776 gen_type_die (vtype, context_die);
25777 add_AT_die_ref (type_die, DW_AT_containing_type,
25778 lookup_type_die (vtype));
25781 else
25783 add_AT_flag (type_die, DW_AT_declaration, 1);
25785 /* We don't need to do this for function-local types. */
25786 if (TYPE_STUB_DECL (type)
25787 && ! decl_function_context (TYPE_STUB_DECL (type)))
25788 vec_safe_push (incomplete_types, type);
25791 if (get_AT (type_die, DW_AT_name))
25792 add_pubtype (type, type_die);
25795 /* Generate a DIE for a subroutine _type_. */
25797 static void
25798 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25800 tree return_type = TREE_TYPE (type);
25801 dw_die_ref subr_die
25802 = new_die (DW_TAG_subroutine_type,
25803 scope_die_for (type, context_die), type);
25805 equate_type_number_to_die (type, subr_die);
25806 add_prototyped_attribute (subr_die, type);
25807 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25808 context_die);
25809 add_alignment_attribute (subr_die, type);
25810 gen_formal_types_die (type, subr_die);
25812 if (get_AT (subr_die, DW_AT_name))
25813 add_pubtype (type, subr_die);
25814 if ((dwarf_version >= 5 || !dwarf_strict)
25815 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25816 add_AT_flag (subr_die, DW_AT_reference, 1);
25817 if ((dwarf_version >= 5 || !dwarf_strict)
25818 && lang_hooks.types.type_dwarf_attribute (type,
25819 DW_AT_rvalue_reference) != -1)
25820 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25823 /* Generate a DIE for a type definition. */
25825 static void
25826 gen_typedef_die (tree decl, dw_die_ref context_die)
25828 dw_die_ref type_die;
25829 tree type;
25831 if (TREE_ASM_WRITTEN (decl))
25833 if (DECL_ORIGINAL_TYPE (decl))
25834 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25835 return;
25838 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25839 checks in process_scope_var and modified_type_die), this should be called
25840 only for original types. */
25841 gcc_assert (decl_ultimate_origin (decl) == NULL
25842 || decl_ultimate_origin (decl) == decl);
25844 TREE_ASM_WRITTEN (decl) = 1;
25845 type_die = new_die (DW_TAG_typedef, context_die, decl);
25847 add_name_and_src_coords_attributes (type_die, decl);
25848 if (DECL_ORIGINAL_TYPE (decl))
25850 type = DECL_ORIGINAL_TYPE (decl);
25851 if (type == error_mark_node)
25852 return;
25854 gcc_assert (type != TREE_TYPE (decl));
25855 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25857 else
25859 type = TREE_TYPE (decl);
25860 if (type == error_mark_node)
25861 return;
25863 if (is_naming_typedef_decl (TYPE_NAME (type)))
25865 /* Here, we are in the case of decl being a typedef naming
25866 an anonymous type, e.g:
25867 typedef struct {...} foo;
25868 In that case TREE_TYPE (decl) is not a typedef variant
25869 type and TYPE_NAME of the anonymous type is set to the
25870 TYPE_DECL of the typedef. This construct is emitted by
25871 the C++ FE.
25873 TYPE is the anonymous struct named by the typedef
25874 DECL. As we need the DW_AT_type attribute of the
25875 DW_TAG_typedef to point to the DIE of TYPE, let's
25876 generate that DIE right away. add_type_attribute
25877 called below will then pick (via lookup_type_die) that
25878 anonymous struct DIE. */
25879 if (!TREE_ASM_WRITTEN (type))
25880 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25882 /* This is a GNU Extension. We are adding a
25883 DW_AT_linkage_name attribute to the DIE of the
25884 anonymous struct TYPE. The value of that attribute
25885 is the name of the typedef decl naming the anonymous
25886 struct. This greatly eases the work of consumers of
25887 this debug info. */
25888 add_linkage_name_raw (lookup_type_die (type), decl);
25892 add_type_attribute (type_die, type, decl_quals (decl), false,
25893 context_die);
25895 if (is_naming_typedef_decl (decl))
25896 /* We want that all subsequent calls to lookup_type_die with
25897 TYPE in argument yield the DW_TAG_typedef we have just
25898 created. */
25899 equate_type_number_to_die (type, type_die);
25901 add_alignment_attribute (type_die, TREE_TYPE (decl));
25903 add_accessibility_attribute (type_die, decl);
25905 if (DECL_ABSTRACT_P (decl))
25906 equate_decl_number_to_die (decl, type_die);
25908 if (get_AT (type_die, DW_AT_name))
25909 add_pubtype (decl, type_die);
25912 /* Generate a DIE for a struct, class, enum or union type. */
25914 static void
25915 gen_tagged_type_die (tree type,
25916 dw_die_ref context_die,
25917 enum debug_info_usage usage)
25919 if (type == NULL_TREE
25920 || !is_tagged_type (type))
25921 return;
25923 if (TREE_ASM_WRITTEN (type))
25925 /* If this is a nested type whose containing class hasn't been written
25926 out yet, writing it out will cover this one, too. This does not apply
25927 to instantiations of member class templates; they need to be added to
25928 the containing class as they are generated. FIXME: This hurts the
25929 idea of combining type decls from multiple TUs, since we can't predict
25930 what set of template instantiations we'll get. */
25931 else if (TYPE_CONTEXT (type)
25932 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25933 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25935 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25937 if (TREE_ASM_WRITTEN (type))
25938 return;
25940 /* If that failed, attach ourselves to the stub. */
25941 context_die = lookup_type_die (TYPE_CONTEXT (type));
25943 else if (TYPE_CONTEXT (type) != NULL_TREE
25944 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25946 /* If this type is local to a function that hasn't been written
25947 out yet, use a NULL context for now; it will be fixed up in
25948 decls_for_scope. */
25949 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25950 /* A declaration DIE doesn't count; nested types need to go in the
25951 specification. */
25952 if (context_die && is_declaration_die (context_die))
25953 context_die = NULL;
25955 else
25956 context_die = declare_in_namespace (type, context_die);
25958 if (TREE_CODE (type) == ENUMERAL_TYPE)
25960 /* This might have been written out by the call to
25961 declare_in_namespace. */
25962 if (!TREE_ASM_WRITTEN (type))
25963 gen_enumeration_type_die (type, context_die);
25965 else
25966 gen_struct_or_union_type_die (type, context_die, usage);
25968 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25969 it up if it is ever completed. gen_*_type_die will set it for us
25970 when appropriate. */
25973 /* Generate a type description DIE. */
25975 static void
25976 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25977 enum debug_info_usage usage)
25979 struct array_descr_info info;
25981 if (type == NULL_TREE || type == error_mark_node)
25982 return;
25984 if (flag_checking && type)
25985 verify_type (type);
25987 if (TYPE_NAME (type) != NULL_TREE
25988 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25989 && is_redundant_typedef (TYPE_NAME (type))
25990 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25991 /* The DECL of this type is a typedef we don't want to emit debug
25992 info for but we want debug info for its underlying typedef.
25993 This can happen for e.g, the injected-class-name of a C++
25994 type. */
25995 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25997 /* If TYPE is a typedef type variant, let's generate debug info
25998 for the parent typedef which TYPE is a type of. */
25999 if (typedef_variant_p (type))
26001 if (TREE_ASM_WRITTEN (type))
26002 return;
26004 tree name = TYPE_NAME (type);
26005 tree origin = decl_ultimate_origin (name);
26006 if (origin != NULL && origin != name)
26008 gen_decl_die (origin, NULL, NULL, context_die);
26009 return;
26012 /* Prevent broken recursion; we can't hand off to the same type. */
26013 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
26015 /* Give typedefs the right scope. */
26016 context_die = scope_die_for (type, context_die);
26018 TREE_ASM_WRITTEN (type) = 1;
26020 gen_decl_die (name, NULL, NULL, context_die);
26021 return;
26024 /* If type is an anonymous tagged type named by a typedef, let's
26025 generate debug info for the typedef. */
26026 if (is_naming_typedef_decl (TYPE_NAME (type)))
26028 /* Give typedefs the right scope. */
26029 context_die = scope_die_for (type, context_die);
26031 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
26032 return;
26035 if (lang_hooks.types.get_debug_type)
26037 tree debug_type = lang_hooks.types.get_debug_type (type);
26039 if (debug_type != NULL_TREE && debug_type != type)
26041 gen_type_die_with_usage (debug_type, context_die, usage);
26042 return;
26046 /* We are going to output a DIE to represent the unqualified version
26047 of this type (i.e. without any const or volatile qualifiers) so
26048 get the main variant (i.e. the unqualified version) of this type
26049 now. (Vectors and arrays are special because the debugging info is in the
26050 cloned type itself. Similarly function/method types can contain extra
26051 ref-qualification). */
26052 if (TREE_CODE (type) == FUNCTION_TYPE
26053 || TREE_CODE (type) == METHOD_TYPE)
26055 /* For function/method types, can't use type_main_variant here,
26056 because that can have different ref-qualifiers for C++,
26057 but try to canonicalize. */
26058 tree main = TYPE_MAIN_VARIANT (type);
26059 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
26060 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
26061 && check_base_type (t, main)
26062 && check_lang_type (t, type))
26064 type = t;
26065 break;
26068 else if (TREE_CODE (type) != VECTOR_TYPE
26069 && TREE_CODE (type) != ARRAY_TYPE)
26070 type = type_main_variant (type);
26072 /* If this is an array type with hidden descriptor, handle it first. */
26073 if (!TREE_ASM_WRITTEN (type)
26074 && lang_hooks.types.get_array_descr_info)
26076 memset (&info, 0, sizeof (info));
26077 if (lang_hooks.types.get_array_descr_info (type, &info))
26079 /* Fortran sometimes emits array types with no dimension. */
26080 gcc_assert (info.ndimensions >= 0
26081 && (info.ndimensions
26082 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
26083 gen_descr_array_type_die (type, &info, context_die);
26084 TREE_ASM_WRITTEN (type) = 1;
26085 return;
26089 if (TREE_ASM_WRITTEN (type))
26091 /* Variable-length types may be incomplete even if
26092 TREE_ASM_WRITTEN. For such types, fall through to
26093 gen_array_type_die() and possibly fill in
26094 DW_AT_{upper,lower}_bound attributes. */
26095 if ((TREE_CODE (type) != ARRAY_TYPE
26096 && TREE_CODE (type) != RECORD_TYPE
26097 && TREE_CODE (type) != UNION_TYPE
26098 && TREE_CODE (type) != QUAL_UNION_TYPE)
26099 || !variably_modified_type_p (type, NULL))
26100 return;
26103 switch (TREE_CODE (type))
26105 case ERROR_MARK:
26106 break;
26108 case POINTER_TYPE:
26109 case REFERENCE_TYPE:
26110 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
26111 ensures that the gen_type_die recursion will terminate even if the
26112 type is recursive. Recursive types are possible in Ada. */
26113 /* ??? We could perhaps do this for all types before the switch
26114 statement. */
26115 TREE_ASM_WRITTEN (type) = 1;
26117 /* For these types, all that is required is that we output a DIE (or a
26118 set of DIEs) to represent the "basis" type. */
26119 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26120 DINFO_USAGE_IND_USE);
26121 break;
26123 case OFFSET_TYPE:
26124 /* This code is used for C++ pointer-to-data-member types.
26125 Output a description of the relevant class type. */
26126 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
26127 DINFO_USAGE_IND_USE);
26129 /* Output a description of the type of the object pointed to. */
26130 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26131 DINFO_USAGE_IND_USE);
26133 /* Now output a DIE to represent this pointer-to-data-member type
26134 itself. */
26135 gen_ptr_to_mbr_type_die (type, context_die);
26136 break;
26138 case FUNCTION_TYPE:
26139 /* Force out return type (in case it wasn't forced out already). */
26140 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26141 DINFO_USAGE_DIR_USE);
26142 gen_subroutine_type_die (type, context_die);
26143 break;
26145 case METHOD_TYPE:
26146 /* Force out return type (in case it wasn't forced out already). */
26147 gen_type_die_with_usage (TREE_TYPE (type), context_die,
26148 DINFO_USAGE_DIR_USE);
26149 gen_subroutine_type_die (type, context_die);
26150 break;
26152 case ARRAY_TYPE:
26153 case VECTOR_TYPE:
26154 gen_array_type_die (type, context_die);
26155 break;
26157 case ENUMERAL_TYPE:
26158 case RECORD_TYPE:
26159 case UNION_TYPE:
26160 case QUAL_UNION_TYPE:
26161 gen_tagged_type_die (type, context_die, usage);
26162 return;
26164 case VOID_TYPE:
26165 case OPAQUE_TYPE:
26166 case INTEGER_TYPE:
26167 case REAL_TYPE:
26168 case FIXED_POINT_TYPE:
26169 case COMPLEX_TYPE:
26170 case BOOLEAN_TYPE:
26171 /* No DIEs needed for fundamental types. */
26172 break;
26174 case NULLPTR_TYPE:
26175 case LANG_TYPE:
26176 /* Just use DW_TAG_unspecified_type. */
26178 dw_die_ref type_die = lookup_type_die (type);
26179 if (type_die == NULL)
26181 tree name = TYPE_IDENTIFIER (type);
26182 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
26183 type);
26184 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
26185 equate_type_number_to_die (type, type_die);
26188 break;
26190 default:
26191 if (is_cxx_auto (type))
26193 tree name = TYPE_IDENTIFIER (type);
26194 dw_die_ref *die = (name == get_identifier ("auto")
26195 ? &auto_die : &decltype_auto_die);
26196 if (!*die)
26198 *die = new_die (DW_TAG_unspecified_type,
26199 comp_unit_die (), NULL_TREE);
26200 add_name_attribute (*die, IDENTIFIER_POINTER (name));
26202 equate_type_number_to_die (type, *die);
26203 break;
26205 gcc_unreachable ();
26208 TREE_ASM_WRITTEN (type) = 1;
26211 static void
26212 gen_type_die (tree type, dw_die_ref context_die)
26214 if (type != error_mark_node)
26216 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
26217 if (flag_checking)
26219 dw_die_ref die = lookup_type_die (type);
26220 if (die)
26221 check_die (die);
26226 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
26227 things which are local to the given block. */
26229 static void
26230 gen_block_die (tree stmt, dw_die_ref context_die)
26232 int must_output_die = 0;
26233 bool inlined_func;
26235 /* Ignore blocks that are NULL. */
26236 if (stmt == NULL_TREE)
26237 return;
26239 inlined_func = inlined_function_outer_scope_p (stmt);
26241 /* If the block is one fragment of a non-contiguous block, do not
26242 process the variables, since they will have been done by the
26243 origin block. Do process subblocks. */
26244 if (BLOCK_FRAGMENT_ORIGIN (stmt))
26246 tree sub;
26248 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
26249 gen_block_die (sub, context_die);
26251 return;
26254 /* Determine if we need to output any Dwarf DIEs at all to represent this
26255 block. */
26256 if (inlined_func)
26257 /* The outer scopes for inlinings *must* always be represented. We
26258 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
26259 must_output_die = 1;
26260 else if (lookup_block_die (stmt))
26261 /* If we already have a DIE then it was filled early. Meanwhile
26262 we might have pruned all BLOCK_VARS as optimized out but we
26263 still want to generate high/low PC attributes so output it. */
26264 must_output_die = 1;
26265 else if (TREE_USED (stmt)
26266 || TREE_ASM_WRITTEN (stmt))
26268 /* Determine if this block directly contains any "significant"
26269 local declarations which we will need to output DIEs for. */
26270 if (debug_info_level > DINFO_LEVEL_TERSE)
26272 /* We are not in terse mode so any local declaration that
26273 is not ignored for debug purposes counts as being a
26274 "significant" one. */
26275 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
26276 must_output_die = 1;
26277 else
26278 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
26279 if (!DECL_IGNORED_P (var))
26281 must_output_die = 1;
26282 break;
26285 else if (!dwarf2out_ignore_block (stmt))
26286 must_output_die = 1;
26289 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
26290 DIE for any block which contains no significant local declarations at
26291 all. Rather, in such cases we just call `decls_for_scope' so that any
26292 needed Dwarf info for any sub-blocks will get properly generated. Note
26293 that in terse mode, our definition of what constitutes a "significant"
26294 local declaration gets restricted to include only inlined function
26295 instances and local (nested) function definitions. */
26296 if (must_output_die)
26298 if (inlined_func)
26299 gen_inlined_subroutine_die (stmt, context_die);
26300 else
26301 gen_lexical_block_die (stmt, context_die);
26303 else
26304 decls_for_scope (stmt, context_die);
26307 /* Process variable DECL (or variable with origin ORIGIN) within
26308 block STMT and add it to CONTEXT_DIE. */
26309 static void
26310 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
26312 dw_die_ref die;
26313 tree decl_or_origin = decl ? decl : origin;
26315 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
26316 die = lookup_decl_die (decl_or_origin);
26317 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
26319 if (TYPE_DECL_IS_STUB (decl_or_origin))
26320 die = lookup_type_die (TREE_TYPE (decl_or_origin));
26321 else
26322 die = lookup_decl_die (decl_or_origin);
26323 /* Avoid re-creating the DIE late if it was optimized as unused early. */
26324 if (! die && ! early_dwarf)
26325 return;
26327 else
26328 die = NULL;
26330 /* Avoid creating DIEs for local typedefs and concrete static variables that
26331 will only be pruned later. */
26332 if ((origin || decl_ultimate_origin (decl))
26333 && (TREE_CODE (decl_or_origin) == TYPE_DECL
26334 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
26336 origin = decl_ultimate_origin (decl_or_origin);
26337 if (decl && VAR_P (decl) && die != NULL)
26339 die = lookup_decl_die (origin);
26340 if (die != NULL)
26341 equate_decl_number_to_die (decl, die);
26343 return;
26346 if (die != NULL && die->die_parent == NULL)
26347 add_child_die (context_die, die);
26348 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
26350 if (early_dwarf)
26351 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
26352 stmt, context_die);
26354 else
26356 if (decl && DECL_P (decl))
26358 die = lookup_decl_die (decl);
26360 /* Early created DIEs do not have a parent as the decls refer
26361 to the function as DECL_CONTEXT rather than the BLOCK. */
26362 if (die && die->die_parent == NULL)
26364 gcc_assert (in_lto_p);
26365 add_child_die (context_die, die);
26369 gen_decl_die (decl, origin, NULL, context_die);
26373 /* Generate all of the decls declared within a given scope and (recursively)
26374 all of its sub-blocks. */
26376 static void
26377 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
26379 tree decl;
26380 unsigned int i;
26381 tree subblocks;
26383 /* Ignore NULL blocks. */
26384 if (stmt == NULL_TREE)
26385 return;
26387 /* Output the DIEs to represent all of the data objects and typedefs
26388 declared directly within this block but not within any nested
26389 sub-blocks. Also, nested function and tag DIEs have been
26390 generated with a parent of NULL; fix that up now. We don't
26391 have to do this if we're at -g1. */
26392 if (debug_info_level > DINFO_LEVEL_TERSE)
26394 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
26395 process_scope_var (stmt, decl, NULL_TREE, context_die);
26396 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
26397 origin - avoid doing this twice as we have no good way to see
26398 if we've done it once already. */
26399 if (! early_dwarf)
26400 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
26402 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
26403 if (decl == current_function_decl)
26404 /* Ignore declarations of the current function, while they
26405 are declarations, gen_subprogram_die would treat them
26406 as definitions again, because they are equal to
26407 current_function_decl and endlessly recurse. */;
26408 else if (TREE_CODE (decl) == FUNCTION_DECL)
26409 process_scope_var (stmt, decl, NULL_TREE, context_die);
26410 else
26411 process_scope_var (stmt, NULL_TREE, decl, context_die);
26415 /* Even if we're at -g1, we need to process the subblocks in order to get
26416 inlined call information. */
26418 /* Output the DIEs to represent all sub-blocks (and the items declared
26419 therein) of this block. */
26420 if (recurse)
26421 for (subblocks = BLOCK_SUBBLOCKS (stmt);
26422 subblocks != NULL;
26423 subblocks = BLOCK_CHAIN (subblocks))
26424 gen_block_die (subblocks, context_die);
26427 /* Is this a typedef we can avoid emitting? */
26429 static bool
26430 is_redundant_typedef (const_tree decl)
26432 if (TYPE_DECL_IS_STUB (decl))
26433 return true;
26435 if (DECL_ARTIFICIAL (decl)
26436 && DECL_CONTEXT (decl)
26437 && is_tagged_type (DECL_CONTEXT (decl))
26438 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
26439 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
26440 /* Also ignore the artificial member typedef for the class name. */
26441 return true;
26443 return false;
26446 /* Return TRUE if TYPE is a typedef that names a type for linkage
26447 purposes. This kind of typedefs is produced by the C++ FE for
26448 constructs like:
26450 typedef struct {...} foo;
26452 In that case, there is no typedef variant type produced for foo.
26453 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
26454 struct type. */
26456 static bool
26457 is_naming_typedef_decl (const_tree decl)
26459 if (decl == NULL_TREE
26460 || TREE_CODE (decl) != TYPE_DECL
26461 || DECL_NAMELESS (decl)
26462 || !is_tagged_type (TREE_TYPE (decl))
26463 || DECL_IS_UNDECLARED_BUILTIN (decl)
26464 || is_redundant_typedef (decl)
26465 /* It looks like Ada produces TYPE_DECLs that are very similar
26466 to C++ naming typedefs but that have different
26467 semantics. Let's be specific to c++ for now. */
26468 || !is_cxx (decl))
26469 return FALSE;
26471 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
26472 && TYPE_NAME (TREE_TYPE (decl)) == decl
26473 && (TYPE_STUB_DECL (TREE_TYPE (decl))
26474 != TYPE_NAME (TREE_TYPE (decl))));
26477 /* Looks up the DIE for a context. */
26479 static inline dw_die_ref
26480 lookup_context_die (tree context)
26482 if (context)
26484 /* Find die that represents this context. */
26485 if (TYPE_P (context))
26487 context = TYPE_MAIN_VARIANT (context);
26488 dw_die_ref ctx = lookup_type_die (context);
26489 if (!ctx)
26490 return NULL;
26491 return strip_naming_typedef (context, ctx);
26493 else
26494 return lookup_decl_die (context);
26496 return comp_unit_die ();
26499 /* Returns the DIE for a context. */
26501 static inline dw_die_ref
26502 get_context_die (tree context)
26504 if (context)
26506 /* Find die that represents this context. */
26507 if (TYPE_P (context))
26509 context = TYPE_MAIN_VARIANT (context);
26510 return strip_naming_typedef (context, force_type_die (context));
26512 else
26513 return force_decl_die (context);
26515 return comp_unit_die ();
26518 /* Returns the DIE for decl. A DIE will always be returned. */
26520 static dw_die_ref
26521 force_decl_die (tree decl)
26523 dw_die_ref decl_die;
26524 unsigned saved_external_flag;
26525 tree save_fn = NULL_TREE;
26526 decl_die = lookup_decl_die (decl);
26527 if (!decl_die)
26529 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
26531 decl_die = lookup_decl_die (decl);
26532 if (decl_die)
26533 return decl_die;
26535 switch (TREE_CODE (decl))
26537 case FUNCTION_DECL:
26538 /* Clear current_function_decl, so that gen_subprogram_die thinks
26539 that this is a declaration. At this point, we just want to force
26540 declaration die. */
26541 save_fn = current_function_decl;
26542 current_function_decl = NULL_TREE;
26543 gen_subprogram_die (decl, context_die);
26544 current_function_decl = save_fn;
26545 break;
26547 case VAR_DECL:
26548 /* Set external flag to force declaration die. Restore it after
26549 gen_decl_die() call. */
26550 saved_external_flag = DECL_EXTERNAL (decl);
26551 DECL_EXTERNAL (decl) = 1;
26552 gen_decl_die (decl, NULL, NULL, context_die);
26553 DECL_EXTERNAL (decl) = saved_external_flag;
26554 break;
26556 case NAMESPACE_DECL:
26557 if (dwarf_version >= 3 || !dwarf_strict)
26558 dwarf2out_decl (decl);
26559 else
26560 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
26561 decl_die = comp_unit_die ();
26562 break;
26564 case CONST_DECL:
26565 /* Enumerators shouldn't need force_decl_die. */
26566 gcc_assert (DECL_CONTEXT (decl) == NULL_TREE
26567 || TREE_CODE (DECL_CONTEXT (decl)) != ENUMERAL_TYPE);
26568 gen_decl_die (decl, NULL, NULL, context_die);
26569 break;
26571 case TRANSLATION_UNIT_DECL:
26572 decl_die = comp_unit_die ();
26573 break;
26575 default:
26576 gcc_unreachable ();
26579 /* We should be able to find the DIE now. */
26580 if (!decl_die)
26581 decl_die = lookup_decl_die (decl);
26582 gcc_assert (decl_die);
26585 return decl_die;
26588 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
26589 always returned. */
26591 static dw_die_ref
26592 force_type_die (tree type)
26594 dw_die_ref type_die;
26596 type_die = lookup_type_die (type);
26597 if (!type_die)
26599 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
26601 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
26602 false, context_die);
26603 gcc_assert (type_die);
26605 return type_die;
26608 /* Force out any required namespaces to be able to output DECL,
26609 and return the new context_die for it, if it's changed. */
26611 static dw_die_ref
26612 setup_namespace_context (tree thing, dw_die_ref context_die)
26614 tree context = (DECL_P (thing)
26615 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
26616 if (context && TREE_CODE (context) == NAMESPACE_DECL)
26617 /* Force out the namespace. */
26618 context_die = force_decl_die (context);
26620 return context_die;
26623 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26624 type) within its namespace, if appropriate.
26626 For compatibility with older debuggers, namespace DIEs only contain
26627 declarations; all definitions are emitted at CU scope, with
26628 DW_AT_specification pointing to the declaration (like with class
26629 members). */
26631 static dw_die_ref
26632 declare_in_namespace (tree thing, dw_die_ref context_die)
26634 dw_die_ref ns_context;
26636 if (debug_info_level <= DINFO_LEVEL_TERSE)
26637 return context_die;
26639 /* External declarations in the local scope only need to be emitted
26640 once, not once in the namespace and once in the scope.
26642 This avoids declaring the `extern' below in the
26643 namespace DIE as well as in the innermost scope:
26645 namespace S
26647 int i=5;
26648 int foo()
26650 int i=8;
26651 extern int i;
26652 return i;
26656 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26657 return context_die;
26659 /* If this decl is from an inlined function, then don't try to emit it in its
26660 namespace, as we will get confused. It would have already been emitted
26661 when the abstract instance of the inline function was emitted anyways. */
26662 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26663 return context_die;
26665 ns_context = setup_namespace_context (thing, context_die);
26667 if (ns_context != context_die)
26669 if (is_fortran () || is_dlang ())
26670 return ns_context;
26671 if (DECL_P (thing))
26672 gen_decl_die (thing, NULL, NULL, ns_context);
26673 else
26674 gen_type_die (thing, ns_context);
26676 return context_die;
26679 /* Generate a DIE for a namespace or namespace alias. */
26681 static void
26682 gen_namespace_die (tree decl, dw_die_ref context_die)
26684 dw_die_ref namespace_die;
26686 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26687 they are an alias of. */
26688 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26690 /* Output a real namespace or module. */
26691 context_die = setup_namespace_context (decl, comp_unit_die ());
26692 namespace_die = new_die (is_fortran () || is_dlang ()
26693 ? DW_TAG_module : DW_TAG_namespace,
26694 context_die, decl);
26695 /* For Fortran modules defined in different CU don't add src coords. */
26696 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26698 const char *name = dwarf2_name (decl, 0);
26699 if (name)
26700 add_name_attribute (namespace_die, name);
26702 else
26703 add_name_and_src_coords_attributes (namespace_die, decl);
26704 if (DECL_EXTERNAL (decl))
26705 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26706 equate_decl_number_to_die (decl, namespace_die);
26708 else
26710 /* Output a namespace alias. */
26712 /* Force out the namespace we are an alias of, if necessary. */
26713 dw_die_ref origin_die
26714 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26716 if (DECL_FILE_SCOPE_P (decl)
26717 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26718 context_die = setup_namespace_context (decl, comp_unit_die ());
26719 /* Now create the namespace alias DIE. */
26720 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26721 add_name_and_src_coords_attributes (namespace_die, decl);
26722 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26723 equate_decl_number_to_die (decl, namespace_die);
26725 if ((dwarf_version >= 5 || !dwarf_strict)
26726 && lang_hooks.decls.decl_dwarf_attribute (decl,
26727 DW_AT_export_symbols) == 1)
26728 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26730 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26731 if (want_pubnames ())
26732 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26735 /* Generate Dwarf debug information for a decl described by DECL.
26736 The return value is currently only meaningful for PARM_DECLs,
26737 for all other decls it returns NULL.
26739 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26740 It can be NULL otherwise. */
26742 static dw_die_ref
26743 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26744 dw_die_ref context_die)
26746 tree decl_or_origin = decl ? decl : origin;
26747 tree class_origin = NULL, ultimate_origin;
26749 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26750 return NULL;
26752 switch (TREE_CODE (decl_or_origin))
26754 case ERROR_MARK:
26755 break;
26757 case CONST_DECL:
26758 if (!is_fortran () && !is_ada () && !is_dlang ())
26760 /* The individual enumerators of an enum type get output when we output
26761 the Dwarf representation of the relevant enum type itself. */
26762 break;
26765 /* Emit its type. */
26766 gen_type_die (TREE_TYPE (decl), context_die);
26768 /* And its containing namespace. */
26769 context_die = declare_in_namespace (decl, context_die);
26771 gen_const_die (decl, context_die);
26772 break;
26774 case FUNCTION_DECL:
26775 #if 0
26776 /* FIXME */
26777 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26778 on local redeclarations of global functions. That seems broken. */
26779 if (current_function_decl != decl)
26780 /* This is only a declaration. */;
26781 #endif
26783 /* We should have abstract copies already and should not generate
26784 stray type DIEs in late LTO dumping. */
26785 if (! early_dwarf)
26788 /* If we're emitting a clone, emit info for the abstract instance. */
26789 else if (origin || DECL_ORIGIN (decl) != decl)
26790 dwarf2out_abstract_function (origin
26791 ? DECL_ORIGIN (origin)
26792 : DECL_ABSTRACT_ORIGIN (decl));
26794 /* If we're emitting a possibly inlined function emit it as
26795 abstract instance. */
26796 else if (cgraph_function_possibly_inlined_p (decl)
26797 && ! DECL_ABSTRACT_P (decl)
26798 && ! class_or_namespace_scope_p (context_die)
26799 /* dwarf2out_abstract_function won't emit a die if this is just
26800 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26801 that case, because that works only if we have a die. */
26802 && DECL_INITIAL (decl) != NULL_TREE)
26803 dwarf2out_abstract_function (decl);
26805 /* Otherwise we're emitting the primary DIE for this decl. */
26806 else if (debug_info_level > DINFO_LEVEL_TERSE)
26808 /* Before we describe the FUNCTION_DECL itself, make sure that we
26809 have its containing type. */
26810 if (!origin)
26811 origin = decl_class_context (decl);
26812 if (origin != NULL_TREE)
26813 gen_type_die (origin, context_die);
26815 /* And its return type. */
26816 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26818 /* And its virtual context. */
26819 if (DECL_VINDEX (decl) != NULL_TREE)
26820 gen_type_die (DECL_CONTEXT (decl), context_die);
26822 /* Make sure we have a member DIE for decl. */
26823 if (origin != NULL_TREE)
26824 gen_type_die_for_member (origin, decl, context_die);
26826 /* And its containing namespace. */
26827 context_die = declare_in_namespace (decl, context_die);
26830 /* Now output a DIE to represent the function itself. */
26831 if (decl)
26832 gen_subprogram_die (decl, context_die);
26833 break;
26835 case TYPE_DECL:
26836 /* If we are in terse mode, don't generate any DIEs to represent any
26837 actual typedefs. */
26838 if (debug_info_level <= DINFO_LEVEL_TERSE)
26839 break;
26841 /* In the special case of a TYPE_DECL node representing the declaration
26842 of some type tag, if the given TYPE_DECL is marked as having been
26843 instantiated from some other (original) TYPE_DECL node (e.g. one which
26844 was generated within the original definition of an inline function) we
26845 used to generate a special (abbreviated) DW_TAG_structure_type,
26846 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26847 should be actually referencing those DIEs, as variable DIEs with that
26848 type would be emitted already in the abstract origin, so it was always
26849 removed during unused type prunning. Don't add anything in this
26850 case. */
26851 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26852 break;
26854 if (is_redundant_typedef (decl))
26855 gen_type_die (TREE_TYPE (decl), context_die);
26856 else
26857 /* Output a DIE to represent the typedef itself. */
26858 gen_typedef_die (decl, context_die);
26859 break;
26861 case LABEL_DECL:
26862 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26863 gen_label_die (decl, context_die);
26864 break;
26866 case VAR_DECL:
26867 case RESULT_DECL:
26868 /* If we are in terse mode, don't generate any DIEs to represent any
26869 variable declarations or definitions unless it is external. */
26870 if (debug_info_level < DINFO_LEVEL_TERSE
26871 || (debug_info_level == DINFO_LEVEL_TERSE
26872 && !TREE_PUBLIC (decl_or_origin)))
26873 break;
26875 if (debug_info_level > DINFO_LEVEL_TERSE)
26877 /* Avoid generating stray type DIEs during late dwarf dumping.
26878 All types have been dumped early. */
26879 if (early_dwarf
26880 /* ??? But in LTRANS we cannot annotate early created variably
26881 modified type DIEs without copying them and adjusting all
26882 references to them. Dump them again as happens for inlining
26883 which copies both the decl and the types. */
26884 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26885 in VLA bound information for example. */
26886 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26887 current_function_decl)))
26889 /* Output any DIEs that are needed to specify the type of this data
26890 object. */
26891 if (decl_by_reference_p (decl_or_origin))
26892 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26893 else
26894 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26897 if (early_dwarf)
26899 /* And its containing type. */
26900 class_origin = decl_class_context (decl_or_origin);
26901 if (class_origin != NULL_TREE)
26902 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26904 /* And its containing namespace. */
26905 context_die = declare_in_namespace (decl_or_origin, context_die);
26909 /* Now output the DIE to represent the data object itself. This gets
26910 complicated because of the possibility that the VAR_DECL really
26911 represents an inlined instance of a formal parameter for an inline
26912 function. */
26913 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26914 if (ultimate_origin != NULL_TREE
26915 && TREE_CODE (ultimate_origin) == PARM_DECL)
26916 gen_formal_parameter_die (decl, origin,
26917 true /* Emit name attribute. */,
26918 context_die);
26919 else
26920 gen_variable_die (decl, origin, context_die);
26921 break;
26923 case FIELD_DECL:
26924 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26925 /* Ignore the nameless fields that are used to skip bits but handle C++
26926 anonymous unions and structs. */
26927 if (DECL_NAME (decl) != NULL_TREE
26928 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26929 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26931 gen_type_die (member_declared_type (decl), context_die);
26932 gen_field_die (decl, ctx, context_die);
26934 break;
26936 case PARM_DECL:
26937 /* Avoid generating stray type DIEs during late dwarf dumping.
26938 All types have been dumped early. */
26939 if (early_dwarf
26940 /* ??? But in LTRANS we cannot annotate early created variably
26941 modified type DIEs without copying them and adjusting all
26942 references to them. Dump them again as happens for inlining
26943 which copies both the decl and the types. */
26944 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26945 in VLA bound information for example. */
26946 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26947 current_function_decl)))
26949 if (DECL_BY_REFERENCE (decl_or_origin))
26950 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26951 else
26952 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26954 return gen_formal_parameter_die (decl, origin,
26955 true /* Emit name attribute. */,
26956 context_die);
26958 case NAMESPACE_DECL:
26959 if (dwarf_version >= 3 || !dwarf_strict)
26960 gen_namespace_die (decl, context_die);
26961 break;
26963 case IMPORTED_DECL:
26964 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26965 DECL_CONTEXT (decl), context_die);
26966 break;
26968 case NAMELIST_DECL:
26969 gen_namelist_decl (DECL_NAME (decl), context_die,
26970 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26971 break;
26973 default:
26974 /* Probably some frontend-internal decl. Assume we don't care. */
26975 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26976 break;
26979 return NULL;
26982 /* Output initial debug information for global DECL. Called at the
26983 end of the parsing process.
26985 This is the initial debug generation process. As such, the DIEs
26986 generated may be incomplete. A later debug generation pass
26987 (dwarf2out_late_global_decl) will augment the information generated
26988 in this pass (e.g., with complete location info). */
26990 static void
26991 dwarf2out_early_global_decl (tree decl)
26993 set_early_dwarf s;
26995 /* gen_decl_die() will set DECL_ABSTRACT because
26996 cgraph_function_possibly_inlined_p() returns true. This is in
26997 turn will cause DW_AT_inline attributes to be set.
26999 This happens because at early dwarf generation, there is no
27000 cgraph information, causing cgraph_function_possibly_inlined_p()
27001 to return true. Trick cgraph_function_possibly_inlined_p()
27002 while we generate dwarf early. */
27003 bool save = symtab->global_info_ready;
27004 symtab->global_info_ready = true;
27006 /* We don't handle TYPE_DECLs. If required, they'll be reached via
27007 other DECLs and they can point to template types or other things
27008 that dwarf2out can't handle when done via dwarf2out_decl. */
27009 if (TREE_CODE (decl) != TYPE_DECL
27010 && TREE_CODE (decl) != PARM_DECL)
27012 if (TREE_CODE (decl) == FUNCTION_DECL)
27014 tree save_fndecl = current_function_decl;
27016 /* For nested functions, make sure we have DIEs for the parents first
27017 so that all nested DIEs are generated at the proper scope in the
27018 first shot. */
27019 tree context = decl_function_context (decl);
27020 if (context != NULL)
27022 dw_die_ref context_die = lookup_decl_die (context);
27023 current_function_decl = context;
27025 /* Avoid emitting DIEs multiple times, but still process CONTEXT
27026 enough so that it lands in its own context. This avoids type
27027 pruning issues later on. */
27028 if (context_die == NULL || is_declaration_die (context_die))
27029 dwarf2out_early_global_decl (context);
27032 /* Emit an abstract origin of a function first. This happens
27033 with C++ constructor clones for example and makes
27034 dwarf2out_abstract_function happy which requires the early
27035 DIE of the abstract instance to be present. */
27036 tree origin = DECL_ABSTRACT_ORIGIN (decl);
27037 dw_die_ref origin_die;
27038 if (origin != NULL
27039 /* Do not emit the DIE multiple times but make sure to
27040 process it fully here in case we just saw a declaration. */
27041 && ((origin_die = lookup_decl_die (origin)) == NULL
27042 || is_declaration_die (origin_die)))
27044 current_function_decl = origin;
27045 dwarf2out_decl (origin);
27048 /* Emit the DIE for decl but avoid doing that multiple times. */
27049 dw_die_ref old_die;
27050 if ((old_die = lookup_decl_die (decl)) == NULL
27051 || is_declaration_die (old_die))
27053 current_function_decl = decl;
27054 dwarf2out_decl (decl);
27057 current_function_decl = save_fndecl;
27059 else
27060 dwarf2out_decl (decl);
27062 symtab->global_info_ready = save;
27065 /* Return whether EXPR is an expression with the following pattern:
27066 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
27068 static bool
27069 is_trivial_indirect_ref (tree expr)
27071 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
27072 return false;
27074 tree nop = TREE_OPERAND (expr, 0);
27075 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
27076 return false;
27078 tree int_cst = TREE_OPERAND (nop, 0);
27079 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
27082 /* Output debug information for global decl DECL. Called from
27083 toplev.c after compilation proper has finished. */
27085 static void
27086 dwarf2out_late_global_decl (tree decl)
27088 /* Fill-in any location information we were unable to determine
27089 on the first pass. */
27090 if (VAR_P (decl))
27092 dw_die_ref die = lookup_decl_die (decl);
27094 /* We may have to generate full debug late for LTO in case debug
27095 was not enabled at compile-time or the target doesn't support
27096 the LTO early debug scheme. */
27097 if (! die && in_lto_p)
27098 dwarf2out_decl (decl);
27099 else if (die)
27101 /* We get called via the symtab code invoking late_global_decl
27102 for symbols that are optimized out.
27104 Do not add locations for those, except if they have a
27105 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
27106 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
27107 INDIRECT_REF expression, as this could generate relocations to
27108 text symbols in LTO object files, which is invalid. */
27109 varpool_node *node = varpool_node::get (decl);
27110 if ((! node || ! node->definition)
27111 && ! (DECL_HAS_VALUE_EXPR_P (decl)
27112 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
27113 tree_add_const_value_attribute_for_decl (die, decl);
27114 else
27115 add_location_or_const_value_attribute (die, decl, false);
27120 /* Output debug information for type decl DECL. Called from toplev.c
27121 and from language front ends (to record built-in types). */
27122 static void
27123 dwarf2out_type_decl (tree decl, int local)
27125 if (!local)
27127 set_early_dwarf s;
27128 dwarf2out_decl (decl);
27132 /* Output debug information for imported module or decl DECL.
27133 NAME is non-NULL name in the lexical block if the decl has been renamed.
27134 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
27135 that DECL belongs to.
27136 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
27137 static void
27138 dwarf2out_imported_module_or_decl_1 (tree decl,
27139 tree name,
27140 tree lexical_block,
27141 dw_die_ref lexical_block_die)
27143 expanded_location xloc;
27144 dw_die_ref imported_die = NULL;
27145 dw_die_ref at_import_die;
27147 if (TREE_CODE (decl) == IMPORTED_DECL)
27149 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
27150 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
27151 gcc_assert (decl);
27153 else
27154 xloc = expand_location (input_location);
27156 if (TREE_CODE (decl) == TYPE_DECL)
27158 at_import_die = force_type_die (TREE_TYPE (decl));
27159 /* For namespace N { typedef void T; } using N::T; base_type_die
27160 returns NULL, but DW_TAG_imported_declaration requires
27161 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
27162 if (!at_import_die)
27164 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
27165 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
27166 at_import_die = lookup_type_die (TREE_TYPE (decl));
27167 gcc_assert (at_import_die);
27170 else
27172 at_import_die = lookup_decl_die (decl);
27173 if (!at_import_die)
27175 /* If we're trying to avoid duplicate debug info, we may not have
27176 emitted the member decl for this field. Emit it now. */
27177 if (TREE_CODE (decl) == FIELD_DECL)
27179 tree type = DECL_CONTEXT (decl);
27181 if (TYPE_CONTEXT (type)
27182 && TYPE_P (TYPE_CONTEXT (type))
27183 && !should_emit_struct_debug (TYPE_CONTEXT (type),
27184 DINFO_USAGE_DIR_USE))
27185 return;
27186 gen_type_die_for_member (type, decl,
27187 get_context_die (TYPE_CONTEXT (type)));
27189 if (TREE_CODE (decl) == CONST_DECL)
27191 /* Individual enumerators of an enum type do not get output here
27192 (see gen_decl_die), so we cannot call force_decl_die. */
27193 if (!is_fortran () && !is_ada () && !is_dlang ())
27194 return;
27196 if (TREE_CODE (decl) == NAMELIST_DECL)
27197 at_import_die = gen_namelist_decl (DECL_NAME (decl),
27198 get_context_die (DECL_CONTEXT (decl)),
27199 NULL_TREE);
27200 else
27201 at_import_die = force_decl_die (decl);
27205 if (TREE_CODE (decl) == NAMESPACE_DECL)
27207 if (dwarf_version >= 3 || !dwarf_strict)
27208 imported_die = new_die (DW_TAG_imported_module,
27209 lexical_block_die,
27210 lexical_block);
27211 else
27212 return;
27214 else
27215 imported_die = new_die (DW_TAG_imported_declaration,
27216 lexical_block_die,
27217 lexical_block);
27219 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
27220 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
27221 if (debug_column_info && xloc.column)
27222 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
27223 if (name)
27224 add_AT_string (imported_die, DW_AT_name,
27225 IDENTIFIER_POINTER (name));
27226 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
27229 /* Output debug information for imported module or decl DECL.
27230 NAME is non-NULL name in context if the decl has been renamed.
27231 CHILD is true if decl is one of the renamed decls as part of
27232 importing whole module.
27233 IMPLICIT is set if this hook is called for an implicit import
27234 such as inline namespace. */
27236 static void
27237 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
27238 bool child, bool implicit)
27240 /* dw_die_ref at_import_die; */
27241 dw_die_ref scope_die;
27243 if (debug_info_level <= DINFO_LEVEL_TERSE)
27244 return;
27246 gcc_assert (decl);
27248 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
27249 should be enough, for DWARF4 and older even if we emit as extension
27250 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
27251 for the benefit of consumers unaware of DW_AT_export_symbols. */
27252 if (implicit
27253 && dwarf_version >= 5
27254 && lang_hooks.decls.decl_dwarf_attribute (decl,
27255 DW_AT_export_symbols) == 1)
27256 return;
27258 set_early_dwarf s;
27260 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
27261 We need decl DIE for reference and scope die. First, get DIE for the decl
27262 itself. */
27264 /* Get the scope die for decl context. Use comp_unit_die for global module
27265 or decl. If die is not found for non globals, force new die. */
27266 if (context
27267 && TYPE_P (context)
27268 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
27269 return;
27271 scope_die = get_context_die (context);
27273 if (child)
27275 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
27276 there is nothing we can do, here. */
27277 if (dwarf_version < 3 && dwarf_strict)
27278 return;
27280 gcc_assert (scope_die->die_child);
27281 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
27282 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
27283 scope_die = scope_die->die_child;
27286 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
27287 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
27290 /* Output debug information for namelists. */
27292 static dw_die_ref
27293 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
27295 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
27296 tree value;
27297 unsigned i;
27299 if (debug_info_level <= DINFO_LEVEL_TERSE)
27300 return NULL;
27302 gcc_assert (scope_die != NULL);
27303 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
27304 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
27306 /* If there are no item_decls, we have a nondefining namelist, e.g.
27307 with USE association; hence, set DW_AT_declaration. */
27308 if (item_decls == NULL_TREE)
27310 add_AT_flag (nml_die, DW_AT_declaration, 1);
27311 return nml_die;
27314 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
27316 nml_item_ref_die = lookup_decl_die (value);
27317 if (!nml_item_ref_die)
27318 nml_item_ref_die = force_decl_die (value);
27320 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
27321 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
27323 return nml_die;
27327 /* Write the debugging output for DECL and return the DIE. */
27329 static void
27330 dwarf2out_decl (tree decl)
27332 dw_die_ref context_die = comp_unit_die ();
27334 switch (TREE_CODE (decl))
27336 case ERROR_MARK:
27337 return;
27339 case FUNCTION_DECL:
27340 /* If we're a nested function, initially use a parent of NULL; if we're
27341 a plain function, this will be fixed up in decls_for_scope. If
27342 we're a method, it will be ignored, since we already have a DIE.
27343 Avoid doing this late though since clones of class methods may
27344 otherwise end up in limbo and create type DIEs late. */
27345 if (early_dwarf
27346 && decl_function_context (decl)
27347 /* But if we're in terse mode, we don't care about scope. */
27348 && debug_info_level > DINFO_LEVEL_TERSE)
27349 context_die = NULL;
27350 break;
27352 case VAR_DECL:
27353 /* For local statics lookup proper context die. */
27354 if (local_function_static (decl))
27355 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27357 /* If we are in terse mode, don't generate any DIEs to represent any
27358 variable declarations or definitions unless it is external. */
27359 if (debug_info_level < DINFO_LEVEL_TERSE
27360 || (debug_info_level == DINFO_LEVEL_TERSE
27361 && !TREE_PUBLIC (decl)))
27362 return;
27363 break;
27365 case CONST_DECL:
27366 if (debug_info_level <= DINFO_LEVEL_TERSE)
27367 return;
27368 if (!is_fortran () && !is_ada () && !is_dlang ())
27369 return;
27370 if (TREE_STATIC (decl) && decl_function_context (decl))
27371 context_die = lookup_decl_die (DECL_CONTEXT (decl));
27372 break;
27374 case NAMESPACE_DECL:
27375 case IMPORTED_DECL:
27376 if (debug_info_level <= DINFO_LEVEL_TERSE)
27377 return;
27378 if (lookup_decl_die (decl) != NULL)
27379 return;
27380 break;
27382 case TYPE_DECL:
27383 /* Don't emit stubs for types unless they are needed by other DIEs. */
27384 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
27385 return;
27387 /* Don't bother trying to generate any DIEs to represent any of the
27388 normal built-in types for the language we are compiling. */
27389 if (DECL_IS_UNDECLARED_BUILTIN (decl))
27390 return;
27392 /* If we are in terse mode, don't generate any DIEs for types. */
27393 if (debug_info_level <= DINFO_LEVEL_TERSE)
27394 return;
27396 /* If we're a function-scope tag, initially use a parent of NULL;
27397 this will be fixed up in decls_for_scope. */
27398 if (decl_function_context (decl))
27399 context_die = NULL;
27401 break;
27403 case NAMELIST_DECL:
27404 break;
27406 default:
27407 return;
27410 gen_decl_die (decl, NULL, NULL, context_die);
27412 if (flag_checking)
27414 dw_die_ref die = lookup_decl_die (decl);
27415 if (die)
27416 check_die (die);
27420 /* Write the debugging output for DECL. */
27422 static void
27423 dwarf2out_function_decl (tree decl)
27425 dwarf2out_decl (decl);
27426 call_arg_locations = NULL;
27427 call_arg_loc_last = NULL;
27428 call_site_count = -1;
27429 tail_call_site_count = -1;
27430 decl_loc_table->empty ();
27431 cached_dw_loc_list_table->empty ();
27434 /* Output a marker (i.e. a label) for the beginning of the generated code for
27435 a lexical block. */
27437 static void
27438 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
27439 unsigned int blocknum)
27441 switch_to_section (current_function_section ());
27442 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
27445 /* Output a marker (i.e. a label) for the end of the generated code for a
27446 lexical block. */
27448 static void
27449 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
27451 switch_to_section (current_function_section ());
27452 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
27455 /* Returns nonzero if it is appropriate not to emit any debugging
27456 information for BLOCK, because it doesn't contain any instructions.
27458 Don't allow this for blocks with nested functions or local classes
27459 as we would end up with orphans, and in the presence of scheduling
27460 we may end up calling them anyway. */
27462 static bool
27463 dwarf2out_ignore_block (const_tree block)
27465 tree decl;
27466 unsigned int i;
27468 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
27469 if (TREE_CODE (decl) == FUNCTION_DECL
27470 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27471 return 0;
27472 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
27474 decl = BLOCK_NONLOCALIZED_VAR (block, i);
27475 if (TREE_CODE (decl) == FUNCTION_DECL
27476 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
27477 return 0;
27480 return 1;
27483 /* Hash table routines for file_hash. */
27485 bool
27486 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
27488 return filename_cmp (p1->key, p2) == 0;
27491 hashval_t
27492 dwarf_file_hasher::hash (dwarf_file_data *p)
27494 return htab_hash_string (p->key);
27497 /* Lookup FILE_NAME (in the list of filenames that we know about here in
27498 dwarf2out.c) and return its "index". The index of each (known) filename is
27499 just a unique number which is associated with only that one filename. We
27500 need such numbers for the sake of generating labels (in the .debug_sfnames
27501 section) and references to those files numbers (in the .debug_srcinfo
27502 and .debug_macinfo sections). If the filename given as an argument is not
27503 found in our current list, add it to the list and assign it the next
27504 available unique index number. */
27506 static struct dwarf_file_data *
27507 lookup_filename (const char *file_name)
27509 struct dwarf_file_data * created;
27511 if (!file_name)
27512 return NULL;
27514 if (!file_name[0])
27515 file_name = "<stdin>";
27517 dwarf_file_data **slot
27518 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
27519 INSERT);
27520 if (*slot)
27521 return *slot;
27523 created = ggc_alloc<dwarf_file_data> ();
27524 created->key = file_name;
27525 created->filename = remap_debug_filename (file_name);
27526 created->emitted_number = 0;
27527 *slot = created;
27528 return created;
27531 /* If the assembler will construct the file table, then translate the compiler
27532 internal file table number into the assembler file table number, and emit
27533 a .file directive if we haven't already emitted one yet. The file table
27534 numbers are different because we prune debug info for unused variables and
27535 types, which may include filenames. */
27537 static int
27538 maybe_emit_file (struct dwarf_file_data * fd)
27540 if (! fd->emitted_number)
27542 if (last_emitted_file)
27543 fd->emitted_number = last_emitted_file->emitted_number + 1;
27544 else
27545 fd->emitted_number = 1;
27546 last_emitted_file = fd;
27548 if (output_asm_line_debug_info ())
27550 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
27551 output_quoted_string (asm_out_file, fd->filename);
27552 fputc ('\n', asm_out_file);
27556 return fd->emitted_number;
27559 /* Schedule generation of a DW_AT_const_value attribute to DIE.
27560 That generation should happen after function debug info has been
27561 generated. The value of the attribute is the constant value of ARG. */
27563 static void
27564 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
27566 die_arg_entry entry;
27568 if (!die || !arg)
27569 return;
27571 gcc_assert (early_dwarf);
27573 if (!tmpl_value_parm_die_table)
27574 vec_alloc (tmpl_value_parm_die_table, 32);
27576 entry.die = die;
27577 entry.arg = arg;
27578 vec_safe_push (tmpl_value_parm_die_table, entry);
27581 /* Return TRUE if T is an instance of generic type, FALSE
27582 otherwise. */
27584 static bool
27585 generic_type_p (tree t)
27587 if (t == NULL_TREE || !TYPE_P (t))
27588 return false;
27589 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
27592 /* Schedule the generation of the generic parameter dies for the
27593 instance of generic type T. The proper generation itself is later
27594 done by gen_scheduled_generic_parms_dies. */
27596 static void
27597 schedule_generic_params_dies_gen (tree t)
27599 if (!generic_type_p (t))
27600 return;
27602 gcc_assert (early_dwarf);
27604 if (!generic_type_instances)
27605 vec_alloc (generic_type_instances, 256);
27607 vec_safe_push (generic_type_instances, t);
27610 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
27611 by append_entry_to_tmpl_value_parm_die_table. This function must
27612 be called after function DIEs have been generated. */
27614 static void
27615 gen_remaining_tmpl_value_param_die_attribute (void)
27617 if (tmpl_value_parm_die_table)
27619 unsigned i, j;
27620 die_arg_entry *e;
27622 /* We do this in two phases - first get the cases we can
27623 handle during early-finish, preserving those we cannot
27624 (containing symbolic constants where we don't yet know
27625 whether we are going to output the referenced symbols).
27626 For those we try again at late-finish. */
27627 j = 0;
27628 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
27630 if (!e->die->removed
27631 && !tree_add_const_value_attribute (e->die, e->arg))
27633 dw_loc_descr_ref loc = NULL;
27634 if (! early_dwarf
27635 && (dwarf_version >= 5 || !dwarf_strict))
27636 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27637 if (loc)
27638 add_AT_loc (e->die, DW_AT_location, loc);
27639 else
27640 (*tmpl_value_parm_die_table)[j++] = *e;
27643 tmpl_value_parm_die_table->truncate (j);
27647 /* Generate generic parameters DIEs for instances of generic types
27648 that have been previously scheduled by
27649 schedule_generic_params_dies_gen. This function must be called
27650 after all the types of the CU have been laid out. */
27652 static void
27653 gen_scheduled_generic_parms_dies (void)
27655 unsigned i;
27656 tree t;
27658 if (!generic_type_instances)
27659 return;
27661 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27662 if (COMPLETE_TYPE_P (t))
27663 gen_generic_params_dies (t);
27665 generic_type_instances = NULL;
27669 /* Replace DW_AT_name for the decl with name. */
27671 static void
27672 dwarf2out_set_name (tree decl, tree name)
27674 dw_die_ref die;
27675 dw_attr_node *attr;
27676 const char *dname;
27678 die = TYPE_SYMTAB_DIE (decl);
27679 if (!die)
27680 return;
27682 dname = dwarf2_name (name, 0);
27683 if (!dname)
27684 return;
27686 attr = get_AT (die, DW_AT_name);
27687 if (attr)
27689 struct indirect_string_node *node;
27691 node = find_AT_string (dname);
27692 /* replace the string. */
27693 attr->dw_attr_val.v.val_str = node;
27696 else
27697 add_name_attribute (die, dname);
27700 /* True if before or during processing of the first function being emitted. */
27701 static bool in_first_function_p = true;
27702 /* True if loc_note during dwarf2out_var_location call might still be
27703 before first real instruction at address equal to .Ltext0. */
27704 static bool maybe_at_text_label_p = true;
27705 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27706 static unsigned int first_loclabel_num_not_at_text_label;
27708 /* Look ahead for a real insn. */
27710 static rtx_insn *
27711 dwarf2out_next_real_insn (rtx_insn *loc_note)
27713 rtx_insn *next_real = NEXT_INSN (loc_note);
27715 while (next_real)
27716 if (INSN_P (next_real))
27717 break;
27718 else
27719 next_real = NEXT_INSN (next_real);
27721 return next_real;
27724 /* Called by the final INSN scan whenever we see a var location. We
27725 use it to drop labels in the right places, and throw the location in
27726 our lookup table. */
27728 static void
27729 dwarf2out_var_location (rtx_insn *loc_note)
27731 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27732 struct var_loc_node *newloc;
27733 rtx_insn *next_real;
27734 rtx_insn *call_insn = NULL;
27735 static const char *last_label;
27736 static const char *last_postcall_label;
27737 static bool last_in_cold_section_p;
27738 static rtx_insn *expected_next_loc_note;
27739 tree decl;
27740 bool var_loc_p;
27741 var_loc_view view = 0;
27743 if (!NOTE_P (loc_note))
27745 if (CALL_P (loc_note))
27747 maybe_reset_location_view (loc_note, cur_line_info_table);
27748 call_site_count++;
27749 if (SIBLING_CALL_P (loc_note))
27750 tail_call_site_count++;
27751 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27753 call_insn = loc_note;
27754 loc_note = NULL;
27755 var_loc_p = false;
27757 next_real = dwarf2out_next_real_insn (call_insn);
27758 cached_next_real_insn = NULL;
27759 goto create_label;
27761 if (optimize == 0 && !flag_var_tracking)
27763 /* When the var-tracking pass is not running, there is no note
27764 for indirect calls whose target is compile-time known. In this
27765 case, process such calls specifically so that we generate call
27766 sites for them anyway. */
27767 rtx x = PATTERN (loc_note);
27768 if (GET_CODE (x) == PARALLEL)
27769 x = XVECEXP (x, 0, 0);
27770 if (GET_CODE (x) == SET)
27771 x = SET_SRC (x);
27772 if (GET_CODE (x) == CALL)
27773 x = XEXP (x, 0);
27774 if (!MEM_P (x)
27775 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27776 || !SYMBOL_REF_DECL (XEXP (x, 0))
27777 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27778 != FUNCTION_DECL))
27780 call_insn = loc_note;
27781 loc_note = NULL;
27782 var_loc_p = false;
27784 next_real = dwarf2out_next_real_insn (call_insn);
27785 cached_next_real_insn = NULL;
27786 goto create_label;
27790 else if (!debug_variable_location_views)
27791 gcc_unreachable ();
27792 else
27793 maybe_reset_location_view (loc_note, cur_line_info_table);
27795 return;
27798 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27799 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27800 return;
27802 /* Optimize processing a large consecutive sequence of location
27803 notes so we don't spend too much time in next_real_insn. If the
27804 next insn is another location note, remember the next_real_insn
27805 calculation for next time. */
27806 next_real = cached_next_real_insn;
27807 if (next_real)
27809 if (expected_next_loc_note != loc_note)
27810 next_real = NULL;
27813 if (! next_real)
27814 next_real = dwarf2out_next_real_insn (loc_note);
27816 if (next_real)
27818 rtx_insn *next_note = NEXT_INSN (loc_note);
27819 while (next_note != next_real)
27821 if (! next_note->deleted ()
27822 && NOTE_P (next_note)
27823 && NOTE_KIND (next_note) == NOTE_INSN_VAR_LOCATION)
27824 break;
27825 next_note = NEXT_INSN (next_note);
27828 if (next_note == next_real)
27829 cached_next_real_insn = NULL;
27830 else
27832 expected_next_loc_note = next_note;
27833 cached_next_real_insn = next_real;
27836 else
27837 cached_next_real_insn = NULL;
27839 /* If there are no instructions which would be affected by this note,
27840 don't do anything. */
27841 if (var_loc_p
27842 && next_real == NULL_RTX
27843 && !NOTE_DURING_CALL_P (loc_note))
27844 return;
27846 create_label:
27848 if (next_real == NULL_RTX)
27849 next_real = get_last_insn ();
27851 /* If there were any real insns between note we processed last time
27852 and this note (or if it is the first note), clear
27853 last_{,postcall_}label so that they are not reused this time. */
27854 if (last_var_location_insn == NULL_RTX
27855 || last_var_location_insn != next_real
27856 || last_in_cold_section_p != in_cold_section_p)
27858 last_label = NULL;
27859 last_postcall_label = NULL;
27862 if (var_loc_p)
27864 const char *label
27865 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27866 view = cur_line_info_table->view;
27867 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27868 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27869 if (newloc == NULL)
27870 return;
27872 else
27874 decl = NULL_TREE;
27875 newloc = NULL;
27878 /* If there were no real insns between note we processed last time
27879 and this note, use the label we emitted last time. Otherwise
27880 create a new label and emit it. */
27881 if (last_label == NULL)
27883 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27884 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27885 loclabel_num++;
27886 last_label = ggc_strdup (loclabel);
27887 /* See if loclabel might be equal to .Ltext0. If yes,
27888 bump first_loclabel_num_not_at_text_label. */
27889 if (!have_multiple_function_sections
27890 && in_first_function_p
27891 && maybe_at_text_label_p)
27893 static rtx_insn *last_start;
27894 rtx_insn *insn;
27895 for (insn = loc_note; insn; insn = previous_insn (insn))
27896 if (insn == last_start)
27897 break;
27898 else if (!NONDEBUG_INSN_P (insn))
27899 continue;
27900 else
27902 rtx body = PATTERN (insn);
27903 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27904 continue;
27905 /* Inline asm could occupy zero bytes. */
27906 else if (GET_CODE (body) == ASM_INPUT
27907 || asm_noperands (body) >= 0)
27908 continue;
27909 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27910 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27911 continue;
27912 #endif
27913 else
27915 /* Assume insn has non-zero length. */
27916 maybe_at_text_label_p = false;
27917 break;
27920 if (maybe_at_text_label_p)
27922 last_start = loc_note;
27923 first_loclabel_num_not_at_text_label = loclabel_num;
27928 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27929 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27931 if (!var_loc_p)
27933 struct call_arg_loc_node *ca_loc
27934 = ggc_cleared_alloc<call_arg_loc_node> ();
27935 rtx_insn *prev = call_insn;
27937 ca_loc->call_arg_loc_note
27938 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27939 ca_loc->next = NULL;
27940 ca_loc->label = last_label;
27941 gcc_assert (prev
27942 && (CALL_P (prev)
27943 || (NONJUMP_INSN_P (prev)
27944 && GET_CODE (PATTERN (prev)) == SEQUENCE
27945 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27946 if (!CALL_P (prev))
27947 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27948 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27950 /* Look for a SYMBOL_REF in the "prev" instruction. */
27951 rtx x = get_call_rtx_from (prev);
27952 if (x)
27954 /* Try to get the call symbol, if any. */
27955 if (MEM_P (XEXP (x, 0)))
27956 x = XEXP (x, 0);
27957 /* First, look for a memory access to a symbol_ref. */
27958 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27959 && SYMBOL_REF_DECL (XEXP (x, 0))
27960 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27961 ca_loc->symbol_ref = XEXP (x, 0);
27962 /* Otherwise, look at a compile-time known user-level function
27963 declaration. */
27964 else if (MEM_P (x)
27965 && MEM_EXPR (x)
27966 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27967 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27970 ca_loc->block = insn_scope (prev);
27971 if (call_arg_locations)
27972 call_arg_loc_last->next = ca_loc;
27973 else
27974 call_arg_locations = ca_loc;
27975 call_arg_loc_last = ca_loc;
27977 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27979 newloc->label = last_label;
27980 newloc->view = view;
27982 else
27984 if (!last_postcall_label)
27986 sprintf (loclabel, "%s-1", last_label);
27987 last_postcall_label = ggc_strdup (loclabel);
27989 newloc->label = last_postcall_label;
27990 /* ??? This view is at last_label, not last_label-1, but we
27991 could only assume view at last_label-1 is zero if we could
27992 assume calls always have length greater than one. This is
27993 probably true in general, though there might be a rare
27994 exception to this rule, e.g. if a call insn is optimized out
27995 by target magic. Then, even the -1 in the label will be
27996 wrong, which might invalidate the range. Anyway, using view,
27997 though technically possibly incorrect, will work as far as
27998 ranges go: since L-1 is in the middle of the call insn,
27999 (L-1).0 and (L-1).V shouldn't make any difference, and having
28000 the loclist entry refer to the .loc entry might be useful, so
28001 leave it like this. */
28002 newloc->view = view;
28005 if (var_loc_p && flag_debug_asm)
28007 const char *name, *sep, *patstr;
28008 if (decl && DECL_NAME (decl))
28009 name = IDENTIFIER_POINTER (DECL_NAME (decl));
28010 else
28011 name = "";
28012 if (NOTE_VAR_LOCATION_LOC (loc_note))
28014 sep = " => ";
28015 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
28017 else
28019 sep = " ";
28020 patstr = "RESET";
28022 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
28023 name, sep, patstr);
28026 last_var_location_insn = next_real;
28027 last_in_cold_section_p = in_cold_section_p;
28030 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
28031 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
28032 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
28033 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
28034 BLOCK_FRAGMENT_ORIGIN links. */
28035 static bool
28036 block_within_block_p (tree block, tree outer, bool bothways)
28038 if (block == outer)
28039 return true;
28041 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
28042 for (tree context = BLOCK_SUPERCONTEXT (block);
28043 context != outer;
28044 context = BLOCK_SUPERCONTEXT (context))
28045 if (!context || TREE_CODE (context) != BLOCK)
28046 return false;
28048 if (!bothways)
28049 return true;
28051 /* Now check that each block is actually referenced by its
28052 parent. */
28053 for (tree context = BLOCK_SUPERCONTEXT (block); ;
28054 context = BLOCK_SUPERCONTEXT (context))
28056 if (BLOCK_FRAGMENT_ORIGIN (context))
28058 gcc_assert (!BLOCK_SUBBLOCKS (context));
28059 context = BLOCK_FRAGMENT_ORIGIN (context);
28061 for (tree sub = BLOCK_SUBBLOCKS (context);
28062 sub != block;
28063 sub = BLOCK_CHAIN (sub))
28064 if (!sub)
28065 return false;
28066 if (context == outer)
28067 return true;
28068 else
28069 block = context;
28073 /* Called during final while assembling the marker of the entry point
28074 for an inlined function. */
28076 static void
28077 dwarf2out_inline_entry (tree block)
28079 gcc_assert (debug_inline_points);
28081 /* If we can't represent it, don't bother. */
28082 if (!(dwarf_version >= 3 || !dwarf_strict))
28083 return;
28085 gcc_assert (DECL_P (block_ultimate_origin (block)));
28087 /* Sanity check the block tree. This would catch a case in which
28088 BLOCK got removed from the tree reachable from the outermost
28089 lexical block, but got retained in markers. It would still link
28090 back to its parents, but some ancestor would be missing a link
28091 down the path to the sub BLOCK. If the block got removed, its
28092 BLOCK_NUMBER will not be a usable value. */
28093 if (flag_checking)
28094 gcc_assert (block_within_block_p (block,
28095 DECL_INITIAL (current_function_decl),
28096 true));
28098 gcc_assert (inlined_function_outer_scope_p (block));
28099 gcc_assert (!lookup_block_die (block));
28101 if (BLOCK_FRAGMENT_ORIGIN (block))
28102 block = BLOCK_FRAGMENT_ORIGIN (block);
28103 /* Can the entry point ever not be at the beginning of an
28104 unfragmented lexical block? */
28105 else if (!(BLOCK_FRAGMENT_CHAIN (block)
28106 || (cur_line_info_table
28107 && !ZERO_VIEW_P (cur_line_info_table->view))))
28108 return;
28110 if (!inline_entry_data_table)
28111 inline_entry_data_table
28112 = hash_table<inline_entry_data_hasher>::create_ggc (10);
28115 inline_entry_data **iedp
28116 = inline_entry_data_table->find_slot_with_hash (block,
28117 htab_hash_pointer (block),
28118 INSERT);
28119 if (*iedp)
28120 /* ??? Ideally, we'd record all entry points for the same inlined
28121 function (some may have been duplicated by e.g. unrolling), but
28122 we have no way to represent that ATM. */
28123 return;
28125 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
28126 ied->block = block;
28127 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
28128 ied->label_num = BLOCK_NUMBER (block);
28129 if (cur_line_info_table)
28130 ied->view = cur_line_info_table->view;
28132 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_INLINE_ENTRY_LABEL,
28133 BLOCK_NUMBER (block));
28136 /* Called from finalize_size_functions for size functions so that their body
28137 can be encoded in the debug info to describe the layout of variable-length
28138 structures. */
28140 static void
28141 dwarf2out_size_function (tree decl)
28143 set_early_dwarf s;
28144 function_to_dwarf_procedure (decl);
28147 /* Note in one location list that text section has changed. */
28150 var_location_switch_text_section_1 (var_loc_list **slot, void *)
28152 var_loc_list *list = *slot;
28153 if (list->first)
28154 list->last_before_switch
28155 = list->last->next ? list->last->next : list->last;
28156 return 1;
28159 /* Note in all location lists that text section has changed. */
28161 static void
28162 var_location_switch_text_section (void)
28164 if (decl_loc_table == NULL)
28165 return;
28167 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
28170 /* Create a new line number table. */
28172 static dw_line_info_table *
28173 new_line_info_table (void)
28175 dw_line_info_table *table;
28177 table = ggc_cleared_alloc<dw_line_info_table> ();
28178 table->file_num = 1;
28179 table->line_num = 1;
28180 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
28181 FORCE_RESET_NEXT_VIEW (table->view);
28182 table->symviews_since_reset = 0;
28184 return table;
28187 /* Lookup the "current" table into which we emit line info, so
28188 that we don't have to do it for every source line. */
28190 static void
28191 set_cur_line_info_table (section *sec)
28193 dw_line_info_table *table;
28195 if (sec == text_section)
28196 table = text_section_line_info;
28197 else if (sec == cold_text_section)
28199 table = cold_text_section_line_info;
28200 if (!table)
28202 cold_text_section_line_info = table = new_line_info_table ();
28203 table->end_label = cold_end_label;
28206 else
28208 const char *end_label;
28210 if (crtl->has_bb_partition)
28212 if (in_cold_section_p)
28213 end_label = crtl->subsections.cold_section_end_label;
28214 else
28215 end_label = crtl->subsections.hot_section_end_label;
28217 else
28219 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28220 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
28221 current_function_funcdef_no);
28222 end_label = ggc_strdup (label);
28225 table = new_line_info_table ();
28226 table->end_label = end_label;
28228 vec_safe_push (separate_line_info, table);
28231 if (output_asm_line_debug_info ())
28232 table->is_stmt = (cur_line_info_table
28233 ? cur_line_info_table->is_stmt
28234 : DWARF_LINE_DEFAULT_IS_STMT_START);
28235 cur_line_info_table = table;
28239 /* We need to reset the locations at the beginning of each
28240 function. We can't do this in the end_function hook, because the
28241 declarations that use the locations won't have been output when
28242 that hook is called. Also compute have_multiple_function_sections here. */
28244 static void
28245 dwarf2out_begin_function (tree fun)
28247 section *sec = function_section (fun);
28249 if (sec != text_section)
28250 have_multiple_function_sections = true;
28252 if (crtl->has_bb_partition && !cold_text_section)
28254 gcc_assert (current_function_decl == fun);
28255 cold_text_section = unlikely_text_section ();
28256 switch_to_section (cold_text_section);
28257 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
28258 switch_to_section (sec);
28261 call_site_count = 0;
28262 tail_call_site_count = 0;
28264 set_cur_line_info_table (sec);
28265 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
28268 /* Helper function of dwarf2out_end_function, called only after emitting
28269 the very first function into assembly. Check if some .debug_loc range
28270 might end with a .LVL* label that could be equal to .Ltext0.
28271 In that case we must force using absolute addresses in .debug_loc ranges,
28272 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
28273 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
28274 list terminator.
28275 Set have_multiple_function_sections to true in that case and
28276 terminate htab traversal. */
28279 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
28281 var_loc_list *entry = *slot;
28282 struct var_loc_node *node;
28284 node = entry->first;
28285 if (node && node->next && node->next->label)
28287 unsigned int i;
28288 const char *label = node->next->label;
28289 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
28291 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
28293 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
28294 if (strcmp (label, loclabel) == 0)
28296 have_multiple_function_sections = true;
28297 return 0;
28301 return 1;
28304 /* Hook called after emitting a function into assembly.
28305 This does something only for the very first function emitted. */
28307 static void
28308 dwarf2out_end_function (unsigned int)
28310 if (in_first_function_p
28311 && !have_multiple_function_sections
28312 && first_loclabel_num_not_at_text_label
28313 && decl_loc_table)
28314 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
28315 in_first_function_p = false;
28316 maybe_at_text_label_p = false;
28319 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
28320 front-ends register a translation unit even before dwarf2out_init is
28321 called. */
28322 static tree main_translation_unit = NULL_TREE;
28324 /* Hook called by front-ends after they built their main translation unit.
28325 Associate comp_unit_die to UNIT. */
28327 static void
28328 dwarf2out_register_main_translation_unit (tree unit)
28330 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
28331 && main_translation_unit == NULL_TREE);
28332 main_translation_unit = unit;
28333 /* If dwarf2out_init has not been called yet, it will perform the association
28334 itself looking at main_translation_unit. */
28335 if (decl_die_table != NULL)
28336 equate_decl_number_to_die (unit, comp_unit_die ());
28339 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
28341 static void
28342 push_dw_line_info_entry (dw_line_info_table *table,
28343 enum dw_line_info_opcode opcode, unsigned int val)
28345 dw_line_info_entry e;
28346 e.opcode = opcode;
28347 e.val = val;
28348 vec_safe_push (table->entries, e);
28351 /* Output a label to mark the beginning of a source code line entry
28352 and record information relating to this source line, in
28353 'line_info_table' for later output of the .debug_line section. */
28354 /* ??? The discriminator parameter ought to be unsigned. */
28356 static void
28357 dwarf2out_source_line (unsigned int line, unsigned int column,
28358 const char *filename,
28359 int discriminator, bool is_stmt)
28361 unsigned int file_num;
28362 dw_line_info_table *table;
28363 static var_loc_view lvugid;
28365 /* 'line_info_table' information gathering is not needed when the debug
28366 info level is set to the lowest value. Also, the current DWARF-based
28367 debug formats do not use this info. */
28368 if (debug_info_level < DINFO_LEVEL_TERSE || !dwarf_debuginfo_p ())
28369 return;
28371 table = cur_line_info_table;
28373 if (line == 0)
28375 if (debug_variable_location_views
28376 && output_asm_line_debug_info ()
28377 && table && !RESETTING_VIEW_P (table->view))
28379 /* If we're using the assembler to compute view numbers, we
28380 can't issue a .loc directive for line zero, so we can't
28381 get a view number at this point. We might attempt to
28382 compute it from the previous view, or equate it to a
28383 subsequent view (though it might not be there!), but
28384 since we're omitting the line number entry, we might as
28385 well omit the view number as well. That means pretending
28386 it's a view number zero, which might very well turn out
28387 to be correct. ??? Extend the assembler so that the
28388 compiler could emit e.g. ".locview .LVU#", to output a
28389 view without changing line number information. We'd then
28390 have to count it in symviews_since_reset; when it's omitted,
28391 it doesn't count. */
28392 if (!zero_view_p)
28393 zero_view_p = BITMAP_GGC_ALLOC ();
28394 bitmap_set_bit (zero_view_p, table->view);
28395 if (flag_debug_asm)
28397 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28398 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28399 fprintf (asm_out_file, "\t%s line 0, omitted view ",
28400 ASM_COMMENT_START);
28401 assemble_name (asm_out_file, label);
28402 putc ('\n', asm_out_file);
28404 table->view = ++lvugid;
28406 return;
28409 /* The discriminator column was added in dwarf4. Simplify the below
28410 by simply removing it if we're not supposed to output it. */
28411 if (dwarf_version < 4 && dwarf_strict)
28412 discriminator = 0;
28414 if (!debug_column_info)
28415 column = 0;
28417 file_num = maybe_emit_file (lookup_filename (filename));
28419 /* ??? TODO: Elide duplicate line number entries. Traditionally,
28420 the debugger has used the second (possibly duplicate) line number
28421 at the beginning of the function to mark the end of the prologue.
28422 We could eliminate any other duplicates within the function. For
28423 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
28424 that second line number entry. */
28425 /* Recall that this end-of-prologue indication is *not* the same thing
28426 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
28427 to which the hook corresponds, follows the last insn that was
28428 emitted by gen_prologue. What we need is to precede the first insn
28429 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
28430 insn that corresponds to something the user wrote. These may be
28431 very different locations once scheduling is enabled. */
28433 if (0 && file_num == table->file_num
28434 && line == table->line_num
28435 && column == table->column_num
28436 && discriminator == table->discrim_num
28437 && is_stmt == table->is_stmt)
28438 return;
28440 switch_to_section (current_function_section ());
28442 /* If requested, emit something human-readable. */
28443 if (flag_debug_asm)
28445 if (debug_column_info)
28446 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
28447 filename, line, column);
28448 else
28449 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
28450 filename, line);
28453 if (output_asm_line_debug_info ())
28455 /* Emit the .loc directive understood by GNU as. */
28456 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
28457 file_num, line, is_stmt, discriminator */
28458 fputs ("\t.loc ", asm_out_file);
28459 fprint_ul (asm_out_file, file_num);
28460 putc (' ', asm_out_file);
28461 fprint_ul (asm_out_file, line);
28462 putc (' ', asm_out_file);
28463 fprint_ul (asm_out_file, column);
28465 if (is_stmt != table->is_stmt)
28467 #if HAVE_GAS_LOC_STMT
28468 fputs (" is_stmt ", asm_out_file);
28469 putc (is_stmt ? '1' : '0', asm_out_file);
28470 #endif
28472 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
28474 gcc_assert (discriminator > 0);
28475 fputs (" discriminator ", asm_out_file);
28476 fprint_ul (asm_out_file, (unsigned long) discriminator);
28478 if (debug_variable_location_views)
28480 if (!RESETTING_VIEW_P (table->view))
28482 table->symviews_since_reset++;
28483 if (table->symviews_since_reset > symview_upper_bound)
28484 symview_upper_bound = table->symviews_since_reset;
28485 /* When we're using the assembler to compute view
28486 numbers, we output symbolic labels after "view" in
28487 .loc directives, and the assembler will set them for
28488 us, so that we can refer to the view numbers in
28489 location lists. The only exceptions are when we know
28490 a view will be zero: "-0" is a forced reset, used
28491 e.g. in the beginning of functions, whereas "0" tells
28492 the assembler to check that there was a PC change
28493 since the previous view, in a way that implicitly
28494 resets the next view. */
28495 fputs (" view ", asm_out_file);
28496 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28497 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
28498 assemble_name (asm_out_file, label);
28499 table->view = ++lvugid;
28501 else
28503 table->symviews_since_reset = 0;
28504 if (FORCE_RESETTING_VIEW_P (table->view))
28505 fputs (" view -0", asm_out_file);
28506 else
28507 fputs (" view 0", asm_out_file);
28508 /* Mark the present view as a zero view. Earlier debug
28509 binds may have already added its id to loclists to be
28510 emitted later, so we can't reuse the id for something
28511 else. However, it's good to know whether a view is
28512 known to be zero, because then we may be able to
28513 optimize out locviews that are all zeros, so take
28514 note of it in zero_view_p. */
28515 if (!zero_view_p)
28516 zero_view_p = BITMAP_GGC_ALLOC ();
28517 bitmap_set_bit (zero_view_p, lvugid);
28518 table->view = ++lvugid;
28521 putc ('\n', asm_out_file);
28523 else
28525 unsigned int label_num = ++line_info_label_num;
28527 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
28529 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
28530 push_dw_line_info_entry (table, LI_adv_address, label_num);
28531 else
28532 push_dw_line_info_entry (table, LI_set_address, label_num);
28533 if (debug_variable_location_views)
28535 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
28536 if (resetting)
28537 table->view = 0;
28539 if (flag_debug_asm)
28540 fprintf (asm_out_file, "\t%s view %s%d\n",
28541 ASM_COMMENT_START,
28542 resetting ? "-" : "",
28543 table->view);
28545 table->view++;
28547 if (file_num != table->file_num)
28548 push_dw_line_info_entry (table, LI_set_file, file_num);
28549 if (discriminator != table->discrim_num)
28550 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
28551 if (is_stmt != table->is_stmt)
28552 push_dw_line_info_entry (table, LI_negate_stmt, 0);
28553 push_dw_line_info_entry (table, LI_set_line, line);
28554 if (debug_column_info)
28555 push_dw_line_info_entry (table, LI_set_column, column);
28558 table->file_num = file_num;
28559 table->line_num = line;
28560 table->column_num = column;
28561 table->discrim_num = discriminator;
28562 table->is_stmt = is_stmt;
28563 table->in_use = true;
28566 /* Record a source file location for a DECL_IGNORED_P function. */
28568 static void
28569 dwarf2out_set_ignored_loc (unsigned int line, unsigned int column,
28570 const char *filename)
28572 dw_fde_ref fde = cfun->fde;
28574 fde->ignored_debug = false;
28575 set_cur_line_info_table (function_section (fde->decl));
28577 dwarf2out_source_line (line, column, filename, 0, true);
28580 /* Record the beginning of a new source file. */
28582 static void
28583 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
28585 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28587 macinfo_entry e;
28588 e.code = DW_MACINFO_start_file;
28589 e.lineno = lineno;
28590 e.info = ggc_strdup (filename);
28591 vec_safe_push (macinfo_table, e);
28595 /* Record the end of a source file. */
28597 static void
28598 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
28600 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28602 macinfo_entry e;
28603 e.code = DW_MACINFO_end_file;
28604 e.lineno = lineno;
28605 e.info = NULL;
28606 vec_safe_push (macinfo_table, e);
28610 /* Called from debug_define in toplev.c. The `buffer' parameter contains
28611 the tail part of the directive line, i.e. the part which is past the
28612 initial whitespace, #, whitespace, directive-name, whitespace part. */
28614 static void
28615 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
28616 const char *buffer ATTRIBUTE_UNUSED)
28618 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28620 macinfo_entry e;
28621 /* Insert a dummy first entry to be able to optimize the whole
28622 predefined macro block using DW_MACRO_import. */
28623 if (macinfo_table->is_empty () && lineno <= 1)
28625 e.code = 0;
28626 e.lineno = 0;
28627 e.info = NULL;
28628 vec_safe_push (macinfo_table, e);
28630 e.code = DW_MACINFO_define;
28631 e.lineno = lineno;
28632 e.info = ggc_strdup (buffer);
28633 vec_safe_push (macinfo_table, e);
28637 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
28638 the tail part of the directive line, i.e. the part which is past the
28639 initial whitespace, #, whitespace, directive-name, whitespace part. */
28641 static void
28642 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
28643 const char *buffer ATTRIBUTE_UNUSED)
28645 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28647 macinfo_entry e;
28648 /* Insert a dummy first entry to be able to optimize the whole
28649 predefined macro block using DW_MACRO_import. */
28650 if (macinfo_table->is_empty () && lineno <= 1)
28652 e.code = 0;
28653 e.lineno = 0;
28654 e.info = NULL;
28655 vec_safe_push (macinfo_table, e);
28657 e.code = DW_MACINFO_undef;
28658 e.lineno = lineno;
28659 e.info = ggc_strdup (buffer);
28660 vec_safe_push (macinfo_table, e);
28664 /* Helpers to manipulate hash table of CUs. */
28666 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28668 static inline hashval_t hash (const macinfo_entry *);
28669 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28672 inline hashval_t
28673 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28675 return htab_hash_string (entry->info);
28678 inline bool
28679 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28680 const macinfo_entry *entry2)
28682 return !strcmp (entry1->info, entry2->info);
28685 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28687 /* Output a single .debug_macinfo entry. */
28689 static void
28690 output_macinfo_op (macinfo_entry *ref)
28692 int file_num;
28693 size_t len;
28694 struct indirect_string_node *node;
28695 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28696 struct dwarf_file_data *fd;
28698 switch (ref->code)
28700 case DW_MACINFO_start_file:
28701 fd = lookup_filename (ref->info);
28702 file_num = maybe_emit_file (fd);
28703 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28704 dw2_asm_output_data_uleb128 (ref->lineno,
28705 "Included from line number %lu",
28706 (unsigned long) ref->lineno);
28707 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28708 break;
28709 case DW_MACINFO_end_file:
28710 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28711 break;
28712 case DW_MACINFO_define:
28713 case DW_MACINFO_undef:
28714 len = strlen (ref->info) + 1;
28715 if ((!dwarf_strict || dwarf_version >= 5)
28716 && len > (size_t) dwarf_offset_size
28717 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28718 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28720 if (dwarf_split_debug_info && dwarf_version >= 5)
28721 ref->code = ref->code == DW_MACINFO_define
28722 ? DW_MACRO_define_strx : DW_MACRO_undef_strx;
28723 else
28724 ref->code = ref->code == DW_MACINFO_define
28725 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28726 output_macinfo_op (ref);
28727 return;
28729 dw2_asm_output_data (1, ref->code,
28730 ref->code == DW_MACINFO_define
28731 ? "Define macro" : "Undefine macro");
28732 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28733 (unsigned long) ref->lineno);
28734 dw2_asm_output_nstring (ref->info, -1, "The macro");
28735 break;
28736 case DW_MACRO_define_strp:
28737 dw2_asm_output_data (1, ref->code, "Define macro strp");
28738 goto do_DW_MACRO_define_strpx;
28739 case DW_MACRO_undef_strp:
28740 dw2_asm_output_data (1, ref->code, "Undefine macro strp");
28741 goto do_DW_MACRO_define_strpx;
28742 case DW_MACRO_define_strx:
28743 dw2_asm_output_data (1, ref->code, "Define macro strx");
28744 goto do_DW_MACRO_define_strpx;
28745 case DW_MACRO_undef_strx:
28746 dw2_asm_output_data (1, ref->code, "Undefine macro strx");
28747 /* FALLTHRU */
28748 do_DW_MACRO_define_strpx:
28749 /* NB: dwarf2out_finish performs:
28750 1. save_macinfo_strings
28751 2. hash table traverse of index_string
28752 3. output_macinfo -> output_macinfo_op
28753 4. output_indirect_strings
28754 -> hash table traverse of output_index_string
28756 When output_macinfo_op is called, all index strings have been
28757 added to hash table by save_macinfo_strings and we can't pass
28758 INSERT to find_slot_with_hash which may expand hash table, even
28759 if no insertion is needed, and change hash table traverse order
28760 between index_string and output_index_string. */
28761 node = find_AT_string (ref->info, NO_INSERT);
28762 gcc_assert (node
28763 && (node->form == DW_FORM_strp
28764 || node->form == dwarf_FORM (DW_FORM_strx)));
28765 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28766 (unsigned long) ref->lineno);
28767 if (node->form == DW_FORM_strp)
28768 dw2_asm_output_offset (dwarf_offset_size, node->label,
28769 debug_str_section, "The macro: \"%s\"",
28770 ref->info);
28771 else
28772 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28773 ref->info);
28774 break;
28775 case DW_MACRO_import:
28776 dw2_asm_output_data (1, ref->code, "Import");
28777 ASM_GENERATE_INTERNAL_LABEL (label,
28778 DEBUG_MACRO_SECTION_LABEL,
28779 ref->lineno + macinfo_label_base);
28780 dw2_asm_output_offset (dwarf_offset_size, label, NULL, NULL);
28781 break;
28782 default:
28783 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28784 ASM_COMMENT_START, (unsigned long) ref->code);
28785 break;
28789 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28790 other compilation unit .debug_macinfo sections. IDX is the first
28791 index of a define/undef, return the number of ops that should be
28792 emitted in a comdat .debug_macinfo section and emit
28793 a DW_MACRO_import entry referencing it.
28794 If the define/undef entry should be emitted normally, return 0. */
28796 static unsigned
28797 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28798 macinfo_hash_type **macinfo_htab)
28800 macinfo_entry *first, *second, *cur, *inc;
28801 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28802 unsigned char checksum[16];
28803 struct md5_ctx ctx;
28804 char *grp_name, *tail;
28805 const char *base;
28806 unsigned int i, count, encoded_filename_len, linebuf_len;
28807 macinfo_entry **slot;
28809 first = &(*macinfo_table)[idx];
28810 second = &(*macinfo_table)[idx + 1];
28812 /* Optimize only if there are at least two consecutive define/undef ops,
28813 and either all of them are before first DW_MACINFO_start_file
28814 with lineno {0,1} (i.e. predefined macro block), or all of them are
28815 in some included header file. */
28816 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28817 return 0;
28818 if (vec_safe_is_empty (files))
28820 if (first->lineno > 1 || second->lineno > 1)
28821 return 0;
28823 else if (first->lineno == 0)
28824 return 0;
28826 /* Find the last define/undef entry that can be grouped together
28827 with first and at the same time compute md5 checksum of their
28828 codes, linenumbers and strings. */
28829 md5_init_ctx (&ctx);
28830 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28831 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28832 break;
28833 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28834 break;
28835 else
28837 unsigned char code = cur->code;
28838 md5_process_bytes (&code, 1, &ctx);
28839 checksum_uleb128 (cur->lineno, &ctx);
28840 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28842 md5_finish_ctx (&ctx, checksum);
28843 count = i - idx;
28845 /* From the containing include filename (if any) pick up just
28846 usable characters from its basename. */
28847 if (vec_safe_is_empty (files))
28848 base = "";
28849 else
28850 base = lbasename (files->last ().info);
28851 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28852 if (ISIDNUM (base[i]) || base[i] == '.')
28853 encoded_filename_len++;
28854 /* Count . at the end. */
28855 if (encoded_filename_len)
28856 encoded_filename_len++;
28858 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28859 linebuf_len = strlen (linebuf);
28861 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28862 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28863 + 16 * 2 + 1);
28864 memcpy (grp_name, dwarf_offset_size == 4 ? "wm4." : "wm8.", 4);
28865 tail = grp_name + 4;
28866 if (encoded_filename_len)
28868 for (i = 0; base[i]; i++)
28869 if (ISIDNUM (base[i]) || base[i] == '.')
28870 *tail++ = base[i];
28871 *tail++ = '.';
28873 memcpy (tail, linebuf, linebuf_len);
28874 tail += linebuf_len;
28875 *tail++ = '.';
28876 for (i = 0; i < 16; i++)
28877 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28879 /* Construct a macinfo_entry for DW_MACRO_import
28880 in the empty vector entry before the first define/undef. */
28881 inc = &(*macinfo_table)[idx - 1];
28882 inc->code = DW_MACRO_import;
28883 inc->lineno = 0;
28884 inc->info = ggc_strdup (grp_name);
28885 if (!*macinfo_htab)
28886 *macinfo_htab = new macinfo_hash_type (10);
28887 /* Avoid emitting duplicates. */
28888 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28889 if (*slot != NULL)
28891 inc->code = 0;
28892 inc->info = NULL;
28893 /* If such an entry has been used before, just emit
28894 a DW_MACRO_import op. */
28895 inc = *slot;
28896 output_macinfo_op (inc);
28897 /* And clear all macinfo_entry in the range to avoid emitting them
28898 in the second pass. */
28899 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28901 cur->code = 0;
28902 cur->info = NULL;
28905 else
28907 *slot = inc;
28908 inc->lineno = (*macinfo_htab)->elements ();
28909 output_macinfo_op (inc);
28911 return count;
28914 /* Save any strings needed by the macinfo table in the debug str
28915 table. All strings must be collected into the table by the time
28916 index_string is called. */
28918 static void
28919 save_macinfo_strings (void)
28921 unsigned len;
28922 unsigned i;
28923 macinfo_entry *ref;
28925 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28927 switch (ref->code)
28929 /* Match the logic in output_macinfo_op to decide on
28930 indirect strings. */
28931 case DW_MACINFO_define:
28932 case DW_MACINFO_undef:
28933 len = strlen (ref->info) + 1;
28934 if ((!dwarf_strict || dwarf_version >= 5)
28935 && len > (unsigned) dwarf_offset_size
28936 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28937 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28938 set_indirect_string (find_AT_string (ref->info));
28939 break;
28940 case DW_MACINFO_start_file:
28941 /* -gsplit-dwarf -g3 will also output filename as indirect
28942 string. */
28943 if (!dwarf_split_debug_info)
28944 break;
28945 /* Fall through. */
28946 case DW_MACRO_define_strp:
28947 case DW_MACRO_undef_strp:
28948 case DW_MACRO_define_strx:
28949 case DW_MACRO_undef_strx:
28950 set_indirect_string (find_AT_string (ref->info));
28951 break;
28952 default:
28953 break;
28958 /* Output macinfo section(s). */
28960 static void
28961 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28963 unsigned i;
28964 unsigned long length = vec_safe_length (macinfo_table);
28965 macinfo_entry *ref;
28966 vec<macinfo_entry, va_gc> *files = NULL;
28967 macinfo_hash_type *macinfo_htab = NULL;
28968 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28970 if (! length)
28971 return;
28973 /* output_macinfo* uses these interchangeably. */
28974 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28975 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28976 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28977 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28979 /* AIX Assembler inserts the length, so adjust the reference to match the
28980 offset expected by debuggers. */
28981 strcpy (dl_section_ref, debug_line_label);
28982 if (XCOFF_DEBUGGING_INFO)
28983 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28985 /* For .debug_macro emit the section header. */
28986 if (!dwarf_strict || dwarf_version >= 5)
28988 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28989 "DWARF macro version number");
28990 if (dwarf_offset_size == 8)
28991 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28992 else
28993 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28994 dw2_asm_output_offset (dwarf_offset_size, debug_line_label,
28995 debug_line_section, NULL);
28998 /* In the first loop, it emits the primary .debug_macinfo section
28999 and after each emitted op the macinfo_entry is cleared.
29000 If a longer range of define/undef ops can be optimized using
29001 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
29002 the vector before the first define/undef in the range and the
29003 whole range of define/undef ops is not emitted and kept. */
29004 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29006 switch (ref->code)
29008 case DW_MACINFO_start_file:
29009 vec_safe_push (files, *ref);
29010 break;
29011 case DW_MACINFO_end_file:
29012 if (!vec_safe_is_empty (files))
29013 files->pop ();
29014 break;
29015 case DW_MACINFO_define:
29016 case DW_MACINFO_undef:
29017 if ((!dwarf_strict || dwarf_version >= 5)
29018 && HAVE_COMDAT_GROUP
29019 && vec_safe_length (files) != 1
29020 && i > 0
29021 && i + 1 < length
29022 && (*macinfo_table)[i - 1].code == 0)
29024 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
29025 if (count)
29027 i += count - 1;
29028 continue;
29031 break;
29032 case 0:
29033 /* A dummy entry may be inserted at the beginning to be able
29034 to optimize the whole block of predefined macros. */
29035 if (i == 0)
29036 continue;
29037 default:
29038 break;
29040 output_macinfo_op (ref);
29041 ref->info = NULL;
29042 ref->code = 0;
29045 if (!macinfo_htab)
29046 return;
29048 /* Save the number of transparent includes so we can adjust the
29049 label number for the fat LTO object DWARF. */
29050 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
29052 delete macinfo_htab;
29053 macinfo_htab = NULL;
29055 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
29056 terminate the current chain and switch to a new comdat .debug_macinfo
29057 section and emit the define/undef entries within it. */
29058 for (i = 0; macinfo_table->iterate (i, &ref); i++)
29059 switch (ref->code)
29061 case 0:
29062 continue;
29063 case DW_MACRO_import:
29065 char label[MAX_ARTIFICIAL_LABEL_BYTES];
29066 tree comdat_key = get_identifier (ref->info);
29067 /* Terminate the previous .debug_macinfo section. */
29068 dw2_asm_output_data (1, 0, "End compilation unit");
29069 targetm.asm_out.named_section (debug_macinfo_section_name,
29070 SECTION_DEBUG
29071 | SECTION_LINKONCE
29072 | (early_lto_debug
29073 ? SECTION_EXCLUDE : 0),
29074 comdat_key);
29075 ASM_GENERATE_INTERNAL_LABEL (label,
29076 DEBUG_MACRO_SECTION_LABEL,
29077 ref->lineno + macinfo_label_base);
29078 ASM_OUTPUT_LABEL (asm_out_file, label);
29079 ref->code = 0;
29080 ref->info = NULL;
29081 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
29082 "DWARF macro version number");
29083 if (dwarf_offset_size == 8)
29084 dw2_asm_output_data (1, 1, "Flags: 64-bit");
29085 else
29086 dw2_asm_output_data (1, 0, "Flags: 32-bit");
29088 break;
29089 case DW_MACINFO_define:
29090 case DW_MACINFO_undef:
29091 output_macinfo_op (ref);
29092 ref->code = 0;
29093 ref->info = NULL;
29094 break;
29095 default:
29096 gcc_unreachable ();
29099 macinfo_label_base += macinfo_label_base_adj;
29102 /* As init_sections_and_labels may get called multiple times, have a
29103 generation count for labels. */
29104 static unsigned init_sections_and_labels_generation;
29106 /* Initialize the various sections and labels for dwarf output and prefix
29107 them with PREFIX if non-NULL. Returns the generation (zero based
29108 number of times function was called). */
29110 static unsigned
29111 init_sections_and_labels (bool early_lto_debug)
29113 if (early_lto_debug)
29115 if (!dwarf_split_debug_info)
29117 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29118 SECTION_DEBUG | SECTION_EXCLUDE,
29119 NULL);
29120 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
29121 SECTION_DEBUG | SECTION_EXCLUDE,
29122 NULL);
29123 debug_macinfo_section_name
29124 = ((dwarf_strict && dwarf_version < 5)
29125 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
29126 debug_macinfo_section = get_section (debug_macinfo_section_name,
29127 SECTION_DEBUG
29128 | SECTION_EXCLUDE, NULL);
29130 else
29132 /* ??? Which of the following do we need early? */
29133 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
29134 SECTION_DEBUG | SECTION_EXCLUDE,
29135 NULL);
29136 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
29137 SECTION_DEBUG | SECTION_EXCLUDE,
29138 NULL);
29139 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
29140 SECTION_DEBUG
29141 | SECTION_EXCLUDE, NULL);
29142 debug_skeleton_abbrev_section
29143 = get_section (DEBUG_LTO_ABBREV_SECTION,
29144 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29145 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29146 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29147 init_sections_and_labels_generation);
29149 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29150 stay in the main .o, but the skeleton_line goes into the split
29151 off dwo. */
29152 debug_skeleton_line_section
29153 = get_section (DEBUG_LTO_LINE_SECTION,
29154 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29155 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29156 DEBUG_SKELETON_LINE_SECTION_LABEL,
29157 init_sections_and_labels_generation);
29158 debug_str_offsets_section
29159 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
29160 SECTION_DEBUG | SECTION_EXCLUDE,
29161 NULL);
29162 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29163 DEBUG_SKELETON_INFO_SECTION_LABEL,
29164 init_sections_and_labels_generation);
29165 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
29166 DEBUG_STR_DWO_SECTION_FLAGS,
29167 NULL);
29168 debug_macinfo_section_name
29169 = ((dwarf_strict && dwarf_version < 5)
29170 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
29171 debug_macinfo_section = get_section (debug_macinfo_section_name,
29172 SECTION_DEBUG | SECTION_EXCLUDE,
29173 NULL);
29175 /* For macro info and the file table we have to refer to a
29176 debug_line section. */
29177 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
29178 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29179 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29180 DEBUG_LINE_SECTION_LABEL,
29181 init_sections_and_labels_generation);
29183 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
29184 DEBUG_STR_SECTION_FLAGS
29185 | SECTION_EXCLUDE, NULL);
29186 if (!dwarf_split_debug_info)
29187 debug_line_str_section
29188 = get_section (DEBUG_LTO_LINE_STR_SECTION,
29189 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
29191 else
29193 if (!dwarf_split_debug_info)
29195 debug_info_section = get_section (DEBUG_INFO_SECTION,
29196 SECTION_DEBUG, NULL);
29197 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29198 SECTION_DEBUG, NULL);
29199 debug_loc_section = get_section (dwarf_version >= 5
29200 ? DEBUG_LOCLISTS_SECTION
29201 : DEBUG_LOC_SECTION,
29202 SECTION_DEBUG, NULL);
29203 debug_macinfo_section_name
29204 = ((dwarf_strict && dwarf_version < 5)
29205 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
29206 debug_macinfo_section = get_section (debug_macinfo_section_name,
29207 SECTION_DEBUG, NULL);
29209 else
29211 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
29212 SECTION_DEBUG | SECTION_EXCLUDE,
29213 NULL);
29214 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
29215 SECTION_DEBUG | SECTION_EXCLUDE,
29216 NULL);
29217 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
29218 SECTION_DEBUG, NULL);
29219 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
29220 SECTION_DEBUG, NULL);
29221 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
29222 SECTION_DEBUG, NULL);
29223 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
29224 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
29225 init_sections_and_labels_generation);
29227 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
29228 stay in the main .o, but the skeleton_line goes into the
29229 split off dwo. */
29230 debug_skeleton_line_section
29231 = get_section (DEBUG_DWO_LINE_SECTION,
29232 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29233 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
29234 DEBUG_SKELETON_LINE_SECTION_LABEL,
29235 init_sections_and_labels_generation);
29236 debug_str_offsets_section
29237 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
29238 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29239 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
29240 DEBUG_SKELETON_INFO_SECTION_LABEL,
29241 init_sections_and_labels_generation);
29242 debug_loc_section = get_section (dwarf_version >= 5
29243 ? DEBUG_DWO_LOCLISTS_SECTION
29244 : DEBUG_DWO_LOC_SECTION,
29245 SECTION_DEBUG | SECTION_EXCLUDE,
29246 NULL);
29247 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
29248 DEBUG_STR_DWO_SECTION_FLAGS,
29249 NULL);
29250 debug_macinfo_section_name
29251 = ((dwarf_strict && dwarf_version < 5)
29252 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
29253 debug_macinfo_section = get_section (debug_macinfo_section_name,
29254 SECTION_DEBUG | SECTION_EXCLUDE,
29255 NULL);
29256 if (dwarf_version >= 5)
29257 debug_ranges_dwo_section
29258 = get_section (DEBUG_DWO_RNGLISTS_SECTION,
29259 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
29261 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
29262 SECTION_DEBUG, NULL);
29263 debug_line_section = get_section (DEBUG_LINE_SECTION,
29264 SECTION_DEBUG, NULL);
29265 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
29266 SECTION_DEBUG, NULL);
29267 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
29268 SECTION_DEBUG, NULL);
29269 debug_str_section = get_section (DEBUG_STR_SECTION,
29270 DEBUG_STR_SECTION_FLAGS, NULL);
29271 if ((!dwarf_split_debug_info && !output_asm_line_debug_info ())
29272 || asm_outputs_debug_line_str ())
29273 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
29274 DEBUG_STR_SECTION_FLAGS, NULL);
29276 debug_ranges_section = get_section (dwarf_version >= 5
29277 ? DEBUG_RNGLISTS_SECTION
29278 : DEBUG_RANGES_SECTION,
29279 SECTION_DEBUG, NULL);
29280 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
29281 SECTION_DEBUG, NULL);
29284 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
29285 DEBUG_ABBREV_SECTION_LABEL,
29286 init_sections_and_labels_generation);
29287 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
29288 DEBUG_INFO_SECTION_LABEL,
29289 init_sections_and_labels_generation);
29290 info_section_emitted = false;
29291 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
29292 DEBUG_LINE_SECTION_LABEL,
29293 init_sections_and_labels_generation);
29294 /* There are up to 6 unique ranges labels per generation.
29295 See also output_rnglists. */
29296 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
29297 DEBUG_RANGES_SECTION_LABEL,
29298 init_sections_and_labels_generation * 6);
29299 if (dwarf_version >= 5 && dwarf_split_debug_info)
29300 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
29301 DEBUG_RANGES_SECTION_LABEL,
29302 1 + init_sections_and_labels_generation * 6);
29303 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
29304 DEBUG_ADDR_SECTION_LABEL,
29305 init_sections_and_labels_generation);
29306 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
29307 (dwarf_strict && dwarf_version < 5)
29308 ? DEBUG_MACINFO_SECTION_LABEL
29309 : DEBUG_MACRO_SECTION_LABEL,
29310 init_sections_and_labels_generation);
29311 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
29312 init_sections_and_labels_generation);
29314 ++init_sections_and_labels_generation;
29315 return init_sections_and_labels_generation - 1;
29318 /* Set up for Dwarf output at the start of compilation. */
29320 static void
29321 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
29323 /* Allocate the file_table. */
29324 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
29326 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29327 /* Allocate the decl_die_table. */
29328 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
29330 /* Allocate the decl_loc_table. */
29331 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
29333 /* Allocate the cached_dw_loc_list_table. */
29334 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
29336 /* Allocate the initial hunk of the abbrev_die_table. */
29337 vec_alloc (abbrev_die_table, 256);
29338 /* Zero-th entry is allocated, but unused. */
29339 abbrev_die_table->quick_push (NULL);
29341 /* Allocate the dwarf_proc_stack_usage_map. */
29342 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
29344 /* Allocate the pubtypes and pubnames vectors. */
29345 vec_alloc (pubname_table, 32);
29346 vec_alloc (pubtype_table, 32);
29348 vec_alloc (incomplete_types, 64);
29350 vec_alloc (used_rtx_array, 32);
29352 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
29353 vec_alloc (macinfo_table, 64);
29354 #endif
29356 /* If front-ends already registered a main translation unit but we were not
29357 ready to perform the association, do this now. */
29358 if (main_translation_unit != NULL_TREE)
29359 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
29362 /* Called before compile () starts outputtting functions, variables
29363 and toplevel asms into assembly. */
29365 static void
29366 dwarf2out_assembly_start (void)
29368 if (text_section_line_info)
29369 return;
29371 #ifndef DWARF2_LINENO_DEBUGGING_INFO
29372 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
29373 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
29374 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
29375 COLD_TEXT_SECTION_LABEL, 0);
29376 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
29378 switch_to_section (text_section);
29379 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
29380 #endif
29382 /* Make sure the line number table for .text always exists. */
29383 text_section_line_info = new_line_info_table ();
29384 text_section_line_info->end_label = text_end_label;
29386 #ifdef DWARF2_LINENO_DEBUGGING_INFO
29387 cur_line_info_table = text_section_line_info;
29388 #endif
29390 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
29391 && dwarf2out_do_cfi_asm ()
29392 && !dwarf2out_do_eh_frame ())
29393 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
29395 #if defined(HAVE_AS_GDWARF_5_DEBUG_FLAG) && defined(HAVE_AS_WORKING_DWARF_N_FLAG)
29396 if (output_asm_line_debug_info () && dwarf_version >= 5)
29398 /* When gas outputs DWARF5 .debug_line[_str] then we have to
29399 tell it the comp_dir and main file name for the zero entry
29400 line table. */
29401 const char *comp_dir, *filename0;
29403 comp_dir = comp_dir_string ();
29404 if (comp_dir == NULL)
29405 comp_dir = "";
29407 filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29408 if (filename0 == NULL)
29409 filename0 = "";
29411 fprintf (asm_out_file, "\t.file 0 ");
29412 output_quoted_string (asm_out_file, remap_debug_filename (comp_dir));
29413 fputc (' ', asm_out_file);
29414 output_quoted_string (asm_out_file, remap_debug_filename (filename0));
29415 fputc ('\n', asm_out_file);
29417 else
29418 #endif
29419 /* Work around for PR101575: output a dummy .file directive. */
29420 if (!last_emitted_file && dwarf_debuginfo_p ()
29421 && debug_info_level >= DINFO_LEVEL_TERSE)
29423 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
29425 if (filename0 == NULL)
29426 filename0 = "<dummy>";
29427 maybe_emit_file (lookup_filename (filename0));
29431 /* A helper function for dwarf2out_finish called through
29432 htab_traverse. Assign a string its index. All strings must be
29433 collected into the table by the time index_string is called,
29434 because the indexing code relies on htab_traverse to traverse nodes
29435 in the same order for each run. */
29438 index_string (indirect_string_node **h, unsigned int *index)
29440 indirect_string_node *node = *h;
29442 find_string_form (node);
29443 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29445 gcc_assert (node->index == NO_INDEX_ASSIGNED);
29446 node->index = *index;
29447 *index += 1;
29449 return 1;
29452 /* A helper function for output_indirect_strings called through
29453 htab_traverse. Output the offset to a string and update the
29454 current offset. */
29457 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
29459 indirect_string_node *node = *h;
29461 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29463 /* Assert that this node has been assigned an index. */
29464 gcc_assert (node->index != NO_INDEX_ASSIGNED
29465 && node->index != NOT_INDEXED);
29466 dw2_asm_output_data (dwarf_offset_size, *offset,
29467 "indexed string 0x%x: %s", node->index, node->str);
29468 *offset += strlen (node->str) + 1;
29470 return 1;
29473 /* A helper function for dwarf2out_finish called through
29474 htab_traverse. Output the indexed string. */
29477 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
29479 struct indirect_string_node *node = *h;
29481 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29483 /* Assert that the strings are output in the same order as their
29484 indexes were assigned. */
29485 gcc_assert (*cur_idx == node->index);
29486 assemble_string (node->str, strlen (node->str) + 1);
29487 *cur_idx += 1;
29489 return 1;
29492 /* A helper function for output_indirect_strings. Counts the number
29493 of index strings offsets. Must match the logic of the functions
29494 output_index_string[_offsets] above. */
29496 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
29498 struct indirect_string_node *node = *h;
29500 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
29501 *last_idx += 1;
29502 return 1;
29505 /* A helper function for dwarf2out_finish called through
29506 htab_traverse. Emit one queued .debug_str string. */
29509 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
29511 struct indirect_string_node *node = *h;
29513 node->form = find_string_form (node);
29514 if (node->form == form && node->refcount > 0)
29516 ASM_OUTPUT_LABEL (asm_out_file, node->label);
29517 assemble_string (node->str, strlen (node->str) + 1);
29520 return 1;
29523 /* Output the indexed string table. */
29525 static void
29526 output_indirect_strings (void)
29528 switch_to_section (debug_str_section);
29529 if (!dwarf_split_debug_info)
29530 debug_str_hash->traverse<enum dwarf_form,
29531 output_indirect_string> (DW_FORM_strp);
29532 else
29534 unsigned int offset = 0;
29535 unsigned int cur_idx = 0;
29537 if (skeleton_debug_str_hash)
29538 skeleton_debug_str_hash->traverse<enum dwarf_form,
29539 output_indirect_string> (DW_FORM_strp);
29541 switch_to_section (debug_str_offsets_section);
29542 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
29543 header. Note that we don't need to generate a label to the
29544 actual index table following the header here, because this is
29545 for the split dwarf case only. In an .dwo file there is only
29546 one string offsets table (and one debug info section). But
29547 if we would start using string offset tables for the main (or
29548 skeleton) unit, then we have to add a DW_AT_str_offsets_base
29549 pointing to the actual index after the header. Split dwarf
29550 units will never have a string offsets base attribute. When
29551 a split unit is moved into a .dwp file the string offsets can
29552 be found through the .debug_cu_index section table. */
29553 if (dwarf_version >= 5)
29555 unsigned int last_idx = 0;
29556 unsigned long str_offsets_length;
29558 debug_str_hash->traverse_noresize
29559 <unsigned int *, count_index_strings> (&last_idx);
29560 str_offsets_length = last_idx * dwarf_offset_size + 4;
29561 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29562 dw2_asm_output_data (4, 0xffffffff,
29563 "Escape value for 64-bit DWARF extension");
29564 dw2_asm_output_data (dwarf_offset_size, str_offsets_length,
29565 "Length of string offsets unit");
29566 dw2_asm_output_data (2, 5, "DWARF string offsets version");
29567 dw2_asm_output_data (2, 0, "Header zero padding");
29569 debug_str_hash->traverse_noresize
29570 <unsigned int *, output_index_string_offset> (&offset);
29571 switch_to_section (debug_str_dwo_section);
29572 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
29573 (&cur_idx);
29577 /* Callback for htab_traverse to assign an index to an entry in the
29578 table, and to write that entry to the .debug_addr section. */
29581 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
29583 addr_table_entry *entry = *slot;
29585 if (entry->refcount == 0)
29587 gcc_assert (entry->index == NO_INDEX_ASSIGNED
29588 || entry->index == NOT_INDEXED);
29589 return 1;
29592 gcc_assert (entry->index == *cur_index);
29593 (*cur_index)++;
29595 switch (entry->kind)
29597 case ate_kind_rtx:
29598 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
29599 "0x%x", entry->index);
29600 break;
29601 case ate_kind_rtx_dtprel:
29602 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
29603 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
29604 DWARF2_ADDR_SIZE,
29605 entry->addr.rtl);
29606 fputc ('\n', asm_out_file);
29607 break;
29608 case ate_kind_label:
29609 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
29610 "0x%x", entry->index);
29611 break;
29612 default:
29613 gcc_unreachable ();
29615 return 1;
29618 /* A helper function for dwarf2out_finish. Counts the number
29619 of indexed addresses. Must match the logic of the functions
29620 output_addr_table_entry above. */
29622 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
29624 addr_table_entry *entry = *slot;
29626 if (entry->refcount > 0)
29627 *last_idx += 1;
29628 return 1;
29631 /* Produce the .debug_addr section. */
29633 static void
29634 output_addr_table (void)
29636 unsigned int index = 0;
29637 if (addr_index_table == NULL || addr_index_table->size () == 0)
29638 return;
29640 switch_to_section (debug_addr_section);
29641 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
29642 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
29643 before DWARF5, didn't have a header for .debug_addr units.
29644 DWARF5 specifies a small header when address tables are used. */
29645 if (dwarf_version >= 5)
29647 unsigned int last_idx = 0;
29648 unsigned long addrs_length;
29650 addr_index_table->traverse_noresize
29651 <unsigned int *, count_index_addrs> (&last_idx);
29652 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
29654 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
29655 dw2_asm_output_data (4, 0xffffffff,
29656 "Escape value for 64-bit DWARF extension");
29657 dw2_asm_output_data (dwarf_offset_size, addrs_length,
29658 "Length of Address Unit");
29659 dw2_asm_output_data (2, 5, "DWARF addr version");
29660 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
29661 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
29663 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
29665 addr_index_table
29666 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
29669 #if ENABLE_ASSERT_CHECKING
29670 /* Verify that all marks are clear. */
29672 static void
29673 verify_marks_clear (dw_die_ref die)
29675 dw_die_ref c;
29677 gcc_assert (! die->die_mark);
29678 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
29680 #endif /* ENABLE_ASSERT_CHECKING */
29682 /* Clear the marks for a die and its children.
29683 Be cool if the mark isn't set. */
29685 static void
29686 prune_unmark_dies (dw_die_ref die)
29688 dw_die_ref c;
29690 if (die->die_mark)
29691 die->die_mark = 0;
29692 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
29695 /* Given LOC that is referenced by a DIE we're marking as used, find all
29696 referenced DWARF procedures it references and mark them as used. */
29698 static void
29699 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
29701 for (; loc != NULL; loc = loc->dw_loc_next)
29702 switch (loc->dw_loc_opc)
29704 case DW_OP_implicit_pointer:
29705 case DW_OP_convert:
29706 case DW_OP_reinterpret:
29707 case DW_OP_GNU_implicit_pointer:
29708 case DW_OP_GNU_convert:
29709 case DW_OP_GNU_reinterpret:
29710 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
29711 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29712 break;
29713 case DW_OP_GNU_variable_value:
29714 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29716 dw_die_ref ref
29717 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29718 if (ref == NULL)
29719 break;
29720 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29721 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29722 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29724 /* FALLTHRU */
29725 case DW_OP_call2:
29726 case DW_OP_call4:
29727 case DW_OP_call_ref:
29728 case DW_OP_const_type:
29729 case DW_OP_GNU_const_type:
29730 case DW_OP_GNU_parameter_ref:
29731 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
29732 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
29733 break;
29734 case DW_OP_regval_type:
29735 case DW_OP_deref_type:
29736 case DW_OP_GNU_regval_type:
29737 case DW_OP_GNU_deref_type:
29738 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
29739 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
29740 break;
29741 case DW_OP_entry_value:
29742 case DW_OP_GNU_entry_value:
29743 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
29744 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
29745 break;
29746 default:
29747 break;
29751 /* Given DIE that we're marking as used, find any other dies
29752 it references as attributes and mark them as used. */
29754 static void
29755 prune_unused_types_walk_attribs (dw_die_ref die)
29757 dw_attr_node *a;
29758 unsigned ix;
29760 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29762 switch (AT_class (a))
29764 /* Make sure DWARF procedures referenced by location descriptions will
29765 get emitted. */
29766 case dw_val_class_loc:
29767 prune_unused_types_walk_loc_descr (AT_loc (a));
29768 break;
29769 case dw_val_class_loc_list:
29770 for (dw_loc_list_ref list = AT_loc_list (a);
29771 list != NULL;
29772 list = list->dw_loc_next)
29773 prune_unused_types_walk_loc_descr (list->expr);
29774 break;
29776 case dw_val_class_view_list:
29777 /* This points to a loc_list in another attribute, so it's
29778 already covered. */
29779 break;
29781 case dw_val_class_die_ref:
29782 /* A reference to another DIE.
29783 Make sure that it will get emitted.
29784 If it was broken out into a comdat group, don't follow it. */
29785 if (! AT_ref (a)->comdat_type_p
29786 || a->dw_attr == DW_AT_specification)
29787 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29788 break;
29790 case dw_val_class_str:
29791 /* Set the string's refcount to 0 so that prune_unused_types_mark
29792 accounts properly for it. */
29793 a->dw_attr_val.v.val_str->refcount = 0;
29794 break;
29796 default:
29797 break;
29802 /* Mark the generic parameters and arguments children DIEs of DIE. */
29804 static void
29805 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29807 dw_die_ref c;
29809 if (die == NULL || die->die_child == NULL)
29810 return;
29811 c = die->die_child;
29814 if (is_template_parameter (c))
29815 prune_unused_types_mark (c, 1);
29816 c = c->die_sib;
29817 } while (c && c != die->die_child);
29820 /* Mark DIE as being used. If DOKIDS is true, then walk down
29821 to DIE's children. */
29823 static void
29824 prune_unused_types_mark (dw_die_ref die, int dokids)
29826 dw_die_ref c;
29828 if (die->die_mark == 0)
29830 /* We haven't done this node yet. Mark it as used. */
29831 die->die_mark = 1;
29832 /* If this is the DIE of a generic type instantiation,
29833 mark the children DIEs that describe its generic parms and
29834 args. */
29835 prune_unused_types_mark_generic_parms_dies (die);
29837 /* We also have to mark its parents as used.
29838 (But we don't want to mark our parent's kids due to this,
29839 unless it is a class.) */
29840 if (die->die_parent)
29841 prune_unused_types_mark (die->die_parent,
29842 class_scope_p (die->die_parent));
29844 /* Mark any referenced nodes. */
29845 prune_unused_types_walk_attribs (die);
29847 /* If this node is a specification,
29848 also mark the definition, if it exists. */
29849 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29850 prune_unused_types_mark (die->die_definition, 1);
29853 if (dokids && die->die_mark != 2)
29855 /* We need to walk the children, but haven't done so yet.
29856 Remember that we've walked the kids. */
29857 die->die_mark = 2;
29859 /* If this is an array type, we need to make sure our
29860 kids get marked, even if they're types. If we're
29861 breaking out types into comdat sections, do this
29862 for all type definitions. */
29863 if (die->die_tag == DW_TAG_array_type
29864 || (use_debug_types
29865 && is_type_die (die) && ! is_declaration_die (die)))
29866 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29867 else
29868 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29872 /* For local classes, look if any static member functions were emitted
29873 and if so, mark them. */
29875 static void
29876 prune_unused_types_walk_local_classes (dw_die_ref die)
29878 dw_die_ref c;
29880 if (die->die_mark == 2)
29881 return;
29883 switch (die->die_tag)
29885 case DW_TAG_structure_type:
29886 case DW_TAG_union_type:
29887 case DW_TAG_class_type:
29888 case DW_TAG_interface_type:
29889 break;
29891 case DW_TAG_subprogram:
29892 if (!get_AT_flag (die, DW_AT_declaration)
29893 || die->die_definition != NULL)
29894 prune_unused_types_mark (die, 1);
29895 return;
29897 default:
29898 return;
29901 /* Mark children. */
29902 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29905 /* Walk the tree DIE and mark types that we actually use. */
29907 static void
29908 prune_unused_types_walk (dw_die_ref die)
29910 dw_die_ref c;
29912 /* Don't do anything if this node is already marked and
29913 children have been marked as well. */
29914 if (die->die_mark == 2)
29915 return;
29917 switch (die->die_tag)
29919 case DW_TAG_structure_type:
29920 case DW_TAG_union_type:
29921 case DW_TAG_class_type:
29922 case DW_TAG_interface_type:
29923 if (die->die_perennial_p)
29924 break;
29926 for (c = die->die_parent; c; c = c->die_parent)
29927 if (c->die_tag == DW_TAG_subprogram)
29928 break;
29930 /* Finding used static member functions inside of classes
29931 is needed just for local classes, because for other classes
29932 static member function DIEs with DW_AT_specification
29933 are emitted outside of the DW_TAG_*_type. If we ever change
29934 it, we'd need to call this even for non-local classes. */
29935 if (c)
29936 prune_unused_types_walk_local_classes (die);
29938 /* It's a type node --- don't mark it. */
29939 return;
29941 case DW_TAG_const_type:
29942 case DW_TAG_packed_type:
29943 case DW_TAG_pointer_type:
29944 case DW_TAG_reference_type:
29945 case DW_TAG_rvalue_reference_type:
29946 case DW_TAG_volatile_type:
29947 case DW_TAG_typedef:
29948 case DW_TAG_array_type:
29949 case DW_TAG_friend:
29950 case DW_TAG_enumeration_type:
29951 case DW_TAG_subroutine_type:
29952 case DW_TAG_string_type:
29953 case DW_TAG_set_type:
29954 case DW_TAG_subrange_type:
29955 case DW_TAG_ptr_to_member_type:
29956 case DW_TAG_file_type:
29957 /* Type nodes are useful only when other DIEs reference them --- don't
29958 mark them. */
29959 /* FALLTHROUGH */
29961 case DW_TAG_dwarf_procedure:
29962 /* Likewise for DWARF procedures. */
29964 if (die->die_perennial_p)
29965 break;
29967 return;
29969 case DW_TAG_variable:
29970 if (flag_debug_only_used_symbols)
29972 if (die->die_perennial_p)
29973 break;
29975 /* For static data members, the declaration in the class is supposed
29976 to have DW_TAG_member tag in DWARF{3,4} but DW_TAG_variable in
29977 DWARF5. DW_TAG_member will be marked, so mark even such
29978 DW_TAG_variables in DWARF5, as long as it has DW_AT_const_value
29979 attribute. */
29980 if (dwarf_version >= 5
29981 && class_scope_p (die->die_parent)
29982 && get_AT (die, DW_AT_const_value))
29983 break;
29985 /* premark_used_variables marks external variables --- don't mark
29986 them here. But function-local externals are always considered
29987 used. */
29988 if (get_AT (die, DW_AT_external))
29990 for (c = die->die_parent; c; c = c->die_parent)
29991 if (c->die_tag == DW_TAG_subprogram)
29992 break;
29993 if (!c)
29994 return;
29997 /* FALLTHROUGH */
29999 default:
30000 /* Mark everything else. */
30001 break;
30004 if (die->die_mark == 0)
30006 die->die_mark = 1;
30008 /* Now, mark any dies referenced from here. */
30009 prune_unused_types_walk_attribs (die);
30012 die->die_mark = 2;
30014 /* Mark children. */
30015 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
30018 /* Increment the string counts on strings referred to from DIE's
30019 attributes. */
30021 static void
30022 prune_unused_types_update_strings (dw_die_ref die)
30024 dw_attr_node *a;
30025 unsigned ix;
30027 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30028 if (AT_class (a) == dw_val_class_str)
30030 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
30031 s->refcount++;
30032 /* Avoid unnecessarily putting strings that are used less than
30033 twice in the hash table. */
30034 if (s->form != DW_FORM_line_strp
30035 && (s->refcount
30036 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2)))
30038 indirect_string_node **slot
30039 = debug_str_hash->find_slot_with_hash (s->str,
30040 htab_hash_string (s->str),
30041 INSERT);
30042 gcc_assert (*slot == NULL);
30043 *slot = s;
30048 /* Mark DIE and its children as removed. */
30050 static void
30051 mark_removed (dw_die_ref die)
30053 dw_die_ref c;
30054 die->removed = true;
30055 FOR_EACH_CHILD (die, c, mark_removed (c));
30058 /* Remove from the tree DIE any dies that aren't marked. */
30060 static void
30061 prune_unused_types_prune (dw_die_ref die)
30063 dw_die_ref c;
30065 gcc_assert (die->die_mark);
30066 prune_unused_types_update_strings (die);
30068 if (! die->die_child)
30069 return;
30071 c = die->die_child;
30072 do {
30073 dw_die_ref prev = c, next;
30074 for (c = c->die_sib; ! c->die_mark; c = next)
30075 if (c == die->die_child)
30077 /* No marked children between 'prev' and the end of the list. */
30078 if (prev == c)
30079 /* No marked children at all. */
30080 die->die_child = NULL;
30081 else
30083 prev->die_sib = c->die_sib;
30084 die->die_child = prev;
30086 c->die_sib = NULL;
30087 mark_removed (c);
30088 return;
30090 else
30092 next = c->die_sib;
30093 c->die_sib = NULL;
30094 mark_removed (c);
30097 if (c != prev->die_sib)
30098 prev->die_sib = c;
30099 prune_unused_types_prune (c);
30100 } while (c != die->die_child);
30103 /* Remove dies representing declarations that we never use. */
30105 static void
30106 prune_unused_types (void)
30108 unsigned int i;
30109 limbo_die_node *node;
30110 comdat_type_node *ctnode;
30111 pubname_entry *pub;
30112 dw_die_ref base_type;
30114 #if ENABLE_ASSERT_CHECKING
30115 /* All the marks should already be clear. */
30116 verify_marks_clear (comp_unit_die ());
30117 for (node = limbo_die_list; node; node = node->next)
30118 verify_marks_clear (node->die);
30119 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30120 verify_marks_clear (ctnode->root_die);
30121 #endif /* ENABLE_ASSERT_CHECKING */
30123 /* Mark types that are used in global variables. */
30124 premark_types_used_by_global_vars ();
30126 /* Mark variables used in the symtab. */
30127 if (flag_debug_only_used_symbols)
30128 premark_used_variables ();
30130 /* Set the mark on nodes that are actually used. */
30131 prune_unused_types_walk (comp_unit_die ());
30132 for (node = limbo_die_list; node; node = node->next)
30133 prune_unused_types_walk (node->die);
30134 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30136 prune_unused_types_walk (ctnode->root_die);
30137 prune_unused_types_mark (ctnode->type_die, 1);
30140 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
30141 are unusual in that they are pubnames that are the children of pubtypes.
30142 They should only be marked via their parent DW_TAG_enumeration_type die,
30143 not as roots in themselves. */
30144 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
30145 if (pub->die->die_tag != DW_TAG_enumerator)
30146 prune_unused_types_mark (pub->die, 1);
30147 for (i = 0; base_types.iterate (i, &base_type); i++)
30148 prune_unused_types_mark (base_type, 1);
30150 /* Also set the mark on nodes that could be referenced by
30151 DW_TAG_call_site DW_AT_call_origin (i.e. direct call callees) or
30152 by DW_TAG_inlined_subroutine origins. */
30153 cgraph_node *cnode;
30154 FOR_EACH_FUNCTION (cnode)
30155 if (cnode->referred_to_p (false))
30157 dw_die_ref die = lookup_decl_die (cnode->decl);
30158 if (die == NULL || die->die_mark)
30159 continue;
30160 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
30161 if (e->caller != cnode)
30163 prune_unused_types_mark (die, 1);
30164 break;
30168 if (debug_str_hash)
30169 debug_str_hash->empty ();
30170 if (skeleton_debug_str_hash)
30171 skeleton_debug_str_hash->empty ();
30172 prune_unused_types_prune (comp_unit_die ());
30173 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
30175 node = *pnode;
30176 if (!node->die->die_mark)
30177 *pnode = node->next;
30178 else
30180 prune_unused_types_prune (node->die);
30181 pnode = &node->next;
30184 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30185 prune_unused_types_prune (ctnode->root_die);
30187 /* Leave the marks clear. */
30188 prune_unmark_dies (comp_unit_die ());
30189 for (node = limbo_die_list; node; node = node->next)
30190 prune_unmark_dies (node->die);
30191 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
30192 prune_unmark_dies (ctnode->root_die);
30195 /* Helpers to manipulate hash table of comdat type units. */
30197 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
30199 static inline hashval_t hash (const comdat_type_node *);
30200 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
30203 inline hashval_t
30204 comdat_type_hasher::hash (const comdat_type_node *type_node)
30206 hashval_t h;
30207 memcpy (&h, type_node->signature, sizeof (h));
30208 return h;
30211 inline bool
30212 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
30213 const comdat_type_node *type_node_2)
30215 return (! memcmp (type_node_1->signature, type_node_2->signature,
30216 DWARF_TYPE_SIGNATURE_SIZE));
30219 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
30220 to the location it would have been added, should we know its
30221 DECL_ASSEMBLER_NAME when we added other attributes. This will
30222 probably improve compactness of debug info, removing equivalent
30223 abbrevs, and hide any differences caused by deferring the
30224 computation of the assembler name, triggered by e.g. PCH. */
30226 static inline void
30227 move_linkage_attr (dw_die_ref die)
30229 unsigned ix = vec_safe_length (die->die_attr);
30230 dw_attr_node linkage = (*die->die_attr)[ix - 1];
30232 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
30233 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
30235 while (--ix > 0)
30237 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
30239 if (prev->dw_attr == DW_AT_decl_line
30240 || prev->dw_attr == DW_AT_decl_column
30241 || prev->dw_attr == DW_AT_name)
30242 break;
30245 if (ix != vec_safe_length (die->die_attr) - 1)
30247 die->die_attr->pop ();
30248 die->die_attr->quick_insert (ix, linkage);
30252 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
30253 referenced from typed stack ops and count how often they are used. */
30255 static void
30256 mark_base_types (dw_loc_descr_ref loc)
30258 dw_die_ref base_type = NULL;
30260 for (; loc; loc = loc->dw_loc_next)
30262 switch (loc->dw_loc_opc)
30264 case DW_OP_regval_type:
30265 case DW_OP_deref_type:
30266 case DW_OP_GNU_regval_type:
30267 case DW_OP_GNU_deref_type:
30268 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
30269 break;
30270 case DW_OP_convert:
30271 case DW_OP_reinterpret:
30272 case DW_OP_GNU_convert:
30273 case DW_OP_GNU_reinterpret:
30274 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
30275 continue;
30276 /* FALLTHRU */
30277 case DW_OP_const_type:
30278 case DW_OP_GNU_const_type:
30279 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
30280 break;
30281 case DW_OP_entry_value:
30282 case DW_OP_GNU_entry_value:
30283 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
30284 continue;
30285 default:
30286 continue;
30288 gcc_assert (base_type->die_parent == comp_unit_die ());
30289 if (base_type->die_mark)
30290 base_type->die_mark++;
30291 else
30293 base_types.safe_push (base_type);
30294 base_type->die_mark = 1;
30299 /* Stripped-down variant of resolve_addr, mark DW_TAG_base_type nodes
30300 referenced from typed stack ops and count how often they are used. */
30302 static void
30303 mark_base_types (dw_die_ref die)
30305 dw_die_ref c;
30306 dw_attr_node *a;
30307 dw_loc_list_ref *curr;
30308 unsigned ix;
30310 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30311 switch (AT_class (a))
30313 case dw_val_class_loc_list:
30314 curr = AT_loc_list_ptr (a);
30315 while (*curr)
30317 mark_base_types ((*curr)->expr);
30318 curr = &(*curr)->dw_loc_next;
30320 break;
30322 case dw_val_class_loc:
30323 mark_base_types (AT_loc (a));
30324 break;
30326 default:
30327 break;
30330 FOR_EACH_CHILD (die, c, mark_base_types (c));
30333 /* Comparison function for sorting marked base types. */
30335 static int
30336 base_type_cmp (const void *x, const void *y)
30338 dw_die_ref dx = *(const dw_die_ref *) x;
30339 dw_die_ref dy = *(const dw_die_ref *) y;
30340 unsigned int byte_size1, byte_size2;
30341 unsigned int encoding1, encoding2;
30342 unsigned int align1, align2;
30343 if (dx->die_mark > dy->die_mark)
30344 return -1;
30345 if (dx->die_mark < dy->die_mark)
30346 return 1;
30347 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
30348 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
30349 if (byte_size1 < byte_size2)
30350 return 1;
30351 if (byte_size1 > byte_size2)
30352 return -1;
30353 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
30354 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
30355 if (encoding1 < encoding2)
30356 return 1;
30357 if (encoding1 > encoding2)
30358 return -1;
30359 align1 = get_AT_unsigned (dx, DW_AT_alignment);
30360 align2 = get_AT_unsigned (dy, DW_AT_alignment);
30361 if (align1 < align2)
30362 return 1;
30363 if (align1 > align2)
30364 return -1;
30365 return 0;
30368 /* Move base types marked by mark_base_types as early as possible
30369 in the CU, sorted by decreasing usage count both to make the
30370 uleb128 references as small as possible and to make sure they
30371 will have die_offset already computed by calc_die_sizes when
30372 sizes of typed stack loc ops is computed. */
30374 static void
30375 move_marked_base_types (void)
30377 unsigned int i;
30378 dw_die_ref base_type, die, c;
30380 if (base_types.is_empty ())
30381 return;
30383 /* Sort by decreasing usage count, they will be added again in that
30384 order later on. */
30385 base_types.qsort (base_type_cmp);
30386 die = comp_unit_die ();
30387 c = die->die_child;
30390 dw_die_ref prev = c;
30391 c = c->die_sib;
30392 while (c->die_mark)
30394 remove_child_with_prev (c, prev);
30395 /* As base types got marked, there must be at least
30396 one node other than DW_TAG_base_type. */
30397 gcc_assert (die->die_child != NULL);
30398 c = prev->die_sib;
30401 while (c != die->die_child);
30402 gcc_assert (die->die_child);
30403 c = die->die_child;
30404 for (i = 0; base_types.iterate (i, &base_type); i++)
30406 base_type->die_mark = 0;
30407 base_type->die_sib = c->die_sib;
30408 c->die_sib = base_type;
30409 c = base_type;
30413 /* Helper function for resolve_addr, attempt to resolve
30414 one CONST_STRING, return true if successful. Similarly verify that
30415 SYMBOL_REFs refer to variables emitted in the current CU. */
30417 static bool
30418 resolve_one_addr (rtx *addr)
30420 rtx rtl = *addr;
30422 if (GET_CODE (rtl) == CONST_STRING)
30424 size_t len = strlen (XSTR (rtl, 0)) + 1;
30425 tree t = build_string (len, XSTR (rtl, 0));
30426 tree tlen = size_int (len - 1);
30427 TREE_TYPE (t)
30428 = build_array_type (char_type_node, build_index_type (tlen));
30429 rtl = lookup_constant_def (t);
30430 if (!rtl || !MEM_P (rtl))
30431 return false;
30432 rtl = XEXP (rtl, 0);
30433 if (GET_CODE (rtl) == SYMBOL_REF
30434 && SYMBOL_REF_DECL (rtl)
30435 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30436 return false;
30437 vec_safe_push (used_rtx_array, rtl);
30438 *addr = rtl;
30439 return true;
30442 if (GET_CODE (rtl) == SYMBOL_REF
30443 && SYMBOL_REF_DECL (rtl))
30445 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
30447 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
30448 return false;
30450 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
30451 return false;
30454 if (GET_CODE (rtl) == CONST)
30456 subrtx_ptr_iterator::array_type array;
30457 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
30458 if (!resolve_one_addr (*iter))
30459 return false;
30462 return true;
30465 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
30466 if possible, and create DW_TAG_dwarf_procedure that can be referenced
30467 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
30469 static rtx
30470 string_cst_pool_decl (tree t)
30472 rtx rtl = output_constant_def (t, 1);
30473 unsigned char *array;
30474 dw_loc_descr_ref l;
30475 tree decl;
30476 size_t len;
30477 dw_die_ref ref;
30479 if (!rtl || !MEM_P (rtl))
30480 return NULL_RTX;
30481 rtl = XEXP (rtl, 0);
30482 if (GET_CODE (rtl) != SYMBOL_REF
30483 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
30484 return NULL_RTX;
30486 decl = SYMBOL_REF_DECL (rtl);
30487 if (!lookup_decl_die (decl))
30489 len = TREE_STRING_LENGTH (t);
30490 vec_safe_push (used_rtx_array, rtl);
30491 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
30492 array = ggc_vec_alloc<unsigned char> (len);
30493 memcpy (array, TREE_STRING_POINTER (t), len);
30494 l = new_loc_descr (DW_OP_implicit_value, len, 0);
30495 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
30496 l->dw_loc_oprnd2.v.val_vec.length = len;
30497 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
30498 l->dw_loc_oprnd2.v.val_vec.array = array;
30499 add_AT_loc (ref, DW_AT_location, l);
30500 equate_decl_number_to_die (decl, ref);
30502 return rtl;
30505 /* Helper function of resolve_addr_in_expr. LOC is
30506 a DW_OP_addr followed by DW_OP_stack_value, either at the start
30507 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
30508 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
30509 with DW_OP_implicit_pointer if possible
30510 and return true, if unsuccessful, return false. */
30512 static bool
30513 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
30515 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
30516 HOST_WIDE_INT offset = 0;
30517 dw_die_ref ref = NULL;
30518 tree decl;
30520 if (GET_CODE (rtl) == CONST
30521 && GET_CODE (XEXP (rtl, 0)) == PLUS
30522 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
30524 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
30525 rtl = XEXP (XEXP (rtl, 0), 0);
30527 if (GET_CODE (rtl) == CONST_STRING)
30529 size_t len = strlen (XSTR (rtl, 0)) + 1;
30530 tree t = build_string (len, XSTR (rtl, 0));
30531 tree tlen = size_int (len - 1);
30533 TREE_TYPE (t)
30534 = build_array_type (char_type_node, build_index_type (tlen));
30535 rtl = string_cst_pool_decl (t);
30536 if (!rtl)
30537 return false;
30539 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
30541 decl = SYMBOL_REF_DECL (rtl);
30542 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
30544 ref = lookup_decl_die (decl);
30545 if (ref && (get_AT (ref, DW_AT_location)
30546 || get_AT (ref, DW_AT_const_value)))
30548 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
30549 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30550 loc->dw_loc_oprnd1.val_entry = NULL;
30551 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30552 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30553 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30554 loc->dw_loc_oprnd2.v.val_int = offset;
30555 return true;
30559 return false;
30562 /* Helper function for resolve_addr, handle one location
30563 expression, return false if at least one CONST_STRING or SYMBOL_REF in
30564 the location list couldn't be resolved. */
30566 static bool
30567 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30569 dw_loc_descr_ref keep = NULL;
30570 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
30571 switch (loc->dw_loc_opc)
30573 case DW_OP_addr:
30574 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30576 if ((prev == NULL
30577 || prev->dw_loc_opc == DW_OP_piece
30578 || prev->dw_loc_opc == DW_OP_bit_piece)
30579 && loc->dw_loc_next
30580 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
30581 && (!dwarf_strict || dwarf_version >= 5)
30582 && optimize_one_addr_into_implicit_ptr (loc))
30583 break;
30584 return false;
30586 break;
30587 case DW_OP_GNU_addr_index:
30588 case DW_OP_addrx:
30589 case DW_OP_GNU_const_index:
30590 case DW_OP_constx:
30591 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
30592 || loc->dw_loc_opc == DW_OP_addrx)
30593 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
30594 || loc->dw_loc_opc == DW_OP_constx)
30595 && loc->dtprel))
30597 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
30598 if (!resolve_one_addr (&rtl))
30599 return false;
30600 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
30601 loc->dw_loc_oprnd1.val_entry
30602 = add_addr_table_entry (rtl, ate_kind_rtx);
30604 break;
30605 case DW_OP_const4u:
30606 case DW_OP_const8u:
30607 if (loc->dtprel
30608 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
30609 return false;
30610 break;
30611 case DW_OP_plus_uconst:
30612 if (size_of_loc_descr (loc)
30613 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
30615 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
30617 dw_loc_descr_ref repl
30618 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
30619 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
30620 add_loc_descr (&repl, loc->dw_loc_next);
30621 *loc = *repl;
30623 break;
30624 case DW_OP_implicit_value:
30625 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
30626 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
30627 return false;
30628 break;
30629 case DW_OP_implicit_pointer:
30630 case DW_OP_GNU_implicit_pointer:
30631 case DW_OP_GNU_parameter_ref:
30632 case DW_OP_GNU_variable_value:
30633 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30635 dw_die_ref ref
30636 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
30637 if (ref == NULL)
30638 return false;
30639 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30640 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30641 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30643 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
30645 if (prev == NULL
30646 && loc->dw_loc_next == NULL
30647 && AT_class (a) == dw_val_class_loc)
30648 switch (a->dw_attr)
30650 /* Following attributes allow both exprloc and reference,
30651 so if the whole expression is DW_OP_GNU_variable_value
30652 alone we could transform it into reference. */
30653 case DW_AT_byte_size:
30654 case DW_AT_bit_size:
30655 case DW_AT_lower_bound:
30656 case DW_AT_upper_bound:
30657 case DW_AT_bit_stride:
30658 case DW_AT_count:
30659 case DW_AT_allocated:
30660 case DW_AT_associated:
30661 case DW_AT_byte_stride:
30662 a->dw_attr_val.val_class = dw_val_class_die_ref;
30663 a->dw_attr_val.val_entry = NULL;
30664 a->dw_attr_val.v.val_die_ref.die
30665 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30666 a->dw_attr_val.v.val_die_ref.external = 0;
30667 return true;
30668 default:
30669 break;
30671 if (dwarf_strict)
30672 return false;
30674 break;
30675 case DW_OP_const_type:
30676 case DW_OP_regval_type:
30677 case DW_OP_deref_type:
30678 case DW_OP_convert:
30679 case DW_OP_reinterpret:
30680 case DW_OP_GNU_const_type:
30681 case DW_OP_GNU_regval_type:
30682 case DW_OP_GNU_deref_type:
30683 case DW_OP_GNU_convert:
30684 case DW_OP_GNU_reinterpret:
30685 while (loc->dw_loc_next
30686 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
30687 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
30689 dw_die_ref base1, base2;
30690 unsigned enc1, enc2, size1, size2;
30691 if (loc->dw_loc_opc == DW_OP_regval_type
30692 || loc->dw_loc_opc == DW_OP_deref_type
30693 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30694 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30695 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
30696 else if (loc->dw_loc_oprnd1.val_class
30697 == dw_val_class_unsigned_const)
30698 break;
30699 else
30700 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
30701 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
30702 == dw_val_class_unsigned_const)
30703 break;
30704 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
30705 gcc_assert (base1->die_tag == DW_TAG_base_type
30706 && base2->die_tag == DW_TAG_base_type);
30707 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
30708 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
30709 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
30710 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
30711 if (size1 == size2
30712 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
30713 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
30714 && loc != keep)
30715 || enc1 == enc2))
30717 /* Optimize away next DW_OP_convert after
30718 adjusting LOC's base type die reference. */
30719 if (loc->dw_loc_opc == DW_OP_regval_type
30720 || loc->dw_loc_opc == DW_OP_deref_type
30721 || loc->dw_loc_opc == DW_OP_GNU_regval_type
30722 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
30723 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
30724 else
30725 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
30726 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
30727 continue;
30729 /* Don't change integer DW_OP_convert after e.g. floating
30730 point typed stack entry. */
30731 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
30732 keep = loc->dw_loc_next;
30733 break;
30735 break;
30736 default:
30737 break;
30739 return true;
30742 /* Helper function of resolve_addr. DIE had DW_AT_location of
30743 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
30744 and DW_OP_addr couldn't be resolved. resolve_addr has already
30745 removed the DW_AT_location attribute. This function attempts to
30746 add a new DW_AT_location attribute with DW_OP_implicit_pointer
30747 to it or DW_AT_const_value attribute, if possible. */
30749 static void
30750 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
30752 if (!VAR_P (decl)
30753 || lookup_decl_die (decl) != die
30754 || DECL_EXTERNAL (decl)
30755 || !TREE_STATIC (decl)
30756 || DECL_INITIAL (decl) == NULL_TREE
30757 || DECL_P (DECL_INITIAL (decl))
30758 || get_AT (die, DW_AT_const_value))
30759 return;
30761 tree init = DECL_INITIAL (decl);
30762 HOST_WIDE_INT offset = 0;
30763 /* For variables that have been optimized away and thus
30764 don't have a memory location, see if we can emit
30765 DW_AT_const_value instead. */
30766 if (tree_add_const_value_attribute (die, init))
30767 return;
30768 if (dwarf_strict && dwarf_version < 5)
30769 return;
30770 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
30771 and ADDR_EXPR refers to a decl that has DW_AT_location or
30772 DW_AT_const_value (but isn't addressable, otherwise
30773 resolving the original DW_OP_addr wouldn't fail), see if
30774 we can add DW_OP_implicit_pointer. */
30775 STRIP_NOPS (init);
30776 if (TREE_CODE (init) == POINTER_PLUS_EXPR
30777 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
30779 offset = tree_to_shwi (TREE_OPERAND (init, 1));
30780 init = TREE_OPERAND (init, 0);
30781 STRIP_NOPS (init);
30783 if (TREE_CODE (init) != ADDR_EXPR)
30784 return;
30785 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
30786 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
30787 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
30788 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
30789 && TREE_OPERAND (init, 0) != decl))
30791 dw_die_ref ref;
30792 dw_loc_descr_ref l;
30794 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
30796 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
30797 if (!rtl)
30798 return;
30799 decl = SYMBOL_REF_DECL (rtl);
30801 else
30802 decl = TREE_OPERAND (init, 0);
30803 ref = lookup_decl_die (decl);
30804 if (ref == NULL
30805 || (!get_AT (ref, DW_AT_location)
30806 && !get_AT (ref, DW_AT_const_value)))
30807 return;
30808 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
30809 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30810 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
30811 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30812 add_AT_loc (die, DW_AT_location, l);
30816 /* Return NULL if l is a DWARF expression, or first op that is not
30817 valid DWARF expression. */
30819 static dw_loc_descr_ref
30820 non_dwarf_expression (dw_loc_descr_ref l)
30822 while (l)
30824 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30825 return l;
30826 switch (l->dw_loc_opc)
30828 case DW_OP_regx:
30829 case DW_OP_implicit_value:
30830 case DW_OP_stack_value:
30831 case DW_OP_implicit_pointer:
30832 case DW_OP_GNU_implicit_pointer:
30833 case DW_OP_GNU_parameter_ref:
30834 case DW_OP_piece:
30835 case DW_OP_bit_piece:
30836 return l;
30837 default:
30838 break;
30840 l = l->dw_loc_next;
30842 return NULL;
30845 /* Return adjusted copy of EXPR:
30846 If it is empty DWARF expression, return it.
30847 If it is valid non-empty DWARF expression,
30848 return copy of EXPR with DW_OP_deref appended to it.
30849 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30850 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30851 If it is DWARF expression followed by DW_OP_stack_value, return
30852 copy of the DWARF expression without anything appended.
30853 Otherwise, return NULL. */
30855 static dw_loc_descr_ref
30856 copy_deref_exprloc (dw_loc_descr_ref expr)
30858 dw_loc_descr_ref tail = NULL;
30860 if (expr == NULL)
30861 return NULL;
30863 dw_loc_descr_ref l = non_dwarf_expression (expr);
30864 if (l && l->dw_loc_next)
30865 return NULL;
30867 if (l)
30869 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30870 tail = new_loc_descr ((enum dwarf_location_atom)
30871 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30872 0, 0);
30873 else
30874 switch (l->dw_loc_opc)
30876 case DW_OP_regx:
30877 tail = new_loc_descr (DW_OP_bregx,
30878 l->dw_loc_oprnd1.v.val_unsigned, 0);
30879 break;
30880 case DW_OP_stack_value:
30881 break;
30882 default:
30883 return NULL;
30886 else
30887 tail = new_loc_descr (DW_OP_deref, 0, 0);
30889 dw_loc_descr_ref ret = NULL, *p = &ret;
30890 while (expr != l)
30892 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30893 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30894 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30895 p = &(*p)->dw_loc_next;
30896 expr = expr->dw_loc_next;
30898 *p = tail;
30899 return ret;
30902 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30903 reference to a variable or argument, adjust it if needed and return:
30904 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30905 attribute if present should be removed
30906 0 keep the attribute perhaps with minor modifications, no need to rescan
30907 1 if the attribute has been successfully adjusted. */
30909 static int
30910 optimize_string_length (dw_attr_node *a)
30912 dw_loc_descr_ref l = AT_loc (a), lv;
30913 dw_die_ref die;
30914 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30916 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30917 die = lookup_decl_die (decl);
30918 if (die)
30920 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30921 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30922 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30924 else
30925 return -1;
30927 else
30928 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30930 /* DWARF5 allows reference class, so we can then reference the DIE.
30931 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30932 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30934 a->dw_attr_val.val_class = dw_val_class_die_ref;
30935 a->dw_attr_val.val_entry = NULL;
30936 a->dw_attr_val.v.val_die_ref.die = die;
30937 a->dw_attr_val.v.val_die_ref.external = 0;
30938 return 0;
30941 dw_attr_node *av = get_AT (die, DW_AT_location);
30942 dw_loc_list_ref d;
30943 bool non_dwarf_expr = false;
30945 if (av == NULL)
30946 return dwarf_strict ? -1 : 0;
30947 switch (AT_class (av))
30949 case dw_val_class_loc_list:
30950 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30951 if (d->expr && non_dwarf_expression (d->expr))
30952 non_dwarf_expr = true;
30953 break;
30954 case dw_val_class_view_list:
30955 gcc_unreachable ();
30956 case dw_val_class_loc:
30957 lv = AT_loc (av);
30958 if (lv == NULL)
30959 return dwarf_strict ? -1 : 0;
30960 if (non_dwarf_expression (lv))
30961 non_dwarf_expr = true;
30962 break;
30963 default:
30964 return dwarf_strict ? -1 : 0;
30967 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30968 into DW_OP_call4 or DW_OP_GNU_variable_value into
30969 DW_OP_call4 DW_OP_deref, do so. */
30970 if (!non_dwarf_expr
30971 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30973 l->dw_loc_opc = DW_OP_call4;
30974 if (l->dw_loc_next)
30975 l->dw_loc_next = NULL;
30976 else
30977 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30978 return 0;
30981 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30982 copy over the DW_AT_location attribute from die to a. */
30983 if (l->dw_loc_next != NULL)
30985 a->dw_attr_val = av->dw_attr_val;
30986 return 1;
30989 dw_loc_list_ref list, *p;
30990 switch (AT_class (av))
30992 case dw_val_class_loc_list:
30993 p = &list;
30994 list = NULL;
30995 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30997 lv = copy_deref_exprloc (d->expr);
30998 if (lv)
31000 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
31001 p = &(*p)->dw_loc_next;
31003 else if (!dwarf_strict && d->expr)
31004 return 0;
31006 if (list == NULL)
31007 return dwarf_strict ? -1 : 0;
31008 a->dw_attr_val.val_class = dw_val_class_loc_list;
31009 gen_llsym (list);
31010 *AT_loc_list_ptr (a) = list;
31011 return 1;
31012 case dw_val_class_loc:
31013 lv = copy_deref_exprloc (AT_loc (av));
31014 if (lv == NULL)
31015 return dwarf_strict ? -1 : 0;
31016 a->dw_attr_val.v.val_loc = lv;
31017 return 1;
31018 default:
31019 gcc_unreachable ();
31023 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
31024 an address in .rodata section if the string literal is emitted there,
31025 or remove the containing location list or replace DW_AT_const_value
31026 with DW_AT_location and empty location expression, if it isn't found
31027 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
31028 to something that has been emitted in the current CU. */
31030 static void
31031 resolve_addr (dw_die_ref die)
31033 dw_die_ref c;
31034 dw_attr_node *a;
31035 dw_loc_list_ref *curr, *start, loc;
31036 unsigned ix;
31037 bool remove_AT_byte_size = false;
31039 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31040 switch (AT_class (a))
31042 case dw_val_class_loc_list:
31043 start = curr = AT_loc_list_ptr (a);
31044 loc = *curr;
31045 gcc_assert (loc);
31046 /* The same list can be referenced more than once. See if we have
31047 already recorded the result from a previous pass. */
31048 if (loc->replaced)
31049 *curr = loc->dw_loc_next;
31050 else if (!loc->resolved_addr)
31052 /* As things stand, we do not expect or allow one die to
31053 reference a suffix of another die's location list chain.
31054 References must be identical or completely separate.
31055 There is therefore no need to cache the result of this
31056 pass on any list other than the first; doing so
31057 would lead to unnecessary writes. */
31058 while (*curr)
31060 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
31061 if (!resolve_addr_in_expr (a, (*curr)->expr))
31063 dw_loc_list_ref next = (*curr)->dw_loc_next;
31064 dw_loc_descr_ref l = (*curr)->expr;
31066 if (next && (*curr)->ll_symbol)
31068 gcc_assert (!next->ll_symbol);
31069 next->ll_symbol = (*curr)->ll_symbol;
31070 next->vl_symbol = (*curr)->vl_symbol;
31072 if (dwarf_split_debug_info)
31073 remove_loc_list_addr_table_entries (l);
31074 *curr = next;
31076 else
31078 mark_base_types ((*curr)->expr);
31079 curr = &(*curr)->dw_loc_next;
31082 if (loc == *start)
31083 loc->resolved_addr = 1;
31084 else
31086 loc->replaced = 1;
31087 loc->dw_loc_next = *start;
31090 if (!*start)
31092 remove_AT (die, a->dw_attr);
31093 ix--;
31095 break;
31096 case dw_val_class_view_list:
31098 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31099 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
31100 dw_val_node *llnode
31101 = view_list_to_loc_list_val_node (&a->dw_attr_val);
31102 /* If we no longer have a loclist, or it no longer needs
31103 views, drop this attribute. */
31104 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
31106 remove_AT (die, a->dw_attr);
31107 ix--;
31109 break;
31111 case dw_val_class_loc:
31113 dw_loc_descr_ref l = AT_loc (a);
31114 /* DW_OP_GNU_variable_value DW_OP_stack_value or
31115 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
31116 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
31117 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
31118 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
31119 with DW_FORM_ref referencing the same DIE as
31120 DW_OP_GNU_variable_value used to reference. */
31121 if (a->dw_attr == DW_AT_string_length
31122 && l
31123 && l->dw_loc_opc == DW_OP_GNU_variable_value
31124 && (l->dw_loc_next == NULL
31125 || (l->dw_loc_next->dw_loc_next == NULL
31126 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
31128 switch (optimize_string_length (a))
31130 case -1:
31131 remove_AT (die, a->dw_attr);
31132 ix--;
31133 /* If we drop DW_AT_string_length, we need to drop also
31134 DW_AT_{string_length_,}byte_size. */
31135 remove_AT_byte_size = true;
31136 continue;
31137 default:
31138 break;
31139 case 1:
31140 /* Even if we keep the optimized DW_AT_string_length,
31141 it might have changed AT_class, so process it again. */
31142 ix--;
31143 continue;
31146 /* For -gdwarf-2 don't attempt to optimize
31147 DW_AT_data_member_location containing
31148 DW_OP_plus_uconst - older consumers might
31149 rely on it being that op instead of a more complex,
31150 but shorter, location description. */
31151 if ((dwarf_version > 2
31152 || a->dw_attr != DW_AT_data_member_location
31153 || l == NULL
31154 || l->dw_loc_opc != DW_OP_plus_uconst
31155 || l->dw_loc_next != NULL)
31156 && !resolve_addr_in_expr (a, l))
31158 if (dwarf_split_debug_info)
31159 remove_loc_list_addr_table_entries (l);
31160 if (l != NULL
31161 && l->dw_loc_next == NULL
31162 && l->dw_loc_opc == DW_OP_addr
31163 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
31164 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
31165 && a->dw_attr == DW_AT_location)
31167 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
31168 remove_AT (die, a->dw_attr);
31169 ix--;
31170 optimize_location_into_implicit_ptr (die, decl);
31171 break;
31173 if (a->dw_attr == DW_AT_string_length)
31174 /* If we drop DW_AT_string_length, we need to drop also
31175 DW_AT_{string_length_,}byte_size. */
31176 remove_AT_byte_size = true;
31177 remove_AT (die, a->dw_attr);
31178 ix--;
31180 else
31181 mark_base_types (l);
31183 break;
31184 case dw_val_class_addr:
31185 if (a->dw_attr == DW_AT_const_value
31186 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
31188 if (AT_index (a) != NOT_INDEXED)
31189 remove_addr_table_entry (a->dw_attr_val.val_entry);
31190 remove_AT (die, a->dw_attr);
31191 ix--;
31193 if ((die->die_tag == DW_TAG_call_site
31194 && a->dw_attr == DW_AT_call_origin)
31195 || (die->die_tag == DW_TAG_GNU_call_site
31196 && a->dw_attr == DW_AT_abstract_origin))
31198 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
31199 dw_die_ref tdie = lookup_decl_die (tdecl);
31200 dw_die_ref cdie;
31201 if (tdie == NULL
31202 && DECL_EXTERNAL (tdecl)
31203 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
31204 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
31206 dw_die_ref pdie = cdie;
31207 /* Make sure we don't add these DIEs into type units.
31208 We could emit skeleton DIEs for context (namespaces,
31209 outer structs/classes) and a skeleton DIE for the
31210 innermost context with DW_AT_signature pointing to the
31211 type unit. See PR78835. */
31212 while (pdie && pdie->die_tag != DW_TAG_type_unit)
31213 pdie = pdie->die_parent;
31214 if (pdie == NULL)
31216 /* Creating a full DIE for tdecl is overly expensive and
31217 at this point even wrong when in the LTO phase
31218 as it can end up generating new type DIEs we didn't
31219 output and thus optimize_external_refs will crash. */
31220 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
31221 add_AT_flag (tdie, DW_AT_external, 1);
31222 add_AT_flag (tdie, DW_AT_declaration, 1);
31223 add_linkage_attr (tdie, tdecl);
31224 add_name_and_src_coords_attributes (tdie, tdecl, true);
31225 equate_decl_number_to_die (tdecl, tdie);
31228 if (tdie)
31230 a->dw_attr_val.val_class = dw_val_class_die_ref;
31231 a->dw_attr_val.v.val_die_ref.die = tdie;
31232 a->dw_attr_val.v.val_die_ref.external = 0;
31234 else
31236 if (AT_index (a) != NOT_INDEXED)
31237 remove_addr_table_entry (a->dw_attr_val.val_entry);
31238 remove_AT (die, a->dw_attr);
31239 ix--;
31242 break;
31243 default:
31244 break;
31247 if (remove_AT_byte_size)
31248 remove_AT (die, dwarf_version >= 5
31249 ? DW_AT_string_length_byte_size
31250 : DW_AT_byte_size);
31252 FOR_EACH_CHILD (die, c, resolve_addr (c));
31255 /* Helper routines for optimize_location_lists.
31256 This pass tries to share identical local lists in .debug_loc
31257 section. */
31259 /* Iteratively hash operands of LOC opcode into HSTATE. */
31261 static void
31262 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
31264 dw_val_ref val1 = &loc->dw_loc_oprnd1;
31265 dw_val_ref val2 = &loc->dw_loc_oprnd2;
31267 switch (loc->dw_loc_opc)
31269 case DW_OP_const4u:
31270 case DW_OP_const8u:
31271 if (loc->dtprel)
31272 goto hash_addr;
31273 /* FALLTHRU */
31274 case DW_OP_const1u:
31275 case DW_OP_const1s:
31276 case DW_OP_const2u:
31277 case DW_OP_const2s:
31278 case DW_OP_const4s:
31279 case DW_OP_const8s:
31280 case DW_OP_constu:
31281 case DW_OP_consts:
31282 case DW_OP_pick:
31283 case DW_OP_plus_uconst:
31284 case DW_OP_breg0:
31285 case DW_OP_breg1:
31286 case DW_OP_breg2:
31287 case DW_OP_breg3:
31288 case DW_OP_breg4:
31289 case DW_OP_breg5:
31290 case DW_OP_breg6:
31291 case DW_OP_breg7:
31292 case DW_OP_breg8:
31293 case DW_OP_breg9:
31294 case DW_OP_breg10:
31295 case DW_OP_breg11:
31296 case DW_OP_breg12:
31297 case DW_OP_breg13:
31298 case DW_OP_breg14:
31299 case DW_OP_breg15:
31300 case DW_OP_breg16:
31301 case DW_OP_breg17:
31302 case DW_OP_breg18:
31303 case DW_OP_breg19:
31304 case DW_OP_breg20:
31305 case DW_OP_breg21:
31306 case DW_OP_breg22:
31307 case DW_OP_breg23:
31308 case DW_OP_breg24:
31309 case DW_OP_breg25:
31310 case DW_OP_breg26:
31311 case DW_OP_breg27:
31312 case DW_OP_breg28:
31313 case DW_OP_breg29:
31314 case DW_OP_breg30:
31315 case DW_OP_breg31:
31316 case DW_OP_regx:
31317 case DW_OP_fbreg:
31318 case DW_OP_piece:
31319 case DW_OP_deref_size:
31320 case DW_OP_xderef_size:
31321 hstate.add_object (val1->v.val_int);
31322 break;
31323 case DW_OP_skip:
31324 case DW_OP_bra:
31326 int offset;
31328 gcc_assert (val1->val_class == dw_val_class_loc);
31329 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
31330 hstate.add_object (offset);
31332 break;
31333 case DW_OP_implicit_value:
31334 hstate.add_object (val1->v.val_unsigned);
31335 switch (val2->val_class)
31337 case dw_val_class_const:
31338 hstate.add_object (val2->v.val_int);
31339 break;
31340 case dw_val_class_vec:
31342 unsigned int elt_size = val2->v.val_vec.elt_size;
31343 unsigned int len = val2->v.val_vec.length;
31345 hstate.add_int (elt_size);
31346 hstate.add_int (len);
31347 hstate.add (val2->v.val_vec.array, len * elt_size);
31349 break;
31350 case dw_val_class_const_double:
31351 hstate.add_object (val2->v.val_double.low);
31352 hstate.add_object (val2->v.val_double.high);
31353 break;
31354 case dw_val_class_wide_int:
31355 hstate.add (val2->v.val_wide->get_val (),
31356 get_full_len (*val2->v.val_wide)
31357 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31358 break;
31359 case dw_val_class_addr:
31360 inchash::add_rtx (val2->v.val_addr, hstate);
31361 break;
31362 default:
31363 gcc_unreachable ();
31365 break;
31366 case DW_OP_bregx:
31367 case DW_OP_bit_piece:
31368 hstate.add_object (val1->v.val_int);
31369 hstate.add_object (val2->v.val_int);
31370 break;
31371 case DW_OP_addr:
31372 hash_addr:
31373 if (loc->dtprel)
31375 unsigned char dtprel = 0xd1;
31376 hstate.add_object (dtprel);
31378 inchash::add_rtx (val1->v.val_addr, hstate);
31379 break;
31380 case DW_OP_GNU_addr_index:
31381 case DW_OP_addrx:
31382 case DW_OP_GNU_const_index:
31383 case DW_OP_constx:
31385 if (loc->dtprel)
31387 unsigned char dtprel = 0xd1;
31388 hstate.add_object (dtprel);
31390 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
31392 break;
31393 case DW_OP_implicit_pointer:
31394 case DW_OP_GNU_implicit_pointer:
31395 hstate.add_int (val2->v.val_int);
31396 break;
31397 case DW_OP_entry_value:
31398 case DW_OP_GNU_entry_value:
31399 hstate.add_object (val1->v.val_loc);
31400 break;
31401 case DW_OP_regval_type:
31402 case DW_OP_deref_type:
31403 case DW_OP_GNU_regval_type:
31404 case DW_OP_GNU_deref_type:
31406 unsigned int byte_size
31407 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
31408 unsigned int encoding
31409 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
31410 hstate.add_object (val1->v.val_int);
31411 hstate.add_object (byte_size);
31412 hstate.add_object (encoding);
31414 break;
31415 case DW_OP_convert:
31416 case DW_OP_reinterpret:
31417 case DW_OP_GNU_convert:
31418 case DW_OP_GNU_reinterpret:
31419 if (val1->val_class == dw_val_class_unsigned_const)
31421 hstate.add_object (val1->v.val_unsigned);
31422 break;
31424 /* FALLTHRU */
31425 case DW_OP_const_type:
31426 case DW_OP_GNU_const_type:
31428 unsigned int byte_size
31429 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
31430 unsigned int encoding
31431 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
31432 hstate.add_object (byte_size);
31433 hstate.add_object (encoding);
31434 if (loc->dw_loc_opc != DW_OP_const_type
31435 && loc->dw_loc_opc != DW_OP_GNU_const_type)
31436 break;
31437 hstate.add_object (val2->val_class);
31438 switch (val2->val_class)
31440 case dw_val_class_const:
31441 hstate.add_object (val2->v.val_int);
31442 break;
31443 case dw_val_class_vec:
31445 unsigned int elt_size = val2->v.val_vec.elt_size;
31446 unsigned int len = val2->v.val_vec.length;
31448 hstate.add_object (elt_size);
31449 hstate.add_object (len);
31450 hstate.add (val2->v.val_vec.array, len * elt_size);
31452 break;
31453 case dw_val_class_const_double:
31454 hstate.add_object (val2->v.val_double.low);
31455 hstate.add_object (val2->v.val_double.high);
31456 break;
31457 case dw_val_class_wide_int:
31458 hstate.add (val2->v.val_wide->get_val (),
31459 get_full_len (*val2->v.val_wide)
31460 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
31461 break;
31462 default:
31463 gcc_unreachable ();
31466 break;
31468 default:
31469 /* Other codes have no operands. */
31470 break;
31474 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
31476 static inline void
31477 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
31479 dw_loc_descr_ref l;
31480 bool sizes_computed = false;
31481 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
31482 size_of_locs (loc);
31484 for (l = loc; l != NULL; l = l->dw_loc_next)
31486 enum dwarf_location_atom opc = l->dw_loc_opc;
31487 hstate.add_object (opc);
31488 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
31490 size_of_locs (loc);
31491 sizes_computed = true;
31493 hash_loc_operands (l, hstate);
31497 /* Compute hash of the whole location list LIST_HEAD. */
31499 static inline void
31500 hash_loc_list (dw_loc_list_ref list_head)
31502 dw_loc_list_ref curr = list_head;
31503 inchash::hash hstate;
31505 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
31507 hstate.add (curr->begin, strlen (curr->begin) + 1);
31508 hstate.add (curr->end, strlen (curr->end) + 1);
31509 hstate.add_object (curr->vbegin);
31510 hstate.add_object (curr->vend);
31511 if (curr->section)
31512 hstate.add (curr->section, strlen (curr->section) + 1);
31513 hash_locs (curr->expr, hstate);
31515 list_head->hash = hstate.end ();
31518 /* Return true if X and Y opcodes have the same operands. */
31520 static inline bool
31521 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
31523 dw_val_ref valx1 = &x->dw_loc_oprnd1;
31524 dw_val_ref valx2 = &x->dw_loc_oprnd2;
31525 dw_val_ref valy1 = &y->dw_loc_oprnd1;
31526 dw_val_ref valy2 = &y->dw_loc_oprnd2;
31528 switch (x->dw_loc_opc)
31530 case DW_OP_const4u:
31531 case DW_OP_const8u:
31532 if (x->dtprel)
31533 goto hash_addr;
31534 /* FALLTHRU */
31535 case DW_OP_const1u:
31536 case DW_OP_const1s:
31537 case DW_OP_const2u:
31538 case DW_OP_const2s:
31539 case DW_OP_const4s:
31540 case DW_OP_const8s:
31541 case DW_OP_constu:
31542 case DW_OP_consts:
31543 case DW_OP_pick:
31544 case DW_OP_plus_uconst:
31545 case DW_OP_breg0:
31546 case DW_OP_breg1:
31547 case DW_OP_breg2:
31548 case DW_OP_breg3:
31549 case DW_OP_breg4:
31550 case DW_OP_breg5:
31551 case DW_OP_breg6:
31552 case DW_OP_breg7:
31553 case DW_OP_breg8:
31554 case DW_OP_breg9:
31555 case DW_OP_breg10:
31556 case DW_OP_breg11:
31557 case DW_OP_breg12:
31558 case DW_OP_breg13:
31559 case DW_OP_breg14:
31560 case DW_OP_breg15:
31561 case DW_OP_breg16:
31562 case DW_OP_breg17:
31563 case DW_OP_breg18:
31564 case DW_OP_breg19:
31565 case DW_OP_breg20:
31566 case DW_OP_breg21:
31567 case DW_OP_breg22:
31568 case DW_OP_breg23:
31569 case DW_OP_breg24:
31570 case DW_OP_breg25:
31571 case DW_OP_breg26:
31572 case DW_OP_breg27:
31573 case DW_OP_breg28:
31574 case DW_OP_breg29:
31575 case DW_OP_breg30:
31576 case DW_OP_breg31:
31577 case DW_OP_regx:
31578 case DW_OP_fbreg:
31579 case DW_OP_piece:
31580 case DW_OP_deref_size:
31581 case DW_OP_xderef_size:
31582 return valx1->v.val_int == valy1->v.val_int;
31583 case DW_OP_skip:
31584 case DW_OP_bra:
31585 /* If splitting debug info, the use of DW_OP_GNU_addr_index
31586 can cause irrelevant differences in dw_loc_addr. */
31587 gcc_assert (valx1->val_class == dw_val_class_loc
31588 && valy1->val_class == dw_val_class_loc
31589 && (dwarf_split_debug_info
31590 || x->dw_loc_addr == y->dw_loc_addr));
31591 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
31592 case DW_OP_implicit_value:
31593 if (valx1->v.val_unsigned != valy1->v.val_unsigned
31594 || valx2->val_class != valy2->val_class)
31595 return false;
31596 switch (valx2->val_class)
31598 case dw_val_class_const:
31599 return valx2->v.val_int == valy2->v.val_int;
31600 case dw_val_class_vec:
31601 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31602 && valx2->v.val_vec.length == valy2->v.val_vec.length
31603 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31604 valx2->v.val_vec.elt_size
31605 * valx2->v.val_vec.length) == 0;
31606 case dw_val_class_const_double:
31607 return valx2->v.val_double.low == valy2->v.val_double.low
31608 && valx2->v.val_double.high == valy2->v.val_double.high;
31609 case dw_val_class_wide_int:
31610 return *valx2->v.val_wide == *valy2->v.val_wide;
31611 case dw_val_class_addr:
31612 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
31613 default:
31614 gcc_unreachable ();
31616 case DW_OP_bregx:
31617 case DW_OP_bit_piece:
31618 return valx1->v.val_int == valy1->v.val_int
31619 && valx2->v.val_int == valy2->v.val_int;
31620 case DW_OP_addr:
31621 hash_addr:
31622 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
31623 case DW_OP_GNU_addr_index:
31624 case DW_OP_addrx:
31625 case DW_OP_GNU_const_index:
31626 case DW_OP_constx:
31628 rtx ax1 = valx1->val_entry->addr.rtl;
31629 rtx ay1 = valy1->val_entry->addr.rtl;
31630 return rtx_equal_p (ax1, ay1);
31632 case DW_OP_implicit_pointer:
31633 case DW_OP_GNU_implicit_pointer:
31634 return valx1->val_class == dw_val_class_die_ref
31635 && valx1->val_class == valy1->val_class
31636 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
31637 && valx2->v.val_int == valy2->v.val_int;
31638 case DW_OP_entry_value:
31639 case DW_OP_GNU_entry_value:
31640 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
31641 case DW_OP_const_type:
31642 case DW_OP_GNU_const_type:
31643 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
31644 || valx2->val_class != valy2->val_class)
31645 return false;
31646 switch (valx2->val_class)
31648 case dw_val_class_const:
31649 return valx2->v.val_int == valy2->v.val_int;
31650 case dw_val_class_vec:
31651 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
31652 && valx2->v.val_vec.length == valy2->v.val_vec.length
31653 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
31654 valx2->v.val_vec.elt_size
31655 * valx2->v.val_vec.length) == 0;
31656 case dw_val_class_const_double:
31657 return valx2->v.val_double.low == valy2->v.val_double.low
31658 && valx2->v.val_double.high == valy2->v.val_double.high;
31659 case dw_val_class_wide_int:
31660 return *valx2->v.val_wide == *valy2->v.val_wide;
31661 default:
31662 gcc_unreachable ();
31664 case DW_OP_regval_type:
31665 case DW_OP_deref_type:
31666 case DW_OP_GNU_regval_type:
31667 case DW_OP_GNU_deref_type:
31668 return valx1->v.val_int == valy1->v.val_int
31669 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
31670 case DW_OP_convert:
31671 case DW_OP_reinterpret:
31672 case DW_OP_GNU_convert:
31673 case DW_OP_GNU_reinterpret:
31674 if (valx1->val_class != valy1->val_class)
31675 return false;
31676 if (valx1->val_class == dw_val_class_unsigned_const)
31677 return valx1->v.val_unsigned == valy1->v.val_unsigned;
31678 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31679 case DW_OP_GNU_parameter_ref:
31680 return valx1->val_class == dw_val_class_die_ref
31681 && valx1->val_class == valy1->val_class
31682 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
31683 default:
31684 /* Other codes have no operands. */
31685 return true;
31689 /* Return true if DWARF location expressions X and Y are the same. */
31691 static inline bool
31692 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
31694 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
31695 if (x->dw_loc_opc != y->dw_loc_opc
31696 || x->dtprel != y->dtprel
31697 || !compare_loc_operands (x, y))
31698 break;
31699 return x == NULL && y == NULL;
31702 /* Hashtable helpers. */
31704 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
31706 static inline hashval_t hash (const dw_loc_list_struct *);
31707 static inline bool equal (const dw_loc_list_struct *,
31708 const dw_loc_list_struct *);
31711 /* Return precomputed hash of location list X. */
31713 inline hashval_t
31714 loc_list_hasher::hash (const dw_loc_list_struct *x)
31716 return x->hash;
31719 /* Return true if location lists A and B are the same. */
31721 inline bool
31722 loc_list_hasher::equal (const dw_loc_list_struct *a,
31723 const dw_loc_list_struct *b)
31725 if (a == b)
31726 return 1;
31727 if (a->hash != b->hash)
31728 return 0;
31729 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
31730 if (strcmp (a->begin, b->begin) != 0
31731 || strcmp (a->end, b->end) != 0
31732 || (a->section == NULL) != (b->section == NULL)
31733 || (a->section && strcmp (a->section, b->section) != 0)
31734 || a->vbegin != b->vbegin || a->vend != b->vend
31735 || !compare_locs (a->expr, b->expr))
31736 break;
31737 return a == NULL && b == NULL;
31740 typedef hash_table<loc_list_hasher> loc_list_hash_type;
31743 /* Recursively optimize location lists referenced from DIE
31744 children and share them whenever possible. */
31746 static void
31747 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
31749 dw_die_ref c;
31750 dw_attr_node *a;
31751 unsigned ix;
31752 dw_loc_list_struct **slot;
31753 bool drop_locviews = false;
31754 bool has_locviews = false;
31756 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31757 if (AT_class (a) == dw_val_class_loc_list)
31759 dw_loc_list_ref list = AT_loc_list (a);
31760 /* TODO: perform some optimizations here, before hashing
31761 it and storing into the hash table. */
31762 hash_loc_list (list);
31763 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
31764 if (*slot == NULL)
31766 *slot = list;
31767 if (loc_list_has_views (list))
31768 gcc_assert (list->vl_symbol);
31769 else if (list->vl_symbol)
31771 drop_locviews = true;
31772 list->vl_symbol = NULL;
31775 else
31777 if (list->vl_symbol && !(*slot)->vl_symbol)
31778 drop_locviews = true;
31779 a->dw_attr_val.v.val_loc_list = *slot;
31782 else if (AT_class (a) == dw_val_class_view_list)
31784 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
31785 has_locviews = true;
31789 if (drop_locviews && has_locviews)
31790 remove_AT (die, DW_AT_GNU_locviews);
31792 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
31796 /* Recursively assign each location list a unique index into the debug_addr
31797 section. */
31799 static void
31800 index_location_lists (dw_die_ref die)
31802 dw_die_ref c;
31803 dw_attr_node *a;
31804 unsigned ix;
31806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31807 if (AT_class (a) == dw_val_class_loc_list)
31809 dw_loc_list_ref list = AT_loc_list (a);
31810 dw_loc_list_ref curr;
31811 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
31813 /* Don't index an entry that has already been indexed
31814 or won't be output. Make sure skip_loc_list_entry doesn't
31815 call size_of_locs, because that might cause circular dependency,
31816 index_location_lists requiring address table indexes to be
31817 computed, but adding new indexes through add_addr_table_entry
31818 and address table index computation requiring no new additions
31819 to the hash table. In the rare case of DWARF[234] >= 64KB
31820 location expression, we'll just waste unused address table entry
31821 for it. */
31822 if (curr->begin_entry != NULL || skip_loc_list_entry (curr))
31823 continue;
31825 curr->begin_entry
31826 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31827 if (dwarf_version >= 5 && !HAVE_AS_LEB128)
31828 curr->end_entry
31829 = add_addr_table_entry (xstrdup (curr->end), ate_kind_label);
31833 FOR_EACH_CHILD (die, c, index_location_lists (c));
31836 /* Optimize location lists referenced from DIE
31837 children and share them whenever possible. */
31839 static void
31840 optimize_location_lists (dw_die_ref die)
31842 loc_list_hash_type htab (500);
31843 optimize_location_lists_1 (die, &htab);
31846 /* Traverse the limbo die list, and add parent/child links. The only
31847 dies without parents that should be here are concrete instances of
31848 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31849 For concrete instances, we can get the parent die from the abstract
31850 instance. */
31852 static void
31853 flush_limbo_die_list (void)
31855 limbo_die_node *node;
31857 /* get_context_die calls force_decl_die, which can put new DIEs on the
31858 limbo list in LTO mode when nested functions are put in a different
31859 partition than that of their parent function. */
31860 while ((node = limbo_die_list))
31862 dw_die_ref die = node->die;
31863 limbo_die_list = node->next;
31865 if (die->die_parent == NULL)
31867 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31869 if (origin && origin->die_parent)
31870 add_child_die (origin->die_parent, die);
31871 else if (is_cu_die (die))
31873 else if (seen_error ())
31874 /* It's OK to be confused by errors in the input. */
31875 add_child_die (comp_unit_die (), die);
31876 else
31878 /* In certain situations, the lexical block containing a
31879 nested function can be optimized away, which results
31880 in the nested function die being orphaned. Likewise
31881 with the return type of that nested function. Force
31882 this to be a child of the containing function.
31884 It may happen that even the containing function got fully
31885 inlined and optimized out. In that case we are lost and
31886 assign the empty child. This should not be big issue as
31887 the function is likely unreachable too. */
31888 gcc_assert (node->created_for);
31890 if (DECL_P (node->created_for))
31891 origin = get_context_die (DECL_CONTEXT (node->created_for));
31892 else if (TYPE_P (node->created_for))
31893 origin = scope_die_for (node->created_for, comp_unit_die ());
31894 else
31895 origin = comp_unit_die ();
31897 add_child_die (origin, die);
31903 /* Reset DIEs so we can output them again. */
31905 static void
31906 reset_dies (dw_die_ref die)
31908 dw_die_ref c;
31910 /* Remove stuff we re-generate. */
31911 die->die_mark = 0;
31912 die->die_offset = 0;
31913 die->die_abbrev = 0;
31914 remove_AT (die, DW_AT_sibling);
31916 FOR_EACH_CHILD (die, c, reset_dies (c));
31919 /* reset_indirect_string removed the references coming from DW_AT_name
31920 and DW_AT_comp_dir attributes on compilation unit DIEs. Readd them as
31921 .debug_line_str strings again. */
31923 static void
31924 adjust_name_comp_dir (dw_die_ref die)
31926 for (int i = 0; i < 2; i++)
31928 dwarf_attribute attr_kind = i ? DW_AT_comp_dir : DW_AT_name;
31929 dw_attr_node *a = get_AT (die, attr_kind);
31930 if (a == NULL || a->dw_attr_val.val_class != dw_val_class_str)
31931 continue;
31933 if (!debug_line_str_hash)
31934 debug_line_str_hash
31935 = hash_table<indirect_string_hasher>::create_ggc (10);
31937 struct indirect_string_node *node
31938 = find_AT_string_in_table (a->dw_attr_val.v.val_str->str,
31939 debug_line_str_hash);
31940 set_indirect_string (node);
31941 node->form = DW_FORM_line_strp;
31942 a->dw_attr_val.v.val_str = node;
31946 /* Output stuff that dwarf requires at the end of every file,
31947 and generate the DWARF-2 debugging info. */
31949 static void
31950 dwarf2out_finish (const char *filename)
31952 comdat_type_node *ctnode;
31953 dw_die_ref main_comp_unit_die;
31954 unsigned char checksum[16];
31955 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31957 /* Generate CTF/BTF debug info. */
31958 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
31959 || btf_debuginfo_p ()) && lang_GNU_C ())
31960 ctf_debug_finish (filename);
31962 /* Skip emitting DWARF if not required. */
31963 if (!dwarf_debuginfo_p ())
31964 return;
31966 /* Flush out any latecomers to the limbo party. */
31967 flush_limbo_die_list ();
31969 if (inline_entry_data_table)
31970 gcc_assert (inline_entry_data_table->is_empty ());
31972 if (flag_checking)
31974 verify_die (comp_unit_die ());
31975 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31976 verify_die (node->die);
31979 /* We shouldn't have any symbols with delayed asm names for
31980 DIEs generated after early finish. */
31981 gcc_assert (deferred_asm_name == NULL);
31983 gen_remaining_tmpl_value_param_die_attribute ();
31985 if (flag_generate_lto || flag_generate_offload)
31987 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31989 /* Prune stuff so that dwarf2out_finish runs successfully
31990 for the fat part of the object. */
31991 reset_dies (comp_unit_die ());
31992 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31993 reset_dies (node->die);
31995 hash_table<comdat_type_hasher> comdat_type_table (100);
31996 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31998 comdat_type_node **slot
31999 = comdat_type_table.find_slot (ctnode, INSERT);
32001 /* Don't reset types twice. */
32002 if (*slot != HTAB_EMPTY_ENTRY)
32003 continue;
32005 /* Remove the pointer to the line table. */
32006 remove_AT (ctnode->root_die, DW_AT_stmt_list);
32008 if (debug_info_level >= DINFO_LEVEL_TERSE)
32009 reset_dies (ctnode->root_die);
32011 *slot = ctnode;
32014 /* Reset die CU symbol so we don't output it twice. */
32015 comp_unit_die ()->die_id.die_symbol = NULL;
32017 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
32018 remove_AT (comp_unit_die (), DW_AT_stmt_list);
32019 if (have_macinfo)
32020 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
32022 /* Remove indirect string decisions. */
32023 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
32024 if (debug_line_str_hash)
32026 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
32027 debug_line_str_hash = NULL;
32028 if (asm_outputs_debug_line_str ())
32030 adjust_name_comp_dir (comp_unit_die ());
32031 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32032 adjust_name_comp_dir (node->die);
32037 #if ENABLE_ASSERT_CHECKING
32039 dw_die_ref die = comp_unit_die (), c;
32040 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
32042 #endif
32043 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32044 resolve_addr (ctnode->root_die);
32045 resolve_addr (comp_unit_die ());
32046 move_marked_base_types ();
32048 if (dump_file)
32050 fprintf (dump_file, "DWARF for %s\n", filename);
32051 print_die (comp_unit_die (), dump_file);
32054 /* Initialize sections and labels used for actual assembler output. */
32055 unsigned generation = init_sections_and_labels (false);
32057 /* Traverse the DIE's and add sibling attributes to those DIE's that
32058 have children. */
32059 add_sibling_attributes (comp_unit_die ());
32060 limbo_die_node *node;
32061 for (node = cu_die_list; node; node = node->next)
32062 add_sibling_attributes (node->die);
32063 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32064 add_sibling_attributes (ctnode->root_die);
32066 /* When splitting DWARF info, we put some attributes in the
32067 skeleton compile_unit DIE that remains in the .o, while
32068 most attributes go in the DWO compile_unit_die. */
32069 if (dwarf_split_debug_info)
32071 limbo_die_node *cu;
32072 main_comp_unit_die = gen_compile_unit_die (NULL);
32073 if (dwarf_version >= 5)
32074 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
32075 cu = limbo_die_list;
32076 gcc_assert (cu->die == main_comp_unit_die);
32077 limbo_die_list = limbo_die_list->next;
32078 cu->next = cu_die_list;
32079 cu_die_list = cu;
32081 else
32082 main_comp_unit_die = comp_unit_die ();
32084 /* Output a terminator label for the .text section. */
32085 switch_to_section (text_section);
32086 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
32087 if (cold_text_section)
32089 switch_to_section (cold_text_section);
32090 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
32093 /* We can only use the low/high_pc attributes if all of the code was
32094 in .text. */
32095 if ((!have_multiple_function_sections
32096 && vec_safe_length (switch_text_ranges) < 2)
32097 || (dwarf_version < 3 && dwarf_strict))
32099 const char *end_label = text_end_label;
32100 if (vec_safe_length (switch_text_ranges) == 1)
32101 end_label = (*switch_text_ranges)[0];
32102 /* Don't add if the CU has no associated code. */
32103 if (switch_text_ranges)
32104 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
32105 end_label, true);
32107 else
32109 unsigned fde_idx;
32110 dw_fde_ref fde;
32111 bool range_list_added = false;
32112 if (switch_text_ranges)
32114 const char *prev_loc = text_section_label;
32115 const char *loc;
32116 unsigned idx;
32118 FOR_EACH_VEC_ELT (*switch_text_ranges, idx, loc)
32119 if (prev_loc)
32121 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32122 loc, &range_list_added, true);
32123 prev_loc = NULL;
32125 else
32126 prev_loc = loc;
32128 if (prev_loc)
32129 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32130 text_end_label, &range_list_added, true);
32133 if (switch_cold_ranges)
32135 const char *prev_loc = cold_text_section_label;
32136 const char *loc;
32137 unsigned idx;
32139 FOR_EACH_VEC_ELT (*switch_cold_ranges, idx, loc)
32140 if (prev_loc)
32142 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32143 loc, &range_list_added, true);
32144 prev_loc = NULL;
32146 else
32147 prev_loc = loc;
32149 if (prev_loc)
32150 add_ranges_by_labels (main_comp_unit_die, prev_loc,
32151 cold_end_label, &range_list_added, true);
32154 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
32156 if (fde->ignored_debug)
32157 continue;
32158 if (!fde->in_std_section)
32159 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
32160 fde->dw_fde_end, &range_list_added,
32161 true);
32162 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
32163 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
32164 fde->dw_fde_second_end, &range_list_added,
32165 true);
32168 if (range_list_added)
32170 /* We need to give .debug_loc and .debug_ranges an appropriate
32171 "base address". Use zero so that these addresses become
32172 absolute. Historically, we've emitted the unexpected
32173 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
32174 Emit both to give time for other tools to adapt. */
32175 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
32176 if (! dwarf_strict && dwarf_version < 4)
32177 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
32179 add_ranges (NULL);
32180 have_multiple_function_sections = true;
32184 /* AIX Assembler inserts the length, so adjust the reference to match the
32185 offset expected by debuggers. */
32186 strcpy (dl_section_ref, debug_line_section_label);
32187 if (XCOFF_DEBUGGING_INFO)
32188 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32190 if (debug_info_level >= DINFO_LEVEL_TERSE)
32191 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
32192 dl_section_ref);
32194 if (have_macinfo)
32195 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32196 macinfo_section_label);
32198 if (dwarf_split_debug_info)
32200 if (have_location_lists)
32202 /* Since we generate the loclists in the split DWARF .dwo
32203 file itself, we don't need to generate a loclists_base
32204 attribute for the split compile unit DIE. That attribute
32205 (and using relocatable sec_offset FORMs) isn't allowed
32206 for a split compile unit. Only if the .debug_loclists
32207 section was in the main file, would we need to generate a
32208 loclists_base attribute here (for the full or skeleton
32209 unit DIE). */
32211 /* optimize_location_lists calculates the size of the lists,
32212 so index them first, and assign indices to the entries.
32213 Although optimize_location_lists will remove entries from
32214 the table, it only does so for duplicates, and therefore
32215 only reduces ref_counts to 1. */
32216 index_location_lists (comp_unit_die ());
32219 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
32220 index_rnglists ();
32222 if (addr_index_table != NULL)
32224 unsigned int index = 0;
32225 addr_index_table
32226 ->traverse_noresize<unsigned int *, index_addr_table_entry>
32227 (&index);
32231 loc_list_idx = 0;
32232 if (have_location_lists)
32234 optimize_location_lists (comp_unit_die ());
32235 /* And finally assign indexes to the entries for -gsplit-dwarf. */
32236 if (dwarf_version >= 5 && dwarf_split_debug_info)
32237 assign_location_list_indexes (comp_unit_die ());
32240 save_macinfo_strings ();
32242 if (dwarf_split_debug_info)
32244 unsigned int index = 0;
32246 /* Add attributes common to skeleton compile_units and
32247 type_units. Because these attributes include strings, it
32248 must be done before freezing the string table. Top-level
32249 skeleton die attrs are added when the skeleton type unit is
32250 created, so ensure it is created by this point. */
32251 add_top_level_skeleton_die_attrs (main_comp_unit_die);
32252 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32255 /* Output all of the compilation units. We put the main one last so that
32256 the offsets are available to output_pubnames. */
32257 for (node = cu_die_list; node; node = node->next)
32258 output_comp_unit (node->die, 0, NULL);
32260 hash_table<comdat_type_hasher> comdat_type_table (100);
32261 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32263 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32265 /* Don't output duplicate types. */
32266 if (*slot != HTAB_EMPTY_ENTRY)
32267 continue;
32269 /* Add a pointer to the line table for the main compilation unit
32270 so that the debugger can make sense of DW_AT_decl_file
32271 attributes. */
32272 if (debug_info_level >= DINFO_LEVEL_TERSE)
32273 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32274 (!dwarf_split_debug_info
32275 ? dl_section_ref
32276 : debug_skeleton_line_section_label));
32278 output_comdat_type_unit (ctnode, false);
32279 *slot = ctnode;
32282 if (dwarf_split_debug_info)
32284 int mark;
32285 struct md5_ctx ctx;
32287 /* Compute a checksum of the comp_unit to use as the dwo_id. */
32288 md5_init_ctx (&ctx);
32289 mark = 0;
32290 die_checksum (comp_unit_die (), &ctx, &mark);
32291 unmark_all_dies (comp_unit_die ());
32292 md5_finish_ctx (&ctx, checksum);
32294 if (dwarf_version < 5)
32296 /* Use the first 8 bytes of the checksum as the dwo_id,
32297 and add it to both comp-unit DIEs. */
32298 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
32299 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
32302 /* Add the base offset of the ranges table to the skeleton
32303 comp-unit DIE. */
32304 if (!vec_safe_is_empty (ranges_table))
32306 if (dwarf_version < 5)
32307 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
32308 ranges_section_label);
32311 output_addr_table ();
32314 /* Output the main compilation unit if non-empty or if .debug_macinfo
32315 or .debug_macro will be emitted. */
32316 output_comp_unit (comp_unit_die (), have_macinfo,
32317 dwarf_split_debug_info ? checksum : NULL);
32319 if (dwarf_split_debug_info && info_section_emitted)
32320 output_skeleton_debug_sections (main_comp_unit_die, checksum);
32322 /* Output the abbreviation table. */
32323 if (vec_safe_length (abbrev_die_table) != 1)
32325 switch_to_section (debug_abbrev_section);
32326 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32327 output_abbrev_section ();
32330 /* Output location list section if necessary. */
32331 if (have_location_lists)
32333 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
32334 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
32335 /* Output the location lists info. */
32336 switch_to_section (debug_loc_section);
32337 if (dwarf_version >= 5)
32339 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
32340 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
32341 if (DWARF_INITIAL_LENGTH_SIZE - dwarf_offset_size == 4)
32342 dw2_asm_output_data (4, 0xffffffff,
32343 "Initial length escape value indicating "
32344 "64-bit DWARF extension");
32345 dw2_asm_output_delta (dwarf_offset_size, l2, l1,
32346 "Length of Location Lists");
32347 ASM_OUTPUT_LABEL (asm_out_file, l1);
32348 output_dwarf_version ();
32349 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
32350 dw2_asm_output_data (1, 0, "Segment Size");
32351 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
32352 "Offset Entry Count");
32354 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
32355 if (dwarf_version >= 5 && dwarf_split_debug_info)
32357 unsigned int save_loc_list_idx = loc_list_idx;
32358 loc_list_idx = 0;
32359 output_loclists_offsets (comp_unit_die ());
32360 gcc_assert (save_loc_list_idx == loc_list_idx);
32362 output_location_lists (comp_unit_die ());
32363 if (dwarf_version >= 5)
32364 ASM_OUTPUT_LABEL (asm_out_file, l2);
32367 output_pubtables ();
32369 /* Output the address range information if a CU (.debug_info section)
32370 was emitted. We output an empty table even if we had no functions
32371 to put in it. This because the consumer has no way to tell the
32372 difference between an empty table that we omitted and failure to
32373 generate a table that would have contained data. */
32374 if (info_section_emitted)
32376 switch_to_section (debug_aranges_section);
32377 output_aranges ();
32380 /* Output ranges section if necessary. */
32381 if (!vec_safe_is_empty (ranges_table))
32383 if (dwarf_version >= 5)
32385 if (dwarf_split_debug_info)
32387 /* We don't know right now whether there are any
32388 ranges for .debug_rnglists and any for .debug_rnglists.dwo.
32389 Depending on into which of those two belongs the first
32390 ranges_table entry, emit that section first and that
32391 output_rnglists call will return true if the other kind of
32392 ranges needs to be emitted as well. */
32393 bool dwo = (*ranges_table)[0].idx != DW_RANGES_IDX_SKELETON;
32394 if (output_rnglists (generation, dwo))
32395 output_rnglists (generation, !dwo);
32397 else
32398 output_rnglists (generation, false);
32400 else
32401 output_ranges ();
32404 /* Have to end the macro section. */
32405 if (have_macinfo)
32407 switch_to_section (debug_macinfo_section);
32408 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32409 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
32410 : debug_skeleton_line_section_label, false);
32411 dw2_asm_output_data (1, 0, "End compilation unit");
32414 /* Output the source line correspondence table. We must do this
32415 even if there is no line information. Otherwise, on an empty
32416 translation unit, we will generate a present, but empty,
32417 .debug_info section. IRIX 6.5 `nm' will then complain when
32418 examining the file. This is done late so that any filenames
32419 used by the debug_info section are marked as 'used'. */
32420 switch_to_section (debug_line_section);
32421 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32422 if (! output_asm_line_debug_info ())
32423 output_line_info (false);
32425 if (dwarf_split_debug_info && info_section_emitted)
32427 switch_to_section (debug_skeleton_line_section);
32428 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
32429 output_line_info (true);
32432 /* If we emitted any indirect strings, output the string table too. */
32433 if (debug_str_hash || skeleton_debug_str_hash)
32434 output_indirect_strings ();
32435 if (debug_line_str_hash)
32437 switch_to_section (debug_line_str_section);
32438 const enum dwarf_form form = DW_FORM_line_strp;
32439 debug_line_str_hash->traverse<enum dwarf_form,
32440 output_indirect_string> (form);
32443 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
32444 symview_upper_bound = 0;
32445 if (zero_view_p)
32446 bitmap_clear (zero_view_p);
32449 /* Returns a hash value for X (which really is a variable_value_struct). */
32451 inline hashval_t
32452 variable_value_hasher::hash (variable_value_struct *x)
32454 return (hashval_t) x->decl_id;
32457 /* Return nonzero if decl_id of variable_value_struct X is the same as
32458 UID of decl Y. */
32460 inline bool
32461 variable_value_hasher::equal (variable_value_struct *x, tree y)
32463 return x->decl_id == DECL_UID (y);
32466 /* Helper function for resolve_variable_value, handle
32467 DW_OP_GNU_variable_value in one location expression.
32468 Return true if exprloc has been changed into loclist. */
32470 static bool
32471 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
32473 dw_loc_descr_ref next;
32474 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
32476 next = loc->dw_loc_next;
32477 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
32478 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
32479 continue;
32481 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32482 if (DECL_CONTEXT (decl) != current_function_decl)
32483 continue;
32485 dw_die_ref ref = lookup_decl_die (decl);
32486 if (ref)
32488 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32489 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32490 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32491 continue;
32493 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
32494 if (l == NULL)
32495 continue;
32496 if (l->dw_loc_next)
32498 if (AT_class (a) != dw_val_class_loc)
32499 continue;
32500 switch (a->dw_attr)
32502 /* Following attributes allow both exprloc and loclist
32503 classes, so we can change them into a loclist. */
32504 case DW_AT_location:
32505 case DW_AT_string_length:
32506 case DW_AT_return_addr:
32507 case DW_AT_data_member_location:
32508 case DW_AT_frame_base:
32509 case DW_AT_segment:
32510 case DW_AT_static_link:
32511 case DW_AT_use_location:
32512 case DW_AT_vtable_elem_location:
32513 if (prev)
32515 prev->dw_loc_next = NULL;
32516 prepend_loc_descr_to_each (l, AT_loc (a));
32518 if (next)
32519 add_loc_descr_to_each (l, next);
32520 a->dw_attr_val.val_class = dw_val_class_loc_list;
32521 a->dw_attr_val.val_entry = NULL;
32522 a->dw_attr_val.v.val_loc_list = l;
32523 have_location_lists = true;
32524 return true;
32525 /* Following attributes allow both exprloc and reference,
32526 so if the whole expression is DW_OP_GNU_variable_value alone
32527 we could transform it into reference. */
32528 case DW_AT_byte_size:
32529 case DW_AT_bit_size:
32530 case DW_AT_lower_bound:
32531 case DW_AT_upper_bound:
32532 case DW_AT_bit_stride:
32533 case DW_AT_count:
32534 case DW_AT_allocated:
32535 case DW_AT_associated:
32536 case DW_AT_byte_stride:
32537 if (prev == NULL && next == NULL)
32538 break;
32539 /* FALLTHRU */
32540 default:
32541 if (dwarf_strict)
32542 continue;
32543 break;
32545 /* Create DW_TAG_variable that we can refer to. */
32546 gen_decl_die (decl, NULL_TREE, NULL,
32547 lookup_decl_die (current_function_decl));
32548 ref = lookup_decl_die (decl);
32549 if (ref)
32551 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32552 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32553 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32555 continue;
32557 if (prev)
32559 prev->dw_loc_next = l->expr;
32560 add_loc_descr (&prev->dw_loc_next, next);
32561 free_loc_descr (loc, NULL);
32562 next = prev->dw_loc_next;
32564 else
32566 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
32567 add_loc_descr (&loc, next);
32568 next = loc;
32570 loc = prev;
32572 return false;
32575 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
32577 static void
32578 resolve_variable_value (dw_die_ref die)
32580 dw_attr_node *a;
32581 dw_loc_list_ref loc;
32582 unsigned ix;
32584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32585 switch (AT_class (a))
32587 case dw_val_class_loc:
32588 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
32589 break;
32590 /* FALLTHRU */
32591 case dw_val_class_loc_list:
32592 loc = AT_loc_list (a);
32593 gcc_assert (loc);
32594 for (; loc; loc = loc->dw_loc_next)
32595 resolve_variable_value_in_expr (a, loc->expr);
32596 break;
32597 default:
32598 break;
32602 /* Attempt to optimize DW_OP_GNU_variable_value refering to
32603 temporaries in the current function. */
32605 static void
32606 resolve_variable_values (void)
32608 if (!variable_value_hash || !current_function_decl)
32609 return;
32611 struct variable_value_struct *node
32612 = variable_value_hash->find_with_hash (current_function_decl,
32613 DECL_UID (current_function_decl));
32615 if (node == NULL)
32616 return;
32618 unsigned int i;
32619 dw_die_ref die;
32620 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
32621 resolve_variable_value (die);
32624 /* Helper function for note_variable_value, handle one location
32625 expression. */
32627 static void
32628 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
32630 for (; loc; loc = loc->dw_loc_next)
32631 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
32632 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
32634 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
32635 dw_die_ref ref = lookup_decl_die (decl);
32636 if (! ref && (flag_generate_lto || flag_generate_offload))
32638 /* ??? This is somewhat a hack because we do not create DIEs
32639 for variables not in BLOCK trees early but when generating
32640 early LTO output we need the dw_val_class_decl_ref to be
32641 fully resolved. For fat LTO objects we'd also like to
32642 undo this after LTO dwarf output. */
32643 gcc_assert (DECL_CONTEXT (decl));
32644 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
32645 gcc_assert (ctx != NULL);
32646 gen_decl_die (decl, NULL_TREE, NULL, ctx);
32647 ref = lookup_decl_die (decl);
32648 gcc_assert (ref != NULL);
32650 if (ref)
32652 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
32653 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
32654 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
32655 continue;
32657 if (VAR_P (decl)
32658 && DECL_CONTEXT (decl)
32659 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
32660 && lookup_decl_die (DECL_CONTEXT (decl)))
32662 if (!variable_value_hash)
32663 variable_value_hash
32664 = hash_table<variable_value_hasher>::create_ggc (10);
32666 tree fndecl = DECL_CONTEXT (decl);
32667 struct variable_value_struct *node;
32668 struct variable_value_struct **slot
32669 = variable_value_hash->find_slot_with_hash (fndecl,
32670 DECL_UID (fndecl),
32671 INSERT);
32672 if (*slot == NULL)
32674 node = ggc_cleared_alloc<variable_value_struct> ();
32675 node->decl_id = DECL_UID (fndecl);
32676 *slot = node;
32678 else
32679 node = *slot;
32681 vec_safe_push (node->dies, die);
32686 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
32687 with dw_val_class_decl_ref operand. */
32689 static void
32690 note_variable_value (dw_die_ref die)
32692 dw_die_ref c;
32693 dw_attr_node *a;
32694 dw_loc_list_ref loc;
32695 unsigned ix;
32697 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
32698 switch (AT_class (a))
32700 case dw_val_class_loc_list:
32701 loc = AT_loc_list (a);
32702 gcc_assert (loc);
32703 if (!loc->noted_variable_value)
32705 loc->noted_variable_value = 1;
32706 for (; loc; loc = loc->dw_loc_next)
32707 note_variable_value_in_expr (die, loc->expr);
32709 break;
32710 case dw_val_class_loc:
32711 note_variable_value_in_expr (die, AT_loc (a));
32712 break;
32713 default:
32714 break;
32717 /* Mark children. */
32718 FOR_EACH_CHILD (die, c, note_variable_value (c));
32721 /* Process DWARF dies for CTF generation. */
32723 static void
32724 ctf_debug_do_cu (dw_die_ref die)
32726 dw_die_ref c;
32728 if (!ctf_do_die (die))
32729 return;
32731 FOR_EACH_CHILD (die, c, ctf_do_die (c));
32734 /* Perform any cleanups needed after the early debug generation pass
32735 has run. */
32737 static void
32738 dwarf2out_early_finish (const char *filename)
32740 comdat_type_node *ctnode;
32741 set_early_dwarf s;
32742 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
32744 /* PCH might result in DW_AT_producer string being restored from the
32745 header compilation, so always fill it with empty string initially
32746 and overwrite only here. */
32747 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
32749 if (dwarf_record_gcc_switches)
32750 producer_string = gen_producer_string (lang_hooks.name,
32751 save_decoded_options,
32752 save_decoded_options_count);
32753 else
32754 producer_string = concat (lang_hooks.name, " ", version_string, NULL);
32756 producer->dw_attr_val.v.val_str->refcount--;
32757 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
32759 /* Add the name for the main input file now. We delayed this from
32760 dwarf2out_init to avoid complications with PCH. */
32761 add_filename_attribute (comp_unit_die (), remap_debug_filename (filename));
32762 add_comp_dir_attribute (comp_unit_die ());
32764 /* With LTO early dwarf was really finished at compile-time, so make
32765 sure to adjust the phase after annotating the LTRANS CU DIE. */
32766 if (in_lto_p)
32768 early_dwarf_finished = true;
32769 if (dump_file)
32771 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
32772 print_die (comp_unit_die (), dump_file);
32774 return;
32777 /* Walk through the list of incomplete types again, trying once more to
32778 emit full debugging info for them. */
32779 retry_incomplete_types ();
32781 gen_scheduled_generic_parms_dies ();
32782 gen_remaining_tmpl_value_param_die_attribute ();
32784 /* The point here is to flush out the limbo list so that it is empty
32785 and we don't need to stream it for LTO. */
32786 flush_limbo_die_list ();
32788 /* Add DW_AT_linkage_name for all deferred DIEs. */
32789 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
32791 tree decl = node->created_for;
32792 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
32793 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
32794 ended up in deferred_asm_name before we knew it was
32795 constant and never written to disk. */
32796 && DECL_ASSEMBLER_NAME (decl))
32798 add_linkage_attr (node->die, decl);
32799 move_linkage_attr (node->die);
32802 deferred_asm_name = NULL;
32804 if (flag_eliminate_unused_debug_types)
32805 prune_unused_types ();
32807 /* Generate separate COMDAT sections for type DIEs. */
32808 if (use_debug_types)
32810 break_out_comdat_types (comp_unit_die ());
32812 /* Each new type_unit DIE was added to the limbo die list when created.
32813 Since these have all been added to comdat_type_list, clear the
32814 limbo die list. */
32815 limbo_die_list = NULL;
32817 /* For each new comdat type unit, copy declarations for incomplete
32818 types to make the new unit self-contained (i.e., no direct
32819 references to the main compile unit). */
32820 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32821 copy_decls_for_unworthy_types (ctnode->root_die);
32822 copy_decls_for_unworthy_types (comp_unit_die ());
32824 /* In the process of copying declarations from one unit to another,
32825 we may have left some declarations behind that are no longer
32826 referenced. Prune them. */
32827 prune_unused_types ();
32830 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
32831 with dw_val_class_decl_ref operand. */
32832 note_variable_value (comp_unit_die ());
32833 for (limbo_die_node *node = cu_die_list; node; node = node->next)
32834 note_variable_value (node->die);
32835 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32836 note_variable_value (ctnode->root_die);
32837 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32838 note_variable_value (node->die);
32840 /* The AT_pubnames attribute needs to go in all skeleton dies, including
32841 both the main_cu and all skeleton TUs. Making this call unconditional
32842 would end up either adding a second copy of the AT_pubnames attribute, or
32843 requiring a special case in add_top_level_skeleton_die_attrs. */
32844 if (!dwarf_split_debug_info)
32845 add_AT_pubnames (comp_unit_die ());
32847 /* The early debug phase is now finished. */
32848 early_dwarf_finished = true;
32849 if (dump_file)
32851 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
32852 print_die (comp_unit_die (), dump_file);
32855 /* Generate CTF/BTF debug info. */
32856 if ((ctf_debug_info_level > CTFINFO_LEVEL_NONE
32857 || btf_debuginfo_p ()) && lang_GNU_C ())
32859 ctf_debug_init ();
32860 ctf_debug_do_cu (comp_unit_die ());
32861 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32862 ctf_debug_do_cu (node->die);
32863 /* Post process the debug data in the CTF container if necessary. */
32864 ctf_debug_init_postprocess (btf_debuginfo_p ());
32866 ctf_debug_early_finish (filename);
32869 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
32870 if ((!flag_generate_lto && !flag_generate_offload)
32871 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
32872 copy_lto_debug_sections operation of the simple object support in
32873 libiberty is not implemented for them yet. */
32874 || TARGET_PECOFF || TARGET_COFF)
32875 return;
32877 /* Now as we are going to output for LTO initialize sections and labels
32878 to the LTO variants. We don't need a random-seed postfix as other
32879 LTO sections as linking the LTO debug sections into one in a partial
32880 link is fine. */
32881 init_sections_and_labels (true);
32883 /* The output below is modeled after dwarf2out_finish with all
32884 location related output removed and some LTO specific changes.
32885 Some refactoring might make both smaller and easier to match up. */
32887 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32888 mark_base_types (ctnode->root_die);
32889 mark_base_types (comp_unit_die ());
32890 move_marked_base_types ();
32892 /* Traverse the DIE's and add sibling attributes to those DIE's
32893 that have children. */
32894 add_sibling_attributes (comp_unit_die ());
32895 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32896 add_sibling_attributes (node->die);
32897 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32898 add_sibling_attributes (ctnode->root_die);
32900 /* AIX Assembler inserts the length, so adjust the reference to match the
32901 offset expected by debuggers. */
32902 strcpy (dl_section_ref, debug_line_section_label);
32903 if (XCOFF_DEBUGGING_INFO)
32904 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32906 if (debug_info_level >= DINFO_LEVEL_TERSE)
32907 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32909 if (have_macinfo)
32910 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32911 macinfo_section_label);
32913 save_macinfo_strings ();
32915 if (dwarf_split_debug_info)
32917 unsigned int index = 0;
32918 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32921 /* Output all of the compilation units. We put the main one last so that
32922 the offsets are available to output_pubnames. */
32923 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32924 output_comp_unit (node->die, 0, NULL);
32926 hash_table<comdat_type_hasher> comdat_type_table (100);
32927 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
32929 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32931 /* Don't output duplicate types. */
32932 if (*slot != HTAB_EMPTY_ENTRY)
32933 continue;
32935 /* Add a pointer to the line table for the main compilation unit
32936 so that the debugger can make sense of DW_AT_decl_file
32937 attributes. */
32938 if (debug_info_level >= DINFO_LEVEL_TERSE)
32939 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32940 (!dwarf_split_debug_info
32941 ? debug_line_section_label
32942 : debug_skeleton_line_section_label));
32944 output_comdat_type_unit (ctnode, true);
32945 *slot = ctnode;
32948 /* Stick a unique symbol to the main debuginfo section. */
32949 compute_comp_unit_symbol (comp_unit_die ());
32951 /* Output the main compilation unit. We always need it if only for
32952 the CU symbol. */
32953 output_comp_unit (comp_unit_die (), true, NULL);
32955 /* Output the abbreviation table. */
32956 if (vec_safe_length (abbrev_die_table) != 1)
32958 switch_to_section (debug_abbrev_section);
32959 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32960 output_abbrev_section ();
32963 /* Have to end the macro section. */
32964 if (have_macinfo)
32966 /* We have to save macinfo state if we need to output it again
32967 for the FAT part of the object. */
32968 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32969 if (flag_fat_lto_objects)
32970 macinfo_table = macinfo_table->copy ();
32972 switch_to_section (debug_macinfo_section);
32973 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32974 output_macinfo (debug_line_section_label, true);
32975 dw2_asm_output_data (1, 0, "End compilation unit");
32977 if (flag_fat_lto_objects)
32979 vec_free (macinfo_table);
32980 macinfo_table = saved_macinfo_table;
32984 /* Emit a skeleton debug_line section. */
32985 switch_to_section (debug_line_section);
32986 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32987 output_line_info (true);
32989 /* If we emitted any indirect strings, output the string table too. */
32990 if (debug_str_hash || skeleton_debug_str_hash)
32991 output_indirect_strings ();
32992 if (debug_line_str_hash)
32994 switch_to_section (debug_line_str_section);
32995 const enum dwarf_form form = DW_FORM_line_strp;
32996 debug_line_str_hash->traverse<enum dwarf_form,
32997 output_indirect_string> (form);
33000 /* Switch back to the text section. */
33001 switch_to_section (text_section);
33004 /* Reset all state within dwarf2out.c so that we can rerun the compiler
33005 within the same process. For use by toplev::finalize. */
33007 void
33008 dwarf2out_c_finalize (void)
33010 last_var_location_insn = NULL;
33011 cached_next_real_insn = NULL;
33012 used_rtx_array = NULL;
33013 incomplete_types = NULL;
33014 debug_info_section = NULL;
33015 debug_skeleton_info_section = NULL;
33016 debug_abbrev_section = NULL;
33017 debug_skeleton_abbrev_section = NULL;
33018 debug_aranges_section = NULL;
33019 debug_addr_section = NULL;
33020 debug_macinfo_section = NULL;
33021 debug_line_section = NULL;
33022 debug_skeleton_line_section = NULL;
33023 debug_loc_section = NULL;
33024 debug_pubnames_section = NULL;
33025 debug_pubtypes_section = NULL;
33026 debug_str_section = NULL;
33027 debug_line_str_section = NULL;
33028 debug_str_dwo_section = NULL;
33029 debug_str_offsets_section = NULL;
33030 debug_ranges_section = NULL;
33031 debug_ranges_dwo_section = NULL;
33032 debug_frame_section = NULL;
33033 fde_vec = NULL;
33034 debug_str_hash = NULL;
33035 debug_line_str_hash = NULL;
33036 skeleton_debug_str_hash = NULL;
33037 dw2_string_counter = 0;
33038 have_multiple_function_sections = false;
33039 in_text_section_p = false;
33040 cold_text_section = NULL;
33041 last_text_label = NULL;
33042 last_cold_label = NULL;
33043 switch_text_ranges = NULL;
33044 switch_cold_ranges = NULL;
33045 current_unit_personality = NULL;
33047 early_dwarf = false;
33048 early_dwarf_finished = false;
33050 next_die_offset = 0;
33051 single_comp_unit_die = NULL;
33052 comdat_type_list = NULL;
33053 limbo_die_list = NULL;
33054 file_table = NULL;
33055 decl_die_table = NULL;
33056 common_block_die_table = NULL;
33057 decl_loc_table = NULL;
33058 call_arg_locations = NULL;
33059 call_arg_loc_last = NULL;
33060 call_site_count = -1;
33061 tail_call_site_count = -1;
33062 cached_dw_loc_list_table = NULL;
33063 abbrev_die_table = NULL;
33064 delete dwarf_proc_stack_usage_map;
33065 dwarf_proc_stack_usage_map = NULL;
33066 line_info_label_num = 0;
33067 cur_line_info_table = NULL;
33068 text_section_line_info = NULL;
33069 cold_text_section_line_info = NULL;
33070 separate_line_info = NULL;
33071 info_section_emitted = false;
33072 pubname_table = NULL;
33073 pubtype_table = NULL;
33074 macinfo_table = NULL;
33075 ranges_table = NULL;
33076 ranges_by_label = NULL;
33077 rnglist_idx = 0;
33078 have_location_lists = false;
33079 loclabel_num = 0;
33080 poc_label_num = 0;
33081 last_emitted_file = NULL;
33082 label_num = 0;
33083 tmpl_value_parm_die_table = NULL;
33084 generic_type_instances = NULL;
33085 frame_pointer_fb_offset = 0;
33086 frame_pointer_fb_offset_valid = false;
33087 base_types.release ();
33088 XDELETEVEC (producer_string);
33089 producer_string = NULL;
33090 output_line_info_generation = 0;
33091 init_sections_and_labels_generation = 0;
33094 #include "gt-dwarf2out.h"